2 * Copyright 2007, Frank A Kingswood <frank@kingswood-consulting.co.uk>
4 * ch341.c implements a serial port driver for the Winchiphead CH341.
6 * The CH341 device can be used to implement an RS232 asynchronous
7 * serial port, an IEEE-1284 parallel printer port or a memory-like
8 * interface. In all cases the CH341 supports an I2C interface as well.
9 * This driver only supports the asynchronous serial interface.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License version
13 * 2 as published by the Free Software Foundation.
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/tty.h>
19 #include <linux/module.h>
20 #include <linux/usb.h>
21 #include <linux/usb/serial.h>
22 #include <linux/serial.h>
24 #define DEFAULT_BAUD_RATE 2400
25 #define DEFAULT_TIMEOUT 1000
29 static struct usb_device_id id_table [] = {
30 { USB_DEVICE(0x4348, 0x5523) },
33 MODULE_DEVICE_TABLE(usb, id_table);
35 struct ch341_private {
41 static int ch341_control_out(struct usb_device *dev, u8 request,
45 dbg("ch341_control_out(%02x,%02x,%04x,%04x)", USB_DIR_OUT|0x40,
46 (int)request, (int)value, (int)index);
48 r = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
49 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
50 value, index, NULL, 0, DEFAULT_TIMEOUT);
55 static int ch341_control_in(struct usb_device *dev,
56 u8 request, u16 value, u16 index,
57 char *buf, unsigned bufsize)
60 dbg("ch341_control_in(%02x,%02x,%04x,%04x,%p,%u)", USB_DIR_IN|0x40,
61 (int)request, (int)value, (int)index, buf, (int)bufsize);
63 r = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
64 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
65 value, index, buf, bufsize, DEFAULT_TIMEOUT);
69 int ch341_set_baudrate(struct usb_device *dev, struct ch341_private *priv)
74 dbg("ch341_set_baudrate(%d)", priv->baud_rate);
75 switch (priv->baud_rate) {
104 r = ch341_control_out(dev, 0x9a, 0x1312, a);
106 r = ch341_control_out(dev, 0x9a, 0x0f2c, b);
111 int ch341_set_handshake(struct usb_device *dev, struct ch341_private *priv)
113 dbg("ch341_set_handshake(%d,%d)", priv->dtr, priv->rts);
114 return ch341_control_out(dev, 0xa4,
115 ~((priv->dtr?1<<5:0)|(priv->rts?1<<6:0)), 0);
118 int ch341_get_status(struct usb_device *dev)
122 const unsigned size = 8;
124 dbg("ch341_get_status()");
126 buffer = kmalloc(size, GFP_KERNEL);
130 r = ch341_control_in(dev, 0x95, 0x0706, 0, buffer, size);
134 /* Not having the datasheet for the CH341, we ignore the bytes returned
135 * from the device. Return error if the device did not respond in time.
143 /* -------------------------------------------------------------------------- */
145 int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
149 const unsigned size = 8;
151 dbg("ch341_configure()");
153 buffer = kmalloc(size, GFP_KERNEL);
157 /* expect two bytes 0x27 0x00 */
158 r = ch341_control_in(dev, 0x5f, 0, 0, buffer, size);
162 r = ch341_control_out(dev, 0xa1, 0, 0);
166 r = ch341_set_baudrate(dev, priv);
170 /* expect two bytes 0x56 0x00 */
171 r = ch341_control_in(dev, 0x95, 0x2518, 0, buffer, size);
175 r = ch341_control_out(dev, 0x9a, 0x2518, 0x0050);
179 /* expect 0xff 0xee */
180 r = ch341_get_status(dev);
184 r = ch341_control_out(dev, 0xa1, 0x501f, 0xd90a);
188 r = ch341_set_baudrate(dev, priv);
192 r = ch341_set_handshake(dev, priv);
196 /* expect 0x9f 0xee */
197 r = ch341_get_status(dev);
203 /* allocate private data */
204 static int ch341_attach(struct usb_serial *serial)
206 struct ch341_private *priv;
209 dbg("ch341_attach()");
212 priv = kzalloc(sizeof(struct ch341_private), GFP_KERNEL);
216 priv->baud_rate = DEFAULT_BAUD_RATE;
220 r = ch341_configure(serial->dev, priv);
224 usb_set_serial_port_data(serial->port[0], priv);
231 /* open this device, set default parameters */
232 static int ch341_open(struct usb_serial_port *port, struct file *filp)
234 struct usb_serial *serial = port->serial;
235 struct ch341_private *priv = usb_get_serial_port_data(serial->port[0]);
240 priv->baud_rate = DEFAULT_BAUD_RATE;
244 r = ch341_configure(serial->dev, priv);
248 r = ch341_set_handshake(serial->dev, priv);
252 r = ch341_set_baudrate(serial->dev, priv);
256 r = usb_serial_generic_open(port, filp);
261 /* Old_termios contains the original termios settings and
262 * tty->termios contains the new setting to be used.
264 static void ch341_set_termios(struct usb_serial_port *port,
265 struct ktermios *old_termios)
267 struct ch341_private *priv = usb_get_serial_port_data(port);
268 struct tty_struct *tty = port->tty;
271 dbg("ch341_set_termios()");
273 if (!tty || !tty->termios)
276 baud_rate = tty_get_baud_rate(tty);
285 priv->baud_rate = baud_rate;
288 dbg("Rate %d not supported, using %d",
289 baud_rate, DEFAULT_BAUD_RATE);
290 priv->baud_rate = DEFAULT_BAUD_RATE;
293 ch341_set_baudrate(port->serial->dev, priv);
296 * (cflag & CSIZE) : data bits [5, 8]
297 * (cflag & PARENB) : parity {NONE, EVEN, ODD}
298 * (cflag & CSTOPB) : stop bits [1, 2]
302 static struct usb_driver ch341_driver = {
304 .probe = usb_serial_probe,
305 .disconnect = usb_serial_disconnect,
306 .id_table = id_table,
310 static struct usb_serial_driver ch341_device = {
312 .owner = THIS_MODULE,
313 .name = "ch341-uart",
315 .id_table = id_table,
316 .usb_driver = &ch341_driver,
317 .num_interrupt_in = NUM_DONT_CARE,
322 .set_termios = ch341_set_termios,
323 .attach = ch341_attach,
326 static int __init ch341_init(void)
330 retval = usb_serial_register(&ch341_device);
333 retval = usb_register(&ch341_driver);
335 usb_serial_deregister(&ch341_device);
339 static void __exit ch341_exit(void)
341 usb_deregister(&ch341_driver);
342 usb_serial_deregister(&ch341_device);
345 module_init(ch341_init);
346 module_exit(ch341_exit);
347 MODULE_LICENSE("GPL");
349 module_param(debug, bool, S_IRUGO | S_IWUSR);
350 MODULE_PARM_DESC(debug, "Debug enabled or not");