2 * usb-serial driver for Quatech SSU-100
4 * based on ftdi_sio.c and the original serqt_usb.c from Quatech
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
42 #define LOOPMODE_BITS 0x41
43 #define RS232_MODE 0x00
44 #define RTSCTS_TO_CONNECTOR 0x40
46 #define FULLPWRBIT 0x00000080
47 #define NEXT_BOARD_POWER_BIT 0x00000004
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 = {
68 .probe = usb_serial_probe,
69 .disconnect = usb_serial_disconnect,
71 .suspend = usb_serial_suspend,
72 .resume = usb_serial_resume,
74 .supports_autosuspend = 1,
77 struct ssu100_port_private {
78 spinlock_t status_lock;
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);
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,
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,
116 static inline int ssu100_getregister(struct usb_device *dev,
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,
133 u16 value = (data << 8) | reg;
135 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
136 QT_SET_GET_REGISTER, 0x40, value, uart,
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,
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' */
159 urb_value |= UART_MCR_DTR;
161 urb_value |= UART_MCR_RTS;
163 result = ssu100_setregister(dev, 0, UART_MCR, urb_value);
165 dbg("%s Error from MODEM_CTRL urb", __func__);
170 static int ssu100_initdevice(struct usb_device *dev)
177 data = kzalloc(3, GFP_KERNEL);
181 result = ssu100_getdevice(dev, data);
183 dbg("%s - get_device failed %i", __func__, result);
187 data[1] &= ~FULLPWRBIT;
189 result = ssu100_setdevice(dev, data);
191 dbg("%s - setdevice failed %i", __func__, result);
195 result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0);
197 dbg("%s - set prebuffer level failed %i", __func__, result);
201 result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0);
203 dbg("%s - set ATFprebuffer level failed %i", __func__, result);
207 result = ssu100_getdevice(dev, data);
209 dbg("%s - get_device failed %i", __func__, result);
213 data[0] &= ~(RR_BITS | DUPMODE_BITS);
215 data[1] &= ~(LOOPMODE_BITS);
216 data[1] |= RS232_MODE;
218 result = ssu100_setdevice(dev, data);
220 dbg("%s - setdevice failed %i", __func__, 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 */
243 if (cflag & PARENB) {
245 urb_value |= UART_LCR_PARITY;
247 urb_value |= SERIAL_EVEN_PARITY;
250 switch (cflag & CSIZE) {
252 urb_value |= UART_LCR_WLEN5;
255 urb_value |= UART_LCR_WLEN6;
258 urb_value |= UART_LCR_WLEN7;
262 urb_value |= UART_LCR_WLEN8;
266 baud = tty_get_baud_rate(tty);
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))
278 urb_value = urb_value << 8;
280 result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value);
282 dbg("%s - set uart failed", __func__);
285 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
288 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
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,
299 result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
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);
316 dbg("%s - port %d", __func__, port->number);
318 data = kzalloc(2, GFP_KERNEL);
322 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
323 QT_OPEN_CLOSE_CHANNEL,
324 QT_TRANSFER_IN, 0x01,
327 dbg("%s - open failed %i", __func__, 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);
340 result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300);
342 dbg("%s - set uart failed", __func__);
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)
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;
364 memset(&tmp, 0, sizeof(tmp));
365 tmp.line = port->serial->minor;
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)))
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;
385 spin_lock_irqsave(&priv->status_lock, flags);
387 spin_unlock_irqrestore(&priv->status_lock, flags);
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))
399 spin_lock_irqsave(&priv->status_lock, flags);
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))
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)))
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;
442 static int ssu100_ioctl(struct tty_struct *tty, struct file *file,
443 unsigned int cmd, unsigned long arg)
445 struct usb_serial_port *port = tty->driver_data;
447 dbg("%s cmd 0x%04x", __func__, cmd);
451 return get_serial_info(port,
452 (struct serial_struct __user *) arg);
455 return wait_modem_info(port, arg);
461 dbg("%s arg not supported", __func__);
466 static int ssu100_attach(struct usb_serial *serial)
468 struct ssu100_port_private *priv;
469 struct usb_serial_port *port = *serial->port;
473 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
475 dev_err(&port->dev, "%s- kmalloc(%Zd) failed.\n", __func__,
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, struct file *file)
489 struct usb_serial_port *port = tty->driver_data;
490 struct usb_device *dev = port->serial->dev;
494 dbg("%s\n", __func__);
496 d = kzalloc(2, GFP_KERNEL);
500 r = ssu100_getregister(dev, 0, UART_MCR, d);
504 r = ssu100_getregister(dev, 0, UART_MSR, d+1);
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);
520 static int ssu100_tiocmset(struct tty_struct *tty, struct file *file,
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 */
540 ssu100_setregister(dev, 0, UART_MCR, 0) < 0)
541 dev_err(&port->dev, "error from flowcontrol urb\n");
542 /* drop RTS and DTR */
544 set_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
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);
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)
564 if (msr & UART_MSR_DDSR)
566 if (msr & UART_MSR_DDCD)
568 if (msr & UART_MSR_TERI)
570 wake_up_interruptible(&priv->delta_msr_wait);
574 static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr,
577 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
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) {
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;
622 dbg("%s - port %d", __func__, port->number);
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]);
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);
649 tty_insert_flip_string_fixed_flag(tty, ch, flag, len);
654 static void ssu100_process_read_urb(struct urb *urb)
656 struct usb_serial_port *port = urb->context;
657 struct tty_struct *tty;
662 tty = tty_port_tty_get(&port->port);
666 count = ssu100_process_packet(urb, tty);
669 tty_flip_buffer_push(tty);
673 static struct usb_serial_driver ssu100_device = {
675 .owner = THIS_MODULE,
678 .description = DRIVER_DESC,
679 .id_table = id_table,
680 .usb_driver = &ssu100_driver,
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)
702 /* register with usb-serial */
703 retval = usb_serial_register(&ssu100_device);
706 goto failed_usb_sio_register;
708 retval = usb_register(&ssu100_driver);
710 goto failed_usb_register;
712 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
718 usb_serial_deregister(&ssu100_device);
719 failed_usb_sio_register:
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");