]> err.no Git - linux-2.6/blob - drivers/usb/serial/ch341.c
Merge commit 'origin/master'
[linux-2.6] / drivers / usb / serial / ch341.c
1 /*
2  * Copyright 2007, Frank A Kingswood <frank@kingswood-consulting.co.uk>
3  * Copyright 2007, Werner Cornelius <werner <at> cornelius-consult.de>
4  *
5  * ch341.c implements a serial port driver for the Winchiphead CH341.
6  *
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.
11  *
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.
15  */
16
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>
26
27 #define DEFAULT_BAUD_RATE 2400
28 #define DEFAULT_TIMEOUT   1000
29
30 /* flags for IO-Bits */
31 #define CH341_BIT_RTS (1 << 6)
32 #define CH341_BIT_DTR (1 << 5)
33
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 */
42
43 /* second interrupt byte */
44 #define CH341_MULT_STAT 0x04 /* multiple status since last interrupt event */
45
46 /* status returned in third interrupt answer byte, inverted in data
47    from irq */
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 */
53
54 /*******************************/
55 /* baudrate calculation factor */
56 /*******************************/
57 #define CH341_BAUDBASE_FACTOR 1532620800
58 #define CH341_BAUDBASE_DIVMAX 3
59
60 static int debug;
61
62 static struct usb_device_id id_table [] = {
63         { USB_DEVICE(0x4348, 0x5523) },
64         { USB_DEVICE(0x1a86, 0x7523) },
65         { },
66 };
67 MODULE_DEVICE_TABLE(usb, id_table);
68
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;
79 };
80
81 static int ch341_control_out(struct usb_device *dev, u8 request,
82                              u16 value, u16 index)
83 {
84         int r;
85         dbg("ch341_control_out(%02x,%02x,%04x,%04x)", USB_DIR_OUT|0x40,
86                 (int)request, (int)value, (int)index);
87
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);
91
92         return r;
93 }
94
95 static int ch341_control_in(struct usb_device *dev,
96                             u8 request, u16 value, u16 index,
97                             char *buf, unsigned bufsize)
98 {
99         int r;
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);
102
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);
106         return r;
107 }
108
109 static int ch341_set_baudrate(struct usb_device *dev,
110                               struct ch341_private *priv)
111 {
112         short a, b;
113         int r;
114         unsigned long factor;
115         short divisor;
116
117         dbg("ch341_set_baudrate(%d)", priv->baud_rate);
118
119         if (!priv->baud_rate)
120                 return -EINVAL;
121         factor = (CH341_BAUDBASE_FACTOR / priv->baud_rate);
122         divisor = CH341_BAUDBASE_DIVMAX;
123
124         while ((factor > 0xfff0) && divisor) {
125                 factor >>= 3;
126                 divisor--;
127         }
128
129         if (factor > 0xfff0)
130                 return -EINVAL;
131
132         factor = 0x10000 - factor;
133         a = (factor & 0xff00) | divisor;
134         b = factor & 0xff;
135
136         r = ch341_control_out(dev, 0x9a, 0x1312, a);
137         if (!r)
138                 r = ch341_control_out(dev, 0x9a, 0x0f2c, b);
139
140         return r;
141 }
142
143 static int ch341_set_handshake(struct usb_device *dev, u8 control)
144 {
145         dbg("ch341_set_handshake(0x%02x)", control);
146         return ch341_control_out(dev, 0xa4, ~control, 0);
147 }
148
149 static int ch341_get_status(struct usb_device *dev, struct ch341_private *priv)
150 {
151         char *buffer;
152         int r;
153         const unsigned size = 8;
154         unsigned long flags;
155
156         dbg("ch341_get_status()");
157
158         buffer = kmalloc(size, GFP_KERNEL);
159         if (!buffer)
160                 return -ENOMEM;
161
162         r = ch341_control_in(dev, 0x95, 0x0706, 0, buffer, size);
163         if (r < 0)
164                 goto out;
165
166         /* setup the private status if available */
167         if (r == 2) {
168                 r = 0;
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);
173         } else
174                 r = -EPROTO;
175
176 out:    kfree(buffer);
177         return r;
178 }
179
180 static void i2c_ch341_setsda(void *data, int state)
181 {
182         struct ch341_private *priv = data;
183         unsigned long flags;
184         u8 control;
185
186         if (state) {
187                 priv->line_control |= CH341_BIT_RTS;
188         } else {
189                 priv->line_control &= ~CH341_BIT_RTS;
190         }
191
192         spin_lock_irqsave(&priv->lock, flags);
193         control = priv->line_control;
194         spin_unlock_irqrestore(&priv->lock, flags);
195
196         ch341_set_handshake(priv->dev, control);
197 }
198
199 static void i2c_ch341_setscl(void *data, int state)
200 {
201
202         struct ch341_private *priv = data;
203         unsigned long flags;
204         u8 control;
205
206         if (state) {
207                 priv->line_control |= CH341_BIT_DTR;
208         } else {
209                 priv->line_control &= ~CH341_BIT_DTR;
210         }
211
212         spin_lock_irqsave(&priv->lock, flags);
213         control = priv->line_control;
214         spin_unlock_irqrestore(&priv->lock, flags);
215
216         ch341_set_handshake(priv->dev, control);
217 }
218
219 static int i2c_ch341_getsda(void *data)
220 {
221         struct ch341_private *priv = data;
222         u8 mcr;
223         u8 status;
224         unsigned long flags;
225
226         spin_lock_irqsave(&priv->lock, flags);
227         mcr = priv->line_control;
228         status = priv->line_status;
229         spin_unlock_irqrestore(&priv->lock, flags);
230
231         return (status & CH341_BIT_CTS);
232 }
233
234 static int i2c_ch341_getscl(void *data)
235 {
236         struct ch341_private *priv = data;
237         u8 mcr;
238         u8 status;
239         unsigned long flags;
240
241         spin_lock_irqsave(&priv->lock, flags);
242         mcr = priv->line_control;
243         status = priv->line_status;
244         spin_unlock_irqrestore(&priv->lock, flags);
245
246         return (mcr & CH341_BIT_DTR);
247 }
248
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,
254         .udelay = 30,
255         .timeout = HZ,
256 };
257
258 /* -------------------------------------------------------------------------- */
259
260 static int ch341_configure(struct usb_device *dev, struct ch341_private *priv)
261 {
262         char *buffer;
263         int r;
264         const unsigned size = 8;
265
266         dbg("ch341_configure()");
267
268         buffer = kmalloc(size, GFP_KERNEL);
269         if (!buffer)
270                 return -ENOMEM;
271
272         /* expect two bytes 0x27 0x00 */
273         r = ch341_control_in(dev, 0x5f, 0, 0, buffer, size);
274         if (r < 0)
275                 goto out;
276
277         r = ch341_control_out(dev, 0xa1, 0, 0);
278         if (r < 0)
279                 goto out;
280
281         r = ch341_set_baudrate(dev, priv);
282         if (r < 0)
283                 goto out;
284
285         /* expect two bytes 0x56 0x00 */
286         r = ch341_control_in(dev, 0x95, 0x2518, 0, buffer, size);
287         if (r < 0)
288                 goto out;
289
290         r = ch341_control_out(dev, 0x9a, 0x2518, 0x0050);
291         if (r < 0)
292                 goto out;
293
294         /* expect 0xff 0xee */
295         r = ch341_get_status(dev, priv);
296         if (r < 0)
297                 goto out;
298
299         r = ch341_control_out(dev, 0xa1, 0x501f, 0xd90a);
300         if (r < 0)
301                 goto out;
302
303         r = ch341_set_baudrate(dev, priv);
304         if (r < 0)
305                 goto out;
306
307         r = ch341_set_handshake(dev, priv->line_control);
308         if (r < 0)
309                 goto out;
310
311         /* expect 0x9f 0xee */
312         r = ch341_get_status(dev, priv);
313
314 out:    kfree(buffer);
315         return r;
316 }
317
318 /* allocate private data */
319 static int ch341_attach(struct usb_serial *serial)
320 {
321         struct ch341_private *priv;
322         int r;
323
324         dbg("ch341_attach()");
325
326         /* private data */
327         priv = kzalloc(sizeof(struct ch341_private), GFP_KERNEL);
328         if (!priv)
329                 return -ENOMEM;
330
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;
335
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;
344
345         r = ch341_configure(serial->dev, priv);
346         if (r < 0)
347                 goto error;
348
349         r = i2c_bit_add_bus(&priv->adapter);
350         if (r < 0) {
351                 goto error;
352         }
353
354         usb_set_serial_port_data(serial->port[0], priv);
355
356         dev_info(&priv->adapter.dev, "connected ch341 device\n");
357
358         return 0;
359
360 error:  kfree(priv);
361         return r;
362 }
363
364 static void ch341_close(struct usb_serial_port *port, struct file *filp)
365 {
366         struct ch341_private *priv = usb_get_serial_port_data(port);
367         unsigned long flags;
368         unsigned int c_cflag;
369
370         dbg("%s - port %d", __func__, port->number);
371
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);
377
378         if (port->tty) {
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);
386                 }
387         }
388         wake_up_interruptible(&priv->delta_msr_wait);
389 }
390
391
392 /* open this device, set default parameters */
393 static int ch341_open(struct tty_struct *tty, struct usb_serial_port *port,
394                                 struct file *filp)
395 {
396         struct usb_serial *serial = port->serial;
397         struct ch341_private *priv = usb_get_serial_port_data(serial->port[0]);
398         int r;
399
400         dbg("ch341_open()");
401
402         priv->baud_rate = DEFAULT_BAUD_RATE;
403         priv->line_control = CH341_BIT_RTS | CH341_BIT_DTR;
404
405         r = ch341_configure(serial->dev, priv);
406         if (r)
407                 goto out;
408
409         r = ch341_set_handshake(serial->dev, priv->line_control);
410         if (r)
411                 goto out;
412
413         r = ch341_set_baudrate(serial->dev, priv);
414         if (r)
415                 goto out;
416
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);
420         if (r) {
421                 dev_err(&port->dev, "%s - failed submitting interrupt urb,"
422                         " error %d\n", __func__, r);
423                 ch341_close(port, NULL);
424                 return -EPROTO;
425         }
426
427         r = usb_serial_generic_open(tty, port, filp);
428
429 out:    return r;
430 }
431
432 /* Old_termios contains the original termios settings and
433  * tty->termios contains the new setting to be used.
434  */
435 static void ch341_set_termios(struct tty_struct *tty,
436                 struct usb_serial_port *port, struct ktermios *old_termios)
437 {
438         struct ch341_private *priv = usb_get_serial_port_data(port);
439         unsigned baud_rate;
440         unsigned long flags;
441
442         dbg("ch341_set_termios()");
443
444         if (!tty || !tty->termios)
445                 return;
446
447         baud_rate = tty_get_baud_rate(tty);
448
449         priv->baud_rate = baud_rate;
450
451         if (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);
456         } else {
457                 spin_lock_irqsave(&priv->lock, flags);
458                 priv->line_control &= ~(CH341_BIT_DTR | CH341_BIT_RTS);
459                 spin_unlock_irqrestore(&priv->lock, flags);
460         }
461
462         ch341_set_handshake(port->serial->dev, priv->line_control);
463
464         /* Unimplemented:
465          * (cflag & CSIZE) : data bits [5, 8]
466          * (cflag & PARENB) : parity {NONE, EVEN, ODD}
467          * (cflag & CSTOPB) : stop bits [1, 2]
468          */
469 }
470
471 static int ch341_tiocmset(struct usb_serial_port *port, struct file *file,
472                           unsigned int set, unsigned int clear)
473 {
474         struct ch341_private *priv = usb_get_serial_port_data(port);
475         unsigned long flags;
476         u8 control;
477
478         spin_lock_irqsave(&priv->lock, flags);
479         if (set & TIOCM_RTS)
480                 priv->line_control |= CH341_BIT_RTS;
481         if (set & TIOCM_DTR)
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);
489
490         return ch341_set_handshake(port->serial->dev, control);
491 }
492
493 static void ch341_read_int_callback(struct urb *urb)
494 {
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;
498         int status;
499
500         dbg("%s (%d)", __func__, port->number);
501
502         switch (urb->status) {
503         case 0:
504                 /* success */
505                 break;
506         case -ECONNRESET:
507         case -ENOENT:
508         case -ESHUTDOWN:
509                 /* this urb is terminated, clean up */
510                 dbg("%s - urb shutting down with status: %d", __func__,
511                     urb->status);
512                 return;
513         default:
514                 dbg("%s - nonzero urb status received: %d", __func__,
515                     urb->status);
516                 goto exit;
517         }
518
519         usb_serial_debug_data(debug, &port->dev, __func__,
520                               urb->actual_length, urb->transfer_buffer);
521
522         if (actual_length >= 4) {
523                 struct ch341_private *priv = usb_get_serial_port_data(port);
524                 unsigned long flags;
525
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);
532         }
533
534 exit:
535         status = usb_submit_urb(urb, GFP_ATOMIC);
536         if (status)
537                 dev_err(&urb->dev->dev,
538                         "%s - usb_submit_urb failed with result %d\n",
539                         __func__, status);
540 }
541
542 static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
543 {
544         struct ch341_private *priv = usb_get_serial_port_data(port);
545         unsigned long flags;
546         u8 prevstatus;
547         u8 status;
548         u8 changed;
549         u8 multi_change = 0;
550
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);
555
556         while (!multi_change) {
557                 interruptible_sleep_on(&priv->delta_msr_wait);
558                 /* see if a signal did it */
559                 if (signal_pending(current))
560                         return -ERESTARTSYS;
561
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);
566
567                 changed = prevstatus ^ status;
568
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))) {
573                         return 0;
574                 }
575                 prevstatus = status;
576         }
577
578         return 0;
579 }
580
581 static int ch341_ioctl(struct usb_serial_port *port, struct file *file,
582                         unsigned int cmd, unsigned long arg)
583 {
584         dbg("%s (%d) cmd = 0x%04x", __func__, port->number, cmd);
585
586         switch (cmd) {
587         case TIOCMIWAIT:
588                 dbg("%s (%d) TIOCMIWAIT", __func__,  port->number);
589                 return wait_modem_info(port, arg);
590
591         default:
592                 dbg("%s not supported = 0x%04x", __func__, cmd);
593                 break;
594         }
595
596         return -ENOIOCTLCMD;
597 }
598
599 static int ch341_tiocmget(struct usb_serial_port *port, struct file *file)
600 {
601         struct ch341_private *priv = usb_get_serial_port_data(port);
602         unsigned long flags;
603         u8 mcr;
604         u8 status;
605         unsigned int result;
606
607         dbg("%s (%d)", __func__, port->number);
608
609         spin_lock_irqsave(&priv->lock, flags);
610         mcr = priv->line_control;
611         status = priv->line_status;
612         spin_unlock_irqrestore(&priv->lock, flags);
613
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);
620
621         dbg("%s - result = %x", __func__, result);
622
623         return result;
624 }
625
626 static void ch341_shutdown(struct usb_serial *serial)
627 {
628         struct ch341_private *priv = usb_get_serial_port_data(serial->port[0]);
629
630         dbg("%s", __FUNCTION__);
631
632         if (priv)
633                 i2c_del_adapter(&priv->adapter);
634         kfree(priv);
635 }
636
637 static u32 ch341_functionality(struct i2c_adapter *adap)
638 {
639   return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
640 }
641
642
643 static struct usb_driver ch341_driver = {
644         .name           = "ch341",
645         .probe          = usb_serial_probe,
646         .disconnect     = usb_serial_disconnect,
647         .id_table       = id_table,
648         .no_dynamic_id  = 1,
649 };
650
651 static struct usb_serial_driver ch341_device = {
652         .driver = {
653                 .owner  = THIS_MODULE,
654                 .name   = "ch341-uart",
655         },
656         .id_table          = id_table,
657         .usb_driver        = &ch341_driver,
658         .num_interrupt_in  = 1,
659         .num_bulk_in       = 1,
660         .num_bulk_out      = 1,
661         .num_ports         = 1,
662         .open              = ch341_open,
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,
671 };
672
673 static int __init ch341_init(void)
674 {
675         int retval;
676
677         retval = usb_serial_register(&ch341_device);
678         if (retval)
679                 return retval;
680         retval = usb_register(&ch341_driver);
681         if (retval)
682                 usb_serial_deregister(&ch341_device);
683         return retval;
684 }
685
686 static void __exit ch341_exit(void)
687 {
688         usb_deregister(&ch341_driver);
689         usb_serial_deregister(&ch341_device);
690 }
691
692 module_init(ch341_init);
693 module_exit(ch341_exit);
694 MODULE_LICENSE("GPL");
695
696 module_param(debug, bool, S_IRUGO | S_IWUSR);
697 MODULE_PARM_DESC(debug, "Debug enabled or not");
698
699 /* EOF ch341.c */