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 inline int ssu100_control_msg(struct usb_device
*dev
,
73 u8 request
, u16 data
, u16 index
)
75 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
76 request
, 0x40, data
, index
,
80 static inline int ssu100_setdevice(struct usb_device
*dev
, u8
*data
)
82 u16 x
= ((u16
)(data
[1] << 8) | (u16
)(data
[0]));
84 return ssu100_control_msg(dev
, QT_SET_GET_DEVICE
, x
, 0);
88 static inline int ssu100_getdevice(struct usb_device
*dev
, u8
*data
)
90 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
91 QT_SET_GET_DEVICE
, 0xc0, 0, 0,
95 static inline int ssu100_getregister(struct usb_device
*dev
,
100 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
101 QT_SET_GET_REGISTER
, 0xc0, reg
,
102 uart
, data
, sizeof(*data
), 300);
107 static inline int ssu100_setregister(struct usb_device
*dev
,
112 u16 value
= (data
<< 8) | reg
;
114 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
115 QT_SET_GET_REGISTER
, 0x40, value
, uart
,
120 #define set_mctrl(dev, set) update_mctrl((dev), (set), 0)
121 #define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear))
123 /* these do not deal with device that have more than 1 port */
124 static inline int update_mctrl(struct usb_device
*dev
, unsigned int set
,
130 if (((set
| clear
) & (TIOCM_DTR
| TIOCM_RTS
)) == 0) {
131 dbg("%s - DTR|RTS not being set|cleared", __func__
);
132 return 0; /* no change */
135 clear
&= ~set
; /* 'set' takes precedence over 'clear' */
138 urb_value
|= UART_MCR_DTR
;
140 urb_value
|= UART_MCR_RTS
;
142 result
= ssu100_setregister(dev
, 0, UART_MCR
, urb_value
);
144 dbg("%s Error from MODEM_CTRL urb", __func__
);
149 static int ssu100_initdevice(struct usb_device
*dev
)
154 data
= kzalloc(3, GFP_KERNEL
);
158 result
= ssu100_getdevice(dev
, data
);
160 dbg("%s - get_device failed %i", __func__
, result
);
164 data
[1] &= ~FULLPWRBIT
;
166 result
= ssu100_setdevice(dev
, data
);
168 dbg("%s - setdevice failed %i", __func__
, result
);
172 result
= ssu100_control_msg(dev
, QT_GET_SET_PREBUF_TRIG_LVL
, 128, 0);
174 dbg("%s - set prebuffer level failed %i", __func__
, result
);
178 result
= ssu100_control_msg(dev
, QT_SET_ATF
, ATC_DISABLED
, 0);
180 dbg("%s - set ATFprebuffer level failed %i", __func__
, result
);
184 result
= ssu100_getdevice(dev
, data
);
186 dbg("%s - get_device failed %i", __func__
, result
);
190 data
[0] &= ~(RR_BITS
| DUPMODE_BITS
);
192 data
[1] &= ~(LOOPMODE_BITS
);
193 data
[1] |= RS232_MODE
;
195 result
= ssu100_setdevice(dev
, data
);
197 dbg("%s - setdevice failed %i", __func__
, result
);
207 static void ssu100_set_termios(struct tty_struct
*tty
,
208 struct usb_serial_port
*port
,
209 struct ktermios
*old_termios
)
211 struct usb_device
*dev
= port
->serial
->dev
;
212 struct ktermios
*termios
= tty
->termios
;
213 u16 baud
, divisor
, remainder
;
214 unsigned int cflag
= termios
->c_cflag
;
215 u16 urb_value
= 0; /* will hold the new flags */
218 if (cflag
& PARENB
) {
220 urb_value
|= UART_LCR_PARITY
;
222 urb_value
|= SERIAL_EVEN_PARITY
;
225 switch (cflag
& CSIZE
) {
227 urb_value
|= UART_LCR_WLEN5
;
230 urb_value
|= UART_LCR_WLEN6
;
233 urb_value
|= UART_LCR_WLEN7
;
237 urb_value
|= UART_LCR_WLEN8
;
241 baud
= tty_get_baud_rate(tty
);
245 dbg("%s - got baud = %d\n", __func__
, baud
);
248 divisor
= MAX_BAUD_RATE
/ baud
;
249 remainder
= MAX_BAUD_RATE
% baud
;
250 if (((remainder
* 2) >= baud
) && (baud
!= 110))
253 urb_value
= urb_value
<< 8;
255 result
= ssu100_control_msg(dev
, QT_GET_SET_UART
, divisor
, urb_value
);
257 dbg("%s - set uart failed", __func__
);
260 result
= ssu100_control_msg(dev
, QT_HW_FLOW_CONTROL_MASK
,
263 result
= ssu100_control_msg(dev
, QT_HW_FLOW_CONTROL_MASK
,
266 dbg("%s - set HW flow control failed", __func__
);
268 if (I_IXOFF(tty
) || I_IXON(tty
)) {
269 u16 x
= ((u16
)(START_CHAR(tty
) << 8) | (u16
)(STOP_CHAR(tty
)));
271 result
= ssu100_control_msg(dev
, QT_SW_FLOW_CONTROL_MASK
,
274 result
= ssu100_control_msg(dev
, QT_SW_FLOW_CONTROL_MASK
,
278 dbg("%s - set SW flow control failed", __func__
);
283 static int ssu100_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
285 struct usb_device
*dev
= port
->serial
->dev
;
286 struct ssu100_port_private
*priv
= usb_get_serial_port_data(port
);
291 data
= kzalloc(2, GFP_KERNEL
);
295 result
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
296 QT_OPEN_CLOSE_CHANNEL
,
297 QT_TRANSFER_IN
, 0x01,
300 dbg("%s - open failed %i", __func__
, result
);
305 spin_lock_irqsave(&priv
->status_lock
, flags
);
306 priv
->shadowLSR
= data
[0];
307 priv
->shadowMSR
= data
[1];
308 spin_unlock_irqrestore(&priv
->status_lock
, flags
);
313 result
= ssu100_control_msg(dev
, QT_GET_SET_UART
, 0x30, 0x0300);
315 dbg("%s - set uart failed", __func__
);
318 ssu100_set_termios(tty
, port
, tty
->termios
);
320 return usb_serial_generic_open(tty
, port
);
323 static void ssu100_close(struct usb_serial_port
*port
)
325 usb_serial_generic_close(port
);
328 static int get_serial_info(struct usb_serial_port
*port
,
329 struct serial_struct __user
*retinfo
)
331 struct serial_struct tmp
;
336 memset(&tmp
, 0, sizeof(tmp
));
337 tmp
.line
= port
->serial
->minor
;
340 tmp
.flags
= ASYNC_SKIP_TEST
| ASYNC_AUTO_IRQ
;
341 tmp
.xmit_fifo_size
= port
->bulk_out_size
;
342 tmp
.baud_base
= 9600;
343 tmp
.close_delay
= 5*HZ
;
344 tmp
.closing_wait
= 30*HZ
;
346 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
351 static int wait_modem_info(struct usb_serial_port
*port
, unsigned int arg
)
353 struct ssu100_port_private
*priv
= usb_get_serial_port_data(port
);
354 struct async_icount prev
, cur
;
357 spin_lock_irqsave(&priv
->status_lock
, flags
);
359 spin_unlock_irqrestore(&priv
->status_lock
, flags
);
362 wait_event_interruptible(priv
->delta_msr_wait
,
363 ((priv
->icount
.rng
!= prev
.rng
) ||
364 (priv
->icount
.dsr
!= prev
.dsr
) ||
365 (priv
->icount
.dcd
!= prev
.dcd
) ||
366 (priv
->icount
.cts
!= prev
.cts
)));
368 if (signal_pending(current
))
371 spin_lock_irqsave(&priv
->status_lock
, flags
);
373 spin_unlock_irqrestore(&priv
->status_lock
, flags
);
375 if ((prev
.rng
== cur
.rng
) &&
376 (prev
.dsr
== cur
.dsr
) &&
377 (prev
.dcd
== cur
.dcd
) &&
378 (prev
.cts
== cur
.cts
))
381 if ((arg
& TIOCM_RNG
&& (prev
.rng
!= cur
.rng
)) ||
382 (arg
& TIOCM_DSR
&& (prev
.dsr
!= cur
.dsr
)) ||
383 (arg
& TIOCM_CD
&& (prev
.dcd
!= cur
.dcd
)) ||
384 (arg
& TIOCM_CTS
&& (prev
.cts
!= cur
.cts
)))
390 static int ssu100_get_icount(struct tty_struct
*tty
,
391 struct serial_icounter_struct
*icount
)
393 struct usb_serial_port
*port
= tty
->driver_data
;
394 struct ssu100_port_private
*priv
= usb_get_serial_port_data(port
);
395 struct async_icount cnow
= priv
->icount
;
397 icount
->cts
= cnow
.cts
;
398 icount
->dsr
= cnow
.dsr
;
399 icount
->rng
= cnow
.rng
;
400 icount
->dcd
= cnow
.dcd
;
401 icount
->rx
= cnow
.rx
;
402 icount
->tx
= cnow
.tx
;
403 icount
->frame
= cnow
.frame
;
404 icount
->overrun
= cnow
.overrun
;
405 icount
->parity
= cnow
.parity
;
406 icount
->brk
= cnow
.brk
;
407 icount
->buf_overrun
= cnow
.buf_overrun
;
414 static int ssu100_ioctl(struct tty_struct
*tty
,
415 unsigned int cmd
, unsigned long arg
)
417 struct usb_serial_port
*port
= tty
->driver_data
;
419 dbg("%s cmd 0x%04x", __func__
, cmd
);
423 return get_serial_info(port
,
424 (struct serial_struct __user
*) arg
);
427 return wait_modem_info(port
, arg
);
433 dbg("%s arg not supported", __func__
);
438 static int ssu100_attach(struct usb_serial
*serial
)
440 return ssu100_initdevice(serial
->dev
);
443 static int ssu100_port_probe(struct usb_serial_port
*port
)
445 struct ssu100_port_private
*priv
;
447 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
451 spin_lock_init(&priv
->status_lock
);
452 init_waitqueue_head(&priv
->delta_msr_wait
);
454 usb_set_serial_port_data(port
, priv
);
459 static int ssu100_port_remove(struct usb_serial_port
*port
)
461 struct ssu100_port_private
*priv
;
463 priv
= usb_get_serial_port_data(port
);
469 static int ssu100_tiocmget(struct tty_struct
*tty
)
471 struct usb_serial_port
*port
= tty
->driver_data
;
472 struct usb_device
*dev
= port
->serial
->dev
;
476 d
= kzalloc(2, GFP_KERNEL
);
480 r
= ssu100_getregister(dev
, 0, UART_MCR
, d
);
484 r
= ssu100_getregister(dev
, 0, UART_MSR
, d
+1);
488 r
= (d
[0] & UART_MCR_DTR
? TIOCM_DTR
: 0) |
489 (d
[0] & UART_MCR_RTS
? TIOCM_RTS
: 0) |
490 (d
[1] & UART_MSR_CTS
? TIOCM_CTS
: 0) |
491 (d
[1] & UART_MSR_DCD
? TIOCM_CAR
: 0) |
492 (d
[1] & UART_MSR_RI
? TIOCM_RI
: 0) |
493 (d
[1] & UART_MSR_DSR
? TIOCM_DSR
: 0);
500 static int ssu100_tiocmset(struct tty_struct
*tty
,
501 unsigned int set
, unsigned int clear
)
503 struct usb_serial_port
*port
= tty
->driver_data
;
504 struct usb_device
*dev
= port
->serial
->dev
;
506 return update_mctrl(dev
, set
, clear
);
509 static void ssu100_dtr_rts(struct usb_serial_port
*port
, int on
)
511 struct usb_device
*dev
= port
->serial
->dev
;
513 mutex_lock(&port
->serial
->disc_mutex
);
514 if (!port
->serial
->disconnected
) {
515 /* Disable flow control */
517 ssu100_setregister(dev
, 0, UART_MCR
, 0) < 0)
518 dev_err(&port
->dev
, "error from flowcontrol urb\n");
519 /* drop RTS and DTR */
521 set_mctrl(dev
, TIOCM_DTR
| TIOCM_RTS
);
523 clear_mctrl(dev
, TIOCM_DTR
| TIOCM_RTS
);
525 mutex_unlock(&port
->serial
->disc_mutex
);
528 static void ssu100_update_msr(struct usb_serial_port
*port
, u8 msr
)
530 struct ssu100_port_private
*priv
= usb_get_serial_port_data(port
);
533 spin_lock_irqsave(&priv
->status_lock
, flags
);
534 priv
->shadowMSR
= msr
;
535 spin_unlock_irqrestore(&priv
->status_lock
, flags
);
537 if (msr
& UART_MSR_ANY_DELTA
) {
538 /* update input line counters */
539 if (msr
& UART_MSR_DCTS
)
541 if (msr
& UART_MSR_DDSR
)
543 if (msr
& UART_MSR_DDCD
)
545 if (msr
& UART_MSR_TERI
)
547 wake_up_interruptible(&priv
->delta_msr_wait
);
551 static void ssu100_update_lsr(struct usb_serial_port
*port
, u8 lsr
,
554 struct ssu100_port_private
*priv
= usb_get_serial_port_data(port
);
557 spin_lock_irqsave(&priv
->status_lock
, flags
);
558 priv
->shadowLSR
= lsr
;
559 spin_unlock_irqrestore(&priv
->status_lock
, flags
);
561 *tty_flag
= TTY_NORMAL
;
562 if (lsr
& UART_LSR_BRK_ERROR_BITS
) {
563 /* we always want to update icount, but we only want to
564 * update tty_flag for one case */
565 if (lsr
& UART_LSR_BI
) {
567 *tty_flag
= TTY_BREAK
;
568 usb_serial_handle_break(port
);
570 if (lsr
& UART_LSR_PE
) {
571 priv
->icount
.parity
++;
572 if (*tty_flag
== TTY_NORMAL
)
573 *tty_flag
= TTY_PARITY
;
575 if (lsr
& UART_LSR_FE
) {
576 priv
->icount
.frame
++;
577 if (*tty_flag
== TTY_NORMAL
)
578 *tty_flag
= TTY_FRAME
;
580 if (lsr
& UART_LSR_OE
){
581 priv
->icount
.overrun
++;
582 if (*tty_flag
== TTY_NORMAL
)
583 *tty_flag
= TTY_OVERRUN
;
589 static int ssu100_process_packet(struct urb
*urb
,
590 struct tty_struct
*tty
)
592 struct usb_serial_port
*port
= urb
->context
;
593 char *packet
= (char *)urb
->transfer_buffer
;
594 char flag
= TTY_NORMAL
;
595 u32 len
= urb
->actual_length
;
600 (packet
[0] == 0x1b) && (packet
[1] == 0x1b) &&
601 ((packet
[2] == 0x00) || (packet
[2] == 0x01))) {
602 if (packet
[2] == 0x00) {
603 ssu100_update_lsr(port
, packet
[3], &flag
);
604 if (flag
== TTY_OVERRUN
)
605 tty_insert_flip_char(tty
, 0, TTY_OVERRUN
);
607 if (packet
[2] == 0x01)
608 ssu100_update_msr(port
, packet
[3]);
616 return 0; /* status only */
618 if (port
->port
.console
&& port
->sysrq
) {
619 for (i
= 0; i
< len
; i
++, ch
++) {
620 if (!usb_serial_handle_sysrq_char(port
, *ch
))
621 tty_insert_flip_char(tty
, *ch
, flag
);
624 tty_insert_flip_string_fixed_flag(tty
, ch
, flag
, len
);
629 static void ssu100_process_read_urb(struct urb
*urb
)
631 struct usb_serial_port
*port
= urb
->context
;
632 struct tty_struct
*tty
;
635 tty
= tty_port_tty_get(&port
->port
);
639 count
= ssu100_process_packet(urb
, tty
);
642 tty_flip_buffer_push(tty
);
646 static struct usb_serial_driver ssu100_device
= {
648 .owner
= THIS_MODULE
,
651 .description
= DRIVER_DESC
,
652 .id_table
= id_table
,
655 .close
= ssu100_close
,
656 .attach
= ssu100_attach
,
657 .port_probe
= ssu100_port_probe
,
658 .port_remove
= ssu100_port_remove
,
659 .dtr_rts
= ssu100_dtr_rts
,
660 .process_read_urb
= ssu100_process_read_urb
,
661 .tiocmget
= ssu100_tiocmget
,
662 .tiocmset
= ssu100_tiocmset
,
663 .get_icount
= ssu100_get_icount
,
664 .ioctl
= ssu100_ioctl
,
665 .set_termios
= ssu100_set_termios
,
666 .disconnect
= usb_serial_generic_disconnect
,
669 static struct usb_serial_driver
* const serial_drivers
[] = {
673 module_usb_serial_driver(serial_drivers
, id_table
);
675 MODULE_DESCRIPTION(DRIVER_DESC
);
676 MODULE_LICENSE("GPL");
678 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
679 MODULE_PARM_DESC(debug
, "Debug enabled or not");