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
49 #define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver"
51 #define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */
52 #define QUATECH_SSU100 0xC020 /* SSU100 */
54 static const struct usb_device_id id_table
[] = {
55 {USB_DEVICE(USB_VENDOR_ID_QUATECH
, QUATECH_SSU100
)},
56 {} /* Terminating entry */
58 MODULE_DEVICE_TABLE(usb
, id_table
);
60 struct ssu100_port_private
{
61 spinlock_t status_lock
;
66 static inline int ssu100_control_msg(struct usb_device
*dev
,
67 u8 request
, u16 data
, u16 index
)
69 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
70 request
, 0x40, data
, index
,
74 static inline int ssu100_setdevice(struct usb_device
*dev
, u8
*data
)
76 u16 x
= ((u16
)(data
[1] << 8) | (u16
)(data
[0]));
78 return ssu100_control_msg(dev
, QT_SET_GET_DEVICE
, x
, 0);
82 static inline int ssu100_getdevice(struct usb_device
*dev
, u8
*data
)
84 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
85 QT_SET_GET_DEVICE
, 0xc0, 0, 0,
89 static inline int ssu100_getregister(struct usb_device
*dev
,
94 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
95 QT_SET_GET_REGISTER
, 0xc0, reg
,
96 uart
, data
, sizeof(*data
), 300);
101 static inline int ssu100_setregister(struct usb_device
*dev
,
106 u16 value
= (data
<< 8) | reg
;
108 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
109 QT_SET_GET_REGISTER
, 0x40, value
, uart
,
114 #define set_mctrl(dev, set) update_mctrl((dev), (set), 0)
115 #define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear))
117 /* these do not deal with device that have more than 1 port */
118 static inline int update_mctrl(struct usb_device
*dev
, unsigned int set
,
124 if (((set
| clear
) & (TIOCM_DTR
| TIOCM_RTS
)) == 0) {
125 dev_dbg(&dev
->dev
, "%s - DTR|RTS not being set|cleared\n", __func__
);
126 return 0; /* no change */
129 clear
&= ~set
; /* 'set' takes precedence over 'clear' */
132 urb_value
|= UART_MCR_DTR
;
134 urb_value
|= UART_MCR_RTS
;
136 result
= ssu100_setregister(dev
, 0, UART_MCR
, urb_value
);
138 dev_dbg(&dev
->dev
, "%s Error from MODEM_CTRL urb\n", __func__
);
143 static int ssu100_initdevice(struct usb_device
*dev
)
148 data
= kzalloc(3, GFP_KERNEL
);
152 result
= ssu100_getdevice(dev
, data
);
154 dev_dbg(&dev
->dev
, "%s - get_device failed %i\n", __func__
, result
);
158 data
[1] &= ~FULLPWRBIT
;
160 result
= ssu100_setdevice(dev
, data
);
162 dev_dbg(&dev
->dev
, "%s - setdevice failed %i\n", __func__
, result
);
166 result
= ssu100_control_msg(dev
, QT_GET_SET_PREBUF_TRIG_LVL
, 128, 0);
168 dev_dbg(&dev
->dev
, "%s - set prebuffer level failed %i\n", __func__
, result
);
172 result
= ssu100_control_msg(dev
, QT_SET_ATF
, ATC_DISABLED
, 0);
174 dev_dbg(&dev
->dev
, "%s - set ATFprebuffer level failed %i\n", __func__
, result
);
178 result
= ssu100_getdevice(dev
, data
);
180 dev_dbg(&dev
->dev
, "%s - get_device failed %i\n", __func__
, result
);
184 data
[0] &= ~(RR_BITS
| DUPMODE_BITS
);
186 data
[1] &= ~(LOOPMODE_BITS
);
187 data
[1] |= RS232_MODE
;
189 result
= ssu100_setdevice(dev
, data
);
191 dev_dbg(&dev
->dev
, "%s - setdevice failed %i\n", __func__
, result
);
201 static void ssu100_set_termios(struct tty_struct
*tty
,
202 struct usb_serial_port
*port
,
203 struct ktermios
*old_termios
)
205 struct usb_device
*dev
= port
->serial
->dev
;
206 struct ktermios
*termios
= &tty
->termios
;
207 u16 baud
, divisor
, remainder
;
208 unsigned int cflag
= termios
->c_cflag
;
209 u16 urb_value
= 0; /* will hold the new flags */
212 if (cflag
& PARENB
) {
214 urb_value
|= UART_LCR_PARITY
;
216 urb_value
|= SERIAL_EVEN_PARITY
;
219 switch (cflag
& CSIZE
) {
221 urb_value
|= UART_LCR_WLEN5
;
224 urb_value
|= UART_LCR_WLEN6
;
227 urb_value
|= UART_LCR_WLEN7
;
231 urb_value
|= UART_LCR_WLEN8
;
235 baud
= tty_get_baud_rate(tty
);
239 dev_dbg(&port
->dev
, "%s - got baud = %d\n", __func__
, baud
);
242 divisor
= MAX_BAUD_RATE
/ baud
;
243 remainder
= MAX_BAUD_RATE
% baud
;
244 if (((remainder
* 2) >= baud
) && (baud
!= 110))
247 urb_value
= urb_value
<< 8;
249 result
= ssu100_control_msg(dev
, QT_GET_SET_UART
, divisor
, urb_value
);
251 dev_dbg(&port
->dev
, "%s - set uart failed\n", __func__
);
254 result
= ssu100_control_msg(dev
, QT_HW_FLOW_CONTROL_MASK
,
257 result
= ssu100_control_msg(dev
, QT_HW_FLOW_CONTROL_MASK
,
260 dev_dbg(&port
->dev
, "%s - set HW flow control failed\n", __func__
);
262 if (I_IXOFF(tty
) || I_IXON(tty
)) {
263 u16 x
= ((u16
)(START_CHAR(tty
) << 8) | (u16
)(STOP_CHAR(tty
)));
265 result
= ssu100_control_msg(dev
, QT_SW_FLOW_CONTROL_MASK
,
268 result
= ssu100_control_msg(dev
, QT_SW_FLOW_CONTROL_MASK
,
272 dev_dbg(&port
->dev
, "%s - set SW flow control failed\n", __func__
);
277 static int ssu100_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
279 struct usb_device
*dev
= port
->serial
->dev
;
280 struct ssu100_port_private
*priv
= usb_get_serial_port_data(port
);
285 data
= kzalloc(2, GFP_KERNEL
);
289 result
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
290 QT_OPEN_CLOSE_CHANNEL
,
291 QT_TRANSFER_IN
, 0x01,
294 dev_dbg(&port
->dev
, "%s - open failed %i\n", __func__
, result
);
299 spin_lock_irqsave(&priv
->status_lock
, flags
);
300 priv
->shadowLSR
= data
[0];
301 priv
->shadowMSR
= data
[1];
302 spin_unlock_irqrestore(&priv
->status_lock
, flags
);
307 result
= ssu100_control_msg(dev
, QT_GET_SET_UART
, 0x30, 0x0300);
309 dev_dbg(&port
->dev
, "%s - set uart failed\n", __func__
);
312 ssu100_set_termios(tty
, port
, &tty
->termios
);
314 return usb_serial_generic_open(tty
, port
);
317 static int get_serial_info(struct usb_serial_port
*port
,
318 struct serial_struct __user
*retinfo
)
320 struct serial_struct tmp
;
325 memset(&tmp
, 0, sizeof(tmp
));
326 tmp
.line
= port
->minor
;
329 tmp
.flags
= ASYNC_SKIP_TEST
| ASYNC_AUTO_IRQ
;
330 tmp
.xmit_fifo_size
= port
->bulk_out_size
;
331 tmp
.baud_base
= 9600;
332 tmp
.close_delay
= 5*HZ
;
333 tmp
.closing_wait
= 30*HZ
;
335 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
340 static int ssu100_ioctl(struct tty_struct
*tty
,
341 unsigned int cmd
, unsigned long arg
)
343 struct usb_serial_port
*port
= tty
->driver_data
;
345 dev_dbg(&port
->dev
, "%s cmd 0x%04x\n", __func__
, cmd
);
349 return get_serial_info(port
,
350 (struct serial_struct __user
*) arg
);
355 dev_dbg(&port
->dev
, "%s arg not supported\n", __func__
);
360 static int ssu100_attach(struct usb_serial
*serial
)
362 return ssu100_initdevice(serial
->dev
);
365 static int ssu100_port_probe(struct usb_serial_port
*port
)
367 struct ssu100_port_private
*priv
;
369 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
373 spin_lock_init(&priv
->status_lock
);
375 usb_set_serial_port_data(port
, priv
);
380 static int ssu100_port_remove(struct usb_serial_port
*port
)
382 struct ssu100_port_private
*priv
;
384 priv
= usb_get_serial_port_data(port
);
390 static int ssu100_tiocmget(struct tty_struct
*tty
)
392 struct usb_serial_port
*port
= tty
->driver_data
;
393 struct usb_device
*dev
= port
->serial
->dev
;
397 d
= kzalloc(2, GFP_KERNEL
);
401 r
= ssu100_getregister(dev
, 0, UART_MCR
, d
);
405 r
= ssu100_getregister(dev
, 0, UART_MSR
, d
+1);
409 r
= (d
[0] & UART_MCR_DTR
? TIOCM_DTR
: 0) |
410 (d
[0] & UART_MCR_RTS
? TIOCM_RTS
: 0) |
411 (d
[1] & UART_MSR_CTS
? TIOCM_CTS
: 0) |
412 (d
[1] & UART_MSR_DCD
? TIOCM_CAR
: 0) |
413 (d
[1] & UART_MSR_RI
? TIOCM_RI
: 0) |
414 (d
[1] & UART_MSR_DSR
? TIOCM_DSR
: 0);
421 static int ssu100_tiocmset(struct tty_struct
*tty
,
422 unsigned int set
, unsigned int clear
)
424 struct usb_serial_port
*port
= tty
->driver_data
;
425 struct usb_device
*dev
= port
->serial
->dev
;
427 return update_mctrl(dev
, set
, clear
);
430 static void ssu100_dtr_rts(struct usb_serial_port
*port
, int on
)
432 struct usb_device
*dev
= port
->serial
->dev
;
434 /* Disable flow control */
436 if (ssu100_setregister(dev
, 0, UART_MCR
, 0) < 0)
437 dev_err(&port
->dev
, "error from flowcontrol urb\n");
439 /* drop RTS and DTR */
441 set_mctrl(dev
, TIOCM_DTR
| TIOCM_RTS
);
443 clear_mctrl(dev
, TIOCM_DTR
| TIOCM_RTS
);
446 static void ssu100_update_msr(struct usb_serial_port
*port
, u8 msr
)
448 struct ssu100_port_private
*priv
= usb_get_serial_port_data(port
);
451 spin_lock_irqsave(&priv
->status_lock
, flags
);
452 priv
->shadowMSR
= msr
;
453 spin_unlock_irqrestore(&priv
->status_lock
, flags
);
455 if (msr
& UART_MSR_ANY_DELTA
) {
456 /* update input line counters */
457 if (msr
& UART_MSR_DCTS
)
459 if (msr
& UART_MSR_DDSR
)
461 if (msr
& UART_MSR_DDCD
)
463 if (msr
& UART_MSR_TERI
)
465 wake_up_interruptible(&port
->port
.delta_msr_wait
);
469 static void ssu100_update_lsr(struct usb_serial_port
*port
, u8 lsr
,
472 struct ssu100_port_private
*priv
= usb_get_serial_port_data(port
);
475 spin_lock_irqsave(&priv
->status_lock
, flags
);
476 priv
->shadowLSR
= lsr
;
477 spin_unlock_irqrestore(&priv
->status_lock
, flags
);
479 *tty_flag
= TTY_NORMAL
;
480 if (lsr
& UART_LSR_BRK_ERROR_BITS
) {
481 /* we always want to update icount, but we only want to
482 * update tty_flag for one case */
483 if (lsr
& UART_LSR_BI
) {
485 *tty_flag
= TTY_BREAK
;
486 usb_serial_handle_break(port
);
488 if (lsr
& UART_LSR_PE
) {
489 port
->icount
.parity
++;
490 if (*tty_flag
== TTY_NORMAL
)
491 *tty_flag
= TTY_PARITY
;
493 if (lsr
& UART_LSR_FE
) {
494 port
->icount
.frame
++;
495 if (*tty_flag
== TTY_NORMAL
)
496 *tty_flag
= TTY_FRAME
;
498 if (lsr
& UART_LSR_OE
){
499 port
->icount
.overrun
++;
500 if (*tty_flag
== TTY_NORMAL
)
501 *tty_flag
= TTY_OVERRUN
;
507 static void ssu100_process_read_urb(struct urb
*urb
)
509 struct usb_serial_port
*port
= urb
->context
;
510 char *packet
= (char *)urb
->transfer_buffer
;
511 char flag
= TTY_NORMAL
;
512 u32 len
= urb
->actual_length
;
517 (packet
[0] == 0x1b) && (packet
[1] == 0x1b) &&
518 ((packet
[2] == 0x00) || (packet
[2] == 0x01))) {
519 if (packet
[2] == 0x00) {
520 ssu100_update_lsr(port
, packet
[3], &flag
);
521 if (flag
== TTY_OVERRUN
)
522 tty_insert_flip_char(&port
->port
, 0,
525 if (packet
[2] == 0x01)
526 ssu100_update_msr(port
, packet
[3]);
534 return; /* status only */
536 if (port
->port
.console
&& port
->sysrq
) {
537 for (i
= 0; i
< len
; i
++, ch
++) {
538 if (!usb_serial_handle_sysrq_char(port
, *ch
))
539 tty_insert_flip_char(&port
->port
, *ch
, flag
);
542 tty_insert_flip_string_fixed_flag(&port
->port
, ch
, flag
, len
);
544 tty_flip_buffer_push(&port
->port
);
547 static struct usb_serial_driver ssu100_device
= {
549 .owner
= THIS_MODULE
,
552 .description
= DRIVER_DESC
,
553 .id_table
= id_table
,
556 .attach
= ssu100_attach
,
557 .port_probe
= ssu100_port_probe
,
558 .port_remove
= ssu100_port_remove
,
559 .dtr_rts
= ssu100_dtr_rts
,
560 .process_read_urb
= ssu100_process_read_urb
,
561 .tiocmget
= ssu100_tiocmget
,
562 .tiocmset
= ssu100_tiocmset
,
563 .tiocmiwait
= usb_serial_generic_tiocmiwait
,
564 .get_icount
= usb_serial_generic_get_icount
,
565 .ioctl
= ssu100_ioctl
,
566 .set_termios
= ssu100_set_termios
,
569 static struct usb_serial_driver
* const serial_drivers
[] = {
573 module_usb_serial_driver(serial_drivers
, id_table
);
575 MODULE_DESCRIPTION(DRIVER_DESC
);
576 MODULE_LICENSE("GPL");