7 #include <linux/errno.h>
9 #include <linux/slab.h>
10 #include <linux/tty.h>
13 #include <linux/module.h>
14 #include <linux/serial.h>
20 #define DRIVER_VERSION "v2.14"
21 #define DRIVER_AUTHOR "Tim Gobeli, Quatech, Inc"
22 #define DRIVER_DESC "Quatech USB to Serial Driver"
24 #define USB_VENDOR_ID_QUATECH 0x061d
25 #define QUATECH_SSU200 0xC030
26 #define QUATECH_DSU100 0xC040
27 #define QUATECH_DSU200 0xC050
28 #define QUATECH_QSU100 0xC060
29 #define QUATECH_QSU200 0xC070
30 #define QUATECH_ESU100A 0xC080
31 #define QUATECH_ESU100B 0xC081
32 #define QUATECH_ESU200A 0xC0A0
33 #define QUATECH_ESU200B 0xC0A1
34 #define QUATECH_HSU100A 0xC090
35 #define QUATECH_HSU100B 0xC091
36 #define QUATECH_HSU100C 0xC092
37 #define QUATECH_HSU100D 0xC093
38 #define QUATECH_HSU200A 0xC0B0
39 #define QUATECH_HSU200B 0xC0B1
40 #define QUATECH_HSU200C 0xC0B2
41 #define QUATECH_HSU200D 0xC0B3
43 #define QT_SET_GET_DEVICE 0xc2
44 #define QT_OPEN_CLOSE_CHANNEL 0xca
45 #define QT_GET_SET_PREBUF_TRIG_LVL 0xcc
46 #define QT_SET_ATF 0xcd
47 #define QT_GET_SET_REGISTER 0xc0
48 #define QT_GET_SET_UART 0xc1
49 #define QT_HW_FLOW_CONTROL_MASK 0xc5
50 #define QT_SW_FLOW_CONTROL_MASK 0xc6
51 #define QT_SW_FLOW_CONTROL_DISABLE 0xc7
52 #define QT_BREAK_CONTROL 0xc8
54 #define USBD_TRANSFER_DIRECTION_IN 0xc0
55 #define USBD_TRANSFER_DIRECTION_OUT 0x40
57 #define MAX_BAUD_RATE 460800
58 #define MAX_BAUD_REMAINDER 4608
60 #define DIV_LATCH_LS 0x00
61 #define XMT_HOLD_REGISTER 0x00
62 #define XVR_BUFFER_REGISTER 0x00
63 #define DIV_LATCH_MS 0x01
64 #define FIFO_CONTROL_REGISTER 0x02
65 #define LINE_CONTROL_REGISTER 0x03
66 #define MODEM_CONTROL_REGISTER 0x04
67 #define LINE_STATUS_REGISTER 0x05
68 #define MODEM_STATUS_REGISTER 0x06
70 #define SERIAL_MCR_DTR 0x01
71 #define SERIAL_MCR_RTS 0x02
72 #define SERIAL_MCR_LOOP 0x10
74 #define SERIAL_MSR_CTS 0x10
75 #define SERIAL_MSR_CD 0x80
76 #define SERIAL_MSR_RI 0x40
77 #define SERIAL_MSR_DSR 0x20
78 #define SERIAL_MSR_MASK 0xf0
80 #define SERIAL_8_DATA 0x03
81 #define SERIAL_7_DATA 0x02
82 #define SERIAL_6_DATA 0x01
83 #define SERIAL_5_DATA 0x00
85 #define SERIAL_ODD_PARITY 0X08
86 #define SERIAL_EVEN_PARITY 0X18
87 #define SERIAL_TWO_STOPB 0x04
88 #define SERIAL_ONE_STOPB 0x00
90 #define DEFAULT_DIVISOR 0x30
91 #define DEFAULT_LCR SERIAL_8_DATA
93 #define FULLPWRBIT 0x00000080
94 #define NEXT_BOARD_POWER_BIT 0x00000004
96 #define SERIAL_LSR_OE 0x02
97 #define SERIAL_LSR_PE 0x04
98 #define SERIAL_LSR_FE 0x08
99 #define SERIAL_LSR_BI 0x10
101 #define SERIAL_MSR_CTS 0x10
102 #define SERIAL_MSR_CD 0x80
103 #define SERIAL_MSR_RI 0x40
104 #define SERIAL_MSR_DSR 0x20
105 #define SERIAL_MSR_MASK 0xf0
107 #define PREFUFF_LEVEL_CONSERVATIVE 128
108 #define ATC_DISABLED 0x0
111 #define DUPMODE_BITS 0xc0
114 #define LOOPMODE_BITS 0x41
115 #define ALL_LOOPBACK 0x01
116 #define MODEM_CTRL 0x40
117 #define RS232_MODE 0x00
182 const char *
function)
185 pr_debug(
"%s - port == NULL",
function);
189 pr_debug(
"%s - port->serial == NULL\n",
function);
197 const char *
function)
200 pr_debug(
"%s - serial == NULL\n",
function);
205 pr_debug(
"%s - serial->type == NULL!",
function);
215 return (
struct quatech_port *)usb_get_serial_port_data(port);
221 usb_set_serial_port_data(port, (
void *)data);
225 const char *
function)
229 port_paranoia_check(port,
function) ||
230 serial_paranoia_check(port->
serial,
function)) {
241 static void ProcessLineStatus(
struct quatech_port *qt_port,
242 unsigned char line_status)
250 static void ProcessModemStatus(
struct quatech_port *qt_port,
251 unsigned char modem_status)
262 if (urb->actual_length)
266 static void qt_write_bulk_callback(
struct urb *urb)
272 status = urb->status;
275 dev_dbg(&urb->dev->dev,
"nonzero write bulk status received:%d\n", status);
279 quatech_port = urb->context;
288 static void qt_interrupt_callback(
struct urb *urb)
293 static void qt_read_bulk_callback(
struct urb *urb)
297 struct usb_serial *serial = get_usb_serial(port, __func__);
298 struct quatech_port *qt_port = qt_get_port_private(port);
302 unsigned int RxCount;
308 dev_dbg(&urb->dev->dev,
"%s - nonzero write bulk status received: %d\n",
309 __func__, urb->status);
317 data = urb->transfer_buffer;
319 RxCount = urb->actual_length;
326 if (port_paranoia_check(port, __func__) != 0) {
336 dev_dbg(&port->
dev,
"%s - (qt_port->closepending == 1\n", __func__);
354 dev_dbg(&port->
dev,
"%s - nonzero read bulk status received: %d\n",
355 __func__, urb->status);
361 for (i = 0; i < RxCount; ++
i) {
363 if ((i <= (RxCount - 3)) && (data[i] == 0x1b)
364 && (data[i + 1] == 0x1b)) {
366 switch (data[i + 2]) {
369 if (i > (RxCount - 4)) {
370 dev_dbg(&port->
dev,
"Illegal escape seuences in received data\n");
373 ProcessLineStatus(qt_port, data[i + 3]);
380 dev_dbg(&port->
dev,
"Modem status status.\n");
381 if (i > (RxCount - 4)) {
382 dev_dbg(&port->
dev,
"Illegal escape sequences in received data\n");
385 ProcessModemStatus(qt_port,
394 ProcessRxChar(tty, port, data[i]);
395 ProcessRxChar(tty, port, data[i + 1]);
404 if (tty && urb->actual_length)
405 tty_insert_flip_char(tty, data[i],
TTY_NORMAL);
413 usb_rcvbulkpipe(serial->
dev,
416 port->
read_urb->transfer_buffer_length,
417 qt_read_bulk_callback, port);
420 dev_dbg(&port->
dev,
"%s - failed resubmitting read urb, error %d",
441 static int qt_get_device(
struct usb_serial *serial,
449 if (!transfer_buffer)
457 memcpy(device_data, transfer_buffer,
459 kfree(transfer_buffer);
468 static int BoxSetPrebufferLevel(
struct usb_serial *serial)
476 buffer_length, 0,
NULL, 0, 300);
504 static int qt_set_device(
struct usb_serial *serial,
511 PortSettings = ((
__u16) (device_data->
portb));
512 PortSettings = (PortSettings << 8);
513 PortSettings += ((
__u16) (device_data->
porta));
523 static int qt_open_channel(
struct usb_serial *serial,
__u16 Uart_Number,
538 static int qt_close_channel(
struct usb_serial *serial,
__u16 Uart_Number)
556 static int BoxGetRegister(
struct usb_serial *serial,
unsigned short Uart_Number,
557 unsigned short Register_Num,
__u8 *pValue)
560 __u16 current_length;
567 Uart_Number, (
void *)pValue,
sizeof(*pValue), 300);
577 static int BoxSetRegister(
struct usb_serial *serial,
unsigned short Uart_Number,
578 unsigned short Register_Num,
unsigned short Value)
581 unsigned short RegAndByte;
584 RegAndByte = RegAndByte << 8;
585 RegAndByte = RegAndByte + Register_Num;
606 static int qt_setuart(
struct usb_serial *serial,
unsigned short Uart_Number,
607 unsigned short default_divisor,
unsigned char default_LCR)
610 unsigned short UartNumandLCR;
612 UartNumandLCR = (default_LCR << 8) + Uart_Number;
617 UartNumandLCR,
NULL, 0, 300);
622 static int BoxSetHW_FlowCtrl(
struct usb_serial *serial,
unsigned int index,
626 __u8 msr = 0, MOUT_Value = 0;
638 MOUT_Value = mcr << 8;
653 index,
NULL, 0, 300);
659 unsigned char stop_char,
unsigned char start_char)
664 nSWflowout = start_char << 8;
665 nSWflowout = (
unsigned short)stop_char;
670 index,
NULL, 0, 300);
675 static int BoxDisable_SW_FlowCtrl(
struct usb_serial *serial,
__u16 index)
687 static int qt_startup(
struct usb_serial *serial)
691 struct quatech_port *qt_port;
697 for (i = 0; i < serial->
num_ports; i++) {
698 port = serial->
port[
i];
699 qt_port = kzalloc(
sizeof(*qt_port),
GFP_KERNEL);
701 for (--i; i >= 0; i--) {
702 port = serial->
port[
i];
703 kfree(usb_get_serial_port_data(port));
704 usb_set_serial_port_data(port,
NULL);
710 usb_set_serial_port_data(port, qt_port);
714 status = qt_get_device(serial, &DeviceData);
718 dev_dbg(dev,
"DeviceData.portb = 0x%x\n", DeviceData.portb);
721 dev_dbg(dev,
"Changing DeviceData.portb to 0x%x\n", DeviceData.portb);
723 status = qt_set_device(serial, &DeviceData);
725 dev_dbg(dev,
"qt_set_device failed\n");
729 status = qt_get_device(serial, &DeviceData);
731 dev_dbg(dev,
"qt_get_device failed\n");
735 switch (serial->
dev->descriptor.idProduct) {
773 status = BoxSetPrebufferLevel(serial);
775 dev_dbg(dev,
"BoxSetPrebufferLevel failed\n");
781 dev_dbg(dev,
"BoxSetATC failed\n");
785 dev_dbg(dev,
"DeviceData.portb = 0x%x\n", DeviceData.portb);
788 dev_dbg(dev,
"Changing DeviceData.portb to 0x%x\n", DeviceData.portb);
790 status = qt_set_device(serial, &DeviceData);
792 dev_dbg(dev,
"qt_set_device failed\n");
799 for (i = 0; i < serial->
num_ports; i++) {
800 port = serial->
port[
i];
801 qt_port = qt_get_port_private(port);
803 usb_set_serial_port_data(port,
NULL);
809 static void qt_release(
struct usb_serial *serial)
812 struct quatech_port *qt_port;
815 for (i = 0; i < serial->
num_ports; i++) {
816 port = serial->
port[
i];
820 qt_port = usb_get_serial_port_data(port);
822 usb_set_serial_port_data(port,
NULL);
831 struct quatech_port *quatech_port;
832 struct quatech_port *port0;
837 if (port_paranoia_check(port, __func__))
842 if (serial_paranoia_check(serial, __func__))
845 quatech_port = qt_get_port_private(port);
846 port0 = qt_get_port_private(serial->
port[0]);
848 if (quatech_port ==
NULL || port0 ==
NULL)
855 result = qt_get_device(serial, &port0->
DeviceData);
858 result = qt_open_channel(serial, port->
number, &ChannelData);
860 dev_dbg(&port->
dev,
"qt_open_channel failed\n");
863 dev_dbg(&port->
dev,
"qt_open_channel completed.\n");
866 quatech_port->
shadowLSR = ChannelData.line_status &
869 quatech_port->
shadowMSR = ChannelData.modem_status &
878 dev_dbg(&port->
dev,
"qt_setuart completed.\n");
887 if (serial->
port[0]->interrupt_in_buffer ==
NULL) {
889 usb_fill_int_urb(serial->
port[0]->interrupt_in_urb,
891 usb_rcvintpipe(serial->
dev,
892 serial->
port[0]->interrupt_in_endpointAddress),
893 serial->
port[0]->interrupt_in_buffer,
895 interrupt_in_urb->transfer_buffer_length,
896 qt_interrupt_callback, serial,
898 interrupt_in_urb->interval);
905 "%s - Error %d submitting "
906 "interrupt urb\n", __func__, result);
923 usb_fill_bulk_urb(quatech_port->
read_urb,
925 usb_rcvbulkpipe(serial->
dev,
928 quatech_port->
read_urb->transfer_buffer_length,
929 qt_read_bulk_callback, quatech_port);
936 "%s - Error %d submitting control urb\n",
952 static int qt_chars_in_buffer(
struct tty_struct *tty)
958 serial = get_usb_serial(port, __func__);
962 chars = port->
write_urb->transfer_buffer_length;
968 static void qt_block_until_empty(
struct tty_struct *tty,
969 struct quatech_port *qt_port)
977 count = qt_chars_in_buffer(tty);
986 dev_dbg(&qt_port->
port->dev,
"%s - TIMEOUT", __func__);
997 struct quatech_port *qt_port;
998 struct quatech_port *port0;
1007 qt_port = qt_get_port_private(port);
1008 port0 = qt_get_port_private(serial->
port[0]);
1018 qt_block_until_empty(tty, qt_port);
1022 status = qt_close_channel(serial, index);
1024 dev_dbg(&port->
dev,
"%s - port %d qt_close_channel failed.\n", __func__, port->
number);
1031 if (serial->
port[0]->interrupt_in_urb) {
1032 dev_dbg(&port->
dev,
"%s",
"Shutdown interrupt_in_urb\n");
1047 const unsigned char *
buf,
int count)
1050 struct usb_serial *serial = get_usb_serial(port, __func__);
1056 dev_dbg(&port->
dev,
"%s - write request of 0 bytes\n", __func__);
1063 dev_dbg(&port->
dev,
"%s - already writing\n", __func__);
1074 usb_sndbulkpipe(serial->
dev,
1076 bulk_out_endpointAddress),
1077 port->
write_urb->transfer_buffer, count,
1078 qt_write_bulk_callback, port);
1083 dev_dbg(&port->
dev,
"%s - failed submitting write urb, error %d\n",
1095 static int qt_write_room(
struct tty_struct *tty)
1099 struct quatech_port *qt_port;
1103 if (port_paranoia_check(port, __func__))
1106 serial = get_usb_serial(port, __func__);
1111 qt_port = qt_get_port_private(port);
1126 unsigned int cmd,
unsigned long arg)
1129 struct quatech_port *qt_port = qt_get_port_private(port);
1130 struct usb_serial *serial = get_usb_serial(port, __func__);
1133 dev_dbg(&port->
dev,
"%s cmd 0x%04x\n", __func__, cmd);
1138 while (qt_port !=
NULL) {
1166 dev_dbg(&port->
dev,
"%s -No ioctl for that one. port = %d\n", __func__, port->
number);
1170 static void qt_set_termios(
struct tty_struct *tty,
1175 unsigned char new_LCR = 0;
1183 switch (cflag &
CSIZE) {
1221 dev_dbg(&port->
dev,
"%s - got baud = %d\n", __func__, baud);
1226 if (((remainder * 2) >= baud) && (baud != 110))
1233 qt_setuart(port->
serial, index, (
unsigned short)divisor, new_LCR);
1241 dev_dbg(&port->
dev,
"%s - Enabling HW flow control port %d\n", __func__, port->
number);
1244 status = BoxSetHW_FlowCtrl(port->
serial, index, 1);
1247 dev_dbg(&port->
dev,
"BoxSetHW_FlowCtrl failed\n");
1252 dev_dbg(&port->
dev,
"%s - disabling HW flow control port %d\n", __func__, port->
number);
1254 status = BoxSetHW_FlowCtrl(port->
serial, index, 0);
1256 dev_dbg(&port->
dev,
"BoxSetHW_FlowCtrl failed\n");
1265 unsigned char stop_char =
STOP_CHAR(tty);
1268 BoxSetSW_FlowCtrl(port->
serial, index, stop_char,
1271 dev_dbg(&port->
dev,
"BoxSetSW_FlowCtrl (enabled) failed\n");
1275 status = BoxDisable_SW_FlowCtrl(port->
serial, index);
1277 dev_dbg(&port->
dev,
"BoxSetSW_FlowCtrl (diabling) failed\n");
1284 static void qt_break(
struct tty_struct *tty,
int break_state)
1287 struct usb_serial *serial = get_usb_serial(port, __func__);
1288 struct quatech_port *qt_port;
1294 qt_port = qt_get_port_private(port);
1296 if (break_state == -1)
1310 static inline int qt_real_tiocmget(
struct tty_struct *tty,
1317 unsigned int result = 0;
1326 BoxGetRegister(port->
serial, index,
1350 static inline int qt_real_tiocmset(
struct tty_struct *tty,
1386 static int qt_tiocmget(
struct tty_struct *tty)
1389 struct usb_serial *serial = get_usb_serial(port, __func__);
1390 struct quatech_port *qt_port = qt_get_port_private(port);
1397 retval = qt_real_tiocmget(tty, port, serial);
1402 static int qt_tiocmset(
struct tty_struct *tty,
1403 unsigned int set,
unsigned int clear)
1407 struct usb_serial *serial = get_usb_serial(port, __func__);
1408 struct quatech_port *qt_port = qt_get_port_private(port);
1415 retval = qt_real_tiocmset(tty, port, serial,
set);
1420 static void qt_throttle(
struct tty_struct *tty)
1423 struct usb_serial *serial = get_usb_serial(port, __func__);
1424 struct quatech_port *qt_port;
1429 qt_port = qt_get_port_private(port);
1439 static void qt_unthrottle(
struct tty_struct *tty)
1442 struct usb_serial *serial = get_usb_serial(port, __func__);
1443 struct quatech_port *qt_port;
1449 qt_port = qt_get_port_private(port);
1454 dev_dbg(&port->
dev,
"%s -qt_port->RxHolding == 1\n", __func__);
1457 dev_dbg(&port->
dev,
"%s - qt_port->RxHolding = 0\n", __func__);
1463 usb_rcvbulkpipe(serial->
dev,
1467 transfer_buffer_length,
1468 qt_read_bulk_callback, port);
1472 "%s - failed restarting read urb, error %d\n",
1479 static int qt_calc_num_ports(
struct usb_serial *serial)
1484 (serial->
interface->cur_altsetting->desc.bNumEndpoints - 1) / 2;
1500 .write_room = qt_write_room,
1501 .chars_in_buffer = qt_chars_in_buffer,
1502 .throttle = qt_throttle,
1503 .unthrottle = qt_unthrottle,
1504 .calc_num_ports = qt_calc_num_ports,
1506 .set_termios = qt_set_termios,
1507 .break_ctl = qt_break,
1508 .tiocmget = qt_tiocmget,
1509 .tiocmset = qt_tiocmset,
1510 .attach = qt_startup,
1511 .release = qt_release,
1515 &quatech_device,
NULL