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