xref: /openbmc/linux/drivers/usb/serial/digi_acceleport.c (revision 60b33c133ca0b7c0b6072c87234b63fee6e80558)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds *  Digi AccelePort USB-4 and USB-2 Serial Converters
31da177e4SLinus Torvalds *
41da177e4SLinus Torvalds *  Copyright 2000 by Digi International
51da177e4SLinus Torvalds *
61da177e4SLinus Torvalds *  This program is free software; you can redistribute it and/or modify
71da177e4SLinus Torvalds *  it under the terms of the GNU General Public License as published by
81da177e4SLinus Torvalds *  the Free Software Foundation; either version 2 of the License, or
91da177e4SLinus Torvalds *  (at your option) any later version.
101da177e4SLinus Torvalds *
111da177e4SLinus Torvalds *  Shamelessly based on Brian Warner's keyspan_pda.c and Greg Kroah-Hartman's
121da177e4SLinus Torvalds *  usb-serial driver.
131da177e4SLinus Torvalds *
141da177e4SLinus Torvalds *  Peter Berger (pberger@brimson.com)
151da177e4SLinus Torvalds *  Al Borchers (borchers@steinerpoint.com)
161da177e4SLinus Torvalds *
171da177e4SLinus Torvalds * (12/03/2001) gkh
1895da310eSAlan Cox *	switched to using port->port.count instead of private version.
191da177e4SLinus Torvalds *	Removed port->active
201da177e4SLinus Torvalds *
211da177e4SLinus Torvalds * (04/08/2001) gb
221da177e4SLinus Torvalds *	Identify version on module load.
231da177e4SLinus Torvalds *
241da177e4SLinus Torvalds * (11/01/2000) Adam J. Richter
251da177e4SLinus Torvalds *	usb_device_id table support
261da177e4SLinus Torvalds *
271da177e4SLinus Torvalds * (11/01/2000) pberger and borchers
281da177e4SLinus Torvalds *    -- Turned off the USB_DISABLE_SPD flag for write bulk urbs--it caused
291da177e4SLinus Torvalds *       USB 4 ports to hang on startup.
301da177e4SLinus Torvalds *    -- Serialized access to write urbs by adding the dp_write_urb_in_use
311da177e4SLinus Torvalds *       flag; otherwise, the driver caused SMP system hangs.  Watching the
321da177e4SLinus Torvalds *       urb status is not sufficient.
331da177e4SLinus Torvalds *
341da177e4SLinus Torvalds * (10/05/2000) gkh
351da177e4SLinus Torvalds *    -- Fixed bug with urb->dev not being set properly, now that the usb
361da177e4SLinus Torvalds *	core needs it.
371da177e4SLinus Torvalds *
381da177e4SLinus Torvalds *  (8/8/2000) pberger and borchers
391da177e4SLinus Torvalds *    -- Fixed close so that
401da177e4SLinus Torvalds *       - it can timeout while waiting for transmit idle, if needed;
411da177e4SLinus Torvalds *       - it ignores interrupts when flushing the port, turning
421da177e4SLinus Torvalds *         of modem signalling, and so on;
431da177e4SLinus Torvalds *       - it waits for the flush to really complete before returning.
441da177e4SLinus Torvalds *    -- Read_bulk_callback and write_bulk_callback check for a closed
451da177e4SLinus Torvalds *       port before using the tty struct or writing to the port.
461da177e4SLinus Torvalds *    -- The two changes above fix the oops caused by interrupted closes.
471da177e4SLinus Torvalds *    -- Added interruptible args to write_oob_command and set_modem_signals
481da177e4SLinus Torvalds *       and added a timeout arg to transmit_idle; needed for fixes to
491da177e4SLinus Torvalds *       close.
501da177e4SLinus Torvalds *    -- Added code for rx_throttle and rx_unthrottle so that input flow
511da177e4SLinus Torvalds *       control works.
521da177e4SLinus Torvalds *    -- Added code to set overrun, parity, framing, and break errors
531da177e4SLinus Torvalds *       (untested).
541da177e4SLinus Torvalds *    -- Set USB_DISABLE_SPD flag for write bulk urbs, so no 0 length
551da177e4SLinus Torvalds *       bulk writes are done.  These hung the Digi USB device.  The
561da177e4SLinus Torvalds *       0 length bulk writes were a new feature of usb-uhci added in
571da177e4SLinus Torvalds *       the 2.4.0-test6 kernels.
581da177e4SLinus Torvalds *    -- Fixed mod inc race in open; do mod inc before sleeping to wait
591da177e4SLinus Torvalds *       for a close to finish.
601da177e4SLinus Torvalds *
611da177e4SLinus Torvalds *  (7/31/2000) pberger
621da177e4SLinus Torvalds *    -- Fixed bugs with hardware handshaking:
631da177e4SLinus Torvalds *       - Added code to set/clear tty->hw_stopped in digi_read_oob_callback()
641da177e4SLinus Torvalds *         and digi_set_termios()
651da177e4SLinus Torvalds *    -- Added code in digi_set_termios() to
661da177e4SLinus Torvalds *       - add conditional in code handling transition from B0 to only
671da177e4SLinus Torvalds *         set RTS if RTS/CTS flow control is either not in use or if
681da177e4SLinus Torvalds *         the port is not currently throttled.
691da177e4SLinus Torvalds *       - handle turning off CRTSCTS.
701da177e4SLinus Torvalds *
711da177e4SLinus Torvalds *  (7/30/2000) borchers
721da177e4SLinus Torvalds *    -- Added support for more than one Digi USB device by moving
731da177e4SLinus Torvalds *       globals to a private structure in the pointed to from the
741da177e4SLinus Torvalds *       usb_serial structure.
751da177e4SLinus Torvalds *    -- Moved the modem change and transmit idle wait queues into
761da177e4SLinus Torvalds *       the port private structure, so each port has its own queue
771da177e4SLinus Torvalds *       rather than sharing global queues.
781da177e4SLinus Torvalds *    -- Added support for break signals.
791da177e4SLinus Torvalds *
801da177e4SLinus Torvalds *  (7/25/2000) pberger
811da177e4SLinus Torvalds *    -- Added USB-2 support.  Note: the USB-2 supports 3 devices: two
821da177e4SLinus Torvalds *       serial and a parallel port.  The parallel port is implemented
831da177e4SLinus Torvalds *       as a serial-to-parallel converter.  That is, the driver actually
841da177e4SLinus Torvalds *       presents all three USB-2 interfaces as serial ports, but the third
851da177e4SLinus Torvalds *       one physically connects to a parallel device.  Thus, for example,
861da177e4SLinus Torvalds *       one could plug a parallel printer into the USB-2's third port,
871da177e4SLinus Torvalds *       but from the kernel's (and userland's) point of view what's
881da177e4SLinus Torvalds *       actually out there is a serial device.
891da177e4SLinus Torvalds *
901da177e4SLinus Torvalds *  (7/15/2000) borchers
911da177e4SLinus Torvalds *    -- Fixed race in open when a close is in progress.
921da177e4SLinus Torvalds *    -- Keep count of opens and dec the module use count for each
931da177e4SLinus Torvalds *       outstanding open when shutdown is called (on disconnect).
941da177e4SLinus Torvalds *    -- Fixed sanity checks in read_bulk_callback and write_bulk_callback
951da177e4SLinus Torvalds *       so pointers are checked before use.
961da177e4SLinus Torvalds *    -- Split read bulk callback into in band and out of band
971da177e4SLinus Torvalds *       callbacks, and no longer restart read chains if there is
981da177e4SLinus Torvalds *       a status error or a sanity error.  This fixed the seg
991da177e4SLinus Torvalds *       faults and other errors we used to get on disconnect.
1001da177e4SLinus Torvalds *    -- Port->active is once again a flag as usb-serial intended it
1011da177e4SLinus Torvalds *       to be, not a count.  Since it was only a char it would
1021da177e4SLinus Torvalds *       have been limited to 256 simultaneous opens.  Now the open
1031da177e4SLinus Torvalds *       count is kept in the port private structure in dp_open_count.
1041da177e4SLinus Torvalds *    -- Added code for modularization of the digi_acceleport driver.
1051da177e4SLinus Torvalds *
1061da177e4SLinus Torvalds *  (6/27/2000) pberger and borchers
1071da177e4SLinus Torvalds *    -- Zeroed out sync field in the wakeup_task before first use;
1081da177e4SLinus Torvalds *       otherwise the uninitialized value might prevent the task from
1091da177e4SLinus Torvalds *       being scheduled.
1101da177e4SLinus Torvalds *    -- Initialized ret value to 0 in write_bulk_callback, otherwise
1111da177e4SLinus Torvalds *       the uninitialized value could cause a spurious debugging message.
1121da177e4SLinus Torvalds *
1131da177e4SLinus Torvalds *  (6/22/2000) pberger and borchers
1141da177e4SLinus Torvalds *    -- Made cond_wait_... inline--apparently on SPARC the flags arg
1151da177e4SLinus Torvalds *       to spin_lock_irqsave cannot be passed to another function
1161da177e4SLinus Torvalds *       to call spin_unlock_irqrestore.  Thanks to Pauline Middelink.
1171da177e4SLinus Torvalds *    -- In digi_set_modem_signals the inner nested spin locks use just
1181da177e4SLinus Torvalds *       spin_lock() rather than spin_lock_irqsave().  The old code
1191da177e4SLinus Torvalds *       mistakenly left interrupts off.  Thanks to Pauline Middelink.
1201da177e4SLinus Torvalds *    -- copy_from_user (which can sleep) is no longer called while a
1211da177e4SLinus Torvalds *       spinlock is held.  We copy to a local buffer before getting
1221da177e4SLinus Torvalds *       the spinlock--don't like the extra copy but the code is simpler.
1231da177e4SLinus Torvalds *    -- Printk and dbg are no longer called while a spin lock is held.
1241da177e4SLinus Torvalds *
1251da177e4SLinus Torvalds *  (6/4/2000) pberger and borchers
1261da177e4SLinus Torvalds *    -- Replaced separate calls to spin_unlock_irqrestore and
1271da177e4SLinus Torvalds *       interruptible_sleep_on_timeout with a new function
1281da177e4SLinus Torvalds *       cond_wait_interruptible_timeout_irqrestore.  This eliminates
1291da177e4SLinus Torvalds *       the race condition where the wake up could happen after
1301da177e4SLinus Torvalds *       the unlock and before the sleep.
1311da177e4SLinus Torvalds *    -- Close now waits for output to drain.
1321da177e4SLinus Torvalds *    -- Open waits until any close in progress is finished.
1331da177e4SLinus Torvalds *    -- All out of band responses are now processed, not just the
1341da177e4SLinus Torvalds *       first in a USB packet.
1351da177e4SLinus Torvalds *    -- Fixed a bug that prevented the driver from working when the
1361da177e4SLinus Torvalds *       first Digi port was not the first USB serial port--the driver
1371da177e4SLinus Torvalds *       was mistakenly using the external USB serial port number to
1381da177e4SLinus Torvalds *       try to index into its internal ports.
1391da177e4SLinus Torvalds *    -- Fixed an SMP bug -- write_bulk_callback is called directly from
1401da177e4SLinus Torvalds *       an interrupt, so spin_lock_irqsave/spin_unlock_irqrestore are
1411da177e4SLinus Torvalds *       needed for locks outside write_bulk_callback that are also
1421da177e4SLinus Torvalds *       acquired by write_bulk_callback to prevent deadlocks.
1431da177e4SLinus Torvalds *    -- Fixed support for select() by making digi_chars_in_buffer()
1441da177e4SLinus Torvalds *       return 256 when -EINPROGRESS is set, as the line discipline
1451da177e4SLinus Torvalds *       code in n_tty.c expects.
1461da177e4SLinus Torvalds *    -- Fixed an include file ordering problem that prevented debugging
1471da177e4SLinus Torvalds *       messages from working.
1481da177e4SLinus Torvalds *    -- Fixed an intermittent timeout problem that caused writes to
1491da177e4SLinus Torvalds *       sometimes get stuck on some machines on some kernels.  It turns
1501da177e4SLinus Torvalds *       out in these circumstances write_chan() (in n_tty.c) was
1511da177e4SLinus Torvalds *       asleep waiting for our wakeup call.  Even though we call
1521da177e4SLinus Torvalds *       wake_up_interruptible() in digi_write_bulk_callback(), there is
1531da177e4SLinus Torvalds *       a race condition that could cause the wakeup to fail: if our
1541da177e4SLinus Torvalds *       wake_up_interruptible() call occurs between the time that our
1551da177e4SLinus Torvalds *       driver write routine finishes and write_chan() sets current->state
1561da177e4SLinus Torvalds *       to TASK_INTERRUPTIBLE, the effect of our wakeup setting the state
1571da177e4SLinus Torvalds *       to TASK_RUNNING will be lost and write_chan's subsequent call to
1581da177e4SLinus Torvalds *       schedule() will never return (unless it catches a signal).
1591da177e4SLinus Torvalds *       This race condition occurs because write_bulk_callback() (and thus
1600779bf2dSMatt LaPlante *       the wakeup) are called asynchronously from an interrupt, rather than
1611da177e4SLinus Torvalds *       from the scheduler.  We can avoid the race by calling the wakeup
1621da177e4SLinus Torvalds *       from the scheduler queue and that's our fix:  Now, at the end of
1631da177e4SLinus Torvalds *       write_bulk_callback() we queue up a wakeup call on the scheduler
1641da177e4SLinus Torvalds *       task queue.  We still also invoke the wakeup directly since that
1651da177e4SLinus Torvalds *       squeezes a bit more performance out of the driver, and any lost
1661da177e4SLinus Torvalds *       race conditions will get cleaned up at the next scheduler run.
1671da177e4SLinus Torvalds *
1681da177e4SLinus Torvalds *       NOTE:  The problem also goes away if you comment out
1691da177e4SLinus Torvalds *       the two code lines in write_chan() where current->state
1701da177e4SLinus Torvalds *       is set to TASK_RUNNING just before calling driver.write() and to
1711da177e4SLinus Torvalds *       TASK_INTERRUPTIBLE immediately afterwards.  This is why the
1721da177e4SLinus Torvalds *       problem did not show up with the 2.2 kernels -- they do not
1731da177e4SLinus Torvalds *       include that code.
1741da177e4SLinus Torvalds *
1751da177e4SLinus Torvalds *  (5/16/2000) pberger and borchers
1761da177e4SLinus Torvalds *    -- Added timeouts to sleeps, to defend against lost wake ups.
1771da177e4SLinus Torvalds *    -- Handle transition to/from B0 baud rate in digi_set_termios.
1781da177e4SLinus Torvalds *
1791da177e4SLinus Torvalds *  (5/13/2000) pberger and borchers
1801da177e4SLinus Torvalds *    -- All commands now sent on out of band port, using
1811da177e4SLinus Torvalds *       digi_write_oob_command.
1821da177e4SLinus Torvalds *    -- Get modem control signals whenever they change, support TIOCMGET/
1831da177e4SLinus Torvalds *       SET/BIS/BIC ioctls.
1841da177e4SLinus Torvalds *    -- digi_set_termios now supports parity, word size, stop bits, and
1851da177e4SLinus Torvalds *       receive enable.
1861da177e4SLinus Torvalds *    -- Cleaned up open and close, use digi_set_termios and
1871da177e4SLinus Torvalds *       digi_write_oob_command to set port parameters.
1881da177e4SLinus Torvalds *    -- Added digi_startup_device to start read chains on all ports.
1891da177e4SLinus Torvalds *    -- Write buffer is only used when count==1, to be sure put_char can
1901da177e4SLinus Torvalds *       write a char (unless the buffer is full).
1911da177e4SLinus Torvalds *
1921da177e4SLinus Torvalds *  (5/10/2000) pberger and borchers
1931da177e4SLinus Torvalds *    -- Added MOD_INC_USE_COUNT/MOD_DEC_USE_COUNT calls on open/close.
1941da177e4SLinus Torvalds *    -- Fixed problem where the first incoming character is lost on
1951da177e4SLinus Torvalds *       port opens after the first close on that port.  Now we keep
1961da177e4SLinus Torvalds *       the read_urb chain open until shutdown.
1971da177e4SLinus Torvalds *    -- Added more port conditioning calls in digi_open and digi_close.
1981da177e4SLinus Torvalds *    -- Convert port->active to a use count so that we can deal with multiple
1991da177e4SLinus Torvalds *       opens and closes properly.
2001da177e4SLinus Torvalds *    -- Fixed some problems with the locking code.
2011da177e4SLinus Torvalds *
2021da177e4SLinus Torvalds *  (5/3/2000) pberger and borchers
2031da177e4SLinus Torvalds *    -- First alpha version of the driver--many known limitations and bugs.
2041da177e4SLinus Torvalds *
2051da177e4SLinus Torvalds *
2061da177e4SLinus Torvalds *  Locking and SMP
2071da177e4SLinus Torvalds *
2081da177e4SLinus Torvalds *  - Each port, including the out-of-band port, has a lock used to
2091da177e4SLinus Torvalds *    serialize all access to the port's private structure.
2101da177e4SLinus Torvalds *  - The port lock is also used to serialize all writes and access to
2111da177e4SLinus Torvalds *    the port's URB.
2121da177e4SLinus Torvalds *  - The port lock is also used for the port write_wait condition
2131da177e4SLinus Torvalds *    variable.  Holding the port lock will prevent a wake up on the
2141da177e4SLinus Torvalds *    port's write_wait; this can be used with cond_wait_... to be sure
2151da177e4SLinus Torvalds *    the wake up is not lost in a race when dropping the lock and
2161da177e4SLinus Torvalds *    sleeping waiting for the wakeup.
2171da177e4SLinus Torvalds *  - digi_write() does not sleep, since it is sometimes called on
2181da177e4SLinus Torvalds *    interrupt time.
2191da177e4SLinus Torvalds *  - digi_write_bulk_callback() and digi_read_bulk_callback() are
2201da177e4SLinus Torvalds *    called directly from interrupts.  Hence spin_lock_irqsave()
2211da177e4SLinus Torvalds *    and spin_unlock_irqrestore() are used in the rest of the code
2221da177e4SLinus Torvalds *    for any locks they acquire.
2231da177e4SLinus Torvalds *  - digi_write_bulk_callback() gets the port lock before waking up
2241da177e4SLinus Torvalds *    processes sleeping on the port write_wait.  It also schedules
2251da177e4SLinus Torvalds *    wake ups so they happen from the scheduler, because the tty
2261da177e4SLinus Torvalds *    system can miss wake ups from interrupts.
2271da177e4SLinus Torvalds *  - All sleeps use a timeout of DIGI_RETRY_TIMEOUT before looping to
2281da177e4SLinus Torvalds *    recheck the condition they are sleeping on.  This is defensive,
2291da177e4SLinus Torvalds *    in case a wake up is lost.
2301da177e4SLinus Torvalds *  - Following Documentation/DocBook/kernel-locking.pdf no spin locks
2311da177e4SLinus Torvalds *    are held when calling copy_to/from_user or printk.
2321da177e4SLinus Torvalds */
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds #include <linux/kernel.h>
2351da177e4SLinus Torvalds #include <linux/errno.h>
2361da177e4SLinus Torvalds #include <linux/init.h>
2371da177e4SLinus Torvalds #include <linux/slab.h>
2381da177e4SLinus Torvalds #include <linux/tty.h>
2391da177e4SLinus Torvalds #include <linux/tty_driver.h>
2401da177e4SLinus Torvalds #include <linux/tty_flip.h>
2411da177e4SLinus Torvalds #include <linux/module.h>
2421da177e4SLinus Torvalds #include <linux/spinlock.h>
2431da177e4SLinus Torvalds #include <linux/workqueue.h>
24441ad427dSAlan Cox #include <linux/uaccess.h>
2451da177e4SLinus Torvalds #include <linux/usb.h>
2461da177e4SLinus Torvalds #include <linux/wait.h>
247a969888cSGreg Kroah-Hartman #include <linux/usb/serial.h>
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds /* Defines */
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds /*
2521da177e4SLinus Torvalds  * Version Information
2531da177e4SLinus Torvalds  */
2541da177e4SLinus Torvalds #define DRIVER_VERSION "v1.80.1.2"
2551da177e4SLinus Torvalds #define DRIVER_AUTHOR "Peter Berger <pberger@brimson.com>, Al Borchers <borchers@steinerpoint.com>"
2561da177e4SLinus Torvalds #define DRIVER_DESC "Digi AccelePort USB-2/USB-4 Serial Converter driver"
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds /* port output buffer length -- must be <= transfer buffer length - 2 */
2591da177e4SLinus Torvalds /* so we can be sure to send the full buffer in one urb */
2601da177e4SLinus Torvalds #define DIGI_OUT_BUF_SIZE		8
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds /* port input buffer length -- must be >= transfer buffer length - 3 */
2631da177e4SLinus Torvalds /* so we can be sure to hold at least one full buffer from one urb */
2641da177e4SLinus Torvalds #define DIGI_IN_BUF_SIZE		64
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds /* retry timeout while sleeping */
2671da177e4SLinus Torvalds #define DIGI_RETRY_TIMEOUT		(HZ/10)
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds /* timeout while waiting for tty output to drain in close */
2701da177e4SLinus Torvalds /* this delay is used twice in close, so the total delay could */
2711da177e4SLinus Torvalds /* be twice this value */
2721da177e4SLinus Torvalds #define DIGI_CLOSE_TIMEOUT		(5*HZ)
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds /* AccelePort USB Defines */
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds /* ids */
2781da177e4SLinus Torvalds #define DIGI_VENDOR_ID			0x05c5
2791da177e4SLinus Torvalds #define DIGI_2_ID			0x0002	/* USB-2 */
2801da177e4SLinus Torvalds #define DIGI_4_ID			0x0004	/* USB-4 */
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds /* commands
2831da177e4SLinus Torvalds  * "INB": can be used on the in-band endpoint
2841da177e4SLinus Torvalds  * "OOB": can be used on the out-of-band endpoint
2851da177e4SLinus Torvalds  */
2861da177e4SLinus Torvalds #define DIGI_CMD_SET_BAUD_RATE			0	/* INB, OOB */
2871da177e4SLinus Torvalds #define DIGI_CMD_SET_WORD_SIZE			1	/* INB, OOB */
2881da177e4SLinus Torvalds #define DIGI_CMD_SET_PARITY			2	/* INB, OOB */
2891da177e4SLinus Torvalds #define DIGI_CMD_SET_STOP_BITS			3	/* INB, OOB */
2901da177e4SLinus Torvalds #define DIGI_CMD_SET_INPUT_FLOW_CONTROL		4	/* INB, OOB */
2911da177e4SLinus Torvalds #define DIGI_CMD_SET_OUTPUT_FLOW_CONTROL	5	/* INB, OOB */
2921da177e4SLinus Torvalds #define DIGI_CMD_SET_DTR_SIGNAL			6	/* INB, OOB */
2931da177e4SLinus Torvalds #define DIGI_CMD_SET_RTS_SIGNAL			7	/* INB, OOB */
2941da177e4SLinus Torvalds #define DIGI_CMD_READ_INPUT_SIGNALS		8	/*      OOB */
2951da177e4SLinus Torvalds #define DIGI_CMD_IFLUSH_FIFO			9	/*      OOB */
2961da177e4SLinus Torvalds #define DIGI_CMD_RECEIVE_ENABLE			10	/* INB, OOB */
2971da177e4SLinus Torvalds #define DIGI_CMD_BREAK_CONTROL			11	/* INB, OOB */
2981da177e4SLinus Torvalds #define DIGI_CMD_LOCAL_LOOPBACK			12	/* INB, OOB */
2991da177e4SLinus Torvalds #define DIGI_CMD_TRANSMIT_IDLE			13	/* INB, OOB */
3001da177e4SLinus Torvalds #define DIGI_CMD_READ_UART_REGISTER		14	/*      OOB */
3011da177e4SLinus Torvalds #define DIGI_CMD_WRITE_UART_REGISTER		15	/* INB, OOB */
3021da177e4SLinus Torvalds #define DIGI_CMD_AND_UART_REGISTER		16	/* INB, OOB */
3031da177e4SLinus Torvalds #define DIGI_CMD_OR_UART_REGISTER		17	/* INB, OOB */
3041da177e4SLinus Torvalds #define DIGI_CMD_SEND_DATA			18	/* INB      */
3051da177e4SLinus Torvalds #define DIGI_CMD_RECEIVE_DATA			19	/* INB      */
3061da177e4SLinus Torvalds #define DIGI_CMD_RECEIVE_DISABLE		20	/* INB      */
3071da177e4SLinus Torvalds #define DIGI_CMD_GET_PORT_TYPE			21	/*      OOB */
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds /* baud rates */
3101da177e4SLinus Torvalds #define DIGI_BAUD_50				0
3111da177e4SLinus Torvalds #define DIGI_BAUD_75				1
3121da177e4SLinus Torvalds #define DIGI_BAUD_110				2
3131da177e4SLinus Torvalds #define DIGI_BAUD_150				3
3141da177e4SLinus Torvalds #define DIGI_BAUD_200				4
3151da177e4SLinus Torvalds #define DIGI_BAUD_300				5
3161da177e4SLinus Torvalds #define DIGI_BAUD_600				6
3171da177e4SLinus Torvalds #define DIGI_BAUD_1200				7
3181da177e4SLinus Torvalds #define DIGI_BAUD_1800				8
3191da177e4SLinus Torvalds #define DIGI_BAUD_2400				9
3201da177e4SLinus Torvalds #define DIGI_BAUD_4800				10
3211da177e4SLinus Torvalds #define DIGI_BAUD_7200				11
3221da177e4SLinus Torvalds #define DIGI_BAUD_9600				12
3231da177e4SLinus Torvalds #define DIGI_BAUD_14400				13
3241da177e4SLinus Torvalds #define DIGI_BAUD_19200				14
3251da177e4SLinus Torvalds #define DIGI_BAUD_28800				15
3261da177e4SLinus Torvalds #define DIGI_BAUD_38400				16
3271da177e4SLinus Torvalds #define DIGI_BAUD_57600				17
3281da177e4SLinus Torvalds #define DIGI_BAUD_76800				18
3291da177e4SLinus Torvalds #define DIGI_BAUD_115200			19
3301da177e4SLinus Torvalds #define DIGI_BAUD_153600			20
3311da177e4SLinus Torvalds #define DIGI_BAUD_230400			21
3321da177e4SLinus Torvalds #define DIGI_BAUD_460800			22
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds /* arguments */
3351da177e4SLinus Torvalds #define DIGI_WORD_SIZE_5			0
3361da177e4SLinus Torvalds #define DIGI_WORD_SIZE_6			1
3371da177e4SLinus Torvalds #define DIGI_WORD_SIZE_7			2
3381da177e4SLinus Torvalds #define DIGI_WORD_SIZE_8			3
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds #define DIGI_PARITY_NONE			0
3411da177e4SLinus Torvalds #define DIGI_PARITY_ODD				1
3421da177e4SLinus Torvalds #define DIGI_PARITY_EVEN			2
3431da177e4SLinus Torvalds #define DIGI_PARITY_MARK			3
3441da177e4SLinus Torvalds #define DIGI_PARITY_SPACE			4
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds #define DIGI_STOP_BITS_1			0
3471da177e4SLinus Torvalds #define DIGI_STOP_BITS_2			1
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds #define DIGI_INPUT_FLOW_CONTROL_XON_XOFF	1
3501da177e4SLinus Torvalds #define DIGI_INPUT_FLOW_CONTROL_RTS		2
3511da177e4SLinus Torvalds #define DIGI_INPUT_FLOW_CONTROL_DTR		4
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds #define DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF	1
3541da177e4SLinus Torvalds #define DIGI_OUTPUT_FLOW_CONTROL_CTS		2
3551da177e4SLinus Torvalds #define DIGI_OUTPUT_FLOW_CONTROL_DSR		4
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds #define DIGI_DTR_INACTIVE			0
3581da177e4SLinus Torvalds #define DIGI_DTR_ACTIVE				1
3591da177e4SLinus Torvalds #define DIGI_DTR_INPUT_FLOW_CONTROL		2
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds #define DIGI_RTS_INACTIVE			0
3621da177e4SLinus Torvalds #define DIGI_RTS_ACTIVE				1
3631da177e4SLinus Torvalds #define DIGI_RTS_INPUT_FLOW_CONTROL		2
3641da177e4SLinus Torvalds #define DIGI_RTS_TOGGLE				3
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds #define DIGI_FLUSH_TX				1
3671da177e4SLinus Torvalds #define DIGI_FLUSH_RX				2
3681da177e4SLinus Torvalds #define DIGI_RESUME_TX				4 /* clears xoff condition */
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds #define DIGI_TRANSMIT_NOT_IDLE			0
3711da177e4SLinus Torvalds #define DIGI_TRANSMIT_IDLE			1
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds #define DIGI_DISABLE				0
3741da177e4SLinus Torvalds #define DIGI_ENABLE				1
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds #define DIGI_DEASSERT				0
3771da177e4SLinus Torvalds #define DIGI_ASSERT				1
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds /* in band status codes */
3801da177e4SLinus Torvalds #define DIGI_OVERRUN_ERROR			4
3811da177e4SLinus Torvalds #define DIGI_PARITY_ERROR			8
3821da177e4SLinus Torvalds #define DIGI_FRAMING_ERROR			16
3831da177e4SLinus Torvalds #define DIGI_BREAK_ERROR			32
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds /* out of band status */
3861da177e4SLinus Torvalds #define DIGI_NO_ERROR				0
3871da177e4SLinus Torvalds #define DIGI_BAD_FIRST_PARAMETER		1
3881da177e4SLinus Torvalds #define DIGI_BAD_SECOND_PARAMETER		2
3891da177e4SLinus Torvalds #define DIGI_INVALID_LINE			3
3901da177e4SLinus Torvalds #define DIGI_INVALID_OPCODE			4
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds /* input signals */
3931da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_SLOT		1
3941da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_ERR		2
3951da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_BUSY		4
3961da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_PE		8
3971da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_CTS		16
3981da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_DSR		32
3991da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_RI		64
4001da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_DCD		128
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds /* Structures */
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds struct digi_serial {
4061da177e4SLinus Torvalds 	spinlock_t ds_serial_lock;
4071da177e4SLinus Torvalds 	struct usb_serial_port *ds_oob_port;	/* out-of-band port */
4081da177e4SLinus Torvalds 	int ds_oob_port_num;			/* index of out-of-band port */
4091da177e4SLinus Torvalds 	int ds_device_started;
4101da177e4SLinus Torvalds };
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds struct digi_port {
4131da177e4SLinus Torvalds 	spinlock_t dp_port_lock;
4141da177e4SLinus Torvalds 	int dp_port_num;
4151da177e4SLinus Torvalds 	int dp_out_buf_len;
4161da177e4SLinus Torvalds 	unsigned char dp_out_buf[DIGI_OUT_BUF_SIZE];
4171da177e4SLinus Torvalds 	int dp_write_urb_in_use;
4181da177e4SLinus Torvalds 	unsigned int dp_modem_signals;
4191da177e4SLinus Torvalds 	wait_queue_head_t dp_modem_change_wait;
4201da177e4SLinus Torvalds 	int dp_transmit_idle;
4211da177e4SLinus Torvalds 	wait_queue_head_t dp_transmit_idle_wait;
4221da177e4SLinus Torvalds 	int dp_throttled;
4231da177e4SLinus Torvalds 	int dp_throttle_restart;
4241da177e4SLinus Torvalds 	wait_queue_head_t dp_flush_wait;
4251da177e4SLinus Torvalds 	wait_queue_head_t dp_close_wait;	/* wait queue for close */
4261da177e4SLinus Torvalds 	struct work_struct dp_wakeup_work;
427c4028958SDavid Howells 	struct usb_serial_port *dp_port;
4281da177e4SLinus Torvalds };
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds /* Local Function Declarations */
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds static void digi_wakeup_write(struct usb_serial_port *port);
434c4028958SDavid Howells static void digi_wakeup_write_lock(struct work_struct *work);
4351da177e4SLinus Torvalds static int digi_write_oob_command(struct usb_serial_port *port,
4361da177e4SLinus Torvalds 	unsigned char *buf, int count, int interruptible);
4371da177e4SLinus Torvalds static int digi_write_inb_command(struct usb_serial_port *port,
4381da177e4SLinus Torvalds 	unsigned char *buf, int count, unsigned long timeout);
4391da177e4SLinus Torvalds static int digi_set_modem_signals(struct usb_serial_port *port,
4401da177e4SLinus Torvalds 	unsigned int modem_signals, int interruptible);
4411da177e4SLinus Torvalds static int digi_transmit_idle(struct usb_serial_port *port,
4421da177e4SLinus Torvalds 	unsigned long timeout);
44395da310eSAlan Cox static void digi_rx_throttle(struct tty_struct *tty);
44495da310eSAlan Cox static void digi_rx_unthrottle(struct tty_struct *tty);
44595da310eSAlan Cox static void digi_set_termios(struct tty_struct *tty,
44695da310eSAlan Cox 		struct usb_serial_port *port, struct ktermios *old_termios);
44795da310eSAlan Cox static void digi_break_ctl(struct tty_struct *tty, int break_state);
448*60b33c13SAlan Cox static int digi_tiocmget(struct tty_struct *tty);
44995da310eSAlan Cox static int digi_tiocmset(struct tty_struct *tty, struct file *file,
4501da177e4SLinus Torvalds 	unsigned int set, unsigned int clear);
45195da310eSAlan Cox static int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
45295da310eSAlan Cox 	const unsigned char *buf, int count);
4537d12e780SDavid Howells static void digi_write_bulk_callback(struct urb *urb);
45495da310eSAlan Cox static int digi_write_room(struct tty_struct *tty);
45595da310eSAlan Cox static int digi_chars_in_buffer(struct tty_struct *tty);
456a509a7e4SAlan Cox static int digi_open(struct tty_struct *tty, struct usb_serial_port *port);
457335f8514SAlan Cox static void digi_close(struct usb_serial_port *port);
458335f8514SAlan Cox static void digi_dtr_rts(struct usb_serial_port *port, int on);
4591da177e4SLinus Torvalds static int digi_startup_device(struct usb_serial *serial);
4601da177e4SLinus Torvalds static int digi_startup(struct usb_serial *serial);
461f9c99bb8SAlan Stern static void digi_disconnect(struct usb_serial *serial);
462f9c99bb8SAlan Stern static void digi_release(struct usb_serial *serial);
4637d12e780SDavid Howells static void digi_read_bulk_callback(struct urb *urb);
4641da177e4SLinus Torvalds static int digi_read_inb_callback(struct urb *urb);
4651da177e4SLinus Torvalds static int digi_read_oob_callback(struct urb *urb);
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds /* Statics */
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds static int debug;
4711da177e4SLinus Torvalds 
4727d40d7e8SNémeth Márton static const struct usb_device_id id_table_combined[] = {
4731da177e4SLinus Torvalds 	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
4741da177e4SLinus Torvalds 	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
4751da177e4SLinus Torvalds 	{ }						/* Terminating entry */
4761da177e4SLinus Torvalds };
4771da177e4SLinus Torvalds 
4787d40d7e8SNémeth Márton static const struct usb_device_id id_table_2[] = {
4791da177e4SLinus Torvalds 	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
4801da177e4SLinus Torvalds 	{ }						/* Terminating entry */
4811da177e4SLinus Torvalds };
4821da177e4SLinus Torvalds 
4837d40d7e8SNémeth Márton static const struct usb_device_id id_table_4[] = {
4841da177e4SLinus Torvalds 	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
4851da177e4SLinus Torvalds 	{ }						/* Terminating entry */
4861da177e4SLinus Torvalds };
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds MODULE_DEVICE_TABLE(usb, id_table_combined);
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds static struct usb_driver digi_driver = {
4911da177e4SLinus Torvalds 	.name =		"digi_acceleport",
4921da177e4SLinus Torvalds 	.probe =	usb_serial_probe,
4931da177e4SLinus Torvalds 	.disconnect =	usb_serial_disconnect,
4941da177e4SLinus Torvalds 	.id_table =	id_table_combined,
495ba9dc657SGreg Kroah-Hartman 	.no_dynamic_id = 	1,
4961da177e4SLinus Torvalds };
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds /* device info needed for the Digi serial converter */
5001da177e4SLinus Torvalds 
501ea65370dSGreg Kroah-Hartman static struct usb_serial_driver digi_acceleport_2_device = {
50218fcac35SGreg Kroah-Hartman 	.driver = {
5031da177e4SLinus Torvalds 		.owner =		THIS_MODULE,
504269bda1cSGreg Kroah-Hartman 		.name =			"digi_2",
50518fcac35SGreg Kroah-Hartman 	},
506269bda1cSGreg Kroah-Hartman 	.description =			"Digi 2 port USB adapter",
507d9b1b787SJohannes Hölzl 	.usb_driver = 			&digi_driver,
5081da177e4SLinus Torvalds 	.id_table =			id_table_2,
5091da177e4SLinus Torvalds 	.num_ports =			3,
5101da177e4SLinus Torvalds 	.open =				digi_open,
5111da177e4SLinus Torvalds 	.close =			digi_close,
512335f8514SAlan Cox 	.dtr_rts =			digi_dtr_rts,
5131da177e4SLinus Torvalds 	.write =			digi_write,
5141da177e4SLinus Torvalds 	.write_room =			digi_write_room,
5151da177e4SLinus Torvalds 	.write_bulk_callback = 		digi_write_bulk_callback,
5161da177e4SLinus Torvalds 	.read_bulk_callback =		digi_read_bulk_callback,
5171da177e4SLinus Torvalds 	.chars_in_buffer =		digi_chars_in_buffer,
5181da177e4SLinus Torvalds 	.throttle =			digi_rx_throttle,
5191da177e4SLinus Torvalds 	.unthrottle =			digi_rx_unthrottle,
5201da177e4SLinus Torvalds 	.set_termios =			digi_set_termios,
5211da177e4SLinus Torvalds 	.break_ctl =			digi_break_ctl,
5221da177e4SLinus Torvalds 	.tiocmget =			digi_tiocmget,
5231da177e4SLinus Torvalds 	.tiocmset =			digi_tiocmset,
5241da177e4SLinus Torvalds 	.attach =			digi_startup,
525f9c99bb8SAlan Stern 	.disconnect =			digi_disconnect,
526f9c99bb8SAlan Stern 	.release =			digi_release,
5271da177e4SLinus Torvalds };
5281da177e4SLinus Torvalds 
529ea65370dSGreg Kroah-Hartman static struct usb_serial_driver digi_acceleport_4_device = {
53018fcac35SGreg Kroah-Hartman 	.driver = {
5311da177e4SLinus Torvalds 		.owner =		THIS_MODULE,
532269bda1cSGreg Kroah-Hartman 		.name =			"digi_4",
53318fcac35SGreg Kroah-Hartman 	},
534269bda1cSGreg Kroah-Hartman 	.description =			"Digi 4 port USB adapter",
535d9b1b787SJohannes Hölzl 	.usb_driver = 			&digi_driver,
5361da177e4SLinus Torvalds 	.id_table =			id_table_4,
5371da177e4SLinus Torvalds 	.num_ports =			4,
5381da177e4SLinus Torvalds 	.open =				digi_open,
5391da177e4SLinus Torvalds 	.close =			digi_close,
5401da177e4SLinus Torvalds 	.write =			digi_write,
5411da177e4SLinus Torvalds 	.write_room =			digi_write_room,
5421da177e4SLinus Torvalds 	.write_bulk_callback = 		digi_write_bulk_callback,
5431da177e4SLinus Torvalds 	.read_bulk_callback =		digi_read_bulk_callback,
5441da177e4SLinus Torvalds 	.chars_in_buffer =		digi_chars_in_buffer,
5451da177e4SLinus Torvalds 	.throttle =			digi_rx_throttle,
5461da177e4SLinus Torvalds 	.unthrottle =			digi_rx_unthrottle,
5471da177e4SLinus Torvalds 	.set_termios =			digi_set_termios,
5481da177e4SLinus Torvalds 	.break_ctl =			digi_break_ctl,
5491da177e4SLinus Torvalds 	.tiocmget =			digi_tiocmget,
5501da177e4SLinus Torvalds 	.tiocmset =			digi_tiocmset,
5511da177e4SLinus Torvalds 	.attach =			digi_startup,
552f9c99bb8SAlan Stern 	.disconnect =			digi_disconnect,
553f9c99bb8SAlan Stern 	.release =			digi_release,
5541da177e4SLinus Torvalds };
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds /* Functions */
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds /*
5601da177e4SLinus Torvalds  *  Cond Wait Interruptible Timeout Irqrestore
5611da177e4SLinus Torvalds  *
5621da177e4SLinus Torvalds  *  Do spin_unlock_irqrestore and interruptible_sleep_on_timeout
5631da177e4SLinus Torvalds  *  so that wake ups are not lost if they occur between the unlock
5641da177e4SLinus Torvalds  *  and the sleep.  In other words, spin_unlock_irqrestore and
5651da177e4SLinus Torvalds  *  interruptible_sleep_on_timeout are "atomic" with respect to
5661da177e4SLinus Torvalds  *  wake ups.  This is used to implement condition variables.
56745f23f18SNishanth Aravamudan  *
56845f23f18SNishanth Aravamudan  *  interruptible_sleep_on_timeout is deprecated and has been replaced
56945f23f18SNishanth Aravamudan  *  with the equivalent code.
5701da177e4SLinus Torvalds  */
5711da177e4SLinus Torvalds 
572c6d61269SAlan Cox static long cond_wait_interruptible_timeout_irqrestore(
5731da177e4SLinus Torvalds 	wait_queue_head_t *q, long timeout,
5741da177e4SLinus Torvalds 	spinlock_t *lock, unsigned long flags)
5755a33956aSHarvey Harrison __releases(lock)
5761da177e4SLinus Torvalds {
5771da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
5781da177e4SLinus Torvalds 
57945f23f18SNishanth Aravamudan 	prepare_to_wait(q, &wait, TASK_INTERRUPTIBLE);
5801da177e4SLinus Torvalds 	spin_unlock_irqrestore(lock, flags);
5811da177e4SLinus Torvalds 	timeout = schedule_timeout(timeout);
5821da177e4SLinus Torvalds 	finish_wait(q, &wait);
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	return timeout;
5851da177e4SLinus Torvalds }
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds /*
5891da177e4SLinus Torvalds  *  Digi Wakeup Write
5901da177e4SLinus Torvalds  *
5911da177e4SLinus Torvalds  *  Wake up port, line discipline, and tty processes sleeping
5921da177e4SLinus Torvalds  *  on writes.
5931da177e4SLinus Torvalds  */
5941da177e4SLinus Torvalds 
595c4028958SDavid Howells static void digi_wakeup_write_lock(struct work_struct *work)
5961da177e4SLinus Torvalds {
59741ad427dSAlan Cox 	struct digi_port *priv =
59841ad427dSAlan Cox 			container_of(work, struct digi_port, dp_wakeup_work);
599c4028958SDavid Howells 	struct usb_serial_port *port = priv->dp_port;
6001da177e4SLinus Torvalds 	unsigned long flags;
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
6031da177e4SLinus Torvalds 	digi_wakeup_write(port);
6041da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
6051da177e4SLinus Torvalds }
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds static void digi_wakeup_write(struct usb_serial_port *port)
6081da177e4SLinus Torvalds {
6094a90f09bSAlan Cox 	struct tty_struct *tty = tty_port_tty_get(&port->port);
6104287341dSAlan Cox 	if (tty) {
6114a90f09bSAlan Cox 		tty_wakeup(tty);
6124a90f09bSAlan Cox 		tty_kref_put(tty);
6131da177e4SLinus Torvalds 	}
6144287341dSAlan Cox }
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds /*
6181da177e4SLinus Torvalds  *  Digi Write OOB Command
6191da177e4SLinus Torvalds  *
6201da177e4SLinus Torvalds  *  Write commands on the out of band port.  Commands are 4
6211da177e4SLinus Torvalds  *  bytes each, multiple commands can be sent at once, and
6221da177e4SLinus Torvalds  *  no command will be split across USB packets.  Returns 0
6231da177e4SLinus Torvalds  *  if successful, -EINTR if interrupted while sleeping and
6241da177e4SLinus Torvalds  *  the interruptible flag is true, or a negative error
6251da177e4SLinus Torvalds  *  returned by usb_submit_urb.
6261da177e4SLinus Torvalds  */
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds static int digi_write_oob_command(struct usb_serial_port *port,
6291da177e4SLinus Torvalds 	unsigned char *buf, int count, int interruptible)
6301da177e4SLinus Torvalds {
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds 	int ret = 0;
6331da177e4SLinus Torvalds 	int len;
6341da177e4SLinus Torvalds 	struct usb_serial_port *oob_port = (struct usb_serial_port *)((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
6351da177e4SLinus Torvalds 	struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
6361da177e4SLinus Torvalds 	unsigned long flags = 0;
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	dbg("digi_write_oob_command: TOP: port=%d, count=%d", oob_priv->dp_port_num, count);
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
6411da177e4SLinus Torvalds 	while (count > 0) {
64250de36f7SGreg Kroah-Hartman 		while (oob_priv->dp_write_urb_in_use) {
6431da177e4SLinus Torvalds 			cond_wait_interruptible_timeout_irqrestore(
6441da177e4SLinus Torvalds 				&oob_port->write_wait, DIGI_RETRY_TIMEOUT,
6451da177e4SLinus Torvalds 				&oob_priv->dp_port_lock, flags);
646c6d61269SAlan Cox 			if (interruptible && signal_pending(current))
647c6d61269SAlan Cox 				return -EINTR;
6481da177e4SLinus Torvalds 			spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
6491da177e4SLinus Torvalds 		}
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 		/* len must be a multiple of 4, so commands are not split */
6521da177e4SLinus Torvalds 		len = min(count, oob_port->bulk_out_size);
6531da177e4SLinus Torvalds 		if (len > 4)
6541da177e4SLinus Torvalds 			len &= ~3;
6551da177e4SLinus Torvalds 		memcpy(oob_port->write_urb->transfer_buffer, buf, len);
6561da177e4SLinus Torvalds 		oob_port->write_urb->transfer_buffer_length = len;
6571da177e4SLinus Torvalds 		oob_port->write_urb->dev = port->serial->dev;
65841ad427dSAlan Cox 		ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
65941ad427dSAlan Cox 		if (ret == 0) {
6601da177e4SLinus Torvalds 			oob_priv->dp_write_urb_in_use = 1;
6611da177e4SLinus Torvalds 			count -= len;
6621da177e4SLinus Torvalds 			buf += len;
6631da177e4SLinus Torvalds 		}
6641da177e4SLinus Torvalds 	}
6651da177e4SLinus Torvalds 	spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
666c6d61269SAlan Cox 	if (ret)
667194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n",
668194343d9SGreg Kroah-Hartman 			__func__, ret);
669c6d61269SAlan Cox 	return ret;
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds }
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds /*
6751da177e4SLinus Torvalds  *  Digi Write In Band Command
6761da177e4SLinus Torvalds  *
6771da177e4SLinus Torvalds  *  Write commands on the given port.  Commands are 4
6781da177e4SLinus Torvalds  *  bytes each, multiple commands can be sent at once, and
6791da177e4SLinus Torvalds  *  no command will be split across USB packets.  If timeout
6801da177e4SLinus Torvalds  *  is non-zero, write in band command will return after
6811da177e4SLinus Torvalds  *  waiting unsuccessfully for the URB status to clear for
6821da177e4SLinus Torvalds  *  timeout ticks.  Returns 0 if successful, or a negative
6831da177e4SLinus Torvalds  *  error returned by digi_write.
6841da177e4SLinus Torvalds  */
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds static int digi_write_inb_command(struct usb_serial_port *port,
6871da177e4SLinus Torvalds 	unsigned char *buf, int count, unsigned long timeout)
6881da177e4SLinus Torvalds {
6891da177e4SLinus Torvalds 	int ret = 0;
6901da177e4SLinus Torvalds 	int len;
6911da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
6921da177e4SLinus Torvalds 	unsigned char *data = port->write_urb->transfer_buffer;
6931da177e4SLinus Torvalds 	unsigned long flags = 0;
6941da177e4SLinus Torvalds 
695c6d61269SAlan Cox 	dbg("digi_write_inb_command: TOP: port=%d, count=%d",
696c6d61269SAlan Cox 		priv->dp_port_num, count);
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	if (timeout)
6991da177e4SLinus Torvalds 		timeout += jiffies;
7001da177e4SLinus Torvalds 	else
7011da177e4SLinus Torvalds 		timeout = ULONG_MAX;
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
7041da177e4SLinus Torvalds 	while (count > 0 && ret == 0) {
70550de36f7SGreg Kroah-Hartman 		while (priv->dp_write_urb_in_use &&
70650de36f7SGreg Kroah-Hartman 		       time_before(jiffies, timeout)) {
7071da177e4SLinus Torvalds 			cond_wait_interruptible_timeout_irqrestore(
7081da177e4SLinus Torvalds 				&port->write_wait, DIGI_RETRY_TIMEOUT,
7091da177e4SLinus Torvalds 				&priv->dp_port_lock, flags);
710c6d61269SAlan Cox 			if (signal_pending(current))
711c6d61269SAlan Cox 				return -EINTR;
7121da177e4SLinus Torvalds 			spin_lock_irqsave(&priv->dp_port_lock, flags);
7131da177e4SLinus Torvalds 		}
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 		/* len must be a multiple of 4 and small enough to */
7161da177e4SLinus Torvalds 		/* guarantee the write will send buffered data first, */
7171da177e4SLinus Torvalds 		/* so commands are in order with data and not split */
7181da177e4SLinus Torvalds 		len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
7191da177e4SLinus Torvalds 		if (len > 4)
7201da177e4SLinus Torvalds 			len &= ~3;
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 		/* write any buffered data first */
7231da177e4SLinus Torvalds 		if (priv->dp_out_buf_len > 0) {
7241da177e4SLinus Torvalds 			data[0] = DIGI_CMD_SEND_DATA;
7251da177e4SLinus Torvalds 			data[1] = priv->dp_out_buf_len;
7261da177e4SLinus Torvalds 			memcpy(data + 2, priv->dp_out_buf,
7271da177e4SLinus Torvalds 				priv->dp_out_buf_len);
7281da177e4SLinus Torvalds 			memcpy(data + 2 + priv->dp_out_buf_len, buf, len);
7291da177e4SLinus Torvalds 			port->write_urb->transfer_buffer_length
7301da177e4SLinus Torvalds 				= priv->dp_out_buf_len + 2 + len;
7311da177e4SLinus Torvalds 		} else {
7321da177e4SLinus Torvalds 			memcpy(data, buf, len);
7331da177e4SLinus Torvalds 			port->write_urb->transfer_buffer_length = len;
7341da177e4SLinus Torvalds 		}
7351da177e4SLinus Torvalds 		port->write_urb->dev = port->serial->dev;
7361da177e4SLinus Torvalds 
73741ad427dSAlan Cox 		ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
73841ad427dSAlan Cox 		if (ret == 0) {
7391da177e4SLinus Torvalds 			priv->dp_write_urb_in_use = 1;
7401da177e4SLinus Torvalds 			priv->dp_out_buf_len = 0;
7411da177e4SLinus Torvalds 			count -= len;
7421da177e4SLinus Torvalds 			buf += len;
7431da177e4SLinus Torvalds 		}
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 	}
7461da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
7471da177e4SLinus Torvalds 
748c6d61269SAlan Cox 	if (ret)
749194343d9SGreg Kroah-Hartman 		dev_err(&port->dev,
750194343d9SGreg Kroah-Hartman 			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
751441b62c1SHarvey Harrison 			__func__, ret, priv->dp_port_num);
752c6d61269SAlan Cox 	return ret;
7531da177e4SLinus Torvalds }
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds /*
7571da177e4SLinus Torvalds  *  Digi Set Modem Signals
7581da177e4SLinus Torvalds  *
7591da177e4SLinus Torvalds  *  Sets or clears DTR and RTS on the port, according to the
7601da177e4SLinus Torvalds  *  modem_signals argument.  Use TIOCM_DTR and TIOCM_RTS flags
7611da177e4SLinus Torvalds  *  for the modem_signals argument.  Returns 0 if successful,
7621da177e4SLinus Torvalds  *  -EINTR if interrupted while sleeping, or a non-zero error
7631da177e4SLinus Torvalds  *  returned by usb_submit_urb.
7641da177e4SLinus Torvalds  */
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds static int digi_set_modem_signals(struct usb_serial_port *port,
7671da177e4SLinus Torvalds 	unsigned int modem_signals, int interruptible)
7681da177e4SLinus Torvalds {
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 	int ret;
7711da177e4SLinus Torvalds 	struct digi_port *port_priv = usb_get_serial_port_data(port);
7721da177e4SLinus Torvalds 	struct usb_serial_port *oob_port = (struct usb_serial_port *) ((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
7731da177e4SLinus Torvalds 	struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
7741da177e4SLinus Torvalds 	unsigned char *data = oob_port->write_urb->transfer_buffer;
7751da177e4SLinus Torvalds 	unsigned long flags = 0;
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	dbg("digi_set_modem_signals: TOP: port=%d, modem_signals=0x%x",
7791da177e4SLinus Torvalds 		port_priv->dp_port_num, modem_signals);
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
7821da177e4SLinus Torvalds 	spin_lock(&port_priv->dp_port_lock);
7831da177e4SLinus Torvalds 
78450de36f7SGreg Kroah-Hartman 	while (oob_priv->dp_write_urb_in_use) {
7851da177e4SLinus Torvalds 		spin_unlock(&port_priv->dp_port_lock);
7861da177e4SLinus Torvalds 		cond_wait_interruptible_timeout_irqrestore(
7871da177e4SLinus Torvalds 			&oob_port->write_wait, DIGI_RETRY_TIMEOUT,
7881da177e4SLinus Torvalds 			&oob_priv->dp_port_lock, flags);
789c6d61269SAlan Cox 		if (interruptible && signal_pending(current))
790c6d61269SAlan Cox 			return -EINTR;
7911da177e4SLinus Torvalds 		spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
7921da177e4SLinus Torvalds 		spin_lock(&port_priv->dp_port_lock);
7931da177e4SLinus Torvalds 	}
7941da177e4SLinus Torvalds 	data[0] = DIGI_CMD_SET_DTR_SIGNAL;
7951da177e4SLinus Torvalds 	data[1] = port_priv->dp_port_num;
79641ad427dSAlan Cox 	data[2] = (modem_signals & TIOCM_DTR) ?
79741ad427dSAlan Cox 					DIGI_DTR_ACTIVE : DIGI_DTR_INACTIVE;
7981da177e4SLinus Torvalds 	data[3] = 0;
7991da177e4SLinus Torvalds 	data[4] = DIGI_CMD_SET_RTS_SIGNAL;
8001da177e4SLinus Torvalds 	data[5] = port_priv->dp_port_num;
80141ad427dSAlan Cox 	data[6] = (modem_signals & TIOCM_RTS) ?
80241ad427dSAlan Cox 					DIGI_RTS_ACTIVE : DIGI_RTS_INACTIVE;
8031da177e4SLinus Torvalds 	data[7] = 0;
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 	oob_port->write_urb->transfer_buffer_length = 8;
8061da177e4SLinus Torvalds 	oob_port->write_urb->dev = port->serial->dev;
8071da177e4SLinus Torvalds 
80841ad427dSAlan Cox 	ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
80941ad427dSAlan Cox 	if (ret == 0) {
8101da177e4SLinus Torvalds 		oob_priv->dp_write_urb_in_use = 1;
8111da177e4SLinus Torvalds 		port_priv->dp_modem_signals =
8121da177e4SLinus Torvalds 			(port_priv->dp_modem_signals&~(TIOCM_DTR|TIOCM_RTS))
8131da177e4SLinus Torvalds 			| (modem_signals&(TIOCM_DTR|TIOCM_RTS));
8141da177e4SLinus Torvalds 	}
8151da177e4SLinus Torvalds 	spin_unlock(&port_priv->dp_port_lock);
8161da177e4SLinus Torvalds 	spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
817c6d61269SAlan Cox 	if (ret)
818194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n",
819194343d9SGreg Kroah-Hartman 			__func__, ret);
820c6d61269SAlan Cox 	return ret;
8211da177e4SLinus Torvalds }
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds /*
8241da177e4SLinus Torvalds  *  Digi Transmit Idle
8251da177e4SLinus Torvalds  *
8261da177e4SLinus Torvalds  *  Digi transmit idle waits, up to timeout ticks, for the transmitter
8271da177e4SLinus Torvalds  *  to go idle.  It returns 0 if successful or a negative error.
8281da177e4SLinus Torvalds  *
8291da177e4SLinus Torvalds  *  There are race conditions here if more than one process is calling
8301da177e4SLinus Torvalds  *  digi_transmit_idle on the same port at the same time.  However, this
8311da177e4SLinus Torvalds  *  is only called from close, and only one process can be in close on a
8321da177e4SLinus Torvalds  *  port at a time, so its ok.
8331da177e4SLinus Torvalds  */
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds static int digi_transmit_idle(struct usb_serial_port *port,
8361da177e4SLinus Torvalds 	unsigned long timeout)
8371da177e4SLinus Torvalds {
8381da177e4SLinus Torvalds 	int ret;
8391da177e4SLinus Torvalds 	unsigned char buf[2];
8401da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
8411da177e4SLinus Torvalds 	unsigned long flags = 0;
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
8441da177e4SLinus Torvalds 	priv->dp_transmit_idle = 0;
8451da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	buf[0] = DIGI_CMD_TRANSMIT_IDLE;
8481da177e4SLinus Torvalds 	buf[1] = 0;
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 	timeout += jiffies;
8511da177e4SLinus Torvalds 
85241ad427dSAlan Cox 	ret = digi_write_inb_command(port, buf, 2, timeout - jiffies);
85341ad427dSAlan Cox 	if (ret != 0)
854c6d61269SAlan Cox 		return ret;
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 	while (time_before(jiffies, timeout) && !priv->dp_transmit_idle) {
8591da177e4SLinus Torvalds 		cond_wait_interruptible_timeout_irqrestore(
8601da177e4SLinus Torvalds 			&priv->dp_transmit_idle_wait, DIGI_RETRY_TIMEOUT,
8611da177e4SLinus Torvalds 			&priv->dp_port_lock, flags);
862c6d61269SAlan Cox 		if (signal_pending(current))
863c6d61269SAlan Cox 			return -EINTR;
8641da177e4SLinus Torvalds 		spin_lock_irqsave(&priv->dp_port_lock, flags);
8651da177e4SLinus Torvalds 	}
8661da177e4SLinus Torvalds 	priv->dp_transmit_idle = 0;
8671da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
868c6d61269SAlan Cox 	return 0;
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds }
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 
87395da310eSAlan Cox static void digi_rx_throttle(struct tty_struct *tty)
8741da177e4SLinus Torvalds {
8751da177e4SLinus Torvalds 	unsigned long flags;
87695da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
8771da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	dbg("digi_rx_throttle: TOP: port=%d", priv->dp_port_num);
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	/* stop receiving characters by not resubmitting the read urb */
8831da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
8841da177e4SLinus Torvalds 	priv->dp_throttled = 1;
8851da177e4SLinus Torvalds 	priv->dp_throttle_restart = 0;
8861da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
8871da177e4SLinus Torvalds }
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 
89095da310eSAlan Cox static void digi_rx_unthrottle(struct tty_struct *tty)
8911da177e4SLinus Torvalds {
8921da177e4SLinus Torvalds 	int ret = 0;
8931da177e4SLinus Torvalds 	unsigned long flags;
89495da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
8951da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 	dbg("digi_rx_unthrottle: TOP: port=%d", priv->dp_port_num);
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	/* restart read chain */
9021da177e4SLinus Torvalds 	if (priv->dp_throttle_restart) {
9031da177e4SLinus Torvalds 		port->read_urb->dev = port->serial->dev;
9041da177e4SLinus Torvalds 		ret = usb_submit_urb(port->read_urb, GFP_ATOMIC);
9051da177e4SLinus Torvalds 	}
9061da177e4SLinus Torvalds 
907ba6b702fSJohan Hovold 	/* turn throttle off */
908ba6b702fSJohan Hovold 	priv->dp_throttled = 0;
909ba6b702fSJohan Hovold 	priv->dp_throttle_restart = 0;
910ba6b702fSJohan Hovold 
9111da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
9121da177e4SLinus Torvalds 
913c6d61269SAlan Cox 	if (ret)
914194343d9SGreg Kroah-Hartman 		dev_err(&port->dev,
915194343d9SGreg Kroah-Hartman 			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
916441b62c1SHarvey Harrison 			__func__, ret, priv->dp_port_num);
9171da177e4SLinus Torvalds }
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 
92095da310eSAlan Cox static void digi_set_termios(struct tty_struct *tty,
92195da310eSAlan Cox 		struct usb_serial_port *port, struct ktermios *old_termios)
9221da177e4SLinus Torvalds {
9231da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
924c6d61269SAlan Cox 	unsigned int iflag = tty->termios->c_iflag;
925c6d61269SAlan Cox 	unsigned int cflag = tty->termios->c_cflag;
9261da177e4SLinus Torvalds 	unsigned int old_iflag = old_termios->c_iflag;
9271da177e4SLinus Torvalds 	unsigned int old_cflag = old_termios->c_cflag;
9281da177e4SLinus Torvalds 	unsigned char buf[32];
9291da177e4SLinus Torvalds 	unsigned int modem_signals;
9301da177e4SLinus Torvalds 	int arg, ret;
9311da177e4SLinus Torvalds 	int i = 0;
932c6d61269SAlan Cox 	speed_t baud;
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds 	dbg("digi_set_termios: TOP: port=%d, iflag=0x%x, old_iflag=0x%x, cflag=0x%x, old_cflag=0x%x", priv->dp_port_num, iflag, old_iflag, cflag, old_cflag);
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	/* set baud rate */
93741ad427dSAlan Cox 	baud = tty_get_baud_rate(tty);
93841ad427dSAlan Cox 	if (baud != tty_termios_baud_rate(old_termios)) {
9391da177e4SLinus Torvalds 		arg = -1;
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 		/* reassert DTR and (maybe) RTS on transition from B0 */
9421da177e4SLinus Torvalds 		if ((old_cflag&CBAUD) == B0) {
9431da177e4SLinus Torvalds 			/* don't set RTS if using hardware flow control */
9441da177e4SLinus Torvalds 			/* and throttling input */
9451da177e4SLinus Torvalds 			modem_signals = TIOCM_DTR;
946c6d61269SAlan Cox 			if (!(tty->termios->c_cflag & CRTSCTS) ||
947c6d61269SAlan Cox 			    !test_bit(TTY_THROTTLED, &tty->flags))
9481da177e4SLinus Torvalds 				modem_signals |= TIOCM_RTS;
9491da177e4SLinus Torvalds 			digi_set_modem_signals(port, modem_signals, 1);
9501da177e4SLinus Torvalds 		}
951c6d61269SAlan Cox 		switch (baud) {
9521da177e4SLinus Torvalds 		/* drop DTR and RTS on transition to B0 */
953c6d61269SAlan Cox 		case 0: digi_set_modem_signals(port, 0, 1); break;
954c6d61269SAlan Cox 		case 50: arg = DIGI_BAUD_50; break;
955c6d61269SAlan Cox 		case 75: arg = DIGI_BAUD_75; break;
956c6d61269SAlan Cox 		case 110: arg = DIGI_BAUD_110; break;
957c6d61269SAlan Cox 		case 150: arg = DIGI_BAUD_150; break;
958c6d61269SAlan Cox 		case 200: arg = DIGI_BAUD_200; break;
959c6d61269SAlan Cox 		case 300: arg = DIGI_BAUD_300; break;
960c6d61269SAlan Cox 		case 600: arg = DIGI_BAUD_600; break;
961c6d61269SAlan Cox 		case 1200: arg = DIGI_BAUD_1200; break;
962c6d61269SAlan Cox 		case 1800: arg = DIGI_BAUD_1800; break;
963c6d61269SAlan Cox 		case 2400: arg = DIGI_BAUD_2400; break;
964c6d61269SAlan Cox 		case 4800: arg = DIGI_BAUD_4800; break;
965c6d61269SAlan Cox 		case 9600: arg = DIGI_BAUD_9600; break;
966c6d61269SAlan Cox 		case 19200: arg = DIGI_BAUD_19200; break;
967c6d61269SAlan Cox 		case 38400: arg = DIGI_BAUD_38400; break;
968c6d61269SAlan Cox 		case 57600: arg = DIGI_BAUD_57600; break;
969c6d61269SAlan Cox 		case 115200: arg = DIGI_BAUD_115200; break;
970c6d61269SAlan Cox 		case 230400: arg = DIGI_BAUD_230400; break;
971c6d61269SAlan Cox 		case 460800: arg = DIGI_BAUD_460800; break;
9721da177e4SLinus Torvalds 		default:
973c6d61269SAlan Cox 			arg = DIGI_BAUD_9600;
974c6d61269SAlan Cox 			baud = 9600;
9751da177e4SLinus Torvalds 			break;
9761da177e4SLinus Torvalds 		}
9771da177e4SLinus Torvalds 		if (arg != -1) {
9781da177e4SLinus Torvalds 			buf[i++] = DIGI_CMD_SET_BAUD_RATE;
9791da177e4SLinus Torvalds 			buf[i++] = priv->dp_port_num;
9801da177e4SLinus Torvalds 			buf[i++] = arg;
9811da177e4SLinus Torvalds 			buf[i++] = 0;
9821da177e4SLinus Torvalds 		}
9831da177e4SLinus Torvalds 	}
9841da177e4SLinus Torvalds 	/* set parity */
9857fa36a99SAlan Cox 	tty->termios->c_cflag &= ~CMSPAR;
9867fa36a99SAlan Cox 
9871da177e4SLinus Torvalds 	if ((cflag&(PARENB|PARODD)) != (old_cflag&(PARENB|PARODD))) {
988c6d61269SAlan Cox 		if (cflag&PARENB) {
989c6d61269SAlan Cox 			if (cflag&PARODD)
9901da177e4SLinus Torvalds 				arg = DIGI_PARITY_ODD;
9911da177e4SLinus Torvalds 			else
9921da177e4SLinus Torvalds 				arg = DIGI_PARITY_EVEN;
9931da177e4SLinus Torvalds 		} else {
9941da177e4SLinus Torvalds 			arg = DIGI_PARITY_NONE;
9951da177e4SLinus Torvalds 		}
9961da177e4SLinus Torvalds 		buf[i++] = DIGI_CMD_SET_PARITY;
9971da177e4SLinus Torvalds 		buf[i++] = priv->dp_port_num;
9981da177e4SLinus Torvalds 		buf[i++] = arg;
9991da177e4SLinus Torvalds 		buf[i++] = 0;
10001da177e4SLinus Torvalds 	}
10011da177e4SLinus Torvalds 	/* set word size */
10021da177e4SLinus Torvalds 	if ((cflag&CSIZE) != (old_cflag&CSIZE)) {
10031da177e4SLinus Torvalds 		arg = -1;
1004c6d61269SAlan Cox 		switch (cflag&CSIZE) {
10051da177e4SLinus Torvalds 		case CS5: arg = DIGI_WORD_SIZE_5; break;
10061da177e4SLinus Torvalds 		case CS6: arg = DIGI_WORD_SIZE_6; break;
10071da177e4SLinus Torvalds 		case CS7: arg = DIGI_WORD_SIZE_7; break;
10081da177e4SLinus Torvalds 		case CS8: arg = DIGI_WORD_SIZE_8; break;
10091da177e4SLinus Torvalds 		default:
10101da177e4SLinus Torvalds 			dbg("digi_set_termios: can't handle word size %d",
10111da177e4SLinus Torvalds 				(cflag&CSIZE));
10121da177e4SLinus Torvalds 			break;
10131da177e4SLinus Torvalds 		}
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds 		if (arg != -1) {
10161da177e4SLinus Torvalds 			buf[i++] = DIGI_CMD_SET_WORD_SIZE;
10171da177e4SLinus Torvalds 			buf[i++] = priv->dp_port_num;
10181da177e4SLinus Torvalds 			buf[i++] = arg;
10191da177e4SLinus Torvalds 			buf[i++] = 0;
10201da177e4SLinus Torvalds 		}
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	}
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 	/* set stop bits */
10251da177e4SLinus Torvalds 	if ((cflag&CSTOPB) != (old_cflag&CSTOPB)) {
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 		if ((cflag&CSTOPB))
10281da177e4SLinus Torvalds 			arg = DIGI_STOP_BITS_2;
10291da177e4SLinus Torvalds 		else
10301da177e4SLinus Torvalds 			arg = DIGI_STOP_BITS_1;
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 		buf[i++] = DIGI_CMD_SET_STOP_BITS;
10331da177e4SLinus Torvalds 		buf[i++] = priv->dp_port_num;
10341da177e4SLinus Torvalds 		buf[i++] = arg;
10351da177e4SLinus Torvalds 		buf[i++] = 0;
10361da177e4SLinus Torvalds 
10371da177e4SLinus Torvalds 	}
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 	/* set input flow control */
10401da177e4SLinus Torvalds 	if ((iflag&IXOFF) != (old_iflag&IXOFF)
10411da177e4SLinus Torvalds 	    || (cflag&CRTSCTS) != (old_cflag&CRTSCTS)) {
10421da177e4SLinus Torvalds 		arg = 0;
1043c6d61269SAlan Cox 		if (iflag&IXOFF)
10441da177e4SLinus Torvalds 			arg |= DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
10451da177e4SLinus Torvalds 		else
10461da177e4SLinus Torvalds 			arg &= ~DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
10471da177e4SLinus Torvalds 
1048c6d61269SAlan Cox 		if (cflag&CRTSCTS) {
10491da177e4SLinus Torvalds 			arg |= DIGI_INPUT_FLOW_CONTROL_RTS;
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 			/* On USB-4 it is necessary to assert RTS prior */
10521da177e4SLinus Torvalds 			/* to selecting RTS input flow control.  */
10531da177e4SLinus Torvalds 			buf[i++] = DIGI_CMD_SET_RTS_SIGNAL;
10541da177e4SLinus Torvalds 			buf[i++] = priv->dp_port_num;
10551da177e4SLinus Torvalds 			buf[i++] = DIGI_RTS_ACTIVE;
10561da177e4SLinus Torvalds 			buf[i++] = 0;
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 		} else {
10591da177e4SLinus Torvalds 			arg &= ~DIGI_INPUT_FLOW_CONTROL_RTS;
10601da177e4SLinus Torvalds 		}
10611da177e4SLinus Torvalds 		buf[i++] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
10621da177e4SLinus Torvalds 		buf[i++] = priv->dp_port_num;
10631da177e4SLinus Torvalds 		buf[i++] = arg;
10641da177e4SLinus Torvalds 		buf[i++] = 0;
10651da177e4SLinus Torvalds 	}
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 	/* set output flow control */
10681da177e4SLinus Torvalds 	if ((iflag & IXON) != (old_iflag & IXON)
10691da177e4SLinus Torvalds 	    || (cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
10701da177e4SLinus Torvalds 		arg = 0;
1071c6d61269SAlan Cox 		if (iflag & IXON)
10721da177e4SLinus Torvalds 			arg |= DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
10731da177e4SLinus Torvalds 		else
10741da177e4SLinus Torvalds 			arg &= ~DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
10751da177e4SLinus Torvalds 
1076c6d61269SAlan Cox 		if (cflag & CRTSCTS) {
10771da177e4SLinus Torvalds 			arg |= DIGI_OUTPUT_FLOW_CONTROL_CTS;
10781da177e4SLinus Torvalds 		} else {
10791da177e4SLinus Torvalds 			arg &= ~DIGI_OUTPUT_FLOW_CONTROL_CTS;
1080c6d61269SAlan Cox 			tty->hw_stopped = 0;
10811da177e4SLinus Torvalds 		}
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds 		buf[i++] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
10841da177e4SLinus Torvalds 		buf[i++] = priv->dp_port_num;
10851da177e4SLinus Torvalds 		buf[i++] = arg;
10861da177e4SLinus Torvalds 		buf[i++] = 0;
10871da177e4SLinus Torvalds 	}
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 	/* set receive enable/disable */
10901da177e4SLinus Torvalds 	if ((cflag & CREAD) != (old_cflag & CREAD)) {
1091c6d61269SAlan Cox 		if (cflag & CREAD)
10921da177e4SLinus Torvalds 			arg = DIGI_ENABLE;
10931da177e4SLinus Torvalds 		else
10941da177e4SLinus Torvalds 			arg = DIGI_DISABLE;
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 		buf[i++] = DIGI_CMD_RECEIVE_ENABLE;
10971da177e4SLinus Torvalds 		buf[i++] = priv->dp_port_num;
10981da177e4SLinus Torvalds 		buf[i++] = arg;
10991da177e4SLinus Torvalds 		buf[i++] = 0;
11001da177e4SLinus Torvalds 	}
110141ad427dSAlan Cox 	ret = digi_write_oob_command(port, buf, i, 1);
110241ad427dSAlan Cox 	if (ret != 0)
11031da177e4SLinus Torvalds 		dbg("digi_set_termios: write oob failed, ret=%d", ret);
11047fa36a99SAlan Cox 	tty_encode_baud_rate(tty, baud, baud);
11051da177e4SLinus Torvalds }
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds 
110895da310eSAlan Cox static void digi_break_ctl(struct tty_struct *tty, int break_state)
11091da177e4SLinus Torvalds {
111095da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
11111da177e4SLinus Torvalds 	unsigned char buf[4];
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 	buf[0] = DIGI_CMD_BREAK_CONTROL;
11141da177e4SLinus Torvalds 	buf[1] = 2;				/* length */
11151da177e4SLinus Torvalds 	buf[2] = break_state ? 1 : 0;
11161da177e4SLinus Torvalds 	buf[3] = 0;				/* pad */
11171da177e4SLinus Torvalds 	digi_write_inb_command(port, buf, 4, 0);
11181da177e4SLinus Torvalds }
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds 
1121*60b33c13SAlan Cox static int digi_tiocmget(struct tty_struct *tty)
11221da177e4SLinus Torvalds {
112395da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
11241da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
11251da177e4SLinus Torvalds 	unsigned int val;
11261da177e4SLinus Torvalds 	unsigned long flags;
11271da177e4SLinus Torvalds 
1128441b62c1SHarvey Harrison 	dbg("%s: TOP: port=%d", __func__, priv->dp_port_num);
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
11311da177e4SLinus Torvalds 	val = priv->dp_modem_signals;
11321da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
11331da177e4SLinus Torvalds 	return val;
11341da177e4SLinus Torvalds }
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds 
113795da310eSAlan Cox static int digi_tiocmset(struct tty_struct *tty, struct file *file,
11381da177e4SLinus Torvalds 	unsigned int set, unsigned int clear)
11391da177e4SLinus Torvalds {
114095da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
11411da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
11421da177e4SLinus Torvalds 	unsigned int val;
11431da177e4SLinus Torvalds 	unsigned long flags;
11441da177e4SLinus Torvalds 
1145441b62c1SHarvey Harrison 	dbg("%s: TOP: port=%d", __func__, priv->dp_port_num);
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
11481da177e4SLinus Torvalds 	val = (priv->dp_modem_signals & ~clear) | set;
11491da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
11501da177e4SLinus Torvalds 	return digi_set_modem_signals(port, val, 1);
11511da177e4SLinus Torvalds }
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds 
115495da310eSAlan Cox static int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
115595da310eSAlan Cox 					const unsigned char *buf, int count)
11561da177e4SLinus Torvalds {
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 	int ret, data_len, new_len;
11591da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
11601da177e4SLinus Torvalds 	unsigned char *data = port->write_urb->transfer_buffer;
11611da177e4SLinus Torvalds 	unsigned long flags = 0;
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds 	dbg("digi_write: TOP: port=%d, count=%d, in_interrupt=%ld",
11641da177e4SLinus Torvalds 		priv->dp_port_num, count, in_interrupt());
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds 	/* copy user data (which can sleep) before getting spin lock */
11671da177e4SLinus Torvalds 	count = min(count, port->bulk_out_size-2);
11681da177e4SLinus Torvalds 	count = min(64, count);
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	/* be sure only one write proceeds at a time */
11711da177e4SLinus Torvalds 	/* there are races on the port private buffer */
11721da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	/* wait for urb status clear to submit another urb */
117550de36f7SGreg Kroah-Hartman 	if (priv->dp_write_urb_in_use) {
11761da177e4SLinus Torvalds 		/* buffer data if count is 1 (probably put_char) if possible */
11771da177e4SLinus Torvalds 		if (count == 1 && priv->dp_out_buf_len < DIGI_OUT_BUF_SIZE) {
11781da177e4SLinus Torvalds 			priv->dp_out_buf[priv->dp_out_buf_len++] = *buf;
11791da177e4SLinus Torvalds 			new_len = 1;
11801da177e4SLinus Torvalds 		} else {
11811da177e4SLinus Torvalds 			new_len = 0;
11821da177e4SLinus Torvalds 		}
11831da177e4SLinus Torvalds 		spin_unlock_irqrestore(&priv->dp_port_lock, flags);
1184c6d61269SAlan Cox 		return new_len;
11851da177e4SLinus Torvalds 	}
11861da177e4SLinus Torvalds 
11871da177e4SLinus Torvalds 	/* allow space for any buffered data and for new data, up to */
11881da177e4SLinus Torvalds 	/* transfer buffer size - 2 (for command and length bytes) */
11891da177e4SLinus Torvalds 	new_len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
11901da177e4SLinus Torvalds 	data_len = new_len + priv->dp_out_buf_len;
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	if (data_len == 0) {
11931da177e4SLinus Torvalds 		spin_unlock_irqrestore(&priv->dp_port_lock, flags);
1194c6d61269SAlan Cox 		return 0;
11951da177e4SLinus Torvalds 	}
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds 	port->write_urb->transfer_buffer_length = data_len+2;
11981da177e4SLinus Torvalds 	port->write_urb->dev = port->serial->dev;
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds 	*data++ = DIGI_CMD_SEND_DATA;
12011da177e4SLinus Torvalds 	*data++ = data_len;
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds 	/* copy in buffered data first */
12041da177e4SLinus Torvalds 	memcpy(data, priv->dp_out_buf, priv->dp_out_buf_len);
12051da177e4SLinus Torvalds 	data += priv->dp_out_buf_len;
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds 	/* copy in new data */
12081da177e4SLinus Torvalds 	memcpy(data, buf, new_len);
12091da177e4SLinus Torvalds 
121041ad427dSAlan Cox 	ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
121141ad427dSAlan Cox 	if (ret == 0) {
12121da177e4SLinus Torvalds 		priv->dp_write_urb_in_use = 1;
12131da177e4SLinus Torvalds 		ret = new_len;
12141da177e4SLinus Torvalds 		priv->dp_out_buf_len = 0;
12151da177e4SLinus Torvalds 	}
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 	/* return length of new data written, or error */
12181da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
1219c6d61269SAlan Cox 	if (ret < 0)
1220194343d9SGreg Kroah-Hartman 		dev_err(&port->dev,
1221194343d9SGreg Kroah-Hartman 			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
1222441b62c1SHarvey Harrison 			__func__, ret, priv->dp_port_num);
12231da177e4SLinus Torvalds 	dbg("digi_write: returning %d", ret);
1224c6d61269SAlan Cox 	return ret;
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds }
12271da177e4SLinus Torvalds 
12287d12e780SDavid Howells static void digi_write_bulk_callback(struct urb *urb)
12291da177e4SLinus Torvalds {
12301da177e4SLinus Torvalds 
1231cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
12321da177e4SLinus Torvalds 	struct usb_serial *serial;
12331da177e4SLinus Torvalds 	struct digi_port *priv;
12341da177e4SLinus Torvalds 	struct digi_serial *serial_priv;
12351da177e4SLinus Torvalds 	int ret = 0;
123685d75107SGreg Kroah-Hartman 	int status = urb->status;
12371da177e4SLinus Torvalds 
123850de36f7SGreg Kroah-Hartman 	dbg("digi_write_bulk_callback: TOP, status=%d", status);
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds 	/* port and serial sanity check */
12411da177e4SLinus Torvalds 	if (port == NULL || (priv = usb_get_serial_port_data(port)) == NULL) {
1242109f34e7SJulia Lawall 		pr_err("%s: port or port->private is NULL, status=%d\n",
1243441b62c1SHarvey Harrison 			__func__, status);
12441da177e4SLinus Torvalds 		return;
12451da177e4SLinus Torvalds 	}
12461da177e4SLinus Torvalds 	serial = port->serial;
12471da177e4SLinus Torvalds 	if (serial == NULL || (serial_priv = usb_get_serial_data(serial)) == NULL) {
1248194343d9SGreg Kroah-Hartman 		dev_err(&port->dev,
1249194343d9SGreg Kroah-Hartman 			"%s: serial or serial->private is NULL, status=%d\n",
1250441b62c1SHarvey Harrison 			__func__, status);
12511da177e4SLinus Torvalds 		return;
12521da177e4SLinus Torvalds 	}
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds 	/* handle oob callback */
12551da177e4SLinus Torvalds 	if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
12561da177e4SLinus Torvalds 		dbg("digi_write_bulk_callback: oob callback");
12571da177e4SLinus Torvalds 		spin_lock(&priv->dp_port_lock);
12581da177e4SLinus Torvalds 		priv->dp_write_urb_in_use = 0;
12591da177e4SLinus Torvalds 		wake_up_interruptible(&port->write_wait);
12601da177e4SLinus Torvalds 		spin_unlock(&priv->dp_port_lock);
12611da177e4SLinus Torvalds 		return;
12621da177e4SLinus Torvalds 	}
12631da177e4SLinus Torvalds 
12641f87158eSAlan Stern 	/* try to send any buffered data on this port */
12651da177e4SLinus Torvalds 	spin_lock(&priv->dp_port_lock);
12661da177e4SLinus Torvalds 	priv->dp_write_urb_in_use = 0;
12671f87158eSAlan Stern 	if (priv->dp_out_buf_len > 0) {
12681da177e4SLinus Torvalds 		*((unsigned char *)(port->write_urb->transfer_buffer))
12691da177e4SLinus Torvalds 			= (unsigned char)DIGI_CMD_SEND_DATA;
12701da177e4SLinus Torvalds 		*((unsigned char *)(port->write_urb->transfer_buffer) + 1)
12711da177e4SLinus Torvalds 			= (unsigned char)priv->dp_out_buf_len;
127241ad427dSAlan Cox 		port->write_urb->transfer_buffer_length =
127341ad427dSAlan Cox 						priv->dp_out_buf_len + 2;
12741da177e4SLinus Torvalds 		port->write_urb->dev = serial->dev;
12751da177e4SLinus Torvalds 		memcpy(port->write_urb->transfer_buffer + 2, priv->dp_out_buf,
12761da177e4SLinus Torvalds 			priv->dp_out_buf_len);
127741ad427dSAlan Cox 		ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
127841ad427dSAlan Cox 		if (ret == 0) {
12791da177e4SLinus Torvalds 			priv->dp_write_urb_in_use = 1;
12801da177e4SLinus Torvalds 			priv->dp_out_buf_len = 0;
12811da177e4SLinus Torvalds 		}
12821da177e4SLinus Torvalds 	}
12831da177e4SLinus Torvalds 	/* wake up processes sleeping on writes immediately */
12841da177e4SLinus Torvalds 	digi_wakeup_write(port);
12851da177e4SLinus Torvalds 	/* also queue up a wakeup at scheduler time, in case we */
12861da177e4SLinus Torvalds 	/* lost the race in write_chan(). */
12871da177e4SLinus Torvalds 	schedule_work(&priv->dp_wakeup_work);
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds 	spin_unlock(&priv->dp_port_lock);
12901f87158eSAlan Stern 	if (ret && ret != -EPERM)
1291194343d9SGreg Kroah-Hartman 		dev_err(&port->dev,
1292194343d9SGreg Kroah-Hartman 			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
1293441b62c1SHarvey Harrison 			__func__, ret, priv->dp_port_num);
12941da177e4SLinus Torvalds }
12951da177e4SLinus Torvalds 
129695da310eSAlan Cox static int digi_write_room(struct tty_struct *tty)
12971da177e4SLinus Torvalds {
129895da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
12991da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
130095da310eSAlan Cox 	int room;
13011da177e4SLinus Torvalds 	unsigned long flags = 0;
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
13041da177e4SLinus Torvalds 
130550de36f7SGreg Kroah-Hartman 	if (priv->dp_write_urb_in_use)
13061da177e4SLinus Torvalds 		room = 0;
13071da177e4SLinus Torvalds 	else
13081da177e4SLinus Torvalds 		room = port->bulk_out_size - 2 - priv->dp_out_buf_len;
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
13111da177e4SLinus Torvalds 	dbg("digi_write_room: port=%d, room=%d", priv->dp_port_num, room);
1312c6d61269SAlan Cox 	return room;
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds }
13151da177e4SLinus Torvalds 
131695da310eSAlan Cox static int digi_chars_in_buffer(struct tty_struct *tty)
13171da177e4SLinus Torvalds {
131895da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
13191da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
13201da177e4SLinus Torvalds 
132150de36f7SGreg Kroah-Hartman 	if (priv->dp_write_urb_in_use) {
1322c6d61269SAlan Cox 		dbg("digi_chars_in_buffer: port=%d, chars=%d",
1323c6d61269SAlan Cox 			priv->dp_port_num, port->bulk_out_size - 2);
13241da177e4SLinus Torvalds 		/* return(port->bulk_out_size - 2); */
1325c6d61269SAlan Cox 		return 256;
13261da177e4SLinus Torvalds 	} else {
1327c6d61269SAlan Cox 		dbg("digi_chars_in_buffer: port=%d, chars=%d",
1328c6d61269SAlan Cox 			priv->dp_port_num, priv->dp_out_buf_len);
1329c6d61269SAlan Cox 		return priv->dp_out_buf_len;
13301da177e4SLinus Torvalds 	}
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds }
13331da177e4SLinus Torvalds 
1334335f8514SAlan Cox static void digi_dtr_rts(struct usb_serial_port *port, int on)
1335335f8514SAlan Cox {
1336335f8514SAlan Cox 	/* Adjust DTR and RTS */
1337335f8514SAlan Cox 	digi_set_modem_signals(port, on * (TIOCM_DTR|TIOCM_RTS), 1);
1338335f8514SAlan Cox }
1339335f8514SAlan Cox 
1340a509a7e4SAlan Cox static int digi_open(struct tty_struct *tty, struct usb_serial_port *port)
13411da177e4SLinus Torvalds {
13421da177e4SLinus Torvalds 	int ret;
13431da177e4SLinus Torvalds 	unsigned char buf[32];
13441da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
1345606d099cSAlan Cox 	struct ktermios not_termios;
13461da177e4SLinus Torvalds 
13471f87158eSAlan Stern 	dbg("digi_open: TOP: port=%d", priv->dp_port_num);
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds 	/* be sure the device is started up */
13501da177e4SLinus Torvalds 	if (digi_startup_device(port->serial) != 0)
1351c6d61269SAlan Cox 		return -ENXIO;
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds 	/* read modem signals automatically whenever they change */
13541da177e4SLinus Torvalds 	buf[0] = DIGI_CMD_READ_INPUT_SIGNALS;
13551da177e4SLinus Torvalds 	buf[1] = priv->dp_port_num;
13561da177e4SLinus Torvalds 	buf[2] = DIGI_ENABLE;
13571da177e4SLinus Torvalds 	buf[3] = 0;
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds 	/* flush fifos */
13601da177e4SLinus Torvalds 	buf[4] = DIGI_CMD_IFLUSH_FIFO;
13611da177e4SLinus Torvalds 	buf[5] = priv->dp_port_num;
13621da177e4SLinus Torvalds 	buf[6] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
13631da177e4SLinus Torvalds 	buf[7] = 0;
13641da177e4SLinus Torvalds 
136541ad427dSAlan Cox 	ret = digi_write_oob_command(port, buf, 8, 1);
136641ad427dSAlan Cox 	if (ret != 0)
13671da177e4SLinus Torvalds 		dbg("digi_open: write oob failed, ret=%d", ret);
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds 	/* set termios settings */
137095da310eSAlan Cox 	if (tty) {
137195da310eSAlan Cox 		not_termios.c_cflag = ~tty->termios->c_cflag;
137295da310eSAlan Cox 		not_termios.c_iflag = ~tty->termios->c_iflag;
137395da310eSAlan Cox 		digi_set_termios(tty, port, &not_termios);
137495da310eSAlan Cox 	}
1375c6d61269SAlan Cox 	return 0;
13761da177e4SLinus Torvalds }
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds 
1379335f8514SAlan Cox static void digi_close(struct usb_serial_port *port)
13801da177e4SLinus Torvalds {
13811da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
13821da177e4SLinus Torvalds 	int ret;
13831da177e4SLinus Torvalds 	unsigned char buf[32];
13841da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
13851da177e4SLinus Torvalds 
13861f87158eSAlan Stern 	dbg("digi_close: TOP: port=%d", priv->dp_port_num);
13871da177e4SLinus Torvalds 
13880915f490SOliver Neukum 	mutex_lock(&port->serial->disc_mutex);
13891da177e4SLinus Torvalds 	/* if disconnected, just clear flags */
13900915f490SOliver Neukum 	if (port->serial->disconnected)
13911da177e4SLinus Torvalds 		goto exit;
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds 	if (port->serial->dev) {
1394335f8514SAlan Cox 		/* FIXME: Transmit idle belongs in the wait_unti_sent path */
13951da177e4SLinus Torvalds 		digi_transmit_idle(port, DIGI_CLOSE_TIMEOUT);
13961da177e4SLinus Torvalds 
13971da177e4SLinus Torvalds 		/* disable input flow control */
13981da177e4SLinus Torvalds 		buf[0] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
13991da177e4SLinus Torvalds 		buf[1] = priv->dp_port_num;
14001da177e4SLinus Torvalds 		buf[2] = DIGI_DISABLE;
14011da177e4SLinus Torvalds 		buf[3] = 0;
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds 		/* disable output flow control */
14041da177e4SLinus Torvalds 		buf[4] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
14051da177e4SLinus Torvalds 		buf[5] = priv->dp_port_num;
14061da177e4SLinus Torvalds 		buf[6] = DIGI_DISABLE;
14071da177e4SLinus Torvalds 		buf[7] = 0;
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds 		/* disable reading modem signals automatically */
14101da177e4SLinus Torvalds 		buf[8] = DIGI_CMD_READ_INPUT_SIGNALS;
14111da177e4SLinus Torvalds 		buf[9] = priv->dp_port_num;
14121da177e4SLinus Torvalds 		buf[10] = DIGI_DISABLE;
14131da177e4SLinus Torvalds 		buf[11] = 0;
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds 		/* disable receive */
14161da177e4SLinus Torvalds 		buf[12] = DIGI_CMD_RECEIVE_ENABLE;
14171da177e4SLinus Torvalds 		buf[13] = priv->dp_port_num;
14181da177e4SLinus Torvalds 		buf[14] = DIGI_DISABLE;
14191da177e4SLinus Torvalds 		buf[15] = 0;
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds 		/* flush fifos */
14221da177e4SLinus Torvalds 		buf[16] = DIGI_CMD_IFLUSH_FIFO;
14231da177e4SLinus Torvalds 		buf[17] = priv->dp_port_num;
14241da177e4SLinus Torvalds 		buf[18] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
14251da177e4SLinus Torvalds 		buf[19] = 0;
14261da177e4SLinus Torvalds 
142741ad427dSAlan Cox 		ret = digi_write_oob_command(port, buf, 20, 0);
142841ad427dSAlan Cox 		if (ret != 0)
14291da177e4SLinus Torvalds 			dbg("digi_close: write oob failed, ret=%d", ret);
14301da177e4SLinus Torvalds 
14311da177e4SLinus Torvalds 		/* wait for final commands on oob port to complete */
143241ad427dSAlan Cox 		prepare_to_wait(&priv->dp_flush_wait, &wait,
143341ad427dSAlan Cox 							TASK_INTERRUPTIBLE);
14341da177e4SLinus Torvalds 		schedule_timeout(DIGI_CLOSE_TIMEOUT);
14351da177e4SLinus Torvalds 		finish_wait(&priv->dp_flush_wait, &wait);
14361da177e4SLinus Torvalds 
14371da177e4SLinus Torvalds 		/* shutdown any outstanding bulk writes */
14381da177e4SLinus Torvalds 		usb_kill_urb(port->write_urb);
14391da177e4SLinus Torvalds 	}
14401da177e4SLinus Torvalds exit:
14410915f490SOliver Neukum 	spin_lock_irq(&priv->dp_port_lock);
14421da177e4SLinus Torvalds 	priv->dp_write_urb_in_use = 0;
14431da177e4SLinus Torvalds 	wake_up_interruptible(&priv->dp_close_wait);
14440915f490SOliver Neukum 	spin_unlock_irq(&priv->dp_port_lock);
14450915f490SOliver Neukum 	mutex_unlock(&port->serial->disc_mutex);
14461da177e4SLinus Torvalds 	dbg("digi_close: done");
14471da177e4SLinus Torvalds }
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds 
14501da177e4SLinus Torvalds /*
14511da177e4SLinus Torvalds  *  Digi Startup Device
14521da177e4SLinus Torvalds  *
14531da177e4SLinus Torvalds  *  Starts reads on all ports.  Must be called AFTER startup, with
14541da177e4SLinus Torvalds  *  urbs initialized.  Returns 0 if successful, non-zero error otherwise.
14551da177e4SLinus Torvalds  */
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds static int digi_startup_device(struct usb_serial *serial)
14581da177e4SLinus Torvalds {
14591da177e4SLinus Torvalds 	int i, ret = 0;
14601da177e4SLinus Torvalds 	struct digi_serial *serial_priv = usb_get_serial_data(serial);
14611da177e4SLinus Torvalds 	struct usb_serial_port *port;
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds 	/* be sure this happens exactly once */
14641da177e4SLinus Torvalds 	spin_lock(&serial_priv->ds_serial_lock);
14651da177e4SLinus Torvalds 	if (serial_priv->ds_device_started) {
14661da177e4SLinus Torvalds 		spin_unlock(&serial_priv->ds_serial_lock);
1467c6d61269SAlan Cox 		return 0;
14681da177e4SLinus Torvalds 	}
14691da177e4SLinus Torvalds 	serial_priv->ds_device_started = 1;
14701da177e4SLinus Torvalds 	spin_unlock(&serial_priv->ds_serial_lock);
14711da177e4SLinus Torvalds 
14721da177e4SLinus Torvalds 	/* start reading from each bulk in endpoint for the device */
14731da177e4SLinus Torvalds 	/* set USB_DISABLE_SPD flag for write bulk urbs */
14741da177e4SLinus Torvalds 	for (i = 0; i < serial->type->num_ports + 1; i++) {
14751da177e4SLinus Torvalds 		port = serial->port[i];
14761da177e4SLinus Torvalds 		port->write_urb->dev = port->serial->dev;
147741ad427dSAlan Cox 		ret = usb_submit_urb(port->read_urb, GFP_KERNEL);
147841ad427dSAlan Cox 		if (ret != 0) {
1479194343d9SGreg Kroah-Hartman 			dev_err(&port->dev,
1480194343d9SGreg Kroah-Hartman 				"%s: usb_submit_urb failed, ret=%d, port=%d\n",
1481441b62c1SHarvey Harrison 				__func__, ret, i);
14821da177e4SLinus Torvalds 			break;
14831da177e4SLinus Torvalds 		}
14841da177e4SLinus Torvalds 	}
1485c6d61269SAlan Cox 	return ret;
14861da177e4SLinus Torvalds }
14871da177e4SLinus Torvalds 
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds static int digi_startup(struct usb_serial *serial)
14901da177e4SLinus Torvalds {
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds 	int i;
14931da177e4SLinus Torvalds 	struct digi_port *priv;
14941da177e4SLinus Torvalds 	struct digi_serial *serial_priv;
14951da177e4SLinus Torvalds 
14961da177e4SLinus Torvalds 	dbg("digi_startup: TOP");
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds 	/* allocate the private data structures for all ports */
14991da177e4SLinus Torvalds 	/* number of regular ports + 1 for the out-of-band port */
15001da177e4SLinus Torvalds 	for (i = 0; i < serial->type->num_ports + 1; i++) {
15011da177e4SLinus Torvalds 		/* allocate port private structure */
1502c6d61269SAlan Cox 		priv = kmalloc(sizeof(struct digi_port), GFP_KERNEL);
1503c6d61269SAlan Cox 		if (priv == NULL) {
15041da177e4SLinus Torvalds 			while (--i >= 0)
15051da177e4SLinus Torvalds 				kfree(usb_get_serial_port_data(serial->port[i]));
1506c6d61269SAlan Cox 			return 1;			/* error */
15071da177e4SLinus Torvalds 		}
15081da177e4SLinus Torvalds 
15091da177e4SLinus Torvalds 		/* initialize port private structure */
15101da177e4SLinus Torvalds 		spin_lock_init(&priv->dp_port_lock);
15111da177e4SLinus Torvalds 		priv->dp_port_num = i;
15121da177e4SLinus Torvalds 		priv->dp_out_buf_len = 0;
15131da177e4SLinus Torvalds 		priv->dp_write_urb_in_use = 0;
15141da177e4SLinus Torvalds 		priv->dp_modem_signals = 0;
15151da177e4SLinus Torvalds 		init_waitqueue_head(&priv->dp_modem_change_wait);
15161da177e4SLinus Torvalds 		priv->dp_transmit_idle = 0;
15171da177e4SLinus Torvalds 		init_waitqueue_head(&priv->dp_transmit_idle_wait);
15181da177e4SLinus Torvalds 		priv->dp_throttled = 0;
15191da177e4SLinus Torvalds 		priv->dp_throttle_restart = 0;
15201da177e4SLinus Torvalds 		init_waitqueue_head(&priv->dp_flush_wait);
15211da177e4SLinus Torvalds 		init_waitqueue_head(&priv->dp_close_wait);
1522c4028958SDavid Howells 		INIT_WORK(&priv->dp_wakeup_work, digi_wakeup_write_lock);
1523c4028958SDavid Howells 		priv->dp_port = serial->port[i];
15241da177e4SLinus Torvalds 		/* initialize write wait queue for this port */
15251da177e4SLinus Torvalds 		init_waitqueue_head(&serial->port[i]->write_wait);
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds 		usb_set_serial_port_data(serial->port[i], priv);
15281da177e4SLinus Torvalds 	}
15291da177e4SLinus Torvalds 
15301da177e4SLinus Torvalds 	/* allocate serial private structure */
1531c6d61269SAlan Cox 	serial_priv = kmalloc(sizeof(struct digi_serial), GFP_KERNEL);
1532c6d61269SAlan Cox 	if (serial_priv == NULL) {
15331da177e4SLinus Torvalds 		for (i = 0; i < serial->type->num_ports + 1; i++)
15341da177e4SLinus Torvalds 			kfree(usb_get_serial_port_data(serial->port[i]));
1535c6d61269SAlan Cox 		return 1;			/* error */
15361da177e4SLinus Torvalds 	}
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds 	/* initialize serial private structure */
15391da177e4SLinus Torvalds 	spin_lock_init(&serial_priv->ds_serial_lock);
15401da177e4SLinus Torvalds 	serial_priv->ds_oob_port_num = serial->type->num_ports;
15411da177e4SLinus Torvalds 	serial_priv->ds_oob_port = serial->port[serial_priv->ds_oob_port_num];
15421da177e4SLinus Torvalds 	serial_priv->ds_device_started = 0;
15431da177e4SLinus Torvalds 	usb_set_serial_data(serial, serial_priv);
15441da177e4SLinus Torvalds 
1545c6d61269SAlan Cox 	return 0;
15461da177e4SLinus Torvalds }
15471da177e4SLinus Torvalds 
15481da177e4SLinus Torvalds 
1549f9c99bb8SAlan Stern static void digi_disconnect(struct usb_serial *serial)
15501da177e4SLinus Torvalds {
15511da177e4SLinus Torvalds 	int i;
1552f9c99bb8SAlan Stern 	dbg("digi_disconnect: TOP, in_interrupt()=%ld", in_interrupt());
15531da177e4SLinus Torvalds 
15541da177e4SLinus Torvalds 	/* stop reads and writes on all ports */
15551da177e4SLinus Torvalds 	for (i = 0; i < serial->type->num_ports + 1; i++) {
15561da177e4SLinus Torvalds 		usb_kill_urb(serial->port[i]->read_urb);
15571da177e4SLinus Torvalds 		usb_kill_urb(serial->port[i]->write_urb);
15581da177e4SLinus Torvalds 	}
1559f9c99bb8SAlan Stern }
1560f9c99bb8SAlan Stern 
1561f9c99bb8SAlan Stern 
1562f9c99bb8SAlan Stern static void digi_release(struct usb_serial *serial)
1563f9c99bb8SAlan Stern {
1564f9c99bb8SAlan Stern 	int i;
1565f9c99bb8SAlan Stern 	dbg("digi_release: TOP, in_interrupt()=%ld", in_interrupt());
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds 	/* free the private data structures for all ports */
15681da177e4SLinus Torvalds 	/* number of regular ports + 1 for the out-of-band port */
15691da177e4SLinus Torvalds 	for (i = 0; i < serial->type->num_ports + 1; i++)
15701da177e4SLinus Torvalds 		kfree(usb_get_serial_port_data(serial->port[i]));
15711da177e4SLinus Torvalds 	kfree(usb_get_serial_data(serial));
15721da177e4SLinus Torvalds }
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds 
15757d12e780SDavid Howells static void digi_read_bulk_callback(struct urb *urb)
15761da177e4SLinus Torvalds {
1577cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
15781da177e4SLinus Torvalds 	struct digi_port *priv;
15791da177e4SLinus Torvalds 	struct digi_serial *serial_priv;
15801da177e4SLinus Torvalds 	int ret;
158185d75107SGreg Kroah-Hartman 	int status = urb->status;
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds 	dbg("digi_read_bulk_callback: TOP");
15841da177e4SLinus Torvalds 
15851da177e4SLinus Torvalds 	/* port sanity check, do not resubmit if port is not valid */
1586194343d9SGreg Kroah-Hartman 	if (port == NULL)
1587194343d9SGreg Kroah-Hartman 		return;
1588194343d9SGreg Kroah-Hartman 	priv = usb_get_serial_port_data(port);
1589194343d9SGreg Kroah-Hartman 	if (priv == NULL) {
1590194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s: port->private is NULL, status=%d\n",
1591441b62c1SHarvey Harrison 			__func__, status);
15921da177e4SLinus Torvalds 		return;
15931da177e4SLinus Torvalds 	}
1594c6d61269SAlan Cox 	if (port->serial == NULL ||
1595c6d61269SAlan Cox 		(serial_priv = usb_get_serial_data(port->serial)) == NULL) {
1596194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s: serial is bad or serial->private "
1597194343d9SGreg Kroah-Hartman 			"is NULL, status=%d\n", __func__, status);
15981da177e4SLinus Torvalds 		return;
15991da177e4SLinus Torvalds 	}
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 	/* do not resubmit urb if it has any status error */
160285d75107SGreg Kroah-Hartman 	if (status) {
1603194343d9SGreg Kroah-Hartman 		dev_err(&port->dev,
1604194343d9SGreg Kroah-Hartman 			"%s: nonzero read bulk status: status=%d, port=%d\n",
1605441b62c1SHarvey Harrison 			__func__, status, priv->dp_port_num);
16061da177e4SLinus Torvalds 		return;
16071da177e4SLinus Torvalds 	}
16081da177e4SLinus Torvalds 
16091da177e4SLinus Torvalds 	/* handle oob or inb callback, do not resubmit if error */
16101da177e4SLinus Torvalds 	if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
16111da177e4SLinus Torvalds 		if (digi_read_oob_callback(urb) != 0)
16121da177e4SLinus Torvalds 			return;
16131da177e4SLinus Torvalds 	} else {
16141da177e4SLinus Torvalds 		if (digi_read_inb_callback(urb) != 0)
16151da177e4SLinus Torvalds 			return;
16161da177e4SLinus Torvalds 	}
16171da177e4SLinus Torvalds 
16181da177e4SLinus Torvalds 	/* continue read */
16191da177e4SLinus Torvalds 	urb->dev = port->serial->dev;
162041ad427dSAlan Cox 	ret = usb_submit_urb(urb, GFP_ATOMIC);
16211f87158eSAlan Stern 	if (ret != 0 && ret != -EPERM) {
1622194343d9SGreg Kroah-Hartman 		dev_err(&port->dev,
1623194343d9SGreg Kroah-Hartman 			"%s: failed resubmitting urb, ret=%d, port=%d\n",
1624441b62c1SHarvey Harrison 			__func__, ret, priv->dp_port_num);
16251da177e4SLinus Torvalds 	}
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds }
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds /*
16301da177e4SLinus Torvalds  *  Digi Read INB Callback
16311da177e4SLinus Torvalds  *
16321da177e4SLinus Torvalds  *  Digi Read INB Callback handles reads on the in band ports, sending
16331da177e4SLinus Torvalds  *  the data on to the tty subsystem.  When called we know port and
16341da177e4SLinus Torvalds  *  port->private are not NULL and port->serial has been validated.
16351da177e4SLinus Torvalds  *  It returns 0 if successful, 1 if successful but the port is
16361da177e4SLinus Torvalds  *  throttled, and -1 if the sanity checks failed.
16371da177e4SLinus Torvalds  */
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds static int digi_read_inb_callback(struct urb *urb)
16401da177e4SLinus Torvalds {
16411da177e4SLinus Torvalds 
1642cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
16434a90f09bSAlan Cox 	struct tty_struct *tty;
16441da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
16451da177e4SLinus Torvalds 	int opcode = ((unsigned char *)urb->transfer_buffer)[0];
16461da177e4SLinus Torvalds 	int len = ((unsigned char *)urb->transfer_buffer)[1];
164785d75107SGreg Kroah-Hartman 	int port_status = ((unsigned char *)urb->transfer_buffer)[2];
16481da177e4SLinus Torvalds 	unsigned char *data = ((unsigned char *)urb->transfer_buffer) + 3;
16491da177e4SLinus Torvalds 	int flag, throttled;
165085d75107SGreg Kroah-Hartman 	int status = urb->status;
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds 	/* do not process callbacks on closed ports */
16531da177e4SLinus Torvalds 	/* but do continue the read chain */
16541f87158eSAlan Stern 	if (urb->status == -ENOENT)
1655c6d61269SAlan Cox 		return 0;
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds 	/* short/multiple packet check */
16581da177e4SLinus Torvalds 	if (urb->actual_length != len + 2) {
1659194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s: INCOMPLETE OR MULTIPLE PACKET, "
166050de36f7SGreg Kroah-Hartman 			"status=%d, port=%d, opcode=%d, len=%d, "
1661194343d9SGreg Kroah-Hartman 			"actual_length=%d, status=%d\n", __func__, status,
1662194343d9SGreg Kroah-Hartman 			priv->dp_port_num, opcode, len, urb->actual_length,
1663194343d9SGreg Kroah-Hartman 			port_status);
1664c6d61269SAlan Cox 		return -1;
16651da177e4SLinus Torvalds 	}
16661da177e4SLinus Torvalds 
16674a90f09bSAlan Cox 	tty = tty_port_tty_get(&port->port);
16681da177e4SLinus Torvalds 	spin_lock(&priv->dp_port_lock);
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds 	/* check for throttle; if set, do not resubmit read urb */
16711da177e4SLinus Torvalds 	/* indicate the read chain needs to be restarted on unthrottle */
16721da177e4SLinus Torvalds 	throttled = priv->dp_throttled;
16731da177e4SLinus Torvalds 	if (throttled)
16741da177e4SLinus Torvalds 		priv->dp_throttle_restart = 1;
16751da177e4SLinus Torvalds 
16761da177e4SLinus Torvalds 	/* receive data */
16774287341dSAlan Cox 	if (tty && opcode == DIGI_CMD_RECEIVE_DATA) {
167885d75107SGreg Kroah-Hartman 		/* get flag from port_status */
16791da177e4SLinus Torvalds 		flag = 0;
16801da177e4SLinus Torvalds 
16811da177e4SLinus Torvalds 		/* overrun is special, not associated with a char */
1682c6d61269SAlan Cox 		if (port_status & DIGI_OVERRUN_ERROR)
16831da177e4SLinus Torvalds 			tty_insert_flip_char(tty, 0, TTY_OVERRUN);
16841da177e4SLinus Torvalds 
16851da177e4SLinus Torvalds 		/* break takes precedence over parity, */
16861da177e4SLinus Torvalds 		/* which takes precedence over framing errors */
1687c6d61269SAlan Cox 		if (port_status & DIGI_BREAK_ERROR)
16881da177e4SLinus Torvalds 			flag = TTY_BREAK;
1689c6d61269SAlan Cox 		else if (port_status & DIGI_PARITY_ERROR)
16901da177e4SLinus Torvalds 			flag = TTY_PARITY;
1691c6d61269SAlan Cox 		else if (port_status & DIGI_FRAMING_ERROR)
16921da177e4SLinus Torvalds 			flag = TTY_FRAME;
16931da177e4SLinus Torvalds 
169485d75107SGreg Kroah-Hartman 		/* data length is len-1 (one byte of len is port_status) */
16951da177e4SLinus Torvalds 		--len;
16961da177e4SLinus Torvalds 		if (len > 0) {
169770ced221SJohan Hovold 			tty_insert_flip_string_fixed_flag(tty, data, flag,
169870ced221SJohan Hovold 									len);
16991da177e4SLinus Torvalds 			tty_flip_buffer_push(tty);
17001da177e4SLinus Torvalds 		}
17011da177e4SLinus Torvalds 	}
17021da177e4SLinus Torvalds 	spin_unlock(&priv->dp_port_lock);
17034a90f09bSAlan Cox 	tty_kref_put(tty);
17041da177e4SLinus Torvalds 
1705c6d61269SAlan Cox 	if (opcode == DIGI_CMD_RECEIVE_DISABLE)
1706441b62c1SHarvey Harrison 		dbg("%s: got RECEIVE_DISABLE", __func__);
1707c6d61269SAlan Cox 	else if (opcode != DIGI_CMD_RECEIVE_DATA)
1708441b62c1SHarvey Harrison 		dbg("%s: unknown opcode: %d", __func__, opcode);
17091da177e4SLinus Torvalds 
171041ad427dSAlan Cox 	return throttled ? 1 : 0;
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds }
17131da177e4SLinus Torvalds 
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds /*
17161da177e4SLinus Torvalds  *  Digi Read OOB Callback
17171da177e4SLinus Torvalds  *
17181da177e4SLinus Torvalds  *  Digi Read OOB Callback handles reads on the out of band port.
17191da177e4SLinus Torvalds  *  When called we know port and port->private are not NULL and
17201da177e4SLinus Torvalds  *  the port->serial is valid.  It returns 0 if successful, and
17211da177e4SLinus Torvalds  *  -1 if the sanity checks failed.
17221da177e4SLinus Torvalds  */
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds static int digi_read_oob_callback(struct urb *urb)
17251da177e4SLinus Torvalds {
17261da177e4SLinus Torvalds 
1727cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
17281da177e4SLinus Torvalds 	struct usb_serial *serial = port->serial;
17294a90f09bSAlan Cox 	struct tty_struct *tty;
17301da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
17311da177e4SLinus Torvalds 	int opcode, line, status, val;
17321da177e4SLinus Torvalds 	int i;
173341ad427dSAlan Cox 	unsigned int rts;
17341da177e4SLinus Torvalds 
1735c6d61269SAlan Cox 	dbg("digi_read_oob_callback: port=%d, len=%d",
1736c6d61269SAlan Cox 			priv->dp_port_num, urb->actual_length);
17371da177e4SLinus Torvalds 
17381da177e4SLinus Torvalds 	/* handle each oob command */
17391da177e4SLinus Torvalds 	for (i = 0; i < urb->actual_length - 3;) {
17401da177e4SLinus Torvalds 		opcode = ((unsigned char *)urb->transfer_buffer)[i++];
17411da177e4SLinus Torvalds 		line = ((unsigned char *)urb->transfer_buffer)[i++];
17421da177e4SLinus Torvalds 		status = ((unsigned char *)urb->transfer_buffer)[i++];
17431da177e4SLinus Torvalds 		val = ((unsigned char *)urb->transfer_buffer)[i++];
17441da177e4SLinus Torvalds 
17451da177e4SLinus Torvalds 		dbg("digi_read_oob_callback: opcode=%d, line=%d, status=%d, val=%d",
17461da177e4SLinus Torvalds 			opcode, line, status, val);
17471da177e4SLinus Torvalds 
17481da177e4SLinus Torvalds 		if (status != 0 || line >= serial->type->num_ports)
17491da177e4SLinus Torvalds 			continue;
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds 		port = serial->port[line];
17521da177e4SLinus Torvalds 
175341ad427dSAlan Cox 		priv = usb_get_serial_port_data(port);
175441ad427dSAlan Cox 		if (priv == NULL)
17551da177e4SLinus Torvalds 			return -1;
17561da177e4SLinus Torvalds 
17574a90f09bSAlan Cox 		tty = tty_port_tty_get(&port->port);
17584287341dSAlan Cox 
175941ad427dSAlan Cox 		rts = 0;
17604287341dSAlan Cox 		if (tty)
17614a90f09bSAlan Cox 			rts = tty->termios->c_cflag & CRTSCTS;
176241ad427dSAlan Cox 
17634287341dSAlan Cox 		if (tty && opcode == DIGI_CMD_READ_INPUT_SIGNALS) {
17641da177e4SLinus Torvalds 			spin_lock(&priv->dp_port_lock);
17651da177e4SLinus Torvalds 			/* convert from digi flags to termiox flags */
17661da177e4SLinus Torvalds 			if (val & DIGI_READ_INPUT_SIGNALS_CTS) {
17671da177e4SLinus Torvalds 				priv->dp_modem_signals |= TIOCM_CTS;
17681da177e4SLinus Torvalds 				/* port must be open to use tty struct */
176941ad427dSAlan Cox 				if (rts) {
17704a90f09bSAlan Cox 					tty->hw_stopped = 0;
17711da177e4SLinus Torvalds 					digi_wakeup_write(port);
17721da177e4SLinus Torvalds 				}
17731da177e4SLinus Torvalds 			} else {
17741da177e4SLinus Torvalds 				priv->dp_modem_signals &= ~TIOCM_CTS;
17751da177e4SLinus Torvalds 				/* port must be open to use tty struct */
177641ad427dSAlan Cox 				if (rts)
17774a90f09bSAlan Cox 					tty->hw_stopped = 1;
17781da177e4SLinus Torvalds 			}
17791da177e4SLinus Torvalds 			if (val & DIGI_READ_INPUT_SIGNALS_DSR)
17801da177e4SLinus Torvalds 				priv->dp_modem_signals |= TIOCM_DSR;
17811da177e4SLinus Torvalds 			else
17821da177e4SLinus Torvalds 				priv->dp_modem_signals &= ~TIOCM_DSR;
17831da177e4SLinus Torvalds 			if (val & DIGI_READ_INPUT_SIGNALS_RI)
17841da177e4SLinus Torvalds 				priv->dp_modem_signals |= TIOCM_RI;
17851da177e4SLinus Torvalds 			else
17861da177e4SLinus Torvalds 				priv->dp_modem_signals &= ~TIOCM_RI;
17871da177e4SLinus Torvalds 			if (val & DIGI_READ_INPUT_SIGNALS_DCD)
17881da177e4SLinus Torvalds 				priv->dp_modem_signals |= TIOCM_CD;
17891da177e4SLinus Torvalds 			else
17901da177e4SLinus Torvalds 				priv->dp_modem_signals &= ~TIOCM_CD;
17911da177e4SLinus Torvalds 
17921da177e4SLinus Torvalds 			wake_up_interruptible(&priv->dp_modem_change_wait);
17931da177e4SLinus Torvalds 			spin_unlock(&priv->dp_port_lock);
17941da177e4SLinus Torvalds 		} else if (opcode == DIGI_CMD_TRANSMIT_IDLE) {
17951da177e4SLinus Torvalds 			spin_lock(&priv->dp_port_lock);
17961da177e4SLinus Torvalds 			priv->dp_transmit_idle = 1;
17971da177e4SLinus Torvalds 			wake_up_interruptible(&priv->dp_transmit_idle_wait);
17981da177e4SLinus Torvalds 			spin_unlock(&priv->dp_port_lock);
17991da177e4SLinus Torvalds 		} else if (opcode == DIGI_CMD_IFLUSH_FIFO) {
180045f23f18SNishanth Aravamudan 			wake_up_interruptible(&priv->dp_flush_wait);
1801c6d61269SAlan Cox 		}
18024a90f09bSAlan Cox 		tty_kref_put(tty);
1803c6d61269SAlan Cox 	}
1804c6d61269SAlan Cox 	return 0;
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds }
18071da177e4SLinus Torvalds 
18081da177e4SLinus Torvalds static int __init digi_init(void)
18091da177e4SLinus Torvalds {
18101da177e4SLinus Torvalds 	int retval;
18111da177e4SLinus Torvalds 	retval = usb_serial_register(&digi_acceleport_2_device);
18121da177e4SLinus Torvalds 	if (retval)
18131da177e4SLinus Torvalds 		goto failed_acceleport_2_device;
18141da177e4SLinus Torvalds 	retval = usb_serial_register(&digi_acceleport_4_device);
18151da177e4SLinus Torvalds 	if (retval)
18161da177e4SLinus Torvalds 		goto failed_acceleport_4_device;
18171da177e4SLinus Torvalds 	retval = usb_register(&digi_driver);
18181da177e4SLinus Torvalds 	if (retval)
18191da177e4SLinus Torvalds 		goto failed_usb_register;
1820c197a8dbSGreg Kroah-Hartman 	printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1821c197a8dbSGreg Kroah-Hartman 	       DRIVER_DESC "\n");
18221da177e4SLinus Torvalds 	return 0;
18231da177e4SLinus Torvalds failed_usb_register:
18241da177e4SLinus Torvalds 	usb_serial_deregister(&digi_acceleport_4_device);
18251da177e4SLinus Torvalds failed_acceleport_4_device:
18261da177e4SLinus Torvalds 	usb_serial_deregister(&digi_acceleport_2_device);
18271da177e4SLinus Torvalds failed_acceleport_2_device:
18281da177e4SLinus Torvalds 	return retval;
18291da177e4SLinus Torvalds }
18301da177e4SLinus Torvalds 
18311da177e4SLinus Torvalds static void __exit digi_exit (void)
18321da177e4SLinus Torvalds {
18331da177e4SLinus Torvalds 	usb_deregister(&digi_driver);
18341da177e4SLinus Torvalds 	usb_serial_deregister(&digi_acceleport_2_device);
18351da177e4SLinus Torvalds 	usb_serial_deregister(&digi_acceleport_4_device);
18361da177e4SLinus Torvalds }
18371da177e4SLinus Torvalds 
18381da177e4SLinus Torvalds 
18391da177e4SLinus Torvalds module_init(digi_init);
18401da177e4SLinus Torvalds module_exit(digi_exit);
18411da177e4SLinus Torvalds 
18421da177e4SLinus Torvalds 
18431da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
18441da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
18451da177e4SLinus Torvalds MODULE_LICENSE("GPL");
18461da177e4SLinus Torvalds 
18471da177e4SLinus Torvalds module_param(debug, bool, S_IRUGO | S_IWUSR);
18481da177e4SLinus Torvalds MODULE_PARM_DESC(debug, "Debug enabled or not");
1849