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/slab.h>
10 #include <linux/tty.h>
11 #include <linux/tty_driver.h>
12 #include <linux/tty_flip.h>
13 #include <linux/module.h>
14 #include <linux/serial.h>
15 #include <linux/usb.h>
16 #include <linux/usb/serial.h>
17 #include <linux/serial_reg.h>
18 #include <linux/uaccess.h>
20 #define QT_OPEN_CLOSE_CHANNEL 0xca
21 #define QT_SET_GET_DEVICE 0xc2
22 #define QT_SET_GET_REGISTER 0xc0
23 #define QT_GET_SET_PREBUF_TRIG_LVL 0xcc
24 #define QT_SET_ATF 0xcd
25 #define QT_GET_SET_UART 0xc1
26 #define QT_TRANSFER_IN 0xc0
27 #define QT_HW_FLOW_CONTROL_MASK 0xc5
28 #define QT_SW_FLOW_CONTROL_MASK 0xc6
30 #define SERIAL_MSR_MASK 0xf0
32 #define SERIAL_CRTSCTS ((UART_MCR_RTS << 8) | UART_MSR_CTS)
34 #define SERIAL_EVEN_PARITY (UART_LCR_PARITY | UART_LCR_EPAR)
36 #define MAX_BAUD_RATE 460800
38 #define ATC_DISABLED 0x00
39 #define DUPMODE_BITS 0xc0
41 #define LOOPMODE_BITS 0x41
42 #define RS232_MODE 0x00
43 #define RTSCTS_TO_CONNECTOR 0x40
45 #define FULLPWRBIT 0x00000080
46 #define NEXT_BOARD_POWER_BIT 0x00000004
48 #define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver"
50 #define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */
51 #define QUATECH_SSU100 0xC020 /* SSU100 */
53 static const struct usb_device_id id_table
[] = {
54 {USB_DEVICE(USB_VENDOR_ID_QUATECH
, QUATECH_SSU100
)},
55 {} /* Terminating entry */
57 MODULE_DEVICE_TABLE(usb
, id_table
);
59 struct ssu100_port_private
{
60 spinlock_t status_lock
;
65 static inline int ssu100_control_msg(struct usb_device
*dev
,
66 u8 request
, u16 data
, u16 index
)
68 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
69 request
, 0x40, data
, index
,
73 static inline int ssu100_setdevice(struct usb_device
*dev
, u8
*data
)
75 u16 x
= ((u16
)(data
[1] << 8) | (u16
)(data
[0]));
77 return ssu100_control_msg(dev
, QT_SET_GET_DEVICE
, x
, 0);
81 static inline int ssu100_getdevice(struct usb_device
*dev
, u8
*data
)
83 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
84 QT_SET_GET_DEVICE
, 0xc0, 0, 0,
88 static inline int ssu100_getregister(struct usb_device
*dev
,
93 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
94 QT_SET_GET_REGISTER
, 0xc0, reg
,
95 uart
, data
, sizeof(*data
), 300);
100 static inline int ssu100_setregister(struct usb_device
*dev
,
105 u16 value
= (data
<< 8) | reg
;
107 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
108 QT_SET_GET_REGISTER
, 0x40, value
, uart
,
113 #define set_mctrl(dev, set) update_mctrl((dev), (set), 0)
114 #define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear))
116 /* these do not deal with device that have more than 1 port */
117 static inline int update_mctrl(struct usb_device
*dev
, unsigned int set
,
123 if (((set
| clear
) & (TIOCM_DTR
| TIOCM_RTS
)) == 0) {
124 dev_dbg(&dev
->dev
, "%s - DTR|RTS not being set|cleared\n", __func__
);
125 return 0; /* no change */
128 clear
&= ~set
; /* 'set' takes precedence over 'clear' */
131 urb_value
|= UART_MCR_DTR
;
133 urb_value
|= UART_MCR_RTS
;
135 result
= ssu100_setregister(dev
, 0, UART_MCR
, urb_value
);
137 dev_dbg(&dev
->dev
, "%s Error from MODEM_CTRL urb\n", __func__
);
142 static int ssu100_initdevice(struct usb_device
*dev
)
147 data
= kzalloc(3, GFP_KERNEL
);
151 result
= ssu100_getdevice(dev
, data
);
153 dev_dbg(&dev
->dev
, "%s - get_device failed %i\n", __func__
, result
);
157 data
[1] &= ~FULLPWRBIT
;
159 result
= ssu100_setdevice(dev
, data
);
161 dev_dbg(&dev
->dev
, "%s - setdevice failed %i\n", __func__
, result
);
165 result
= ssu100_control_msg(dev
, QT_GET_SET_PREBUF_TRIG_LVL
, 128, 0);
167 dev_dbg(&dev
->dev
, "%s - set prebuffer level failed %i\n", __func__
, result
);
171 result
= ssu100_control_msg(dev
, QT_SET_ATF
, ATC_DISABLED
, 0);
173 dev_dbg(&dev
->dev
, "%s - set ATFprebuffer level failed %i\n", __func__
, result
);
177 result
= ssu100_getdevice(dev
, data
);
179 dev_dbg(&dev
->dev
, "%s - get_device failed %i\n", __func__
, result
);
183 data
[0] &= ~(RR_BITS
| DUPMODE_BITS
);
185 data
[1] &= ~(LOOPMODE_BITS
);
186 data
[1] |= RS232_MODE
;
188 result
= ssu100_setdevice(dev
, data
);
190 dev_dbg(&dev
->dev
, "%s - setdevice failed %i\n", __func__
, result
);
200 static void ssu100_set_termios(struct tty_struct
*tty
,
201 struct usb_serial_port
*port
,
202 struct ktermios
*old_termios
)
204 struct usb_device
*dev
= port
->serial
->dev
;
205 struct ktermios
*termios
= &tty
->termios
;
206 u16 baud
, divisor
, remainder
;
207 unsigned int cflag
= termios
->c_cflag
;
208 u16 urb_value
= 0; /* will hold the new flags */
211 if (cflag
& PARENB
) {
213 urb_value
|= UART_LCR_PARITY
;
215 urb_value
|= SERIAL_EVEN_PARITY
;
218 switch (cflag
& CSIZE
) {
220 urb_value
|= UART_LCR_WLEN5
;
223 urb_value
|= UART_LCR_WLEN6
;
226 urb_value
|= UART_LCR_WLEN7
;
230 urb_value
|= UART_LCR_WLEN8
;
234 baud
= tty_get_baud_rate(tty
);
238 dev_dbg(&port
->dev
, "%s - got baud = %d\n", __func__
, baud
);
241 divisor
= MAX_BAUD_RATE
/ baud
;
242 remainder
= MAX_BAUD_RATE
% baud
;
243 if (((remainder
* 2) >= baud
) && (baud
!= 110))
246 urb_value
= urb_value
<< 8;
248 result
= ssu100_control_msg(dev
, QT_GET_SET_UART
, divisor
, urb_value
);
250 dev_dbg(&port
->dev
, "%s - set uart failed\n", __func__
);
253 result
= ssu100_control_msg(dev
, QT_HW_FLOW_CONTROL_MASK
,
256 result
= ssu100_control_msg(dev
, QT_HW_FLOW_CONTROL_MASK
,
259 dev_dbg(&port
->dev
, "%s - set HW flow control failed\n", __func__
);
261 if (I_IXOFF(tty
) || I_IXON(tty
)) {
262 u16 x
= ((u16
)(START_CHAR(tty
) << 8) | (u16
)(STOP_CHAR(tty
)));
264 result
= ssu100_control_msg(dev
, QT_SW_FLOW_CONTROL_MASK
,
267 result
= ssu100_control_msg(dev
, QT_SW_FLOW_CONTROL_MASK
,
271 dev_dbg(&port
->dev
, "%s - set SW flow control failed\n", __func__
);
276 static int ssu100_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
278 struct usb_device
*dev
= port
->serial
->dev
;
279 struct ssu100_port_private
*priv
= usb_get_serial_port_data(port
);
284 data
= kzalloc(2, GFP_KERNEL
);
288 result
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
289 QT_OPEN_CLOSE_CHANNEL
,
290 QT_TRANSFER_IN
, 0x01,
293 dev_dbg(&port
->dev
, "%s - open failed %i\n", __func__
, result
);
298 spin_lock_irqsave(&priv
->status_lock
, flags
);
299 priv
->shadowLSR
= data
[0];
300 priv
->shadowMSR
= data
[1];
301 spin_unlock_irqrestore(&priv
->status_lock
, flags
);
306 result
= ssu100_control_msg(dev
, QT_GET_SET_UART
, 0x30, 0x0300);
308 dev_dbg(&port
->dev
, "%s - set uart failed\n", __func__
);
311 ssu100_set_termios(tty
, port
, &tty
->termios
);
313 return usb_serial_generic_open(tty
, port
);
316 static int get_serial_info(struct usb_serial_port
*port
,
317 struct serial_struct __user
*retinfo
)
319 struct serial_struct tmp
;
324 memset(&tmp
, 0, sizeof(tmp
));
325 tmp
.line
= port
->minor
;
328 tmp
.flags
= ASYNC_SKIP_TEST
| ASYNC_AUTO_IRQ
;
329 tmp
.xmit_fifo_size
= port
->bulk_out_size
;
330 tmp
.baud_base
= 9600;
331 tmp
.close_delay
= 5*HZ
;
332 tmp
.closing_wait
= 30*HZ
;
334 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
339 static int ssu100_ioctl(struct tty_struct
*tty
,
340 unsigned int cmd
, unsigned long arg
)
342 struct usb_serial_port
*port
= tty
->driver_data
;
346 return get_serial_info(port
,
347 (struct serial_struct __user
*) arg
);
355 static int ssu100_attach(struct usb_serial
*serial
)
357 return ssu100_initdevice(serial
->dev
);
360 static int ssu100_port_probe(struct usb_serial_port
*port
)
362 struct ssu100_port_private
*priv
;
364 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
368 spin_lock_init(&priv
->status_lock
);
370 usb_set_serial_port_data(port
, priv
);
375 static int ssu100_port_remove(struct usb_serial_port
*port
)
377 struct ssu100_port_private
*priv
;
379 priv
= usb_get_serial_port_data(port
);
385 static int ssu100_tiocmget(struct tty_struct
*tty
)
387 struct usb_serial_port
*port
= tty
->driver_data
;
388 struct usb_device
*dev
= port
->serial
->dev
;
392 d
= kzalloc(2, GFP_KERNEL
);
396 r
= ssu100_getregister(dev
, 0, UART_MCR
, d
);
400 r
= ssu100_getregister(dev
, 0, UART_MSR
, d
+1);
404 r
= (d
[0] & UART_MCR_DTR
? TIOCM_DTR
: 0) |
405 (d
[0] & UART_MCR_RTS
? TIOCM_RTS
: 0) |
406 (d
[1] & UART_MSR_CTS
? TIOCM_CTS
: 0) |
407 (d
[1] & UART_MSR_DCD
? TIOCM_CAR
: 0) |
408 (d
[1] & UART_MSR_RI
? TIOCM_RI
: 0) |
409 (d
[1] & UART_MSR_DSR
? TIOCM_DSR
: 0);
416 static int ssu100_tiocmset(struct tty_struct
*tty
,
417 unsigned int set
, unsigned int clear
)
419 struct usb_serial_port
*port
= tty
->driver_data
;
420 struct usb_device
*dev
= port
->serial
->dev
;
422 return update_mctrl(dev
, set
, clear
);
425 static void ssu100_dtr_rts(struct usb_serial_port
*port
, int on
)
427 struct usb_device
*dev
= port
->serial
->dev
;
429 /* Disable flow control */
431 if (ssu100_setregister(dev
, 0, UART_MCR
, 0) < 0)
432 dev_err(&port
->dev
, "error from flowcontrol urb\n");
434 /* drop RTS and DTR */
436 set_mctrl(dev
, TIOCM_DTR
| TIOCM_RTS
);
438 clear_mctrl(dev
, TIOCM_DTR
| TIOCM_RTS
);
441 static void ssu100_update_msr(struct usb_serial_port
*port
, u8 msr
)
443 struct ssu100_port_private
*priv
= usb_get_serial_port_data(port
);
446 spin_lock_irqsave(&priv
->status_lock
, flags
);
447 priv
->shadowMSR
= msr
;
448 spin_unlock_irqrestore(&priv
->status_lock
, flags
);
450 if (msr
& UART_MSR_ANY_DELTA
) {
451 /* update input line counters */
452 if (msr
& UART_MSR_DCTS
)
454 if (msr
& UART_MSR_DDSR
)
456 if (msr
& UART_MSR_DDCD
)
458 if (msr
& UART_MSR_TERI
)
460 wake_up_interruptible(&port
->port
.delta_msr_wait
);
464 static void ssu100_update_lsr(struct usb_serial_port
*port
, u8 lsr
,
467 struct ssu100_port_private
*priv
= usb_get_serial_port_data(port
);
470 spin_lock_irqsave(&priv
->status_lock
, flags
);
471 priv
->shadowLSR
= lsr
;
472 spin_unlock_irqrestore(&priv
->status_lock
, flags
);
474 *tty_flag
= TTY_NORMAL
;
475 if (lsr
& UART_LSR_BRK_ERROR_BITS
) {
476 /* we always want to update icount, but we only want to
477 * update tty_flag for one case */
478 if (lsr
& UART_LSR_BI
) {
480 *tty_flag
= TTY_BREAK
;
481 usb_serial_handle_break(port
);
483 if (lsr
& UART_LSR_PE
) {
484 port
->icount
.parity
++;
485 if (*tty_flag
== TTY_NORMAL
)
486 *tty_flag
= TTY_PARITY
;
488 if (lsr
& UART_LSR_FE
) {
489 port
->icount
.frame
++;
490 if (*tty_flag
== TTY_NORMAL
)
491 *tty_flag
= TTY_FRAME
;
493 if (lsr
& UART_LSR_OE
){
494 port
->icount
.overrun
++;
495 if (*tty_flag
== TTY_NORMAL
)
496 *tty_flag
= TTY_OVERRUN
;
502 static void ssu100_process_read_urb(struct urb
*urb
)
504 struct usb_serial_port
*port
= urb
->context
;
505 char *packet
= (char *)urb
->transfer_buffer
;
506 char flag
= TTY_NORMAL
;
507 u32 len
= urb
->actual_length
;
512 (packet
[0] == 0x1b) && (packet
[1] == 0x1b) &&
513 ((packet
[2] == 0x00) || (packet
[2] == 0x01))) {
514 if (packet
[2] == 0x00) {
515 ssu100_update_lsr(port
, packet
[3], &flag
);
516 if (flag
== TTY_OVERRUN
)
517 tty_insert_flip_char(&port
->port
, 0,
520 if (packet
[2] == 0x01)
521 ssu100_update_msr(port
, packet
[3]);
529 return; /* status only */
531 if (port
->port
.console
&& port
->sysrq
) {
532 for (i
= 0; i
< len
; i
++, ch
++) {
533 if (!usb_serial_handle_sysrq_char(port
, *ch
))
534 tty_insert_flip_char(&port
->port
, *ch
, flag
);
537 tty_insert_flip_string_fixed_flag(&port
->port
, ch
, flag
, len
);
539 tty_flip_buffer_push(&port
->port
);
542 static struct usb_serial_driver ssu100_device
= {
544 .owner
= THIS_MODULE
,
547 .description
= DRIVER_DESC
,
548 .id_table
= id_table
,
551 .attach
= ssu100_attach
,
552 .port_probe
= ssu100_port_probe
,
553 .port_remove
= ssu100_port_remove
,
554 .dtr_rts
= ssu100_dtr_rts
,
555 .process_read_urb
= ssu100_process_read_urb
,
556 .tiocmget
= ssu100_tiocmget
,
557 .tiocmset
= ssu100_tiocmset
,
558 .tiocmiwait
= usb_serial_generic_tiocmiwait
,
559 .get_icount
= usb_serial_generic_get_icount
,
560 .ioctl
= ssu100_ioctl
,
561 .set_termios
= ssu100_set_termios
,
564 static struct usb_serial_driver
* const serial_drivers
[] = {
568 module_usb_serial_driver(serial_drivers
, id_table
);
570 MODULE_DESCRIPTION(DRIVER_DESC
);
571 MODULE_LICENSE("GPL");