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 /* Version Information */
50 #define DRIVER_VERSION "v0.1"
51 #define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver"
53 #define USB_VENDOR_ID_QUATECH 0x061d /* Quatech VID */
54 #define QUATECH_SSU100 0xC020 /* SSU100 */
56 static const struct usb_device_id id_table
[] = {
57 {USB_DEVICE(USB_VENDOR_ID_QUATECH
, QUATECH_SSU100
)},
58 {} /* Terminating entry */
60 MODULE_DEVICE_TABLE(usb
, id_table
);
62 struct ssu100_port_private
{
63 spinlock_t status_lock
;
66 wait_queue_head_t delta_msr_wait
; /* Used for TIOCMIWAIT */
67 struct async_icount icount
;
70 static inline int ssu100_control_msg(struct usb_device
*dev
,
71 u8 request
, u16 data
, u16 index
)
73 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
74 request
, 0x40, data
, index
,
78 static inline int ssu100_setdevice(struct usb_device
*dev
, u8
*data
)
80 u16 x
= ((u16
)(data
[1] << 8) | (u16
)(data
[0]));
82 return ssu100_control_msg(dev
, QT_SET_GET_DEVICE
, x
, 0);
86 static inline int ssu100_getdevice(struct usb_device
*dev
, u8
*data
)
88 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
89 QT_SET_GET_DEVICE
, 0xc0, 0, 0,
93 static inline int ssu100_getregister(struct usb_device
*dev
,
98 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
99 QT_SET_GET_REGISTER
, 0xc0, reg
,
100 uart
, data
, sizeof(*data
), 300);
105 static inline int ssu100_setregister(struct usb_device
*dev
,
110 u16 value
= (data
<< 8) | reg
;
112 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
113 QT_SET_GET_REGISTER
, 0x40, value
, uart
,
118 #define set_mctrl(dev, set) update_mctrl((dev), (set), 0)
119 #define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear))
121 /* these do not deal with device that have more than 1 port */
122 static inline int update_mctrl(struct usb_device
*dev
, unsigned int set
,
128 if (((set
| clear
) & (TIOCM_DTR
| TIOCM_RTS
)) == 0) {
129 dev_dbg(&dev
->dev
, "%s - DTR|RTS not being set|cleared\n", __func__
);
130 return 0; /* no change */
133 clear
&= ~set
; /* 'set' takes precedence over 'clear' */
136 urb_value
|= UART_MCR_DTR
;
138 urb_value
|= UART_MCR_RTS
;
140 result
= ssu100_setregister(dev
, 0, UART_MCR
, urb_value
);
142 dev_dbg(&dev
->dev
, "%s Error from MODEM_CTRL urb\n", __func__
);
147 static int ssu100_initdevice(struct usb_device
*dev
)
152 data
= kzalloc(3, GFP_KERNEL
);
156 result
= ssu100_getdevice(dev
, data
);
158 dev_dbg(&dev
->dev
, "%s - get_device failed %i\n", __func__
, result
);
162 data
[1] &= ~FULLPWRBIT
;
164 result
= ssu100_setdevice(dev
, data
);
166 dev_dbg(&dev
->dev
, "%s - setdevice failed %i\n", __func__
, result
);
170 result
= ssu100_control_msg(dev
, QT_GET_SET_PREBUF_TRIG_LVL
, 128, 0);
172 dev_dbg(&dev
->dev
, "%s - set prebuffer level failed %i\n", __func__
, result
);
176 result
= ssu100_control_msg(dev
, QT_SET_ATF
, ATC_DISABLED
, 0);
178 dev_dbg(&dev
->dev
, "%s - set ATFprebuffer level failed %i\n", __func__
, result
);
182 result
= ssu100_getdevice(dev
, data
);
184 dev_dbg(&dev
->dev
, "%s - get_device failed %i\n", __func__
, result
);
188 data
[0] &= ~(RR_BITS
| DUPMODE_BITS
);
190 data
[1] &= ~(LOOPMODE_BITS
);
191 data
[1] |= RS232_MODE
;
193 result
= ssu100_setdevice(dev
, data
);
195 dev_dbg(&dev
->dev
, "%s - setdevice failed %i\n", __func__
, result
);
205 static void ssu100_set_termios(struct tty_struct
*tty
,
206 struct usb_serial_port
*port
,
207 struct ktermios
*old_termios
)
209 struct usb_device
*dev
= port
->serial
->dev
;
210 struct ktermios
*termios
= &tty
->termios
;
211 u16 baud
, divisor
, remainder
;
212 unsigned int cflag
= termios
->c_cflag
;
213 u16 urb_value
= 0; /* will hold the new flags */
216 if (cflag
& PARENB
) {
218 urb_value
|= UART_LCR_PARITY
;
220 urb_value
|= SERIAL_EVEN_PARITY
;
223 switch (cflag
& CSIZE
) {
225 urb_value
|= UART_LCR_WLEN5
;
228 urb_value
|= UART_LCR_WLEN6
;
231 urb_value
|= UART_LCR_WLEN7
;
235 urb_value
|= UART_LCR_WLEN8
;
239 baud
= tty_get_baud_rate(tty
);
243 dev_dbg(&port
->dev
, "%s - got baud = %d\n", __func__
, baud
);
246 divisor
= MAX_BAUD_RATE
/ baud
;
247 remainder
= MAX_BAUD_RATE
% baud
;
248 if (((remainder
* 2) >= baud
) && (baud
!= 110))
251 urb_value
= urb_value
<< 8;
253 result
= ssu100_control_msg(dev
, QT_GET_SET_UART
, divisor
, urb_value
);
255 dev_dbg(&port
->dev
, "%s - set uart failed\n", __func__
);
258 result
= ssu100_control_msg(dev
, QT_HW_FLOW_CONTROL_MASK
,
261 result
= ssu100_control_msg(dev
, QT_HW_FLOW_CONTROL_MASK
,
264 dev_dbg(&port
->dev
, "%s - set HW flow control failed\n", __func__
);
266 if (I_IXOFF(tty
) || I_IXON(tty
)) {
267 u16 x
= ((u16
)(START_CHAR(tty
) << 8) | (u16
)(STOP_CHAR(tty
)));
269 result
= ssu100_control_msg(dev
, QT_SW_FLOW_CONTROL_MASK
,
272 result
= ssu100_control_msg(dev
, QT_SW_FLOW_CONTROL_MASK
,
276 dev_dbg(&port
->dev
, "%s - set SW flow control failed\n", __func__
);
281 static int ssu100_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
283 struct usb_device
*dev
= port
->serial
->dev
;
284 struct ssu100_port_private
*priv
= usb_get_serial_port_data(port
);
289 data
= kzalloc(2, GFP_KERNEL
);
293 result
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
294 QT_OPEN_CLOSE_CHANNEL
,
295 QT_TRANSFER_IN
, 0x01,
298 dev_dbg(&port
->dev
, "%s - open failed %i\n", __func__
, result
);
303 spin_lock_irqsave(&priv
->status_lock
, flags
);
304 priv
->shadowLSR
= data
[0];
305 priv
->shadowMSR
= data
[1];
306 spin_unlock_irqrestore(&priv
->status_lock
, flags
);
311 result
= ssu100_control_msg(dev
, QT_GET_SET_UART
, 0x30, 0x0300);
313 dev_dbg(&port
->dev
, "%s - set uart failed\n", __func__
);
316 ssu100_set_termios(tty
, port
, &tty
->termios
);
318 return usb_serial_generic_open(tty
, port
);
321 static void ssu100_close(struct usb_serial_port
*port
)
323 usb_serial_generic_close(port
);
326 static int get_serial_info(struct usb_serial_port
*port
,
327 struct serial_struct __user
*retinfo
)
329 struct serial_struct tmp
;
334 memset(&tmp
, 0, sizeof(tmp
));
335 tmp
.line
= port
->serial
->minor
;
338 tmp
.flags
= ASYNC_SKIP_TEST
| ASYNC_AUTO_IRQ
;
339 tmp
.xmit_fifo_size
= port
->bulk_out_size
;
340 tmp
.baud_base
= 9600;
341 tmp
.close_delay
= 5*HZ
;
342 tmp
.closing_wait
= 30*HZ
;
344 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
349 static int wait_modem_info(struct usb_serial_port
*port
, unsigned int arg
)
351 struct ssu100_port_private
*priv
= usb_get_serial_port_data(port
);
352 struct async_icount prev
, cur
;
355 spin_lock_irqsave(&priv
->status_lock
, flags
);
357 spin_unlock_irqrestore(&priv
->status_lock
, flags
);
360 wait_event_interruptible(priv
->delta_msr_wait
,
361 ((priv
->icount
.rng
!= prev
.rng
) ||
362 (priv
->icount
.dsr
!= prev
.dsr
) ||
363 (priv
->icount
.dcd
!= prev
.dcd
) ||
364 (priv
->icount
.cts
!= prev
.cts
)));
366 if (signal_pending(current
))
369 spin_lock_irqsave(&priv
->status_lock
, flags
);
371 spin_unlock_irqrestore(&priv
->status_lock
, flags
);
373 if ((prev
.rng
== cur
.rng
) &&
374 (prev
.dsr
== cur
.dsr
) &&
375 (prev
.dcd
== cur
.dcd
) &&
376 (prev
.cts
== cur
.cts
))
379 if ((arg
& TIOCM_RNG
&& (prev
.rng
!= cur
.rng
)) ||
380 (arg
& TIOCM_DSR
&& (prev
.dsr
!= cur
.dsr
)) ||
381 (arg
& TIOCM_CD
&& (prev
.dcd
!= cur
.dcd
)) ||
382 (arg
& TIOCM_CTS
&& (prev
.cts
!= cur
.cts
)))
388 static int ssu100_get_icount(struct tty_struct
*tty
,
389 struct serial_icounter_struct
*icount
)
391 struct usb_serial_port
*port
= tty
->driver_data
;
392 struct ssu100_port_private
*priv
= usb_get_serial_port_data(port
);
393 struct async_icount cnow
= priv
->icount
;
395 icount
->cts
= cnow
.cts
;
396 icount
->dsr
= cnow
.dsr
;
397 icount
->rng
= cnow
.rng
;
398 icount
->dcd
= cnow
.dcd
;
399 icount
->rx
= cnow
.rx
;
400 icount
->tx
= cnow
.tx
;
401 icount
->frame
= cnow
.frame
;
402 icount
->overrun
= cnow
.overrun
;
403 icount
->parity
= cnow
.parity
;
404 icount
->brk
= cnow
.brk
;
405 icount
->buf_overrun
= cnow
.buf_overrun
;
412 static int ssu100_ioctl(struct tty_struct
*tty
,
413 unsigned int cmd
, unsigned long arg
)
415 struct usb_serial_port
*port
= tty
->driver_data
;
417 dev_dbg(&port
->dev
, "%s cmd 0x%04x\n", __func__
, cmd
);
421 return get_serial_info(port
,
422 (struct serial_struct __user
*) arg
);
425 return wait_modem_info(port
, arg
);
431 dev_dbg(&port
->dev
, "%s arg not supported\n", __func__
);
436 static int ssu100_attach(struct usb_serial
*serial
)
438 return ssu100_initdevice(serial
->dev
);
441 static int ssu100_port_probe(struct usb_serial_port
*port
)
443 struct ssu100_port_private
*priv
;
445 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
449 spin_lock_init(&priv
->status_lock
);
450 init_waitqueue_head(&priv
->delta_msr_wait
);
452 usb_set_serial_port_data(port
, priv
);
457 static int ssu100_port_remove(struct usb_serial_port
*port
)
459 struct ssu100_port_private
*priv
;
461 priv
= usb_get_serial_port_data(port
);
467 static int ssu100_tiocmget(struct tty_struct
*tty
)
469 struct usb_serial_port
*port
= tty
->driver_data
;
470 struct usb_device
*dev
= port
->serial
->dev
;
474 d
= kzalloc(2, GFP_KERNEL
);
478 r
= ssu100_getregister(dev
, 0, UART_MCR
, d
);
482 r
= ssu100_getregister(dev
, 0, UART_MSR
, d
+1);
486 r
= (d
[0] & UART_MCR_DTR
? TIOCM_DTR
: 0) |
487 (d
[0] & UART_MCR_RTS
? TIOCM_RTS
: 0) |
488 (d
[1] & UART_MSR_CTS
? TIOCM_CTS
: 0) |
489 (d
[1] & UART_MSR_DCD
? TIOCM_CAR
: 0) |
490 (d
[1] & UART_MSR_RI
? TIOCM_RI
: 0) |
491 (d
[1] & UART_MSR_DSR
? TIOCM_DSR
: 0);
498 static int ssu100_tiocmset(struct tty_struct
*tty
,
499 unsigned int set
, unsigned int clear
)
501 struct usb_serial_port
*port
= tty
->driver_data
;
502 struct usb_device
*dev
= port
->serial
->dev
;
504 return update_mctrl(dev
, set
, clear
);
507 static void ssu100_dtr_rts(struct usb_serial_port
*port
, int on
)
509 struct usb_device
*dev
= port
->serial
->dev
;
511 mutex_lock(&port
->serial
->disc_mutex
);
512 if (!port
->serial
->disconnected
) {
513 /* Disable flow control */
515 ssu100_setregister(dev
, 0, UART_MCR
, 0) < 0)
516 dev_err(&port
->dev
, "error from flowcontrol urb\n");
517 /* drop RTS and DTR */
519 set_mctrl(dev
, TIOCM_DTR
| TIOCM_RTS
);
521 clear_mctrl(dev
, TIOCM_DTR
| TIOCM_RTS
);
523 mutex_unlock(&port
->serial
->disc_mutex
);
526 static void ssu100_update_msr(struct usb_serial_port
*port
, u8 msr
)
528 struct ssu100_port_private
*priv
= usb_get_serial_port_data(port
);
531 spin_lock_irqsave(&priv
->status_lock
, flags
);
532 priv
->shadowMSR
= msr
;
533 spin_unlock_irqrestore(&priv
->status_lock
, flags
);
535 if (msr
& UART_MSR_ANY_DELTA
) {
536 /* update input line counters */
537 if (msr
& UART_MSR_DCTS
)
539 if (msr
& UART_MSR_DDSR
)
541 if (msr
& UART_MSR_DDCD
)
543 if (msr
& UART_MSR_TERI
)
545 wake_up_interruptible(&priv
->delta_msr_wait
);
549 static void ssu100_update_lsr(struct usb_serial_port
*port
, u8 lsr
,
552 struct ssu100_port_private
*priv
= usb_get_serial_port_data(port
);
555 spin_lock_irqsave(&priv
->status_lock
, flags
);
556 priv
->shadowLSR
= lsr
;
557 spin_unlock_irqrestore(&priv
->status_lock
, flags
);
559 *tty_flag
= TTY_NORMAL
;
560 if (lsr
& UART_LSR_BRK_ERROR_BITS
) {
561 /* we always want to update icount, but we only want to
562 * update tty_flag for one case */
563 if (lsr
& UART_LSR_BI
) {
565 *tty_flag
= TTY_BREAK
;
566 usb_serial_handle_break(port
);
568 if (lsr
& UART_LSR_PE
) {
569 priv
->icount
.parity
++;
570 if (*tty_flag
== TTY_NORMAL
)
571 *tty_flag
= TTY_PARITY
;
573 if (lsr
& UART_LSR_FE
) {
574 priv
->icount
.frame
++;
575 if (*tty_flag
== TTY_NORMAL
)
576 *tty_flag
= TTY_FRAME
;
578 if (lsr
& UART_LSR_OE
){
579 priv
->icount
.overrun
++;
580 if (*tty_flag
== TTY_NORMAL
)
581 *tty_flag
= TTY_OVERRUN
;
587 static int ssu100_process_packet(struct urb
*urb
,
588 struct tty_struct
*tty
)
590 struct usb_serial_port
*port
= urb
->context
;
591 char *packet
= (char *)urb
->transfer_buffer
;
592 char flag
= TTY_NORMAL
;
593 u32 len
= urb
->actual_length
;
598 (packet
[0] == 0x1b) && (packet
[1] == 0x1b) &&
599 ((packet
[2] == 0x00) || (packet
[2] == 0x01))) {
600 if (packet
[2] == 0x00) {
601 ssu100_update_lsr(port
, packet
[3], &flag
);
602 if (flag
== TTY_OVERRUN
)
603 tty_insert_flip_char(tty
, 0, TTY_OVERRUN
);
605 if (packet
[2] == 0x01)
606 ssu100_update_msr(port
, packet
[3]);
614 return 0; /* status only */
616 if (port
->port
.console
&& port
->sysrq
) {
617 for (i
= 0; i
< len
; i
++, ch
++) {
618 if (!usb_serial_handle_sysrq_char(port
, *ch
))
619 tty_insert_flip_char(tty
, *ch
, flag
);
622 tty_insert_flip_string_fixed_flag(tty
, ch
, flag
, len
);
627 static void ssu100_process_read_urb(struct urb
*urb
)
629 struct usb_serial_port
*port
= urb
->context
;
630 struct tty_struct
*tty
;
633 tty
= tty_port_tty_get(&port
->port
);
637 count
= ssu100_process_packet(urb
, tty
);
640 tty_flip_buffer_push(tty
);
644 static struct usb_serial_driver ssu100_device
= {
646 .owner
= THIS_MODULE
,
649 .description
= DRIVER_DESC
,
650 .id_table
= id_table
,
653 .close
= ssu100_close
,
654 .attach
= ssu100_attach
,
655 .port_probe
= ssu100_port_probe
,
656 .port_remove
= ssu100_port_remove
,
657 .dtr_rts
= ssu100_dtr_rts
,
658 .process_read_urb
= ssu100_process_read_urb
,
659 .tiocmget
= ssu100_tiocmget
,
660 .tiocmset
= ssu100_tiocmset
,
661 .get_icount
= ssu100_get_icount
,
662 .ioctl
= ssu100_ioctl
,
663 .set_termios
= ssu100_set_termios
,
664 .disconnect
= usb_serial_generic_disconnect
,
667 static struct usb_serial_driver
* const serial_drivers
[] = {
671 module_usb_serial_driver(serial_drivers
, id_table
);
673 MODULE_DESCRIPTION(DRIVER_DESC
);
674 MODULE_LICENSE("GPL");