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); 44895da310eSAlan Cox static int digi_tiocmget(struct tty_struct *tty, struct file *file); 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); 45695da310eSAlan Cox static int digi_open(struct tty_struct *tty, struct usb_serial_port *port, 45795da310eSAlan Cox struct file *filp); 458335f8514SAlan Cox static void digi_close(struct usb_serial_port *port); 459335f8514SAlan Cox static int digi_carrier_raised(struct usb_serial_port *port); 460335f8514SAlan Cox static void digi_dtr_rts(struct usb_serial_port *port, int on); 4611da177e4SLinus Torvalds static int digi_startup_device(struct usb_serial *serial); 4621da177e4SLinus Torvalds static int digi_startup(struct usb_serial *serial); 463*f9c99bb8SAlan Stern static void digi_disconnect(struct usb_serial *serial); 464*f9c99bb8SAlan Stern static void digi_release(struct usb_serial *serial); 4657d12e780SDavid Howells static void digi_read_bulk_callback(struct urb *urb); 4661da177e4SLinus Torvalds static int digi_read_inb_callback(struct urb *urb); 4671da177e4SLinus Torvalds static int digi_read_oob_callback(struct urb *urb); 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds /* Statics */ 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds static int debug; 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds static struct usb_device_id id_table_combined [] = { 4751da177e4SLinus Torvalds { USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) }, 4761da177e4SLinus Torvalds { USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) }, 4771da177e4SLinus Torvalds { } /* Terminating entry */ 4781da177e4SLinus Torvalds }; 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds static struct usb_device_id id_table_2 [] = { 4811da177e4SLinus Torvalds { USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) }, 4821da177e4SLinus Torvalds { } /* Terminating entry */ 4831da177e4SLinus Torvalds }; 4841da177e4SLinus Torvalds 4851da177e4SLinus Torvalds static struct usb_device_id id_table_4 [] = { 4861da177e4SLinus Torvalds { USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) }, 4871da177e4SLinus Torvalds { } /* Terminating entry */ 4881da177e4SLinus Torvalds }; 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds MODULE_DEVICE_TABLE(usb, id_table_combined); 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds static struct usb_driver digi_driver = { 4931da177e4SLinus Torvalds .name = "digi_acceleport", 4941da177e4SLinus Torvalds .probe = usb_serial_probe, 4951da177e4SLinus Torvalds .disconnect = usb_serial_disconnect, 4961da177e4SLinus Torvalds .id_table = id_table_combined, 497ba9dc657SGreg Kroah-Hartman .no_dynamic_id = 1, 4981da177e4SLinus Torvalds }; 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds /* device info needed for the Digi serial converter */ 5021da177e4SLinus Torvalds 503ea65370dSGreg Kroah-Hartman static struct usb_serial_driver digi_acceleport_2_device = { 50418fcac35SGreg Kroah-Hartman .driver = { 5051da177e4SLinus Torvalds .owner = THIS_MODULE, 506269bda1cSGreg Kroah-Hartman .name = "digi_2", 50718fcac35SGreg Kroah-Hartman }, 508269bda1cSGreg Kroah-Hartman .description = "Digi 2 port USB adapter", 509d9b1b787SJohannes Hölzl .usb_driver = &digi_driver, 5101da177e4SLinus Torvalds .id_table = id_table_2, 5111da177e4SLinus Torvalds .num_ports = 3, 5121da177e4SLinus Torvalds .open = digi_open, 5131da177e4SLinus Torvalds .close = digi_close, 514335f8514SAlan Cox .dtr_rts = digi_dtr_rts, 515335f8514SAlan Cox .carrier_raised = digi_carrier_raised, 5161da177e4SLinus Torvalds .write = digi_write, 5171da177e4SLinus Torvalds .write_room = digi_write_room, 5181da177e4SLinus Torvalds .write_bulk_callback = digi_write_bulk_callback, 5191da177e4SLinus Torvalds .read_bulk_callback = digi_read_bulk_callback, 5201da177e4SLinus Torvalds .chars_in_buffer = digi_chars_in_buffer, 5211da177e4SLinus Torvalds .throttle = digi_rx_throttle, 5221da177e4SLinus Torvalds .unthrottle = digi_rx_unthrottle, 5231da177e4SLinus Torvalds .set_termios = digi_set_termios, 5241da177e4SLinus Torvalds .break_ctl = digi_break_ctl, 5251da177e4SLinus Torvalds .tiocmget = digi_tiocmget, 5261da177e4SLinus Torvalds .tiocmset = digi_tiocmset, 5271da177e4SLinus Torvalds .attach = digi_startup, 528*f9c99bb8SAlan Stern .disconnect = digi_disconnect, 529*f9c99bb8SAlan Stern .release = digi_release, 5301da177e4SLinus Torvalds }; 5311da177e4SLinus Torvalds 532ea65370dSGreg Kroah-Hartman static struct usb_serial_driver digi_acceleport_4_device = { 53318fcac35SGreg Kroah-Hartman .driver = { 5341da177e4SLinus Torvalds .owner = THIS_MODULE, 535269bda1cSGreg Kroah-Hartman .name = "digi_4", 53618fcac35SGreg Kroah-Hartman }, 537269bda1cSGreg Kroah-Hartman .description = "Digi 4 port USB adapter", 538d9b1b787SJohannes Hölzl .usb_driver = &digi_driver, 5391da177e4SLinus Torvalds .id_table = id_table_4, 5401da177e4SLinus Torvalds .num_ports = 4, 5411da177e4SLinus Torvalds .open = digi_open, 5421da177e4SLinus Torvalds .close = digi_close, 5431da177e4SLinus Torvalds .write = digi_write, 5441da177e4SLinus Torvalds .write_room = digi_write_room, 5451da177e4SLinus Torvalds .write_bulk_callback = digi_write_bulk_callback, 5461da177e4SLinus Torvalds .read_bulk_callback = digi_read_bulk_callback, 5471da177e4SLinus Torvalds .chars_in_buffer = digi_chars_in_buffer, 5481da177e4SLinus Torvalds .throttle = digi_rx_throttle, 5491da177e4SLinus Torvalds .unthrottle = digi_rx_unthrottle, 5501da177e4SLinus Torvalds .set_termios = digi_set_termios, 5511da177e4SLinus Torvalds .break_ctl = digi_break_ctl, 5521da177e4SLinus Torvalds .tiocmget = digi_tiocmget, 5531da177e4SLinus Torvalds .tiocmset = digi_tiocmset, 5541da177e4SLinus Torvalds .attach = digi_startup, 555*f9c99bb8SAlan Stern .disconnect = digi_disconnect, 556*f9c99bb8SAlan Stern .release = digi_release, 5571da177e4SLinus Torvalds }; 5581da177e4SLinus Torvalds 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds /* Functions */ 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds /* 5631da177e4SLinus Torvalds * Cond Wait Interruptible Timeout Irqrestore 5641da177e4SLinus Torvalds * 5651da177e4SLinus Torvalds * Do spin_unlock_irqrestore and interruptible_sleep_on_timeout 5661da177e4SLinus Torvalds * so that wake ups are not lost if they occur between the unlock 5671da177e4SLinus Torvalds * and the sleep. In other words, spin_unlock_irqrestore and 5681da177e4SLinus Torvalds * interruptible_sleep_on_timeout are "atomic" with respect to 5691da177e4SLinus Torvalds * wake ups. This is used to implement condition variables. 57045f23f18SNishanth Aravamudan * 57145f23f18SNishanth Aravamudan * interruptible_sleep_on_timeout is deprecated and has been replaced 57245f23f18SNishanth Aravamudan * with the equivalent code. 5731da177e4SLinus Torvalds */ 5741da177e4SLinus Torvalds 575c6d61269SAlan Cox static long cond_wait_interruptible_timeout_irqrestore( 5761da177e4SLinus Torvalds wait_queue_head_t *q, long timeout, 5771da177e4SLinus Torvalds spinlock_t *lock, unsigned long flags) 5785a33956aSHarvey Harrison __releases(lock) 5791da177e4SLinus Torvalds { 5801da177e4SLinus Torvalds DEFINE_WAIT(wait); 5811da177e4SLinus Torvalds 58245f23f18SNishanth Aravamudan prepare_to_wait(q, &wait, TASK_INTERRUPTIBLE); 5831da177e4SLinus Torvalds spin_unlock_irqrestore(lock, flags); 5841da177e4SLinus Torvalds timeout = schedule_timeout(timeout); 5851da177e4SLinus Torvalds finish_wait(q, &wait); 5861da177e4SLinus Torvalds 5871da177e4SLinus Torvalds return timeout; 5881da177e4SLinus Torvalds } 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds /* 5921da177e4SLinus Torvalds * Digi Wakeup Write 5931da177e4SLinus Torvalds * 5941da177e4SLinus Torvalds * Wake up port, line discipline, and tty processes sleeping 5951da177e4SLinus Torvalds * on writes. 5961da177e4SLinus Torvalds */ 5971da177e4SLinus Torvalds 598c4028958SDavid Howells static void digi_wakeup_write_lock(struct work_struct *work) 5991da177e4SLinus Torvalds { 60041ad427dSAlan Cox struct digi_port *priv = 60141ad427dSAlan Cox container_of(work, struct digi_port, dp_wakeup_work); 602c4028958SDavid Howells struct usb_serial_port *port = priv->dp_port; 6031da177e4SLinus Torvalds unsigned long flags; 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds spin_lock_irqsave(&priv->dp_port_lock, flags); 6061da177e4SLinus Torvalds digi_wakeup_write(port); 6071da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->dp_port_lock, flags); 6081da177e4SLinus Torvalds } 6091da177e4SLinus Torvalds 6101da177e4SLinus Torvalds static void digi_wakeup_write(struct usb_serial_port *port) 6111da177e4SLinus Torvalds { 6124a90f09bSAlan Cox struct tty_struct *tty = tty_port_tty_get(&port->port); 6134a90f09bSAlan Cox tty_wakeup(tty); 6144a90f09bSAlan Cox tty_kref_put(tty); 6151da177e4SLinus Torvalds } 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds 6181da177e4SLinus Torvalds /* 6191da177e4SLinus Torvalds * Digi Write OOB Command 6201da177e4SLinus Torvalds * 6211da177e4SLinus Torvalds * Write commands on the out of band port. Commands are 4 6221da177e4SLinus Torvalds * bytes each, multiple commands can be sent at once, and 6231da177e4SLinus Torvalds * no command will be split across USB packets. Returns 0 6241da177e4SLinus Torvalds * if successful, -EINTR if interrupted while sleeping and 6251da177e4SLinus Torvalds * the interruptible flag is true, or a negative error 6261da177e4SLinus Torvalds * returned by usb_submit_urb. 6271da177e4SLinus Torvalds */ 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds static int digi_write_oob_command(struct usb_serial_port *port, 6301da177e4SLinus Torvalds unsigned char *buf, int count, int interruptible) 6311da177e4SLinus Torvalds { 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds int ret = 0; 6341da177e4SLinus Torvalds int len; 6351da177e4SLinus Torvalds struct usb_serial_port *oob_port = (struct usb_serial_port *)((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port; 6361da177e4SLinus Torvalds struct digi_port *oob_priv = usb_get_serial_port_data(oob_port); 6371da177e4SLinus Torvalds unsigned long flags = 0; 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds dbg("digi_write_oob_command: TOP: port=%d, count=%d", oob_priv->dp_port_num, count); 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds spin_lock_irqsave(&oob_priv->dp_port_lock, flags); 6421da177e4SLinus Torvalds while (count > 0) { 64350de36f7SGreg Kroah-Hartman while (oob_priv->dp_write_urb_in_use) { 6441da177e4SLinus Torvalds cond_wait_interruptible_timeout_irqrestore( 6451da177e4SLinus Torvalds &oob_port->write_wait, DIGI_RETRY_TIMEOUT, 6461da177e4SLinus Torvalds &oob_priv->dp_port_lock, flags); 647c6d61269SAlan Cox if (interruptible && signal_pending(current)) 648c6d61269SAlan Cox return -EINTR; 6491da177e4SLinus Torvalds spin_lock_irqsave(&oob_priv->dp_port_lock, flags); 6501da177e4SLinus Torvalds } 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds /* len must be a multiple of 4, so commands are not split */ 6531da177e4SLinus Torvalds len = min(count, oob_port->bulk_out_size); 6541da177e4SLinus Torvalds if (len > 4) 6551da177e4SLinus Torvalds len &= ~3; 6561da177e4SLinus Torvalds memcpy(oob_port->write_urb->transfer_buffer, buf, len); 6571da177e4SLinus Torvalds oob_port->write_urb->transfer_buffer_length = len; 6581da177e4SLinus Torvalds oob_port->write_urb->dev = port->serial->dev; 65941ad427dSAlan Cox ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC); 66041ad427dSAlan Cox if (ret == 0) { 6611da177e4SLinus Torvalds oob_priv->dp_write_urb_in_use = 1; 6621da177e4SLinus Torvalds count -= len; 6631da177e4SLinus Torvalds buf += len; 6641da177e4SLinus Torvalds } 6651da177e4SLinus Torvalds } 6661da177e4SLinus Torvalds spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags); 667c6d61269SAlan Cox if (ret) 668194343d9SGreg Kroah-Hartman dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n", 669194343d9SGreg Kroah-Hartman __func__, ret); 670c6d61269SAlan Cox return ret; 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds } 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds /* 6761da177e4SLinus Torvalds * Digi Write In Band Command 6771da177e4SLinus Torvalds * 6781da177e4SLinus Torvalds * Write commands on the given port. Commands are 4 6791da177e4SLinus Torvalds * bytes each, multiple commands can be sent at once, and 6801da177e4SLinus Torvalds * no command will be split across USB packets. If timeout 6811da177e4SLinus Torvalds * is non-zero, write in band command will return after 6821da177e4SLinus Torvalds * waiting unsuccessfully for the URB status to clear for 6831da177e4SLinus Torvalds * timeout ticks. Returns 0 if successful, or a negative 6841da177e4SLinus Torvalds * error returned by digi_write. 6851da177e4SLinus Torvalds */ 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds static int digi_write_inb_command(struct usb_serial_port *port, 6881da177e4SLinus Torvalds unsigned char *buf, int count, unsigned long timeout) 6891da177e4SLinus Torvalds { 6901da177e4SLinus Torvalds int ret = 0; 6911da177e4SLinus Torvalds int len; 6921da177e4SLinus Torvalds struct digi_port *priv = usb_get_serial_port_data(port); 6931da177e4SLinus Torvalds unsigned char *data = port->write_urb->transfer_buffer; 6941da177e4SLinus Torvalds unsigned long flags = 0; 6951da177e4SLinus Torvalds 696c6d61269SAlan Cox dbg("digi_write_inb_command: TOP: port=%d, count=%d", 697c6d61269SAlan Cox priv->dp_port_num, count); 6981da177e4SLinus Torvalds 6991da177e4SLinus Torvalds if (timeout) 7001da177e4SLinus Torvalds timeout += jiffies; 7011da177e4SLinus Torvalds else 7021da177e4SLinus Torvalds timeout = ULONG_MAX; 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds spin_lock_irqsave(&priv->dp_port_lock, flags); 7051da177e4SLinus Torvalds while (count > 0 && ret == 0) { 70650de36f7SGreg Kroah-Hartman while (priv->dp_write_urb_in_use && 70750de36f7SGreg Kroah-Hartman time_before(jiffies, timeout)) { 7081da177e4SLinus Torvalds cond_wait_interruptible_timeout_irqrestore( 7091da177e4SLinus Torvalds &port->write_wait, DIGI_RETRY_TIMEOUT, 7101da177e4SLinus Torvalds &priv->dp_port_lock, flags); 711c6d61269SAlan Cox if (signal_pending(current)) 712c6d61269SAlan Cox return -EINTR; 7131da177e4SLinus Torvalds spin_lock_irqsave(&priv->dp_port_lock, flags); 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds /* len must be a multiple of 4 and small enough to */ 7171da177e4SLinus Torvalds /* guarantee the write will send buffered data first, */ 7181da177e4SLinus Torvalds /* so commands are in order with data and not split */ 7191da177e4SLinus Torvalds len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len); 7201da177e4SLinus Torvalds if (len > 4) 7211da177e4SLinus Torvalds len &= ~3; 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds /* write any buffered data first */ 7241da177e4SLinus Torvalds if (priv->dp_out_buf_len > 0) { 7251da177e4SLinus Torvalds data[0] = DIGI_CMD_SEND_DATA; 7261da177e4SLinus Torvalds data[1] = priv->dp_out_buf_len; 7271da177e4SLinus Torvalds memcpy(data + 2, priv->dp_out_buf, 7281da177e4SLinus Torvalds priv->dp_out_buf_len); 7291da177e4SLinus Torvalds memcpy(data + 2 + priv->dp_out_buf_len, buf, len); 7301da177e4SLinus Torvalds port->write_urb->transfer_buffer_length 7311da177e4SLinus Torvalds = priv->dp_out_buf_len + 2 + len; 7321da177e4SLinus Torvalds } else { 7331da177e4SLinus Torvalds memcpy(data, buf, len); 7341da177e4SLinus Torvalds port->write_urb->transfer_buffer_length = len; 7351da177e4SLinus Torvalds } 7361da177e4SLinus Torvalds port->write_urb->dev = port->serial->dev; 7371da177e4SLinus Torvalds 73841ad427dSAlan Cox ret = usb_submit_urb(port->write_urb, GFP_ATOMIC); 73941ad427dSAlan Cox if (ret == 0) { 7401da177e4SLinus Torvalds priv->dp_write_urb_in_use = 1; 7411da177e4SLinus Torvalds priv->dp_out_buf_len = 0; 7421da177e4SLinus Torvalds count -= len; 7431da177e4SLinus Torvalds buf += len; 7441da177e4SLinus Torvalds } 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds } 7471da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->dp_port_lock, flags); 7481da177e4SLinus Torvalds 749c6d61269SAlan Cox if (ret) 750194343d9SGreg Kroah-Hartman dev_err(&port->dev, 751194343d9SGreg Kroah-Hartman "%s: usb_submit_urb failed, ret=%d, port=%d\n", 752441b62c1SHarvey Harrison __func__, ret, priv->dp_port_num); 753c6d61269SAlan Cox return ret; 7541da177e4SLinus Torvalds } 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds /* 7581da177e4SLinus Torvalds * Digi Set Modem Signals 7591da177e4SLinus Torvalds * 7601da177e4SLinus Torvalds * Sets or clears DTR and RTS on the port, according to the 7611da177e4SLinus Torvalds * modem_signals argument. Use TIOCM_DTR and TIOCM_RTS flags 7621da177e4SLinus Torvalds * for the modem_signals argument. Returns 0 if successful, 7631da177e4SLinus Torvalds * -EINTR if interrupted while sleeping, or a non-zero error 7641da177e4SLinus Torvalds * returned by usb_submit_urb. 7651da177e4SLinus Torvalds */ 7661da177e4SLinus Torvalds 7671da177e4SLinus Torvalds static int digi_set_modem_signals(struct usb_serial_port *port, 7681da177e4SLinus Torvalds unsigned int modem_signals, int interruptible) 7691da177e4SLinus Torvalds { 7701da177e4SLinus Torvalds 7711da177e4SLinus Torvalds int ret; 7721da177e4SLinus Torvalds struct digi_port *port_priv = usb_get_serial_port_data(port); 7731da177e4SLinus Torvalds struct usb_serial_port *oob_port = (struct usb_serial_port *) ((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port; 7741da177e4SLinus Torvalds struct digi_port *oob_priv = usb_get_serial_port_data(oob_port); 7751da177e4SLinus Torvalds unsigned char *data = oob_port->write_urb->transfer_buffer; 7761da177e4SLinus Torvalds unsigned long flags = 0; 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds dbg("digi_set_modem_signals: TOP: port=%d, modem_signals=0x%x", 7801da177e4SLinus Torvalds port_priv->dp_port_num, modem_signals); 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds spin_lock_irqsave(&oob_priv->dp_port_lock, flags); 7831da177e4SLinus Torvalds spin_lock(&port_priv->dp_port_lock); 7841da177e4SLinus Torvalds 78550de36f7SGreg Kroah-Hartman while (oob_priv->dp_write_urb_in_use) { 7861da177e4SLinus Torvalds spin_unlock(&port_priv->dp_port_lock); 7871da177e4SLinus Torvalds cond_wait_interruptible_timeout_irqrestore( 7881da177e4SLinus Torvalds &oob_port->write_wait, DIGI_RETRY_TIMEOUT, 7891da177e4SLinus Torvalds &oob_priv->dp_port_lock, flags); 790c6d61269SAlan Cox if (interruptible && signal_pending(current)) 791c6d61269SAlan Cox return -EINTR; 7921da177e4SLinus Torvalds spin_lock_irqsave(&oob_priv->dp_port_lock, flags); 7931da177e4SLinus Torvalds spin_lock(&port_priv->dp_port_lock); 7941da177e4SLinus Torvalds } 7951da177e4SLinus Torvalds data[0] = DIGI_CMD_SET_DTR_SIGNAL; 7961da177e4SLinus Torvalds data[1] = port_priv->dp_port_num; 79741ad427dSAlan Cox data[2] = (modem_signals & TIOCM_DTR) ? 79841ad427dSAlan Cox DIGI_DTR_ACTIVE : DIGI_DTR_INACTIVE; 7991da177e4SLinus Torvalds data[3] = 0; 8001da177e4SLinus Torvalds data[4] = DIGI_CMD_SET_RTS_SIGNAL; 8011da177e4SLinus Torvalds data[5] = port_priv->dp_port_num; 80241ad427dSAlan Cox data[6] = (modem_signals & TIOCM_RTS) ? 80341ad427dSAlan Cox DIGI_RTS_ACTIVE : DIGI_RTS_INACTIVE; 8041da177e4SLinus Torvalds data[7] = 0; 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds oob_port->write_urb->transfer_buffer_length = 8; 8071da177e4SLinus Torvalds oob_port->write_urb->dev = port->serial->dev; 8081da177e4SLinus Torvalds 80941ad427dSAlan Cox ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC); 81041ad427dSAlan Cox if (ret == 0) { 8111da177e4SLinus Torvalds oob_priv->dp_write_urb_in_use = 1; 8121da177e4SLinus Torvalds port_priv->dp_modem_signals = 8131da177e4SLinus Torvalds (port_priv->dp_modem_signals&~(TIOCM_DTR|TIOCM_RTS)) 8141da177e4SLinus Torvalds | (modem_signals&(TIOCM_DTR|TIOCM_RTS)); 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds spin_unlock(&port_priv->dp_port_lock); 8171da177e4SLinus Torvalds spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags); 818c6d61269SAlan Cox if (ret) 819194343d9SGreg Kroah-Hartman dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n", 820194343d9SGreg Kroah-Hartman __func__, ret); 821c6d61269SAlan Cox return ret; 8221da177e4SLinus Torvalds } 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds /* 8251da177e4SLinus Torvalds * Digi Transmit Idle 8261da177e4SLinus Torvalds * 8271da177e4SLinus Torvalds * Digi transmit idle waits, up to timeout ticks, for the transmitter 8281da177e4SLinus Torvalds * to go idle. It returns 0 if successful or a negative error. 8291da177e4SLinus Torvalds * 8301da177e4SLinus Torvalds * There are race conditions here if more than one process is calling 8311da177e4SLinus Torvalds * digi_transmit_idle on the same port at the same time. However, this 8321da177e4SLinus Torvalds * is only called from close, and only one process can be in close on a 8331da177e4SLinus Torvalds * port at a time, so its ok. 8341da177e4SLinus Torvalds */ 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds static int digi_transmit_idle(struct usb_serial_port *port, 8371da177e4SLinus Torvalds unsigned long timeout) 8381da177e4SLinus Torvalds { 8391da177e4SLinus Torvalds int ret; 8401da177e4SLinus Torvalds unsigned char buf[2]; 8411da177e4SLinus Torvalds struct digi_port *priv = usb_get_serial_port_data(port); 8421da177e4SLinus Torvalds unsigned long flags = 0; 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds spin_lock_irqsave(&priv->dp_port_lock, flags); 8451da177e4SLinus Torvalds priv->dp_transmit_idle = 0; 8461da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->dp_port_lock, flags); 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds buf[0] = DIGI_CMD_TRANSMIT_IDLE; 8491da177e4SLinus Torvalds buf[1] = 0; 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds timeout += jiffies; 8521da177e4SLinus Torvalds 85341ad427dSAlan Cox ret = digi_write_inb_command(port, buf, 2, timeout - jiffies); 85441ad427dSAlan Cox if (ret != 0) 855c6d61269SAlan Cox return ret; 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds spin_lock_irqsave(&priv->dp_port_lock, flags); 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds while (time_before(jiffies, timeout) && !priv->dp_transmit_idle) { 8601da177e4SLinus Torvalds cond_wait_interruptible_timeout_irqrestore( 8611da177e4SLinus Torvalds &priv->dp_transmit_idle_wait, DIGI_RETRY_TIMEOUT, 8621da177e4SLinus Torvalds &priv->dp_port_lock, flags); 863c6d61269SAlan Cox if (signal_pending(current)) 864c6d61269SAlan Cox return -EINTR; 8651da177e4SLinus Torvalds spin_lock_irqsave(&priv->dp_port_lock, flags); 8661da177e4SLinus Torvalds } 8671da177e4SLinus Torvalds priv->dp_transmit_idle = 0; 8681da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->dp_port_lock, flags); 869c6d61269SAlan Cox return 0; 8701da177e4SLinus Torvalds 8711da177e4SLinus Torvalds } 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds 87495da310eSAlan Cox static void digi_rx_throttle(struct tty_struct *tty) 8751da177e4SLinus Torvalds { 8761da177e4SLinus Torvalds unsigned long flags; 87795da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 8781da177e4SLinus Torvalds struct digi_port *priv = usb_get_serial_port_data(port); 8791da177e4SLinus Torvalds 8801da177e4SLinus Torvalds 8811da177e4SLinus Torvalds dbg("digi_rx_throttle: TOP: port=%d", priv->dp_port_num); 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds /* stop receiving characters by not resubmitting the read urb */ 8841da177e4SLinus Torvalds spin_lock_irqsave(&priv->dp_port_lock, flags); 8851da177e4SLinus Torvalds priv->dp_throttled = 1; 8861da177e4SLinus Torvalds priv->dp_throttle_restart = 0; 8871da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->dp_port_lock, flags); 8881da177e4SLinus Torvalds } 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds 89195da310eSAlan Cox static void digi_rx_unthrottle(struct tty_struct *tty) 8921da177e4SLinus Torvalds { 8931da177e4SLinus Torvalds int ret = 0; 8941da177e4SLinus Torvalds unsigned long flags; 89595da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 8961da177e4SLinus Torvalds struct digi_port *priv = usb_get_serial_port_data(port); 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds dbg("digi_rx_unthrottle: TOP: port=%d", priv->dp_port_num); 8991da177e4SLinus Torvalds 9001da177e4SLinus Torvalds spin_lock_irqsave(&priv->dp_port_lock, flags); 9011da177e4SLinus Torvalds 90239892da4SOliver Neukum /* turn throttle off */ 90339892da4SOliver Neukum priv->dp_throttled = 0; 90439892da4SOliver Neukum priv->dp_throttle_restart = 0; 9051da177e4SLinus Torvalds 9061da177e4SLinus Torvalds /* restart read chain */ 9071da177e4SLinus Torvalds if (priv->dp_throttle_restart) { 9081da177e4SLinus Torvalds port->read_urb->dev = port->serial->dev; 9091da177e4SLinus Torvalds ret = usb_submit_urb(port->read_urb, GFP_ATOMIC); 9101da177e4SLinus Torvalds } 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->dp_port_lock, flags); 9131da177e4SLinus Torvalds 914c6d61269SAlan Cox if (ret) 915194343d9SGreg Kroah-Hartman dev_err(&port->dev, 916194343d9SGreg Kroah-Hartman "%s: usb_submit_urb failed, ret=%d, port=%d\n", 917441b62c1SHarvey Harrison __func__, ret, priv->dp_port_num); 9181da177e4SLinus Torvalds } 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds 92195da310eSAlan Cox static void digi_set_termios(struct tty_struct *tty, 92295da310eSAlan Cox struct usb_serial_port *port, struct ktermios *old_termios) 9231da177e4SLinus Torvalds { 9241da177e4SLinus Torvalds struct digi_port *priv = usb_get_serial_port_data(port); 925c6d61269SAlan Cox unsigned int iflag = tty->termios->c_iflag; 926c6d61269SAlan Cox unsigned int cflag = tty->termios->c_cflag; 9271da177e4SLinus Torvalds unsigned int old_iflag = old_termios->c_iflag; 9281da177e4SLinus Torvalds unsigned int old_cflag = old_termios->c_cflag; 9291da177e4SLinus Torvalds unsigned char buf[32]; 9301da177e4SLinus Torvalds unsigned int modem_signals; 9311da177e4SLinus Torvalds int arg, ret; 9321da177e4SLinus Torvalds int i = 0; 933c6d61269SAlan Cox speed_t baud; 9341da177e4SLinus Torvalds 9351da177e4SLinus 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); 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds /* set baud rate */ 93841ad427dSAlan Cox baud = tty_get_baud_rate(tty); 93941ad427dSAlan Cox if (baud != tty_termios_baud_rate(old_termios)) { 9401da177e4SLinus Torvalds arg = -1; 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds /* reassert DTR and (maybe) RTS on transition from B0 */ 9431da177e4SLinus Torvalds if ((old_cflag&CBAUD) == B0) { 9441da177e4SLinus Torvalds /* don't set RTS if using hardware flow control */ 9451da177e4SLinus Torvalds /* and throttling input */ 9461da177e4SLinus Torvalds modem_signals = TIOCM_DTR; 947c6d61269SAlan Cox if (!(tty->termios->c_cflag & CRTSCTS) || 948c6d61269SAlan Cox !test_bit(TTY_THROTTLED, &tty->flags)) 9491da177e4SLinus Torvalds modem_signals |= TIOCM_RTS; 9501da177e4SLinus Torvalds digi_set_modem_signals(port, modem_signals, 1); 9511da177e4SLinus Torvalds } 952c6d61269SAlan Cox switch (baud) { 9531da177e4SLinus Torvalds /* drop DTR and RTS on transition to B0 */ 954c6d61269SAlan Cox case 0: digi_set_modem_signals(port, 0, 1); break; 955c6d61269SAlan Cox case 50: arg = DIGI_BAUD_50; break; 956c6d61269SAlan Cox case 75: arg = DIGI_BAUD_75; break; 957c6d61269SAlan Cox case 110: arg = DIGI_BAUD_110; break; 958c6d61269SAlan Cox case 150: arg = DIGI_BAUD_150; break; 959c6d61269SAlan Cox case 200: arg = DIGI_BAUD_200; break; 960c6d61269SAlan Cox case 300: arg = DIGI_BAUD_300; break; 961c6d61269SAlan Cox case 600: arg = DIGI_BAUD_600; break; 962c6d61269SAlan Cox case 1200: arg = DIGI_BAUD_1200; break; 963c6d61269SAlan Cox case 1800: arg = DIGI_BAUD_1800; break; 964c6d61269SAlan Cox case 2400: arg = DIGI_BAUD_2400; break; 965c6d61269SAlan Cox case 4800: arg = DIGI_BAUD_4800; break; 966c6d61269SAlan Cox case 9600: arg = DIGI_BAUD_9600; break; 967c6d61269SAlan Cox case 19200: arg = DIGI_BAUD_19200; break; 968c6d61269SAlan Cox case 38400: arg = DIGI_BAUD_38400; break; 969c6d61269SAlan Cox case 57600: arg = DIGI_BAUD_57600; break; 970c6d61269SAlan Cox case 115200: arg = DIGI_BAUD_115200; break; 971c6d61269SAlan Cox case 230400: arg = DIGI_BAUD_230400; break; 972c6d61269SAlan Cox case 460800: arg = DIGI_BAUD_460800; break; 9731da177e4SLinus Torvalds default: 974c6d61269SAlan Cox arg = DIGI_BAUD_9600; 975c6d61269SAlan Cox baud = 9600; 9761da177e4SLinus Torvalds break; 9771da177e4SLinus Torvalds } 9781da177e4SLinus Torvalds if (arg != -1) { 9791da177e4SLinus Torvalds buf[i++] = DIGI_CMD_SET_BAUD_RATE; 9801da177e4SLinus Torvalds buf[i++] = priv->dp_port_num; 9811da177e4SLinus Torvalds buf[i++] = arg; 9821da177e4SLinus Torvalds buf[i++] = 0; 9831da177e4SLinus Torvalds } 9841da177e4SLinus Torvalds } 9851da177e4SLinus Torvalds /* set parity */ 9867fa36a99SAlan Cox tty->termios->c_cflag &= ~CMSPAR; 9877fa36a99SAlan Cox 9881da177e4SLinus Torvalds if ((cflag&(PARENB|PARODD)) != (old_cflag&(PARENB|PARODD))) { 989c6d61269SAlan Cox if (cflag&PARENB) { 990c6d61269SAlan Cox if (cflag&PARODD) 9911da177e4SLinus Torvalds arg = DIGI_PARITY_ODD; 9921da177e4SLinus Torvalds else 9931da177e4SLinus Torvalds arg = DIGI_PARITY_EVEN; 9941da177e4SLinus Torvalds } else { 9951da177e4SLinus Torvalds arg = DIGI_PARITY_NONE; 9961da177e4SLinus Torvalds } 9971da177e4SLinus Torvalds buf[i++] = DIGI_CMD_SET_PARITY; 9981da177e4SLinus Torvalds buf[i++] = priv->dp_port_num; 9991da177e4SLinus Torvalds buf[i++] = arg; 10001da177e4SLinus Torvalds buf[i++] = 0; 10011da177e4SLinus Torvalds } 10021da177e4SLinus Torvalds /* set word size */ 10031da177e4SLinus Torvalds if ((cflag&CSIZE) != (old_cflag&CSIZE)) { 10041da177e4SLinus Torvalds arg = -1; 1005c6d61269SAlan Cox switch (cflag&CSIZE) { 10061da177e4SLinus Torvalds case CS5: arg = DIGI_WORD_SIZE_5; break; 10071da177e4SLinus Torvalds case CS6: arg = DIGI_WORD_SIZE_6; break; 10081da177e4SLinus Torvalds case CS7: arg = DIGI_WORD_SIZE_7; break; 10091da177e4SLinus Torvalds case CS8: arg = DIGI_WORD_SIZE_8; break; 10101da177e4SLinus Torvalds default: 10111da177e4SLinus Torvalds dbg("digi_set_termios: can't handle word size %d", 10121da177e4SLinus Torvalds (cflag&CSIZE)); 10131da177e4SLinus Torvalds break; 10141da177e4SLinus Torvalds } 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds if (arg != -1) { 10171da177e4SLinus Torvalds buf[i++] = DIGI_CMD_SET_WORD_SIZE; 10181da177e4SLinus Torvalds buf[i++] = priv->dp_port_num; 10191da177e4SLinus Torvalds buf[i++] = arg; 10201da177e4SLinus Torvalds buf[i++] = 0; 10211da177e4SLinus Torvalds } 10221da177e4SLinus Torvalds 10231da177e4SLinus Torvalds } 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds /* set stop bits */ 10261da177e4SLinus Torvalds if ((cflag&CSTOPB) != (old_cflag&CSTOPB)) { 10271da177e4SLinus Torvalds 10281da177e4SLinus Torvalds if ((cflag&CSTOPB)) 10291da177e4SLinus Torvalds arg = DIGI_STOP_BITS_2; 10301da177e4SLinus Torvalds else 10311da177e4SLinus Torvalds arg = DIGI_STOP_BITS_1; 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds buf[i++] = DIGI_CMD_SET_STOP_BITS; 10341da177e4SLinus Torvalds buf[i++] = priv->dp_port_num; 10351da177e4SLinus Torvalds buf[i++] = arg; 10361da177e4SLinus Torvalds buf[i++] = 0; 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds } 10391da177e4SLinus Torvalds 10401da177e4SLinus Torvalds /* set input flow control */ 10411da177e4SLinus Torvalds if ((iflag&IXOFF) != (old_iflag&IXOFF) 10421da177e4SLinus Torvalds || (cflag&CRTSCTS) != (old_cflag&CRTSCTS)) { 10431da177e4SLinus Torvalds arg = 0; 1044c6d61269SAlan Cox if (iflag&IXOFF) 10451da177e4SLinus Torvalds arg |= DIGI_INPUT_FLOW_CONTROL_XON_XOFF; 10461da177e4SLinus Torvalds else 10471da177e4SLinus Torvalds arg &= ~DIGI_INPUT_FLOW_CONTROL_XON_XOFF; 10481da177e4SLinus Torvalds 1049c6d61269SAlan Cox if (cflag&CRTSCTS) { 10501da177e4SLinus Torvalds arg |= DIGI_INPUT_FLOW_CONTROL_RTS; 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds /* On USB-4 it is necessary to assert RTS prior */ 10531da177e4SLinus Torvalds /* to selecting RTS input flow control. */ 10541da177e4SLinus Torvalds buf[i++] = DIGI_CMD_SET_RTS_SIGNAL; 10551da177e4SLinus Torvalds buf[i++] = priv->dp_port_num; 10561da177e4SLinus Torvalds buf[i++] = DIGI_RTS_ACTIVE; 10571da177e4SLinus Torvalds buf[i++] = 0; 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds } else { 10601da177e4SLinus Torvalds arg &= ~DIGI_INPUT_FLOW_CONTROL_RTS; 10611da177e4SLinus Torvalds } 10621da177e4SLinus Torvalds buf[i++] = DIGI_CMD_SET_INPUT_FLOW_CONTROL; 10631da177e4SLinus Torvalds buf[i++] = priv->dp_port_num; 10641da177e4SLinus Torvalds buf[i++] = arg; 10651da177e4SLinus Torvalds buf[i++] = 0; 10661da177e4SLinus Torvalds } 10671da177e4SLinus Torvalds 10681da177e4SLinus Torvalds /* set output flow control */ 10691da177e4SLinus Torvalds if ((iflag & IXON) != (old_iflag & IXON) 10701da177e4SLinus Torvalds || (cflag & CRTSCTS) != (old_cflag & CRTSCTS)) { 10711da177e4SLinus Torvalds arg = 0; 1072c6d61269SAlan Cox if (iflag & IXON) 10731da177e4SLinus Torvalds arg |= DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF; 10741da177e4SLinus Torvalds else 10751da177e4SLinus Torvalds arg &= ~DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF; 10761da177e4SLinus Torvalds 1077c6d61269SAlan Cox if (cflag & CRTSCTS) { 10781da177e4SLinus Torvalds arg |= DIGI_OUTPUT_FLOW_CONTROL_CTS; 10791da177e4SLinus Torvalds } else { 10801da177e4SLinus Torvalds arg &= ~DIGI_OUTPUT_FLOW_CONTROL_CTS; 1081c6d61269SAlan Cox tty->hw_stopped = 0; 10821da177e4SLinus Torvalds } 10831da177e4SLinus Torvalds 10841da177e4SLinus Torvalds buf[i++] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL; 10851da177e4SLinus Torvalds buf[i++] = priv->dp_port_num; 10861da177e4SLinus Torvalds buf[i++] = arg; 10871da177e4SLinus Torvalds buf[i++] = 0; 10881da177e4SLinus Torvalds } 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds /* set receive enable/disable */ 10911da177e4SLinus Torvalds if ((cflag & CREAD) != (old_cflag & CREAD)) { 1092c6d61269SAlan Cox if (cflag & CREAD) 10931da177e4SLinus Torvalds arg = DIGI_ENABLE; 10941da177e4SLinus Torvalds else 10951da177e4SLinus Torvalds arg = DIGI_DISABLE; 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds buf[i++] = DIGI_CMD_RECEIVE_ENABLE; 10981da177e4SLinus Torvalds buf[i++] = priv->dp_port_num; 10991da177e4SLinus Torvalds buf[i++] = arg; 11001da177e4SLinus Torvalds buf[i++] = 0; 11011da177e4SLinus Torvalds } 110241ad427dSAlan Cox ret = digi_write_oob_command(port, buf, i, 1); 110341ad427dSAlan Cox if (ret != 0) 11041da177e4SLinus Torvalds dbg("digi_set_termios: write oob failed, ret=%d", ret); 11057fa36a99SAlan Cox tty_encode_baud_rate(tty, baud, baud); 11061da177e4SLinus Torvalds } 11071da177e4SLinus Torvalds 11081da177e4SLinus Torvalds 110995da310eSAlan Cox static void digi_break_ctl(struct tty_struct *tty, int break_state) 11101da177e4SLinus Torvalds { 111195da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 11121da177e4SLinus Torvalds unsigned char buf[4]; 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds buf[0] = DIGI_CMD_BREAK_CONTROL; 11151da177e4SLinus Torvalds buf[1] = 2; /* length */ 11161da177e4SLinus Torvalds buf[2] = break_state ? 1 : 0; 11171da177e4SLinus Torvalds buf[3] = 0; /* pad */ 11181da177e4SLinus Torvalds digi_write_inb_command(port, buf, 4, 0); 11191da177e4SLinus Torvalds } 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds 112295da310eSAlan Cox static int digi_tiocmget(struct tty_struct *tty, struct file *file) 11231da177e4SLinus Torvalds { 112495da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 11251da177e4SLinus Torvalds struct digi_port *priv = usb_get_serial_port_data(port); 11261da177e4SLinus Torvalds unsigned int val; 11271da177e4SLinus Torvalds unsigned long flags; 11281da177e4SLinus Torvalds 1129441b62c1SHarvey Harrison dbg("%s: TOP: port=%d", __func__, priv->dp_port_num); 11301da177e4SLinus Torvalds 11311da177e4SLinus Torvalds spin_lock_irqsave(&priv->dp_port_lock, flags); 11321da177e4SLinus Torvalds val = priv->dp_modem_signals; 11331da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->dp_port_lock, flags); 11341da177e4SLinus Torvalds return val; 11351da177e4SLinus Torvalds } 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds 113895da310eSAlan Cox static int digi_tiocmset(struct tty_struct *tty, struct file *file, 11391da177e4SLinus Torvalds unsigned int set, unsigned int clear) 11401da177e4SLinus Torvalds { 114195da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 11421da177e4SLinus Torvalds struct digi_port *priv = usb_get_serial_port_data(port); 11431da177e4SLinus Torvalds unsigned int val; 11441da177e4SLinus Torvalds unsigned long flags; 11451da177e4SLinus Torvalds 1146441b62c1SHarvey Harrison dbg("%s: TOP: port=%d", __func__, priv->dp_port_num); 11471da177e4SLinus Torvalds 11481da177e4SLinus Torvalds spin_lock_irqsave(&priv->dp_port_lock, flags); 11491da177e4SLinus Torvalds val = (priv->dp_modem_signals & ~clear) | set; 11501da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->dp_port_lock, flags); 11511da177e4SLinus Torvalds return digi_set_modem_signals(port, val, 1); 11521da177e4SLinus Torvalds } 11531da177e4SLinus Torvalds 11541da177e4SLinus Torvalds 115595da310eSAlan Cox static int digi_write(struct tty_struct *tty, struct usb_serial_port *port, 115695da310eSAlan Cox const unsigned char *buf, int count) 11571da177e4SLinus Torvalds { 11581da177e4SLinus Torvalds 11591da177e4SLinus Torvalds int ret, data_len, new_len; 11601da177e4SLinus Torvalds struct digi_port *priv = usb_get_serial_port_data(port); 11611da177e4SLinus Torvalds unsigned char *data = port->write_urb->transfer_buffer; 11621da177e4SLinus Torvalds unsigned long flags = 0; 11631da177e4SLinus Torvalds 11641da177e4SLinus Torvalds dbg("digi_write: TOP: port=%d, count=%d, in_interrupt=%ld", 11651da177e4SLinus Torvalds priv->dp_port_num, count, in_interrupt()); 11661da177e4SLinus Torvalds 11671da177e4SLinus Torvalds /* copy user data (which can sleep) before getting spin lock */ 11681da177e4SLinus Torvalds count = min(count, port->bulk_out_size-2); 11691da177e4SLinus Torvalds count = min(64, count); 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds /* be sure only one write proceeds at a time */ 11721da177e4SLinus Torvalds /* there are races on the port private buffer */ 11731da177e4SLinus Torvalds spin_lock_irqsave(&priv->dp_port_lock, flags); 11741da177e4SLinus Torvalds 11751da177e4SLinus Torvalds /* wait for urb status clear to submit another urb */ 117650de36f7SGreg Kroah-Hartman if (priv->dp_write_urb_in_use) { 11771da177e4SLinus Torvalds /* buffer data if count is 1 (probably put_char) if possible */ 11781da177e4SLinus Torvalds if (count == 1 && priv->dp_out_buf_len < DIGI_OUT_BUF_SIZE) { 11791da177e4SLinus Torvalds priv->dp_out_buf[priv->dp_out_buf_len++] = *buf; 11801da177e4SLinus Torvalds new_len = 1; 11811da177e4SLinus Torvalds } else { 11821da177e4SLinus Torvalds new_len = 0; 11831da177e4SLinus Torvalds } 11841da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->dp_port_lock, flags); 1185c6d61269SAlan Cox return new_len; 11861da177e4SLinus Torvalds } 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds /* allow space for any buffered data and for new data, up to */ 11891da177e4SLinus Torvalds /* transfer buffer size - 2 (for command and length bytes) */ 11901da177e4SLinus Torvalds new_len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len); 11911da177e4SLinus Torvalds data_len = new_len + priv->dp_out_buf_len; 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds if (data_len == 0) { 11941da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->dp_port_lock, flags); 1195c6d61269SAlan Cox return 0; 11961da177e4SLinus Torvalds } 11971da177e4SLinus Torvalds 11981da177e4SLinus Torvalds port->write_urb->transfer_buffer_length = data_len+2; 11991da177e4SLinus Torvalds port->write_urb->dev = port->serial->dev; 12001da177e4SLinus Torvalds 12011da177e4SLinus Torvalds *data++ = DIGI_CMD_SEND_DATA; 12021da177e4SLinus Torvalds *data++ = data_len; 12031da177e4SLinus Torvalds 12041da177e4SLinus Torvalds /* copy in buffered data first */ 12051da177e4SLinus Torvalds memcpy(data, priv->dp_out_buf, priv->dp_out_buf_len); 12061da177e4SLinus Torvalds data += priv->dp_out_buf_len; 12071da177e4SLinus Torvalds 12081da177e4SLinus Torvalds /* copy in new data */ 12091da177e4SLinus Torvalds memcpy(data, buf, new_len); 12101da177e4SLinus Torvalds 121141ad427dSAlan Cox ret = usb_submit_urb(port->write_urb, GFP_ATOMIC); 121241ad427dSAlan Cox if (ret == 0) { 12131da177e4SLinus Torvalds priv->dp_write_urb_in_use = 1; 12141da177e4SLinus Torvalds ret = new_len; 12151da177e4SLinus Torvalds priv->dp_out_buf_len = 0; 12161da177e4SLinus Torvalds } 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds /* return length of new data written, or error */ 12191da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->dp_port_lock, flags); 1220c6d61269SAlan Cox if (ret < 0) 1221194343d9SGreg Kroah-Hartman dev_err(&port->dev, 1222194343d9SGreg Kroah-Hartman "%s: usb_submit_urb failed, ret=%d, port=%d\n", 1223441b62c1SHarvey Harrison __func__, ret, priv->dp_port_num); 12241da177e4SLinus Torvalds dbg("digi_write: returning %d", ret); 1225c6d61269SAlan Cox return ret; 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds 12297d12e780SDavid Howells static void digi_write_bulk_callback(struct urb *urb) 12301da177e4SLinus Torvalds { 12311da177e4SLinus Torvalds 1232cdc97792SMing Lei struct usb_serial_port *port = urb->context; 12331da177e4SLinus Torvalds struct usb_serial *serial; 12341da177e4SLinus Torvalds struct digi_port *priv; 12351da177e4SLinus Torvalds struct digi_serial *serial_priv; 12361da177e4SLinus Torvalds int ret = 0; 123785d75107SGreg Kroah-Hartman int status = urb->status; 12381da177e4SLinus Torvalds 123950de36f7SGreg Kroah-Hartman dbg("digi_write_bulk_callback: TOP, status=%d", status); 12401da177e4SLinus Torvalds 12411da177e4SLinus Torvalds /* port and serial sanity check */ 12421da177e4SLinus Torvalds if (port == NULL || (priv = usb_get_serial_port_data(port)) == NULL) { 1243194343d9SGreg Kroah-Hartman dev_err(&port->dev, 1244194343d9SGreg Kroah-Hartman "%s: port or port->private is NULL, status=%d\n", 1245441b62c1SHarvey Harrison __func__, status); 12461da177e4SLinus Torvalds return; 12471da177e4SLinus Torvalds } 12481da177e4SLinus Torvalds serial = port->serial; 12491da177e4SLinus Torvalds if (serial == NULL || (serial_priv = usb_get_serial_data(serial)) == NULL) { 1250194343d9SGreg Kroah-Hartman dev_err(&port->dev, 1251194343d9SGreg Kroah-Hartman "%s: serial or serial->private is NULL, status=%d\n", 1252441b62c1SHarvey Harrison __func__, status); 12531da177e4SLinus Torvalds return; 12541da177e4SLinus Torvalds } 12551da177e4SLinus Torvalds 12561da177e4SLinus Torvalds /* handle oob callback */ 12571da177e4SLinus Torvalds if (priv->dp_port_num == serial_priv->ds_oob_port_num) { 12581da177e4SLinus Torvalds dbg("digi_write_bulk_callback: oob callback"); 12591da177e4SLinus Torvalds spin_lock(&priv->dp_port_lock); 12601da177e4SLinus Torvalds priv->dp_write_urb_in_use = 0; 12611da177e4SLinus Torvalds wake_up_interruptible(&port->write_wait); 12621da177e4SLinus Torvalds spin_unlock(&priv->dp_port_lock); 12631da177e4SLinus Torvalds return; 12641da177e4SLinus Torvalds } 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds /* try to send any buffered data on this port, if it is open */ 12671da177e4SLinus Torvalds spin_lock(&priv->dp_port_lock); 12681da177e4SLinus Torvalds priv->dp_write_urb_in_use = 0; 126950de36f7SGreg Kroah-Hartman if (port->port.count && priv->dp_out_buf_len > 0) { 12701da177e4SLinus Torvalds *((unsigned char *)(port->write_urb->transfer_buffer)) 12711da177e4SLinus Torvalds = (unsigned char)DIGI_CMD_SEND_DATA; 12721da177e4SLinus Torvalds *((unsigned char *)(port->write_urb->transfer_buffer) + 1) 12731da177e4SLinus Torvalds = (unsigned char)priv->dp_out_buf_len; 127441ad427dSAlan Cox port->write_urb->transfer_buffer_length = 127541ad427dSAlan Cox priv->dp_out_buf_len + 2; 12761da177e4SLinus Torvalds port->write_urb->dev = serial->dev; 12771da177e4SLinus Torvalds memcpy(port->write_urb->transfer_buffer + 2, priv->dp_out_buf, 12781da177e4SLinus Torvalds priv->dp_out_buf_len); 127941ad427dSAlan Cox ret = usb_submit_urb(port->write_urb, GFP_ATOMIC); 128041ad427dSAlan Cox if (ret == 0) { 12811da177e4SLinus Torvalds priv->dp_write_urb_in_use = 1; 12821da177e4SLinus Torvalds priv->dp_out_buf_len = 0; 12831da177e4SLinus Torvalds } 12841da177e4SLinus Torvalds } 12851da177e4SLinus Torvalds /* wake up processes sleeping on writes immediately */ 12861da177e4SLinus Torvalds digi_wakeup_write(port); 12871da177e4SLinus Torvalds /* also queue up a wakeup at scheduler time, in case we */ 12881da177e4SLinus Torvalds /* lost the race in write_chan(). */ 12891da177e4SLinus Torvalds schedule_work(&priv->dp_wakeup_work); 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds spin_unlock(&priv->dp_port_lock); 1292c6d61269SAlan Cox if (ret) 1293194343d9SGreg Kroah-Hartman dev_err(&port->dev, 1294194343d9SGreg Kroah-Hartman "%s: usb_submit_urb failed, ret=%d, port=%d\n", 1295441b62c1SHarvey Harrison __func__, ret, priv->dp_port_num); 12961da177e4SLinus Torvalds } 12971da177e4SLinus Torvalds 129895da310eSAlan Cox static int digi_write_room(struct tty_struct *tty) 12991da177e4SLinus Torvalds { 130095da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 13011da177e4SLinus Torvalds struct digi_port *priv = usb_get_serial_port_data(port); 130295da310eSAlan Cox int room; 13031da177e4SLinus Torvalds unsigned long flags = 0; 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds spin_lock_irqsave(&priv->dp_port_lock, flags); 13061da177e4SLinus Torvalds 130750de36f7SGreg Kroah-Hartman if (priv->dp_write_urb_in_use) 13081da177e4SLinus Torvalds room = 0; 13091da177e4SLinus Torvalds else 13101da177e4SLinus Torvalds room = port->bulk_out_size - 2 - priv->dp_out_buf_len; 13111da177e4SLinus Torvalds 13121da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->dp_port_lock, flags); 13131da177e4SLinus Torvalds dbg("digi_write_room: port=%d, room=%d", priv->dp_port_num, room); 1314c6d61269SAlan Cox return room; 13151da177e4SLinus Torvalds 13161da177e4SLinus Torvalds } 13171da177e4SLinus Torvalds 131895da310eSAlan Cox static int digi_chars_in_buffer(struct tty_struct *tty) 13191da177e4SLinus Torvalds { 132095da310eSAlan Cox struct usb_serial_port *port = tty->driver_data; 13211da177e4SLinus Torvalds struct digi_port *priv = usb_get_serial_port_data(port); 13221da177e4SLinus Torvalds 132350de36f7SGreg Kroah-Hartman if (priv->dp_write_urb_in_use) { 1324c6d61269SAlan Cox dbg("digi_chars_in_buffer: port=%d, chars=%d", 1325c6d61269SAlan Cox priv->dp_port_num, port->bulk_out_size - 2); 13261da177e4SLinus Torvalds /* return(port->bulk_out_size - 2); */ 1327c6d61269SAlan Cox return 256; 13281da177e4SLinus Torvalds } else { 1329c6d61269SAlan Cox dbg("digi_chars_in_buffer: port=%d, chars=%d", 1330c6d61269SAlan Cox priv->dp_port_num, priv->dp_out_buf_len); 1331c6d61269SAlan Cox return priv->dp_out_buf_len; 13321da177e4SLinus Torvalds } 13331da177e4SLinus Torvalds 13341da177e4SLinus Torvalds } 13351da177e4SLinus Torvalds 1336335f8514SAlan Cox static void digi_dtr_rts(struct usb_serial_port *port, int on) 1337335f8514SAlan Cox { 1338335f8514SAlan Cox /* Adjust DTR and RTS */ 1339335f8514SAlan Cox digi_set_modem_signals(port, on * (TIOCM_DTR|TIOCM_RTS), 1); 1340335f8514SAlan Cox } 1341335f8514SAlan Cox 1342335f8514SAlan Cox static int digi_carrier_raised(struct usb_serial_port *port) 1343335f8514SAlan Cox { 1344335f8514SAlan Cox struct digi_port *priv = usb_get_serial_port_data(port); 1345335f8514SAlan Cox if (priv->dp_modem_signals & TIOCM_CD) 1346335f8514SAlan Cox return 1; 1347335f8514SAlan Cox return 0; 1348335f8514SAlan Cox } 13491da177e4SLinus Torvalds 135095da310eSAlan Cox static int digi_open(struct tty_struct *tty, struct usb_serial_port *port, 135195da310eSAlan Cox struct file *filp) 13521da177e4SLinus Torvalds { 13531da177e4SLinus Torvalds int ret; 13541da177e4SLinus Torvalds unsigned char buf[32]; 13551da177e4SLinus Torvalds struct digi_port *priv = usb_get_serial_port_data(port); 1356606d099cSAlan Cox struct ktermios not_termios; 13571da177e4SLinus Torvalds 1358c6d61269SAlan Cox dbg("digi_open: TOP: port=%d, open_count=%d", 135995da310eSAlan Cox priv->dp_port_num, port->port.count); 13601da177e4SLinus Torvalds 13611da177e4SLinus Torvalds /* be sure the device is started up */ 13621da177e4SLinus Torvalds if (digi_startup_device(port->serial) != 0) 1363c6d61269SAlan Cox return -ENXIO; 13641da177e4SLinus Torvalds 13651da177e4SLinus Torvalds /* read modem signals automatically whenever they change */ 13661da177e4SLinus Torvalds buf[0] = DIGI_CMD_READ_INPUT_SIGNALS; 13671da177e4SLinus Torvalds buf[1] = priv->dp_port_num; 13681da177e4SLinus Torvalds buf[2] = DIGI_ENABLE; 13691da177e4SLinus Torvalds buf[3] = 0; 13701da177e4SLinus Torvalds 13711da177e4SLinus Torvalds /* flush fifos */ 13721da177e4SLinus Torvalds buf[4] = DIGI_CMD_IFLUSH_FIFO; 13731da177e4SLinus Torvalds buf[5] = priv->dp_port_num; 13741da177e4SLinus Torvalds buf[6] = DIGI_FLUSH_TX | DIGI_FLUSH_RX; 13751da177e4SLinus Torvalds buf[7] = 0; 13761da177e4SLinus Torvalds 137741ad427dSAlan Cox ret = digi_write_oob_command(port, buf, 8, 1); 137841ad427dSAlan Cox if (ret != 0) 13791da177e4SLinus Torvalds dbg("digi_open: write oob failed, ret=%d", ret); 13801da177e4SLinus Torvalds 13811da177e4SLinus Torvalds /* set termios settings */ 138295da310eSAlan Cox if (tty) { 138395da310eSAlan Cox not_termios.c_cflag = ~tty->termios->c_cflag; 138495da310eSAlan Cox not_termios.c_iflag = ~tty->termios->c_iflag; 138595da310eSAlan Cox digi_set_termios(tty, port, ¬_termios); 138695da310eSAlan Cox } 1387c6d61269SAlan Cox return 0; 13881da177e4SLinus Torvalds } 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds 1391335f8514SAlan Cox static void digi_close(struct usb_serial_port *port) 13921da177e4SLinus Torvalds { 13931da177e4SLinus Torvalds DEFINE_WAIT(wait); 13941da177e4SLinus Torvalds int ret; 13951da177e4SLinus Torvalds unsigned char buf[32]; 13961da177e4SLinus Torvalds struct digi_port *priv = usb_get_serial_port_data(port); 13971da177e4SLinus Torvalds 1398c6d61269SAlan Cox dbg("digi_close: TOP: port=%d, open_count=%d", 139995da310eSAlan Cox priv->dp_port_num, port->port.count); 14001da177e4SLinus Torvalds 14010915f490SOliver Neukum mutex_lock(&port->serial->disc_mutex); 14021da177e4SLinus Torvalds /* if disconnected, just clear flags */ 14030915f490SOliver Neukum if (port->serial->disconnected) 14041da177e4SLinus Torvalds goto exit; 14051da177e4SLinus Torvalds 14061da177e4SLinus Torvalds if (port->serial->dev) { 1407335f8514SAlan Cox /* FIXME: Transmit idle belongs in the wait_unti_sent path */ 14081da177e4SLinus Torvalds digi_transmit_idle(port, DIGI_CLOSE_TIMEOUT); 14091da177e4SLinus Torvalds 14101da177e4SLinus Torvalds /* disable input flow control */ 14111da177e4SLinus Torvalds buf[0] = DIGI_CMD_SET_INPUT_FLOW_CONTROL; 14121da177e4SLinus Torvalds buf[1] = priv->dp_port_num; 14131da177e4SLinus Torvalds buf[2] = DIGI_DISABLE; 14141da177e4SLinus Torvalds buf[3] = 0; 14151da177e4SLinus Torvalds 14161da177e4SLinus Torvalds /* disable output flow control */ 14171da177e4SLinus Torvalds buf[4] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL; 14181da177e4SLinus Torvalds buf[5] = priv->dp_port_num; 14191da177e4SLinus Torvalds buf[6] = DIGI_DISABLE; 14201da177e4SLinus Torvalds buf[7] = 0; 14211da177e4SLinus Torvalds 14221da177e4SLinus Torvalds /* disable reading modem signals automatically */ 14231da177e4SLinus Torvalds buf[8] = DIGI_CMD_READ_INPUT_SIGNALS; 14241da177e4SLinus Torvalds buf[9] = priv->dp_port_num; 14251da177e4SLinus Torvalds buf[10] = DIGI_DISABLE; 14261da177e4SLinus Torvalds buf[11] = 0; 14271da177e4SLinus Torvalds 14281da177e4SLinus Torvalds /* disable receive */ 14291da177e4SLinus Torvalds buf[12] = DIGI_CMD_RECEIVE_ENABLE; 14301da177e4SLinus Torvalds buf[13] = priv->dp_port_num; 14311da177e4SLinus Torvalds buf[14] = DIGI_DISABLE; 14321da177e4SLinus Torvalds buf[15] = 0; 14331da177e4SLinus Torvalds 14341da177e4SLinus Torvalds /* flush fifos */ 14351da177e4SLinus Torvalds buf[16] = DIGI_CMD_IFLUSH_FIFO; 14361da177e4SLinus Torvalds buf[17] = priv->dp_port_num; 14371da177e4SLinus Torvalds buf[18] = DIGI_FLUSH_TX | DIGI_FLUSH_RX; 14381da177e4SLinus Torvalds buf[19] = 0; 14391da177e4SLinus Torvalds 144041ad427dSAlan Cox ret = digi_write_oob_command(port, buf, 20, 0); 144141ad427dSAlan Cox if (ret != 0) 14421da177e4SLinus Torvalds dbg("digi_close: write oob failed, ret=%d", ret); 14431da177e4SLinus Torvalds 14441da177e4SLinus Torvalds /* wait for final commands on oob port to complete */ 144541ad427dSAlan Cox prepare_to_wait(&priv->dp_flush_wait, &wait, 144641ad427dSAlan Cox TASK_INTERRUPTIBLE); 14471da177e4SLinus Torvalds schedule_timeout(DIGI_CLOSE_TIMEOUT); 14481da177e4SLinus Torvalds finish_wait(&priv->dp_flush_wait, &wait); 14491da177e4SLinus Torvalds 14501da177e4SLinus Torvalds /* shutdown any outstanding bulk writes */ 14511da177e4SLinus Torvalds usb_kill_urb(port->write_urb); 14521da177e4SLinus Torvalds } 14531da177e4SLinus Torvalds exit: 14540915f490SOliver Neukum spin_lock_irq(&priv->dp_port_lock); 14551da177e4SLinus Torvalds priv->dp_write_urb_in_use = 0; 14561da177e4SLinus Torvalds wake_up_interruptible(&priv->dp_close_wait); 14570915f490SOliver Neukum spin_unlock_irq(&priv->dp_port_lock); 14580915f490SOliver Neukum mutex_unlock(&port->serial->disc_mutex); 14591da177e4SLinus Torvalds dbg("digi_close: done"); 14601da177e4SLinus Torvalds } 14611da177e4SLinus Torvalds 14621da177e4SLinus Torvalds 14631da177e4SLinus Torvalds /* 14641da177e4SLinus Torvalds * Digi Startup Device 14651da177e4SLinus Torvalds * 14661da177e4SLinus Torvalds * Starts reads on all ports. Must be called AFTER startup, with 14671da177e4SLinus Torvalds * urbs initialized. Returns 0 if successful, non-zero error otherwise. 14681da177e4SLinus Torvalds */ 14691da177e4SLinus Torvalds 14701da177e4SLinus Torvalds static int digi_startup_device(struct usb_serial *serial) 14711da177e4SLinus Torvalds { 14721da177e4SLinus Torvalds int i, ret = 0; 14731da177e4SLinus Torvalds struct digi_serial *serial_priv = usb_get_serial_data(serial); 14741da177e4SLinus Torvalds struct usb_serial_port *port; 14751da177e4SLinus Torvalds 14761da177e4SLinus Torvalds /* be sure this happens exactly once */ 14771da177e4SLinus Torvalds spin_lock(&serial_priv->ds_serial_lock); 14781da177e4SLinus Torvalds if (serial_priv->ds_device_started) { 14791da177e4SLinus Torvalds spin_unlock(&serial_priv->ds_serial_lock); 1480c6d61269SAlan Cox return 0; 14811da177e4SLinus Torvalds } 14821da177e4SLinus Torvalds serial_priv->ds_device_started = 1; 14831da177e4SLinus Torvalds spin_unlock(&serial_priv->ds_serial_lock); 14841da177e4SLinus Torvalds 14851da177e4SLinus Torvalds /* start reading from each bulk in endpoint for the device */ 14861da177e4SLinus Torvalds /* set USB_DISABLE_SPD flag for write bulk urbs */ 14871da177e4SLinus Torvalds for (i = 0; i < serial->type->num_ports + 1; i++) { 14881da177e4SLinus Torvalds port = serial->port[i]; 14891da177e4SLinus Torvalds port->write_urb->dev = port->serial->dev; 149041ad427dSAlan Cox ret = usb_submit_urb(port->read_urb, GFP_KERNEL); 149141ad427dSAlan Cox if (ret != 0) { 1492194343d9SGreg Kroah-Hartman dev_err(&port->dev, 1493194343d9SGreg Kroah-Hartman "%s: usb_submit_urb failed, ret=%d, port=%d\n", 1494441b62c1SHarvey Harrison __func__, ret, i); 14951da177e4SLinus Torvalds break; 14961da177e4SLinus Torvalds } 14971da177e4SLinus Torvalds } 1498c6d61269SAlan Cox return ret; 14991da177e4SLinus Torvalds } 15001da177e4SLinus Torvalds 15011da177e4SLinus Torvalds 15021da177e4SLinus Torvalds static int digi_startup(struct usb_serial *serial) 15031da177e4SLinus Torvalds { 15041da177e4SLinus Torvalds 15051da177e4SLinus Torvalds int i; 15061da177e4SLinus Torvalds struct digi_port *priv; 15071da177e4SLinus Torvalds struct digi_serial *serial_priv; 15081da177e4SLinus Torvalds 15091da177e4SLinus Torvalds dbg("digi_startup: TOP"); 15101da177e4SLinus Torvalds 15111da177e4SLinus Torvalds /* allocate the private data structures for all ports */ 15121da177e4SLinus Torvalds /* number of regular ports + 1 for the out-of-band port */ 15131da177e4SLinus Torvalds for (i = 0; i < serial->type->num_ports + 1; i++) { 15141da177e4SLinus Torvalds /* allocate port private structure */ 1515c6d61269SAlan Cox priv = kmalloc(sizeof(struct digi_port), GFP_KERNEL); 1516c6d61269SAlan Cox if (priv == NULL) { 15171da177e4SLinus Torvalds while (--i >= 0) 15181da177e4SLinus Torvalds kfree(usb_get_serial_port_data(serial->port[i])); 1519c6d61269SAlan Cox return 1; /* error */ 15201da177e4SLinus Torvalds } 15211da177e4SLinus Torvalds 15221da177e4SLinus Torvalds /* initialize port private structure */ 15231da177e4SLinus Torvalds spin_lock_init(&priv->dp_port_lock); 15241da177e4SLinus Torvalds priv->dp_port_num = i; 15251da177e4SLinus Torvalds priv->dp_out_buf_len = 0; 15261da177e4SLinus Torvalds priv->dp_write_urb_in_use = 0; 15271da177e4SLinus Torvalds priv->dp_modem_signals = 0; 15281da177e4SLinus Torvalds init_waitqueue_head(&priv->dp_modem_change_wait); 15291da177e4SLinus Torvalds priv->dp_transmit_idle = 0; 15301da177e4SLinus Torvalds init_waitqueue_head(&priv->dp_transmit_idle_wait); 15311da177e4SLinus Torvalds priv->dp_throttled = 0; 15321da177e4SLinus Torvalds priv->dp_throttle_restart = 0; 15331da177e4SLinus Torvalds init_waitqueue_head(&priv->dp_flush_wait); 15341da177e4SLinus Torvalds init_waitqueue_head(&priv->dp_close_wait); 1535c4028958SDavid Howells INIT_WORK(&priv->dp_wakeup_work, digi_wakeup_write_lock); 1536c4028958SDavid Howells priv->dp_port = serial->port[i]; 15371da177e4SLinus Torvalds /* initialize write wait queue for this port */ 15381da177e4SLinus Torvalds init_waitqueue_head(&serial->port[i]->write_wait); 15391da177e4SLinus Torvalds 15401da177e4SLinus Torvalds usb_set_serial_port_data(serial->port[i], priv); 15411da177e4SLinus Torvalds } 15421da177e4SLinus Torvalds 15431da177e4SLinus Torvalds /* allocate serial private structure */ 1544c6d61269SAlan Cox serial_priv = kmalloc(sizeof(struct digi_serial), GFP_KERNEL); 1545c6d61269SAlan Cox if (serial_priv == NULL) { 15461da177e4SLinus Torvalds for (i = 0; i < serial->type->num_ports + 1; i++) 15471da177e4SLinus Torvalds kfree(usb_get_serial_port_data(serial->port[i])); 1548c6d61269SAlan Cox return 1; /* error */ 15491da177e4SLinus Torvalds } 15501da177e4SLinus Torvalds 15511da177e4SLinus Torvalds /* initialize serial private structure */ 15521da177e4SLinus Torvalds spin_lock_init(&serial_priv->ds_serial_lock); 15531da177e4SLinus Torvalds serial_priv->ds_oob_port_num = serial->type->num_ports; 15541da177e4SLinus Torvalds serial_priv->ds_oob_port = serial->port[serial_priv->ds_oob_port_num]; 15551da177e4SLinus Torvalds serial_priv->ds_device_started = 0; 15561da177e4SLinus Torvalds usb_set_serial_data(serial, serial_priv); 15571da177e4SLinus Torvalds 1558c6d61269SAlan Cox return 0; 15591da177e4SLinus Torvalds } 15601da177e4SLinus Torvalds 15611da177e4SLinus Torvalds 1562*f9c99bb8SAlan Stern static void digi_disconnect(struct usb_serial *serial) 15631da177e4SLinus Torvalds { 15641da177e4SLinus Torvalds int i; 1565*f9c99bb8SAlan Stern dbg("digi_disconnect: TOP, in_interrupt()=%ld", in_interrupt()); 15661da177e4SLinus Torvalds 15671da177e4SLinus Torvalds /* stop reads and writes on all ports */ 15681da177e4SLinus Torvalds for (i = 0; i < serial->type->num_ports + 1; i++) { 15691da177e4SLinus Torvalds usb_kill_urb(serial->port[i]->read_urb); 15701da177e4SLinus Torvalds usb_kill_urb(serial->port[i]->write_urb); 15711da177e4SLinus Torvalds } 1572*f9c99bb8SAlan Stern } 1573*f9c99bb8SAlan Stern 1574*f9c99bb8SAlan Stern 1575*f9c99bb8SAlan Stern static void digi_release(struct usb_serial *serial) 1576*f9c99bb8SAlan Stern { 1577*f9c99bb8SAlan Stern int i; 1578*f9c99bb8SAlan Stern dbg("digi_release: TOP, in_interrupt()=%ld", in_interrupt()); 15791da177e4SLinus Torvalds 15801da177e4SLinus Torvalds /* free the private data structures for all ports */ 15811da177e4SLinus Torvalds /* number of regular ports + 1 for the out-of-band port */ 15821da177e4SLinus Torvalds for (i = 0; i < serial->type->num_ports + 1; i++) 15831da177e4SLinus Torvalds kfree(usb_get_serial_port_data(serial->port[i])); 15841da177e4SLinus Torvalds kfree(usb_get_serial_data(serial)); 15851da177e4SLinus Torvalds } 15861da177e4SLinus Torvalds 15871da177e4SLinus Torvalds 15887d12e780SDavid Howells static void digi_read_bulk_callback(struct urb *urb) 15891da177e4SLinus Torvalds { 1590cdc97792SMing Lei struct usb_serial_port *port = urb->context; 15911da177e4SLinus Torvalds struct digi_port *priv; 15921da177e4SLinus Torvalds struct digi_serial *serial_priv; 15931da177e4SLinus Torvalds int ret; 159485d75107SGreg Kroah-Hartman int status = urb->status; 15951da177e4SLinus Torvalds 15961da177e4SLinus Torvalds dbg("digi_read_bulk_callback: TOP"); 15971da177e4SLinus Torvalds 15981da177e4SLinus Torvalds /* port sanity check, do not resubmit if port is not valid */ 1599194343d9SGreg Kroah-Hartman if (port == NULL) 1600194343d9SGreg Kroah-Hartman return; 1601194343d9SGreg Kroah-Hartman priv = usb_get_serial_port_data(port); 1602194343d9SGreg Kroah-Hartman if (priv == NULL) { 1603194343d9SGreg Kroah-Hartman dev_err(&port->dev, "%s: port->private is NULL, status=%d\n", 1604441b62c1SHarvey Harrison __func__, status); 16051da177e4SLinus Torvalds return; 16061da177e4SLinus Torvalds } 1607c6d61269SAlan Cox if (port->serial == NULL || 1608c6d61269SAlan Cox (serial_priv = usb_get_serial_data(port->serial)) == NULL) { 1609194343d9SGreg Kroah-Hartman dev_err(&port->dev, "%s: serial is bad or serial->private " 1610194343d9SGreg Kroah-Hartman "is NULL, status=%d\n", __func__, status); 16111da177e4SLinus Torvalds return; 16121da177e4SLinus Torvalds } 16131da177e4SLinus Torvalds 16141da177e4SLinus Torvalds /* do not resubmit urb if it has any status error */ 161585d75107SGreg Kroah-Hartman if (status) { 1616194343d9SGreg Kroah-Hartman dev_err(&port->dev, 1617194343d9SGreg Kroah-Hartman "%s: nonzero read bulk status: status=%d, port=%d\n", 1618441b62c1SHarvey Harrison __func__, status, priv->dp_port_num); 16191da177e4SLinus Torvalds return; 16201da177e4SLinus Torvalds } 16211da177e4SLinus Torvalds 16221da177e4SLinus Torvalds /* handle oob or inb callback, do not resubmit if error */ 16231da177e4SLinus Torvalds if (priv->dp_port_num == serial_priv->ds_oob_port_num) { 16241da177e4SLinus Torvalds if (digi_read_oob_callback(urb) != 0) 16251da177e4SLinus Torvalds return; 16261da177e4SLinus Torvalds } else { 16271da177e4SLinus Torvalds if (digi_read_inb_callback(urb) != 0) 16281da177e4SLinus Torvalds return; 16291da177e4SLinus Torvalds } 16301da177e4SLinus Torvalds 16311da177e4SLinus Torvalds /* continue read */ 16321da177e4SLinus Torvalds urb->dev = port->serial->dev; 163341ad427dSAlan Cox ret = usb_submit_urb(urb, GFP_ATOMIC); 163441ad427dSAlan Cox if (ret != 0) { 1635194343d9SGreg Kroah-Hartman dev_err(&port->dev, 1636194343d9SGreg Kroah-Hartman "%s: failed resubmitting urb, ret=%d, port=%d\n", 1637441b62c1SHarvey Harrison __func__, ret, priv->dp_port_num); 16381da177e4SLinus Torvalds } 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds } 16411da177e4SLinus Torvalds 16421da177e4SLinus Torvalds /* 16431da177e4SLinus Torvalds * Digi Read INB Callback 16441da177e4SLinus Torvalds * 16451da177e4SLinus Torvalds * Digi Read INB Callback handles reads on the in band ports, sending 16461da177e4SLinus Torvalds * the data on to the tty subsystem. When called we know port and 16471da177e4SLinus Torvalds * port->private are not NULL and port->serial has been validated. 16481da177e4SLinus Torvalds * It returns 0 if successful, 1 if successful but the port is 16491da177e4SLinus Torvalds * throttled, and -1 if the sanity checks failed. 16501da177e4SLinus Torvalds */ 16511da177e4SLinus Torvalds 16521da177e4SLinus Torvalds static int digi_read_inb_callback(struct urb *urb) 16531da177e4SLinus Torvalds { 16541da177e4SLinus Torvalds 1655cdc97792SMing Lei struct usb_serial_port *port = urb->context; 16564a90f09bSAlan Cox struct tty_struct *tty; 16571da177e4SLinus Torvalds struct digi_port *priv = usb_get_serial_port_data(port); 16581da177e4SLinus Torvalds int opcode = ((unsigned char *)urb->transfer_buffer)[0]; 16591da177e4SLinus Torvalds int len = ((unsigned char *)urb->transfer_buffer)[1]; 166085d75107SGreg Kroah-Hartman int port_status = ((unsigned char *)urb->transfer_buffer)[2]; 16611da177e4SLinus Torvalds unsigned char *data = ((unsigned char *)urb->transfer_buffer) + 3; 16621da177e4SLinus Torvalds int flag, throttled; 166333f0f88fSAlan Cox int i; 166485d75107SGreg Kroah-Hartman int status = urb->status; 16651da177e4SLinus Torvalds 16661da177e4SLinus Torvalds /* do not process callbacks on closed ports */ 16671da177e4SLinus Torvalds /* but do continue the read chain */ 166895da310eSAlan Cox if (port->port.count == 0) 1669c6d61269SAlan Cox return 0; 16701da177e4SLinus Torvalds 16711da177e4SLinus Torvalds /* short/multiple packet check */ 16721da177e4SLinus Torvalds if (urb->actual_length != len + 2) { 1673194343d9SGreg Kroah-Hartman dev_err(&port->dev, "%s: INCOMPLETE OR MULTIPLE PACKET, " 167450de36f7SGreg Kroah-Hartman "status=%d, port=%d, opcode=%d, len=%d, " 1675194343d9SGreg Kroah-Hartman "actual_length=%d, status=%d\n", __func__, status, 1676194343d9SGreg Kroah-Hartman priv->dp_port_num, opcode, len, urb->actual_length, 1677194343d9SGreg Kroah-Hartman port_status); 1678c6d61269SAlan Cox return -1; 16791da177e4SLinus Torvalds } 16801da177e4SLinus Torvalds 16814a90f09bSAlan Cox tty = tty_port_tty_get(&port->port); 16821da177e4SLinus Torvalds spin_lock(&priv->dp_port_lock); 16831da177e4SLinus Torvalds 16841da177e4SLinus Torvalds /* check for throttle; if set, do not resubmit read urb */ 16851da177e4SLinus Torvalds /* indicate the read chain needs to be restarted on unthrottle */ 16861da177e4SLinus Torvalds throttled = priv->dp_throttled; 16871da177e4SLinus Torvalds if (throttled) 16881da177e4SLinus Torvalds priv->dp_throttle_restart = 1; 16891da177e4SLinus Torvalds 16901da177e4SLinus Torvalds /* receive data */ 16911da177e4SLinus Torvalds if (opcode == DIGI_CMD_RECEIVE_DATA) { 169285d75107SGreg Kroah-Hartman /* get flag from port_status */ 16931da177e4SLinus Torvalds flag = 0; 16941da177e4SLinus Torvalds 16951da177e4SLinus Torvalds /* overrun is special, not associated with a char */ 1696c6d61269SAlan Cox if (port_status & DIGI_OVERRUN_ERROR) 16971da177e4SLinus Torvalds tty_insert_flip_char(tty, 0, TTY_OVERRUN); 16981da177e4SLinus Torvalds 16991da177e4SLinus Torvalds /* break takes precedence over parity, */ 17001da177e4SLinus Torvalds /* which takes precedence over framing errors */ 1701c6d61269SAlan Cox if (port_status & DIGI_BREAK_ERROR) 17021da177e4SLinus Torvalds flag = TTY_BREAK; 1703c6d61269SAlan Cox else if (port_status & DIGI_PARITY_ERROR) 17041da177e4SLinus Torvalds flag = TTY_PARITY; 1705c6d61269SAlan Cox else if (port_status & DIGI_FRAMING_ERROR) 17061da177e4SLinus Torvalds flag = TTY_FRAME; 17071da177e4SLinus Torvalds 170885d75107SGreg Kroah-Hartman /* data length is len-1 (one byte of len is port_status) */ 17091da177e4SLinus Torvalds --len; 17101da177e4SLinus Torvalds 171133f0f88fSAlan Cox len = tty_buffer_request_room(tty, len); 17121da177e4SLinus Torvalds if (len > 0) { 171333f0f88fSAlan Cox /* Hot path */ 171433f0f88fSAlan Cox if (flag == TTY_NORMAL) 171533f0f88fSAlan Cox tty_insert_flip_string(tty, data, len); 171633f0f88fSAlan Cox else { 171733f0f88fSAlan Cox for (i = 0; i < len; i++) 171841ad427dSAlan Cox tty_insert_flip_char(tty, 171941ad427dSAlan Cox data[i], flag); 172033f0f88fSAlan Cox } 17211da177e4SLinus Torvalds tty_flip_buffer_push(tty); 17221da177e4SLinus Torvalds } 17231da177e4SLinus Torvalds } 17241da177e4SLinus Torvalds spin_unlock(&priv->dp_port_lock); 17254a90f09bSAlan Cox tty_kref_put(tty); 17261da177e4SLinus Torvalds 1727c6d61269SAlan Cox if (opcode == DIGI_CMD_RECEIVE_DISABLE) 1728441b62c1SHarvey Harrison dbg("%s: got RECEIVE_DISABLE", __func__); 1729c6d61269SAlan Cox else if (opcode != DIGI_CMD_RECEIVE_DATA) 1730441b62c1SHarvey Harrison dbg("%s: unknown opcode: %d", __func__, opcode); 17311da177e4SLinus Torvalds 173241ad427dSAlan Cox return throttled ? 1 : 0; 17331da177e4SLinus Torvalds 17341da177e4SLinus Torvalds } 17351da177e4SLinus Torvalds 17361da177e4SLinus Torvalds 17371da177e4SLinus Torvalds /* 17381da177e4SLinus Torvalds * Digi Read OOB Callback 17391da177e4SLinus Torvalds * 17401da177e4SLinus Torvalds * Digi Read OOB Callback handles reads on the out of band port. 17411da177e4SLinus Torvalds * When called we know port and port->private are not NULL and 17421da177e4SLinus Torvalds * the port->serial is valid. It returns 0 if successful, and 17431da177e4SLinus Torvalds * -1 if the sanity checks failed. 17441da177e4SLinus Torvalds */ 17451da177e4SLinus Torvalds 17461da177e4SLinus Torvalds static int digi_read_oob_callback(struct urb *urb) 17471da177e4SLinus Torvalds { 17481da177e4SLinus Torvalds 1749cdc97792SMing Lei struct usb_serial_port *port = urb->context; 17501da177e4SLinus Torvalds struct usb_serial *serial = port->serial; 17514a90f09bSAlan Cox struct tty_struct *tty; 17521da177e4SLinus Torvalds struct digi_port *priv = usb_get_serial_port_data(port); 17531da177e4SLinus Torvalds int opcode, line, status, val; 17541da177e4SLinus Torvalds int i; 175541ad427dSAlan Cox unsigned int rts; 17561da177e4SLinus Torvalds 1757c6d61269SAlan Cox dbg("digi_read_oob_callback: port=%d, len=%d", 1758c6d61269SAlan Cox priv->dp_port_num, urb->actual_length); 17591da177e4SLinus Torvalds 17601da177e4SLinus Torvalds /* handle each oob command */ 17611da177e4SLinus Torvalds for (i = 0; i < urb->actual_length - 3;) { 17621da177e4SLinus Torvalds opcode = ((unsigned char *)urb->transfer_buffer)[i++]; 17631da177e4SLinus Torvalds line = ((unsigned char *)urb->transfer_buffer)[i++]; 17641da177e4SLinus Torvalds status = ((unsigned char *)urb->transfer_buffer)[i++]; 17651da177e4SLinus Torvalds val = ((unsigned char *)urb->transfer_buffer)[i++]; 17661da177e4SLinus Torvalds 17671da177e4SLinus Torvalds dbg("digi_read_oob_callback: opcode=%d, line=%d, status=%d, val=%d", 17681da177e4SLinus Torvalds opcode, line, status, val); 17691da177e4SLinus Torvalds 17701da177e4SLinus Torvalds if (status != 0 || line >= serial->type->num_ports) 17711da177e4SLinus Torvalds continue; 17721da177e4SLinus Torvalds 17731da177e4SLinus Torvalds port = serial->port[line]; 17741da177e4SLinus Torvalds 177541ad427dSAlan Cox priv = usb_get_serial_port_data(port); 177641ad427dSAlan Cox if (priv == NULL) 17771da177e4SLinus Torvalds return -1; 17781da177e4SLinus Torvalds 17794a90f09bSAlan Cox tty = tty_port_tty_get(&port->port); 178041ad427dSAlan Cox rts = 0; 178141ad427dSAlan Cox if (port->port.count) 17824a90f09bSAlan Cox rts = tty->termios->c_cflag & CRTSCTS; 178341ad427dSAlan Cox 17841da177e4SLinus Torvalds if (opcode == DIGI_CMD_READ_INPUT_SIGNALS) { 17851da177e4SLinus Torvalds spin_lock(&priv->dp_port_lock); 17861da177e4SLinus Torvalds /* convert from digi flags to termiox flags */ 17871da177e4SLinus Torvalds if (val & DIGI_READ_INPUT_SIGNALS_CTS) { 17881da177e4SLinus Torvalds priv->dp_modem_signals |= TIOCM_CTS; 17891da177e4SLinus Torvalds /* port must be open to use tty struct */ 179041ad427dSAlan Cox if (rts) { 17914a90f09bSAlan Cox tty->hw_stopped = 0; 17921da177e4SLinus Torvalds digi_wakeup_write(port); 17931da177e4SLinus Torvalds } 17941da177e4SLinus Torvalds } else { 17951da177e4SLinus Torvalds priv->dp_modem_signals &= ~TIOCM_CTS; 17961da177e4SLinus Torvalds /* port must be open to use tty struct */ 179741ad427dSAlan Cox if (rts) 17984a90f09bSAlan Cox tty->hw_stopped = 1; 17991da177e4SLinus Torvalds } 18001da177e4SLinus Torvalds if (val & DIGI_READ_INPUT_SIGNALS_DSR) 18011da177e4SLinus Torvalds priv->dp_modem_signals |= TIOCM_DSR; 18021da177e4SLinus Torvalds else 18031da177e4SLinus Torvalds priv->dp_modem_signals &= ~TIOCM_DSR; 18041da177e4SLinus Torvalds if (val & DIGI_READ_INPUT_SIGNALS_RI) 18051da177e4SLinus Torvalds priv->dp_modem_signals |= TIOCM_RI; 18061da177e4SLinus Torvalds else 18071da177e4SLinus Torvalds priv->dp_modem_signals &= ~TIOCM_RI; 18081da177e4SLinus Torvalds if (val & DIGI_READ_INPUT_SIGNALS_DCD) 18091da177e4SLinus Torvalds priv->dp_modem_signals |= TIOCM_CD; 18101da177e4SLinus Torvalds else 18111da177e4SLinus Torvalds priv->dp_modem_signals &= ~TIOCM_CD; 18121da177e4SLinus Torvalds 18131da177e4SLinus Torvalds wake_up_interruptible(&priv->dp_modem_change_wait); 18141da177e4SLinus Torvalds spin_unlock(&priv->dp_port_lock); 18151da177e4SLinus Torvalds } else if (opcode == DIGI_CMD_TRANSMIT_IDLE) { 18161da177e4SLinus Torvalds spin_lock(&priv->dp_port_lock); 18171da177e4SLinus Torvalds priv->dp_transmit_idle = 1; 18181da177e4SLinus Torvalds wake_up_interruptible(&priv->dp_transmit_idle_wait); 18191da177e4SLinus Torvalds spin_unlock(&priv->dp_port_lock); 18201da177e4SLinus Torvalds } else if (opcode == DIGI_CMD_IFLUSH_FIFO) { 182145f23f18SNishanth Aravamudan wake_up_interruptible(&priv->dp_flush_wait); 1822c6d61269SAlan Cox } 18234a90f09bSAlan Cox tty_kref_put(tty); 1824c6d61269SAlan Cox } 1825c6d61269SAlan Cox return 0; 18261da177e4SLinus Torvalds 18271da177e4SLinus Torvalds } 18281da177e4SLinus Torvalds 18291da177e4SLinus Torvalds static int __init digi_init(void) 18301da177e4SLinus Torvalds { 18311da177e4SLinus Torvalds int retval; 18321da177e4SLinus Torvalds retval = usb_serial_register(&digi_acceleport_2_device); 18331da177e4SLinus Torvalds if (retval) 18341da177e4SLinus Torvalds goto failed_acceleport_2_device; 18351da177e4SLinus Torvalds retval = usb_serial_register(&digi_acceleport_4_device); 18361da177e4SLinus Torvalds if (retval) 18371da177e4SLinus Torvalds goto failed_acceleport_4_device; 18381da177e4SLinus Torvalds retval = usb_register(&digi_driver); 18391da177e4SLinus Torvalds if (retval) 18401da177e4SLinus Torvalds goto failed_usb_register; 1841c197a8dbSGreg Kroah-Hartman printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":" 1842c197a8dbSGreg Kroah-Hartman DRIVER_DESC "\n"); 18431da177e4SLinus Torvalds return 0; 18441da177e4SLinus Torvalds failed_usb_register: 18451da177e4SLinus Torvalds usb_serial_deregister(&digi_acceleport_4_device); 18461da177e4SLinus Torvalds failed_acceleport_4_device: 18471da177e4SLinus Torvalds usb_serial_deregister(&digi_acceleport_2_device); 18481da177e4SLinus Torvalds failed_acceleport_2_device: 18491da177e4SLinus Torvalds return retval; 18501da177e4SLinus Torvalds } 18511da177e4SLinus Torvalds 18521da177e4SLinus Torvalds static void __exit digi_exit (void) 18531da177e4SLinus Torvalds { 18541da177e4SLinus Torvalds usb_deregister(&digi_driver); 18551da177e4SLinus Torvalds usb_serial_deregister(&digi_acceleport_2_device); 18561da177e4SLinus Torvalds usb_serial_deregister(&digi_acceleport_4_device); 18571da177e4SLinus Torvalds } 18581da177e4SLinus Torvalds 18591da177e4SLinus Torvalds 18601da177e4SLinus Torvalds module_init(digi_init); 18611da177e4SLinus Torvalds module_exit(digi_exit); 18621da177e4SLinus Torvalds 18631da177e4SLinus Torvalds 18641da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR); 18651da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC); 18661da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 18671da177e4SLinus Torvalds 18681da177e4SLinus Torvalds module_param(debug, bool, S_IRUGO | S_IWUSR); 18691da177e4SLinus Torvalds MODULE_PARM_DESC(debug, "Debug enabled or not"); 1870