2 * Copyright 2007, Frank A Kingswood <frank@kingswood-consulting.co.uk>
3 * Copyright 2007, Werner Cornelius <werner <at> cornelius-consult.de>
5 * ch341.c implements a serial port driver for the Winchiphead CH341.
7 * The CH341 device can be used to implement an RS232 asynchronous
8 * serial port, an IEEE-1284 parallel printer port or a memory-like
9 * interface. In all cases the CH341 supports an I2C interface as well.
10 * This driver only supports the asynchronous serial interface.
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License version
14 * 2 as published by the Free Software Foundation.
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/tty.h>
20 #include <linux/module.h>
21 #include <linux/usb.h>
22 #include <linux/usb/serial.h>
23 #include <linux/serial.h>
24 #include <linux/i2c.h>
25 #include <linux/i2c-algo-bit.h>
27 #define DEFAULT_BAUD_RATE 2400
28 #define DEFAULT_TIMEOUT 1000
30 /* flags for IO-Bits */
31 #define CH341_BIT_RTS (1 << 6)
32 #define CH341_BIT_DTR (1 << 5)
34 /******************************/
35 /* interrupt pipe definitions */
36 /******************************/
37 /* always 4 interrupt bytes */
38 /* first irq byte normally 0x08 */
39 /* second irq byte base 0x7d + below */
40 /* third irq byte base 0x94 + below */
41 /* fourth irq byte normally 0xee */
43 /* second interrupt byte */
44 #define CH341_MULT_STAT 0x04 /* multiple status since last interrupt event */
46 /* status returned in third interrupt answer byte, inverted in data
48 #define CH341_BIT_CTS 0x01
49 #define CH341_BIT_DSR 0x02
50 #define CH341_BIT_RI 0x04
51 #define CH341_BIT_DCD 0x08
52 #define CH341_BITS_MODEM_STAT 0x0f /* all bits */
54 /*******************************/
55 /* baudrate calculation factor */
56 /*******************************/
57 #define CH341_BAUDBASE_FACTOR 1532620800
58 #define CH341_BAUDBASE_DIVMAX 3
62 static struct usb_device_id id_table [] = {
63 { USB_DEVICE(0x4348, 0x5523) },
64 { USB_DEVICE(0x1a86, 0x7523) },
67 MODULE_DEVICE_TABLE(usb, id_table);
69 struct ch341_private {
70 spinlock_t lock; /* access lock */
71 wait_queue_head_t delta_msr_wait; /* wait queue for modem status */
72 unsigned baud_rate; /* set baud rate */
73 u8 line_control; /* set line control value RTS/DTR */
74 u8 line_status; /* active status of modem control inputs */
75 u8 multi_status_change; /* status changed multiple since last call */
76 struct i2c_adapter adapter;
77 struct i2c_algo_bit_data algorithm;
78 struct usb_device *dev;
81 static int ch341_control_out(struct usb_device *dev, u8 request,
85 dbg("ch341_control_out(%02x,%02x,%04x,%04x)", USB_DIR_OUT|0x40,
86 (int)request, (int)value, (int)index);
88 r = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
89 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
90 value, index, NULL, 0, DEFAULT_TIMEOUT);
95 static int ch341_control_in(struct usb_device *dev,
96 u8 request, u16 value, u16 index,
97 char *buf, unsigned bufsize)
100 dbg("ch341_control_in(%02x,%02x,%04x,%04x,%p,%u)", USB_DIR_IN|0x40,
101 (int)request, (int)value, (int)index, buf, (int)bufsize);
103 r = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
104 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
105 value, index, buf, bufsize, DEFAULT_TIMEOUT);
109 static int ch341_set_baudrate(struct usb_device *dev,
110 struct ch341_private *priv)
114 unsigned long factor;
117 dbg("ch341_set_baudrate(%d)", priv->baud_rate);
119 if (!priv->baud_rate)
121 factor = (CH341_BAUDBASE_FACTOR / priv->baud_rate);
122 divisor = CH341_BAUDBASE_DIVMAX;
124 while ((factor > 0xfff0) && divisor) {
132 factor = 0x10000 - factor;
133 a = (factor & 0xff00) | divisor;
136 r = ch341_control_out(dev, 0x9a, 0x1312, a);
138 r = ch341_control_out(dev, 0x9a, 0x0f2c, b);
143 static int ch341_set_handshake(struct usb_device *dev, u8 control)
145 dbg("ch341_set_handshake(0x%02x)", control);
146 return ch341_control_out(dev, 0xa4, ~control, 0);
149 static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv)
153 const unsigned size = 8;
156 dbg("ch341_get_status()");
158 buffer = kmalloc(size, GFP_KERNEL);
162 r = ch341_control_in(dev, 0x95, 0x0706, 0, buffer, size);
166 /* setup the private status if available */
169 spin_lock_irqsave(&priv->lock, flags);
170 priv->line_status = (~(*buffer)) & CH341_BITS_MODEM_STAT;
171 priv->multi_status_change = 0;
172 spin_unlock_irqrestore(&priv->lock, flags);
180 static void i2c_ch341_setsda(void *data, int state)
182 struct ch341_private *priv = data;
187 priv->line_control |= CH341_BIT_RTS;
189 priv->line_control &= ~CH341_BIT_RTS;
192 spin_lock_irqsave(&priv->lock, flags);
193 control = priv->line_control;
194 spin_unlock_irqrestore(&priv->lock, flags);
196 ch341_set_handshake(priv->dev, control);
199 static void i2c_ch341_setscl(void *data, int state)
202 struct ch341_private *priv = data;
207 priv->line_control |= CH341_BIT_DTR;
209 priv->line_control &= ~CH341_BIT_DTR;
212 spin_lock_irqsave(&priv->lock, flags);
213 control = priv->line_control;
214 spin_unlock_irqrestore(&priv->lock, flags);
216 ch341_set_handshake(priv->dev, control);
219 static int i2c_ch341_getsda(void *data)
221 struct ch341_private *priv = data;
226 spin_lock_irqsave(&priv->lock, flags);
227 mcr = priv->line_control;
228 status = priv->line_status;
229 spin_unlock_irqrestore(&priv->lock, flags);
231 return (status & CH341_BIT_CTS);
234 static int i2c_ch341_getscl(void *data)
236 struct ch341_private *priv = data;
241 spin_lock_irqsave(&priv->lock, flags);
242 mcr = priv->line_control;
243 status = priv->line_status;
244 spin_unlock_irqrestore(&priv->lock, flags);
246 return (mcr & CH341_BIT_DTR);
249 static struct i2c_algo_bit_data i2c_ch341_algo = {
250 .setsda = i2c_ch341_setsda,
251 .setscl = i2c_ch341_setscl,
252 .getsda = i2c_ch341_getsda,
253 .getscl = i2c_ch341_getscl,
258 /* -------------------------------------------------------------------------- */
260 static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
264 const unsigned size = 8;
266 dbg("ch341_configure()");
268 buffer = kmalloc(size, GFP_KERNEL);
272 /* expect two bytes 0x27 0x00 */
273 r = ch341_control_in(dev, 0x5f, 0, 0, buffer, size);
277 r = ch341_control_out(dev, 0xa1, 0, 0);
281 r = ch341_set_baudrate(dev, priv);
285 /* expect two bytes 0x56 0x00 */
286 r = ch341_control_in(dev, 0x95, 0x2518, 0, buffer, size);
290 r = ch341_control_out(dev, 0x9a, 0x2518, 0x0050);
294 /* expect 0xff 0xee */
295 r = ch341_get_status(dev, priv);
299 r = ch341_control_out(dev, 0xa1, 0x501f, 0xd90a);
303 r = ch341_set_baudrate(dev, priv);
307 r = ch341_set_handshake(dev, priv->line_control);
311 /* expect 0x9f 0xee */
312 r = ch341_get_status(dev, priv);
318 /* allocate private data */
319 static int ch341_attach(struct usb_serial *serial)
321 struct ch341_private *priv;
324 dbg("ch341_attach()");
327 priv = kzalloc(sizeof(struct ch341_private), GFP_KERNEL);
331 spin_lock_init(&priv->lock);
332 init_waitqueue_head(&priv->delta_msr_wait);
333 priv->baud_rate = DEFAULT_BAUD_RATE;
334 priv->line_control = CH341_BIT_RTS | CH341_BIT_DTR;
336 priv->dev = serial->dev;
337 priv->adapter.owner = THIS_MODULE;
338 strlcpy(priv->adapter.name, "CH341 I2C adapter", sizeof(priv->adapter.name));
339 priv->adapter.class = I2C_CLASS_HWMON;
340 priv->adapter.algo_data = &priv->algorithm;
341 priv->adapter.dev.parent = &priv->dev->dev;
342 priv->algorithm = i2c_ch341_algo;
343 priv->algorithm.data = priv;
345 r = ch341_configure(serial->dev, priv);
349 r = i2c_bit_add_bus(&priv->adapter);
354 usb_set_serial_port_data(serial->port[0], priv);
356 dev_info(&priv->adapter.dev, "connected ch341 device\n");
364 static void ch341_close(struct usb_serial_port *port, struct file *filp)
366 struct ch341_private *priv = usb_get_serial_port_data(port);
368 unsigned int c_cflag;
370 dbg("%s - port %d", __func__, port->number);
372 /* shutdown our urbs */
373 dbg("%s - shutting down urbs", __func__);
374 usb_kill_urb(port->write_urb);
375 usb_kill_urb(port->read_urb);
376 usb_kill_urb(port->interrupt_in_urb);
379 c_cflag = port->tty->termios->c_cflag;
380 if (c_cflag & HUPCL) {
381 /* drop DTR and RTS */
382 spin_lock_irqsave(&priv->lock, flags);
383 priv->line_control = 0;
384 spin_unlock_irqrestore(&priv->lock, flags);
385 ch341_set_handshake(port->serial->dev, 0);
388 wake_up_interruptible(&priv->delta_msr_wait);
392 /* open this device, set default parameters */
393 static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port,
396 struct usb_serial *serial = port->serial;
397 struct ch341_private *priv = usb_get_serial_port_data(serial->port[0]);
402 priv->baud_rate = DEFAULT_BAUD_RATE;
403 priv->line_control = CH341_BIT_RTS | CH341_BIT_DTR;
405 r = ch341_configure(serial->dev, priv);
409 r = ch341_set_handshake(serial->dev, priv->line_control);
413 r = ch341_set_baudrate(serial->dev, priv);
417 dbg("%s - submitting interrupt urb", __func__);
418 port->interrupt_in_urb->dev = serial->dev;
419 r = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
421 dev_err(&port->dev, "%s - failed submitting interrupt urb,"
422 " error %d\n", __func__, r);
423 ch341_close(port, NULL);
427 r = usb_serial_generic_open(tty, port, filp);
432 /* Old_termios contains the original termios settings and
433 * tty->termios contains the new setting to be used.
435 static void ch341_set_termios(struct tty_struct *tty,
436 struct usb_serial_port *port, struct ktermios *old_termios)
438 struct ch341_private *priv = usb_get_serial_port_data(port);
442 dbg("ch341_set_termios()");
444 if (!tty || !tty->termios)
447 baud_rate = tty_get_baud_rate(tty);
449 priv->baud_rate = baud_rate;
452 spin_lock_irqsave(&priv->lock, flags);
453 priv->line_control |= (CH341_BIT_DTR | CH341_BIT_RTS);
454 spin_unlock_irqrestore(&priv->lock, flags);
455 ch341_set_baudrate(port->serial->dev, priv);
457 spin_lock_irqsave(&priv->lock, flags);
458 priv->line_control &= ~(CH341_BIT_DTR | CH341_BIT_RTS);
459 spin_unlock_irqrestore(&priv->lock, flags);
462 ch341_set_handshake(port->serial->dev, priv->line_control);
465 * (cflag & CSIZE) : data bits [5, 8]
466 * (cflag & PARENB) : parity {NONE, EVEN, ODD}
467 * (cflag & CSTOPB) : stop bits [1, 2]
471 static int ch341_tiocmset(struct usb_serial_port *port, struct file *file,
472 unsigned int set, unsigned int clear)
474 struct ch341_private *priv = usb_get_serial_port_data(port);
478 spin_lock_irqsave(&priv->lock, flags);
480 priv->line_control |= CH341_BIT_RTS;
482 priv->line_control |= CH341_BIT_DTR;
483 if (clear & TIOCM_RTS)
484 priv->line_control &= ~CH341_BIT_RTS;
485 if (clear & TIOCM_DTR)
486 priv->line_control &= ~CH341_BIT_DTR;
487 control = priv->line_control;
488 spin_unlock_irqrestore(&priv->lock, flags);
490 return ch341_set_handshake(port->serial->dev, control);
493 static void ch341_read_int_callback(struct urb *urb)
495 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
496 unsigned char *data = urb->transfer_buffer;
497 unsigned int actual_length = urb->actual_length;
500 dbg("%s (%d)", __func__, port->number);
502 switch (urb->status) {
509 /* this urb is terminated, clean up */
510 dbg("%s - urb shutting down with status: %d", __func__,
514 dbg("%s - nonzero urb status received: %d", __func__,
519 usb_serial_debug_data(debug, &port->dev, __func__,
520 urb->actual_length, urb->transfer_buffer);
522 if (actual_length >= 4) {
523 struct ch341_private *priv = usb_get_serial_port_data(port);
526 spin_lock_irqsave(&priv->lock, flags);
527 priv->line_status = (~(data[2])) & CH341_BITS_MODEM_STAT;
528 if ((data[1] & CH341_MULT_STAT))
529 priv->multi_status_change = 1;
530 spin_unlock_irqrestore(&priv->lock, flags);
531 wake_up_interruptible(&priv->delta_msr_wait);
535 status = usb_submit_urb(urb, GFP_ATOMIC);
537 dev_err(&urb->dev->dev,
538 "%s - usb_submit_urb failed with result %d\n",
542 static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
544 struct ch341_private *priv = usb_get_serial_port_data(port);
551 spin_lock_irqsave(&priv->lock, flags);
552 prevstatus = priv->line_status;
553 priv->multi_status_change = 0;
554 spin_unlock_irqrestore(&priv->lock, flags);
556 while (!multi_change) {
557 interruptible_sleep_on(&priv->delta_msr_wait);
558 /* see if a signal did it */
559 if (signal_pending(current))
562 spin_lock_irqsave(&priv->lock, flags);
563 status = priv->line_status;
564 multi_change = priv->multi_status_change;
565 spin_unlock_irqrestore(&priv->lock, flags);
567 changed = prevstatus ^ status;
569 if (((arg & TIOCM_RNG) && (changed & CH341_BIT_RI)) ||
570 ((arg & TIOCM_DSR) && (changed & CH341_BIT_DSR)) ||
571 ((arg & TIOCM_CD) && (changed & CH341_BIT_DCD)) ||
572 ((arg & TIOCM_CTS) && (changed & CH341_BIT_CTS))) {
581 static int ch341_ioctl(struct usb_serial_port *port, struct file *file,
582 unsigned int cmd, unsigned long arg)
584 dbg("%s (%d) cmd = 0x%04x", __func__, port->number, cmd);
588 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
589 return wait_modem_info(port, arg);
592 dbg("%s not supported = 0x%04x", __func__, cmd);
599 static int ch341_tiocmget(struct usb_serial_port *port, struct file *file)
601 struct ch341_private *priv = usb_get_serial_port_data(port);
607 dbg("%s (%d)", __func__, port->number);
609 spin_lock_irqsave(&priv->lock, flags);
610 mcr = priv->line_control;
611 status = priv->line_status;
612 spin_unlock_irqrestore(&priv->lock, flags);
614 result = ((mcr & CH341_BIT_DTR) ? TIOCM_DTR : 0)
615 | ((mcr & CH341_BIT_RTS) ? TIOCM_RTS : 0)
616 | ((status & CH341_BIT_CTS) ? TIOCM_CTS : 0)
617 | ((status & CH341_BIT_DSR) ? TIOCM_DSR : 0)
618 | ((status & CH341_BIT_RI) ? TIOCM_RI : 0)
619 | ((status & CH341_BIT_DCD) ? TIOCM_CD : 0);
621 dbg("%s - result = %x", __func__, result);
626 static void ch341_shutdown(struct usb_serial *serial)
628 struct ch341_private *priv = usb_get_serial_port_data(serial->port[0]);
630 dbg("%s", __FUNCTION__);
633 i2c_del_adapter(&priv->adapter);
637 static u32 ch341_functionality(struct i2c_adapter *adap)
639 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
643 static struct usb_driver ch341_driver = {
645 .probe = usb_serial_probe,
646 .disconnect = usb_serial_disconnect,
647 .id_table = id_table,
651 static struct usb_serial_driver ch341_device = {
653 .owner = THIS_MODULE,
654 .name = "ch341-uart",
656 .id_table = id_table,
657 .usb_driver = &ch341_driver,
658 .num_interrupt_in = 1,
663 .close = ch341_close,
664 .ioctl = ch341_ioctl,
665 .set_termios = ch341_set_termios,
666 .tiocmget = ch341_tiocmget,
667 .tiocmset = ch341_tiocmset,
668 .read_int_callback = ch341_read_int_callback,
669 .attach = ch341_attach,
670 .shutdown = ch341_shutdown,
673 static int __init ch341_init(void)
677 retval = usb_serial_register(&ch341_device);
680 retval = usb_register(&ch341_driver);
682 usb_serial_deregister(&ch341_device);
686 static void __exit ch341_exit(void)
688 usb_deregister(&ch341_driver);
689 usb_serial_deregister(&ch341_device);
692 module_init(ch341_init);
693 module_exit(ch341_exit);
694 MODULE_LICENSE("GPL");
696 module_param(debug, bool, S_IRUGO | S_IWUSR);
697 MODULE_PARM_DESC(debug, "Debug enabled or not");