18 #include <linux/kernel.h>
19 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/tty.h>
25 #include <linux/module.h>
30 #include <linux/wait.h>
38 #define DRIVER_VERSION "v1.80.1.2"
40 #define DRIVER_DESC "Digi AccelePort USB-2/USB-4 Serial Converter driver"
44 #define DIGI_OUT_BUF_SIZE 8
48 #define DIGI_IN_BUF_SIZE 64
51 #define DIGI_RETRY_TIMEOUT (HZ/10)
56 #define DIGI_CLOSE_TIMEOUT (5*HZ)
62 #define DIGI_VENDOR_ID 0x05c5
63 #define DIGI_2_ID 0x0002
64 #define DIGI_4_ID 0x0004
70 #define DIGI_CMD_SET_BAUD_RATE 0
71 #define DIGI_CMD_SET_WORD_SIZE 1
72 #define DIGI_CMD_SET_PARITY 2
73 #define DIGI_CMD_SET_STOP_BITS 3
74 #define DIGI_CMD_SET_INPUT_FLOW_CONTROL 4
75 #define DIGI_CMD_SET_OUTPUT_FLOW_CONTROL 5
76 #define DIGI_CMD_SET_DTR_SIGNAL 6
77 #define DIGI_CMD_SET_RTS_SIGNAL 7
78 #define DIGI_CMD_READ_INPUT_SIGNALS 8
79 #define DIGI_CMD_IFLUSH_FIFO 9
80 #define DIGI_CMD_RECEIVE_ENABLE 10
81 #define DIGI_CMD_BREAK_CONTROL 11
82 #define DIGI_CMD_LOCAL_LOOPBACK 12
83 #define DIGI_CMD_TRANSMIT_IDLE 13
84 #define DIGI_CMD_READ_UART_REGISTER 14
85 #define DIGI_CMD_WRITE_UART_REGISTER 15
86 #define DIGI_CMD_AND_UART_REGISTER 16
87 #define DIGI_CMD_OR_UART_REGISTER 17
88 #define DIGI_CMD_SEND_DATA 18
89 #define DIGI_CMD_RECEIVE_DATA 19
90 #define DIGI_CMD_RECEIVE_DISABLE 20
91 #define DIGI_CMD_GET_PORT_TYPE 21
94 #define DIGI_BAUD_50 0
95 #define DIGI_BAUD_75 1
96 #define DIGI_BAUD_110 2
97 #define DIGI_BAUD_150 3
98 #define DIGI_BAUD_200 4
99 #define DIGI_BAUD_300 5
100 #define DIGI_BAUD_600 6
101 #define DIGI_BAUD_1200 7
102 #define DIGI_BAUD_1800 8
103 #define DIGI_BAUD_2400 9
104 #define DIGI_BAUD_4800 10
105 #define DIGI_BAUD_7200 11
106 #define DIGI_BAUD_9600 12
107 #define DIGI_BAUD_14400 13
108 #define DIGI_BAUD_19200 14
109 #define DIGI_BAUD_28800 15
110 #define DIGI_BAUD_38400 16
111 #define DIGI_BAUD_57600 17
112 #define DIGI_BAUD_76800 18
113 #define DIGI_BAUD_115200 19
114 #define DIGI_BAUD_153600 20
115 #define DIGI_BAUD_230400 21
116 #define DIGI_BAUD_460800 22
119 #define DIGI_WORD_SIZE_5 0
120 #define DIGI_WORD_SIZE_6 1
121 #define DIGI_WORD_SIZE_7 2
122 #define DIGI_WORD_SIZE_8 3
124 #define DIGI_PARITY_NONE 0
125 #define DIGI_PARITY_ODD 1
126 #define DIGI_PARITY_EVEN 2
127 #define DIGI_PARITY_MARK 3
128 #define DIGI_PARITY_SPACE 4
130 #define DIGI_STOP_BITS_1 0
131 #define DIGI_STOP_BITS_2 1
133 #define DIGI_INPUT_FLOW_CONTROL_XON_XOFF 1
134 #define DIGI_INPUT_FLOW_CONTROL_RTS 2
135 #define DIGI_INPUT_FLOW_CONTROL_DTR 4
137 #define DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF 1
138 #define DIGI_OUTPUT_FLOW_CONTROL_CTS 2
139 #define DIGI_OUTPUT_FLOW_CONTROL_DSR 4
141 #define DIGI_DTR_INACTIVE 0
142 #define DIGI_DTR_ACTIVE 1
143 #define DIGI_DTR_INPUT_FLOW_CONTROL 2
145 #define DIGI_RTS_INACTIVE 0
146 #define DIGI_RTS_ACTIVE 1
147 #define DIGI_RTS_INPUT_FLOW_CONTROL 2
148 #define DIGI_RTS_TOGGLE 3
150 #define DIGI_FLUSH_TX 1
151 #define DIGI_FLUSH_RX 2
152 #define DIGI_RESUME_TX 4
154 #define DIGI_TRANSMIT_NOT_IDLE 0
155 #define DIGI_TRANSMIT_IDLE 1
157 #define DIGI_DISABLE 0
158 #define DIGI_ENABLE 1
160 #define DIGI_DEASSERT 0
161 #define DIGI_ASSERT 1
164 #define DIGI_OVERRUN_ERROR 4
165 #define DIGI_PARITY_ERROR 8
166 #define DIGI_FRAMING_ERROR 16
167 #define DIGI_BREAK_ERROR 32
170 #define DIGI_NO_ERROR 0
171 #define DIGI_BAD_FIRST_PARAMETER 1
172 #define DIGI_BAD_SECOND_PARAMETER 2
173 #define DIGI_INVALID_LINE 3
174 #define DIGI_INVALID_OPCODE 4
177 #define DIGI_READ_INPUT_SIGNALS_SLOT 1
178 #define DIGI_READ_INPUT_SIGNALS_ERR 2
179 #define DIGI_READ_INPUT_SIGNALS_BUSY 4
180 #define DIGI_READ_INPUT_SIGNALS_PE 8
181 #define DIGI_READ_INPUT_SIGNALS_CTS 16
182 #define DIGI_READ_INPUT_SIGNALS_DSR 32
183 #define DIGI_READ_INPUT_SIGNALS_RI 64
184 #define DIGI_READ_INPUT_SIGNALS_DCD 128
220 unsigned char *
buf,
int count,
int interruptible);
222 unsigned char *
buf,
int count,
unsigned long timeout);
224 unsigned int modem_signals,
int interruptible);
226 unsigned long timeout);
227 static void digi_rx_throttle(
struct tty_struct *tty);
228 static void digi_rx_unthrottle(
struct tty_struct *tty);
229 static void digi_set_termios(
struct tty_struct *tty,
231 static void digi_break_ctl(
struct tty_struct *tty,
int break_state);
232 static int digi_tiocmget(
struct tty_struct *tty);
233 static int digi_tiocmset(
struct tty_struct *tty,
unsigned int set,
236 const unsigned char *
buf,
int count);
237 static void digi_write_bulk_callback(
struct urb *
urb);
238 static int digi_write_room(
struct tty_struct *tty);
239 static int digi_chars_in_buffer(
struct tty_struct *tty);
249 static void digi_read_bulk_callback(
struct urb *
urb);
250 static int digi_read_inb_callback(
struct urb *
urb);
251 static int digi_read_oob_callback(
struct urb *
urb);
279 .description =
"Digi 2 port USB adapter",
280 .id_table = id_table_2,
284 .dtr_rts = digi_dtr_rts,
286 .write_room = digi_write_room,
287 .write_bulk_callback = digi_write_bulk_callback,
288 .read_bulk_callback = digi_read_bulk_callback,
289 .chars_in_buffer = digi_chars_in_buffer,
290 .throttle = digi_rx_throttle,
291 .unthrottle = digi_rx_unthrottle,
292 .set_termios = digi_set_termios,
293 .break_ctl = digi_break_ctl,
294 .tiocmget = digi_tiocmget,
295 .tiocmset = digi_tiocmset,
296 .attach = digi_startup,
297 .disconnect = digi_disconnect,
298 .release = digi_release,
299 .port_probe = digi_port_probe,
300 .port_remove = digi_port_remove,
308 .description =
"Digi 4 port USB adapter",
309 .id_table = id_table_4,
314 .write_room = digi_write_room,
315 .write_bulk_callback = digi_write_bulk_callback,
316 .read_bulk_callback = digi_read_bulk_callback,
317 .chars_in_buffer = digi_chars_in_buffer,
318 .throttle = digi_rx_throttle,
319 .unthrottle = digi_rx_unthrottle,
320 .set_termios = digi_set_termios,
321 .break_ctl = digi_break_ctl,
322 .tiocmget = digi_tiocmget,
323 .tiocmset = digi_tiocmset,
324 .attach = digi_startup,
325 .disconnect = digi_disconnect,
326 .release = digi_release,
327 .port_probe = digi_port_probe,
328 .port_remove = digi_port_remove,
332 &digi_acceleport_2_device, &digi_acceleport_4_device,
NULL
350 static long cond_wait_interruptible_timeout_irqrestore(
358 spin_unlock_irqrestore(lock,
flags);
381 digi_wakeup_write(port);
407 unsigned char *
buf,
int count,
int interruptible)
412 struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
413 unsigned long flags = 0;
416 "digi_write_oob_command: TOP: port=%d, count=%d\n",
422 cond_wait_interruptible_timeout_irqrestore(
425 if (interruptible && signal_pending(
current))
435 oob_port->
write_urb->transfer_buffer_length = len;
445 dev_err(&port->
dev,
"%s: usb_submit_urb failed, ret=%d\n",
465 unsigned char *buf,
int count,
unsigned long timeout)
471 unsigned long flags = 0;
473 dev_dbg(&port->
dev,
"digi_write_inb_command: TOP: port=%d, count=%d\n",
482 while (count > 0 && ret == 0) {
485 cond_wait_interruptible_timeout_irqrestore(
511 port->
write_urb->transfer_buffer_length = len;
527 "%s: usb_submit_urb failed, ret=%d, port=%d\n",
544 unsigned int modem_signals,
int interruptible)
548 struct digi_port *port_priv = usb_get_serial_port_data(port);
550 struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
551 unsigned char *data = oob_port->
write_urb->transfer_buffer;
552 unsigned long flags = 0;
556 "digi_set_modem_signals: TOP: port=%d, modem_signals=0x%x\n",
564 cond_wait_interruptible_timeout_irqrestore(
567 if (interruptible && signal_pending(
current))
583 oob_port->
write_urb->transfer_buffer_length = 8;
595 dev_err(&port->
dev,
"%s: usb_submit_urb failed, ret=%d\n",
613 unsigned long timeout)
616 unsigned char buf[2];
617 struct digi_port *priv = usb_get_serial_port_data(port);
618 unsigned long flags = 0;
629 ret = digi_write_inb_command(port, buf, 2, timeout - jiffies);
636 cond_wait_interruptible_timeout_irqrestore(
650 static void digi_rx_throttle(
struct tty_struct *tty)
654 struct digi_port *priv = usb_get_serial_port_data(port);
664 static void digi_rx_unthrottle(
struct tty_struct *tty)
669 struct digi_port *priv = usb_get_serial_port_data(port);
685 "%s: usb_submit_urb failed, ret=%d, port=%d\n",
690 static void digi_set_termios(
struct tty_struct *tty,
693 struct digi_port *priv = usb_get_serial_port_data(port);
695 unsigned int iflag = tty->termios.c_iflag;
696 unsigned int cflag = tty->termios.c_cflag;
697 unsigned int old_iflag = old_termios->
c_iflag;
698 unsigned int old_cflag = old_termios->
c_cflag;
699 unsigned char buf[32];
700 unsigned int modem_signals;
706 "digi_set_termios: TOP: port=%d, iflag=0x%x, old_iflag=0x%x, cflag=0x%x, old_cflag=0x%x\n",
707 priv->
dp_port_num, iflag, old_iflag, cflag, old_cflag);
719 if (!(tty->termios.c_cflag &
CRTSCTS) ||
722 digi_set_modem_signals(port, modem_signals, 1);
726 case 0: digi_set_modem_signals(port, 0, 1);
break;
758 tty->termios.c_cflag &= ~
CMSPAR;
775 if ((cflag&
CSIZE) != (old_cflag&CSIZE)) {
777 switch (cflag&CSIZE) {
784 "digi_set_termios: can't handle word size %d\n",
799 if ((cflag&
CSTOPB) != (old_cflag&CSTOPB)) {
814 if ((iflag&
IXOFF) != (old_iflag&IXOFF)
815 || (cflag&
CRTSCTS) != (old_cflag&CRTSCTS)) {
842 if ((iflag &
IXON) != (old_iflag & IXON)
843 || (cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
850 if (cflag & CRTSCTS) {
864 if ((cflag &
CREAD) != (old_cflag & CREAD)) {
875 ret = digi_write_oob_command(port, buf, i, 1);
877 dev_dbg(dev,
"digi_set_termios: write oob failed, ret=%d\n", ret);
882 static void digi_break_ctl(
struct tty_struct *tty,
int break_state)
885 unsigned char buf[4];
889 buf[2] = break_state ? 1 : 0;
891 digi_write_inb_command(port, buf, 4, 0);
895 static int digi_tiocmget(
struct tty_struct *tty)
898 struct digi_port *priv = usb_get_serial_port_data(port);
909 static int digi_tiocmset(
struct tty_struct *tty,
910 unsigned int set,
unsigned int clear)
913 struct digi_port *priv = usb_get_serial_port_data(port);
920 return digi_set_modem_signals(port, val, 1);
925 const unsigned char *buf,
int count)
929 struct digi_port *priv = usb_get_serial_port_data(port);
930 unsigned char *data = port->
write_urb->transfer_buffer;
931 unsigned long flags = 0;
934 "digi_write: TOP: port=%d, count=%d, in_interrupt=%ld\n",
939 count =
min(64, count);
968 port->
write_urb->transfer_buffer_length = data_len+2;
978 memcpy(data, buf, new_len);
991 "%s: usb_submit_urb failed, ret=%d, port=%d\n",
993 dev_dbg(&port->
dev,
"digi_write: returning %d\n", ret);
998 static void digi_write_bulk_callback(
struct urb *
urb)
1006 int status = urb->status;
1009 if (port ==
NULL || (priv = usb_get_serial_port_data(port)) ==
NULL) {
1010 pr_err(
"%s: port or port->private is NULL, status=%d\n",
1015 if (serial ==
NULL || (serial_priv = usb_get_serial_data(serial)) ==
NULL) {
1017 "%s: serial or serial->private is NULL, status=%d\n",
1024 dev_dbg(&port->
dev,
"digi_write_bulk_callback: oob callback\n");
1036 *((
unsigned char *)(port->
write_urb->transfer_buffer))
1038 *((
unsigned char *)(port->
write_urb->transfer_buffer) + 1)
1040 port->
write_urb->transfer_buffer_length =
1051 digi_wakeup_write(port);
1057 if (ret && ret != -
EPERM)
1059 "%s: usb_submit_urb failed, ret=%d, port=%d\n",
1063 static int digi_write_room(
struct tty_struct *tty)
1066 struct digi_port *priv = usb_get_serial_port_data(port);
1068 unsigned long flags = 0;
1083 static int digi_chars_in_buffer(
struct tty_struct *tty)
1086 struct digi_port *priv = usb_get_serial_port_data(port);
1089 dev_dbg(&port->
dev,
"digi_chars_in_buffer: port=%d, chars=%d\n",
1094 dev_dbg(&port->
dev,
"digi_chars_in_buffer: port=%d, chars=%d\n",
1110 unsigned char buf[32];
1111 struct digi_port *priv = usb_get_serial_port_data(port);
1115 if (digi_startup_device(port->
serial) != 0)
1130 ret = digi_write_oob_command(port, buf, 8, 1);
1132 dev_dbg(&port->
dev,
"digi_open: write oob failed, ret=%d\n", ret);
1136 not_termios.c_cflag = ~tty->termios.c_cflag;
1137 not_termios.c_iflag = ~tty->termios.c_iflag;
1138 digi_set_termios(tty, port, ¬_termios);
1148 unsigned char buf[32];
1149 struct digi_port *priv = usb_get_serial_port_data(port);
1153 if (port->
serial->disconnected)
1190 ret = digi_write_oob_command(port, buf, 20, 0);
1192 dev_dbg(&port->
dev,
"digi_close: write oob failed, ret=%d\n", ret);
1219 static int digi_startup_device(
struct usb_serial *serial)
1222 struct digi_serial *serial_priv = usb_get_serial_data(serial);
1236 for (i = 0; i < serial->
type->num_ports + 1; i++) {
1237 port = serial->
port[
i];
1241 "%s: usb_submit_urb failed, ret=%d, port=%d\n",
1268 usb_set_serial_port_data(port, priv);
1273 static int digi_startup(
struct usb_serial *serial)
1278 serial_priv = kzalloc(
sizeof(*serial_priv),
GFP_KERNEL);
1293 usb_set_serial_data(serial, serial_priv);
1299 static void digi_disconnect(
struct usb_serial *serial)
1304 for (i = 0; i < serial->
type->num_ports + 1; i++) {
1311 static void digi_release(
struct usb_serial *serial)
1316 serial_priv = usb_get_serial_data(serial);
1318 priv = usb_get_serial_port_data(serial_priv->
ds_oob_port);
1330 return digi_port_init(port, port_num);
1337 priv = usb_get_serial_port_data(port);
1343 static void digi_read_bulk_callback(
struct urb *urb)
1349 int status = urb->status;
1354 priv = usb_get_serial_port_data(port);
1356 dev_err(&port->
dev,
"%s: port->private is NULL, status=%d\n",
1361 (serial_priv = usb_get_serial_data(port->
serial)) ==
NULL) {
1362 dev_err(&port->
dev,
"%s: serial is bad or serial->private "
1363 "is NULL, status=%d\n", __func__, status);
1370 "%s: nonzero read bulk status: status=%d, port=%d\n",
1377 if (digi_read_oob_callback(urb) != 0)
1380 if (digi_read_inb_callback(urb) != 0)
1386 if (ret != 0 && ret != -
EPERM) {
1388 "%s: failed resubmitting urb, ret=%d, port=%d\n",
1404 static int digi_read_inb_callback(
struct urb *urb)
1409 struct digi_port *priv = usb_get_serial_port_data(port);
1410 int opcode = ((
unsigned char *)urb->transfer_buffer)[0];
1411 int len = ((
unsigned char *)urb->transfer_buffer)[1];
1412 int port_status = ((
unsigned char *)urb->transfer_buffer)[2];
1413 unsigned char *data = ((
unsigned char *)urb->transfer_buffer) + 3;
1414 int flag, throttled;
1415 int status = urb->status;
1419 if (urb->status == -
ENOENT)
1423 if (urb->actual_length != len + 2) {
1424 dev_err(&port->
dev,
"%s: INCOMPLETE OR MULTIPLE PACKET, "
1425 "status=%d, port=%d, opcode=%d, len=%d, "
1426 "actual_length=%d, status=%d\n", __func__, status,
1427 priv->
dp_port_num, opcode, len, urb->actual_length,
1471 dev_dbg(&port->
dev,
"%s: got RECEIVE_DISABLE\n", __func__);
1473 dev_dbg(&port->
dev,
"%s: unknown opcode: %d\n", __func__, opcode);
1475 return throttled ? 1 : 0;
1489 static int digi_read_oob_callback(
struct urb *urb)
1495 struct digi_port *priv = usb_get_serial_port_data(port);
1501 for (i = 0; i < urb->actual_length - 3;) {
1502 opcode = ((
unsigned char *)urb->transfer_buffer)[i++];
1503 line = ((
unsigned char *)urb->transfer_buffer)[i++];
1504 status = ((
unsigned char *)urb->transfer_buffer)[i++];
1505 val = ((
unsigned char *)urb->transfer_buffer)[i++];
1507 dev_dbg(&port->
dev,
"digi_read_oob_callback: opcode=%d, line=%d, status=%d, val=%d\n",
1508 opcode, line, status, val);
1510 if (status != 0 || line >= serial->
type->num_ports)
1513 port = serial->
port[line];
1515 priv = usb_get_serial_port_data(port);
1523 rts = tty->termios.c_cflag &
CRTSCTS;
1533 digi_wakeup_write(port);