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 * This program is free software; you can redistribute it and/or 15 * modify it under the terms of the GNU General Public License version 16 * 2 as published by the Free Software Foundation. 17 */ 18 19 #include <linux/kernel.h> 20 #include <linux/tty.h> 21 #include <linux/module.h> 22 #include <linux/slab.h> 23 #include <linux/usb.h> 24 #include <linux/usb/serial.h> 25 #include <linux/serial.h> 26 #include <asm/unaligned.h> 27 28 #define DEFAULT_BAUD_RATE 9600 29 #define DEFAULT_TIMEOUT 1000 30 31 /* flags for IO-Bits */ 32 #define CH341_BIT_RTS (1 << 6) 33 #define CH341_BIT_DTR (1 << 5) 34 35 /******************************/ 36 /* interrupt pipe definitions */ 37 /******************************/ 38 /* always 4 interrupt bytes */ 39 /* first irq byte normally 0x08 */ 40 /* second irq byte base 0x7d + below */ 41 /* third irq byte base 0x94 + below */ 42 /* fourth irq byte normally 0xee */ 43 44 /* second interrupt byte */ 45 #define CH341_MULT_STAT 0x04 /* multiple status since last interrupt event */ 46 47 /* status returned in third interrupt answer byte, inverted in data 48 from irq */ 49 #define CH341_BIT_CTS 0x01 50 #define CH341_BIT_DSR 0x02 51 #define CH341_BIT_RI 0x04 52 #define CH341_BIT_DCD 0x08 53 #define CH341_BITS_MODEM_STAT 0x0f /* all bits */ 54 55 /*******************************/ 56 /* baudrate calculation factor */ 57 /*******************************/ 58 #define CH341_BAUDBASE_FACTOR 1532620800 59 #define CH341_BAUDBASE_DIVMAX 3 60 61 /* Break support - the information used to implement this was gleaned from 62 * the Net/FreeBSD uchcom.c driver by Takanori Watanabe. Domo arigato. 63 */ 64 65 #define CH341_REQ_READ_VERSION 0x5F 66 #define CH341_REQ_WRITE_REG 0x9A 67 #define CH341_REQ_READ_REG 0x95 68 #define CH341_REQ_SERIAL_INIT 0xA1 69 #define CH341_REQ_MODEM_CTRL 0xA4 70 71 #define CH341_REG_BREAK 0x05 72 #define CH341_REG_LCR 0x18 73 #define CH341_NBREAK_BITS 0x01 74 75 #define CH341_LCR_ENABLE_RX 0x80 76 #define CH341_LCR_ENABLE_TX 0x40 77 #define CH341_LCR_MARK_SPACE 0x20 78 #define CH341_LCR_PAR_EVEN 0x10 79 #define CH341_LCR_ENABLE_PAR 0x08 80 #define CH341_LCR_STOP_BITS_2 0x04 81 #define CH341_LCR_CS8 0x03 82 #define CH341_LCR_CS7 0x02 83 #define CH341_LCR_CS6 0x01 84 #define CH341_LCR_CS5 0x00 85 86 static const struct usb_device_id id_table[] = { 87 { USB_DEVICE(0x4348, 0x5523) }, 88 { USB_DEVICE(0x1a86, 0x7523) }, 89 { USB_DEVICE(0x1a86, 0x5523) }, 90 { }, 91 }; 92 MODULE_DEVICE_TABLE(usb, id_table); 93 94 struct ch341_private { 95 spinlock_t lock; /* access lock */ 96 unsigned baud_rate; /* set baud rate */ 97 u8 mcr; 98 u8 msr; 99 u8 lcr; 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 < 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 252 static int ch341_port_probe(struct usb_serial_port *port) 253 { 254 struct ch341_private *priv; 255 int r; 256 257 priv = kzalloc(sizeof(struct ch341_private), GFP_KERNEL); 258 if (!priv) 259 return -ENOMEM; 260 261 spin_lock_init(&priv->lock); 262 priv->baud_rate = DEFAULT_BAUD_RATE; 263 /* 264 * Some CH340 devices appear unable to change the initial LCR 265 * settings, so set a sane 8N1 default. 266 */ 267 priv->lcr = CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX | CH341_LCR_CS8; 268 269 r = ch341_configure(port->serial->dev, priv); 270 if (r < 0) 271 goto error; 272 273 usb_set_serial_port_data(port, priv); 274 return 0; 275 276 error: kfree(priv); 277 return r; 278 } 279 280 static int ch341_port_remove(struct usb_serial_port *port) 281 { 282 struct ch341_private *priv; 283 284 priv = usb_get_serial_port_data(port); 285 kfree(priv); 286 287 return 0; 288 } 289 290 static int ch341_carrier_raised(struct usb_serial_port *port) 291 { 292 struct ch341_private *priv = usb_get_serial_port_data(port); 293 if (priv->msr & CH341_BIT_DCD) 294 return 1; 295 return 0; 296 } 297 298 static void ch341_dtr_rts(struct usb_serial_port *port, int on) 299 { 300 struct ch341_private *priv = usb_get_serial_port_data(port); 301 unsigned long flags; 302 303 /* drop DTR and RTS */ 304 spin_lock_irqsave(&priv->lock, flags); 305 if (on) 306 priv->mcr |= CH341_BIT_RTS | CH341_BIT_DTR; 307 else 308 priv->mcr &= ~(CH341_BIT_RTS | CH341_BIT_DTR); 309 spin_unlock_irqrestore(&priv->lock, flags); 310 ch341_set_handshake(port->serial->dev, priv->mcr); 311 } 312 313 static void ch341_close(struct usb_serial_port *port) 314 { 315 usb_serial_generic_close(port); 316 usb_kill_urb(port->interrupt_in_urb); 317 } 318 319 320 /* open this device, set default parameters */ 321 static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port) 322 { 323 struct ch341_private *priv = usb_get_serial_port_data(port); 324 int r; 325 326 if (tty) 327 ch341_set_termios(tty, port, NULL); 328 329 dev_dbg(&port->dev, "%s - submitting interrupt urb\n", __func__); 330 r = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); 331 if (r) { 332 dev_err(&port->dev, "%s - failed to submit interrupt urb: %d\n", 333 __func__, r); 334 return r; 335 } 336 337 r = ch341_get_status(port->serial->dev, priv); 338 if (r < 0) { 339 dev_err(&port->dev, "failed to read modem status: %d\n", r); 340 goto err_kill_interrupt_urb; 341 } 342 343 r = usb_serial_generic_open(tty, port); 344 if (r) 345 goto err_kill_interrupt_urb; 346 347 return 0; 348 349 err_kill_interrupt_urb: 350 usb_kill_urb(port->interrupt_in_urb); 351 352 return r; 353 } 354 355 /* Old_termios contains the original termios settings and 356 * tty->termios contains the new setting to be used. 357 */ 358 static void ch341_set_termios(struct tty_struct *tty, 359 struct usb_serial_port *port, struct ktermios *old_termios) 360 { 361 struct ch341_private *priv = usb_get_serial_port_data(port); 362 unsigned baud_rate; 363 unsigned long flags; 364 u8 lcr; 365 int r; 366 367 /* redundant changes may cause the chip to lose bytes */ 368 if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios)) 369 return; 370 371 baud_rate = tty_get_baud_rate(tty); 372 373 lcr = CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX; 374 375 switch (C_CSIZE(tty)) { 376 case CS5: 377 lcr |= CH341_LCR_CS5; 378 break; 379 case CS6: 380 lcr |= CH341_LCR_CS6; 381 break; 382 case CS7: 383 lcr |= CH341_LCR_CS7; 384 break; 385 case CS8: 386 lcr |= CH341_LCR_CS8; 387 break; 388 } 389 390 if (C_PARENB(tty)) { 391 lcr |= CH341_LCR_ENABLE_PAR; 392 if (C_PARODD(tty) == 0) 393 lcr |= CH341_LCR_PAR_EVEN; 394 if (C_CMSPAR(tty)) 395 lcr |= CH341_LCR_MARK_SPACE; 396 } 397 398 if (C_CSTOPB(tty)) 399 lcr |= CH341_LCR_STOP_BITS_2; 400 401 if (baud_rate) { 402 priv->baud_rate = baud_rate; 403 404 r = ch341_set_baudrate_lcr(port->serial->dev, priv, lcr); 405 if (r < 0 && old_termios) { 406 priv->baud_rate = tty_termios_baud_rate(old_termios); 407 tty_termios_copy_hw(&tty->termios, old_termios); 408 } else if (r == 0) { 409 priv->lcr = lcr; 410 } 411 } 412 413 spin_lock_irqsave(&priv->lock, flags); 414 if (C_BAUD(tty) == B0) 415 priv->mcr &= ~(CH341_BIT_DTR | CH341_BIT_RTS); 416 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0) 417 priv->mcr |= (CH341_BIT_DTR | CH341_BIT_RTS); 418 spin_unlock_irqrestore(&priv->lock, flags); 419 420 ch341_set_handshake(port->serial->dev, priv->mcr); 421 } 422 423 static void ch341_break_ctl(struct tty_struct *tty, int break_state) 424 { 425 const uint16_t ch341_break_reg = 426 ((uint16_t) CH341_REG_LCR << 8) | CH341_REG_BREAK; 427 struct usb_serial_port *port = tty->driver_data; 428 int r; 429 uint16_t reg_contents; 430 uint8_t *break_reg; 431 432 break_reg = kmalloc(2, GFP_KERNEL); 433 if (!break_reg) 434 return; 435 436 r = ch341_control_in(port->serial->dev, CH341_REQ_READ_REG, 437 ch341_break_reg, 0, break_reg, 2); 438 if (r < 0) { 439 dev_err(&port->dev, "%s - USB control read error (%d)\n", 440 __func__, r); 441 goto out; 442 } 443 dev_dbg(&port->dev, "%s - initial ch341 break register contents - reg1: %x, reg2: %x\n", 444 __func__, break_reg[0], break_reg[1]); 445 if (break_state != 0) { 446 dev_dbg(&port->dev, "%s - Enter break state requested\n", __func__); 447 break_reg[0] &= ~CH341_NBREAK_BITS; 448 break_reg[1] &= ~CH341_LCR_ENABLE_TX; 449 } else { 450 dev_dbg(&port->dev, "%s - Leave break state requested\n", __func__); 451 break_reg[0] |= CH341_NBREAK_BITS; 452 break_reg[1] |= CH341_LCR_ENABLE_TX; 453 } 454 dev_dbg(&port->dev, "%s - New ch341 break register contents - reg1: %x, reg2: %x\n", 455 __func__, break_reg[0], break_reg[1]); 456 reg_contents = get_unaligned_le16(break_reg); 457 r = ch341_control_out(port->serial->dev, CH341_REQ_WRITE_REG, 458 ch341_break_reg, reg_contents); 459 if (r < 0) 460 dev_err(&port->dev, "%s - USB control write error (%d)\n", 461 __func__, r); 462 out: 463 kfree(break_reg); 464 } 465 466 static int ch341_tiocmset(struct tty_struct *tty, 467 unsigned int set, unsigned int clear) 468 { 469 struct usb_serial_port *port = tty->driver_data; 470 struct ch341_private *priv = usb_get_serial_port_data(port); 471 unsigned long flags; 472 u8 control; 473 474 spin_lock_irqsave(&priv->lock, flags); 475 if (set & TIOCM_RTS) 476 priv->mcr |= CH341_BIT_RTS; 477 if (set & TIOCM_DTR) 478 priv->mcr |= CH341_BIT_DTR; 479 if (clear & TIOCM_RTS) 480 priv->mcr &= ~CH341_BIT_RTS; 481 if (clear & TIOCM_DTR) 482 priv->mcr &= ~CH341_BIT_DTR; 483 control = priv->mcr; 484 spin_unlock_irqrestore(&priv->lock, flags); 485 486 return ch341_set_handshake(port->serial->dev, control); 487 } 488 489 static void ch341_update_status(struct usb_serial_port *port, 490 unsigned char *data, size_t len) 491 { 492 struct ch341_private *priv = usb_get_serial_port_data(port); 493 struct tty_struct *tty; 494 unsigned long flags; 495 u8 status; 496 u8 delta; 497 498 if (len < 4) 499 return; 500 501 status = ~data[2] & CH341_BITS_MODEM_STAT; 502 503 spin_lock_irqsave(&priv->lock, flags); 504 delta = status ^ priv->msr; 505 priv->msr = status; 506 spin_unlock_irqrestore(&priv->lock, flags); 507 508 if (data[1] & CH341_MULT_STAT) 509 dev_dbg(&port->dev, "%s - multiple status change\n", __func__); 510 511 if (!delta) 512 return; 513 514 if (delta & CH341_BIT_CTS) 515 port->icount.cts++; 516 if (delta & CH341_BIT_DSR) 517 port->icount.dsr++; 518 if (delta & CH341_BIT_RI) 519 port->icount.rng++; 520 if (delta & CH341_BIT_DCD) { 521 port->icount.dcd++; 522 tty = tty_port_tty_get(&port->port); 523 if (tty) { 524 usb_serial_handle_dcd_change(port, tty, 525 status & CH341_BIT_DCD); 526 tty_kref_put(tty); 527 } 528 } 529 530 wake_up_interruptible(&port->port.delta_msr_wait); 531 } 532 533 static void ch341_read_int_callback(struct urb *urb) 534 { 535 struct usb_serial_port *port = urb->context; 536 unsigned char *data = urb->transfer_buffer; 537 unsigned int len = urb->actual_length; 538 int status; 539 540 switch (urb->status) { 541 case 0: 542 /* success */ 543 break; 544 case -ECONNRESET: 545 case -ENOENT: 546 case -ESHUTDOWN: 547 /* this urb is terminated, clean up */ 548 dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n", 549 __func__, urb->status); 550 return; 551 default: 552 dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n", 553 __func__, urb->status); 554 goto exit; 555 } 556 557 usb_serial_debug_data(&port->dev, __func__, len, data); 558 ch341_update_status(port, data, len); 559 exit: 560 status = usb_submit_urb(urb, GFP_ATOMIC); 561 if (status) { 562 dev_err(&urb->dev->dev, "%s - usb_submit_urb failed: %d\n", 563 __func__, status); 564 } 565 } 566 567 static int ch341_tiocmget(struct tty_struct *tty) 568 { 569 struct usb_serial_port *port = tty->driver_data; 570 struct ch341_private *priv = usb_get_serial_port_data(port); 571 unsigned long flags; 572 u8 mcr; 573 u8 status; 574 unsigned int result; 575 576 spin_lock_irqsave(&priv->lock, flags); 577 mcr = priv->mcr; 578 status = priv->msr; 579 spin_unlock_irqrestore(&priv->lock, flags); 580 581 result = ((mcr & CH341_BIT_DTR) ? TIOCM_DTR : 0) 582 | ((mcr & CH341_BIT_RTS) ? TIOCM_RTS : 0) 583 | ((status & CH341_BIT_CTS) ? TIOCM_CTS : 0) 584 | ((status & CH341_BIT_DSR) ? TIOCM_DSR : 0) 585 | ((status & CH341_BIT_RI) ? TIOCM_RI : 0) 586 | ((status & CH341_BIT_DCD) ? TIOCM_CD : 0); 587 588 dev_dbg(&port->dev, "%s - result = %x\n", __func__, result); 589 590 return result; 591 } 592 593 static int ch341_reset_resume(struct usb_serial *serial) 594 { 595 struct usb_serial_port *port = serial->port[0]; 596 struct ch341_private *priv = usb_get_serial_port_data(port); 597 int ret; 598 599 /* reconfigure ch341 serial port after bus-reset */ 600 ch341_configure(serial->dev, priv); 601 602 if (tty_port_initialized(&port->port)) { 603 ret = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO); 604 if (ret) { 605 dev_err(&port->dev, "failed to submit interrupt urb: %d\n", 606 ret); 607 return ret; 608 } 609 610 ret = ch341_get_status(port->serial->dev, priv); 611 if (ret < 0) { 612 dev_err(&port->dev, "failed to read modem status: %d\n", 613 ret); 614 } 615 } 616 617 return usb_serial_generic_resume(serial); 618 } 619 620 static struct usb_serial_driver ch341_device = { 621 .driver = { 622 .owner = THIS_MODULE, 623 .name = "ch341-uart", 624 }, 625 .id_table = id_table, 626 .num_ports = 1, 627 .open = ch341_open, 628 .dtr_rts = ch341_dtr_rts, 629 .carrier_raised = ch341_carrier_raised, 630 .close = ch341_close, 631 .set_termios = ch341_set_termios, 632 .break_ctl = ch341_break_ctl, 633 .tiocmget = ch341_tiocmget, 634 .tiocmset = ch341_tiocmset, 635 .tiocmiwait = usb_serial_generic_tiocmiwait, 636 .read_int_callback = ch341_read_int_callback, 637 .port_probe = ch341_port_probe, 638 .port_remove = ch341_port_remove, 639 .reset_resume = ch341_reset_resume, 640 }; 641 642 static struct usb_serial_driver * const serial_drivers[] = { 643 &ch341_device, NULL 644 }; 645 646 module_usb_serial_driver(serial_drivers, id_table); 647 648 MODULE_LICENSE("GPL"); 649