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
);
252 static const struct usb_device_id id_table_combined
[] = {
253 { USB_DEVICE(DIGI_VENDOR_ID
, DIGI_2_ID
) },
254 { USB_DEVICE(DIGI_VENDOR_ID
, DIGI_4_ID
) },
255 { } /* Terminating entry */
258 static const struct usb_device_id id_table_2
[] = {
259 { USB_DEVICE(DIGI_VENDOR_ID
, DIGI_2_ID
) },
260 { } /* Terminating entry */
263 static const struct usb_device_id id_table_4
[] = {
264 { USB_DEVICE(DIGI_VENDOR_ID
, DIGI_4_ID
) },
265 { } /* Terminating entry */
268 MODULE_DEVICE_TABLE(usb
, id_table_combined
);
270 /* device info needed for the Digi serial converter */
272 static struct usb_serial_driver digi_acceleport_2_device
= {
274 .owner
= THIS_MODULE
,
277 .description
= "Digi 2 port USB adapter",
278 .id_table
= id_table_2
,
282 .dtr_rts
= digi_dtr_rts
,
284 .write_room
= digi_write_room
,
285 .write_bulk_callback
= digi_write_bulk_callback
,
286 .read_bulk_callback
= digi_read_bulk_callback
,
287 .chars_in_buffer
= digi_chars_in_buffer
,
288 .throttle
= digi_rx_throttle
,
289 .unthrottle
= digi_rx_unthrottle
,
290 .set_termios
= digi_set_termios
,
291 .break_ctl
= digi_break_ctl
,
292 .tiocmget
= digi_tiocmget
,
293 .tiocmset
= digi_tiocmset
,
294 .attach
= digi_startup
,
295 .disconnect
= digi_disconnect
,
296 .release
= digi_release
,
299 static struct usb_serial_driver digi_acceleport_4_device
= {
301 .owner
= THIS_MODULE
,
304 .description
= "Digi 4 port USB adapter",
305 .id_table
= id_table_4
,
310 .write_room
= digi_write_room
,
311 .write_bulk_callback
= digi_write_bulk_callback
,
312 .read_bulk_callback
= digi_read_bulk_callback
,
313 .chars_in_buffer
= digi_chars_in_buffer
,
314 .throttle
= digi_rx_throttle
,
315 .unthrottle
= digi_rx_unthrottle
,
316 .set_termios
= digi_set_termios
,
317 .break_ctl
= digi_break_ctl
,
318 .tiocmget
= digi_tiocmget
,
319 .tiocmset
= digi_tiocmset
,
320 .attach
= digi_startup
,
321 .disconnect
= digi_disconnect
,
322 .release
= digi_release
,
325 static struct usb_serial_driver
* const serial_drivers
[] = {
326 &digi_acceleport_2_device
, &digi_acceleport_4_device
, NULL
332 * Cond Wait Interruptible Timeout Irqrestore
334 * Do spin_unlock_irqrestore and interruptible_sleep_on_timeout
335 * so that wake ups are not lost if they occur between the unlock
336 * and the sleep. In other words, spin_unlock_irqrestore and
337 * interruptible_sleep_on_timeout are "atomic" with respect to
338 * wake ups. This is used to implement condition variables.
340 * interruptible_sleep_on_timeout is deprecated and has been replaced
341 * with the equivalent code.
344 static long cond_wait_interruptible_timeout_irqrestore(
345 wait_queue_head_t
*q
, long timeout
,
346 spinlock_t
*lock
, unsigned long flags
)
351 prepare_to_wait(q
, &wait
, TASK_INTERRUPTIBLE
);
352 spin_unlock_irqrestore(lock
, flags
);
353 timeout
= schedule_timeout(timeout
);
354 finish_wait(q
, &wait
);
363 * Wake up port, line discipline, and tty processes sleeping
367 static void digi_wakeup_write_lock(struct work_struct
*work
)
369 struct digi_port
*priv
=
370 container_of(work
, struct digi_port
, dp_wakeup_work
);
371 struct usb_serial_port
*port
= priv
->dp_port
;
374 spin_lock_irqsave(&priv
->dp_port_lock
, flags
);
375 digi_wakeup_write(port
);
376 spin_unlock_irqrestore(&priv
->dp_port_lock
, flags
);
379 static void digi_wakeup_write(struct usb_serial_port
*port
)
381 struct tty_struct
*tty
= tty_port_tty_get(&port
->port
);
390 * Digi Write OOB Command
392 * Write commands on the out of band port. Commands are 4
393 * bytes each, multiple commands can be sent at once, and
394 * no command will be split across USB packets. Returns 0
395 * if successful, -EINTR if interrupted while sleeping and
396 * the interruptible flag is true, or a negative error
397 * returned by usb_submit_urb.
400 static int digi_write_oob_command(struct usb_serial_port
*port
,
401 unsigned char *buf
, int count
, int interruptible
)
405 struct usb_serial_port
*oob_port
= (struct usb_serial_port
*)((struct digi_serial
*)(usb_get_serial_data(port
->serial
)))->ds_oob_port
;
406 struct digi_port
*oob_priv
= usb_get_serial_port_data(oob_port
);
407 unsigned long flags
= 0;
410 "digi_write_oob_command: TOP: port=%d, count=%d\n",
411 oob_priv
->dp_port_num
, count
);
413 spin_lock_irqsave(&oob_priv
->dp_port_lock
, flags
);
415 while (oob_priv
->dp_write_urb_in_use
) {
416 cond_wait_interruptible_timeout_irqrestore(
417 &oob_port
->write_wait
, DIGI_RETRY_TIMEOUT
,
418 &oob_priv
->dp_port_lock
, flags
);
419 if (interruptible
&& signal_pending(current
))
421 spin_lock_irqsave(&oob_priv
->dp_port_lock
, flags
);
424 /* len must be a multiple of 4, so commands are not split */
425 len
= min(count
, oob_port
->bulk_out_size
);
428 memcpy(oob_port
->write_urb
->transfer_buffer
, buf
, len
);
429 oob_port
->write_urb
->transfer_buffer_length
= len
;
430 ret
= usb_submit_urb(oob_port
->write_urb
, GFP_ATOMIC
);
432 oob_priv
->dp_write_urb_in_use
= 1;
437 spin_unlock_irqrestore(&oob_priv
->dp_port_lock
, flags
);
439 dev_err(&port
->dev
, "%s: usb_submit_urb failed, ret=%d\n",
447 * Digi Write In Band Command
449 * Write commands on the given port. Commands are 4
450 * bytes each, multiple commands can be sent at once, and
451 * no command will be split across USB packets. If timeout
452 * is non-zero, write in band command will return after
453 * waiting unsuccessfully for the URB status to clear for
454 * timeout ticks. Returns 0 if successful, or a negative
455 * error returned by digi_write.
458 static int digi_write_inb_command(struct usb_serial_port
*port
,
459 unsigned char *buf
, int count
, unsigned long timeout
)
463 struct digi_port
*priv
= usb_get_serial_port_data(port
);
464 unsigned char *data
= port
->write_urb
->transfer_buffer
;
465 unsigned long flags
= 0;
467 dev_dbg(&port
->dev
, "digi_write_inb_command: TOP: port=%d, count=%d\n",
468 priv
->dp_port_num
, count
);
475 spin_lock_irqsave(&priv
->dp_port_lock
, flags
);
476 while (count
> 0 && ret
== 0) {
477 while (priv
->dp_write_urb_in_use
&&
478 time_before(jiffies
, timeout
)) {
479 cond_wait_interruptible_timeout_irqrestore(
480 &port
->write_wait
, DIGI_RETRY_TIMEOUT
,
481 &priv
->dp_port_lock
, flags
);
482 if (signal_pending(current
))
484 spin_lock_irqsave(&priv
->dp_port_lock
, flags
);
487 /* len must be a multiple of 4 and small enough to */
488 /* guarantee the write will send buffered data first, */
489 /* so commands are in order with data and not split */
490 len
= min(count
, port
->bulk_out_size
-2-priv
->dp_out_buf_len
);
494 /* write any buffered data first */
495 if (priv
->dp_out_buf_len
> 0) {
496 data
[0] = DIGI_CMD_SEND_DATA
;
497 data
[1] = priv
->dp_out_buf_len
;
498 memcpy(data
+ 2, priv
->dp_out_buf
,
499 priv
->dp_out_buf_len
);
500 memcpy(data
+ 2 + priv
->dp_out_buf_len
, buf
, len
);
501 port
->write_urb
->transfer_buffer_length
502 = priv
->dp_out_buf_len
+ 2 + len
;
504 memcpy(data
, buf
, len
);
505 port
->write_urb
->transfer_buffer_length
= len
;
508 ret
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
510 priv
->dp_write_urb_in_use
= 1;
511 priv
->dp_out_buf_len
= 0;
517 spin_unlock_irqrestore(&priv
->dp_port_lock
, flags
);
521 "%s: usb_submit_urb failed, ret=%d, port=%d\n",
522 __func__
, ret
, priv
->dp_port_num
);
528 * Digi Set Modem Signals
530 * Sets or clears DTR and RTS on the port, according to the
531 * modem_signals argument. Use TIOCM_DTR and TIOCM_RTS flags
532 * for the modem_signals argument. Returns 0 if successful,
533 * -EINTR if interrupted while sleeping, or a non-zero error
534 * returned by usb_submit_urb.
537 static int digi_set_modem_signals(struct usb_serial_port
*port
,
538 unsigned int modem_signals
, int interruptible
)
542 struct digi_port
*port_priv
= usb_get_serial_port_data(port
);
543 struct usb_serial_port
*oob_port
= (struct usb_serial_port
*) ((struct digi_serial
*)(usb_get_serial_data(port
->serial
)))->ds_oob_port
;
544 struct digi_port
*oob_priv
= usb_get_serial_port_data(oob_port
);
545 unsigned char *data
= oob_port
->write_urb
->transfer_buffer
;
546 unsigned long flags
= 0;
550 "digi_set_modem_signals: TOP: port=%d, modem_signals=0x%x\n",
551 port_priv
->dp_port_num
, modem_signals
);
553 spin_lock_irqsave(&oob_priv
->dp_port_lock
, flags
);
554 spin_lock(&port_priv
->dp_port_lock
);
556 while (oob_priv
->dp_write_urb_in_use
) {
557 spin_unlock(&port_priv
->dp_port_lock
);
558 cond_wait_interruptible_timeout_irqrestore(
559 &oob_port
->write_wait
, DIGI_RETRY_TIMEOUT
,
560 &oob_priv
->dp_port_lock
, flags
);
561 if (interruptible
&& signal_pending(current
))
563 spin_lock_irqsave(&oob_priv
->dp_port_lock
, flags
);
564 spin_lock(&port_priv
->dp_port_lock
);
566 data
[0] = DIGI_CMD_SET_DTR_SIGNAL
;
567 data
[1] = port_priv
->dp_port_num
;
568 data
[2] = (modem_signals
& TIOCM_DTR
) ?
569 DIGI_DTR_ACTIVE
: DIGI_DTR_INACTIVE
;
571 data
[4] = DIGI_CMD_SET_RTS_SIGNAL
;
572 data
[5] = port_priv
->dp_port_num
;
573 data
[6] = (modem_signals
& TIOCM_RTS
) ?
574 DIGI_RTS_ACTIVE
: DIGI_RTS_INACTIVE
;
577 oob_port
->write_urb
->transfer_buffer_length
= 8;
579 ret
= usb_submit_urb(oob_port
->write_urb
, GFP_ATOMIC
);
581 oob_priv
->dp_write_urb_in_use
= 1;
582 port_priv
->dp_modem_signals
=
583 (port_priv
->dp_modem_signals
&~(TIOCM_DTR
|TIOCM_RTS
))
584 | (modem_signals
&(TIOCM_DTR
|TIOCM_RTS
));
586 spin_unlock(&port_priv
->dp_port_lock
);
587 spin_unlock_irqrestore(&oob_priv
->dp_port_lock
, flags
);
589 dev_err(&port
->dev
, "%s: usb_submit_urb failed, ret=%d\n",
597 * Digi transmit idle waits, up to timeout ticks, for the transmitter
598 * to go idle. It returns 0 if successful or a negative error.
600 * There are race conditions here if more than one process is calling
601 * digi_transmit_idle on the same port at the same time. However, this
602 * is only called from close, and only one process can be in close on a
603 * port at a time, so its ok.
606 static int digi_transmit_idle(struct usb_serial_port
*port
,
607 unsigned long timeout
)
610 unsigned char buf
[2];
611 struct digi_port
*priv
= usb_get_serial_port_data(port
);
612 unsigned long flags
= 0;
614 spin_lock_irqsave(&priv
->dp_port_lock
, flags
);
615 priv
->dp_transmit_idle
= 0;
616 spin_unlock_irqrestore(&priv
->dp_port_lock
, flags
);
618 buf
[0] = DIGI_CMD_TRANSMIT_IDLE
;
623 ret
= digi_write_inb_command(port
, buf
, 2, timeout
- jiffies
);
627 spin_lock_irqsave(&priv
->dp_port_lock
, flags
);
629 while (time_before(jiffies
, timeout
) && !priv
->dp_transmit_idle
) {
630 cond_wait_interruptible_timeout_irqrestore(
631 &priv
->dp_transmit_idle_wait
, DIGI_RETRY_TIMEOUT
,
632 &priv
->dp_port_lock
, flags
);
633 if (signal_pending(current
))
635 spin_lock_irqsave(&priv
->dp_port_lock
, flags
);
637 priv
->dp_transmit_idle
= 0;
638 spin_unlock_irqrestore(&priv
->dp_port_lock
, flags
);
644 static void digi_rx_throttle(struct tty_struct
*tty
)
647 struct usb_serial_port
*port
= tty
->driver_data
;
648 struct digi_port
*priv
= usb_get_serial_port_data(port
);
650 /* stop receiving characters by not resubmitting the read urb */
651 spin_lock_irqsave(&priv
->dp_port_lock
, flags
);
652 priv
->dp_throttled
= 1;
653 priv
->dp_throttle_restart
= 0;
654 spin_unlock_irqrestore(&priv
->dp_port_lock
, flags
);
658 static void digi_rx_unthrottle(struct tty_struct
*tty
)
662 struct usb_serial_port
*port
= tty
->driver_data
;
663 struct digi_port
*priv
= usb_get_serial_port_data(port
);
665 spin_lock_irqsave(&priv
->dp_port_lock
, flags
);
667 /* restart read chain */
668 if (priv
->dp_throttle_restart
)
669 ret
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
671 /* turn throttle off */
672 priv
->dp_throttled
= 0;
673 priv
->dp_throttle_restart
= 0;
675 spin_unlock_irqrestore(&priv
->dp_port_lock
, flags
);
679 "%s: usb_submit_urb failed, ret=%d, port=%d\n",
680 __func__
, ret
, priv
->dp_port_num
);
684 static void digi_set_termios(struct tty_struct
*tty
,
685 struct usb_serial_port
*port
, struct ktermios
*old_termios
)
687 struct digi_port
*priv
= usb_get_serial_port_data(port
);
688 struct device
*dev
= &port
->dev
;
689 unsigned int iflag
= tty
->termios
.c_iflag
;
690 unsigned int cflag
= tty
->termios
.c_cflag
;
691 unsigned int old_iflag
= old_termios
->c_iflag
;
692 unsigned int old_cflag
= old_termios
->c_cflag
;
693 unsigned char buf
[32];
694 unsigned int modem_signals
;
700 "digi_set_termios: TOP: port=%d, iflag=0x%x, old_iflag=0x%x, cflag=0x%x, old_cflag=0x%x\n",
701 priv
->dp_port_num
, iflag
, old_iflag
, cflag
, old_cflag
);
704 baud
= tty_get_baud_rate(tty
);
705 if (baud
!= tty_termios_baud_rate(old_termios
)) {
708 /* reassert DTR and (maybe) RTS on transition from B0 */
709 if ((old_cflag
&CBAUD
) == B0
) {
710 /* don't set RTS if using hardware flow control */
711 /* and throttling input */
712 modem_signals
= TIOCM_DTR
;
713 if (!(tty
->termios
.c_cflag
& CRTSCTS
) ||
714 !test_bit(TTY_THROTTLED
, &tty
->flags
))
715 modem_signals
|= TIOCM_RTS
;
716 digi_set_modem_signals(port
, modem_signals
, 1);
719 /* drop DTR and RTS on transition to B0 */
720 case 0: digi_set_modem_signals(port
, 0, 1); break;
721 case 50: arg
= DIGI_BAUD_50
; break;
722 case 75: arg
= DIGI_BAUD_75
; break;
723 case 110: arg
= DIGI_BAUD_110
; break;
724 case 150: arg
= DIGI_BAUD_150
; break;
725 case 200: arg
= DIGI_BAUD_200
; break;
726 case 300: arg
= DIGI_BAUD_300
; break;
727 case 600: arg
= DIGI_BAUD_600
; break;
728 case 1200: arg
= DIGI_BAUD_1200
; break;
729 case 1800: arg
= DIGI_BAUD_1800
; break;
730 case 2400: arg
= DIGI_BAUD_2400
; break;
731 case 4800: arg
= DIGI_BAUD_4800
; break;
732 case 9600: arg
= DIGI_BAUD_9600
; break;
733 case 19200: arg
= DIGI_BAUD_19200
; break;
734 case 38400: arg
= DIGI_BAUD_38400
; break;
735 case 57600: arg
= DIGI_BAUD_57600
; break;
736 case 115200: arg
= DIGI_BAUD_115200
; break;
737 case 230400: arg
= DIGI_BAUD_230400
; break;
738 case 460800: arg
= DIGI_BAUD_460800
; break;
740 arg
= DIGI_BAUD_9600
;
745 buf
[i
++] = DIGI_CMD_SET_BAUD_RATE
;
746 buf
[i
++] = priv
->dp_port_num
;
752 tty
->termios
.c_cflag
&= ~CMSPAR
;
754 if ((cflag
&(PARENB
|PARODD
)) != (old_cflag
&(PARENB
|PARODD
))) {
757 arg
= DIGI_PARITY_ODD
;
759 arg
= DIGI_PARITY_EVEN
;
761 arg
= DIGI_PARITY_NONE
;
763 buf
[i
++] = DIGI_CMD_SET_PARITY
;
764 buf
[i
++] = priv
->dp_port_num
;
769 if ((cflag
&CSIZE
) != (old_cflag
&CSIZE
)) {
771 switch (cflag
&CSIZE
) {
772 case CS5
: arg
= DIGI_WORD_SIZE_5
; break;
773 case CS6
: arg
= DIGI_WORD_SIZE_6
; break;
774 case CS7
: arg
= DIGI_WORD_SIZE_7
; break;
775 case CS8
: arg
= DIGI_WORD_SIZE_8
; break;
778 "digi_set_termios: can't handle word size %d\n",
784 buf
[i
++] = DIGI_CMD_SET_WORD_SIZE
;
785 buf
[i
++] = priv
->dp_port_num
;
793 if ((cflag
&CSTOPB
) != (old_cflag
&CSTOPB
)) {
796 arg
= DIGI_STOP_BITS_2
;
798 arg
= DIGI_STOP_BITS_1
;
800 buf
[i
++] = DIGI_CMD_SET_STOP_BITS
;
801 buf
[i
++] = priv
->dp_port_num
;
807 /* set input flow control */
808 if ((iflag
&IXOFF
) != (old_iflag
&IXOFF
)
809 || (cflag
&CRTSCTS
) != (old_cflag
&CRTSCTS
)) {
812 arg
|= DIGI_INPUT_FLOW_CONTROL_XON_XOFF
;
814 arg
&= ~DIGI_INPUT_FLOW_CONTROL_XON_XOFF
;
817 arg
|= DIGI_INPUT_FLOW_CONTROL_RTS
;
819 /* On USB-4 it is necessary to assert RTS prior */
820 /* to selecting RTS input flow control. */
821 buf
[i
++] = DIGI_CMD_SET_RTS_SIGNAL
;
822 buf
[i
++] = priv
->dp_port_num
;
823 buf
[i
++] = DIGI_RTS_ACTIVE
;
827 arg
&= ~DIGI_INPUT_FLOW_CONTROL_RTS
;
829 buf
[i
++] = DIGI_CMD_SET_INPUT_FLOW_CONTROL
;
830 buf
[i
++] = priv
->dp_port_num
;
835 /* set output flow control */
836 if ((iflag
& IXON
) != (old_iflag
& IXON
)
837 || (cflag
& CRTSCTS
) != (old_cflag
& CRTSCTS
)) {
840 arg
|= DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF
;
842 arg
&= ~DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF
;
844 if (cflag
& CRTSCTS
) {
845 arg
|= DIGI_OUTPUT_FLOW_CONTROL_CTS
;
847 arg
&= ~DIGI_OUTPUT_FLOW_CONTROL_CTS
;
851 buf
[i
++] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL
;
852 buf
[i
++] = priv
->dp_port_num
;
857 /* set receive enable/disable */
858 if ((cflag
& CREAD
) != (old_cflag
& CREAD
)) {
864 buf
[i
++] = DIGI_CMD_RECEIVE_ENABLE
;
865 buf
[i
++] = priv
->dp_port_num
;
869 ret
= digi_write_oob_command(port
, buf
, i
, 1);
871 dev_dbg(dev
, "digi_set_termios: write oob failed, ret=%d\n", ret
);
872 tty_encode_baud_rate(tty
, baud
, baud
);
876 static void digi_break_ctl(struct tty_struct
*tty
, int break_state
)
878 struct usb_serial_port
*port
= tty
->driver_data
;
879 unsigned char buf
[4];
881 buf
[0] = DIGI_CMD_BREAK_CONTROL
;
882 buf
[1] = 2; /* length */
883 buf
[2] = break_state
? 1 : 0;
884 buf
[3] = 0; /* pad */
885 digi_write_inb_command(port
, buf
, 4, 0);
889 static int digi_tiocmget(struct tty_struct
*tty
)
891 struct usb_serial_port
*port
= tty
->driver_data
;
892 struct digi_port
*priv
= usb_get_serial_port_data(port
);
896 spin_lock_irqsave(&priv
->dp_port_lock
, flags
);
897 val
= priv
->dp_modem_signals
;
898 spin_unlock_irqrestore(&priv
->dp_port_lock
, flags
);
903 static int digi_tiocmset(struct tty_struct
*tty
,
904 unsigned int set
, unsigned int clear
)
906 struct usb_serial_port
*port
= tty
->driver_data
;
907 struct digi_port
*priv
= usb_get_serial_port_data(port
);
911 spin_lock_irqsave(&priv
->dp_port_lock
, flags
);
912 val
= (priv
->dp_modem_signals
& ~clear
) | set
;
913 spin_unlock_irqrestore(&priv
->dp_port_lock
, flags
);
914 return digi_set_modem_signals(port
, val
, 1);
918 static int digi_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
919 const unsigned char *buf
, int count
)
922 int ret
, data_len
, new_len
;
923 struct digi_port
*priv
= usb_get_serial_port_data(port
);
924 unsigned char *data
= port
->write_urb
->transfer_buffer
;
925 unsigned long flags
= 0;
928 "digi_write: TOP: port=%d, count=%d, in_interrupt=%ld\n",
929 priv
->dp_port_num
, count
, in_interrupt());
931 /* copy user data (which can sleep) before getting spin lock */
932 count
= min(count
, port
->bulk_out_size
-2);
933 count
= min(64, count
);
935 /* be sure only one write proceeds at a time */
936 /* there are races on the port private buffer */
937 spin_lock_irqsave(&priv
->dp_port_lock
, flags
);
939 /* wait for urb status clear to submit another urb */
940 if (priv
->dp_write_urb_in_use
) {
941 /* buffer data if count is 1 (probably put_char) if possible */
942 if (count
== 1 && priv
->dp_out_buf_len
< DIGI_OUT_BUF_SIZE
) {
943 priv
->dp_out_buf
[priv
->dp_out_buf_len
++] = *buf
;
948 spin_unlock_irqrestore(&priv
->dp_port_lock
, flags
);
952 /* allow space for any buffered data and for new data, up to */
953 /* transfer buffer size - 2 (for command and length bytes) */
954 new_len
= min(count
, port
->bulk_out_size
-2-priv
->dp_out_buf_len
);
955 data_len
= new_len
+ priv
->dp_out_buf_len
;
958 spin_unlock_irqrestore(&priv
->dp_port_lock
, flags
);
962 port
->write_urb
->transfer_buffer_length
= data_len
+2;
964 *data
++ = DIGI_CMD_SEND_DATA
;
967 /* copy in buffered data first */
968 memcpy(data
, priv
->dp_out_buf
, priv
->dp_out_buf_len
);
969 data
+= priv
->dp_out_buf_len
;
971 /* copy in new data */
972 memcpy(data
, buf
, new_len
);
974 ret
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
976 priv
->dp_write_urb_in_use
= 1;
978 priv
->dp_out_buf_len
= 0;
981 /* return length of new data written, or error */
982 spin_unlock_irqrestore(&priv
->dp_port_lock
, flags
);
984 dev_err_console(port
,
985 "%s: usb_submit_urb failed, ret=%d, port=%d\n",
986 __func__
, ret
, priv
->dp_port_num
);
987 dev_dbg(&port
->dev
, "digi_write: returning %d\n", ret
);
992 static void digi_write_bulk_callback(struct urb
*urb
)
995 struct usb_serial_port
*port
= urb
->context
;
996 struct usb_serial
*serial
;
997 struct digi_port
*priv
;
998 struct digi_serial
*serial_priv
;
1000 int status
= urb
->status
;
1002 /* port and serial sanity check */
1003 if (port
== NULL
|| (priv
= usb_get_serial_port_data(port
)) == NULL
) {
1004 pr_err("%s: port or port->private is NULL, status=%d\n",
1008 serial
= port
->serial
;
1009 if (serial
== NULL
|| (serial_priv
= usb_get_serial_data(serial
)) == NULL
) {
1011 "%s: serial or serial->private is NULL, status=%d\n",
1016 /* handle oob callback */
1017 if (priv
->dp_port_num
== serial_priv
->ds_oob_port_num
) {
1018 dev_dbg(&port
->dev
, "digi_write_bulk_callback: oob callback\n");
1019 spin_lock(&priv
->dp_port_lock
);
1020 priv
->dp_write_urb_in_use
= 0;
1021 wake_up_interruptible(&port
->write_wait
);
1022 spin_unlock(&priv
->dp_port_lock
);
1026 /* try to send any buffered data on this port */
1027 spin_lock(&priv
->dp_port_lock
);
1028 priv
->dp_write_urb_in_use
= 0;
1029 if (priv
->dp_out_buf_len
> 0) {
1030 *((unsigned char *)(port
->write_urb
->transfer_buffer
))
1031 = (unsigned char)DIGI_CMD_SEND_DATA
;
1032 *((unsigned char *)(port
->write_urb
->transfer_buffer
) + 1)
1033 = (unsigned char)priv
->dp_out_buf_len
;
1034 port
->write_urb
->transfer_buffer_length
=
1035 priv
->dp_out_buf_len
+ 2;
1036 memcpy(port
->write_urb
->transfer_buffer
+ 2, priv
->dp_out_buf
,
1037 priv
->dp_out_buf_len
);
1038 ret
= usb_submit_urb(port
->write_urb
, GFP_ATOMIC
);
1040 priv
->dp_write_urb_in_use
= 1;
1041 priv
->dp_out_buf_len
= 0;
1044 /* wake up processes sleeping on writes immediately */
1045 digi_wakeup_write(port
);
1046 /* also queue up a wakeup at scheduler time, in case we */
1047 /* lost the race in write_chan(). */
1048 schedule_work(&priv
->dp_wakeup_work
);
1050 spin_unlock(&priv
->dp_port_lock
);
1051 if (ret
&& ret
!= -EPERM
)
1052 dev_err_console(port
,
1053 "%s: usb_submit_urb failed, ret=%d, port=%d\n",
1054 __func__
, ret
, priv
->dp_port_num
);
1057 static int digi_write_room(struct tty_struct
*tty
)
1059 struct usb_serial_port
*port
= tty
->driver_data
;
1060 struct digi_port
*priv
= usb_get_serial_port_data(port
);
1062 unsigned long flags
= 0;
1064 spin_lock_irqsave(&priv
->dp_port_lock
, flags
);
1066 if (priv
->dp_write_urb_in_use
)
1069 room
= port
->bulk_out_size
- 2 - priv
->dp_out_buf_len
;
1071 spin_unlock_irqrestore(&priv
->dp_port_lock
, flags
);
1072 dev_dbg(&port
->dev
, "digi_write_room: port=%d, room=%d\n", priv
->dp_port_num
, room
);
1077 static int digi_chars_in_buffer(struct tty_struct
*tty
)
1079 struct usb_serial_port
*port
= tty
->driver_data
;
1080 struct digi_port
*priv
= usb_get_serial_port_data(port
);
1082 if (priv
->dp_write_urb_in_use
) {
1083 dev_dbg(&port
->dev
, "digi_chars_in_buffer: port=%d, chars=%d\n",
1084 priv
->dp_port_num
, port
->bulk_out_size
- 2);
1085 /* return(port->bulk_out_size - 2); */
1088 dev_dbg(&port
->dev
, "digi_chars_in_buffer: port=%d, chars=%d\n",
1089 priv
->dp_port_num
, priv
->dp_out_buf_len
);
1090 return priv
->dp_out_buf_len
;
1095 static void digi_dtr_rts(struct usb_serial_port
*port
, int on
)
1097 /* Adjust DTR and RTS */
1098 digi_set_modem_signals(port
, on
* (TIOCM_DTR
|TIOCM_RTS
), 1);
1101 static int digi_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
1104 unsigned char buf
[32];
1105 struct digi_port
*priv
= usb_get_serial_port_data(port
);
1106 struct ktermios not_termios
;
1108 /* be sure the device is started up */
1109 if (digi_startup_device(port
->serial
) != 0)
1112 /* read modem signals automatically whenever they change */
1113 buf
[0] = DIGI_CMD_READ_INPUT_SIGNALS
;
1114 buf
[1] = priv
->dp_port_num
;
1115 buf
[2] = DIGI_ENABLE
;
1119 buf
[4] = DIGI_CMD_IFLUSH_FIFO
;
1120 buf
[5] = priv
->dp_port_num
;
1121 buf
[6] = DIGI_FLUSH_TX
| DIGI_FLUSH_RX
;
1124 ret
= digi_write_oob_command(port
, buf
, 8, 1);
1126 dev_dbg(&port
->dev
, "digi_open: write oob failed, ret=%d\n", ret
);
1128 /* set termios settings */
1130 not_termios
.c_cflag
= ~tty
->termios
.c_cflag
;
1131 not_termios
.c_iflag
= ~tty
->termios
.c_iflag
;
1132 digi_set_termios(tty
, port
, ¬_termios
);
1138 static void digi_close(struct usb_serial_port
*port
)
1142 unsigned char buf
[32];
1143 struct digi_port
*priv
= usb_get_serial_port_data(port
);
1145 mutex_lock(&port
->serial
->disc_mutex
);
1146 /* if disconnected, just clear flags */
1147 if (port
->serial
->disconnected
)
1150 if (port
->serial
->dev
) {
1151 /* FIXME: Transmit idle belongs in the wait_unti_sent path */
1152 digi_transmit_idle(port
, DIGI_CLOSE_TIMEOUT
);
1154 /* disable input flow control */
1155 buf
[0] = DIGI_CMD_SET_INPUT_FLOW_CONTROL
;
1156 buf
[1] = priv
->dp_port_num
;
1157 buf
[2] = DIGI_DISABLE
;
1160 /* disable output flow control */
1161 buf
[4] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL
;
1162 buf
[5] = priv
->dp_port_num
;
1163 buf
[6] = DIGI_DISABLE
;
1166 /* disable reading modem signals automatically */
1167 buf
[8] = DIGI_CMD_READ_INPUT_SIGNALS
;
1168 buf
[9] = priv
->dp_port_num
;
1169 buf
[10] = DIGI_DISABLE
;
1172 /* disable receive */
1173 buf
[12] = DIGI_CMD_RECEIVE_ENABLE
;
1174 buf
[13] = priv
->dp_port_num
;
1175 buf
[14] = DIGI_DISABLE
;
1179 buf
[16] = DIGI_CMD_IFLUSH_FIFO
;
1180 buf
[17] = priv
->dp_port_num
;
1181 buf
[18] = DIGI_FLUSH_TX
| DIGI_FLUSH_RX
;
1184 ret
= digi_write_oob_command(port
, buf
, 20, 0);
1186 dev_dbg(&port
->dev
, "digi_close: write oob failed, ret=%d\n", ret
);
1188 /* wait for final commands on oob port to complete */
1189 prepare_to_wait(&priv
->dp_flush_wait
, &wait
,
1190 TASK_INTERRUPTIBLE
);
1191 schedule_timeout(DIGI_CLOSE_TIMEOUT
);
1192 finish_wait(&priv
->dp_flush_wait
, &wait
);
1194 /* shutdown any outstanding bulk writes */
1195 usb_kill_urb(port
->write_urb
);
1198 spin_lock_irq(&priv
->dp_port_lock
);
1199 priv
->dp_write_urb_in_use
= 0;
1200 wake_up_interruptible(&priv
->dp_close_wait
);
1201 spin_unlock_irq(&priv
->dp_port_lock
);
1202 mutex_unlock(&port
->serial
->disc_mutex
);
1207 * Digi Startup Device
1209 * Starts reads on all ports. Must be called AFTER startup, with
1210 * urbs initialized. Returns 0 if successful, non-zero error otherwise.
1213 static int digi_startup_device(struct usb_serial
*serial
)
1216 struct digi_serial
*serial_priv
= usb_get_serial_data(serial
);
1217 struct usb_serial_port
*port
;
1219 /* be sure this happens exactly once */
1220 spin_lock(&serial_priv
->ds_serial_lock
);
1221 if (serial_priv
->ds_device_started
) {
1222 spin_unlock(&serial_priv
->ds_serial_lock
);
1225 serial_priv
->ds_device_started
= 1;
1226 spin_unlock(&serial_priv
->ds_serial_lock
);
1228 /* start reading from each bulk in endpoint for the device */
1229 /* set USB_DISABLE_SPD flag for write bulk urbs */
1230 for (i
= 0; i
< serial
->type
->num_ports
+ 1; i
++) {
1231 port
= serial
->port
[i
];
1232 ret
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
1235 "%s: usb_submit_urb failed, ret=%d, port=%d\n",
1244 static int digi_startup(struct usb_serial
*serial
)
1248 struct digi_port
*priv
;
1249 struct digi_serial
*serial_priv
;
1251 /* allocate the private data structures for all ports */
1252 /* number of regular ports + 1 for the out-of-band port */
1253 for (i
= 0; i
< serial
->type
->num_ports
+ 1; i
++) {
1254 /* allocate port private structure */
1255 priv
= kmalloc(sizeof(struct digi_port
), GFP_KERNEL
);
1258 kfree(usb_get_serial_port_data(serial
->port
[i
]));
1259 return 1; /* error */
1262 /* initialize port private structure */
1263 spin_lock_init(&priv
->dp_port_lock
);
1264 priv
->dp_port_num
= i
;
1265 priv
->dp_out_buf_len
= 0;
1266 priv
->dp_write_urb_in_use
= 0;
1267 priv
->dp_modem_signals
= 0;
1268 init_waitqueue_head(&priv
->dp_modem_change_wait
);
1269 priv
->dp_transmit_idle
= 0;
1270 init_waitqueue_head(&priv
->dp_transmit_idle_wait
);
1271 priv
->dp_throttled
= 0;
1272 priv
->dp_throttle_restart
= 0;
1273 init_waitqueue_head(&priv
->dp_flush_wait
);
1274 init_waitqueue_head(&priv
->dp_close_wait
);
1275 INIT_WORK(&priv
->dp_wakeup_work
, digi_wakeup_write_lock
);
1276 priv
->dp_port
= serial
->port
[i
];
1277 /* initialize write wait queue for this port */
1278 init_waitqueue_head(&serial
->port
[i
]->write_wait
);
1280 usb_set_serial_port_data(serial
->port
[i
], priv
);
1283 /* allocate serial private structure */
1284 serial_priv
= kmalloc(sizeof(struct digi_serial
), GFP_KERNEL
);
1285 if (serial_priv
== NULL
) {
1286 for (i
= 0; i
< serial
->type
->num_ports
+ 1; i
++)
1287 kfree(usb_get_serial_port_data(serial
->port
[i
]));
1288 return 1; /* error */
1291 /* initialize serial private structure */
1292 spin_lock_init(&serial_priv
->ds_serial_lock
);
1293 serial_priv
->ds_oob_port_num
= serial
->type
->num_ports
;
1294 serial_priv
->ds_oob_port
= serial
->port
[serial_priv
->ds_oob_port_num
];
1295 serial_priv
->ds_device_started
= 0;
1296 usb_set_serial_data(serial
, serial_priv
);
1302 static void digi_disconnect(struct usb_serial
*serial
)
1306 /* stop reads and writes on all ports */
1307 for (i
= 0; i
< serial
->type
->num_ports
+ 1; i
++) {
1308 usb_kill_urb(serial
->port
[i
]->read_urb
);
1309 usb_kill_urb(serial
->port
[i
]->write_urb
);
1314 static void digi_release(struct usb_serial
*serial
)
1318 /* free the private data structures for all ports */
1319 /* number of regular ports + 1 for the out-of-band port */
1320 for (i
= 0; i
< serial
->type
->num_ports
+ 1; i
++)
1321 kfree(usb_get_serial_port_data(serial
->port
[i
]));
1322 kfree(usb_get_serial_data(serial
));
1326 static void digi_read_bulk_callback(struct urb
*urb
)
1328 struct usb_serial_port
*port
= urb
->context
;
1329 struct digi_port
*priv
;
1330 struct digi_serial
*serial_priv
;
1332 int status
= urb
->status
;
1334 /* port sanity check, do not resubmit if port is not valid */
1337 priv
= usb_get_serial_port_data(port
);
1339 dev_err(&port
->dev
, "%s: port->private is NULL, status=%d\n",
1343 if (port
->serial
== NULL
||
1344 (serial_priv
= usb_get_serial_data(port
->serial
)) == NULL
) {
1345 dev_err(&port
->dev
, "%s: serial is bad or serial->private "
1346 "is NULL, status=%d\n", __func__
, status
);
1350 /* do not resubmit urb if it has any status error */
1353 "%s: nonzero read bulk status: status=%d, port=%d\n",
1354 __func__
, status
, priv
->dp_port_num
);
1358 /* handle oob or inb callback, do not resubmit if error */
1359 if (priv
->dp_port_num
== serial_priv
->ds_oob_port_num
) {
1360 if (digi_read_oob_callback(urb
) != 0)
1363 if (digi_read_inb_callback(urb
) != 0)
1368 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
1369 if (ret
!= 0 && ret
!= -EPERM
) {
1371 "%s: failed resubmitting urb, ret=%d, port=%d\n",
1372 __func__
, ret
, priv
->dp_port_num
);
1378 * Digi Read INB Callback
1380 * Digi Read INB Callback handles reads on the in band ports, sending
1381 * the data on to the tty subsystem. When called we know port and
1382 * port->private are not NULL and port->serial has been validated.
1383 * It returns 0 if successful, 1 if successful but the port is
1384 * throttled, and -1 if the sanity checks failed.
1387 static int digi_read_inb_callback(struct urb
*urb
)
1390 struct usb_serial_port
*port
= urb
->context
;
1391 struct tty_struct
*tty
;
1392 struct digi_port
*priv
= usb_get_serial_port_data(port
);
1393 int opcode
= ((unsigned char *)urb
->transfer_buffer
)[0];
1394 int len
= ((unsigned char *)urb
->transfer_buffer
)[1];
1395 int port_status
= ((unsigned char *)urb
->transfer_buffer
)[2];
1396 unsigned char *data
= ((unsigned char *)urb
->transfer_buffer
) + 3;
1397 int flag
, throttled
;
1398 int status
= urb
->status
;
1400 /* do not process callbacks on closed ports */
1401 /* but do continue the read chain */
1402 if (urb
->status
== -ENOENT
)
1405 /* short/multiple packet check */
1406 if (urb
->actual_length
!= len
+ 2) {
1407 dev_err(&port
->dev
, "%s: INCOMPLETE OR MULTIPLE PACKET, "
1408 "status=%d, port=%d, opcode=%d, len=%d, "
1409 "actual_length=%d, status=%d\n", __func__
, status
,
1410 priv
->dp_port_num
, opcode
, len
, urb
->actual_length
,
1415 tty
= tty_port_tty_get(&port
->port
);
1416 spin_lock(&priv
->dp_port_lock
);
1418 /* check for throttle; if set, do not resubmit read urb */
1419 /* indicate the read chain needs to be restarted on unthrottle */
1420 throttled
= priv
->dp_throttled
;
1422 priv
->dp_throttle_restart
= 1;
1425 if (tty
&& opcode
== DIGI_CMD_RECEIVE_DATA
) {
1426 /* get flag from port_status */
1429 /* overrun is special, not associated with a char */
1430 if (port_status
& DIGI_OVERRUN_ERROR
)
1431 tty_insert_flip_char(tty
, 0, TTY_OVERRUN
);
1433 /* break takes precedence over parity, */
1434 /* which takes precedence over framing errors */
1435 if (port_status
& DIGI_BREAK_ERROR
)
1437 else if (port_status
& DIGI_PARITY_ERROR
)
1439 else if (port_status
& DIGI_FRAMING_ERROR
)
1442 /* data length is len-1 (one byte of len is port_status) */
1445 tty_insert_flip_string_fixed_flag(tty
, data
, flag
,
1447 tty_flip_buffer_push(tty
);
1450 spin_unlock(&priv
->dp_port_lock
);
1453 if (opcode
== DIGI_CMD_RECEIVE_DISABLE
)
1454 dev_dbg(&port
->dev
, "%s: got RECEIVE_DISABLE\n", __func__
);
1455 else if (opcode
!= DIGI_CMD_RECEIVE_DATA
)
1456 dev_dbg(&port
->dev
, "%s: unknown opcode: %d\n", __func__
, opcode
);
1458 return throttled
? 1 : 0;
1464 * Digi Read OOB Callback
1466 * Digi Read OOB Callback handles reads on the out of band port.
1467 * When called we know port and port->private are not NULL and
1468 * the port->serial is valid. It returns 0 if successful, and
1469 * -1 if the sanity checks failed.
1472 static int digi_read_oob_callback(struct urb
*urb
)
1475 struct usb_serial_port
*port
= urb
->context
;
1476 struct usb_serial
*serial
= port
->serial
;
1477 struct tty_struct
*tty
;
1478 struct digi_port
*priv
= usb_get_serial_port_data(port
);
1479 int opcode
, line
, status
, val
;
1483 /* handle each oob command */
1484 for (i
= 0; i
< urb
->actual_length
- 3;) {
1485 opcode
= ((unsigned char *)urb
->transfer_buffer
)[i
++];
1486 line
= ((unsigned char *)urb
->transfer_buffer
)[i
++];
1487 status
= ((unsigned char *)urb
->transfer_buffer
)[i
++];
1488 val
= ((unsigned char *)urb
->transfer_buffer
)[i
++];
1490 dev_dbg(&port
->dev
, "digi_read_oob_callback: opcode=%d, line=%d, status=%d, val=%d\n",
1491 opcode
, line
, status
, val
);
1493 if (status
!= 0 || line
>= serial
->type
->num_ports
)
1496 port
= serial
->port
[line
];
1498 priv
= usb_get_serial_port_data(port
);
1502 tty
= tty_port_tty_get(&port
->port
);
1506 rts
= tty
->termios
.c_cflag
& CRTSCTS
;
1508 if (tty
&& opcode
== DIGI_CMD_READ_INPUT_SIGNALS
) {
1509 spin_lock(&priv
->dp_port_lock
);
1510 /* convert from digi flags to termiox flags */
1511 if (val
& DIGI_READ_INPUT_SIGNALS_CTS
) {
1512 priv
->dp_modem_signals
|= TIOCM_CTS
;
1513 /* port must be open to use tty struct */
1515 tty
->hw_stopped
= 0;
1516 digi_wakeup_write(port
);
1519 priv
->dp_modem_signals
&= ~TIOCM_CTS
;
1520 /* port must be open to use tty struct */
1522 tty
->hw_stopped
= 1;
1524 if (val
& DIGI_READ_INPUT_SIGNALS_DSR
)
1525 priv
->dp_modem_signals
|= TIOCM_DSR
;
1527 priv
->dp_modem_signals
&= ~TIOCM_DSR
;
1528 if (val
& DIGI_READ_INPUT_SIGNALS_RI
)
1529 priv
->dp_modem_signals
|= TIOCM_RI
;
1531 priv
->dp_modem_signals
&= ~TIOCM_RI
;
1532 if (val
& DIGI_READ_INPUT_SIGNALS_DCD
)
1533 priv
->dp_modem_signals
|= TIOCM_CD
;
1535 priv
->dp_modem_signals
&= ~TIOCM_CD
;
1537 wake_up_interruptible(&priv
->dp_modem_change_wait
);
1538 spin_unlock(&priv
->dp_port_lock
);
1539 } else if (opcode
== DIGI_CMD_TRANSMIT_IDLE
) {
1540 spin_lock(&priv
->dp_port_lock
);
1541 priv
->dp_transmit_idle
= 1;
1542 wake_up_interruptible(&priv
->dp_transmit_idle_wait
);
1543 spin_unlock(&priv
->dp_port_lock
);
1544 } else if (opcode
== DIGI_CMD_IFLUSH_FIFO
) {
1545 wake_up_interruptible(&priv
->dp_flush_wait
);
1553 module_usb_serial_driver(serial_drivers
, id_table_combined
);
1555 MODULE_AUTHOR(DRIVER_AUTHOR
);
1556 MODULE_DESCRIPTION(DRIVER_DESC
);
1557 MODULE_LICENSE("GPL");