xref: /openbmc/linux/drivers/usb/serial/digi_acceleport.c (revision 9db276f8f02145068d8c04614bc28c2a4532a8c7)
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 
181da177e4SLinus Torvalds #include <linux/kernel.h>
191da177e4SLinus Torvalds #include <linux/errno.h>
201da177e4SLinus Torvalds #include <linux/slab.h>
211da177e4SLinus Torvalds #include <linux/tty.h>
221da177e4SLinus Torvalds #include <linux/tty_driver.h>
231da177e4SLinus Torvalds #include <linux/tty_flip.h>
241da177e4SLinus Torvalds #include <linux/module.h>
251da177e4SLinus Torvalds #include <linux/spinlock.h>
261da177e4SLinus Torvalds #include <linux/workqueue.h>
2741ad427dSAlan Cox #include <linux/uaccess.h>
281da177e4SLinus Torvalds #include <linux/usb.h>
291da177e4SLinus Torvalds #include <linux/wait.h>
30a969888cSGreg Kroah-Hartman #include <linux/usb/serial.h>
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds /* Defines */
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds #define DRIVER_AUTHOR "Peter Berger <pberger@brimson.com>, Al Borchers <borchers@steinerpoint.com>"
351da177e4SLinus Torvalds #define DRIVER_DESC "Digi AccelePort USB-2/USB-4 Serial Converter driver"
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds /* port output buffer length -- must be <= transfer buffer length - 2 */
381da177e4SLinus Torvalds /* so we can be sure to send the full buffer in one urb */
391da177e4SLinus Torvalds #define DIGI_OUT_BUF_SIZE		8
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds /* port input buffer length -- must be >= transfer buffer length - 3 */
421da177e4SLinus Torvalds /* so we can be sure to hold at least one full buffer from one urb */
431da177e4SLinus Torvalds #define DIGI_IN_BUF_SIZE		64
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds /* retry timeout while sleeping */
461da177e4SLinus Torvalds #define DIGI_RETRY_TIMEOUT		(HZ/10)
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /* timeout while waiting for tty output to drain in close */
491da177e4SLinus Torvalds /* this delay is used twice in close, so the total delay could */
501da177e4SLinus Torvalds /* be twice this value */
511da177e4SLinus Torvalds #define DIGI_CLOSE_TIMEOUT		(5*HZ)
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds /* AccelePort USB Defines */
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds /* ids */
571da177e4SLinus Torvalds #define DIGI_VENDOR_ID			0x05c5
581da177e4SLinus Torvalds #define DIGI_2_ID			0x0002	/* USB-2 */
591da177e4SLinus Torvalds #define DIGI_4_ID			0x0004	/* USB-4 */
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds /* commands
621da177e4SLinus Torvalds  * "INB": can be used on the in-band endpoint
631da177e4SLinus Torvalds  * "OOB": can be used on the out-of-band endpoint
641da177e4SLinus Torvalds  */
651da177e4SLinus Torvalds #define DIGI_CMD_SET_BAUD_RATE			0	/* INB, OOB */
661da177e4SLinus Torvalds #define DIGI_CMD_SET_WORD_SIZE			1	/* INB, OOB */
671da177e4SLinus Torvalds #define DIGI_CMD_SET_PARITY			2	/* INB, OOB */
681da177e4SLinus Torvalds #define DIGI_CMD_SET_STOP_BITS			3	/* INB, OOB */
691da177e4SLinus Torvalds #define DIGI_CMD_SET_INPUT_FLOW_CONTROL		4	/* INB, OOB */
701da177e4SLinus Torvalds #define DIGI_CMD_SET_OUTPUT_FLOW_CONTROL	5	/* INB, OOB */
711da177e4SLinus Torvalds #define DIGI_CMD_SET_DTR_SIGNAL			6	/* INB, OOB */
721da177e4SLinus Torvalds #define DIGI_CMD_SET_RTS_SIGNAL			7	/* INB, OOB */
731da177e4SLinus Torvalds #define DIGI_CMD_READ_INPUT_SIGNALS		8	/*      OOB */
741da177e4SLinus Torvalds #define DIGI_CMD_IFLUSH_FIFO			9	/*      OOB */
751da177e4SLinus Torvalds #define DIGI_CMD_RECEIVE_ENABLE			10	/* INB, OOB */
761da177e4SLinus Torvalds #define DIGI_CMD_BREAK_CONTROL			11	/* INB, OOB */
771da177e4SLinus Torvalds #define DIGI_CMD_LOCAL_LOOPBACK			12	/* INB, OOB */
781da177e4SLinus Torvalds #define DIGI_CMD_TRANSMIT_IDLE			13	/* INB, OOB */
791da177e4SLinus Torvalds #define DIGI_CMD_READ_UART_REGISTER		14	/*      OOB */
801da177e4SLinus Torvalds #define DIGI_CMD_WRITE_UART_REGISTER		15	/* INB, OOB */
811da177e4SLinus Torvalds #define DIGI_CMD_AND_UART_REGISTER		16	/* INB, OOB */
821da177e4SLinus Torvalds #define DIGI_CMD_OR_UART_REGISTER		17	/* INB, OOB */
831da177e4SLinus Torvalds #define DIGI_CMD_SEND_DATA			18	/* INB      */
841da177e4SLinus Torvalds #define DIGI_CMD_RECEIVE_DATA			19	/* INB      */
851da177e4SLinus Torvalds #define DIGI_CMD_RECEIVE_DISABLE		20	/* INB      */
861da177e4SLinus Torvalds #define DIGI_CMD_GET_PORT_TYPE			21	/*      OOB */
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds /* baud rates */
891da177e4SLinus Torvalds #define DIGI_BAUD_50				0
901da177e4SLinus Torvalds #define DIGI_BAUD_75				1
911da177e4SLinus Torvalds #define DIGI_BAUD_110				2
921da177e4SLinus Torvalds #define DIGI_BAUD_150				3
931da177e4SLinus Torvalds #define DIGI_BAUD_200				4
941da177e4SLinus Torvalds #define DIGI_BAUD_300				5
951da177e4SLinus Torvalds #define DIGI_BAUD_600				6
961da177e4SLinus Torvalds #define DIGI_BAUD_1200				7
971da177e4SLinus Torvalds #define DIGI_BAUD_1800				8
981da177e4SLinus Torvalds #define DIGI_BAUD_2400				9
991da177e4SLinus Torvalds #define DIGI_BAUD_4800				10
1001da177e4SLinus Torvalds #define DIGI_BAUD_7200				11
1011da177e4SLinus Torvalds #define DIGI_BAUD_9600				12
1021da177e4SLinus Torvalds #define DIGI_BAUD_14400				13
1031da177e4SLinus Torvalds #define DIGI_BAUD_19200				14
1041da177e4SLinus Torvalds #define DIGI_BAUD_28800				15
1051da177e4SLinus Torvalds #define DIGI_BAUD_38400				16
1061da177e4SLinus Torvalds #define DIGI_BAUD_57600				17
1071da177e4SLinus Torvalds #define DIGI_BAUD_76800				18
1081da177e4SLinus Torvalds #define DIGI_BAUD_115200			19
1091da177e4SLinus Torvalds #define DIGI_BAUD_153600			20
1101da177e4SLinus Torvalds #define DIGI_BAUD_230400			21
1111da177e4SLinus Torvalds #define DIGI_BAUD_460800			22
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds /* arguments */
1141da177e4SLinus Torvalds #define DIGI_WORD_SIZE_5			0
1151da177e4SLinus Torvalds #define DIGI_WORD_SIZE_6			1
1161da177e4SLinus Torvalds #define DIGI_WORD_SIZE_7			2
1171da177e4SLinus Torvalds #define DIGI_WORD_SIZE_8			3
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds #define DIGI_PARITY_NONE			0
1201da177e4SLinus Torvalds #define DIGI_PARITY_ODD				1
1211da177e4SLinus Torvalds #define DIGI_PARITY_EVEN			2
1221da177e4SLinus Torvalds #define DIGI_PARITY_MARK			3
1231da177e4SLinus Torvalds #define DIGI_PARITY_SPACE			4
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds #define DIGI_STOP_BITS_1			0
1261da177e4SLinus Torvalds #define DIGI_STOP_BITS_2			1
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds #define DIGI_INPUT_FLOW_CONTROL_XON_XOFF	1
1291da177e4SLinus Torvalds #define DIGI_INPUT_FLOW_CONTROL_RTS		2
1301da177e4SLinus Torvalds #define DIGI_INPUT_FLOW_CONTROL_DTR		4
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds #define DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF	1
1331da177e4SLinus Torvalds #define DIGI_OUTPUT_FLOW_CONTROL_CTS		2
1341da177e4SLinus Torvalds #define DIGI_OUTPUT_FLOW_CONTROL_DSR		4
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds #define DIGI_DTR_INACTIVE			0
1371da177e4SLinus Torvalds #define DIGI_DTR_ACTIVE				1
1381da177e4SLinus Torvalds #define DIGI_DTR_INPUT_FLOW_CONTROL		2
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds #define DIGI_RTS_INACTIVE			0
1411da177e4SLinus Torvalds #define DIGI_RTS_ACTIVE				1
1421da177e4SLinus Torvalds #define DIGI_RTS_INPUT_FLOW_CONTROL		2
1431da177e4SLinus Torvalds #define DIGI_RTS_TOGGLE				3
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds #define DIGI_FLUSH_TX				1
1461da177e4SLinus Torvalds #define DIGI_FLUSH_RX				2
1471da177e4SLinus Torvalds #define DIGI_RESUME_TX				4 /* clears xoff condition */
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds #define DIGI_TRANSMIT_NOT_IDLE			0
1501da177e4SLinus Torvalds #define DIGI_TRANSMIT_IDLE			1
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds #define DIGI_DISABLE				0
1531da177e4SLinus Torvalds #define DIGI_ENABLE				1
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds #define DIGI_DEASSERT				0
1561da177e4SLinus Torvalds #define DIGI_ASSERT				1
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds /* in band status codes */
1591da177e4SLinus Torvalds #define DIGI_OVERRUN_ERROR			4
1601da177e4SLinus Torvalds #define DIGI_PARITY_ERROR			8
1611da177e4SLinus Torvalds #define DIGI_FRAMING_ERROR			16
1621da177e4SLinus Torvalds #define DIGI_BREAK_ERROR			32
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds /* out of band status */
1651da177e4SLinus Torvalds #define DIGI_NO_ERROR				0
1661da177e4SLinus Torvalds #define DIGI_BAD_FIRST_PARAMETER		1
1671da177e4SLinus Torvalds #define DIGI_BAD_SECOND_PARAMETER		2
1681da177e4SLinus Torvalds #define DIGI_INVALID_LINE			3
1691da177e4SLinus Torvalds #define DIGI_INVALID_OPCODE			4
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds /* input signals */
1721da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_SLOT		1
1731da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_ERR		2
1741da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_BUSY		4
1751da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_PE		8
1761da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_CTS		16
1771da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_DSR		32
1781da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_RI		64
1791da177e4SLinus Torvalds #define DIGI_READ_INPUT_SIGNALS_DCD		128
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds /* Structures */
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds struct digi_serial {
1851da177e4SLinus Torvalds 	spinlock_t ds_serial_lock;
1861da177e4SLinus Torvalds 	struct usb_serial_port *ds_oob_port;	/* out-of-band port */
1871da177e4SLinus Torvalds 	int ds_oob_port_num;			/* index of out-of-band port */
1881da177e4SLinus Torvalds 	int ds_device_started;
1891da177e4SLinus Torvalds };
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds struct digi_port {
1921da177e4SLinus Torvalds 	spinlock_t dp_port_lock;
1931da177e4SLinus Torvalds 	int dp_port_num;
1941da177e4SLinus Torvalds 	int dp_out_buf_len;
1951da177e4SLinus Torvalds 	unsigned char dp_out_buf[DIGI_OUT_BUF_SIZE];
1961da177e4SLinus Torvalds 	int dp_write_urb_in_use;
1971da177e4SLinus Torvalds 	unsigned int dp_modem_signals;
1981da177e4SLinus Torvalds 	int dp_transmit_idle;
1991da177e4SLinus Torvalds 	wait_queue_head_t dp_transmit_idle_wait;
2001da177e4SLinus Torvalds 	int dp_throttled;
2011da177e4SLinus Torvalds 	int dp_throttle_restart;
2021da177e4SLinus Torvalds 	wait_queue_head_t dp_flush_wait;
2031da177e4SLinus Torvalds 	wait_queue_head_t dp_close_wait;	/* wait queue for close */
2041da177e4SLinus Torvalds 	struct work_struct dp_wakeup_work;
205c4028958SDavid Howells 	struct usb_serial_port *dp_port;
2061da177e4SLinus Torvalds };
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds /* Local Function Declarations */
2101da177e4SLinus Torvalds 
211c4028958SDavid Howells static void digi_wakeup_write_lock(struct work_struct *work);
2121da177e4SLinus Torvalds static int digi_write_oob_command(struct usb_serial_port *port,
2131da177e4SLinus Torvalds 	unsigned char *buf, int count, int interruptible);
2141da177e4SLinus Torvalds static int digi_write_inb_command(struct usb_serial_port *port,
2151da177e4SLinus Torvalds 	unsigned char *buf, int count, unsigned long timeout);
2161da177e4SLinus Torvalds static int digi_set_modem_signals(struct usb_serial_port *port,
2171da177e4SLinus Torvalds 	unsigned int modem_signals, int interruptible);
2181da177e4SLinus Torvalds static int digi_transmit_idle(struct usb_serial_port *port,
2191da177e4SLinus Torvalds 	unsigned long timeout);
22095da310eSAlan Cox static void digi_rx_throttle(struct tty_struct *tty);
22195da310eSAlan Cox static void digi_rx_unthrottle(struct tty_struct *tty);
22295da310eSAlan Cox static void digi_set_termios(struct tty_struct *tty,
22395da310eSAlan Cox 		struct usb_serial_port *port, struct ktermios *old_termios);
22495da310eSAlan Cox static void digi_break_ctl(struct tty_struct *tty, int break_state);
22560b33c13SAlan Cox static int digi_tiocmget(struct tty_struct *tty);
22620b9d177SAlan Cox static int digi_tiocmset(struct tty_struct *tty, unsigned int set,
22720b9d177SAlan Cox 		unsigned int clear);
22895da310eSAlan Cox static int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
22995da310eSAlan Cox 		const unsigned char *buf, int count);
2307d12e780SDavid Howells static void digi_write_bulk_callback(struct urb *urb);
23195da310eSAlan Cox static int digi_write_room(struct tty_struct *tty);
23295da310eSAlan Cox static int digi_chars_in_buffer(struct tty_struct *tty);
233a509a7e4SAlan Cox static int digi_open(struct tty_struct *tty, struct usb_serial_port *port);
234335f8514SAlan Cox static void digi_close(struct usb_serial_port *port);
235335f8514SAlan Cox static void digi_dtr_rts(struct usb_serial_port *port, int on);
2361da177e4SLinus Torvalds static int digi_startup_device(struct usb_serial *serial);
2371da177e4SLinus Torvalds static int digi_startup(struct usb_serial *serial);
238f9c99bb8SAlan Stern static void digi_disconnect(struct usb_serial *serial);
239f9c99bb8SAlan Stern static void digi_release(struct usb_serial *serial);
240fb44ff85SJohan Hovold static int digi_port_probe(struct usb_serial_port *port);
241fb44ff85SJohan Hovold static int digi_port_remove(struct usb_serial_port *port);
2427d12e780SDavid Howells static void digi_read_bulk_callback(struct urb *urb);
2431da177e4SLinus Torvalds static int digi_read_inb_callback(struct urb *urb);
2441da177e4SLinus Torvalds static int digi_read_oob_callback(struct urb *urb);
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 
2477d40d7e8SNémeth Márton static const struct usb_device_id id_table_combined[] = {
2481da177e4SLinus Torvalds 	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
2491da177e4SLinus Torvalds 	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
2501da177e4SLinus Torvalds 	{ }						/* Terminating entry */
2511da177e4SLinus Torvalds };
2521da177e4SLinus Torvalds 
2537d40d7e8SNémeth Márton static const struct usb_device_id id_table_2[] = {
2541da177e4SLinus Torvalds 	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
2551da177e4SLinus Torvalds 	{ }						/* Terminating entry */
2561da177e4SLinus Torvalds };
2571da177e4SLinus Torvalds 
2587d40d7e8SNémeth Márton static const struct usb_device_id id_table_4[] = {
2591da177e4SLinus Torvalds 	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
2601da177e4SLinus Torvalds 	{ }						/* Terminating entry */
2611da177e4SLinus Torvalds };
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds MODULE_DEVICE_TABLE(usb, id_table_combined);
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds /* device info needed for the Digi serial converter */
2661da177e4SLinus Torvalds 
267ea65370dSGreg Kroah-Hartman static struct usb_serial_driver digi_acceleport_2_device = {
26818fcac35SGreg Kroah-Hartman 	.driver = {
2691da177e4SLinus Torvalds 		.owner =		THIS_MODULE,
270269bda1cSGreg Kroah-Hartman 		.name =			"digi_2",
27118fcac35SGreg Kroah-Hartman 	},
272269bda1cSGreg Kroah-Hartman 	.description =			"Digi 2 port USB adapter",
2731da177e4SLinus Torvalds 	.id_table =			id_table_2,
2741da177e4SLinus Torvalds 	.num_ports =			3,
2751da177e4SLinus Torvalds 	.open =				digi_open,
2761da177e4SLinus Torvalds 	.close =			digi_close,
277335f8514SAlan Cox 	.dtr_rts =			digi_dtr_rts,
2781da177e4SLinus Torvalds 	.write =			digi_write,
2791da177e4SLinus Torvalds 	.write_room =			digi_write_room,
2801da177e4SLinus Torvalds 	.write_bulk_callback = 		digi_write_bulk_callback,
2811da177e4SLinus Torvalds 	.read_bulk_callback =		digi_read_bulk_callback,
2821da177e4SLinus Torvalds 	.chars_in_buffer =		digi_chars_in_buffer,
2831da177e4SLinus Torvalds 	.throttle =			digi_rx_throttle,
2841da177e4SLinus Torvalds 	.unthrottle =			digi_rx_unthrottle,
2851da177e4SLinus Torvalds 	.set_termios =			digi_set_termios,
2861da177e4SLinus Torvalds 	.break_ctl =			digi_break_ctl,
2871da177e4SLinus Torvalds 	.tiocmget =			digi_tiocmget,
2881da177e4SLinus Torvalds 	.tiocmset =			digi_tiocmset,
2891da177e4SLinus Torvalds 	.attach =			digi_startup,
290f9c99bb8SAlan Stern 	.disconnect =			digi_disconnect,
291f9c99bb8SAlan Stern 	.release =			digi_release,
292fb44ff85SJohan Hovold 	.port_probe =			digi_port_probe,
293fb44ff85SJohan Hovold 	.port_remove =			digi_port_remove,
2941da177e4SLinus Torvalds };
2951da177e4SLinus Torvalds 
296ea65370dSGreg Kroah-Hartman static struct usb_serial_driver digi_acceleport_4_device = {
29718fcac35SGreg Kroah-Hartman 	.driver = {
2981da177e4SLinus Torvalds 		.owner =		THIS_MODULE,
299269bda1cSGreg Kroah-Hartman 		.name =			"digi_4",
30018fcac35SGreg Kroah-Hartman 	},
301269bda1cSGreg Kroah-Hartman 	.description =			"Digi 4 port USB adapter",
3021da177e4SLinus Torvalds 	.id_table =			id_table_4,
3031da177e4SLinus Torvalds 	.num_ports =			4,
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 
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 /*
3601da177e4SLinus Torvalds  *  Digi Wakeup Write
3611da177e4SLinus Torvalds  *
3621da177e4SLinus Torvalds  *  Wake up port, line discipline, and tty processes sleeping
3631da177e4SLinus Torvalds  *  on writes.
3641da177e4SLinus Torvalds  */
3651da177e4SLinus Torvalds 
366c4028958SDavid Howells static void digi_wakeup_write_lock(struct work_struct *work)
3671da177e4SLinus Torvalds {
36841ad427dSAlan Cox 	struct digi_port *priv =
36941ad427dSAlan Cox 			container_of(work, struct digi_port, dp_wakeup_work);
370c4028958SDavid Howells 	struct usb_serial_port *port = priv->dp_port;
3711da177e4SLinus Torvalds 	unsigned long flags;
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
3746aad04f2SJiri Slaby 	tty_port_tty_wakeup(&port->port);
3751da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
3761da177e4SLinus Torvalds }
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds /*
3791da177e4SLinus Torvalds  *  Digi Write OOB Command
3801da177e4SLinus Torvalds  *
3811da177e4SLinus Torvalds  *  Write commands on the out of band port.  Commands are 4
3821da177e4SLinus Torvalds  *  bytes each, multiple commands can be sent at once, and
3831da177e4SLinus Torvalds  *  no command will be split across USB packets.  Returns 0
3841da177e4SLinus Torvalds  *  if successful, -EINTR if interrupted while sleeping and
3851da177e4SLinus Torvalds  *  the interruptible flag is true, or a negative error
3861da177e4SLinus Torvalds  *  returned by usb_submit_urb.
3871da177e4SLinus Torvalds  */
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds static int digi_write_oob_command(struct usb_serial_port *port,
3901da177e4SLinus Torvalds 	unsigned char *buf, int count, int interruptible)
3911da177e4SLinus Torvalds {
3921da177e4SLinus Torvalds 	int ret = 0;
3931da177e4SLinus Torvalds 	int len;
3941da177e4SLinus Torvalds 	struct usb_serial_port *oob_port = (struct usb_serial_port *)((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
3951da177e4SLinus Torvalds 	struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
3961da177e4SLinus Torvalds 	unsigned long flags = 0;
3971da177e4SLinus Torvalds 
398f9dfbebbSGreg Kroah-Hartman 	dev_dbg(&port->dev,
399f9dfbebbSGreg Kroah-Hartman 		"digi_write_oob_command: TOP: port=%d, count=%d\n",
400f9dfbebbSGreg Kroah-Hartman 		oob_priv->dp_port_num, count);
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
4031da177e4SLinus Torvalds 	while (count > 0) {
40450de36f7SGreg Kroah-Hartman 		while (oob_priv->dp_write_urb_in_use) {
4051da177e4SLinus Torvalds 			cond_wait_interruptible_timeout_irqrestore(
4061da177e4SLinus Torvalds 				&oob_port->write_wait, DIGI_RETRY_TIMEOUT,
4071da177e4SLinus Torvalds 				&oob_priv->dp_port_lock, flags);
408c6d61269SAlan Cox 			if (interruptible && signal_pending(current))
409c6d61269SAlan Cox 				return -EINTR;
4101da177e4SLinus Torvalds 			spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
4111da177e4SLinus Torvalds 		}
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 		/* len must be a multiple of 4, so commands are not split */
4141da177e4SLinus Torvalds 		len = min(count, oob_port->bulk_out_size);
4151da177e4SLinus Torvalds 		if (len > 4)
4161da177e4SLinus Torvalds 			len &= ~3;
4171da177e4SLinus Torvalds 		memcpy(oob_port->write_urb->transfer_buffer, buf, len);
4181da177e4SLinus Torvalds 		oob_port->write_urb->transfer_buffer_length = len;
41941ad427dSAlan Cox 		ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
42041ad427dSAlan Cox 		if (ret == 0) {
4211da177e4SLinus Torvalds 			oob_priv->dp_write_urb_in_use = 1;
4221da177e4SLinus Torvalds 			count -= len;
4231da177e4SLinus Torvalds 			buf += len;
4241da177e4SLinus Torvalds 		}
4251da177e4SLinus Torvalds 	}
4261da177e4SLinus Torvalds 	spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
427c6d61269SAlan Cox 	if (ret)
428194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n",
429194343d9SGreg Kroah-Hartman 			__func__, ret);
430c6d61269SAlan Cox 	return ret;
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds }
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds /*
4361da177e4SLinus Torvalds  *  Digi Write In Band Command
4371da177e4SLinus Torvalds  *
4381da177e4SLinus Torvalds  *  Write commands on the given port.  Commands are 4
4391da177e4SLinus Torvalds  *  bytes each, multiple commands can be sent at once, and
4401da177e4SLinus Torvalds  *  no command will be split across USB packets.  If timeout
4411da177e4SLinus Torvalds  *  is non-zero, write in band command will return after
4421da177e4SLinus Torvalds  *  waiting unsuccessfully for the URB status to clear for
4431da177e4SLinus Torvalds  *  timeout ticks.  Returns 0 if successful, or a negative
4441da177e4SLinus Torvalds  *  error returned by digi_write.
4451da177e4SLinus Torvalds  */
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds static int digi_write_inb_command(struct usb_serial_port *port,
4481da177e4SLinus Torvalds 	unsigned char *buf, int count, unsigned long timeout)
4491da177e4SLinus Torvalds {
4501da177e4SLinus Torvalds 	int ret = 0;
4511da177e4SLinus Torvalds 	int len;
4521da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
4531da177e4SLinus Torvalds 	unsigned char *data = port->write_urb->transfer_buffer;
4541da177e4SLinus Torvalds 	unsigned long flags = 0;
4551da177e4SLinus Torvalds 
456f9dfbebbSGreg Kroah-Hartman 	dev_dbg(&port->dev, "digi_write_inb_command: TOP: port=%d, count=%d\n",
457c6d61269SAlan Cox 		priv->dp_port_num, count);
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	if (timeout)
4601da177e4SLinus Torvalds 		timeout += jiffies;
4611da177e4SLinus Torvalds 	else
4621da177e4SLinus Torvalds 		timeout = ULONG_MAX;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
4651da177e4SLinus Torvalds 	while (count > 0 && ret == 0) {
46650de36f7SGreg Kroah-Hartman 		while (priv->dp_write_urb_in_use &&
46750de36f7SGreg Kroah-Hartman 		       time_before(jiffies, timeout)) {
4681da177e4SLinus Torvalds 			cond_wait_interruptible_timeout_irqrestore(
4691da177e4SLinus Torvalds 				&port->write_wait, DIGI_RETRY_TIMEOUT,
4701da177e4SLinus Torvalds 				&priv->dp_port_lock, flags);
471c6d61269SAlan Cox 			if (signal_pending(current))
472c6d61269SAlan Cox 				return -EINTR;
4731da177e4SLinus Torvalds 			spin_lock_irqsave(&priv->dp_port_lock, flags);
4741da177e4SLinus Torvalds 		}
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 		/* len must be a multiple of 4 and small enough to */
4771da177e4SLinus Torvalds 		/* guarantee the write will send buffered data first, */
4781da177e4SLinus Torvalds 		/* so commands are in order with data and not split */
4791da177e4SLinus Torvalds 		len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
4801da177e4SLinus Torvalds 		if (len > 4)
4811da177e4SLinus Torvalds 			len &= ~3;
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 		/* write any buffered data first */
4841da177e4SLinus Torvalds 		if (priv->dp_out_buf_len > 0) {
4851da177e4SLinus Torvalds 			data[0] = DIGI_CMD_SEND_DATA;
4861da177e4SLinus Torvalds 			data[1] = priv->dp_out_buf_len;
4871da177e4SLinus Torvalds 			memcpy(data + 2, priv->dp_out_buf,
4881da177e4SLinus Torvalds 				priv->dp_out_buf_len);
4891da177e4SLinus Torvalds 			memcpy(data + 2 + priv->dp_out_buf_len, buf, len);
4901da177e4SLinus Torvalds 			port->write_urb->transfer_buffer_length
4911da177e4SLinus Torvalds 				= priv->dp_out_buf_len + 2 + len;
4921da177e4SLinus Torvalds 		} else {
4931da177e4SLinus Torvalds 			memcpy(data, buf, len);
4941da177e4SLinus Torvalds 			port->write_urb->transfer_buffer_length = len;
4951da177e4SLinus Torvalds 		}
4961da177e4SLinus Torvalds 
49741ad427dSAlan Cox 		ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
49841ad427dSAlan Cox 		if (ret == 0) {
4991da177e4SLinus Torvalds 			priv->dp_write_urb_in_use = 1;
5001da177e4SLinus Torvalds 			priv->dp_out_buf_len = 0;
5011da177e4SLinus Torvalds 			count -= len;
5021da177e4SLinus Torvalds 			buf += len;
5031da177e4SLinus Torvalds 		}
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	}
5061da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
5071da177e4SLinus Torvalds 
508c6d61269SAlan Cox 	if (ret)
509194343d9SGreg Kroah-Hartman 		dev_err(&port->dev,
510194343d9SGreg Kroah-Hartman 			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
511441b62c1SHarvey Harrison 			__func__, ret, priv->dp_port_num);
512c6d61269SAlan Cox 	return ret;
5131da177e4SLinus Torvalds }
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds /*
5171da177e4SLinus Torvalds  *  Digi Set Modem Signals
5181da177e4SLinus Torvalds  *
5191da177e4SLinus Torvalds  *  Sets or clears DTR and RTS on the port, according to the
5201da177e4SLinus Torvalds  *  modem_signals argument.  Use TIOCM_DTR and TIOCM_RTS flags
5211da177e4SLinus Torvalds  *  for the modem_signals argument.  Returns 0 if successful,
5221da177e4SLinus Torvalds  *  -EINTR if interrupted while sleeping, or a non-zero error
5231da177e4SLinus Torvalds  *  returned by usb_submit_urb.
5241da177e4SLinus Torvalds  */
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds static int digi_set_modem_signals(struct usb_serial_port *port,
5271da177e4SLinus Torvalds 	unsigned int modem_signals, int interruptible)
5281da177e4SLinus Torvalds {
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds 	int ret;
5311da177e4SLinus Torvalds 	struct digi_port *port_priv = usb_get_serial_port_data(port);
5321da177e4SLinus Torvalds 	struct usb_serial_port *oob_port = (struct usb_serial_port *) ((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
5331da177e4SLinus Torvalds 	struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
5341da177e4SLinus Torvalds 	unsigned char *data = oob_port->write_urb->transfer_buffer;
5351da177e4SLinus Torvalds 	unsigned long flags = 0;
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 
538f9dfbebbSGreg Kroah-Hartman 	dev_dbg(&port->dev,
539f9dfbebbSGreg Kroah-Hartman 		"digi_set_modem_signals: TOP: port=%d, modem_signals=0x%x\n",
5401da177e4SLinus Torvalds 		port_priv->dp_port_num, modem_signals);
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
5431da177e4SLinus Torvalds 	spin_lock(&port_priv->dp_port_lock);
5441da177e4SLinus Torvalds 
54550de36f7SGreg Kroah-Hartman 	while (oob_priv->dp_write_urb_in_use) {
5461da177e4SLinus Torvalds 		spin_unlock(&port_priv->dp_port_lock);
5471da177e4SLinus Torvalds 		cond_wait_interruptible_timeout_irqrestore(
5481da177e4SLinus Torvalds 			&oob_port->write_wait, DIGI_RETRY_TIMEOUT,
5491da177e4SLinus Torvalds 			&oob_priv->dp_port_lock, flags);
550c6d61269SAlan Cox 		if (interruptible && signal_pending(current))
551c6d61269SAlan Cox 			return -EINTR;
5521da177e4SLinus Torvalds 		spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
5531da177e4SLinus Torvalds 		spin_lock(&port_priv->dp_port_lock);
5541da177e4SLinus Torvalds 	}
5551da177e4SLinus Torvalds 	data[0] = DIGI_CMD_SET_DTR_SIGNAL;
5561da177e4SLinus Torvalds 	data[1] = port_priv->dp_port_num;
55741ad427dSAlan Cox 	data[2] = (modem_signals & TIOCM_DTR) ?
55841ad427dSAlan Cox 					DIGI_DTR_ACTIVE : DIGI_DTR_INACTIVE;
5591da177e4SLinus Torvalds 	data[3] = 0;
5601da177e4SLinus Torvalds 	data[4] = DIGI_CMD_SET_RTS_SIGNAL;
5611da177e4SLinus Torvalds 	data[5] = port_priv->dp_port_num;
56241ad427dSAlan Cox 	data[6] = (modem_signals & TIOCM_RTS) ?
56341ad427dSAlan Cox 					DIGI_RTS_ACTIVE : DIGI_RTS_INACTIVE;
5641da177e4SLinus Torvalds 	data[7] = 0;
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	oob_port->write_urb->transfer_buffer_length = 8;
5671da177e4SLinus Torvalds 
56841ad427dSAlan Cox 	ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
56941ad427dSAlan Cox 	if (ret == 0) {
5701da177e4SLinus Torvalds 		oob_priv->dp_write_urb_in_use = 1;
5711da177e4SLinus Torvalds 		port_priv->dp_modem_signals =
5721da177e4SLinus Torvalds 			(port_priv->dp_modem_signals&~(TIOCM_DTR|TIOCM_RTS))
5731da177e4SLinus Torvalds 			| (modem_signals&(TIOCM_DTR|TIOCM_RTS));
5741da177e4SLinus Torvalds 	}
5751da177e4SLinus Torvalds 	spin_unlock(&port_priv->dp_port_lock);
5761da177e4SLinus Torvalds 	spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
577c6d61269SAlan Cox 	if (ret)
578194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n",
579194343d9SGreg Kroah-Hartman 			__func__, ret);
580c6d61269SAlan Cox 	return ret;
5811da177e4SLinus Torvalds }
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds /*
5841da177e4SLinus Torvalds  *  Digi Transmit Idle
5851da177e4SLinus Torvalds  *
5861da177e4SLinus Torvalds  *  Digi transmit idle waits, up to timeout ticks, for the transmitter
5871da177e4SLinus Torvalds  *  to go idle.  It returns 0 if successful or a negative error.
5881da177e4SLinus Torvalds  *
5891da177e4SLinus Torvalds  *  There are race conditions here if more than one process is calling
5901da177e4SLinus Torvalds  *  digi_transmit_idle on the same port at the same time.  However, this
5911da177e4SLinus Torvalds  *  is only called from close, and only one process can be in close on a
5921da177e4SLinus Torvalds  *  port at a time, so its ok.
5931da177e4SLinus Torvalds  */
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds static int digi_transmit_idle(struct usb_serial_port *port,
5961da177e4SLinus Torvalds 	unsigned long timeout)
5971da177e4SLinus Torvalds {
5981da177e4SLinus Torvalds 	int ret;
5991da177e4SLinus Torvalds 	unsigned char buf[2];
6001da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
6011da177e4SLinus Torvalds 	unsigned long flags = 0;
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
6041da177e4SLinus Torvalds 	priv->dp_transmit_idle = 0;
6051da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	buf[0] = DIGI_CMD_TRANSMIT_IDLE;
6081da177e4SLinus Torvalds 	buf[1] = 0;
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	timeout += jiffies;
6111da177e4SLinus Torvalds 
61241ad427dSAlan Cox 	ret = digi_write_inb_command(port, buf, 2, timeout - jiffies);
61341ad427dSAlan Cox 	if (ret != 0)
614c6d61269SAlan Cox 		return ret;
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 	while (time_before(jiffies, timeout) && !priv->dp_transmit_idle) {
6191da177e4SLinus Torvalds 		cond_wait_interruptible_timeout_irqrestore(
6201da177e4SLinus Torvalds 			&priv->dp_transmit_idle_wait, DIGI_RETRY_TIMEOUT,
6211da177e4SLinus Torvalds 			&priv->dp_port_lock, flags);
622c6d61269SAlan Cox 		if (signal_pending(current))
623c6d61269SAlan Cox 			return -EINTR;
6241da177e4SLinus Torvalds 		spin_lock_irqsave(&priv->dp_port_lock, flags);
6251da177e4SLinus Torvalds 	}
6261da177e4SLinus Torvalds 	priv->dp_transmit_idle = 0;
6271da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
628c6d61269SAlan Cox 	return 0;
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds }
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds 
63395da310eSAlan Cox static void digi_rx_throttle(struct tty_struct *tty)
6341da177e4SLinus Torvalds {
6351da177e4SLinus Torvalds 	unsigned long flags;
63695da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
6371da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	/* stop receiving characters by not resubmitting the read urb */
6401da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
6411da177e4SLinus Torvalds 	priv->dp_throttled = 1;
6421da177e4SLinus Torvalds 	priv->dp_throttle_restart = 0;
6431da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 
64795da310eSAlan Cox static void digi_rx_unthrottle(struct tty_struct *tty)
6481da177e4SLinus Torvalds {
6491da177e4SLinus Torvalds 	int ret = 0;
6501da177e4SLinus Torvalds 	unsigned long flags;
65195da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
6521da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	/* restart read chain */
6575833041fSJohan Hovold 	if (priv->dp_throttle_restart)
6581da177e4SLinus Torvalds 		ret = usb_submit_urb(port->read_urb, GFP_ATOMIC);
6591da177e4SLinus Torvalds 
660ba6b702fSJohan Hovold 	/* turn throttle off */
661ba6b702fSJohan Hovold 	priv->dp_throttled = 0;
662ba6b702fSJohan Hovold 	priv->dp_throttle_restart = 0;
663ba6b702fSJohan Hovold 
6641da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
6651da177e4SLinus Torvalds 
666c6d61269SAlan Cox 	if (ret)
667194343d9SGreg Kroah-Hartman 		dev_err(&port->dev,
668194343d9SGreg Kroah-Hartman 			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
669441b62c1SHarvey Harrison 			__func__, ret, priv->dp_port_num);
6701da177e4SLinus Torvalds }
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 
67395da310eSAlan Cox static void digi_set_termios(struct tty_struct *tty,
67495da310eSAlan Cox 		struct usb_serial_port *port, struct ktermios *old_termios)
6751da177e4SLinus Torvalds {
6761da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
677f9dfbebbSGreg Kroah-Hartman 	struct device *dev = &port->dev;
678adc8d746SAlan Cox 	unsigned int iflag = tty->termios.c_iflag;
679adc8d746SAlan Cox 	unsigned int cflag = tty->termios.c_cflag;
6801da177e4SLinus Torvalds 	unsigned int old_iflag = old_termios->c_iflag;
6811da177e4SLinus Torvalds 	unsigned int old_cflag = old_termios->c_cflag;
6821da177e4SLinus Torvalds 	unsigned char buf[32];
6831da177e4SLinus Torvalds 	unsigned int modem_signals;
6841da177e4SLinus Torvalds 	int arg, ret;
6851da177e4SLinus Torvalds 	int i = 0;
686c6d61269SAlan Cox 	speed_t baud;
6871da177e4SLinus Torvalds 
688f9dfbebbSGreg Kroah-Hartman 	dev_dbg(dev,
689f9dfbebbSGreg Kroah-Hartman 		"digi_set_termios: TOP: port=%d, iflag=0x%x, old_iflag=0x%x, cflag=0x%x, old_cflag=0x%x\n",
690f9dfbebbSGreg Kroah-Hartman 		priv->dp_port_num, iflag, old_iflag, cflag, old_cflag);
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	/* set baud rate */
69341ad427dSAlan Cox 	baud = tty_get_baud_rate(tty);
69441ad427dSAlan Cox 	if (baud != tty_termios_baud_rate(old_termios)) {
6951da177e4SLinus Torvalds 		arg = -1;
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 		/* reassert DTR and (maybe) RTS on transition from B0 */
6981da177e4SLinus Torvalds 		if ((old_cflag & CBAUD) == B0) {
6991da177e4SLinus Torvalds 			/* don't set RTS if using hardware flow control */
7001da177e4SLinus Torvalds 			/* and throttling input */
7011da177e4SLinus Torvalds 			modem_signals = TIOCM_DTR;
702*9db276f8SPeter Hurley 			if (!C_CRTSCTS(tty) ||
703c6d61269SAlan Cox 			    !test_bit(TTY_THROTTLED, &tty->flags))
7041da177e4SLinus Torvalds 				modem_signals |= TIOCM_RTS;
7051da177e4SLinus Torvalds 			digi_set_modem_signals(port, modem_signals, 1);
7061da177e4SLinus Torvalds 		}
707c6d61269SAlan Cox 		switch (baud) {
7081da177e4SLinus Torvalds 		/* drop DTR and RTS on transition to B0 */
709c6d61269SAlan Cox 		case 0: digi_set_modem_signals(port, 0, 1); break;
710c6d61269SAlan Cox 		case 50: arg = DIGI_BAUD_50; break;
711c6d61269SAlan Cox 		case 75: arg = DIGI_BAUD_75; break;
712c6d61269SAlan Cox 		case 110: arg = DIGI_BAUD_110; break;
713c6d61269SAlan Cox 		case 150: arg = DIGI_BAUD_150; break;
714c6d61269SAlan Cox 		case 200: arg = DIGI_BAUD_200; break;
715c6d61269SAlan Cox 		case 300: arg = DIGI_BAUD_300; break;
716c6d61269SAlan Cox 		case 600: arg = DIGI_BAUD_600; break;
717c6d61269SAlan Cox 		case 1200: arg = DIGI_BAUD_1200; break;
718c6d61269SAlan Cox 		case 1800: arg = DIGI_BAUD_1800; break;
719c6d61269SAlan Cox 		case 2400: arg = DIGI_BAUD_2400; break;
720c6d61269SAlan Cox 		case 4800: arg = DIGI_BAUD_4800; break;
721c6d61269SAlan Cox 		case 9600: arg = DIGI_BAUD_9600; break;
722c6d61269SAlan Cox 		case 19200: arg = DIGI_BAUD_19200; break;
723c6d61269SAlan Cox 		case 38400: arg = DIGI_BAUD_38400; break;
724c6d61269SAlan Cox 		case 57600: arg = DIGI_BAUD_57600; break;
725c6d61269SAlan Cox 		case 115200: arg = DIGI_BAUD_115200; break;
726c6d61269SAlan Cox 		case 230400: arg = DIGI_BAUD_230400; break;
727c6d61269SAlan Cox 		case 460800: arg = DIGI_BAUD_460800; break;
7281da177e4SLinus Torvalds 		default:
729c6d61269SAlan Cox 			arg = DIGI_BAUD_9600;
730c6d61269SAlan Cox 			baud = 9600;
7311da177e4SLinus Torvalds 			break;
7321da177e4SLinus Torvalds 		}
7331da177e4SLinus Torvalds 		if (arg != -1) {
7341da177e4SLinus Torvalds 			buf[i++] = DIGI_CMD_SET_BAUD_RATE;
7351da177e4SLinus Torvalds 			buf[i++] = priv->dp_port_num;
7361da177e4SLinus Torvalds 			buf[i++] = arg;
7371da177e4SLinus Torvalds 			buf[i++] = 0;
7381da177e4SLinus Torvalds 		}
7391da177e4SLinus Torvalds 	}
7401da177e4SLinus Torvalds 	/* set parity */
741adc8d746SAlan Cox 	tty->termios.c_cflag &= ~CMSPAR;
7427fa36a99SAlan Cox 
7431da177e4SLinus Torvalds 	if ((cflag&(PARENB|PARODD)) != (old_cflag&(PARENB|PARODD))) {
744c6d61269SAlan Cox 		if (cflag&PARENB) {
745c6d61269SAlan Cox 			if (cflag&PARODD)
7461da177e4SLinus Torvalds 				arg = DIGI_PARITY_ODD;
7471da177e4SLinus Torvalds 			else
7481da177e4SLinus Torvalds 				arg = DIGI_PARITY_EVEN;
7491da177e4SLinus Torvalds 		} else {
7501da177e4SLinus Torvalds 			arg = DIGI_PARITY_NONE;
7511da177e4SLinus Torvalds 		}
7521da177e4SLinus Torvalds 		buf[i++] = DIGI_CMD_SET_PARITY;
7531da177e4SLinus Torvalds 		buf[i++] = priv->dp_port_num;
7541da177e4SLinus Torvalds 		buf[i++] = arg;
7551da177e4SLinus Torvalds 		buf[i++] = 0;
7561da177e4SLinus Torvalds 	}
7571da177e4SLinus Torvalds 	/* set word size */
7581da177e4SLinus Torvalds 	if ((cflag&CSIZE) != (old_cflag&CSIZE)) {
7591da177e4SLinus Torvalds 		arg = -1;
760c6d61269SAlan Cox 		switch (cflag&CSIZE) {
7611da177e4SLinus Torvalds 		case CS5: arg = DIGI_WORD_SIZE_5; break;
7621da177e4SLinus Torvalds 		case CS6: arg = DIGI_WORD_SIZE_6; break;
7631da177e4SLinus Torvalds 		case CS7: arg = DIGI_WORD_SIZE_7; break;
7641da177e4SLinus Torvalds 		case CS8: arg = DIGI_WORD_SIZE_8; break;
7651da177e4SLinus Torvalds 		default:
766f9dfbebbSGreg Kroah-Hartman 			dev_dbg(dev,
767f9dfbebbSGreg Kroah-Hartman 				"digi_set_termios: can't handle word size %d\n",
7681da177e4SLinus Torvalds 				(cflag&CSIZE));
7691da177e4SLinus Torvalds 			break;
7701da177e4SLinus Torvalds 		}
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 		if (arg != -1) {
7731da177e4SLinus Torvalds 			buf[i++] = DIGI_CMD_SET_WORD_SIZE;
7741da177e4SLinus Torvalds 			buf[i++] = priv->dp_port_num;
7751da177e4SLinus Torvalds 			buf[i++] = arg;
7761da177e4SLinus Torvalds 			buf[i++] = 0;
7771da177e4SLinus Torvalds 		}
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 	}
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	/* set stop bits */
7821da177e4SLinus Torvalds 	if ((cflag&CSTOPB) != (old_cflag&CSTOPB)) {
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 		if ((cflag&CSTOPB))
7851da177e4SLinus Torvalds 			arg = DIGI_STOP_BITS_2;
7861da177e4SLinus Torvalds 		else
7871da177e4SLinus Torvalds 			arg = DIGI_STOP_BITS_1;
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 		buf[i++] = DIGI_CMD_SET_STOP_BITS;
7901da177e4SLinus Torvalds 		buf[i++] = priv->dp_port_num;
7911da177e4SLinus Torvalds 		buf[i++] = arg;
7921da177e4SLinus Torvalds 		buf[i++] = 0;
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	}
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	/* set input flow control */
7971da177e4SLinus Torvalds 	if ((iflag&IXOFF) != (old_iflag&IXOFF)
7981da177e4SLinus Torvalds 	    || (cflag&CRTSCTS) != (old_cflag&CRTSCTS)) {
7991da177e4SLinus Torvalds 		arg = 0;
800c6d61269SAlan Cox 		if (iflag&IXOFF)
8011da177e4SLinus Torvalds 			arg |= DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
8021da177e4SLinus Torvalds 		else
8031da177e4SLinus Torvalds 			arg &= ~DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
8041da177e4SLinus Torvalds 
805c6d61269SAlan Cox 		if (cflag&CRTSCTS) {
8061da177e4SLinus Torvalds 			arg |= DIGI_INPUT_FLOW_CONTROL_RTS;
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 			/* On USB-4 it is necessary to assert RTS prior */
8091da177e4SLinus Torvalds 			/* to selecting RTS input flow control.  */
8101da177e4SLinus Torvalds 			buf[i++] = DIGI_CMD_SET_RTS_SIGNAL;
8111da177e4SLinus Torvalds 			buf[i++] = priv->dp_port_num;
8121da177e4SLinus Torvalds 			buf[i++] = DIGI_RTS_ACTIVE;
8131da177e4SLinus Torvalds 			buf[i++] = 0;
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 		} else {
8161da177e4SLinus Torvalds 			arg &= ~DIGI_INPUT_FLOW_CONTROL_RTS;
8171da177e4SLinus Torvalds 		}
8181da177e4SLinus Torvalds 		buf[i++] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
8191da177e4SLinus Torvalds 		buf[i++] = priv->dp_port_num;
8201da177e4SLinus Torvalds 		buf[i++] = arg;
8211da177e4SLinus Torvalds 		buf[i++] = 0;
8221da177e4SLinus Torvalds 	}
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	/* set output flow control */
8251da177e4SLinus Torvalds 	if ((iflag & IXON) != (old_iflag & IXON)
8261da177e4SLinus Torvalds 	    || (cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
8271da177e4SLinus Torvalds 		arg = 0;
828c6d61269SAlan Cox 		if (iflag & IXON)
8291da177e4SLinus Torvalds 			arg |= DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
8301da177e4SLinus Torvalds 		else
8311da177e4SLinus Torvalds 			arg &= ~DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
8321da177e4SLinus Torvalds 
833c6d61269SAlan Cox 		if (cflag & CRTSCTS) {
8341da177e4SLinus Torvalds 			arg |= DIGI_OUTPUT_FLOW_CONTROL_CTS;
8351da177e4SLinus Torvalds 		} else {
8361da177e4SLinus Torvalds 			arg &= ~DIGI_OUTPUT_FLOW_CONTROL_CTS;
8371da177e4SLinus Torvalds 		}
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 		buf[i++] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
8401da177e4SLinus Torvalds 		buf[i++] = priv->dp_port_num;
8411da177e4SLinus Torvalds 		buf[i++] = arg;
8421da177e4SLinus Torvalds 		buf[i++] = 0;
8431da177e4SLinus Torvalds 	}
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 	/* set receive enable/disable */
8461da177e4SLinus Torvalds 	if ((cflag & CREAD) != (old_cflag & CREAD)) {
847c6d61269SAlan Cox 		if (cflag & CREAD)
8481da177e4SLinus Torvalds 			arg = DIGI_ENABLE;
8491da177e4SLinus Torvalds 		else
8501da177e4SLinus Torvalds 			arg = DIGI_DISABLE;
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds 		buf[i++] = DIGI_CMD_RECEIVE_ENABLE;
8531da177e4SLinus Torvalds 		buf[i++] = priv->dp_port_num;
8541da177e4SLinus Torvalds 		buf[i++] = arg;
8551da177e4SLinus Torvalds 		buf[i++] = 0;
8561da177e4SLinus Torvalds 	}
85741ad427dSAlan Cox 	ret = digi_write_oob_command(port, buf, i, 1);
85841ad427dSAlan Cox 	if (ret != 0)
859f9dfbebbSGreg Kroah-Hartman 		dev_dbg(dev, "digi_set_termios: write oob failed, ret=%d\n", ret);
8607fa36a99SAlan Cox 	tty_encode_baud_rate(tty, baud, baud);
8611da177e4SLinus Torvalds }
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 
86495da310eSAlan Cox static void digi_break_ctl(struct tty_struct *tty, int break_state)
8651da177e4SLinus Torvalds {
86695da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
8671da177e4SLinus Torvalds 	unsigned char buf[4];
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	buf[0] = DIGI_CMD_BREAK_CONTROL;
8701da177e4SLinus Torvalds 	buf[1] = 2;				/* length */
8711da177e4SLinus Torvalds 	buf[2] = break_state ? 1 : 0;
8721da177e4SLinus Torvalds 	buf[3] = 0;				/* pad */
8731da177e4SLinus Torvalds 	digi_write_inb_command(port, buf, 4, 0);
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 
87760b33c13SAlan Cox static int digi_tiocmget(struct tty_struct *tty)
8781da177e4SLinus Torvalds {
87995da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
8801da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
8811da177e4SLinus Torvalds 	unsigned int val;
8821da177e4SLinus Torvalds 	unsigned long flags;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
8851da177e4SLinus Torvalds 	val = priv->dp_modem_signals;
8861da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
8871da177e4SLinus Torvalds 	return val;
8881da177e4SLinus Torvalds }
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 
89120b9d177SAlan Cox static int digi_tiocmset(struct tty_struct *tty,
8921da177e4SLinus Torvalds 					unsigned int set, unsigned int clear)
8931da177e4SLinus Torvalds {
89495da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
8951da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
8961da177e4SLinus Torvalds 	unsigned int val;
8971da177e4SLinus Torvalds 	unsigned long flags;
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
9001da177e4SLinus Torvalds 	val = (priv->dp_modem_signals & ~clear) | set;
9011da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
9021da177e4SLinus Torvalds 	return digi_set_modem_signals(port, val, 1);
9031da177e4SLinus Torvalds }
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 
90695da310eSAlan Cox static int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
90795da310eSAlan Cox 					const unsigned char *buf, int count)
9081da177e4SLinus Torvalds {
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 	int ret, data_len, new_len;
9111da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
9121da177e4SLinus Torvalds 	unsigned char *data = port->write_urb->transfer_buffer;
9131da177e4SLinus Torvalds 	unsigned long flags = 0;
9141da177e4SLinus Torvalds 
915f9dfbebbSGreg Kroah-Hartman 	dev_dbg(&port->dev,
916f9dfbebbSGreg Kroah-Hartman 		"digi_write: TOP: port=%d, count=%d, in_interrupt=%ld\n",
9171da177e4SLinus Torvalds 		priv->dp_port_num, count, in_interrupt());
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 	/* copy user data (which can sleep) before getting spin lock */
9201da177e4SLinus Torvalds 	count = min(count, port->bulk_out_size-2);
9211da177e4SLinus Torvalds 	count = min(64, count);
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds 	/* be sure only one write proceeds at a time */
9241da177e4SLinus Torvalds 	/* there are races on the port private buffer */
9251da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	/* wait for urb status clear to submit another urb */
92850de36f7SGreg Kroah-Hartman 	if (priv->dp_write_urb_in_use) {
9291da177e4SLinus Torvalds 		/* buffer data if count is 1 (probably put_char) if possible */
9301da177e4SLinus Torvalds 		if (count == 1 && priv->dp_out_buf_len < DIGI_OUT_BUF_SIZE) {
9311da177e4SLinus Torvalds 			priv->dp_out_buf[priv->dp_out_buf_len++] = *buf;
9321da177e4SLinus Torvalds 			new_len = 1;
9331da177e4SLinus Torvalds 		} else {
9341da177e4SLinus Torvalds 			new_len = 0;
9351da177e4SLinus Torvalds 		}
9361da177e4SLinus Torvalds 		spin_unlock_irqrestore(&priv->dp_port_lock, flags);
937c6d61269SAlan Cox 		return new_len;
9381da177e4SLinus Torvalds 	}
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds 	/* allow space for any buffered data and for new data, up to */
9411da177e4SLinus Torvalds 	/* transfer buffer size - 2 (for command and length bytes) */
9421da177e4SLinus Torvalds 	new_len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
9431da177e4SLinus Torvalds 	data_len = new_len + priv->dp_out_buf_len;
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	if (data_len == 0) {
9461da177e4SLinus Torvalds 		spin_unlock_irqrestore(&priv->dp_port_lock, flags);
947c6d61269SAlan Cox 		return 0;
9481da177e4SLinus Torvalds 	}
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 	port->write_urb->transfer_buffer_length = data_len+2;
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 	*data++ = DIGI_CMD_SEND_DATA;
9531da177e4SLinus Torvalds 	*data++ = data_len;
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	/* copy in buffered data first */
9561da177e4SLinus Torvalds 	memcpy(data, priv->dp_out_buf, priv->dp_out_buf_len);
9571da177e4SLinus Torvalds 	data += priv->dp_out_buf_len;
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds 	/* copy in new data */
9601da177e4SLinus Torvalds 	memcpy(data, buf, new_len);
9611da177e4SLinus Torvalds 
96241ad427dSAlan Cox 	ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
96341ad427dSAlan Cox 	if (ret == 0) {
9641da177e4SLinus Torvalds 		priv->dp_write_urb_in_use = 1;
9651da177e4SLinus Torvalds 		ret = new_len;
9661da177e4SLinus Torvalds 		priv->dp_out_buf_len = 0;
9671da177e4SLinus Torvalds 	}
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds 	/* return length of new data written, or error */
9701da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
971c6d61269SAlan Cox 	if (ret < 0)
97222a416c4SJohan Hovold 		dev_err_console(port,
973194343d9SGreg Kroah-Hartman 			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
974441b62c1SHarvey Harrison 			__func__, ret, priv->dp_port_num);
975f9dfbebbSGreg Kroah-Hartman 	dev_dbg(&port->dev, "digi_write: returning %d\n", ret);
976c6d61269SAlan Cox 	return ret;
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds }
9791da177e4SLinus Torvalds 
9807d12e780SDavid Howells static void digi_write_bulk_callback(struct urb *urb)
9811da177e4SLinus Torvalds {
9821da177e4SLinus Torvalds 
983cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
9841da177e4SLinus Torvalds 	struct usb_serial *serial;
9851da177e4SLinus Torvalds 	struct digi_port *priv;
9861da177e4SLinus Torvalds 	struct digi_serial *serial_priv;
9871da177e4SLinus Torvalds 	int ret = 0;
98885d75107SGreg Kroah-Hartman 	int status = urb->status;
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds 	/* port and serial sanity check */
9911da177e4SLinus Torvalds 	if (port == NULL || (priv = usb_get_serial_port_data(port)) == NULL) {
992109f34e7SJulia Lawall 		pr_err("%s: port or port->private is NULL, status=%d\n",
993441b62c1SHarvey Harrison 			__func__, status);
9941da177e4SLinus Torvalds 		return;
9951da177e4SLinus Torvalds 	}
9961da177e4SLinus Torvalds 	serial = port->serial;
9971da177e4SLinus Torvalds 	if (serial == NULL || (serial_priv = usb_get_serial_data(serial)) == NULL) {
998194343d9SGreg Kroah-Hartman 		dev_err(&port->dev,
999194343d9SGreg Kroah-Hartman 			"%s: serial or serial->private is NULL, status=%d\n",
1000441b62c1SHarvey Harrison 			__func__, status);
10011da177e4SLinus Torvalds 		return;
10021da177e4SLinus Torvalds 	}
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	/* handle oob callback */
10051da177e4SLinus Torvalds 	if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
1006f9dfbebbSGreg Kroah-Hartman 		dev_dbg(&port->dev, "digi_write_bulk_callback: oob callback\n");
10071da177e4SLinus Torvalds 		spin_lock(&priv->dp_port_lock);
10081da177e4SLinus Torvalds 		priv->dp_write_urb_in_use = 0;
10091da177e4SLinus Torvalds 		wake_up_interruptible(&port->write_wait);
10101da177e4SLinus Torvalds 		spin_unlock(&priv->dp_port_lock);
10111da177e4SLinus Torvalds 		return;
10121da177e4SLinus Torvalds 	}
10131da177e4SLinus Torvalds 
10141f87158eSAlan Stern 	/* try to send any buffered data on this port */
10151da177e4SLinus Torvalds 	spin_lock(&priv->dp_port_lock);
10161da177e4SLinus Torvalds 	priv->dp_write_urb_in_use = 0;
10171f87158eSAlan Stern 	if (priv->dp_out_buf_len > 0) {
10181da177e4SLinus Torvalds 		*((unsigned char *)(port->write_urb->transfer_buffer))
10191da177e4SLinus Torvalds 			= (unsigned char)DIGI_CMD_SEND_DATA;
10201da177e4SLinus Torvalds 		*((unsigned char *)(port->write_urb->transfer_buffer) + 1)
10211da177e4SLinus Torvalds 			= (unsigned char)priv->dp_out_buf_len;
102241ad427dSAlan Cox 		port->write_urb->transfer_buffer_length =
102341ad427dSAlan Cox 						priv->dp_out_buf_len + 2;
10241da177e4SLinus Torvalds 		memcpy(port->write_urb->transfer_buffer + 2, priv->dp_out_buf,
10251da177e4SLinus Torvalds 			priv->dp_out_buf_len);
102641ad427dSAlan Cox 		ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
102741ad427dSAlan Cox 		if (ret == 0) {
10281da177e4SLinus Torvalds 			priv->dp_write_urb_in_use = 1;
10291da177e4SLinus Torvalds 			priv->dp_out_buf_len = 0;
10301da177e4SLinus Torvalds 		}
10311da177e4SLinus Torvalds 	}
10321da177e4SLinus Torvalds 	/* wake up processes sleeping on writes immediately */
10336aad04f2SJiri Slaby 	tty_port_tty_wakeup(&port->port);
10341da177e4SLinus Torvalds 	/* also queue up a wakeup at scheduler time, in case we */
10351da177e4SLinus Torvalds 	/* lost the race in write_chan(). */
10361da177e4SLinus Torvalds 	schedule_work(&priv->dp_wakeup_work);
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	spin_unlock(&priv->dp_port_lock);
10391f87158eSAlan Stern 	if (ret && ret != -EPERM)
104022a416c4SJohan Hovold 		dev_err_console(port,
1041194343d9SGreg Kroah-Hartman 			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
1042441b62c1SHarvey Harrison 			__func__, ret, priv->dp_port_num);
10431da177e4SLinus Torvalds }
10441da177e4SLinus Torvalds 
104595da310eSAlan Cox static int digi_write_room(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);
104995da310eSAlan Cox 	int room;
10501da177e4SLinus Torvalds 	unsigned long flags = 0;
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->dp_port_lock, flags);
10531da177e4SLinus Torvalds 
105450de36f7SGreg Kroah-Hartman 	if (priv->dp_write_urb_in_use)
10551da177e4SLinus Torvalds 		room = 0;
10561da177e4SLinus Torvalds 	else
10571da177e4SLinus Torvalds 		room = port->bulk_out_size - 2 - priv->dp_out_buf_len;
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
1060f9dfbebbSGreg Kroah-Hartman 	dev_dbg(&port->dev, "digi_write_room: port=%d, room=%d\n", priv->dp_port_num, room);
1061c6d61269SAlan Cox 	return room;
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds }
10641da177e4SLinus Torvalds 
106595da310eSAlan Cox static int digi_chars_in_buffer(struct tty_struct *tty)
10661da177e4SLinus Torvalds {
106795da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
10681da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
10691da177e4SLinus Torvalds 
107050de36f7SGreg Kroah-Hartman 	if (priv->dp_write_urb_in_use) {
1071f9dfbebbSGreg Kroah-Hartman 		dev_dbg(&port->dev, "digi_chars_in_buffer: port=%d, chars=%d\n",
1072c6d61269SAlan Cox 			priv->dp_port_num, port->bulk_out_size - 2);
10731da177e4SLinus Torvalds 		/* return(port->bulk_out_size - 2); */
1074c6d61269SAlan Cox 		return 256;
10751da177e4SLinus Torvalds 	} else {
1076f9dfbebbSGreg Kroah-Hartman 		dev_dbg(&port->dev, "digi_chars_in_buffer: port=%d, chars=%d\n",
1077c6d61269SAlan Cox 			priv->dp_port_num, priv->dp_out_buf_len);
1078c6d61269SAlan Cox 		return priv->dp_out_buf_len;
10791da177e4SLinus Torvalds 	}
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds }
10821da177e4SLinus Torvalds 
1083335f8514SAlan Cox static void digi_dtr_rts(struct usb_serial_port *port, int on)
1084335f8514SAlan Cox {
1085335f8514SAlan Cox 	/* Adjust DTR and RTS */
1086335f8514SAlan Cox 	digi_set_modem_signals(port, on * (TIOCM_DTR|TIOCM_RTS), 1);
1087335f8514SAlan Cox }
1088335f8514SAlan Cox 
1089a509a7e4SAlan Cox static int digi_open(struct tty_struct *tty, struct usb_serial_port *port)
10901da177e4SLinus Torvalds {
10911da177e4SLinus Torvalds 	int ret;
10921da177e4SLinus Torvalds 	unsigned char buf[32];
10931da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
1094606d099cSAlan Cox 	struct ktermios not_termios;
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 	/* be sure the device is started up */
10971da177e4SLinus Torvalds 	if (digi_startup_device(port->serial) != 0)
1098c6d61269SAlan Cox 		return -ENXIO;
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 	/* read modem signals automatically whenever they change */
11011da177e4SLinus Torvalds 	buf[0] = DIGI_CMD_READ_INPUT_SIGNALS;
11021da177e4SLinus Torvalds 	buf[1] = priv->dp_port_num;
11031da177e4SLinus Torvalds 	buf[2] = DIGI_ENABLE;
11041da177e4SLinus Torvalds 	buf[3] = 0;
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 	/* flush fifos */
11071da177e4SLinus Torvalds 	buf[4] = DIGI_CMD_IFLUSH_FIFO;
11081da177e4SLinus Torvalds 	buf[5] = priv->dp_port_num;
11091da177e4SLinus Torvalds 	buf[6] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
11101da177e4SLinus Torvalds 	buf[7] = 0;
11111da177e4SLinus Torvalds 
111241ad427dSAlan Cox 	ret = digi_write_oob_command(port, buf, 8, 1);
111341ad427dSAlan Cox 	if (ret != 0)
1114f9dfbebbSGreg Kroah-Hartman 		dev_dbg(&port->dev, "digi_open: write oob failed, ret=%d\n", ret);
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 	/* set termios settings */
111795da310eSAlan Cox 	if (tty) {
1118adc8d746SAlan Cox 		not_termios.c_cflag = ~tty->termios.c_cflag;
1119adc8d746SAlan Cox 		not_termios.c_iflag = ~tty->termios.c_iflag;
112095da310eSAlan Cox 		digi_set_termios(tty, port, &not_termios);
112195da310eSAlan Cox 	}
1122c6d61269SAlan Cox 	return 0;
11231da177e4SLinus Torvalds }
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 
1126335f8514SAlan Cox static void digi_close(struct usb_serial_port *port)
11271da177e4SLinus Torvalds {
11281da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
11291da177e4SLinus Torvalds 	int ret;
11301da177e4SLinus Torvalds 	unsigned char buf[32];
11311da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
11321da177e4SLinus Torvalds 
11330915f490SOliver Neukum 	mutex_lock(&port->serial->disc_mutex);
11341da177e4SLinus Torvalds 	/* if disconnected, just clear flags */
11350915f490SOliver Neukum 	if (port->serial->disconnected)
11361da177e4SLinus Torvalds 		goto exit;
11371da177e4SLinus Torvalds 
1138335f8514SAlan Cox 	/* FIXME: Transmit idle belongs in the wait_unti_sent path */
11391da177e4SLinus Torvalds 	digi_transmit_idle(port, DIGI_CLOSE_TIMEOUT);
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds 	/* disable input flow control */
11421da177e4SLinus Torvalds 	buf[0] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
11431da177e4SLinus Torvalds 	buf[1] = priv->dp_port_num;
11441da177e4SLinus Torvalds 	buf[2] = DIGI_DISABLE;
11451da177e4SLinus Torvalds 	buf[3] = 0;
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds 	/* disable output flow control */
11481da177e4SLinus Torvalds 	buf[4] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
11491da177e4SLinus Torvalds 	buf[5] = priv->dp_port_num;
11501da177e4SLinus Torvalds 	buf[6] = DIGI_DISABLE;
11511da177e4SLinus Torvalds 	buf[7] = 0;
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds 	/* disable reading modem signals automatically */
11541da177e4SLinus Torvalds 	buf[8] = DIGI_CMD_READ_INPUT_SIGNALS;
11551da177e4SLinus Torvalds 	buf[9] = priv->dp_port_num;
11561da177e4SLinus Torvalds 	buf[10] = DIGI_DISABLE;
11571da177e4SLinus Torvalds 	buf[11] = 0;
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds 	/* disable receive */
11601da177e4SLinus Torvalds 	buf[12] = DIGI_CMD_RECEIVE_ENABLE;
11611da177e4SLinus Torvalds 	buf[13] = priv->dp_port_num;
11621da177e4SLinus Torvalds 	buf[14] = DIGI_DISABLE;
11631da177e4SLinus Torvalds 	buf[15] = 0;
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 	/* flush fifos */
11661da177e4SLinus Torvalds 	buf[16] = DIGI_CMD_IFLUSH_FIFO;
11671da177e4SLinus Torvalds 	buf[17] = priv->dp_port_num;
11681da177e4SLinus Torvalds 	buf[18] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
11691da177e4SLinus Torvalds 	buf[19] = 0;
11701da177e4SLinus Torvalds 
117141ad427dSAlan Cox 	ret = digi_write_oob_command(port, buf, 20, 0);
117241ad427dSAlan Cox 	if (ret != 0)
117328e679aeSJohan Hovold 		dev_dbg(&port->dev, "digi_close: write oob failed, ret=%d\n",
117428e679aeSJohan Hovold 									ret);
11751da177e4SLinus Torvalds 	/* wait for final commands on oob port to complete */
117641ad427dSAlan Cox 	prepare_to_wait(&priv->dp_flush_wait, &wait,
117741ad427dSAlan Cox 			TASK_INTERRUPTIBLE);
11781da177e4SLinus Torvalds 	schedule_timeout(DIGI_CLOSE_TIMEOUT);
11791da177e4SLinus Torvalds 	finish_wait(&priv->dp_flush_wait, &wait);
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 	/* shutdown any outstanding bulk writes */
11821da177e4SLinus Torvalds 	usb_kill_urb(port->write_urb);
11831da177e4SLinus Torvalds exit:
11840915f490SOliver Neukum 	spin_lock_irq(&priv->dp_port_lock);
11851da177e4SLinus Torvalds 	priv->dp_write_urb_in_use = 0;
11861da177e4SLinus Torvalds 	wake_up_interruptible(&priv->dp_close_wait);
11870915f490SOliver Neukum 	spin_unlock_irq(&priv->dp_port_lock);
11880915f490SOliver Neukum 	mutex_unlock(&port->serial->disc_mutex);
11891da177e4SLinus Torvalds }
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds /*
11931da177e4SLinus Torvalds  *  Digi Startup Device
11941da177e4SLinus Torvalds  *
11951da177e4SLinus Torvalds  *  Starts reads on all ports.  Must be called AFTER startup, with
11961da177e4SLinus Torvalds  *  urbs initialized.  Returns 0 if successful, non-zero error otherwise.
11971da177e4SLinus Torvalds  */
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds static int digi_startup_device(struct usb_serial *serial)
12001da177e4SLinus Torvalds {
12011da177e4SLinus Torvalds 	int i, ret = 0;
12021da177e4SLinus Torvalds 	struct digi_serial *serial_priv = usb_get_serial_data(serial);
12031da177e4SLinus Torvalds 	struct usb_serial_port *port;
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds 	/* be sure this happens exactly once */
12061da177e4SLinus Torvalds 	spin_lock(&serial_priv->ds_serial_lock);
12071da177e4SLinus Torvalds 	if (serial_priv->ds_device_started) {
12081da177e4SLinus Torvalds 		spin_unlock(&serial_priv->ds_serial_lock);
1209c6d61269SAlan Cox 		return 0;
12101da177e4SLinus Torvalds 	}
12111da177e4SLinus Torvalds 	serial_priv->ds_device_started = 1;
12121da177e4SLinus Torvalds 	spin_unlock(&serial_priv->ds_serial_lock);
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 	/* start reading from each bulk in endpoint for the device */
12151da177e4SLinus Torvalds 	/* set USB_DISABLE_SPD flag for write bulk urbs */
12161da177e4SLinus Torvalds 	for (i = 0; i < serial->type->num_ports + 1; i++) {
12171da177e4SLinus Torvalds 		port = serial->port[i];
121841ad427dSAlan Cox 		ret = usb_submit_urb(port->read_urb, GFP_KERNEL);
121941ad427dSAlan Cox 		if (ret != 0) {
1220194343d9SGreg Kroah-Hartman 			dev_err(&port->dev,
1221194343d9SGreg Kroah-Hartman 				"%s: usb_submit_urb failed, ret=%d, port=%d\n",
1222441b62c1SHarvey Harrison 				__func__, ret, i);
12231da177e4SLinus Torvalds 			break;
12241da177e4SLinus Torvalds 		}
12251da177e4SLinus Torvalds 	}
1226c6d61269SAlan Cox 	return ret;
12271da177e4SLinus Torvalds }
12281da177e4SLinus Torvalds 
1229fb44ff85SJohan Hovold static int digi_port_init(struct usb_serial_port *port, unsigned port_num)
12301da177e4SLinus Torvalds {
12311da177e4SLinus Torvalds 	struct digi_port *priv;
12321da177e4SLinus Torvalds 
1233fb44ff85SJohan Hovold 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1234fb44ff85SJohan Hovold 	if (!priv)
1235fb44ff85SJohan Hovold 		return -ENOMEM;
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 	spin_lock_init(&priv->dp_port_lock);
1238fb44ff85SJohan Hovold 	priv->dp_port_num = port_num;
12391da177e4SLinus Torvalds 	init_waitqueue_head(&priv->dp_transmit_idle_wait);
12401da177e4SLinus Torvalds 	init_waitqueue_head(&priv->dp_flush_wait);
12411da177e4SLinus Torvalds 	init_waitqueue_head(&priv->dp_close_wait);
1242c4028958SDavid Howells 	INIT_WORK(&priv->dp_wakeup_work, digi_wakeup_write_lock);
1243fb44ff85SJohan Hovold 	priv->dp_port = port;
12441da177e4SLinus Torvalds 
1245fb44ff85SJohan Hovold 	init_waitqueue_head(&port->write_wait);
1246fb44ff85SJohan Hovold 
1247fb44ff85SJohan Hovold 	usb_set_serial_port_data(port, priv);
1248fb44ff85SJohan Hovold 
1249fb44ff85SJohan Hovold 	return 0;
12501da177e4SLinus Torvalds }
12511da177e4SLinus Torvalds 
1252fb44ff85SJohan Hovold static int digi_startup(struct usb_serial *serial)
1253fb44ff85SJohan Hovold {
1254fb44ff85SJohan Hovold 	struct digi_serial *serial_priv;
1255fb44ff85SJohan Hovold 	int ret;
12561da177e4SLinus Torvalds 
1257fb44ff85SJohan Hovold 	serial_priv = kzalloc(sizeof(*serial_priv), GFP_KERNEL);
1258fb44ff85SJohan Hovold 	if (!serial_priv)
1259fb44ff85SJohan Hovold 		return -ENOMEM;
1260fb44ff85SJohan Hovold 
12611da177e4SLinus Torvalds 	spin_lock_init(&serial_priv->ds_serial_lock);
12621da177e4SLinus Torvalds 	serial_priv->ds_oob_port_num = serial->type->num_ports;
12631da177e4SLinus Torvalds 	serial_priv->ds_oob_port = serial->port[serial_priv->ds_oob_port_num];
1264fb44ff85SJohan Hovold 
1265fb44ff85SJohan Hovold 	ret = digi_port_init(serial_priv->ds_oob_port,
1266fb44ff85SJohan Hovold 						serial_priv->ds_oob_port_num);
1267fb44ff85SJohan Hovold 	if (ret) {
1268fb44ff85SJohan Hovold 		kfree(serial_priv);
1269fb44ff85SJohan Hovold 		return ret;
1270fb44ff85SJohan Hovold 	}
1271fb44ff85SJohan Hovold 
12721da177e4SLinus Torvalds 	usb_set_serial_data(serial, serial_priv);
12731da177e4SLinus Torvalds 
1274c6d61269SAlan Cox 	return 0;
12751da177e4SLinus Torvalds }
12761da177e4SLinus Torvalds 
12771da177e4SLinus Torvalds 
1278f9c99bb8SAlan Stern static void digi_disconnect(struct usb_serial *serial)
12791da177e4SLinus Torvalds {
12801da177e4SLinus Torvalds 	int i;
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds 	/* stop reads and writes on all ports */
12831da177e4SLinus Torvalds 	for (i = 0; i < serial->type->num_ports + 1; i++) {
12841da177e4SLinus Torvalds 		usb_kill_urb(serial->port[i]->read_urb);
12851da177e4SLinus Torvalds 		usb_kill_urb(serial->port[i]->write_urb);
12861da177e4SLinus Torvalds 	}
1287f9c99bb8SAlan Stern }
1288f9c99bb8SAlan Stern 
1289f9c99bb8SAlan Stern 
1290f9c99bb8SAlan Stern static void digi_release(struct usb_serial *serial)
1291f9c99bb8SAlan Stern {
1292fb44ff85SJohan Hovold 	struct digi_serial *serial_priv;
1293fb44ff85SJohan Hovold 	struct digi_port *priv;
12941da177e4SLinus Torvalds 
1295fb44ff85SJohan Hovold 	serial_priv = usb_get_serial_data(serial);
1296fb44ff85SJohan Hovold 
1297fb44ff85SJohan Hovold 	priv = usb_get_serial_port_data(serial_priv->ds_oob_port);
1298fb44ff85SJohan Hovold 	kfree(priv);
1299fb44ff85SJohan Hovold 
1300fb44ff85SJohan Hovold 	kfree(serial_priv);
13011da177e4SLinus Torvalds }
13021da177e4SLinus Torvalds 
1303fb44ff85SJohan Hovold static int digi_port_probe(struct usb_serial_port *port)
1304fb44ff85SJohan Hovold {
13051143832eSGreg Kroah-Hartman 	return digi_port_init(port, port->port_number);
1306fb44ff85SJohan Hovold }
1307fb44ff85SJohan Hovold 
1308fb44ff85SJohan Hovold static int digi_port_remove(struct usb_serial_port *port)
1309fb44ff85SJohan Hovold {
1310fb44ff85SJohan Hovold 	struct digi_port *priv;
1311fb44ff85SJohan Hovold 
1312fb44ff85SJohan Hovold 	priv = usb_get_serial_port_data(port);
1313fb44ff85SJohan Hovold 	kfree(priv);
1314fb44ff85SJohan Hovold 
1315fb44ff85SJohan Hovold 	return 0;
1316fb44ff85SJohan Hovold }
13171da177e4SLinus Torvalds 
13187d12e780SDavid Howells static void digi_read_bulk_callback(struct urb *urb)
13191da177e4SLinus Torvalds {
1320cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
13211da177e4SLinus Torvalds 	struct digi_port *priv;
13221da177e4SLinus Torvalds 	struct digi_serial *serial_priv;
13231da177e4SLinus Torvalds 	int ret;
132485d75107SGreg Kroah-Hartman 	int status = urb->status;
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds 	/* port sanity check, do not resubmit if port is not valid */
1327194343d9SGreg Kroah-Hartman 	if (port == NULL)
1328194343d9SGreg Kroah-Hartman 		return;
1329194343d9SGreg Kroah-Hartman 	priv = usb_get_serial_port_data(port);
1330194343d9SGreg Kroah-Hartman 	if (priv == NULL) {
1331194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s: port->private is NULL, status=%d\n",
1332441b62c1SHarvey Harrison 			__func__, status);
13331da177e4SLinus Torvalds 		return;
13341da177e4SLinus Torvalds 	}
1335c6d61269SAlan Cox 	if (port->serial == NULL ||
1336c6d61269SAlan Cox 		(serial_priv = usb_get_serial_data(port->serial)) == NULL) {
1337194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s: serial is bad or serial->private "
1338194343d9SGreg Kroah-Hartman 			"is NULL, status=%d\n", __func__, status);
13391da177e4SLinus Torvalds 		return;
13401da177e4SLinus Torvalds 	}
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds 	/* do not resubmit urb if it has any status error */
134385d75107SGreg Kroah-Hartman 	if (status) {
1344194343d9SGreg Kroah-Hartman 		dev_err(&port->dev,
1345194343d9SGreg Kroah-Hartman 			"%s: nonzero read bulk status: status=%d, port=%d\n",
1346441b62c1SHarvey Harrison 			__func__, status, priv->dp_port_num);
13471da177e4SLinus Torvalds 		return;
13481da177e4SLinus Torvalds 	}
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds 	/* handle oob or inb callback, do not resubmit if error */
13511da177e4SLinus Torvalds 	if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
13521da177e4SLinus Torvalds 		if (digi_read_oob_callback(urb) != 0)
13531da177e4SLinus Torvalds 			return;
13541da177e4SLinus Torvalds 	} else {
13551da177e4SLinus Torvalds 		if (digi_read_inb_callback(urb) != 0)
13561da177e4SLinus Torvalds 			return;
13571da177e4SLinus Torvalds 	}
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds 	/* continue read */
136041ad427dSAlan Cox 	ret = usb_submit_urb(urb, GFP_ATOMIC);
13611f87158eSAlan Stern 	if (ret != 0 && ret != -EPERM) {
1362194343d9SGreg Kroah-Hartman 		dev_err(&port->dev,
1363194343d9SGreg Kroah-Hartman 			"%s: failed resubmitting urb, ret=%d, port=%d\n",
1364441b62c1SHarvey Harrison 			__func__, ret, priv->dp_port_num);
13651da177e4SLinus Torvalds 	}
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds }
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds /*
13701da177e4SLinus Torvalds  *  Digi Read INB Callback
13711da177e4SLinus Torvalds  *
13721da177e4SLinus Torvalds  *  Digi Read INB Callback handles reads on the in band ports, sending
13731da177e4SLinus Torvalds  *  the data on to the tty subsystem.  When called we know port and
13741da177e4SLinus Torvalds  *  port->private are not NULL and port->serial has been validated.
13751da177e4SLinus Torvalds  *  It returns 0 if successful, 1 if successful but the port is
13761da177e4SLinus Torvalds  *  throttled, and -1 if the sanity checks failed.
13771da177e4SLinus Torvalds  */
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds static int digi_read_inb_callback(struct urb *urb)
13801da177e4SLinus Torvalds {
1381cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
13821da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
13831da177e4SLinus Torvalds 	int opcode = ((unsigned char *)urb->transfer_buffer)[0];
13841da177e4SLinus Torvalds 	int len = ((unsigned char *)urb->transfer_buffer)[1];
138585d75107SGreg Kroah-Hartman 	int port_status = ((unsigned char *)urb->transfer_buffer)[2];
13861da177e4SLinus Torvalds 	unsigned char *data = ((unsigned char *)urb->transfer_buffer) + 3;
13871da177e4SLinus Torvalds 	int flag, throttled;
138885d75107SGreg Kroah-Hartman 	int status = urb->status;
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds 	/* do not process callbacks on closed ports */
13911da177e4SLinus Torvalds 	/* but do continue the read chain */
13921f87158eSAlan Stern 	if (urb->status == -ENOENT)
1393c6d61269SAlan Cox 		return 0;
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds 	/* short/multiple packet check */
13961da177e4SLinus Torvalds 	if (urb->actual_length != len + 2) {
1397194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "%s: INCOMPLETE OR MULTIPLE PACKET, "
139850de36f7SGreg Kroah-Hartman 			"status=%d, port=%d, opcode=%d, len=%d, "
1399194343d9SGreg Kroah-Hartman 			"actual_length=%d, status=%d\n", __func__, status,
1400194343d9SGreg Kroah-Hartman 			priv->dp_port_num, opcode, len, urb->actual_length,
1401194343d9SGreg Kroah-Hartman 			port_status);
1402c6d61269SAlan Cox 		return -1;
14031da177e4SLinus Torvalds 	}
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 	spin_lock(&priv->dp_port_lock);
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds 	/* check for throttle; if set, do not resubmit read urb */
14081da177e4SLinus Torvalds 	/* indicate the read chain needs to be restarted on unthrottle */
14091da177e4SLinus Torvalds 	throttled = priv->dp_throttled;
14101da177e4SLinus Torvalds 	if (throttled)
14111da177e4SLinus Torvalds 		priv->dp_throttle_restart = 1;
14121da177e4SLinus Torvalds 
14131da177e4SLinus Torvalds 	/* receive data */
14142e124b4aSJiri Slaby 	if (opcode == DIGI_CMD_RECEIVE_DATA) {
141585d75107SGreg Kroah-Hartman 		/* get flag from port_status */
14161da177e4SLinus Torvalds 		flag = 0;
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds 		/* overrun is special, not associated with a char */
1419c6d61269SAlan Cox 		if (port_status & DIGI_OVERRUN_ERROR)
142092a19f9cSJiri Slaby 			tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
14211da177e4SLinus Torvalds 
14221da177e4SLinus Torvalds 		/* break takes precedence over parity, */
14231da177e4SLinus Torvalds 		/* which takes precedence over framing errors */
1424c6d61269SAlan Cox 		if (port_status & DIGI_BREAK_ERROR)
14251da177e4SLinus Torvalds 			flag = TTY_BREAK;
1426c6d61269SAlan Cox 		else if (port_status & DIGI_PARITY_ERROR)
14271da177e4SLinus Torvalds 			flag = TTY_PARITY;
1428c6d61269SAlan Cox 		else if (port_status & DIGI_FRAMING_ERROR)
14291da177e4SLinus Torvalds 			flag = TTY_FRAME;
14301da177e4SLinus Torvalds 
143185d75107SGreg Kroah-Hartman 		/* data length is len-1 (one byte of len is port_status) */
14321da177e4SLinus Torvalds 		--len;
14331da177e4SLinus Torvalds 		if (len > 0) {
14342f693357SJiri Slaby 			tty_insert_flip_string_fixed_flag(&port->port, data,
14352f693357SJiri Slaby 					flag, len);
14362e124b4aSJiri Slaby 			tty_flip_buffer_push(&port->port);
14371da177e4SLinus Torvalds 		}
14381da177e4SLinus Torvalds 	}
14391da177e4SLinus Torvalds 	spin_unlock(&priv->dp_port_lock);
14401da177e4SLinus Torvalds 
1441c6d61269SAlan Cox 	if (opcode == DIGI_CMD_RECEIVE_DISABLE)
1442f9dfbebbSGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s: got RECEIVE_DISABLE\n", __func__);
1443c6d61269SAlan Cox 	else if (opcode != DIGI_CMD_RECEIVE_DATA)
1444f9dfbebbSGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s: unknown opcode: %d\n", __func__, opcode);
14451da177e4SLinus Torvalds 
144641ad427dSAlan Cox 	return throttled ? 1 : 0;
14471da177e4SLinus Torvalds 
14481da177e4SLinus Torvalds }
14491da177e4SLinus Torvalds 
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds /*
14521da177e4SLinus Torvalds  *  Digi Read OOB Callback
14531da177e4SLinus Torvalds  *
14541da177e4SLinus Torvalds  *  Digi Read OOB Callback handles reads on the out of band port.
14551da177e4SLinus Torvalds  *  When called we know port and port->private are not NULL and
14561da177e4SLinus Torvalds  *  the port->serial is valid.  It returns 0 if successful, and
14571da177e4SLinus Torvalds  *  -1 if the sanity checks failed.
14581da177e4SLinus Torvalds  */
14591da177e4SLinus Torvalds 
14601da177e4SLinus Torvalds static int digi_read_oob_callback(struct urb *urb)
14611da177e4SLinus Torvalds {
14621da177e4SLinus Torvalds 
1463cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
14641da177e4SLinus Torvalds 	struct usb_serial *serial = port->serial;
14654a90f09bSAlan Cox 	struct tty_struct *tty;
14661da177e4SLinus Torvalds 	struct digi_port *priv = usb_get_serial_port_data(port);
14671da177e4SLinus Torvalds 	int opcode, line, status, val;
14681da177e4SLinus Torvalds 	int i;
146941ad427dSAlan Cox 	unsigned int rts;
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds 	/* handle each oob command */
14721da177e4SLinus Torvalds 	for (i = 0; i < urb->actual_length - 3;) {
14731da177e4SLinus Torvalds 		opcode = ((unsigned char *)urb->transfer_buffer)[i++];
14741da177e4SLinus Torvalds 		line = ((unsigned char *)urb->transfer_buffer)[i++];
14751da177e4SLinus Torvalds 		status = ((unsigned char *)urb->transfer_buffer)[i++];
14761da177e4SLinus Torvalds 		val = ((unsigned char *)urb->transfer_buffer)[i++];
14771da177e4SLinus Torvalds 
1478f9dfbebbSGreg Kroah-Hartman 		dev_dbg(&port->dev, "digi_read_oob_callback: opcode=%d, line=%d, status=%d, val=%d\n",
14791da177e4SLinus Torvalds 			opcode, line, status, val);
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds 		if (status != 0 || line >= serial->type->num_ports)
14821da177e4SLinus Torvalds 			continue;
14831da177e4SLinus Torvalds 
14841da177e4SLinus Torvalds 		port = serial->port[line];
14851da177e4SLinus Torvalds 
148641ad427dSAlan Cox 		priv = usb_get_serial_port_data(port);
148741ad427dSAlan Cox 		if (priv == NULL)
14881da177e4SLinus Torvalds 			return -1;
14891da177e4SLinus Torvalds 
14904a90f09bSAlan Cox 		tty = tty_port_tty_get(&port->port);
14914287341dSAlan Cox 
149241ad427dSAlan Cox 		rts = 0;
14934287341dSAlan Cox 		if (tty)
1494*9db276f8SPeter Hurley 			rts = C_CRTSCTS(tty);
149541ad427dSAlan Cox 
14964287341dSAlan Cox 		if (tty && opcode == DIGI_CMD_READ_INPUT_SIGNALS) {
14971da177e4SLinus Torvalds 			spin_lock(&priv->dp_port_lock);
14981da177e4SLinus Torvalds 			/* convert from digi flags to termiox flags */
14991da177e4SLinus Torvalds 			if (val & DIGI_READ_INPUT_SIGNALS_CTS) {
15001da177e4SLinus Torvalds 				priv->dp_modem_signals |= TIOCM_CTS;
15011da177e4SLinus Torvalds 				/* port must be open to use tty struct */
1502d95e3caeSPeter Hurley 				if (rts)
15036aad04f2SJiri Slaby 					tty_port_tty_wakeup(&port->port);
15041da177e4SLinus Torvalds 			} else {
15051da177e4SLinus Torvalds 				priv->dp_modem_signals &= ~TIOCM_CTS;
15061da177e4SLinus Torvalds 				/* port must be open to use tty struct */
15071da177e4SLinus Torvalds 			}
15081da177e4SLinus Torvalds 			if (val & DIGI_READ_INPUT_SIGNALS_DSR)
15091da177e4SLinus Torvalds 				priv->dp_modem_signals |= TIOCM_DSR;
15101da177e4SLinus Torvalds 			else
15111da177e4SLinus Torvalds 				priv->dp_modem_signals &= ~TIOCM_DSR;
15121da177e4SLinus Torvalds 			if (val & DIGI_READ_INPUT_SIGNALS_RI)
15131da177e4SLinus Torvalds 				priv->dp_modem_signals |= TIOCM_RI;
15141da177e4SLinus Torvalds 			else
15151da177e4SLinus Torvalds 				priv->dp_modem_signals &= ~TIOCM_RI;
15161da177e4SLinus Torvalds 			if (val & DIGI_READ_INPUT_SIGNALS_DCD)
15171da177e4SLinus Torvalds 				priv->dp_modem_signals |= TIOCM_CD;
15181da177e4SLinus Torvalds 			else
15191da177e4SLinus Torvalds 				priv->dp_modem_signals &= ~TIOCM_CD;
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds 			spin_unlock(&priv->dp_port_lock);
15221da177e4SLinus Torvalds 		} else if (opcode == DIGI_CMD_TRANSMIT_IDLE) {
15231da177e4SLinus Torvalds 			spin_lock(&priv->dp_port_lock);
15241da177e4SLinus Torvalds 			priv->dp_transmit_idle = 1;
15251da177e4SLinus Torvalds 			wake_up_interruptible(&priv->dp_transmit_idle_wait);
15261da177e4SLinus Torvalds 			spin_unlock(&priv->dp_port_lock);
15271da177e4SLinus Torvalds 		} else if (opcode == DIGI_CMD_IFLUSH_FIFO) {
152845f23f18SNishanth Aravamudan 			wake_up_interruptible(&priv->dp_flush_wait);
1529c6d61269SAlan Cox 		}
15304a90f09bSAlan Cox 		tty_kref_put(tty);
1531c6d61269SAlan Cox 	}
1532c6d61269SAlan Cox 	return 0;
15331da177e4SLinus Torvalds 
15341da177e4SLinus Torvalds }
15351da177e4SLinus Torvalds 
153668e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table_combined);
15371da177e4SLinus Torvalds 
15381da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
15391da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
15401da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1541