xref: /openbmc/linux/drivers/usb/serial/digi_acceleport.c (revision 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2)
1*1da177e4SLinus Torvalds /*
2*1da177e4SLinus Torvalds *  Digi AccelePort USB-4 and USB-2 Serial Converters
3*1da177e4SLinus Torvalds *
4*1da177e4SLinus Torvalds *  Copyright 2000 by Digi International
5*1da177e4SLinus Torvalds *
6*1da177e4SLinus Torvalds *  This program is free software; you can redistribute it and/or modify
7*1da177e4SLinus Torvalds *  it under the terms of the GNU General Public License as published by
8*1da177e4SLinus Torvalds *  the Free Software Foundation; either version 2 of the License, or
9*1da177e4SLinus Torvalds *  (at your option) any later version.
10*1da177e4SLinus Torvalds *
11*1da177e4SLinus Torvalds *  Shamelessly based on Brian Warner's keyspan_pda.c and Greg Kroah-Hartman's
12*1da177e4SLinus Torvalds *  usb-serial driver.
13*1da177e4SLinus Torvalds *
14*1da177e4SLinus Torvalds *  Peter Berger (pberger@brimson.com)
15*1da177e4SLinus Torvalds *  Al Borchers (borchers@steinerpoint.com)
16*1da177e4SLinus Torvalds *
17*1da177e4SLinus Torvalds * (12/03/2001) gkh
18*1da177e4SLinus Torvalds *	switched to using port->open_count instead of private version.
19*1da177e4SLinus Torvalds *	Removed port->active
20*1da177e4SLinus Torvalds *
21*1da177e4SLinus Torvalds * (04/08/2001) gb
22*1da177e4SLinus Torvalds *	Identify version on module load.
23*1da177e4SLinus Torvalds *
24*1da177e4SLinus Torvalds * (11/01/2000) Adam J. Richter
25*1da177e4SLinus Torvalds *	usb_device_id table support
26*1da177e4SLinus Torvalds *
27*1da177e4SLinus Torvalds * (11/01/2000) pberger and borchers
28*1da177e4SLinus Torvalds *    -- Turned off the USB_DISABLE_SPD flag for write bulk urbs--it caused
29*1da177e4SLinus Torvalds *       USB 4 ports to hang on startup.
30*1da177e4SLinus Torvalds *    -- Serialized access to write urbs by adding the dp_write_urb_in_use
31*1da177e4SLinus Torvalds *       flag; otherwise, the driver caused SMP system hangs.  Watching the
32*1da177e4SLinus Torvalds *       urb status is not sufficient.
33*1da177e4SLinus Torvalds *
34*1da177e4SLinus Torvalds * (10/05/2000) gkh
35*1da177e4SLinus Torvalds *    -- Fixed bug with urb->dev not being set properly, now that the usb
36*1da177e4SLinus Torvalds *	core needs it.
37*1da177e4SLinus Torvalds *
38*1da177e4SLinus Torvalds *  (8/8/2000) pberger and borchers
39*1da177e4SLinus Torvalds *    -- Fixed close so that
40*1da177e4SLinus Torvalds *       - it can timeout while waiting for transmit idle, if needed;
41*1da177e4SLinus Torvalds *       - it ignores interrupts when flushing the port, turning
42*1da177e4SLinus Torvalds *         of modem signalling, and so on;
43*1da177e4SLinus Torvalds *       - it waits for the flush to really complete before returning.
44*1da177e4SLinus Torvalds *    -- Read_bulk_callback and write_bulk_callback check for a closed
45*1da177e4SLinus Torvalds *       port before using the tty struct or writing to the port.
46*1da177e4SLinus Torvalds *    -- The two changes above fix the oops caused by interrupted closes.
47*1da177e4SLinus Torvalds *    -- Added interruptible args to write_oob_command and set_modem_signals
48*1da177e4SLinus Torvalds *       and added a timeout arg to transmit_idle; needed for fixes to
49*1da177e4SLinus Torvalds *       close.
50*1da177e4SLinus Torvalds *    -- Added code for rx_throttle and rx_unthrottle so that input flow
51*1da177e4SLinus Torvalds *       control works.
52*1da177e4SLinus Torvalds *    -- Added code to set overrun, parity, framing, and break errors
53*1da177e4SLinus Torvalds *       (untested).
54*1da177e4SLinus Torvalds *    -- Set USB_DISABLE_SPD flag for write bulk urbs, so no 0 length
55*1da177e4SLinus Torvalds *       bulk writes are done.  These hung the Digi USB device.  The
56*1da177e4SLinus Torvalds *       0 length bulk writes were a new feature of usb-uhci added in
57*1da177e4SLinus Torvalds *       the 2.4.0-test6 kernels.
58*1da177e4SLinus Torvalds *    -- Fixed mod inc race in open; do mod inc before sleeping to wait
59*1da177e4SLinus Torvalds *       for a close to finish.
60*1da177e4SLinus Torvalds *
61*1da177e4SLinus Torvalds *  (7/31/2000) pberger
62*1da177e4SLinus Torvalds *    -- Fixed bugs with hardware handshaking:
63*1da177e4SLinus Torvalds *       - Added code to set/clear tty->hw_stopped in digi_read_oob_callback()
64*1da177e4SLinus Torvalds *         and digi_set_termios()
65*1da177e4SLinus Torvalds *    -- Added code in digi_set_termios() to
66*1da177e4SLinus Torvalds *       - add conditional in code handling transition from B0 to only
67*1da177e4SLinus Torvalds *         set RTS if RTS/CTS flow control is either not in use or if
68*1da177e4SLinus Torvalds *         the port is not currently throttled.
69*1da177e4SLinus Torvalds *       - handle turning off CRTSCTS.
70*1da177e4SLinus Torvalds *
71*1da177e4SLinus Torvalds *  (7/30/2000) borchers
72*1da177e4SLinus Torvalds *    -- Added support for more than one Digi USB device by moving
73*1da177e4SLinus Torvalds *       globals to a private structure in the pointed to from the
74*1da177e4SLinus Torvalds *       usb_serial structure.
75*1da177e4SLinus Torvalds *    -- Moved the modem change and transmit idle wait queues into
76*1da177e4SLinus Torvalds *       the port private structure, so each port has its own queue
77*1da177e4SLinus Torvalds *       rather than sharing global queues.
78*1da177e4SLinus Torvalds *    -- Added support for break signals.
79*1da177e4SLinus Torvalds *
80*1da177e4SLinus Torvalds *  (7/25/2000) pberger
81*1da177e4SLinus Torvalds *    -- Added USB-2 support.  Note: the USB-2 supports 3 devices: two
82*1da177e4SLinus Torvalds *       serial and a parallel port.  The parallel port is implemented
83*1da177e4SLinus Torvalds *       as a serial-to-parallel converter.  That is, the driver actually
84*1da177e4SLinus Torvalds *       presents all three USB-2 interfaces as serial ports, but the third
85*1da177e4SLinus Torvalds *       one physically connects to a parallel device.  Thus, for example,
86*1da177e4SLinus Torvalds *       one could plug a parallel printer into the USB-2's third port,
87*1da177e4SLinus Torvalds *       but from the kernel's (and userland's) point of view what's
88*1da177e4SLinus Torvalds *       actually out there is a serial device.
89*1da177e4SLinus Torvalds *
90*1da177e4SLinus Torvalds *  (7/15/2000) borchers
91*1da177e4SLinus Torvalds *    -- Fixed race in open when a close is in progress.
92*1da177e4SLinus Torvalds *    -- Keep count of opens and dec the module use count for each
93*1da177e4SLinus Torvalds *       outstanding open when shutdown is called (on disconnect).
94*1da177e4SLinus Torvalds *    -- Fixed sanity checks in read_bulk_callback and write_bulk_callback
95*1da177e4SLinus Torvalds *       so pointers are checked before use.
96*1da177e4SLinus Torvalds *    -- Split read bulk callback into in band and out of band
97*1da177e4SLinus Torvalds *       callbacks, and no longer restart read chains if there is
98*1da177e4SLinus Torvalds *       a status error or a sanity error.  This fixed the seg
99*1da177e4SLinus Torvalds *       faults and other errors we used to get on disconnect.
100*1da177e4SLinus Torvalds *    -- Port->active is once again a flag as usb-serial intended it
101*1da177e4SLinus Torvalds *       to be, not a count.  Since it was only a char it would
102*1da177e4SLinus Torvalds *       have been limited to 256 simultaneous opens.  Now the open
103*1da177e4SLinus Torvalds *       count is kept in the port private structure in dp_open_count.
104*1da177e4SLinus Torvalds *    -- Added code for modularization of the digi_acceleport driver.
105*1da177e4SLinus Torvalds *
106*1da177e4SLinus Torvalds *  (6/27/2000) pberger and borchers
107*1da177e4SLinus Torvalds *    -- Zeroed out sync field in the wakeup_task before first use;
108*1da177e4SLinus Torvalds *       otherwise the uninitialized value might prevent the task from
109*1da177e4SLinus Torvalds *       being scheduled.
110*1da177e4SLinus Torvalds *    -- Initialized ret value to 0 in write_bulk_callback, otherwise
111*1da177e4SLinus Torvalds *       the uninitialized value could cause a spurious debugging message.
112*1da177e4SLinus Torvalds *
113*1da177e4SLinus Torvalds *  (6/22/2000) pberger and borchers
114*1da177e4SLinus Torvalds *    -- Made cond_wait_... inline--apparently on SPARC the flags arg
115*1da177e4SLinus Torvalds *       to spin_lock_irqsave cannot be passed to another function
116*1da177e4SLinus Torvalds *       to call spin_unlock_irqrestore.  Thanks to Pauline Middelink.
117*1da177e4SLinus Torvalds *    -- In digi_set_modem_signals the inner nested spin locks use just
118*1da177e4SLinus Torvalds *       spin_lock() rather than spin_lock_irqsave().  The old code
119*1da177e4SLinus Torvalds *       mistakenly left interrupts off.  Thanks to Pauline Middelink.
120*1da177e4SLinus Torvalds *    -- copy_from_user (which can sleep) is no longer called while a
121*1da177e4SLinus Torvalds *       spinlock is held.  We copy to a local buffer before getting
122*1da177e4SLinus Torvalds *       the spinlock--don't like the extra copy but the code is simpler.
123*1da177e4SLinus Torvalds *    -- Printk and dbg are no longer called while a spin lock is held.
124*1da177e4SLinus Torvalds *
125*1da177e4SLinus Torvalds *  (6/4/2000) pberger and borchers
126*1da177e4SLinus Torvalds *    -- Replaced separate calls to spin_unlock_irqrestore and
127*1da177e4SLinus Torvalds *       interruptible_sleep_on_timeout with a new function
128*1da177e4SLinus Torvalds *       cond_wait_interruptible_timeout_irqrestore.  This eliminates
129*1da177e4SLinus Torvalds *       the race condition where the wake up could happen after
130*1da177e4SLinus Torvalds *       the unlock and before the sleep.
131*1da177e4SLinus Torvalds *    -- Close now waits for output to drain.
132*1da177e4SLinus Torvalds *    -- Open waits until any close in progress is finished.
133*1da177e4SLinus Torvalds *    -- All out of band responses are now processed, not just the
134*1da177e4SLinus Torvalds *       first in a USB packet.
135*1da177e4SLinus Torvalds *    -- Fixed a bug that prevented the driver from working when the
136*1da177e4SLinus Torvalds *       first Digi port was not the first USB serial port--the driver
137*1da177e4SLinus Torvalds *       was mistakenly using the external USB serial port number to
138*1da177e4SLinus Torvalds *       try to index into its internal ports.
139*1da177e4SLinus Torvalds *    -- Fixed an SMP bug -- write_bulk_callback is called directly from
140*1da177e4SLinus Torvalds *       an interrupt, so spin_lock_irqsave/spin_unlock_irqrestore are
141*1da177e4SLinus Torvalds *       needed for locks outside write_bulk_callback that are also
142*1da177e4SLinus Torvalds *       acquired by write_bulk_callback to prevent deadlocks.
143*1da177e4SLinus Torvalds *    -- Fixed support for select() by making digi_chars_in_buffer()
144*1da177e4SLinus Torvalds *       return 256 when -EINPROGRESS is set, as the line discipline
145*1da177e4SLinus Torvalds *       code in n_tty.c expects.
146*1da177e4SLinus Torvalds *    -- Fixed an include file ordering problem that prevented debugging
147*1da177e4SLinus Torvalds *       messages from working.
148*1da177e4SLinus Torvalds *    -- Fixed an intermittent timeout problem that caused writes to
149*1da177e4SLinus Torvalds *       sometimes get stuck on some machines on some kernels.  It turns
150*1da177e4SLinus Torvalds *       out in these circumstances write_chan() (in n_tty.c) was
151*1da177e4SLinus Torvalds *       asleep waiting for our wakeup call.  Even though we call
152*1da177e4SLinus Torvalds *       wake_up_interruptible() in digi_write_bulk_callback(), there is
153*1da177e4SLinus Torvalds *       a race condition that could cause the wakeup to fail: if our
154*1da177e4SLinus Torvalds *       wake_up_interruptible() call occurs between the time that our
155*1da177e4SLinus Torvalds *       driver write routine finishes and write_chan() sets current->state
156*1da177e4SLinus Torvalds *       to TASK_INTERRUPTIBLE, the effect of our wakeup setting the state
157*1da177e4SLinus Torvalds *       to TASK_RUNNING will be lost and write_chan's subsequent call to
158*1da177e4SLinus Torvalds *       schedule() will never return (unless it catches a signal).
159*1da177e4SLinus Torvalds *       This race condition occurs because write_bulk_callback() (and thus
160*1da177e4SLinus Torvalds *       the wakeup) are called asynchonously from an interrupt, rather than
161*1da177e4SLinus Torvalds *       from the scheduler.  We can avoid the race by calling the wakeup
162*1da177e4SLinus Torvalds *       from the scheduler queue and that's our fix:  Now, at the end of
163*1da177e4SLinus Torvalds *       write_bulk_callback() we queue up a wakeup call on the scheduler
164*1da177e4SLinus Torvalds *       task queue.  We still also invoke the wakeup directly since that
165*1da177e4SLinus Torvalds *       squeezes a bit more performance out of the driver, and any lost
166*1da177e4SLinus Torvalds *       race conditions will get cleaned up at the next scheduler run.
167*1da177e4SLinus Torvalds *
168*1da177e4SLinus Torvalds *       NOTE:  The problem also goes away if you comment out
169*1da177e4SLinus Torvalds *       the two code lines in write_chan() where current->state
170*1da177e4SLinus Torvalds *       is set to TASK_RUNNING just before calling driver.write() and to
171*1da177e4SLinus Torvalds *       TASK_INTERRUPTIBLE immediately afterwards.  This is why the
172*1da177e4SLinus Torvalds *       problem did not show up with the 2.2 kernels -- they do not
173*1da177e4SLinus Torvalds *       include that code.
174*1da177e4SLinus Torvalds *
175*1da177e4SLinus Torvalds *  (5/16/2000) pberger and borchers
176*1da177e4SLinus Torvalds *    -- Added timeouts to sleeps, to defend against lost wake ups.
177*1da177e4SLinus Torvalds *    -- Handle transition to/from B0 baud rate in digi_set_termios.
178*1da177e4SLinus Torvalds *
179*1da177e4SLinus Torvalds *  (5/13/2000) pberger and borchers
180*1da177e4SLinus Torvalds *    -- All commands now sent on out of band port, using
181*1da177e4SLinus Torvalds *       digi_write_oob_command.
182*1da177e4SLinus Torvalds *    -- Get modem control signals whenever they change, support TIOCMGET/
183*1da177e4SLinus Torvalds *       SET/BIS/BIC ioctls.
184*1da177e4SLinus Torvalds *    -- digi_set_termios now supports parity, word size, stop bits, and
185*1da177e4SLinus Torvalds *       receive enable.
186*1da177e4SLinus Torvalds *    -- Cleaned up open and close, use digi_set_termios and
187*1da177e4SLinus Torvalds *       digi_write_oob_command to set port parameters.
188*1da177e4SLinus Torvalds *    -- Added digi_startup_device to start read chains on all ports.
189*1da177e4SLinus Torvalds *    -- Write buffer is only used when count==1, to be sure put_char can
190*1da177e4SLinus Torvalds *       write a char (unless the buffer is full).
191*1da177e4SLinus Torvalds *
192*1da177e4SLinus Torvalds *  (5/10/2000) pberger and borchers
193*1da177e4SLinus Torvalds *    -- Added MOD_INC_USE_COUNT/MOD_DEC_USE_COUNT calls on open/close.
194*1da177e4SLinus Torvalds *    -- Fixed problem where the first incoming character is lost on
195*1da177e4SLinus Torvalds *       port opens after the first close on that port.  Now we keep
196*1da177e4SLinus Torvalds *       the read_urb chain open until shutdown.
197*1da177e4SLinus Torvalds *    -- Added more port conditioning calls in digi_open and digi_close.
198*1da177e4SLinus Torvalds *    -- Convert port->active to a use count so that we can deal with multiple
199*1da177e4SLinus Torvalds *       opens and closes properly.
200*1da177e4SLinus Torvalds *    -- Fixed some problems with the locking code.
201*1da177e4SLinus Torvalds *
202*1da177e4SLinus Torvalds *  (5/3/2000) pberger and borchers
203*1da177e4SLinus Torvalds *    -- First alpha version of the driver--many known limitations and bugs.
204*1da177e4SLinus Torvalds *
205*1da177e4SLinus Torvalds *
206*1da177e4SLinus Torvalds *  Locking and SMP
207*1da177e4SLinus Torvalds *
208*1da177e4SLinus Torvalds *  - Each port, including the out-of-band port, has a lock used to
209*1da177e4SLinus Torvalds *    serialize all access to the port's private structure.
210*1da177e4SLinus Torvalds *  - The port lock is also used to serialize all writes and access to
211*1da177e4SLinus Torvalds *    the port's URB.
212*1da177e4SLinus Torvalds *  - The port lock is also used for the port write_wait condition
213*1da177e4SLinus Torvalds *    variable.  Holding the port lock will prevent a wake up on the
214*1da177e4SLinus Torvalds *    port's write_wait; this can be used with cond_wait_... to be sure
215*1da177e4SLinus Torvalds *    the wake up is not lost in a race when dropping the lock and
216*1da177e4SLinus Torvalds *    sleeping waiting for the wakeup.
217*1da177e4SLinus Torvalds *  - digi_write() does not sleep, since it is sometimes called on
218*1da177e4SLinus Torvalds *    interrupt time.
219*1da177e4SLinus Torvalds *  - digi_write_bulk_callback() and digi_read_bulk_callback() are
220*1da177e4SLinus Torvalds *    called directly from interrupts.  Hence spin_lock_irqsave()
221*1da177e4SLinus Torvalds *    and spin_unlock_irqrestore() are used in the rest of the code
222*1da177e4SLinus Torvalds *    for any locks they acquire.
223*1da177e4SLinus Torvalds *  - digi_write_bulk_callback() gets the port lock before waking up
224*1da177e4SLinus Torvalds *    processes sleeping on the port write_wait.  It also schedules
225*1da177e4SLinus Torvalds *    wake ups so they happen from the scheduler, because the tty
226*1da177e4SLinus Torvalds *    system can miss wake ups from interrupts.
227*1da177e4SLinus Torvalds *  - All sleeps use a timeout of DIGI_RETRY_TIMEOUT before looping to
228*1da177e4SLinus Torvalds *    recheck the condition they are sleeping on.  This is defensive,
229*1da177e4SLinus Torvalds *    in case a wake up is lost.
230*1da177e4SLinus Torvalds *  - Following Documentation/DocBook/kernel-locking.pdf no spin locks
231*1da177e4SLinus Torvalds *    are held when calling copy_to/from_user or printk.
232*1da177e4SLinus Torvalds *
233*1da177e4SLinus Torvalds *  $Id: digi_acceleport.c,v 1.80.1.2 2000/11/02 05:45:08 root Exp $
234*1da177e4SLinus Torvalds */
235*1da177e4SLinus Torvalds 
236*1da177e4SLinus Torvalds #include <linux/config.h>
237*1da177e4SLinus Torvalds #include <linux/kernel.h>
238*1da177e4SLinus Torvalds #include <linux/errno.h>
239*1da177e4SLinus Torvalds #include <linux/init.h>
240*1da177e4SLinus Torvalds #include <linux/slab.h>
241*1da177e4SLinus Torvalds #include <linux/tty.h>
242*1da177e4SLinus Torvalds #include <linux/tty_driver.h>
243*1da177e4SLinus Torvalds #include <linux/tty_flip.h>
244*1da177e4SLinus Torvalds #include <linux/module.h>
245*1da177e4SLinus Torvalds #include <linux/spinlock.h>
246*1da177e4SLinus Torvalds #include <linux/workqueue.h>
247*1da177e4SLinus Torvalds #include <asm/uaccess.h>
248*1da177e4SLinus Torvalds #include <linux/usb.h>
249*1da177e4SLinus Torvalds #include <linux/wait.h>
250*1da177e4SLinus Torvalds #include "usb-serial.h"
251*1da177e4SLinus Torvalds 
252*1da177e4SLinus Torvalds /* Defines */
253*1da177e4SLinus Torvalds 
254*1da177e4SLinus Torvalds /*
255*1da177e4SLinus Torvalds  * Version Information
256*1da177e4SLinus Torvalds  */
257*1da177e4SLinus Torvalds #define DRIVER_VERSION "v1.80.1.2"
258*1da177e4SLinus Torvalds #define DRIVER_AUTHOR "Peter Berger <pberger@brimson.com>, Al Borchers <borchers@steinerpoint.com>"
259*1da177e4SLinus Torvalds #define DRIVER_DESC "Digi AccelePort USB-2/USB-4 Serial Converter driver"
260*1da177e4SLinus Torvalds 
261*1da177e4SLinus Torvalds /* port output buffer length -- must be <= transfer buffer length - 2 */
262*1da177e4SLinus Torvalds /* so we can be sure to send the full buffer in one urb */
263*1da177e4SLinus Torvalds #define DIGI_OUT_BUF_SIZE		8
264*1da177e4SLinus Torvalds 
265*1da177e4SLinus Torvalds /* port input buffer length -- must be >= transfer buffer length - 3 */
266*1da177e4SLinus Torvalds /* so we can be sure to hold at least one full buffer from one urb */
267*1da177e4SLinus Torvalds #define DIGI_IN_BUF_SIZE		64
268*1da177e4SLinus Torvalds 
269*1da177e4SLinus Torvalds /* retry timeout while sleeping */
270*1da177e4SLinus Torvalds #define DIGI_RETRY_TIMEOUT		(HZ/10)
271*1da177e4SLinus Torvalds 
272*1da177e4SLinus Torvalds /* timeout while waiting for tty output to drain in close */
273*1da177e4SLinus Torvalds /* this delay is used twice in close, so the total delay could */
274*1da177e4SLinus Torvalds /* be twice this value */
275*1da177e4SLinus Torvalds #define DIGI_CLOSE_TIMEOUT		(5*HZ)
276*1da177e4SLinus Torvalds 
277*1da177e4SLinus Torvalds 
278*1da177e4SLinus Torvalds /* AccelePort USB Defines */
279*1da177e4SLinus Torvalds 
280*1da177e4SLinus Torvalds /* ids */
281*1da177e4SLinus Torvalds #define DIGI_VENDOR_ID			0x05c5
282*1da177e4SLinus Torvalds #define DIGI_2_ID			0x0002	/* USB-2 */
283*1da177e4SLinus Torvalds #define DIGI_4_ID			0x0004	/* USB-4 */
284*1da177e4SLinus Torvalds 
285*1da177e4SLinus Torvalds /* commands
286*1da177e4SLinus Torvalds  * "INB": can be used on the in-band endpoint
287*1da177e4SLinus Torvalds  * "OOB": can be used on the out-of-band endpoint
288*1da177e4SLinus Torvalds  */
289*1da177e4SLinus Torvalds #define DIGI_CMD_SET_BAUD_RATE			0	/* INB, OOB */
290*1da177e4SLinus Torvalds #define DIGI_CMD_SET_WORD_SIZE			1	/* INB, OOB */
291*1da177e4SLinus Torvalds #define DIGI_CMD_SET_PARITY			2	/* INB, OOB */
292*1da177e4SLinus Torvalds #define DIGI_CMD_SET_STOP_BITS			3	/* INB, OOB */
293*1da177e4SLinus Torvalds #define DIGI_CMD_SET_INPUT_FLOW_CONTROL		4	/* INB, OOB */
294*1da177e4SLinus Torvalds #define DIGI_CMD_SET_OUTPUT_FLOW_CONTROL	5	/* INB, OOB */
295*1da177e4SLinus Torvalds #define DIGI_CMD_SET_DTR_SIGNAL			6	/* INB, OOB */
296*1da177e4SLinus Torvalds #define DIGI_CMD_SET_RTS_SIGNAL			7	/* INB, OOB */
297*1da177e4SLinus Torvalds #define DIGI_CMD_READ_INPUT_SIGNALS		8	/*      OOB */
298*1da177e4SLinus Torvalds #define DIGI_CMD_IFLUSH_FIFO			9	/*      OOB */
299*1da177e4SLinus Torvalds #define DIGI_CMD_RECEIVE_ENABLE			10	/* INB, OOB */
300*1da177e4SLinus Torvalds #define DIGI_CMD_BREAK_CONTROL			11	/* INB, OOB */
301*1da177e4SLinus Torvalds #define DIGI_CMD_LOCAL_LOOPBACK			12	/* INB, OOB */
302*1da177e4SLinus Torvalds #define DIGI_CMD_TRANSMIT_IDLE			13	/* INB, OOB */
303*1da177e4SLinus Torvalds #define DIGI_CMD_READ_UART_REGISTER		14	/*      OOB */
304*1da177e4SLinus Torvalds #define DIGI_CMD_WRITE_UART_REGISTER		15	/* INB, OOB */
305*1da177e4SLinus Torvalds #define DIGI_CMD_AND_UART_REGISTER		16	/* INB, OOB */
306*1da177e4SLinus Torvalds #define DIGI_CMD_OR_UART_REGISTER		17	/* INB, OOB */
307*1da177e4SLinus Torvalds #define DIGI_CMD_SEND_DATA			18	/* INB      */
308*1da177e4SLinus Torvalds #define DIGI_CMD_RECEIVE_DATA			19	/* INB      */
309*1da177e4SLinus Torvalds #define DIGI_CMD_RECEIVE_DISABLE		20	/* INB      */
310*1da177e4SLinus Torvalds #define DIGI_CMD_GET_PORT_TYPE			21	/*      OOB */
311*1da177e4SLinus Torvalds 
312*1da177e4SLinus Torvalds /* baud rates */
313*1da177e4SLinus Torvalds #define DIGI_BAUD_50				0
314*1da177e4SLinus Torvalds #define DIGI_BAUD_75				1
315*1da177e4SLinus Torvalds #define DIGI_BAUD_110				2
316*1da177e4SLinus Torvalds #define DIGI_BAUD_150				3
317*1da177e4SLinus Torvalds #define DIGI_BAUD_200				4
318*1da177e4SLinus Torvalds #define DIGI_BAUD_300				5
319*1da177e4SLinus Torvalds #define DIGI_BAUD_600				6
320*1da177e4SLinus Torvalds #define DIGI_BAUD_1200				7
321*1da177e4SLinus Torvalds #define DIGI_BAUD_1800				8
322*1da177e4SLinus Torvalds #define DIGI_BAUD_2400				9
323*1da177e4SLinus Torvalds #define DIGI_BAUD_4800				10
324*1da177e4SLinus Torvalds #define DIGI_BAUD_7200				11
325*1da177e4SLinus Torvalds #define DIGI_BAUD_9600				12
326*1da177e4SLinus Torvalds #define DIGI_BAUD_14400				13
327*1da177e4SLinus Torvalds #define DIGI_BAUD_19200				14
328*1da177e4SLinus Torvalds #define DIGI_BAUD_28800				15
329*1da177e4SLinus Torvalds #define DIGI_BAUD_38400				16
330*1da177e4SLinus Torvalds #define DIGI_BAUD_57600				17
331*1da177e4SLinus Torvalds #define DIGI_BAUD_76800				18
332*1da177e4SLinus Torvalds #define DIGI_BAUD_115200			19
333*1da177e4SLinus Torvalds #define DIGI_BAUD_153600			20
334*1da177e4SLinus Torvalds #define DIGI_BAUD_230400			21
335*1da177e4SLinus Torvalds #define DIGI_BAUD_460800			22
336*1da177e4SLinus Torvalds 
337*1da177e4SLinus Torvalds /* arguments */
338*1da177e4SLinus Torvalds #define DIGI_WORD_SIZE_5			0
339*1da177e4SLinus Torvalds #define DIGI_WORD_SIZE_6			1
340*1da177e4SLinus Torvalds #define DIGI_WORD_SIZE_7			2
341*1da177e4SLinus Torvalds #define DIGI_WORD_SIZE_8			3
342*1da177e4SLinus Torvalds 
343*1da177e4SLinus Torvalds #define DIGI_PARITY_NONE			0
344*1da177e4SLinus Torvalds #define DIGI_PARITY_ODD				1
345*1da177e4SLinus Torvalds #define DIGI_PARITY_EVEN			2
346*1da177e4SLinus Torvalds #define DIGI_PARITY_MARK			3
347*1da177e4SLinus Torvalds #define DIGI_PARITY_SPACE			4
348*1da177e4SLinus Torvalds 
349*1da177e4SLinus Torvalds #define DIGI_STOP_BITS_1			0
350*1da177e4SLinus Torvalds #define DIGI_STOP_BITS_2			1
351*1da177e4SLinus Torvalds 
352*1da177e4SLinus Torvalds #define DIGI_INPUT_FLOW_CONTROL_XON_XOFF	1
353*1da177e4SLinus Torvalds #define DIGI_INPUT_FLOW_CONTROL_RTS		2
354*1da177e4SLinus Torvalds #define DIGI_INPUT_FLOW_CONTROL_DTR		4
355*1da177e4SLinus Torvalds 
356*1da177e4SLinus Torvalds #define DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF	1
357*1da177e4SLinus Torvalds #define DIGI_OUTPUT_FLOW_CONTROL_CTS		2
358*1da177e4SLinus Torvalds #define DIGI_OUTPUT_FLOW_CONTROL_DSR		4
359*1da177e4SLinus Torvalds 
360*1da177e4SLinus Torvalds #define DIGI_DTR_INACTIVE			0
361*1da177e4SLinus Torvalds #define DIGI_DTR_ACTIVE				1
362*1da177e4SLinus Torvalds #define DIGI_DTR_INPUT_FLOW_CONTROL		2
363*1da177e4SLinus Torvalds 
364*1da177e4SLinus Torvalds #define DIGI_RTS_INACTIVE			0
365*1da177e4SLinus Torvalds #define DIGI_RTS_ACTIVE				1
366*1da177e4SLinus Torvalds #define DIGI_RTS_INPUT_FLOW_CONTROL		2
367*1da177e4SLinus Torvalds #define DIGI_RTS_TOGGLE				3
368*1da177e4SLinus Torvalds 
369*1da177e4SLinus Torvalds #define DIGI_FLUSH_TX				1
370*1da177e4SLinus Torvalds #define DIGI_FLUSH_RX				2
371*1da177e4SLinus Torvalds #define DIGI_RESUME_TX				4 /* clears xoff condition */
372*1da177e4SLinus Torvalds 
373*1da177e4SLinus Torvalds #define DIGI_TRANSMIT_NOT_IDLE			0
374*1da177e4SLinus Torvalds #define DIGI_TRANSMIT_IDLE			1
375*1da177e4SLinus Torvalds 
376*1da177e4SLinus Torvalds #define DIGI_DISABLE				0
377*1da177e4SLinus Torvalds #define DIGI_ENABLE				1
378*1da177e4SLinus Torvalds 
379*1da177e4SLinus Torvalds #define DIGI_DEASSERT				0
380*1da177e4SLinus Torvalds #define DIGI_ASSERT				1
381*1da177e4SLinus Torvalds 
382*1da177e4SLinus Torvalds /* in band status codes */
383*1da177e4SLinus Torvalds #define DIGI_OVERRUN_ERROR			4
384*1da177e4SLinus Torvalds #define DIGI_PARITY_ERROR			8
385*1da177e4SLinus Torvalds #define DIGI_FRAMING_ERROR			16
386*1da177e4SLinus Torvalds #define DIGI_BREAK_ERROR			32
387*1da177e4SLinus Torvalds 
388*1da177e4SLinus Torvalds /* out of band status */
389*1da177e4SLinus Torvalds #define DIGI_NO_ERROR				0
390*1da177e4SLinus Torvalds #define DIGI_BAD_FIRST_PARAMETER		1
391*1da177e4SLinus Torvalds #define DIGI_BAD_SECOND_PARAMETER		2
392*1da177e4SLinus Torvalds #define DIGI_INVALID_LINE			3
393*1da177e4SLinus Torvalds #define DIGI_INVALID_OPCODE			4
394*1da177e4SLinus Torvalds 
395*1da177e4SLinus Torvalds /* input signals */
396*1da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_SLOT		1
397*1da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_ERR		2
398*1da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_BUSY		4
399*1da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_PE		8
400*1da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_CTS		16
401*1da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_DSR		32
402*1da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_RI		64
403*1da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_DCD		128
404*1da177e4SLinus Torvalds 
405*1da177e4SLinus Torvalds 
406*1da177e4SLinus Torvalds /* Structures */
407*1da177e4SLinus Torvalds 
408*1da177e4SLinus Torvalds struct digi_serial {
409*1da177e4SLinus Torvalds 	spinlock_t ds_serial_lock;
410*1da177e4SLinus Torvalds 	struct usb_serial_port *ds_oob_port;	/* out-of-band port */
411*1da177e4SLinus Torvalds 	int ds_oob_port_num;			/* index of out-of-band port */
412*1da177e4SLinus Torvalds 	int ds_device_started;
413*1da177e4SLinus Torvalds };
414*1da177e4SLinus Torvalds 
415*1da177e4SLinus Torvalds struct digi_port {
416*1da177e4SLinus Torvalds 	spinlock_t dp_port_lock;
417*1da177e4SLinus Torvalds 	int dp_port_num;
418*1da177e4SLinus Torvalds 	int dp_out_buf_len;
419*1da177e4SLinus Torvalds 	unsigned char dp_out_buf[DIGI_OUT_BUF_SIZE];
420*1da177e4SLinus Torvalds 	int dp_in_buf_len;
421*1da177e4SLinus Torvalds 	unsigned char dp_in_buf[DIGI_IN_BUF_SIZE];
422*1da177e4SLinus Torvalds 	unsigned char dp_in_flag_buf[DIGI_IN_BUF_SIZE];
423*1da177e4SLinus Torvalds 	int dp_write_urb_in_use;
424*1da177e4SLinus Torvalds 	unsigned int dp_modem_signals;
425*1da177e4SLinus Torvalds 	wait_queue_head_t dp_modem_change_wait;
426*1da177e4SLinus Torvalds 	int dp_transmit_idle;
427*1da177e4SLinus Torvalds 	wait_queue_head_t dp_transmit_idle_wait;
428*1da177e4SLinus Torvalds 	int dp_throttled;
429*1da177e4SLinus Torvalds 	int dp_throttle_restart;
430*1da177e4SLinus Torvalds 	wait_queue_head_t dp_flush_wait;
431*1da177e4SLinus Torvalds 	int dp_in_close;			/* close in progress */
432*1da177e4SLinus Torvalds 	wait_queue_head_t dp_close_wait;	/* wait queue for close */
433*1da177e4SLinus Torvalds 	struct work_struct dp_wakeup_work;
434*1da177e4SLinus Torvalds };
435*1da177e4SLinus Torvalds 
436*1da177e4SLinus Torvalds 
437*1da177e4SLinus Torvalds /* Local Function Declarations */
438*1da177e4SLinus Torvalds 
439*1da177e4SLinus Torvalds static void digi_wakeup_write( struct usb_serial_port *port );
440*1da177e4SLinus Torvalds static void digi_wakeup_write_lock(void *);
441*1da177e4SLinus Torvalds static int digi_write_oob_command( struct usb_serial_port *port,
442*1da177e4SLinus Torvalds 	unsigned char *buf, int count, int interruptible );
443*1da177e4SLinus Torvalds static int digi_write_inb_command( struct usb_serial_port *port,
444*1da177e4SLinus Torvalds 	unsigned char *buf, int count, unsigned long timeout );
445*1da177e4SLinus Torvalds static int digi_set_modem_signals( struct usb_serial_port *port,
446*1da177e4SLinus Torvalds 	unsigned int modem_signals, int interruptible );
447*1da177e4SLinus Torvalds static int digi_transmit_idle( struct usb_serial_port *port,
448*1da177e4SLinus Torvalds 	unsigned long timeout );
449*1da177e4SLinus Torvalds static void digi_rx_throttle (struct usb_serial_port *port);
450*1da177e4SLinus Torvalds static void digi_rx_unthrottle (struct usb_serial_port *port);
451*1da177e4SLinus Torvalds static void digi_set_termios( struct usb_serial_port *port,
452*1da177e4SLinus Torvalds 	struct termios *old_termios );
453*1da177e4SLinus Torvalds static void digi_break_ctl( struct usb_serial_port *port, int break_state );
454*1da177e4SLinus Torvalds static int digi_ioctl( struct usb_serial_port *port, struct file *file,
455*1da177e4SLinus Torvalds 	unsigned int cmd, unsigned long arg );
456*1da177e4SLinus Torvalds static int digi_tiocmget( struct usb_serial_port *port, struct file *file );
457*1da177e4SLinus Torvalds static int digi_tiocmset( struct usb_serial_port *port, struct file *file,
458*1da177e4SLinus Torvalds 	unsigned int set, unsigned int clear );
459*1da177e4SLinus Torvalds static int digi_write( struct usb_serial_port *port, const unsigned char *buf, int count );
460*1da177e4SLinus Torvalds static void digi_write_bulk_callback( struct urb *urb, struct pt_regs *regs );
461*1da177e4SLinus Torvalds static int digi_write_room( struct usb_serial_port *port );
462*1da177e4SLinus Torvalds static int digi_chars_in_buffer( struct usb_serial_port *port );
463*1da177e4SLinus Torvalds static int digi_open( struct usb_serial_port *port, struct file *filp );
464*1da177e4SLinus Torvalds static void digi_close( struct usb_serial_port *port, struct file *filp );
465*1da177e4SLinus Torvalds static int digi_startup_device( struct usb_serial *serial );
466*1da177e4SLinus Torvalds static int digi_startup( struct usb_serial *serial );
467*1da177e4SLinus Torvalds static void digi_shutdown( struct usb_serial *serial );
468*1da177e4SLinus Torvalds static void digi_read_bulk_callback( struct urb *urb, struct pt_regs *regs );
469*1da177e4SLinus Torvalds static int digi_read_inb_callback( struct urb *urb );
470*1da177e4SLinus Torvalds static int digi_read_oob_callback( struct urb *urb );
471*1da177e4SLinus Torvalds 
472*1da177e4SLinus Torvalds 
473*1da177e4SLinus Torvalds /* Statics */
474*1da177e4SLinus Torvalds 
475*1da177e4SLinus Torvalds static int debug;
476*1da177e4SLinus Torvalds 
477*1da177e4SLinus Torvalds static struct usb_device_id id_table_combined [] = {
478*1da177e4SLinus Torvalds 	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
479*1da177e4SLinus Torvalds 	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
480*1da177e4SLinus Torvalds 	{ }						/* Terminating entry */
481*1da177e4SLinus Torvalds };
482*1da177e4SLinus Torvalds 
483*1da177e4SLinus Torvalds static struct usb_device_id id_table_2 [] = {
484*1da177e4SLinus Torvalds 	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
485*1da177e4SLinus Torvalds 	{ }						/* Terminating entry */
486*1da177e4SLinus Torvalds };
487*1da177e4SLinus Torvalds 
488*1da177e4SLinus Torvalds static struct usb_device_id id_table_4 [] = {
489*1da177e4SLinus Torvalds 	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
490*1da177e4SLinus Torvalds 	{ }						/* Terminating entry */
491*1da177e4SLinus Torvalds };
492*1da177e4SLinus Torvalds 
493*1da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, id_table_combined);
494*1da177e4SLinus Torvalds 
495*1da177e4SLinus Torvalds static struct usb_driver digi_driver = {
496*1da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
497*1da177e4SLinus Torvalds 	.name =		"digi_acceleport",
498*1da177e4SLinus Torvalds 	.probe =	usb_serial_probe,
499*1da177e4SLinus Torvalds 	.disconnect =	usb_serial_disconnect,
500*1da177e4SLinus Torvalds 	.id_table =	id_table_combined,
501*1da177e4SLinus Torvalds };
502*1da177e4SLinus Torvalds 
503*1da177e4SLinus Torvalds 
504*1da177e4SLinus Torvalds /* device info needed for the Digi serial converter */
505*1da177e4SLinus Torvalds 
506*1da177e4SLinus Torvalds static struct usb_serial_device_type digi_acceleport_2_device = {
507*1da177e4SLinus Torvalds 	.owner =			THIS_MODULE,
508*1da177e4SLinus Torvalds 	.name =				"Digi 2 port USB adapter",
509*1da177e4SLinus Torvalds 	.short_name =			"digi_2",
510*1da177e4SLinus Torvalds 	.id_table =			id_table_2,
511*1da177e4SLinus Torvalds 	.num_interrupt_in =		0,
512*1da177e4SLinus Torvalds 	.num_bulk_in =			4,
513*1da177e4SLinus Torvalds 	.num_bulk_out =			4,
514*1da177e4SLinus Torvalds 	.num_ports =			3,
515*1da177e4SLinus Torvalds 	.open =				digi_open,
516*1da177e4SLinus Torvalds 	.close =			digi_close,
517*1da177e4SLinus Torvalds 	.write =			digi_write,
518*1da177e4SLinus Torvalds 	.write_room =			digi_write_room,
519*1da177e4SLinus Torvalds 	.write_bulk_callback = 		digi_write_bulk_callback,
520*1da177e4SLinus Torvalds 	.read_bulk_callback =		digi_read_bulk_callback,
521*1da177e4SLinus Torvalds 	.chars_in_buffer =		digi_chars_in_buffer,
522*1da177e4SLinus Torvalds 	.throttle =			digi_rx_throttle,
523*1da177e4SLinus Torvalds 	.unthrottle =			digi_rx_unthrottle,
524*1da177e4SLinus Torvalds 	.ioctl =			digi_ioctl,
525*1da177e4SLinus Torvalds 	.set_termios =			digi_set_termios,
526*1da177e4SLinus Torvalds 	.break_ctl =			digi_break_ctl,
527*1da177e4SLinus Torvalds 	.tiocmget =			digi_tiocmget,
528*1da177e4SLinus Torvalds 	.tiocmset =			digi_tiocmset,
529*1da177e4SLinus Torvalds 	.attach =			digi_startup,
530*1da177e4SLinus Torvalds 	.shutdown =			digi_shutdown,
531*1da177e4SLinus Torvalds };
532*1da177e4SLinus Torvalds 
533*1da177e4SLinus Torvalds static struct usb_serial_device_type digi_acceleport_4_device = {
534*1da177e4SLinus Torvalds 	.owner =			THIS_MODULE,
535*1da177e4SLinus Torvalds 	.name =				"Digi 4 port USB adapter",
536*1da177e4SLinus Torvalds 	.short_name =			"digi_4",
537*1da177e4SLinus Torvalds 	.id_table =			id_table_4,
538*1da177e4SLinus Torvalds 	.num_interrupt_in =		0,
539*1da177e4SLinus Torvalds 	.num_bulk_in =			5,
540*1da177e4SLinus Torvalds 	.num_bulk_out =			5,
541*1da177e4SLinus Torvalds 	.num_ports =			4,
542*1da177e4SLinus Torvalds 	.open =				digi_open,
543*1da177e4SLinus Torvalds 	.close =			digi_close,
544*1da177e4SLinus Torvalds 	.write =			digi_write,
545*1da177e4SLinus Torvalds 	.write_room =			digi_write_room,
546*1da177e4SLinus Torvalds 	.write_bulk_callback = 		digi_write_bulk_callback,
547*1da177e4SLinus Torvalds 	.read_bulk_callback =		digi_read_bulk_callback,
548*1da177e4SLinus Torvalds 	.chars_in_buffer =		digi_chars_in_buffer,
549*1da177e4SLinus Torvalds 	.throttle =			digi_rx_throttle,
550*1da177e4SLinus Torvalds 	.unthrottle =			digi_rx_unthrottle,
551*1da177e4SLinus Torvalds 	.ioctl =			digi_ioctl,
552*1da177e4SLinus Torvalds 	.set_termios =			digi_set_termios,
553*1da177e4SLinus Torvalds 	.break_ctl =			digi_break_ctl,
554*1da177e4SLinus Torvalds 	.tiocmget =			digi_tiocmget,
555*1da177e4SLinus Torvalds 	.tiocmset =			digi_tiocmset,
556*1da177e4SLinus Torvalds 	.attach =			digi_startup,
557*1da177e4SLinus Torvalds 	.shutdown =			digi_shutdown,
558*1da177e4SLinus Torvalds };
559*1da177e4SLinus Torvalds 
560*1da177e4SLinus Torvalds 
561*1da177e4SLinus Torvalds /* Functions */
562*1da177e4SLinus Torvalds 
563*1da177e4SLinus Torvalds /*
564*1da177e4SLinus Torvalds *  Cond Wait Interruptible Timeout Irqrestore
565*1da177e4SLinus Torvalds *
566*1da177e4SLinus Torvalds *  Do spin_unlock_irqrestore and interruptible_sleep_on_timeout
567*1da177e4SLinus Torvalds *  so that wake ups are not lost if they occur between the unlock
568*1da177e4SLinus Torvalds *  and the sleep.  In other words, spin_unlock_irqrestore and
569*1da177e4SLinus Torvalds *  interruptible_sleep_on_timeout are "atomic" with respect to
570*1da177e4SLinus Torvalds *  wake ups.  This is used to implement condition variables.
571*1da177e4SLinus Torvalds */
572*1da177e4SLinus Torvalds 
573*1da177e4SLinus Torvalds static inline long cond_wait_interruptible_timeout_irqrestore(
574*1da177e4SLinus Torvalds 	wait_queue_head_t *q, long timeout,
575*1da177e4SLinus Torvalds 	spinlock_t *lock, unsigned long flags )
576*1da177e4SLinus Torvalds {
577*1da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
578*1da177e4SLinus Torvalds 
579*1da177e4SLinus Torvalds 	prepare_to_wait(q, &wait, TASK_UNINTERRUPTIBLE);
580*1da177e4SLinus Torvalds 	spin_unlock_irqrestore(lock, flags);
581*1da177e4SLinus Torvalds 	timeout = schedule_timeout(timeout);
582*1da177e4SLinus Torvalds 	finish_wait(q, &wait);
583*1da177e4SLinus Torvalds 
584*1da177e4SLinus Torvalds 	return timeout;
585*1da177e4SLinus Torvalds 
586*1da177e4SLinus Torvalds }
587*1da177e4SLinus Torvalds 
588*1da177e4SLinus Torvalds 
589*1da177e4SLinus Torvalds /*
590*1da177e4SLinus Torvalds *  Digi Wakeup Write
591*1da177e4SLinus Torvalds *
592*1da177e4SLinus Torvalds *  Wake up port, line discipline, and tty processes sleeping
593*1da177e4SLinus Torvalds *  on writes.
594*1da177e4SLinus Torvalds */
595*1da177e4SLinus Torvalds 
596*1da177e4SLinus Torvalds static void digi_wakeup_write_lock(void *arg)
597*1da177e4SLinus Torvalds {
598*1da177e4SLinus Torvalds 	struct usb_serial_port *port = arg;
599*1da177e4SLinus Torvalds 	unsigned long flags;
600*1da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
601*1da177e4SLinus Torvalds 
602*1da177e4SLinus Torvalds 
603*1da177e4SLinus Torvalds 	spin_lock_irqsave( &priv->dp_port_lock, flags );
604*1da177e4SLinus Torvalds 	digi_wakeup_write( port );
605*1da177e4SLinus Torvalds 	spin_unlock_irqrestore( &priv->dp_port_lock, flags );
606*1da177e4SLinus Torvalds }
607*1da177e4SLinus Torvalds 
608*1da177e4SLinus Torvalds static void digi_wakeup_write( struct usb_serial_port *port )
609*1da177e4SLinus Torvalds {
610*1da177e4SLinus Torvalds 
611*1da177e4SLinus Torvalds 	struct tty_struct *tty = port->tty;
612*1da177e4SLinus Torvalds 
613*1da177e4SLinus Torvalds 
614*1da177e4SLinus Torvalds 	/* wake up port processes */
615*1da177e4SLinus Torvalds 	wake_up_interruptible( &port->write_wait );
616*1da177e4SLinus Torvalds 
617*1da177e4SLinus Torvalds 	/* wake up line discipline */
618*1da177e4SLinus Torvalds 	tty_wakeup(tty);
619*1da177e4SLinus Torvalds }
620*1da177e4SLinus Torvalds 
621*1da177e4SLinus Torvalds 
622*1da177e4SLinus Torvalds /*
623*1da177e4SLinus Torvalds *  Digi Write OOB Command
624*1da177e4SLinus Torvalds *
625*1da177e4SLinus Torvalds *  Write commands on the out of band port.  Commands are 4
626*1da177e4SLinus Torvalds *  bytes each, multiple commands can be sent at once, and
627*1da177e4SLinus Torvalds *  no command will be split across USB packets.  Returns 0
628*1da177e4SLinus Torvalds *  if successful, -EINTR if interrupted while sleeping and
629*1da177e4SLinus Torvalds *  the interruptible flag is true, or a negative error
630*1da177e4SLinus Torvalds *  returned by usb_submit_urb.
631*1da177e4SLinus Torvalds */
632*1da177e4SLinus Torvalds 
633*1da177e4SLinus Torvalds static int digi_write_oob_command( struct usb_serial_port *port,
634*1da177e4SLinus Torvalds 	unsigned char *buf, int count, int interruptible )
635*1da177e4SLinus Torvalds {
636*1da177e4SLinus Torvalds 
637*1da177e4SLinus Torvalds 	int ret = 0;
638*1da177e4SLinus Torvalds 	int len;
639*1da177e4SLinus Torvalds 	struct usb_serial_port *oob_port = (struct usb_serial_port *)((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
640*1da177e4SLinus Torvalds 	struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
641*1da177e4SLinus Torvalds 	unsigned long flags = 0;
642*1da177e4SLinus Torvalds 
643*1da177e4SLinus Torvalds 
644*1da177e4SLinus Torvalds dbg( "digi_write_oob_command: TOP: port=%d, count=%d", oob_priv->dp_port_num, count );
645*1da177e4SLinus Torvalds 
646*1da177e4SLinus Torvalds 	spin_lock_irqsave( &oob_priv->dp_port_lock, flags );
647*1da177e4SLinus Torvalds 
648*1da177e4SLinus Torvalds 	while( count > 0 ) {
649*1da177e4SLinus Torvalds 
650*1da177e4SLinus Torvalds 		while( oob_port->write_urb->status == -EINPROGRESS
651*1da177e4SLinus Torvalds 		|| oob_priv->dp_write_urb_in_use ) {
652*1da177e4SLinus Torvalds 			cond_wait_interruptible_timeout_irqrestore(
653*1da177e4SLinus Torvalds 				&oob_port->write_wait, DIGI_RETRY_TIMEOUT,
654*1da177e4SLinus Torvalds 				&oob_priv->dp_port_lock, flags );
655*1da177e4SLinus Torvalds 			if( interruptible && signal_pending(current) ) {
656*1da177e4SLinus Torvalds 				return( -EINTR );
657*1da177e4SLinus Torvalds 			}
658*1da177e4SLinus Torvalds 			spin_lock_irqsave( &oob_priv->dp_port_lock, flags );
659*1da177e4SLinus Torvalds 		}
660*1da177e4SLinus Torvalds 
661*1da177e4SLinus Torvalds 		/* len must be a multiple of 4, so commands are not split */
662*1da177e4SLinus Torvalds 		len = min(count, oob_port->bulk_out_size );
663*1da177e4SLinus Torvalds 		if( len > 4 )
664*1da177e4SLinus Torvalds 			len &= ~3;
665*1da177e4SLinus Torvalds 
666*1da177e4SLinus Torvalds 		memcpy( oob_port->write_urb->transfer_buffer, buf, len );
667*1da177e4SLinus Torvalds 		oob_port->write_urb->transfer_buffer_length = len;
668*1da177e4SLinus Torvalds 		oob_port->write_urb->dev = port->serial->dev;
669*1da177e4SLinus Torvalds 
670*1da177e4SLinus Torvalds 		if( (ret=usb_submit_urb(oob_port->write_urb, GFP_ATOMIC)) == 0 ) {
671*1da177e4SLinus Torvalds 			oob_priv->dp_write_urb_in_use = 1;
672*1da177e4SLinus Torvalds 			count -= len;
673*1da177e4SLinus Torvalds 			buf += len;
674*1da177e4SLinus Torvalds 		}
675*1da177e4SLinus Torvalds 
676*1da177e4SLinus Torvalds 	}
677*1da177e4SLinus Torvalds 
678*1da177e4SLinus Torvalds 	spin_unlock_irqrestore( &oob_priv->dp_port_lock, flags );
679*1da177e4SLinus Torvalds 
680*1da177e4SLinus Torvalds 	if( ret ) {
681*1da177e4SLinus Torvalds 		err("%s: usb_submit_urb failed, ret=%d", __FUNCTION__,
682*1da177e4SLinus Torvalds 			ret );
683*1da177e4SLinus Torvalds 	}
684*1da177e4SLinus Torvalds 
685*1da177e4SLinus Torvalds 	return( ret );
686*1da177e4SLinus Torvalds 
687*1da177e4SLinus Torvalds }
688*1da177e4SLinus Torvalds 
689*1da177e4SLinus Torvalds 
690*1da177e4SLinus Torvalds /*
691*1da177e4SLinus Torvalds *  Digi Write In Band Command
692*1da177e4SLinus Torvalds *
693*1da177e4SLinus Torvalds *  Write commands on the given port.  Commands are 4
694*1da177e4SLinus Torvalds *  bytes each, multiple commands can be sent at once, and
695*1da177e4SLinus Torvalds *  no command will be split across USB packets.  If timeout
696*1da177e4SLinus Torvalds *  is non-zero, write in band command will return after
697*1da177e4SLinus Torvalds *  waiting unsuccessfully for the URB status to clear for
698*1da177e4SLinus Torvalds *  timeout ticks.  Returns 0 if successful, or a negative
699*1da177e4SLinus Torvalds *  error returned by digi_write.
700*1da177e4SLinus Torvalds */
701*1da177e4SLinus Torvalds 
702*1da177e4SLinus Torvalds static int digi_write_inb_command( struct usb_serial_port *port,
703*1da177e4SLinus Torvalds 	unsigned char *buf, int count, unsigned long timeout )
704*1da177e4SLinus Torvalds {
705*1da177e4SLinus Torvalds 
706*1da177e4SLinus Torvalds 	int ret = 0;
707*1da177e4SLinus Torvalds 	int len;
708*1da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
709*1da177e4SLinus Torvalds 	unsigned char *data = port->write_urb->transfer_buffer;
710*1da177e4SLinus Torvalds 	unsigned long flags = 0;
711*1da177e4SLinus Torvalds 
712*1da177e4SLinus Torvalds 
713*1da177e4SLinus Torvalds dbg( "digi_write_inb_command: TOP: port=%d, count=%d", priv->dp_port_num,
714*1da177e4SLinus Torvalds count );
715*1da177e4SLinus Torvalds 
716*1da177e4SLinus Torvalds 	if( timeout )
717*1da177e4SLinus Torvalds 		timeout += jiffies;
718*1da177e4SLinus Torvalds 	else
719*1da177e4SLinus Torvalds 		timeout = ULONG_MAX;
720*1da177e4SLinus Torvalds 
721*1da177e4SLinus Torvalds 	spin_lock_irqsave( &priv->dp_port_lock, flags );
722*1da177e4SLinus Torvalds 
723*1da177e4SLinus Torvalds 	while( count > 0 && ret == 0 ) {
724*1da177e4SLinus Torvalds 
725*1da177e4SLinus Torvalds 		while( (port->write_urb->status == -EINPROGRESS
726*1da177e4SLinus Torvalds 		|| priv->dp_write_urb_in_use) && time_before(jiffies, timeout)) {
727*1da177e4SLinus Torvalds 			cond_wait_interruptible_timeout_irqrestore(
728*1da177e4SLinus Torvalds 				&port->write_wait, DIGI_RETRY_TIMEOUT,
729*1da177e4SLinus Torvalds 				&priv->dp_port_lock, flags );
730*1da177e4SLinus Torvalds 			if( signal_pending(current) ) {
731*1da177e4SLinus Torvalds 				return( -EINTR );
732*1da177e4SLinus Torvalds 			}
733*1da177e4SLinus Torvalds 			spin_lock_irqsave( &priv->dp_port_lock, flags );
734*1da177e4SLinus Torvalds 		}
735*1da177e4SLinus Torvalds 
736*1da177e4SLinus Torvalds 		/* len must be a multiple of 4 and small enough to */
737*1da177e4SLinus Torvalds 		/* guarantee the write will send buffered data first, */
738*1da177e4SLinus Torvalds 		/* so commands are in order with data and not split */
739*1da177e4SLinus Torvalds 		len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len );
740*1da177e4SLinus Torvalds 		if( len > 4 )
741*1da177e4SLinus Torvalds 			len &= ~3;
742*1da177e4SLinus Torvalds 
743*1da177e4SLinus Torvalds 		/* write any buffered data first */
744*1da177e4SLinus Torvalds 		if( priv->dp_out_buf_len > 0 ) {
745*1da177e4SLinus Torvalds 			data[0] = DIGI_CMD_SEND_DATA;
746*1da177e4SLinus Torvalds 			data[1] = priv->dp_out_buf_len;
747*1da177e4SLinus Torvalds 			memcpy( data+2, priv->dp_out_buf,
748*1da177e4SLinus Torvalds 				priv->dp_out_buf_len );
749*1da177e4SLinus Torvalds 			memcpy( data+2+priv->dp_out_buf_len, buf, len );
750*1da177e4SLinus Torvalds 			port->write_urb->transfer_buffer_length
751*1da177e4SLinus Torvalds 				= priv->dp_out_buf_len+2+len;
752*1da177e4SLinus Torvalds 		} else {
753*1da177e4SLinus Torvalds 			memcpy( data, buf, len );
754*1da177e4SLinus Torvalds 			port->write_urb->transfer_buffer_length = len;
755*1da177e4SLinus Torvalds 		}
756*1da177e4SLinus Torvalds 		port->write_urb->dev = port->serial->dev;
757*1da177e4SLinus Torvalds 
758*1da177e4SLinus Torvalds 		if( (ret=usb_submit_urb(port->write_urb, GFP_ATOMIC)) == 0 ) {
759*1da177e4SLinus Torvalds 			priv->dp_write_urb_in_use = 1;
760*1da177e4SLinus Torvalds 			priv->dp_out_buf_len = 0;
761*1da177e4SLinus Torvalds 			count -= len;
762*1da177e4SLinus Torvalds 			buf += len;
763*1da177e4SLinus Torvalds 		}
764*1da177e4SLinus Torvalds 
765*1da177e4SLinus Torvalds 	}
766*1da177e4SLinus Torvalds 
767*1da177e4SLinus Torvalds 	spin_unlock_irqrestore( &priv->dp_port_lock, flags );
768*1da177e4SLinus Torvalds 
769*1da177e4SLinus Torvalds 	if( ret ) {
770*1da177e4SLinus Torvalds 		err("%s: usb_submit_urb failed, ret=%d, port=%d", __FUNCTION__,
771*1da177e4SLinus Torvalds 		ret, priv->dp_port_num );
772*1da177e4SLinus Torvalds 	}
773*1da177e4SLinus Torvalds 
774*1da177e4SLinus Torvalds 	return( ret );
775*1da177e4SLinus Torvalds 
776*1da177e4SLinus Torvalds }
777*1da177e4SLinus Torvalds 
778*1da177e4SLinus Torvalds 
779*1da177e4SLinus Torvalds /*
780*1da177e4SLinus Torvalds *  Digi Set Modem Signals
781*1da177e4SLinus Torvalds *
782*1da177e4SLinus Torvalds *  Sets or clears DTR and RTS on the port, according to the
783*1da177e4SLinus Torvalds *  modem_signals argument.  Use TIOCM_DTR and TIOCM_RTS flags
784*1da177e4SLinus Torvalds *  for the modem_signals argument.  Returns 0 if successful,
785*1da177e4SLinus Torvalds *  -EINTR if interrupted while sleeping, or a non-zero error
786*1da177e4SLinus Torvalds *  returned by usb_submit_urb.
787*1da177e4SLinus Torvalds */
788*1da177e4SLinus Torvalds 
789*1da177e4SLinus Torvalds static int digi_set_modem_signals( struct usb_serial_port *port,
790*1da177e4SLinus Torvalds 	unsigned int modem_signals, int interruptible )
791*1da177e4SLinus Torvalds {
792*1da177e4SLinus Torvalds 
793*1da177e4SLinus Torvalds 	int ret;
794*1da177e4SLinus Torvalds 	struct digi_port *port_priv = usb_get_serial_port_data(port);
795*1da177e4SLinus Torvalds 	struct usb_serial_port *oob_port = (struct usb_serial_port *)((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
796*1da177e4SLinus Torvalds 	struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
797*1da177e4SLinus Torvalds 	unsigned char *data = oob_port->write_urb->transfer_buffer;
798*1da177e4SLinus Torvalds 	unsigned long flags = 0;
799*1da177e4SLinus Torvalds 
800*1da177e4SLinus Torvalds 
801*1da177e4SLinus Torvalds dbg( "digi_set_modem_signals: TOP: port=%d, modem_signals=0x%x",
802*1da177e4SLinus Torvalds port_priv->dp_port_num, modem_signals );
803*1da177e4SLinus Torvalds 
804*1da177e4SLinus Torvalds 	spin_lock_irqsave( &oob_priv->dp_port_lock, flags );
805*1da177e4SLinus Torvalds 	spin_lock( &port_priv->dp_port_lock );
806*1da177e4SLinus Torvalds 
807*1da177e4SLinus Torvalds 	while( oob_port->write_urb->status == -EINPROGRESS
808*1da177e4SLinus Torvalds 	|| oob_priv->dp_write_urb_in_use ) {
809*1da177e4SLinus Torvalds 		spin_unlock( &port_priv->dp_port_lock );
810*1da177e4SLinus Torvalds 		cond_wait_interruptible_timeout_irqrestore(
811*1da177e4SLinus Torvalds 			&oob_port->write_wait, DIGI_RETRY_TIMEOUT,
812*1da177e4SLinus Torvalds 			&oob_priv->dp_port_lock, flags );
813*1da177e4SLinus Torvalds 		if( interruptible && signal_pending(current) ) {
814*1da177e4SLinus Torvalds 			return( -EINTR );
815*1da177e4SLinus Torvalds 		}
816*1da177e4SLinus Torvalds 		spin_lock_irqsave( &oob_priv->dp_port_lock, flags );
817*1da177e4SLinus Torvalds 		spin_lock( &port_priv->dp_port_lock );
818*1da177e4SLinus Torvalds 	}
819*1da177e4SLinus Torvalds 
820*1da177e4SLinus Torvalds 	data[0] = DIGI_CMD_SET_DTR_SIGNAL;
821*1da177e4SLinus Torvalds 	data[1] = port_priv->dp_port_num;
822*1da177e4SLinus Torvalds 	data[2] = (modem_signals&TIOCM_DTR) ?
823*1da177e4SLinus Torvalds 		DIGI_DTR_ACTIVE : DIGI_DTR_INACTIVE;
824*1da177e4SLinus Torvalds 	data[3] = 0;
825*1da177e4SLinus Torvalds 
826*1da177e4SLinus Torvalds 	data[4] = DIGI_CMD_SET_RTS_SIGNAL;
827*1da177e4SLinus Torvalds 	data[5] = port_priv->dp_port_num;
828*1da177e4SLinus Torvalds 	data[6] = (modem_signals&TIOCM_RTS) ?
829*1da177e4SLinus Torvalds 		DIGI_RTS_ACTIVE : DIGI_RTS_INACTIVE;
830*1da177e4SLinus Torvalds 	data[7] = 0;
831*1da177e4SLinus Torvalds 
832*1da177e4SLinus Torvalds 	oob_port->write_urb->transfer_buffer_length = 8;
833*1da177e4SLinus Torvalds 	oob_port->write_urb->dev = port->serial->dev;
834*1da177e4SLinus Torvalds 
835*1da177e4SLinus Torvalds 	if( (ret=usb_submit_urb(oob_port->write_urb, GFP_ATOMIC)) == 0 ) {
836*1da177e4SLinus Torvalds 		oob_priv->dp_write_urb_in_use = 1;
837*1da177e4SLinus Torvalds 		port_priv->dp_modem_signals =
838*1da177e4SLinus Torvalds 			(port_priv->dp_modem_signals&~(TIOCM_DTR|TIOCM_RTS))
839*1da177e4SLinus Torvalds 			| (modem_signals&(TIOCM_DTR|TIOCM_RTS));
840*1da177e4SLinus Torvalds 	}
841*1da177e4SLinus Torvalds 
842*1da177e4SLinus Torvalds 	spin_unlock( &port_priv->dp_port_lock );
843*1da177e4SLinus Torvalds 	spin_unlock_irqrestore( &oob_priv->dp_port_lock, flags );
844*1da177e4SLinus Torvalds 
845*1da177e4SLinus Torvalds 	if( ret ) {
846*1da177e4SLinus Torvalds 		err("%s: usb_submit_urb failed, ret=%d", __FUNCTION__,
847*1da177e4SLinus Torvalds 		ret );
848*1da177e4SLinus Torvalds 	}
849*1da177e4SLinus Torvalds 
850*1da177e4SLinus Torvalds 	return( ret );
851*1da177e4SLinus Torvalds 
852*1da177e4SLinus Torvalds }
853*1da177e4SLinus Torvalds 
854*1da177e4SLinus Torvalds 
855*1da177e4SLinus Torvalds /*
856*1da177e4SLinus Torvalds *  Digi Transmit Idle
857*1da177e4SLinus Torvalds *
858*1da177e4SLinus Torvalds *  Digi transmit idle waits, up to timeout ticks, for the transmitter
859*1da177e4SLinus Torvalds *  to go idle.  It returns 0 if successful or a negative error.
860*1da177e4SLinus Torvalds *
861*1da177e4SLinus Torvalds *  There are race conditions here if more than one process is calling
862*1da177e4SLinus Torvalds *  digi_transmit_idle on the same port at the same time.  However, this
863*1da177e4SLinus Torvalds *  is only called from close, and only one process can be in close on a
864*1da177e4SLinus Torvalds *  port at a time, so its ok.
865*1da177e4SLinus Torvalds */
866*1da177e4SLinus Torvalds 
867*1da177e4SLinus Torvalds static int digi_transmit_idle( struct usb_serial_port *port,
868*1da177e4SLinus Torvalds 	unsigned long timeout )
869*1da177e4SLinus Torvalds {
870*1da177e4SLinus Torvalds 
871*1da177e4SLinus Torvalds 	int ret;
872*1da177e4SLinus Torvalds 	unsigned char buf[2];
873*1da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
874*1da177e4SLinus Torvalds 	unsigned long flags = 0;
875*1da177e4SLinus Torvalds 
876*1da177e4SLinus Torvalds 
877*1da177e4SLinus Torvalds 	spin_lock_irqsave( &priv->dp_port_lock, flags );
878*1da177e4SLinus Torvalds 	priv->dp_transmit_idle = 0;
879*1da177e4SLinus Torvalds 	spin_unlock_irqrestore( &priv->dp_port_lock, flags );
880*1da177e4SLinus Torvalds 
881*1da177e4SLinus Torvalds 	buf[0] = DIGI_CMD_TRANSMIT_IDLE;
882*1da177e4SLinus Torvalds 	buf[1] = 0;
883*1da177e4SLinus Torvalds 
884*1da177e4SLinus Torvalds 	timeout += jiffies;
885*1da177e4SLinus Torvalds 
886*1da177e4SLinus Torvalds 	if( (ret=digi_write_inb_command( port, buf, 2, timeout-jiffies )) != 0 )
887*1da177e4SLinus Torvalds 		return( ret );
888*1da177e4SLinus Torvalds 
889*1da177e4SLinus Torvalds 	spin_lock_irqsave( &priv->dp_port_lock, flags );
890*1da177e4SLinus Torvalds 
891*1da177e4SLinus Torvalds 	while( time_before(jiffies, timeout) && !priv->dp_transmit_idle ) {
892*1da177e4SLinus Torvalds 		cond_wait_interruptible_timeout_irqrestore(
893*1da177e4SLinus Torvalds 			&priv->dp_transmit_idle_wait, DIGI_RETRY_TIMEOUT,
894*1da177e4SLinus Torvalds 			&priv->dp_port_lock, flags );
895*1da177e4SLinus Torvalds 		if( signal_pending(current) ) {
896*1da177e4SLinus Torvalds 			return( -EINTR );
897*1da177e4SLinus Torvalds 		}
898*1da177e4SLinus Torvalds 		spin_lock_irqsave( &priv->dp_port_lock, flags );
899*1da177e4SLinus Torvalds 	}
900*1da177e4SLinus Torvalds 
901*1da177e4SLinus Torvalds 	priv->dp_transmit_idle = 0;
902*1da177e4SLinus Torvalds 	spin_unlock_irqrestore( &priv->dp_port_lock, flags );
903*1da177e4SLinus Torvalds 
904*1da177e4SLinus Torvalds 	return( 0 );
905*1da177e4SLinus Torvalds 
906*1da177e4SLinus Torvalds }
907*1da177e4SLinus Torvalds 
908*1da177e4SLinus Torvalds 
909*1da177e4SLinus Torvalds static void digi_rx_throttle( struct usb_serial_port *port )
910*1da177e4SLinus Torvalds {
911*1da177e4SLinus Torvalds 
912*1da177e4SLinus Torvalds 	unsigned long flags;
913*1da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
914*1da177e4SLinus Torvalds 
915*1da177e4SLinus Torvalds 
916*1da177e4SLinus Torvalds dbg( "digi_rx_throttle: TOP: port=%d", priv->dp_port_num );
917*1da177e4SLinus Torvalds 
918*1da177e4SLinus Torvalds 	/* stop receiving characters by not resubmitting the read urb */
919*1da177e4SLinus Torvalds 	spin_lock_irqsave( &priv->dp_port_lock, flags );
920*1da177e4SLinus Torvalds 	priv->dp_throttled = 1;
921*1da177e4SLinus Torvalds 	priv->dp_throttle_restart = 0;
922*1da177e4SLinus Torvalds 	priv->dp_in_buf_len = 0;
923*1da177e4SLinus Torvalds 	spin_unlock_irqrestore( &priv->dp_port_lock, flags );
924*1da177e4SLinus Torvalds 
925*1da177e4SLinus Torvalds }
926*1da177e4SLinus Torvalds 
927*1da177e4SLinus Torvalds 
928*1da177e4SLinus Torvalds static void digi_rx_unthrottle( struct usb_serial_port *port )
929*1da177e4SLinus Torvalds {
930*1da177e4SLinus Torvalds 
931*1da177e4SLinus Torvalds 	int ret = 0;
932*1da177e4SLinus Torvalds 	int len;
933*1da177e4SLinus Torvalds 	unsigned long flags;
934*1da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
935*1da177e4SLinus Torvalds 	struct tty_struct *tty = port->tty;
936*1da177e4SLinus Torvalds 
937*1da177e4SLinus Torvalds 
938*1da177e4SLinus Torvalds dbg( "digi_rx_unthrottle: TOP: port=%d", priv->dp_port_num );
939*1da177e4SLinus Torvalds 
940*1da177e4SLinus Torvalds 	spin_lock_irqsave( &priv->dp_port_lock, flags );
941*1da177e4SLinus Torvalds 
942*1da177e4SLinus Torvalds 	/* send any buffered chars from throttle time on to tty subsystem */
943*1da177e4SLinus Torvalds 	len = min(priv->dp_in_buf_len, TTY_FLIPBUF_SIZE - tty->flip.count );
944*1da177e4SLinus Torvalds 	if( len > 0 ) {
945*1da177e4SLinus Torvalds 		memcpy( tty->flip.char_buf_ptr, priv->dp_in_buf, len );
946*1da177e4SLinus Torvalds 		memcpy( tty->flip.flag_buf_ptr, priv->dp_in_flag_buf, len );
947*1da177e4SLinus Torvalds 		tty->flip.char_buf_ptr += len;
948*1da177e4SLinus Torvalds 		tty->flip.flag_buf_ptr += len;
949*1da177e4SLinus Torvalds 		tty->flip.count += len;
950*1da177e4SLinus Torvalds 		tty_flip_buffer_push( tty );
951*1da177e4SLinus Torvalds 	}
952*1da177e4SLinus Torvalds 
953*1da177e4SLinus Torvalds 	/* restart read chain */
954*1da177e4SLinus Torvalds 	if( priv->dp_throttle_restart ) {
955*1da177e4SLinus Torvalds 		port->read_urb->dev = port->serial->dev;
956*1da177e4SLinus Torvalds 		ret = usb_submit_urb( port->read_urb, GFP_ATOMIC );
957*1da177e4SLinus Torvalds 	}
958*1da177e4SLinus Torvalds 
959*1da177e4SLinus Torvalds 	/* turn throttle off */
960*1da177e4SLinus Torvalds 	priv->dp_throttled = 0;
961*1da177e4SLinus Torvalds 	priv->dp_in_buf_len = 0;
962*1da177e4SLinus Torvalds 	priv->dp_throttle_restart = 0;
963*1da177e4SLinus Torvalds 
964*1da177e4SLinus Torvalds 	spin_unlock_irqrestore( &priv->dp_port_lock, flags );
965*1da177e4SLinus Torvalds 
966*1da177e4SLinus Torvalds 	if( ret ) {
967*1da177e4SLinus Torvalds 		err("%s: usb_submit_urb failed, ret=%d, port=%d", __FUNCTION__,
968*1da177e4SLinus Torvalds 			ret, priv->dp_port_num );
969*1da177e4SLinus Torvalds 	}
970*1da177e4SLinus Torvalds 
971*1da177e4SLinus Torvalds }
972*1da177e4SLinus Torvalds 
973*1da177e4SLinus Torvalds 
974*1da177e4SLinus Torvalds static void digi_set_termios( struct usb_serial_port *port,
975*1da177e4SLinus Torvalds 	struct termios *old_termios )
976*1da177e4SLinus Torvalds {
977*1da177e4SLinus Torvalds 
978*1da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
979*1da177e4SLinus Torvalds 	unsigned int iflag = port->tty->termios->c_iflag;
980*1da177e4SLinus Torvalds 	unsigned int cflag = port->tty->termios->c_cflag;
981*1da177e4SLinus Torvalds 	unsigned int old_iflag = old_termios->c_iflag;
982*1da177e4SLinus Torvalds 	unsigned int old_cflag = old_termios->c_cflag;
983*1da177e4SLinus Torvalds 	unsigned char buf[32];
984*1da177e4SLinus Torvalds 	unsigned int modem_signals;
985*1da177e4SLinus Torvalds 	int arg,ret;
986*1da177e4SLinus Torvalds 	int i = 0;
987*1da177e4SLinus Torvalds 
988*1da177e4SLinus Torvalds 
989*1da177e4SLinus Torvalds 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 );
990*1da177e4SLinus Torvalds 
991*1da177e4SLinus Torvalds 	/* set baud rate */
992*1da177e4SLinus Torvalds 	if( (cflag&CBAUD) != (old_cflag&CBAUD) ) {
993*1da177e4SLinus Torvalds 
994*1da177e4SLinus Torvalds 		arg = -1;
995*1da177e4SLinus Torvalds 
996*1da177e4SLinus Torvalds 		/* reassert DTR and (maybe) RTS on transition from B0 */
997*1da177e4SLinus Torvalds 		if( (old_cflag&CBAUD) == B0 ) {
998*1da177e4SLinus Torvalds 			/* don't set RTS if using hardware flow control */
999*1da177e4SLinus Torvalds 			/* and throttling input */
1000*1da177e4SLinus Torvalds 			modem_signals = TIOCM_DTR;
1001*1da177e4SLinus Torvalds 			if( !(port->tty->termios->c_cflag & CRTSCTS) ||
1002*1da177e4SLinus Torvalds 			!test_bit(TTY_THROTTLED, &port->tty->flags) ) {
1003*1da177e4SLinus Torvalds 				modem_signals |= TIOCM_RTS;
1004*1da177e4SLinus Torvalds 			}
1005*1da177e4SLinus Torvalds 			digi_set_modem_signals( port, modem_signals, 1 );
1006*1da177e4SLinus Torvalds 		}
1007*1da177e4SLinus Torvalds 
1008*1da177e4SLinus Torvalds 		switch( (cflag&CBAUD) ) {
1009*1da177e4SLinus Torvalds 			/* drop DTR and RTS on transition to B0 */
1010*1da177e4SLinus Torvalds 		case B0: digi_set_modem_signals( port, 0, 1 ); break;
1011*1da177e4SLinus Torvalds 		case B50: arg = DIGI_BAUD_50; break;
1012*1da177e4SLinus Torvalds 		case B75: arg = DIGI_BAUD_75; break;
1013*1da177e4SLinus Torvalds 		case B110: arg = DIGI_BAUD_110; break;
1014*1da177e4SLinus Torvalds 		case B150: arg = DIGI_BAUD_150; break;
1015*1da177e4SLinus Torvalds 		case B200: arg = DIGI_BAUD_200; break;
1016*1da177e4SLinus Torvalds 		case B300: arg = DIGI_BAUD_300; break;
1017*1da177e4SLinus Torvalds 		case B600: arg = DIGI_BAUD_600; break;
1018*1da177e4SLinus Torvalds 		case B1200: arg = DIGI_BAUD_1200; break;
1019*1da177e4SLinus Torvalds 		case B1800: arg = DIGI_BAUD_1800; break;
1020*1da177e4SLinus Torvalds 		case B2400: arg = DIGI_BAUD_2400; break;
1021*1da177e4SLinus Torvalds 		case B4800: arg = DIGI_BAUD_4800; break;
1022*1da177e4SLinus Torvalds 		case B9600: arg = DIGI_BAUD_9600; break;
1023*1da177e4SLinus Torvalds 		case B19200: arg = DIGI_BAUD_19200; break;
1024*1da177e4SLinus Torvalds 		case B38400: arg = DIGI_BAUD_38400; break;
1025*1da177e4SLinus Torvalds 		case B57600: arg = DIGI_BAUD_57600; break;
1026*1da177e4SLinus Torvalds 		case B115200: arg = DIGI_BAUD_115200; break;
1027*1da177e4SLinus Torvalds 		case B230400: arg = DIGI_BAUD_230400; break;
1028*1da177e4SLinus Torvalds 		case B460800: arg = DIGI_BAUD_460800; break;
1029*1da177e4SLinus Torvalds 		default:
1030*1da177e4SLinus Torvalds 			dbg( "digi_set_termios: can't handle baud rate 0x%x",
1031*1da177e4SLinus Torvalds 				(cflag&CBAUD) );
1032*1da177e4SLinus Torvalds 			break;
1033*1da177e4SLinus Torvalds 		}
1034*1da177e4SLinus Torvalds 
1035*1da177e4SLinus Torvalds 		if( arg != -1 ) {
1036*1da177e4SLinus Torvalds 			buf[i++] = DIGI_CMD_SET_BAUD_RATE;
1037*1da177e4SLinus Torvalds 			buf[i++] = priv->dp_port_num;
1038*1da177e4SLinus Torvalds 			buf[i++] = arg;
1039*1da177e4SLinus Torvalds 			buf[i++] = 0;
1040*1da177e4SLinus Torvalds 		}
1041*1da177e4SLinus Torvalds 
1042*1da177e4SLinus Torvalds 	}
1043*1da177e4SLinus Torvalds 
1044*1da177e4SLinus Torvalds 	/* set parity */
1045*1da177e4SLinus Torvalds 	if( (cflag&(PARENB|PARODD)) != (old_cflag&(PARENB|PARODD)) ) {
1046*1da177e4SLinus Torvalds 
1047*1da177e4SLinus Torvalds 		if( (cflag&PARENB) ) {
1048*1da177e4SLinus Torvalds 			if( (cflag&PARODD) )
1049*1da177e4SLinus Torvalds 				arg = DIGI_PARITY_ODD;
1050*1da177e4SLinus Torvalds 			else
1051*1da177e4SLinus Torvalds 				arg = DIGI_PARITY_EVEN;
1052*1da177e4SLinus Torvalds 		} else {
1053*1da177e4SLinus Torvalds 			arg = DIGI_PARITY_NONE;
1054*1da177e4SLinus Torvalds 		}
1055*1da177e4SLinus Torvalds 
1056*1da177e4SLinus Torvalds 		buf[i++] = DIGI_CMD_SET_PARITY;
1057*1da177e4SLinus Torvalds 		buf[i++] = priv->dp_port_num;
1058*1da177e4SLinus Torvalds 		buf[i++] = arg;
1059*1da177e4SLinus Torvalds 		buf[i++] = 0;
1060*1da177e4SLinus Torvalds 
1061*1da177e4SLinus Torvalds 	}
1062*1da177e4SLinus Torvalds 
1063*1da177e4SLinus Torvalds 	/* set word size */
1064*1da177e4SLinus Torvalds 	if( (cflag&CSIZE) != (old_cflag&CSIZE) ) {
1065*1da177e4SLinus Torvalds 
1066*1da177e4SLinus Torvalds 		arg = -1;
1067*1da177e4SLinus Torvalds 
1068*1da177e4SLinus Torvalds 		switch( (cflag&CSIZE) ) {
1069*1da177e4SLinus Torvalds 		case CS5: arg = DIGI_WORD_SIZE_5; break;
1070*1da177e4SLinus Torvalds 		case CS6: arg = DIGI_WORD_SIZE_6; break;
1071*1da177e4SLinus Torvalds 		case CS7: arg = DIGI_WORD_SIZE_7; break;
1072*1da177e4SLinus Torvalds 		case CS8: arg = DIGI_WORD_SIZE_8; break;
1073*1da177e4SLinus Torvalds 		default:
1074*1da177e4SLinus Torvalds 			dbg( "digi_set_termios: can't handle word size %d",
1075*1da177e4SLinus Torvalds 				(cflag&CSIZE) );
1076*1da177e4SLinus Torvalds 			break;
1077*1da177e4SLinus Torvalds 		}
1078*1da177e4SLinus Torvalds 
1079*1da177e4SLinus Torvalds 		if( arg != -1 ) {
1080*1da177e4SLinus Torvalds 			buf[i++] = DIGI_CMD_SET_WORD_SIZE;
1081*1da177e4SLinus Torvalds 			buf[i++] = priv->dp_port_num;
1082*1da177e4SLinus Torvalds 			buf[i++] = arg;
1083*1da177e4SLinus Torvalds 			buf[i++] = 0;
1084*1da177e4SLinus Torvalds 		}
1085*1da177e4SLinus Torvalds 
1086*1da177e4SLinus Torvalds 	}
1087*1da177e4SLinus Torvalds 
1088*1da177e4SLinus Torvalds 	/* set stop bits */
1089*1da177e4SLinus Torvalds 	if( (cflag&CSTOPB) != (old_cflag&CSTOPB) ) {
1090*1da177e4SLinus Torvalds 
1091*1da177e4SLinus Torvalds 		if( (cflag&CSTOPB) )
1092*1da177e4SLinus Torvalds 			arg = DIGI_STOP_BITS_2;
1093*1da177e4SLinus Torvalds 		else
1094*1da177e4SLinus Torvalds 			arg = DIGI_STOP_BITS_1;
1095*1da177e4SLinus Torvalds 
1096*1da177e4SLinus Torvalds 		buf[i++] = DIGI_CMD_SET_STOP_BITS;
1097*1da177e4SLinus Torvalds 		buf[i++] = priv->dp_port_num;
1098*1da177e4SLinus Torvalds 		buf[i++] = arg;
1099*1da177e4SLinus Torvalds 		buf[i++] = 0;
1100*1da177e4SLinus Torvalds 
1101*1da177e4SLinus Torvalds 	}
1102*1da177e4SLinus Torvalds 
1103*1da177e4SLinus Torvalds 	/* set input flow control */
1104*1da177e4SLinus Torvalds 	if( (iflag&IXOFF) != (old_iflag&IXOFF)
1105*1da177e4SLinus Torvalds 	|| (cflag&CRTSCTS) != (old_cflag&CRTSCTS) ) {
1106*1da177e4SLinus Torvalds 
1107*1da177e4SLinus Torvalds 		arg = 0;
1108*1da177e4SLinus Torvalds 
1109*1da177e4SLinus Torvalds 		if( (iflag&IXOFF) )
1110*1da177e4SLinus Torvalds 			arg |= DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
1111*1da177e4SLinus Torvalds 		else
1112*1da177e4SLinus Torvalds 			arg &= ~DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
1113*1da177e4SLinus Torvalds 
1114*1da177e4SLinus Torvalds 		if( (cflag&CRTSCTS) ) {
1115*1da177e4SLinus Torvalds 
1116*1da177e4SLinus Torvalds 			arg |= DIGI_INPUT_FLOW_CONTROL_RTS;
1117*1da177e4SLinus Torvalds 
1118*1da177e4SLinus Torvalds 			/* On USB-4 it is necessary to assert RTS prior */
1119*1da177e4SLinus Torvalds 			/* to selecting RTS input flow control.  */
1120*1da177e4SLinus Torvalds 			buf[i++] = DIGI_CMD_SET_RTS_SIGNAL;
1121*1da177e4SLinus Torvalds 			buf[i++] = priv->dp_port_num;
1122*1da177e4SLinus Torvalds 			buf[i++] = DIGI_RTS_ACTIVE;
1123*1da177e4SLinus Torvalds 			buf[i++] = 0;
1124*1da177e4SLinus Torvalds 
1125*1da177e4SLinus Torvalds 		} else {
1126*1da177e4SLinus Torvalds 			arg &= ~DIGI_INPUT_FLOW_CONTROL_RTS;
1127*1da177e4SLinus Torvalds 		}
1128*1da177e4SLinus Torvalds 
1129*1da177e4SLinus Torvalds 		buf[i++] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
1130*1da177e4SLinus Torvalds 		buf[i++] = priv->dp_port_num;
1131*1da177e4SLinus Torvalds 		buf[i++] = arg;
1132*1da177e4SLinus Torvalds 		buf[i++] = 0;
1133*1da177e4SLinus Torvalds 
1134*1da177e4SLinus Torvalds 	}
1135*1da177e4SLinus Torvalds 
1136*1da177e4SLinus Torvalds 	/* set output flow control */
1137*1da177e4SLinus Torvalds 	if( (iflag&IXON) != (old_iflag&IXON)
1138*1da177e4SLinus Torvalds 	|| (cflag&CRTSCTS) != (old_cflag&CRTSCTS) ) {
1139*1da177e4SLinus Torvalds 
1140*1da177e4SLinus Torvalds 		arg = 0;
1141*1da177e4SLinus Torvalds 
1142*1da177e4SLinus Torvalds 		if( (iflag&IXON) )
1143*1da177e4SLinus Torvalds 			arg |= DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
1144*1da177e4SLinus Torvalds 		else
1145*1da177e4SLinus Torvalds 			arg &= ~DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
1146*1da177e4SLinus Torvalds 
1147*1da177e4SLinus Torvalds 		if( (cflag&CRTSCTS) ) {
1148*1da177e4SLinus Torvalds 			arg |= DIGI_OUTPUT_FLOW_CONTROL_CTS;
1149*1da177e4SLinus Torvalds 		} else {
1150*1da177e4SLinus Torvalds 			arg &= ~DIGI_OUTPUT_FLOW_CONTROL_CTS;
1151*1da177e4SLinus Torvalds 			port->tty->hw_stopped = 0;
1152*1da177e4SLinus Torvalds 		}
1153*1da177e4SLinus Torvalds 
1154*1da177e4SLinus Torvalds 		buf[i++] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
1155*1da177e4SLinus Torvalds 		buf[i++] = priv->dp_port_num;
1156*1da177e4SLinus Torvalds 		buf[i++] = arg;
1157*1da177e4SLinus Torvalds 		buf[i++] = 0;
1158*1da177e4SLinus Torvalds 
1159*1da177e4SLinus Torvalds 	}
1160*1da177e4SLinus Torvalds 
1161*1da177e4SLinus Torvalds 	/* set receive enable/disable */
1162*1da177e4SLinus Torvalds 	if( (cflag&CREAD) != (old_cflag&CREAD) ) {
1163*1da177e4SLinus Torvalds 
1164*1da177e4SLinus Torvalds 		if( (cflag&CREAD) )
1165*1da177e4SLinus Torvalds 			arg = DIGI_ENABLE;
1166*1da177e4SLinus Torvalds 		else
1167*1da177e4SLinus Torvalds 			arg = DIGI_DISABLE;
1168*1da177e4SLinus Torvalds 
1169*1da177e4SLinus Torvalds 		buf[i++] = DIGI_CMD_RECEIVE_ENABLE;
1170*1da177e4SLinus Torvalds 		buf[i++] = priv->dp_port_num;
1171*1da177e4SLinus Torvalds 		buf[i++] = arg;
1172*1da177e4SLinus Torvalds 		buf[i++] = 0;
1173*1da177e4SLinus Torvalds 
1174*1da177e4SLinus Torvalds 	}
1175*1da177e4SLinus Torvalds 
1176*1da177e4SLinus Torvalds 	if( (ret=digi_write_oob_command( port, buf, i, 1 )) != 0 )
1177*1da177e4SLinus Torvalds 		dbg( "digi_set_termios: write oob failed, ret=%d", ret );
1178*1da177e4SLinus Torvalds 
1179*1da177e4SLinus Torvalds }
1180*1da177e4SLinus Torvalds 
1181*1da177e4SLinus Torvalds 
1182*1da177e4SLinus Torvalds static void digi_break_ctl( struct usb_serial_port *port, int break_state )
1183*1da177e4SLinus Torvalds {
1184*1da177e4SLinus Torvalds 
1185*1da177e4SLinus Torvalds 	unsigned char buf[4];
1186*1da177e4SLinus Torvalds 
1187*1da177e4SLinus Torvalds 
1188*1da177e4SLinus Torvalds 	buf[0] = DIGI_CMD_BREAK_CONTROL;
1189*1da177e4SLinus Torvalds 	buf[1] = 2;				/* length */
1190*1da177e4SLinus Torvalds 	buf[2] = break_state ? 1 : 0;
1191*1da177e4SLinus Torvalds 	buf[3] = 0;				/* pad */
1192*1da177e4SLinus Torvalds 
1193*1da177e4SLinus Torvalds 	digi_write_inb_command( port, buf, 4, 0 );
1194*1da177e4SLinus Torvalds 
1195*1da177e4SLinus Torvalds }
1196*1da177e4SLinus Torvalds 
1197*1da177e4SLinus Torvalds 
1198*1da177e4SLinus Torvalds static int digi_tiocmget( struct usb_serial_port *port, struct file *file )
1199*1da177e4SLinus Torvalds {
1200*1da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
1201*1da177e4SLinus Torvalds 	unsigned int val;
1202*1da177e4SLinus Torvalds 	unsigned long flags;
1203*1da177e4SLinus Torvalds 
1204*1da177e4SLinus Torvalds 	dbg("%s: TOP: port=%d", __FUNCTION__, priv->dp_port_num);
1205*1da177e4SLinus Torvalds 
1206*1da177e4SLinus Torvalds 	spin_lock_irqsave( &priv->dp_port_lock, flags );
1207*1da177e4SLinus Torvalds 	val = priv->dp_modem_signals;
1208*1da177e4SLinus Torvalds 	spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1209*1da177e4SLinus Torvalds 	return val;
1210*1da177e4SLinus Torvalds }
1211*1da177e4SLinus Torvalds 
1212*1da177e4SLinus Torvalds 
1213*1da177e4SLinus Torvalds static int digi_tiocmset( struct usb_serial_port *port, struct file *file,
1214*1da177e4SLinus Torvalds 	unsigned int set, unsigned int clear )
1215*1da177e4SLinus Torvalds {
1216*1da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
1217*1da177e4SLinus Torvalds 	unsigned int val;
1218*1da177e4SLinus Torvalds 	unsigned long flags;
1219*1da177e4SLinus Torvalds 
1220*1da177e4SLinus Torvalds 	dbg("%s: TOP: port=%d", __FUNCTION__, priv->dp_port_num);
1221*1da177e4SLinus Torvalds 
1222*1da177e4SLinus Torvalds 	spin_lock_irqsave( &priv->dp_port_lock, flags );
1223*1da177e4SLinus Torvalds 	val = (priv->dp_modem_signals & ~clear) | set;
1224*1da177e4SLinus Torvalds 	spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1225*1da177e4SLinus Torvalds 	return digi_set_modem_signals( port, val, 1 );
1226*1da177e4SLinus Torvalds }
1227*1da177e4SLinus Torvalds 
1228*1da177e4SLinus Torvalds 
1229*1da177e4SLinus Torvalds static int digi_ioctl( struct usb_serial_port *port, struct file *file,
1230*1da177e4SLinus Torvalds 	unsigned int cmd, unsigned long arg )
1231*1da177e4SLinus Torvalds {
1232*1da177e4SLinus Torvalds 
1233*1da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
1234*1da177e4SLinus Torvalds 
1235*1da177e4SLinus Torvalds dbg( "digi_ioctl: TOP: port=%d, cmd=0x%x", priv->dp_port_num, cmd );
1236*1da177e4SLinus Torvalds 
1237*1da177e4SLinus Torvalds 	switch (cmd) {
1238*1da177e4SLinus Torvalds 
1239*1da177e4SLinus Torvalds 	case TIOCMIWAIT:
1240*1da177e4SLinus Torvalds 		/* wait for any of the 4 modem inputs (DCD,RI,DSR,CTS)*/
1241*1da177e4SLinus Torvalds 		/* TODO */
1242*1da177e4SLinus Torvalds 		return( 0 );
1243*1da177e4SLinus Torvalds 
1244*1da177e4SLinus Torvalds 	case TIOCGICOUNT:
1245*1da177e4SLinus Torvalds 		/* return count of modemline transitions */
1246*1da177e4SLinus Torvalds 		/* TODO */
1247*1da177e4SLinus Torvalds 		return 0;
1248*1da177e4SLinus Torvalds 
1249*1da177e4SLinus Torvalds 	}
1250*1da177e4SLinus Torvalds 
1251*1da177e4SLinus Torvalds 	return( -ENOIOCTLCMD );
1252*1da177e4SLinus Torvalds 
1253*1da177e4SLinus Torvalds }
1254*1da177e4SLinus Torvalds 
1255*1da177e4SLinus Torvalds 
1256*1da177e4SLinus Torvalds static int digi_write( struct usb_serial_port *port, const unsigned char *buf, int count )
1257*1da177e4SLinus Torvalds {
1258*1da177e4SLinus Torvalds 
1259*1da177e4SLinus Torvalds 	int ret,data_len,new_len;
1260*1da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
1261*1da177e4SLinus Torvalds 	unsigned char *data = port->write_urb->transfer_buffer;
1262*1da177e4SLinus Torvalds 	unsigned long flags = 0;
1263*1da177e4SLinus Torvalds 
1264*1da177e4SLinus Torvalds 
1265*1da177e4SLinus Torvalds dbg( "digi_write: TOP: port=%d, count=%d, in_interrupt=%ld",
1266*1da177e4SLinus Torvalds priv->dp_port_num, count, in_interrupt() );
1267*1da177e4SLinus Torvalds 
1268*1da177e4SLinus Torvalds 	/* copy user data (which can sleep) before getting spin lock */
1269*1da177e4SLinus Torvalds 	count = min( count, port->bulk_out_size-2 );
1270*1da177e4SLinus Torvalds 	count = min( 64, count);
1271*1da177e4SLinus Torvalds 
1272*1da177e4SLinus Torvalds 	/* be sure only one write proceeds at a time */
1273*1da177e4SLinus Torvalds 	/* there are races on the port private buffer */
1274*1da177e4SLinus Torvalds 	/* and races to check write_urb->status */
1275*1da177e4SLinus Torvalds 	spin_lock_irqsave( &priv->dp_port_lock, flags );
1276*1da177e4SLinus Torvalds 
1277*1da177e4SLinus Torvalds 	/* wait for urb status clear to submit another urb */
1278*1da177e4SLinus Torvalds 	if( port->write_urb->status == -EINPROGRESS
1279*1da177e4SLinus Torvalds 	|| priv->dp_write_urb_in_use ) {
1280*1da177e4SLinus Torvalds 
1281*1da177e4SLinus Torvalds 		/* buffer data if count is 1 (probably put_char) if possible */
1282*1da177e4SLinus Torvalds 		if( count == 1 && priv->dp_out_buf_len < DIGI_OUT_BUF_SIZE ) {
1283*1da177e4SLinus Torvalds 			priv->dp_out_buf[priv->dp_out_buf_len++] = *buf;
1284*1da177e4SLinus Torvalds 			new_len = 1;
1285*1da177e4SLinus Torvalds 		} else {
1286*1da177e4SLinus Torvalds 			new_len = 0;
1287*1da177e4SLinus Torvalds 		}
1288*1da177e4SLinus Torvalds 
1289*1da177e4SLinus Torvalds 		spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1290*1da177e4SLinus Torvalds 
1291*1da177e4SLinus Torvalds 		return( new_len );
1292*1da177e4SLinus Torvalds 
1293*1da177e4SLinus Torvalds 	}
1294*1da177e4SLinus Torvalds 
1295*1da177e4SLinus Torvalds 	/* allow space for any buffered data and for new data, up to */
1296*1da177e4SLinus Torvalds 	/* transfer buffer size - 2 (for command and length bytes) */
1297*1da177e4SLinus Torvalds 	new_len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
1298*1da177e4SLinus Torvalds 	data_len = new_len + priv->dp_out_buf_len;
1299*1da177e4SLinus Torvalds 
1300*1da177e4SLinus Torvalds 	if( data_len == 0 ) {
1301*1da177e4SLinus Torvalds 		spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1302*1da177e4SLinus Torvalds 		return( 0 );
1303*1da177e4SLinus Torvalds 	}
1304*1da177e4SLinus Torvalds 
1305*1da177e4SLinus Torvalds 	port->write_urb->transfer_buffer_length = data_len+2;
1306*1da177e4SLinus Torvalds 	port->write_urb->dev = port->serial->dev;
1307*1da177e4SLinus Torvalds 
1308*1da177e4SLinus Torvalds 	*data++ = DIGI_CMD_SEND_DATA;
1309*1da177e4SLinus Torvalds 	*data++ = data_len;
1310*1da177e4SLinus Torvalds 
1311*1da177e4SLinus Torvalds 	/* copy in buffered data first */
1312*1da177e4SLinus Torvalds 	memcpy( data, priv->dp_out_buf, priv->dp_out_buf_len );
1313*1da177e4SLinus Torvalds 	data += priv->dp_out_buf_len;
1314*1da177e4SLinus Torvalds 
1315*1da177e4SLinus Torvalds 	/* copy in new data */
1316*1da177e4SLinus Torvalds 	memcpy( data, buf, new_len );
1317*1da177e4SLinus Torvalds 
1318*1da177e4SLinus Torvalds 	if( (ret=usb_submit_urb(port->write_urb, GFP_ATOMIC)) == 0 ) {
1319*1da177e4SLinus Torvalds 		priv->dp_write_urb_in_use = 1;
1320*1da177e4SLinus Torvalds 		ret = new_len;
1321*1da177e4SLinus Torvalds 		priv->dp_out_buf_len = 0;
1322*1da177e4SLinus Torvalds 	}
1323*1da177e4SLinus Torvalds 
1324*1da177e4SLinus Torvalds 	/* return length of new data written, or error */
1325*1da177e4SLinus Torvalds 	spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1326*1da177e4SLinus Torvalds 	if( ret < 0 ) {
1327*1da177e4SLinus Torvalds 		err("%s: usb_submit_urb failed, ret=%d, port=%d", __FUNCTION__,
1328*1da177e4SLinus Torvalds 			ret, priv->dp_port_num );
1329*1da177e4SLinus Torvalds 	}
1330*1da177e4SLinus Torvalds 
1331*1da177e4SLinus Torvalds dbg( "digi_write: returning %d", ret );
1332*1da177e4SLinus Torvalds 	return( ret );
1333*1da177e4SLinus Torvalds 
1334*1da177e4SLinus Torvalds }
1335*1da177e4SLinus Torvalds 
1336*1da177e4SLinus Torvalds 
1337*1da177e4SLinus Torvalds static void digi_write_bulk_callback( struct urb *urb, struct pt_regs *regs )
1338*1da177e4SLinus Torvalds {
1339*1da177e4SLinus Torvalds 
1340*1da177e4SLinus Torvalds 	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1341*1da177e4SLinus Torvalds 	struct usb_serial *serial;
1342*1da177e4SLinus Torvalds 	struct digi_port *priv;
1343*1da177e4SLinus Torvalds 	struct digi_serial *serial_priv;
1344*1da177e4SLinus Torvalds 	int ret = 0;
1345*1da177e4SLinus Torvalds 
1346*1da177e4SLinus Torvalds 
1347*1da177e4SLinus Torvalds dbg( "digi_write_bulk_callback: TOP, urb->status=%d", urb->status );
1348*1da177e4SLinus Torvalds 
1349*1da177e4SLinus Torvalds 	/* port and serial sanity check */
1350*1da177e4SLinus Torvalds 	if( port == NULL || (priv=usb_get_serial_port_data(port)) == NULL ) {
1351*1da177e4SLinus Torvalds 		err("%s: port or port->private is NULL, status=%d", __FUNCTION__,
1352*1da177e4SLinus Torvalds 			urb->status );
1353*1da177e4SLinus Torvalds 		return;
1354*1da177e4SLinus Torvalds 	}
1355*1da177e4SLinus Torvalds 	serial = port->serial;
1356*1da177e4SLinus Torvalds 	if( serial == NULL || (serial_priv=usb_get_serial_data(serial)) == NULL ) {
1357*1da177e4SLinus Torvalds 		err("%s: serial or serial->private is NULL, status=%d", __FUNCTION__, urb->status );
1358*1da177e4SLinus Torvalds 		return;
1359*1da177e4SLinus Torvalds 	}
1360*1da177e4SLinus Torvalds 
1361*1da177e4SLinus Torvalds 	/* handle oob callback */
1362*1da177e4SLinus Torvalds 	if( priv->dp_port_num == serial_priv->ds_oob_port_num ) {
1363*1da177e4SLinus Torvalds 		dbg( "digi_write_bulk_callback: oob callback" );
1364*1da177e4SLinus Torvalds 		spin_lock( &priv->dp_port_lock );
1365*1da177e4SLinus Torvalds 		priv->dp_write_urb_in_use = 0;
1366*1da177e4SLinus Torvalds 		wake_up_interruptible( &port->write_wait );
1367*1da177e4SLinus Torvalds 		spin_unlock( &priv->dp_port_lock );
1368*1da177e4SLinus Torvalds 		return;
1369*1da177e4SLinus Torvalds 	}
1370*1da177e4SLinus Torvalds 
1371*1da177e4SLinus Torvalds 	/* try to send any buffered data on this port, if it is open */
1372*1da177e4SLinus Torvalds 	spin_lock( &priv->dp_port_lock );
1373*1da177e4SLinus Torvalds 	priv->dp_write_urb_in_use = 0;
1374*1da177e4SLinus Torvalds 	if( port->open_count && port->write_urb->status != -EINPROGRESS
1375*1da177e4SLinus Torvalds 	&& priv->dp_out_buf_len > 0 ) {
1376*1da177e4SLinus Torvalds 
1377*1da177e4SLinus Torvalds 		*((unsigned char *)(port->write_urb->transfer_buffer))
1378*1da177e4SLinus Torvalds 			= (unsigned char)DIGI_CMD_SEND_DATA;
1379*1da177e4SLinus Torvalds 		*((unsigned char *)(port->write_urb->transfer_buffer)+1)
1380*1da177e4SLinus Torvalds 			= (unsigned char)priv->dp_out_buf_len;
1381*1da177e4SLinus Torvalds 
1382*1da177e4SLinus Torvalds 		port->write_urb->transfer_buffer_length
1383*1da177e4SLinus Torvalds 			= priv->dp_out_buf_len+2;
1384*1da177e4SLinus Torvalds 		port->write_urb->dev = serial->dev;
1385*1da177e4SLinus Torvalds 
1386*1da177e4SLinus Torvalds 		memcpy( port->write_urb->transfer_buffer+2, priv->dp_out_buf,
1387*1da177e4SLinus Torvalds 			priv->dp_out_buf_len );
1388*1da177e4SLinus Torvalds 
1389*1da177e4SLinus Torvalds 		if( (ret=usb_submit_urb(port->write_urb, GFP_ATOMIC)) == 0 ) {
1390*1da177e4SLinus Torvalds 			priv->dp_write_urb_in_use = 1;
1391*1da177e4SLinus Torvalds 			priv->dp_out_buf_len = 0;
1392*1da177e4SLinus Torvalds 		}
1393*1da177e4SLinus Torvalds 
1394*1da177e4SLinus Torvalds 	}
1395*1da177e4SLinus Torvalds 
1396*1da177e4SLinus Torvalds 	/* wake up processes sleeping on writes immediately */
1397*1da177e4SLinus Torvalds 	digi_wakeup_write( port );
1398*1da177e4SLinus Torvalds 
1399*1da177e4SLinus Torvalds 	/* also queue up a wakeup at scheduler time, in case we */
1400*1da177e4SLinus Torvalds 	/* lost the race in write_chan(). */
1401*1da177e4SLinus Torvalds 	schedule_work(&priv->dp_wakeup_work);
1402*1da177e4SLinus Torvalds 
1403*1da177e4SLinus Torvalds 	spin_unlock( &priv->dp_port_lock );
1404*1da177e4SLinus Torvalds 
1405*1da177e4SLinus Torvalds 	if( ret ) {
1406*1da177e4SLinus Torvalds 		err("%s: usb_submit_urb failed, ret=%d, port=%d", __FUNCTION__,
1407*1da177e4SLinus Torvalds 			ret, priv->dp_port_num );
1408*1da177e4SLinus Torvalds 	}
1409*1da177e4SLinus Torvalds 
1410*1da177e4SLinus Torvalds }
1411*1da177e4SLinus Torvalds 
1412*1da177e4SLinus Torvalds 
1413*1da177e4SLinus Torvalds static int digi_write_room( struct usb_serial_port *port )
1414*1da177e4SLinus Torvalds {
1415*1da177e4SLinus Torvalds 
1416*1da177e4SLinus Torvalds 	int room;
1417*1da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
1418*1da177e4SLinus Torvalds 	unsigned long flags = 0;
1419*1da177e4SLinus Torvalds 
1420*1da177e4SLinus Torvalds 
1421*1da177e4SLinus Torvalds 	spin_lock_irqsave( &priv->dp_port_lock, flags );
1422*1da177e4SLinus Torvalds 
1423*1da177e4SLinus Torvalds 	if( port->write_urb->status == -EINPROGRESS
1424*1da177e4SLinus Torvalds 	|| priv->dp_write_urb_in_use )
1425*1da177e4SLinus Torvalds 		room = 0;
1426*1da177e4SLinus Torvalds 	else
1427*1da177e4SLinus Torvalds 		room = port->bulk_out_size - 2 - priv->dp_out_buf_len;
1428*1da177e4SLinus Torvalds 
1429*1da177e4SLinus Torvalds 	spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1430*1da177e4SLinus Torvalds 
1431*1da177e4SLinus Torvalds dbg( "digi_write_room: port=%d, room=%d", priv->dp_port_num, room );
1432*1da177e4SLinus Torvalds 	return( room );
1433*1da177e4SLinus Torvalds 
1434*1da177e4SLinus Torvalds }
1435*1da177e4SLinus Torvalds 
1436*1da177e4SLinus Torvalds 
1437*1da177e4SLinus Torvalds static int digi_chars_in_buffer( struct usb_serial_port *port )
1438*1da177e4SLinus Torvalds {
1439*1da177e4SLinus Torvalds 
1440*1da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
1441*1da177e4SLinus Torvalds 
1442*1da177e4SLinus Torvalds 
1443*1da177e4SLinus Torvalds 	if( port->write_urb->status == -EINPROGRESS
1444*1da177e4SLinus Torvalds 	|| priv->dp_write_urb_in_use ) {
1445*1da177e4SLinus Torvalds dbg( "digi_chars_in_buffer: port=%d, chars=%d", priv->dp_port_num, port->bulk_out_size - 2 );
1446*1da177e4SLinus Torvalds 		/* return( port->bulk_out_size - 2 ); */
1447*1da177e4SLinus Torvalds 		return( 256 );
1448*1da177e4SLinus Torvalds 	} else {
1449*1da177e4SLinus Torvalds dbg( "digi_chars_in_buffer: port=%d, chars=%d", priv->dp_port_num, priv->dp_out_buf_len );
1450*1da177e4SLinus Torvalds 		return( priv->dp_out_buf_len );
1451*1da177e4SLinus Torvalds 	}
1452*1da177e4SLinus Torvalds 
1453*1da177e4SLinus Torvalds }
1454*1da177e4SLinus Torvalds 
1455*1da177e4SLinus Torvalds 
1456*1da177e4SLinus Torvalds static int digi_open( struct usb_serial_port *port, struct file *filp )
1457*1da177e4SLinus Torvalds {
1458*1da177e4SLinus Torvalds 
1459*1da177e4SLinus Torvalds 	int ret;
1460*1da177e4SLinus Torvalds 	unsigned char buf[32];
1461*1da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
1462*1da177e4SLinus Torvalds 	struct termios not_termios;
1463*1da177e4SLinus Torvalds 	unsigned long flags = 0;
1464*1da177e4SLinus Torvalds 
1465*1da177e4SLinus Torvalds 
1466*1da177e4SLinus Torvalds dbg( "digi_open: TOP: port=%d, open_count=%d", priv->dp_port_num, port->open_count );
1467*1da177e4SLinus Torvalds 
1468*1da177e4SLinus Torvalds 	/* be sure the device is started up */
1469*1da177e4SLinus Torvalds 	if( digi_startup_device( port->serial ) != 0 )
1470*1da177e4SLinus Torvalds 		return( -ENXIO );
1471*1da177e4SLinus Torvalds 
1472*1da177e4SLinus Torvalds 	spin_lock_irqsave( &priv->dp_port_lock, flags );
1473*1da177e4SLinus Torvalds 
1474*1da177e4SLinus Torvalds 	/* don't wait on a close in progress for non-blocking opens */
1475*1da177e4SLinus Torvalds 	if( priv->dp_in_close && (filp->f_flags&(O_NDELAY|O_NONBLOCK)) == 0 ) {
1476*1da177e4SLinus Torvalds 		spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1477*1da177e4SLinus Torvalds 		return( -EAGAIN );
1478*1da177e4SLinus Torvalds 	}
1479*1da177e4SLinus Torvalds 
1480*1da177e4SLinus Torvalds 	/* wait for a close in progress to finish */
1481*1da177e4SLinus Torvalds 	while( priv->dp_in_close ) {
1482*1da177e4SLinus Torvalds 		cond_wait_interruptible_timeout_irqrestore(
1483*1da177e4SLinus Torvalds 			&priv->dp_close_wait, DIGI_RETRY_TIMEOUT,
1484*1da177e4SLinus Torvalds 			&priv->dp_port_lock, flags );
1485*1da177e4SLinus Torvalds 		if( signal_pending(current) ) {
1486*1da177e4SLinus Torvalds 			return( -EINTR );
1487*1da177e4SLinus Torvalds 		}
1488*1da177e4SLinus Torvalds 		spin_lock_irqsave( &priv->dp_port_lock, flags );
1489*1da177e4SLinus Torvalds 	}
1490*1da177e4SLinus Torvalds 
1491*1da177e4SLinus Torvalds 	spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1492*1da177e4SLinus Torvalds 
1493*1da177e4SLinus Torvalds 	/* read modem signals automatically whenever they change */
1494*1da177e4SLinus Torvalds 	buf[0] = DIGI_CMD_READ_INPUT_SIGNALS;
1495*1da177e4SLinus Torvalds 	buf[1] = priv->dp_port_num;
1496*1da177e4SLinus Torvalds 	buf[2] = DIGI_ENABLE;
1497*1da177e4SLinus Torvalds 	buf[3] = 0;
1498*1da177e4SLinus Torvalds 
1499*1da177e4SLinus Torvalds 	/* flush fifos */
1500*1da177e4SLinus Torvalds 	buf[4] = DIGI_CMD_IFLUSH_FIFO;
1501*1da177e4SLinus Torvalds 	buf[5] = priv->dp_port_num;
1502*1da177e4SLinus Torvalds 	buf[6] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
1503*1da177e4SLinus Torvalds 	buf[7] = 0;
1504*1da177e4SLinus Torvalds 
1505*1da177e4SLinus Torvalds 	if( (ret=digi_write_oob_command( port, buf, 8, 1 )) != 0 )
1506*1da177e4SLinus Torvalds 		dbg( "digi_open: write oob failed, ret=%d", ret );
1507*1da177e4SLinus Torvalds 
1508*1da177e4SLinus Torvalds 	/* set termios settings */
1509*1da177e4SLinus Torvalds 	not_termios.c_cflag = ~port->tty->termios->c_cflag;
1510*1da177e4SLinus Torvalds 	not_termios.c_iflag = ~port->tty->termios->c_iflag;
1511*1da177e4SLinus Torvalds 	digi_set_termios( port, &not_termios );
1512*1da177e4SLinus Torvalds 
1513*1da177e4SLinus Torvalds 	/* set DTR and RTS */
1514*1da177e4SLinus Torvalds 	digi_set_modem_signals( port, TIOCM_DTR|TIOCM_RTS, 1 );
1515*1da177e4SLinus Torvalds 
1516*1da177e4SLinus Torvalds 	return( 0 );
1517*1da177e4SLinus Torvalds 
1518*1da177e4SLinus Torvalds }
1519*1da177e4SLinus Torvalds 
1520*1da177e4SLinus Torvalds 
1521*1da177e4SLinus Torvalds static void digi_close( struct usb_serial_port *port, struct file *filp )
1522*1da177e4SLinus Torvalds {
1523*1da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
1524*1da177e4SLinus Torvalds 	int ret;
1525*1da177e4SLinus Torvalds 	unsigned char buf[32];
1526*1da177e4SLinus Torvalds 	struct tty_struct *tty = port->tty;
1527*1da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
1528*1da177e4SLinus Torvalds 	unsigned long flags = 0;
1529*1da177e4SLinus Torvalds 
1530*1da177e4SLinus Torvalds 
1531*1da177e4SLinus Torvalds dbg( "digi_close: TOP: port=%d, open_count=%d", priv->dp_port_num, port->open_count );
1532*1da177e4SLinus Torvalds 
1533*1da177e4SLinus Torvalds 
1534*1da177e4SLinus Torvalds 	/* if disconnected, just clear flags */
1535*1da177e4SLinus Torvalds 	if (!usb_get_intfdata(port->serial->interface))
1536*1da177e4SLinus Torvalds 		goto exit;
1537*1da177e4SLinus Torvalds 
1538*1da177e4SLinus Torvalds 	/* do cleanup only after final close on this port */
1539*1da177e4SLinus Torvalds 	spin_lock_irqsave( &priv->dp_port_lock, flags );
1540*1da177e4SLinus Torvalds 	priv->dp_in_close = 1;
1541*1da177e4SLinus Torvalds 	spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1542*1da177e4SLinus Torvalds 
1543*1da177e4SLinus Torvalds 	/* tell line discipline to process only XON/XOFF */
1544*1da177e4SLinus Torvalds 	tty->closing = 1;
1545*1da177e4SLinus Torvalds 
1546*1da177e4SLinus Torvalds 	/* wait for output to drain */
1547*1da177e4SLinus Torvalds 	if( (filp->f_flags&(O_NDELAY|O_NONBLOCK)) == 0 ) {
1548*1da177e4SLinus Torvalds 		tty_wait_until_sent( tty, DIGI_CLOSE_TIMEOUT );
1549*1da177e4SLinus Torvalds 	}
1550*1da177e4SLinus Torvalds 
1551*1da177e4SLinus Torvalds 	/* flush driver and line discipline buffers */
1552*1da177e4SLinus Torvalds 	if( tty->driver->flush_buffer )
1553*1da177e4SLinus Torvalds 		tty->driver->flush_buffer( tty );
1554*1da177e4SLinus Torvalds 	tty_ldisc_flush(tty);
1555*1da177e4SLinus Torvalds 
1556*1da177e4SLinus Torvalds 	if (port->serial->dev) {
1557*1da177e4SLinus Torvalds 		/* wait for transmit idle */
1558*1da177e4SLinus Torvalds 		if( (filp->f_flags&(O_NDELAY|O_NONBLOCK)) == 0 ) {
1559*1da177e4SLinus Torvalds 			digi_transmit_idle( port, DIGI_CLOSE_TIMEOUT );
1560*1da177e4SLinus Torvalds 		}
1561*1da177e4SLinus Torvalds 
1562*1da177e4SLinus Torvalds 		/* drop DTR and RTS */
1563*1da177e4SLinus Torvalds 		digi_set_modem_signals( port, 0, 0 );
1564*1da177e4SLinus Torvalds 
1565*1da177e4SLinus Torvalds 		/* disable input flow control */
1566*1da177e4SLinus Torvalds 		buf[0] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
1567*1da177e4SLinus Torvalds 		buf[1] = priv->dp_port_num;
1568*1da177e4SLinus Torvalds 		buf[2] = DIGI_DISABLE;
1569*1da177e4SLinus Torvalds 		buf[3] = 0;
1570*1da177e4SLinus Torvalds 
1571*1da177e4SLinus Torvalds 		/* disable output flow control */
1572*1da177e4SLinus Torvalds 		buf[4] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
1573*1da177e4SLinus Torvalds 		buf[5] = priv->dp_port_num;
1574*1da177e4SLinus Torvalds 		buf[6] = DIGI_DISABLE;
1575*1da177e4SLinus Torvalds 		buf[7] = 0;
1576*1da177e4SLinus Torvalds 
1577*1da177e4SLinus Torvalds 		/* disable reading modem signals automatically */
1578*1da177e4SLinus Torvalds 		buf[8] = DIGI_CMD_READ_INPUT_SIGNALS;
1579*1da177e4SLinus Torvalds 		buf[9] = priv->dp_port_num;
1580*1da177e4SLinus Torvalds 		buf[10] = DIGI_DISABLE;
1581*1da177e4SLinus Torvalds 		buf[11] = 0;
1582*1da177e4SLinus Torvalds 
1583*1da177e4SLinus Torvalds 		/* disable receive */
1584*1da177e4SLinus Torvalds 		buf[12] = DIGI_CMD_RECEIVE_ENABLE;
1585*1da177e4SLinus Torvalds 		buf[13] = priv->dp_port_num;
1586*1da177e4SLinus Torvalds 		buf[14] = DIGI_DISABLE;
1587*1da177e4SLinus Torvalds 		buf[15] = 0;
1588*1da177e4SLinus Torvalds 
1589*1da177e4SLinus Torvalds 		/* flush fifos */
1590*1da177e4SLinus Torvalds 		buf[16] = DIGI_CMD_IFLUSH_FIFO;
1591*1da177e4SLinus Torvalds 		buf[17] = priv->dp_port_num;
1592*1da177e4SLinus Torvalds 		buf[18] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
1593*1da177e4SLinus Torvalds 		buf[19] = 0;
1594*1da177e4SLinus Torvalds 
1595*1da177e4SLinus Torvalds 		if( (ret=digi_write_oob_command( port, buf, 20, 0 )) != 0 )
1596*1da177e4SLinus Torvalds 			dbg( "digi_close: write oob failed, ret=%d", ret );
1597*1da177e4SLinus Torvalds 
1598*1da177e4SLinus Torvalds 		/* wait for final commands on oob port to complete */
1599*1da177e4SLinus Torvalds 		prepare_to_wait(&priv->dp_flush_wait, &wait, TASK_UNINTERRUPTIBLE);
1600*1da177e4SLinus Torvalds 		schedule_timeout(DIGI_CLOSE_TIMEOUT);
1601*1da177e4SLinus Torvalds 		finish_wait(&priv->dp_flush_wait, &wait);
1602*1da177e4SLinus Torvalds 
1603*1da177e4SLinus Torvalds 		/* shutdown any outstanding bulk writes */
1604*1da177e4SLinus Torvalds 		usb_kill_urb(port->write_urb);
1605*1da177e4SLinus Torvalds 	}
1606*1da177e4SLinus Torvalds 
1607*1da177e4SLinus Torvalds 	tty->closing = 0;
1608*1da177e4SLinus Torvalds 
1609*1da177e4SLinus Torvalds exit:
1610*1da177e4SLinus Torvalds 	spin_lock_irqsave( &priv->dp_port_lock, flags );
1611*1da177e4SLinus Torvalds 	priv->dp_write_urb_in_use = 0;
1612*1da177e4SLinus Torvalds 	priv->dp_in_close = 0;
1613*1da177e4SLinus Torvalds 	wake_up_interruptible( &priv->dp_close_wait );
1614*1da177e4SLinus Torvalds 	spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1615*1da177e4SLinus Torvalds 
1616*1da177e4SLinus Torvalds dbg( "digi_close: done" );
1617*1da177e4SLinus Torvalds }
1618*1da177e4SLinus Torvalds 
1619*1da177e4SLinus Torvalds 
1620*1da177e4SLinus Torvalds /*
1621*1da177e4SLinus Torvalds *  Digi Startup Device
1622*1da177e4SLinus Torvalds *
1623*1da177e4SLinus Torvalds *  Starts reads on all ports.  Must be called AFTER startup, with
1624*1da177e4SLinus Torvalds *  urbs initialized.  Returns 0 if successful, non-zero error otherwise.
1625*1da177e4SLinus Torvalds */
1626*1da177e4SLinus Torvalds 
1627*1da177e4SLinus Torvalds static int digi_startup_device( struct usb_serial *serial )
1628*1da177e4SLinus Torvalds {
1629*1da177e4SLinus Torvalds 
1630*1da177e4SLinus Torvalds 	int i,ret = 0;
1631*1da177e4SLinus Torvalds 	struct digi_serial *serial_priv = usb_get_serial_data(serial);
1632*1da177e4SLinus Torvalds 	struct usb_serial_port *port;
1633*1da177e4SLinus Torvalds 
1634*1da177e4SLinus Torvalds 
1635*1da177e4SLinus Torvalds 	/* be sure this happens exactly once */
1636*1da177e4SLinus Torvalds 	spin_lock( &serial_priv->ds_serial_lock );
1637*1da177e4SLinus Torvalds 	if( serial_priv->ds_device_started ) {
1638*1da177e4SLinus Torvalds 		spin_unlock( &serial_priv->ds_serial_lock );
1639*1da177e4SLinus Torvalds 		return( 0 );
1640*1da177e4SLinus Torvalds 	}
1641*1da177e4SLinus Torvalds 	serial_priv->ds_device_started = 1;
1642*1da177e4SLinus Torvalds 	spin_unlock( &serial_priv->ds_serial_lock );
1643*1da177e4SLinus Torvalds 
1644*1da177e4SLinus Torvalds 	/* start reading from each bulk in endpoint for the device */
1645*1da177e4SLinus Torvalds 	/* set USB_DISABLE_SPD flag for write bulk urbs */
1646*1da177e4SLinus Torvalds 	for( i=0; i<serial->type->num_ports+1; i++ ) {
1647*1da177e4SLinus Torvalds 
1648*1da177e4SLinus Torvalds 		port = serial->port[i];
1649*1da177e4SLinus Torvalds 
1650*1da177e4SLinus Torvalds 		port->write_urb->dev = port->serial->dev;
1651*1da177e4SLinus Torvalds 
1652*1da177e4SLinus Torvalds 		if( (ret=usb_submit_urb(port->read_urb, GFP_KERNEL)) != 0 ) {
1653*1da177e4SLinus Torvalds 			err("%s: usb_submit_urb failed, ret=%d, port=%d", __FUNCTION__,
1654*1da177e4SLinus Torvalds 			ret, i );
1655*1da177e4SLinus Torvalds 			break;
1656*1da177e4SLinus Torvalds 		}
1657*1da177e4SLinus Torvalds 
1658*1da177e4SLinus Torvalds 	}
1659*1da177e4SLinus Torvalds 
1660*1da177e4SLinus Torvalds 	return( ret );
1661*1da177e4SLinus Torvalds 
1662*1da177e4SLinus Torvalds }
1663*1da177e4SLinus Torvalds 
1664*1da177e4SLinus Torvalds 
1665*1da177e4SLinus Torvalds static int digi_startup( struct usb_serial *serial )
1666*1da177e4SLinus Torvalds {
1667*1da177e4SLinus Torvalds 
1668*1da177e4SLinus Torvalds 	int i;
1669*1da177e4SLinus Torvalds 	struct digi_port *priv;
1670*1da177e4SLinus Torvalds 	struct digi_serial *serial_priv;
1671*1da177e4SLinus Torvalds 
1672*1da177e4SLinus Torvalds 
1673*1da177e4SLinus Torvalds dbg( "digi_startup: TOP" );
1674*1da177e4SLinus Torvalds 
1675*1da177e4SLinus Torvalds 	/* allocate the private data structures for all ports */
1676*1da177e4SLinus Torvalds 	/* number of regular ports + 1 for the out-of-band port */
1677*1da177e4SLinus Torvalds 	for( i=0; i<serial->type->num_ports+1; i++ ) {
1678*1da177e4SLinus Torvalds 
1679*1da177e4SLinus Torvalds 		/* allocate port private structure */
1680*1da177e4SLinus Torvalds 		priv = (struct digi_port *)kmalloc( sizeof(struct digi_port),
1681*1da177e4SLinus Torvalds 			GFP_KERNEL );
1682*1da177e4SLinus Torvalds 		if( priv == (struct digi_port *)0 ) {
1683*1da177e4SLinus Torvalds 			while( --i >= 0 )
1684*1da177e4SLinus Torvalds 				kfree( usb_get_serial_port_data(serial->port[i]) );
1685*1da177e4SLinus Torvalds 			return( 1 );			/* error */
1686*1da177e4SLinus Torvalds 		}
1687*1da177e4SLinus Torvalds 
1688*1da177e4SLinus Torvalds 		/* initialize port private structure */
1689*1da177e4SLinus Torvalds 		spin_lock_init( &priv->dp_port_lock );
1690*1da177e4SLinus Torvalds 		priv->dp_port_num = i;
1691*1da177e4SLinus Torvalds 		priv->dp_out_buf_len = 0;
1692*1da177e4SLinus Torvalds 		priv->dp_in_buf_len = 0;
1693*1da177e4SLinus Torvalds 		priv->dp_write_urb_in_use = 0;
1694*1da177e4SLinus Torvalds 		priv->dp_modem_signals = 0;
1695*1da177e4SLinus Torvalds 		init_waitqueue_head( &priv->dp_modem_change_wait );
1696*1da177e4SLinus Torvalds 		priv->dp_transmit_idle = 0;
1697*1da177e4SLinus Torvalds 		init_waitqueue_head( &priv->dp_transmit_idle_wait );
1698*1da177e4SLinus Torvalds 		priv->dp_throttled = 0;
1699*1da177e4SLinus Torvalds 		priv->dp_throttle_restart = 0;
1700*1da177e4SLinus Torvalds 		init_waitqueue_head( &priv->dp_flush_wait );
1701*1da177e4SLinus Torvalds 		priv->dp_in_close = 0;
1702*1da177e4SLinus Torvalds 		init_waitqueue_head( &priv->dp_close_wait );
1703*1da177e4SLinus Torvalds 		INIT_WORK(&priv->dp_wakeup_work,
1704*1da177e4SLinus Torvalds 				digi_wakeup_write_lock, serial->port[i]);
1705*1da177e4SLinus Torvalds 
1706*1da177e4SLinus Torvalds 		/* initialize write wait queue for this port */
1707*1da177e4SLinus Torvalds 		init_waitqueue_head( &serial->port[i]->write_wait );
1708*1da177e4SLinus Torvalds 
1709*1da177e4SLinus Torvalds 		usb_set_serial_port_data(serial->port[i], priv);
1710*1da177e4SLinus Torvalds 	}
1711*1da177e4SLinus Torvalds 
1712*1da177e4SLinus Torvalds 	/* allocate serial private structure */
1713*1da177e4SLinus Torvalds 	serial_priv = (struct digi_serial *)kmalloc( sizeof(struct digi_serial),
1714*1da177e4SLinus Torvalds 		GFP_KERNEL );
1715*1da177e4SLinus Torvalds 	if( serial_priv == (struct digi_serial *)0 ) {
1716*1da177e4SLinus Torvalds 		for( i=0; i<serial->type->num_ports+1; i++ )
1717*1da177e4SLinus Torvalds 			kfree( usb_get_serial_port_data(serial->port[i]) );
1718*1da177e4SLinus Torvalds 		return( 1 );			/* error */
1719*1da177e4SLinus Torvalds 	}
1720*1da177e4SLinus Torvalds 
1721*1da177e4SLinus Torvalds 	/* initialize serial private structure */
1722*1da177e4SLinus Torvalds 	spin_lock_init( &serial_priv->ds_serial_lock );
1723*1da177e4SLinus Torvalds 	serial_priv->ds_oob_port_num = serial->type->num_ports;
1724*1da177e4SLinus Torvalds 	serial_priv->ds_oob_port = serial->port[serial_priv->ds_oob_port_num];
1725*1da177e4SLinus Torvalds 	serial_priv->ds_device_started = 0;
1726*1da177e4SLinus Torvalds 	usb_set_serial_data(serial, serial_priv);
1727*1da177e4SLinus Torvalds 
1728*1da177e4SLinus Torvalds 	return( 0 );
1729*1da177e4SLinus Torvalds 
1730*1da177e4SLinus Torvalds }
1731*1da177e4SLinus Torvalds 
1732*1da177e4SLinus Torvalds 
1733*1da177e4SLinus Torvalds static void digi_shutdown( struct usb_serial *serial )
1734*1da177e4SLinus Torvalds {
1735*1da177e4SLinus Torvalds 
1736*1da177e4SLinus Torvalds 	int i;
1737*1da177e4SLinus Torvalds 
1738*1da177e4SLinus Torvalds 
1739*1da177e4SLinus Torvalds dbg( "digi_shutdown: TOP, in_interrupt()=%ld", in_interrupt() );
1740*1da177e4SLinus Torvalds 
1741*1da177e4SLinus Torvalds 	/* stop reads and writes on all ports */
1742*1da177e4SLinus Torvalds 	for( i=0; i<serial->type->num_ports+1; i++ ) {
1743*1da177e4SLinus Torvalds 		usb_kill_urb(serial->port[i]->read_urb);
1744*1da177e4SLinus Torvalds 		usb_kill_urb(serial->port[i]->write_urb);
1745*1da177e4SLinus Torvalds 	}
1746*1da177e4SLinus Torvalds 
1747*1da177e4SLinus Torvalds 	/* free the private data structures for all ports */
1748*1da177e4SLinus Torvalds 	/* number of regular ports + 1 for the out-of-band port */
1749*1da177e4SLinus Torvalds 	for( i=0; i<serial->type->num_ports+1; i++ )
1750*1da177e4SLinus Torvalds 		kfree( usb_get_serial_port_data(serial->port[i]) );
1751*1da177e4SLinus Torvalds 	kfree( usb_get_serial_data(serial) );
1752*1da177e4SLinus Torvalds }
1753*1da177e4SLinus Torvalds 
1754*1da177e4SLinus Torvalds 
1755*1da177e4SLinus Torvalds static void digi_read_bulk_callback( struct urb *urb, struct pt_regs *regs )
1756*1da177e4SLinus Torvalds {
1757*1da177e4SLinus Torvalds 
1758*1da177e4SLinus Torvalds 	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1759*1da177e4SLinus Torvalds 	struct digi_port *priv;
1760*1da177e4SLinus Torvalds 	struct digi_serial *serial_priv;
1761*1da177e4SLinus Torvalds 	int ret;
1762*1da177e4SLinus Torvalds 
1763*1da177e4SLinus Torvalds 
1764*1da177e4SLinus Torvalds dbg( "digi_read_bulk_callback: TOP" );
1765*1da177e4SLinus Torvalds 
1766*1da177e4SLinus Torvalds 	/* port sanity check, do not resubmit if port is not valid */
1767*1da177e4SLinus Torvalds 	if( port == NULL || (priv=usb_get_serial_port_data(port)) == NULL ) {
1768*1da177e4SLinus Torvalds 		err("%s: port or port->private is NULL, status=%d", __FUNCTION__,
1769*1da177e4SLinus Torvalds 			urb->status );
1770*1da177e4SLinus Torvalds 		return;
1771*1da177e4SLinus Torvalds 	}
1772*1da177e4SLinus Torvalds 	if( port->serial == NULL
1773*1da177e4SLinus Torvalds 	|| (serial_priv=usb_get_serial_data(port->serial)) == NULL ) {
1774*1da177e4SLinus Torvalds 		err("%s: serial is bad or serial->private is NULL, status=%d", __FUNCTION__, urb->status );
1775*1da177e4SLinus Torvalds 		return;
1776*1da177e4SLinus Torvalds 	}
1777*1da177e4SLinus Torvalds 
1778*1da177e4SLinus Torvalds 	/* do not resubmit urb if it has any status error */
1779*1da177e4SLinus Torvalds 	if( urb->status ) {
1780*1da177e4SLinus Torvalds 		err("%s: nonzero read bulk status: status=%d, port=%d", __FUNCTION__, urb->status, priv->dp_port_num );
1781*1da177e4SLinus Torvalds 		return;
1782*1da177e4SLinus Torvalds 	}
1783*1da177e4SLinus Torvalds 
1784*1da177e4SLinus Torvalds 	/* handle oob or inb callback, do not resubmit if error */
1785*1da177e4SLinus Torvalds 	if( priv->dp_port_num == serial_priv->ds_oob_port_num ) {
1786*1da177e4SLinus Torvalds 		if( digi_read_oob_callback( urb ) != 0 )
1787*1da177e4SLinus Torvalds 			return;
1788*1da177e4SLinus Torvalds 	} else {
1789*1da177e4SLinus Torvalds 		if( digi_read_inb_callback( urb ) != 0 )
1790*1da177e4SLinus Torvalds 			return;
1791*1da177e4SLinus Torvalds 	}
1792*1da177e4SLinus Torvalds 
1793*1da177e4SLinus Torvalds 	/* continue read */
1794*1da177e4SLinus Torvalds 	urb->dev = port->serial->dev;
1795*1da177e4SLinus Torvalds 	if( (ret=usb_submit_urb(urb, GFP_ATOMIC)) != 0 ) {
1796*1da177e4SLinus Torvalds 		err("%s: failed resubmitting urb, ret=%d, port=%d", __FUNCTION__,
1797*1da177e4SLinus Torvalds 			ret, priv->dp_port_num );
1798*1da177e4SLinus Torvalds 	}
1799*1da177e4SLinus Torvalds 
1800*1da177e4SLinus Torvalds }
1801*1da177e4SLinus Torvalds 
1802*1da177e4SLinus Torvalds 
1803*1da177e4SLinus Torvalds /*
1804*1da177e4SLinus Torvalds *  Digi Read INB Callback
1805*1da177e4SLinus Torvalds *
1806*1da177e4SLinus Torvalds *  Digi Read INB Callback handles reads on the in band ports, sending
1807*1da177e4SLinus Torvalds *  the data on to the tty subsystem.  When called we know port and
1808*1da177e4SLinus Torvalds *  port->private are not NULL and port->serial has been validated.
1809*1da177e4SLinus Torvalds *  It returns 0 if successful, 1 if successful but the port is
1810*1da177e4SLinus Torvalds *  throttled, and -1 if the sanity checks failed.
1811*1da177e4SLinus Torvalds */
1812*1da177e4SLinus Torvalds 
1813*1da177e4SLinus Torvalds static int digi_read_inb_callback( struct urb *urb )
1814*1da177e4SLinus Torvalds {
1815*1da177e4SLinus Torvalds 
1816*1da177e4SLinus Torvalds 	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1817*1da177e4SLinus Torvalds 	struct tty_struct *tty = port->tty;
1818*1da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
1819*1da177e4SLinus Torvalds 	int opcode = ((unsigned char *)urb->transfer_buffer)[0];
1820*1da177e4SLinus Torvalds 	int len = ((unsigned char *)urb->transfer_buffer)[1];
1821*1da177e4SLinus Torvalds 	int status = ((unsigned char *)urb->transfer_buffer)[2];
1822*1da177e4SLinus Torvalds 	unsigned char *data = ((unsigned char *)urb->transfer_buffer)+3;
1823*1da177e4SLinus Torvalds 	int flag,throttled;
1824*1da177e4SLinus Torvalds 
1825*1da177e4SLinus Torvalds 	/* do not process callbacks on closed ports */
1826*1da177e4SLinus Torvalds 	/* but do continue the read chain */
1827*1da177e4SLinus Torvalds 	if( port->open_count == 0 )
1828*1da177e4SLinus Torvalds 		return( 0 );
1829*1da177e4SLinus Torvalds 
1830*1da177e4SLinus Torvalds 	/* short/multiple packet check */
1831*1da177e4SLinus Torvalds 	if( urb->actual_length != len + 2 ) {
1832*1da177e4SLinus Torvalds      		err("%s: INCOMPLETE OR MULTIPLE PACKET, urb->status=%d, port=%d, opcode=%d, len=%d, actual_length=%d, status=%d", __FUNCTION__, urb->status, priv->dp_port_num, opcode, len, urb->actual_length, status );
1833*1da177e4SLinus Torvalds 		return( -1 );
1834*1da177e4SLinus Torvalds 	}
1835*1da177e4SLinus Torvalds 
1836*1da177e4SLinus Torvalds 	spin_lock( &priv->dp_port_lock );
1837*1da177e4SLinus Torvalds 
1838*1da177e4SLinus Torvalds 	/* check for throttle; if set, do not resubmit read urb */
1839*1da177e4SLinus Torvalds 	/* indicate the read chain needs to be restarted on unthrottle */
1840*1da177e4SLinus Torvalds 	throttled = priv->dp_throttled;
1841*1da177e4SLinus Torvalds 	if( throttled )
1842*1da177e4SLinus Torvalds 		priv->dp_throttle_restart = 1;
1843*1da177e4SLinus Torvalds 
1844*1da177e4SLinus Torvalds 	/* receive data */
1845*1da177e4SLinus Torvalds 	if( opcode == DIGI_CMD_RECEIVE_DATA ) {
1846*1da177e4SLinus Torvalds 
1847*1da177e4SLinus Torvalds 		/* get flag from status */
1848*1da177e4SLinus Torvalds 		flag = 0;
1849*1da177e4SLinus Torvalds 
1850*1da177e4SLinus Torvalds 		/* overrun is special, not associated with a char */
1851*1da177e4SLinus Torvalds 		if( status & DIGI_OVERRUN_ERROR ) {
1852*1da177e4SLinus Torvalds 			tty_insert_flip_char( tty, 0, TTY_OVERRUN );
1853*1da177e4SLinus Torvalds 		}
1854*1da177e4SLinus Torvalds 
1855*1da177e4SLinus Torvalds 		/* break takes precedence over parity, */
1856*1da177e4SLinus Torvalds 		/* which takes precedence over framing errors */
1857*1da177e4SLinus Torvalds 		if( status & DIGI_BREAK_ERROR ) {
1858*1da177e4SLinus Torvalds 			flag = TTY_BREAK;
1859*1da177e4SLinus Torvalds 		} else if( status & DIGI_PARITY_ERROR ) {
1860*1da177e4SLinus Torvalds 			flag = TTY_PARITY;
1861*1da177e4SLinus Torvalds 		} else if( status & DIGI_FRAMING_ERROR ) {
1862*1da177e4SLinus Torvalds 			flag = TTY_FRAME;
1863*1da177e4SLinus Torvalds 		}
1864*1da177e4SLinus Torvalds 
1865*1da177e4SLinus Torvalds 		/* data length is len-1 (one byte of len is status) */
1866*1da177e4SLinus Torvalds 		--len;
1867*1da177e4SLinus Torvalds 
1868*1da177e4SLinus Torvalds 		if( throttled ) {
1869*1da177e4SLinus Torvalds 
1870*1da177e4SLinus Torvalds 			len = min( len,
1871*1da177e4SLinus Torvalds 				DIGI_IN_BUF_SIZE - priv->dp_in_buf_len );
1872*1da177e4SLinus Torvalds 
1873*1da177e4SLinus Torvalds 			if( len > 0 ) {
1874*1da177e4SLinus Torvalds 				memcpy( priv->dp_in_buf + priv->dp_in_buf_len,
1875*1da177e4SLinus Torvalds 					data, len );
1876*1da177e4SLinus Torvalds 				memset( priv->dp_in_flag_buf
1877*1da177e4SLinus Torvalds 					+ priv->dp_in_buf_len, flag, len );
1878*1da177e4SLinus Torvalds 				priv->dp_in_buf_len += len;
1879*1da177e4SLinus Torvalds 			}
1880*1da177e4SLinus Torvalds 
1881*1da177e4SLinus Torvalds 		} else {
1882*1da177e4SLinus Torvalds 
1883*1da177e4SLinus Torvalds 			len = min( len, TTY_FLIPBUF_SIZE - tty->flip.count );
1884*1da177e4SLinus Torvalds 
1885*1da177e4SLinus Torvalds 			if( len > 0 ) {
1886*1da177e4SLinus Torvalds 				memcpy( tty->flip.char_buf_ptr, data, len );
1887*1da177e4SLinus Torvalds 				memset( tty->flip.flag_buf_ptr, flag, len );
1888*1da177e4SLinus Torvalds 				tty->flip.char_buf_ptr += len;
1889*1da177e4SLinus Torvalds 				tty->flip.flag_buf_ptr += len;
1890*1da177e4SLinus Torvalds 				tty->flip.count += len;
1891*1da177e4SLinus Torvalds 				tty_flip_buffer_push( tty );
1892*1da177e4SLinus Torvalds 			}
1893*1da177e4SLinus Torvalds 
1894*1da177e4SLinus Torvalds 		}
1895*1da177e4SLinus Torvalds 
1896*1da177e4SLinus Torvalds 	}
1897*1da177e4SLinus Torvalds 
1898*1da177e4SLinus Torvalds 	spin_unlock( &priv->dp_port_lock );
1899*1da177e4SLinus Torvalds 
1900*1da177e4SLinus Torvalds 	if( opcode == DIGI_CMD_RECEIVE_DISABLE ) {
1901*1da177e4SLinus Torvalds 		dbg("%s: got RECEIVE_DISABLE", __FUNCTION__ );
1902*1da177e4SLinus Torvalds 	} else if( opcode != DIGI_CMD_RECEIVE_DATA ) {
1903*1da177e4SLinus Torvalds 		dbg("%s: unknown opcode: %d", __FUNCTION__, opcode );
1904*1da177e4SLinus Torvalds 	}
1905*1da177e4SLinus Torvalds 
1906*1da177e4SLinus Torvalds 	return( throttled ? 1 : 0 );
1907*1da177e4SLinus Torvalds 
1908*1da177e4SLinus Torvalds }
1909*1da177e4SLinus Torvalds 
1910*1da177e4SLinus Torvalds 
1911*1da177e4SLinus Torvalds /*
1912*1da177e4SLinus Torvalds *  Digi Read OOB Callback
1913*1da177e4SLinus Torvalds *
1914*1da177e4SLinus Torvalds *  Digi Read OOB Callback handles reads on the out of band port.
1915*1da177e4SLinus Torvalds *  When called we know port and port->private are not NULL and
1916*1da177e4SLinus Torvalds *  the port->serial is valid.  It returns 0 if successful, and
1917*1da177e4SLinus Torvalds *  -1 if the sanity checks failed.
1918*1da177e4SLinus Torvalds */
1919*1da177e4SLinus Torvalds 
1920*1da177e4SLinus Torvalds static int digi_read_oob_callback( struct urb *urb )
1921*1da177e4SLinus Torvalds {
1922*1da177e4SLinus Torvalds 
1923*1da177e4SLinus Torvalds 	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1924*1da177e4SLinus Torvalds 	struct usb_serial *serial = port->serial;
1925*1da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
1926*1da177e4SLinus Torvalds 	int opcode, line, status, val;
1927*1da177e4SLinus Torvalds 	int i;
1928*1da177e4SLinus Torvalds 
1929*1da177e4SLinus Torvalds 
1930*1da177e4SLinus Torvalds dbg( "digi_read_oob_callback: port=%d, len=%d", priv->dp_port_num,
1931*1da177e4SLinus Torvalds urb->actual_length );
1932*1da177e4SLinus Torvalds 
1933*1da177e4SLinus Torvalds 	/* handle each oob command */
1934*1da177e4SLinus Torvalds 	for( i=0; i<urb->actual_length-3; ) {
1935*1da177e4SLinus Torvalds 
1936*1da177e4SLinus Torvalds 		opcode = ((unsigned char *)urb->transfer_buffer)[i++];
1937*1da177e4SLinus Torvalds 		line = ((unsigned char *)urb->transfer_buffer)[i++];
1938*1da177e4SLinus Torvalds 		status = ((unsigned char *)urb->transfer_buffer)[i++];
1939*1da177e4SLinus Torvalds 		val = ((unsigned char *)urb->transfer_buffer)[i++];
1940*1da177e4SLinus Torvalds 
1941*1da177e4SLinus Torvalds dbg( "digi_read_oob_callback: opcode=%d, line=%d, status=%d, val=%d",
1942*1da177e4SLinus Torvalds opcode, line, status, val );
1943*1da177e4SLinus Torvalds 
1944*1da177e4SLinus Torvalds 		if( status != 0 || line >= serial->type->num_ports )
1945*1da177e4SLinus Torvalds 			continue;
1946*1da177e4SLinus Torvalds 
1947*1da177e4SLinus Torvalds 		port = serial->port[line];
1948*1da177e4SLinus Torvalds 
1949*1da177e4SLinus Torvalds 		if ((priv=usb_get_serial_port_data(port)) == NULL )
1950*1da177e4SLinus Torvalds 			return -1;
1951*1da177e4SLinus Torvalds 
1952*1da177e4SLinus Torvalds 		if( opcode == DIGI_CMD_READ_INPUT_SIGNALS ) {
1953*1da177e4SLinus Torvalds 
1954*1da177e4SLinus Torvalds 			spin_lock( &priv->dp_port_lock );
1955*1da177e4SLinus Torvalds 
1956*1da177e4SLinus Torvalds 			/* convert from digi flags to termiox flags */
1957*1da177e4SLinus Torvalds 			if( val & DIGI_READ_INPUT_SIGNALS_CTS ) {
1958*1da177e4SLinus Torvalds 				priv->dp_modem_signals |= TIOCM_CTS;
1959*1da177e4SLinus Torvalds 				/* port must be open to use tty struct */
1960*1da177e4SLinus Torvalds 				if( port->open_count
1961*1da177e4SLinus Torvalds 				&& port->tty->termios->c_cflag & CRTSCTS ) {
1962*1da177e4SLinus Torvalds 					port->tty->hw_stopped = 0;
1963*1da177e4SLinus Torvalds 					digi_wakeup_write( port );
1964*1da177e4SLinus Torvalds 				}
1965*1da177e4SLinus Torvalds 			} else {
1966*1da177e4SLinus Torvalds 				priv->dp_modem_signals &= ~TIOCM_CTS;
1967*1da177e4SLinus Torvalds 				/* port must be open to use tty struct */
1968*1da177e4SLinus Torvalds 				if( port->open_count
1969*1da177e4SLinus Torvalds 				&& port->tty->termios->c_cflag & CRTSCTS ) {
1970*1da177e4SLinus Torvalds 					port->tty->hw_stopped = 1;
1971*1da177e4SLinus Torvalds 				}
1972*1da177e4SLinus Torvalds 			}
1973*1da177e4SLinus Torvalds 			if( val & DIGI_READ_INPUT_SIGNALS_DSR )
1974*1da177e4SLinus Torvalds 				priv->dp_modem_signals |= TIOCM_DSR;
1975*1da177e4SLinus Torvalds 			else
1976*1da177e4SLinus Torvalds 				priv->dp_modem_signals &= ~TIOCM_DSR;
1977*1da177e4SLinus Torvalds 			if( val & DIGI_READ_INPUT_SIGNALS_RI )
1978*1da177e4SLinus Torvalds 				priv->dp_modem_signals |= TIOCM_RI;
1979*1da177e4SLinus Torvalds 			else
1980*1da177e4SLinus Torvalds 				priv->dp_modem_signals &= ~TIOCM_RI;
1981*1da177e4SLinus Torvalds 			if( val & DIGI_READ_INPUT_SIGNALS_DCD )
1982*1da177e4SLinus Torvalds 				priv->dp_modem_signals |= TIOCM_CD;
1983*1da177e4SLinus Torvalds 			else
1984*1da177e4SLinus Torvalds 				priv->dp_modem_signals &= ~TIOCM_CD;
1985*1da177e4SLinus Torvalds 
1986*1da177e4SLinus Torvalds 			wake_up_interruptible( &priv->dp_modem_change_wait );
1987*1da177e4SLinus Torvalds 			spin_unlock( &priv->dp_port_lock );
1988*1da177e4SLinus Torvalds 
1989*1da177e4SLinus Torvalds 		} else if( opcode == DIGI_CMD_TRANSMIT_IDLE ) {
1990*1da177e4SLinus Torvalds 
1991*1da177e4SLinus Torvalds 			spin_lock( &priv->dp_port_lock );
1992*1da177e4SLinus Torvalds 			priv->dp_transmit_idle = 1;
1993*1da177e4SLinus Torvalds 			wake_up_interruptible( &priv->dp_transmit_idle_wait );
1994*1da177e4SLinus Torvalds 			spin_unlock( &priv->dp_port_lock );
1995*1da177e4SLinus Torvalds 
1996*1da177e4SLinus Torvalds 		} else if( opcode == DIGI_CMD_IFLUSH_FIFO ) {
1997*1da177e4SLinus Torvalds 
1998*1da177e4SLinus Torvalds 			wake_up( &priv->dp_flush_wait );
1999*1da177e4SLinus Torvalds 
2000*1da177e4SLinus Torvalds 		}
2001*1da177e4SLinus Torvalds 
2002*1da177e4SLinus Torvalds 	}
2003*1da177e4SLinus Torvalds 
2004*1da177e4SLinus Torvalds 	return( 0 );
2005*1da177e4SLinus Torvalds 
2006*1da177e4SLinus Torvalds }
2007*1da177e4SLinus Torvalds 
2008*1da177e4SLinus Torvalds 
2009*1da177e4SLinus Torvalds static int __init digi_init (void)
2010*1da177e4SLinus Torvalds {
2011*1da177e4SLinus Torvalds 	int retval;
2012*1da177e4SLinus Torvalds 	retval = usb_serial_register(&digi_acceleport_2_device);
2013*1da177e4SLinus Torvalds 	if (retval)
2014*1da177e4SLinus Torvalds 		goto failed_acceleport_2_device;
2015*1da177e4SLinus Torvalds 	retval = usb_serial_register(&digi_acceleport_4_device);
2016*1da177e4SLinus Torvalds 	if (retval)
2017*1da177e4SLinus Torvalds 		goto failed_acceleport_4_device;
2018*1da177e4SLinus Torvalds 	retval = usb_register(&digi_driver);
2019*1da177e4SLinus Torvalds 	if (retval)
2020*1da177e4SLinus Torvalds 		goto failed_usb_register;
2021*1da177e4SLinus Torvalds 	info(DRIVER_VERSION ":" DRIVER_DESC);
2022*1da177e4SLinus Torvalds 	return 0;
2023*1da177e4SLinus Torvalds failed_usb_register:
2024*1da177e4SLinus Torvalds 	usb_serial_deregister(&digi_acceleport_4_device);
2025*1da177e4SLinus Torvalds failed_acceleport_4_device:
2026*1da177e4SLinus Torvalds 	usb_serial_deregister(&digi_acceleport_2_device);
2027*1da177e4SLinus Torvalds failed_acceleport_2_device:
2028*1da177e4SLinus Torvalds 	return retval;
2029*1da177e4SLinus Torvalds }
2030*1da177e4SLinus Torvalds 
2031*1da177e4SLinus Torvalds 
2032*1da177e4SLinus Torvalds static void __exit digi_exit (void)
2033*1da177e4SLinus Torvalds {
2034*1da177e4SLinus Torvalds 	usb_deregister (&digi_driver);
2035*1da177e4SLinus Torvalds 	usb_serial_deregister (&digi_acceleport_2_device);
2036*1da177e4SLinus Torvalds 	usb_serial_deregister (&digi_acceleport_4_device);
2037*1da177e4SLinus Torvalds }
2038*1da177e4SLinus Torvalds 
2039*1da177e4SLinus Torvalds 
2040*1da177e4SLinus Torvalds module_init(digi_init);
2041*1da177e4SLinus Torvalds module_exit(digi_exit);
2042*1da177e4SLinus Torvalds 
2043*1da177e4SLinus Torvalds 
2044*1da177e4SLinus Torvalds MODULE_AUTHOR( DRIVER_AUTHOR );
2045*1da177e4SLinus Torvalds MODULE_DESCRIPTION( DRIVER_DESC );
2046*1da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2047*1da177e4SLinus Torvalds 
2048*1da177e4SLinus Torvalds module_param(debug, bool, S_IRUGO | S_IWUSR);
2049*1da177e4SLinus Torvalds MODULE_PARM_DESC(debug, "Debug enabled or not");
2050