xref: /openbmc/linux/drivers/usb/serial/digi_acceleport.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds *  Digi AccelePort USB-4 and USB-2 Serial Converters
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds *  Copyright 2000 by Digi International
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds *  Shamelessly based on Brian Warner's keyspan_pda.c and Greg Kroah-Hartman's
81da177e4SLinus Torvalds *  usb-serial driver.
91da177e4SLinus Torvalds *
101da177e4SLinus Torvalds *  Peter Berger (pberger@brimson.com)
111da177e4SLinus Torvalds *  Al Borchers (borchers@steinerpoint.com)
121da177e4SLinus Torvalds */
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #include <linux/kernel.h>
151da177e4SLinus Torvalds #include <linux/errno.h>
161da177e4SLinus Torvalds #include <linux/slab.h>
171da177e4SLinus Torvalds #include <linux/tty.h>
181da177e4SLinus Torvalds #include <linux/tty_driver.h>
191da177e4SLinus Torvalds #include <linux/tty_flip.h>
201da177e4SLinus Torvalds #include <linux/module.h>
211da177e4SLinus Torvalds #include <linux/spinlock.h>
2241ad427dSAlan Cox #include <linux/uaccess.h>
231da177e4SLinus Torvalds #include <linux/usb.h>
241da177e4SLinus Torvalds #include <linux/wait.h>
25174cd4b1SIngo Molnar #include <linux/sched/signal.h>
26a969888cSGreg Kroah-Hartman #include <linux/usb/serial.h>
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds /* Defines */
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #define DRIVER_AUTHOR "Peter Berger <pberger@brimson.com>, Al Borchers <borchers@steinerpoint.com>"
311da177e4SLinus Torvalds #define DRIVER_DESC "Digi AccelePort USB-2/USB-4 Serial Converter driver"
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds /* port output buffer length -- must be <= transfer buffer length - 2 */
341da177e4SLinus Torvalds /* so we can be sure to send the full buffer in one urb */
351da177e4SLinus Torvalds #define DIGI_OUT_BUF_SIZE		8
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds /* port input buffer length -- must be >= transfer buffer length - 3 */
381da177e4SLinus Torvalds /* so we can be sure to hold at least one full buffer from one urb */
391da177e4SLinus Torvalds #define DIGI_IN_BUF_SIZE		64
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds /* retry timeout while sleeping */
421da177e4SLinus Torvalds #define DIGI_RETRY_TIMEOUT		(HZ/10)
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds /* timeout while waiting for tty output to drain in close */
451da177e4SLinus Torvalds /* this delay is used twice in close, so the total delay could */
461da177e4SLinus Torvalds /* be twice this value */
471da177e4SLinus Torvalds #define DIGI_CLOSE_TIMEOUT		(5*HZ)
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds /* AccelePort USB Defines */
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds /* ids */
531da177e4SLinus Torvalds #define DIGI_VENDOR_ID			0x05c5
541da177e4SLinus Torvalds #define DIGI_2_ID			0x0002	/* USB-2 */
551da177e4SLinus Torvalds #define DIGI_4_ID			0x0004	/* USB-4 */
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds /* commands
581da177e4SLinus Torvalds  * "INB": can be used on the in-band endpoint
591da177e4SLinus Torvalds  * "OOB": can be used on the out-of-band endpoint
601da177e4SLinus Torvalds  */
611da177e4SLinus Torvalds #define DIGI_CMD_SET_BAUD_RATE			0	/* INB, OOB */
621da177e4SLinus Torvalds #define DIGI_CMD_SET_WORD_SIZE			1	/* INB, OOB */
631da177e4SLinus Torvalds #define DIGI_CMD_SET_PARITY			2	/* INB, OOB */
641da177e4SLinus Torvalds #define DIGI_CMD_SET_STOP_BITS			3	/* INB, OOB */
651da177e4SLinus Torvalds #define DIGI_CMD_SET_INPUT_FLOW_CONTROL		4	/* INB, OOB */
661da177e4SLinus Torvalds #define DIGI_CMD_SET_OUTPUT_FLOW_CONTROL	5	/* INB, OOB */
671da177e4SLinus Torvalds #define DIGI_CMD_SET_DTR_SIGNAL			6	/* INB, OOB */
681da177e4SLinus Torvalds #define DIGI_CMD_SET_RTS_SIGNAL			7	/* INB, OOB */
691da177e4SLinus Torvalds #define DIGI_CMD_READ_INPUT_SIGNALS		8	/*      OOB */
701da177e4SLinus Torvalds #define DIGI_CMD_IFLUSH_FIFO			9	/*      OOB */
711da177e4SLinus Torvalds #define DIGI_CMD_RECEIVE_ENABLE			10	/* INB, OOB */
721da177e4SLinus Torvalds #define DIGI_CMD_BREAK_CONTROL			11	/* INB, OOB */
731da177e4SLinus Torvalds #define DIGI_CMD_LOCAL_LOOPBACK			12	/* INB, OOB */
741da177e4SLinus Torvalds #define DIGI_CMD_TRANSMIT_IDLE			13	/* INB, OOB */
751da177e4SLinus Torvalds #define DIGI_CMD_READ_UART_REGISTER		14	/*      OOB */
761da177e4SLinus Torvalds #define DIGI_CMD_WRITE_UART_REGISTER		15	/* INB, OOB */
771da177e4SLinus Torvalds #define DIGI_CMD_AND_UART_REGISTER		16	/* INB, OOB */
781da177e4SLinus Torvalds #define DIGI_CMD_OR_UART_REGISTER		17	/* INB, OOB */
791da177e4SLinus Torvalds #define DIGI_CMD_SEND_DATA			18	/* INB      */
801da177e4SLinus Torvalds #define DIGI_CMD_RECEIVE_DATA			19	/* INB      */
811da177e4SLinus Torvalds #define DIGI_CMD_RECEIVE_DISABLE		20	/* INB      */
821da177e4SLinus Torvalds #define DIGI_CMD_GET_PORT_TYPE			21	/*      OOB */
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds /* baud rates */
851da177e4SLinus Torvalds #define DIGI_BAUD_50				0
861da177e4SLinus Torvalds #define DIGI_BAUD_75				1
871da177e4SLinus Torvalds #define DIGI_BAUD_110				2
881da177e4SLinus Torvalds #define DIGI_BAUD_150				3
891da177e4SLinus Torvalds #define DIGI_BAUD_200				4
901da177e4SLinus Torvalds #define DIGI_BAUD_300				5
911da177e4SLinus Torvalds #define DIGI_BAUD_600				6
921da177e4SLinus Torvalds #define DIGI_BAUD_1200				7
931da177e4SLinus Torvalds #define DIGI_BAUD_1800				8
941da177e4SLinus Torvalds #define DIGI_BAUD_2400				9
951da177e4SLinus Torvalds #define DIGI_BAUD_4800				10
961da177e4SLinus Torvalds #define DIGI_BAUD_7200				11
971da177e4SLinus Torvalds #define DIGI_BAUD_9600				12
981da177e4SLinus Torvalds #define DIGI_BAUD_14400				13
991da177e4SLinus Torvalds #define DIGI_BAUD_19200				14
1001da177e4SLinus Torvalds #define DIGI_BAUD_28800				15
1011da177e4SLinus Torvalds #define DIGI_BAUD_38400				16
1021da177e4SLinus Torvalds #define DIGI_BAUD_57600				17
1031da177e4SLinus Torvalds #define DIGI_BAUD_76800				18
1041da177e4SLinus Torvalds #define DIGI_BAUD_115200			19
1051da177e4SLinus Torvalds #define DIGI_BAUD_153600			20
1061da177e4SLinus Torvalds #define DIGI_BAUD_230400			21
1071da177e4SLinus Torvalds #define DIGI_BAUD_460800			22
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds /* arguments */
1101da177e4SLinus Torvalds #define DIGI_WORD_SIZE_5			0
1111da177e4SLinus Torvalds #define DIGI_WORD_SIZE_6			1
1121da177e4SLinus Torvalds #define DIGI_WORD_SIZE_7			2
1131da177e4SLinus Torvalds #define DIGI_WORD_SIZE_8			3
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds #define DIGI_PARITY_NONE			0
1161da177e4SLinus Torvalds #define DIGI_PARITY_ODD				1
1171da177e4SLinus Torvalds #define DIGI_PARITY_EVEN			2
1181da177e4SLinus Torvalds #define DIGI_PARITY_MARK			3
1191da177e4SLinus Torvalds #define DIGI_PARITY_SPACE			4
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds #define DIGI_STOP_BITS_1			0
1221da177e4SLinus Torvalds #define DIGI_STOP_BITS_2			1
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds #define DIGI_INPUT_FLOW_CONTROL_XON_XOFF	1
1251da177e4SLinus Torvalds #define DIGI_INPUT_FLOW_CONTROL_RTS		2
1261da177e4SLinus Torvalds #define DIGI_INPUT_FLOW_CONTROL_DTR		4
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds #define DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF	1
1291da177e4SLinus Torvalds #define DIGI_OUTPUT_FLOW_CONTROL_CTS		2
1301da177e4SLinus Torvalds #define DIGI_OUTPUT_FLOW_CONTROL_DSR		4
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds #define DIGI_DTR_INACTIVE			0
1331da177e4SLinus Torvalds #define DIGI_DTR_ACTIVE				1
1341da177e4SLinus Torvalds #define DIGI_DTR_INPUT_FLOW_CONTROL		2
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds #define DIGI_RTS_INACTIVE			0
1371da177e4SLinus Torvalds #define DIGI_RTS_ACTIVE				1
1381da177e4SLinus Torvalds #define DIGI_RTS_INPUT_FLOW_CONTROL		2
1391da177e4SLinus Torvalds #define DIGI_RTS_TOGGLE				3
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds #define DIGI_FLUSH_TX				1
1421da177e4SLinus Torvalds #define DIGI_FLUSH_RX				2
1431da177e4SLinus Torvalds #define DIGI_RESUME_TX				4 /* clears xoff condition */
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds #define DIGI_TRANSMIT_NOT_IDLE			0
1461da177e4SLinus Torvalds #define DIGI_TRANSMIT_IDLE			1
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds #define DIGI_DISABLE				0
1491da177e4SLinus Torvalds #define DIGI_ENABLE				1
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds #define DIGI_DEASSERT				0
1521da177e4SLinus Torvalds #define DIGI_ASSERT				1
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds /* in band status codes */
1551da177e4SLinus Torvalds #define DIGI_OVERRUN_ERROR			4
1561da177e4SLinus Torvalds #define DIGI_PARITY_ERROR			8
1571da177e4SLinus Torvalds #define DIGI_FRAMING_ERROR			16
1581da177e4SLinus Torvalds #define DIGI_BREAK_ERROR			32
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds /* out of band status */
1611da177e4SLinus Torvalds #define DIGI_NO_ERROR				0
1621da177e4SLinus Torvalds #define DIGI_BAD_FIRST_PARAMETER		1
1631da177e4SLinus Torvalds #define DIGI_BAD_SECOND_PARAMETER		2
1641da177e4SLinus Torvalds #define DIGI_INVALID_LINE			3
1651da177e4SLinus Torvalds #define DIGI_INVALID_OPCODE			4
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds /* input signals */
1681da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_SLOT		1
1691da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_ERR		2
1701da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_BUSY		4
1711da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_PE		8
1721da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_CTS		16
1731da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_DSR		32
1741da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_RI		64
1751da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_DCD		128
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds /* Structures */
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds struct digi_serial {
1811da177e4SLinus Torvalds 	spinlock_t ds_serial_lock;
1821da177e4SLinus Torvalds 	struct usb_serial_port *ds_oob_port;	/* out-of-band port */
1831da177e4SLinus Torvalds 	int ds_oob_port_num;			/* index of out-of-band port */
1841da177e4SLinus Torvalds 	int ds_device_started;
1851da177e4SLinus Torvalds };
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds struct digi_port {
1881da177e4SLinus Torvalds 	spinlock_t dp_port_lock;
1891da177e4SLinus Torvalds 	int dp_port_num;
1901da177e4SLinus Torvalds 	int dp_out_buf_len;
1911da177e4SLinus Torvalds 	unsigned char dp_out_buf[DIGI_OUT_BUF_SIZE];
1921da177e4SLinus Torvalds 	int dp_write_urb_in_use;
1931da177e4SLinus Torvalds 	unsigned int dp_modem_signals;
1941da177e4SLinus Torvalds 	int dp_transmit_idle;
1951da177e4SLinus Torvalds 	wait_queue_head_t dp_transmit_idle_wait;
1961da177e4SLinus Torvalds 	int dp_throttled;
1971da177e4SLinus Torvalds 	int dp_throttle_restart;
1981da177e4SLinus Torvalds 	wait_queue_head_t dp_flush_wait;
1991da177e4SLinus Torvalds 	wait_queue_head_t dp_close_wait;	/* wait queue for close */
200179dfb95SJohan Hovold 	wait_queue_head_t write_wait;
201c4028958SDavid Howells 	struct usb_serial_port *dp_port;
2021da177e4SLinus Torvalds };
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds /* Local Function Declarations */
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds static int digi_write_oob_command(struct usb_serial_port *port,
2081da177e4SLinus Torvalds 	unsigned char *buf, int count, int interruptible);
2091da177e4SLinus Torvalds static int digi_write_inb_command(struct usb_serial_port *port,
2101da177e4SLinus Torvalds 	unsigned char *buf, int count, unsigned long timeout);
2111da177e4SLinus Torvalds static int digi_set_modem_signals(struct usb_serial_port *port,
2121da177e4SLinus Torvalds 	unsigned int modem_signals, int interruptible);
2131da177e4SLinus Torvalds static int digi_transmit_idle(struct usb_serial_port *port,
2141da177e4SLinus Torvalds 	unsigned long timeout);
21595da310eSAlan Cox static void digi_rx_throttle(struct tty_struct *tty);
21695da310eSAlan Cox static void digi_rx_unthrottle(struct tty_struct *tty);
21795da310eSAlan Cox static void digi_set_termios(struct tty_struct *tty,
218f6d47fe5SIlpo Järvinen 			     struct usb_serial_port *port,
219f6d47fe5SIlpo Järvinen 			     const struct ktermios *old_termios);
220*6ff58ae1SJohan Hovold static int digi_break_ctl(struct tty_struct *tty, int break_state);
22160b33c13SAlan Cox static int digi_tiocmget(struct tty_struct *tty);
22220b9d177SAlan Cox static int digi_tiocmset(struct tty_struct *tty, unsigned int set,
22320b9d177SAlan Cox 		unsigned int clear);
22495da310eSAlan Cox static int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
22595da310eSAlan Cox 		const unsigned char *buf, int count);
2267d12e780SDavid Howells static void digi_write_bulk_callback(struct urb *urb);
22794cc7aeaSJiri Slaby static unsigned int digi_write_room(struct tty_struct *tty);
228155591d3SJiri Slaby static unsigned int digi_chars_in_buffer(struct tty_struct *tty);
229a509a7e4SAlan Cox static int digi_open(struct tty_struct *tty, struct usb_serial_port *port);
230335f8514SAlan Cox static void digi_close(struct usb_serial_port *port);
231335f8514SAlan Cox static void digi_dtr_rts(struct usb_serial_port *port, int on);
2321da177e4SLinus Torvalds static int digi_startup_device(struct usb_serial *serial);
2331da177e4SLinus Torvalds static int digi_startup(struct usb_serial *serial);
234f9c99bb8SAlan Stern static void digi_disconnect(struct usb_serial *serial);
235f9c99bb8SAlan Stern static void digi_release(struct usb_serial *serial);
236fb44ff85SJohan Hovold static int digi_port_probe(struct usb_serial_port *port);
237c5d1448fSUwe Kleine-König static void digi_port_remove(struct usb_serial_port *port);
2387d12e780SDavid Howells static void digi_read_bulk_callback(struct urb *urb);
2391da177e4SLinus Torvalds static int digi_read_inb_callback(struct urb *urb);
2401da177e4SLinus Torvalds static int digi_read_oob_callback(struct urb *urb);
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 
2437d40d7e8SNémeth Márton static const struct usb_device_id id_table_combined[] = {
2441da177e4SLinus Torvalds 	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
2451da177e4SLinus Torvalds 	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
2461da177e4SLinus Torvalds 	{ }						/* Terminating entry */
2471da177e4SLinus Torvalds };
2481da177e4SLinus Torvalds 
2497d40d7e8SNémeth Márton static const struct usb_device_id id_table_2[] = {
2501da177e4SLinus Torvalds 	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
2511da177e4SLinus Torvalds 	{ }						/* Terminating entry */
2521da177e4SLinus Torvalds };
2531da177e4SLinus Torvalds 
2547d40d7e8SNémeth Márton static const struct usb_device_id id_table_4[] = {
2551da177e4SLinus Torvalds 	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
2561da177e4SLinus Torvalds 	{ }						/* Terminating entry */
2571da177e4SLinus Torvalds };
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds MODULE_DEVICE_TABLE(usb, id_table_combined);
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds /* device info needed for the Digi serial converter */
2621da177e4SLinus Torvalds 
263ea65370dSGreg Kroah-Hartman static struct usb_serial_driver digi_acceleport_2_device = {
26418fcac35SGreg Kroah-Hartman 	.driver = {
2651da177e4SLinus Torvalds 		.owner =		THIS_MODULE,
266269bda1cSGreg Kroah-Hartman 		.name =			"digi_2",
26718fcac35SGreg Kroah-Hartman 	},
268269bda1cSGreg Kroah-Hartman 	.description =			"Digi 2 port USB adapter",
2691da177e4SLinus Torvalds 	.id_table =			id_table_2,
2701da177e4SLinus Torvalds 	.num_ports =			3,
271e7d6507eSJohan Hovold 	.num_bulk_in =			4,
272e7d6507eSJohan Hovold 	.num_bulk_out =			4,
2731da177e4SLinus Torvalds 	.open =				digi_open,
2741da177e4SLinus Torvalds 	.close =			digi_close,
275335f8514SAlan Cox 	.dtr_rts =			digi_dtr_rts,
2761da177e4SLinus Torvalds 	.write =			digi_write,
2771da177e4SLinus Torvalds 	.write_room =			digi_write_room,
2781da177e4SLinus Torvalds 	.write_bulk_callback = 		digi_write_bulk_callback,
2791da177e4SLinus Torvalds 	.read_bulk_callback =		digi_read_bulk_callback,
2801da177e4SLinus Torvalds 	.chars_in_buffer =		digi_chars_in_buffer,
2811da177e4SLinus Torvalds 	.throttle =			digi_rx_throttle,
2821da177e4SLinus Torvalds 	.unthrottle =			digi_rx_unthrottle,
2831da177e4SLinus Torvalds 	.set_termios =			digi_set_termios,
2841da177e4SLinus Torvalds 	.break_ctl =			digi_break_ctl,
2851da177e4SLinus Torvalds 	.tiocmget =			digi_tiocmget,
2861da177e4SLinus Torvalds 	.tiocmset =			digi_tiocmset,
2871da177e4SLinus Torvalds 	.attach =			digi_startup,
288f9c99bb8SAlan Stern 	.disconnect =			digi_disconnect,
289f9c99bb8SAlan Stern 	.release =			digi_release,
290fb44ff85SJohan Hovold 	.port_probe =			digi_port_probe,
291fb44ff85SJohan Hovold 	.port_remove =			digi_port_remove,
2921da177e4SLinus Torvalds };
2931da177e4SLinus Torvalds 
294ea65370dSGreg Kroah-Hartman static struct usb_serial_driver digi_acceleport_4_device = {
29518fcac35SGreg Kroah-Hartman 	.driver = {
2961da177e4SLinus Torvalds 		.owner =		THIS_MODULE,
297269bda1cSGreg Kroah-Hartman 		.name =			"digi_4",
29818fcac35SGreg Kroah-Hartman 	},
299269bda1cSGreg Kroah-Hartman 	.description =			"Digi 4 port USB adapter",
3001da177e4SLinus Torvalds 	.id_table =			id_table_4,
3011da177e4SLinus Torvalds 	.num_ports =			4,
302e7d6507eSJohan Hovold 	.num_bulk_in =			5,
303e7d6507eSJohan Hovold 	.num_bulk_out =			5,
3041da177e4SLinus Torvalds 	.open =				digi_open,
3051da177e4SLinus Torvalds 	.close =			digi_close,
3061da177e4SLinus Torvalds 	.write =			digi_write,
3071da177e4SLinus Torvalds 	.write_room =			digi_write_room,
3081da177e4SLinus Torvalds 	.write_bulk_callback = 		digi_write_bulk_callback,
3091da177e4SLinus Torvalds 	.read_bulk_callback =		digi_read_bulk_callback,
3101da177e4SLinus Torvalds 	.chars_in_buffer =		digi_chars_in_buffer,
3111da177e4SLinus Torvalds 	.throttle =			digi_rx_throttle,
3121da177e4SLinus Torvalds 	.unthrottle =			digi_rx_unthrottle,
3131da177e4SLinus Torvalds 	.set_termios =			digi_set_termios,
3141da177e4SLinus Torvalds 	.break_ctl =			digi_break_ctl,
3151da177e4SLinus Torvalds 	.tiocmget =			digi_tiocmget,
3161da177e4SLinus Torvalds 	.tiocmset =			digi_tiocmset,
3171da177e4SLinus Torvalds 	.attach =			digi_startup,
318f9c99bb8SAlan Stern 	.disconnect =			digi_disconnect,
319f9c99bb8SAlan Stern 	.release =			digi_release,
320fb44ff85SJohan Hovold 	.port_probe =			digi_port_probe,
321fb44ff85SJohan Hovold 	.port_remove =			digi_port_remove,
3221da177e4SLinus Torvalds };
3231da177e4SLinus Torvalds 
32497b6b6d2SAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
32597b6b6d2SAlan Stern 	&digi_acceleport_2_device, &digi_acceleport_4_device, NULL
32697b6b6d2SAlan Stern };
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds /* Functions */
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds /*
3311da177e4SLinus Torvalds  *  Cond Wait Interruptible Timeout Irqrestore
3321da177e4SLinus Torvalds  *
3331da177e4SLinus Torvalds  *  Do spin_unlock_irqrestore and interruptible_sleep_on_timeout
3341da177e4SLinus Torvalds  *  so that wake ups are not lost if they occur between the unlock
3351da177e4SLinus Torvalds  *  and the sleep.  In other words, spin_unlock_irqrestore and
3361da177e4SLinus Torvalds  *  interruptible_sleep_on_timeout are "atomic" with respect to
3371da177e4SLinus Torvalds  *  wake ups.  This is used to implement condition variables.
33845f23f18SNishanth Aravamudan  *
33945f23f18SNishanth Aravamudan  *  interruptible_sleep_on_timeout is deprecated and has been replaced
34045f23f18SNishanth Aravamudan  *  with the equivalent code.
3411da177e4SLinus Torvalds  */
3421da177e4SLinus Torvalds 
cond_wait_interruptible_timeout_irqrestore(wait_queue_head_t * q,long timeout,spinlock_t * lock,unsigned long flags)343c6d61269SAlan Cox static long cond_wait_interruptible_timeout_irqrestore(
3441da177e4SLinus Torvalds 	wait_queue_head_t *q, long timeout,
3451da177e4SLinus Torvalds 	spinlock_t *lock, unsigned long flags)
3465a33956aSHarvey Harrison __releases(lock)
3471da177e4SLinus Torvalds {
3481da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
3491da177e4SLinus Torvalds 
35045f23f18SNishanth Aravamudan 	prepare_to_wait(q, &wait, TASK_INTERRUPTIBLE);
3511da177e4SLinus Torvalds 	spin_unlock_irqrestore(lock, flags);
3521da177e4SLinus Torvalds 	timeout = schedule_timeout(timeout);
3531da177e4SLinus Torvalds 	finish_wait(q, &wait);
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	return timeout;
3561da177e4SLinus Torvalds }
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds /*
3591da177e4SLinus Torvalds  *  Digi Write OOB Command
3601da177e4SLinus Torvalds  *
3611da177e4SLinus Torvalds  *  Write commands on the out of band port.  Commands are 4
3621da177e4SLinus Torvalds  *  bytes each, multiple commands can be sent at once, and
3631da177e4SLinus Torvalds  *  no command will be split across USB packets.  Returns 0
3641da177e4SLinus Torvalds  *  if successful, -EINTR if interrupted while sleeping and
3651da177e4SLinus Torvalds  *  the interruptible flag is true, or a negative error
3661da177e4SLinus Torvalds  *  returned by usb_submit_urb.
3671da177e4SLinus Torvalds  */
3681da177e4SLinus Torvalds 
digi_write_oob_command(struct usb_serial_port * port,unsigned char * buf,int count,int interruptible)3691da177e4SLinus Torvalds static int digi_write_oob_command(struct usb_serial_port *port,
3701da177e4SLinus Torvalds 	unsigned char *buf, int count, int interruptible)
3711da177e4SLinus Torvalds {
3721da177e4SLinus Torvalds 	int ret = 0;
3731da177e4SLinus Torvalds 	int len;
3741da177e4SLinus Torvalds 	struct usb_serial_port *oob_port = (struct usb_serial_port *)((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
3751da177e4SLinus Torvalds 	struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
37617cd3a10SJohan Hovold 	unsigned long flags;
3771da177e4SLinus Torvalds 
378f9dfbebbSGreg Kroah-Hartman 	dev_dbg(&port->dev,
379f9dfbebbSGreg Kroah-Hartman 		"digi_write_oob_command: TOP: port=%d, count=%d\n",
380f9dfbebbSGreg Kroah-Hartman 		oob_priv->dp_port_num, count);
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
3831da177e4SLinus Torvalds 	while (count > 0) {
38450de36f7SGreg Kroah-Hartman 		while (oob_priv->dp_write_urb_in_use) {
3851da177e4SLinus Torvalds 			cond_wait_interruptible_timeout_irqrestore(
386179dfb95SJohan Hovold 				&oob_priv->write_wait, DIGI_RETRY_TIMEOUT,
3871da177e4SLinus Torvalds 				&oob_priv->dp_port_lock, flags);
388c6d61269SAlan Cox 			if (interruptible && signal_pending(current))
389c6d61269SAlan Cox 				return -EINTR;
3901da177e4SLinus Torvalds 			spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
3911da177e4SLinus Torvalds 		}
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 		/* len must be a multiple of 4, so commands are not split */
3941da177e4SLinus Torvalds 		len = min(count, oob_port->bulk_out_size);
3951da177e4SLinus Torvalds 		if (len > 4)
3961da177e4SLinus Torvalds 			len &= ~3;
3971da177e4SLinus Torvalds 		memcpy(oob_port->write_urb->transfer_buffer, buf, len);
3981da177e4SLinus Torvalds 		oob_port->write_urb->transfer_buffer_length = len;
39941ad427dSAlan Cox 		ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
40041ad427dSAlan Cox 		if (ret == 0) {
4011da177e4SLinus Torvalds 			oob_priv->dp_write_urb_in_use = 1;
4021da177e4SLinus Torvalds 			count -= len;
4031da177e4SLinus Torvalds 			buf += len;
4041da177e4SLinus Torvalds 		}
4051da177e4SLinus Torvalds 	}
4061da177e4SLinus Torvalds 	spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
407c6d61269SAlan Cox 	if (ret)
408194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n",
409194343d9SGreg Kroah-Hartman 			__func__, ret);
410c6d61269SAlan Cox 	return ret;
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds }
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds /*
4161da177e4SLinus Torvalds  *  Digi Write In Band Command
4171da177e4SLinus Torvalds  *
4181da177e4SLinus Torvalds  *  Write commands on the given port.  Commands are 4
4191da177e4SLinus Torvalds  *  bytes each, multiple commands can be sent at once, and
4201da177e4SLinus Torvalds  *  no command will be split across USB packets.  If timeout
4211da177e4SLinus Torvalds  *  is non-zero, write in band command will return after
4221da177e4SLinus Torvalds  *  waiting unsuccessfully for the URB status to clear for
4231da177e4SLinus Torvalds  *  timeout ticks.  Returns 0 if successful, or a negative
4241da177e4SLinus Torvalds  *  error returned by digi_write.
4251da177e4SLinus Torvalds  */
4261da177e4SLinus Torvalds 
digi_write_inb_command(struct usb_serial_port * port,unsigned char * buf,int count,unsigned long timeout)4271da177e4SLinus Torvalds static int digi_write_inb_command(struct usb_serial_port *port,
4281da177e4SLinus Torvalds 	unsigned char *buf, int count, unsigned long timeout)
4291da177e4SLinus Torvalds {
4301da177e4SLinus Torvalds 	int ret = 0;
4311da177e4SLinus Torvalds 	int len;
4321da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
4331da177e4SLinus Torvalds 	unsigned char *data = port->write_urb->transfer_buffer;
43417cd3a10SJohan Hovold 	unsigned long flags;
4351da177e4SLinus Torvalds 
436f9dfbebbSGreg Kroah-Hartman 	dev_dbg(&port->dev, "digi_write_inb_command: TOP: port=%d, count=%d\n",
437c6d61269SAlan Cox 		priv->dp_port_num, count);
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	if (timeout)
4401da177e4SLinus Torvalds 		timeout += jiffies;
4411da177e4SLinus Torvalds 	else
4421da177e4SLinus Torvalds 		timeout = ULONG_MAX;
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
4451da177e4SLinus Torvalds 	while (count > 0 && ret == 0) {
44650de36f7SGreg Kroah-Hartman 		while (priv->dp_write_urb_in_use &&
44750de36f7SGreg Kroah-Hartman 		       time_before(jiffies, timeout)) {
4481da177e4SLinus Torvalds 			cond_wait_interruptible_timeout_irqrestore(
449179dfb95SJohan Hovold 				&priv->write_wait, DIGI_RETRY_TIMEOUT,
4501da177e4SLinus Torvalds 				&priv->dp_port_lock, flags);
451c6d61269SAlan Cox 			if (signal_pending(current))
452c6d61269SAlan Cox 				return -EINTR;
4531da177e4SLinus Torvalds 			spin_lock_irqsave(&priv->dp_port_lock, flags);
4541da177e4SLinus Torvalds 		}
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 		/* len must be a multiple of 4 and small enough to */
4571da177e4SLinus Torvalds 		/* guarantee the write will send buffered data first, */
4581da177e4SLinus Torvalds 		/* so commands are in order with data and not split */
4591da177e4SLinus Torvalds 		len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
4601da177e4SLinus Torvalds 		if (len > 4)
4611da177e4SLinus Torvalds 			len &= ~3;
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 		/* write any buffered data first */
4641da177e4SLinus Torvalds 		if (priv->dp_out_buf_len > 0) {
4651da177e4SLinus Torvalds 			data[0] = DIGI_CMD_SEND_DATA;
4661da177e4SLinus Torvalds 			data[1] = priv->dp_out_buf_len;
4671da177e4SLinus Torvalds 			memcpy(data + 2, priv->dp_out_buf,
4681da177e4SLinus Torvalds 				priv->dp_out_buf_len);
4691da177e4SLinus Torvalds 			memcpy(data + 2 + priv->dp_out_buf_len, buf, len);
4701da177e4SLinus Torvalds 			port->write_urb->transfer_buffer_length
4711da177e4SLinus Torvalds 				= priv->dp_out_buf_len + 2 + len;
4721da177e4SLinus Torvalds 		} else {
4731da177e4SLinus Torvalds 			memcpy(data, buf, len);
4741da177e4SLinus Torvalds 			port->write_urb->transfer_buffer_length = len;
4751da177e4SLinus Torvalds 		}
4761da177e4SLinus Torvalds 
47741ad427dSAlan Cox 		ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
47841ad427dSAlan Cox 		if (ret == 0) {
4791da177e4SLinus Torvalds 			priv->dp_write_urb_in_use = 1;
4801da177e4SLinus Torvalds 			priv->dp_out_buf_len = 0;
4811da177e4SLinus Torvalds 			count -= len;
4821da177e4SLinus Torvalds 			buf += len;
4831da177e4SLinus Torvalds 		}
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	}
4861da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
4871da177e4SLinus Torvalds 
488c6d61269SAlan Cox 	if (ret)
489194343d9SGreg Kroah-Hartman 		dev_err(&port->dev,
490194343d9SGreg Kroah-Hartman 			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
491441b62c1SHarvey Harrison 			__func__, ret, priv->dp_port_num);
492c6d61269SAlan Cox 	return ret;
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds /*
4971da177e4SLinus Torvalds  *  Digi Set Modem Signals
4981da177e4SLinus Torvalds  *
4991da177e4SLinus Torvalds  *  Sets or clears DTR and RTS on the port, according to the
5001da177e4SLinus Torvalds  *  modem_signals argument.  Use TIOCM_DTR and TIOCM_RTS flags
5011da177e4SLinus Torvalds  *  for the modem_signals argument.  Returns 0 if successful,
5021da177e4SLinus Torvalds  *  -EINTR if interrupted while sleeping, or a non-zero error
5031da177e4SLinus Torvalds  *  returned by usb_submit_urb.
5041da177e4SLinus Torvalds  */
5051da177e4SLinus Torvalds 
digi_set_modem_signals(struct usb_serial_port * port,unsigned int modem_signals,int interruptible)5061da177e4SLinus Torvalds static int digi_set_modem_signals(struct usb_serial_port *port,
5071da177e4SLinus Torvalds 	unsigned int modem_signals, int interruptible)
5081da177e4SLinus Torvalds {
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	int ret;
5111da177e4SLinus Torvalds 	struct digi_port *port_priv = usb_get_serial_port_data(port);
5121da177e4SLinus Torvalds 	struct usb_serial_port *oob_port = (struct usb_serial_port *) ((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
5131da177e4SLinus Torvalds 	struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
5141da177e4SLinus Torvalds 	unsigned char *data = oob_port->write_urb->transfer_buffer;
51517cd3a10SJohan Hovold 	unsigned long flags;
5161da177e4SLinus Torvalds 
517f9dfbebbSGreg Kroah-Hartman 	dev_dbg(&port->dev,
518f9dfbebbSGreg Kroah-Hartman 		"digi_set_modem_signals: TOP: port=%d, modem_signals=0x%x\n",
5191da177e4SLinus Torvalds 		port_priv->dp_port_num, modem_signals);
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
5221da177e4SLinus Torvalds 	spin_lock(&port_priv->dp_port_lock);
5231da177e4SLinus Torvalds 
52450de36f7SGreg Kroah-Hartman 	while (oob_priv->dp_write_urb_in_use) {
5251da177e4SLinus Torvalds 		spin_unlock(&port_priv->dp_port_lock);
5261da177e4SLinus Torvalds 		cond_wait_interruptible_timeout_irqrestore(
527179dfb95SJohan Hovold 			&oob_priv->write_wait, DIGI_RETRY_TIMEOUT,
5281da177e4SLinus Torvalds 			&oob_priv->dp_port_lock, flags);
529c6d61269SAlan Cox 		if (interruptible && signal_pending(current))
530c6d61269SAlan Cox 			return -EINTR;
5311da177e4SLinus Torvalds 		spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
5321da177e4SLinus Torvalds 		spin_lock(&port_priv->dp_port_lock);
5331da177e4SLinus Torvalds 	}
5341da177e4SLinus Torvalds 	data[0] = DIGI_CMD_SET_DTR_SIGNAL;
5351da177e4SLinus Torvalds 	data[1] = port_priv->dp_port_num;
53641ad427dSAlan Cox 	data[2] = (modem_signals & TIOCM_DTR) ?
53741ad427dSAlan Cox 					DIGI_DTR_ACTIVE : DIGI_DTR_INACTIVE;
5381da177e4SLinus Torvalds 	data[3] = 0;
5391da177e4SLinus Torvalds 	data[4] = DIGI_CMD_SET_RTS_SIGNAL;
5401da177e4SLinus Torvalds 	data[5] = port_priv->dp_port_num;
54141ad427dSAlan Cox 	data[6] = (modem_signals & TIOCM_RTS) ?
54241ad427dSAlan Cox 					DIGI_RTS_ACTIVE : DIGI_RTS_INACTIVE;
5431da177e4SLinus Torvalds 	data[7] = 0;
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	oob_port->write_urb->transfer_buffer_length = 8;
5461da177e4SLinus Torvalds 
54741ad427dSAlan Cox 	ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
54841ad427dSAlan Cox 	if (ret == 0) {
5491da177e4SLinus Torvalds 		oob_priv->dp_write_urb_in_use = 1;
550a7f9f290SJohan Hovold 		port_priv->dp_modem_signals &= ~(TIOCM_DTR | TIOCM_RTS);
551a7f9f290SJohan Hovold 		port_priv->dp_modem_signals |=
552a7f9f290SJohan Hovold 				modem_signals & (TIOCM_DTR | TIOCM_RTS);
5531da177e4SLinus Torvalds 	}
5541da177e4SLinus Torvalds 	spin_unlock(&port_priv->dp_port_lock);
5551da177e4SLinus Torvalds 	spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
556c6d61269SAlan Cox 	if (ret)
557194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n",
558194343d9SGreg Kroah-Hartman 			__func__, ret);
559c6d61269SAlan Cox 	return ret;
5601da177e4SLinus Torvalds }
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds /*
5631da177e4SLinus Torvalds  *  Digi Transmit Idle
5641da177e4SLinus Torvalds  *
5651da177e4SLinus Torvalds  *  Digi transmit idle waits, up to timeout ticks, for the transmitter
5661da177e4SLinus Torvalds  *  to go idle.  It returns 0 if successful or a negative error.
5671da177e4SLinus Torvalds  *
5681da177e4SLinus Torvalds  *  There are race conditions here if more than one process is calling
5691da177e4SLinus Torvalds  *  digi_transmit_idle on the same port at the same time.  However, this
5701da177e4SLinus Torvalds  *  is only called from close, and only one process can be in close on a
5711da177e4SLinus Torvalds  *  port at a time, so its ok.
5721da177e4SLinus Torvalds  */
5731da177e4SLinus Torvalds 
digi_transmit_idle(struct usb_serial_port * port,unsigned long timeout)5741da177e4SLinus Torvalds static int digi_transmit_idle(struct usb_serial_port *port,
5751da177e4SLinus Torvalds 	unsigned long timeout)
5761da177e4SLinus Torvalds {
5771da177e4SLinus Torvalds 	int ret;
5781da177e4SLinus Torvalds 	unsigned char buf[2];
5791da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
58017cd3a10SJohan Hovold 	unsigned long flags;
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
5831da177e4SLinus Torvalds 	priv->dp_transmit_idle = 0;
5841da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	buf[0] = DIGI_CMD_TRANSMIT_IDLE;
5871da177e4SLinus Torvalds 	buf[1] = 0;
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 	timeout += jiffies;
5901da177e4SLinus Torvalds 
59141ad427dSAlan Cox 	ret = digi_write_inb_command(port, buf, 2, timeout - jiffies);
59241ad427dSAlan Cox 	if (ret != 0)
593c6d61269SAlan Cox 		return ret;
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	while (time_before(jiffies, timeout) && !priv->dp_transmit_idle) {
5981da177e4SLinus Torvalds 		cond_wait_interruptible_timeout_irqrestore(
5991da177e4SLinus Torvalds 			&priv->dp_transmit_idle_wait, DIGI_RETRY_TIMEOUT,
6001da177e4SLinus Torvalds 			&priv->dp_port_lock, flags);
601c6d61269SAlan Cox 		if (signal_pending(current))
602c6d61269SAlan Cox 			return -EINTR;
6031da177e4SLinus Torvalds 		spin_lock_irqsave(&priv->dp_port_lock, flags);
6041da177e4SLinus Torvalds 	}
6051da177e4SLinus Torvalds 	priv->dp_transmit_idle = 0;
6061da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
607c6d61269SAlan Cox 	return 0;
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds }
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 
digi_rx_throttle(struct tty_struct * tty)61295da310eSAlan Cox static void digi_rx_throttle(struct tty_struct *tty)
6131da177e4SLinus Torvalds {
6141da177e4SLinus Torvalds 	unsigned long flags;
61595da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
6161da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 	/* stop receiving characters by not resubmitting the read urb */
6191da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
6201da177e4SLinus Torvalds 	priv->dp_throttled = 1;
6211da177e4SLinus Torvalds 	priv->dp_throttle_restart = 0;
6221da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
6231da177e4SLinus Torvalds }
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 
digi_rx_unthrottle(struct tty_struct * tty)62695da310eSAlan Cox static void digi_rx_unthrottle(struct tty_struct *tty)
6271da177e4SLinus Torvalds {
6281da177e4SLinus Torvalds 	int ret = 0;
6291da177e4SLinus Torvalds 	unsigned long flags;
63095da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
6311da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 	/* restart read chain */
6365833041fSJohan Hovold 	if (priv->dp_throttle_restart)
6371da177e4SLinus Torvalds 		ret = usb_submit_urb(port->read_urb, GFP_ATOMIC);
6381da177e4SLinus Torvalds 
639ba6b702fSJohan Hovold 	/* turn throttle off */
640ba6b702fSJohan Hovold 	priv->dp_throttled = 0;
641ba6b702fSJohan Hovold 	priv->dp_throttle_restart = 0;
642ba6b702fSJohan Hovold 
6431da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
6441da177e4SLinus Torvalds 
645c6d61269SAlan Cox 	if (ret)
646194343d9SGreg Kroah-Hartman 		dev_err(&port->dev,
647194343d9SGreg Kroah-Hartman 			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
648441b62c1SHarvey Harrison 			__func__, ret, priv->dp_port_num);
6491da177e4SLinus Torvalds }
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 
digi_set_termios(struct tty_struct * tty,struct usb_serial_port * port,const struct ktermios * old_termios)65295da310eSAlan Cox static void digi_set_termios(struct tty_struct *tty,
653f6d47fe5SIlpo Järvinen 			     struct usb_serial_port *port,
654f6d47fe5SIlpo Järvinen 			     const struct ktermios *old_termios)
6551da177e4SLinus Torvalds {
6561da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
657f9dfbebbSGreg Kroah-Hartman 	struct device *dev = &port->dev;
658adc8d746SAlan Cox 	unsigned int iflag = tty->termios.c_iflag;
659adc8d746SAlan Cox 	unsigned int cflag = tty->termios.c_cflag;
6601da177e4SLinus Torvalds 	unsigned int old_iflag = old_termios->c_iflag;
6611da177e4SLinus Torvalds 	unsigned int old_cflag = old_termios->c_cflag;
6621da177e4SLinus Torvalds 	unsigned char buf[32];
6631da177e4SLinus Torvalds 	unsigned int modem_signals;
6641da177e4SLinus Torvalds 	int arg, ret;
6651da177e4SLinus Torvalds 	int i = 0;
666c6d61269SAlan Cox 	speed_t baud;
6671da177e4SLinus Torvalds 
668f9dfbebbSGreg Kroah-Hartman 	dev_dbg(dev,
669f9dfbebbSGreg Kroah-Hartman 		"digi_set_termios: TOP: port=%d, iflag=0x%x, old_iflag=0x%x, cflag=0x%x, old_cflag=0x%x\n",
670f9dfbebbSGreg Kroah-Hartman 		priv->dp_port_num, iflag, old_iflag, cflag, old_cflag);
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	/* set baud rate */
67341ad427dSAlan Cox 	baud = tty_get_baud_rate(tty);
67441ad427dSAlan Cox 	if (baud != tty_termios_baud_rate(old_termios)) {
6751da177e4SLinus Torvalds 		arg = -1;
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 		/* reassert DTR and (maybe) RTS on transition from B0 */
6781da177e4SLinus Torvalds 		if ((old_cflag & CBAUD) == B0) {
6791da177e4SLinus Torvalds 			/* don't set RTS if using hardware flow control */
6801da177e4SLinus Torvalds 			/* and throttling input */
6811da177e4SLinus Torvalds 			modem_signals = TIOCM_DTR;
68297ef38b8SPeter Hurley 			if (!C_CRTSCTS(tty) || !tty_throttled(tty))
6831da177e4SLinus Torvalds 				modem_signals |= TIOCM_RTS;
6841da177e4SLinus Torvalds 			digi_set_modem_signals(port, modem_signals, 1);
6851da177e4SLinus Torvalds 		}
686c6d61269SAlan Cox 		switch (baud) {
6871da177e4SLinus Torvalds 		/* drop DTR and RTS on transition to B0 */
688c6d61269SAlan Cox 		case 0: digi_set_modem_signals(port, 0, 1); break;
689c6d61269SAlan Cox 		case 50: arg = DIGI_BAUD_50; break;
690c6d61269SAlan Cox 		case 75: arg = DIGI_BAUD_75; break;
691c6d61269SAlan Cox 		case 110: arg = DIGI_BAUD_110; break;
692c6d61269SAlan Cox 		case 150: arg = DIGI_BAUD_150; break;
693c6d61269SAlan Cox 		case 200: arg = DIGI_BAUD_200; break;
694c6d61269SAlan Cox 		case 300: arg = DIGI_BAUD_300; break;
695c6d61269SAlan Cox 		case 600: arg = DIGI_BAUD_600; break;
696c6d61269SAlan Cox 		case 1200: arg = DIGI_BAUD_1200; break;
697c6d61269SAlan Cox 		case 1800: arg = DIGI_BAUD_1800; break;
698c6d61269SAlan Cox 		case 2400: arg = DIGI_BAUD_2400; break;
699c6d61269SAlan Cox 		case 4800: arg = DIGI_BAUD_4800; break;
700c6d61269SAlan Cox 		case 9600: arg = DIGI_BAUD_9600; break;
701c6d61269SAlan Cox 		case 19200: arg = DIGI_BAUD_19200; break;
702c6d61269SAlan Cox 		case 38400: arg = DIGI_BAUD_38400; break;
703c6d61269SAlan Cox 		case 57600: arg = DIGI_BAUD_57600; break;
704c6d61269SAlan Cox 		case 115200: arg = DIGI_BAUD_115200; break;
705c6d61269SAlan Cox 		case 230400: arg = DIGI_BAUD_230400; break;
706c6d61269SAlan Cox 		case 460800: arg = DIGI_BAUD_460800; break;
7071da177e4SLinus Torvalds 		default:
708c6d61269SAlan Cox 			arg = DIGI_BAUD_9600;
709c6d61269SAlan Cox 			baud = 9600;
7101da177e4SLinus Torvalds 			break;
7111da177e4SLinus Torvalds 		}
7121da177e4SLinus Torvalds 		if (arg != -1) {
7131da177e4SLinus Torvalds 			buf[i++] = DIGI_CMD_SET_BAUD_RATE;
7141da177e4SLinus Torvalds 			buf[i++] = priv->dp_port_num;
7151da177e4SLinus Torvalds 			buf[i++] = arg;
7161da177e4SLinus Torvalds 			buf[i++] = 0;
7171da177e4SLinus Torvalds 		}
7181da177e4SLinus Torvalds 	}
7191da177e4SLinus Torvalds 	/* set parity */
720adc8d746SAlan Cox 	tty->termios.c_cflag &= ~CMSPAR;
7217fa36a99SAlan Cox 
7221da177e4SLinus Torvalds 	if ((cflag & (PARENB | PARODD)) != (old_cflag & (PARENB | PARODD))) {
723c6d61269SAlan Cox 		if (cflag & PARENB) {
724c6d61269SAlan Cox 			if (cflag & PARODD)
7251da177e4SLinus Torvalds 				arg = DIGI_PARITY_ODD;
7261da177e4SLinus Torvalds 			else
7271da177e4SLinus Torvalds 				arg = DIGI_PARITY_EVEN;
7281da177e4SLinus Torvalds 		} else {
7291da177e4SLinus Torvalds 			arg = DIGI_PARITY_NONE;
7301da177e4SLinus Torvalds 		}
7311da177e4SLinus Torvalds 		buf[i++] = DIGI_CMD_SET_PARITY;
7321da177e4SLinus Torvalds 		buf[i++] = priv->dp_port_num;
7331da177e4SLinus Torvalds 		buf[i++] = arg;
7341da177e4SLinus Torvalds 		buf[i++] = 0;
7351da177e4SLinus Torvalds 	}
7361da177e4SLinus Torvalds 	/* set word size */
7371da177e4SLinus Torvalds 	if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
7381da177e4SLinus Torvalds 		arg = -1;
739c6d61269SAlan Cox 		switch (cflag & CSIZE) {
7401da177e4SLinus Torvalds 		case CS5: arg = DIGI_WORD_SIZE_5; break;
7411da177e4SLinus Torvalds 		case CS6: arg = DIGI_WORD_SIZE_6; break;
7421da177e4SLinus Torvalds 		case CS7: arg = DIGI_WORD_SIZE_7; break;
7431da177e4SLinus Torvalds 		case CS8: arg = DIGI_WORD_SIZE_8; break;
7441da177e4SLinus Torvalds 		default:
745f9dfbebbSGreg Kroah-Hartman 			dev_dbg(dev,
746f9dfbebbSGreg Kroah-Hartman 				"digi_set_termios: can't handle word size %d\n",
74774d81395SJohan Hovold 				cflag & CSIZE);
7481da177e4SLinus Torvalds 			break;
7491da177e4SLinus Torvalds 		}
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 		if (arg != -1) {
7521da177e4SLinus Torvalds 			buf[i++] = DIGI_CMD_SET_WORD_SIZE;
7531da177e4SLinus Torvalds 			buf[i++] = priv->dp_port_num;
7541da177e4SLinus Torvalds 			buf[i++] = arg;
7551da177e4SLinus Torvalds 			buf[i++] = 0;
7561da177e4SLinus Torvalds 		}
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 	}
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	/* set stop bits */
7611da177e4SLinus Torvalds 	if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 		if ((cflag & CSTOPB))
7641da177e4SLinus Torvalds 			arg = DIGI_STOP_BITS_2;
7651da177e4SLinus Torvalds 		else
7661da177e4SLinus Torvalds 			arg = DIGI_STOP_BITS_1;
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 		buf[i++] = DIGI_CMD_SET_STOP_BITS;
7691da177e4SLinus Torvalds 		buf[i++] = priv->dp_port_num;
7701da177e4SLinus Torvalds 		buf[i++] = arg;
7711da177e4SLinus Torvalds 		buf[i++] = 0;
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	}
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	/* set input flow control */
77674d81395SJohan Hovold 	if ((iflag & IXOFF) != (old_iflag & IXOFF) ||
77774d81395SJohan Hovold 			(cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
7781da177e4SLinus Torvalds 		arg = 0;
779c6d61269SAlan Cox 		if (iflag & IXOFF)
7801da177e4SLinus Torvalds 			arg |= DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
7811da177e4SLinus Torvalds 		else
7821da177e4SLinus Torvalds 			arg &= ~DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
7831da177e4SLinus Torvalds 
784c6d61269SAlan Cox 		if (cflag & CRTSCTS) {
7851da177e4SLinus Torvalds 			arg |= DIGI_INPUT_FLOW_CONTROL_RTS;
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 			/* On USB-4 it is necessary to assert RTS prior */
7881da177e4SLinus Torvalds 			/* to selecting RTS input flow control.  */
7891da177e4SLinus Torvalds 			buf[i++] = DIGI_CMD_SET_RTS_SIGNAL;
7901da177e4SLinus Torvalds 			buf[i++] = priv->dp_port_num;
7911da177e4SLinus Torvalds 			buf[i++] = DIGI_RTS_ACTIVE;
7921da177e4SLinus Torvalds 			buf[i++] = 0;
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 		} else {
7951da177e4SLinus Torvalds 			arg &= ~DIGI_INPUT_FLOW_CONTROL_RTS;
7961da177e4SLinus Torvalds 		}
7971da177e4SLinus Torvalds 		buf[i++] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
7981da177e4SLinus Torvalds 		buf[i++] = priv->dp_port_num;
7991da177e4SLinus Torvalds 		buf[i++] = arg;
8001da177e4SLinus Torvalds 		buf[i++] = 0;
8011da177e4SLinus Torvalds 	}
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 	/* set output flow control */
80474d81395SJohan Hovold 	if ((iflag & IXON) != (old_iflag & IXON) ||
80574d81395SJohan Hovold 			(cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
8061da177e4SLinus Torvalds 		arg = 0;
807c6d61269SAlan Cox 		if (iflag & IXON)
8081da177e4SLinus Torvalds 			arg |= DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
8091da177e4SLinus Torvalds 		else
8101da177e4SLinus Torvalds 			arg &= ~DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
8111da177e4SLinus Torvalds 
81274d81395SJohan Hovold 		if (cflag & CRTSCTS)
8131da177e4SLinus Torvalds 			arg |= DIGI_OUTPUT_FLOW_CONTROL_CTS;
81474d81395SJohan Hovold 		else
8151da177e4SLinus Torvalds 			arg &= ~DIGI_OUTPUT_FLOW_CONTROL_CTS;
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 		buf[i++] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
8181da177e4SLinus Torvalds 		buf[i++] = priv->dp_port_num;
8191da177e4SLinus Torvalds 		buf[i++] = arg;
8201da177e4SLinus Torvalds 		buf[i++] = 0;
8211da177e4SLinus Torvalds 	}
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	/* set receive enable/disable */
8241da177e4SLinus Torvalds 	if ((cflag & CREAD) != (old_cflag & CREAD)) {
825c6d61269SAlan Cox 		if (cflag & CREAD)
8261da177e4SLinus Torvalds 			arg = DIGI_ENABLE;
8271da177e4SLinus Torvalds 		else
8281da177e4SLinus Torvalds 			arg = DIGI_DISABLE;
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 		buf[i++] = DIGI_CMD_RECEIVE_ENABLE;
8311da177e4SLinus Torvalds 		buf[i++] = priv->dp_port_num;
8321da177e4SLinus Torvalds 		buf[i++] = arg;
8331da177e4SLinus Torvalds 		buf[i++] = 0;
8341da177e4SLinus Torvalds 	}
83541ad427dSAlan Cox 	ret = digi_write_oob_command(port, buf, i, 1);
83641ad427dSAlan Cox 	if (ret != 0)
837f9dfbebbSGreg Kroah-Hartman 		dev_dbg(dev, "digi_set_termios: write oob failed, ret=%d\n", ret);
8387fa36a99SAlan Cox 	tty_encode_baud_rate(tty, baud, baud);
8391da177e4SLinus Torvalds }
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 
digi_break_ctl(struct tty_struct * tty,int break_state)842*6ff58ae1SJohan Hovold static int digi_break_ctl(struct tty_struct *tty, int break_state)
8431da177e4SLinus Torvalds {
84495da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
8451da177e4SLinus Torvalds 	unsigned char buf[4];
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	buf[0] = DIGI_CMD_BREAK_CONTROL;
8481da177e4SLinus Torvalds 	buf[1] = 2;				/* length */
8491da177e4SLinus Torvalds 	buf[2] = break_state ? 1 : 0;
8501da177e4SLinus Torvalds 	buf[3] = 0;				/* pad */
851*6ff58ae1SJohan Hovold 
852*6ff58ae1SJohan Hovold 	return digi_write_inb_command(port, buf, 4, 0);
8531da177e4SLinus Torvalds }
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 
digi_tiocmget(struct tty_struct * tty)85660b33c13SAlan Cox static int digi_tiocmget(struct tty_struct *tty)
8571da177e4SLinus Torvalds {
85895da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
8591da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
8601da177e4SLinus Torvalds 	unsigned int val;
8611da177e4SLinus Torvalds 	unsigned long flags;
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
8641da177e4SLinus Torvalds 	val = priv->dp_modem_signals;
8651da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
8661da177e4SLinus Torvalds 	return val;
8671da177e4SLinus Torvalds }
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 
digi_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)87020b9d177SAlan Cox static int digi_tiocmset(struct tty_struct *tty,
8711da177e4SLinus Torvalds 					unsigned int set, unsigned int clear)
8721da177e4SLinus Torvalds {
87395da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
8741da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
8751da177e4SLinus Torvalds 	unsigned int val;
8761da177e4SLinus Torvalds 	unsigned long flags;
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
8791da177e4SLinus Torvalds 	val = (priv->dp_modem_signals & ~clear) | set;
8801da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
8811da177e4SLinus Torvalds 	return digi_set_modem_signals(port, val, 1);
8821da177e4SLinus Torvalds }
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 
digi_write(struct tty_struct * tty,struct usb_serial_port * port,const unsigned char * buf,int count)88595da310eSAlan Cox static int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
88695da310eSAlan Cox 					const unsigned char *buf, int count)
8871da177e4SLinus Torvalds {
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	int ret, data_len, new_len;
8901da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
8911da177e4SLinus Torvalds 	unsigned char *data = port->write_urb->transfer_buffer;
89217cd3a10SJohan Hovold 	unsigned long flags;
8931da177e4SLinus Torvalds 
8941fcf6891SAhmed S. Darwish 	dev_dbg(&port->dev, "digi_write: TOP: port=%d, count=%d\n",
8951fcf6891SAhmed S. Darwish 		priv->dp_port_num, count);
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 	/* copy user data (which can sleep) before getting spin lock */
8981da177e4SLinus Torvalds 	count = min(count, port->bulk_out_size-2);
8991da177e4SLinus Torvalds 	count = min(64, count);
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	/* be sure only one write proceeds at a time */
9021da177e4SLinus Torvalds 	/* there are races on the port private buffer */
9031da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	/* wait for urb status clear to submit another urb */
90650de36f7SGreg Kroah-Hartman 	if (priv->dp_write_urb_in_use) {
9071da177e4SLinus Torvalds 		/* buffer data if count is 1 (probably put_char) if possible */
9081da177e4SLinus Torvalds 		if (count == 1 && priv->dp_out_buf_len < DIGI_OUT_BUF_SIZE) {
9091da177e4SLinus Torvalds 			priv->dp_out_buf[priv->dp_out_buf_len++] = *buf;
9101da177e4SLinus Torvalds 			new_len = 1;
9111da177e4SLinus Torvalds 		} else {
9121da177e4SLinus Torvalds 			new_len = 0;
9131da177e4SLinus Torvalds 		}
9141da177e4SLinus Torvalds 		spin_unlock_irqrestore(&priv->dp_port_lock, flags);
915c6d61269SAlan Cox 		return new_len;
9161da177e4SLinus Torvalds 	}
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	/* allow space for any buffered data and for new data, up to */
9191da177e4SLinus Torvalds 	/* transfer buffer size - 2 (for command and length bytes) */
9201da177e4SLinus Torvalds 	new_len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
9211da177e4SLinus Torvalds 	data_len = new_len + priv->dp_out_buf_len;
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds 	if (data_len == 0) {
9241da177e4SLinus Torvalds 		spin_unlock_irqrestore(&priv->dp_port_lock, flags);
925c6d61269SAlan Cox 		return 0;
9261da177e4SLinus Torvalds 	}
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 	port->write_urb->transfer_buffer_length = data_len+2;
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 	*data++ = DIGI_CMD_SEND_DATA;
9311da177e4SLinus Torvalds 	*data++ = data_len;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	/* copy in buffered data first */
9341da177e4SLinus Torvalds 	memcpy(data, priv->dp_out_buf, priv->dp_out_buf_len);
9351da177e4SLinus Torvalds 	data += priv->dp_out_buf_len;
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 	/* copy in new data */
9381da177e4SLinus Torvalds 	memcpy(data, buf, new_len);
9391da177e4SLinus Torvalds 
94041ad427dSAlan Cox 	ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
94141ad427dSAlan Cox 	if (ret == 0) {
9421da177e4SLinus Torvalds 		priv->dp_write_urb_in_use = 1;
9431da177e4SLinus Torvalds 		ret = new_len;
9441da177e4SLinus Torvalds 		priv->dp_out_buf_len = 0;
9451da177e4SLinus Torvalds 	}
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	/* return length of new data written, or error */
9481da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
949c6d61269SAlan Cox 	if (ret < 0)
95022a416c4SJohan Hovold 		dev_err_console(port,
951194343d9SGreg Kroah-Hartman 			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
952441b62c1SHarvey Harrison 			__func__, ret, priv->dp_port_num);
953f9dfbebbSGreg Kroah-Hartman 	dev_dbg(&port->dev, "digi_write: returning %d\n", ret);
954c6d61269SAlan Cox 	return ret;
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds }
9571da177e4SLinus Torvalds 
digi_write_bulk_callback(struct urb * urb)9587d12e780SDavid Howells static void digi_write_bulk_callback(struct urb *urb)
9591da177e4SLinus Torvalds {
9601da177e4SLinus Torvalds 
961cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
9621da177e4SLinus Torvalds 	struct usb_serial *serial;
9631da177e4SLinus Torvalds 	struct digi_port *priv;
9641da177e4SLinus Torvalds 	struct digi_serial *serial_priv;
965041b7db9SJohn Ogness 	unsigned long flags;
9661da177e4SLinus Torvalds 	int ret = 0;
96785d75107SGreg Kroah-Hartman 	int status = urb->status;
9685098e779SJohan Hovold 	bool wakeup;
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	/* port and serial sanity check */
9711da177e4SLinus Torvalds 	if (port == NULL || (priv = usb_get_serial_port_data(port)) == NULL) {
972109f34e7SJulia Lawall 		pr_err("%s: port or port->private is NULL, status=%d\n",
973441b62c1SHarvey Harrison 			__func__, status);
9741da177e4SLinus Torvalds 		return;
9751da177e4SLinus Torvalds 	}
9761da177e4SLinus Torvalds 	serial = port->serial;
9771da177e4SLinus Torvalds 	if (serial == NULL || (serial_priv = usb_get_serial_data(serial)) == NULL) {
978194343d9SGreg Kroah-Hartman 		dev_err(&port->dev,
979194343d9SGreg Kroah-Hartman 			"%s: serial or serial->private is NULL, status=%d\n",
980441b62c1SHarvey Harrison 			__func__, status);
9811da177e4SLinus Torvalds 		return;
9821da177e4SLinus Torvalds 	}
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 	/* handle oob callback */
9851da177e4SLinus Torvalds 	if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
986f9dfbebbSGreg Kroah-Hartman 		dev_dbg(&port->dev, "digi_write_bulk_callback: oob callback\n");
987041b7db9SJohn Ogness 		spin_lock_irqsave(&priv->dp_port_lock, flags);
9881da177e4SLinus Torvalds 		priv->dp_write_urb_in_use = 0;
989179dfb95SJohan Hovold 		wake_up_interruptible(&priv->write_wait);
990041b7db9SJohn Ogness 		spin_unlock_irqrestore(&priv->dp_port_lock, flags);
9911da177e4SLinus Torvalds 		return;
9921da177e4SLinus Torvalds 	}
9931da177e4SLinus Torvalds 
9941f87158eSAlan Stern 	/* try to send any buffered data on this port */
9955098e779SJohan Hovold 	wakeup = true;
996041b7db9SJohn Ogness 	spin_lock_irqsave(&priv->dp_port_lock, flags);
9971da177e4SLinus Torvalds 	priv->dp_write_urb_in_use = 0;
9981f87158eSAlan Stern 	if (priv->dp_out_buf_len > 0) {
9991da177e4SLinus Torvalds 		*((unsigned char *)(port->write_urb->transfer_buffer))
10001da177e4SLinus Torvalds 			= (unsigned char)DIGI_CMD_SEND_DATA;
10011da177e4SLinus Torvalds 		*((unsigned char *)(port->write_urb->transfer_buffer) + 1)
10021da177e4SLinus Torvalds 			= (unsigned char)priv->dp_out_buf_len;
100341ad427dSAlan Cox 		port->write_urb->transfer_buffer_length =
100441ad427dSAlan Cox 						priv->dp_out_buf_len + 2;
10051da177e4SLinus Torvalds 		memcpy(port->write_urb->transfer_buffer + 2, priv->dp_out_buf,
10061da177e4SLinus Torvalds 			priv->dp_out_buf_len);
100741ad427dSAlan Cox 		ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
100841ad427dSAlan Cox 		if (ret == 0) {
10091da177e4SLinus Torvalds 			priv->dp_write_urb_in_use = 1;
10101da177e4SLinus Torvalds 			priv->dp_out_buf_len = 0;
10115098e779SJohan Hovold 			wakeup = false;
10121da177e4SLinus Torvalds 		}
10131da177e4SLinus Torvalds 	}
1014041b7db9SJohn Ogness 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
10155098e779SJohan Hovold 
10161f87158eSAlan Stern 	if (ret && ret != -EPERM)
101722a416c4SJohan Hovold 		dev_err_console(port,
1018194343d9SGreg Kroah-Hartman 			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
1019441b62c1SHarvey Harrison 			__func__, ret, priv->dp_port_num);
10205098e779SJohan Hovold 
10215098e779SJohan Hovold 	if (wakeup)
10225098e779SJohan Hovold 		tty_port_tty_wakeup(&port->port);
10231da177e4SLinus Torvalds }
10241da177e4SLinus Torvalds 
digi_write_room(struct tty_struct * tty)102594cc7aeaSJiri Slaby static unsigned int digi_write_room(struct tty_struct *tty)
10261da177e4SLinus Torvalds {
102795da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
10281da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
102917cd3a10SJohan Hovold 	unsigned long flags;
103094cc7aeaSJiri Slaby 	unsigned int room;
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
10331da177e4SLinus Torvalds 
103450de36f7SGreg Kroah-Hartman 	if (priv->dp_write_urb_in_use)
10351da177e4SLinus Torvalds 		room = 0;
10361da177e4SLinus Torvalds 	else
10371da177e4SLinus Torvalds 		room = port->bulk_out_size - 2 - priv->dp_out_buf_len;
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
104094cc7aeaSJiri Slaby 	dev_dbg(&port->dev, "digi_write_room: port=%d, room=%u\n", priv->dp_port_num, room);
1041c6d61269SAlan Cox 	return room;
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds }
10441da177e4SLinus Torvalds 
digi_chars_in_buffer(struct tty_struct * tty)1045155591d3SJiri Slaby static unsigned int digi_chars_in_buffer(struct tty_struct *tty)
10461da177e4SLinus Torvalds {
104795da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
10481da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
1049dcbc0ae4SJohan Hovold 	unsigned long flags;
1050dcbc0ae4SJohan Hovold 	unsigned int chars;
10511da177e4SLinus Torvalds 
1052dcbc0ae4SJohan Hovold 	spin_lock_irqsave(&priv->dp_port_lock, flags);
1053dcbc0ae4SJohan Hovold 	if (priv->dp_write_urb_in_use)
1054dcbc0ae4SJohan Hovold 		chars = port->bulk_out_size - 2;
1055dcbc0ae4SJohan Hovold 	else
1056dcbc0ae4SJohan Hovold 		chars = priv->dp_out_buf_len;
1057dcbc0ae4SJohan Hovold 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
10581da177e4SLinus Torvalds 
1059dcbc0ae4SJohan Hovold 	dev_dbg(&port->dev, "%s: port=%d, chars=%d\n", __func__,
1060dcbc0ae4SJohan Hovold 			priv->dp_port_num, chars);
1061dcbc0ae4SJohan Hovold 	return chars;
10621da177e4SLinus Torvalds }
10631da177e4SLinus Torvalds 
digi_dtr_rts(struct usb_serial_port * port,int on)1064335f8514SAlan Cox static void digi_dtr_rts(struct usb_serial_port *port, int on)
1065335f8514SAlan Cox {
1066335f8514SAlan Cox 	/* Adjust DTR and RTS */
1067335f8514SAlan Cox 	digi_set_modem_signals(port, on * (TIOCM_DTR | TIOCM_RTS), 1);
1068335f8514SAlan Cox }
1069335f8514SAlan Cox 
digi_open(struct tty_struct * tty,struct usb_serial_port * port)1070a509a7e4SAlan Cox static int digi_open(struct tty_struct *tty, struct usb_serial_port *port)
10711da177e4SLinus Torvalds {
10721da177e4SLinus Torvalds 	int ret;
10731da177e4SLinus Torvalds 	unsigned char buf[32];
10741da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
1075606d099cSAlan Cox 	struct ktermios not_termios;
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds 	/* be sure the device is started up */
10781da177e4SLinus Torvalds 	if (digi_startup_device(port->serial) != 0)
1079c6d61269SAlan Cox 		return -ENXIO;
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds 	/* read modem signals automatically whenever they change */
10821da177e4SLinus Torvalds 	buf[0] = DIGI_CMD_READ_INPUT_SIGNALS;
10831da177e4SLinus Torvalds 	buf[1] = priv->dp_port_num;
10841da177e4SLinus Torvalds 	buf[2] = DIGI_ENABLE;
10851da177e4SLinus Torvalds 	buf[3] = 0;
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 	/* flush fifos */
10881da177e4SLinus Torvalds 	buf[4] = DIGI_CMD_IFLUSH_FIFO;
10891da177e4SLinus Torvalds 	buf[5] = priv->dp_port_num;
10901da177e4SLinus Torvalds 	buf[6] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
10911da177e4SLinus Torvalds 	buf[7] = 0;
10921da177e4SLinus Torvalds 
109341ad427dSAlan Cox 	ret = digi_write_oob_command(port, buf, 8, 1);
109441ad427dSAlan Cox 	if (ret != 0)
1095f9dfbebbSGreg Kroah-Hartman 		dev_dbg(&port->dev, "digi_open: write oob failed, ret=%d\n", ret);
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds 	/* set termios settings */
109895da310eSAlan Cox 	if (tty) {
1099adc8d746SAlan Cox 		not_termios.c_cflag = ~tty->termios.c_cflag;
1100adc8d746SAlan Cox 		not_termios.c_iflag = ~tty->termios.c_iflag;
110195da310eSAlan Cox 		digi_set_termios(tty, port, &not_termios);
110295da310eSAlan Cox 	}
1103c6d61269SAlan Cox 	return 0;
11041da177e4SLinus Torvalds }
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 
digi_close(struct usb_serial_port * port)1107335f8514SAlan Cox static void digi_close(struct usb_serial_port *port)
11081da177e4SLinus Torvalds {
11091da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
11101da177e4SLinus Torvalds 	int ret;
11111da177e4SLinus Torvalds 	unsigned char buf[32];
11121da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
11131da177e4SLinus Torvalds 
11140915f490SOliver Neukum 	mutex_lock(&port->serial->disc_mutex);
11151da177e4SLinus Torvalds 	/* if disconnected, just clear flags */
11160915f490SOliver Neukum 	if (port->serial->disconnected)
11171da177e4SLinus Torvalds 		goto exit;
11181da177e4SLinus Torvalds 
1119335f8514SAlan Cox 	/* FIXME: Transmit idle belongs in the wait_unti_sent path */
11201da177e4SLinus Torvalds 	digi_transmit_idle(port, DIGI_CLOSE_TIMEOUT);
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 	/* disable input flow control */
11231da177e4SLinus Torvalds 	buf[0] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
11241da177e4SLinus Torvalds 	buf[1] = priv->dp_port_num;
11251da177e4SLinus Torvalds 	buf[2] = DIGI_DISABLE;
11261da177e4SLinus Torvalds 	buf[3] = 0;
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 	/* disable output flow control */
11291da177e4SLinus Torvalds 	buf[4] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
11301da177e4SLinus Torvalds 	buf[5] = priv->dp_port_num;
11311da177e4SLinus Torvalds 	buf[6] = DIGI_DISABLE;
11321da177e4SLinus Torvalds 	buf[7] = 0;
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds 	/* disable reading modem signals automatically */
11351da177e4SLinus Torvalds 	buf[8] = DIGI_CMD_READ_INPUT_SIGNALS;
11361da177e4SLinus Torvalds 	buf[9] = priv->dp_port_num;
11371da177e4SLinus Torvalds 	buf[10] = DIGI_DISABLE;
11381da177e4SLinus Torvalds 	buf[11] = 0;
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 	/* disable receive */
11411da177e4SLinus Torvalds 	buf[12] = DIGI_CMD_RECEIVE_ENABLE;
11421da177e4SLinus Torvalds 	buf[13] = priv->dp_port_num;
11431da177e4SLinus Torvalds 	buf[14] = DIGI_DISABLE;
11441da177e4SLinus Torvalds 	buf[15] = 0;
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 	/* flush fifos */
11471da177e4SLinus Torvalds 	buf[16] = DIGI_CMD_IFLUSH_FIFO;
11481da177e4SLinus Torvalds 	buf[17] = priv->dp_port_num;
11491da177e4SLinus Torvalds 	buf[18] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
11501da177e4SLinus Torvalds 	buf[19] = 0;
11511da177e4SLinus Torvalds 
115241ad427dSAlan Cox 	ret = digi_write_oob_command(port, buf, 20, 0);
115341ad427dSAlan Cox 	if (ret != 0)
115428e679aeSJohan Hovold 		dev_dbg(&port->dev, "digi_close: write oob failed, ret=%d\n",
115528e679aeSJohan Hovold 									ret);
11561da177e4SLinus Torvalds 	/* wait for final commands on oob port to complete */
115741ad427dSAlan Cox 	prepare_to_wait(&priv->dp_flush_wait, &wait,
115841ad427dSAlan Cox 			TASK_INTERRUPTIBLE);
11591da177e4SLinus Torvalds 	schedule_timeout(DIGI_CLOSE_TIMEOUT);
11601da177e4SLinus Torvalds 	finish_wait(&priv->dp_flush_wait, &wait);
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds 	/* shutdown any outstanding bulk writes */
11631da177e4SLinus Torvalds 	usb_kill_urb(port->write_urb);
11641da177e4SLinus Torvalds exit:
11650915f490SOliver Neukum 	spin_lock_irq(&priv->dp_port_lock);
11661da177e4SLinus Torvalds 	priv->dp_write_urb_in_use = 0;
11671da177e4SLinus Torvalds 	wake_up_interruptible(&priv->dp_close_wait);
11680915f490SOliver Neukum 	spin_unlock_irq(&priv->dp_port_lock);
11690915f490SOliver Neukum 	mutex_unlock(&port->serial->disc_mutex);
11701da177e4SLinus Torvalds }
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds /*
11741da177e4SLinus Torvalds  *  Digi Startup Device
11751da177e4SLinus Torvalds  *
11761da177e4SLinus Torvalds  *  Starts reads on all ports.  Must be called AFTER startup, with
11771da177e4SLinus Torvalds  *  urbs initialized.  Returns 0 if successful, non-zero error otherwise.
11781da177e4SLinus Torvalds  */
11791da177e4SLinus Torvalds 
digi_startup_device(struct usb_serial * serial)11801da177e4SLinus Torvalds static int digi_startup_device(struct usb_serial *serial)
11811da177e4SLinus Torvalds {
11821da177e4SLinus Torvalds 	int i, ret = 0;
11831da177e4SLinus Torvalds 	struct digi_serial *serial_priv = usb_get_serial_data(serial);
11841da177e4SLinus Torvalds 	struct usb_serial_port *port;
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds 	/* be sure this happens exactly once */
11871da177e4SLinus Torvalds 	spin_lock(&serial_priv->ds_serial_lock);
11881da177e4SLinus Torvalds 	if (serial_priv->ds_device_started) {
11891da177e4SLinus Torvalds 		spin_unlock(&serial_priv->ds_serial_lock);
1190c6d61269SAlan Cox 		return 0;
11911da177e4SLinus Torvalds 	}
11921da177e4SLinus Torvalds 	serial_priv->ds_device_started = 1;
11931da177e4SLinus Torvalds 	spin_unlock(&serial_priv->ds_serial_lock);
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds 	/* start reading from each bulk in endpoint for the device */
11961da177e4SLinus Torvalds 	/* set USB_DISABLE_SPD flag for write bulk urbs */
11971da177e4SLinus Torvalds 	for (i = 0; i < serial->type->num_ports + 1; i++) {
11981da177e4SLinus Torvalds 		port = serial->port[i];
119941ad427dSAlan Cox 		ret = usb_submit_urb(port->read_urb, GFP_KERNEL);
120041ad427dSAlan Cox 		if (ret != 0) {
1201194343d9SGreg Kroah-Hartman 			dev_err(&port->dev,
1202194343d9SGreg Kroah-Hartman 				"%s: usb_submit_urb failed, ret=%d, port=%d\n",
1203441b62c1SHarvey Harrison 				__func__, ret, i);
12041da177e4SLinus Torvalds 			break;
12051da177e4SLinus Torvalds 		}
12061da177e4SLinus Torvalds 	}
1207c6d61269SAlan Cox 	return ret;
12081da177e4SLinus Torvalds }
12091da177e4SLinus Torvalds 
digi_port_init(struct usb_serial_port * port,unsigned port_num)1210fb44ff85SJohan Hovold static int digi_port_init(struct usb_serial_port *port, unsigned port_num)
12111da177e4SLinus Torvalds {
12121da177e4SLinus Torvalds 	struct digi_port *priv;
12131da177e4SLinus Torvalds 
1214fb44ff85SJohan Hovold 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1215fb44ff85SJohan Hovold 	if (!priv)
1216fb44ff85SJohan Hovold 		return -ENOMEM;
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 	spin_lock_init(&priv->dp_port_lock);
1219fb44ff85SJohan Hovold 	priv->dp_port_num = port_num;
12201da177e4SLinus Torvalds 	init_waitqueue_head(&priv->dp_transmit_idle_wait);
12211da177e4SLinus Torvalds 	init_waitqueue_head(&priv->dp_flush_wait);
12221da177e4SLinus Torvalds 	init_waitqueue_head(&priv->dp_close_wait);
1223179dfb95SJohan Hovold 	init_waitqueue_head(&priv->write_wait);
1224fb44ff85SJohan Hovold 	priv->dp_port = port;
12251da177e4SLinus Torvalds 
1226fb44ff85SJohan Hovold 	usb_set_serial_port_data(port, priv);
1227fb44ff85SJohan Hovold 
1228fb44ff85SJohan Hovold 	return 0;
12291da177e4SLinus Torvalds }
12301da177e4SLinus Torvalds 
digi_startup(struct usb_serial * serial)1231fb44ff85SJohan Hovold static int digi_startup(struct usb_serial *serial)
1232fb44ff85SJohan Hovold {
1233fb44ff85SJohan Hovold 	struct digi_serial *serial_priv;
1234fb44ff85SJohan Hovold 	int ret;
12351da177e4SLinus Torvalds 
1236fb44ff85SJohan Hovold 	serial_priv = kzalloc(sizeof(*serial_priv), GFP_KERNEL);
1237fb44ff85SJohan Hovold 	if (!serial_priv)
1238fb44ff85SJohan Hovold 		return -ENOMEM;
1239fb44ff85SJohan Hovold 
12401da177e4SLinus Torvalds 	spin_lock_init(&serial_priv->ds_serial_lock);
12411da177e4SLinus Torvalds 	serial_priv->ds_oob_port_num = serial->type->num_ports;
12421da177e4SLinus Torvalds 	serial_priv->ds_oob_port = serial->port[serial_priv->ds_oob_port_num];
1243fb44ff85SJohan Hovold 
1244fb44ff85SJohan Hovold 	ret = digi_port_init(serial_priv->ds_oob_port,
1245fb44ff85SJohan Hovold 						serial_priv->ds_oob_port_num);
1246fb44ff85SJohan Hovold 	if (ret) {
1247fb44ff85SJohan Hovold 		kfree(serial_priv);
1248fb44ff85SJohan Hovold 		return ret;
1249fb44ff85SJohan Hovold 	}
1250fb44ff85SJohan Hovold 
12511da177e4SLinus Torvalds 	usb_set_serial_data(serial, serial_priv);
12521da177e4SLinus Torvalds 
1253c6d61269SAlan Cox 	return 0;
12541da177e4SLinus Torvalds }
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 
digi_disconnect(struct usb_serial * serial)1257f9c99bb8SAlan Stern static void digi_disconnect(struct usb_serial *serial)
12581da177e4SLinus Torvalds {
12591da177e4SLinus Torvalds 	int i;
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds 	/* stop reads and writes on all ports */
12621da177e4SLinus Torvalds 	for (i = 0; i < serial->type->num_ports + 1; i++) {
12631da177e4SLinus Torvalds 		usb_kill_urb(serial->port[i]->read_urb);
12641da177e4SLinus Torvalds 		usb_kill_urb(serial->port[i]->write_urb);
12651da177e4SLinus Torvalds 	}
1266f9c99bb8SAlan Stern }
1267f9c99bb8SAlan Stern 
1268f9c99bb8SAlan Stern 
digi_release(struct usb_serial * serial)1269f9c99bb8SAlan Stern static void digi_release(struct usb_serial *serial)
1270f9c99bb8SAlan Stern {
1271fb44ff85SJohan Hovold 	struct digi_serial *serial_priv;
1272fb44ff85SJohan Hovold 	struct digi_port *priv;
12731da177e4SLinus Torvalds 
1274fb44ff85SJohan Hovold 	serial_priv = usb_get_serial_data(serial);
1275fb44ff85SJohan Hovold 
1276fb44ff85SJohan Hovold 	priv = usb_get_serial_port_data(serial_priv->ds_oob_port);
1277fb44ff85SJohan Hovold 	kfree(priv);
1278fb44ff85SJohan Hovold 
1279fb44ff85SJohan Hovold 	kfree(serial_priv);
12801da177e4SLinus Torvalds }
12811da177e4SLinus Torvalds 
digi_port_probe(struct usb_serial_port * port)1282fb44ff85SJohan Hovold static int digi_port_probe(struct usb_serial_port *port)
1283fb44ff85SJohan Hovold {
12841143832eSGreg Kroah-Hartman 	return digi_port_init(port, port->port_number);
1285fb44ff85SJohan Hovold }
1286fb44ff85SJohan Hovold 
digi_port_remove(struct usb_serial_port * port)1287c5d1448fSUwe Kleine-König static void digi_port_remove(struct usb_serial_port *port)
1288fb44ff85SJohan Hovold {
1289fb44ff85SJohan Hovold 	struct digi_port *priv;
1290fb44ff85SJohan Hovold 
1291fb44ff85SJohan Hovold 	priv = usb_get_serial_port_data(port);
1292fb44ff85SJohan Hovold 	kfree(priv);
1293fb44ff85SJohan Hovold }
12941da177e4SLinus Torvalds 
digi_read_bulk_callback(struct urb * urb)12957d12e780SDavid Howells static void digi_read_bulk_callback(struct urb *urb)
12961da177e4SLinus Torvalds {
1297cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
12981da177e4SLinus Torvalds 	struct digi_port *priv;
12991da177e4SLinus Torvalds 	struct digi_serial *serial_priv;
13001da177e4SLinus Torvalds 	int ret;
130185d75107SGreg Kroah-Hartman 	int status = urb->status;
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds 	/* port sanity check, do not resubmit if port is not valid */
1304194343d9SGreg Kroah-Hartman 	if (port == NULL)
1305194343d9SGreg Kroah-Hartman 		return;
1306194343d9SGreg Kroah-Hartman 	priv = usb_get_serial_port_data(port);
1307194343d9SGreg Kroah-Hartman 	if (priv == NULL) {
1308194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s: port->private is NULL, status=%d\n",
1309441b62c1SHarvey Harrison 			__func__, status);
13101da177e4SLinus Torvalds 		return;
13111da177e4SLinus Torvalds 	}
1312c6d61269SAlan Cox 	if (port->serial == NULL ||
1313c6d61269SAlan Cox 		(serial_priv = usb_get_serial_data(port->serial)) == NULL) {
1314194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s: serial is bad or serial->private "
1315194343d9SGreg Kroah-Hartman 			"is NULL, status=%d\n", __func__, status);
13161da177e4SLinus Torvalds 		return;
13171da177e4SLinus Torvalds 	}
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds 	/* do not resubmit urb if it has any status error */
132085d75107SGreg Kroah-Hartman 	if (status) {
1321194343d9SGreg Kroah-Hartman 		dev_err(&port->dev,
1322194343d9SGreg Kroah-Hartman 			"%s: nonzero read bulk status: status=%d, port=%d\n",
1323441b62c1SHarvey Harrison 			__func__, status, priv->dp_port_num);
13241da177e4SLinus Torvalds 		return;
13251da177e4SLinus Torvalds 	}
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds 	/* handle oob or inb callback, do not resubmit if error */
13281da177e4SLinus Torvalds 	if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
13291da177e4SLinus Torvalds 		if (digi_read_oob_callback(urb) != 0)
13301da177e4SLinus Torvalds 			return;
13311da177e4SLinus Torvalds 	} else {
13321da177e4SLinus Torvalds 		if (digi_read_inb_callback(urb) != 0)
13331da177e4SLinus Torvalds 			return;
13341da177e4SLinus Torvalds 	}
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds 	/* continue read */
133741ad427dSAlan Cox 	ret = usb_submit_urb(urb, GFP_ATOMIC);
13381f87158eSAlan Stern 	if (ret != 0 && ret != -EPERM) {
1339194343d9SGreg Kroah-Hartman 		dev_err(&port->dev,
1340194343d9SGreg Kroah-Hartman 			"%s: failed resubmitting urb, ret=%d, port=%d\n",
1341441b62c1SHarvey Harrison 			__func__, ret, priv->dp_port_num);
13421da177e4SLinus Torvalds 	}
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds }
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds /*
13471da177e4SLinus Torvalds  *  Digi Read INB Callback
13481da177e4SLinus Torvalds  *
13491da177e4SLinus Torvalds  *  Digi Read INB Callback handles reads on the in band ports, sending
13501da177e4SLinus Torvalds  *  the data on to the tty subsystem.  When called we know port and
13511da177e4SLinus Torvalds  *  port->private are not NULL and port->serial has been validated.
13521da177e4SLinus Torvalds  *  It returns 0 if successful, 1 if successful but the port is
13531da177e4SLinus Torvalds  *  throttled, and -1 if the sanity checks failed.
13541da177e4SLinus Torvalds  */
13551da177e4SLinus Torvalds 
digi_read_inb_callback(struct urb * urb)13561da177e4SLinus Torvalds static int digi_read_inb_callback(struct urb *urb)
13571da177e4SLinus Torvalds {
1358cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
13591da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
13601b0aed2bSJohan Hovold 	unsigned char *buf = urb->transfer_buffer;
1361041b7db9SJohn Ogness 	unsigned long flags;
13621b0aed2bSJohan Hovold 	int opcode;
13631b0aed2bSJohan Hovold 	int len;
13641b0aed2bSJohan Hovold 	int port_status;
13651b0aed2bSJohan Hovold 	unsigned char *data;
13661d1de580SJohan Hovold 	int tty_flag, throttled;
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 	/* short/multiple packet check */
13691b0aed2bSJohan Hovold 	if (urb->actual_length < 2) {
13701b0aed2bSJohan Hovold 		dev_warn(&port->dev, "short packet received\n");
13711b0aed2bSJohan Hovold 		return -1;
13721b0aed2bSJohan Hovold 	}
13731b0aed2bSJohan Hovold 
13741b0aed2bSJohan Hovold 	opcode = buf[0];
13751b0aed2bSJohan Hovold 	len = buf[1];
13761b0aed2bSJohan Hovold 
13771da177e4SLinus Torvalds 	if (urb->actual_length != len + 2) {
13781b0aed2bSJohan Hovold 		dev_err(&port->dev, "malformed packet received: port=%d, opcode=%d, len=%d, actual_length=%u\n",
13791b0aed2bSJohan Hovold 			priv->dp_port_num, opcode, len, urb->actual_length);
13801b0aed2bSJohan Hovold 		return -1;
13811b0aed2bSJohan Hovold 	}
13821b0aed2bSJohan Hovold 
13831b0aed2bSJohan Hovold 	if (opcode == DIGI_CMD_RECEIVE_DATA && len < 1) {
13841b0aed2bSJohan Hovold 		dev_err(&port->dev, "malformed data packet received\n");
1385c6d61269SAlan Cox 		return -1;
13861da177e4SLinus Torvalds 	}
13871da177e4SLinus Torvalds 
1388041b7db9SJohn Ogness 	spin_lock_irqsave(&priv->dp_port_lock, flags);
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds 	/* check for throttle; if set, do not resubmit read urb */
13911da177e4SLinus Torvalds 	/* indicate the read chain needs to be restarted on unthrottle */
13921da177e4SLinus Torvalds 	throttled = priv->dp_throttled;
13931da177e4SLinus Torvalds 	if (throttled)
13941da177e4SLinus Torvalds 		priv->dp_throttle_restart = 1;
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds 	/* receive data */
13972e124b4aSJiri Slaby 	if (opcode == DIGI_CMD_RECEIVE_DATA) {
13981b0aed2bSJohan Hovold 		port_status = buf[2];
13991b0aed2bSJohan Hovold 		data = &buf[3];
14001b0aed2bSJohan Hovold 
140185d75107SGreg Kroah-Hartman 		/* get flag from port_status */
14021d1de580SJohan Hovold 		tty_flag = 0;
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds 		/* overrun is special, not associated with a char */
1405c6d61269SAlan Cox 		if (port_status & DIGI_OVERRUN_ERROR)
140692a19f9cSJiri Slaby 			tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds 		/* break takes precedence over parity, */
14091da177e4SLinus Torvalds 		/* which takes precedence over framing errors */
1410c6d61269SAlan Cox 		if (port_status & DIGI_BREAK_ERROR)
14111d1de580SJohan Hovold 			tty_flag = TTY_BREAK;
1412c6d61269SAlan Cox 		else if (port_status & DIGI_PARITY_ERROR)
14131d1de580SJohan Hovold 			tty_flag = TTY_PARITY;
1414c6d61269SAlan Cox 		else if (port_status & DIGI_FRAMING_ERROR)
14151d1de580SJohan Hovold 			tty_flag = TTY_FRAME;
14161da177e4SLinus Torvalds 
141785d75107SGreg Kroah-Hartman 		/* data length is len-1 (one byte of len is port_status) */
14181da177e4SLinus Torvalds 		--len;
14191da177e4SLinus Torvalds 		if (len > 0) {
14202f693357SJiri Slaby 			tty_insert_flip_string_fixed_flag(&port->port, data,
14211d1de580SJohan Hovold 					tty_flag, len);
14222e124b4aSJiri Slaby 			tty_flip_buffer_push(&port->port);
14231da177e4SLinus Torvalds 		}
14241da177e4SLinus Torvalds 	}
1425041b7db9SJohn Ogness 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
14261da177e4SLinus Torvalds 
1427c6d61269SAlan Cox 	if (opcode == DIGI_CMD_RECEIVE_DISABLE)
1428f9dfbebbSGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s: got RECEIVE_DISABLE\n", __func__);
1429c6d61269SAlan Cox 	else if (opcode != DIGI_CMD_RECEIVE_DATA)
1430f9dfbebbSGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s: unknown opcode: %d\n", __func__, opcode);
14311da177e4SLinus Torvalds 
143241ad427dSAlan Cox 	return throttled ? 1 : 0;
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds }
14351da177e4SLinus Torvalds 
14361da177e4SLinus Torvalds 
14371da177e4SLinus Torvalds /*
14381da177e4SLinus Torvalds  *  Digi Read OOB Callback
14391da177e4SLinus Torvalds  *
14401da177e4SLinus Torvalds  *  Digi Read OOB Callback handles reads on the out of band port.
14411da177e4SLinus Torvalds  *  When called we know port and port->private are not NULL and
14421da177e4SLinus Torvalds  *  the port->serial is valid.  It returns 0 if successful, and
14431da177e4SLinus Torvalds  *  -1 if the sanity checks failed.
14441da177e4SLinus Torvalds  */
14451da177e4SLinus Torvalds 
digi_read_oob_callback(struct urb * urb)14461da177e4SLinus Torvalds static int digi_read_oob_callback(struct urb *urb)
14471da177e4SLinus Torvalds {
14481da177e4SLinus Torvalds 
1449cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
14501da177e4SLinus Torvalds 	struct usb_serial *serial = port->serial;
14514a90f09bSAlan Cox 	struct tty_struct *tty;
14522c0bee08SColin Ian King 	struct digi_port *priv;
14532d380889SJohan Hovold 	unsigned char *buf = urb->transfer_buffer;
14541da177e4SLinus Torvalds 	int opcode, line, status, val;
1455041b7db9SJohn Ogness 	unsigned long flags;
14561da177e4SLinus Torvalds 	int i;
145741ad427dSAlan Cox 	unsigned int rts;
14581da177e4SLinus Torvalds 
14592d380889SJohan Hovold 	if (urb->actual_length < 4)
14602d380889SJohan Hovold 		return -1;
14612d380889SJohan Hovold 
14621da177e4SLinus Torvalds 	/* handle each oob command */
14632f682146SJohan Hovold 	for (i = 0; i < urb->actual_length - 3; i += 4) {
14642d380889SJohan Hovold 		opcode = buf[i];
14652d380889SJohan Hovold 		line = buf[i + 1];
14662d380889SJohan Hovold 		status = buf[i + 2];
14672d380889SJohan Hovold 		val = buf[i + 3];
14681da177e4SLinus Torvalds 
1469f9dfbebbSGreg Kroah-Hartman 		dev_dbg(&port->dev, "digi_read_oob_callback: opcode=%d, line=%d, status=%d, val=%d\n",
14701da177e4SLinus Torvalds 			opcode, line, status, val);
14711da177e4SLinus Torvalds 
14721da177e4SLinus Torvalds 		if (status != 0 || line >= serial->type->num_ports)
14731da177e4SLinus Torvalds 			continue;
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds 		port = serial->port[line];
14761da177e4SLinus Torvalds 
147741ad427dSAlan Cox 		priv = usb_get_serial_port_data(port);
147841ad427dSAlan Cox 		if (priv == NULL)
14791da177e4SLinus Torvalds 			return -1;
14801da177e4SLinus Torvalds 
14814a90f09bSAlan Cox 		tty = tty_port_tty_get(&port->port);
14824287341dSAlan Cox 
148341ad427dSAlan Cox 		rts = 0;
14844287341dSAlan Cox 		if (tty)
14859db276f8SPeter Hurley 			rts = C_CRTSCTS(tty);
148641ad427dSAlan Cox 
14874287341dSAlan Cox 		if (tty && opcode == DIGI_CMD_READ_INPUT_SIGNALS) {
14885098e779SJohan Hovold 			bool wakeup = false;
14895098e779SJohan Hovold 
1490041b7db9SJohn Ogness 			spin_lock_irqsave(&priv->dp_port_lock, flags);
14911da177e4SLinus Torvalds 			/* convert from digi flags to termiox flags */
14921da177e4SLinus Torvalds 			if (val & DIGI_READ_INPUT_SIGNALS_CTS) {
14931da177e4SLinus Torvalds 				priv->dp_modem_signals |= TIOCM_CTS;
1494d95e3caeSPeter Hurley 				if (rts)
14955098e779SJohan Hovold 					wakeup = true;
14961da177e4SLinus Torvalds 			} else {
14971da177e4SLinus Torvalds 				priv->dp_modem_signals &= ~TIOCM_CTS;
14981da177e4SLinus Torvalds 				/* port must be open to use tty struct */
14991da177e4SLinus Torvalds 			}
15001da177e4SLinus Torvalds 			if (val & DIGI_READ_INPUT_SIGNALS_DSR)
15011da177e4SLinus Torvalds 				priv->dp_modem_signals |= TIOCM_DSR;
15021da177e4SLinus Torvalds 			else
15031da177e4SLinus Torvalds 				priv->dp_modem_signals &= ~TIOCM_DSR;
15041da177e4SLinus Torvalds 			if (val & DIGI_READ_INPUT_SIGNALS_RI)
15051da177e4SLinus Torvalds 				priv->dp_modem_signals |= TIOCM_RI;
15061da177e4SLinus Torvalds 			else
15071da177e4SLinus Torvalds 				priv->dp_modem_signals &= ~TIOCM_RI;
15081da177e4SLinus Torvalds 			if (val & DIGI_READ_INPUT_SIGNALS_DCD)
15091da177e4SLinus Torvalds 				priv->dp_modem_signals |= TIOCM_CD;
15101da177e4SLinus Torvalds 			else
15111da177e4SLinus Torvalds 				priv->dp_modem_signals &= ~TIOCM_CD;
15121da177e4SLinus Torvalds 
1513041b7db9SJohn Ogness 			spin_unlock_irqrestore(&priv->dp_port_lock, flags);
15145098e779SJohan Hovold 
15155098e779SJohan Hovold 			if (wakeup)
15165098e779SJohan Hovold 				tty_port_tty_wakeup(&port->port);
15171da177e4SLinus Torvalds 		} else if (opcode == DIGI_CMD_TRANSMIT_IDLE) {
1518041b7db9SJohn Ogness 			spin_lock_irqsave(&priv->dp_port_lock, flags);
15191da177e4SLinus Torvalds 			priv->dp_transmit_idle = 1;
15201da177e4SLinus Torvalds 			wake_up_interruptible(&priv->dp_transmit_idle_wait);
1521041b7db9SJohn Ogness 			spin_unlock_irqrestore(&priv->dp_port_lock, flags);
15221da177e4SLinus Torvalds 		} else if (opcode == DIGI_CMD_IFLUSH_FIFO) {
152345f23f18SNishanth Aravamudan 			wake_up_interruptible(&priv->dp_flush_wait);
1524c6d61269SAlan Cox 		}
15254a90f09bSAlan Cox 		tty_kref_put(tty);
1526c6d61269SAlan Cox 	}
1527c6d61269SAlan Cox 	return 0;
15281da177e4SLinus Torvalds 
15291da177e4SLinus Torvalds }
15301da177e4SLinus Torvalds 
153168e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table_combined);
15321da177e4SLinus Torvalds 
15331da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
15341da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
15351da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1536