b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright 2007, Frank A Kingswood <frank@kingswood-consulting.co.uk> |
| 4 | * Copyright 2007, Werner Cornelius <werner@cornelius-consult.de> |
| 5 | * Copyright 2009, Boris Hajduk <boris@hajduk.org> |
| 6 | * |
| 7 | * ch341.c implements a serial port driver for the Winchiphead CH341. |
| 8 | * |
| 9 | * The CH341 device can be used to implement an RS232 asynchronous |
| 10 | * serial port, an IEEE-1284 parallel printer port or a memory-like |
| 11 | * interface. In all cases the CH341 supports an I2C interface as well. |
| 12 | * This driver only supports the asynchronous serial interface. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/tty.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/usb.h> |
| 20 | #include <linux/usb/serial.h> |
| 21 | #include <linux/serial.h> |
| 22 | #include <asm/unaligned.h> |
| 23 | |
| 24 | #define DEFAULT_BAUD_RATE 9600 |
| 25 | #define DEFAULT_TIMEOUT 1000 |
| 26 | |
| 27 | /* flags for IO-Bits */ |
| 28 | #define CH341_BIT_RTS (1 << 6) |
| 29 | #define CH341_BIT_DTR (1 << 5) |
| 30 | |
| 31 | /******************************/ |
| 32 | /* interrupt pipe definitions */ |
| 33 | /******************************/ |
| 34 | /* always 4 interrupt bytes */ |
| 35 | /* first irq byte normally 0x08 */ |
| 36 | /* second irq byte base 0x7d + below */ |
| 37 | /* third irq byte base 0x94 + below */ |
| 38 | /* fourth irq byte normally 0xee */ |
| 39 | |
| 40 | /* second interrupt byte */ |
| 41 | #define CH341_MULT_STAT 0x04 /* multiple status since last interrupt event */ |
| 42 | |
| 43 | /* status returned in third interrupt answer byte, inverted in data |
| 44 | from irq */ |
| 45 | #define CH341_BIT_CTS 0x01 |
| 46 | #define CH341_BIT_DSR 0x02 |
| 47 | #define CH341_BIT_RI 0x04 |
| 48 | #define CH341_BIT_DCD 0x08 |
| 49 | #define CH341_BITS_MODEM_STAT 0x0f /* all bits */ |
| 50 | |
| 51 | /*******************************/ |
| 52 | /* baudrate calculation factor */ |
| 53 | /*******************************/ |
| 54 | #define CH341_BAUDBASE_FACTOR 1532620800 |
| 55 | #define CH341_BAUDBASE_DIVMAX 3 |
| 56 | |
| 57 | /* Break support - the information used to implement this was gleaned from |
| 58 | * the Net/FreeBSD uchcom.c driver by Takanori Watanabe. Domo arigato. |
| 59 | */ |
| 60 | |
| 61 | #define CH341_REQ_READ_VERSION 0x5F |
| 62 | #define CH341_REQ_WRITE_REG 0x9A |
| 63 | #define CH341_REQ_READ_REG 0x95 |
| 64 | #define CH341_REQ_SERIAL_INIT 0xA1 |
| 65 | #define CH341_REQ_MODEM_CTRL 0xA4 |
| 66 | |
| 67 | #define CH341_REG_BREAK 0x05 |
| 68 | #define CH341_REG_LCR 0x18 |
| 69 | #define CH341_NBREAK_BITS 0x01 |
| 70 | |
| 71 | #define CH341_LCR_ENABLE_RX 0x80 |
| 72 | #define CH341_LCR_ENABLE_TX 0x40 |
| 73 | #define CH341_LCR_MARK_SPACE 0x20 |
| 74 | #define CH341_LCR_PAR_EVEN 0x10 |
| 75 | #define CH341_LCR_ENABLE_PAR 0x08 |
| 76 | #define CH341_LCR_STOP_BITS_2 0x04 |
| 77 | #define CH341_LCR_CS8 0x03 |
| 78 | #define CH341_LCR_CS7 0x02 |
| 79 | #define CH341_LCR_CS6 0x01 |
| 80 | #define CH341_LCR_CS5 0x00 |
| 81 | |
| 82 | static const struct usb_device_id id_table[] = { |
| 83 | { USB_DEVICE(0x1a86, 0x5523) }, |
| 84 | { USB_DEVICE(0x1a86, 0x7522) }, |
| 85 | { USB_DEVICE(0x1a86, 0x7523) }, |
| 86 | { USB_DEVICE(0x2184, 0x0057) }, |
| 87 | { USB_DEVICE(0x4348, 0x5523) }, |
| 88 | { USB_DEVICE(0x9986, 0x7523) }, |
| 89 | { }, |
| 90 | }; |
| 91 | MODULE_DEVICE_TABLE(usb, id_table); |
| 92 | |
| 93 | struct ch341_private { |
| 94 | spinlock_t lock; /* access lock */ |
| 95 | unsigned baud_rate; /* set baud rate */ |
| 96 | u8 mcr; |
| 97 | u8 msr; |
| 98 | u8 lcr; |
| 99 | |
| 100 | unsigned long quirks; |
| 101 | u8 version; |
| 102 | }; |
| 103 | |
| 104 | static void ch341_set_termios(struct tty_struct *tty, |
| 105 | struct usb_serial_port *port, |
| 106 | struct ktermios *old_termios); |
| 107 | |
| 108 | static int ch341_control_out(struct usb_device *dev, u8 request, |
| 109 | u16 value, u16 index) |
| 110 | { |
| 111 | int r; |
| 112 | |
| 113 | dev_dbg(&dev->dev, "%s - (%02x,%04x,%04x)\n", __func__, |
| 114 | request, value, index); |
| 115 | |
| 116 | r = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request, |
| 117 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, |
| 118 | value, index, NULL, 0, DEFAULT_TIMEOUT); |
| 119 | if (r < 0) |
| 120 | dev_err(&dev->dev, "failed to send control message: %d\n", r); |
| 121 | |
| 122 | return r; |
| 123 | } |
| 124 | |
| 125 | static int ch341_control_in(struct usb_device *dev, |
| 126 | u8 request, u16 value, u16 index, |
| 127 | char *buf, unsigned bufsize) |
| 128 | { |
| 129 | int r; |
| 130 | |
| 131 | dev_dbg(&dev->dev, "%s - (%02x,%04x,%04x,%u)\n", __func__, |
| 132 | request, value, index, bufsize); |
| 133 | |
| 134 | r = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request, |
| 135 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
| 136 | value, index, buf, bufsize, DEFAULT_TIMEOUT); |
| 137 | if (r < (int)bufsize) { |
| 138 | if (r >= 0) { |
| 139 | dev_err(&dev->dev, |
| 140 | "short control message received (%d < %u)\n", |
| 141 | r, bufsize); |
| 142 | r = -EIO; |
| 143 | } |
| 144 | |
| 145 | dev_err(&dev->dev, "failed to receive control message: %d\n", |
| 146 | r); |
| 147 | return r; |
| 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static int ch341_set_baudrate_lcr(struct usb_device *dev, |
| 154 | struct ch341_private *priv, u8 lcr) |
| 155 | { |
| 156 | short a; |
| 157 | int r; |
| 158 | unsigned long factor; |
| 159 | short divisor; |
| 160 | |
| 161 | if (!priv->baud_rate) |
| 162 | return -EINVAL; |
| 163 | factor = (CH341_BAUDBASE_FACTOR / priv->baud_rate); |
| 164 | divisor = CH341_BAUDBASE_DIVMAX; |
| 165 | |
| 166 | while ((factor > 0xfff0) && divisor) { |
| 167 | factor >>= 3; |
| 168 | divisor--; |
| 169 | } |
| 170 | |
| 171 | if (factor > 0xfff0) |
| 172 | return -EINVAL; |
| 173 | |
| 174 | factor = 0x10000 - factor; |
| 175 | a = (factor & 0xff00) | divisor; |
| 176 | |
| 177 | /* |
| 178 | * CH341A buffers data until a full endpoint-size packet (32 bytes) |
| 179 | * has been received unless bit 7 is set. |
| 180 | * |
| 181 | * At least one device with version 0x27 appears to have this bit |
| 182 | * inverted. |
| 183 | */ |
| 184 | if (priv->version > 0x27) |
| 185 | a |= BIT(7); |
| 186 | |
| 187 | r = ch341_control_out(dev, CH341_REQ_WRITE_REG, 0x1312, a); |
| 188 | if (r) |
| 189 | return r; |
| 190 | |
| 191 | if (priv->version < 0x30) |
| 192 | return 0; |
| 193 | |
| 194 | r = ch341_control_out(dev, CH341_REQ_WRITE_REG, 0x2518, lcr); |
| 195 | if (r) |
| 196 | return r; |
| 197 | |
| 198 | return r; |
| 199 | } |
| 200 | |
| 201 | static int ch341_set_handshake(struct usb_device *dev, u8 control) |
| 202 | { |
| 203 | return ch341_control_out(dev, CH341_REQ_MODEM_CTRL, ~control, 0); |
| 204 | } |
| 205 | |
| 206 | static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv) |
| 207 | { |
| 208 | const unsigned int size = 2; |
| 209 | char *buffer; |
| 210 | int r; |
| 211 | unsigned long flags; |
| 212 | |
| 213 | buffer = kmalloc(size, GFP_KERNEL); |
| 214 | if (!buffer) |
| 215 | return -ENOMEM; |
| 216 | |
| 217 | r = ch341_control_in(dev, CH341_REQ_READ_REG, 0x0706, 0, buffer, size); |
| 218 | if (r < 0) |
| 219 | goto out; |
| 220 | |
| 221 | spin_lock_irqsave(&priv->lock, flags); |
| 222 | priv->msr = (~(*buffer)) & CH341_BITS_MODEM_STAT; |
| 223 | spin_unlock_irqrestore(&priv->lock, flags); |
| 224 | |
| 225 | out: kfree(buffer); |
| 226 | return r; |
| 227 | } |
| 228 | |
| 229 | /* -------------------------------------------------------------------------- */ |
| 230 | |
| 231 | static int ch341_configure(struct usb_device *dev, struct ch341_private *priv) |
| 232 | { |
| 233 | const unsigned int size = 2; |
| 234 | char *buffer; |
| 235 | int r; |
| 236 | |
| 237 | buffer = kmalloc(size, GFP_KERNEL); |
| 238 | if (!buffer) |
| 239 | return -ENOMEM; |
| 240 | |
| 241 | /* expect two bytes 0x27 0x00 */ |
| 242 | r = ch341_control_in(dev, CH341_REQ_READ_VERSION, 0, 0, buffer, size); |
| 243 | if (r < 0) |
| 244 | goto out; |
| 245 | |
| 246 | priv->version = buffer[0]; |
| 247 | dev_dbg(&dev->dev, "Chip version: 0x%02x\n", priv->version); |
| 248 | |
| 249 | r = ch341_control_out(dev, CH341_REQ_SERIAL_INIT, 0, 0); |
| 250 | if (r < 0) |
| 251 | goto out; |
| 252 | |
| 253 | r = ch341_set_baudrate_lcr(dev, priv, priv->lcr); |
| 254 | if (r < 0) |
| 255 | goto out; |
| 256 | |
| 257 | r = ch341_set_handshake(dev, priv->mcr); |
| 258 | |
| 259 | out: kfree(buffer); |
| 260 | return r; |
| 261 | } |
| 262 | |
| 263 | static int ch341_detect_quirks(struct usb_serial_port *port) |
| 264 | { |
| 265 | struct ch341_private *priv = usb_get_serial_port_data(port); |
| 266 | struct usb_device *udev = port->serial->dev; |
| 267 | const unsigned int size = 2; |
| 268 | unsigned long quirks = 0; |
| 269 | char *buffer; |
| 270 | int r; |
| 271 | |
| 272 | buffer = kmalloc(size, GFP_KERNEL); |
| 273 | if (!buffer) |
| 274 | return -ENOMEM; |
| 275 | |
| 276 | /* |
| 277 | * A subset of CH34x devices does not support all features. The |
| 278 | * prescaler is limited and there is no support for sending a RS232 |
| 279 | * break condition. A read failure when trying to set up the latter is |
| 280 | * used to detect these devices. |
| 281 | */ |
| 282 | r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), CH341_REQ_READ_REG, |
| 283 | USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, |
| 284 | CH341_REG_BREAK, 0, buffer, size, DEFAULT_TIMEOUT); |
| 285 | if (r == -EPIPE) { |
| 286 | dev_dbg(&port->dev, "break control not supported\n"); |
| 287 | r = 0; |
| 288 | goto out; |
| 289 | } |
| 290 | |
| 291 | if (r != size) { |
| 292 | if (r >= 0) |
| 293 | r = -EIO; |
| 294 | dev_err(&port->dev, "failed to read break control: %d\n", r); |
| 295 | goto out; |
| 296 | } |
| 297 | |
| 298 | r = 0; |
| 299 | out: |
| 300 | kfree(buffer); |
| 301 | |
| 302 | if (quirks) { |
| 303 | dev_dbg(&port->dev, "enabling quirk flags: 0x%02lx\n", quirks); |
| 304 | priv->quirks |= quirks; |
| 305 | } |
| 306 | |
| 307 | return r; |
| 308 | } |
| 309 | |
| 310 | static int ch341_port_probe(struct usb_serial_port *port) |
| 311 | { |
| 312 | struct ch341_private *priv; |
| 313 | int r; |
| 314 | |
| 315 | priv = kzalloc(sizeof(struct ch341_private), GFP_KERNEL); |
| 316 | if (!priv) |
| 317 | return -ENOMEM; |
| 318 | |
| 319 | spin_lock_init(&priv->lock); |
| 320 | priv->baud_rate = DEFAULT_BAUD_RATE; |
| 321 | /* |
| 322 | * Some CH340 devices appear unable to change the initial LCR |
| 323 | * settings, so set a sane 8N1 default. |
| 324 | */ |
| 325 | priv->lcr = CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX | CH341_LCR_CS8; |
| 326 | |
| 327 | r = ch341_configure(port->serial->dev, priv); |
| 328 | if (r < 0) |
| 329 | goto error; |
| 330 | |
| 331 | usb_set_serial_port_data(port, priv); |
| 332 | |
| 333 | r = ch341_detect_quirks(port); |
| 334 | if (r < 0) |
| 335 | goto error; |
| 336 | |
| 337 | return 0; |
| 338 | |
| 339 | error: kfree(priv); |
| 340 | return r; |
| 341 | } |
| 342 | |
| 343 | static int ch341_port_remove(struct usb_serial_port *port) |
| 344 | { |
| 345 | struct ch341_private *priv; |
| 346 | |
| 347 | priv = usb_get_serial_port_data(port); |
| 348 | kfree(priv); |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | static int ch341_carrier_raised(struct usb_serial_port *port) |
| 354 | { |
| 355 | struct ch341_private *priv = usb_get_serial_port_data(port); |
| 356 | if (priv->msr & CH341_BIT_DCD) |
| 357 | return 1; |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | static void ch341_dtr_rts(struct usb_serial_port *port, int on) |
| 362 | { |
| 363 | struct ch341_private *priv = usb_get_serial_port_data(port); |
| 364 | unsigned long flags; |
| 365 | |
| 366 | /* drop DTR and RTS */ |
| 367 | spin_lock_irqsave(&priv->lock, flags); |
| 368 | if (on) |
| 369 | priv->mcr |= CH341_BIT_RTS | CH341_BIT_DTR; |
| 370 | else |
| 371 | priv->mcr &= ~(CH341_BIT_RTS | CH341_BIT_DTR); |
| 372 | spin_unlock_irqrestore(&priv->lock, flags); |
| 373 | ch341_set_handshake(port->serial->dev, priv->mcr); |
| 374 | } |
| 375 | |
| 376 | static void ch341_close(struct usb_serial_port *port) |
| 377 | { |
| 378 | usb_serial_generic_close(port); |
| 379 | usb_kill_urb(port->interrupt_in_urb); |
| 380 | } |
| 381 | |
| 382 | |
| 383 | /* open this device, set default parameters */ |
| 384 | static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port) |
| 385 | { |
| 386 | struct ch341_private *priv = usb_get_serial_port_data(port); |
| 387 | int r; |
| 388 | |
| 389 | if (tty) |
| 390 | ch341_set_termios(tty, port, NULL); |
| 391 | |
| 392 | dev_dbg(&port->dev, "%s - submitting interrupt urb\n", __func__); |
| 393 | r = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); |
| 394 | if (r) { |
| 395 | dev_err(&port->dev, "%s - failed to submit interrupt urb: %d\n", |
| 396 | __func__, r); |
| 397 | return r; |
| 398 | } |
| 399 | |
| 400 | r = ch341_get_status(port->serial->dev, priv); |
| 401 | if (r < 0) { |
| 402 | dev_err(&port->dev, "failed to read modem status: %d\n", r); |
| 403 | goto err_kill_interrupt_urb; |
| 404 | } |
| 405 | |
| 406 | r = usb_serial_generic_open(tty, port); |
| 407 | if (r) |
| 408 | goto err_kill_interrupt_urb; |
| 409 | |
| 410 | return 0; |
| 411 | |
| 412 | err_kill_interrupt_urb: |
| 413 | usb_kill_urb(port->interrupt_in_urb); |
| 414 | |
| 415 | return r; |
| 416 | } |
| 417 | |
| 418 | /* Old_termios contains the original termios settings and |
| 419 | * tty->termios contains the new setting to be used. |
| 420 | */ |
| 421 | static void ch341_set_termios(struct tty_struct *tty, |
| 422 | struct usb_serial_port *port, struct ktermios *old_termios) |
| 423 | { |
| 424 | struct ch341_private *priv = usb_get_serial_port_data(port); |
| 425 | unsigned baud_rate; |
| 426 | unsigned long flags; |
| 427 | u8 lcr; |
| 428 | int r; |
| 429 | |
| 430 | /* redundant changes may cause the chip to lose bytes */ |
| 431 | if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios)) |
| 432 | return; |
| 433 | |
| 434 | baud_rate = tty_get_baud_rate(tty); |
| 435 | |
| 436 | lcr = CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX; |
| 437 | |
| 438 | switch (C_CSIZE(tty)) { |
| 439 | case CS5: |
| 440 | lcr |= CH341_LCR_CS5; |
| 441 | break; |
| 442 | case CS6: |
| 443 | lcr |= CH341_LCR_CS6; |
| 444 | break; |
| 445 | case CS7: |
| 446 | lcr |= CH341_LCR_CS7; |
| 447 | break; |
| 448 | case CS8: |
| 449 | lcr |= CH341_LCR_CS8; |
| 450 | break; |
| 451 | } |
| 452 | |
| 453 | if (C_PARENB(tty)) { |
| 454 | lcr |= CH341_LCR_ENABLE_PAR; |
| 455 | if (C_PARODD(tty) == 0) |
| 456 | lcr |= CH341_LCR_PAR_EVEN; |
| 457 | if (C_CMSPAR(tty)) |
| 458 | lcr |= CH341_LCR_MARK_SPACE; |
| 459 | } |
| 460 | |
| 461 | if (C_CSTOPB(tty)) |
| 462 | lcr |= CH341_LCR_STOP_BITS_2; |
| 463 | |
| 464 | if (baud_rate) { |
| 465 | priv->baud_rate = baud_rate; |
| 466 | |
| 467 | r = ch341_set_baudrate_lcr(port->serial->dev, priv, lcr); |
| 468 | if (r < 0 && old_termios) { |
| 469 | priv->baud_rate = tty_termios_baud_rate(old_termios); |
| 470 | tty_termios_copy_hw(&tty->termios, old_termios); |
| 471 | } else if (r == 0) { |
| 472 | priv->lcr = lcr; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | spin_lock_irqsave(&priv->lock, flags); |
| 477 | if (C_BAUD(tty) == B0) |
| 478 | priv->mcr &= ~(CH341_BIT_DTR | CH341_BIT_RTS); |
| 479 | else if (old_termios && (old_termios->c_cflag & CBAUD) == B0) |
| 480 | priv->mcr |= (CH341_BIT_DTR | CH341_BIT_RTS); |
| 481 | spin_unlock_irqrestore(&priv->lock, flags); |
| 482 | |
| 483 | ch341_set_handshake(port->serial->dev, priv->mcr); |
| 484 | } |
| 485 | |
| 486 | static void ch341_break_ctl(struct tty_struct *tty, int break_state) |
| 487 | { |
| 488 | const uint16_t ch341_break_reg = |
| 489 | ((uint16_t) CH341_REG_LCR << 8) | CH341_REG_BREAK; |
| 490 | struct usb_serial_port *port = tty->driver_data; |
| 491 | int r; |
| 492 | uint16_t reg_contents; |
| 493 | uint8_t *break_reg; |
| 494 | |
| 495 | break_reg = kmalloc(2, GFP_KERNEL); |
| 496 | if (!break_reg) |
| 497 | return; |
| 498 | |
| 499 | r = ch341_control_in(port->serial->dev, CH341_REQ_READ_REG, |
| 500 | ch341_break_reg, 0, break_reg, 2); |
| 501 | if (r < 0) { |
| 502 | dev_err(&port->dev, "%s - USB control read error (%d)\n", |
| 503 | __func__, r); |
| 504 | goto out; |
| 505 | } |
| 506 | dev_dbg(&port->dev, "%s - initial ch341 break register contents - reg1: %x, reg2: %x\n", |
| 507 | __func__, break_reg[0], break_reg[1]); |
| 508 | if (break_state != 0) { |
| 509 | dev_dbg(&port->dev, "%s - Enter break state requested\n", __func__); |
| 510 | break_reg[0] &= ~CH341_NBREAK_BITS; |
| 511 | break_reg[1] &= ~CH341_LCR_ENABLE_TX; |
| 512 | } else { |
| 513 | dev_dbg(&port->dev, "%s - Leave break state requested\n", __func__); |
| 514 | break_reg[0] |= CH341_NBREAK_BITS; |
| 515 | break_reg[1] |= CH341_LCR_ENABLE_TX; |
| 516 | } |
| 517 | dev_dbg(&port->dev, "%s - New ch341 break register contents - reg1: %x, reg2: %x\n", |
| 518 | __func__, break_reg[0], break_reg[1]); |
| 519 | reg_contents = get_unaligned_le16(break_reg); |
| 520 | r = ch341_control_out(port->serial->dev, CH341_REQ_WRITE_REG, |
| 521 | ch341_break_reg, reg_contents); |
| 522 | if (r < 0) |
| 523 | dev_err(&port->dev, "%s - USB control write error (%d)\n", |
| 524 | __func__, r); |
| 525 | out: |
| 526 | kfree(break_reg); |
| 527 | } |
| 528 | |
| 529 | static int ch341_tiocmset(struct tty_struct *tty, |
| 530 | unsigned int set, unsigned int clear) |
| 531 | { |
| 532 | struct usb_serial_port *port = tty->driver_data; |
| 533 | struct ch341_private *priv = usb_get_serial_port_data(port); |
| 534 | unsigned long flags; |
| 535 | u8 control; |
| 536 | |
| 537 | spin_lock_irqsave(&priv->lock, flags); |
| 538 | if (set & TIOCM_RTS) |
| 539 | priv->mcr |= CH341_BIT_RTS; |
| 540 | if (set & TIOCM_DTR) |
| 541 | priv->mcr |= CH341_BIT_DTR; |
| 542 | if (clear & TIOCM_RTS) |
| 543 | priv->mcr &= ~CH341_BIT_RTS; |
| 544 | if (clear & TIOCM_DTR) |
| 545 | priv->mcr &= ~CH341_BIT_DTR; |
| 546 | control = priv->mcr; |
| 547 | spin_unlock_irqrestore(&priv->lock, flags); |
| 548 | |
| 549 | return ch341_set_handshake(port->serial->dev, control); |
| 550 | } |
| 551 | |
| 552 | static void ch341_update_status(struct usb_serial_port *port, |
| 553 | unsigned char *data, size_t len) |
| 554 | { |
| 555 | struct ch341_private *priv = usb_get_serial_port_data(port); |
| 556 | struct tty_struct *tty; |
| 557 | unsigned long flags; |
| 558 | u8 status; |
| 559 | u8 delta; |
| 560 | |
| 561 | if (len < 4) |
| 562 | return; |
| 563 | |
| 564 | status = ~data[2] & CH341_BITS_MODEM_STAT; |
| 565 | |
| 566 | spin_lock_irqsave(&priv->lock, flags); |
| 567 | delta = status ^ priv->msr; |
| 568 | priv->msr = status; |
| 569 | spin_unlock_irqrestore(&priv->lock, flags); |
| 570 | |
| 571 | if (data[1] & CH341_MULT_STAT) |
| 572 | dev_dbg(&port->dev, "%s - multiple status change\n", __func__); |
| 573 | |
| 574 | if (!delta) |
| 575 | return; |
| 576 | |
| 577 | if (delta & CH341_BIT_CTS) |
| 578 | port->icount.cts++; |
| 579 | if (delta & CH341_BIT_DSR) |
| 580 | port->icount.dsr++; |
| 581 | if (delta & CH341_BIT_RI) |
| 582 | port->icount.rng++; |
| 583 | if (delta & CH341_BIT_DCD) { |
| 584 | port->icount.dcd++; |
| 585 | tty = tty_port_tty_get(&port->port); |
| 586 | if (tty) { |
| 587 | usb_serial_handle_dcd_change(port, tty, |
| 588 | status & CH341_BIT_DCD); |
| 589 | tty_kref_put(tty); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | wake_up_interruptible(&port->port.delta_msr_wait); |
| 594 | } |
| 595 | |
| 596 | static void ch341_read_int_callback(struct urb *urb) |
| 597 | { |
| 598 | struct usb_serial_port *port = urb->context; |
| 599 | unsigned char *data = urb->transfer_buffer; |
| 600 | unsigned int len = urb->actual_length; |
| 601 | int status; |
| 602 | |
| 603 | switch (urb->status) { |
| 604 | case 0: |
| 605 | /* success */ |
| 606 | break; |
| 607 | case -ECONNRESET: |
| 608 | case -ENOENT: |
| 609 | case -ESHUTDOWN: |
| 610 | /* this urb is terminated, clean up */ |
| 611 | dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n", |
| 612 | __func__, urb->status); |
| 613 | return; |
| 614 | default: |
| 615 | dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n", |
| 616 | __func__, urb->status); |
| 617 | goto exit; |
| 618 | } |
| 619 | |
| 620 | usb_serial_debug_data(&port->dev, __func__, len, data); |
| 621 | ch341_update_status(port, data, len); |
| 622 | exit: |
| 623 | status = usb_submit_urb(urb, GFP_ATOMIC); |
| 624 | if (status) { |
| 625 | dev_err(&urb->dev->dev, "%s - usb_submit_urb failed: %d\n", |
| 626 | __func__, status); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | static int ch341_tiocmget(struct tty_struct *tty) |
| 631 | { |
| 632 | struct usb_serial_port *port = tty->driver_data; |
| 633 | struct ch341_private *priv = usb_get_serial_port_data(port); |
| 634 | unsigned long flags; |
| 635 | u8 mcr; |
| 636 | u8 status; |
| 637 | unsigned int result; |
| 638 | |
| 639 | spin_lock_irqsave(&priv->lock, flags); |
| 640 | mcr = priv->mcr; |
| 641 | status = priv->msr; |
| 642 | spin_unlock_irqrestore(&priv->lock, flags); |
| 643 | |
| 644 | result = ((mcr & CH341_BIT_DTR) ? TIOCM_DTR : 0) |
| 645 | | ((mcr & CH341_BIT_RTS) ? TIOCM_RTS : 0) |
| 646 | | ((status & CH341_BIT_CTS) ? TIOCM_CTS : 0) |
| 647 | | ((status & CH341_BIT_DSR) ? TIOCM_DSR : 0) |
| 648 | | ((status & CH341_BIT_RI) ? TIOCM_RI : 0) |
| 649 | | ((status & CH341_BIT_DCD) ? TIOCM_CD : 0); |
| 650 | |
| 651 | dev_dbg(&port->dev, "%s - result = %x\n", __func__, result); |
| 652 | |
| 653 | return result; |
| 654 | } |
| 655 | |
| 656 | static int ch341_reset_resume(struct usb_serial *serial) |
| 657 | { |
| 658 | struct usb_serial_port *port = serial->port[0]; |
| 659 | struct ch341_private *priv; |
| 660 | int ret; |
| 661 | |
| 662 | priv = usb_get_serial_port_data(port); |
| 663 | if (!priv) |
| 664 | return 0; |
| 665 | |
| 666 | /* reconfigure ch341 serial port after bus-reset */ |
| 667 | ch341_configure(serial->dev, priv); |
| 668 | |
| 669 | if (tty_port_initialized(&port->port)) { |
| 670 | ret = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO); |
| 671 | if (ret) { |
| 672 | dev_err(&port->dev, "failed to submit interrupt urb: %d\n", |
| 673 | ret); |
| 674 | return ret; |
| 675 | } |
| 676 | |
| 677 | ret = ch341_get_status(port->serial->dev, priv); |
| 678 | if (ret < 0) { |
| 679 | dev_err(&port->dev, "failed to read modem status: %d\n", |
| 680 | ret); |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | return usb_serial_generic_resume(serial); |
| 685 | } |
| 686 | |
| 687 | static struct usb_serial_driver ch341_device = { |
| 688 | .driver = { |
| 689 | .owner = THIS_MODULE, |
| 690 | .name = "ch341-uart", |
| 691 | }, |
| 692 | .id_table = id_table, |
| 693 | .num_ports = 1, |
| 694 | .open = ch341_open, |
| 695 | .dtr_rts = ch341_dtr_rts, |
| 696 | .carrier_raised = ch341_carrier_raised, |
| 697 | .close = ch341_close, |
| 698 | .set_termios = ch341_set_termios, |
| 699 | .break_ctl = ch341_break_ctl, |
| 700 | .tiocmget = ch341_tiocmget, |
| 701 | .tiocmset = ch341_tiocmset, |
| 702 | .tiocmiwait = usb_serial_generic_tiocmiwait, |
| 703 | .read_int_callback = ch341_read_int_callback, |
| 704 | .port_probe = ch341_port_probe, |
| 705 | .port_remove = ch341_port_remove, |
| 706 | .reset_resume = ch341_reset_resume, |
| 707 | }; |
| 708 | |
| 709 | static struct usb_serial_driver * const serial_drivers[] = { |
| 710 | &ch341_device, NULL |
| 711 | }; |
| 712 | |
| 713 | module_usb_serial_driver(serial_drivers, id_table); |
| 714 | |
| 715 | MODULE_LICENSE("GPL v2"); |