1 // SPDX-License-Identifier: GPL-2.0
3 * usb-serial driver for Quatech SSU-100
5 * based on ftdi_sio.c and the original serqt_usb.c from Quatech
9 #include <linux/errno.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
)
86 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
87 QT_SET_GET_DEVICE
, 0xc0, 0, 0,
97 static inline int ssu100_getregister(struct usb_device
*dev
,
104 ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
105 QT_SET_GET_REGISTER
, 0xc0, reg
,
106 uart
, data
, sizeof(*data
), 300);
107 if (ret
< sizeof(*data
)) {
116 static inline int ssu100_setregister(struct usb_device
*dev
,
121 u16 value
= (data
<< 8) | reg
;
123 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
124 QT_SET_GET_REGISTER
, 0x40, value
, uart
,
129 #define set_mctrl(dev, set) update_mctrl((dev), (set), 0)
130 #define clear_mctrl(dev, clear) update_mctrl((dev), 0, (clear))
132 /* these do not deal with device that have more than 1 port */
133 static inline int update_mctrl(struct usb_device
*dev
, unsigned int set
,
139 if (((set
| clear
) & (TIOCM_DTR
| TIOCM_RTS
)) == 0) {
140 dev_dbg(&dev
->dev
, "%s - DTR|RTS not being set|cleared\n", __func__
);
141 return 0; /* no change */
144 clear
&= ~set
; /* 'set' takes precedence over 'clear' */
147 urb_value
|= UART_MCR_DTR
;
149 urb_value
|= UART_MCR_RTS
;
151 result
= ssu100_setregister(dev
, 0, UART_MCR
, urb_value
);
153 dev_dbg(&dev
->dev
, "%s Error from MODEM_CTRL urb\n", __func__
);
158 static int ssu100_initdevice(struct usb_device
*dev
)
163 data
= kzalloc(3, GFP_KERNEL
);
167 result
= ssu100_getdevice(dev
, data
);
169 dev_dbg(&dev
->dev
, "%s - get_device failed %i\n", __func__
, result
);
173 data
[1] &= ~FULLPWRBIT
;
175 result
= ssu100_setdevice(dev
, data
);
177 dev_dbg(&dev
->dev
, "%s - setdevice failed %i\n", __func__
, result
);
181 result
= ssu100_control_msg(dev
, QT_GET_SET_PREBUF_TRIG_LVL
, 128, 0);
183 dev_dbg(&dev
->dev
, "%s - set prebuffer level failed %i\n", __func__
, result
);
187 result
= ssu100_control_msg(dev
, QT_SET_ATF
, ATC_DISABLED
, 0);
189 dev_dbg(&dev
->dev
, "%s - set ATFprebuffer level failed %i\n", __func__
, result
);
193 result
= ssu100_getdevice(dev
, data
);
195 dev_dbg(&dev
->dev
, "%s - get_device failed %i\n", __func__
, result
);
199 data
[0] &= ~(RR_BITS
| DUPMODE_BITS
);
201 data
[1] &= ~(LOOPMODE_BITS
);
202 data
[1] |= RS232_MODE
;
204 result
= ssu100_setdevice(dev
, data
);
206 dev_dbg(&dev
->dev
, "%s - setdevice failed %i\n", __func__
, result
);
216 static void ssu100_set_termios(struct tty_struct
*tty
,
217 struct usb_serial_port
*port
,
218 struct ktermios
*old_termios
)
220 struct usb_device
*dev
= port
->serial
->dev
;
221 struct ktermios
*termios
= &tty
->termios
;
222 u16 baud
, divisor
, remainder
;
223 unsigned int cflag
= termios
->c_cflag
;
224 u16 urb_value
= 0; /* will hold the new flags */
227 if (cflag
& PARENB
) {
229 urb_value
|= UART_LCR_PARITY
;
231 urb_value
|= SERIAL_EVEN_PARITY
;
234 switch (cflag
& CSIZE
) {
236 urb_value
|= UART_LCR_WLEN5
;
239 urb_value
|= UART_LCR_WLEN6
;
242 urb_value
|= UART_LCR_WLEN7
;
246 urb_value
|= UART_LCR_WLEN8
;
250 baud
= tty_get_baud_rate(tty
);
254 dev_dbg(&port
->dev
, "%s - got baud = %d\n", __func__
, baud
);
257 divisor
= MAX_BAUD_RATE
/ baud
;
258 remainder
= MAX_BAUD_RATE
% baud
;
259 if (((remainder
* 2) >= baud
) && (baud
!= 110))
262 urb_value
= urb_value
<< 8;
264 result
= ssu100_control_msg(dev
, QT_GET_SET_UART
, divisor
, urb_value
);
266 dev_dbg(&port
->dev
, "%s - set uart failed\n", __func__
);
269 result
= ssu100_control_msg(dev
, QT_HW_FLOW_CONTROL_MASK
,
272 result
= ssu100_control_msg(dev
, QT_HW_FLOW_CONTROL_MASK
,
275 dev_dbg(&port
->dev
, "%s - set HW flow control failed\n", __func__
);
277 if (I_IXOFF(tty
) || I_IXON(tty
)) {
278 u16 x
= ((u16
)(START_CHAR(tty
) << 8) | (u16
)(STOP_CHAR(tty
)));
280 result
= ssu100_control_msg(dev
, QT_SW_FLOW_CONTROL_MASK
,
283 result
= ssu100_control_msg(dev
, QT_SW_FLOW_CONTROL_MASK
,
287 dev_dbg(&port
->dev
, "%s - set SW flow control failed\n", __func__
);
292 static int ssu100_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
294 struct usb_device
*dev
= port
->serial
->dev
;
295 struct ssu100_port_private
*priv
= usb_get_serial_port_data(port
);
300 data
= kzalloc(2, GFP_KERNEL
);
304 result
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
305 QT_OPEN_CLOSE_CHANNEL
,
306 QT_TRANSFER_IN
, 0x01,
309 dev_dbg(&port
->dev
, "%s - open failed %i\n", __func__
, result
);
316 spin_lock_irqsave(&priv
->status_lock
, flags
);
317 priv
->shadowLSR
= data
[0];
318 priv
->shadowMSR
= data
[1];
319 spin_unlock_irqrestore(&priv
->status_lock
, flags
);
324 result
= ssu100_control_msg(dev
, QT_GET_SET_UART
, 0x30, 0x0300);
326 dev_dbg(&port
->dev
, "%s - set uart failed\n", __func__
);
329 ssu100_set_termios(tty
, port
, &tty
->termios
);
331 return usb_serial_generic_open(tty
, port
);
334 static int get_serial_info(struct usb_serial_port
*port
,
335 struct serial_struct __user
*retinfo
)
337 struct serial_struct tmp
;
339 memset(&tmp
, 0, sizeof(tmp
));
340 tmp
.line
= port
->minor
;
343 tmp
.xmit_fifo_size
= port
->bulk_out_size
;
344 tmp
.baud_base
= 9600;
345 tmp
.close_delay
= 5*HZ
;
346 tmp
.closing_wait
= 30*HZ
;
348 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
353 static int ssu100_ioctl(struct tty_struct
*tty
,
354 unsigned int cmd
, unsigned long arg
)
356 struct usb_serial_port
*port
= tty
->driver_data
;
360 return get_serial_info(port
,
361 (struct serial_struct __user
*) arg
);
369 static int ssu100_attach(struct usb_serial
*serial
)
371 return ssu100_initdevice(serial
->dev
);
374 static int ssu100_port_probe(struct usb_serial_port
*port
)
376 struct ssu100_port_private
*priv
;
378 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
382 spin_lock_init(&priv
->status_lock
);
384 usb_set_serial_port_data(port
, priv
);
389 static int ssu100_port_remove(struct usb_serial_port
*port
)
391 struct ssu100_port_private
*priv
;
393 priv
= usb_get_serial_port_data(port
);
399 static int ssu100_tiocmget(struct tty_struct
*tty
)
401 struct usb_serial_port
*port
= tty
->driver_data
;
402 struct usb_device
*dev
= port
->serial
->dev
;
406 d
= kzalloc(2, GFP_KERNEL
);
410 r
= ssu100_getregister(dev
, 0, UART_MCR
, d
);
414 r
= ssu100_getregister(dev
, 0, UART_MSR
, d
+1);
418 r
= (d
[0] & UART_MCR_DTR
? TIOCM_DTR
: 0) |
419 (d
[0] & UART_MCR_RTS
? TIOCM_RTS
: 0) |
420 (d
[1] & UART_MSR_CTS
? TIOCM_CTS
: 0) |
421 (d
[1] & UART_MSR_DCD
? TIOCM_CAR
: 0) |
422 (d
[1] & UART_MSR_RI
? TIOCM_RI
: 0) |
423 (d
[1] & UART_MSR_DSR
? TIOCM_DSR
: 0);
430 static int ssu100_tiocmset(struct tty_struct
*tty
,
431 unsigned int set
, unsigned int clear
)
433 struct usb_serial_port
*port
= tty
->driver_data
;
434 struct usb_device
*dev
= port
->serial
->dev
;
436 return update_mctrl(dev
, set
, clear
);
439 static void ssu100_dtr_rts(struct usb_serial_port
*port
, int on
)
441 struct usb_device
*dev
= port
->serial
->dev
;
443 /* Disable flow control */
445 if (ssu100_setregister(dev
, 0, UART_MCR
, 0) < 0)
446 dev_err(&port
->dev
, "error from flowcontrol urb\n");
448 /* drop RTS and DTR */
450 set_mctrl(dev
, TIOCM_DTR
| TIOCM_RTS
);
452 clear_mctrl(dev
, TIOCM_DTR
| TIOCM_RTS
);
455 static void ssu100_update_msr(struct usb_serial_port
*port
, u8 msr
)
457 struct ssu100_port_private
*priv
= usb_get_serial_port_data(port
);
460 spin_lock_irqsave(&priv
->status_lock
, flags
);
461 priv
->shadowMSR
= msr
;
462 spin_unlock_irqrestore(&priv
->status_lock
, flags
);
464 if (msr
& UART_MSR_ANY_DELTA
) {
465 /* update input line counters */
466 if (msr
& UART_MSR_DCTS
)
468 if (msr
& UART_MSR_DDSR
)
470 if (msr
& UART_MSR_DDCD
)
472 if (msr
& UART_MSR_TERI
)
474 wake_up_interruptible(&port
->port
.delta_msr_wait
);
478 static void ssu100_update_lsr(struct usb_serial_port
*port
, u8 lsr
,
481 struct ssu100_port_private
*priv
= usb_get_serial_port_data(port
);
484 spin_lock_irqsave(&priv
->status_lock
, flags
);
485 priv
->shadowLSR
= lsr
;
486 spin_unlock_irqrestore(&priv
->status_lock
, flags
);
488 *tty_flag
= TTY_NORMAL
;
489 if (lsr
& UART_LSR_BRK_ERROR_BITS
) {
490 /* we always want to update icount, but we only want to
491 * update tty_flag for one case */
492 if (lsr
& UART_LSR_BI
) {
494 *tty_flag
= TTY_BREAK
;
495 usb_serial_handle_break(port
);
497 if (lsr
& UART_LSR_PE
) {
498 port
->icount
.parity
++;
499 if (*tty_flag
== TTY_NORMAL
)
500 *tty_flag
= TTY_PARITY
;
502 if (lsr
& UART_LSR_FE
) {
503 port
->icount
.frame
++;
504 if (*tty_flag
== TTY_NORMAL
)
505 *tty_flag
= TTY_FRAME
;
507 if (lsr
& UART_LSR_OE
) {
508 port
->icount
.overrun
++;
509 tty_insert_flip_char(&port
->port
, 0, TTY_OVERRUN
);
515 static void ssu100_process_read_urb(struct urb
*urb
)
517 struct usb_serial_port
*port
= urb
->context
;
518 char *packet
= (char *)urb
->transfer_buffer
;
519 char flag
= TTY_NORMAL
;
520 u32 len
= urb
->actual_length
;
525 (packet
[0] == 0x1b) && (packet
[1] == 0x1b) &&
526 ((packet
[2] == 0x00) || (packet
[2] == 0x01))) {
527 if (packet
[2] == 0x00)
528 ssu100_update_lsr(port
, packet
[3], &flag
);
529 if (packet
[2] == 0x01)
530 ssu100_update_msr(port
, packet
[3]);
538 return; /* status only */
540 if (port
->port
.console
&& port
->sysrq
) {
541 for (i
= 0; i
< len
; i
++, ch
++) {
542 if (!usb_serial_handle_sysrq_char(port
, *ch
))
543 tty_insert_flip_char(&port
->port
, *ch
, flag
);
546 tty_insert_flip_string_fixed_flag(&port
->port
, ch
, flag
, len
);
548 tty_flip_buffer_push(&port
->port
);
551 static struct usb_serial_driver ssu100_device
= {
553 .owner
= THIS_MODULE
,
556 .description
= DRIVER_DESC
,
557 .id_table
= id_table
,
560 .attach
= ssu100_attach
,
561 .port_probe
= ssu100_port_probe
,
562 .port_remove
= ssu100_port_remove
,
563 .dtr_rts
= ssu100_dtr_rts
,
564 .process_read_urb
= ssu100_process_read_urb
,
565 .tiocmget
= ssu100_tiocmget
,
566 .tiocmset
= ssu100_tiocmset
,
567 .tiocmiwait
= usb_serial_generic_tiocmiwait
,
568 .get_icount
= usb_serial_generic_get_icount
,
569 .ioctl
= ssu100_ioctl
,
570 .set_termios
= ssu100_set_termios
,
573 static struct usb_serial_driver
* const serial_drivers
[] = {
577 module_usb_serial_driver(serial_drivers
, id_table
);
579 MODULE_DESCRIPTION(DRIVER_DESC
);
580 MODULE_LICENSE("GPL v2");