Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / usb / serial / ssu100.c
blob87362e48796e2662b82533b34f894ec9c08e966b
1 /*
2 * usb-serial driver for Quatech SSU-100
4 * based on ftdi_sio.c and the original serqt_usb.c from Quatech
6 */
8 #include <linux/errno.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/tty.h>
12 #include <linux/tty_driver.h>
13 #include <linux/tty_flip.h>
14 #include <linux/module.h>
15 #include <linux/serial.h>
16 #include <linux/usb.h>
17 #include <linux/usb/serial.h>
18 #include <linux/serial_reg.h>
19 #include <linux/uaccess.h>
21 #define QT_OPEN_CLOSE_CHANNEL 0xca
22 #define QT_SET_GET_DEVICE 0xc2
23 #define QT_SET_GET_REGISTER 0xc0
24 #define QT_GET_SET_PREBUF_TRIG_LVL 0xcc
25 #define QT_SET_ATF 0xcd
26 #define QT_GET_SET_UART 0xc1
27 #define QT_TRANSFER_IN 0xc0
28 #define QT_HW_FLOW_CONTROL_MASK 0xc5
29 #define QT_SW_FLOW_CONTROL_MASK 0xc6
31 #define SERIAL_MSR_MASK 0xf0
33 #define SERIAL_CRTSCTS ((UART_MCR_RTS << 8) | UART_MSR_CTS)
35 #define SERIAL_EVEN_PARITY (UART_LCR_PARITY | UART_LCR_EPAR)
37 #define MAX_BAUD_RATE 460800
39 #define ATC_DISABLED 0x00
40 #define DUPMODE_BITS 0xc0
41 #define RR_BITS 0x03
42 #define LOOPMODE_BITS 0x41
43 #define RS232_MODE 0x00
44 #define RTSCTS_TO_CONNECTOR 0x40
45 #define CLKS_X4 0x02
46 #define FULLPWRBIT 0x00000080
47 #define NEXT_BOARD_POWER_BIT 0x00000004
49 static int debug;
51 /* Version Information */
52 #define DRIVER_VERSION "v0.1"
53 #define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver"
55 #define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */
56 #define QUATECH_SSU100 0xC020 /* SSU100 */
58 static const struct usb_device_id id_table[] = {
59 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100)},
60 {} /* Terminating entry */
63 MODULE_DEVICE_TABLE(usb, id_table);
66 static struct usb_driver ssu100_driver = {
67 .name = "ssu100",
68 .probe = usb_serial_probe,
69 .disconnect = usb_serial_disconnect,
70 .id_table = id_table,
71 .suspend = usb_serial_suspend,
72 .resume = usb_serial_resume,
73 .no_dynamic_id = 1,
74 .supports_autosuspend = 1,
77 struct ssu100_port_private {
78 spinlock_t status_lock;
79 u8 shadowLSR;
80 u8 shadowMSR;
81 wait_queue_head_t delta_msr_wait; /* Used for TIOCMIWAIT */
82 struct async_icount icount;
85 static void ssu100_release(struct usb_serial *serial)
87 struct ssu100_port_private *priv = usb_get_serial_port_data(*serial->port);
89 dbg("%s", __func__);
90 kfree(priv);
93 static inline int ssu100_control_msg(struct usb_device *dev,
94 u8 request, u16 data, u16 index)
96 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
97 request, 0x40, data, index,
98 NULL, 0, 300);
101 static inline int ssu100_setdevice(struct usb_device *dev, u8 *data)
103 u16 x = ((u16)(data[1] << 8) | (u16)(data[0]));
105 return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0);
109 static inline int ssu100_getdevice(struct usb_device *dev, u8 *data)
111 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
112 QT_SET_GET_DEVICE, 0xc0, 0, 0,
113 data, 3, 300);
116 static inline int ssu100_getregister(struct usb_device *dev,
117 unsigned short uart,
118 unsigned short reg,
119 u8 *data)
121 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
122 QT_SET_GET_REGISTER, 0xc0, reg,
123 uart, data, sizeof(*data), 300);
128 static inline int ssu100_setregister(struct usb_device *dev,
129 unsigned short uart,
130 unsigned short reg,
131 u16 data)
133 u16 value = (data << 8) | reg;
135 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
136 QT_SET_GET_REGISTER, 0x40, value, uart,
137 NULL, 0, 300);
141 #define set_mctrl(dev, set) update_mctrl((dev), (set), 0)
142 #define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear))
144 /* these do not deal with device that have more than 1 port */
145 static inline int update_mctrl(struct usb_device *dev, unsigned int set,
146 unsigned int clear)
148 unsigned urb_value;
149 int result;
151 if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) {
152 dbg("%s - DTR|RTS not being set|cleared", __func__);
153 return 0; /* no change */
156 clear &= ~set; /* 'set' takes precedence over 'clear' */
157 urb_value = 0;
158 if (set & TIOCM_DTR)
159 urb_value |= UART_MCR_DTR;
160 if (set & TIOCM_RTS)
161 urb_value |= UART_MCR_RTS;
163 result = ssu100_setregister(dev, 0, UART_MCR, urb_value);
164 if (result < 0)
165 dbg("%s Error from MODEM_CTRL urb", __func__);
167 return result;
170 static int ssu100_initdevice(struct usb_device *dev)
172 u8 *data;
173 int result = 0;
175 dbg("%s", __func__);
177 data = kzalloc(3, GFP_KERNEL);
178 if (!data)
179 return -ENOMEM;
181 result = ssu100_getdevice(dev, data);
182 if (result < 0) {
183 dbg("%s - get_device failed %i", __func__, result);
184 goto out;
187 data[1] &= ~FULLPWRBIT;
189 result = ssu100_setdevice(dev, data);
190 if (result < 0) {
191 dbg("%s - setdevice failed %i", __func__, result);
192 goto out;
195 result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0);
196 if (result < 0) {
197 dbg("%s - set prebuffer level failed %i", __func__, result);
198 goto out;
201 result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0);
202 if (result < 0) {
203 dbg("%s - set ATFprebuffer level failed %i", __func__, result);
204 goto out;
207 result = ssu100_getdevice(dev, data);
208 if (result < 0) {
209 dbg("%s - get_device failed %i", __func__, result);
210 goto out;
213 data[0] &= ~(RR_BITS | DUPMODE_BITS);
214 data[0] |= CLKS_X4;
215 data[1] &= ~(LOOPMODE_BITS);
216 data[1] |= RS232_MODE;
218 result = ssu100_setdevice(dev, data);
219 if (result < 0) {
220 dbg("%s - setdevice failed %i", __func__, result);
221 goto out;
224 out: kfree(data);
225 return result;
230 static void ssu100_set_termios(struct tty_struct *tty,
231 struct usb_serial_port *port,
232 struct ktermios *old_termios)
234 struct usb_device *dev = port->serial->dev;
235 struct ktermios *termios = tty->termios;
236 u16 baud, divisor, remainder;
237 unsigned int cflag = termios->c_cflag;
238 u16 urb_value = 0; /* will hold the new flags */
239 int result;
241 dbg("%s", __func__);
243 if (cflag & PARENB) {
244 if (cflag & PARODD)
245 urb_value |= UART_LCR_PARITY;
246 else
247 urb_value |= SERIAL_EVEN_PARITY;
250 switch (cflag & CSIZE) {
251 case CS5:
252 urb_value |= UART_LCR_WLEN5;
253 break;
254 case CS6:
255 urb_value |= UART_LCR_WLEN6;
256 break;
257 case CS7:
258 urb_value |= UART_LCR_WLEN7;
259 break;
260 default:
261 case CS8:
262 urb_value |= UART_LCR_WLEN8;
263 break;
266 baud = tty_get_baud_rate(tty);
267 if (!baud)
268 baud = 9600;
270 dbg("%s - got baud = %d\n", __func__, baud);
273 divisor = MAX_BAUD_RATE / baud;
274 remainder = MAX_BAUD_RATE % baud;
275 if (((remainder * 2) >= baud) && (baud != 110))
276 divisor++;
278 urb_value = urb_value << 8;
280 result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value);
281 if (result < 0)
282 dbg("%s - set uart failed", __func__);
284 if (cflag & CRTSCTS)
285 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
286 SERIAL_CRTSCTS, 0);
287 else
288 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
289 0, 0);
290 if (result < 0)
291 dbg("%s - set HW flow control failed", __func__);
293 if (I_IXOFF(tty) || I_IXON(tty)) {
294 u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty)));
296 result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
297 x, 0);
298 } else
299 result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
300 0, 0);
302 if (result < 0)
303 dbg("%s - set SW flow control failed", __func__);
308 static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port)
310 struct usb_device *dev = port->serial->dev;
311 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
312 u8 *data;
313 int result;
314 unsigned long flags;
316 dbg("%s - port %d", __func__, port->number);
318 data = kzalloc(2, GFP_KERNEL);
319 if (!data)
320 return -ENOMEM;
322 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
323 QT_OPEN_CLOSE_CHANNEL,
324 QT_TRANSFER_IN, 0x01,
325 0, data, 2, 300);
326 if (result < 0) {
327 dbg("%s - open failed %i", __func__, result);
328 kfree(data);
329 return result;
332 spin_lock_irqsave(&priv->status_lock, flags);
333 priv->shadowLSR = data[0];
334 priv->shadowMSR = data[1];
335 spin_unlock_irqrestore(&priv->status_lock, flags);
337 kfree(data);
339 /* set to 9600 */
340 result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300);
341 if (result < 0)
342 dbg("%s - set uart failed", __func__);
344 if (tty)
345 ssu100_set_termios(tty, port, tty->termios);
347 return usb_serial_generic_open(tty, port);
350 static void ssu100_close(struct usb_serial_port *port)
352 dbg("%s", __func__);
353 usb_serial_generic_close(port);
356 static int get_serial_info(struct usb_serial_port *port,
357 struct serial_struct __user *retinfo)
359 struct serial_struct tmp;
361 if (!retinfo)
362 return -EFAULT;
364 memset(&tmp, 0, sizeof(tmp));
365 tmp.line = port->serial->minor;
366 tmp.port = 0;
367 tmp.irq = 0;
368 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
369 tmp.xmit_fifo_size = port->bulk_out_size;
370 tmp.baud_base = 9600;
371 tmp.close_delay = 5*HZ;
372 tmp.closing_wait = 30*HZ;
374 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
375 return -EFAULT;
376 return 0;
379 static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
381 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
382 struct async_icount prev, cur;
383 unsigned long flags;
385 spin_lock_irqsave(&priv->status_lock, flags);
386 prev = priv->icount;
387 spin_unlock_irqrestore(&priv->status_lock, flags);
389 while (1) {
390 wait_event_interruptible(priv->delta_msr_wait,
391 ((priv->icount.rng != prev.rng) ||
392 (priv->icount.dsr != prev.dsr) ||
393 (priv->icount.dcd != prev.dcd) ||
394 (priv->icount.cts != prev.cts)));
396 if (signal_pending(current))
397 return -ERESTARTSYS;
399 spin_lock_irqsave(&priv->status_lock, flags);
400 cur = priv->icount;
401 spin_unlock_irqrestore(&priv->status_lock, flags);
403 if ((prev.rng == cur.rng) &&
404 (prev.dsr == cur.dsr) &&
405 (prev.dcd == cur.dcd) &&
406 (prev.cts == cur.cts))
407 return -EIO;
409 if ((arg & TIOCM_RNG && (prev.rng != cur.rng)) ||
410 (arg & TIOCM_DSR && (prev.dsr != cur.dsr)) ||
411 (arg & TIOCM_CD && (prev.dcd != cur.dcd)) ||
412 (arg & TIOCM_CTS && (prev.cts != cur.cts)))
413 return 0;
415 return 0;
418 static int ssu100_get_icount(struct tty_struct *tty,
419 struct serial_icounter_struct *icount)
421 struct usb_serial_port *port = tty->driver_data;
422 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
423 struct async_icount cnow = priv->icount;
425 icount->cts = cnow.cts;
426 icount->dsr = cnow.dsr;
427 icount->rng = cnow.rng;
428 icount->dcd = cnow.dcd;
429 icount->rx = cnow.rx;
430 icount->tx = cnow.tx;
431 icount->frame = cnow.frame;
432 icount->overrun = cnow.overrun;
433 icount->parity = cnow.parity;
434 icount->brk = cnow.brk;
435 icount->buf_overrun = cnow.buf_overrun;
437 return 0;
442 static int ssu100_ioctl(struct tty_struct *tty,
443 unsigned int cmd, unsigned long arg)
445 struct usb_serial_port *port = tty->driver_data;
447 dbg("%s cmd 0x%04x", __func__, cmd);
449 switch (cmd) {
450 case TIOCGSERIAL:
451 return get_serial_info(port,
452 (struct serial_struct __user *) arg);
454 case TIOCMIWAIT:
455 return wait_modem_info(port, arg);
457 default:
458 break;
461 dbg("%s arg not supported", __func__);
463 return -ENOIOCTLCMD;
466 static int ssu100_attach(struct usb_serial *serial)
468 struct ssu100_port_private *priv;
469 struct usb_serial_port *port = *serial->port;
471 dbg("%s", __func__);
473 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
474 if (!priv) {
475 dev_err(&port->dev, "%s- kmalloc(%Zd) failed.\n", __func__,
476 sizeof(*priv));
477 return -ENOMEM;
480 spin_lock_init(&priv->status_lock);
481 init_waitqueue_head(&priv->delta_msr_wait);
482 usb_set_serial_port_data(port, priv);
484 return ssu100_initdevice(serial->dev);
487 static int ssu100_tiocmget(struct tty_struct *tty)
489 struct usb_serial_port *port = tty->driver_data;
490 struct usb_device *dev = port->serial->dev;
491 u8 *d;
492 int r;
494 dbg("%s\n", __func__);
496 d = kzalloc(2, GFP_KERNEL);
497 if (!d)
498 return -ENOMEM;
500 r = ssu100_getregister(dev, 0, UART_MCR, d);
501 if (r < 0)
502 goto mget_out;
504 r = ssu100_getregister(dev, 0, UART_MSR, d+1);
505 if (r < 0)
506 goto mget_out;
508 r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) |
509 (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) |
510 (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) |
511 (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) |
512 (d[1] & UART_MSR_RI ? TIOCM_RI : 0) |
513 (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0);
515 mget_out:
516 kfree(d);
517 return r;
520 static int ssu100_tiocmset(struct tty_struct *tty,
521 unsigned int set, unsigned int clear)
523 struct usb_serial_port *port = tty->driver_data;
524 struct usb_device *dev = port->serial->dev;
526 dbg("%s\n", __func__);
527 return update_mctrl(dev, set, clear);
530 static void ssu100_dtr_rts(struct usb_serial_port *port, int on)
532 struct usb_device *dev = port->serial->dev;
534 dbg("%s\n", __func__);
536 mutex_lock(&port->serial->disc_mutex);
537 if (!port->serial->disconnected) {
538 /* Disable flow control */
539 if (!on &&
540 ssu100_setregister(dev, 0, UART_MCR, 0) < 0)
541 dev_err(&port->dev, "error from flowcontrol urb\n");
542 /* drop RTS and DTR */
543 if (on)
544 set_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
545 else
546 clear_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
548 mutex_unlock(&port->serial->disc_mutex);
551 static void ssu100_update_msr(struct usb_serial_port *port, u8 msr)
553 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
554 unsigned long flags;
556 spin_lock_irqsave(&priv->status_lock, flags);
557 priv->shadowMSR = msr;
558 spin_unlock_irqrestore(&priv->status_lock, flags);
560 if (msr & UART_MSR_ANY_DELTA) {
561 /* update input line counters */
562 if (msr & UART_MSR_DCTS)
563 priv->icount.cts++;
564 if (msr & UART_MSR_DDSR)
565 priv->icount.dsr++;
566 if (msr & UART_MSR_DDCD)
567 priv->icount.dcd++;
568 if (msr & UART_MSR_TERI)
569 priv->icount.rng++;
570 wake_up_interruptible(&priv->delta_msr_wait);
574 static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr,
575 char *tty_flag)
577 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
578 unsigned long flags;
580 spin_lock_irqsave(&priv->status_lock, flags);
581 priv->shadowLSR = lsr;
582 spin_unlock_irqrestore(&priv->status_lock, flags);
584 *tty_flag = TTY_NORMAL;
585 if (lsr & UART_LSR_BRK_ERROR_BITS) {
586 /* we always want to update icount, but we only want to
587 * update tty_flag for one case */
588 if (lsr & UART_LSR_BI) {
589 priv->icount.brk++;
590 *tty_flag = TTY_BREAK;
591 usb_serial_handle_break(port);
593 if (lsr & UART_LSR_PE) {
594 priv->icount.parity++;
595 if (*tty_flag == TTY_NORMAL)
596 *tty_flag = TTY_PARITY;
598 if (lsr & UART_LSR_FE) {
599 priv->icount.frame++;
600 if (*tty_flag == TTY_NORMAL)
601 *tty_flag = TTY_FRAME;
603 if (lsr & UART_LSR_OE){
604 priv->icount.overrun++;
605 if (*tty_flag == TTY_NORMAL)
606 *tty_flag = TTY_OVERRUN;
612 static int ssu100_process_packet(struct urb *urb,
613 struct tty_struct *tty)
615 struct usb_serial_port *port = urb->context;
616 char *packet = (char *)urb->transfer_buffer;
617 char flag = TTY_NORMAL;
618 u32 len = urb->actual_length;
619 int i;
620 char *ch;
622 dbg("%s - port %d", __func__, port->number);
624 if ((len >= 4) &&
625 (packet[0] == 0x1b) && (packet[1] == 0x1b) &&
626 ((packet[2] == 0x00) || (packet[2] == 0x01))) {
627 if (packet[2] == 0x00) {
628 ssu100_update_lsr(port, packet[3], &flag);
629 if (flag == TTY_OVERRUN)
630 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
632 if (packet[2] == 0x01)
633 ssu100_update_msr(port, packet[3]);
635 len -= 4;
636 ch = packet + 4;
637 } else
638 ch = packet;
640 if (!len)
641 return 0; /* status only */
643 if (port->port.console && port->sysrq) {
644 for (i = 0; i < len; i++, ch++) {
645 if (!usb_serial_handle_sysrq_char(port, *ch))
646 tty_insert_flip_char(tty, *ch, flag);
648 } else
649 tty_insert_flip_string_fixed_flag(tty, ch, flag, len);
651 return len;
654 static void ssu100_process_read_urb(struct urb *urb)
656 struct usb_serial_port *port = urb->context;
657 struct tty_struct *tty;
658 int count;
660 dbg("%s", __func__);
662 tty = tty_port_tty_get(&port->port);
663 if (!tty)
664 return;
666 count = ssu100_process_packet(urb, tty);
668 if (count)
669 tty_flip_buffer_push(tty);
670 tty_kref_put(tty);
673 static struct usb_serial_driver ssu100_device = {
674 .driver = {
675 .owner = THIS_MODULE,
676 .name = "ssu100",
678 .description = DRIVER_DESC,
679 .id_table = id_table,
680 .usb_driver = &ssu100_driver,
681 .num_ports = 1,
682 .open = ssu100_open,
683 .close = ssu100_close,
684 .attach = ssu100_attach,
685 .release = ssu100_release,
686 .dtr_rts = ssu100_dtr_rts,
687 .process_read_urb = ssu100_process_read_urb,
688 .tiocmget = ssu100_tiocmget,
689 .tiocmset = ssu100_tiocmset,
690 .get_icount = ssu100_get_icount,
691 .ioctl = ssu100_ioctl,
692 .set_termios = ssu100_set_termios,
693 .disconnect = usb_serial_generic_disconnect,
696 static int __init ssu100_init(void)
698 int retval;
700 dbg("%s", __func__);
702 /* register with usb-serial */
703 retval = usb_serial_register(&ssu100_device);
705 if (retval)
706 goto failed_usb_sio_register;
708 retval = usb_register(&ssu100_driver);
709 if (retval)
710 goto failed_usb_register;
712 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
713 DRIVER_DESC "\n");
715 return 0;
717 failed_usb_register:
718 usb_serial_deregister(&ssu100_device);
719 failed_usb_sio_register:
720 return retval;
723 static void __exit ssu100_exit(void)
725 usb_deregister(&ssu100_driver);
726 usb_serial_deregister(&ssu100_device);
729 module_init(ssu100_init);
730 module_exit(ssu100_exit);
732 MODULE_DESCRIPTION(DRIVER_DESC);
733 MODULE_LICENSE("GPL");
735 module_param(debug, bool, S_IRUGO | S_IWUSR);
736 MODULE_PARM_DESC(debug, "Debug enabled or not");