2 * Digi AccelePort USB-4 and USB-2 Serial Converters
4 * Copyright 2000 by Digi International
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Shamelessly based on Brian Warner's keyspan_pda.c and Greg Kroah-Hartman's
14 * Peter Berger (pberger@brimson.com)
15 * Al Borchers (borchers@steinerpoint.com)
18 #include <linux/kernel.h>
19 #include <linux/errno.h>
20 #include <linux/init.h>
21 #include <linux/slab.h>
22 #include <linux/tty.h>
23 #include <linux/tty_driver.h>
24 #include <linux/tty_flip.h>
25 #include <linux/module.h>
26 #include <linux/spinlock.h>
27 #include <linux/workqueue.h>
28 #include <linux/uaccess.h>
29 #include <linux/usb.h>
30 #include <linux/wait.h>
31 #include <linux/usb/serial.h>
38 #define DRIVER_VERSION "v1.80.1.2"
39 #define DRIVER_AUTHOR "Peter Berger <pberger@brimson.com>, Al Borchers <borchers@steinerpoint.com>"
40 #define DRIVER_DESC "Digi AccelePort USB-2/USB-4 Serial Converter driver"
42 /* port output buffer length -- must be <= transfer buffer length - 2 */
43 /* so we can be sure to send the full buffer in one urb */
44 #define DIGI_OUT_BUF_SIZE 8
46 /* port input buffer length -- must be >= transfer buffer length - 3 */
47 /* so we can be sure to hold at least one full buffer from one urb */
48 #define DIGI_IN_BUF_SIZE 64
50 /* retry timeout while sleeping */
51 #define DIGI_RETRY_TIMEOUT (HZ/10)
53 /* timeout while waiting for tty output to drain in close */
54 /* this delay is used twice in close, so the total delay could */
55 /* be twice this value */
56 #define DIGI_CLOSE_TIMEOUT (5*HZ)
59 /* AccelePort USB Defines */
62 #define DIGI_VENDOR_ID 0x05c5
63 #define DIGI_2_ID 0x0002 /* USB-2 */
64 #define DIGI_4_ID 0x0004 /* USB-4 */
67 * "INB": can be used on the in-band endpoint
68 * "OOB": can be used on the out-of-band endpoint
70 #define DIGI_CMD_SET_BAUD_RATE 0 /* INB, OOB */
71 #define DIGI_CMD_SET_WORD_SIZE 1 /* INB, OOB */
72 #define DIGI_CMD_SET_PARITY 2 /* INB, OOB */
73 #define DIGI_CMD_SET_STOP_BITS 3 /* INB, OOB */
74 #define DIGI_CMD_SET_INPUT_FLOW_CONTROL 4 /* INB, OOB */
75 #define DIGI_CMD_SET_OUTPUT_FLOW_CONTROL 5 /* INB, OOB */
76 #define DIGI_CMD_SET_DTR_SIGNAL 6 /* INB, OOB */
77 #define DIGI_CMD_SET_RTS_SIGNAL 7 /* INB, OOB */
78 #define DIGI_CMD_READ_INPUT_SIGNALS 8 /* OOB */
79 #define DIGI_CMD_IFLUSH_FIFO 9 /* OOB */
80 #define DIGI_CMD_RECEIVE_ENABLE 10 /* INB, OOB */
81 #define DIGI_CMD_BREAK_CONTROL 11 /* INB, OOB */
82 #define DIGI_CMD_LOCAL_LOOPBACK 12 /* INB, OOB */
83 #define DIGI_CMD_TRANSMIT_IDLE 13 /* INB, OOB */
84 #define DIGI_CMD_READ_UART_REGISTER 14 /* OOB */
85 #define DIGI_CMD_WRITE_UART_REGISTER 15 /* INB, OOB */
86 #define DIGI_CMD_AND_UART_REGISTER 16 /* INB, OOB */
87 #define DIGI_CMD_OR_UART_REGISTER 17 /* INB, OOB */
88 #define DIGI_CMD_SEND_DATA 18 /* INB */
89 #define DIGI_CMD_RECEIVE_DATA 19 /* INB */
90 #define DIGI_CMD_RECEIVE_DISABLE 20 /* INB */
91 #define DIGI_CMD_GET_PORT_TYPE 21 /* OOB */
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 /* clears xoff condition */
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
163 /* in band status codes */
164 #define DIGI_OVERRUN_ERROR 4
165 #define DIGI_PARITY_ERROR 8
166 #define DIGI_FRAMING_ERROR 16
167 #define DIGI_BREAK_ERROR 32
169 /* out of band status */
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
190 spinlock_t ds_serial_lock;
191 struct usb_serial_port *ds_oob_port; /* out-of-band port */
192 int ds_oob_port_num; /* index of out-of-band port */
193 int ds_device_started;
197 spinlock_t dp_port_lock;
200 unsigned char dp_out_buf[DIGI_OUT_BUF_SIZE];
201 int dp_write_urb_in_use;
202 unsigned int dp_modem_signals;
203 wait_queue_head_t dp_modem_change_wait;
204 int dp_transmit_idle;
205 wait_queue_head_t dp_transmit_idle_wait;
207 int dp_throttle_restart;
208 wait_queue_head_t dp_flush_wait;
209 wait_queue_head_t dp_close_wait; /* wait queue for close */
210 struct work_struct dp_wakeup_work;
211 struct usb_serial_port *dp_port;
215 /* Local Function Declarations */
217 static void digi_wakeup_write(struct usb_serial_port *port);
218 static void digi_wakeup_write_lock(struct work_struct *work);
219 static int digi_write_oob_command(struct usb_serial_port *port,
220 unsigned char *buf, int count, int interruptible);
221 static int digi_write_inb_command(struct usb_serial_port *port,
222 unsigned char *buf, int count, unsigned long timeout);
223 static int digi_set_modem_signals(struct usb_serial_port *port,
224 unsigned int modem_signals, int interruptible);
225 static int digi_transmit_idle(struct usb_serial_port *port,
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,
230 struct usb_serial_port *port, struct ktermios *old_termios);
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,
235 static int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
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);
240 static int digi_open(struct tty_struct *tty, struct usb_serial_port *port);
241 static void digi_close(struct usb_serial_port *port);
242 static void digi_dtr_rts(struct usb_serial_port *port, int on);
243 static int digi_startup_device(struct usb_serial *serial);
244 static int digi_startup(struct usb_serial *serial);
245 static void digi_disconnect(struct usb_serial *serial);
246 static void digi_release(struct usb_serial *serial);
247 static void digi_read_bulk_callback(struct urb *urb);
248 static int digi_read_inb_callback(struct urb *urb);
249 static int digi_read_oob_callback(struct urb *urb);
256 static const struct usb_device_id id_table_combined[] = {
257 { USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
258 { USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
259 { } /* Terminating entry */
262 static const struct usb_device_id id_table_2[] = {
263 { USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
264 { } /* Terminating entry */
267 static const struct usb_device_id id_table_4[] = {
268 { USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
269 { } /* Terminating entry */
272 MODULE_DEVICE_TABLE(usb, id_table_combined);
274 static struct usb_driver digi_driver = {
275 .name = "digi_acceleport",
276 .disconnect = usb_serial_disconnect,
277 .id_table = id_table_combined,
281 /* device info needed for the Digi serial converter */
283 static struct usb_serial_driver digi_acceleport_2_device = {
285 .owner = THIS_MODULE,
288 .description = "Digi 2 port USB adapter",
289 .id_table = id_table_2,
293 .dtr_rts = digi_dtr_rts,
295 .write_room = digi_write_room,
296 .write_bulk_callback = digi_write_bulk_callback,
297 .read_bulk_callback = digi_read_bulk_callback,
298 .chars_in_buffer = digi_chars_in_buffer,
299 .throttle = digi_rx_throttle,
300 .unthrottle = digi_rx_unthrottle,
301 .set_termios = digi_set_termios,
302 .break_ctl = digi_break_ctl,
303 .tiocmget = digi_tiocmget,
304 .tiocmset = digi_tiocmset,
305 .attach = digi_startup,
306 .disconnect = digi_disconnect,
307 .release = digi_release,
310 static struct usb_serial_driver digi_acceleport_4_device = {
312 .owner = THIS_MODULE,
315 .description = "Digi 4 port USB adapter",
316 .id_table = id_table_4,
321 .write_room = digi_write_room,
322 .write_bulk_callback = digi_write_bulk_callback,
323 .read_bulk_callback = digi_read_bulk_callback,
324 .chars_in_buffer = digi_chars_in_buffer,
325 .throttle = digi_rx_throttle,
326 .unthrottle = digi_rx_unthrottle,
327 .set_termios = digi_set_termios,
328 .break_ctl = digi_break_ctl,
329 .tiocmget = digi_tiocmget,
330 .tiocmset = digi_tiocmset,
331 .attach = digi_startup,
332 .disconnect = digi_disconnect,
333 .release = digi_release,
336 static struct usb_serial_driver * const serial_drivers[] = {
337 &digi_acceleport_2_device, &digi_acceleport_4_device, NULL
343 * Cond Wait Interruptible Timeout Irqrestore
345 * Do spin_unlock_irqrestore and interruptible_sleep_on_timeout
346 * so that wake ups are not lost if they occur between the unlock
347 * and the sleep. In other words, spin_unlock_irqrestore and
348 * interruptible_sleep_on_timeout are "atomic" with respect to
349 * wake ups. This is used to implement condition variables.
351 * interruptible_sleep_on_timeout is deprecated and has been replaced
352 * with the equivalent code.
355 static long cond_wait_interruptible_timeout_irqrestore(
356 wait_queue_head_t *q, long timeout,
357 spinlock_t *lock, unsigned long flags)
362 prepare_to_wait(q, &wait, TASK_INTERRUPTIBLE);
363 spin_unlock_irqrestore(lock, flags);
364 timeout = schedule_timeout(timeout);
365 finish_wait(q, &wait);
374 * Wake up port, line discipline, and tty processes sleeping
378 static void digi_wakeup_write_lock(struct work_struct *work)
380 struct digi_port *priv =
381 container_of(work, struct digi_port, dp_wakeup_work);
382 struct usb_serial_port *port = priv->dp_port;
385 spin_lock_irqsave(&priv->dp_port_lock, flags);
386 digi_wakeup_write(port);
387 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
390 static void digi_wakeup_write(struct usb_serial_port *port)
392 struct tty_struct *tty = tty_port_tty_get(&port->port);
401 * Digi Write OOB Command
403 * Write commands on the out of band port. Commands are 4
404 * bytes each, multiple commands can be sent at once, and
405 * no command will be split across USB packets. Returns 0
406 * if successful, -EINTR if interrupted while sleeping and
407 * the interruptible flag is true, or a negative error
408 * returned by usb_submit_urb.
411 static int digi_write_oob_command(struct usb_serial_port *port,
412 unsigned char *buf, int count, int interruptible)
417 struct usb_serial_port *oob_port = (struct usb_serial_port *)((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
418 struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
419 unsigned long flags = 0;
421 dbg("digi_write_oob_command: TOP: port=%d, count=%d", oob_priv->dp_port_num, count);
423 spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
425 while (oob_priv->dp_write_urb_in_use) {
426 cond_wait_interruptible_timeout_irqrestore(
427 &oob_port->write_wait, DIGI_RETRY_TIMEOUT,
428 &oob_priv->dp_port_lock, flags);
429 if (interruptible && signal_pending(current))
431 spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
434 /* len must be a multiple of 4, so commands are not split */
435 len = min(count, oob_port->bulk_out_size);
438 memcpy(oob_port->write_urb->transfer_buffer, buf, len);
439 oob_port->write_urb->transfer_buffer_length = len;
440 ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
442 oob_priv->dp_write_urb_in_use = 1;
447 spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
449 dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n",
457 * Digi Write In Band Command
459 * Write commands on the given port. Commands are 4
460 * bytes each, multiple commands can be sent at once, and
461 * no command will be split across USB packets. If timeout
462 * is non-zero, write in band command will return after
463 * waiting unsuccessfully for the URB status to clear for
464 * timeout ticks. Returns 0 if successful, or a negative
465 * error returned by digi_write.
468 static int digi_write_inb_command(struct usb_serial_port *port,
469 unsigned char *buf, int count, unsigned long timeout)
473 struct digi_port *priv = usb_get_serial_port_data(port);
474 unsigned char *data = port->write_urb->transfer_buffer;
475 unsigned long flags = 0;
477 dbg("digi_write_inb_command: TOP: port=%d, count=%d",
478 priv->dp_port_num, count);
485 spin_lock_irqsave(&priv->dp_port_lock, flags);
486 while (count > 0 && ret == 0) {
487 while (priv->dp_write_urb_in_use &&
488 time_before(jiffies, timeout)) {
489 cond_wait_interruptible_timeout_irqrestore(
490 &port->write_wait, DIGI_RETRY_TIMEOUT,
491 &priv->dp_port_lock, flags);
492 if (signal_pending(current))
494 spin_lock_irqsave(&priv->dp_port_lock, flags);
497 /* len must be a multiple of 4 and small enough to */
498 /* guarantee the write will send buffered data first, */
499 /* so commands are in order with data and not split */
500 len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
504 /* write any buffered data first */
505 if (priv->dp_out_buf_len > 0) {
506 data[0] = DIGI_CMD_SEND_DATA;
507 data[1] = priv->dp_out_buf_len;
508 memcpy(data + 2, priv->dp_out_buf,
509 priv->dp_out_buf_len);
510 memcpy(data + 2 + priv->dp_out_buf_len, buf, len);
511 port->write_urb->transfer_buffer_length
512 = priv->dp_out_buf_len + 2 + len;
514 memcpy(data, buf, len);
515 port->write_urb->transfer_buffer_length = len;
518 ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
520 priv->dp_write_urb_in_use = 1;
521 priv->dp_out_buf_len = 0;
527 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
531 "%s: usb_submit_urb failed, ret=%d, port=%d\n",
532 __func__, ret, priv->dp_port_num);
538 * Digi Set Modem Signals
540 * Sets or clears DTR and RTS on the port, according to the
541 * modem_signals argument. Use TIOCM_DTR and TIOCM_RTS flags
542 * for the modem_signals argument. Returns 0 if successful,
543 * -EINTR if interrupted while sleeping, or a non-zero error
544 * returned by usb_submit_urb.
547 static int digi_set_modem_signals(struct usb_serial_port *port,
548 unsigned int modem_signals, int interruptible)
552 struct digi_port *port_priv = usb_get_serial_port_data(port);
553 struct usb_serial_port *oob_port = (struct usb_serial_port *) ((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
554 struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
555 unsigned char *data = oob_port->write_urb->transfer_buffer;
556 unsigned long flags = 0;
559 dbg("digi_set_modem_signals: TOP: port=%d, modem_signals=0x%x",
560 port_priv->dp_port_num, modem_signals);
562 spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
563 spin_lock(&port_priv->dp_port_lock);
565 while (oob_priv->dp_write_urb_in_use) {
566 spin_unlock(&port_priv->dp_port_lock);
567 cond_wait_interruptible_timeout_irqrestore(
568 &oob_port->write_wait, DIGI_RETRY_TIMEOUT,
569 &oob_priv->dp_port_lock, flags);
570 if (interruptible && signal_pending(current))
572 spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
573 spin_lock(&port_priv->dp_port_lock);
575 data[0] = DIGI_CMD_SET_DTR_SIGNAL;
576 data[1] = port_priv->dp_port_num;
577 data[2] = (modem_signals & TIOCM_DTR) ?
578 DIGI_DTR_ACTIVE : DIGI_DTR_INACTIVE;
580 data[4] = DIGI_CMD_SET_RTS_SIGNAL;
581 data[5] = port_priv->dp_port_num;
582 data[6] = (modem_signals & TIOCM_RTS) ?
583 DIGI_RTS_ACTIVE : DIGI_RTS_INACTIVE;
586 oob_port->write_urb->transfer_buffer_length = 8;
588 ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
590 oob_priv->dp_write_urb_in_use = 1;
591 port_priv->dp_modem_signals =
592 (port_priv->dp_modem_signals&~(TIOCM_DTR|TIOCM_RTS))
593 | (modem_signals&(TIOCM_DTR|TIOCM_RTS));
595 spin_unlock(&port_priv->dp_port_lock);
596 spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
598 dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n",
606 * Digi transmit idle waits, up to timeout ticks, for the transmitter
607 * to go idle. It returns 0 if successful or a negative error.
609 * There are race conditions here if more than one process is calling
610 * digi_transmit_idle on the same port at the same time. However, this
611 * is only called from close, and only one process can be in close on a
612 * port at a time, so its ok.
615 static int digi_transmit_idle(struct usb_serial_port *port,
616 unsigned long timeout)
619 unsigned char buf[2];
620 struct digi_port *priv = usb_get_serial_port_data(port);
621 unsigned long flags = 0;
623 spin_lock_irqsave(&priv->dp_port_lock, flags);
624 priv->dp_transmit_idle = 0;
625 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
627 buf[0] = DIGI_CMD_TRANSMIT_IDLE;
632 ret = digi_write_inb_command(port, buf, 2, timeout - jiffies);
636 spin_lock_irqsave(&priv->dp_port_lock, flags);
638 while (time_before(jiffies, timeout) && !priv->dp_transmit_idle) {
639 cond_wait_interruptible_timeout_irqrestore(
640 &priv->dp_transmit_idle_wait, DIGI_RETRY_TIMEOUT,
641 &priv->dp_port_lock, flags);
642 if (signal_pending(current))
644 spin_lock_irqsave(&priv->dp_port_lock, flags);
646 priv->dp_transmit_idle = 0;
647 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
653 static void digi_rx_throttle(struct tty_struct *tty)
656 struct usb_serial_port *port = tty->driver_data;
657 struct digi_port *priv = usb_get_serial_port_data(port);
659 /* stop receiving characters by not resubmitting the read urb */
660 spin_lock_irqsave(&priv->dp_port_lock, flags);
661 priv->dp_throttled = 1;
662 priv->dp_throttle_restart = 0;
663 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
667 static void digi_rx_unthrottle(struct tty_struct *tty)
671 struct usb_serial_port *port = tty->driver_data;
672 struct digi_port *priv = usb_get_serial_port_data(port);
674 spin_lock_irqsave(&priv->dp_port_lock, flags);
676 /* restart read chain */
677 if (priv->dp_throttle_restart)
678 ret = usb_submit_urb(port->read_urb, GFP_ATOMIC);
680 /* turn throttle off */
681 priv->dp_throttled = 0;
682 priv->dp_throttle_restart = 0;
684 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
688 "%s: usb_submit_urb failed, ret=%d, port=%d\n",
689 __func__, ret, priv->dp_port_num);
693 static void digi_set_termios(struct tty_struct *tty,
694 struct usb_serial_port *port, struct ktermios *old_termios)
696 struct digi_port *priv = usb_get_serial_port_data(port);
697 unsigned int iflag = tty->termios->c_iflag;
698 unsigned int cflag = tty->termios->c_cflag;
699 unsigned int old_iflag = old_termios->c_iflag;
700 unsigned int old_cflag = old_termios->c_cflag;
701 unsigned char buf[32];
702 unsigned int modem_signals;
707 dbg("digi_set_termios: TOP: port=%d, iflag=0x%x, old_iflag=0x%x, cflag=0x%x, old_cflag=0x%x", priv->dp_port_num, iflag, old_iflag, cflag, old_cflag);
710 baud = tty_get_baud_rate(tty);
711 if (baud != tty_termios_baud_rate(old_termios)) {
714 /* reassert DTR and (maybe) RTS on transition from B0 */
715 if ((old_cflag&CBAUD) == B0) {
716 /* don't set RTS if using hardware flow control */
717 /* and throttling input */
718 modem_signals = TIOCM_DTR;
719 if (!(tty->termios->c_cflag & CRTSCTS) ||
720 !test_bit(TTY_THROTTLED, &tty->flags))
721 modem_signals |= TIOCM_RTS;
722 digi_set_modem_signals(port, modem_signals, 1);
725 /* drop DTR and RTS on transition to B0 */
726 case 0: digi_set_modem_signals(port, 0, 1); break;
727 case 50: arg = DIGI_BAUD_50; break;
728 case 75: arg = DIGI_BAUD_75; break;
729 case 110: arg = DIGI_BAUD_110; break;
730 case 150: arg = DIGI_BAUD_150; break;
731 case 200: arg = DIGI_BAUD_200; break;
732 case 300: arg = DIGI_BAUD_300; break;
733 case 600: arg = DIGI_BAUD_600; break;
734 case 1200: arg = DIGI_BAUD_1200; break;
735 case 1800: arg = DIGI_BAUD_1800; break;
736 case 2400: arg = DIGI_BAUD_2400; break;
737 case 4800: arg = DIGI_BAUD_4800; break;
738 case 9600: arg = DIGI_BAUD_9600; break;
739 case 19200: arg = DIGI_BAUD_19200; break;
740 case 38400: arg = DIGI_BAUD_38400; break;
741 case 57600: arg = DIGI_BAUD_57600; break;
742 case 115200: arg = DIGI_BAUD_115200; break;
743 case 230400: arg = DIGI_BAUD_230400; break;
744 case 460800: arg = DIGI_BAUD_460800; break;
746 arg = DIGI_BAUD_9600;
751 buf[i++] = DIGI_CMD_SET_BAUD_RATE;
752 buf[i++] = priv->dp_port_num;
758 tty->termios->c_cflag &= ~CMSPAR;
760 if ((cflag&(PARENB|PARODD)) != (old_cflag&(PARENB|PARODD))) {
763 arg = DIGI_PARITY_ODD;
765 arg = DIGI_PARITY_EVEN;
767 arg = DIGI_PARITY_NONE;
769 buf[i++] = DIGI_CMD_SET_PARITY;
770 buf[i++] = priv->dp_port_num;
775 if ((cflag&CSIZE) != (old_cflag&CSIZE)) {
777 switch (cflag&CSIZE) {
778 case CS5: arg = DIGI_WORD_SIZE_5; break;
779 case CS6: arg = DIGI_WORD_SIZE_6; break;
780 case CS7: arg = DIGI_WORD_SIZE_7; break;
781 case CS8: arg = DIGI_WORD_SIZE_8; break;
783 dbg("digi_set_termios: can't handle word size %d",
789 buf[i++] = DIGI_CMD_SET_WORD_SIZE;
790 buf[i++] = priv->dp_port_num;
798 if ((cflag&CSTOPB) != (old_cflag&CSTOPB)) {
801 arg = DIGI_STOP_BITS_2;
803 arg = DIGI_STOP_BITS_1;
805 buf[i++] = DIGI_CMD_SET_STOP_BITS;
806 buf[i++] = priv->dp_port_num;
812 /* set input flow control */
813 if ((iflag&IXOFF) != (old_iflag&IXOFF)
814 || (cflag&CRTSCTS) != (old_cflag&CRTSCTS)) {
817 arg |= DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
819 arg &= ~DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
822 arg |= DIGI_INPUT_FLOW_CONTROL_RTS;
824 /* On USB-4 it is necessary to assert RTS prior */
825 /* to selecting RTS input flow control. */
826 buf[i++] = DIGI_CMD_SET_RTS_SIGNAL;
827 buf[i++] = priv->dp_port_num;
828 buf[i++] = DIGI_RTS_ACTIVE;
832 arg &= ~DIGI_INPUT_FLOW_CONTROL_RTS;
834 buf[i++] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
835 buf[i++] = priv->dp_port_num;
840 /* set output flow control */
841 if ((iflag & IXON) != (old_iflag & IXON)
842 || (cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
845 arg |= DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
847 arg &= ~DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
849 if (cflag & CRTSCTS) {
850 arg |= DIGI_OUTPUT_FLOW_CONTROL_CTS;
852 arg &= ~DIGI_OUTPUT_FLOW_CONTROL_CTS;
856 buf[i++] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
857 buf[i++] = priv->dp_port_num;
862 /* set receive enable/disable */
863 if ((cflag & CREAD) != (old_cflag & CREAD)) {
869 buf[i++] = DIGI_CMD_RECEIVE_ENABLE;
870 buf[i++] = priv->dp_port_num;
874 ret = digi_write_oob_command(port, buf, i, 1);
876 dbg("digi_set_termios: write oob failed, ret=%d", ret);
877 tty_encode_baud_rate(tty, baud, baud);
881 static void digi_break_ctl(struct tty_struct *tty, int break_state)
883 struct usb_serial_port *port = tty->driver_data;
884 unsigned char buf[4];
886 buf[0] = DIGI_CMD_BREAK_CONTROL;
887 buf[1] = 2; /* length */
888 buf[2] = break_state ? 1 : 0;
889 buf[3] = 0; /* pad */
890 digi_write_inb_command(port, buf, 4, 0);
894 static int digi_tiocmget(struct tty_struct *tty)
896 struct usb_serial_port *port = tty->driver_data;
897 struct digi_port *priv = usb_get_serial_port_data(port);
901 spin_lock_irqsave(&priv->dp_port_lock, flags);
902 val = priv->dp_modem_signals;
903 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
908 static int digi_tiocmset(struct tty_struct *tty,
909 unsigned int set, unsigned int clear)
911 struct usb_serial_port *port = tty->driver_data;
912 struct digi_port *priv = usb_get_serial_port_data(port);
916 spin_lock_irqsave(&priv->dp_port_lock, flags);
917 val = (priv->dp_modem_signals & ~clear) | set;
918 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
919 return digi_set_modem_signals(port, val, 1);
923 static int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
924 const unsigned char *buf, int count)
927 int ret, data_len, new_len;
928 struct digi_port *priv = usb_get_serial_port_data(port);
929 unsigned char *data = port->write_urb->transfer_buffer;
930 unsigned long flags = 0;
932 dbg("digi_write: TOP: port=%d, count=%d, in_interrupt=%ld",
933 priv->dp_port_num, count, in_interrupt());
935 /* copy user data (which can sleep) before getting spin lock */
936 count = min(count, port->bulk_out_size-2);
937 count = min(64, count);
939 /* be sure only one write proceeds at a time */
940 /* there are races on the port private buffer */
941 spin_lock_irqsave(&priv->dp_port_lock, flags);
943 /* wait for urb status clear to submit another urb */
944 if (priv->dp_write_urb_in_use) {
945 /* buffer data if count is 1 (probably put_char) if possible */
946 if (count == 1 && priv->dp_out_buf_len < DIGI_OUT_BUF_SIZE) {
947 priv->dp_out_buf[priv->dp_out_buf_len++] = *buf;
952 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
956 /* allow space for any buffered data and for new data, up to */
957 /* transfer buffer size - 2 (for command and length bytes) */
958 new_len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
959 data_len = new_len + priv->dp_out_buf_len;
962 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
966 port->write_urb->transfer_buffer_length = data_len+2;
968 *data++ = DIGI_CMD_SEND_DATA;
971 /* copy in buffered data first */
972 memcpy(data, priv->dp_out_buf, priv->dp_out_buf_len);
973 data += priv->dp_out_buf_len;
975 /* copy in new data */
976 memcpy(data, buf, new_len);
978 ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
980 priv->dp_write_urb_in_use = 1;
982 priv->dp_out_buf_len = 0;
985 /* return length of new data written, or error */
986 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
988 dev_err_console(port,
989 "%s: usb_submit_urb failed, ret=%d, port=%d\n",
990 __func__, ret, priv->dp_port_num);
991 dbg("digi_write: returning %d", ret);
996 static void digi_write_bulk_callback(struct urb *urb)
999 struct usb_serial_port *port = urb->context;
1000 struct usb_serial *serial;
1001 struct digi_port *priv;
1002 struct digi_serial *serial_priv;
1004 int status = urb->status;
1006 /* port and serial sanity check */
1007 if (port == NULL || (priv = usb_get_serial_port_data(port)) == NULL) {
1008 pr_err("%s: port or port->private is NULL, status=%d\n",
1012 serial = port->serial;
1013 if (serial == NULL || (serial_priv = usb_get_serial_data(serial)) == NULL) {
1015 "%s: serial or serial->private is NULL, status=%d\n",
1020 /* handle oob callback */
1021 if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
1022 dbg("digi_write_bulk_callback: oob callback");
1023 spin_lock(&priv->dp_port_lock);
1024 priv->dp_write_urb_in_use = 0;
1025 wake_up_interruptible(&port->write_wait);
1026 spin_unlock(&priv->dp_port_lock);
1030 /* try to send any buffered data on this port */
1031 spin_lock(&priv->dp_port_lock);
1032 priv->dp_write_urb_in_use = 0;
1033 if (priv->dp_out_buf_len > 0) {
1034 *((unsigned char *)(port->write_urb->transfer_buffer))
1035 = (unsigned char)DIGI_CMD_SEND_DATA;
1036 *((unsigned char *)(port->write_urb->transfer_buffer) + 1)
1037 = (unsigned char)priv->dp_out_buf_len;
1038 port->write_urb->transfer_buffer_length =
1039 priv->dp_out_buf_len + 2;
1040 memcpy(port->write_urb->transfer_buffer + 2, priv->dp_out_buf,
1041 priv->dp_out_buf_len);
1042 ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1044 priv->dp_write_urb_in_use = 1;
1045 priv->dp_out_buf_len = 0;
1048 /* wake up processes sleeping on writes immediately */
1049 digi_wakeup_write(port);
1050 /* also queue up a wakeup at scheduler time, in case we */
1051 /* lost the race in write_chan(). */
1052 schedule_work(&priv->dp_wakeup_work);
1054 spin_unlock(&priv->dp_port_lock);
1055 if (ret && ret != -EPERM)
1056 dev_err_console(port,
1057 "%s: usb_submit_urb failed, ret=%d, port=%d\n",
1058 __func__, ret, priv->dp_port_num);
1061 static int digi_write_room(struct tty_struct *tty)
1063 struct usb_serial_port *port = tty->driver_data;
1064 struct digi_port *priv = usb_get_serial_port_data(port);
1066 unsigned long flags = 0;
1068 spin_lock_irqsave(&priv->dp_port_lock, flags);
1070 if (priv->dp_write_urb_in_use)
1073 room = port->bulk_out_size - 2 - priv->dp_out_buf_len;
1075 spin_unlock_irqrestore(&priv->dp_port_lock, flags);
1076 dbg("digi_write_room: port=%d, room=%d", priv->dp_port_num, room);
1081 static int digi_chars_in_buffer(struct tty_struct *tty)
1083 struct usb_serial_port *port = tty->driver_data;
1084 struct digi_port *priv = usb_get_serial_port_data(port);
1086 if (priv->dp_write_urb_in_use) {
1087 dbg("digi_chars_in_buffer: port=%d, chars=%d",
1088 priv->dp_port_num, port->bulk_out_size - 2);
1089 /* return(port->bulk_out_size - 2); */
1092 dbg("digi_chars_in_buffer: port=%d, chars=%d",
1093 priv->dp_port_num, priv->dp_out_buf_len);
1094 return priv->dp_out_buf_len;
1099 static void digi_dtr_rts(struct usb_serial_port *port, int on)
1101 /* Adjust DTR and RTS */
1102 digi_set_modem_signals(port, on * (TIOCM_DTR|TIOCM_RTS), 1);
1105 static int digi_open(struct tty_struct *tty, struct usb_serial_port *port)
1108 unsigned char buf[32];
1109 struct digi_port *priv = usb_get_serial_port_data(port);
1110 struct ktermios not_termios;
1112 /* be sure the device is started up */
1113 if (digi_startup_device(port->serial) != 0)
1116 /* read modem signals automatically whenever they change */
1117 buf[0] = DIGI_CMD_READ_INPUT_SIGNALS;
1118 buf[1] = priv->dp_port_num;
1119 buf[2] = DIGI_ENABLE;
1123 buf[4] = DIGI_CMD_IFLUSH_FIFO;
1124 buf[5] = priv->dp_port_num;
1125 buf[6] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
1128 ret = digi_write_oob_command(port, buf, 8, 1);
1130 dbg("digi_open: write oob failed, ret=%d", ret);
1132 /* set termios settings */
1134 not_termios.c_cflag = ~tty->termios->c_cflag;
1135 not_termios.c_iflag = ~tty->termios->c_iflag;
1136 digi_set_termios(tty, port, ¬_termios);
1142 static void digi_close(struct usb_serial_port *port)
1146 unsigned char buf[32];
1147 struct digi_port *priv = usb_get_serial_port_data(port);
1149 mutex_lock(&port->serial->disc_mutex);
1150 /* if disconnected, just clear flags */
1151 if (port->serial->disconnected)
1154 if (port->serial->dev) {
1155 /* FIXME: Transmit idle belongs in the wait_unti_sent path */
1156 digi_transmit_idle(port, DIGI_CLOSE_TIMEOUT);
1158 /* disable input flow control */
1159 buf[0] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
1160 buf[1] = priv->dp_port_num;
1161 buf[2] = DIGI_DISABLE;
1164 /* disable output flow control */
1165 buf[4] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
1166 buf[5] = priv->dp_port_num;
1167 buf[6] = DIGI_DISABLE;
1170 /* disable reading modem signals automatically */
1171 buf[8] = DIGI_CMD_READ_INPUT_SIGNALS;
1172 buf[9] = priv->dp_port_num;
1173 buf[10] = DIGI_DISABLE;
1176 /* disable receive */
1177 buf[12] = DIGI_CMD_RECEIVE_ENABLE;
1178 buf[13] = priv->dp_port_num;
1179 buf[14] = DIGI_DISABLE;
1183 buf[16] = DIGI_CMD_IFLUSH_FIFO;
1184 buf[17] = priv->dp_port_num;
1185 buf[18] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
1188 ret = digi_write_oob_command(port, buf, 20, 0);
1190 dbg("digi_close: write oob failed, ret=%d", ret);
1192 /* wait for final commands on oob port to complete */
1193 prepare_to_wait(&priv->dp_flush_wait, &wait,
1194 TASK_INTERRUPTIBLE);
1195 schedule_timeout(DIGI_CLOSE_TIMEOUT);
1196 finish_wait(&priv->dp_flush_wait, &wait);
1198 /* shutdown any outstanding bulk writes */
1199 usb_kill_urb(port->write_urb);
1202 spin_lock_irq(&priv->dp_port_lock);
1203 priv->dp_write_urb_in_use = 0;
1204 wake_up_interruptible(&priv->dp_close_wait);
1205 spin_unlock_irq(&priv->dp_port_lock);
1206 mutex_unlock(&port->serial->disc_mutex);
1211 * Digi Startup Device
1213 * Starts reads on all ports. Must be called AFTER startup, with
1214 * urbs initialized. Returns 0 if successful, non-zero error otherwise.
1217 static int digi_startup_device(struct usb_serial *serial)
1220 struct digi_serial *serial_priv = usb_get_serial_data(serial);
1221 struct usb_serial_port *port;
1223 /* be sure this happens exactly once */
1224 spin_lock(&serial_priv->ds_serial_lock);
1225 if (serial_priv->ds_device_started) {
1226 spin_unlock(&serial_priv->ds_serial_lock);
1229 serial_priv->ds_device_started = 1;
1230 spin_unlock(&serial_priv->ds_serial_lock);
1232 /* start reading from each bulk in endpoint for the device */
1233 /* set USB_DISABLE_SPD flag for write bulk urbs */
1234 for (i = 0; i < serial->type->num_ports + 1; i++) {
1235 port = serial->port[i];
1236 ret = usb_submit_urb(port->read_urb, GFP_KERNEL);
1239 "%s: usb_submit_urb failed, ret=%d, port=%d\n",
1248 static int digi_startup(struct usb_serial *serial)
1252 struct digi_port *priv;
1253 struct digi_serial *serial_priv;
1255 /* allocate the private data structures for all ports */
1256 /* number of regular ports + 1 for the out-of-band port */
1257 for (i = 0; i < serial->type->num_ports + 1; i++) {
1258 /* allocate port private structure */
1259 priv = kmalloc(sizeof(struct digi_port), GFP_KERNEL);
1262 kfree(usb_get_serial_port_data(serial->port[i]));
1263 return 1; /* error */
1266 /* initialize port private structure */
1267 spin_lock_init(&priv->dp_port_lock);
1268 priv->dp_port_num = i;
1269 priv->dp_out_buf_len = 0;
1270 priv->dp_write_urb_in_use = 0;
1271 priv->dp_modem_signals = 0;
1272 init_waitqueue_head(&priv->dp_modem_change_wait);
1273 priv->dp_transmit_idle = 0;
1274 init_waitqueue_head(&priv->dp_transmit_idle_wait);
1275 priv->dp_throttled = 0;
1276 priv->dp_throttle_restart = 0;
1277 init_waitqueue_head(&priv->dp_flush_wait);
1278 init_waitqueue_head(&priv->dp_close_wait);
1279 INIT_WORK(&priv->dp_wakeup_work, digi_wakeup_write_lock);
1280 priv->dp_port = serial->port[i];
1281 /* initialize write wait queue for this port */
1282 init_waitqueue_head(&serial->port[i]->write_wait);
1284 usb_set_serial_port_data(serial->port[i], priv);
1287 /* allocate serial private structure */
1288 serial_priv = kmalloc(sizeof(struct digi_serial), GFP_KERNEL);
1289 if (serial_priv == NULL) {
1290 for (i = 0; i < serial->type->num_ports + 1; i++)
1291 kfree(usb_get_serial_port_data(serial->port[i]));
1292 return 1; /* error */
1295 /* initialize serial private structure */
1296 spin_lock_init(&serial_priv->ds_serial_lock);
1297 serial_priv->ds_oob_port_num = serial->type->num_ports;
1298 serial_priv->ds_oob_port = serial->port[serial_priv->ds_oob_port_num];
1299 serial_priv->ds_device_started = 0;
1300 usb_set_serial_data(serial, serial_priv);
1306 static void digi_disconnect(struct usb_serial *serial)
1310 /* stop reads and writes on all ports */
1311 for (i = 0; i < serial->type->num_ports + 1; i++) {
1312 usb_kill_urb(serial->port[i]->read_urb);
1313 usb_kill_urb(serial->port[i]->write_urb);
1318 static void digi_release(struct usb_serial *serial)
1322 /* free the private data structures for all ports */
1323 /* number of regular ports + 1 for the out-of-band port */
1324 for (i = 0; i < serial->type->num_ports + 1; i++)
1325 kfree(usb_get_serial_port_data(serial->port[i]));
1326 kfree(usb_get_serial_data(serial));
1330 static void digi_read_bulk_callback(struct urb *urb)
1332 struct usb_serial_port *port = urb->context;
1333 struct digi_port *priv;
1334 struct digi_serial *serial_priv;
1336 int status = urb->status;
1338 /* port sanity check, do not resubmit if port is not valid */
1341 priv = usb_get_serial_port_data(port);
1343 dev_err(&port->dev, "%s: port->private is NULL, status=%d\n",
1347 if (port->serial == NULL ||
1348 (serial_priv = usb_get_serial_data(port->serial)) == NULL) {
1349 dev_err(&port->dev, "%s: serial is bad or serial->private "
1350 "is NULL, status=%d\n", __func__, status);
1354 /* do not resubmit urb if it has any status error */
1357 "%s: nonzero read bulk status: status=%d, port=%d\n",
1358 __func__, status, priv->dp_port_num);
1362 /* handle oob or inb callback, do not resubmit if error */
1363 if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
1364 if (digi_read_oob_callback(urb) != 0)
1367 if (digi_read_inb_callback(urb) != 0)
1372 ret = usb_submit_urb(urb, GFP_ATOMIC);
1373 if (ret != 0 && ret != -EPERM) {
1375 "%s: failed resubmitting urb, ret=%d, port=%d\n",
1376 __func__, ret, priv->dp_port_num);
1382 * Digi Read INB Callback
1384 * Digi Read INB Callback handles reads on the in band ports, sending
1385 * the data on to the tty subsystem. When called we know port and
1386 * port->private are not NULL and port->serial has been validated.
1387 * It returns 0 if successful, 1 if successful but the port is
1388 * throttled, and -1 if the sanity checks failed.
1391 static int digi_read_inb_callback(struct urb *urb)
1394 struct usb_serial_port *port = urb->context;
1395 struct tty_struct *tty;
1396 struct digi_port *priv = usb_get_serial_port_data(port);
1397 int opcode = ((unsigned char *)urb->transfer_buffer)[0];
1398 int len = ((unsigned char *)urb->transfer_buffer)[1];
1399 int port_status = ((unsigned char *)urb->transfer_buffer)[2];
1400 unsigned char *data = ((unsigned char *)urb->transfer_buffer) + 3;
1401 int flag, throttled;
1402 int status = urb->status;
1404 /* do not process callbacks on closed ports */
1405 /* but do continue the read chain */
1406 if (urb->status == -ENOENT)
1409 /* short/multiple packet check */
1410 if (urb->actual_length != len + 2) {
1411 dev_err(&port->dev, "%s: INCOMPLETE OR MULTIPLE PACKET, "
1412 "status=%d, port=%d, opcode=%d, len=%d, "
1413 "actual_length=%d, status=%d\n", __func__, status,
1414 priv->dp_port_num, opcode, len, urb->actual_length,
1419 tty = tty_port_tty_get(&port->port);
1420 spin_lock(&priv->dp_port_lock);
1422 /* check for throttle; if set, do not resubmit read urb */
1423 /* indicate the read chain needs to be restarted on unthrottle */
1424 throttled = priv->dp_throttled;
1426 priv->dp_throttle_restart = 1;
1429 if (tty && opcode == DIGI_CMD_RECEIVE_DATA) {
1430 /* get flag from port_status */
1433 /* overrun is special, not associated with a char */
1434 if (port_status & DIGI_OVERRUN_ERROR)
1435 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
1437 /* break takes precedence over parity, */
1438 /* which takes precedence over framing errors */
1439 if (port_status & DIGI_BREAK_ERROR)
1441 else if (port_status & DIGI_PARITY_ERROR)
1443 else if (port_status & DIGI_FRAMING_ERROR)
1446 /* data length is len-1 (one byte of len is port_status) */
1449 tty_insert_flip_string_fixed_flag(tty, data, flag,
1451 tty_flip_buffer_push(tty);
1454 spin_unlock(&priv->dp_port_lock);
1457 if (opcode == DIGI_CMD_RECEIVE_DISABLE)
1458 dbg("%s: got RECEIVE_DISABLE", __func__);
1459 else if (opcode != DIGI_CMD_RECEIVE_DATA)
1460 dbg("%s: unknown opcode: %d", __func__, opcode);
1462 return throttled ? 1 : 0;
1468 * Digi Read OOB Callback
1470 * Digi Read OOB Callback handles reads on the out of band port.
1471 * When called we know port and port->private are not NULL and
1472 * the port->serial is valid. It returns 0 if successful, and
1473 * -1 if the sanity checks failed.
1476 static int digi_read_oob_callback(struct urb *urb)
1479 struct usb_serial_port *port = urb->context;
1480 struct usb_serial *serial = port->serial;
1481 struct tty_struct *tty;
1482 struct digi_port *priv = usb_get_serial_port_data(port);
1483 int opcode, line, status, val;
1487 /* handle each oob command */
1488 for (i = 0; i < urb->actual_length - 3;) {
1489 opcode = ((unsigned char *)urb->transfer_buffer)[i++];
1490 line = ((unsigned char *)urb->transfer_buffer)[i++];
1491 status = ((unsigned char *)urb->transfer_buffer)[i++];
1492 val = ((unsigned char *)urb->transfer_buffer)[i++];
1494 dbg("digi_read_oob_callback: opcode=%d, line=%d, status=%d, val=%d",
1495 opcode, line, status, val);
1497 if (status != 0 || line >= serial->type->num_ports)
1500 port = serial->port[line];
1502 priv = usb_get_serial_port_data(port);
1506 tty = tty_port_tty_get(&port->port);
1510 rts = tty->termios->c_cflag & CRTSCTS;
1512 if (tty && opcode == DIGI_CMD_READ_INPUT_SIGNALS) {
1513 spin_lock(&priv->dp_port_lock);
1514 /* convert from digi flags to termiox flags */
1515 if (val & DIGI_READ_INPUT_SIGNALS_CTS) {
1516 priv->dp_modem_signals |= TIOCM_CTS;
1517 /* port must be open to use tty struct */
1519 tty->hw_stopped = 0;
1520 digi_wakeup_write(port);
1523 priv->dp_modem_signals &= ~TIOCM_CTS;
1524 /* port must be open to use tty struct */
1526 tty->hw_stopped = 1;
1528 if (val & DIGI_READ_INPUT_SIGNALS_DSR)
1529 priv->dp_modem_signals |= TIOCM_DSR;
1531 priv->dp_modem_signals &= ~TIOCM_DSR;
1532 if (val & DIGI_READ_INPUT_SIGNALS_RI)
1533 priv->dp_modem_signals |= TIOCM_RI;
1535 priv->dp_modem_signals &= ~TIOCM_RI;
1536 if (val & DIGI_READ_INPUT_SIGNALS_DCD)
1537 priv->dp_modem_signals |= TIOCM_CD;
1539 priv->dp_modem_signals &= ~TIOCM_CD;
1541 wake_up_interruptible(&priv->dp_modem_change_wait);
1542 spin_unlock(&priv->dp_port_lock);
1543 } else if (opcode == DIGI_CMD_TRANSMIT_IDLE) {
1544 spin_lock(&priv->dp_port_lock);
1545 priv->dp_transmit_idle = 1;
1546 wake_up_interruptible(&priv->dp_transmit_idle_wait);
1547 spin_unlock(&priv->dp_port_lock);
1548 } else if (opcode == DIGI_CMD_IFLUSH_FIFO) {
1549 wake_up_interruptible(&priv->dp_flush_wait);
1557 module_usb_serial_driver(digi_driver, serial_drivers);
1559 MODULE_AUTHOR(DRIVER_AUTHOR);
1560 MODULE_DESCRIPTION(DRIVER_DESC);
1561 MODULE_LICENSE("GPL");
1563 module_param(debug, bool, S_IRUGO | S_IWUSR);
1564 MODULE_PARM_DESC(debug, "Debug enabled or not");