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 */
62 MODULE_DEVICE_TABLE(usb, id_table);
64 struct ssu100_port_private {
65 spinlock_t status_lock;
68 wait_queue_head_t delta_msr_wait; /* Used for TIOCMIWAIT */
69 struct async_icount icount;
72 static void ssu100_release(struct usb_serial *serial)
74 struct ssu100_port_private *priv = usb_get_serial_port_data(*serial->port);
79 static inline int ssu100_control_msg(struct usb_device *dev,
80 u8 request, u16 data, u16 index)
82 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
83 request, 0x40, data, index,
87 static inline int ssu100_setdevice(struct usb_device *dev, u8 *data)
89 u16 x = ((u16)(data[1] << 8) | (u16)(data[0]));
91 return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0);
95 static inline int ssu100_getdevice(struct usb_device *dev, u8 *data)
97 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
98 QT_SET_GET_DEVICE, 0xc0, 0, 0,
102 static inline int ssu100_getregister(struct usb_device *dev,
107 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
108 QT_SET_GET_REGISTER, 0xc0, reg,
109 uart, data, sizeof(*data), 300);
114 static inline int ssu100_setregister(struct usb_device *dev,
119 u16 value = (data << 8) | reg;
121 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
122 QT_SET_GET_REGISTER, 0x40, value, uart,
127 #define set_mctrl(dev, set) update_mctrl((dev), (set), 0)
128 #define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear))
130 /* these do not deal with device that have more than 1 port */
131 static inline int update_mctrl(struct usb_device *dev, unsigned int set,
137 if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) {
138 dbg("%s - DTR|RTS not being set|cleared", __func__);
139 return 0; /* no change */
142 clear &= ~set; /* 'set' takes precedence over 'clear' */
145 urb_value |= UART_MCR_DTR;
147 urb_value |= UART_MCR_RTS;
149 result = ssu100_setregister(dev, 0, UART_MCR, urb_value);
151 dbg("%s Error from MODEM_CTRL urb", __func__);
156 static int ssu100_initdevice(struct usb_device *dev)
161 data = kzalloc(3, GFP_KERNEL);
165 result = ssu100_getdevice(dev, data);
167 dbg("%s - get_device failed %i", __func__, result);
171 data[1] &= ~FULLPWRBIT;
173 result = ssu100_setdevice(dev, data);
175 dbg("%s - setdevice failed %i", __func__, result);
179 result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0);
181 dbg("%s - set prebuffer level failed %i", __func__, result);
185 result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0);
187 dbg("%s - set ATFprebuffer level failed %i", __func__, result);
191 result = ssu100_getdevice(dev, data);
193 dbg("%s - get_device failed %i", __func__, result);
197 data[0] &= ~(RR_BITS | DUPMODE_BITS);
199 data[1] &= ~(LOOPMODE_BITS);
200 data[1] |= RS232_MODE;
202 result = ssu100_setdevice(dev, data);
204 dbg("%s - setdevice failed %i", __func__, result);
214 static void ssu100_set_termios(struct tty_struct *tty,
215 struct usb_serial_port *port,
216 struct ktermios *old_termios)
218 struct usb_device *dev = port->serial->dev;
219 struct ktermios *termios = tty->termios;
220 u16 baud, divisor, remainder;
221 unsigned int cflag = termios->c_cflag;
222 u16 urb_value = 0; /* will hold the new flags */
225 if (cflag & PARENB) {
227 urb_value |= UART_LCR_PARITY;
229 urb_value |= SERIAL_EVEN_PARITY;
232 switch (cflag & CSIZE) {
234 urb_value |= UART_LCR_WLEN5;
237 urb_value |= UART_LCR_WLEN6;
240 urb_value |= UART_LCR_WLEN7;
244 urb_value |= UART_LCR_WLEN8;
248 baud = tty_get_baud_rate(tty);
252 dbg("%s - got baud = %d\n", __func__, baud);
255 divisor = MAX_BAUD_RATE / baud;
256 remainder = MAX_BAUD_RATE % baud;
257 if (((remainder * 2) >= baud) && (baud != 110))
260 urb_value = urb_value << 8;
262 result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value);
264 dbg("%s - set uart failed", __func__);
267 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
270 result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
273 dbg("%s - set HW flow control failed", __func__);
275 if (I_IXOFF(tty) || I_IXON(tty)) {
276 u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty)));
278 result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
281 result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
285 dbg("%s - set SW flow control failed", __func__);
290 static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port)
292 struct usb_device *dev = port->serial->dev;
293 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
298 data = kzalloc(2, GFP_KERNEL);
302 result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
303 QT_OPEN_CLOSE_CHANNEL,
304 QT_TRANSFER_IN, 0x01,
307 dbg("%s - open failed %i", __func__, result);
312 spin_lock_irqsave(&priv->status_lock, flags);
313 priv->shadowLSR = data[0];
314 priv->shadowMSR = data[1];
315 spin_unlock_irqrestore(&priv->status_lock, flags);
320 result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300);
322 dbg("%s - set uart failed", __func__);
325 ssu100_set_termios(tty, port, tty->termios);
327 return usb_serial_generic_open(tty, port);
330 static void ssu100_close(struct usb_serial_port *port)
332 usb_serial_generic_close(port);
335 static int get_serial_info(struct usb_serial_port *port,
336 struct serial_struct __user *retinfo)
338 struct serial_struct tmp;
343 memset(&tmp, 0, sizeof(tmp));
344 tmp.line = port->serial->minor;
347 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
348 tmp.xmit_fifo_size = port->bulk_out_size;
349 tmp.baud_base = 9600;
350 tmp.close_delay = 5*HZ;
351 tmp.closing_wait = 30*HZ;
353 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
358 static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
360 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
361 struct async_icount prev, cur;
364 spin_lock_irqsave(&priv->status_lock, flags);
366 spin_unlock_irqrestore(&priv->status_lock, flags);
369 wait_event_interruptible(priv->delta_msr_wait,
370 ((priv->icount.rng != prev.rng) ||
371 (priv->icount.dsr != prev.dsr) ||
372 (priv->icount.dcd != prev.dcd) ||
373 (priv->icount.cts != prev.cts)));
375 if (signal_pending(current))
378 spin_lock_irqsave(&priv->status_lock, flags);
380 spin_unlock_irqrestore(&priv->status_lock, flags);
382 if ((prev.rng == cur.rng) &&
383 (prev.dsr == cur.dsr) &&
384 (prev.dcd == cur.dcd) &&
385 (prev.cts == cur.cts))
388 if ((arg & TIOCM_RNG && (prev.rng != cur.rng)) ||
389 (arg & TIOCM_DSR && (prev.dsr != cur.dsr)) ||
390 (arg & TIOCM_CD && (prev.dcd != cur.dcd)) ||
391 (arg & TIOCM_CTS && (prev.cts != cur.cts)))
397 static int ssu100_get_icount(struct tty_struct *tty,
398 struct serial_icounter_struct *icount)
400 struct usb_serial_port *port = tty->driver_data;
401 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
402 struct async_icount cnow = priv->icount;
404 icount->cts = cnow.cts;
405 icount->dsr = cnow.dsr;
406 icount->rng = cnow.rng;
407 icount->dcd = cnow.dcd;
408 icount->rx = cnow.rx;
409 icount->tx = cnow.tx;
410 icount->frame = cnow.frame;
411 icount->overrun = cnow.overrun;
412 icount->parity = cnow.parity;
413 icount->brk = cnow.brk;
414 icount->buf_overrun = cnow.buf_overrun;
421 static int ssu100_ioctl(struct tty_struct *tty,
422 unsigned int cmd, unsigned long arg)
424 struct usb_serial_port *port = tty->driver_data;
426 dbg("%s cmd 0x%04x", __func__, cmd);
430 return get_serial_info(port,
431 (struct serial_struct __user *) arg);
434 return wait_modem_info(port, arg);
440 dbg("%s arg not supported", __func__);
445 static int ssu100_attach(struct usb_serial *serial)
447 struct ssu100_port_private *priv;
448 struct usb_serial_port *port = *serial->port;
450 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
452 dev_err(&port->dev, "%s- kmalloc(%Zd) failed.\n", __func__,
457 spin_lock_init(&priv->status_lock);
458 init_waitqueue_head(&priv->delta_msr_wait);
459 usb_set_serial_port_data(port, priv);
461 return ssu100_initdevice(serial->dev);
464 static int ssu100_tiocmget(struct tty_struct *tty)
466 struct usb_serial_port *port = tty->driver_data;
467 struct usb_device *dev = port->serial->dev;
471 d = kzalloc(2, GFP_KERNEL);
475 r = ssu100_getregister(dev, 0, UART_MCR, d);
479 r = ssu100_getregister(dev, 0, UART_MSR, d+1);
483 r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) |
484 (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) |
485 (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) |
486 (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) |
487 (d[1] & UART_MSR_RI ? TIOCM_RI : 0) |
488 (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0);
495 static int ssu100_tiocmset(struct tty_struct *tty,
496 unsigned int set, unsigned int clear)
498 struct usb_serial_port *port = tty->driver_data;
499 struct usb_device *dev = port->serial->dev;
501 return update_mctrl(dev, set, clear);
504 static void ssu100_dtr_rts(struct usb_serial_port *port, int on)
506 struct usb_device *dev = port->serial->dev;
508 mutex_lock(&port->serial->disc_mutex);
509 if (!port->serial->disconnected) {
510 /* Disable flow control */
512 ssu100_setregister(dev, 0, UART_MCR, 0) < 0)
513 dev_err(&port->dev, "error from flowcontrol urb\n");
514 /* drop RTS and DTR */
516 set_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
518 clear_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
520 mutex_unlock(&port->serial->disc_mutex);
523 static void ssu100_update_msr(struct usb_serial_port *port, u8 msr)
525 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
528 spin_lock_irqsave(&priv->status_lock, flags);
529 priv->shadowMSR = msr;
530 spin_unlock_irqrestore(&priv->status_lock, flags);
532 if (msr & UART_MSR_ANY_DELTA) {
533 /* update input line counters */
534 if (msr & UART_MSR_DCTS)
536 if (msr & UART_MSR_DDSR)
538 if (msr & UART_MSR_DDCD)
540 if (msr & UART_MSR_TERI)
542 wake_up_interruptible(&priv->delta_msr_wait);
546 static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr,
549 struct ssu100_port_private *priv = usb_get_serial_port_data(port);
552 spin_lock_irqsave(&priv->status_lock, flags);
553 priv->shadowLSR = lsr;
554 spin_unlock_irqrestore(&priv->status_lock, flags);
556 *tty_flag = TTY_NORMAL;
557 if (lsr & UART_LSR_BRK_ERROR_BITS) {
558 /* we always want to update icount, but we only want to
559 * update tty_flag for one case */
560 if (lsr & UART_LSR_BI) {
562 *tty_flag = TTY_BREAK;
563 usb_serial_handle_break(port);
565 if (lsr & UART_LSR_PE) {
566 priv->icount.parity++;
567 if (*tty_flag == TTY_NORMAL)
568 *tty_flag = TTY_PARITY;
570 if (lsr & UART_LSR_FE) {
571 priv->icount.frame++;
572 if (*tty_flag == TTY_NORMAL)
573 *tty_flag = TTY_FRAME;
575 if (lsr & UART_LSR_OE){
576 priv->icount.overrun++;
577 if (*tty_flag == TTY_NORMAL)
578 *tty_flag = TTY_OVERRUN;
584 static int ssu100_process_packet(struct urb *urb,
585 struct tty_struct *tty)
587 struct usb_serial_port *port = urb->context;
588 char *packet = (char *)urb->transfer_buffer;
589 char flag = TTY_NORMAL;
590 u32 len = urb->actual_length;
595 (packet[0] == 0x1b) && (packet[1] == 0x1b) &&
596 ((packet[2] == 0x00) || (packet[2] == 0x01))) {
597 if (packet[2] == 0x00) {
598 ssu100_update_lsr(port, packet[3], &flag);
599 if (flag == TTY_OVERRUN)
600 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
602 if (packet[2] == 0x01)
603 ssu100_update_msr(port, packet[3]);
611 return 0; /* status only */
613 if (port->port.console && port->sysrq) {
614 for (i = 0; i < len; i++, ch++) {
615 if (!usb_serial_handle_sysrq_char(port, *ch))
616 tty_insert_flip_char(tty, *ch, flag);
619 tty_insert_flip_string_fixed_flag(tty, ch, flag, len);
624 static void ssu100_process_read_urb(struct urb *urb)
626 struct usb_serial_port *port = urb->context;
627 struct tty_struct *tty;
630 tty = tty_port_tty_get(&port->port);
634 count = ssu100_process_packet(urb, tty);
637 tty_flip_buffer_push(tty);
641 static struct usb_serial_driver ssu100_device = {
643 .owner = THIS_MODULE,
646 .description = DRIVER_DESC,
647 .id_table = id_table,
650 .close = ssu100_close,
651 .attach = ssu100_attach,
652 .release = ssu100_release,
653 .dtr_rts = ssu100_dtr_rts,
654 .process_read_urb = ssu100_process_read_urb,
655 .tiocmget = ssu100_tiocmget,
656 .tiocmset = ssu100_tiocmset,
657 .get_icount = ssu100_get_icount,
658 .ioctl = ssu100_ioctl,
659 .set_termios = ssu100_set_termios,
660 .disconnect = usb_serial_generic_disconnect,
663 static struct usb_serial_driver * const serial_drivers[] = {
667 module_usb_serial_driver(serial_drivers, id_table);
669 MODULE_DESCRIPTION(DRIVER_DESC);
670 MODULE_LICENSE("GPL");
672 module_param(debug, bool, S_IRUGO | S_IWUSR);
673 MODULE_PARM_DESC(debug, "Debug enabled or not");