18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci*  Digi AccelePort USB-4 and USB-2 Serial Converters
48c2ecf20Sopenharmony_ci*
58c2ecf20Sopenharmony_ci*  Copyright 2000 by Digi International
68c2ecf20Sopenharmony_ci*
78c2ecf20Sopenharmony_ci*  Shamelessly based on Brian Warner's keyspan_pda.c and Greg Kroah-Hartman's
88c2ecf20Sopenharmony_ci*  usb-serial driver.
98c2ecf20Sopenharmony_ci*
108c2ecf20Sopenharmony_ci*  Peter Berger (pberger@brimson.com)
118c2ecf20Sopenharmony_ci*  Al Borchers (borchers@steinerpoint.com)
128c2ecf20Sopenharmony_ci*/
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/kernel.h>
158c2ecf20Sopenharmony_ci#include <linux/errno.h>
168c2ecf20Sopenharmony_ci#include <linux/slab.h>
178c2ecf20Sopenharmony_ci#include <linux/tty.h>
188c2ecf20Sopenharmony_ci#include <linux/tty_driver.h>
198c2ecf20Sopenharmony_ci#include <linux/tty_flip.h>
208c2ecf20Sopenharmony_ci#include <linux/module.h>
218c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
228c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
238c2ecf20Sopenharmony_ci#include <linux/usb.h>
248c2ecf20Sopenharmony_ci#include <linux/wait.h>
258c2ecf20Sopenharmony_ci#include <linux/sched/signal.h>
268c2ecf20Sopenharmony_ci#include <linux/usb/serial.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/* Defines */
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define DRIVER_AUTHOR "Peter Berger <pberger@brimson.com>, Al Borchers <borchers@steinerpoint.com>"
318c2ecf20Sopenharmony_ci#define DRIVER_DESC "Digi AccelePort USB-2/USB-4 Serial Converter driver"
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/* port output buffer length -- must be <= transfer buffer length - 2 */
348c2ecf20Sopenharmony_ci/* so we can be sure to send the full buffer in one urb */
358c2ecf20Sopenharmony_ci#define DIGI_OUT_BUF_SIZE		8
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* port input buffer length -- must be >= transfer buffer length - 3 */
388c2ecf20Sopenharmony_ci/* so we can be sure to hold at least one full buffer from one urb */
398c2ecf20Sopenharmony_ci#define DIGI_IN_BUF_SIZE		64
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/* retry timeout while sleeping */
428c2ecf20Sopenharmony_ci#define DIGI_RETRY_TIMEOUT		(HZ/10)
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/* timeout while waiting for tty output to drain in close */
458c2ecf20Sopenharmony_ci/* this delay is used twice in close, so the total delay could */
468c2ecf20Sopenharmony_ci/* be twice this value */
478c2ecf20Sopenharmony_ci#define DIGI_CLOSE_TIMEOUT		(5*HZ)
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci/* AccelePort USB Defines */
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/* ids */
538c2ecf20Sopenharmony_ci#define DIGI_VENDOR_ID			0x05c5
548c2ecf20Sopenharmony_ci#define DIGI_2_ID			0x0002	/* USB-2 */
558c2ecf20Sopenharmony_ci#define DIGI_4_ID			0x0004	/* USB-4 */
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/* commands
588c2ecf20Sopenharmony_ci * "INB": can be used on the in-band endpoint
598c2ecf20Sopenharmony_ci * "OOB": can be used on the out-of-band endpoint
608c2ecf20Sopenharmony_ci */
618c2ecf20Sopenharmony_ci#define DIGI_CMD_SET_BAUD_RATE			0	/* INB, OOB */
628c2ecf20Sopenharmony_ci#define DIGI_CMD_SET_WORD_SIZE			1	/* INB, OOB */
638c2ecf20Sopenharmony_ci#define DIGI_CMD_SET_PARITY			2	/* INB, OOB */
648c2ecf20Sopenharmony_ci#define DIGI_CMD_SET_STOP_BITS			3	/* INB, OOB */
658c2ecf20Sopenharmony_ci#define DIGI_CMD_SET_INPUT_FLOW_CONTROL		4	/* INB, OOB */
668c2ecf20Sopenharmony_ci#define DIGI_CMD_SET_OUTPUT_FLOW_CONTROL	5	/* INB, OOB */
678c2ecf20Sopenharmony_ci#define DIGI_CMD_SET_DTR_SIGNAL			6	/* INB, OOB */
688c2ecf20Sopenharmony_ci#define DIGI_CMD_SET_RTS_SIGNAL			7	/* INB, OOB */
698c2ecf20Sopenharmony_ci#define DIGI_CMD_READ_INPUT_SIGNALS		8	/*      OOB */
708c2ecf20Sopenharmony_ci#define DIGI_CMD_IFLUSH_FIFO			9	/*      OOB */
718c2ecf20Sopenharmony_ci#define DIGI_CMD_RECEIVE_ENABLE			10	/* INB, OOB */
728c2ecf20Sopenharmony_ci#define DIGI_CMD_BREAK_CONTROL			11	/* INB, OOB */
738c2ecf20Sopenharmony_ci#define DIGI_CMD_LOCAL_LOOPBACK			12	/* INB, OOB */
748c2ecf20Sopenharmony_ci#define DIGI_CMD_TRANSMIT_IDLE			13	/* INB, OOB */
758c2ecf20Sopenharmony_ci#define DIGI_CMD_READ_UART_REGISTER		14	/*      OOB */
768c2ecf20Sopenharmony_ci#define DIGI_CMD_WRITE_UART_REGISTER		15	/* INB, OOB */
778c2ecf20Sopenharmony_ci#define DIGI_CMD_AND_UART_REGISTER		16	/* INB, OOB */
788c2ecf20Sopenharmony_ci#define DIGI_CMD_OR_UART_REGISTER		17	/* INB, OOB */
798c2ecf20Sopenharmony_ci#define DIGI_CMD_SEND_DATA			18	/* INB      */
808c2ecf20Sopenharmony_ci#define DIGI_CMD_RECEIVE_DATA			19	/* INB      */
818c2ecf20Sopenharmony_ci#define DIGI_CMD_RECEIVE_DISABLE		20	/* INB      */
828c2ecf20Sopenharmony_ci#define DIGI_CMD_GET_PORT_TYPE			21	/*      OOB */
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci/* baud rates */
858c2ecf20Sopenharmony_ci#define DIGI_BAUD_50				0
868c2ecf20Sopenharmony_ci#define DIGI_BAUD_75				1
878c2ecf20Sopenharmony_ci#define DIGI_BAUD_110				2
888c2ecf20Sopenharmony_ci#define DIGI_BAUD_150				3
898c2ecf20Sopenharmony_ci#define DIGI_BAUD_200				4
908c2ecf20Sopenharmony_ci#define DIGI_BAUD_300				5
918c2ecf20Sopenharmony_ci#define DIGI_BAUD_600				6
928c2ecf20Sopenharmony_ci#define DIGI_BAUD_1200				7
938c2ecf20Sopenharmony_ci#define DIGI_BAUD_1800				8
948c2ecf20Sopenharmony_ci#define DIGI_BAUD_2400				9
958c2ecf20Sopenharmony_ci#define DIGI_BAUD_4800				10
968c2ecf20Sopenharmony_ci#define DIGI_BAUD_7200				11
978c2ecf20Sopenharmony_ci#define DIGI_BAUD_9600				12
988c2ecf20Sopenharmony_ci#define DIGI_BAUD_14400				13
998c2ecf20Sopenharmony_ci#define DIGI_BAUD_19200				14
1008c2ecf20Sopenharmony_ci#define DIGI_BAUD_28800				15
1018c2ecf20Sopenharmony_ci#define DIGI_BAUD_38400				16
1028c2ecf20Sopenharmony_ci#define DIGI_BAUD_57600				17
1038c2ecf20Sopenharmony_ci#define DIGI_BAUD_76800				18
1048c2ecf20Sopenharmony_ci#define DIGI_BAUD_115200			19
1058c2ecf20Sopenharmony_ci#define DIGI_BAUD_153600			20
1068c2ecf20Sopenharmony_ci#define DIGI_BAUD_230400			21
1078c2ecf20Sopenharmony_ci#define DIGI_BAUD_460800			22
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci/* arguments */
1108c2ecf20Sopenharmony_ci#define DIGI_WORD_SIZE_5			0
1118c2ecf20Sopenharmony_ci#define DIGI_WORD_SIZE_6			1
1128c2ecf20Sopenharmony_ci#define DIGI_WORD_SIZE_7			2
1138c2ecf20Sopenharmony_ci#define DIGI_WORD_SIZE_8			3
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci#define DIGI_PARITY_NONE			0
1168c2ecf20Sopenharmony_ci#define DIGI_PARITY_ODD				1
1178c2ecf20Sopenharmony_ci#define DIGI_PARITY_EVEN			2
1188c2ecf20Sopenharmony_ci#define DIGI_PARITY_MARK			3
1198c2ecf20Sopenharmony_ci#define DIGI_PARITY_SPACE			4
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci#define DIGI_STOP_BITS_1			0
1228c2ecf20Sopenharmony_ci#define DIGI_STOP_BITS_2			1
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci#define DIGI_INPUT_FLOW_CONTROL_XON_XOFF	1
1258c2ecf20Sopenharmony_ci#define DIGI_INPUT_FLOW_CONTROL_RTS		2
1268c2ecf20Sopenharmony_ci#define DIGI_INPUT_FLOW_CONTROL_DTR		4
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci#define DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF	1
1298c2ecf20Sopenharmony_ci#define DIGI_OUTPUT_FLOW_CONTROL_CTS		2
1308c2ecf20Sopenharmony_ci#define DIGI_OUTPUT_FLOW_CONTROL_DSR		4
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci#define DIGI_DTR_INACTIVE			0
1338c2ecf20Sopenharmony_ci#define DIGI_DTR_ACTIVE				1
1348c2ecf20Sopenharmony_ci#define DIGI_DTR_INPUT_FLOW_CONTROL		2
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci#define DIGI_RTS_INACTIVE			0
1378c2ecf20Sopenharmony_ci#define DIGI_RTS_ACTIVE				1
1388c2ecf20Sopenharmony_ci#define DIGI_RTS_INPUT_FLOW_CONTROL		2
1398c2ecf20Sopenharmony_ci#define DIGI_RTS_TOGGLE				3
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci#define DIGI_FLUSH_TX				1
1428c2ecf20Sopenharmony_ci#define DIGI_FLUSH_RX				2
1438c2ecf20Sopenharmony_ci#define DIGI_RESUME_TX				4 /* clears xoff condition */
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci#define DIGI_TRANSMIT_NOT_IDLE			0
1468c2ecf20Sopenharmony_ci#define DIGI_TRANSMIT_IDLE			1
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci#define DIGI_DISABLE				0
1498c2ecf20Sopenharmony_ci#define DIGI_ENABLE				1
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci#define DIGI_DEASSERT				0
1528c2ecf20Sopenharmony_ci#define DIGI_ASSERT				1
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci/* in band status codes */
1558c2ecf20Sopenharmony_ci#define DIGI_OVERRUN_ERROR			4
1568c2ecf20Sopenharmony_ci#define DIGI_PARITY_ERROR			8
1578c2ecf20Sopenharmony_ci#define DIGI_FRAMING_ERROR			16
1588c2ecf20Sopenharmony_ci#define DIGI_BREAK_ERROR			32
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci/* out of band status */
1618c2ecf20Sopenharmony_ci#define DIGI_NO_ERROR				0
1628c2ecf20Sopenharmony_ci#define DIGI_BAD_FIRST_PARAMETER		1
1638c2ecf20Sopenharmony_ci#define DIGI_BAD_SECOND_PARAMETER		2
1648c2ecf20Sopenharmony_ci#define DIGI_INVALID_LINE			3
1658c2ecf20Sopenharmony_ci#define DIGI_INVALID_OPCODE			4
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci/* input signals */
1688c2ecf20Sopenharmony_ci#define DIGI_READ_INPUT_SIGNALS_SLOT		1
1698c2ecf20Sopenharmony_ci#define DIGI_READ_INPUT_SIGNALS_ERR		2
1708c2ecf20Sopenharmony_ci#define DIGI_READ_INPUT_SIGNALS_BUSY		4
1718c2ecf20Sopenharmony_ci#define DIGI_READ_INPUT_SIGNALS_PE		8
1728c2ecf20Sopenharmony_ci#define DIGI_READ_INPUT_SIGNALS_CTS		16
1738c2ecf20Sopenharmony_ci#define DIGI_READ_INPUT_SIGNALS_DSR		32
1748c2ecf20Sopenharmony_ci#define DIGI_READ_INPUT_SIGNALS_RI		64
1758c2ecf20Sopenharmony_ci#define DIGI_READ_INPUT_SIGNALS_DCD		128
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci/* Structures */
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_cistruct digi_serial {
1818c2ecf20Sopenharmony_ci	spinlock_t ds_serial_lock;
1828c2ecf20Sopenharmony_ci	struct usb_serial_port *ds_oob_port;	/* out-of-band port */
1838c2ecf20Sopenharmony_ci	int ds_oob_port_num;			/* index of out-of-band port */
1848c2ecf20Sopenharmony_ci	int ds_device_started;
1858c2ecf20Sopenharmony_ci};
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistruct digi_port {
1888c2ecf20Sopenharmony_ci	spinlock_t dp_port_lock;
1898c2ecf20Sopenharmony_ci	int dp_port_num;
1908c2ecf20Sopenharmony_ci	int dp_out_buf_len;
1918c2ecf20Sopenharmony_ci	unsigned char dp_out_buf[DIGI_OUT_BUF_SIZE];
1928c2ecf20Sopenharmony_ci	int dp_write_urb_in_use;
1938c2ecf20Sopenharmony_ci	unsigned int dp_modem_signals;
1948c2ecf20Sopenharmony_ci	int dp_transmit_idle;
1958c2ecf20Sopenharmony_ci	wait_queue_head_t dp_transmit_idle_wait;
1968c2ecf20Sopenharmony_ci	int dp_throttled;
1978c2ecf20Sopenharmony_ci	int dp_throttle_restart;
1988c2ecf20Sopenharmony_ci	wait_queue_head_t dp_flush_wait;
1998c2ecf20Sopenharmony_ci	wait_queue_head_t dp_close_wait;	/* wait queue for close */
2008c2ecf20Sopenharmony_ci	struct usb_serial_port *dp_port;
2018c2ecf20Sopenharmony_ci};
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci/* Local Function Declarations */
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cistatic int digi_write_oob_command(struct usb_serial_port *port,
2078c2ecf20Sopenharmony_ci	unsigned char *buf, int count, int interruptible);
2088c2ecf20Sopenharmony_cistatic int digi_write_inb_command(struct usb_serial_port *port,
2098c2ecf20Sopenharmony_ci	unsigned char *buf, int count, unsigned long timeout);
2108c2ecf20Sopenharmony_cistatic int digi_set_modem_signals(struct usb_serial_port *port,
2118c2ecf20Sopenharmony_ci	unsigned int modem_signals, int interruptible);
2128c2ecf20Sopenharmony_cistatic int digi_transmit_idle(struct usb_serial_port *port,
2138c2ecf20Sopenharmony_ci	unsigned long timeout);
2148c2ecf20Sopenharmony_cistatic void digi_rx_throttle(struct tty_struct *tty);
2158c2ecf20Sopenharmony_cistatic void digi_rx_unthrottle(struct tty_struct *tty);
2168c2ecf20Sopenharmony_cistatic void digi_set_termios(struct tty_struct *tty,
2178c2ecf20Sopenharmony_ci		struct usb_serial_port *port, struct ktermios *old_termios);
2188c2ecf20Sopenharmony_cistatic void digi_break_ctl(struct tty_struct *tty, int break_state);
2198c2ecf20Sopenharmony_cistatic int digi_tiocmget(struct tty_struct *tty);
2208c2ecf20Sopenharmony_cistatic int digi_tiocmset(struct tty_struct *tty, unsigned int set,
2218c2ecf20Sopenharmony_ci		unsigned int clear);
2228c2ecf20Sopenharmony_cistatic int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
2238c2ecf20Sopenharmony_ci		const unsigned char *buf, int count);
2248c2ecf20Sopenharmony_cistatic void digi_write_bulk_callback(struct urb *urb);
2258c2ecf20Sopenharmony_cistatic int digi_write_room(struct tty_struct *tty);
2268c2ecf20Sopenharmony_cistatic int digi_chars_in_buffer(struct tty_struct *tty);
2278c2ecf20Sopenharmony_cistatic int digi_open(struct tty_struct *tty, struct usb_serial_port *port);
2288c2ecf20Sopenharmony_cistatic void digi_close(struct usb_serial_port *port);
2298c2ecf20Sopenharmony_cistatic void digi_dtr_rts(struct usb_serial_port *port, int on);
2308c2ecf20Sopenharmony_cistatic int digi_startup_device(struct usb_serial *serial);
2318c2ecf20Sopenharmony_cistatic int digi_startup(struct usb_serial *serial);
2328c2ecf20Sopenharmony_cistatic void digi_disconnect(struct usb_serial *serial);
2338c2ecf20Sopenharmony_cistatic void digi_release(struct usb_serial *serial);
2348c2ecf20Sopenharmony_cistatic int digi_port_probe(struct usb_serial_port *port);
2358c2ecf20Sopenharmony_cistatic int digi_port_remove(struct usb_serial_port *port);
2368c2ecf20Sopenharmony_cistatic void digi_read_bulk_callback(struct urb *urb);
2378c2ecf20Sopenharmony_cistatic int digi_read_inb_callback(struct urb *urb);
2388c2ecf20Sopenharmony_cistatic int digi_read_oob_callback(struct urb *urb);
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_cistatic const struct usb_device_id id_table_combined[] = {
2428c2ecf20Sopenharmony_ci	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
2438c2ecf20Sopenharmony_ci	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
2448c2ecf20Sopenharmony_ci	{ }						/* Terminating entry */
2458c2ecf20Sopenharmony_ci};
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_cistatic const struct usb_device_id id_table_2[] = {
2488c2ecf20Sopenharmony_ci	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
2498c2ecf20Sopenharmony_ci	{ }						/* Terminating entry */
2508c2ecf20Sopenharmony_ci};
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_cistatic const struct usb_device_id id_table_4[] = {
2538c2ecf20Sopenharmony_ci	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
2548c2ecf20Sopenharmony_ci	{ }						/* Terminating entry */
2558c2ecf20Sopenharmony_ci};
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, id_table_combined);
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci/* device info needed for the Digi serial converter */
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_cistatic struct usb_serial_driver digi_acceleport_2_device = {
2628c2ecf20Sopenharmony_ci	.driver = {
2638c2ecf20Sopenharmony_ci		.owner =		THIS_MODULE,
2648c2ecf20Sopenharmony_ci		.name =			"digi_2",
2658c2ecf20Sopenharmony_ci	},
2668c2ecf20Sopenharmony_ci	.description =			"Digi 2 port USB adapter",
2678c2ecf20Sopenharmony_ci	.id_table =			id_table_2,
2688c2ecf20Sopenharmony_ci	.num_ports =			3,
2698c2ecf20Sopenharmony_ci	.num_bulk_in =			4,
2708c2ecf20Sopenharmony_ci	.num_bulk_out =			4,
2718c2ecf20Sopenharmony_ci	.open =				digi_open,
2728c2ecf20Sopenharmony_ci	.close =			digi_close,
2738c2ecf20Sopenharmony_ci	.dtr_rts =			digi_dtr_rts,
2748c2ecf20Sopenharmony_ci	.write =			digi_write,
2758c2ecf20Sopenharmony_ci	.write_room =			digi_write_room,
2768c2ecf20Sopenharmony_ci	.write_bulk_callback = 		digi_write_bulk_callback,
2778c2ecf20Sopenharmony_ci	.read_bulk_callback =		digi_read_bulk_callback,
2788c2ecf20Sopenharmony_ci	.chars_in_buffer =		digi_chars_in_buffer,
2798c2ecf20Sopenharmony_ci	.throttle =			digi_rx_throttle,
2808c2ecf20Sopenharmony_ci	.unthrottle =			digi_rx_unthrottle,
2818c2ecf20Sopenharmony_ci	.set_termios =			digi_set_termios,
2828c2ecf20Sopenharmony_ci	.break_ctl =			digi_break_ctl,
2838c2ecf20Sopenharmony_ci	.tiocmget =			digi_tiocmget,
2848c2ecf20Sopenharmony_ci	.tiocmset =			digi_tiocmset,
2858c2ecf20Sopenharmony_ci	.attach =			digi_startup,
2868c2ecf20Sopenharmony_ci	.disconnect =			digi_disconnect,
2878c2ecf20Sopenharmony_ci	.release =			digi_release,
2888c2ecf20Sopenharmony_ci	.port_probe =			digi_port_probe,
2898c2ecf20Sopenharmony_ci	.port_remove =			digi_port_remove,
2908c2ecf20Sopenharmony_ci};
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_cistatic struct usb_serial_driver digi_acceleport_4_device = {
2938c2ecf20Sopenharmony_ci	.driver = {
2948c2ecf20Sopenharmony_ci		.owner =		THIS_MODULE,
2958c2ecf20Sopenharmony_ci		.name =			"digi_4",
2968c2ecf20Sopenharmony_ci	},
2978c2ecf20Sopenharmony_ci	.description =			"Digi 4 port USB adapter",
2988c2ecf20Sopenharmony_ci	.id_table =			id_table_4,
2998c2ecf20Sopenharmony_ci	.num_ports =			4,
3008c2ecf20Sopenharmony_ci	.num_bulk_in =			5,
3018c2ecf20Sopenharmony_ci	.num_bulk_out =			5,
3028c2ecf20Sopenharmony_ci	.open =				digi_open,
3038c2ecf20Sopenharmony_ci	.close =			digi_close,
3048c2ecf20Sopenharmony_ci	.write =			digi_write,
3058c2ecf20Sopenharmony_ci	.write_room =			digi_write_room,
3068c2ecf20Sopenharmony_ci	.write_bulk_callback = 		digi_write_bulk_callback,
3078c2ecf20Sopenharmony_ci	.read_bulk_callback =		digi_read_bulk_callback,
3088c2ecf20Sopenharmony_ci	.chars_in_buffer =		digi_chars_in_buffer,
3098c2ecf20Sopenharmony_ci	.throttle =			digi_rx_throttle,
3108c2ecf20Sopenharmony_ci	.unthrottle =			digi_rx_unthrottle,
3118c2ecf20Sopenharmony_ci	.set_termios =			digi_set_termios,
3128c2ecf20Sopenharmony_ci	.break_ctl =			digi_break_ctl,
3138c2ecf20Sopenharmony_ci	.tiocmget =			digi_tiocmget,
3148c2ecf20Sopenharmony_ci	.tiocmset =			digi_tiocmset,
3158c2ecf20Sopenharmony_ci	.attach =			digi_startup,
3168c2ecf20Sopenharmony_ci	.disconnect =			digi_disconnect,
3178c2ecf20Sopenharmony_ci	.release =			digi_release,
3188c2ecf20Sopenharmony_ci	.port_probe =			digi_port_probe,
3198c2ecf20Sopenharmony_ci	.port_remove =			digi_port_remove,
3208c2ecf20Sopenharmony_ci};
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_cistatic struct usb_serial_driver * const serial_drivers[] = {
3238c2ecf20Sopenharmony_ci	&digi_acceleport_2_device, &digi_acceleport_4_device, NULL
3248c2ecf20Sopenharmony_ci};
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci/* Functions */
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci/*
3298c2ecf20Sopenharmony_ci *  Cond Wait Interruptible Timeout Irqrestore
3308c2ecf20Sopenharmony_ci *
3318c2ecf20Sopenharmony_ci *  Do spin_unlock_irqrestore and interruptible_sleep_on_timeout
3328c2ecf20Sopenharmony_ci *  so that wake ups are not lost if they occur between the unlock
3338c2ecf20Sopenharmony_ci *  and the sleep.  In other words, spin_unlock_irqrestore and
3348c2ecf20Sopenharmony_ci *  interruptible_sleep_on_timeout are "atomic" with respect to
3358c2ecf20Sopenharmony_ci *  wake ups.  This is used to implement condition variables.
3368c2ecf20Sopenharmony_ci *
3378c2ecf20Sopenharmony_ci *  interruptible_sleep_on_timeout is deprecated and has been replaced
3388c2ecf20Sopenharmony_ci *  with the equivalent code.
3398c2ecf20Sopenharmony_ci */
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_cistatic long cond_wait_interruptible_timeout_irqrestore(
3428c2ecf20Sopenharmony_ci	wait_queue_head_t *q, long timeout,
3438c2ecf20Sopenharmony_ci	spinlock_t *lock, unsigned long flags)
3448c2ecf20Sopenharmony_ci__releases(lock)
3458c2ecf20Sopenharmony_ci{
3468c2ecf20Sopenharmony_ci	DEFINE_WAIT(wait);
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	prepare_to_wait(q, &wait, TASK_INTERRUPTIBLE);
3498c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(lock, flags);
3508c2ecf20Sopenharmony_ci	timeout = schedule_timeout(timeout);
3518c2ecf20Sopenharmony_ci	finish_wait(q, &wait);
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	return timeout;
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci/*
3578c2ecf20Sopenharmony_ci *  Digi Write OOB Command
3588c2ecf20Sopenharmony_ci *
3598c2ecf20Sopenharmony_ci *  Write commands on the out of band port.  Commands are 4
3608c2ecf20Sopenharmony_ci *  bytes each, multiple commands can be sent at once, and
3618c2ecf20Sopenharmony_ci *  no command will be split across USB packets.  Returns 0
3628c2ecf20Sopenharmony_ci *  if successful, -EINTR if interrupted while sleeping and
3638c2ecf20Sopenharmony_ci *  the interruptible flag is true, or a negative error
3648c2ecf20Sopenharmony_ci *  returned by usb_submit_urb.
3658c2ecf20Sopenharmony_ci */
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_cistatic int digi_write_oob_command(struct usb_serial_port *port,
3688c2ecf20Sopenharmony_ci	unsigned char *buf, int count, int interruptible)
3698c2ecf20Sopenharmony_ci{
3708c2ecf20Sopenharmony_ci	int ret = 0;
3718c2ecf20Sopenharmony_ci	int len;
3728c2ecf20Sopenharmony_ci	struct usb_serial_port *oob_port = (struct usb_serial_port *)((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
3738c2ecf20Sopenharmony_ci	struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
3748c2ecf20Sopenharmony_ci	unsigned long flags = 0;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	dev_dbg(&port->dev,
3778c2ecf20Sopenharmony_ci		"digi_write_oob_command: TOP: port=%d, count=%d\n",
3788c2ecf20Sopenharmony_ci		oob_priv->dp_port_num, count);
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
3818c2ecf20Sopenharmony_ci	while (count > 0) {
3828c2ecf20Sopenharmony_ci		while (oob_priv->dp_write_urb_in_use) {
3838c2ecf20Sopenharmony_ci			cond_wait_interruptible_timeout_irqrestore(
3848c2ecf20Sopenharmony_ci				&oob_port->write_wait, DIGI_RETRY_TIMEOUT,
3858c2ecf20Sopenharmony_ci				&oob_priv->dp_port_lock, flags);
3868c2ecf20Sopenharmony_ci			if (interruptible && signal_pending(current))
3878c2ecf20Sopenharmony_ci				return -EINTR;
3888c2ecf20Sopenharmony_ci			spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
3898c2ecf20Sopenharmony_ci		}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci		/* len must be a multiple of 4, so commands are not split */
3928c2ecf20Sopenharmony_ci		len = min(count, oob_port->bulk_out_size);
3938c2ecf20Sopenharmony_ci		if (len > 4)
3948c2ecf20Sopenharmony_ci			len &= ~3;
3958c2ecf20Sopenharmony_ci		memcpy(oob_port->write_urb->transfer_buffer, buf, len);
3968c2ecf20Sopenharmony_ci		oob_port->write_urb->transfer_buffer_length = len;
3978c2ecf20Sopenharmony_ci		ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
3988c2ecf20Sopenharmony_ci		if (ret == 0) {
3998c2ecf20Sopenharmony_ci			oob_priv->dp_write_urb_in_use = 1;
4008c2ecf20Sopenharmony_ci			count -= len;
4018c2ecf20Sopenharmony_ci			buf += len;
4028c2ecf20Sopenharmony_ci		}
4038c2ecf20Sopenharmony_ci	}
4048c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
4058c2ecf20Sopenharmony_ci	if (ret)
4068c2ecf20Sopenharmony_ci		dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n",
4078c2ecf20Sopenharmony_ci			__func__, ret);
4088c2ecf20Sopenharmony_ci	return ret;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci/*
4148c2ecf20Sopenharmony_ci *  Digi Write In Band Command
4158c2ecf20Sopenharmony_ci *
4168c2ecf20Sopenharmony_ci *  Write commands on the given port.  Commands are 4
4178c2ecf20Sopenharmony_ci *  bytes each, multiple commands can be sent at once, and
4188c2ecf20Sopenharmony_ci *  no command will be split across USB packets.  If timeout
4198c2ecf20Sopenharmony_ci *  is non-zero, write in band command will return after
4208c2ecf20Sopenharmony_ci *  waiting unsuccessfully for the URB status to clear for
4218c2ecf20Sopenharmony_ci *  timeout ticks.  Returns 0 if successful, or a negative
4228c2ecf20Sopenharmony_ci *  error returned by digi_write.
4238c2ecf20Sopenharmony_ci */
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_cistatic int digi_write_inb_command(struct usb_serial_port *port,
4268c2ecf20Sopenharmony_ci	unsigned char *buf, int count, unsigned long timeout)
4278c2ecf20Sopenharmony_ci{
4288c2ecf20Sopenharmony_ci	int ret = 0;
4298c2ecf20Sopenharmony_ci	int len;
4308c2ecf20Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
4318c2ecf20Sopenharmony_ci	unsigned char *data = port->write_urb->transfer_buffer;
4328c2ecf20Sopenharmony_ci	unsigned long flags = 0;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	dev_dbg(&port->dev, "digi_write_inb_command: TOP: port=%d, count=%d\n",
4358c2ecf20Sopenharmony_ci		priv->dp_port_num, count);
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	if (timeout)
4388c2ecf20Sopenharmony_ci		timeout += jiffies;
4398c2ecf20Sopenharmony_ci	else
4408c2ecf20Sopenharmony_ci		timeout = ULONG_MAX;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->dp_port_lock, flags);
4438c2ecf20Sopenharmony_ci	while (count > 0 && ret == 0) {
4448c2ecf20Sopenharmony_ci		while (priv->dp_write_urb_in_use &&
4458c2ecf20Sopenharmony_ci		       time_before(jiffies, timeout)) {
4468c2ecf20Sopenharmony_ci			cond_wait_interruptible_timeout_irqrestore(
4478c2ecf20Sopenharmony_ci				&port->write_wait, DIGI_RETRY_TIMEOUT,
4488c2ecf20Sopenharmony_ci				&priv->dp_port_lock, flags);
4498c2ecf20Sopenharmony_ci			if (signal_pending(current))
4508c2ecf20Sopenharmony_ci				return -EINTR;
4518c2ecf20Sopenharmony_ci			spin_lock_irqsave(&priv->dp_port_lock, flags);
4528c2ecf20Sopenharmony_ci		}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci		/* len must be a multiple of 4 and small enough to */
4558c2ecf20Sopenharmony_ci		/* guarantee the write will send buffered data first, */
4568c2ecf20Sopenharmony_ci		/* so commands are in order with data and not split */
4578c2ecf20Sopenharmony_ci		len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
4588c2ecf20Sopenharmony_ci		if (len > 4)
4598c2ecf20Sopenharmony_ci			len &= ~3;
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci		/* write any buffered data first */
4628c2ecf20Sopenharmony_ci		if (priv->dp_out_buf_len > 0) {
4638c2ecf20Sopenharmony_ci			data[0] = DIGI_CMD_SEND_DATA;
4648c2ecf20Sopenharmony_ci			data[1] = priv->dp_out_buf_len;
4658c2ecf20Sopenharmony_ci			memcpy(data + 2, priv->dp_out_buf,
4668c2ecf20Sopenharmony_ci				priv->dp_out_buf_len);
4678c2ecf20Sopenharmony_ci			memcpy(data + 2 + priv->dp_out_buf_len, buf, len);
4688c2ecf20Sopenharmony_ci			port->write_urb->transfer_buffer_length
4698c2ecf20Sopenharmony_ci				= priv->dp_out_buf_len + 2 + len;
4708c2ecf20Sopenharmony_ci		} else {
4718c2ecf20Sopenharmony_ci			memcpy(data, buf, len);
4728c2ecf20Sopenharmony_ci			port->write_urb->transfer_buffer_length = len;
4738c2ecf20Sopenharmony_ci		}
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci		ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
4768c2ecf20Sopenharmony_ci		if (ret == 0) {
4778c2ecf20Sopenharmony_ci			priv->dp_write_urb_in_use = 1;
4788c2ecf20Sopenharmony_ci			priv->dp_out_buf_len = 0;
4798c2ecf20Sopenharmony_ci			count -= len;
4808c2ecf20Sopenharmony_ci			buf += len;
4818c2ecf20Sopenharmony_ci		}
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	}
4848c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	if (ret)
4878c2ecf20Sopenharmony_ci		dev_err(&port->dev,
4888c2ecf20Sopenharmony_ci			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
4898c2ecf20Sopenharmony_ci			__func__, ret, priv->dp_port_num);
4908c2ecf20Sopenharmony_ci	return ret;
4918c2ecf20Sopenharmony_ci}
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci/*
4958c2ecf20Sopenharmony_ci *  Digi Set Modem Signals
4968c2ecf20Sopenharmony_ci *
4978c2ecf20Sopenharmony_ci *  Sets or clears DTR and RTS on the port, according to the
4988c2ecf20Sopenharmony_ci *  modem_signals argument.  Use TIOCM_DTR and TIOCM_RTS flags
4998c2ecf20Sopenharmony_ci *  for the modem_signals argument.  Returns 0 if successful,
5008c2ecf20Sopenharmony_ci *  -EINTR if interrupted while sleeping, or a non-zero error
5018c2ecf20Sopenharmony_ci *  returned by usb_submit_urb.
5028c2ecf20Sopenharmony_ci */
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_cistatic int digi_set_modem_signals(struct usb_serial_port *port,
5058c2ecf20Sopenharmony_ci	unsigned int modem_signals, int interruptible)
5068c2ecf20Sopenharmony_ci{
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	int ret;
5098c2ecf20Sopenharmony_ci	struct digi_port *port_priv = usb_get_serial_port_data(port);
5108c2ecf20Sopenharmony_ci	struct usb_serial_port *oob_port = (struct usb_serial_port *) ((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
5118c2ecf20Sopenharmony_ci	struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
5128c2ecf20Sopenharmony_ci	unsigned char *data = oob_port->write_urb->transfer_buffer;
5138c2ecf20Sopenharmony_ci	unsigned long flags = 0;
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	dev_dbg(&port->dev,
5178c2ecf20Sopenharmony_ci		"digi_set_modem_signals: TOP: port=%d, modem_signals=0x%x\n",
5188c2ecf20Sopenharmony_ci		port_priv->dp_port_num, modem_signals);
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
5218c2ecf20Sopenharmony_ci	spin_lock(&port_priv->dp_port_lock);
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	while (oob_priv->dp_write_urb_in_use) {
5248c2ecf20Sopenharmony_ci		spin_unlock(&port_priv->dp_port_lock);
5258c2ecf20Sopenharmony_ci		cond_wait_interruptible_timeout_irqrestore(
5268c2ecf20Sopenharmony_ci			&oob_port->write_wait, DIGI_RETRY_TIMEOUT,
5278c2ecf20Sopenharmony_ci			&oob_priv->dp_port_lock, flags);
5288c2ecf20Sopenharmony_ci		if (interruptible && signal_pending(current))
5298c2ecf20Sopenharmony_ci			return -EINTR;
5308c2ecf20Sopenharmony_ci		spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
5318c2ecf20Sopenharmony_ci		spin_lock(&port_priv->dp_port_lock);
5328c2ecf20Sopenharmony_ci	}
5338c2ecf20Sopenharmony_ci	data[0] = DIGI_CMD_SET_DTR_SIGNAL;
5348c2ecf20Sopenharmony_ci	data[1] = port_priv->dp_port_num;
5358c2ecf20Sopenharmony_ci	data[2] = (modem_signals & TIOCM_DTR) ?
5368c2ecf20Sopenharmony_ci					DIGI_DTR_ACTIVE : DIGI_DTR_INACTIVE;
5378c2ecf20Sopenharmony_ci	data[3] = 0;
5388c2ecf20Sopenharmony_ci	data[4] = DIGI_CMD_SET_RTS_SIGNAL;
5398c2ecf20Sopenharmony_ci	data[5] = port_priv->dp_port_num;
5408c2ecf20Sopenharmony_ci	data[6] = (modem_signals & TIOCM_RTS) ?
5418c2ecf20Sopenharmony_ci					DIGI_RTS_ACTIVE : DIGI_RTS_INACTIVE;
5428c2ecf20Sopenharmony_ci	data[7] = 0;
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	oob_port->write_urb->transfer_buffer_length = 8;
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
5478c2ecf20Sopenharmony_ci	if (ret == 0) {
5488c2ecf20Sopenharmony_ci		oob_priv->dp_write_urb_in_use = 1;
5498c2ecf20Sopenharmony_ci		port_priv->dp_modem_signals &= ~(TIOCM_DTR | TIOCM_RTS);
5508c2ecf20Sopenharmony_ci		port_priv->dp_modem_signals |=
5518c2ecf20Sopenharmony_ci				modem_signals & (TIOCM_DTR | TIOCM_RTS);
5528c2ecf20Sopenharmony_ci	}
5538c2ecf20Sopenharmony_ci	spin_unlock(&port_priv->dp_port_lock);
5548c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
5558c2ecf20Sopenharmony_ci	if (ret)
5568c2ecf20Sopenharmony_ci		dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n",
5578c2ecf20Sopenharmony_ci			__func__, ret);
5588c2ecf20Sopenharmony_ci	return ret;
5598c2ecf20Sopenharmony_ci}
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci/*
5628c2ecf20Sopenharmony_ci *  Digi Transmit Idle
5638c2ecf20Sopenharmony_ci *
5648c2ecf20Sopenharmony_ci *  Digi transmit idle waits, up to timeout ticks, for the transmitter
5658c2ecf20Sopenharmony_ci *  to go idle.  It returns 0 if successful or a negative error.
5668c2ecf20Sopenharmony_ci *
5678c2ecf20Sopenharmony_ci *  There are race conditions here if more than one process is calling
5688c2ecf20Sopenharmony_ci *  digi_transmit_idle on the same port at the same time.  However, this
5698c2ecf20Sopenharmony_ci *  is only called from close, and only one process can be in close on a
5708c2ecf20Sopenharmony_ci *  port at a time, so its ok.
5718c2ecf20Sopenharmony_ci */
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_cistatic int digi_transmit_idle(struct usb_serial_port *port,
5748c2ecf20Sopenharmony_ci	unsigned long timeout)
5758c2ecf20Sopenharmony_ci{
5768c2ecf20Sopenharmony_ci	int ret;
5778c2ecf20Sopenharmony_ci	unsigned char buf[2];
5788c2ecf20Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
5798c2ecf20Sopenharmony_ci	unsigned long flags = 0;
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->dp_port_lock, flags);
5828c2ecf20Sopenharmony_ci	priv->dp_transmit_idle = 0;
5838c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	buf[0] = DIGI_CMD_TRANSMIT_IDLE;
5868c2ecf20Sopenharmony_ci	buf[1] = 0;
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	timeout += jiffies;
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	ret = digi_write_inb_command(port, buf, 2, timeout - jiffies);
5918c2ecf20Sopenharmony_ci	if (ret != 0)
5928c2ecf20Sopenharmony_ci		return ret;
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->dp_port_lock, flags);
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	while (time_before(jiffies, timeout) && !priv->dp_transmit_idle) {
5978c2ecf20Sopenharmony_ci		cond_wait_interruptible_timeout_irqrestore(
5988c2ecf20Sopenharmony_ci			&priv->dp_transmit_idle_wait, DIGI_RETRY_TIMEOUT,
5998c2ecf20Sopenharmony_ci			&priv->dp_port_lock, flags);
6008c2ecf20Sopenharmony_ci		if (signal_pending(current))
6018c2ecf20Sopenharmony_ci			return -EINTR;
6028c2ecf20Sopenharmony_ci		spin_lock_irqsave(&priv->dp_port_lock, flags);
6038c2ecf20Sopenharmony_ci	}
6048c2ecf20Sopenharmony_ci	priv->dp_transmit_idle = 0;
6058c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
6068c2ecf20Sopenharmony_ci	return 0;
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci}
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_cistatic void digi_rx_throttle(struct tty_struct *tty)
6128c2ecf20Sopenharmony_ci{
6138c2ecf20Sopenharmony_ci	unsigned long flags;
6148c2ecf20Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
6158c2ecf20Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	/* stop receiving characters by not resubmitting the read urb */
6188c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->dp_port_lock, flags);
6198c2ecf20Sopenharmony_ci	priv->dp_throttled = 1;
6208c2ecf20Sopenharmony_ci	priv->dp_throttle_restart = 0;
6218c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
6228c2ecf20Sopenharmony_ci}
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_cistatic void digi_rx_unthrottle(struct tty_struct *tty)
6268c2ecf20Sopenharmony_ci{
6278c2ecf20Sopenharmony_ci	int ret = 0;
6288c2ecf20Sopenharmony_ci	unsigned long flags;
6298c2ecf20Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
6308c2ecf20Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->dp_port_lock, flags);
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	/* restart read chain */
6358c2ecf20Sopenharmony_ci	if (priv->dp_throttle_restart)
6368c2ecf20Sopenharmony_ci		ret = usb_submit_urb(port->read_urb, GFP_ATOMIC);
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	/* turn throttle off */
6398c2ecf20Sopenharmony_ci	priv->dp_throttled = 0;
6408c2ecf20Sopenharmony_ci	priv->dp_throttle_restart = 0;
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	if (ret)
6458c2ecf20Sopenharmony_ci		dev_err(&port->dev,
6468c2ecf20Sopenharmony_ci			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
6478c2ecf20Sopenharmony_ci			__func__, ret, priv->dp_port_num);
6488c2ecf20Sopenharmony_ci}
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_cistatic void digi_set_termios(struct tty_struct *tty,
6528c2ecf20Sopenharmony_ci		struct usb_serial_port *port, struct ktermios *old_termios)
6538c2ecf20Sopenharmony_ci{
6548c2ecf20Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
6558c2ecf20Sopenharmony_ci	struct device *dev = &port->dev;
6568c2ecf20Sopenharmony_ci	unsigned int iflag = tty->termios.c_iflag;
6578c2ecf20Sopenharmony_ci	unsigned int cflag = tty->termios.c_cflag;
6588c2ecf20Sopenharmony_ci	unsigned int old_iflag = old_termios->c_iflag;
6598c2ecf20Sopenharmony_ci	unsigned int old_cflag = old_termios->c_cflag;
6608c2ecf20Sopenharmony_ci	unsigned char buf[32];
6618c2ecf20Sopenharmony_ci	unsigned int modem_signals;
6628c2ecf20Sopenharmony_ci	int arg, ret;
6638c2ecf20Sopenharmony_ci	int i = 0;
6648c2ecf20Sopenharmony_ci	speed_t baud;
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	dev_dbg(dev,
6678c2ecf20Sopenharmony_ci		"digi_set_termios: TOP: port=%d, iflag=0x%x, old_iflag=0x%x, cflag=0x%x, old_cflag=0x%x\n",
6688c2ecf20Sopenharmony_ci		priv->dp_port_num, iflag, old_iflag, cflag, old_cflag);
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	/* set baud rate */
6718c2ecf20Sopenharmony_ci	baud = tty_get_baud_rate(tty);
6728c2ecf20Sopenharmony_ci	if (baud != tty_termios_baud_rate(old_termios)) {
6738c2ecf20Sopenharmony_ci		arg = -1;
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci		/* reassert DTR and (maybe) RTS on transition from B0 */
6768c2ecf20Sopenharmony_ci		if ((old_cflag & CBAUD) == B0) {
6778c2ecf20Sopenharmony_ci			/* don't set RTS if using hardware flow control */
6788c2ecf20Sopenharmony_ci			/* and throttling input */
6798c2ecf20Sopenharmony_ci			modem_signals = TIOCM_DTR;
6808c2ecf20Sopenharmony_ci			if (!C_CRTSCTS(tty) || !tty_throttled(tty))
6818c2ecf20Sopenharmony_ci				modem_signals |= TIOCM_RTS;
6828c2ecf20Sopenharmony_ci			digi_set_modem_signals(port, modem_signals, 1);
6838c2ecf20Sopenharmony_ci		}
6848c2ecf20Sopenharmony_ci		switch (baud) {
6858c2ecf20Sopenharmony_ci		/* drop DTR and RTS on transition to B0 */
6868c2ecf20Sopenharmony_ci		case 0: digi_set_modem_signals(port, 0, 1); break;
6878c2ecf20Sopenharmony_ci		case 50: arg = DIGI_BAUD_50; break;
6888c2ecf20Sopenharmony_ci		case 75: arg = DIGI_BAUD_75; break;
6898c2ecf20Sopenharmony_ci		case 110: arg = DIGI_BAUD_110; break;
6908c2ecf20Sopenharmony_ci		case 150: arg = DIGI_BAUD_150; break;
6918c2ecf20Sopenharmony_ci		case 200: arg = DIGI_BAUD_200; break;
6928c2ecf20Sopenharmony_ci		case 300: arg = DIGI_BAUD_300; break;
6938c2ecf20Sopenharmony_ci		case 600: arg = DIGI_BAUD_600; break;
6948c2ecf20Sopenharmony_ci		case 1200: arg = DIGI_BAUD_1200; break;
6958c2ecf20Sopenharmony_ci		case 1800: arg = DIGI_BAUD_1800; break;
6968c2ecf20Sopenharmony_ci		case 2400: arg = DIGI_BAUD_2400; break;
6978c2ecf20Sopenharmony_ci		case 4800: arg = DIGI_BAUD_4800; break;
6988c2ecf20Sopenharmony_ci		case 9600: arg = DIGI_BAUD_9600; break;
6998c2ecf20Sopenharmony_ci		case 19200: arg = DIGI_BAUD_19200; break;
7008c2ecf20Sopenharmony_ci		case 38400: arg = DIGI_BAUD_38400; break;
7018c2ecf20Sopenharmony_ci		case 57600: arg = DIGI_BAUD_57600; break;
7028c2ecf20Sopenharmony_ci		case 115200: arg = DIGI_BAUD_115200; break;
7038c2ecf20Sopenharmony_ci		case 230400: arg = DIGI_BAUD_230400; break;
7048c2ecf20Sopenharmony_ci		case 460800: arg = DIGI_BAUD_460800; break;
7058c2ecf20Sopenharmony_ci		default:
7068c2ecf20Sopenharmony_ci			arg = DIGI_BAUD_9600;
7078c2ecf20Sopenharmony_ci			baud = 9600;
7088c2ecf20Sopenharmony_ci			break;
7098c2ecf20Sopenharmony_ci		}
7108c2ecf20Sopenharmony_ci		if (arg != -1) {
7118c2ecf20Sopenharmony_ci			buf[i++] = DIGI_CMD_SET_BAUD_RATE;
7128c2ecf20Sopenharmony_ci			buf[i++] = priv->dp_port_num;
7138c2ecf20Sopenharmony_ci			buf[i++] = arg;
7148c2ecf20Sopenharmony_ci			buf[i++] = 0;
7158c2ecf20Sopenharmony_ci		}
7168c2ecf20Sopenharmony_ci	}
7178c2ecf20Sopenharmony_ci	/* set parity */
7188c2ecf20Sopenharmony_ci	tty->termios.c_cflag &= ~CMSPAR;
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci	if ((cflag & (PARENB | PARODD)) != (old_cflag & (PARENB | PARODD))) {
7218c2ecf20Sopenharmony_ci		if (cflag & PARENB) {
7228c2ecf20Sopenharmony_ci			if (cflag & PARODD)
7238c2ecf20Sopenharmony_ci				arg = DIGI_PARITY_ODD;
7248c2ecf20Sopenharmony_ci			else
7258c2ecf20Sopenharmony_ci				arg = DIGI_PARITY_EVEN;
7268c2ecf20Sopenharmony_ci		} else {
7278c2ecf20Sopenharmony_ci			arg = DIGI_PARITY_NONE;
7288c2ecf20Sopenharmony_ci		}
7298c2ecf20Sopenharmony_ci		buf[i++] = DIGI_CMD_SET_PARITY;
7308c2ecf20Sopenharmony_ci		buf[i++] = priv->dp_port_num;
7318c2ecf20Sopenharmony_ci		buf[i++] = arg;
7328c2ecf20Sopenharmony_ci		buf[i++] = 0;
7338c2ecf20Sopenharmony_ci	}
7348c2ecf20Sopenharmony_ci	/* set word size */
7358c2ecf20Sopenharmony_ci	if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
7368c2ecf20Sopenharmony_ci		arg = -1;
7378c2ecf20Sopenharmony_ci		switch (cflag & CSIZE) {
7388c2ecf20Sopenharmony_ci		case CS5: arg = DIGI_WORD_SIZE_5; break;
7398c2ecf20Sopenharmony_ci		case CS6: arg = DIGI_WORD_SIZE_6; break;
7408c2ecf20Sopenharmony_ci		case CS7: arg = DIGI_WORD_SIZE_7; break;
7418c2ecf20Sopenharmony_ci		case CS8: arg = DIGI_WORD_SIZE_8; break;
7428c2ecf20Sopenharmony_ci		default:
7438c2ecf20Sopenharmony_ci			dev_dbg(dev,
7448c2ecf20Sopenharmony_ci				"digi_set_termios: can't handle word size %d\n",
7458c2ecf20Sopenharmony_ci				cflag & CSIZE);
7468c2ecf20Sopenharmony_ci			break;
7478c2ecf20Sopenharmony_ci		}
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci		if (arg != -1) {
7508c2ecf20Sopenharmony_ci			buf[i++] = DIGI_CMD_SET_WORD_SIZE;
7518c2ecf20Sopenharmony_ci			buf[i++] = priv->dp_port_num;
7528c2ecf20Sopenharmony_ci			buf[i++] = arg;
7538c2ecf20Sopenharmony_ci			buf[i++] = 0;
7548c2ecf20Sopenharmony_ci		}
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	}
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	/* set stop bits */
7598c2ecf20Sopenharmony_ci	if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci		if ((cflag & CSTOPB))
7628c2ecf20Sopenharmony_ci			arg = DIGI_STOP_BITS_2;
7638c2ecf20Sopenharmony_ci		else
7648c2ecf20Sopenharmony_ci			arg = DIGI_STOP_BITS_1;
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci		buf[i++] = DIGI_CMD_SET_STOP_BITS;
7678c2ecf20Sopenharmony_ci		buf[i++] = priv->dp_port_num;
7688c2ecf20Sopenharmony_ci		buf[i++] = arg;
7698c2ecf20Sopenharmony_ci		buf[i++] = 0;
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	}
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	/* set input flow control */
7748c2ecf20Sopenharmony_ci	if ((iflag & IXOFF) != (old_iflag & IXOFF) ||
7758c2ecf20Sopenharmony_ci			(cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
7768c2ecf20Sopenharmony_ci		arg = 0;
7778c2ecf20Sopenharmony_ci		if (iflag & IXOFF)
7788c2ecf20Sopenharmony_ci			arg |= DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
7798c2ecf20Sopenharmony_ci		else
7808c2ecf20Sopenharmony_ci			arg &= ~DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci		if (cflag & CRTSCTS) {
7838c2ecf20Sopenharmony_ci			arg |= DIGI_INPUT_FLOW_CONTROL_RTS;
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci			/* On USB-4 it is necessary to assert RTS prior */
7868c2ecf20Sopenharmony_ci			/* to selecting RTS input flow control.  */
7878c2ecf20Sopenharmony_ci			buf[i++] = DIGI_CMD_SET_RTS_SIGNAL;
7888c2ecf20Sopenharmony_ci			buf[i++] = priv->dp_port_num;
7898c2ecf20Sopenharmony_ci			buf[i++] = DIGI_RTS_ACTIVE;
7908c2ecf20Sopenharmony_ci			buf[i++] = 0;
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci		} else {
7938c2ecf20Sopenharmony_ci			arg &= ~DIGI_INPUT_FLOW_CONTROL_RTS;
7948c2ecf20Sopenharmony_ci		}
7958c2ecf20Sopenharmony_ci		buf[i++] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
7968c2ecf20Sopenharmony_ci		buf[i++] = priv->dp_port_num;
7978c2ecf20Sopenharmony_ci		buf[i++] = arg;
7988c2ecf20Sopenharmony_ci		buf[i++] = 0;
7998c2ecf20Sopenharmony_ci	}
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	/* set output flow control */
8028c2ecf20Sopenharmony_ci	if ((iflag & IXON) != (old_iflag & IXON) ||
8038c2ecf20Sopenharmony_ci			(cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
8048c2ecf20Sopenharmony_ci		arg = 0;
8058c2ecf20Sopenharmony_ci		if (iflag & IXON)
8068c2ecf20Sopenharmony_ci			arg |= DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
8078c2ecf20Sopenharmony_ci		else
8088c2ecf20Sopenharmony_ci			arg &= ~DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci		if (cflag & CRTSCTS)
8118c2ecf20Sopenharmony_ci			arg |= DIGI_OUTPUT_FLOW_CONTROL_CTS;
8128c2ecf20Sopenharmony_ci		else
8138c2ecf20Sopenharmony_ci			arg &= ~DIGI_OUTPUT_FLOW_CONTROL_CTS;
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ci		buf[i++] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
8168c2ecf20Sopenharmony_ci		buf[i++] = priv->dp_port_num;
8178c2ecf20Sopenharmony_ci		buf[i++] = arg;
8188c2ecf20Sopenharmony_ci		buf[i++] = 0;
8198c2ecf20Sopenharmony_ci	}
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	/* set receive enable/disable */
8228c2ecf20Sopenharmony_ci	if ((cflag & CREAD) != (old_cflag & CREAD)) {
8238c2ecf20Sopenharmony_ci		if (cflag & CREAD)
8248c2ecf20Sopenharmony_ci			arg = DIGI_ENABLE;
8258c2ecf20Sopenharmony_ci		else
8268c2ecf20Sopenharmony_ci			arg = DIGI_DISABLE;
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci		buf[i++] = DIGI_CMD_RECEIVE_ENABLE;
8298c2ecf20Sopenharmony_ci		buf[i++] = priv->dp_port_num;
8308c2ecf20Sopenharmony_ci		buf[i++] = arg;
8318c2ecf20Sopenharmony_ci		buf[i++] = 0;
8328c2ecf20Sopenharmony_ci	}
8338c2ecf20Sopenharmony_ci	ret = digi_write_oob_command(port, buf, i, 1);
8348c2ecf20Sopenharmony_ci	if (ret != 0)
8358c2ecf20Sopenharmony_ci		dev_dbg(dev, "digi_set_termios: write oob failed, ret=%d\n", ret);
8368c2ecf20Sopenharmony_ci	tty_encode_baud_rate(tty, baud, baud);
8378c2ecf20Sopenharmony_ci}
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_cistatic void digi_break_ctl(struct tty_struct *tty, int break_state)
8418c2ecf20Sopenharmony_ci{
8428c2ecf20Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
8438c2ecf20Sopenharmony_ci	unsigned char buf[4];
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	buf[0] = DIGI_CMD_BREAK_CONTROL;
8468c2ecf20Sopenharmony_ci	buf[1] = 2;				/* length */
8478c2ecf20Sopenharmony_ci	buf[2] = break_state ? 1 : 0;
8488c2ecf20Sopenharmony_ci	buf[3] = 0;				/* pad */
8498c2ecf20Sopenharmony_ci	digi_write_inb_command(port, buf, 4, 0);
8508c2ecf20Sopenharmony_ci}
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_cistatic int digi_tiocmget(struct tty_struct *tty)
8548c2ecf20Sopenharmony_ci{
8558c2ecf20Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
8568c2ecf20Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
8578c2ecf20Sopenharmony_ci	unsigned int val;
8588c2ecf20Sopenharmony_ci	unsigned long flags;
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->dp_port_lock, flags);
8618c2ecf20Sopenharmony_ci	val = priv->dp_modem_signals;
8628c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
8638c2ecf20Sopenharmony_ci	return val;
8648c2ecf20Sopenharmony_ci}
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_cistatic int digi_tiocmset(struct tty_struct *tty,
8688c2ecf20Sopenharmony_ci					unsigned int set, unsigned int clear)
8698c2ecf20Sopenharmony_ci{
8708c2ecf20Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
8718c2ecf20Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
8728c2ecf20Sopenharmony_ci	unsigned int val;
8738c2ecf20Sopenharmony_ci	unsigned long flags;
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->dp_port_lock, flags);
8768c2ecf20Sopenharmony_ci	val = (priv->dp_modem_signals & ~clear) | set;
8778c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
8788c2ecf20Sopenharmony_ci	return digi_set_modem_signals(port, val, 1);
8798c2ecf20Sopenharmony_ci}
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_cistatic int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
8838c2ecf20Sopenharmony_ci					const unsigned char *buf, int count)
8848c2ecf20Sopenharmony_ci{
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	int ret, data_len, new_len;
8878c2ecf20Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
8888c2ecf20Sopenharmony_ci	unsigned char *data = port->write_urb->transfer_buffer;
8898c2ecf20Sopenharmony_ci	unsigned long flags = 0;
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci	dev_dbg(&port->dev,
8928c2ecf20Sopenharmony_ci		"digi_write: TOP: port=%d, count=%d, in_interrupt=%ld\n",
8938c2ecf20Sopenharmony_ci		priv->dp_port_num, count, in_interrupt());
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	/* copy user data (which can sleep) before getting spin lock */
8968c2ecf20Sopenharmony_ci	count = min(count, port->bulk_out_size-2);
8978c2ecf20Sopenharmony_ci	count = min(64, count);
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci	/* be sure only one write proceeds at a time */
9008c2ecf20Sopenharmony_ci	/* there are races on the port private buffer */
9018c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->dp_port_lock, flags);
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_ci	/* wait for urb status clear to submit another urb */
9048c2ecf20Sopenharmony_ci	if (priv->dp_write_urb_in_use) {
9058c2ecf20Sopenharmony_ci		/* buffer data if count is 1 (probably put_char) if possible */
9068c2ecf20Sopenharmony_ci		if (count == 1 && priv->dp_out_buf_len < DIGI_OUT_BUF_SIZE) {
9078c2ecf20Sopenharmony_ci			priv->dp_out_buf[priv->dp_out_buf_len++] = *buf;
9088c2ecf20Sopenharmony_ci			new_len = 1;
9098c2ecf20Sopenharmony_ci		} else {
9108c2ecf20Sopenharmony_ci			new_len = 0;
9118c2ecf20Sopenharmony_ci		}
9128c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&priv->dp_port_lock, flags);
9138c2ecf20Sopenharmony_ci		return new_len;
9148c2ecf20Sopenharmony_ci	}
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_ci	/* allow space for any buffered data and for new data, up to */
9178c2ecf20Sopenharmony_ci	/* transfer buffer size - 2 (for command and length bytes) */
9188c2ecf20Sopenharmony_ci	new_len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
9198c2ecf20Sopenharmony_ci	data_len = new_len + priv->dp_out_buf_len;
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_ci	if (data_len == 0) {
9228c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&priv->dp_port_lock, flags);
9238c2ecf20Sopenharmony_ci		return 0;
9248c2ecf20Sopenharmony_ci	}
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci	port->write_urb->transfer_buffer_length = data_len+2;
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci	*data++ = DIGI_CMD_SEND_DATA;
9298c2ecf20Sopenharmony_ci	*data++ = data_len;
9308c2ecf20Sopenharmony_ci
9318c2ecf20Sopenharmony_ci	/* copy in buffered data first */
9328c2ecf20Sopenharmony_ci	memcpy(data, priv->dp_out_buf, priv->dp_out_buf_len);
9338c2ecf20Sopenharmony_ci	data += priv->dp_out_buf_len;
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_ci	/* copy in new data */
9368c2ecf20Sopenharmony_ci	memcpy(data, buf, new_len);
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci	ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
9398c2ecf20Sopenharmony_ci	if (ret == 0) {
9408c2ecf20Sopenharmony_ci		priv->dp_write_urb_in_use = 1;
9418c2ecf20Sopenharmony_ci		ret = new_len;
9428c2ecf20Sopenharmony_ci		priv->dp_out_buf_len = 0;
9438c2ecf20Sopenharmony_ci	}
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci	/* return length of new data written, or error */
9468c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
9478c2ecf20Sopenharmony_ci	if (ret < 0)
9488c2ecf20Sopenharmony_ci		dev_err_console(port,
9498c2ecf20Sopenharmony_ci			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
9508c2ecf20Sopenharmony_ci			__func__, ret, priv->dp_port_num);
9518c2ecf20Sopenharmony_ci	dev_dbg(&port->dev, "digi_write: returning %d\n", ret);
9528c2ecf20Sopenharmony_ci	return ret;
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_ci}
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_cistatic void digi_write_bulk_callback(struct urb *urb)
9578c2ecf20Sopenharmony_ci{
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci	struct usb_serial_port *port = urb->context;
9608c2ecf20Sopenharmony_ci	struct usb_serial *serial;
9618c2ecf20Sopenharmony_ci	struct digi_port *priv;
9628c2ecf20Sopenharmony_ci	struct digi_serial *serial_priv;
9638c2ecf20Sopenharmony_ci	unsigned long flags;
9648c2ecf20Sopenharmony_ci	int ret = 0;
9658c2ecf20Sopenharmony_ci	int status = urb->status;
9668c2ecf20Sopenharmony_ci	bool wakeup;
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_ci	/* port and serial sanity check */
9698c2ecf20Sopenharmony_ci	if (port == NULL || (priv = usb_get_serial_port_data(port)) == NULL) {
9708c2ecf20Sopenharmony_ci		pr_err("%s: port or port->private is NULL, status=%d\n",
9718c2ecf20Sopenharmony_ci			__func__, status);
9728c2ecf20Sopenharmony_ci		return;
9738c2ecf20Sopenharmony_ci	}
9748c2ecf20Sopenharmony_ci	serial = port->serial;
9758c2ecf20Sopenharmony_ci	if (serial == NULL || (serial_priv = usb_get_serial_data(serial)) == NULL) {
9768c2ecf20Sopenharmony_ci		dev_err(&port->dev,
9778c2ecf20Sopenharmony_ci			"%s: serial or serial->private is NULL, status=%d\n",
9788c2ecf20Sopenharmony_ci			__func__, status);
9798c2ecf20Sopenharmony_ci		return;
9808c2ecf20Sopenharmony_ci	}
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_ci	/* handle oob callback */
9838c2ecf20Sopenharmony_ci	if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
9848c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "digi_write_bulk_callback: oob callback\n");
9858c2ecf20Sopenharmony_ci		spin_lock_irqsave(&priv->dp_port_lock, flags);
9868c2ecf20Sopenharmony_ci		priv->dp_write_urb_in_use = 0;
9878c2ecf20Sopenharmony_ci		wake_up_interruptible(&port->write_wait);
9888c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&priv->dp_port_lock, flags);
9898c2ecf20Sopenharmony_ci		return;
9908c2ecf20Sopenharmony_ci	}
9918c2ecf20Sopenharmony_ci
9928c2ecf20Sopenharmony_ci	/* try to send any buffered data on this port */
9938c2ecf20Sopenharmony_ci	wakeup = true;
9948c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->dp_port_lock, flags);
9958c2ecf20Sopenharmony_ci	priv->dp_write_urb_in_use = 0;
9968c2ecf20Sopenharmony_ci	if (priv->dp_out_buf_len > 0) {
9978c2ecf20Sopenharmony_ci		*((unsigned char *)(port->write_urb->transfer_buffer))
9988c2ecf20Sopenharmony_ci			= (unsigned char)DIGI_CMD_SEND_DATA;
9998c2ecf20Sopenharmony_ci		*((unsigned char *)(port->write_urb->transfer_buffer) + 1)
10008c2ecf20Sopenharmony_ci			= (unsigned char)priv->dp_out_buf_len;
10018c2ecf20Sopenharmony_ci		port->write_urb->transfer_buffer_length =
10028c2ecf20Sopenharmony_ci						priv->dp_out_buf_len + 2;
10038c2ecf20Sopenharmony_ci		memcpy(port->write_urb->transfer_buffer + 2, priv->dp_out_buf,
10048c2ecf20Sopenharmony_ci			priv->dp_out_buf_len);
10058c2ecf20Sopenharmony_ci		ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
10068c2ecf20Sopenharmony_ci		if (ret == 0) {
10078c2ecf20Sopenharmony_ci			priv->dp_write_urb_in_use = 1;
10088c2ecf20Sopenharmony_ci			priv->dp_out_buf_len = 0;
10098c2ecf20Sopenharmony_ci			wakeup = false;
10108c2ecf20Sopenharmony_ci		}
10118c2ecf20Sopenharmony_ci	}
10128c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
10138c2ecf20Sopenharmony_ci
10148c2ecf20Sopenharmony_ci	if (ret && ret != -EPERM)
10158c2ecf20Sopenharmony_ci		dev_err_console(port,
10168c2ecf20Sopenharmony_ci			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
10178c2ecf20Sopenharmony_ci			__func__, ret, priv->dp_port_num);
10188c2ecf20Sopenharmony_ci
10198c2ecf20Sopenharmony_ci	if (wakeup)
10208c2ecf20Sopenharmony_ci		tty_port_tty_wakeup(&port->port);
10218c2ecf20Sopenharmony_ci}
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_cistatic int digi_write_room(struct tty_struct *tty)
10248c2ecf20Sopenharmony_ci{
10258c2ecf20Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
10268c2ecf20Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
10278c2ecf20Sopenharmony_ci	int room;
10288c2ecf20Sopenharmony_ci	unsigned long flags = 0;
10298c2ecf20Sopenharmony_ci
10308c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->dp_port_lock, flags);
10318c2ecf20Sopenharmony_ci
10328c2ecf20Sopenharmony_ci	if (priv->dp_write_urb_in_use)
10338c2ecf20Sopenharmony_ci		room = 0;
10348c2ecf20Sopenharmony_ci	else
10358c2ecf20Sopenharmony_ci		room = port->bulk_out_size - 2 - priv->dp_out_buf_len;
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
10388c2ecf20Sopenharmony_ci	dev_dbg(&port->dev, "digi_write_room: port=%d, room=%d\n", priv->dp_port_num, room);
10398c2ecf20Sopenharmony_ci	return room;
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_ci}
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_cistatic int digi_chars_in_buffer(struct tty_struct *tty)
10448c2ecf20Sopenharmony_ci{
10458c2ecf20Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
10468c2ecf20Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
10478c2ecf20Sopenharmony_ci
10488c2ecf20Sopenharmony_ci	if (priv->dp_write_urb_in_use) {
10498c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "digi_chars_in_buffer: port=%d, chars=%d\n",
10508c2ecf20Sopenharmony_ci			priv->dp_port_num, port->bulk_out_size - 2);
10518c2ecf20Sopenharmony_ci		/* return(port->bulk_out_size - 2); */
10528c2ecf20Sopenharmony_ci		return 256;
10538c2ecf20Sopenharmony_ci	} else {
10548c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "digi_chars_in_buffer: port=%d, chars=%d\n",
10558c2ecf20Sopenharmony_ci			priv->dp_port_num, priv->dp_out_buf_len);
10568c2ecf20Sopenharmony_ci		return priv->dp_out_buf_len;
10578c2ecf20Sopenharmony_ci	}
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_ci}
10608c2ecf20Sopenharmony_ci
10618c2ecf20Sopenharmony_cistatic void digi_dtr_rts(struct usb_serial_port *port, int on)
10628c2ecf20Sopenharmony_ci{
10638c2ecf20Sopenharmony_ci	/* Adjust DTR and RTS */
10648c2ecf20Sopenharmony_ci	digi_set_modem_signals(port, on * (TIOCM_DTR | TIOCM_RTS), 1);
10658c2ecf20Sopenharmony_ci}
10668c2ecf20Sopenharmony_ci
10678c2ecf20Sopenharmony_cistatic int digi_open(struct tty_struct *tty, struct usb_serial_port *port)
10688c2ecf20Sopenharmony_ci{
10698c2ecf20Sopenharmony_ci	int ret;
10708c2ecf20Sopenharmony_ci	unsigned char buf[32];
10718c2ecf20Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
10728c2ecf20Sopenharmony_ci	struct ktermios not_termios;
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_ci	/* be sure the device is started up */
10758c2ecf20Sopenharmony_ci	if (digi_startup_device(port->serial) != 0)
10768c2ecf20Sopenharmony_ci		return -ENXIO;
10778c2ecf20Sopenharmony_ci
10788c2ecf20Sopenharmony_ci	/* read modem signals automatically whenever they change */
10798c2ecf20Sopenharmony_ci	buf[0] = DIGI_CMD_READ_INPUT_SIGNALS;
10808c2ecf20Sopenharmony_ci	buf[1] = priv->dp_port_num;
10818c2ecf20Sopenharmony_ci	buf[2] = DIGI_ENABLE;
10828c2ecf20Sopenharmony_ci	buf[3] = 0;
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_ci	/* flush fifos */
10858c2ecf20Sopenharmony_ci	buf[4] = DIGI_CMD_IFLUSH_FIFO;
10868c2ecf20Sopenharmony_ci	buf[5] = priv->dp_port_num;
10878c2ecf20Sopenharmony_ci	buf[6] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
10888c2ecf20Sopenharmony_ci	buf[7] = 0;
10898c2ecf20Sopenharmony_ci
10908c2ecf20Sopenharmony_ci	ret = digi_write_oob_command(port, buf, 8, 1);
10918c2ecf20Sopenharmony_ci	if (ret != 0)
10928c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "digi_open: write oob failed, ret=%d\n", ret);
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_ci	/* set termios settings */
10958c2ecf20Sopenharmony_ci	if (tty) {
10968c2ecf20Sopenharmony_ci		not_termios.c_cflag = ~tty->termios.c_cflag;
10978c2ecf20Sopenharmony_ci		not_termios.c_iflag = ~tty->termios.c_iflag;
10988c2ecf20Sopenharmony_ci		digi_set_termios(tty, port, &not_termios);
10998c2ecf20Sopenharmony_ci	}
11008c2ecf20Sopenharmony_ci	return 0;
11018c2ecf20Sopenharmony_ci}
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_cistatic void digi_close(struct usb_serial_port *port)
11058c2ecf20Sopenharmony_ci{
11068c2ecf20Sopenharmony_ci	DEFINE_WAIT(wait);
11078c2ecf20Sopenharmony_ci	int ret;
11088c2ecf20Sopenharmony_ci	unsigned char buf[32];
11098c2ecf20Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci	mutex_lock(&port->serial->disc_mutex);
11128c2ecf20Sopenharmony_ci	/* if disconnected, just clear flags */
11138c2ecf20Sopenharmony_ci	if (port->serial->disconnected)
11148c2ecf20Sopenharmony_ci		goto exit;
11158c2ecf20Sopenharmony_ci
11168c2ecf20Sopenharmony_ci	/* FIXME: Transmit idle belongs in the wait_unti_sent path */
11178c2ecf20Sopenharmony_ci	digi_transmit_idle(port, DIGI_CLOSE_TIMEOUT);
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_ci	/* disable input flow control */
11208c2ecf20Sopenharmony_ci	buf[0] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
11218c2ecf20Sopenharmony_ci	buf[1] = priv->dp_port_num;
11228c2ecf20Sopenharmony_ci	buf[2] = DIGI_DISABLE;
11238c2ecf20Sopenharmony_ci	buf[3] = 0;
11248c2ecf20Sopenharmony_ci
11258c2ecf20Sopenharmony_ci	/* disable output flow control */
11268c2ecf20Sopenharmony_ci	buf[4] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
11278c2ecf20Sopenharmony_ci	buf[5] = priv->dp_port_num;
11288c2ecf20Sopenharmony_ci	buf[6] = DIGI_DISABLE;
11298c2ecf20Sopenharmony_ci	buf[7] = 0;
11308c2ecf20Sopenharmony_ci
11318c2ecf20Sopenharmony_ci	/* disable reading modem signals automatically */
11328c2ecf20Sopenharmony_ci	buf[8] = DIGI_CMD_READ_INPUT_SIGNALS;
11338c2ecf20Sopenharmony_ci	buf[9] = priv->dp_port_num;
11348c2ecf20Sopenharmony_ci	buf[10] = DIGI_DISABLE;
11358c2ecf20Sopenharmony_ci	buf[11] = 0;
11368c2ecf20Sopenharmony_ci
11378c2ecf20Sopenharmony_ci	/* disable receive */
11388c2ecf20Sopenharmony_ci	buf[12] = DIGI_CMD_RECEIVE_ENABLE;
11398c2ecf20Sopenharmony_ci	buf[13] = priv->dp_port_num;
11408c2ecf20Sopenharmony_ci	buf[14] = DIGI_DISABLE;
11418c2ecf20Sopenharmony_ci	buf[15] = 0;
11428c2ecf20Sopenharmony_ci
11438c2ecf20Sopenharmony_ci	/* flush fifos */
11448c2ecf20Sopenharmony_ci	buf[16] = DIGI_CMD_IFLUSH_FIFO;
11458c2ecf20Sopenharmony_ci	buf[17] = priv->dp_port_num;
11468c2ecf20Sopenharmony_ci	buf[18] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
11478c2ecf20Sopenharmony_ci	buf[19] = 0;
11488c2ecf20Sopenharmony_ci
11498c2ecf20Sopenharmony_ci	ret = digi_write_oob_command(port, buf, 20, 0);
11508c2ecf20Sopenharmony_ci	if (ret != 0)
11518c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "digi_close: write oob failed, ret=%d\n",
11528c2ecf20Sopenharmony_ci									ret);
11538c2ecf20Sopenharmony_ci	/* wait for final commands on oob port to complete */
11548c2ecf20Sopenharmony_ci	prepare_to_wait(&priv->dp_flush_wait, &wait,
11558c2ecf20Sopenharmony_ci			TASK_INTERRUPTIBLE);
11568c2ecf20Sopenharmony_ci	schedule_timeout(DIGI_CLOSE_TIMEOUT);
11578c2ecf20Sopenharmony_ci	finish_wait(&priv->dp_flush_wait, &wait);
11588c2ecf20Sopenharmony_ci
11598c2ecf20Sopenharmony_ci	/* shutdown any outstanding bulk writes */
11608c2ecf20Sopenharmony_ci	usb_kill_urb(port->write_urb);
11618c2ecf20Sopenharmony_ciexit:
11628c2ecf20Sopenharmony_ci	spin_lock_irq(&priv->dp_port_lock);
11638c2ecf20Sopenharmony_ci	priv->dp_write_urb_in_use = 0;
11648c2ecf20Sopenharmony_ci	wake_up_interruptible(&priv->dp_close_wait);
11658c2ecf20Sopenharmony_ci	spin_unlock_irq(&priv->dp_port_lock);
11668c2ecf20Sopenharmony_ci	mutex_unlock(&port->serial->disc_mutex);
11678c2ecf20Sopenharmony_ci}
11688c2ecf20Sopenharmony_ci
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_ci/*
11718c2ecf20Sopenharmony_ci *  Digi Startup Device
11728c2ecf20Sopenharmony_ci *
11738c2ecf20Sopenharmony_ci *  Starts reads on all ports.  Must be called AFTER startup, with
11748c2ecf20Sopenharmony_ci *  urbs initialized.  Returns 0 if successful, non-zero error otherwise.
11758c2ecf20Sopenharmony_ci */
11768c2ecf20Sopenharmony_ci
11778c2ecf20Sopenharmony_cistatic int digi_startup_device(struct usb_serial *serial)
11788c2ecf20Sopenharmony_ci{
11798c2ecf20Sopenharmony_ci	int i, ret = 0;
11808c2ecf20Sopenharmony_ci	struct digi_serial *serial_priv = usb_get_serial_data(serial);
11818c2ecf20Sopenharmony_ci	struct usb_serial_port *port;
11828c2ecf20Sopenharmony_ci
11838c2ecf20Sopenharmony_ci	/* be sure this happens exactly once */
11848c2ecf20Sopenharmony_ci	spin_lock(&serial_priv->ds_serial_lock);
11858c2ecf20Sopenharmony_ci	if (serial_priv->ds_device_started) {
11868c2ecf20Sopenharmony_ci		spin_unlock(&serial_priv->ds_serial_lock);
11878c2ecf20Sopenharmony_ci		return 0;
11888c2ecf20Sopenharmony_ci	}
11898c2ecf20Sopenharmony_ci	serial_priv->ds_device_started = 1;
11908c2ecf20Sopenharmony_ci	spin_unlock(&serial_priv->ds_serial_lock);
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ci	/* start reading from each bulk in endpoint for the device */
11938c2ecf20Sopenharmony_ci	/* set USB_DISABLE_SPD flag for write bulk urbs */
11948c2ecf20Sopenharmony_ci	for (i = 0; i < serial->type->num_ports + 1; i++) {
11958c2ecf20Sopenharmony_ci		port = serial->port[i];
11968c2ecf20Sopenharmony_ci		ret = usb_submit_urb(port->read_urb, GFP_KERNEL);
11978c2ecf20Sopenharmony_ci		if (ret != 0) {
11988c2ecf20Sopenharmony_ci			dev_err(&port->dev,
11998c2ecf20Sopenharmony_ci				"%s: usb_submit_urb failed, ret=%d, port=%d\n",
12008c2ecf20Sopenharmony_ci				__func__, ret, i);
12018c2ecf20Sopenharmony_ci			break;
12028c2ecf20Sopenharmony_ci		}
12038c2ecf20Sopenharmony_ci	}
12048c2ecf20Sopenharmony_ci	return ret;
12058c2ecf20Sopenharmony_ci}
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_cistatic int digi_port_init(struct usb_serial_port *port, unsigned port_num)
12088c2ecf20Sopenharmony_ci{
12098c2ecf20Sopenharmony_ci	struct digi_port *priv;
12108c2ecf20Sopenharmony_ci
12118c2ecf20Sopenharmony_ci	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
12128c2ecf20Sopenharmony_ci	if (!priv)
12138c2ecf20Sopenharmony_ci		return -ENOMEM;
12148c2ecf20Sopenharmony_ci
12158c2ecf20Sopenharmony_ci	spin_lock_init(&priv->dp_port_lock);
12168c2ecf20Sopenharmony_ci	priv->dp_port_num = port_num;
12178c2ecf20Sopenharmony_ci	init_waitqueue_head(&priv->dp_transmit_idle_wait);
12188c2ecf20Sopenharmony_ci	init_waitqueue_head(&priv->dp_flush_wait);
12198c2ecf20Sopenharmony_ci	init_waitqueue_head(&priv->dp_close_wait);
12208c2ecf20Sopenharmony_ci	priv->dp_port = port;
12218c2ecf20Sopenharmony_ci
12228c2ecf20Sopenharmony_ci	init_waitqueue_head(&port->write_wait);
12238c2ecf20Sopenharmony_ci
12248c2ecf20Sopenharmony_ci	usb_set_serial_port_data(port, priv);
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_ci	return 0;
12278c2ecf20Sopenharmony_ci}
12288c2ecf20Sopenharmony_ci
12298c2ecf20Sopenharmony_cistatic int digi_startup(struct usb_serial *serial)
12308c2ecf20Sopenharmony_ci{
12318c2ecf20Sopenharmony_ci	struct digi_serial *serial_priv;
12328c2ecf20Sopenharmony_ci	int ret;
12338c2ecf20Sopenharmony_ci
12348c2ecf20Sopenharmony_ci	serial_priv = kzalloc(sizeof(*serial_priv), GFP_KERNEL);
12358c2ecf20Sopenharmony_ci	if (!serial_priv)
12368c2ecf20Sopenharmony_ci		return -ENOMEM;
12378c2ecf20Sopenharmony_ci
12388c2ecf20Sopenharmony_ci	spin_lock_init(&serial_priv->ds_serial_lock);
12398c2ecf20Sopenharmony_ci	serial_priv->ds_oob_port_num = serial->type->num_ports;
12408c2ecf20Sopenharmony_ci	serial_priv->ds_oob_port = serial->port[serial_priv->ds_oob_port_num];
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ci	ret = digi_port_init(serial_priv->ds_oob_port,
12438c2ecf20Sopenharmony_ci						serial_priv->ds_oob_port_num);
12448c2ecf20Sopenharmony_ci	if (ret) {
12458c2ecf20Sopenharmony_ci		kfree(serial_priv);
12468c2ecf20Sopenharmony_ci		return ret;
12478c2ecf20Sopenharmony_ci	}
12488c2ecf20Sopenharmony_ci
12498c2ecf20Sopenharmony_ci	usb_set_serial_data(serial, serial_priv);
12508c2ecf20Sopenharmony_ci
12518c2ecf20Sopenharmony_ci	return 0;
12528c2ecf20Sopenharmony_ci}
12538c2ecf20Sopenharmony_ci
12548c2ecf20Sopenharmony_ci
12558c2ecf20Sopenharmony_cistatic void digi_disconnect(struct usb_serial *serial)
12568c2ecf20Sopenharmony_ci{
12578c2ecf20Sopenharmony_ci	int i;
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_ci	/* stop reads and writes on all ports */
12608c2ecf20Sopenharmony_ci	for (i = 0; i < serial->type->num_ports + 1; i++) {
12618c2ecf20Sopenharmony_ci		usb_kill_urb(serial->port[i]->read_urb);
12628c2ecf20Sopenharmony_ci		usb_kill_urb(serial->port[i]->write_urb);
12638c2ecf20Sopenharmony_ci	}
12648c2ecf20Sopenharmony_ci}
12658c2ecf20Sopenharmony_ci
12668c2ecf20Sopenharmony_ci
12678c2ecf20Sopenharmony_cistatic void digi_release(struct usb_serial *serial)
12688c2ecf20Sopenharmony_ci{
12698c2ecf20Sopenharmony_ci	struct digi_serial *serial_priv;
12708c2ecf20Sopenharmony_ci	struct digi_port *priv;
12718c2ecf20Sopenharmony_ci
12728c2ecf20Sopenharmony_ci	serial_priv = usb_get_serial_data(serial);
12738c2ecf20Sopenharmony_ci
12748c2ecf20Sopenharmony_ci	priv = usb_get_serial_port_data(serial_priv->ds_oob_port);
12758c2ecf20Sopenharmony_ci	kfree(priv);
12768c2ecf20Sopenharmony_ci
12778c2ecf20Sopenharmony_ci	kfree(serial_priv);
12788c2ecf20Sopenharmony_ci}
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_cistatic int digi_port_probe(struct usb_serial_port *port)
12818c2ecf20Sopenharmony_ci{
12828c2ecf20Sopenharmony_ci	return digi_port_init(port, port->port_number);
12838c2ecf20Sopenharmony_ci}
12848c2ecf20Sopenharmony_ci
12858c2ecf20Sopenharmony_cistatic int digi_port_remove(struct usb_serial_port *port)
12868c2ecf20Sopenharmony_ci{
12878c2ecf20Sopenharmony_ci	struct digi_port *priv;
12888c2ecf20Sopenharmony_ci
12898c2ecf20Sopenharmony_ci	priv = usb_get_serial_port_data(port);
12908c2ecf20Sopenharmony_ci	kfree(priv);
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_ci	return 0;
12938c2ecf20Sopenharmony_ci}
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_cistatic void digi_read_bulk_callback(struct urb *urb)
12968c2ecf20Sopenharmony_ci{
12978c2ecf20Sopenharmony_ci	struct usb_serial_port *port = urb->context;
12988c2ecf20Sopenharmony_ci	struct digi_port *priv;
12998c2ecf20Sopenharmony_ci	struct digi_serial *serial_priv;
13008c2ecf20Sopenharmony_ci	int ret;
13018c2ecf20Sopenharmony_ci	int status = urb->status;
13028c2ecf20Sopenharmony_ci
13038c2ecf20Sopenharmony_ci	/* port sanity check, do not resubmit if port is not valid */
13048c2ecf20Sopenharmony_ci	if (port == NULL)
13058c2ecf20Sopenharmony_ci		return;
13068c2ecf20Sopenharmony_ci	priv = usb_get_serial_port_data(port);
13078c2ecf20Sopenharmony_ci	if (priv == NULL) {
13088c2ecf20Sopenharmony_ci		dev_err(&port->dev, "%s: port->private is NULL, status=%d\n",
13098c2ecf20Sopenharmony_ci			__func__, status);
13108c2ecf20Sopenharmony_ci		return;
13118c2ecf20Sopenharmony_ci	}
13128c2ecf20Sopenharmony_ci	if (port->serial == NULL ||
13138c2ecf20Sopenharmony_ci		(serial_priv = usb_get_serial_data(port->serial)) == NULL) {
13148c2ecf20Sopenharmony_ci		dev_err(&port->dev, "%s: serial is bad or serial->private "
13158c2ecf20Sopenharmony_ci			"is NULL, status=%d\n", __func__, status);
13168c2ecf20Sopenharmony_ci		return;
13178c2ecf20Sopenharmony_ci	}
13188c2ecf20Sopenharmony_ci
13198c2ecf20Sopenharmony_ci	/* do not resubmit urb if it has any status error */
13208c2ecf20Sopenharmony_ci	if (status) {
13218c2ecf20Sopenharmony_ci		dev_err(&port->dev,
13228c2ecf20Sopenharmony_ci			"%s: nonzero read bulk status: status=%d, port=%d\n",
13238c2ecf20Sopenharmony_ci			__func__, status, priv->dp_port_num);
13248c2ecf20Sopenharmony_ci		return;
13258c2ecf20Sopenharmony_ci	}
13268c2ecf20Sopenharmony_ci
13278c2ecf20Sopenharmony_ci	/* handle oob or inb callback, do not resubmit if error */
13288c2ecf20Sopenharmony_ci	if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
13298c2ecf20Sopenharmony_ci		if (digi_read_oob_callback(urb) != 0)
13308c2ecf20Sopenharmony_ci			return;
13318c2ecf20Sopenharmony_ci	} else {
13328c2ecf20Sopenharmony_ci		if (digi_read_inb_callback(urb) != 0)
13338c2ecf20Sopenharmony_ci			return;
13348c2ecf20Sopenharmony_ci	}
13358c2ecf20Sopenharmony_ci
13368c2ecf20Sopenharmony_ci	/* continue read */
13378c2ecf20Sopenharmony_ci	ret = usb_submit_urb(urb, GFP_ATOMIC);
13388c2ecf20Sopenharmony_ci	if (ret != 0 && ret != -EPERM) {
13398c2ecf20Sopenharmony_ci		dev_err(&port->dev,
13408c2ecf20Sopenharmony_ci			"%s: failed resubmitting urb, ret=%d, port=%d\n",
13418c2ecf20Sopenharmony_ci			__func__, ret, priv->dp_port_num);
13428c2ecf20Sopenharmony_ci	}
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_ci}
13458c2ecf20Sopenharmony_ci
13468c2ecf20Sopenharmony_ci/*
13478c2ecf20Sopenharmony_ci *  Digi Read INB Callback
13488c2ecf20Sopenharmony_ci *
13498c2ecf20Sopenharmony_ci *  Digi Read INB Callback handles reads on the in band ports, sending
13508c2ecf20Sopenharmony_ci *  the data on to the tty subsystem.  When called we know port and
13518c2ecf20Sopenharmony_ci *  port->private are not NULL and port->serial has been validated.
13528c2ecf20Sopenharmony_ci *  It returns 0 if successful, 1 if successful but the port is
13538c2ecf20Sopenharmony_ci *  throttled, and -1 if the sanity checks failed.
13548c2ecf20Sopenharmony_ci */
13558c2ecf20Sopenharmony_ci
13568c2ecf20Sopenharmony_cistatic int digi_read_inb_callback(struct urb *urb)
13578c2ecf20Sopenharmony_ci{
13588c2ecf20Sopenharmony_ci	struct usb_serial_port *port = urb->context;
13598c2ecf20Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
13608c2ecf20Sopenharmony_ci	unsigned char *buf = urb->transfer_buffer;
13618c2ecf20Sopenharmony_ci	unsigned long flags;
13628c2ecf20Sopenharmony_ci	int opcode;
13638c2ecf20Sopenharmony_ci	int len;
13648c2ecf20Sopenharmony_ci	int port_status;
13658c2ecf20Sopenharmony_ci	unsigned char *data;
13668c2ecf20Sopenharmony_ci	int tty_flag, throttled;
13678c2ecf20Sopenharmony_ci
13688c2ecf20Sopenharmony_ci	/* short/multiple packet check */
13698c2ecf20Sopenharmony_ci	if (urb->actual_length < 2) {
13708c2ecf20Sopenharmony_ci		dev_warn(&port->dev, "short packet received\n");
13718c2ecf20Sopenharmony_ci		return -1;
13728c2ecf20Sopenharmony_ci	}
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_ci	opcode = buf[0];
13758c2ecf20Sopenharmony_ci	len = buf[1];
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_ci	if (urb->actual_length != len + 2) {
13788c2ecf20Sopenharmony_ci		dev_err(&port->dev, "malformed packet received: port=%d, opcode=%d, len=%d, actual_length=%u\n",
13798c2ecf20Sopenharmony_ci			priv->dp_port_num, opcode, len, urb->actual_length);
13808c2ecf20Sopenharmony_ci		return -1;
13818c2ecf20Sopenharmony_ci	}
13828c2ecf20Sopenharmony_ci
13838c2ecf20Sopenharmony_ci	if (opcode == DIGI_CMD_RECEIVE_DATA && len < 1) {
13848c2ecf20Sopenharmony_ci		dev_err(&port->dev, "malformed data packet received\n");
13858c2ecf20Sopenharmony_ci		return -1;
13868c2ecf20Sopenharmony_ci	}
13878c2ecf20Sopenharmony_ci
13888c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->dp_port_lock, flags);
13898c2ecf20Sopenharmony_ci
13908c2ecf20Sopenharmony_ci	/* check for throttle; if set, do not resubmit read urb */
13918c2ecf20Sopenharmony_ci	/* indicate the read chain needs to be restarted on unthrottle */
13928c2ecf20Sopenharmony_ci	throttled = priv->dp_throttled;
13938c2ecf20Sopenharmony_ci	if (throttled)
13948c2ecf20Sopenharmony_ci		priv->dp_throttle_restart = 1;
13958c2ecf20Sopenharmony_ci
13968c2ecf20Sopenharmony_ci	/* receive data */
13978c2ecf20Sopenharmony_ci	if (opcode == DIGI_CMD_RECEIVE_DATA) {
13988c2ecf20Sopenharmony_ci		port_status = buf[2];
13998c2ecf20Sopenharmony_ci		data = &buf[3];
14008c2ecf20Sopenharmony_ci
14018c2ecf20Sopenharmony_ci		/* get flag from port_status */
14028c2ecf20Sopenharmony_ci		tty_flag = 0;
14038c2ecf20Sopenharmony_ci
14048c2ecf20Sopenharmony_ci		/* overrun is special, not associated with a char */
14058c2ecf20Sopenharmony_ci		if (port_status & DIGI_OVERRUN_ERROR)
14068c2ecf20Sopenharmony_ci			tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
14078c2ecf20Sopenharmony_ci
14088c2ecf20Sopenharmony_ci		/* break takes precedence over parity, */
14098c2ecf20Sopenharmony_ci		/* which takes precedence over framing errors */
14108c2ecf20Sopenharmony_ci		if (port_status & DIGI_BREAK_ERROR)
14118c2ecf20Sopenharmony_ci			tty_flag = TTY_BREAK;
14128c2ecf20Sopenharmony_ci		else if (port_status & DIGI_PARITY_ERROR)
14138c2ecf20Sopenharmony_ci			tty_flag = TTY_PARITY;
14148c2ecf20Sopenharmony_ci		else if (port_status & DIGI_FRAMING_ERROR)
14158c2ecf20Sopenharmony_ci			tty_flag = TTY_FRAME;
14168c2ecf20Sopenharmony_ci
14178c2ecf20Sopenharmony_ci		/* data length is len-1 (one byte of len is port_status) */
14188c2ecf20Sopenharmony_ci		--len;
14198c2ecf20Sopenharmony_ci		if (len > 0) {
14208c2ecf20Sopenharmony_ci			tty_insert_flip_string_fixed_flag(&port->port, data,
14218c2ecf20Sopenharmony_ci					tty_flag, len);
14228c2ecf20Sopenharmony_ci			tty_flip_buffer_push(&port->port);
14238c2ecf20Sopenharmony_ci		}
14248c2ecf20Sopenharmony_ci	}
14258c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
14268c2ecf20Sopenharmony_ci
14278c2ecf20Sopenharmony_ci	if (opcode == DIGI_CMD_RECEIVE_DISABLE)
14288c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s: got RECEIVE_DISABLE\n", __func__);
14298c2ecf20Sopenharmony_ci	else if (opcode != DIGI_CMD_RECEIVE_DATA)
14308c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "%s: unknown opcode: %d\n", __func__, opcode);
14318c2ecf20Sopenharmony_ci
14328c2ecf20Sopenharmony_ci	return throttled ? 1 : 0;
14338c2ecf20Sopenharmony_ci
14348c2ecf20Sopenharmony_ci}
14358c2ecf20Sopenharmony_ci
14368c2ecf20Sopenharmony_ci
14378c2ecf20Sopenharmony_ci/*
14388c2ecf20Sopenharmony_ci *  Digi Read OOB Callback
14398c2ecf20Sopenharmony_ci *
14408c2ecf20Sopenharmony_ci *  Digi Read OOB Callback handles reads on the out of band port.
14418c2ecf20Sopenharmony_ci *  When called we know port and port->private are not NULL and
14428c2ecf20Sopenharmony_ci *  the port->serial is valid.  It returns 0 if successful, and
14438c2ecf20Sopenharmony_ci *  -1 if the sanity checks failed.
14448c2ecf20Sopenharmony_ci */
14458c2ecf20Sopenharmony_ci
14468c2ecf20Sopenharmony_cistatic int digi_read_oob_callback(struct urb *urb)
14478c2ecf20Sopenharmony_ci{
14488c2ecf20Sopenharmony_ci
14498c2ecf20Sopenharmony_ci	struct usb_serial_port *port = urb->context;
14508c2ecf20Sopenharmony_ci	struct usb_serial *serial = port->serial;
14518c2ecf20Sopenharmony_ci	struct tty_struct *tty;
14528c2ecf20Sopenharmony_ci	struct digi_port *priv;
14538c2ecf20Sopenharmony_ci	unsigned char *buf = urb->transfer_buffer;
14548c2ecf20Sopenharmony_ci	int opcode, line, status, val;
14558c2ecf20Sopenharmony_ci	unsigned long flags;
14568c2ecf20Sopenharmony_ci	int i;
14578c2ecf20Sopenharmony_ci	unsigned int rts;
14588c2ecf20Sopenharmony_ci
14598c2ecf20Sopenharmony_ci	if (urb->actual_length < 4)
14608c2ecf20Sopenharmony_ci		return -1;
14618c2ecf20Sopenharmony_ci
14628c2ecf20Sopenharmony_ci	/* handle each oob command */
14638c2ecf20Sopenharmony_ci	for (i = 0; i < urb->actual_length - 3; i += 4) {
14648c2ecf20Sopenharmony_ci		opcode = buf[i];
14658c2ecf20Sopenharmony_ci		line = buf[i + 1];
14668c2ecf20Sopenharmony_ci		status = buf[i + 2];
14678c2ecf20Sopenharmony_ci		val = buf[i + 3];
14688c2ecf20Sopenharmony_ci
14698c2ecf20Sopenharmony_ci		dev_dbg(&port->dev, "digi_read_oob_callback: opcode=%d, line=%d, status=%d, val=%d\n",
14708c2ecf20Sopenharmony_ci			opcode, line, status, val);
14718c2ecf20Sopenharmony_ci
14728c2ecf20Sopenharmony_ci		if (status != 0 || line >= serial->type->num_ports)
14738c2ecf20Sopenharmony_ci			continue;
14748c2ecf20Sopenharmony_ci
14758c2ecf20Sopenharmony_ci		port = serial->port[line];
14768c2ecf20Sopenharmony_ci
14778c2ecf20Sopenharmony_ci		priv = usb_get_serial_port_data(port);
14788c2ecf20Sopenharmony_ci		if (priv == NULL)
14798c2ecf20Sopenharmony_ci			return -1;
14808c2ecf20Sopenharmony_ci
14818c2ecf20Sopenharmony_ci		tty = tty_port_tty_get(&port->port);
14828c2ecf20Sopenharmony_ci
14838c2ecf20Sopenharmony_ci		rts = 0;
14848c2ecf20Sopenharmony_ci		if (tty)
14858c2ecf20Sopenharmony_ci			rts = C_CRTSCTS(tty);
14868c2ecf20Sopenharmony_ci
14878c2ecf20Sopenharmony_ci		if (tty && opcode == DIGI_CMD_READ_INPUT_SIGNALS) {
14888c2ecf20Sopenharmony_ci			bool wakeup = false;
14898c2ecf20Sopenharmony_ci
14908c2ecf20Sopenharmony_ci			spin_lock_irqsave(&priv->dp_port_lock, flags);
14918c2ecf20Sopenharmony_ci			/* convert from digi flags to termiox flags */
14928c2ecf20Sopenharmony_ci			if (val & DIGI_READ_INPUT_SIGNALS_CTS) {
14938c2ecf20Sopenharmony_ci				priv->dp_modem_signals |= TIOCM_CTS;
14948c2ecf20Sopenharmony_ci				if (rts)
14958c2ecf20Sopenharmony_ci					wakeup = true;
14968c2ecf20Sopenharmony_ci			} else {
14978c2ecf20Sopenharmony_ci				priv->dp_modem_signals &= ~TIOCM_CTS;
14988c2ecf20Sopenharmony_ci				/* port must be open to use tty struct */
14998c2ecf20Sopenharmony_ci			}
15008c2ecf20Sopenharmony_ci			if (val & DIGI_READ_INPUT_SIGNALS_DSR)
15018c2ecf20Sopenharmony_ci				priv->dp_modem_signals |= TIOCM_DSR;
15028c2ecf20Sopenharmony_ci			else
15038c2ecf20Sopenharmony_ci				priv->dp_modem_signals &= ~TIOCM_DSR;
15048c2ecf20Sopenharmony_ci			if (val & DIGI_READ_INPUT_SIGNALS_RI)
15058c2ecf20Sopenharmony_ci				priv->dp_modem_signals |= TIOCM_RI;
15068c2ecf20Sopenharmony_ci			else
15078c2ecf20Sopenharmony_ci				priv->dp_modem_signals &= ~TIOCM_RI;
15088c2ecf20Sopenharmony_ci			if (val & DIGI_READ_INPUT_SIGNALS_DCD)
15098c2ecf20Sopenharmony_ci				priv->dp_modem_signals |= TIOCM_CD;
15108c2ecf20Sopenharmony_ci			else
15118c2ecf20Sopenharmony_ci				priv->dp_modem_signals &= ~TIOCM_CD;
15128c2ecf20Sopenharmony_ci
15138c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&priv->dp_port_lock, flags);
15148c2ecf20Sopenharmony_ci
15158c2ecf20Sopenharmony_ci			if (wakeup)
15168c2ecf20Sopenharmony_ci				tty_port_tty_wakeup(&port->port);
15178c2ecf20Sopenharmony_ci		} else if (opcode == DIGI_CMD_TRANSMIT_IDLE) {
15188c2ecf20Sopenharmony_ci			spin_lock_irqsave(&priv->dp_port_lock, flags);
15198c2ecf20Sopenharmony_ci			priv->dp_transmit_idle = 1;
15208c2ecf20Sopenharmony_ci			wake_up_interruptible(&priv->dp_transmit_idle_wait);
15218c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&priv->dp_port_lock, flags);
15228c2ecf20Sopenharmony_ci		} else if (opcode == DIGI_CMD_IFLUSH_FIFO) {
15238c2ecf20Sopenharmony_ci			wake_up_interruptible(&priv->dp_flush_wait);
15248c2ecf20Sopenharmony_ci		}
15258c2ecf20Sopenharmony_ci		tty_kref_put(tty);
15268c2ecf20Sopenharmony_ci	}
15278c2ecf20Sopenharmony_ci	return 0;
15288c2ecf20Sopenharmony_ci
15298c2ecf20Sopenharmony_ci}
15308c2ecf20Sopenharmony_ci
15318c2ecf20Sopenharmony_cimodule_usb_serial_driver(serial_drivers, id_table_combined);
15328c2ecf20Sopenharmony_ci
15338c2ecf20Sopenharmony_ciMODULE_AUTHOR(DRIVER_AUTHOR);
15348c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(DRIVER_DESC);
15358c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1536