1 // SPDX-License-Identifier: GPL-2.0
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>
7 * ch341.c implements a serial port driver for the Winchiphead CH341.
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.
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>
24 #define DEFAULT_BAUD_RATE 9600
25 #define DEFAULT_TIMEOUT 1000
27 /* flags for IO-Bits */
28 #define CH341_BIT_RTS (1 << 6)
29 #define CH341_BIT_DTR (1 << 5)
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 */
40 /* second interrupt byte */
41 #define CH341_MULT_STAT 0x04 /* multiple status since last interrupt event */
43 /* status returned in third interrupt answer byte, inverted in data
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 */
51 /*******************************/
52 /* baudrate calculation factor */
53 /*******************************/
54 #define CH341_BAUDBASE_FACTOR 1532620800
55 #define CH341_BAUDBASE_DIVMAX 3
57 /* Break support - the information used to implement this was gleaned from
58 * the Net/FreeBSD uchcom.c driver by Takanori Watanabe. Domo arigato.
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
67 #define CH341_REG_BREAK 0x05
68 #define CH341_REG_LCR 0x18
69 #define CH341_NBREAK_BITS 0x01
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
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) },
91 MODULE_DEVICE_TABLE(usb, id_table);
93 struct ch341_private {
94 spinlock_t lock; /* access lock */
95 unsigned baud_rate; /* set baud rate */
103 static void ch341_set_termios(struct tty_struct *tty,
104 struct usb_serial_port *port,
105 struct ktermios *old_termios);
107 static int ch341_control_out(struct usb_device *dev, u8 request,
108 u16 value, u16 index)
112 dev_dbg(&dev->dev, "%s - (%02x,%04x,%04x)\n", __func__,
113 request, value, index);
115 r = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
116 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
117 value, index, NULL, 0, DEFAULT_TIMEOUT);
119 dev_err(&dev->dev, "failed to send control message: %d\n", r);
124 static int ch341_control_in(struct usb_device *dev,
125 u8 request, u16 value, u16 index,
126 char *buf, unsigned bufsize)
130 dev_dbg(&dev->dev, "%s - (%02x,%04x,%04x,%u)\n", __func__,
131 request, value, index, bufsize);
133 r = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
134 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
135 value, index, buf, bufsize, DEFAULT_TIMEOUT);
136 if (r < (int)bufsize) {
139 "short control message received (%d < %u)\n",
144 dev_err(&dev->dev, "failed to receive control message: %d\n",
152 static int ch341_set_baudrate_lcr(struct usb_device *dev,
153 struct ch341_private *priv, u8 lcr)
157 unsigned long factor;
160 if (!priv->baud_rate)
162 factor = (CH341_BAUDBASE_FACTOR / priv->baud_rate);
163 divisor = CH341_BAUDBASE_DIVMAX;
165 while ((factor > 0xfff0) && divisor) {
173 factor = 0x10000 - factor;
174 a = (factor & 0xff00) | divisor;
177 * CH341A buffers data until a full endpoint-size packet (32 bytes)
178 * has been received unless bit 7 is set.
180 * At least one device with version 0x27 appears to have this bit
183 if (priv->version > 0x27)
186 r = ch341_control_out(dev, CH341_REQ_WRITE_REG, 0x1312, a);
190 if (priv->version < 0x30)
193 r = ch341_control_out(dev, CH341_REQ_WRITE_REG, 0x2518, lcr);
200 static int ch341_set_handshake(struct usb_device *dev, u8 control)
202 return ch341_control_out(dev, CH341_REQ_MODEM_CTRL, ~control, 0);
205 static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv)
207 const unsigned int size = 2;
212 buffer = kmalloc(size, GFP_KERNEL);
216 r = ch341_control_in(dev, CH341_REQ_READ_REG, 0x0706, 0, buffer, size);
220 spin_lock_irqsave(&priv->lock, flags);
221 priv->msr = (~(*buffer)) & CH341_BITS_MODEM_STAT;
222 spin_unlock_irqrestore(&priv->lock, flags);
228 /* -------------------------------------------------------------------------- */
230 static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
232 const unsigned int size = 2;
236 buffer = kmalloc(size, GFP_KERNEL);
240 /* expect two bytes 0x27 0x00 */
241 r = ch341_control_in(dev, CH341_REQ_READ_VERSION, 0, 0, buffer, size);
245 priv->version = buffer[0];
246 dev_dbg(&dev->dev, "Chip version: 0x%02x\n", priv->version);
248 r = ch341_control_out(dev, CH341_REQ_SERIAL_INIT, 0, 0);
252 r = ch341_set_baudrate_lcr(dev, priv, priv->lcr);
256 r = ch341_set_handshake(dev, priv->mcr);
262 static int ch341_port_probe(struct usb_serial_port *port)
264 struct ch341_private *priv;
267 priv = kzalloc(sizeof(struct ch341_private), GFP_KERNEL);
271 spin_lock_init(&priv->lock);
272 priv->baud_rate = DEFAULT_BAUD_RATE;
274 * Some CH340 devices appear unable to change the initial LCR
275 * settings, so set a sane 8N1 default.
277 priv->lcr = CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX | CH341_LCR_CS8;
279 r = ch341_configure(port->serial->dev, priv);
283 usb_set_serial_port_data(port, priv);
290 static int ch341_port_remove(struct usb_serial_port *port)
292 struct ch341_private *priv;
294 priv = usb_get_serial_port_data(port);
300 static int ch341_carrier_raised(struct usb_serial_port *port)
302 struct ch341_private *priv = usb_get_serial_port_data(port);
303 if (priv->msr & CH341_BIT_DCD)
308 static void ch341_dtr_rts(struct usb_serial_port *port, int on)
310 struct ch341_private *priv = usb_get_serial_port_data(port);
313 /* drop DTR and RTS */
314 spin_lock_irqsave(&priv->lock, flags);
316 priv->mcr |= CH341_BIT_RTS | CH341_BIT_DTR;
318 priv->mcr &= ~(CH341_BIT_RTS | CH341_BIT_DTR);
319 spin_unlock_irqrestore(&priv->lock, flags);
320 ch341_set_handshake(port->serial->dev, priv->mcr);
323 static void ch341_close(struct usb_serial_port *port)
325 usb_serial_generic_close(port);
326 usb_kill_urb(port->interrupt_in_urb);
330 /* open this device, set default parameters */
331 static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port)
333 struct ch341_private *priv = usb_get_serial_port_data(port);
337 ch341_set_termios(tty, port, NULL);
339 dev_dbg(&port->dev, "%s - submitting interrupt urb\n", __func__);
340 r = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
342 dev_err(&port->dev, "%s - failed to submit interrupt urb: %d\n",
347 r = ch341_get_status(port->serial->dev, priv);
349 dev_err(&port->dev, "failed to read modem status: %d\n", r);
350 goto err_kill_interrupt_urb;
353 r = usb_serial_generic_open(tty, port);
355 goto err_kill_interrupt_urb;
359 err_kill_interrupt_urb:
360 usb_kill_urb(port->interrupt_in_urb);
365 /* Old_termios contains the original termios settings and
366 * tty->termios contains the new setting to be used.
368 static void ch341_set_termios(struct tty_struct *tty,
369 struct usb_serial_port *port, struct ktermios *old_termios)
371 struct ch341_private *priv = usb_get_serial_port_data(port);
377 /* redundant changes may cause the chip to lose bytes */
378 if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
381 baud_rate = tty_get_baud_rate(tty);
383 lcr = CH341_LCR_ENABLE_RX | CH341_LCR_ENABLE_TX;
385 switch (C_CSIZE(tty)) {
387 lcr |= CH341_LCR_CS5;
390 lcr |= CH341_LCR_CS6;
393 lcr |= CH341_LCR_CS7;
396 lcr |= CH341_LCR_CS8;
401 lcr |= CH341_LCR_ENABLE_PAR;
402 if (C_PARODD(tty) == 0)
403 lcr |= CH341_LCR_PAR_EVEN;
405 lcr |= CH341_LCR_MARK_SPACE;
409 lcr |= CH341_LCR_STOP_BITS_2;
412 priv->baud_rate = baud_rate;
414 r = ch341_set_baudrate_lcr(port->serial->dev, priv, lcr);
415 if (r < 0 && old_termios) {
416 priv->baud_rate = tty_termios_baud_rate(old_termios);
417 tty_termios_copy_hw(&tty->termios, old_termios);
423 spin_lock_irqsave(&priv->lock, flags);
424 if (C_BAUD(tty) == B0)
425 priv->mcr &= ~(CH341_BIT_DTR | CH341_BIT_RTS);
426 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
427 priv->mcr |= (CH341_BIT_DTR | CH341_BIT_RTS);
428 spin_unlock_irqrestore(&priv->lock, flags);
430 ch341_set_handshake(port->serial->dev, priv->mcr);
433 static void ch341_break_ctl(struct tty_struct *tty, int break_state)
435 const uint16_t ch341_break_reg =
436 ((uint16_t) CH341_REG_LCR << 8) | CH341_REG_BREAK;
437 struct usb_serial_port *port = tty->driver_data;
439 uint16_t reg_contents;
442 break_reg = kmalloc(2, GFP_KERNEL);
446 r = ch341_control_in(port->serial->dev, CH341_REQ_READ_REG,
447 ch341_break_reg, 0, break_reg, 2);
449 dev_err(&port->dev, "%s - USB control read error (%d)\n",
453 dev_dbg(&port->dev, "%s - initial ch341 break register contents - reg1: %x, reg2: %x\n",
454 __func__, break_reg[0], break_reg[1]);
455 if (break_state != 0) {
456 dev_dbg(&port->dev, "%s - Enter break state requested\n", __func__);
457 break_reg[0] &= ~CH341_NBREAK_BITS;
458 break_reg[1] &= ~CH341_LCR_ENABLE_TX;
460 dev_dbg(&port->dev, "%s - Leave break state requested\n", __func__);
461 break_reg[0] |= CH341_NBREAK_BITS;
462 break_reg[1] |= CH341_LCR_ENABLE_TX;
464 dev_dbg(&port->dev, "%s - New ch341 break register contents - reg1: %x, reg2: %x\n",
465 __func__, break_reg[0], break_reg[1]);
466 reg_contents = get_unaligned_le16(break_reg);
467 r = ch341_control_out(port->serial->dev, CH341_REQ_WRITE_REG,
468 ch341_break_reg, reg_contents);
470 dev_err(&port->dev, "%s - USB control write error (%d)\n",
476 static int ch341_tiocmset(struct tty_struct *tty,
477 unsigned int set, unsigned int clear)
479 struct usb_serial_port *port = tty->driver_data;
480 struct ch341_private *priv = usb_get_serial_port_data(port);
484 spin_lock_irqsave(&priv->lock, flags);
486 priv->mcr |= CH341_BIT_RTS;
488 priv->mcr |= CH341_BIT_DTR;
489 if (clear & TIOCM_RTS)
490 priv->mcr &= ~CH341_BIT_RTS;
491 if (clear & TIOCM_DTR)
492 priv->mcr &= ~CH341_BIT_DTR;
494 spin_unlock_irqrestore(&priv->lock, flags);
496 return ch341_set_handshake(port->serial->dev, control);
499 static void ch341_update_status(struct usb_serial_port *port,
500 unsigned char *data, size_t len)
502 struct ch341_private *priv = usb_get_serial_port_data(port);
503 struct tty_struct *tty;
511 status = ~data[2] & CH341_BITS_MODEM_STAT;
513 spin_lock_irqsave(&priv->lock, flags);
514 delta = status ^ priv->msr;
516 spin_unlock_irqrestore(&priv->lock, flags);
518 if (data[1] & CH341_MULT_STAT)
519 dev_dbg(&port->dev, "%s - multiple status change\n", __func__);
524 if (delta & CH341_BIT_CTS)
526 if (delta & CH341_BIT_DSR)
528 if (delta & CH341_BIT_RI)
530 if (delta & CH341_BIT_DCD) {
532 tty = tty_port_tty_get(&port->port);
534 usb_serial_handle_dcd_change(port, tty,
535 status & CH341_BIT_DCD);
540 wake_up_interruptible(&port->port.delta_msr_wait);
543 static void ch341_read_int_callback(struct urb *urb)
545 struct usb_serial_port *port = urb->context;
546 unsigned char *data = urb->transfer_buffer;
547 unsigned int len = urb->actual_length;
550 switch (urb->status) {
557 /* this urb is terminated, clean up */
558 dev_dbg(&urb->dev->dev, "%s - urb shutting down: %d\n",
559 __func__, urb->status);
562 dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n",
563 __func__, urb->status);
567 usb_serial_debug_data(&port->dev, __func__, len, data);
568 ch341_update_status(port, data, len);
570 status = usb_submit_urb(urb, GFP_ATOMIC);
572 dev_err(&urb->dev->dev, "%s - usb_submit_urb failed: %d\n",
577 static int ch341_tiocmget(struct tty_struct *tty)
579 struct usb_serial_port *port = tty->driver_data;
580 struct ch341_private *priv = usb_get_serial_port_data(port);
586 spin_lock_irqsave(&priv->lock, flags);
589 spin_unlock_irqrestore(&priv->lock, flags);
591 result = ((mcr & CH341_BIT_DTR) ? TIOCM_DTR : 0)
592 | ((mcr & CH341_BIT_RTS) ? TIOCM_RTS : 0)
593 | ((status & CH341_BIT_CTS) ? TIOCM_CTS : 0)
594 | ((status & CH341_BIT_DSR) ? TIOCM_DSR : 0)
595 | ((status & CH341_BIT_RI) ? TIOCM_RI : 0)
596 | ((status & CH341_BIT_DCD) ? TIOCM_CD : 0);
598 dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
603 static int ch341_reset_resume(struct usb_serial *serial)
605 struct usb_serial_port *port = serial->port[0];
606 struct ch341_private *priv;
609 priv = usb_get_serial_port_data(port);
613 /* reconfigure ch341 serial port after bus-reset */
614 ch341_configure(serial->dev, priv);
616 if (tty_port_initialized(&port->port)) {
617 ret = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
619 dev_err(&port->dev, "failed to submit interrupt urb: %d\n",
624 ret = ch341_get_status(port->serial->dev, priv);
626 dev_err(&port->dev, "failed to read modem status: %d\n",
631 return usb_serial_generic_resume(serial);
634 static struct usb_serial_driver ch341_device = {
636 .owner = THIS_MODULE,
637 .name = "ch341-uart",
639 .id_table = id_table,
642 .dtr_rts = ch341_dtr_rts,
643 .carrier_raised = ch341_carrier_raised,
644 .close = ch341_close,
645 .set_termios = ch341_set_termios,
646 .break_ctl = ch341_break_ctl,
647 .tiocmget = ch341_tiocmget,
648 .tiocmset = ch341_tiocmset,
649 .tiocmiwait = usb_serial_generic_tiocmiwait,
650 .read_int_callback = ch341_read_int_callback,
651 .port_probe = ch341_port_probe,
652 .port_remove = ch341_port_remove,
653 .reset_resume = ch341_reset_resume,
656 static struct usb_serial_driver * const serial_drivers[] = {
660 module_usb_serial_driver(serial_drivers, id_table);
662 MODULE_LICENSE("GPL v2");