162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci*  Digi AccelePort USB-4 and USB-2 Serial Converters
462306a36Sopenharmony_ci*
562306a36Sopenharmony_ci*  Copyright 2000 by Digi International
662306a36Sopenharmony_ci*
762306a36Sopenharmony_ci*  Shamelessly based on Brian Warner's keyspan_pda.c and Greg Kroah-Hartman's
862306a36Sopenharmony_ci*  usb-serial driver.
962306a36Sopenharmony_ci*
1062306a36Sopenharmony_ci*  Peter Berger (pberger@brimson.com)
1162306a36Sopenharmony_ci*  Al Borchers (borchers@steinerpoint.com)
1262306a36Sopenharmony_ci*/
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include <linux/kernel.h>
1562306a36Sopenharmony_ci#include <linux/errno.h>
1662306a36Sopenharmony_ci#include <linux/slab.h>
1762306a36Sopenharmony_ci#include <linux/tty.h>
1862306a36Sopenharmony_ci#include <linux/tty_driver.h>
1962306a36Sopenharmony_ci#include <linux/tty_flip.h>
2062306a36Sopenharmony_ci#include <linux/module.h>
2162306a36Sopenharmony_ci#include <linux/spinlock.h>
2262306a36Sopenharmony_ci#include <linux/uaccess.h>
2362306a36Sopenharmony_ci#include <linux/usb.h>
2462306a36Sopenharmony_ci#include <linux/wait.h>
2562306a36Sopenharmony_ci#include <linux/sched/signal.h>
2662306a36Sopenharmony_ci#include <linux/usb/serial.h>
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci/* Defines */
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci#define DRIVER_AUTHOR "Peter Berger <pberger@brimson.com>, Al Borchers <borchers@steinerpoint.com>"
3162306a36Sopenharmony_ci#define DRIVER_DESC "Digi AccelePort USB-2/USB-4 Serial Converter driver"
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci/* port output buffer length -- must be <= transfer buffer length - 2 */
3462306a36Sopenharmony_ci/* so we can be sure to send the full buffer in one urb */
3562306a36Sopenharmony_ci#define DIGI_OUT_BUF_SIZE		8
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci/* port input buffer length -- must be >= transfer buffer length - 3 */
3862306a36Sopenharmony_ci/* so we can be sure to hold at least one full buffer from one urb */
3962306a36Sopenharmony_ci#define DIGI_IN_BUF_SIZE		64
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci/* retry timeout while sleeping */
4262306a36Sopenharmony_ci#define DIGI_RETRY_TIMEOUT		(HZ/10)
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci/* timeout while waiting for tty output to drain in close */
4562306a36Sopenharmony_ci/* this delay is used twice in close, so the total delay could */
4662306a36Sopenharmony_ci/* be twice this value */
4762306a36Sopenharmony_ci#define DIGI_CLOSE_TIMEOUT		(5*HZ)
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci/* AccelePort USB Defines */
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci/* ids */
5362306a36Sopenharmony_ci#define DIGI_VENDOR_ID			0x05c5
5462306a36Sopenharmony_ci#define DIGI_2_ID			0x0002	/* USB-2 */
5562306a36Sopenharmony_ci#define DIGI_4_ID			0x0004	/* USB-4 */
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci/* commands
5862306a36Sopenharmony_ci * "INB": can be used on the in-band endpoint
5962306a36Sopenharmony_ci * "OOB": can be used on the out-of-band endpoint
6062306a36Sopenharmony_ci */
6162306a36Sopenharmony_ci#define DIGI_CMD_SET_BAUD_RATE			0	/* INB, OOB */
6262306a36Sopenharmony_ci#define DIGI_CMD_SET_WORD_SIZE			1	/* INB, OOB */
6362306a36Sopenharmony_ci#define DIGI_CMD_SET_PARITY			2	/* INB, OOB */
6462306a36Sopenharmony_ci#define DIGI_CMD_SET_STOP_BITS			3	/* INB, OOB */
6562306a36Sopenharmony_ci#define DIGI_CMD_SET_INPUT_FLOW_CONTROL		4	/* INB, OOB */
6662306a36Sopenharmony_ci#define DIGI_CMD_SET_OUTPUT_FLOW_CONTROL	5	/* INB, OOB */
6762306a36Sopenharmony_ci#define DIGI_CMD_SET_DTR_SIGNAL			6	/* INB, OOB */
6862306a36Sopenharmony_ci#define DIGI_CMD_SET_RTS_SIGNAL			7	/* INB, OOB */
6962306a36Sopenharmony_ci#define DIGI_CMD_READ_INPUT_SIGNALS		8	/*      OOB */
7062306a36Sopenharmony_ci#define DIGI_CMD_IFLUSH_FIFO			9	/*      OOB */
7162306a36Sopenharmony_ci#define DIGI_CMD_RECEIVE_ENABLE			10	/* INB, OOB */
7262306a36Sopenharmony_ci#define DIGI_CMD_BREAK_CONTROL			11	/* INB, OOB */
7362306a36Sopenharmony_ci#define DIGI_CMD_LOCAL_LOOPBACK			12	/* INB, OOB */
7462306a36Sopenharmony_ci#define DIGI_CMD_TRANSMIT_IDLE			13	/* INB, OOB */
7562306a36Sopenharmony_ci#define DIGI_CMD_READ_UART_REGISTER		14	/*      OOB */
7662306a36Sopenharmony_ci#define DIGI_CMD_WRITE_UART_REGISTER		15	/* INB, OOB */
7762306a36Sopenharmony_ci#define DIGI_CMD_AND_UART_REGISTER		16	/* INB, OOB */
7862306a36Sopenharmony_ci#define DIGI_CMD_OR_UART_REGISTER		17	/* INB, OOB */
7962306a36Sopenharmony_ci#define DIGI_CMD_SEND_DATA			18	/* INB      */
8062306a36Sopenharmony_ci#define DIGI_CMD_RECEIVE_DATA			19	/* INB      */
8162306a36Sopenharmony_ci#define DIGI_CMD_RECEIVE_DISABLE		20	/* INB      */
8262306a36Sopenharmony_ci#define DIGI_CMD_GET_PORT_TYPE			21	/*      OOB */
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci/* baud rates */
8562306a36Sopenharmony_ci#define DIGI_BAUD_50				0
8662306a36Sopenharmony_ci#define DIGI_BAUD_75				1
8762306a36Sopenharmony_ci#define DIGI_BAUD_110				2
8862306a36Sopenharmony_ci#define DIGI_BAUD_150				3
8962306a36Sopenharmony_ci#define DIGI_BAUD_200				4
9062306a36Sopenharmony_ci#define DIGI_BAUD_300				5
9162306a36Sopenharmony_ci#define DIGI_BAUD_600				6
9262306a36Sopenharmony_ci#define DIGI_BAUD_1200				7
9362306a36Sopenharmony_ci#define DIGI_BAUD_1800				8
9462306a36Sopenharmony_ci#define DIGI_BAUD_2400				9
9562306a36Sopenharmony_ci#define DIGI_BAUD_4800				10
9662306a36Sopenharmony_ci#define DIGI_BAUD_7200				11
9762306a36Sopenharmony_ci#define DIGI_BAUD_9600				12
9862306a36Sopenharmony_ci#define DIGI_BAUD_14400				13
9962306a36Sopenharmony_ci#define DIGI_BAUD_19200				14
10062306a36Sopenharmony_ci#define DIGI_BAUD_28800				15
10162306a36Sopenharmony_ci#define DIGI_BAUD_38400				16
10262306a36Sopenharmony_ci#define DIGI_BAUD_57600				17
10362306a36Sopenharmony_ci#define DIGI_BAUD_76800				18
10462306a36Sopenharmony_ci#define DIGI_BAUD_115200			19
10562306a36Sopenharmony_ci#define DIGI_BAUD_153600			20
10662306a36Sopenharmony_ci#define DIGI_BAUD_230400			21
10762306a36Sopenharmony_ci#define DIGI_BAUD_460800			22
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci/* arguments */
11062306a36Sopenharmony_ci#define DIGI_WORD_SIZE_5			0
11162306a36Sopenharmony_ci#define DIGI_WORD_SIZE_6			1
11262306a36Sopenharmony_ci#define DIGI_WORD_SIZE_7			2
11362306a36Sopenharmony_ci#define DIGI_WORD_SIZE_8			3
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci#define DIGI_PARITY_NONE			0
11662306a36Sopenharmony_ci#define DIGI_PARITY_ODD				1
11762306a36Sopenharmony_ci#define DIGI_PARITY_EVEN			2
11862306a36Sopenharmony_ci#define DIGI_PARITY_MARK			3
11962306a36Sopenharmony_ci#define DIGI_PARITY_SPACE			4
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci#define DIGI_STOP_BITS_1			0
12262306a36Sopenharmony_ci#define DIGI_STOP_BITS_2			1
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci#define DIGI_INPUT_FLOW_CONTROL_XON_XOFF	1
12562306a36Sopenharmony_ci#define DIGI_INPUT_FLOW_CONTROL_RTS		2
12662306a36Sopenharmony_ci#define DIGI_INPUT_FLOW_CONTROL_DTR		4
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci#define DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF	1
12962306a36Sopenharmony_ci#define DIGI_OUTPUT_FLOW_CONTROL_CTS		2
13062306a36Sopenharmony_ci#define DIGI_OUTPUT_FLOW_CONTROL_DSR		4
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci#define DIGI_DTR_INACTIVE			0
13362306a36Sopenharmony_ci#define DIGI_DTR_ACTIVE				1
13462306a36Sopenharmony_ci#define DIGI_DTR_INPUT_FLOW_CONTROL		2
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci#define DIGI_RTS_INACTIVE			0
13762306a36Sopenharmony_ci#define DIGI_RTS_ACTIVE				1
13862306a36Sopenharmony_ci#define DIGI_RTS_INPUT_FLOW_CONTROL		2
13962306a36Sopenharmony_ci#define DIGI_RTS_TOGGLE				3
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci#define DIGI_FLUSH_TX				1
14262306a36Sopenharmony_ci#define DIGI_FLUSH_RX				2
14362306a36Sopenharmony_ci#define DIGI_RESUME_TX				4 /* clears xoff condition */
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci#define DIGI_TRANSMIT_NOT_IDLE			0
14662306a36Sopenharmony_ci#define DIGI_TRANSMIT_IDLE			1
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci#define DIGI_DISABLE				0
14962306a36Sopenharmony_ci#define DIGI_ENABLE				1
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci#define DIGI_DEASSERT				0
15262306a36Sopenharmony_ci#define DIGI_ASSERT				1
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci/* in band status codes */
15562306a36Sopenharmony_ci#define DIGI_OVERRUN_ERROR			4
15662306a36Sopenharmony_ci#define DIGI_PARITY_ERROR			8
15762306a36Sopenharmony_ci#define DIGI_FRAMING_ERROR			16
15862306a36Sopenharmony_ci#define DIGI_BREAK_ERROR			32
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci/* out of band status */
16162306a36Sopenharmony_ci#define DIGI_NO_ERROR				0
16262306a36Sopenharmony_ci#define DIGI_BAD_FIRST_PARAMETER		1
16362306a36Sopenharmony_ci#define DIGI_BAD_SECOND_PARAMETER		2
16462306a36Sopenharmony_ci#define DIGI_INVALID_LINE			3
16562306a36Sopenharmony_ci#define DIGI_INVALID_OPCODE			4
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci/* input signals */
16862306a36Sopenharmony_ci#define DIGI_READ_INPUT_SIGNALS_SLOT		1
16962306a36Sopenharmony_ci#define DIGI_READ_INPUT_SIGNALS_ERR		2
17062306a36Sopenharmony_ci#define DIGI_READ_INPUT_SIGNALS_BUSY		4
17162306a36Sopenharmony_ci#define DIGI_READ_INPUT_SIGNALS_PE		8
17262306a36Sopenharmony_ci#define DIGI_READ_INPUT_SIGNALS_CTS		16
17362306a36Sopenharmony_ci#define DIGI_READ_INPUT_SIGNALS_DSR		32
17462306a36Sopenharmony_ci#define DIGI_READ_INPUT_SIGNALS_RI		64
17562306a36Sopenharmony_ci#define DIGI_READ_INPUT_SIGNALS_DCD		128
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci/* Structures */
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_cistruct digi_serial {
18162306a36Sopenharmony_ci	spinlock_t ds_serial_lock;
18262306a36Sopenharmony_ci	struct usb_serial_port *ds_oob_port;	/* out-of-band port */
18362306a36Sopenharmony_ci	int ds_oob_port_num;			/* index of out-of-band port */
18462306a36Sopenharmony_ci	int ds_device_started;
18562306a36Sopenharmony_ci};
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_cistruct digi_port {
18862306a36Sopenharmony_ci	spinlock_t dp_port_lock;
18962306a36Sopenharmony_ci	int dp_port_num;
19062306a36Sopenharmony_ci	int dp_out_buf_len;
19162306a36Sopenharmony_ci	unsigned char dp_out_buf[DIGI_OUT_BUF_SIZE];
19262306a36Sopenharmony_ci	int dp_write_urb_in_use;
19362306a36Sopenharmony_ci	unsigned int dp_modem_signals;
19462306a36Sopenharmony_ci	int dp_transmit_idle;
19562306a36Sopenharmony_ci	wait_queue_head_t dp_transmit_idle_wait;
19662306a36Sopenharmony_ci	int dp_throttled;
19762306a36Sopenharmony_ci	int dp_throttle_restart;
19862306a36Sopenharmony_ci	wait_queue_head_t dp_flush_wait;
19962306a36Sopenharmony_ci	wait_queue_head_t dp_close_wait;	/* wait queue for close */
20062306a36Sopenharmony_ci	wait_queue_head_t write_wait;
20162306a36Sopenharmony_ci	struct usb_serial_port *dp_port;
20262306a36Sopenharmony_ci};
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci/* Local Function Declarations */
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_cistatic int digi_write_oob_command(struct usb_serial_port *port,
20862306a36Sopenharmony_ci	unsigned char *buf, int count, int interruptible);
20962306a36Sopenharmony_cistatic int digi_write_inb_command(struct usb_serial_port *port,
21062306a36Sopenharmony_ci	unsigned char *buf, int count, unsigned long timeout);
21162306a36Sopenharmony_cistatic int digi_set_modem_signals(struct usb_serial_port *port,
21262306a36Sopenharmony_ci	unsigned int modem_signals, int interruptible);
21362306a36Sopenharmony_cistatic int digi_transmit_idle(struct usb_serial_port *port,
21462306a36Sopenharmony_ci	unsigned long timeout);
21562306a36Sopenharmony_cistatic void digi_rx_throttle(struct tty_struct *tty);
21662306a36Sopenharmony_cistatic void digi_rx_unthrottle(struct tty_struct *tty);
21762306a36Sopenharmony_cistatic void digi_set_termios(struct tty_struct *tty,
21862306a36Sopenharmony_ci			     struct usb_serial_port *port,
21962306a36Sopenharmony_ci			     const struct ktermios *old_termios);
22062306a36Sopenharmony_cistatic int digi_break_ctl(struct tty_struct *tty, int break_state);
22162306a36Sopenharmony_cistatic int digi_tiocmget(struct tty_struct *tty);
22262306a36Sopenharmony_cistatic int digi_tiocmset(struct tty_struct *tty, unsigned int set,
22362306a36Sopenharmony_ci		unsigned int clear);
22462306a36Sopenharmony_cistatic int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
22562306a36Sopenharmony_ci		const unsigned char *buf, int count);
22662306a36Sopenharmony_cistatic void digi_write_bulk_callback(struct urb *urb);
22762306a36Sopenharmony_cistatic unsigned int digi_write_room(struct tty_struct *tty);
22862306a36Sopenharmony_cistatic unsigned int digi_chars_in_buffer(struct tty_struct *tty);
22962306a36Sopenharmony_cistatic int digi_open(struct tty_struct *tty, struct usb_serial_port *port);
23062306a36Sopenharmony_cistatic void digi_close(struct usb_serial_port *port);
23162306a36Sopenharmony_cistatic void digi_dtr_rts(struct usb_serial_port *port, int on);
23262306a36Sopenharmony_cistatic int digi_startup_device(struct usb_serial *serial);
23362306a36Sopenharmony_cistatic int digi_startup(struct usb_serial *serial);
23462306a36Sopenharmony_cistatic void digi_disconnect(struct usb_serial *serial);
23562306a36Sopenharmony_cistatic void digi_release(struct usb_serial *serial);
23662306a36Sopenharmony_cistatic int digi_port_probe(struct usb_serial_port *port);
23762306a36Sopenharmony_cistatic void digi_port_remove(struct usb_serial_port *port);
23862306a36Sopenharmony_cistatic void digi_read_bulk_callback(struct urb *urb);
23962306a36Sopenharmony_cistatic int digi_read_inb_callback(struct urb *urb);
24062306a36Sopenharmony_cistatic int digi_read_oob_callback(struct urb *urb);
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_cistatic const struct usb_device_id id_table_combined[] = {
24462306a36Sopenharmony_ci	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
24562306a36Sopenharmony_ci	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
24662306a36Sopenharmony_ci	{ }						/* Terminating entry */
24762306a36Sopenharmony_ci};
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_cistatic const struct usb_device_id id_table_2[] = {
25062306a36Sopenharmony_ci	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
25162306a36Sopenharmony_ci	{ }						/* Terminating entry */
25262306a36Sopenharmony_ci};
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_cistatic const struct usb_device_id id_table_4[] = {
25562306a36Sopenharmony_ci	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
25662306a36Sopenharmony_ci	{ }						/* Terminating entry */
25762306a36Sopenharmony_ci};
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ciMODULE_DEVICE_TABLE(usb, id_table_combined);
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci/* device info needed for the Digi serial converter */
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_cistatic struct usb_serial_driver digi_acceleport_2_device = {
26462306a36Sopenharmony_ci	.driver = {
26562306a36Sopenharmony_ci		.owner =		THIS_MODULE,
26662306a36Sopenharmony_ci		.name =			"digi_2",
26762306a36Sopenharmony_ci	},
26862306a36Sopenharmony_ci	.description =			"Digi 2 port USB adapter",
26962306a36Sopenharmony_ci	.id_table =			id_table_2,
27062306a36Sopenharmony_ci	.num_ports =			3,
27162306a36Sopenharmony_ci	.num_bulk_in =			4,
27262306a36Sopenharmony_ci	.num_bulk_out =			4,
27362306a36Sopenharmony_ci	.open =				digi_open,
27462306a36Sopenharmony_ci	.close =			digi_close,
27562306a36Sopenharmony_ci	.dtr_rts =			digi_dtr_rts,
27662306a36Sopenharmony_ci	.write =			digi_write,
27762306a36Sopenharmony_ci	.write_room =			digi_write_room,
27862306a36Sopenharmony_ci	.write_bulk_callback = 		digi_write_bulk_callback,
27962306a36Sopenharmony_ci	.read_bulk_callback =		digi_read_bulk_callback,
28062306a36Sopenharmony_ci	.chars_in_buffer =		digi_chars_in_buffer,
28162306a36Sopenharmony_ci	.throttle =			digi_rx_throttle,
28262306a36Sopenharmony_ci	.unthrottle =			digi_rx_unthrottle,
28362306a36Sopenharmony_ci	.set_termios =			digi_set_termios,
28462306a36Sopenharmony_ci	.break_ctl =			digi_break_ctl,
28562306a36Sopenharmony_ci	.tiocmget =			digi_tiocmget,
28662306a36Sopenharmony_ci	.tiocmset =			digi_tiocmset,
28762306a36Sopenharmony_ci	.attach =			digi_startup,
28862306a36Sopenharmony_ci	.disconnect =			digi_disconnect,
28962306a36Sopenharmony_ci	.release =			digi_release,
29062306a36Sopenharmony_ci	.port_probe =			digi_port_probe,
29162306a36Sopenharmony_ci	.port_remove =			digi_port_remove,
29262306a36Sopenharmony_ci};
29362306a36Sopenharmony_ci
29462306a36Sopenharmony_cistatic struct usb_serial_driver digi_acceleport_4_device = {
29562306a36Sopenharmony_ci	.driver = {
29662306a36Sopenharmony_ci		.owner =		THIS_MODULE,
29762306a36Sopenharmony_ci		.name =			"digi_4",
29862306a36Sopenharmony_ci	},
29962306a36Sopenharmony_ci	.description =			"Digi 4 port USB adapter",
30062306a36Sopenharmony_ci	.id_table =			id_table_4,
30162306a36Sopenharmony_ci	.num_ports =			4,
30262306a36Sopenharmony_ci	.num_bulk_in =			5,
30362306a36Sopenharmony_ci	.num_bulk_out =			5,
30462306a36Sopenharmony_ci	.open =				digi_open,
30562306a36Sopenharmony_ci	.close =			digi_close,
30662306a36Sopenharmony_ci	.write =			digi_write,
30762306a36Sopenharmony_ci	.write_room =			digi_write_room,
30862306a36Sopenharmony_ci	.write_bulk_callback = 		digi_write_bulk_callback,
30962306a36Sopenharmony_ci	.read_bulk_callback =		digi_read_bulk_callback,
31062306a36Sopenharmony_ci	.chars_in_buffer =		digi_chars_in_buffer,
31162306a36Sopenharmony_ci	.throttle =			digi_rx_throttle,
31262306a36Sopenharmony_ci	.unthrottle =			digi_rx_unthrottle,
31362306a36Sopenharmony_ci	.set_termios =			digi_set_termios,
31462306a36Sopenharmony_ci	.break_ctl =			digi_break_ctl,
31562306a36Sopenharmony_ci	.tiocmget =			digi_tiocmget,
31662306a36Sopenharmony_ci	.tiocmset =			digi_tiocmset,
31762306a36Sopenharmony_ci	.attach =			digi_startup,
31862306a36Sopenharmony_ci	.disconnect =			digi_disconnect,
31962306a36Sopenharmony_ci	.release =			digi_release,
32062306a36Sopenharmony_ci	.port_probe =			digi_port_probe,
32162306a36Sopenharmony_ci	.port_remove =			digi_port_remove,
32262306a36Sopenharmony_ci};
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_cistatic struct usb_serial_driver * const serial_drivers[] = {
32562306a36Sopenharmony_ci	&digi_acceleport_2_device, &digi_acceleport_4_device, NULL
32662306a36Sopenharmony_ci};
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_ci/* Functions */
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_ci/*
33162306a36Sopenharmony_ci *  Cond Wait Interruptible Timeout Irqrestore
33262306a36Sopenharmony_ci *
33362306a36Sopenharmony_ci *  Do spin_unlock_irqrestore and interruptible_sleep_on_timeout
33462306a36Sopenharmony_ci *  so that wake ups are not lost if they occur between the unlock
33562306a36Sopenharmony_ci *  and the sleep.  In other words, spin_unlock_irqrestore and
33662306a36Sopenharmony_ci *  interruptible_sleep_on_timeout are "atomic" with respect to
33762306a36Sopenharmony_ci *  wake ups.  This is used to implement condition variables.
33862306a36Sopenharmony_ci *
33962306a36Sopenharmony_ci *  interruptible_sleep_on_timeout is deprecated and has been replaced
34062306a36Sopenharmony_ci *  with the equivalent code.
34162306a36Sopenharmony_ci */
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_cistatic long cond_wait_interruptible_timeout_irqrestore(
34462306a36Sopenharmony_ci	wait_queue_head_t *q, long timeout,
34562306a36Sopenharmony_ci	spinlock_t *lock, unsigned long flags)
34662306a36Sopenharmony_ci__releases(lock)
34762306a36Sopenharmony_ci{
34862306a36Sopenharmony_ci	DEFINE_WAIT(wait);
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ci	prepare_to_wait(q, &wait, TASK_INTERRUPTIBLE);
35162306a36Sopenharmony_ci	spin_unlock_irqrestore(lock, flags);
35262306a36Sopenharmony_ci	timeout = schedule_timeout(timeout);
35362306a36Sopenharmony_ci	finish_wait(q, &wait);
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci	return timeout;
35662306a36Sopenharmony_ci}
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ci/*
35962306a36Sopenharmony_ci *  Digi Write OOB Command
36062306a36Sopenharmony_ci *
36162306a36Sopenharmony_ci *  Write commands on the out of band port.  Commands are 4
36262306a36Sopenharmony_ci *  bytes each, multiple commands can be sent at once, and
36362306a36Sopenharmony_ci *  no command will be split across USB packets.  Returns 0
36462306a36Sopenharmony_ci *  if successful, -EINTR if interrupted while sleeping and
36562306a36Sopenharmony_ci *  the interruptible flag is true, or a negative error
36662306a36Sopenharmony_ci *  returned by usb_submit_urb.
36762306a36Sopenharmony_ci */
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_cistatic int digi_write_oob_command(struct usb_serial_port *port,
37062306a36Sopenharmony_ci	unsigned char *buf, int count, int interruptible)
37162306a36Sopenharmony_ci{
37262306a36Sopenharmony_ci	int ret = 0;
37362306a36Sopenharmony_ci	int len;
37462306a36Sopenharmony_ci	struct usb_serial_port *oob_port = (struct usb_serial_port *)((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
37562306a36Sopenharmony_ci	struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
37662306a36Sopenharmony_ci	unsigned long flags;
37762306a36Sopenharmony_ci
37862306a36Sopenharmony_ci	dev_dbg(&port->dev,
37962306a36Sopenharmony_ci		"digi_write_oob_command: TOP: port=%d, count=%d\n",
38062306a36Sopenharmony_ci		oob_priv->dp_port_num, count);
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_ci	spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
38362306a36Sopenharmony_ci	while (count > 0) {
38462306a36Sopenharmony_ci		while (oob_priv->dp_write_urb_in_use) {
38562306a36Sopenharmony_ci			cond_wait_interruptible_timeout_irqrestore(
38662306a36Sopenharmony_ci				&oob_priv->write_wait, DIGI_RETRY_TIMEOUT,
38762306a36Sopenharmony_ci				&oob_priv->dp_port_lock, flags);
38862306a36Sopenharmony_ci			if (interruptible && signal_pending(current))
38962306a36Sopenharmony_ci				return -EINTR;
39062306a36Sopenharmony_ci			spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
39162306a36Sopenharmony_ci		}
39262306a36Sopenharmony_ci
39362306a36Sopenharmony_ci		/* len must be a multiple of 4, so commands are not split */
39462306a36Sopenharmony_ci		len = min(count, oob_port->bulk_out_size);
39562306a36Sopenharmony_ci		if (len > 4)
39662306a36Sopenharmony_ci			len &= ~3;
39762306a36Sopenharmony_ci		memcpy(oob_port->write_urb->transfer_buffer, buf, len);
39862306a36Sopenharmony_ci		oob_port->write_urb->transfer_buffer_length = len;
39962306a36Sopenharmony_ci		ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
40062306a36Sopenharmony_ci		if (ret == 0) {
40162306a36Sopenharmony_ci			oob_priv->dp_write_urb_in_use = 1;
40262306a36Sopenharmony_ci			count -= len;
40362306a36Sopenharmony_ci			buf += len;
40462306a36Sopenharmony_ci		}
40562306a36Sopenharmony_ci	}
40662306a36Sopenharmony_ci	spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
40762306a36Sopenharmony_ci	if (ret)
40862306a36Sopenharmony_ci		dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n",
40962306a36Sopenharmony_ci			__func__, ret);
41062306a36Sopenharmony_ci	return ret;
41162306a36Sopenharmony_ci
41262306a36Sopenharmony_ci}
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_ci
41562306a36Sopenharmony_ci/*
41662306a36Sopenharmony_ci *  Digi Write In Band Command
41762306a36Sopenharmony_ci *
41862306a36Sopenharmony_ci *  Write commands on the given port.  Commands are 4
41962306a36Sopenharmony_ci *  bytes each, multiple commands can be sent at once, and
42062306a36Sopenharmony_ci *  no command will be split across USB packets.  If timeout
42162306a36Sopenharmony_ci *  is non-zero, write in band command will return after
42262306a36Sopenharmony_ci *  waiting unsuccessfully for the URB status to clear for
42362306a36Sopenharmony_ci *  timeout ticks.  Returns 0 if successful, or a negative
42462306a36Sopenharmony_ci *  error returned by digi_write.
42562306a36Sopenharmony_ci */
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_cistatic int digi_write_inb_command(struct usb_serial_port *port,
42862306a36Sopenharmony_ci	unsigned char *buf, int count, unsigned long timeout)
42962306a36Sopenharmony_ci{
43062306a36Sopenharmony_ci	int ret = 0;
43162306a36Sopenharmony_ci	int len;
43262306a36Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
43362306a36Sopenharmony_ci	unsigned char *data = port->write_urb->transfer_buffer;
43462306a36Sopenharmony_ci	unsigned long flags;
43562306a36Sopenharmony_ci
43662306a36Sopenharmony_ci	dev_dbg(&port->dev, "digi_write_inb_command: TOP: port=%d, count=%d\n",
43762306a36Sopenharmony_ci		priv->dp_port_num, count);
43862306a36Sopenharmony_ci
43962306a36Sopenharmony_ci	if (timeout)
44062306a36Sopenharmony_ci		timeout += jiffies;
44162306a36Sopenharmony_ci	else
44262306a36Sopenharmony_ci		timeout = ULONG_MAX;
44362306a36Sopenharmony_ci
44462306a36Sopenharmony_ci	spin_lock_irqsave(&priv->dp_port_lock, flags);
44562306a36Sopenharmony_ci	while (count > 0 && ret == 0) {
44662306a36Sopenharmony_ci		while (priv->dp_write_urb_in_use &&
44762306a36Sopenharmony_ci		       time_before(jiffies, timeout)) {
44862306a36Sopenharmony_ci			cond_wait_interruptible_timeout_irqrestore(
44962306a36Sopenharmony_ci				&priv->write_wait, DIGI_RETRY_TIMEOUT,
45062306a36Sopenharmony_ci				&priv->dp_port_lock, flags);
45162306a36Sopenharmony_ci			if (signal_pending(current))
45262306a36Sopenharmony_ci				return -EINTR;
45362306a36Sopenharmony_ci			spin_lock_irqsave(&priv->dp_port_lock, flags);
45462306a36Sopenharmony_ci		}
45562306a36Sopenharmony_ci
45662306a36Sopenharmony_ci		/* len must be a multiple of 4 and small enough to */
45762306a36Sopenharmony_ci		/* guarantee the write will send buffered data first, */
45862306a36Sopenharmony_ci		/* so commands are in order with data and not split */
45962306a36Sopenharmony_ci		len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
46062306a36Sopenharmony_ci		if (len > 4)
46162306a36Sopenharmony_ci			len &= ~3;
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_ci		/* write any buffered data first */
46462306a36Sopenharmony_ci		if (priv->dp_out_buf_len > 0) {
46562306a36Sopenharmony_ci			data[0] = DIGI_CMD_SEND_DATA;
46662306a36Sopenharmony_ci			data[1] = priv->dp_out_buf_len;
46762306a36Sopenharmony_ci			memcpy(data + 2, priv->dp_out_buf,
46862306a36Sopenharmony_ci				priv->dp_out_buf_len);
46962306a36Sopenharmony_ci			memcpy(data + 2 + priv->dp_out_buf_len, buf, len);
47062306a36Sopenharmony_ci			port->write_urb->transfer_buffer_length
47162306a36Sopenharmony_ci				= priv->dp_out_buf_len + 2 + len;
47262306a36Sopenharmony_ci		} else {
47362306a36Sopenharmony_ci			memcpy(data, buf, len);
47462306a36Sopenharmony_ci			port->write_urb->transfer_buffer_length = len;
47562306a36Sopenharmony_ci		}
47662306a36Sopenharmony_ci
47762306a36Sopenharmony_ci		ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
47862306a36Sopenharmony_ci		if (ret == 0) {
47962306a36Sopenharmony_ci			priv->dp_write_urb_in_use = 1;
48062306a36Sopenharmony_ci			priv->dp_out_buf_len = 0;
48162306a36Sopenharmony_ci			count -= len;
48262306a36Sopenharmony_ci			buf += len;
48362306a36Sopenharmony_ci		}
48462306a36Sopenharmony_ci
48562306a36Sopenharmony_ci	}
48662306a36Sopenharmony_ci	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_ci	if (ret)
48962306a36Sopenharmony_ci		dev_err(&port->dev,
49062306a36Sopenharmony_ci			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
49162306a36Sopenharmony_ci			__func__, ret, priv->dp_port_num);
49262306a36Sopenharmony_ci	return ret;
49362306a36Sopenharmony_ci}
49462306a36Sopenharmony_ci
49562306a36Sopenharmony_ci
49662306a36Sopenharmony_ci/*
49762306a36Sopenharmony_ci *  Digi Set Modem Signals
49862306a36Sopenharmony_ci *
49962306a36Sopenharmony_ci *  Sets or clears DTR and RTS on the port, according to the
50062306a36Sopenharmony_ci *  modem_signals argument.  Use TIOCM_DTR and TIOCM_RTS flags
50162306a36Sopenharmony_ci *  for the modem_signals argument.  Returns 0 if successful,
50262306a36Sopenharmony_ci *  -EINTR if interrupted while sleeping, or a non-zero error
50362306a36Sopenharmony_ci *  returned by usb_submit_urb.
50462306a36Sopenharmony_ci */
50562306a36Sopenharmony_ci
50662306a36Sopenharmony_cistatic int digi_set_modem_signals(struct usb_serial_port *port,
50762306a36Sopenharmony_ci	unsigned int modem_signals, int interruptible)
50862306a36Sopenharmony_ci{
50962306a36Sopenharmony_ci
51062306a36Sopenharmony_ci	int ret;
51162306a36Sopenharmony_ci	struct digi_port *port_priv = usb_get_serial_port_data(port);
51262306a36Sopenharmony_ci	struct usb_serial_port *oob_port = (struct usb_serial_port *) ((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
51362306a36Sopenharmony_ci	struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
51462306a36Sopenharmony_ci	unsigned char *data = oob_port->write_urb->transfer_buffer;
51562306a36Sopenharmony_ci	unsigned long flags;
51662306a36Sopenharmony_ci
51762306a36Sopenharmony_ci	dev_dbg(&port->dev,
51862306a36Sopenharmony_ci		"digi_set_modem_signals: TOP: port=%d, modem_signals=0x%x\n",
51962306a36Sopenharmony_ci		port_priv->dp_port_num, modem_signals);
52062306a36Sopenharmony_ci
52162306a36Sopenharmony_ci	spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
52262306a36Sopenharmony_ci	spin_lock(&port_priv->dp_port_lock);
52362306a36Sopenharmony_ci
52462306a36Sopenharmony_ci	while (oob_priv->dp_write_urb_in_use) {
52562306a36Sopenharmony_ci		spin_unlock(&port_priv->dp_port_lock);
52662306a36Sopenharmony_ci		cond_wait_interruptible_timeout_irqrestore(
52762306a36Sopenharmony_ci			&oob_priv->write_wait, DIGI_RETRY_TIMEOUT,
52862306a36Sopenharmony_ci			&oob_priv->dp_port_lock, flags);
52962306a36Sopenharmony_ci		if (interruptible && signal_pending(current))
53062306a36Sopenharmony_ci			return -EINTR;
53162306a36Sopenharmony_ci		spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
53262306a36Sopenharmony_ci		spin_lock(&port_priv->dp_port_lock);
53362306a36Sopenharmony_ci	}
53462306a36Sopenharmony_ci	data[0] = DIGI_CMD_SET_DTR_SIGNAL;
53562306a36Sopenharmony_ci	data[1] = port_priv->dp_port_num;
53662306a36Sopenharmony_ci	data[2] = (modem_signals & TIOCM_DTR) ?
53762306a36Sopenharmony_ci					DIGI_DTR_ACTIVE : DIGI_DTR_INACTIVE;
53862306a36Sopenharmony_ci	data[3] = 0;
53962306a36Sopenharmony_ci	data[4] = DIGI_CMD_SET_RTS_SIGNAL;
54062306a36Sopenharmony_ci	data[5] = port_priv->dp_port_num;
54162306a36Sopenharmony_ci	data[6] = (modem_signals & TIOCM_RTS) ?
54262306a36Sopenharmony_ci					DIGI_RTS_ACTIVE : DIGI_RTS_INACTIVE;
54362306a36Sopenharmony_ci	data[7] = 0;
54462306a36Sopenharmony_ci
54562306a36Sopenharmony_ci	oob_port->write_urb->transfer_buffer_length = 8;
54662306a36Sopenharmony_ci
54762306a36Sopenharmony_ci	ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
54862306a36Sopenharmony_ci	if (ret == 0) {
54962306a36Sopenharmony_ci		oob_priv->dp_write_urb_in_use = 1;
55062306a36Sopenharmony_ci		port_priv->dp_modem_signals &= ~(TIOCM_DTR | TIOCM_RTS);
55162306a36Sopenharmony_ci		port_priv->dp_modem_signals |=
55262306a36Sopenharmony_ci				modem_signals & (TIOCM_DTR | TIOCM_RTS);
55362306a36Sopenharmony_ci	}
55462306a36Sopenharmony_ci	spin_unlock(&port_priv->dp_port_lock);
55562306a36Sopenharmony_ci	spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
55662306a36Sopenharmony_ci	if (ret)
55762306a36Sopenharmony_ci		dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n",
55862306a36Sopenharmony_ci			__func__, ret);
55962306a36Sopenharmony_ci	return ret;
56062306a36Sopenharmony_ci}
56162306a36Sopenharmony_ci
56262306a36Sopenharmony_ci/*
56362306a36Sopenharmony_ci *  Digi Transmit Idle
56462306a36Sopenharmony_ci *
56562306a36Sopenharmony_ci *  Digi transmit idle waits, up to timeout ticks, for the transmitter
56662306a36Sopenharmony_ci *  to go idle.  It returns 0 if successful or a negative error.
56762306a36Sopenharmony_ci *
56862306a36Sopenharmony_ci *  There are race conditions here if more than one process is calling
56962306a36Sopenharmony_ci *  digi_transmit_idle on the same port at the same time.  However, this
57062306a36Sopenharmony_ci *  is only called from close, and only one process can be in close on a
57162306a36Sopenharmony_ci *  port at a time, so its ok.
57262306a36Sopenharmony_ci */
57362306a36Sopenharmony_ci
57462306a36Sopenharmony_cistatic int digi_transmit_idle(struct usb_serial_port *port,
57562306a36Sopenharmony_ci	unsigned long timeout)
57662306a36Sopenharmony_ci{
57762306a36Sopenharmony_ci	int ret;
57862306a36Sopenharmony_ci	unsigned char buf[2];
57962306a36Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
58062306a36Sopenharmony_ci	unsigned long flags;
58162306a36Sopenharmony_ci
58262306a36Sopenharmony_ci	spin_lock_irqsave(&priv->dp_port_lock, flags);
58362306a36Sopenharmony_ci	priv->dp_transmit_idle = 0;
58462306a36Sopenharmony_ci	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
58562306a36Sopenharmony_ci
58662306a36Sopenharmony_ci	buf[0] = DIGI_CMD_TRANSMIT_IDLE;
58762306a36Sopenharmony_ci	buf[1] = 0;
58862306a36Sopenharmony_ci
58962306a36Sopenharmony_ci	timeout += jiffies;
59062306a36Sopenharmony_ci
59162306a36Sopenharmony_ci	ret = digi_write_inb_command(port, buf, 2, timeout - jiffies);
59262306a36Sopenharmony_ci	if (ret != 0)
59362306a36Sopenharmony_ci		return ret;
59462306a36Sopenharmony_ci
59562306a36Sopenharmony_ci	spin_lock_irqsave(&priv->dp_port_lock, flags);
59662306a36Sopenharmony_ci
59762306a36Sopenharmony_ci	while (time_before(jiffies, timeout) && !priv->dp_transmit_idle) {
59862306a36Sopenharmony_ci		cond_wait_interruptible_timeout_irqrestore(
59962306a36Sopenharmony_ci			&priv->dp_transmit_idle_wait, DIGI_RETRY_TIMEOUT,
60062306a36Sopenharmony_ci			&priv->dp_port_lock, flags);
60162306a36Sopenharmony_ci		if (signal_pending(current))
60262306a36Sopenharmony_ci			return -EINTR;
60362306a36Sopenharmony_ci		spin_lock_irqsave(&priv->dp_port_lock, flags);
60462306a36Sopenharmony_ci	}
60562306a36Sopenharmony_ci	priv->dp_transmit_idle = 0;
60662306a36Sopenharmony_ci	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
60762306a36Sopenharmony_ci	return 0;
60862306a36Sopenharmony_ci
60962306a36Sopenharmony_ci}
61062306a36Sopenharmony_ci
61162306a36Sopenharmony_ci
61262306a36Sopenharmony_cistatic void digi_rx_throttle(struct tty_struct *tty)
61362306a36Sopenharmony_ci{
61462306a36Sopenharmony_ci	unsigned long flags;
61562306a36Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
61662306a36Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
61762306a36Sopenharmony_ci
61862306a36Sopenharmony_ci	/* stop receiving characters by not resubmitting the read urb */
61962306a36Sopenharmony_ci	spin_lock_irqsave(&priv->dp_port_lock, flags);
62062306a36Sopenharmony_ci	priv->dp_throttled = 1;
62162306a36Sopenharmony_ci	priv->dp_throttle_restart = 0;
62262306a36Sopenharmony_ci	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
62362306a36Sopenharmony_ci}
62462306a36Sopenharmony_ci
62562306a36Sopenharmony_ci
62662306a36Sopenharmony_cistatic void digi_rx_unthrottle(struct tty_struct *tty)
62762306a36Sopenharmony_ci{
62862306a36Sopenharmony_ci	int ret = 0;
62962306a36Sopenharmony_ci	unsigned long flags;
63062306a36Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
63162306a36Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
63262306a36Sopenharmony_ci
63362306a36Sopenharmony_ci	spin_lock_irqsave(&priv->dp_port_lock, flags);
63462306a36Sopenharmony_ci
63562306a36Sopenharmony_ci	/* restart read chain */
63662306a36Sopenharmony_ci	if (priv->dp_throttle_restart)
63762306a36Sopenharmony_ci		ret = usb_submit_urb(port->read_urb, GFP_ATOMIC);
63862306a36Sopenharmony_ci
63962306a36Sopenharmony_ci	/* turn throttle off */
64062306a36Sopenharmony_ci	priv->dp_throttled = 0;
64162306a36Sopenharmony_ci	priv->dp_throttle_restart = 0;
64262306a36Sopenharmony_ci
64362306a36Sopenharmony_ci	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
64462306a36Sopenharmony_ci
64562306a36Sopenharmony_ci	if (ret)
64662306a36Sopenharmony_ci		dev_err(&port->dev,
64762306a36Sopenharmony_ci			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
64862306a36Sopenharmony_ci			__func__, ret, priv->dp_port_num);
64962306a36Sopenharmony_ci}
65062306a36Sopenharmony_ci
65162306a36Sopenharmony_ci
65262306a36Sopenharmony_cistatic void digi_set_termios(struct tty_struct *tty,
65362306a36Sopenharmony_ci			     struct usb_serial_port *port,
65462306a36Sopenharmony_ci			     const struct ktermios *old_termios)
65562306a36Sopenharmony_ci{
65662306a36Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
65762306a36Sopenharmony_ci	struct device *dev = &port->dev;
65862306a36Sopenharmony_ci	unsigned int iflag = tty->termios.c_iflag;
65962306a36Sopenharmony_ci	unsigned int cflag = tty->termios.c_cflag;
66062306a36Sopenharmony_ci	unsigned int old_iflag = old_termios->c_iflag;
66162306a36Sopenharmony_ci	unsigned int old_cflag = old_termios->c_cflag;
66262306a36Sopenharmony_ci	unsigned char buf[32];
66362306a36Sopenharmony_ci	unsigned int modem_signals;
66462306a36Sopenharmony_ci	int arg, ret;
66562306a36Sopenharmony_ci	int i = 0;
66662306a36Sopenharmony_ci	speed_t baud;
66762306a36Sopenharmony_ci
66862306a36Sopenharmony_ci	dev_dbg(dev,
66962306a36Sopenharmony_ci		"digi_set_termios: TOP: port=%d, iflag=0x%x, old_iflag=0x%x, cflag=0x%x, old_cflag=0x%x\n",
67062306a36Sopenharmony_ci		priv->dp_port_num, iflag, old_iflag, cflag, old_cflag);
67162306a36Sopenharmony_ci
67262306a36Sopenharmony_ci	/* set baud rate */
67362306a36Sopenharmony_ci	baud = tty_get_baud_rate(tty);
67462306a36Sopenharmony_ci	if (baud != tty_termios_baud_rate(old_termios)) {
67562306a36Sopenharmony_ci		arg = -1;
67662306a36Sopenharmony_ci
67762306a36Sopenharmony_ci		/* reassert DTR and (maybe) RTS on transition from B0 */
67862306a36Sopenharmony_ci		if ((old_cflag & CBAUD) == B0) {
67962306a36Sopenharmony_ci			/* don't set RTS if using hardware flow control */
68062306a36Sopenharmony_ci			/* and throttling input */
68162306a36Sopenharmony_ci			modem_signals = TIOCM_DTR;
68262306a36Sopenharmony_ci			if (!C_CRTSCTS(tty) || !tty_throttled(tty))
68362306a36Sopenharmony_ci				modem_signals |= TIOCM_RTS;
68462306a36Sopenharmony_ci			digi_set_modem_signals(port, modem_signals, 1);
68562306a36Sopenharmony_ci		}
68662306a36Sopenharmony_ci		switch (baud) {
68762306a36Sopenharmony_ci		/* drop DTR and RTS on transition to B0 */
68862306a36Sopenharmony_ci		case 0: digi_set_modem_signals(port, 0, 1); break;
68962306a36Sopenharmony_ci		case 50: arg = DIGI_BAUD_50; break;
69062306a36Sopenharmony_ci		case 75: arg = DIGI_BAUD_75; break;
69162306a36Sopenharmony_ci		case 110: arg = DIGI_BAUD_110; break;
69262306a36Sopenharmony_ci		case 150: arg = DIGI_BAUD_150; break;
69362306a36Sopenharmony_ci		case 200: arg = DIGI_BAUD_200; break;
69462306a36Sopenharmony_ci		case 300: arg = DIGI_BAUD_300; break;
69562306a36Sopenharmony_ci		case 600: arg = DIGI_BAUD_600; break;
69662306a36Sopenharmony_ci		case 1200: arg = DIGI_BAUD_1200; break;
69762306a36Sopenharmony_ci		case 1800: arg = DIGI_BAUD_1800; break;
69862306a36Sopenharmony_ci		case 2400: arg = DIGI_BAUD_2400; break;
69962306a36Sopenharmony_ci		case 4800: arg = DIGI_BAUD_4800; break;
70062306a36Sopenharmony_ci		case 9600: arg = DIGI_BAUD_9600; break;
70162306a36Sopenharmony_ci		case 19200: arg = DIGI_BAUD_19200; break;
70262306a36Sopenharmony_ci		case 38400: arg = DIGI_BAUD_38400; break;
70362306a36Sopenharmony_ci		case 57600: arg = DIGI_BAUD_57600; break;
70462306a36Sopenharmony_ci		case 115200: arg = DIGI_BAUD_115200; break;
70562306a36Sopenharmony_ci		case 230400: arg = DIGI_BAUD_230400; break;
70662306a36Sopenharmony_ci		case 460800: arg = DIGI_BAUD_460800; break;
70762306a36Sopenharmony_ci		default:
70862306a36Sopenharmony_ci			arg = DIGI_BAUD_9600;
70962306a36Sopenharmony_ci			baud = 9600;
71062306a36Sopenharmony_ci			break;
71162306a36Sopenharmony_ci		}
71262306a36Sopenharmony_ci		if (arg != -1) {
71362306a36Sopenharmony_ci			buf[i++] = DIGI_CMD_SET_BAUD_RATE;
71462306a36Sopenharmony_ci			buf[i++] = priv->dp_port_num;
71562306a36Sopenharmony_ci			buf[i++] = arg;
71662306a36Sopenharmony_ci			buf[i++] = 0;
71762306a36Sopenharmony_ci		}
71862306a36Sopenharmony_ci	}
71962306a36Sopenharmony_ci	/* set parity */
72062306a36Sopenharmony_ci	tty->termios.c_cflag &= ~CMSPAR;
72162306a36Sopenharmony_ci
72262306a36Sopenharmony_ci	if ((cflag & (PARENB | PARODD)) != (old_cflag & (PARENB | PARODD))) {
72362306a36Sopenharmony_ci		if (cflag & PARENB) {
72462306a36Sopenharmony_ci			if (cflag & PARODD)
72562306a36Sopenharmony_ci				arg = DIGI_PARITY_ODD;
72662306a36Sopenharmony_ci			else
72762306a36Sopenharmony_ci				arg = DIGI_PARITY_EVEN;
72862306a36Sopenharmony_ci		} else {
72962306a36Sopenharmony_ci			arg = DIGI_PARITY_NONE;
73062306a36Sopenharmony_ci		}
73162306a36Sopenharmony_ci		buf[i++] = DIGI_CMD_SET_PARITY;
73262306a36Sopenharmony_ci		buf[i++] = priv->dp_port_num;
73362306a36Sopenharmony_ci		buf[i++] = arg;
73462306a36Sopenharmony_ci		buf[i++] = 0;
73562306a36Sopenharmony_ci	}
73662306a36Sopenharmony_ci	/* set word size */
73762306a36Sopenharmony_ci	if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
73862306a36Sopenharmony_ci		arg = -1;
73962306a36Sopenharmony_ci		switch (cflag & CSIZE) {
74062306a36Sopenharmony_ci		case CS5: arg = DIGI_WORD_SIZE_5; break;
74162306a36Sopenharmony_ci		case CS6: arg = DIGI_WORD_SIZE_6; break;
74262306a36Sopenharmony_ci		case CS7: arg = DIGI_WORD_SIZE_7; break;
74362306a36Sopenharmony_ci		case CS8: arg = DIGI_WORD_SIZE_8; break;
74462306a36Sopenharmony_ci		default:
74562306a36Sopenharmony_ci			dev_dbg(dev,
74662306a36Sopenharmony_ci				"digi_set_termios: can't handle word size %d\n",
74762306a36Sopenharmony_ci				cflag & CSIZE);
74862306a36Sopenharmony_ci			break;
74962306a36Sopenharmony_ci		}
75062306a36Sopenharmony_ci
75162306a36Sopenharmony_ci		if (arg != -1) {
75262306a36Sopenharmony_ci			buf[i++] = DIGI_CMD_SET_WORD_SIZE;
75362306a36Sopenharmony_ci			buf[i++] = priv->dp_port_num;
75462306a36Sopenharmony_ci			buf[i++] = arg;
75562306a36Sopenharmony_ci			buf[i++] = 0;
75662306a36Sopenharmony_ci		}
75762306a36Sopenharmony_ci
75862306a36Sopenharmony_ci	}
75962306a36Sopenharmony_ci
76062306a36Sopenharmony_ci	/* set stop bits */
76162306a36Sopenharmony_ci	if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
76262306a36Sopenharmony_ci
76362306a36Sopenharmony_ci		if ((cflag & CSTOPB))
76462306a36Sopenharmony_ci			arg = DIGI_STOP_BITS_2;
76562306a36Sopenharmony_ci		else
76662306a36Sopenharmony_ci			arg = DIGI_STOP_BITS_1;
76762306a36Sopenharmony_ci
76862306a36Sopenharmony_ci		buf[i++] = DIGI_CMD_SET_STOP_BITS;
76962306a36Sopenharmony_ci		buf[i++] = priv->dp_port_num;
77062306a36Sopenharmony_ci		buf[i++] = arg;
77162306a36Sopenharmony_ci		buf[i++] = 0;
77262306a36Sopenharmony_ci
77362306a36Sopenharmony_ci	}
77462306a36Sopenharmony_ci
77562306a36Sopenharmony_ci	/* set input flow control */
77662306a36Sopenharmony_ci	if ((iflag & IXOFF) != (old_iflag & IXOFF) ||
77762306a36Sopenharmony_ci			(cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
77862306a36Sopenharmony_ci		arg = 0;
77962306a36Sopenharmony_ci		if (iflag & IXOFF)
78062306a36Sopenharmony_ci			arg |= DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
78162306a36Sopenharmony_ci		else
78262306a36Sopenharmony_ci			arg &= ~DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
78362306a36Sopenharmony_ci
78462306a36Sopenharmony_ci		if (cflag & CRTSCTS) {
78562306a36Sopenharmony_ci			arg |= DIGI_INPUT_FLOW_CONTROL_RTS;
78662306a36Sopenharmony_ci
78762306a36Sopenharmony_ci			/* On USB-4 it is necessary to assert RTS prior */
78862306a36Sopenharmony_ci			/* to selecting RTS input flow control.  */
78962306a36Sopenharmony_ci			buf[i++] = DIGI_CMD_SET_RTS_SIGNAL;
79062306a36Sopenharmony_ci			buf[i++] = priv->dp_port_num;
79162306a36Sopenharmony_ci			buf[i++] = DIGI_RTS_ACTIVE;
79262306a36Sopenharmony_ci			buf[i++] = 0;
79362306a36Sopenharmony_ci
79462306a36Sopenharmony_ci		} else {
79562306a36Sopenharmony_ci			arg &= ~DIGI_INPUT_FLOW_CONTROL_RTS;
79662306a36Sopenharmony_ci		}
79762306a36Sopenharmony_ci		buf[i++] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
79862306a36Sopenharmony_ci		buf[i++] = priv->dp_port_num;
79962306a36Sopenharmony_ci		buf[i++] = arg;
80062306a36Sopenharmony_ci		buf[i++] = 0;
80162306a36Sopenharmony_ci	}
80262306a36Sopenharmony_ci
80362306a36Sopenharmony_ci	/* set output flow control */
80462306a36Sopenharmony_ci	if ((iflag & IXON) != (old_iflag & IXON) ||
80562306a36Sopenharmony_ci			(cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
80662306a36Sopenharmony_ci		arg = 0;
80762306a36Sopenharmony_ci		if (iflag & IXON)
80862306a36Sopenharmony_ci			arg |= DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
80962306a36Sopenharmony_ci		else
81062306a36Sopenharmony_ci			arg &= ~DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
81162306a36Sopenharmony_ci
81262306a36Sopenharmony_ci		if (cflag & CRTSCTS)
81362306a36Sopenharmony_ci			arg |= DIGI_OUTPUT_FLOW_CONTROL_CTS;
81462306a36Sopenharmony_ci		else
81562306a36Sopenharmony_ci			arg &= ~DIGI_OUTPUT_FLOW_CONTROL_CTS;
81662306a36Sopenharmony_ci
81762306a36Sopenharmony_ci		buf[i++] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
81862306a36Sopenharmony_ci		buf[i++] = priv->dp_port_num;
81962306a36Sopenharmony_ci		buf[i++] = arg;
82062306a36Sopenharmony_ci		buf[i++] = 0;
82162306a36Sopenharmony_ci	}
82262306a36Sopenharmony_ci
82362306a36Sopenharmony_ci	/* set receive enable/disable */
82462306a36Sopenharmony_ci	if ((cflag & CREAD) != (old_cflag & CREAD)) {
82562306a36Sopenharmony_ci		if (cflag & CREAD)
82662306a36Sopenharmony_ci			arg = DIGI_ENABLE;
82762306a36Sopenharmony_ci		else
82862306a36Sopenharmony_ci			arg = DIGI_DISABLE;
82962306a36Sopenharmony_ci
83062306a36Sopenharmony_ci		buf[i++] = DIGI_CMD_RECEIVE_ENABLE;
83162306a36Sopenharmony_ci		buf[i++] = priv->dp_port_num;
83262306a36Sopenharmony_ci		buf[i++] = arg;
83362306a36Sopenharmony_ci		buf[i++] = 0;
83462306a36Sopenharmony_ci	}
83562306a36Sopenharmony_ci	ret = digi_write_oob_command(port, buf, i, 1);
83662306a36Sopenharmony_ci	if (ret != 0)
83762306a36Sopenharmony_ci		dev_dbg(dev, "digi_set_termios: write oob failed, ret=%d\n", ret);
83862306a36Sopenharmony_ci	tty_encode_baud_rate(tty, baud, baud);
83962306a36Sopenharmony_ci}
84062306a36Sopenharmony_ci
84162306a36Sopenharmony_ci
84262306a36Sopenharmony_cistatic int digi_break_ctl(struct tty_struct *tty, int break_state)
84362306a36Sopenharmony_ci{
84462306a36Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
84562306a36Sopenharmony_ci	unsigned char buf[4];
84662306a36Sopenharmony_ci
84762306a36Sopenharmony_ci	buf[0] = DIGI_CMD_BREAK_CONTROL;
84862306a36Sopenharmony_ci	buf[1] = 2;				/* length */
84962306a36Sopenharmony_ci	buf[2] = break_state ? 1 : 0;
85062306a36Sopenharmony_ci	buf[3] = 0;				/* pad */
85162306a36Sopenharmony_ci
85262306a36Sopenharmony_ci	return digi_write_inb_command(port, buf, 4, 0);
85362306a36Sopenharmony_ci}
85462306a36Sopenharmony_ci
85562306a36Sopenharmony_ci
85662306a36Sopenharmony_cistatic int digi_tiocmget(struct tty_struct *tty)
85762306a36Sopenharmony_ci{
85862306a36Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
85962306a36Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
86062306a36Sopenharmony_ci	unsigned int val;
86162306a36Sopenharmony_ci	unsigned long flags;
86262306a36Sopenharmony_ci
86362306a36Sopenharmony_ci	spin_lock_irqsave(&priv->dp_port_lock, flags);
86462306a36Sopenharmony_ci	val = priv->dp_modem_signals;
86562306a36Sopenharmony_ci	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
86662306a36Sopenharmony_ci	return val;
86762306a36Sopenharmony_ci}
86862306a36Sopenharmony_ci
86962306a36Sopenharmony_ci
87062306a36Sopenharmony_cistatic int digi_tiocmset(struct tty_struct *tty,
87162306a36Sopenharmony_ci					unsigned int set, unsigned int clear)
87262306a36Sopenharmony_ci{
87362306a36Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
87462306a36Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
87562306a36Sopenharmony_ci	unsigned int val;
87662306a36Sopenharmony_ci	unsigned long flags;
87762306a36Sopenharmony_ci
87862306a36Sopenharmony_ci	spin_lock_irqsave(&priv->dp_port_lock, flags);
87962306a36Sopenharmony_ci	val = (priv->dp_modem_signals & ~clear) | set;
88062306a36Sopenharmony_ci	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
88162306a36Sopenharmony_ci	return digi_set_modem_signals(port, val, 1);
88262306a36Sopenharmony_ci}
88362306a36Sopenharmony_ci
88462306a36Sopenharmony_ci
88562306a36Sopenharmony_cistatic int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
88662306a36Sopenharmony_ci					const unsigned char *buf, int count)
88762306a36Sopenharmony_ci{
88862306a36Sopenharmony_ci
88962306a36Sopenharmony_ci	int ret, data_len, new_len;
89062306a36Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
89162306a36Sopenharmony_ci	unsigned char *data = port->write_urb->transfer_buffer;
89262306a36Sopenharmony_ci	unsigned long flags;
89362306a36Sopenharmony_ci
89462306a36Sopenharmony_ci	dev_dbg(&port->dev, "digi_write: TOP: port=%d, count=%d\n",
89562306a36Sopenharmony_ci		priv->dp_port_num, count);
89662306a36Sopenharmony_ci
89762306a36Sopenharmony_ci	/* copy user data (which can sleep) before getting spin lock */
89862306a36Sopenharmony_ci	count = min(count, port->bulk_out_size-2);
89962306a36Sopenharmony_ci	count = min(64, count);
90062306a36Sopenharmony_ci
90162306a36Sopenharmony_ci	/* be sure only one write proceeds at a time */
90262306a36Sopenharmony_ci	/* there are races on the port private buffer */
90362306a36Sopenharmony_ci	spin_lock_irqsave(&priv->dp_port_lock, flags);
90462306a36Sopenharmony_ci
90562306a36Sopenharmony_ci	/* wait for urb status clear to submit another urb */
90662306a36Sopenharmony_ci	if (priv->dp_write_urb_in_use) {
90762306a36Sopenharmony_ci		/* buffer data if count is 1 (probably put_char) if possible */
90862306a36Sopenharmony_ci		if (count == 1 && priv->dp_out_buf_len < DIGI_OUT_BUF_SIZE) {
90962306a36Sopenharmony_ci			priv->dp_out_buf[priv->dp_out_buf_len++] = *buf;
91062306a36Sopenharmony_ci			new_len = 1;
91162306a36Sopenharmony_ci		} else {
91262306a36Sopenharmony_ci			new_len = 0;
91362306a36Sopenharmony_ci		}
91462306a36Sopenharmony_ci		spin_unlock_irqrestore(&priv->dp_port_lock, flags);
91562306a36Sopenharmony_ci		return new_len;
91662306a36Sopenharmony_ci	}
91762306a36Sopenharmony_ci
91862306a36Sopenharmony_ci	/* allow space for any buffered data and for new data, up to */
91962306a36Sopenharmony_ci	/* transfer buffer size - 2 (for command and length bytes) */
92062306a36Sopenharmony_ci	new_len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
92162306a36Sopenharmony_ci	data_len = new_len + priv->dp_out_buf_len;
92262306a36Sopenharmony_ci
92362306a36Sopenharmony_ci	if (data_len == 0) {
92462306a36Sopenharmony_ci		spin_unlock_irqrestore(&priv->dp_port_lock, flags);
92562306a36Sopenharmony_ci		return 0;
92662306a36Sopenharmony_ci	}
92762306a36Sopenharmony_ci
92862306a36Sopenharmony_ci	port->write_urb->transfer_buffer_length = data_len+2;
92962306a36Sopenharmony_ci
93062306a36Sopenharmony_ci	*data++ = DIGI_CMD_SEND_DATA;
93162306a36Sopenharmony_ci	*data++ = data_len;
93262306a36Sopenharmony_ci
93362306a36Sopenharmony_ci	/* copy in buffered data first */
93462306a36Sopenharmony_ci	memcpy(data, priv->dp_out_buf, priv->dp_out_buf_len);
93562306a36Sopenharmony_ci	data += priv->dp_out_buf_len;
93662306a36Sopenharmony_ci
93762306a36Sopenharmony_ci	/* copy in new data */
93862306a36Sopenharmony_ci	memcpy(data, buf, new_len);
93962306a36Sopenharmony_ci
94062306a36Sopenharmony_ci	ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
94162306a36Sopenharmony_ci	if (ret == 0) {
94262306a36Sopenharmony_ci		priv->dp_write_urb_in_use = 1;
94362306a36Sopenharmony_ci		ret = new_len;
94462306a36Sopenharmony_ci		priv->dp_out_buf_len = 0;
94562306a36Sopenharmony_ci	}
94662306a36Sopenharmony_ci
94762306a36Sopenharmony_ci	/* return length of new data written, or error */
94862306a36Sopenharmony_ci	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
94962306a36Sopenharmony_ci	if (ret < 0)
95062306a36Sopenharmony_ci		dev_err_console(port,
95162306a36Sopenharmony_ci			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
95262306a36Sopenharmony_ci			__func__, ret, priv->dp_port_num);
95362306a36Sopenharmony_ci	dev_dbg(&port->dev, "digi_write: returning %d\n", ret);
95462306a36Sopenharmony_ci	return ret;
95562306a36Sopenharmony_ci
95662306a36Sopenharmony_ci}
95762306a36Sopenharmony_ci
95862306a36Sopenharmony_cistatic void digi_write_bulk_callback(struct urb *urb)
95962306a36Sopenharmony_ci{
96062306a36Sopenharmony_ci
96162306a36Sopenharmony_ci	struct usb_serial_port *port = urb->context;
96262306a36Sopenharmony_ci	struct usb_serial *serial;
96362306a36Sopenharmony_ci	struct digi_port *priv;
96462306a36Sopenharmony_ci	struct digi_serial *serial_priv;
96562306a36Sopenharmony_ci	unsigned long flags;
96662306a36Sopenharmony_ci	int ret = 0;
96762306a36Sopenharmony_ci	int status = urb->status;
96862306a36Sopenharmony_ci	bool wakeup;
96962306a36Sopenharmony_ci
97062306a36Sopenharmony_ci	/* port and serial sanity check */
97162306a36Sopenharmony_ci	if (port == NULL || (priv = usb_get_serial_port_data(port)) == NULL) {
97262306a36Sopenharmony_ci		pr_err("%s: port or port->private is NULL, status=%d\n",
97362306a36Sopenharmony_ci			__func__, status);
97462306a36Sopenharmony_ci		return;
97562306a36Sopenharmony_ci	}
97662306a36Sopenharmony_ci	serial = port->serial;
97762306a36Sopenharmony_ci	if (serial == NULL || (serial_priv = usb_get_serial_data(serial)) == NULL) {
97862306a36Sopenharmony_ci		dev_err(&port->dev,
97962306a36Sopenharmony_ci			"%s: serial or serial->private is NULL, status=%d\n",
98062306a36Sopenharmony_ci			__func__, status);
98162306a36Sopenharmony_ci		return;
98262306a36Sopenharmony_ci	}
98362306a36Sopenharmony_ci
98462306a36Sopenharmony_ci	/* handle oob callback */
98562306a36Sopenharmony_ci	if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
98662306a36Sopenharmony_ci		dev_dbg(&port->dev, "digi_write_bulk_callback: oob callback\n");
98762306a36Sopenharmony_ci		spin_lock_irqsave(&priv->dp_port_lock, flags);
98862306a36Sopenharmony_ci		priv->dp_write_urb_in_use = 0;
98962306a36Sopenharmony_ci		wake_up_interruptible(&priv->write_wait);
99062306a36Sopenharmony_ci		spin_unlock_irqrestore(&priv->dp_port_lock, flags);
99162306a36Sopenharmony_ci		return;
99262306a36Sopenharmony_ci	}
99362306a36Sopenharmony_ci
99462306a36Sopenharmony_ci	/* try to send any buffered data on this port */
99562306a36Sopenharmony_ci	wakeup = true;
99662306a36Sopenharmony_ci	spin_lock_irqsave(&priv->dp_port_lock, flags);
99762306a36Sopenharmony_ci	priv->dp_write_urb_in_use = 0;
99862306a36Sopenharmony_ci	if (priv->dp_out_buf_len > 0) {
99962306a36Sopenharmony_ci		*((unsigned char *)(port->write_urb->transfer_buffer))
100062306a36Sopenharmony_ci			= (unsigned char)DIGI_CMD_SEND_DATA;
100162306a36Sopenharmony_ci		*((unsigned char *)(port->write_urb->transfer_buffer) + 1)
100262306a36Sopenharmony_ci			= (unsigned char)priv->dp_out_buf_len;
100362306a36Sopenharmony_ci		port->write_urb->transfer_buffer_length =
100462306a36Sopenharmony_ci						priv->dp_out_buf_len + 2;
100562306a36Sopenharmony_ci		memcpy(port->write_urb->transfer_buffer + 2, priv->dp_out_buf,
100662306a36Sopenharmony_ci			priv->dp_out_buf_len);
100762306a36Sopenharmony_ci		ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
100862306a36Sopenharmony_ci		if (ret == 0) {
100962306a36Sopenharmony_ci			priv->dp_write_urb_in_use = 1;
101062306a36Sopenharmony_ci			priv->dp_out_buf_len = 0;
101162306a36Sopenharmony_ci			wakeup = false;
101262306a36Sopenharmony_ci		}
101362306a36Sopenharmony_ci	}
101462306a36Sopenharmony_ci	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
101562306a36Sopenharmony_ci
101662306a36Sopenharmony_ci	if (ret && ret != -EPERM)
101762306a36Sopenharmony_ci		dev_err_console(port,
101862306a36Sopenharmony_ci			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
101962306a36Sopenharmony_ci			__func__, ret, priv->dp_port_num);
102062306a36Sopenharmony_ci
102162306a36Sopenharmony_ci	if (wakeup)
102262306a36Sopenharmony_ci		tty_port_tty_wakeup(&port->port);
102362306a36Sopenharmony_ci}
102462306a36Sopenharmony_ci
102562306a36Sopenharmony_cistatic unsigned int digi_write_room(struct tty_struct *tty)
102662306a36Sopenharmony_ci{
102762306a36Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
102862306a36Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
102962306a36Sopenharmony_ci	unsigned long flags;
103062306a36Sopenharmony_ci	unsigned int room;
103162306a36Sopenharmony_ci
103262306a36Sopenharmony_ci	spin_lock_irqsave(&priv->dp_port_lock, flags);
103362306a36Sopenharmony_ci
103462306a36Sopenharmony_ci	if (priv->dp_write_urb_in_use)
103562306a36Sopenharmony_ci		room = 0;
103662306a36Sopenharmony_ci	else
103762306a36Sopenharmony_ci		room = port->bulk_out_size - 2 - priv->dp_out_buf_len;
103862306a36Sopenharmony_ci
103962306a36Sopenharmony_ci	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
104062306a36Sopenharmony_ci	dev_dbg(&port->dev, "digi_write_room: port=%d, room=%u\n", priv->dp_port_num, room);
104162306a36Sopenharmony_ci	return room;
104262306a36Sopenharmony_ci
104362306a36Sopenharmony_ci}
104462306a36Sopenharmony_ci
104562306a36Sopenharmony_cistatic unsigned int digi_chars_in_buffer(struct tty_struct *tty)
104662306a36Sopenharmony_ci{
104762306a36Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
104862306a36Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
104962306a36Sopenharmony_ci	unsigned long flags;
105062306a36Sopenharmony_ci	unsigned int chars;
105162306a36Sopenharmony_ci
105262306a36Sopenharmony_ci	spin_lock_irqsave(&priv->dp_port_lock, flags);
105362306a36Sopenharmony_ci	if (priv->dp_write_urb_in_use)
105462306a36Sopenharmony_ci		chars = port->bulk_out_size - 2;
105562306a36Sopenharmony_ci	else
105662306a36Sopenharmony_ci		chars = priv->dp_out_buf_len;
105762306a36Sopenharmony_ci	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
105862306a36Sopenharmony_ci
105962306a36Sopenharmony_ci	dev_dbg(&port->dev, "%s: port=%d, chars=%d\n", __func__,
106062306a36Sopenharmony_ci			priv->dp_port_num, chars);
106162306a36Sopenharmony_ci	return chars;
106262306a36Sopenharmony_ci}
106362306a36Sopenharmony_ci
106462306a36Sopenharmony_cistatic void digi_dtr_rts(struct usb_serial_port *port, int on)
106562306a36Sopenharmony_ci{
106662306a36Sopenharmony_ci	/* Adjust DTR and RTS */
106762306a36Sopenharmony_ci	digi_set_modem_signals(port, on * (TIOCM_DTR | TIOCM_RTS), 1);
106862306a36Sopenharmony_ci}
106962306a36Sopenharmony_ci
107062306a36Sopenharmony_cistatic int digi_open(struct tty_struct *tty, struct usb_serial_port *port)
107162306a36Sopenharmony_ci{
107262306a36Sopenharmony_ci	int ret;
107362306a36Sopenharmony_ci	unsigned char buf[32];
107462306a36Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
107562306a36Sopenharmony_ci	struct ktermios not_termios;
107662306a36Sopenharmony_ci
107762306a36Sopenharmony_ci	/* be sure the device is started up */
107862306a36Sopenharmony_ci	if (digi_startup_device(port->serial) != 0)
107962306a36Sopenharmony_ci		return -ENXIO;
108062306a36Sopenharmony_ci
108162306a36Sopenharmony_ci	/* read modem signals automatically whenever they change */
108262306a36Sopenharmony_ci	buf[0] = DIGI_CMD_READ_INPUT_SIGNALS;
108362306a36Sopenharmony_ci	buf[1] = priv->dp_port_num;
108462306a36Sopenharmony_ci	buf[2] = DIGI_ENABLE;
108562306a36Sopenharmony_ci	buf[3] = 0;
108662306a36Sopenharmony_ci
108762306a36Sopenharmony_ci	/* flush fifos */
108862306a36Sopenharmony_ci	buf[4] = DIGI_CMD_IFLUSH_FIFO;
108962306a36Sopenharmony_ci	buf[5] = priv->dp_port_num;
109062306a36Sopenharmony_ci	buf[6] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
109162306a36Sopenharmony_ci	buf[7] = 0;
109262306a36Sopenharmony_ci
109362306a36Sopenharmony_ci	ret = digi_write_oob_command(port, buf, 8, 1);
109462306a36Sopenharmony_ci	if (ret != 0)
109562306a36Sopenharmony_ci		dev_dbg(&port->dev, "digi_open: write oob failed, ret=%d\n", ret);
109662306a36Sopenharmony_ci
109762306a36Sopenharmony_ci	/* set termios settings */
109862306a36Sopenharmony_ci	if (tty) {
109962306a36Sopenharmony_ci		not_termios.c_cflag = ~tty->termios.c_cflag;
110062306a36Sopenharmony_ci		not_termios.c_iflag = ~tty->termios.c_iflag;
110162306a36Sopenharmony_ci		digi_set_termios(tty, port, &not_termios);
110262306a36Sopenharmony_ci	}
110362306a36Sopenharmony_ci	return 0;
110462306a36Sopenharmony_ci}
110562306a36Sopenharmony_ci
110662306a36Sopenharmony_ci
110762306a36Sopenharmony_cistatic void digi_close(struct usb_serial_port *port)
110862306a36Sopenharmony_ci{
110962306a36Sopenharmony_ci	DEFINE_WAIT(wait);
111062306a36Sopenharmony_ci	int ret;
111162306a36Sopenharmony_ci	unsigned char buf[32];
111262306a36Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
111362306a36Sopenharmony_ci
111462306a36Sopenharmony_ci	mutex_lock(&port->serial->disc_mutex);
111562306a36Sopenharmony_ci	/* if disconnected, just clear flags */
111662306a36Sopenharmony_ci	if (port->serial->disconnected)
111762306a36Sopenharmony_ci		goto exit;
111862306a36Sopenharmony_ci
111962306a36Sopenharmony_ci	/* FIXME: Transmit idle belongs in the wait_unti_sent path */
112062306a36Sopenharmony_ci	digi_transmit_idle(port, DIGI_CLOSE_TIMEOUT);
112162306a36Sopenharmony_ci
112262306a36Sopenharmony_ci	/* disable input flow control */
112362306a36Sopenharmony_ci	buf[0] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
112462306a36Sopenharmony_ci	buf[1] = priv->dp_port_num;
112562306a36Sopenharmony_ci	buf[2] = DIGI_DISABLE;
112662306a36Sopenharmony_ci	buf[3] = 0;
112762306a36Sopenharmony_ci
112862306a36Sopenharmony_ci	/* disable output flow control */
112962306a36Sopenharmony_ci	buf[4] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
113062306a36Sopenharmony_ci	buf[5] = priv->dp_port_num;
113162306a36Sopenharmony_ci	buf[6] = DIGI_DISABLE;
113262306a36Sopenharmony_ci	buf[7] = 0;
113362306a36Sopenharmony_ci
113462306a36Sopenharmony_ci	/* disable reading modem signals automatically */
113562306a36Sopenharmony_ci	buf[8] = DIGI_CMD_READ_INPUT_SIGNALS;
113662306a36Sopenharmony_ci	buf[9] = priv->dp_port_num;
113762306a36Sopenharmony_ci	buf[10] = DIGI_DISABLE;
113862306a36Sopenharmony_ci	buf[11] = 0;
113962306a36Sopenharmony_ci
114062306a36Sopenharmony_ci	/* disable receive */
114162306a36Sopenharmony_ci	buf[12] = DIGI_CMD_RECEIVE_ENABLE;
114262306a36Sopenharmony_ci	buf[13] = priv->dp_port_num;
114362306a36Sopenharmony_ci	buf[14] = DIGI_DISABLE;
114462306a36Sopenharmony_ci	buf[15] = 0;
114562306a36Sopenharmony_ci
114662306a36Sopenharmony_ci	/* flush fifos */
114762306a36Sopenharmony_ci	buf[16] = DIGI_CMD_IFLUSH_FIFO;
114862306a36Sopenharmony_ci	buf[17] = priv->dp_port_num;
114962306a36Sopenharmony_ci	buf[18] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
115062306a36Sopenharmony_ci	buf[19] = 0;
115162306a36Sopenharmony_ci
115262306a36Sopenharmony_ci	ret = digi_write_oob_command(port, buf, 20, 0);
115362306a36Sopenharmony_ci	if (ret != 0)
115462306a36Sopenharmony_ci		dev_dbg(&port->dev, "digi_close: write oob failed, ret=%d\n",
115562306a36Sopenharmony_ci									ret);
115662306a36Sopenharmony_ci	/* wait for final commands on oob port to complete */
115762306a36Sopenharmony_ci	prepare_to_wait(&priv->dp_flush_wait, &wait,
115862306a36Sopenharmony_ci			TASK_INTERRUPTIBLE);
115962306a36Sopenharmony_ci	schedule_timeout(DIGI_CLOSE_TIMEOUT);
116062306a36Sopenharmony_ci	finish_wait(&priv->dp_flush_wait, &wait);
116162306a36Sopenharmony_ci
116262306a36Sopenharmony_ci	/* shutdown any outstanding bulk writes */
116362306a36Sopenharmony_ci	usb_kill_urb(port->write_urb);
116462306a36Sopenharmony_ciexit:
116562306a36Sopenharmony_ci	spin_lock_irq(&priv->dp_port_lock);
116662306a36Sopenharmony_ci	priv->dp_write_urb_in_use = 0;
116762306a36Sopenharmony_ci	wake_up_interruptible(&priv->dp_close_wait);
116862306a36Sopenharmony_ci	spin_unlock_irq(&priv->dp_port_lock);
116962306a36Sopenharmony_ci	mutex_unlock(&port->serial->disc_mutex);
117062306a36Sopenharmony_ci}
117162306a36Sopenharmony_ci
117262306a36Sopenharmony_ci
117362306a36Sopenharmony_ci/*
117462306a36Sopenharmony_ci *  Digi Startup Device
117562306a36Sopenharmony_ci *
117662306a36Sopenharmony_ci *  Starts reads on all ports.  Must be called AFTER startup, with
117762306a36Sopenharmony_ci *  urbs initialized.  Returns 0 if successful, non-zero error otherwise.
117862306a36Sopenharmony_ci */
117962306a36Sopenharmony_ci
118062306a36Sopenharmony_cistatic int digi_startup_device(struct usb_serial *serial)
118162306a36Sopenharmony_ci{
118262306a36Sopenharmony_ci	int i, ret = 0;
118362306a36Sopenharmony_ci	struct digi_serial *serial_priv = usb_get_serial_data(serial);
118462306a36Sopenharmony_ci	struct usb_serial_port *port;
118562306a36Sopenharmony_ci
118662306a36Sopenharmony_ci	/* be sure this happens exactly once */
118762306a36Sopenharmony_ci	spin_lock(&serial_priv->ds_serial_lock);
118862306a36Sopenharmony_ci	if (serial_priv->ds_device_started) {
118962306a36Sopenharmony_ci		spin_unlock(&serial_priv->ds_serial_lock);
119062306a36Sopenharmony_ci		return 0;
119162306a36Sopenharmony_ci	}
119262306a36Sopenharmony_ci	serial_priv->ds_device_started = 1;
119362306a36Sopenharmony_ci	spin_unlock(&serial_priv->ds_serial_lock);
119462306a36Sopenharmony_ci
119562306a36Sopenharmony_ci	/* start reading from each bulk in endpoint for the device */
119662306a36Sopenharmony_ci	/* set USB_DISABLE_SPD flag for write bulk urbs */
119762306a36Sopenharmony_ci	for (i = 0; i < serial->type->num_ports + 1; i++) {
119862306a36Sopenharmony_ci		port = serial->port[i];
119962306a36Sopenharmony_ci		ret = usb_submit_urb(port->read_urb, GFP_KERNEL);
120062306a36Sopenharmony_ci		if (ret != 0) {
120162306a36Sopenharmony_ci			dev_err(&port->dev,
120262306a36Sopenharmony_ci				"%s: usb_submit_urb failed, ret=%d, port=%d\n",
120362306a36Sopenharmony_ci				__func__, ret, i);
120462306a36Sopenharmony_ci			break;
120562306a36Sopenharmony_ci		}
120662306a36Sopenharmony_ci	}
120762306a36Sopenharmony_ci	return ret;
120862306a36Sopenharmony_ci}
120962306a36Sopenharmony_ci
121062306a36Sopenharmony_cistatic int digi_port_init(struct usb_serial_port *port, unsigned port_num)
121162306a36Sopenharmony_ci{
121262306a36Sopenharmony_ci	struct digi_port *priv;
121362306a36Sopenharmony_ci
121462306a36Sopenharmony_ci	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
121562306a36Sopenharmony_ci	if (!priv)
121662306a36Sopenharmony_ci		return -ENOMEM;
121762306a36Sopenharmony_ci
121862306a36Sopenharmony_ci	spin_lock_init(&priv->dp_port_lock);
121962306a36Sopenharmony_ci	priv->dp_port_num = port_num;
122062306a36Sopenharmony_ci	init_waitqueue_head(&priv->dp_transmit_idle_wait);
122162306a36Sopenharmony_ci	init_waitqueue_head(&priv->dp_flush_wait);
122262306a36Sopenharmony_ci	init_waitqueue_head(&priv->dp_close_wait);
122362306a36Sopenharmony_ci	init_waitqueue_head(&priv->write_wait);
122462306a36Sopenharmony_ci	priv->dp_port = port;
122562306a36Sopenharmony_ci
122662306a36Sopenharmony_ci	usb_set_serial_port_data(port, priv);
122762306a36Sopenharmony_ci
122862306a36Sopenharmony_ci	return 0;
122962306a36Sopenharmony_ci}
123062306a36Sopenharmony_ci
123162306a36Sopenharmony_cistatic int digi_startup(struct usb_serial *serial)
123262306a36Sopenharmony_ci{
123362306a36Sopenharmony_ci	struct digi_serial *serial_priv;
123462306a36Sopenharmony_ci	int ret;
123562306a36Sopenharmony_ci
123662306a36Sopenharmony_ci	serial_priv = kzalloc(sizeof(*serial_priv), GFP_KERNEL);
123762306a36Sopenharmony_ci	if (!serial_priv)
123862306a36Sopenharmony_ci		return -ENOMEM;
123962306a36Sopenharmony_ci
124062306a36Sopenharmony_ci	spin_lock_init(&serial_priv->ds_serial_lock);
124162306a36Sopenharmony_ci	serial_priv->ds_oob_port_num = serial->type->num_ports;
124262306a36Sopenharmony_ci	serial_priv->ds_oob_port = serial->port[serial_priv->ds_oob_port_num];
124362306a36Sopenharmony_ci
124462306a36Sopenharmony_ci	ret = digi_port_init(serial_priv->ds_oob_port,
124562306a36Sopenharmony_ci						serial_priv->ds_oob_port_num);
124662306a36Sopenharmony_ci	if (ret) {
124762306a36Sopenharmony_ci		kfree(serial_priv);
124862306a36Sopenharmony_ci		return ret;
124962306a36Sopenharmony_ci	}
125062306a36Sopenharmony_ci
125162306a36Sopenharmony_ci	usb_set_serial_data(serial, serial_priv);
125262306a36Sopenharmony_ci
125362306a36Sopenharmony_ci	return 0;
125462306a36Sopenharmony_ci}
125562306a36Sopenharmony_ci
125662306a36Sopenharmony_ci
125762306a36Sopenharmony_cistatic void digi_disconnect(struct usb_serial *serial)
125862306a36Sopenharmony_ci{
125962306a36Sopenharmony_ci	int i;
126062306a36Sopenharmony_ci
126162306a36Sopenharmony_ci	/* stop reads and writes on all ports */
126262306a36Sopenharmony_ci	for (i = 0; i < serial->type->num_ports + 1; i++) {
126362306a36Sopenharmony_ci		usb_kill_urb(serial->port[i]->read_urb);
126462306a36Sopenharmony_ci		usb_kill_urb(serial->port[i]->write_urb);
126562306a36Sopenharmony_ci	}
126662306a36Sopenharmony_ci}
126762306a36Sopenharmony_ci
126862306a36Sopenharmony_ci
126962306a36Sopenharmony_cistatic void digi_release(struct usb_serial *serial)
127062306a36Sopenharmony_ci{
127162306a36Sopenharmony_ci	struct digi_serial *serial_priv;
127262306a36Sopenharmony_ci	struct digi_port *priv;
127362306a36Sopenharmony_ci
127462306a36Sopenharmony_ci	serial_priv = usb_get_serial_data(serial);
127562306a36Sopenharmony_ci
127662306a36Sopenharmony_ci	priv = usb_get_serial_port_data(serial_priv->ds_oob_port);
127762306a36Sopenharmony_ci	kfree(priv);
127862306a36Sopenharmony_ci
127962306a36Sopenharmony_ci	kfree(serial_priv);
128062306a36Sopenharmony_ci}
128162306a36Sopenharmony_ci
128262306a36Sopenharmony_cistatic int digi_port_probe(struct usb_serial_port *port)
128362306a36Sopenharmony_ci{
128462306a36Sopenharmony_ci	return digi_port_init(port, port->port_number);
128562306a36Sopenharmony_ci}
128662306a36Sopenharmony_ci
128762306a36Sopenharmony_cistatic void digi_port_remove(struct usb_serial_port *port)
128862306a36Sopenharmony_ci{
128962306a36Sopenharmony_ci	struct digi_port *priv;
129062306a36Sopenharmony_ci
129162306a36Sopenharmony_ci	priv = usb_get_serial_port_data(port);
129262306a36Sopenharmony_ci	kfree(priv);
129362306a36Sopenharmony_ci}
129462306a36Sopenharmony_ci
129562306a36Sopenharmony_cistatic void digi_read_bulk_callback(struct urb *urb)
129662306a36Sopenharmony_ci{
129762306a36Sopenharmony_ci	struct usb_serial_port *port = urb->context;
129862306a36Sopenharmony_ci	struct digi_port *priv;
129962306a36Sopenharmony_ci	struct digi_serial *serial_priv;
130062306a36Sopenharmony_ci	int ret;
130162306a36Sopenharmony_ci	int status = urb->status;
130262306a36Sopenharmony_ci
130362306a36Sopenharmony_ci	/* port sanity check, do not resubmit if port is not valid */
130462306a36Sopenharmony_ci	if (port == NULL)
130562306a36Sopenharmony_ci		return;
130662306a36Sopenharmony_ci	priv = usb_get_serial_port_data(port);
130762306a36Sopenharmony_ci	if (priv == NULL) {
130862306a36Sopenharmony_ci		dev_err(&port->dev, "%s: port->private is NULL, status=%d\n",
130962306a36Sopenharmony_ci			__func__, status);
131062306a36Sopenharmony_ci		return;
131162306a36Sopenharmony_ci	}
131262306a36Sopenharmony_ci	if (port->serial == NULL ||
131362306a36Sopenharmony_ci		(serial_priv = usb_get_serial_data(port->serial)) == NULL) {
131462306a36Sopenharmony_ci		dev_err(&port->dev, "%s: serial is bad or serial->private "
131562306a36Sopenharmony_ci			"is NULL, status=%d\n", __func__, status);
131662306a36Sopenharmony_ci		return;
131762306a36Sopenharmony_ci	}
131862306a36Sopenharmony_ci
131962306a36Sopenharmony_ci	/* do not resubmit urb if it has any status error */
132062306a36Sopenharmony_ci	if (status) {
132162306a36Sopenharmony_ci		dev_err(&port->dev,
132262306a36Sopenharmony_ci			"%s: nonzero read bulk status: status=%d, port=%d\n",
132362306a36Sopenharmony_ci			__func__, status, priv->dp_port_num);
132462306a36Sopenharmony_ci		return;
132562306a36Sopenharmony_ci	}
132662306a36Sopenharmony_ci
132762306a36Sopenharmony_ci	/* handle oob or inb callback, do not resubmit if error */
132862306a36Sopenharmony_ci	if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
132962306a36Sopenharmony_ci		if (digi_read_oob_callback(urb) != 0)
133062306a36Sopenharmony_ci			return;
133162306a36Sopenharmony_ci	} else {
133262306a36Sopenharmony_ci		if (digi_read_inb_callback(urb) != 0)
133362306a36Sopenharmony_ci			return;
133462306a36Sopenharmony_ci	}
133562306a36Sopenharmony_ci
133662306a36Sopenharmony_ci	/* continue read */
133762306a36Sopenharmony_ci	ret = usb_submit_urb(urb, GFP_ATOMIC);
133862306a36Sopenharmony_ci	if (ret != 0 && ret != -EPERM) {
133962306a36Sopenharmony_ci		dev_err(&port->dev,
134062306a36Sopenharmony_ci			"%s: failed resubmitting urb, ret=%d, port=%d\n",
134162306a36Sopenharmony_ci			__func__, ret, priv->dp_port_num);
134262306a36Sopenharmony_ci	}
134362306a36Sopenharmony_ci
134462306a36Sopenharmony_ci}
134562306a36Sopenharmony_ci
134662306a36Sopenharmony_ci/*
134762306a36Sopenharmony_ci *  Digi Read INB Callback
134862306a36Sopenharmony_ci *
134962306a36Sopenharmony_ci *  Digi Read INB Callback handles reads on the in band ports, sending
135062306a36Sopenharmony_ci *  the data on to the tty subsystem.  When called we know port and
135162306a36Sopenharmony_ci *  port->private are not NULL and port->serial has been validated.
135262306a36Sopenharmony_ci *  It returns 0 if successful, 1 if successful but the port is
135362306a36Sopenharmony_ci *  throttled, and -1 if the sanity checks failed.
135462306a36Sopenharmony_ci */
135562306a36Sopenharmony_ci
135662306a36Sopenharmony_cistatic int digi_read_inb_callback(struct urb *urb)
135762306a36Sopenharmony_ci{
135862306a36Sopenharmony_ci	struct usb_serial_port *port = urb->context;
135962306a36Sopenharmony_ci	struct digi_port *priv = usb_get_serial_port_data(port);
136062306a36Sopenharmony_ci	unsigned char *buf = urb->transfer_buffer;
136162306a36Sopenharmony_ci	unsigned long flags;
136262306a36Sopenharmony_ci	int opcode;
136362306a36Sopenharmony_ci	int len;
136462306a36Sopenharmony_ci	int port_status;
136562306a36Sopenharmony_ci	unsigned char *data;
136662306a36Sopenharmony_ci	int tty_flag, throttled;
136762306a36Sopenharmony_ci
136862306a36Sopenharmony_ci	/* short/multiple packet check */
136962306a36Sopenharmony_ci	if (urb->actual_length < 2) {
137062306a36Sopenharmony_ci		dev_warn(&port->dev, "short packet received\n");
137162306a36Sopenharmony_ci		return -1;
137262306a36Sopenharmony_ci	}
137362306a36Sopenharmony_ci
137462306a36Sopenharmony_ci	opcode = buf[0];
137562306a36Sopenharmony_ci	len = buf[1];
137662306a36Sopenharmony_ci
137762306a36Sopenharmony_ci	if (urb->actual_length != len + 2) {
137862306a36Sopenharmony_ci		dev_err(&port->dev, "malformed packet received: port=%d, opcode=%d, len=%d, actual_length=%u\n",
137962306a36Sopenharmony_ci			priv->dp_port_num, opcode, len, urb->actual_length);
138062306a36Sopenharmony_ci		return -1;
138162306a36Sopenharmony_ci	}
138262306a36Sopenharmony_ci
138362306a36Sopenharmony_ci	if (opcode == DIGI_CMD_RECEIVE_DATA && len < 1) {
138462306a36Sopenharmony_ci		dev_err(&port->dev, "malformed data packet received\n");
138562306a36Sopenharmony_ci		return -1;
138662306a36Sopenharmony_ci	}
138762306a36Sopenharmony_ci
138862306a36Sopenharmony_ci	spin_lock_irqsave(&priv->dp_port_lock, flags);
138962306a36Sopenharmony_ci
139062306a36Sopenharmony_ci	/* check for throttle; if set, do not resubmit read urb */
139162306a36Sopenharmony_ci	/* indicate the read chain needs to be restarted on unthrottle */
139262306a36Sopenharmony_ci	throttled = priv->dp_throttled;
139362306a36Sopenharmony_ci	if (throttled)
139462306a36Sopenharmony_ci		priv->dp_throttle_restart = 1;
139562306a36Sopenharmony_ci
139662306a36Sopenharmony_ci	/* receive data */
139762306a36Sopenharmony_ci	if (opcode == DIGI_CMD_RECEIVE_DATA) {
139862306a36Sopenharmony_ci		port_status = buf[2];
139962306a36Sopenharmony_ci		data = &buf[3];
140062306a36Sopenharmony_ci
140162306a36Sopenharmony_ci		/* get flag from port_status */
140262306a36Sopenharmony_ci		tty_flag = 0;
140362306a36Sopenharmony_ci
140462306a36Sopenharmony_ci		/* overrun is special, not associated with a char */
140562306a36Sopenharmony_ci		if (port_status & DIGI_OVERRUN_ERROR)
140662306a36Sopenharmony_ci			tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
140762306a36Sopenharmony_ci
140862306a36Sopenharmony_ci		/* break takes precedence over parity, */
140962306a36Sopenharmony_ci		/* which takes precedence over framing errors */
141062306a36Sopenharmony_ci		if (port_status & DIGI_BREAK_ERROR)
141162306a36Sopenharmony_ci			tty_flag = TTY_BREAK;
141262306a36Sopenharmony_ci		else if (port_status & DIGI_PARITY_ERROR)
141362306a36Sopenharmony_ci			tty_flag = TTY_PARITY;
141462306a36Sopenharmony_ci		else if (port_status & DIGI_FRAMING_ERROR)
141562306a36Sopenharmony_ci			tty_flag = TTY_FRAME;
141662306a36Sopenharmony_ci
141762306a36Sopenharmony_ci		/* data length is len-1 (one byte of len is port_status) */
141862306a36Sopenharmony_ci		--len;
141962306a36Sopenharmony_ci		if (len > 0) {
142062306a36Sopenharmony_ci			tty_insert_flip_string_fixed_flag(&port->port, data,
142162306a36Sopenharmony_ci					tty_flag, len);
142262306a36Sopenharmony_ci			tty_flip_buffer_push(&port->port);
142362306a36Sopenharmony_ci		}
142462306a36Sopenharmony_ci	}
142562306a36Sopenharmony_ci	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
142662306a36Sopenharmony_ci
142762306a36Sopenharmony_ci	if (opcode == DIGI_CMD_RECEIVE_DISABLE)
142862306a36Sopenharmony_ci		dev_dbg(&port->dev, "%s: got RECEIVE_DISABLE\n", __func__);
142962306a36Sopenharmony_ci	else if (opcode != DIGI_CMD_RECEIVE_DATA)
143062306a36Sopenharmony_ci		dev_dbg(&port->dev, "%s: unknown opcode: %d\n", __func__, opcode);
143162306a36Sopenharmony_ci
143262306a36Sopenharmony_ci	return throttled ? 1 : 0;
143362306a36Sopenharmony_ci
143462306a36Sopenharmony_ci}
143562306a36Sopenharmony_ci
143662306a36Sopenharmony_ci
143762306a36Sopenharmony_ci/*
143862306a36Sopenharmony_ci *  Digi Read OOB Callback
143962306a36Sopenharmony_ci *
144062306a36Sopenharmony_ci *  Digi Read OOB Callback handles reads on the out of band port.
144162306a36Sopenharmony_ci *  When called we know port and port->private are not NULL and
144262306a36Sopenharmony_ci *  the port->serial is valid.  It returns 0 if successful, and
144362306a36Sopenharmony_ci *  -1 if the sanity checks failed.
144462306a36Sopenharmony_ci */
144562306a36Sopenharmony_ci
144662306a36Sopenharmony_cistatic int digi_read_oob_callback(struct urb *urb)
144762306a36Sopenharmony_ci{
144862306a36Sopenharmony_ci
144962306a36Sopenharmony_ci	struct usb_serial_port *port = urb->context;
145062306a36Sopenharmony_ci	struct usb_serial *serial = port->serial;
145162306a36Sopenharmony_ci	struct tty_struct *tty;
145262306a36Sopenharmony_ci	struct digi_port *priv;
145362306a36Sopenharmony_ci	unsigned char *buf = urb->transfer_buffer;
145462306a36Sopenharmony_ci	int opcode, line, status, val;
145562306a36Sopenharmony_ci	unsigned long flags;
145662306a36Sopenharmony_ci	int i;
145762306a36Sopenharmony_ci	unsigned int rts;
145862306a36Sopenharmony_ci
145962306a36Sopenharmony_ci	if (urb->actual_length < 4)
146062306a36Sopenharmony_ci		return -1;
146162306a36Sopenharmony_ci
146262306a36Sopenharmony_ci	/* handle each oob command */
146362306a36Sopenharmony_ci	for (i = 0; i < urb->actual_length - 3; i += 4) {
146462306a36Sopenharmony_ci		opcode = buf[i];
146562306a36Sopenharmony_ci		line = buf[i + 1];
146662306a36Sopenharmony_ci		status = buf[i + 2];
146762306a36Sopenharmony_ci		val = buf[i + 3];
146862306a36Sopenharmony_ci
146962306a36Sopenharmony_ci		dev_dbg(&port->dev, "digi_read_oob_callback: opcode=%d, line=%d, status=%d, val=%d\n",
147062306a36Sopenharmony_ci			opcode, line, status, val);
147162306a36Sopenharmony_ci
147262306a36Sopenharmony_ci		if (status != 0 || line >= serial->type->num_ports)
147362306a36Sopenharmony_ci			continue;
147462306a36Sopenharmony_ci
147562306a36Sopenharmony_ci		port = serial->port[line];
147662306a36Sopenharmony_ci
147762306a36Sopenharmony_ci		priv = usb_get_serial_port_data(port);
147862306a36Sopenharmony_ci		if (priv == NULL)
147962306a36Sopenharmony_ci			return -1;
148062306a36Sopenharmony_ci
148162306a36Sopenharmony_ci		tty = tty_port_tty_get(&port->port);
148262306a36Sopenharmony_ci
148362306a36Sopenharmony_ci		rts = 0;
148462306a36Sopenharmony_ci		if (tty)
148562306a36Sopenharmony_ci			rts = C_CRTSCTS(tty);
148662306a36Sopenharmony_ci
148762306a36Sopenharmony_ci		if (tty && opcode == DIGI_CMD_READ_INPUT_SIGNALS) {
148862306a36Sopenharmony_ci			bool wakeup = false;
148962306a36Sopenharmony_ci
149062306a36Sopenharmony_ci			spin_lock_irqsave(&priv->dp_port_lock, flags);
149162306a36Sopenharmony_ci			/* convert from digi flags to termiox flags */
149262306a36Sopenharmony_ci			if (val & DIGI_READ_INPUT_SIGNALS_CTS) {
149362306a36Sopenharmony_ci				priv->dp_modem_signals |= TIOCM_CTS;
149462306a36Sopenharmony_ci				if (rts)
149562306a36Sopenharmony_ci					wakeup = true;
149662306a36Sopenharmony_ci			} else {
149762306a36Sopenharmony_ci				priv->dp_modem_signals &= ~TIOCM_CTS;
149862306a36Sopenharmony_ci				/* port must be open to use tty struct */
149962306a36Sopenharmony_ci			}
150062306a36Sopenharmony_ci			if (val & DIGI_READ_INPUT_SIGNALS_DSR)
150162306a36Sopenharmony_ci				priv->dp_modem_signals |= TIOCM_DSR;
150262306a36Sopenharmony_ci			else
150362306a36Sopenharmony_ci				priv->dp_modem_signals &= ~TIOCM_DSR;
150462306a36Sopenharmony_ci			if (val & DIGI_READ_INPUT_SIGNALS_RI)
150562306a36Sopenharmony_ci				priv->dp_modem_signals |= TIOCM_RI;
150662306a36Sopenharmony_ci			else
150762306a36Sopenharmony_ci				priv->dp_modem_signals &= ~TIOCM_RI;
150862306a36Sopenharmony_ci			if (val & DIGI_READ_INPUT_SIGNALS_DCD)
150962306a36Sopenharmony_ci				priv->dp_modem_signals |= TIOCM_CD;
151062306a36Sopenharmony_ci			else
151162306a36Sopenharmony_ci				priv->dp_modem_signals &= ~TIOCM_CD;
151262306a36Sopenharmony_ci
151362306a36Sopenharmony_ci			spin_unlock_irqrestore(&priv->dp_port_lock, flags);
151462306a36Sopenharmony_ci
151562306a36Sopenharmony_ci			if (wakeup)
151662306a36Sopenharmony_ci				tty_port_tty_wakeup(&port->port);
151762306a36Sopenharmony_ci		} else if (opcode == DIGI_CMD_TRANSMIT_IDLE) {
151862306a36Sopenharmony_ci			spin_lock_irqsave(&priv->dp_port_lock, flags);
151962306a36Sopenharmony_ci			priv->dp_transmit_idle = 1;
152062306a36Sopenharmony_ci			wake_up_interruptible(&priv->dp_transmit_idle_wait);
152162306a36Sopenharmony_ci			spin_unlock_irqrestore(&priv->dp_port_lock, flags);
152262306a36Sopenharmony_ci		} else if (opcode == DIGI_CMD_IFLUSH_FIFO) {
152362306a36Sopenharmony_ci			wake_up_interruptible(&priv->dp_flush_wait);
152462306a36Sopenharmony_ci		}
152562306a36Sopenharmony_ci		tty_kref_put(tty);
152662306a36Sopenharmony_ci	}
152762306a36Sopenharmony_ci	return 0;
152862306a36Sopenharmony_ci
152962306a36Sopenharmony_ci}
153062306a36Sopenharmony_ci
153162306a36Sopenharmony_cimodule_usb_serial_driver(serial_drivers, id_table_combined);
153262306a36Sopenharmony_ci
153362306a36Sopenharmony_ciMODULE_AUTHOR(DRIVER_AUTHOR);
153462306a36Sopenharmony_ciMODULE_DESCRIPTION(DRIVER_DESC);
153562306a36Sopenharmony_ciMODULE_LICENSE("GPL");
1536