digi_acceleport.c revision 441b62c1edb986827154768d89bbac0ba779984f
11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Digi AccelePort USB-4 and USB-2 Serial Converters
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Copyright 2000 by Digi International
51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  This program is free software; you can redistribute it and/or modify
71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  it under the terms of the GNU General Public License as published by
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  the Free Software Foundation; either version 2 of the License, or
91da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  (at your option) any later version.
101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Shamelessly based on Brian Warner's keyspan_pda.c and Greg Kroah-Hartman's
121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  usb-serial driver.
131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Peter Berger (pberger@brimson.com)
151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Al Borchers (borchers@steinerpoint.com)
161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds* (12/03/2001) gkh
181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*	switched to using port->open_count instead of private version.
191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*	Removed port->active
201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds* (04/08/2001) gb
221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*	Identify version on module load.
231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds* (11/01/2000) Adam J. Richter
251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*	usb_device_id table support
261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds* (11/01/2000) pberger and borchers
281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Turned off the USB_DISABLE_SPD flag for write bulk urbs--it caused
291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       USB 4 ports to hang on startup.
301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Serialized access to write urbs by adding the dp_write_urb_in_use
311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       flag; otherwise, the driver caused SMP system hangs.  Watching the
321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       urb status is not sufficient.
331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds* (10/05/2000) gkh
351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Fixed bug with urb->dev not being set properly, now that the usb
361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*	core needs it.
371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  (8/8/2000) pberger and borchers
391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Fixed close so that
401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       - it can timeout while waiting for transmit idle, if needed;
411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       - it ignores interrupts when flushing the port, turning
421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*         of modem signalling, and so on;
431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       - it waits for the flush to really complete before returning.
441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Read_bulk_callback and write_bulk_callback check for a closed
451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       port before using the tty struct or writing to the port.
461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- The two changes above fix the oops caused by interrupted closes.
471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Added interruptible args to write_oob_command and set_modem_signals
481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       and added a timeout arg to transmit_idle; needed for fixes to
491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       close.
501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Added code for rx_throttle and rx_unthrottle so that input flow
511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       control works.
521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Added code to set overrun, parity, framing, and break errors
531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       (untested).
541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Set USB_DISABLE_SPD flag for write bulk urbs, so no 0 length
551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       bulk writes are done.  These hung the Digi USB device.  The
561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       0 length bulk writes were a new feature of usb-uhci added in
571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       the 2.4.0-test6 kernels.
581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Fixed mod inc race in open; do mod inc before sleeping to wait
591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       for a close to finish.
601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  (7/31/2000) pberger
621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Fixed bugs with hardware handshaking:
631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       - Added code to set/clear tty->hw_stopped in digi_read_oob_callback()
641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*         and digi_set_termios()
651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Added code in digi_set_termios() to
661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       - add conditional in code handling transition from B0 to only
671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*         set RTS if RTS/CTS flow control is either not in use or if
681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*         the port is not currently throttled.
691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       - handle turning off CRTSCTS.
701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  (7/30/2000) borchers
721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Added support for more than one Digi USB device by moving
731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       globals to a private structure in the pointed to from the
741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       usb_serial structure.
751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Moved the modem change and transmit idle wait queues into
761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       the port private structure, so each port has its own queue
771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       rather than sharing global queues.
781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Added support for break signals.
791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  (7/25/2000) pberger
811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Added USB-2 support.  Note: the USB-2 supports 3 devices: two
821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       serial and a parallel port.  The parallel port is implemented
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       as a serial-to-parallel converter.  That is, the driver actually
841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       presents all three USB-2 interfaces as serial ports, but the third
851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       one physically connects to a parallel device.  Thus, for example,
861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       one could plug a parallel printer into the USB-2's third port,
871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       but from the kernel's (and userland's) point of view what's
881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       actually out there is a serial device.
891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  (7/15/2000) borchers
911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Fixed race in open when a close is in progress.
921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Keep count of opens and dec the module use count for each
931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       outstanding open when shutdown is called (on disconnect).
941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Fixed sanity checks in read_bulk_callback and write_bulk_callback
951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       so pointers are checked before use.
961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Split read bulk callback into in band and out of band
971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       callbacks, and no longer restart read chains if there is
981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       a status error or a sanity error.  This fixed the seg
991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       faults and other errors we used to get on disconnect.
1001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Port->active is once again a flag as usb-serial intended it
1011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       to be, not a count.  Since it was only a char it would
1021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       have been limited to 256 simultaneous opens.  Now the open
1031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       count is kept in the port private structure in dp_open_count.
1041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Added code for modularization of the digi_acceleport driver.
1051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
1061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  (6/27/2000) pberger and borchers
1071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Zeroed out sync field in the wakeup_task before first use;
1081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       otherwise the uninitialized value might prevent the task from
1091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       being scheduled.
1101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Initialized ret value to 0 in write_bulk_callback, otherwise
1111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       the uninitialized value could cause a spurious debugging message.
1121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
1131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  (6/22/2000) pberger and borchers
1141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Made cond_wait_... inline--apparently on SPARC the flags arg
1151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       to spin_lock_irqsave cannot be passed to another function
1161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       to call spin_unlock_irqrestore.  Thanks to Pauline Middelink.
1171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- In digi_set_modem_signals the inner nested spin locks use just
1181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       spin_lock() rather than spin_lock_irqsave().  The old code
1191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       mistakenly left interrupts off.  Thanks to Pauline Middelink.
1201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- copy_from_user (which can sleep) is no longer called while a
1211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       spinlock is held.  We copy to a local buffer before getting
1221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       the spinlock--don't like the extra copy but the code is simpler.
1231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Printk and dbg are no longer called while a spin lock is held.
1241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
1251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  (6/4/2000) pberger and borchers
1261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Replaced separate calls to spin_unlock_irqrestore and
1271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       interruptible_sleep_on_timeout with a new function
1281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       cond_wait_interruptible_timeout_irqrestore.  This eliminates
1291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       the race condition where the wake up could happen after
1301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       the unlock and before the sleep.
1311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Close now waits for output to drain.
1321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Open waits until any close in progress is finished.
1331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- All out of band responses are now processed, not just the
1341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       first in a USB packet.
1351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Fixed a bug that prevented the driver from working when the
1361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       first Digi port was not the first USB serial port--the driver
1371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       was mistakenly using the external USB serial port number to
1381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       try to index into its internal ports.
1391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Fixed an SMP bug -- write_bulk_callback is called directly from
1401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       an interrupt, so spin_lock_irqsave/spin_unlock_irqrestore are
1411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       needed for locks outside write_bulk_callback that are also
1421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       acquired by write_bulk_callback to prevent deadlocks.
1431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Fixed support for select() by making digi_chars_in_buffer()
1441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       return 256 when -EINPROGRESS is set, as the line discipline
1451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       code in n_tty.c expects.
1461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Fixed an include file ordering problem that prevented debugging
1471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       messages from working.
1481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Fixed an intermittent timeout problem that caused writes to
1491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       sometimes get stuck on some machines on some kernels.  It turns
1501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       out in these circumstances write_chan() (in n_tty.c) was
1511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       asleep waiting for our wakeup call.  Even though we call
1521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       wake_up_interruptible() in digi_write_bulk_callback(), there is
1531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       a race condition that could cause the wakeup to fail: if our
1541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       wake_up_interruptible() call occurs between the time that our
1551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       driver write routine finishes and write_chan() sets current->state
1561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       to TASK_INTERRUPTIBLE, the effect of our wakeup setting the state
1571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       to TASK_RUNNING will be lost and write_chan's subsequent call to
1581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       schedule() will never return (unless it catches a signal).
1591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       This race condition occurs because write_bulk_callback() (and thus
1600779bf2d2ecc4d9b1e9437ae659f50e6776a7666Matt LaPlante*       the wakeup) are called asynchronously from an interrupt, rather than
1611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       from the scheduler.  We can avoid the race by calling the wakeup
1621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       from the scheduler queue and that's our fix:  Now, at the end of
1631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       write_bulk_callback() we queue up a wakeup call on the scheduler
1641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       task queue.  We still also invoke the wakeup directly since that
1651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       squeezes a bit more performance out of the driver, and any lost
1661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       race conditions will get cleaned up at the next scheduler run.
1671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
1681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       NOTE:  The problem also goes away if you comment out
1691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       the two code lines in write_chan() where current->state
1701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       is set to TASK_RUNNING just before calling driver.write() and to
1711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       TASK_INTERRUPTIBLE immediately afterwards.  This is why the
1721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       problem did not show up with the 2.2 kernels -- they do not
1731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       include that code.
1741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
1751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  (5/16/2000) pberger and borchers
1761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Added timeouts to sleeps, to defend against lost wake ups.
1771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Handle transition to/from B0 baud rate in digi_set_termios.
1781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
1791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  (5/13/2000) pberger and borchers
1801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- All commands now sent on out of band port, using
1811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       digi_write_oob_command.
1821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Get modem control signals whenever they change, support TIOCMGET/
1831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       SET/BIS/BIC ioctls.
1841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- digi_set_termios now supports parity, word size, stop bits, and
1851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       receive enable.
1861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Cleaned up open and close, use digi_set_termios and
1871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       digi_write_oob_command to set port parameters.
1881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Added digi_startup_device to start read chains on all ports.
1891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Write buffer is only used when count==1, to be sure put_char can
1901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       write a char (unless the buffer is full).
1911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
1921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  (5/10/2000) pberger and borchers
1931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Added MOD_INC_USE_COUNT/MOD_DEC_USE_COUNT calls on open/close.
1941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Fixed problem where the first incoming character is lost on
1951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       port opens after the first close on that port.  Now we keep
1961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       the read_urb chain open until shutdown.
1971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Added more port conditioning calls in digi_open and digi_close.
1981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Convert port->active to a use count so that we can deal with multiple
1991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*       opens and closes properly.
2001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- Fixed some problems with the locking code.
2011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
2021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  (5/3/2000) pberger and borchers
2031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    -- First alpha version of the driver--many known limitations and bugs.
2041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
2051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
2061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Locking and SMP
2071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
2081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  - Each port, including the out-of-band port, has a lock used to
2091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    serialize all access to the port's private structure.
2101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  - The port lock is also used to serialize all writes and access to
2111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    the port's URB.
2121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  - The port lock is also used for the port write_wait condition
2131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    variable.  Holding the port lock will prevent a wake up on the
2141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    port's write_wait; this can be used with cond_wait_... to be sure
2151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    the wake up is not lost in a race when dropping the lock and
2161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    sleeping waiting for the wakeup.
2171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  - digi_write() does not sleep, since it is sometimes called on
2181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    interrupt time.
2191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  - digi_write_bulk_callback() and digi_read_bulk_callback() are
2201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    called directly from interrupts.  Hence spin_lock_irqsave()
2211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    and spin_unlock_irqrestore() are used in the rest of the code
2221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    for any locks they acquire.
2231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  - digi_write_bulk_callback() gets the port lock before waking up
2241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    processes sleeping on the port write_wait.  It also schedules
2251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    wake ups so they happen from the scheduler, because the tty
2261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    system can miss wake ups from interrupts.
2271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  - All sleeps use a timeout of DIGI_RETRY_TIMEOUT before looping to
2281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    recheck the condition they are sleeping on.  This is defensive,
2291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    in case a wake up is lost.
2301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  - Following Documentation/DocBook/kernel-locking.pdf no spin locks
2311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*    are held when calling copy_to/from_user or printk.
2321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
2331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  $Id: digi_acceleport.c,v 1.80.1.2 2000/11/02 05:45:08 root Exp $
2341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*/
2351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/kernel.h>
2371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/errno.h>
2381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/init.h>
2391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/slab.h>
2401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/tty.h>
2411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/tty_driver.h>
2421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/tty_flip.h>
2431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/module.h>
2441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/spinlock.h>
2451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/workqueue.h>
2461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <asm/uaccess.h>
2471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/usb.h>
2481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/wait.h>
249a969888ce91673c7f4b86520d851a6f0d5a5fa7dGreg Kroah-Hartman#include <linux/usb/serial.h>
2501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Defines */
2521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
2541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Version Information
2551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
2561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DRIVER_VERSION "v1.80.1.2"
2571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DRIVER_AUTHOR "Peter Berger <pberger@brimson.com>, Al Borchers <borchers@steinerpoint.com>"
2581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DRIVER_DESC "Digi AccelePort USB-2/USB-4 Serial Converter driver"
2591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* port output buffer length -- must be <= transfer buffer length - 2 */
2611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* so we can be sure to send the full buffer in one urb */
2621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_OUT_BUF_SIZE		8
2631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* port input buffer length -- must be >= transfer buffer length - 3 */
2651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* so we can be sure to hold at least one full buffer from one urb */
2661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_IN_BUF_SIZE		64
2671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* retry timeout while sleeping */
2691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_RETRY_TIMEOUT		(HZ/10)
2701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* timeout while waiting for tty output to drain in close */
2721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* this delay is used twice in close, so the total delay could */
2731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* be twice this value */
2741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_CLOSE_TIMEOUT		(5*HZ)
2751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* AccelePort USB Defines */
2781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* ids */
2801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_VENDOR_ID			0x05c5
2811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_2_ID			0x0002	/* USB-2 */
2821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_4_ID			0x0004	/* USB-4 */
2831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* commands
2851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * "INB": can be used on the in-band endpoint
2861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * "OOB": can be used on the out-of-band endpoint
2871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
2881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_CMD_SET_BAUD_RATE			0	/* INB, OOB */
2891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_CMD_SET_WORD_SIZE			1	/* INB, OOB */
2901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_CMD_SET_PARITY			2	/* INB, OOB */
2911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_CMD_SET_STOP_BITS			3	/* INB, OOB */
2921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_CMD_SET_INPUT_FLOW_CONTROL		4	/* INB, OOB */
2931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_CMD_SET_OUTPUT_FLOW_CONTROL	5	/* INB, OOB */
2941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_CMD_SET_DTR_SIGNAL			6	/* INB, OOB */
2951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_CMD_SET_RTS_SIGNAL			7	/* INB, OOB */
2961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_CMD_READ_INPUT_SIGNALS		8	/*      OOB */
2971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_CMD_IFLUSH_FIFO			9	/*      OOB */
2981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_CMD_RECEIVE_ENABLE			10	/* INB, OOB */
2991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_CMD_BREAK_CONTROL			11	/* INB, OOB */
3001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_CMD_LOCAL_LOOPBACK			12	/* INB, OOB */
3011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_CMD_TRANSMIT_IDLE			13	/* INB, OOB */
3021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_CMD_READ_UART_REGISTER		14	/*      OOB */
3031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_CMD_WRITE_UART_REGISTER		15	/* INB, OOB */
3041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_CMD_AND_UART_REGISTER		16	/* INB, OOB */
3051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_CMD_OR_UART_REGISTER		17	/* INB, OOB */
3061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_CMD_SEND_DATA			18	/* INB      */
3071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_CMD_RECEIVE_DATA			19	/* INB      */
3081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_CMD_RECEIVE_DISABLE		20	/* INB      */
3091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_CMD_GET_PORT_TYPE			21	/*      OOB */
3101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* baud rates */
3121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAUD_50				0
3131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAUD_75				1
3141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAUD_110				2
3151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAUD_150				3
3161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAUD_200				4
3171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAUD_300				5
3181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAUD_600				6
3191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAUD_1200				7
3201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAUD_1800				8
3211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAUD_2400				9
3221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAUD_4800				10
3231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAUD_7200				11
3241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAUD_9600				12
3251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAUD_14400				13
3261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAUD_19200				14
3271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAUD_28800				15
3281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAUD_38400				16
3291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAUD_57600				17
3301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAUD_76800				18
3311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAUD_115200			19
3321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAUD_153600			20
3331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAUD_230400			21
3341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAUD_460800			22
3351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* arguments */
3371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_WORD_SIZE_5			0
3381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_WORD_SIZE_6			1
3391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_WORD_SIZE_7			2
3401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_WORD_SIZE_8			3
3411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_PARITY_NONE			0
3431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_PARITY_ODD				1
3441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_PARITY_EVEN			2
3451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_PARITY_MARK			3
3461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_PARITY_SPACE			4
3471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_STOP_BITS_1			0
3491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_STOP_BITS_2			1
3501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_INPUT_FLOW_CONTROL_XON_XOFF	1
3521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_INPUT_FLOW_CONTROL_RTS		2
3531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_INPUT_FLOW_CONTROL_DTR		4
3541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF	1
3561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_OUTPUT_FLOW_CONTROL_CTS		2
3571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_OUTPUT_FLOW_CONTROL_DSR		4
3581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_DTR_INACTIVE			0
3601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_DTR_ACTIVE				1
3611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_DTR_INPUT_FLOW_CONTROL		2
3621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_RTS_INACTIVE			0
3641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_RTS_ACTIVE				1
3651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_RTS_INPUT_FLOW_CONTROL		2
3661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_RTS_TOGGLE				3
3671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_FLUSH_TX				1
3691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_FLUSH_RX				2
3701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_RESUME_TX				4 /* clears xoff condition */
3711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_TRANSMIT_NOT_IDLE			0
3731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_TRANSMIT_IDLE			1
3741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_DISABLE				0
3761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_ENABLE				1
3771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_DEASSERT				0
3791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_ASSERT				1
3801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* in band status codes */
3821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_OVERRUN_ERROR			4
3831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_PARITY_ERROR			8
3841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_FRAMING_ERROR			16
3851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BREAK_ERROR			32
3861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* out of band status */
3881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_NO_ERROR				0
3891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAD_FIRST_PARAMETER		1
3901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_BAD_SECOND_PARAMETER		2
3911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_INVALID_LINE			3
3921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_INVALID_OPCODE			4
3931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* input signals */
3951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_READ_INPUT_SIGNALS_SLOT		1
3961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_READ_INPUT_SIGNALS_ERR		2
3971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_READ_INPUT_SIGNALS_BUSY		4
3981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_READ_INPUT_SIGNALS_PE		8
3991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_READ_INPUT_SIGNALS_CTS		16
4001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_READ_INPUT_SIGNALS_DSR		32
4011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_READ_INPUT_SIGNALS_RI		64
4021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DIGI_READ_INPUT_SIGNALS_DCD		128
4031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Structures */
4061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct digi_serial {
4081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spinlock_t ds_serial_lock;
4091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct usb_serial_port *ds_oob_port;	/* out-of-band port */
4101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ds_oob_port_num;			/* index of out-of-band port */
4111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ds_device_started;
4121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
4131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct digi_port {
4151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spinlock_t dp_port_lock;
4161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int dp_port_num;
4171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int dp_out_buf_len;
4181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned char dp_out_buf[DIGI_OUT_BUF_SIZE];
4191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int dp_write_urb_in_use;
4201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int dp_modem_signals;
4211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	wait_queue_head_t dp_modem_change_wait;
4221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int dp_transmit_idle;
4231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	wait_queue_head_t dp_transmit_idle_wait;
4241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int dp_throttled;
4251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int dp_throttle_restart;
4261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	wait_queue_head_t dp_flush_wait;
4271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int dp_in_close;			/* close in progress */
4281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	wait_queue_head_t dp_close_wait;	/* wait queue for close */
4291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct work_struct dp_wakeup_work;
430c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	struct usb_serial_port *dp_port;
4311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
4321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Local Function Declarations */
4351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
436c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic void digi_wakeup_write(struct usb_serial_port *port);
437c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsstatic void digi_wakeup_write_lock(struct work_struct *work);
438c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_write_oob_command(struct usb_serial_port *port,
439c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	unsigned char *buf, int count, int interruptible);
440c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_write_inb_command(struct usb_serial_port *port,
441c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	unsigned char *buf, int count, unsigned long timeout);
442c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_set_modem_signals(struct usb_serial_port *port,
443c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	unsigned int modem_signals, int interruptible);
444c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_transmit_idle(struct usb_serial_port *port,
445c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	unsigned long timeout);
4461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void digi_rx_throttle (struct usb_serial_port *port);
4471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void digi_rx_unthrottle (struct usb_serial_port *port);
448c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic void digi_set_termios(struct usb_serial_port *port,
449c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	struct ktermios *old_termios);
450c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic void digi_break_ctl(struct usb_serial_port *port, int break_state);
451c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_ioctl(struct usb_serial_port *port, struct file *file,
452c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	unsigned int cmd, unsigned long arg);
453c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_tiocmget(struct usb_serial_port *port, struct file *file);
454c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_tiocmset(struct usb_serial_port *port, struct file *file,
455c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	unsigned int set, unsigned int clear);
456c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_write(struct usb_serial_port *port, const unsigned char *buf, int count);
457c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic void digi_write_bulk_callback(struct urb *urb);
458c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_write_room(struct usb_serial_port *port);
459c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_chars_in_buffer(struct usb_serial_port *port);
460c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_open(struct usb_serial_port *port, struct file *filp);
461c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic void digi_close(struct usb_serial_port *port, struct file *filp);
462c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_startup_device(struct usb_serial *serial);
463c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_startup(struct usb_serial *serial);
464c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic void digi_shutdown(struct usb_serial *serial);
465c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic void digi_read_bulk_callback(struct urb *urb);
466c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_read_inb_callback(struct urb *urb);
467c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_read_oob_callback(struct urb *urb);
4681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Statics */
4711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int debug;
4731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct usb_device_id id_table_combined [] = {
4751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
4761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
4771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ }						/* Terminating entry */
4781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
4791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct usb_device_id id_table_2 [] = {
4811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
4821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ }						/* Terminating entry */
4831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
4841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct usb_device_id id_table_4 [] = {
4861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
4871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ }						/* Terminating entry */
4881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
4891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_DEVICE_TABLE (usb, id_table_combined);
4911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct usb_driver digi_driver = {
4931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.name =		"digi_acceleport",
4941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.probe =	usb_serial_probe,
4951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.disconnect =	usb_serial_disconnect,
4961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.id_table =	id_table_combined,
497ba9dc657af86d05d2971633e57d1f6f94ed60472Greg Kroah-Hartman	.no_dynamic_id = 	1,
4981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
4991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* device info needed for the Digi serial converter */
5021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
503ea65370d025f5005649e5cb37c4d025e92c6fc38Greg Kroah-Hartmanstatic struct usb_serial_driver digi_acceleport_2_device = {
50418fcac353fdc7cd072b0d24c8667042e675a4c11Greg Kroah-Hartman	.driver = {
505269bda1c123c7caf88e1deb2264f9086f0344192Greg Kroah-Hartman		.owner =		THIS_MODULE,
506269bda1c123c7caf88e1deb2264f9086f0344192Greg Kroah-Hartman		.name =			"digi_2",
50718fcac353fdc7cd072b0d24c8667042e675a4c11Greg Kroah-Hartman	},
508269bda1c123c7caf88e1deb2264f9086f0344192Greg Kroah-Hartman	.description =			"Digi 2 port USB adapter",
509d9b1b787736852f462dbf277b3ca708cbbf693aeJohannes Hölzl	.usb_driver = 			&digi_driver,
5101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.id_table =			id_table_2,
5111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.num_ports =			3,
5121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.open =				digi_open,
5131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.close =			digi_close,
5141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.write =			digi_write,
5151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.write_room =			digi_write_room,
5161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.write_bulk_callback = 		digi_write_bulk_callback,
5171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.read_bulk_callback =		digi_read_bulk_callback,
5181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.chars_in_buffer =		digi_chars_in_buffer,
5191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.throttle =			digi_rx_throttle,
5201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.unthrottle =			digi_rx_unthrottle,
5211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.ioctl =			digi_ioctl,
5221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.set_termios =			digi_set_termios,
5231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.break_ctl =			digi_break_ctl,
5241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.tiocmget =			digi_tiocmget,
5251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.tiocmset =			digi_tiocmset,
5261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.attach =			digi_startup,
5271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.shutdown =			digi_shutdown,
5281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
5291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
530ea65370d025f5005649e5cb37c4d025e92c6fc38Greg Kroah-Hartmanstatic struct usb_serial_driver digi_acceleport_4_device = {
53118fcac353fdc7cd072b0d24c8667042e675a4c11Greg Kroah-Hartman	.driver = {
532269bda1c123c7caf88e1deb2264f9086f0344192Greg Kroah-Hartman		.owner =		THIS_MODULE,
533269bda1c123c7caf88e1deb2264f9086f0344192Greg Kroah-Hartman		.name =			"digi_4",
53418fcac353fdc7cd072b0d24c8667042e675a4c11Greg Kroah-Hartman	},
535269bda1c123c7caf88e1deb2264f9086f0344192Greg Kroah-Hartman	.description =			"Digi 4 port USB adapter",
536d9b1b787736852f462dbf277b3ca708cbbf693aeJohannes Hölzl	.usb_driver = 			&digi_driver,
5371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.id_table =			id_table_4,
5381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.num_ports =			4,
5391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.open =				digi_open,
5401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.close =			digi_close,
5411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.write =			digi_write,
5421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.write_room =			digi_write_room,
5431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.write_bulk_callback = 		digi_write_bulk_callback,
5441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.read_bulk_callback =		digi_read_bulk_callback,
5451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.chars_in_buffer =		digi_chars_in_buffer,
5461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.throttle =			digi_rx_throttle,
5471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.unthrottle =			digi_rx_unthrottle,
5481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.ioctl =			digi_ioctl,
5491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.set_termios =			digi_set_termios,
5501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.break_ctl =			digi_break_ctl,
5511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.tiocmget =			digi_tiocmget,
5521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.tiocmset =			digi_tiocmset,
5531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.attach =			digi_startup,
5541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.shutdown =			digi_shutdown,
5551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
5561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Functions */
5591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
5611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Cond Wait Interruptible Timeout Irqrestore
5621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
5631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Do spin_unlock_irqrestore and interruptible_sleep_on_timeout
5641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  so that wake ups are not lost if they occur between the unlock
5651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  and the sleep.  In other words, spin_unlock_irqrestore and
5661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  interruptible_sleep_on_timeout are "atomic" with respect to
5671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  wake ups.  This is used to implement condition variables.
56845f23f189ca66d98b1f8b7f3d30a194d3188039dNishanth Aravamudan*
56945f23f189ca66d98b1f8b7f3d30a194d3188039dNishanth Aravamudan*  interruptible_sleep_on_timeout is deprecated and has been replaced
57045f23f189ca66d98b1f8b7f3d30a194d3188039dNishanth Aravamudan*  with the equivalent code.
5711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*/
5721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
573c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic long cond_wait_interruptible_timeout_irqrestore(
5741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	wait_queue_head_t *q, long timeout,
575c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spinlock_t *lock, unsigned long flags)
5761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	DEFINE_WAIT(wait);
5781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
57945f23f189ca66d98b1f8b7f3d30a194d3188039dNishanth Aravamudan	prepare_to_wait(q, &wait, TASK_INTERRUPTIBLE);
5801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock_irqrestore(lock, flags);
5811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	timeout = schedule_timeout(timeout);
5821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	finish_wait(q, &wait);
5831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return timeout;
5851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
5891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Digi Wakeup Write
5901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
5911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Wake up port, line discipline, and tty processes sleeping
5921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  on writes.
5931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*/
5941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
595c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howellsstatic void digi_wakeup_write_lock(struct work_struct *work)
5961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
597c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	struct digi_port *priv = container_of(work, struct digi_port, dp_wakeup_work);
598c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells	struct usb_serial_port *port = priv->dp_port;
5991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
6001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
601c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_lock_irqsave(&priv->dp_port_lock, flags);
602c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	digi_wakeup_write(port);
603c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
6041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
606c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic void digi_wakeup_write(struct usb_serial_port *port)
6071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
608b963a8441cb95999c97bea379607071a869c65f0Jiri Slaby	tty_wakeup(port->tty);
6091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
6131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Digi Write OOB Command
6141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
6151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Write commands on the out of band port.  Commands are 4
6161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  bytes each, multiple commands can be sent at once, and
6171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  no command will be split across USB packets.  Returns 0
6181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  if successful, -EINTR if interrupted while sleeping and
6191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  the interruptible flag is true, or a negative error
6201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  returned by usb_submit_urb.
6211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*/
6221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
623c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_write_oob_command(struct usb_serial_port *port,
624c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	unsigned char *buf, int count, int interruptible)
6251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
6261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret = 0;
6281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int len;
6291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct usb_serial_port *oob_port = (struct usb_serial_port *)((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
6301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
6311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags = 0;
6321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
633c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	dbg("digi_write_oob_command: TOP: port=%d, count=%d", oob_priv->dp_port_num, count);
6341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
635c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
636c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	while(count > 0) {
637c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		while(oob_port->write_urb->status == -EINPROGRESS
638c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			|| oob_priv->dp_write_urb_in_use) {
6391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cond_wait_interruptible_timeout_irqrestore(
6401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				&oob_port->write_wait, DIGI_RETRY_TIMEOUT,
641c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox				&oob_priv->dp_port_lock, flags);
642c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			if (interruptible && signal_pending(current))
643c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox				return -EINTR;
644c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
6451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
6461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* len must be a multiple of 4, so commands are not split */
648c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		len = min(count, oob_port->bulk_out_size);
649c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if (len > 4)
6501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			len &= ~3;
651c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		memcpy(oob_port->write_urb->transfer_buffer, buf, len);
6521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		oob_port->write_urb->transfer_buffer_length = len;
6531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		oob_port->write_urb->dev = port->serial->dev;
654c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if ((ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC)) == 0) {
6551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			oob_priv->dp_write_urb_in_use = 1;
6561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			count -= len;
6571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			buf += len;
6581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
6591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
660c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
661c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (ret)
662441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison		err("%s: usb_submit_urb failed, ret=%d", __func__, ret);
663c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	return ret;
6641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
6691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Digi Write In Band Command
6701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
6711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Write commands on the given port.  Commands are 4
6721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  bytes each, multiple commands can be sent at once, and
6731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  no command will be split across USB packets.  If timeout
6741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  is non-zero, write in band command will return after
6751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  waiting unsuccessfully for the URB status to clear for
6761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  timeout ticks.  Returns 0 if successful, or a negative
6771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  error returned by digi_write.
6781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*/
6791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
680c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_write_inb_command(struct usb_serial_port *port,
681c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	unsigned char *buf, int count, unsigned long timeout)
6821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
6831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret = 0;
6841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int len;
6851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_port *priv = usb_get_serial_port_data(port);
6861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned char *data = port->write_urb->transfer_buffer;
6871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags = 0;
6881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
689c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	dbg("digi_write_inb_command: TOP: port=%d, count=%d",
690c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		priv->dp_port_num, count);
6911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
692c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (timeout)
6931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		timeout += jiffies;
6941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
6951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		timeout = ULONG_MAX;
6961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
697c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_lock_irqsave(&priv->dp_port_lock, flags);
698c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	while(count > 0 && ret == 0) {
699c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		while((port->write_urb->status == -EINPROGRESS
700c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			|| priv->dp_write_urb_in_use) && time_before(jiffies, timeout)) {
7011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			cond_wait_interruptible_timeout_irqrestore(
7021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				&port->write_wait, DIGI_RETRY_TIMEOUT,
703c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox				&priv->dp_port_lock, flags);
704c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			if (signal_pending(current))
705c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox				return -EINTR;
706c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			spin_lock_irqsave(&priv->dp_port_lock, flags);
7071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
7081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* len must be a multiple of 4 and small enough to */
7101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* guarantee the write will send buffered data first, */
7111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* so commands are in order with data and not split */
712c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
713c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if (len > 4)
7141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			len &= ~3;
7151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* write any buffered data first */
717c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if (priv->dp_out_buf_len > 0) {
7181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			data[0] = DIGI_CMD_SEND_DATA;
7191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			data[1] = priv->dp_out_buf_len;
720c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			memcpy(data + 2, priv->dp_out_buf,
721c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox				priv->dp_out_buf_len);
722c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			memcpy(data + 2 + priv->dp_out_buf_len, buf, len);
7231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			port->write_urb->transfer_buffer_length
724c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox				= priv->dp_out_buf_len + 2 + len;
7251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		} else {
726c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			memcpy(data, buf, len);
7271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			port->write_urb->transfer_buffer_length = len;
7281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
7291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		port->write_urb->dev = port->serial->dev;
7301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
731c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if ((ret = usb_submit_urb(port->write_urb, GFP_ATOMIC)) == 0) {
7321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			priv->dp_write_urb_in_use = 1;
7331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			priv->dp_out_buf_len = 0;
7341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			count -= len;
7351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			buf += len;
7361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
7371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
739c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
7401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
741c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (ret)
742c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		err("%s: usb_submit_urb failed, ret=%d, port=%d",
743441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison			__func__, ret, priv->dp_port_num);
744c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	return ret;
7451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
7491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Digi Set Modem Signals
7501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
7511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Sets or clears DTR and RTS on the port, according to the
7521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  modem_signals argument.  Use TIOCM_DTR and TIOCM_RTS flags
7531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  for the modem_signals argument.  Returns 0 if successful,
7541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  -EINTR if interrupted while sleeping, or a non-zero error
7551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  returned by usb_submit_urb.
7561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*/
7571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
758c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_set_modem_signals(struct usb_serial_port *port,
759c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	unsigned int modem_signals, int interruptible)
7601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
7611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
7631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_port *port_priv = usb_get_serial_port_data(port);
7641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct usb_serial_port *oob_port = (struct usb_serial_port *)((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
7651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
7661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned char *data = oob_port->write_urb->transfer_buffer;
7671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags = 0;
7681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
770c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	dbg("digi_set_modem_signals: TOP: port=%d, modem_signals=0x%x",
771c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		port_priv->dp_port_num, modem_signals);
7721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
773c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
774c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_lock(&port_priv->dp_port_lock);
7751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
776c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	while(oob_port->write_urb->status == -EINPROGRESS || oob_priv->dp_write_urb_in_use) {
777c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		spin_unlock(&port_priv->dp_port_lock);
7781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cond_wait_interruptible_timeout_irqrestore(
7791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			&oob_port->write_wait, DIGI_RETRY_TIMEOUT,
780c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			&oob_priv->dp_port_lock, flags);
781c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if (interruptible && signal_pending(current))
782c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			return -EINTR;
783c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
784c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		spin_lock(&port_priv->dp_port_lock);
7851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
7861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	data[0] = DIGI_CMD_SET_DTR_SIGNAL;
7871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	data[1] = port_priv->dp_port_num;
788c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	data[2] = (modem_signals&TIOCM_DTR) ? DIGI_DTR_ACTIVE : DIGI_DTR_INACTIVE;
7891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	data[3] = 0;
7901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	data[4] = DIGI_CMD_SET_RTS_SIGNAL;
7911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	data[5] = port_priv->dp_port_num;
792c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	data[6] = (modem_signals&TIOCM_RTS) ? DIGI_RTS_ACTIVE : DIGI_RTS_INACTIVE;
7931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	data[7] = 0;
7941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
7951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	oob_port->write_urb->transfer_buffer_length = 8;
7961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	oob_port->write_urb->dev = port->serial->dev;
7971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
798c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if ((ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC)) == 0) {
7991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		oob_priv->dp_write_urb_in_use = 1;
8001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		port_priv->dp_modem_signals =
8011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			(port_priv->dp_modem_signals&~(TIOCM_DTR|TIOCM_RTS))
8021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			| (modem_signals&(TIOCM_DTR|TIOCM_RTS));
8031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
804c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_unlock(&port_priv->dp_port_lock);
805c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
806c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (ret)
807441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison		err("%s: usb_submit_urb failed, ret=%d", __func__, ret);
808c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	return ret;
8091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
8101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
8121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Digi Transmit Idle
8131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
8141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Digi transmit idle waits, up to timeout ticks, for the transmitter
8151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  to go idle.  It returns 0 if successful or a negative error.
8161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
8171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  There are race conditions here if more than one process is calling
8181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  digi_transmit_idle on the same port at the same time.  However, this
8191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  is only called from close, and only one process can be in close on a
8201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  port at a time, so its ok.
8211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*/
8221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
823c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_transmit_idle(struct usb_serial_port *port,
824c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	unsigned long timeout)
8251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
8261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
8271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned char buf[2];
8281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_port *priv = usb_get_serial_port_data(port);
8291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags = 0;
8301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
831c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_lock_irqsave(&priv->dp_port_lock, flags);
8321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	priv->dp_transmit_idle = 0;
833c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
8341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buf[0] = DIGI_CMD_TRANSMIT_IDLE;
8361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buf[1] = 0;
8371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	timeout += jiffies;
8391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
840c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if ((ret = digi_write_inb_command(port, buf, 2, timeout - jiffies)) != 0)
841c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		return ret;
8421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
843c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_lock_irqsave(&priv->dp_port_lock, flags);
8441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
845c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	while(time_before(jiffies, timeout) && !priv->dp_transmit_idle) {
8461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cond_wait_interruptible_timeout_irqrestore(
8471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			&priv->dp_transmit_idle_wait, DIGI_RETRY_TIMEOUT,
848c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			&priv->dp_port_lock, flags);
849c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if (signal_pending(current))
850c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			return -EINTR;
851c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		spin_lock_irqsave(&priv->dp_port_lock, flags);
8521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
8531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	priv->dp_transmit_idle = 0;
854c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
855c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	return 0;
8561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
8581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
860c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic void digi_rx_throttle(struct usb_serial_port *port)
8611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
8621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
8631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_port *priv = usb_get_serial_port_data(port);
8641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
866c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	dbg("digi_rx_throttle: TOP: port=%d", priv->dp_port_num);
8671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* stop receiving characters by not resubmitting the read urb */
869c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_lock_irqsave(&priv->dp_port_lock, flags);
8701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	priv->dp_throttled = 1;
8711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	priv->dp_throttle_restart = 0;
872c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
8731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
8741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
876c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic void digi_rx_unthrottle(struct usb_serial_port *port)
8771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
8781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret = 0;
8791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
8801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_port *priv = usb_get_serial_port_data(port);
8811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
882c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	dbg("digi_rx_unthrottle: TOP: port=%d", priv->dp_port_num);
8831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
884c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_lock_irqsave(&priv->dp_port_lock, flags);
8851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
88639892da44b21b5362eb848ca424d73a25ccc488fOliver Neukum	/* turn throttle off */
88739892da44b21b5362eb848ca424d73a25ccc488fOliver Neukum	priv->dp_throttled = 0;
88839892da44b21b5362eb848ca424d73a25ccc488fOliver Neukum	priv->dp_throttle_restart = 0;
8891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* restart read chain */
891c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (priv->dp_throttle_restart) {
8921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		port->read_urb->dev = port->serial->dev;
893c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		ret = usb_submit_urb(port->read_urb, GFP_ATOMIC);
8941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
8951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
896c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
8971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
898c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (ret)
899c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		err("%s: usb_submit_urb failed, ret=%d, port=%d",
900441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison			__func__, ret, priv->dp_port_num);
9011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
9021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
904c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic void digi_set_termios(struct usb_serial_port *port,
905c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox					struct ktermios *old_termios)
9061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
9071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_port *priv = usb_get_serial_port_data(port);
909c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	struct tty_struct *tty = port->tty;
910c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	unsigned int iflag = tty->termios->c_iflag;
911c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	unsigned int cflag = tty->termios->c_cflag;
9121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int old_iflag = old_termios->c_iflag;
9131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int old_cflag = old_termios->c_cflag;
9141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned char buf[32];
9151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int modem_signals;
9161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int arg,ret;
9171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i = 0;
918c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	speed_t baud;
9191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
920c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	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);
9211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* set baud rate */
923c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if ((baud = tty_get_baud_rate(tty)) != tty_termios_baud_rate(old_termios)) {
9241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		arg = -1;
9251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* reassert DTR and (maybe) RTS on transition from B0 */
927c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if ((old_cflag&CBAUD) == B0) {
9281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/* don't set RTS if using hardware flow control */
9291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/* and throttling input */
9301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			modem_signals = TIOCM_DTR;
931c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			if (!(tty->termios->c_cflag & CRTSCTS) ||
932c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			    !test_bit(TTY_THROTTLED, &tty->flags))
9331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				modem_signals |= TIOCM_RTS;
934c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			digi_set_modem_signals(port, modem_signals, 1);
9351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
936c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		switch (baud) {
9371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/* drop DTR and RTS on transition to B0 */
938c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			case 0: digi_set_modem_signals(port, 0, 1); break;
939c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			case 50: arg = DIGI_BAUD_50; break;
940c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			case 75: arg = DIGI_BAUD_75; break;
941c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			case 110: arg = DIGI_BAUD_110; break;
942c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			case 150: arg = DIGI_BAUD_150; break;
943c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			case 200: arg = DIGI_BAUD_200; break;
944c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			case 300: arg = DIGI_BAUD_300; break;
945c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			case 600: arg = DIGI_BAUD_600; break;
946c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			case 1200: arg = DIGI_BAUD_1200; break;
947c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			case 1800: arg = DIGI_BAUD_1800; break;
948c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			case 2400: arg = DIGI_BAUD_2400; break;
949c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			case 4800: arg = DIGI_BAUD_4800; break;
950c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			case 9600: arg = DIGI_BAUD_9600; break;
951c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			case 19200: arg = DIGI_BAUD_19200; break;
952c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			case 38400: arg = DIGI_BAUD_38400; break;
953c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			case 57600: arg = DIGI_BAUD_57600; break;
954c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			case 115200: arg = DIGI_BAUD_115200; break;
955c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			case 230400: arg = DIGI_BAUD_230400; break;
956c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			case 460800: arg = DIGI_BAUD_460800; break;
957c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			default:
958c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox				arg = DIGI_BAUD_9600;
959c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox				baud = 9600;
960c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox				break;
9611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
962c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if (arg != -1) {
9631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			buf[i++] = DIGI_CMD_SET_BAUD_RATE;
9641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			buf[i++] = priv->dp_port_num;
9651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			buf[i++] = arg;
9661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			buf[i++] = 0;
9671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
9681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
9691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* set parity */
9707fa36a994cb4298f29994a248ced831be8dc7051Alan Cox	tty->termios->c_cflag &= ~CMSPAR;
9717fa36a994cb4298f29994a248ced831be8dc7051Alan Cox
972c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if ((cflag&(PARENB|PARODD)) != (old_cflag&(PARENB|PARODD))) {
973c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if (cflag&PARENB) {
974c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			if (cflag&PARODD)
9751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				arg = DIGI_PARITY_ODD;
9761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			else
9771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				arg = DIGI_PARITY_EVEN;
9781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		} else {
9791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			arg = DIGI_PARITY_NONE;
9801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
9811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[i++] = DIGI_CMD_SET_PARITY;
9821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[i++] = priv->dp_port_num;
9831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[i++] = arg;
9841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[i++] = 0;
9851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
9861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* set word size */
987c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if ((cflag&CSIZE) != (old_cflag&CSIZE)) {
9881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		arg = -1;
989c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		switch (cflag&CSIZE) {
9901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case CS5: arg = DIGI_WORD_SIZE_5; break;
9911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case CS6: arg = DIGI_WORD_SIZE_6; break;
9921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case CS7: arg = DIGI_WORD_SIZE_7; break;
9931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case CS8: arg = DIGI_WORD_SIZE_8; break;
9941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		default:
995c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			dbg("digi_set_termios: can't handle word size %d",
996c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox				(cflag&CSIZE));
9971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
9981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
9991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1000c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if (arg != -1) {
10011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			buf[i++] = DIGI_CMD_SET_WORD_SIZE;
10021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			buf[i++] = priv->dp_port_num;
10031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			buf[i++] = arg;
10041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			buf[i++] = 0;
10051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
10061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
10081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* set stop bits */
1010c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if ((cflag&CSTOPB) != (old_cflag&CSTOPB)) {
10111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1012c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if ((cflag&CSTOPB))
10131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			arg = DIGI_STOP_BITS_2;
10141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else
10151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			arg = DIGI_STOP_BITS_1;
10161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[i++] = DIGI_CMD_SET_STOP_BITS;
10181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[i++] = priv->dp_port_num;
10191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[i++] = arg;
10201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[i++] = 0;
10211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
10231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* set input flow control */
1025c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if ((iflag&IXOFF) != (old_iflag&IXOFF)
1026c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	    || (cflag&CRTSCTS) != (old_cflag&CRTSCTS)) {
10271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		arg = 0;
1028c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if (iflag&IXOFF)
10291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			arg |= DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
10301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else
10311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			arg &= ~DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
10321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1033c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if (cflag&CRTSCTS) {
10341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			arg |= DIGI_INPUT_FLOW_CONTROL_RTS;
10351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/* On USB-4 it is necessary to assert RTS prior */
10371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/* to selecting RTS input flow control.  */
10381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			buf[i++] = DIGI_CMD_SET_RTS_SIGNAL;
10391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			buf[i++] = priv->dp_port_num;
10401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			buf[i++] = DIGI_RTS_ACTIVE;
10411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			buf[i++] = 0;
10421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		} else {
10441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			arg &= ~DIGI_INPUT_FLOW_CONTROL_RTS;
10451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
10461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[i++] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
10471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[i++] = priv->dp_port_num;
10481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[i++] = arg;
10491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[i++] = 0;
10501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
10511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* set output flow control */
10537fa36a994cb4298f29994a248ced831be8dc7051Alan Cox	if ((iflag & IXON) != (old_iflag & IXON)
10547fa36a994cb4298f29994a248ced831be8dc7051Alan Cox	    || (cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
10551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		arg = 0;
10567fa36a994cb4298f29994a248ced831be8dc7051Alan Cox		if (iflag & IXON)
10571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			arg |= DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
10581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else
10591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			arg &= ~DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
10601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10617fa36a994cb4298f29994a248ced831be8dc7051Alan Cox		if (cflag & CRTSCTS) {
10621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			arg |= DIGI_OUTPUT_FLOW_CONTROL_CTS;
10631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		} else {
10641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			arg &= ~DIGI_OUTPUT_FLOW_CONTROL_CTS;
1065c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			tty->hw_stopped = 0;
10661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
10671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[i++] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
10691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[i++] = priv->dp_port_num;
10701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[i++] = arg;
10711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[i++] = 0;
10721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
10731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* set receive enable/disable */
10757fa36a994cb4298f29994a248ced831be8dc7051Alan Cox	if ((cflag & CREAD) != (old_cflag & CREAD)) {
10767fa36a994cb4298f29994a248ced831be8dc7051Alan Cox		if (cflag & CREAD)
10771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			arg = DIGI_ENABLE;
10781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else
10791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			arg = DIGI_DISABLE;
10801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[i++] = DIGI_CMD_RECEIVE_ENABLE;
10821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[i++] = priv->dp_port_num;
10831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[i++] = arg;
10841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[i++] = 0;
10851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1086c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if ((ret = digi_write_oob_command(port, buf, i, 1)) != 0)
1087c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		dbg("digi_set_termios: write oob failed, ret=%d", ret);
10887fa36a994cb4298f29994a248ced831be8dc7051Alan Cox	tty_encode_baud_rate(tty, baud, baud);
10891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
10901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1092c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic void digi_break_ctl(struct usb_serial_port *port, int break_state)
10931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
10941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned char buf[4];
10951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buf[0] = DIGI_CMD_BREAK_CONTROL;
10971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buf[1] = 2;				/* length */
10981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buf[2] = break_state ? 1 : 0;
10991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buf[3] = 0;				/* pad */
1100c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	digi_write_inb_command(port, buf, 4, 0);
11011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
11021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1104c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_tiocmget(struct usb_serial_port *port, struct file *file)
11051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
11061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_port *priv = usb_get_serial_port_data(port);
11071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int val;
11081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
11091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1110441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison	dbg("%s: TOP: port=%d", __func__, priv->dp_port_num);
11111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1112c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_lock_irqsave(&priv->dp_port_lock, flags);
11131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	val = priv->dp_modem_signals;
1114c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
11151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return val;
11161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
11171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1119c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_tiocmset(struct usb_serial_port *port, struct file *file,
1120c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	unsigned int set, unsigned int clear)
11211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
11221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_port *priv = usb_get_serial_port_data(port);
11231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int val;
11241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags;
11251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1126441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison	dbg("%s: TOP: port=%d", __func__, priv->dp_port_num);
11271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1128c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_lock_irqsave(&priv->dp_port_lock, flags);
11291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	val = (priv->dp_modem_signals & ~clear) | set;
1130c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
1131c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	return digi_set_modem_signals(port, val, 1);
11321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
11331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1135c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_ioctl(struct usb_serial_port *port, struct file *file,
1136c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	unsigned int cmd, unsigned long arg)
11371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
11381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_port *priv = usb_get_serial_port_data(port);
1139c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	dbg("digi_ioctl: TOP: port=%d, cmd=0x%x", priv->dp_port_num, cmd);
11401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (cmd) {
11421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case TIOCMIWAIT:
11431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* wait for any of the 4 modem inputs (DCD,RI,DSR,CTS)*/
11441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* TODO */
1145c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		return 0;
11461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case TIOCGICOUNT:
11471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* return count of modemline transitions */
11481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* TODO */
11491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
11501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1151c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	return -ENOIOCTLCMD;
11521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
11541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1155c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_write(struct usb_serial_port *port, const unsigned char *buf, int count)
11561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
11571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret,data_len,new_len;
11591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_port *priv = usb_get_serial_port_data(port);
11601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned char *data = port->write_urb->transfer_buffer;
11611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags = 0;
11621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1163c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	dbg("digi_write: TOP: port=%d, count=%d, in_interrupt=%ld",
1164c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		priv->dp_port_num, count, in_interrupt());
11651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* copy user data (which can sleep) before getting spin lock */
1167c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	count = min(count, port->bulk_out_size-2);
1168c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	count = min(64, count);
11691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* be sure only one write proceeds at a time */
11711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* there are races on the port private buffer */
11721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* and races to check write_urb->status */
1173c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_lock_irqsave(&priv->dp_port_lock, flags);
11741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* wait for urb status clear to submit another urb */
1176c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (port->write_urb->status == -EINPROGRESS || priv->dp_write_urb_in_use) {
11771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* buffer data if count is 1 (probably put_char) if possible */
1178c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if (count == 1 && priv->dp_out_buf_len < DIGI_OUT_BUF_SIZE) {
11791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			priv->dp_out_buf[priv->dp_out_buf_len++] = *buf;
11801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			new_len = 1;
11811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		} else {
11821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			new_len = 0;
11831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
1184c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		spin_unlock_irqrestore(&priv->dp_port_lock, flags);
1185c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		return new_len;
11861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
11871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* allow space for any buffered data and for new data, up to */
11891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* transfer buffer size - 2 (for command and length bytes) */
11901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	new_len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
11911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	data_len = new_len + priv->dp_out_buf_len;
11921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1193c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (data_len == 0) {
1194c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		spin_unlock_irqrestore(&priv->dp_port_lock, flags);
1195c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		return 0;
11961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
11971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	port->write_urb->transfer_buffer_length = data_len+2;
11991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	port->write_urb->dev = port->serial->dev;
12001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*data++ = DIGI_CMD_SEND_DATA;
12021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*data++ = data_len;
12031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* copy in buffered data first */
1205c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	memcpy(data, priv->dp_out_buf, priv->dp_out_buf_len);
12061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	data += priv->dp_out_buf_len;
12071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* copy in new data */
1209c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	memcpy(data, buf, new_len);
12101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1211c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if ((ret = usb_submit_urb(port->write_urb, GFP_ATOMIC)) == 0) {
12121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		priv->dp_write_urb_in_use = 1;
12131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = new_len;
12141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		priv->dp_out_buf_len = 0;
12151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
12161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* return length of new data written, or error */
1218c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
1219c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (ret < 0)
1220c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		err("%s: usb_submit_urb failed, ret=%d, port=%d",
1221441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison			__func__, ret, priv->dp_port_num);
1222c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	dbg("digi_write: returning %d", ret);
1223c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	return ret;
12241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
12261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1227c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic void digi_write_bulk_callback(struct urb *urb)
12281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
12291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
12311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct usb_serial *serial;
12321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_port *priv;
12331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_serial *serial_priv;
12341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret = 0;
123585d75107117eca0e29ac3da8cb82b45f9cd3a7faGreg Kroah-Hartman	int status = urb->status;
12361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1237c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	dbg("digi_write_bulk_callback: TOP, urb->status=%d", status);
12381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* port and serial sanity check */
1240c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (port == NULL || (priv=usb_get_serial_port_data(port)) == NULL) {
124185d75107117eca0e29ac3da8cb82b45f9cd3a7faGreg Kroah-Hartman		err("%s: port or port->private is NULL, status=%d",
1242441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison		    __func__, status);
12431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
12441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
12451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	serial = port->serial;
1246c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (serial == NULL || (serial_priv=usb_get_serial_data(serial)) == NULL) {
124785d75107117eca0e29ac3da8cb82b45f9cd3a7faGreg Kroah-Hartman		err("%s: serial or serial->private is NULL, status=%d",
1248441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison		    __func__, status);
12491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
12501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
12511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* handle oob callback */
1253c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
1254c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		dbg("digi_write_bulk_callback: oob callback");
1255c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		spin_lock(&priv->dp_port_lock);
12561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		priv->dp_write_urb_in_use = 0;
1257c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		wake_up_interruptible(&port->write_wait);
1258c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		spin_unlock(&priv->dp_port_lock);
12591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
12601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
12611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* try to send any buffered data on this port, if it is open */
1263c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_lock(&priv->dp_port_lock);
12641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	priv->dp_write_urb_in_use = 0;
1265c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (port->open_count && port->write_urb->status != -EINPROGRESS
1266c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	    && priv->dp_out_buf_len > 0) {
12671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*((unsigned char *)(port->write_urb->transfer_buffer))
12681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			= (unsigned char)DIGI_CMD_SEND_DATA;
12691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*((unsigned char *)(port->write_urb->transfer_buffer)+1)
12701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			= (unsigned char)priv->dp_out_buf_len;
1271c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		port->write_urb->transfer_buffer_length = priv->dp_out_buf_len+2;
12721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		port->write_urb->dev = serial->dev;
1273c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		memcpy(port->write_urb->transfer_buffer+2, priv->dp_out_buf,
1274c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			priv->dp_out_buf_len);
1275c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if ((ret = usb_submit_urb(port->write_urb, GFP_ATOMIC)) == 0) {
12761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			priv->dp_write_urb_in_use = 1;
12771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			priv->dp_out_buf_len = 0;
12781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
12791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
12801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* wake up processes sleeping on writes immediately */
1281c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	digi_wakeup_write(port);
12821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* also queue up a wakeup at scheduler time, in case we */
12831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* lost the race in write_chan(). */
12841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	schedule_work(&priv->dp_wakeup_work);
12851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1286c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_unlock(&priv->dp_port_lock);
1287c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (ret)
1288c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		err("%s: usb_submit_urb failed, ret=%d, port=%d",
1289441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison			__func__, ret, priv->dp_port_num);
12901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
12911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1292c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_write_room(struct usb_serial_port *port)
12931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
12941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int room;
12961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_port *priv = usb_get_serial_port_data(port);
12971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags = 0;
12981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1299c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_lock_irqsave(&priv->dp_port_lock, flags);
13001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1301c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (port->write_urb->status == -EINPROGRESS || priv->dp_write_urb_in_use)
13021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		room = 0;
13031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
13041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		room = port->bulk_out_size - 2 - priv->dp_out_buf_len;
13051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1306c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
1307c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	dbg("digi_write_room: port=%d, room=%d", priv->dp_port_num, room);
1308c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	return room;
13091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
13111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1312c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_chars_in_buffer(struct usb_serial_port *port)
13131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
13141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_port *priv = usb_get_serial_port_data(port);
13161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1318c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (port->write_urb->status == -EINPROGRESS
1319c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	    || priv->dp_write_urb_in_use) {
1320c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		dbg("digi_chars_in_buffer: port=%d, chars=%d",
1321c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			priv->dp_port_num, port->bulk_out_size - 2);
1322c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		/* return(port->bulk_out_size - 2); */
1323c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		return 256;
13241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
1325c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		dbg("digi_chars_in_buffer: port=%d, chars=%d",
1326c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			priv->dp_port_num, priv->dp_out_buf_len);
1327c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		return priv->dp_out_buf_len;
13281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
13291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
13311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1333c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_open(struct usb_serial_port *port, struct file *filp)
13341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
13351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
13361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned char buf[32];
13371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_port *priv = usb_get_serial_port_data(port);
1338606d099cdd1080bbb50ea50dc52d98252f8f10a1Alan Cox	struct ktermios not_termios;
13391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long flags = 0;
13401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1341c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	dbg("digi_open: TOP: port=%d, open_count=%d",
1342c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		priv->dp_port_num, port->open_count);
13431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* be sure the device is started up */
1345c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (digi_startup_device(port->serial) != 0)
1346c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		return -ENXIO;
13471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1348c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_lock_irqsave(&priv->dp_port_lock, flags);
13491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* don't wait on a close in progress for non-blocking opens */
1351c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (priv->dp_in_close && (filp->f_flags&(O_NDELAY|O_NONBLOCK)) == 0) {
1352c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		spin_unlock_irqrestore(&priv->dp_port_lock, flags);
1353c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		return -EAGAIN;
13541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
13551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* wait for a close in progress to finish */
1357c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	while(priv->dp_in_close) {
13581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cond_wait_interruptible_timeout_irqrestore(
13591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			&priv->dp_close_wait, DIGI_RETRY_TIMEOUT,
1360c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			&priv->dp_port_lock, flags);
1361c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if (signal_pending(current))
1362c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			return -EINTR;
1363c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		spin_lock_irqsave(&priv->dp_port_lock, flags);
13641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
13651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1366c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
13671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* read modem signals automatically whenever they change */
13691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buf[0] = DIGI_CMD_READ_INPUT_SIGNALS;
13701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buf[1] = priv->dp_port_num;
13711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buf[2] = DIGI_ENABLE;
13721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buf[3] = 0;
13731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* flush fifos */
13751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buf[4] = DIGI_CMD_IFLUSH_FIFO;
13761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buf[5] = priv->dp_port_num;
13771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buf[6] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
13781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buf[7] = 0;
13791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1380c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if ((ret = digi_write_oob_command(port, buf, 8, 1)) != 0)
1381c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		dbg("digi_open: write oob failed, ret=%d", ret);
13821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* set termios settings */
13841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	not_termios.c_cflag = ~port->tty->termios->c_cflag;
13851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	not_termios.c_iflag = ~port->tty->termios->c_iflag;
1386c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	digi_set_termios(port, &not_termios);
13871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* set DTR and RTS */
1389c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	digi_set_modem_signals(port, TIOCM_DTR|TIOCM_RTS, 1);
13901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1391c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	return 0;
13921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
13931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1395c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic void digi_close(struct usb_serial_port *port, struct file *filp)
13961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
13971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	DEFINE_WAIT(wait);
13981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
13991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned char buf[32];
14001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct tty_struct *tty = port->tty;
14011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_port *priv = usb_get_serial_port_data(port);
14021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1403c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	dbg("digi_close: TOP: port=%d, open_count=%d",
1404c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		priv->dp_port_num, port->open_count);
14051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14060915f490d81c1a5098b399ec6c0be45bd421ee1dOliver Neukum	mutex_lock(&port->serial->disc_mutex);
14071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* if disconnected, just clear flags */
14080915f490d81c1a5098b399ec6c0be45bd421ee1dOliver Neukum	if (port->serial->disconnected)
14091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto exit;
14101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* do cleanup only after final close on this port */
14120915f490d81c1a5098b399ec6c0be45bd421ee1dOliver Neukum	spin_lock_irq(&priv->dp_port_lock);
14131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	priv->dp_in_close = 1;
14140915f490d81c1a5098b399ec6c0be45bd421ee1dOliver Neukum	spin_unlock_irq(&priv->dp_port_lock);
14151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* tell line discipline to process only XON/XOFF */
14171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tty->closing = 1;
14181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* wait for output to drain */
1420c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if ((filp->f_flags&(O_NDELAY|O_NONBLOCK)) == 0)
1421c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		tty_wait_until_sent(tty, DIGI_CLOSE_TIMEOUT);
14221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* flush driver and line discipline buffers */
1424c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (tty->driver->flush_buffer)
1425c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		tty->driver->flush_buffer(tty);
14261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tty_ldisc_flush(tty);
14271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (port->serial->dev) {
14291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* wait for transmit idle */
1430c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if ((filp->f_flags&(O_NDELAY|O_NONBLOCK)) == 0) {
1431c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			digi_transmit_idle(port, DIGI_CLOSE_TIMEOUT);
14321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
14331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* drop DTR and RTS */
1434c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		digi_set_modem_signals(port, 0, 0);
14351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* disable input flow control */
14371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[0] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
14381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[1] = priv->dp_port_num;
14391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[2] = DIGI_DISABLE;
14401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[3] = 0;
14411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* disable output flow control */
14431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[4] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
14441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[5] = priv->dp_port_num;
14451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[6] = DIGI_DISABLE;
14461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[7] = 0;
14471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* disable reading modem signals automatically */
14491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[8] = DIGI_CMD_READ_INPUT_SIGNALS;
14501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[9] = priv->dp_port_num;
14511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[10] = DIGI_DISABLE;
14521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[11] = 0;
14531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* disable receive */
14551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[12] = DIGI_CMD_RECEIVE_ENABLE;
14561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[13] = priv->dp_port_num;
14571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[14] = DIGI_DISABLE;
14581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[15] = 0;
14591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* flush fifos */
14611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[16] = DIGI_CMD_IFLUSH_FIFO;
14621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[17] = priv->dp_port_num;
14631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[18] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
14641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		buf[19] = 0;
14651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1466c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if ((ret = digi_write_oob_command(port, buf, 20, 0)) != 0)
1467c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			dbg("digi_close: write oob failed, ret=%d", ret);
14681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* wait for final commands on oob port to complete */
147045f23f189ca66d98b1f8b7f3d30a194d3188039dNishanth Aravamudan		prepare_to_wait(&priv->dp_flush_wait, &wait, TASK_INTERRUPTIBLE);
14711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		schedule_timeout(DIGI_CLOSE_TIMEOUT);
14721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		finish_wait(&priv->dp_flush_wait, &wait);
14731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* shutdown any outstanding bulk writes */
14751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		usb_kill_urb(port->write_urb);
14761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
14771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tty->closing = 0;
14781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsexit:
14790915f490d81c1a5098b399ec6c0be45bd421ee1dOliver Neukum	spin_lock_irq(&priv->dp_port_lock);
14801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	priv->dp_write_urb_in_use = 0;
14811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	priv->dp_in_close = 0;
1482c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	wake_up_interruptible(&priv->dp_close_wait);
14830915f490d81c1a5098b399ec6c0be45bd421ee1dOliver Neukum	spin_unlock_irq(&priv->dp_port_lock);
14840915f490d81c1a5098b399ec6c0be45bd421ee1dOliver Neukum	mutex_unlock(&port->serial->disc_mutex);
1485c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	dbg("digi_close: done");
14861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
14871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
14901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Digi Startup Device
14911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
14921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Starts reads on all ports.  Must be called AFTER startup, with
14931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  urbs initialized.  Returns 0 if successful, non-zero error otherwise.
14941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*/
14951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1496c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_startup_device(struct usb_serial *serial)
14971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
14981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i,ret = 0;
14991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_serial *serial_priv = usb_get_serial_data(serial);
15001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct usb_serial_port *port;
15011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* be sure this happens exactly once */
1503c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_lock(&serial_priv->ds_serial_lock);
1504c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (serial_priv->ds_device_started) {
1505c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		spin_unlock(&serial_priv->ds_serial_lock);
1506c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		return 0;
15071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
15081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	serial_priv->ds_device_started = 1;
1509c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_unlock(&serial_priv->ds_serial_lock);
15101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* start reading from each bulk in endpoint for the device */
15121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* set USB_DISABLE_SPD flag for write bulk urbs */
1513c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	for (i = 0; i < serial->type->num_ports + 1; i++) {
15141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		port = serial->port[i];
15151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		port->write_urb->dev = port->serial->dev;
1516c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if ((ret = usb_submit_urb(port->read_urb, GFP_KERNEL)) != 0) {
1517c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			err("%s: usb_submit_urb failed, ret=%d, port=%d",
1518441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison					__func__, ret, i);
15191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
15201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
15211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1522c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	return ret;
15231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1526c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_startup(struct usb_serial *serial)
15271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
15281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;
15301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_port *priv;
15311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_serial *serial_priv;
15321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1533c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	dbg("digi_startup: TOP");
15341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* allocate the private data structures for all ports */
15361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* number of regular ports + 1 for the out-of-band port */
1537c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	for(i = 0; i < serial->type->num_ports + 1; i++) {
15381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* allocate port private structure */
1539c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		priv = kmalloc(sizeof(struct digi_port), GFP_KERNEL);
1540c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if (priv == NULL) {
1541c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			while (--i >= 0)
1542c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox				kfree(usb_get_serial_port_data(serial->port[i]));
1543c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			return 1;			/* error */
15441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
15451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* initialize port private structure */
1547c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		spin_lock_init(&priv->dp_port_lock);
15481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		priv->dp_port_num = i;
15491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		priv->dp_out_buf_len = 0;
15501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		priv->dp_write_urb_in_use = 0;
15511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		priv->dp_modem_signals = 0;
1552c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		init_waitqueue_head(&priv->dp_modem_change_wait);
15531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		priv->dp_transmit_idle = 0;
1554c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		init_waitqueue_head(&priv->dp_transmit_idle_wait);
15551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		priv->dp_throttled = 0;
15561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		priv->dp_throttle_restart = 0;
1557c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		init_waitqueue_head(&priv->dp_flush_wait);
15581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		priv->dp_in_close = 0;
1559c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		init_waitqueue_head(&priv->dp_close_wait);
1560c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		INIT_WORK(&priv->dp_wakeup_work, digi_wakeup_write_lock);
1561c4028958b6ecad064b1a6303a6a5906d4fe48d73David Howells		priv->dp_port = serial->port[i];
15621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* initialize write wait queue for this port */
1563c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		init_waitqueue_head(&serial->port[i]->write_wait);
15641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		usb_set_serial_port_data(serial->port[i], priv);
15661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
15671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* allocate serial private structure */
1569c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	serial_priv = kmalloc(sizeof(struct digi_serial), GFP_KERNEL);
1570c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (serial_priv == NULL) {
1571c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		for (i = 0; i < serial->type->num_ports + 1; i++)
1572c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			kfree(usb_get_serial_port_data(serial->port[i]));
1573c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		return 1;			/* error */
15741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
15751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* initialize serial private structure */
1577c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_lock_init(&serial_priv->ds_serial_lock);
15781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	serial_priv->ds_oob_port_num = serial->type->num_ports;
15791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	serial_priv->ds_oob_port = serial->port[serial_priv->ds_oob_port_num];
15801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	serial_priv->ds_device_started = 0;
15811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	usb_set_serial_data(serial, serial_priv);
15821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1583c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	return 0;
15841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1587c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic void digi_shutdown(struct usb_serial *serial)
15881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
15891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;
1590c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	dbg("digi_shutdown: TOP, in_interrupt()=%ld", in_interrupt());
15911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* stop reads and writes on all ports */
1593c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	for (i = 0; i < serial->type->num_ports + 1; i++) {
15941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		usb_kill_urb(serial->port[i]->read_urb);
15951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		usb_kill_urb(serial->port[i]->write_urb);
15961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
15971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* free the private data structures for all ports */
15991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* number of regular ports + 1 for the out-of-band port */
1600c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	for(i = 0; i < serial->type->num_ports + 1; i++)
1601c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		kfree(usb_get_serial_port_data(serial->port[i]));
1602c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	kfree(usb_get_serial_data(serial));
16031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
16041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1606c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic void digi_read_bulk_callback(struct urb *urb)
16071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
16081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
16091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_port *priv;
16101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_serial *serial_priv;
16111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
161285d75107117eca0e29ac3da8cb82b45f9cd3a7faGreg Kroah-Hartman	int status = urb->status;
16131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1614c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	dbg("digi_read_bulk_callback: TOP");
16151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* port sanity check, do not resubmit if port is not valid */
1617c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (port == NULL || (priv = usb_get_serial_port_data(port)) == NULL) {
161885d75107117eca0e29ac3da8cb82b45f9cd3a7faGreg Kroah-Hartman		err("%s: port or port->private is NULL, status=%d",
1619441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison		    __func__, status);
16201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
16211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1622c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (port->serial == NULL ||
1623c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		(serial_priv=usb_get_serial_data(port->serial)) == NULL) {
162485d75107117eca0e29ac3da8cb82b45f9cd3a7faGreg Kroah-Hartman		err("%s: serial is bad or serial->private is NULL, status=%d",
1625441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison			__func__, status);
16261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
16271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
16281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* do not resubmit urb if it has any status error */
163085d75107117eca0e29ac3da8cb82b45f9cd3a7faGreg Kroah-Hartman	if (status) {
163185d75107117eca0e29ac3da8cb82b45f9cd3a7faGreg Kroah-Hartman		err("%s: nonzero read bulk status: status=%d, port=%d",
1632441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison		    __func__, status, priv->dp_port_num);
16331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return;
16341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
16351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* handle oob or inb callback, do not resubmit if error */
1637c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
1638c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if (digi_read_oob_callback(urb) != 0)
16391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return;
16401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
1641c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if (digi_read_inb_callback(urb) != 0)
16421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return;
16431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
16441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* continue read */
16461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	urb->dev = port->serial->dev;
1647c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) != 0) {
1648c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		err("%s: failed resubmitting urb, ret=%d, port=%d",
1649441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison		    __func__, ret, priv->dp_port_num);
16501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
16511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
16531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
16551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Digi Read INB Callback
16561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
16571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Digi Read INB Callback handles reads on the in band ports, sending
16581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  the data on to the tty subsystem.  When called we know port and
16591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  port->private are not NULL and port->serial has been validated.
16601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  It returns 0 if successful, 1 if successful but the port is
16611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  throttled, and -1 if the sanity checks failed.
16621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*/
16631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1664c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_read_inb_callback(struct urb *urb)
16651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
16661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
16681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct tty_struct *tty = port->tty;
16691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_port *priv = usb_get_serial_port_data(port);
16701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int opcode = ((unsigned char *)urb->transfer_buffer)[0];
16711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int len = ((unsigned char *)urb->transfer_buffer)[1];
167285d75107117eca0e29ac3da8cb82b45f9cd3a7faGreg Kroah-Hartman	int port_status = ((unsigned char *)urb->transfer_buffer)[2];
16731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned char *data = ((unsigned char *)urb->transfer_buffer)+3;
16741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int flag,throttled;
167533f0f88f1c51ae5c2d593d26960c760ea154c2e2Alan Cox	int i;
167685d75107117eca0e29ac3da8cb82b45f9cd3a7faGreg Kroah-Hartman	int status = urb->status;
16771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* do not process callbacks on closed ports */
16791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* but do continue the read chain */
1680c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (port->open_count == 0)
1681c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		return 0;
16821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* short/multiple packet check */
1684c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (urb->actual_length != len + 2) {
1685c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox     		err("%s: INCOMPLETE OR MULTIPLE PACKET, urb->status=%d, "
168685d75107117eca0e29ac3da8cb82b45f9cd3a7faGreg Kroah-Hartman		    "port=%d, opcode=%d, len=%d, actual_length=%d, "
1687441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison		    "status=%d", __func__, status, priv->dp_port_num,
168885d75107117eca0e29ac3da8cb82b45f9cd3a7faGreg Kroah-Hartman		    opcode, len, urb->actual_length, port_status);
1689c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		return -1;
16901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
16911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1692c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_lock(&priv->dp_port_lock);
16931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* check for throttle; if set, do not resubmit read urb */
16951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* indicate the read chain needs to be restarted on unthrottle */
16961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	throttled = priv->dp_throttled;
1697c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (throttled)
16981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		priv->dp_throttle_restart = 1;
16991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* receive data */
1701c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (opcode == DIGI_CMD_RECEIVE_DATA) {
170285d75107117eca0e29ac3da8cb82b45f9cd3a7faGreg Kroah-Hartman		/* get flag from port_status */
17031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		flag = 0;
17041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* overrun is special, not associated with a char */
1706c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if (port_status & DIGI_OVERRUN_ERROR)
1707c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			tty_insert_flip_char(tty, 0, TTY_OVERRUN);
17081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* break takes precedence over parity, */
17101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* which takes precedence over framing errors */
1711c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if (port_status & DIGI_BREAK_ERROR)
17121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			flag = TTY_BREAK;
1713c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		else if (port_status & DIGI_PARITY_ERROR)
17141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			flag = TTY_PARITY;
1715c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		else if (port_status & DIGI_FRAMING_ERROR)
17161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			flag = TTY_FRAME;
17171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
171885d75107117eca0e29ac3da8cb82b45f9cd3a7faGreg Kroah-Hartman		/* data length is len-1 (one byte of len is port_status) */
17191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		--len;
17201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
172139892da44b21b5362eb848ca424d73a25ccc488fOliver Neukum		len = tty_buffer_request_room(tty, len);
1722c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if (len > 0) {
172339892da44b21b5362eb848ca424d73a25ccc488fOliver Neukum			/* Hot path */
1724c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			if (flag == TTY_NORMAL)
172539892da44b21b5362eb848ca424d73a25ccc488fOliver Neukum				tty_insert_flip_string(tty, data, len);
172639892da44b21b5362eb848ca424d73a25ccc488fOliver Neukum			else {
172739892da44b21b5362eb848ca424d73a25ccc488fOliver Neukum				for(i = 0; i < len; i++)
172839892da44b21b5362eb848ca424d73a25ccc488fOliver Neukum					tty_insert_flip_char(tty, data[i], flag);
17291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
1730c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			tty_flip_buffer_push(tty);
17311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
17321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1733c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	spin_unlock(&priv->dp_port_lock);
17341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1735c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	if (opcode == DIGI_CMD_RECEIVE_DISABLE)
1736441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison		dbg("%s: got RECEIVE_DISABLE", __func__);
1737c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	else if (opcode != DIGI_CMD_RECEIVE_DATA)
1738441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison		dbg("%s: unknown opcode: %d", __func__, opcode);
17391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1740c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	return(throttled ? 1 : 0);
17411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
17431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
17461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Digi Read OOB Callback
17471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*
17481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  Digi Read OOB Callback handles reads on the out of band port.
17491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  When called we know port and port->private are not NULL and
17501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  the port->serial is valid.  It returns 0 if successful, and
17511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*  -1 if the sanity checks failed.
17521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds*/
17531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1754c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int digi_read_oob_callback(struct urb *urb)
17551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
17561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
17581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct usb_serial *serial = port->serial;
17591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct digi_port *priv = usb_get_serial_port_data(port);
17601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int opcode, line, status, val;
17611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;
17621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1763c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	dbg("digi_read_oob_callback: port=%d, len=%d",
1764c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			priv->dp_port_num, urb->actual_length);
17651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* handle each oob command */
1767c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	for(i = 0; i < urb->actual_length - 3;) {
17681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		opcode = ((unsigned char *)urb->transfer_buffer)[i++];
17691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		line = ((unsigned char *)urb->transfer_buffer)[i++];
17701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		status = ((unsigned char *)urb->transfer_buffer)[i++];
17711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		val = ((unsigned char *)urb->transfer_buffer)[i++];
17721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1773c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		dbg("digi_read_oob_callback: opcode=%d, line=%d, status=%d, val=%d",
1774c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			opcode, line, status, val);
17751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1776c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if (status != 0 || line >= serial->type->num_ports)
17771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			continue;
17781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		port = serial->port[line];
17801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1781c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if ((priv=usb_get_serial_port_data(port)) == NULL)
17821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return -1;
17831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1784c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		if (opcode == DIGI_CMD_READ_INPUT_SIGNALS) {
1785c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			spin_lock(&priv->dp_port_lock);
17861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/* convert from digi flags to termiox flags */
1787c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			if (val & DIGI_READ_INPUT_SIGNALS_CTS) {
17881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				priv->dp_modem_signals |= TIOCM_CTS;
17891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				/* port must be open to use tty struct */
1790c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox				if (port->open_count
1791c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox					&& port->tty->termios->c_cflag & CRTSCTS) {
17921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					port->tty->hw_stopped = 0;
1793c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox					digi_wakeup_write(port);
17941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				}
17951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			} else {
17961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				priv->dp_modem_signals &= ~TIOCM_CTS;
17971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				/* port must be open to use tty struct */
1798c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox				if (port->open_count
1799c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox					&& port->tty->termios->c_cflag & CRTSCTS) {
18001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					port->tty->hw_stopped = 1;
18011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				}
18021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
1803c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			if (val & DIGI_READ_INPUT_SIGNALS_DSR)
18041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				priv->dp_modem_signals |= TIOCM_DSR;
18051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			else
18061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				priv->dp_modem_signals &= ~TIOCM_DSR;
1807c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			if (val & DIGI_READ_INPUT_SIGNALS_RI)
18081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				priv->dp_modem_signals |= TIOCM_RI;
18091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			else
18101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				priv->dp_modem_signals &= ~TIOCM_RI;
1811c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			if (val & DIGI_READ_INPUT_SIGNALS_DCD)
18121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				priv->dp_modem_signals |= TIOCM_CD;
18131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			else
18141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				priv->dp_modem_signals &= ~TIOCM_CD;
18151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1816c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			wake_up_interruptible(&priv->dp_modem_change_wait);
1817c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			spin_unlock(&priv->dp_port_lock);
1818c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		} else if (opcode == DIGI_CMD_TRANSMIT_IDLE) {
1819c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			spin_lock(&priv->dp_port_lock);
18201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			priv->dp_transmit_idle = 1;
1821c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			wake_up_interruptible(&priv->dp_transmit_idle_wait);
1822c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			spin_unlock(&priv->dp_port_lock);
1823c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox		} else if (opcode == DIGI_CMD_IFLUSH_FIFO) {
1824c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox			wake_up_interruptible(&priv->dp_flush_wait);
18251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
18261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1827c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	return 0;
18281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
18301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1831c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Coxstatic int __init digi_init(void)
18321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
18331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int retval;
18341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	retval = usb_serial_register(&digi_acceleport_2_device);
18351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (retval)
18361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto failed_acceleport_2_device;
18371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	retval = usb_serial_register(&digi_acceleport_4_device);
18381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (retval)
18391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto failed_acceleport_4_device;
18401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	retval = usb_register(&digi_driver);
18411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (retval)
18421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto failed_usb_register;
18431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	info(DRIVER_VERSION ":" DRIVER_DESC);
18441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
18451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfailed_usb_register:
18461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	usb_serial_deregister(&digi_acceleport_4_device);
18471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfailed_acceleport_4_device:
18481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	usb_serial_deregister(&digi_acceleport_2_device);
18491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsfailed_acceleport_2_device:
18501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return retval;
18511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
18521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void __exit digi_exit (void)
18541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1855c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	usb_deregister(&digi_driver);
1856c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	usb_serial_deregister(&digi_acceleport_2_device);
1857c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan Cox	usb_serial_deregister(&digi_acceleport_4_device);
18581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
18591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_init(digi_init);
18621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_exit(digi_exit);
18631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1865c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan CoxMODULE_AUTHOR(DRIVER_AUTHOR);
1866c6d61269f530e3f7da6bad32e8b42ab7993a5927Alan CoxMODULE_DESCRIPTION(DRIVER_DESC);
18671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_LICENSE("GPL");
18681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_param(debug, bool, S_IRUGO | S_IWUSR);
18701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_PARM_DESC(debug, "Debug enabled or not");
1871