aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorGreg Kroah-Hartman <gregkh@suse.de>2009-04-07 14:56:32 -0400
committerGreg Kroah-Hartman <gregkh@suse.de>2009-04-17 13:50:26 -0400
commitc7877e1983f051cc267e1ec8c232a02d23c0a82a (patch)
tree2773db9e81ed3a1573c3e462f5479589f2659aa8 /drivers
parent0ec8fd70fbd7327e1bf747c4a2f6b67efdf16618 (diff)
Revert USB: usb-serial ch341: support for DTR/RTS/CTS
Reverts commit 664d5df92e88b6ef091048a802b3750f4e989180 as the commit log information was not complete, and we didn't have a proper signed-off-by by the author of the original BSD code. Cc: Werner Cornelius <Werner.Cornelius@cornelius-consult.de> Cc: Boris Hajduk <boris@hajduk.org> Cc: Alan Cox <alan@lxorguk.ukuu.org.uk> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/usb/serial/ch341.c374
1 files changed, 72 insertions, 302 deletions
diff --git a/drivers/usb/serial/ch341.c b/drivers/usb/serial/ch341.c
index ab4cc277aa65..d33d39d9b7e2 100644
--- a/drivers/usb/serial/ch341.c
+++ b/drivers/usb/serial/ch341.c
@@ -1,7 +1,5 @@
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>
5 * 3 *
6 * ch341.c implements a serial port driver for the Winchiphead CH341. 4 * ch341.c implements a serial port driver for the Winchiphead CH341.
7 * 5 *
@@ -23,39 +21,9 @@
23#include <linux/usb/serial.h> 21#include <linux/usb/serial.h>
24#include <linux/serial.h> 22#include <linux/serial.h>
25 23
26#define DEFAULT_BAUD_RATE 9600 24#define DEFAULT_BAUD_RATE 2400
27#define DEFAULT_TIMEOUT 1000 25#define DEFAULT_TIMEOUT 1000
28 26
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
59static int debug; 27static int debug;
60 28
61static struct usb_device_id id_table [] = { 29static struct usb_device_id id_table [] = {
@@ -66,12 +34,9 @@ static struct usb_device_id id_table [] = {
66MODULE_DEVICE_TABLE(usb, id_table); 34MODULE_DEVICE_TABLE(usb, id_table);
67 35
68struct ch341_private { 36struct ch341_private {
69 spinlock_t lock; /* access lock */ 37 unsigned baud_rate;
70 wait_queue_head_t delta_msr_wait; /* wait queue for modem status */ 38 u8 dtr;
71 unsigned baud_rate; /* set baud rate */ 39 u8 rts;
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 */
75}; 40};
76 41
77static int ch341_control_out(struct usb_device *dev, u8 request, 42static int ch341_control_out(struct usb_device *dev, u8 request,
@@ -107,28 +72,37 @@ static int ch341_set_baudrate(struct usb_device *dev,
107{ 72{
108 short a, b; 73 short a, b;
109 int r; 74 int r;
110 unsigned long factor;
111 short divisor;
112 75
113 dbg("ch341_set_baudrate(%d)", priv->baud_rate); 76 dbg("ch341_set_baudrate(%d)", priv->baud_rate);
114 77 switch (priv->baud_rate) {
115 if (!priv->baud_rate) 78 case 2400:
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:
116 return -EINVAL; 103 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--;
123 } 104 }
124 105
125 if (factor > 0xfff0)
126 return -EINVAL;
127
128 factor = 0x10000 - factor;
129 a = (factor & 0xff00) | divisor;
130 b = factor & 0xff;
131
132 r = ch341_control_out(dev, 0x9a, 0x1312, a); 106 r = ch341_control_out(dev, 0x9a, 0x1312, a);
133 if (!r) 107 if (!r)
134 r = ch341_control_out(dev, 0x9a, 0x0f2c, b); 108 r = ch341_control_out(dev, 0x9a, 0x0f2c, b);
@@ -136,18 +110,19 @@ static int ch341_set_baudrate(struct usb_device *dev,
136 return r; 110 return r;
137} 111}
138 112
139static int ch341_set_handshake(struct usb_device *dev, u8 control) 113static int ch341_set_handshake(struct usb_device *dev,
114 struct ch341_private *priv)
140{ 115{
141 dbg("ch341_set_handshake(0x%02x)", control); 116 dbg("ch341_set_handshake(%d,%d)", priv->dtr, priv->rts);
142 return ch341_control_out(dev, 0xa4, ~control, 0); 117 return ch341_control_out(dev, 0xa4,
118 ~((priv->dtr?1<<5:0)|(priv->rts?1<<6:0)), 0);
143} 119}
144 120
145static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv) 121static int ch341_get_status(struct usb_device *dev)
146{ 122{
147 char *buffer; 123 char *buffer;
148 int r; 124 int r;
149 const unsigned size = 8; 125 const unsigned size = 8;
150 unsigned long flags;
151 126
152 dbg("ch341_get_status()"); 127 dbg("ch341_get_status()");
153 128
@@ -159,15 +134,10 @@ static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv)
159 if (r < 0) 134 if (r < 0)
160 goto out; 135 goto out;
161 136
162 /* setup the private status if available */ 137 /* Not having the datasheet for the CH341, we ignore the bytes returned
163 if (r == 2) { 138 * from the device. Return error if the device did not respond in time.
164 r = 0; 139 */
165 spin_lock_irqsave(&priv->lock, flags); 140 r = 0;
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;
171 141
172out: kfree(buffer); 142out: kfree(buffer);
173 return r; 143 return r;
@@ -210,7 +180,7 @@ static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
210 goto out; 180 goto out;
211 181
212 /* expect 0xff 0xee */ 182 /* expect 0xff 0xee */
213 r = ch341_get_status(dev, priv); 183 r = ch341_get_status(dev);
214 if (r < 0) 184 if (r < 0)
215 goto out; 185 goto out;
216 186
@@ -222,12 +192,12 @@ static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
222 if (r < 0) 192 if (r < 0)
223 goto out; 193 goto out;
224 194
225 r = ch341_set_handshake(dev, priv->line_control); 195 r = ch341_set_handshake(dev, priv);
226 if (r < 0) 196 if (r < 0)
227 goto out; 197 goto out;
228 198
229 /* expect 0x9f 0xee */ 199 /* expect 0x9f 0xee */
230 r = ch341_get_status(dev, priv); 200 r = ch341_get_status(dev);
231 201
232out: kfree(buffer); 202out: kfree(buffer);
233 return r; 203 return r;
@@ -246,10 +216,9 @@ static int ch341_attach(struct usb_serial *serial)
246 if (!priv) 216 if (!priv)
247 return -ENOMEM; 217 return -ENOMEM;
248 218
249 spin_lock_init(&priv->lock);
250 init_waitqueue_head(&priv->delta_msr_wait);
251 priv->baud_rate = DEFAULT_BAUD_RATE; 219 priv->baud_rate = DEFAULT_BAUD_RATE;
252 priv->line_control = CH341_BIT_RTS | CH341_BIT_DTR; 220 priv->dtr = 1;
221 priv->rts = 1;
253 222
254 r = ch341_configure(serial->dev, priv); 223 r = ch341_configure(serial->dev, priv);
255 if (r < 0) 224 if (r < 0)
@@ -262,35 +231,6 @@ error: kfree(priv);
262 return r; 231 return r;
263} 232}
264 233
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
294/* open this device, set default parameters */ 234/* open this device, set default parameters */
295static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port, 235static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port,
296 struct file *filp) 236 struct file *filp)
@@ -302,13 +242,14 @@ static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port,
302 dbg("ch341_open()"); 242 dbg("ch341_open()");
303 243
304 priv->baud_rate = DEFAULT_BAUD_RATE; 244 priv->baud_rate = DEFAULT_BAUD_RATE;
305 priv->line_control = CH341_BIT_RTS | CH341_BIT_DTR; 245 priv->dtr = 1;
246 priv->rts = 1;
306 247
307 r = ch341_configure(serial->dev, priv); 248 r = ch341_configure(serial->dev, priv);
308 if (r) 249 if (r)
309 goto out; 250 goto out;
310 251
311 r = ch341_set_handshake(serial->dev, priv->line_control); 252 r = ch341_set_handshake(serial->dev, priv);
312 if (r) 253 if (r)
313 goto out; 254 goto out;
314 255
@@ -316,16 +257,6 @@ static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port,
316 if (r) 257 if (r)
317 goto out; 258 goto out;
318 259
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
329 r = usb_serial_generic_open(tty, port, filp); 260 r = usb_serial_generic_open(tty, port, filp);
330 261
331out: return r; 262out: return r;
@@ -339,194 +270,38 @@ static void ch341_set_termios(struct tty_struct *tty,
339{ 270{
340 struct ch341_private *priv = usb_get_serial_port_data(port); 271 struct ch341_private *priv = usb_get_serial_port_data(port);
341 unsigned baud_rate; 272 unsigned baud_rate;
342 unsigned long flags;
343 273
344 dbg("ch341_set_termios()"); 274 dbg("ch341_set_termios()");
345 275
346 if (!tty || !tty->termios)
347 return;
348
349 baud_rate = tty_get_baud_rate(tty); 276 baud_rate = tty_get_baud_rate(tty);
350 277
351 priv->baud_rate = baud_rate; 278 switch (baud_rate) {
352 279 case 2400:
353 if (baud_rate) { 280 case 4800:
354 spin_lock_irqsave(&priv->lock, flags); 281 case 9600:
355 priv->line_control |= (CH341_BIT_DTR | CH341_BIT_RTS); 282 case 19200:
356 spin_unlock_irqrestore(&priv->lock, flags); 283 case 38400:
357 ch341_set_baudrate(port->serial->dev, priv); 284 case 115200:
358 } else { 285 priv->baud_rate = baud_rate;
359 spin_lock_irqsave(&priv->lock, flags); 286 break;
360 priv->line_control &= ~(CH341_BIT_DTR | CH341_BIT_RTS); 287 default:
361 spin_unlock_irqrestore(&priv->lock, flags); 288 dbg("Rate %d not supported, using %d",
289 baud_rate, DEFAULT_BAUD_RATE);
290 priv->baud_rate = DEFAULT_BAUD_RATE;
362 } 291 }
363 292
364 ch341_set_handshake(port->serial->dev, priv->line_control); 293 ch341_set_baudrate(port->serial->dev, priv);
365 294
366 /* Unimplemented: 295 /* Unimplemented:
367 * (cflag & CSIZE) : data bits [5, 8] 296 * (cflag & CSIZE) : data bits [5, 8]
368 * (cflag & PARENB) : parity {NONE, EVEN, ODD} 297 * (cflag & PARENB) : parity {NONE, EVEN, ODD}
369 * (cflag & CSTOPB) : stop bits [1, 2] 298 * (cflag & CSTOPB) : stop bits [1, 2]
370 */ 299 */
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 }
421
422 usb_serial_debug_data(debug, &port->dev, __func__,
423 urb->actual_length, urb->transfer_buffer);
424
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 300
529 return result; 301 /* Copy back the old hardware settings */
302 tty_termios_copy_hw(tty->termios, old_termios);
303 /* And re-encode with the new baud */
304 tty_encode_baud_rate(tty, baud_rate, baud_rate);
530} 305}
531 306
532 307
@@ -564,17 +339,12 @@ static struct usb_serial_driver ch341_device = {
564 .owner = THIS_MODULE, 339 .owner = THIS_MODULE,
565 .name = "ch341-uart", 340 .name = "ch341-uart",
566 }, 341 },
567 .id_table = id_table, 342 .id_table = id_table,
568 .usb_driver = &ch341_driver, 343 .usb_driver = &ch341_driver,
569 .num_ports = 1, 344 .num_ports = 1,
570 .open = ch341_open, 345 .open = ch341_open,
571 .close = ch341_close, 346 .set_termios = ch341_set_termios,
572 .ioctl = ch341_ioctl, 347 .attach = ch341_attach,
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,
578}; 348};
579 349
580static int __init ch341_init(void) 350static int __init ch341_init(void)