2 * Digi AccelePort USB-4 Serial Converter
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)
17 * (8/8/2000) pberger and borchers
18 * -- Fixed close so that
19 * - it can timeout while waiting for transmit idle, if needed;
20 * - it ignores interrupts when flushing the port, turning
21 * of modem signalling, and so on;
22 * - it waits for the flush to really complete before returning.
23 * -- Read_bulk_callback and write_bulk_callback check for a closed
24 * port before using the tty struct or writing to the port.
25 * -- The two changes above fix the oops caused by interrupted closes.
26 * -- Added interruptible args to write_oob_command and set_modem_signals
27 * and added a timeout arg to transmit_idle; needed for fixes to
29 * -- Added code for rx_throttle and rx_unthrottle so that input flow
31 * -- Added code to set overrun, parity, framing, and break errors
33 * -- Set USB_DISABLE_SPD flag for write bulk urbs, so no 0 length
34 * bulk writes are done. These hung the Digi USB device. The
35 * 0 length bulk writes were a new feature of usb-uhci added in
36 * the 2.4.0-test6 kernels.
37 * -- Fixed mod inc race in open; do mod inc before sleeping to wait
38 * for a close to finish.
41 * -- Fixed bugs with hardware handshaking:
42 * - Added code to set/clear tty->hw_stopped in digi_read_oob_callback()
43 * and digi_set_termios()
44 * -- Added code in digi_set_termios() to
45 * - add conditional in code handling transition from B0 to only
46 * set RTS if RTS/CTS flow control is either not in use or if
47 * the port is not currently throttled.
48 * - handle turning off CRTSCTS.
50 * (7/30/2000) borchers
51 * -- Added support for more than one Digi USB device by moving
52 * globals to a private structure in the pointed to from the
53 * usb_serial structure.
54 * -- Moved the modem change and transmit idle wait queues into
55 * the port private structure, so each port has its own queue
56 * rather than sharing global queues.
57 * -- Added support for break signals.
60 * -- Added USB-2 support. Note: the USB-2 supports 3 devices: two
61 * serial and a parallel port. The parallel port is implemented
62 * as a serial-to-parallel converter. That is, the driver actually
63 * presents all three USB-2 interfaces as serial ports, but the third
64 * one physically connects to a parallel device. Thus, for example,
65 * one could plug a parallel printer into the USB-2's third port,
66 * but from the kernel's (and userland's) point of view what's
67 * actually out there is a serial device.
69 * (7/15/2000) borchers
70 * -- Fixed race in open when a close is in progress.
71 * -- Keep count of opens and dec the module use count for each
72 * outstanding open when shutdown is called (on disconnect).
73 * -- Fixed sanity checks in read_bulk_callback and write_bulk_callback
74 * so pointers are checked before use.
75 * -- Split read bulk callback into in band and out of band
76 * callbacks, and no longer restart read chains if there is
77 * a status error or a sanity error. This fixed the seg
78 * faults and other errors we used to get on disconnect.
79 * -- Port->active is once again a flag as usb-serial intended it
80 * to be, not a count. Since it was only a char it would
81 * have been limited to 256 simultaneous opens. Now the open
82 * count is kept in the port private structure in dp_open_count.
83 * -- Added code for modularization of the digi_acceleport driver.
85 * (6/27/2000) pberger and borchers
86 * -- Zeroed out sync field in the wakeup_task before first use;
87 * otherwise the uninitialized value might prevent the task from
89 * -- Initialized ret value to 0 in write_bulk_callback, otherwise
90 * the uninitialized value could cause a spurious debugging message.
92 * (6/22/2000) pberger and borchers
93 * -- Made cond_wait_... inline--apparently on SPARC the flags arg
94 * to spin_lock_irqsave cannot be passed to another function
95 * to call spin_unlock_irqrestore. Thanks to Pauline Middelink.
96 * -- In digi_set_modem_signals the inner nested spin locks use just
97 * spin_lock() rather than spin_lock_irqsave(). The old code
98 * mistakenly left interrupts off. Thanks to Pauline Middelink.
99 * -- copy_from_user (which can sleep) is no longer called while a
100 * spinlock is held. We copy to a local buffer before getting
101 * the spinlock--don't like the extra copy but the code is simpler.
102 * -- Printk and dbg are no longer called while a spin lock is held.
104 * (6/4/2000) pberger and borchers
105 * -- Replaced separate calls to spin_unlock_irqrestore and
106 * interruptible_sleep_on_timeout with a new function
107 * cond_wait_interruptible_timeout_irqrestore. This eliminates
108 * the race condition where the wake up could happen after
109 * the unlock and before the sleep.
110 * -- Close now waits for output to drain.
111 * -- Open waits until any close in progress is finished.
112 * -- All out of band responses are now processed, not just the
113 * first in a USB packet.
114 * -- Fixed a bug that prevented the driver from working when the
115 * first Digi port was not the first USB serial port--the driver
116 * was mistakenly using the external USB serial port number to
117 * try to index into its internal ports.
118 * -- Fixed an SMP bug -- write_bulk_callback is called directly from
119 * an interrupt, so spin_lock_irqsave/spin_unlock_irqrestore are
120 * needed for locks outside write_bulk_callback that are also
121 * acquired by write_bulk_callback to prevent deadlocks.
122 * -- Fixed support for select() by making digi_chars_in_buffer()
123 * return 256 when -EINPROGRESS is set, as the line discipline
124 * code in n_tty.c expects.
125 * -- Fixed an include file ordering problem that prevented debugging
126 * messages from working.
127 * -- Fixed an intermittent timeout problem that caused writes to
128 * sometimes get stuck on some machines on some kernels. It turns
129 * out in these circumstances write_chan() (in n_tty.c) was
130 * asleep waiting for our wakeup call. Even though we call
131 * wake_up_interruptible() in digi_write_bulk_callback(), there is
132 * a race condition that could cause the wakeup to fail: if our
133 * wake_up_interruptible() call occurs between the time that our
134 * driver write routine finishes and write_chan() sets current->state
135 * to TASK_INTERRUPTIBLE, the effect of our wakeup setting the state
136 * to TASK_RUNNING will be lost and write_chan's subsequent call to
137 * schedule() will never return (unless it catches a signal).
138 * This race condition occurs because write_bulk_callback() (and thus
139 * the wakeup) are called asynchonously from an interrupt, rather than
140 * from the scheduler. We can avoid the race by calling the wakeup
141 * from the scheduler queue and that's our fix: Now, at the end of
142 * write_bulk_callback() we queue up a wakeup call on the scheduler
143 * task queue. We still also invoke the wakeup directly since that
144 * squeezes a bit more performance out of the driver, and any lost
145 * race conditions will get cleaned up at the next scheduler run.
147 * NOTE: The problem also goes away if you comment out
148 * the two code lines in write_chan() where current->state
149 * is set to TASK_RUNNING just before calling driver.write() and to
150 * TASK_INTERRUPTIBLE immediately afterwards. This is why the
151 * problem did not show up with the 2.2 kernels -- they do not
154 * (5/16/2000) pberger and borchers
155 * -- Added timeouts to sleeps, to defend against lost wake ups.
156 * -- Handle transition to/from B0 baud rate in digi_set_termios.
158 * (5/13/2000) pberger and borchers
159 * -- All commands now sent on out of band port, using
160 * digi_write_oob_command.
161 * -- Get modem control signals whenever they change, support TIOCMGET/
162 * SET/BIS/BIC ioctls.
163 * -- digi_set_termios now supports parity, word size, stop bits, and
165 * -- Cleaned up open and close, use digi_set_termios and
166 * digi_write_oob_command to set port parameters.
167 * -- Added digi_startup_device to start read chains on all ports.
168 * -- Write buffer is only used when count==1, to be sure put_char can
169 * write a char (unless the buffer is full).
171 * (5/10/2000) pberger and borchers
172 * -- Added MOD_INC_USE_COUNT/MOD_DEC_USE_COUNT calls on open/close.
173 * -- Fixed problem where the first incoming character is lost on
174 * port opens after the first close on that port. Now we keep
175 * the read_urb chain open until shutdown.
176 * -- Added more port conditioning calls in digi_open and digi_close.
177 * -- Convert port->active to a use count so that we can deal with multiple
178 * opens and closes properly.
179 * -- Fixed some problems with the locking code.
181 * (5/3/2000) pberger and borchers
182 * -- First alpha version of the driver--many known limitations and bugs.
187 * - Each port, including the out-of-band port, has a lock used to
188 * serialize all access to the port's private structure.
189 * - The port lock is also used to serialize all writes and access to
191 * - The port lock is also used for the port write_wait condition
192 * variable. Holding the port lock will prevent a wake up on the
193 * port's write_wait; this can be used with cond_wait_... to be sure
194 * the wake up is not lost in a race when dropping the lock and
195 * sleeping waiting for the wakeup.
196 * - digi_write() does not sleep, since it is sometimes called on
198 * - digi_write_bulk_callback() and digi_read_bulk_callback() are
199 * called directly from interrupts. Hence spin_lock_irqsave()
200 * and spin_lock_irqrestore() are used in the rest of the code
201 * for any locks they acquire.
202 * - digi_write_bulk_callback() gets the port lock before waking up
203 * processes sleeping on the port write_wait. It also schedules
204 * wake ups so they happen from the scheduler, because the tty
205 * system can miss wake ups from interrupts.
206 * - All sleeps use a timeout of DIGI_RETRY_TIMEOUT before looping to
207 * recheck the condition they are sleeping on. This is defensive,
208 * in case a wake up is lost.
209 * - Following Documentation/DocBook/kernel-locking.pdf no spin locks
210 * are held when calling copy_to/from_user or printk.
212 * $Id: digi_acceleport.c,v 1.80 2000/08/09 06:36:18 root Exp $
215 #include <linux/config.h>
216 #include <linux/kernel.h>
217 #include <linux/sched.h>
218 #include <linux/signal.h>
219 #include <linux/errno.h>
220 #include <linux/poll.h>
221 #include <linux/init.h>
222 #include <linux/malloc.h>
223 #include <linux/fcntl.h>
224 #include <linux/tty_driver.h>
225 #include <linux/tty_flip.h>
226 #include <linux/tty.h>
227 #include <linux/module.h>
228 #include <linux/spinlock.h>
229 #include <linux/tqueue.h>
231 #ifdef CONFIG_USB_SERIAL_DEBUG
237 #include <linux/usb.h>
238 #include "usb-serial.h"
243 /* port output buffer length -- must be <= transfer buffer length - 2 */
244 /* so we can be sure to send the full buffer in one urb */
245 #define DIGI_OUT_BUF_SIZE 8
247 /* port input buffer length -- must be >= transfer buffer length - 3 */
248 /* so we can be sure to hold at least one full buffer from one urb */
249 #define DIGI_IN_BUF_SIZE 64
251 /* retry timeout while sleeping */
252 #define DIGI_RETRY_TIMEOUT (HZ/10)
254 /* timeout while waiting for tty output to drain in close */
255 /* this delay is used twice in close, so the total delay could */
256 /* be twice this value */
257 #define DIGI_CLOSE_TIMEOUT (5*HZ)
260 /* AccelePort USB Defines */
263 #define DIGI_VENDOR_ID 0x05c5
264 #define DIGI_2_ID 0x0002 /* USB-2 */
265 #define DIGI_4_ID 0x0004 /* USB-4 */
268 * "INB": can be used on the in-band endpoint
269 * "OOB": can be used on the out-of-band endpoint
271 #define DIGI_CMD_SET_BAUD_RATE 0 /* INB, OOB */
272 #define DIGI_CMD_SET_WORD_SIZE 1 /* INB, OOB */
273 #define DIGI_CMD_SET_PARITY 2 /* INB, OOB */
274 #define DIGI_CMD_SET_STOP_BITS 3 /* INB, OOB */
275 #define DIGI_CMD_SET_INPUT_FLOW_CONTROL 4 /* INB, OOB */
276 #define DIGI_CMD_SET_OUTPUT_FLOW_CONTROL 5 /* INB, OOB */
277 #define DIGI_CMD_SET_DTR_SIGNAL 6 /* INB, OOB */
278 #define DIGI_CMD_SET_RTS_SIGNAL 7 /* INB, OOB */
279 #define DIGI_CMD_READ_INPUT_SIGNALS 8 /* OOB */
280 #define DIGI_CMD_IFLUSH_FIFO 9 /* OOB */
281 #define DIGI_CMD_RECEIVE_ENABLE 10 /* INB, OOB */
282 #define DIGI_CMD_BREAK_CONTROL 11 /* INB, OOB */
283 #define DIGI_CMD_LOCAL_LOOPBACK 12 /* INB, OOB */
284 #define DIGI_CMD_TRANSMIT_IDLE 13 /* INB, OOB */
285 #define DIGI_CMD_READ_UART_REGISTER 14 /* OOB */
286 #define DIGI_CMD_WRITE_UART_REGISTER 15 /* INB, OOB */
287 #define DIGI_CMD_AND_UART_REGISTER 16 /* INB, OOB */
288 #define DIGI_CMD_OR_UART_REGISTER 17 /* INB, OOB */
289 #define DIGI_CMD_SEND_DATA 18 /* INB */
290 #define DIGI_CMD_RECEIVE_DATA 19 /* INB */
291 #define DIGI_CMD_RECEIVE_DISABLE 20 /* INB */
292 #define DIGI_CMD_GET_PORT_TYPE 21 /* OOB */
295 #define DIGI_BAUD_50 0
296 #define DIGI_BAUD_75 1
297 #define DIGI_BAUD_110 2
298 #define DIGI_BAUD_150 3
299 #define DIGI_BAUD_200 4
300 #define DIGI_BAUD_300 5
301 #define DIGI_BAUD_600 6
302 #define DIGI_BAUD_1200 7
303 #define DIGI_BAUD_1800 8
304 #define DIGI_BAUD_2400 9
305 #define DIGI_BAUD_4800 10
306 #define DIGI_BAUD_7200 11
307 #define DIGI_BAUD_9600 12
308 #define DIGI_BAUD_14400 13
309 #define DIGI_BAUD_19200 14
310 #define DIGI_BAUD_28800 15
311 #define DIGI_BAUD_38400 16
312 #define DIGI_BAUD_57600 17
313 #define DIGI_BAUD_76800 18
314 #define DIGI_BAUD_115200 19
315 #define DIGI_BAUD_153600 20
316 #define DIGI_BAUD_230400 21
317 #define DIGI_BAUD_460800 22
320 #define DIGI_WORD_SIZE_5 0
321 #define DIGI_WORD_SIZE_6 1
322 #define DIGI_WORD_SIZE_7 2
323 #define DIGI_WORD_SIZE_8 3
325 #define DIGI_PARITY_NONE 0
326 #define DIGI_PARITY_ODD 1
327 #define DIGI_PARITY_EVEN 2
328 #define DIGI_PARITY_MARK 3
329 #define DIGI_PARITY_SPACE 4
331 #define DIGI_STOP_BITS_1 0
332 #define DIGI_STOP_BITS_2 1
334 #define DIGI_INPUT_FLOW_CONTROL_XON_XOFF 1
335 #define DIGI_INPUT_FLOW_CONTROL_RTS 2
336 #define DIGI_INPUT_FLOW_CONTROL_DTR 4
338 #define DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF 1
339 #define DIGI_OUTPUT_FLOW_CONTROL_CTS 2
340 #define DIGI_OUTPUT_FLOW_CONTROL_DSR 4
342 #define DIGI_DTR_INACTIVE 0
343 #define DIGI_DTR_ACTIVE 1
344 #define DIGI_DTR_INPUT_FLOW_CONTROL 2
346 #define DIGI_RTS_INACTIVE 0
347 #define DIGI_RTS_ACTIVE 1
348 #define DIGI_RTS_INPUT_FLOW_CONTROL 2
349 #define DIGI_RTS_TOGGLE 3
351 #define DIGI_FLUSH_TX 1
352 #define DIGI_FLUSH_RX 2
353 #define DIGI_RESUME_TX 4 /* clears xoff condition */
355 #define DIGI_TRANSMIT_NOT_IDLE 0
356 #define DIGI_TRANSMIT_IDLE 1
358 #define DIGI_DISABLE 0
359 #define DIGI_ENABLE 1
361 #define DIGI_DEASSERT 0
362 #define DIGI_ASSERT 1
364 /* in band status codes */
365 #define DIGI_OVERRUN_ERROR 4
366 #define DIGI_PARITY_ERROR 8
367 #define DIGI_FRAMING_ERROR 16
368 #define DIGI_BREAK_ERROR 32
370 /* out of band status */
371 #define DIGI_NO_ERROR 0
372 #define DIGI_BAD_FIRST_PARAMETER 1
373 #define DIGI_BAD_SECOND_PARAMETER 2
374 #define DIGI_INVALID_LINE 3
375 #define DIGI_INVALID_OPCODE 4
378 #define DIGI_READ_INPUT_SIGNALS_SLOT 1
379 #define DIGI_READ_INPUT_SIGNALS_ERR 2
380 #define DIGI_READ_INPUT_SIGNALS_BUSY 4
381 #define DIGI_READ_INPUT_SIGNALS_PE 8
382 #define DIGI_READ_INPUT_SIGNALS_CTS 16
383 #define DIGI_READ_INPUT_SIGNALS_DSR 32
384 #define DIGI_READ_INPUT_SIGNALS_RI 64
385 #define DIGI_READ_INPUT_SIGNALS_DCD 128
389 #define MAX(a,b) (((a)>(b))?(a):(b))
390 #define MIN(a,b) (((a)<(b))?(a):(b))
395 typedef struct digi_serial
{
396 spinlock_t ds_serial_lock
;
397 struct usb_serial_port
*ds_oob_port
; /* out-of-band port */
398 int ds_oob_port_num
; /* index of out-of-band port */
399 int ds_device_started
;
402 typedef struct digi_port
{
403 spinlock_t dp_port_lock
;
406 unsigned char dp_out_buf
[DIGI_OUT_BUF_SIZE
];
408 unsigned char dp_in_buf
[DIGI_IN_BUF_SIZE
];
409 unsigned char dp_in_flag_buf
[DIGI_IN_BUF_SIZE
];
410 unsigned int dp_modem_signals
;
411 wait_queue_head_t dp_modem_change_wait
;
412 int dp_open_count
; /* inc on open, dec on close */
413 int dp_transmit_idle
;
414 wait_queue_head_t dp_transmit_idle_wait
;
416 int dp_throttle_restart
;
417 wait_queue_head_t dp_flush_wait
;
418 int dp_in_close
; /* close in progress */
419 wait_queue_head_t dp_close_wait
; /* wait queue for close */
420 struct tq_struct dp_wakeup_task
;
424 /* Local Function Declarations */
426 static void digi_wakeup_write( struct usb_serial_port
*port
);
427 static void digi_wakeup_write_lock( struct usb_serial_port
*port
);
428 static int digi_write_oob_command( struct usb_serial_port
*port
,
429 unsigned char *buf
, int count
, int interruptible
);
430 static int digi_write_inb_command( struct usb_serial_port
*port
,
431 unsigned char *buf
, int count
, unsigned long timeout
);
432 static int digi_set_modem_signals( struct usb_serial_port
*port
,
433 unsigned int modem_signals
, int interruptible
);
434 static int digi_transmit_idle( struct usb_serial_port
*port
,
435 unsigned long timeout
);
436 static void digi_rx_throttle (struct usb_serial_port
*port
);
437 static void digi_rx_unthrottle (struct usb_serial_port
*port
);
438 static void digi_set_termios( struct usb_serial_port
*port
,
439 struct termios
*old_termios
);
440 static void digi_break_ctl( struct usb_serial_port
*port
, int break_state
);
441 static int digi_ioctl( struct usb_serial_port
*port
, struct file
*file
,
442 unsigned int cmd
, unsigned long arg
);
443 static int digi_write( struct usb_serial_port
*port
, int from_user
,
444 const unsigned char *buf
, int count
);
445 static void digi_write_bulk_callback( struct urb
*urb
);
446 static int digi_write_room( struct usb_serial_port
*port
);
447 static int digi_chars_in_buffer( struct usb_serial_port
*port
);
448 static int digi_open( struct usb_serial_port
*port
, struct file
*filp
);
449 static void digi_close( struct usb_serial_port
*port
, struct file
*filp
);
450 static int digi_startup_device( struct usb_serial
*serial
);
451 static int digi_startup( struct usb_serial
*serial
);
452 static void digi_shutdown( struct usb_serial
*serial
);
453 static void digi_read_bulk_callback( struct urb
*urb
);
454 static int digi_read_inb_callback( struct urb
*urb
);
455 static int digi_read_oob_callback( struct urb
*urb
);
460 /* device info needed for the Digi serial converter */
461 static u16 digi_vendor_id
= DIGI_VENDOR_ID
;
462 static u16 digi_product_2_id
= DIGI_2_ID
; /* USB 2 */
463 static u16 digi_product_4_id
= DIGI_4_ID
; /* USB 4 */
465 static struct usb_serial_device_type digi_acceleport_2_device
= {
467 idVendor
: &digi_vendor_id
,
468 idProduct
: &digi_product_2_id
,
469 needs_interrupt_in
: DONT_CARE
,
470 needs_bulk_in
: MUST_HAVE
,
471 needs_bulk_out
: MUST_HAVE
,
479 write_room
: digi_write_room
,
480 write_bulk_callback
: digi_write_bulk_callback
,
481 read_bulk_callback
: digi_read_bulk_callback
,
482 chars_in_buffer
: digi_chars_in_buffer
,
483 throttle
: digi_rx_throttle
,
484 unthrottle
: digi_rx_unthrottle
,
486 set_termios
: digi_set_termios
,
487 break_ctl
: digi_break_ctl
,
488 startup
: digi_startup
,
489 shutdown
: digi_shutdown
,
492 static struct usb_serial_device_type digi_acceleport_4_device
= {
494 idVendor
: &digi_vendor_id
,
495 idProduct
: &digi_product_4_id
,
496 needs_interrupt_in
: DONT_CARE
,
497 needs_bulk_in
: MUST_HAVE
,
498 needs_bulk_out
: MUST_HAVE
,
506 write_room
: digi_write_room
,
507 write_bulk_callback
: digi_write_bulk_callback
,
508 read_bulk_callback
: digi_read_bulk_callback
,
509 chars_in_buffer
: digi_chars_in_buffer
,
510 throttle
: digi_rx_throttle
,
511 unthrottle
: digi_rx_unthrottle
,
513 set_termios
: digi_set_termios
,
514 break_ctl
: digi_break_ctl
,
515 startup
: digi_startup
,
516 shutdown
: digi_shutdown
,
523 * Cond Wait Interruptible Timeout Irqrestore
525 * Do spin_unlock_irqrestore and interruptible_sleep_on_timeout
526 * so that wake ups are not lost if they occur between the unlock
527 * and the sleep. In other words, spin_lock_irqrestore and
528 * interruptible_sleep_on_timeout are "atomic" with respect to
529 * wake ups. This is used to implement condition variables.
532 static inline long cond_wait_interruptible_timeout_irqrestore(
533 wait_queue_head_t
*q
, long timeout
,
534 spinlock_t
*lock
, unsigned long flags
)
540 init_waitqueue_entry( &wait
, current
);
542 set_current_state( TASK_INTERRUPTIBLE
);
544 add_wait_queue( q
, &wait
);
546 spin_unlock_irqrestore( lock
, flags
);
548 timeout
= schedule_timeout(timeout
);
550 set_current_state( TASK_RUNNING
);
552 remove_wait_queue( q
, &wait
);
562 * Wake up port, line discipline, and tty processes sleeping
566 static void digi_wakeup_write_lock( struct usb_serial_port
*port
)
570 digi_port_t
*priv
= (digi_port_t
*)(port
->private);
573 spin_lock_irqsave( &priv
->dp_port_lock
, flags
);
574 digi_wakeup_write( port
);
575 spin_unlock_irqrestore( &priv
->dp_port_lock
, flags
);
579 static void digi_wakeup_write( struct usb_serial_port
*port
)
582 struct tty_struct
*tty
= port
->tty
;
585 /* wake up port processes */
586 wake_up_interruptible( &port
->write_wait
);
588 /* wake up line discipline */
589 if( (tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
))
590 && tty
->ldisc
.write_wakeup
)
591 (tty
->ldisc
.write_wakeup
)(tty
);
593 /* wake up other tty processes */
594 wake_up_interruptible( &tty
->write_wait
);
595 /* For 2.2.16 backport -- wake_up_interruptible( &tty->poll_wait ); */
601 * Digi Write OOB Command
603 * Write commands on the out of band port. Commands are 4
604 * bytes each, multiple commands can be sent at once, and
605 * no command will be split across USB packets. Returns 0
606 * if successful, -EINTR if interrupted while sleeping and
607 * the interruptible flag is true, or a negative error
608 * returned by usb_submit_urb.
611 static int digi_write_oob_command( struct usb_serial_port
*port
,
612 unsigned char *buf
, int count
, int interruptible
)
617 struct usb_serial_port
*oob_port
= (struct usb_serial_port
*)((digi_serial_t
*)port
->serial
->private)->ds_oob_port
;
618 digi_port_t
*oob_priv
= (digi_port_t
*)oob_port
->private;
619 unsigned long flags
= 0;
622 dbg( "digi_write_oob_command: TOP: port=%d, count=%d", oob_priv
->dp_port_num
, count
);
624 spin_lock_irqsave( &oob_priv
->dp_port_lock
, flags
);
628 while( oob_port
->write_urb
->status
== -EINPROGRESS
) {
629 cond_wait_interruptible_timeout_irqrestore(
630 &oob_port
->write_wait
, DIGI_RETRY_TIMEOUT
,
631 &oob_priv
->dp_port_lock
, flags
);
632 if( interruptible
&& signal_pending(current
) ) {
635 spin_lock_irqsave( &oob_priv
->dp_port_lock
, flags
);
638 /* len must be a multiple of 4, so commands are not split */
639 len
= MIN( count
, oob_port
->bulk_out_size
);
643 memcpy( oob_port
->write_urb
->transfer_buffer
, buf
, len
);
644 oob_port
->write_urb
->transfer_buffer_length
= len
;
646 if( (ret
=usb_submit_urb(oob_port
->write_urb
)) == 0 ) {
653 spin_unlock_irqrestore( &oob_priv
->dp_port_lock
, flags
);
656 err( __FUNCTION__
": usb_submit_urb failed, ret=%d",
666 * Digi Write In Band Command
668 * Write commands on the given port. Commands are 4
669 * bytes each, multiple commands can be sent at once, and
670 * no command will be split across USB packets. If timeout
671 * is non-zero, write in band command will return after
672 * waiting unsuccessfully for the URB status to clear for
673 * timeout ticks. Returns 0 if successful, or a negative
674 * error returned by digi_write.
677 static int digi_write_inb_command( struct usb_serial_port
*port
,
678 unsigned char *buf
, int count
, unsigned long timeout
)
683 digi_port_t
*priv
= (digi_port_t
*)(port
->private);
684 unsigned char *data
= port
->write_urb
->transfer_buffer
;
685 unsigned long flags
= 0;
688 dbg( "digi_write_inb_command: TOP: port=%d, count=%d", priv
->dp_port_num
,
696 spin_lock_irqsave( &priv
->dp_port_lock
, flags
);
698 while( count
> 0 && ret
== 0 ) {
700 while( port
->write_urb
->status
== -EINPROGRESS
701 && jiffies
< timeout
) {
702 cond_wait_interruptible_timeout_irqrestore(
703 &port
->write_wait
, DIGI_RETRY_TIMEOUT
,
704 &priv
->dp_port_lock
, flags
);
705 if( signal_pending(current
) ) {
708 spin_lock_irqsave( &priv
->dp_port_lock
, flags
);
711 /* len must be a multiple of 4 and small enough to */
712 /* guarantee the write will send buffered data first, */
713 /* so commands are in order with data and not split */
714 len
= MIN( count
, port
->bulk_out_size
-2-priv
->dp_out_buf_len
);
718 /* write any buffered data first */
719 if( priv
->dp_out_buf_len
> 0 ) {
720 data
[0] = DIGI_CMD_SEND_DATA
;
721 data
[1] = priv
->dp_out_buf_len
;
722 memcpy( data
+2, priv
->dp_out_buf
,
723 priv
->dp_out_buf_len
);
724 memcpy( data
+2+priv
->dp_out_buf_len
, buf
, len
);
725 port
->write_urb
->transfer_buffer_length
726 = priv
->dp_out_buf_len
+2+len
;
728 memcpy( data
, buf
, len
);
729 port
->write_urb
->transfer_buffer_length
= len
;
732 if( (ret
=usb_submit_urb(port
->write_urb
)) == 0 ) {
733 priv
->dp_out_buf_len
= 0;
740 spin_unlock_irqrestore( &priv
->dp_port_lock
, flags
);
743 err( __FUNCTION__
": usb_submit_urb failed, ret=%d, port=%d",
744 ret
, priv
->dp_port_num
);
753 * Digi Set Modem Signals
755 * Sets or clears DTR and RTS on the port, according to the
756 * modem_signals argument. Use TIOCM_DTR and TIOCM_RTS flags
757 * for the modem_signals argument. Returns 0 if successful,
758 * -EINTR if interrupted while sleeping, or a non-zero error
759 * returned by usb_submit_urb.
762 static int digi_set_modem_signals( struct usb_serial_port
*port
,
763 unsigned int modem_signals
, int interruptible
)
767 digi_port_t
*port_priv
= (digi_port_t
*)port
->private;
768 struct usb_serial_port
*oob_port
= (struct usb_serial_port
*)((digi_serial_t
*)port
->serial
->private)->ds_oob_port
;
769 digi_port_t
*oob_priv
= (digi_port_t
*)oob_port
->private;
770 unsigned char *data
= oob_port
->write_urb
->transfer_buffer
;
771 unsigned long flags
= 0;
774 dbg( "digi_set_modem_signals: TOP: port=%d, modem_signals=0x%x",
775 port_priv
->dp_port_num
, modem_signals
);
777 spin_lock_irqsave( &oob_priv
->dp_port_lock
, flags
);
778 spin_lock( &port_priv
->dp_port_lock
);
780 while( oob_port
->write_urb
->status
== -EINPROGRESS
) {
781 spin_unlock( &port_priv
->dp_port_lock
);
782 cond_wait_interruptible_timeout_irqrestore(
783 &oob_port
->write_wait
, DIGI_RETRY_TIMEOUT
,
784 &oob_priv
->dp_port_lock
, flags
);
785 if( interruptible
&& signal_pending(current
) ) {
788 spin_lock_irqsave( &oob_priv
->dp_port_lock
, flags
);
789 spin_lock( &port_priv
->dp_port_lock
);
792 data
[0] = DIGI_CMD_SET_DTR_SIGNAL
;
793 data
[1] = port_priv
->dp_port_num
;
794 data
[2] = (modem_signals
&TIOCM_DTR
) ?
795 DIGI_DTR_ACTIVE
: DIGI_DTR_INACTIVE
;
798 data
[4] = DIGI_CMD_SET_RTS_SIGNAL
;
799 data
[5] = port_priv
->dp_port_num
;
800 data
[6] = (modem_signals
&TIOCM_RTS
) ?
801 DIGI_RTS_ACTIVE
: DIGI_RTS_INACTIVE
;
804 oob_port
->write_urb
->transfer_buffer_length
= 8;
806 if( (ret
=usb_submit_urb(oob_port
->write_urb
)) == 0 ) {
807 port_priv
->dp_modem_signals
=
808 (port_priv
->dp_modem_signals
&~(TIOCM_DTR
|TIOCM_RTS
))
809 | (modem_signals
&(TIOCM_DTR
|TIOCM_RTS
));
812 spin_unlock( &port_priv
->dp_port_lock
);
813 spin_unlock_irqrestore( &oob_priv
->dp_port_lock
, flags
);
816 err( __FUNCTION__
": usb_submit_urb failed, ret=%d",
828 * Digi transmit idle waits, up to timeout ticks, for the transmitter
829 * to go idle. It returns 0 if successful or a negative error.
831 * There are race conditions here if more than one process is calling
832 * digi_transmit_idle on the same port at the same time. However, this
833 * is only called from close, and only one process can be in close on a
834 * port at a time, so its ok.
837 static int digi_transmit_idle( struct usb_serial_port
*port
,
838 unsigned long timeout
)
842 unsigned char buf
[2];
843 digi_port_t
*priv
= (digi_port_t
*)(port
->private);
844 unsigned long flags
= 0;
847 spin_lock_irqsave( &priv
->dp_port_lock
, flags
);
848 priv
->dp_transmit_idle
= 0;
849 spin_unlock_irqrestore( &priv
->dp_port_lock
, flags
);
851 buf
[0] = DIGI_CMD_TRANSMIT_IDLE
;
856 if( (ret
=digi_write_inb_command( port
, buf
, 2, timeout
-jiffies
)) != 0 )
859 spin_lock_irqsave( &priv
->dp_port_lock
, flags
);
861 while( jiffies
< timeout
&& !priv
->dp_transmit_idle
) {
862 cond_wait_interruptible_timeout_irqrestore(
863 &priv
->dp_transmit_idle_wait
, DIGI_RETRY_TIMEOUT
,
864 &priv
->dp_port_lock
, flags
);
865 if( signal_pending(current
) ) {
868 spin_lock_irqsave( &priv
->dp_port_lock
, flags
);
871 priv
->dp_transmit_idle
= 0;
872 spin_unlock_irqrestore( &priv
->dp_port_lock
, flags
);
879 static void digi_rx_throttle( struct usb_serial_port
*port
)
883 digi_port_t
*priv
= (digi_port_t
*)(port
->private);
886 dbg( "digi_rx_throttle: TOP: port=%d", priv
->dp_port_num
);
888 /* stop receiving characters by not resubmitting the read urb */
889 spin_lock_irqsave( &priv
->dp_port_lock
, flags
);
890 priv
->dp_throttled
= 1;
891 priv
->dp_throttle_restart
= 0;
892 priv
->dp_in_buf_len
= 0;
893 spin_unlock_irqrestore( &priv
->dp_port_lock
, flags
);
898 static void digi_rx_unthrottle( struct usb_serial_port
*port
)
904 digi_port_t
*priv
= (digi_port_t
*)(port
->private);
905 struct tty_struct
*tty
= port
->tty
;
908 dbg( "digi_rx_unthrottle: TOP: port=%d", priv
->dp_port_num
);
910 spin_lock_irqsave( &priv
->dp_port_lock
, flags
);
912 /* send any buffered chars from throttle time on to tty subsystem */
913 len
= MIN( priv
->dp_in_buf_len
, TTY_FLIPBUF_SIZE
- tty
->flip
.count
);
915 memcpy( tty
->flip
.char_buf_ptr
, priv
->dp_in_buf
, len
);
916 memcpy( tty
->flip
.flag_buf_ptr
, priv
->dp_in_flag_buf
, len
);
917 tty
->flip
.char_buf_ptr
+= len
;
918 tty
->flip
.flag_buf_ptr
+= len
;
919 tty
->flip
.count
+= len
;
920 tty_flip_buffer_push( tty
);
923 /* restart read chain */
924 if( priv
->dp_throttle_restart
)
925 ret
= usb_submit_urb( port
->read_urb
);
927 /* turn throttle off */
928 priv
->dp_throttled
= 0;
929 priv
->dp_in_buf_len
= 0;
930 priv
->dp_throttle_restart
= 0;
932 spin_unlock_irqrestore( &priv
->dp_port_lock
, flags
);
935 err( __FUNCTION__
": usb_submit_urb failed, ret=%d, port=%d",
936 ret
, priv
->dp_port_num
);
942 static void digi_set_termios( struct usb_serial_port
*port
,
943 struct termios
*old_termios
)
946 digi_port_t
*priv
= (digi_port_t
*)(port
->private);
947 unsigned int iflag
= port
->tty
->termios
->c_iflag
;
948 unsigned int cflag
= port
->tty
->termios
->c_cflag
;
949 unsigned int old_iflag
= old_termios
->c_iflag
;
950 unsigned int old_cflag
= old_termios
->c_cflag
;
951 unsigned char buf
[32];
952 unsigned int modem_signals
;
957 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
);
960 if( (cflag
&CBAUD
) != (old_cflag
&CBAUD
) ) {
964 /* reassert DTR and (maybe) RTS on transition from B0 */
965 if( (old_cflag
&CBAUD
) == B0
) {
966 /* don't set RTS if using hardware flow control */
967 /* and throttling input */
968 modem_signals
= TIOCM_DTR
;
969 if( !(port
->tty
->termios
->c_cflag
& CRTSCTS
) ||
970 !test_bit(TTY_THROTTLED
, &port
->tty
->flags
) ) {
971 modem_signals
|= TIOCM_RTS
;
973 digi_set_modem_signals( port
, modem_signals
, 1 );
976 switch( (cflag
&CBAUD
) ) {
977 /* drop DTR and RTS on transition to B0 */
978 case B0
: digi_set_modem_signals( port
, 0, 1 ); break;
979 case B50
: arg
= DIGI_BAUD_50
; break;
980 case B75
: arg
= DIGI_BAUD_75
; break;
981 case B110
: arg
= DIGI_BAUD_110
; break;
982 case B150
: arg
= DIGI_BAUD_150
; break;
983 case B200
: arg
= DIGI_BAUD_200
; break;
984 case B300
: arg
= DIGI_BAUD_300
; break;
985 case B600
: arg
= DIGI_BAUD_600
; break;
986 case B1200
: arg
= DIGI_BAUD_1200
; break;
987 case B1800
: arg
= DIGI_BAUD_1800
; break;
988 case B2400
: arg
= DIGI_BAUD_2400
; break;
989 case B4800
: arg
= DIGI_BAUD_4800
; break;
990 case B9600
: arg
= DIGI_BAUD_9600
; break;
991 case B19200
: arg
= DIGI_BAUD_19200
; break;
992 case B38400
: arg
= DIGI_BAUD_38400
; break;
993 case B57600
: arg
= DIGI_BAUD_57600
; break;
994 case B115200
: arg
= DIGI_BAUD_115200
; break;
995 case B230400
: arg
= DIGI_BAUD_230400
; break;
996 case B460800
: arg
= DIGI_BAUD_460800
; break;
998 dbg( "digi_set_termios: can't handle baud rate 0x%x",
1004 buf
[i
++] = DIGI_CMD_SET_BAUD_RATE
;
1005 buf
[i
++] = priv
->dp_port_num
;
1013 if( (cflag
&(PARENB
|PARODD
)) != (old_cflag
&(PARENB
|PARODD
)) ) {
1015 if( (cflag
&PARENB
) ) {
1016 if( (cflag
&PARODD
) )
1017 arg
= DIGI_PARITY_ODD
;
1019 arg
= DIGI_PARITY_EVEN
;
1021 arg
= DIGI_PARITY_NONE
;
1024 buf
[i
++] = DIGI_CMD_SET_PARITY
;
1025 buf
[i
++] = priv
->dp_port_num
;
1032 if( (cflag
&CSIZE
) != (old_cflag
&CSIZE
) ) {
1036 switch( (cflag
&CSIZE
) ) {
1037 case CS5
: arg
= DIGI_WORD_SIZE_5
; break;
1038 case CS6
: arg
= DIGI_WORD_SIZE_6
; break;
1039 case CS7
: arg
= DIGI_WORD_SIZE_7
; break;
1040 case CS8
: arg
= DIGI_WORD_SIZE_8
; break;
1042 dbg( "digi_set_termios: can't handle word size %d",
1048 buf
[i
++] = DIGI_CMD_SET_WORD_SIZE
;
1049 buf
[i
++] = priv
->dp_port_num
;
1057 if( (cflag
&CSTOPB
) != (old_cflag
&CSTOPB
) ) {
1059 if( (cflag
&CSTOPB
) )
1060 arg
= DIGI_STOP_BITS_2
;
1062 arg
= DIGI_STOP_BITS_1
;
1064 buf
[i
++] = DIGI_CMD_SET_STOP_BITS
;
1065 buf
[i
++] = priv
->dp_port_num
;
1071 /* set input flow control */
1072 if( (iflag
&IXOFF
) != (old_iflag
&IXOFF
)
1073 || (cflag
&CRTSCTS
) != (old_cflag
&CRTSCTS
) ) {
1078 arg
|= DIGI_INPUT_FLOW_CONTROL_XON_XOFF
;
1080 arg
&= ~DIGI_INPUT_FLOW_CONTROL_XON_XOFF
;
1082 if( (cflag
&CRTSCTS
) ) {
1084 arg
|= DIGI_INPUT_FLOW_CONTROL_RTS
;
1086 /* On USB-4 it is necessary to assert RTS prior */
1087 /* to selecting RTS input flow control. */
1088 buf
[i
++] = DIGI_CMD_SET_RTS_SIGNAL
;
1089 buf
[i
++] = priv
->dp_port_num
;
1090 buf
[i
++] = DIGI_RTS_ACTIVE
;
1094 arg
&= ~DIGI_INPUT_FLOW_CONTROL_RTS
;
1097 buf
[i
++] = DIGI_CMD_SET_INPUT_FLOW_CONTROL
;
1098 buf
[i
++] = priv
->dp_port_num
;
1104 /* set output flow control */
1105 if( (iflag
&IXON
) != (old_iflag
&IXON
)
1106 || (cflag
&CRTSCTS
) != (old_cflag
&CRTSCTS
) ) {
1111 arg
|= DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF
;
1113 arg
&= ~DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF
;
1115 if( (cflag
&CRTSCTS
) ) {
1116 arg
|= DIGI_OUTPUT_FLOW_CONTROL_CTS
;
1118 arg
&= ~DIGI_OUTPUT_FLOW_CONTROL_CTS
;
1119 port
->tty
->hw_stopped
= 0;
1122 buf
[i
++] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL
;
1123 buf
[i
++] = priv
->dp_port_num
;
1129 /* set receive enable/disable */
1130 if( (cflag
&CREAD
) != (old_cflag
&CREAD
) ) {
1137 buf
[i
++] = DIGI_CMD_RECEIVE_ENABLE
;
1138 buf
[i
++] = priv
->dp_port_num
;
1144 if( (ret
=digi_write_oob_command( port
, buf
, i
, 1 )) != 0 )
1145 dbg( "digi_set_termios: write oob failed, ret=%d", ret
);
1150 static void digi_break_ctl( struct usb_serial_port
*port
, int break_state
)
1153 unsigned char buf
[4];
1156 buf
[0] = DIGI_CMD_BREAK_CONTROL
;
1157 buf
[1] = 2; /* length */
1158 buf
[2] = break_state
? 1 : 0;
1159 buf
[3] = 0; /* pad */
1161 digi_write_inb_command( port
, buf
, 4, 0 );
1166 static int digi_ioctl( struct usb_serial_port
*port
, struct file
*file
,
1167 unsigned int cmd
, unsigned long arg
)
1170 digi_port_t
*priv
= (digi_port_t
*)(port
->private);
1172 unsigned long flags
= 0;
1175 dbg( "digi_ioctl: TOP: port=%d, cmd=0x%x", priv
->dp_port_num
, cmd
);
1180 spin_lock_irqsave( &priv
->dp_port_lock
, flags
);
1181 val
= priv
->dp_modem_signals
;
1182 spin_unlock_irqrestore( &priv
->dp_port_lock
, flags
);
1183 if( copy_to_user((unsigned int *)arg
, &val
, sizeof(int)) )
1190 if( copy_from_user(&val
, (unsigned int *)arg
, sizeof(int)) )
1192 spin_lock_irqsave( &priv
->dp_port_lock
, flags
);
1193 if( cmd
== TIOCMBIS
)
1194 val
= priv
->dp_modem_signals
| val
;
1195 else if( cmd
== TIOCMBIC
)
1196 val
= priv
->dp_modem_signals
& ~val
;
1197 spin_unlock_irqrestore( &priv
->dp_port_lock
, flags
);
1198 return( digi_set_modem_signals( port
, val
, 1 ) );
1201 /* wait for any of the 4 modem inputs (DCD,RI,DSR,CTS)*/
1206 /* return count of modemline transitions */
1212 return( -ENOIOCTLCMD
);
1217 static int digi_write( struct usb_serial_port
*port
, int from_user
,
1218 const unsigned char *buf
, int count
)
1221 int ret
,data_len
,new_len
;
1222 digi_port_t
*priv
= (digi_port_t
*)(port
->private);
1223 unsigned char *data
= port
->write_urb
->transfer_buffer
;
1224 unsigned char user_buf
[64]; /* 64 bytes is max USB bulk packet */
1225 unsigned long flags
= 0;
1228 dbg( "digi_write: TOP: port=%d, count=%d, from_user=%d, in_interrupt=%d",
1229 priv
->dp_port_num
, count
, from_user
, in_interrupt() );
1231 /* copy user data (which can sleep) before getting spin lock */
1232 count
= MIN( 64, MIN( count
, port
->bulk_out_size
-2 ) );
1233 if( from_user
&& copy_from_user( user_buf
, buf
, count
) ) {
1237 /* be sure only one write proceeds at a time */
1238 /* there are races on the port private buffer */
1239 /* and races to check write_urb->status */
1240 spin_lock_irqsave( &priv
->dp_port_lock
, flags
);
1242 /* wait for urb status clear to submit another urb */
1243 if( port
->write_urb
->status
== -EINPROGRESS
) {
1245 /* buffer data if count is 1 (probably put_char) if possible */
1247 new_len
= MIN( count
,
1248 DIGI_OUT_BUF_SIZE
-priv
->dp_out_buf_len
);
1249 memcpy( priv
->dp_out_buf
+priv
->dp_out_buf_len
, buf
,
1251 priv
->dp_out_buf_len
+= new_len
;
1256 spin_unlock_irqrestore( &priv
->dp_port_lock
, flags
);
1262 /* allow space for any buffered data and for new data, up to */
1263 /* transfer buffer size - 2 (for command and length bytes) */
1264 new_len
= MIN( count
, port
->bulk_out_size
-2-priv
->dp_out_buf_len
);
1265 data_len
= new_len
+ priv
->dp_out_buf_len
;
1267 if( data_len
== 0 ) {
1268 spin_unlock_irqrestore( &priv
->dp_port_lock
, flags
);
1272 port
->write_urb
->transfer_buffer_length
= data_len
+2;
1274 *data
++ = DIGI_CMD_SEND_DATA
;
1277 /* copy in buffered data first */
1278 memcpy( data
, priv
->dp_out_buf
, priv
->dp_out_buf_len
);
1279 data
+= priv
->dp_out_buf_len
;
1281 /* copy in new data */
1282 memcpy( data
, from_user
? user_buf
: buf
, new_len
);
1284 if( (ret
=usb_submit_urb(port
->write_urb
)) == 0 ) {
1286 priv
->dp_out_buf_len
= 0;
1289 /* return length of new data written, or error */
1290 spin_unlock_irqrestore( &priv
->dp_port_lock
, flags
);
1292 err( __FUNCTION__
": usb_submit_urb failed, ret=%d, port=%d",
1293 ret
, priv
->dp_port_num
);
1296 dbg( "digi_write: returning %d", ret
);
1302 static void digi_write_bulk_callback( struct urb
*urb
)
1305 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
1306 struct usb_serial
*serial
;
1311 dbg( "digi_write_bulk_callback: TOP, urb->status=%d", urb
->status
);
1313 /* port and serial sanity check */
1314 if( port
== NULL
|| (priv
=(digi_port_t
*)(port
->private)) == NULL
) {
1315 err( __FUNCTION__
": port or port->private is NULL, status=%d",
1319 serial
= port
->serial
;
1320 if( serial
== NULL
|| serial
->private == NULL
) {
1321 err( __FUNCTION__
": serial or serial->private is NULL, status=%d", urb
->status
);
1325 /* handle oob callback */
1326 if( priv
->dp_port_num
1327 == ((digi_serial_t
*)(serial
->private))->ds_oob_port_num
) {
1328 dbg( "digi_write_bulk_callback: oob callback" );
1329 spin_lock( &priv
->dp_port_lock
);
1330 wake_up_interruptible( &port
->write_wait
);
1331 spin_unlock( &priv
->dp_port_lock
);
1335 /* further sanity checks */
1336 if( port_paranoia_check( port
, __FUNCTION__
)
1337 || serial_paranoia_check( serial
, __FUNCTION__
) )
1340 /* try to send any buffered data on this port, if it is open */
1341 spin_lock( &priv
->dp_port_lock
);
1342 if( priv
->dp_open_count
&& port
->write_urb
->status
!= -EINPROGRESS
1343 && priv
->dp_out_buf_len
> 0 ) {
1345 *((unsigned char *)(port
->write_urb
->transfer_buffer
))
1346 = (unsigned char)DIGI_CMD_SEND_DATA
;
1347 *((unsigned char *)(port
->write_urb
->transfer_buffer
)+1)
1348 = (unsigned char)priv
->dp_out_buf_len
;
1350 port
->write_urb
->transfer_buffer_length
1351 = priv
->dp_out_buf_len
+2;
1353 memcpy( port
->write_urb
->transfer_buffer
+2, priv
->dp_out_buf
,
1354 priv
->dp_out_buf_len
);
1356 if( (ret
=usb_submit_urb(port
->write_urb
)) == 0 ) {
1357 priv
->dp_out_buf_len
= 0;
1362 /* wake up processes sleeping on writes immediately */
1363 digi_wakeup_write( port
);
1365 /* also queue up a wakeup at scheduler time, in case we */
1366 /* lost the race in write_chan(). */
1367 queue_task( &priv
->dp_wakeup_task
, &tq_scheduler
);
1369 spin_unlock( &priv
->dp_port_lock
);
1372 err( __FUNCTION__
": usb_submit_urb failed, ret=%d, port=%d",
1373 ret
, priv
->dp_port_num
);
1379 static int digi_write_room( struct usb_serial_port
*port
)
1383 digi_port_t
*priv
= (digi_port_t
*)(port
->private);
1384 unsigned long flags
= 0;
1387 spin_lock_irqsave( &priv
->dp_port_lock
, flags
);
1389 if( port
->write_urb
->status
== -EINPROGRESS
)
1392 room
= port
->bulk_out_size
- 2 - priv
->dp_out_buf_len
;
1394 spin_unlock_irqrestore( &priv
->dp_port_lock
, flags
);
1396 dbg( "digi_write_room: port=%d, room=%d", priv
->dp_port_num
, room
);
1402 static int digi_chars_in_buffer( struct usb_serial_port
*port
)
1405 digi_port_t
*priv
= (digi_port_t
*)(port
->private);
1408 if( port
->write_urb
->status
== -EINPROGRESS
) {
1409 dbg( "digi_chars_in_buffer: port=%d, chars=%d", priv
->dp_port_num
, port
->bulk_out_size
- 2 );
1410 /* return( port->bulk_out_size - 2 ); */
1413 dbg( "digi_chars_in_buffer: port=%d, chars=%d", priv
->dp_port_num
, priv
->dp_out_buf_len
);
1414 return( priv
->dp_out_buf_len
);
1420 static int digi_open( struct usb_serial_port
*port
, struct file
*filp
)
1424 unsigned char buf
[32];
1425 digi_port_t
*priv
= (digi_port_t
*)(port
->private);
1426 struct termios not_termios
;
1427 unsigned long flags
= 0;
1430 dbg( "digi_open: TOP: port=%d, active=%d, open_count=%d", priv
->dp_port_num
, port
->active
, priv
->dp_open_count
);
1432 /* be sure the device is started up */
1433 if( digi_startup_device( port
->serial
) != 0 )
1436 spin_lock_irqsave( &priv
->dp_port_lock
, flags
);
1438 /* don't wait on a close in progress for non-blocking opens */
1439 if( priv
->dp_in_close
&& (filp
->f_flags
&(O_NDELAY
|O_NONBLOCK
)) == 0 ) {
1440 spin_unlock_irqrestore( &priv
->dp_port_lock
, flags
);
1444 /* inc module use count before sleeping to wait for closes */
1445 ++priv
->dp_open_count
;
1448 /* wait for a close in progress to finish */
1449 while( priv
->dp_in_close
) {
1450 cond_wait_interruptible_timeout_irqrestore(
1451 &priv
->dp_close_wait
, DIGI_RETRY_TIMEOUT
,
1452 &priv
->dp_port_lock
, flags
);
1453 if( signal_pending(current
) ) {
1454 --priv
->dp_open_count
;
1458 spin_lock_irqsave( &priv
->dp_port_lock
, flags
);
1461 /* if port is already open, just return */
1462 /* be sure exactly one open proceeds */
1463 if( port
->active
) {
1464 spin_unlock_irqrestore( &priv
->dp_port_lock
, flags
);
1468 /* first open, mark port as active */
1470 spin_unlock_irqrestore( &priv
->dp_port_lock
, flags
);
1472 /* read modem signals automatically whenever they change */
1473 buf
[0] = DIGI_CMD_READ_INPUT_SIGNALS
;
1474 buf
[1] = priv
->dp_port_num
;
1475 buf
[2] = DIGI_ENABLE
;
1479 buf
[4] = DIGI_CMD_IFLUSH_FIFO
;
1480 buf
[5] = priv
->dp_port_num
;
1481 buf
[6] = DIGI_FLUSH_TX
| DIGI_FLUSH_RX
;
1484 if( (ret
=digi_write_oob_command( port
, buf
, 8, 1 )) != 0 )
1485 dbg( "digi_open: write oob failed, ret=%d", ret
);
1487 /* set termios settings */
1488 not_termios
.c_cflag
= ~port
->tty
->termios
->c_cflag
;
1489 not_termios
.c_iflag
= ~port
->tty
->termios
->c_iflag
;
1490 digi_set_termios( port
, ¬_termios
);
1492 /* set DTR and RTS */
1493 digi_set_modem_signals( port
, TIOCM_DTR
|TIOCM_RTS
, 1 );
1500 static void digi_close( struct usb_serial_port
*port
, struct file
*filp
)
1504 unsigned char buf
[32];
1505 struct tty_struct
*tty
= port
->tty
;
1506 digi_port_t
*priv
= (digi_port_t
*)port
->private;
1507 unsigned long flags
= 0;
1510 dbg( "digi_close: TOP: port=%d, active=%d, open_count=%d", priv
->dp_port_num
, port
->active
, priv
->dp_open_count
);
1513 /* do cleanup only after final close on this port */
1514 spin_lock_irqsave( &priv
->dp_port_lock
, flags
);
1515 if( priv
->dp_open_count
> 1 ) {
1516 --priv
->dp_open_count
;
1518 spin_unlock_irqrestore( &priv
->dp_port_lock
, flags
);
1520 } else if( priv
->dp_open_count
<= 0 ) {
1521 spin_unlock_irqrestore( &priv
->dp_port_lock
, flags
);
1524 priv
->dp_in_close
= 1;
1525 spin_unlock_irqrestore( &priv
->dp_port_lock
, flags
);
1527 /* tell line discipline to process only XON/XOFF */
1530 /* wait for output to drain */
1531 if( (filp
->f_flags
&(O_NDELAY
|O_NONBLOCK
)) == 0 ) {
1532 tty_wait_until_sent( tty
, DIGI_CLOSE_TIMEOUT
);
1535 /* flush driver and line discipline buffers */
1536 if( tty
->driver
.flush_buffer
)
1537 tty
->driver
.flush_buffer( tty
);
1538 if( tty
->ldisc
.flush_buffer
)
1539 tty
->ldisc
.flush_buffer( tty
);
1541 /* wait for transmit idle */
1542 if( (filp
->f_flags
&(O_NDELAY
|O_NONBLOCK
)) == 0 ) {
1543 digi_transmit_idle( port
, DIGI_CLOSE_TIMEOUT
);
1546 /* drop DTR and RTS */
1547 digi_set_modem_signals( port
, 0, 0 );
1549 /* disable input flow control */
1550 buf
[0] = DIGI_CMD_SET_INPUT_FLOW_CONTROL
;
1551 buf
[1] = priv
->dp_port_num
;
1552 buf
[2] = DIGI_DISABLE
;
1555 /* disable output flow control */
1556 buf
[4] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL
;
1557 buf
[5] = priv
->dp_port_num
;
1558 buf
[6] = DIGI_DISABLE
;
1561 /* disable reading modem signals automatically */
1562 buf
[8] = DIGI_CMD_READ_INPUT_SIGNALS
;
1563 buf
[9] = priv
->dp_port_num
;
1564 buf
[10] = DIGI_DISABLE
;
1567 /* disable receive */
1568 buf
[12] = DIGI_CMD_RECEIVE_ENABLE
;
1569 buf
[13] = priv
->dp_port_num
;
1570 buf
[14] = DIGI_DISABLE
;
1574 buf
[16] = DIGI_CMD_IFLUSH_FIFO
;
1575 buf
[17] = priv
->dp_port_num
;
1576 buf
[18] = DIGI_FLUSH_TX
| DIGI_FLUSH_RX
;
1579 if( (ret
=digi_write_oob_command( port
, buf
, 20, 0 )) != 0 )
1580 dbg( "digi_close: write oob failed, ret=%d", ret
);
1582 /* wait for final commands on oob port to complete */
1583 interruptible_sleep_on_timeout( &priv
->dp_flush_wait
,
1584 DIGI_CLOSE_TIMEOUT
);
1586 /* shutdown any outstanding bulk writes */
1587 usb_unlink_urb (port
->write_urb
);
1591 spin_lock_irqsave( &priv
->dp_port_lock
, flags
);
1593 priv
->dp_in_close
= 0;
1594 --priv
->dp_open_count
;
1596 wake_up_interruptible( &priv
->dp_close_wait
);
1597 spin_unlock_irqrestore( &priv
->dp_port_lock
, flags
);
1599 dbg( "digi_close: done" );
1604 * Digi Startup Device
1606 * Starts reads on all ports. Must be called AFTER startup, with
1607 * urbs initialized. Returns 0 if successful, non-zero error otherwise.
1610 static int digi_startup_device( struct usb_serial
*serial
)
1614 digi_serial_t
*serial_priv
= (digi_serial_t
*)serial
->private;
1615 struct usb_serial_port
*port
;
1618 /* be sure this happens exactly once */
1619 spin_lock( &serial_priv
->ds_serial_lock
);
1620 if( serial_priv
->ds_device_started
) {
1621 spin_unlock( &serial_priv
->ds_serial_lock
);
1624 serial_priv
->ds_device_started
= 1;
1625 spin_unlock( &serial_priv
->ds_serial_lock
);
1627 /* start reading from each bulk in endpoint for the device */
1628 /* set USB_DISABLE_SPD flag for write bulk urbs */
1629 for( i
=0; i
<serial
->type
->num_ports
+1; i
++ ) {
1631 port
= &serial
->port
[i
];
1633 port
->write_urb
->transfer_flags
|= USB_DISABLE_SPD
;
1635 if( (ret
=usb_submit_urb(port
->read_urb
)) != 0 ) {
1637 __FUNCTION__
": usb_submit_urb failed, ret=%d, port=%d",
1649 static int digi_startup( struct usb_serial
*serial
)
1654 digi_serial_t
*serial_priv
;
1657 dbg( "digi_startup: TOP" );
1659 /* allocate the private data structures for all ports */
1660 /* number of regular ports + 1 for the out-of-band port */
1661 for( i
=0; i
<serial
->type
->num_ports
+1; i
++ ) {
1663 serial
->port
[i
].active
= 0;
1665 /* allocate port private structure */
1666 priv
= serial
->port
[i
].private =
1667 (digi_port_t
*)kmalloc( sizeof(digi_port_t
),
1669 if( priv
== (digi_port_t
*)0 ) {
1671 kfree( serial
->port
[i
].private );
1672 return( 1 ); /* error */
1675 /* initialize port private structure */
1676 spin_lock_init( &priv
->dp_port_lock
);
1677 priv
->dp_port_num
= i
;
1678 priv
->dp_out_buf_len
= 0;
1679 priv
->dp_in_buf_len
= 0;
1680 priv
->dp_modem_signals
= 0;
1681 init_waitqueue_head( &priv
->dp_modem_change_wait
);
1682 priv
->dp_open_count
= 0;
1683 priv
->dp_transmit_idle
= 0;
1684 init_waitqueue_head( &priv
->dp_transmit_idle_wait
);
1685 priv
->dp_throttled
= 0;
1686 priv
->dp_throttle_restart
= 0;
1687 init_waitqueue_head( &priv
->dp_flush_wait
);
1688 priv
->dp_in_close
= 0;
1689 init_waitqueue_head( &priv
->dp_close_wait
);
1690 priv
->dp_wakeup_task
.next
= NULL
;
1691 priv
->dp_wakeup_task
.sync
= 0;
1692 priv
->dp_wakeup_task
.routine
= (void *)digi_wakeup_write_lock
;
1693 priv
->dp_wakeup_task
.data
= (void *)(&serial
->port
[i
]);
1695 /* initialize write wait queue for this port */
1696 init_waitqueue_head( &serial
->port
[i
].write_wait
);
1700 /* allocate serial private structure */
1701 serial_priv
= serial
->private =
1702 (digi_serial_t
*)kmalloc( sizeof(digi_serial_t
),
1704 if( serial_priv
== (digi_serial_t
*)0 ) {
1705 for( i
=0; i
<serial
->type
->num_ports
+1; i
++ )
1706 kfree( serial
->port
[i
].private );
1707 return( 1 ); /* error */
1710 /* initialize serial private structure */
1711 spin_lock_init( &serial_priv
->ds_serial_lock
);
1712 serial_priv
->ds_oob_port_num
= serial
->type
->num_ports
;
1713 serial_priv
->ds_oob_port
= &serial
->port
[serial_priv
->ds_oob_port_num
];
1714 serial_priv
->ds_device_started
= 0;
1721 static void digi_shutdown( struct usb_serial
*serial
)
1726 unsigned long flags
;
1729 dbg( "digi_shutdown: TOP, in_interrupt()=%d", in_interrupt() );
1731 /* stop reads and writes on all ports */
1732 for( i
=0; i
<serial
->type
->num_ports
+1; i
++ ) {
1733 usb_unlink_urb( serial
->port
[i
].read_urb
);
1734 usb_unlink_urb( serial
->port
[i
].write_urb
);
1737 /* dec module use count */
1738 for( i
=0; i
<serial
->type
->num_ports
; i
++ ) {
1739 priv
= serial
->port
[i
].private;
1740 spin_lock_irqsave( &priv
->dp_port_lock
, flags
);
1741 while( priv
->dp_open_count
> 0 ) {
1743 --priv
->dp_open_count
;
1745 spin_unlock_irqrestore( &priv
->dp_port_lock
, flags
);
1748 /* free the private data structures for all ports */
1749 /* number of regular ports + 1 for the out-of-band port */
1750 for( i
=0; i
<serial
->type
->num_ports
+1; i
++ )
1751 kfree( serial
->port
[i
].private );
1752 kfree( serial
->private );
1757 static void digi_read_bulk_callback( struct urb
*urb
)
1760 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
1765 dbg( "digi_read_bulk_callback: TOP" );
1767 /* port sanity check, do not resubmit if port is not valid */
1768 if( port
== NULL
|| (priv
=(digi_port_t
*)(port
->private)) == NULL
) {
1769 err( __FUNCTION__
": port or port->private is NULL, status=%d",
1773 if( port
->serial
== NULL
1774 || serial_paranoia_check( port
->serial
, __FUNCTION__
)
1775 || port
->serial
->private == NULL
) {
1776 err( __FUNCTION__
": serial is bad or serial->private is NULL, status=%d", urb
->status
);
1780 /* do not resubmit urb if it has any status error */
1782 err( __FUNCTION__
": nonzero read bulk status: status=%d, port=%d", urb
->status
, priv
->dp_port_num
);
1786 /* handle oob or inb callback, do not resubmit if error */
1787 if( priv
->dp_port_num
1788 == ((digi_serial_t
*)(port
->serial
->private))->ds_oob_port_num
) {
1789 if( digi_read_oob_callback( urb
) != 0 )
1792 if( digi_read_inb_callback( urb
) != 0 )
1797 if( (ret
=usb_submit_urb(urb
)) != 0 ) {
1798 err( __FUNCTION__
": failed resubmitting urb, ret=%d, port=%d",
1799 ret
, priv
->dp_port_num
);
1806 * Digi Read INB Callback
1808 * Digi Read INB Callback handles reads on the in band ports, sending
1809 * the data on to the tty subsystem. When called we know port and
1810 * port->private are not NULL and port->serial has been validated.
1811 * It returns 0 if successful, 1 if successful but the port is
1812 * throttled, and -1 if the sanity checks failed.
1815 static int digi_read_inb_callback( struct urb
*urb
)
1818 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
1819 struct tty_struct
*tty
= port
->tty
;
1820 digi_port_t
*priv
= (digi_port_t
*)(port
->private);
1821 int opcode
= ((unsigned char *)urb
->transfer_buffer
)[0];
1822 int len
= ((unsigned char *)urb
->transfer_buffer
)[1];
1823 int status
= ((unsigned char *)urb
->transfer_buffer
)[2];
1824 unsigned char *data
= ((unsigned char *)urb
->transfer_buffer
)+3;
1829 if( port_paranoia_check( port
, __FUNCTION__
) )
1832 /* do not process callbacks on closed ports */
1833 /* but do continue the read chain */
1834 if( priv
->dp_open_count
== 0 )
1837 /* short/multiple packet check */
1838 if( urb
->actual_length
!= len
+ 2 ) {
1839 err( __FUNCTION__
": INCOMPLETE OR MULTIPLE PACKET, urb->status=%d, port=%d, opcode=%d, len=%d, actual_length=%d, status=%d", urb
->status
, priv
->dp_port_num
, opcode
, len
, urb
->actual_length
, status
);
1843 spin_lock( &priv
->dp_port_lock
);
1845 /* check for throttle; if set, do not resubmit read urb */
1846 /* indicate the read chain needs to be restarted on unthrottle */
1847 throttled
= priv
->dp_throttled
;
1849 priv
->dp_throttle_restart
= 1;
1852 if( opcode
== DIGI_CMD_RECEIVE_DATA
) {
1854 /* get flag from status */
1857 /* overrun is special, not associated with a char */
1858 if( status
& DIGI_OVERRUN_ERROR
) {
1859 tty_insert_flip_char( tty
, 0, TTY_OVERRUN
);
1862 /* break takes precedence over parity, */
1863 /* which takes precedence over framing errors */
1864 if( status
& DIGI_BREAK_ERROR
) {
1866 } else if( status
& DIGI_PARITY_ERROR
) {
1868 } else if( status
& DIGI_FRAMING_ERROR
) {
1872 /* data length is len-1 (one byte of len is status) */
1878 DIGI_IN_BUF_SIZE
- priv
->dp_in_buf_len
);
1881 memcpy( priv
->dp_in_buf
+ priv
->dp_in_buf_len
,
1883 memset( priv
->dp_in_flag_buf
1884 + priv
->dp_in_buf_len
, flag
, len
);
1885 priv
->dp_in_buf_len
+= len
;
1890 len
= MIN( len
, TTY_FLIPBUF_SIZE
- tty
->flip
.count
);
1893 memcpy( tty
->flip
.char_buf_ptr
, data
, len
);
1894 memset( tty
->flip
.flag_buf_ptr
, flag
, len
);
1895 tty
->flip
.char_buf_ptr
+= len
;
1896 tty
->flip
.flag_buf_ptr
+= len
;
1897 tty
->flip
.count
+= len
;
1898 tty_flip_buffer_push( tty
);
1905 spin_unlock( &priv
->dp_port_lock
);
1907 if( opcode
== DIGI_CMD_RECEIVE_DISABLE
) {
1908 dbg( __FUNCTION__
": got RECEIVE_DISABLE" );
1909 } else if( opcode
!= DIGI_CMD_RECEIVE_DATA
) {
1910 dbg( __FUNCTION__
": unknown opcode: %d", opcode
);
1913 return( throttled
? 1 : 0 );
1919 * Digi Read OOB Callback
1921 * Digi Read OOB Callback handles reads on the out of band port.
1922 * When called we know port and port->private are not NULL and
1923 * the port->serial is valid. It returns 0 if successful, and
1924 * -1 if the sanity checks failed.
1927 static int digi_read_oob_callback( struct urb
*urb
)
1930 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
1931 struct usb_serial
*serial
= port
->serial
;
1932 digi_port_t
*priv
= (digi_port_t
*)(port
->private);
1933 int opcode
, line
, status
, val
;
1937 dbg( "digi_read_oob_callback: port=%d, len=%d", priv
->dp_port_num
,
1938 urb
->actual_length
);
1940 /* handle each oob command */
1941 for( i
=0; i
<urb
->actual_length
-3; ) {
1943 opcode
= ((unsigned char *)urb
->transfer_buffer
)[i
++];
1944 line
= ((unsigned char *)urb
->transfer_buffer
)[i
++];
1945 status
= ((unsigned char *)urb
->transfer_buffer
)[i
++];
1946 val
= ((unsigned char *)urb
->transfer_buffer
)[i
++];
1948 dbg( "digi_read_oob_callback: opcode=%d, line=%d, status=%d, val=%d",
1949 opcode
, line
, status
, val
);
1951 if( status
!= 0 || line
>= serial
->type
->num_ports
)
1954 port
= &serial
->port
[line
];
1956 if( port_paranoia_check( port
, __FUNCTION__
)
1957 || (priv
=port
->private) == NULL
)
1960 if( opcode
== DIGI_CMD_READ_INPUT_SIGNALS
) {
1962 spin_lock( &priv
->dp_port_lock
);
1964 /* convert from digi flags to termiox flags */
1965 if( val
& DIGI_READ_INPUT_SIGNALS_CTS
) {
1966 priv
->dp_modem_signals
|= TIOCM_CTS
;
1967 /* port must be open to use tty struct */
1968 if( priv
->dp_open_count
1969 && port
->tty
->termios
->c_cflag
& CRTSCTS
) {
1970 port
->tty
->hw_stopped
= 0;
1971 digi_wakeup_write( port
);
1974 priv
->dp_modem_signals
&= ~TIOCM_CTS
;
1975 /* port must be open to use tty struct */
1976 if( priv
->dp_open_count
1977 && port
->tty
->termios
->c_cflag
& CRTSCTS
) {
1978 port
->tty
->hw_stopped
= 1;
1981 if( val
& DIGI_READ_INPUT_SIGNALS_DSR
)
1982 priv
->dp_modem_signals
|= TIOCM_DSR
;
1984 priv
->dp_modem_signals
&= ~TIOCM_DSR
;
1985 if( val
& DIGI_READ_INPUT_SIGNALS_RI
)
1986 priv
->dp_modem_signals
|= TIOCM_RI
;
1988 priv
->dp_modem_signals
&= ~TIOCM_RI
;
1989 if( val
& DIGI_READ_INPUT_SIGNALS_DCD
)
1990 priv
->dp_modem_signals
|= TIOCM_CD
;
1992 priv
->dp_modem_signals
&= ~TIOCM_CD
;
1994 wake_up_interruptible( &priv
->dp_modem_change_wait
);
1995 spin_unlock( &priv
->dp_port_lock
);
1997 } else if( opcode
== DIGI_CMD_TRANSMIT_IDLE
) {
1999 spin_lock( &priv
->dp_port_lock
);
2000 priv
->dp_transmit_idle
= 1;
2001 wake_up_interruptible( &priv
->dp_transmit_idle_wait
);
2002 spin_unlock( &priv
->dp_port_lock
);
2004 } else if( opcode
== DIGI_CMD_IFLUSH_FIFO
) {
2006 wake_up_interruptible( &priv
->dp_flush_wait
);
2017 static int __init
digi_init (void)
2019 usb_serial_register (&digi_acceleport_2_device
);
2020 usb_serial_register (&digi_acceleport_4_device
);
2025 static void __exit
digi_exit (void)
2027 usb_serial_deregister (&digi_acceleport_2_device
);
2028 usb_serial_deregister (&digi_acceleport_4_device
);
2032 module_init(digi_init
);
2033 module_exit(digi_exit
);
2036 MODULE_AUTHOR("Peter Berger <pberger@brimson.com>, Al Borchers <borchers@steinerpoint.com>");
2037 MODULE_DESCRIPTION("Digi AccelePort USB-4 Serial Converter driver");