aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/serial/ch341.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/serial/ch341.c')
-rw-r--r--drivers/usb/serial/ch341.c396
1 files changed, 324 insertions, 72 deletions
diff --git a/drivers/usb/serial/ch341.c b/drivers/usb/serial/ch341.c
index f61e3ca64305..ab4cc277aa65 100644
--- a/drivers/usb/serial/ch341.c
+++ b/drivers/usb/serial/ch341.c
@@ -1,5 +1,7 @@
1/* 1/*
2 * Copyright 2007, Frank A Kingswood <frank@kingswood-consulting.co.uk> 2 * Copyright 2007, Frank A Kingswood <frank@kingswood-consulting.co.uk>
3 * Copyright 2007, Werner Cornelius <werner@cornelius-consult.de>
4 * Copyright 2009, Boris Hajduk <boris@hajduk.org>
3 * 5 *
4 * ch341.c implements a serial port driver for the Winchiphead CH341. 6 * ch341.c implements a serial port driver for the Winchiphead CH341.
5 * 7 *
@@ -21,9 +23,39 @@
21#include <linux/usb/serial.h> 23#include <linux/usb/serial.h>
22#include <linux/serial.h> 24#include <linux/serial.h>
23 25
24#define DEFAULT_BAUD_RATE 2400 26#define DEFAULT_BAUD_RATE 9600
25#define DEFAULT_TIMEOUT 1000 27#define DEFAULT_TIMEOUT 1000
26 28
29/* flags for IO-Bits */
30#define CH341_BIT_RTS (1 << 6)
31#define CH341_BIT_DTR (1 << 5)
32
33/******************************/
34/* interrupt pipe definitions */
35/******************************/
36/* always 4 interrupt bytes */
37/* first irq byte normally 0x08 */
38/* second irq byte base 0x7d + below */
39/* third irq byte base 0x94 + below */
40/* fourth irq byte normally 0xee */
41
42/* second interrupt byte */
43#define CH341_MULT_STAT 0x04 /* multiple status since last interrupt event */
44
45/* status returned in third interrupt answer byte, inverted in data
46 from irq */
47#define CH341_BIT_CTS 0x01
48#define CH341_BIT_DSR 0x02
49#define CH341_BIT_RI 0x04
50#define CH341_BIT_DCD 0x08
51#define CH341_BITS_MODEM_STAT 0x0f /* all bits */
52
53/*******************************/
54/* baudrate calculation factor */
55/*******************************/
56#define CH341_BAUDBASE_FACTOR 1532620800
57#define CH341_BAUDBASE_DIVMAX 3
58
27static int debug; 59static int debug;
28 60
29static struct usb_device_id id_table [] = { 61static struct usb_device_id id_table [] = {
@@ -34,9 +66,12 @@ static struct usb_device_id id_table [] = {
34MODULE_DEVICE_TABLE(usb, id_table); 66MODULE_DEVICE_TABLE(usb, id_table);
35 67
36struct ch341_private { 68struct ch341_private {
37 unsigned baud_rate; 69 spinlock_t lock; /* access lock */
38 u8 dtr; 70 wait_queue_head_t delta_msr_wait; /* wait queue for modem status */
39 u8 rts; 71 unsigned baud_rate; /* set baud rate */
72 u8 line_control; /* set line control value RTS/DTR */
73 u8 line_status; /* active status of modem control inputs */
74 u8 multi_status_change; /* status changed multiple since last call */
40}; 75};
41 76
42static int ch341_control_out(struct usb_device *dev, u8 request, 77static int ch341_control_out(struct usb_device *dev, u8 request,
@@ -72,37 +107,28 @@ static int ch341_set_baudrate(struct usb_device *dev,
72{ 107{
73 short a, b; 108 short a, b;
74 int r; 109 int r;
110 unsigned long factor;
111 short divisor;
75 112
76 dbg("ch341_set_baudrate(%d)", priv->baud_rate); 113 dbg("ch341_set_baudrate(%d)", priv->baud_rate);
77 switch (priv->baud_rate) { 114
78 case 2400: 115 if (!priv->baud_rate)
79 a = 0xd901;
80 b = 0x0038;
81 break;
82 case 4800:
83 a = 0x6402;
84 b = 0x001f;
85 break;
86 case 9600:
87 a = 0xb202;
88 b = 0x0013;
89 break;
90 case 19200:
91 a = 0xd902;
92 b = 0x000d;
93 break;
94 case 38400:
95 a = 0x6403;
96 b = 0x000a;
97 break;
98 case 115200:
99 a = 0xcc03;
100 b = 0x0008;
101 break;
102 default:
103 return -EINVAL; 116 return -EINVAL;
117 factor = (CH341_BAUDBASE_FACTOR / priv->baud_rate);
118 divisor = CH341_BAUDBASE_DIVMAX;
119
120 while ((factor > 0xfff0) && divisor) {
121 factor >>= 3;
122 divisor--;
104 } 123 }
105 124
125 if (factor > 0xfff0)
126 return -EINVAL;
127
128 factor = 0x10000 - factor;
129 a = (factor & 0xff00) | divisor;
130 b = factor & 0xff;
131
106 r = ch341_control_out(dev, 0x9a, 0x1312, a); 132 r = ch341_control_out(dev, 0x9a, 0x1312, a);
107 if (!r) 133 if (!r)
108 r = ch341_control_out(dev, 0x9a, 0x0f2c, b); 134 r = ch341_control_out(dev, 0x9a, 0x0f2c, b);
@@ -110,19 +136,18 @@ static int ch341_set_baudrate(struct usb_device *dev,
110 return r; 136 return r;
111} 137}
112 138
113static int ch341_set_handshake(struct usb_device *dev, 139static int ch341_set_handshake(struct usb_device *dev, u8 control)
114 struct ch341_private *priv)
115{ 140{
116 dbg("ch341_set_handshake(%d,%d)", priv->dtr, priv->rts); 141 dbg("ch341_set_handshake(0x%02x)", control);
117 return ch341_control_out(dev, 0xa4, 142 return ch341_control_out(dev, 0xa4, ~control, 0);
118 ~((priv->dtr?1<<5:0)|(priv->rts?1<<6:0)), 0);
119} 143}
120 144
121static int ch341_get_status(struct usb_device *dev) 145static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv)
122{ 146{
123 char *buffer; 147 char *buffer;
124 int r; 148 int r;
125 const unsigned size = 8; 149 const unsigned size = 8;
150 unsigned long flags;
126 151
127 dbg("ch341_get_status()"); 152 dbg("ch341_get_status()");
128 153
@@ -134,10 +159,15 @@ static int ch341_get_status(struct usb_device *dev)
134 if (r < 0) 159 if (r < 0)
135 goto out; 160 goto out;
136 161
137 /* Not having the datasheet for the CH341, we ignore the bytes returned 162 /* setup the private status if available */
138 * from the device. Return error if the device did not respond in time. 163 if (r == 2) {
139 */ 164 r = 0;
140 r = 0; 165 spin_lock_irqsave(&priv->lock, flags);
166 priv->line_status = (~(*buffer)) & CH341_BITS_MODEM_STAT;
167 priv->multi_status_change = 0;
168 spin_unlock_irqrestore(&priv->lock, flags);
169 } else
170 r = -EPROTO;
141 171
142out: kfree(buffer); 172out: kfree(buffer);
143 return r; 173 return r;
@@ -180,7 +210,7 @@ static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
180 goto out; 210 goto out;
181 211
182 /* expect 0xff 0xee */ 212 /* expect 0xff 0xee */
183 r = ch341_get_status(dev); 213 r = ch341_get_status(dev, priv);
184 if (r < 0) 214 if (r < 0)
185 goto out; 215 goto out;
186 216
@@ -192,12 +222,12 @@ static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
192 if (r < 0) 222 if (r < 0)
193 goto out; 223 goto out;
194 224
195 r = ch341_set_handshake(dev, priv); 225 r = ch341_set_handshake(dev, priv->line_control);
196 if (r < 0) 226 if (r < 0)
197 goto out; 227 goto out;
198 228
199 /* expect 0x9f 0xee */ 229 /* expect 0x9f 0xee */
200 r = ch341_get_status(dev); 230 r = ch341_get_status(dev, priv);
201 231
202out: kfree(buffer); 232out: kfree(buffer);
203 return r; 233 return r;
@@ -216,9 +246,10 @@ static int ch341_attach(struct usb_serial *serial)
216 if (!priv) 246 if (!priv)
217 return -ENOMEM; 247 return -ENOMEM;
218 248
249 spin_lock_init(&priv->lock);
250 init_waitqueue_head(&priv->delta_msr_wait);
219 priv->baud_rate = DEFAULT_BAUD_RATE; 251 priv->baud_rate = DEFAULT_BAUD_RATE;
220 priv->dtr = 1; 252 priv->line_control = CH341_BIT_RTS | CH341_BIT_DTR;
221 priv->rts = 1;
222 253
223 r = ch341_configure(serial->dev, priv); 254 r = ch341_configure(serial->dev, priv);
224 if (r < 0) 255 if (r < 0)
@@ -231,6 +262,35 @@ error: kfree(priv);
231 return r; 262 return r;
232} 263}
233 264
265static void ch341_close(struct tty_struct *tty, struct usb_serial_port *port,
266 struct file *filp)
267{
268 struct ch341_private *priv = usb_get_serial_port_data(port);
269 unsigned long flags;
270 unsigned int c_cflag;
271
272 dbg("%s - port %d", __func__, port->number);
273
274 /* shutdown our urbs */
275 dbg("%s - shutting down urbs", __func__);
276 usb_kill_urb(port->write_urb);
277 usb_kill_urb(port->read_urb);
278 usb_kill_urb(port->interrupt_in_urb);
279
280 if (tty) {
281 c_cflag = tty->termios->c_cflag;
282 if (c_cflag & HUPCL) {
283 /* drop DTR and RTS */
284 spin_lock_irqsave(&priv->lock, flags);
285 priv->line_control = 0;
286 spin_unlock_irqrestore(&priv->lock, flags);
287 ch341_set_handshake(port->serial->dev, 0);
288 }
289 }
290 wake_up_interruptible(&priv->delta_msr_wait);
291}
292
293
234/* open this device, set default parameters */ 294/* open this device, set default parameters */
235static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port, 295static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port,
236 struct file *filp) 296 struct file *filp)
@@ -242,14 +302,13 @@ static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port,
242 dbg("ch341_open()"); 302 dbg("ch341_open()");
243 303
244 priv->baud_rate = DEFAULT_BAUD_RATE; 304 priv->baud_rate = DEFAULT_BAUD_RATE;
245 priv->dtr = 1; 305 priv->line_control = CH341_BIT_RTS | CH341_BIT_DTR;
246 priv->rts = 1;
247 306
248 r = ch341_configure(serial->dev, priv); 307 r = ch341_configure(serial->dev, priv);
249 if (r) 308 if (r)
250 goto out; 309 goto out;
251 310
252 r = ch341_set_handshake(serial->dev, priv); 311 r = ch341_set_handshake(serial->dev, priv->line_control);
253 if (r) 312 if (r)
254 goto out; 313 goto out;
255 314
@@ -257,6 +316,16 @@ static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port,
257 if (r) 316 if (r)
258 goto out; 317 goto out;
259 318
319 dbg("%s - submitting interrupt urb", __func__);
320 port->interrupt_in_urb->dev = serial->dev;
321 r = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
322 if (r) {
323 dev_err(&port->dev, "%s - failed submitting interrupt urb,"
324 " error %d\n", __func__, r);
325 ch341_close(tty, port, NULL);
326 return -EPROTO;
327 }
328
260 r = usb_serial_generic_open(tty, port, filp); 329 r = usb_serial_generic_open(tty, port, filp);
261 330
262out: return r; 331out: return r;
@@ -270,46 +339,224 @@ static void ch341_set_termios(struct tty_struct *tty,
270{ 339{
271 struct ch341_private *priv = usb_get_serial_port_data(port); 340 struct ch341_private *priv = usb_get_serial_port_data(port);
272 unsigned baud_rate; 341 unsigned baud_rate;
342 unsigned long flags;
273 343
274 dbg("ch341_set_termios()"); 344 dbg("ch341_set_termios()");
275 345
346 if (!tty || !tty->termios)
347 return;
348
276 baud_rate = tty_get_baud_rate(tty); 349 baud_rate = tty_get_baud_rate(tty);
277 350
278 switch (baud_rate) { 351 priv->baud_rate = baud_rate;
279 case 2400: 352
280 case 4800: 353 if (baud_rate) {
281 case 9600: 354 spin_lock_irqsave(&priv->lock, flags);
282 case 19200: 355 priv->line_control |= (CH341_BIT_DTR | CH341_BIT_RTS);
283 case 38400: 356 spin_unlock_irqrestore(&priv->lock, flags);
284 case 115200: 357 ch341_set_baudrate(port->serial->dev, priv);
285 priv->baud_rate = baud_rate; 358 } else {
286 break; 359 spin_lock_irqsave(&priv->lock, flags);
287 default: 360 priv->line_control &= ~(CH341_BIT_DTR | CH341_BIT_RTS);
288 dbg("Rate %d not supported, using %d", 361 spin_unlock_irqrestore(&priv->lock, flags);
289 baud_rate, DEFAULT_BAUD_RATE);
290 priv->baud_rate = DEFAULT_BAUD_RATE;
291 } 362 }
292 363
293 ch341_set_baudrate(port->serial->dev, priv); 364 ch341_set_handshake(port->serial->dev, priv->line_control);
294 365
295 /* Unimplemented: 366 /* Unimplemented:
296 * (cflag & CSIZE) : data bits [5, 8] 367 * (cflag & CSIZE) : data bits [5, 8]
297 * (cflag & PARENB) : parity {NONE, EVEN, ODD} 368 * (cflag & PARENB) : parity {NONE, EVEN, ODD}
298 * (cflag & CSTOPB) : stop bits [1, 2] 369 * (cflag & CSTOPB) : stop bits [1, 2]
299 */ 370 */
371}
372
373static int ch341_tiocmset(struct tty_struct *tty, struct file *file,
374 unsigned int set, unsigned int clear)
375{
376 struct usb_serial_port *port = tty->driver_data;
377 struct ch341_private *priv = usb_get_serial_port_data(port);
378 unsigned long flags;
379 u8 control;
380
381 spin_lock_irqsave(&priv->lock, flags);
382 if (set & TIOCM_RTS)
383 priv->line_control |= CH341_BIT_RTS;
384 if (set & TIOCM_DTR)
385 priv->line_control |= CH341_BIT_DTR;
386 if (clear & TIOCM_RTS)
387 priv->line_control &= ~CH341_BIT_RTS;
388 if (clear & TIOCM_DTR)
389 priv->line_control &= ~CH341_BIT_DTR;
390 control = priv->line_control;
391 spin_unlock_irqrestore(&priv->lock, flags);
392
393 return ch341_set_handshake(port->serial->dev, control);
394}
395
396static void ch341_read_int_callback(struct urb *urb)
397{
398 struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
399 unsigned char *data = urb->transfer_buffer;
400 unsigned int actual_length = urb->actual_length;
401 int status;
402
403 dbg("%s (%d)", __func__, port->number);
404
405 switch (urb->status) {
406 case 0:
407 /* success */
408 break;
409 case -ECONNRESET:
410 case -ENOENT:
411 case -ESHUTDOWN:
412 /* this urb is terminated, clean up */
413 dbg("%s - urb shutting down with status: %d", __func__,
414 urb->status);
415 return;
416 default:
417 dbg("%s - nonzero urb status received: %d", __func__,
418 urb->status);
419 goto exit;
420 }
300 421
301 /* Copy back the old hardware settings */ 422 usb_serial_debug_data(debug, &port->dev, __func__,
302 tty_termios_copy_hw(tty->termios, old_termios); 423 urb->actual_length, urb->transfer_buffer);
303 /* And re-encode with the new baud */ 424
304 tty_encode_baud_rate(tty, baud_rate, baud_rate); 425 if (actual_length >= 4) {
426 struct ch341_private *priv = usb_get_serial_port_data(port);
427 unsigned long flags;
428
429 spin_lock_irqsave(&priv->lock, flags);
430 priv->line_status = (~(data[2])) & CH341_BITS_MODEM_STAT;
431 if ((data[1] & CH341_MULT_STAT))
432 priv->multi_status_change = 1;
433 spin_unlock_irqrestore(&priv->lock, flags);
434 wake_up_interruptible(&priv->delta_msr_wait);
435 }
436
437exit:
438 status = usb_submit_urb(urb, GFP_ATOMIC);
439 if (status)
440 dev_err(&urb->dev->dev,
441 "%s - usb_submit_urb failed with result %d\n",
442 __func__, status);
443}
444
445static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
446{
447 struct ch341_private *priv = usb_get_serial_port_data(port);
448 unsigned long flags;
449 u8 prevstatus;
450 u8 status;
451 u8 changed;
452 u8 multi_change = 0;
453
454 spin_lock_irqsave(&priv->lock, flags);
455 prevstatus = priv->line_status;
456 priv->multi_status_change = 0;
457 spin_unlock_irqrestore(&priv->lock, flags);
458
459 while (!multi_change) {
460 interruptible_sleep_on(&priv->delta_msr_wait);
461 /* see if a signal did it */
462 if (signal_pending(current))
463 return -ERESTARTSYS;
464
465 spin_lock_irqsave(&priv->lock, flags);
466 status = priv->line_status;
467 multi_change = priv->multi_status_change;
468 spin_unlock_irqrestore(&priv->lock, flags);
469
470 changed = prevstatus ^ status;
471
472 if (((arg & TIOCM_RNG) && (changed & CH341_BIT_RI)) ||
473 ((arg & TIOCM_DSR) && (changed & CH341_BIT_DSR)) ||
474 ((arg & TIOCM_CD) && (changed & CH341_BIT_DCD)) ||
475 ((arg & TIOCM_CTS) && (changed & CH341_BIT_CTS))) {
476 return 0;
477 }
478 prevstatus = status;
479 }
480
481 return 0;
482}
483
484/*static int ch341_ioctl(struct usb_serial_port *port, struct file *file,*/
485static int ch341_ioctl(struct tty_struct *tty, struct file *file,
486 unsigned int cmd, unsigned long arg)
487{
488 struct usb_serial_port *port = tty->driver_data;
489 dbg("%s (%d) cmd = 0x%04x", __func__, port->number, cmd);
490
491 switch (cmd) {
492 case TIOCMIWAIT:
493 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
494 return wait_modem_info(port, arg);
495
496 default:
497 dbg("%s not supported = 0x%04x", __func__, cmd);
498 break;
499 }
500
501 return -ENOIOCTLCMD;
502}
503
504static int ch341_tiocmget(struct tty_struct *tty, struct file *file)
505{
506 struct usb_serial_port *port = tty->driver_data;
507 struct ch341_private *priv = usb_get_serial_port_data(port);
508 unsigned long flags;
509 u8 mcr;
510 u8 status;
511 unsigned int result;
512
513 dbg("%s (%d)", __func__, port->number);
514
515 spin_lock_irqsave(&priv->lock, flags);
516 mcr = priv->line_control;
517 status = priv->line_status;
518 spin_unlock_irqrestore(&priv->lock, flags);
519
520 result = ((mcr & CH341_BIT_DTR) ? TIOCM_DTR : 0)
521 | ((mcr & CH341_BIT_RTS) ? TIOCM_RTS : 0)
522 | ((status & CH341_BIT_CTS) ? TIOCM_CTS : 0)
523 | ((status & CH341_BIT_DSR) ? TIOCM_DSR : 0)
524 | ((status & CH341_BIT_RI) ? TIOCM_RI : 0)
525 | ((status & CH341_BIT_DCD) ? TIOCM_CD : 0);
526
527 dbg("%s - result = %x", __func__, result);
528
529 return result;
530}
531
532
533static int ch341_reset_resume(struct usb_interface *intf)
534{
535 struct usb_device *dev = interface_to_usbdev(intf);
536 struct usb_serial *serial = NULL;
537 struct ch341_private *priv;
538
539 serial = usb_get_intfdata(intf);
540 priv = usb_get_serial_port_data(serial->port[0]);
541
542 /*reconfigure ch341 serial port after bus-reset*/
543 ch341_configure(dev, priv);
544
545 usb_serial_resume(intf);
546
547 return 0;
305} 548}
306 549
307static struct usb_driver ch341_driver = { 550static struct usb_driver ch341_driver = {
308 .name = "ch341", 551 .name = "ch341",
309 .probe = usb_serial_probe, 552 .probe = usb_serial_probe,
310 .disconnect = usb_serial_disconnect, 553 .disconnect = usb_serial_disconnect,
554 .suspend = usb_serial_suspend,
555 .resume = usb_serial_resume,
556 .reset_resume = ch341_reset_resume,
311 .id_table = id_table, 557 .id_table = id_table,
312 .no_dynamic_id = 1, 558 .no_dynamic_id = 1,
559 .supports_autosuspend = 1,
313}; 560};
314 561
315static struct usb_serial_driver ch341_device = { 562static struct usb_serial_driver ch341_device = {
@@ -317,12 +564,17 @@ static struct usb_serial_driver ch341_device = {
317 .owner = THIS_MODULE, 564 .owner = THIS_MODULE,
318 .name = "ch341-uart", 565 .name = "ch341-uart",
319 }, 566 },
320 .id_table = id_table, 567 .id_table = id_table,
321 .usb_driver = &ch341_driver, 568 .usb_driver = &ch341_driver,
322 .num_ports = 1, 569 .num_ports = 1,
323 .open = ch341_open, 570 .open = ch341_open,
324 .set_termios = ch341_set_termios, 571 .close = ch341_close,
325 .attach = ch341_attach, 572 .ioctl = ch341_ioctl,
573 .set_termios = ch341_set_termios,
574 .tiocmget = ch341_tiocmget,
575 .tiocmset = ch341_tiocmset,
576 .read_int_callback = ch341_read_int_callback,
577 .attach = ch341_attach,
326}; 578};
327 579
328static int __init ch341_init(void) 580static int __init ch341_init(void)