1 /*
2 *  Digi AccelePort USB-4 and USB-2 Serial Converters
3 *
4 *  Copyright 2000 by Digi International
5 *
6 *  This program is free software; you can redistribute it and/or modify
7 *  it under the terms of the GNU General Public License as published by
8 *  the Free Software Foundation; either version 2 of the License, or
9 *  (at your option) any later version.
10 *
11 *  Shamelessly based on Brian Warner's keyspan_pda.c and Greg Kroah-Hartman's
12 *  usb-serial driver.
13 *
14 *  Peter Berger (pberger@brimson.com)
15 *  Al Borchers (borchers@steinerpoint.com)
16 */
17 
18 #include <linux/kernel.h>
19 #include <linux/errno.h>
20 #include <linux/slab.h>
21 #include <linux/tty.h>
22 #include <linux/tty_driver.h>
23 #include <linux/tty_flip.h>
24 #include <linux/module.h>
25 #include <linux/spinlock.h>
26 #include <linux/workqueue.h>
27 #include <linux/uaccess.h>
28 #include <linux/usb.h>
29 #include <linux/wait.h>
30 #include <linux/sched/signal.h>
31 #include <linux/usb/serial.h>
32 
33 /* Defines */
34 
35 #define DRIVER_AUTHOR "Peter Berger <pberger@brimson.com>, Al Borchers <borchers@steinerpoint.com>"
36 #define DRIVER_DESC "Digi AccelePort USB-2/USB-4 Serial Converter driver"
37 
38 /* port output buffer length -- must be <= transfer buffer length - 2 */
39 /* so we can be sure to send the full buffer in one urb */
40 #define DIGI_OUT_BUF_SIZE		8
41 
42 /* port input buffer length -- must be >= transfer buffer length - 3 */
43 /* so we can be sure to hold at least one full buffer from one urb */
44 #define DIGI_IN_BUF_SIZE		64
45 
46 /* retry timeout while sleeping */
47 #define DIGI_RETRY_TIMEOUT		(HZ/10)
48 
49 /* timeout while waiting for tty output to drain in close */
50 /* this delay is used twice in close, so the total delay could */
51 /* be twice this value */
52 #define DIGI_CLOSE_TIMEOUT		(5*HZ)
53 
54 
55 /* AccelePort USB Defines */
56 
57 /* ids */
58 #define DIGI_VENDOR_ID			0x05c5
59 #define DIGI_2_ID			0x0002	/* USB-2 */
60 #define DIGI_4_ID			0x0004	/* USB-4 */
61 
62 /* commands
63  * "INB": can be used on the in-band endpoint
64  * "OOB": can be used on the out-of-band endpoint
65  */
66 #define DIGI_CMD_SET_BAUD_RATE			0	/* INB, OOB */
67 #define DIGI_CMD_SET_WORD_SIZE			1	/* INB, OOB */
68 #define DIGI_CMD_SET_PARITY			2	/* INB, OOB */
69 #define DIGI_CMD_SET_STOP_BITS			3	/* INB, OOB */
70 #define DIGI_CMD_SET_INPUT_FLOW_CONTROL		4	/* INB, OOB */
71 #define DIGI_CMD_SET_OUTPUT_FLOW_CONTROL	5	/* INB, OOB */
72 #define DIGI_CMD_SET_DTR_SIGNAL			6	/* INB, OOB */
73 #define DIGI_CMD_SET_RTS_SIGNAL			7	/* INB, OOB */
74 #define DIGI_CMD_READ_INPUT_SIGNALS		8	/*      OOB */
75 #define DIGI_CMD_IFLUSH_FIFO			9	/*      OOB */
76 #define DIGI_CMD_RECEIVE_ENABLE			10	/* INB, OOB */
77 #define DIGI_CMD_BREAK_CONTROL			11	/* INB, OOB */
78 #define DIGI_CMD_LOCAL_LOOPBACK			12	/* INB, OOB */
79 #define DIGI_CMD_TRANSMIT_IDLE			13	/* INB, OOB */
80 #define DIGI_CMD_READ_UART_REGISTER		14	/*      OOB */
81 #define DIGI_CMD_WRITE_UART_REGISTER		15	/* INB, OOB */
82 #define DIGI_CMD_AND_UART_REGISTER		16	/* INB, OOB */
83 #define DIGI_CMD_OR_UART_REGISTER		17	/* INB, OOB */
84 #define DIGI_CMD_SEND_DATA			18	/* INB      */
85 #define DIGI_CMD_RECEIVE_DATA			19	/* INB      */
86 #define DIGI_CMD_RECEIVE_DISABLE		20	/* INB      */
87 #define DIGI_CMD_GET_PORT_TYPE			21	/*      OOB */
88 
89 /* baud rates */
90 #define DIGI_BAUD_50				0
91 #define DIGI_BAUD_75				1
92 #define DIGI_BAUD_110				2
93 #define DIGI_BAUD_150				3
94 #define DIGI_BAUD_200				4
95 #define DIGI_BAUD_300				5
96 #define DIGI_BAUD_600				6
97 #define DIGI_BAUD_1200				7
98 #define DIGI_BAUD_1800				8
99 #define DIGI_BAUD_2400				9
100 #define DIGI_BAUD_4800				10
101 #define DIGI_BAUD_7200				11
102 #define DIGI_BAUD_9600				12
103 #define DIGI_BAUD_14400				13
104 #define DIGI_BAUD_19200				14
105 #define DIGI_BAUD_28800				15
106 #define DIGI_BAUD_38400				16
107 #define DIGI_BAUD_57600				17
108 #define DIGI_BAUD_76800				18
109 #define DIGI_BAUD_115200			19
110 #define DIGI_BAUD_153600			20
111 #define DIGI_BAUD_230400			21
112 #define DIGI_BAUD_460800			22
113 
114 /* arguments */
115 #define DIGI_WORD_SIZE_5			0
116 #define DIGI_WORD_SIZE_6			1
117 #define DIGI_WORD_SIZE_7			2
118 #define DIGI_WORD_SIZE_8			3
119 
120 #define DIGI_PARITY_NONE			0
121 #define DIGI_PARITY_ODD				1
122 #define DIGI_PARITY_EVEN			2
123 #define DIGI_PARITY_MARK			3
124 #define DIGI_PARITY_SPACE			4
125 
126 #define DIGI_STOP_BITS_1			0
127 #define DIGI_STOP_BITS_2			1
128 
129 #define DIGI_INPUT_FLOW_CONTROL_XON_XOFF	1
130 #define DIGI_INPUT_FLOW_CONTROL_RTS		2
131 #define DIGI_INPUT_FLOW_CONTROL_DTR		4
132 
133 #define DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF	1
134 #define DIGI_OUTPUT_FLOW_CONTROL_CTS		2
135 #define DIGI_OUTPUT_FLOW_CONTROL_DSR		4
136 
137 #define DIGI_DTR_INACTIVE			0
138 #define DIGI_DTR_ACTIVE				1
139 #define DIGI_DTR_INPUT_FLOW_CONTROL		2
140 
141 #define DIGI_RTS_INACTIVE			0
142 #define DIGI_RTS_ACTIVE				1
143 #define DIGI_RTS_INPUT_FLOW_CONTROL		2
144 #define DIGI_RTS_TOGGLE				3
145 
146 #define DIGI_FLUSH_TX				1
147 #define DIGI_FLUSH_RX				2
148 #define DIGI_RESUME_TX				4 /* clears xoff condition */
149 
150 #define DIGI_TRANSMIT_NOT_IDLE			0
151 #define DIGI_TRANSMIT_IDLE			1
152 
153 #define DIGI_DISABLE				0
154 #define DIGI_ENABLE				1
155 
156 #define DIGI_DEASSERT				0
157 #define DIGI_ASSERT				1
158 
159 /* in band status codes */
160 #define DIGI_OVERRUN_ERROR			4
161 #define DIGI_PARITY_ERROR			8
162 #define DIGI_FRAMING_ERROR			16
163 #define DIGI_BREAK_ERROR			32
164 
165 /* out of band status */
166 #define DIGI_NO_ERROR				0
167 #define DIGI_BAD_FIRST_PARAMETER		1
168 #define DIGI_BAD_SECOND_PARAMETER		2
169 #define DIGI_INVALID_LINE			3
170 #define DIGI_INVALID_OPCODE			4
171 
172 /* input signals */
173 #define DIGI_READ_INPUT_SIGNALS_SLOT		1
174 #define DIGI_READ_INPUT_SIGNALS_ERR		2
175 #define DIGI_READ_INPUT_SIGNALS_BUSY		4
176 #define DIGI_READ_INPUT_SIGNALS_PE		8
177 #define DIGI_READ_INPUT_SIGNALS_CTS		16
178 #define DIGI_READ_INPUT_SIGNALS_DSR		32
179 #define DIGI_READ_INPUT_SIGNALS_RI		64
180 #define DIGI_READ_INPUT_SIGNALS_DCD		128
181 
182 
183 /* Structures */
184 
185 struct digi_serial {
186 	spinlock_t ds_serial_lock;
187 	struct usb_serial_port *ds_oob_port;	/* out-of-band port */
188 	int ds_oob_port_num;			/* index of out-of-band port */
189 	int ds_device_started;
190 };
191 
192 struct digi_port {
193 	spinlock_t dp_port_lock;
194 	int dp_port_num;
195 	int dp_out_buf_len;
196 	unsigned char dp_out_buf[DIGI_OUT_BUF_SIZE];
197 	int dp_write_urb_in_use;
198 	unsigned int dp_modem_signals;
199 	int dp_transmit_idle;
200 	wait_queue_head_t dp_transmit_idle_wait;
201 	int dp_throttled;
202 	int dp_throttle_restart;
203 	wait_queue_head_t dp_flush_wait;
204 	wait_queue_head_t dp_close_wait;	/* wait queue for close */
205 	struct work_struct dp_wakeup_work;
206 	struct usb_serial_port *dp_port;
207 };
208 
209 
210 /* Local Function Declarations */
211 
212 static void digi_wakeup_write_lock(struct work_struct *work);
213 static int digi_write_oob_command(struct usb_serial_port *port,
214 	unsigned char *buf, int count, int interruptible);
215 static int digi_write_inb_command(struct usb_serial_port *port,
216 	unsigned char *buf, int count, unsigned long timeout);
217 static int digi_set_modem_signals(struct usb_serial_port *port,
218 	unsigned int modem_signals, int interruptible);
219 static int digi_transmit_idle(struct usb_serial_port *port,
220 	unsigned long timeout);
221 static void digi_rx_throttle(struct tty_struct *tty);
222 static void digi_rx_unthrottle(struct tty_struct *tty);
223 static void digi_set_termios(struct tty_struct *tty,
224 		struct usb_serial_port *port, struct ktermios *old_termios);
225 static void digi_break_ctl(struct tty_struct *tty, int break_state);
226 static int digi_tiocmget(struct tty_struct *tty);
227 static int digi_tiocmset(struct tty_struct *tty, unsigned int set,
228 		unsigned int clear);
229 static int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
230 		const unsigned char *buf, int count);
231 static void digi_write_bulk_callback(struct urb *urb);
232 static int digi_write_room(struct tty_struct *tty);
233 static int digi_chars_in_buffer(struct tty_struct *tty);
234 static int digi_open(struct tty_struct *tty, struct usb_serial_port *port);
235 static void digi_close(struct usb_serial_port *port);
236 static void digi_dtr_rts(struct usb_serial_port *port, int on);
237 static int digi_startup_device(struct usb_serial *serial);
238 static int digi_startup(struct usb_serial *serial);
239 static void digi_disconnect(struct usb_serial *serial);
240 static void digi_release(struct usb_serial *serial);
241 static int digi_port_probe(struct usb_serial_port *port);
242 static int digi_port_remove(struct usb_serial_port *port);
243 static void digi_read_bulk_callback(struct urb *urb);
244 static int digi_read_inb_callback(struct urb *urb);
245 static int digi_read_oob_callback(struct urb *urb);
246 
247 
248 static const struct usb_device_id id_table_combined[] = {
249 	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
250 	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
251 	{ }						/* Terminating entry */
252 };
253 
254 static const struct usb_device_id id_table_2[] = {
255 	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
256 	{ }						/* Terminating entry */
257 };
258 
259 static const struct usb_device_id id_table_4[] = {
260 	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
261 	{ }						/* Terminating entry */
262 };
263 
264 MODULE_DEVICE_TABLE(usb, id_table_combined);
265 
266 /* device info needed for the Digi serial converter */
267 
268 static struct usb_serial_driver digi_acceleport_2_device = {
269 	.driver = {
270 		.owner =		THIS_MODULE,
271 		.name =			"digi_2",
272 	},
273 	.description =			"Digi 2 port USB adapter",
274 	.id_table =			id_table_2,
275 	.num_ports =			3,
276 	.num_bulk_in =			4,
277 	.num_bulk_out =			4,
278 	.open =				digi_open,
279 	.close =			digi_close,
280 	.dtr_rts =			digi_dtr_rts,
281 	.write =			digi_write,
282 	.write_room =			digi_write_room,
283 	.write_bulk_callback = 		digi_write_bulk_callback,
284 	.read_bulk_callback =		digi_read_bulk_callback,
285 	.chars_in_buffer =		digi_chars_in_buffer,
286 	.throttle =			digi_rx_throttle,
287 	.unthrottle =			digi_rx_unthrottle,
288 	.set_termios =			digi_set_termios,
289 	.break_ctl =			digi_break_ctl,
290 	.tiocmget =			digi_tiocmget,
291 	.tiocmset =			digi_tiocmset,
292 	.attach =			digi_startup,
293 	.disconnect =			digi_disconnect,
294 	.release =			digi_release,
295 	.port_probe =			digi_port_probe,
296 	.port_remove =			digi_port_remove,
297 };
298 
299 static struct usb_serial_driver digi_acceleport_4_device = {
300 	.driver = {
301 		.owner =		THIS_MODULE,
302 		.name =			"digi_4",
303 	},
304 	.description =			"Digi 4 port USB adapter",
305 	.id_table =			id_table_4,
306 	.num_ports =			4,
307 	.num_bulk_in =			5,
308 	.num_bulk_out =			5,
309 	.open =				digi_open,
310 	.close =			digi_close,
311 	.write =			digi_write,
312 	.write_room =			digi_write_room,
313 	.write_bulk_callback = 		digi_write_bulk_callback,
314 	.read_bulk_callback =		digi_read_bulk_callback,
315 	.chars_in_buffer =		digi_chars_in_buffer,
316 	.throttle =			digi_rx_throttle,
317 	.unthrottle =			digi_rx_unthrottle,
318 	.set_termios =			digi_set_termios,
319 	.break_ctl =			digi_break_ctl,
320 	.tiocmget =			digi_tiocmget,
321 	.tiocmset =			digi_tiocmset,
322 	.attach =			digi_startup,
323 	.disconnect =			digi_disconnect,
324 	.release =			digi_release,
325 	.port_probe =			digi_port_probe,
326 	.port_remove =			digi_port_remove,
327 };
328 
329 static struct usb_serial_driver * const serial_drivers[] = {
330 	&digi_acceleport_2_device, &digi_acceleport_4_device, NULL
331 };
332 
333 /* Functions */
334 
335 /*
336  *  Cond Wait Interruptible Timeout Irqrestore
337  *
338  *  Do spin_unlock_irqrestore and interruptible_sleep_on_timeout
339  *  so that wake ups are not lost if they occur between the unlock
340  *  and the sleep.  In other words, spin_unlock_irqrestore and
341  *  interruptible_sleep_on_timeout are "atomic" with respect to
342  *  wake ups.  This is used to implement condition variables.
343  *
344  *  interruptible_sleep_on_timeout is deprecated and has been replaced
345  *  with the equivalent code.
346  */
347 
348 static long cond_wait_interruptible_timeout_irqrestore(
349 	wait_queue_head_t *q, long timeout,
350 	spinlock_t *lock, unsigned long flags)
351 __releases(lock)
352 {
353 	DEFINE_WAIT(wait);
354 
355 	prepare_to_wait(q, &wait, TASK_INTERRUPTIBLE);
356 	spin_unlock_irqrestore(lock, flags);
357 	timeout = schedule_timeout(timeout);
358 	finish_wait(q, &wait);
359 
360 	return timeout;
361 }
362 
363 
364 /*
365  *  Digi Wakeup Write
366  *
367  *  Wake up port, line discipline, and tty processes sleeping
368  *  on writes.
369  */
370 
371 static void digi_wakeup_write_lock(struct work_struct *work)
372 {
373 	struct digi_port *priv =
374 			container_of(work, struct digi_port, dp_wakeup_work);
375 	struct usb_serial_port *port = priv->dp_port;
376 	unsigned long flags;
377 
378 	spin_lock_irqsave(&priv->dp_port_lock, flags);
379 	tty_port_tty_wakeup(&port->port);
380 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
381 }
382 
383 /*
384  *  Digi Write OOB Command
385  *
386  *  Write commands on the out of band port.  Commands are 4
387  *  bytes each, multiple commands can be sent at once, and
388  *  no command will be split across USB packets.  Returns 0
389  *  if successful, -EINTR if interrupted while sleeping and
390  *  the interruptible flag is true, or a negative error
391  *  returned by usb_submit_urb.
392  */
393 
394 static int digi_write_oob_command(struct usb_serial_port *port,
395 	unsigned char *buf, int count, int interruptible)
396 {
397 	int ret = 0;
398 	int len;
399 	struct usb_serial_port *oob_port = (struct usb_serial_port *)((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
400 	struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
401 	unsigned long flags = 0;
402 
403 	dev_dbg(&port->dev,
404 		"digi_write_oob_command: TOP: port=%d, count=%d\n",
405 		oob_priv->dp_port_num, count);
406 
407 	spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
408 	while (count > 0) {
409 		while (oob_priv->dp_write_urb_in_use) {
410 			cond_wait_interruptible_timeout_irqrestore(
411 				&oob_port->write_wait, DIGI_RETRY_TIMEOUT,
412 				&oob_priv->dp_port_lock, flags);
413 			if (interruptible && signal_pending(current))
414 				return -EINTR;
415 			spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
416 		}
417 
418 		/* len must be a multiple of 4, so commands are not split */
419 		len = min(count, oob_port->bulk_out_size);
420 		if (len > 4)
421 			len &= ~3;
422 		memcpy(oob_port->write_urb->transfer_buffer, buf, len);
423 		oob_port->write_urb->transfer_buffer_length = len;
424 		ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
425 		if (ret == 0) {
426 			oob_priv->dp_write_urb_in_use = 1;
427 			count -= len;
428 			buf += len;
429 		}
430 	}
431 	spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
432 	if (ret)
433 		dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n",
434 			__func__, ret);
435 	return ret;
436 
437 }
438 
439 
440 /*
441  *  Digi Write In Band Command
442  *
443  *  Write commands on the given port.  Commands are 4
444  *  bytes each, multiple commands can be sent at once, and
445  *  no command will be split across USB packets.  If timeout
446  *  is non-zero, write in band command will return after
447  *  waiting unsuccessfully for the URB status to clear for
448  *  timeout ticks.  Returns 0 if successful, or a negative
449  *  error returned by digi_write.
450  */
451 
452 static int digi_write_inb_command(struct usb_serial_port *port,
453 	unsigned char *buf, int count, unsigned long timeout)
454 {
455 	int ret = 0;
456 	int len;
457 	struct digi_port *priv = usb_get_serial_port_data(port);
458 	unsigned char *data = port->write_urb->transfer_buffer;
459 	unsigned long flags = 0;
460 
461 	dev_dbg(&port->dev, "digi_write_inb_command: TOP: port=%d, count=%d\n",
462 		priv->dp_port_num, count);
463 
464 	if (timeout)
465 		timeout += jiffies;
466 	else
467 		timeout = ULONG_MAX;
468 
469 	spin_lock_irqsave(&priv->dp_port_lock, flags);
470 	while (count > 0 && ret == 0) {
471 		while (priv->dp_write_urb_in_use &&
472 		       time_before(jiffies, timeout)) {
473 			cond_wait_interruptible_timeout_irqrestore(
474 				&port->write_wait, DIGI_RETRY_TIMEOUT,
475 				&priv->dp_port_lock, flags);
476 			if (signal_pending(current))
477 				return -EINTR;
478 			spin_lock_irqsave(&priv->dp_port_lock, flags);
479 		}
480 
481 		/* len must be a multiple of 4 and small enough to */
482 		/* guarantee the write will send buffered data first, */
483 		/* so commands are in order with data and not split */
484 		len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
485 		if (len > 4)
486 			len &= ~3;
487 
488 		/* write any buffered data first */
489 		if (priv->dp_out_buf_len > 0) {
490 			data[0] = DIGI_CMD_SEND_DATA;
491 			data[1] = priv->dp_out_buf_len;
492 			memcpy(data + 2, priv->dp_out_buf,
493 				priv->dp_out_buf_len);
494 			memcpy(data + 2 + priv->dp_out_buf_len, buf, len);
495 			port->write_urb->transfer_buffer_length
496 				= priv->dp_out_buf_len + 2 + len;
497 		} else {
498 			memcpy(data, buf, len);
499 			port->write_urb->transfer_buffer_length = len;
500 		}
501 
502 		ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
503 		if (ret == 0) {
504 			priv->dp_write_urb_in_use = 1;
505 			priv->dp_out_buf_len = 0;
506 			count -= len;
507 			buf += len;
508 		}
509 
510 	}
511 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
512 
513 	if (ret)
514 		dev_err(&port->dev,
515 			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
516 			__func__, ret, priv->dp_port_num);
517 	return ret;
518 }
519 
520 
521 /*
522  *  Digi Set Modem Signals
523  *
524  *  Sets or clears DTR and RTS on the port, according to the
525  *  modem_signals argument.  Use TIOCM_DTR and TIOCM_RTS flags
526  *  for the modem_signals argument.  Returns 0 if successful,
527  *  -EINTR if interrupted while sleeping, or a non-zero error
528  *  returned by usb_submit_urb.
529  */
530 
531 static int digi_set_modem_signals(struct usb_serial_port *port,
532 	unsigned int modem_signals, int interruptible)
533 {
534 
535 	int ret;
536 	struct digi_port *port_priv = usb_get_serial_port_data(port);
537 	struct usb_serial_port *oob_port = (struct usb_serial_port *) ((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
538 	struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
539 	unsigned char *data = oob_port->write_urb->transfer_buffer;
540 	unsigned long flags = 0;
541 
542 
543 	dev_dbg(&port->dev,
544 		"digi_set_modem_signals: TOP: port=%d, modem_signals=0x%x\n",
545 		port_priv->dp_port_num, modem_signals);
546 
547 	spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
548 	spin_lock(&port_priv->dp_port_lock);
549 
550 	while (oob_priv->dp_write_urb_in_use) {
551 		spin_unlock(&port_priv->dp_port_lock);
552 		cond_wait_interruptible_timeout_irqrestore(
553 			&oob_port->write_wait, DIGI_RETRY_TIMEOUT,
554 			&oob_priv->dp_port_lock, flags);
555 		if (interruptible && signal_pending(current))
556 			return -EINTR;
557 		spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
558 		spin_lock(&port_priv->dp_port_lock);
559 	}
560 	data[0] = DIGI_CMD_SET_DTR_SIGNAL;
561 	data[1] = port_priv->dp_port_num;
562 	data[2] = (modem_signals & TIOCM_DTR) ?
563 					DIGI_DTR_ACTIVE : DIGI_DTR_INACTIVE;
564 	data[3] = 0;
565 	data[4] = DIGI_CMD_SET_RTS_SIGNAL;
566 	data[5] = port_priv->dp_port_num;
567 	data[6] = (modem_signals & TIOCM_RTS) ?
568 					DIGI_RTS_ACTIVE : DIGI_RTS_INACTIVE;
569 	data[7] = 0;
570 
571 	oob_port->write_urb->transfer_buffer_length = 8;
572 
573 	ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
574 	if (ret == 0) {
575 		oob_priv->dp_write_urb_in_use = 1;
576 		port_priv->dp_modem_signals =
577 			(port_priv->dp_modem_signals&~(TIOCM_DTR|TIOCM_RTS))
578 			| (modem_signals&(TIOCM_DTR|TIOCM_RTS));
579 	}
580 	spin_unlock(&port_priv->dp_port_lock);
581 	spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
582 	if (ret)
583 		dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n",
584 			__func__, ret);
585 	return ret;
586 }
587 
588 /*
589  *  Digi Transmit Idle
590  *
591  *  Digi transmit idle waits, up to timeout ticks, for the transmitter
592  *  to go idle.  It returns 0 if successful or a negative error.
593  *
594  *  There are race conditions here if more than one process is calling
595  *  digi_transmit_idle on the same port at the same time.  However, this
596  *  is only called from close, and only one process can be in close on a
597  *  port at a time, so its ok.
598  */
599 
600 static int digi_transmit_idle(struct usb_serial_port *port,
601 	unsigned long timeout)
602 {
603 	int ret;
604 	unsigned char buf[2];
605 	struct digi_port *priv = usb_get_serial_port_data(port);
606 	unsigned long flags = 0;
607 
608 	spin_lock_irqsave(&priv->dp_port_lock, flags);
609 	priv->dp_transmit_idle = 0;
610 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
611 
612 	buf[0] = DIGI_CMD_TRANSMIT_IDLE;
613 	buf[1] = 0;
614 
615 	timeout += jiffies;
616 
617 	ret = digi_write_inb_command(port, buf, 2, timeout - jiffies);
618 	if (ret != 0)
619 		return ret;
620 
621 	spin_lock_irqsave(&priv->dp_port_lock, flags);
622 
623 	while (time_before(jiffies, timeout) && !priv->dp_transmit_idle) {
624 		cond_wait_interruptible_timeout_irqrestore(
625 			&priv->dp_transmit_idle_wait, DIGI_RETRY_TIMEOUT,
626 			&priv->dp_port_lock, flags);
627 		if (signal_pending(current))
628 			return -EINTR;
629 		spin_lock_irqsave(&priv->dp_port_lock, flags);
630 	}
631 	priv->dp_transmit_idle = 0;
632 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
633 	return 0;
634 
635 }
636 
637 
638 static void digi_rx_throttle(struct tty_struct *tty)
639 {
640 	unsigned long flags;
641 	struct usb_serial_port *port = tty->driver_data;
642 	struct digi_port *priv = usb_get_serial_port_data(port);
643 
644 	/* stop receiving characters by not resubmitting the read urb */
645 	spin_lock_irqsave(&priv->dp_port_lock, flags);
646 	priv->dp_throttled = 1;
647 	priv->dp_throttle_restart = 0;
648 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
649 }
650 
651 
652 static void digi_rx_unthrottle(struct tty_struct *tty)
653 {
654 	int ret = 0;
655 	unsigned long flags;
656 	struct usb_serial_port *port = tty->driver_data;
657 	struct digi_port *priv = usb_get_serial_port_data(port);
658 
659 	spin_lock_irqsave(&priv->dp_port_lock, flags);
660 
661 	/* restart read chain */
662 	if (priv->dp_throttle_restart)
663 		ret = usb_submit_urb(port->read_urb, GFP_ATOMIC);
664 
665 	/* turn throttle off */
666 	priv->dp_throttled = 0;
667 	priv->dp_throttle_restart = 0;
668 
669 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
670 
671 	if (ret)
672 		dev_err(&port->dev,
673 			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
674 			__func__, ret, priv->dp_port_num);
675 }
676 
677 
678 static void digi_set_termios(struct tty_struct *tty,
679 		struct usb_serial_port *port, struct ktermios *old_termios)
680 {
681 	struct digi_port *priv = usb_get_serial_port_data(port);
682 	struct device *dev = &port->dev;
683 	unsigned int iflag = tty->termios.c_iflag;
684 	unsigned int cflag = tty->termios.c_cflag;
685 	unsigned int old_iflag = old_termios->c_iflag;
686 	unsigned int old_cflag = old_termios->c_cflag;
687 	unsigned char buf[32];
688 	unsigned int modem_signals;
689 	int arg, ret;
690 	int i = 0;
691 	speed_t baud;
692 
693 	dev_dbg(dev,
694 		"digi_set_termios: TOP: port=%d, iflag=0x%x, old_iflag=0x%x, cflag=0x%x, old_cflag=0x%x\n",
695 		priv->dp_port_num, iflag, old_iflag, cflag, old_cflag);
696 
697 	/* set baud rate */
698 	baud = tty_get_baud_rate(tty);
699 	if (baud != tty_termios_baud_rate(old_termios)) {
700 		arg = -1;
701 
702 		/* reassert DTR and (maybe) RTS on transition from B0 */
703 		if ((old_cflag & CBAUD) == B0) {
704 			/* don't set RTS if using hardware flow control */
705 			/* and throttling input */
706 			modem_signals = TIOCM_DTR;
707 			if (!C_CRTSCTS(tty) || !tty_throttled(tty))
708 				modem_signals |= TIOCM_RTS;
709 			digi_set_modem_signals(port, modem_signals, 1);
710 		}
711 		switch (baud) {
712 		/* drop DTR and RTS on transition to B0 */
713 		case 0: digi_set_modem_signals(port, 0, 1); break;
714 		case 50: arg = DIGI_BAUD_50; break;
715 		case 75: arg = DIGI_BAUD_75; break;
716 		case 110: arg = DIGI_BAUD_110; break;
717 		case 150: arg = DIGI_BAUD_150; break;
718 		case 200: arg = DIGI_BAUD_200; break;
719 		case 300: arg = DIGI_BAUD_300; break;
720 		case 600: arg = DIGI_BAUD_600; break;
721 		case 1200: arg = DIGI_BAUD_1200; break;
722 		case 1800: arg = DIGI_BAUD_1800; break;
723 		case 2400: arg = DIGI_BAUD_2400; break;
724 		case 4800: arg = DIGI_BAUD_4800; break;
725 		case 9600: arg = DIGI_BAUD_9600; break;
726 		case 19200: arg = DIGI_BAUD_19200; break;
727 		case 38400: arg = DIGI_BAUD_38400; break;
728 		case 57600: arg = DIGI_BAUD_57600; break;
729 		case 115200: arg = DIGI_BAUD_115200; break;
730 		case 230400: arg = DIGI_BAUD_230400; break;
731 		case 460800: arg = DIGI_BAUD_460800; break;
732 		default:
733 			arg = DIGI_BAUD_9600;
734 			baud = 9600;
735 			break;
736 		}
737 		if (arg != -1) {
738 			buf[i++] = DIGI_CMD_SET_BAUD_RATE;
739 			buf[i++] = priv->dp_port_num;
740 			buf[i++] = arg;
741 			buf[i++] = 0;
742 		}
743 	}
744 	/* set parity */
745 	tty->termios.c_cflag &= ~CMSPAR;
746 
747 	if ((cflag&(PARENB|PARODD)) != (old_cflag&(PARENB|PARODD))) {
748 		if (cflag&PARENB) {
749 			if (cflag&PARODD)
750 				arg = DIGI_PARITY_ODD;
751 			else
752 				arg = DIGI_PARITY_EVEN;
753 		} else {
754 			arg = DIGI_PARITY_NONE;
755 		}
756 		buf[i++] = DIGI_CMD_SET_PARITY;
757 		buf[i++] = priv->dp_port_num;
758 		buf[i++] = arg;
759 		buf[i++] = 0;
760 	}
761 	/* set word size */
762 	if ((cflag&CSIZE) != (old_cflag&CSIZE)) {
763 		arg = -1;
764 		switch (cflag&CSIZE) {
765 		case CS5: arg = DIGI_WORD_SIZE_5; break;
766 		case CS6: arg = DIGI_WORD_SIZE_6; break;
767 		case CS7: arg = DIGI_WORD_SIZE_7; break;
768 		case CS8: arg = DIGI_WORD_SIZE_8; break;
769 		default:
770 			dev_dbg(dev,
771 				"digi_set_termios: can't handle word size %d\n",
772 				(cflag&CSIZE));
773 			break;
774 		}
775 
776 		if (arg != -1) {
777 			buf[i++] = DIGI_CMD_SET_WORD_SIZE;
778 			buf[i++] = priv->dp_port_num;
779 			buf[i++] = arg;
780 			buf[i++] = 0;
781 		}
782 
783 	}
784 
785 	/* set stop bits */
786 	if ((cflag&CSTOPB) != (old_cflag&CSTOPB)) {
787 
788 		if ((cflag&CSTOPB))
789 			arg = DIGI_STOP_BITS_2;
790 		else
791 			arg = DIGI_STOP_BITS_1;
792 
793 		buf[i++] = DIGI_CMD_SET_STOP_BITS;
794 		buf[i++] = priv->dp_port_num;
795 		buf[i++] = arg;
796 		buf[i++] = 0;
797 
798 	}
799 
800 	/* set input flow control */
801 	if ((iflag&IXOFF) != (old_iflag&IXOFF)
802 	    || (cflag&CRTSCTS) != (old_cflag&CRTSCTS)) {
803 		arg = 0;
804 		if (iflag&IXOFF)
805 			arg |= DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
806 		else
807 			arg &= ~DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
808 
809 		if (cflag&CRTSCTS) {
810 			arg |= DIGI_INPUT_FLOW_CONTROL_RTS;
811 
812 			/* On USB-4 it is necessary to assert RTS prior */
813 			/* to selecting RTS input flow control.  */
814 			buf[i++] = DIGI_CMD_SET_RTS_SIGNAL;
815 			buf[i++] = priv->dp_port_num;
816 			buf[i++] = DIGI_RTS_ACTIVE;
817 			buf[i++] = 0;
818 
819 		} else {
820 			arg &= ~DIGI_INPUT_FLOW_CONTROL_RTS;
821 		}
822 		buf[i++] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
823 		buf[i++] = priv->dp_port_num;
824 		buf[i++] = arg;
825 		buf[i++] = 0;
826 	}
827 
828 	/* set output flow control */
829 	if ((iflag & IXON) != (old_iflag & IXON)
830 	    || (cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
831 		arg = 0;
832 		if (iflag & IXON)
833 			arg |= DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
834 		else
835 			arg &= ~DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
836 
837 		if (cflag & CRTSCTS) {
838 			arg |= DIGI_OUTPUT_FLOW_CONTROL_CTS;
839 		} else {
840 			arg &= ~DIGI_OUTPUT_FLOW_CONTROL_CTS;
841 		}
842 
843 		buf[i++] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
844 		buf[i++] = priv->dp_port_num;
845 		buf[i++] = arg;
846 		buf[i++] = 0;
847 	}
848 
849 	/* set receive enable/disable */
850 	if ((cflag & CREAD) != (old_cflag & CREAD)) {
851 		if (cflag & CREAD)
852 			arg = DIGI_ENABLE;
853 		else
854 			arg = DIGI_DISABLE;
855 
856 		buf[i++] = DIGI_CMD_RECEIVE_ENABLE;
857 		buf[i++] = priv->dp_port_num;
858 		buf[i++] = arg;
859 		buf[i++] = 0;
860 	}
861 	ret = digi_write_oob_command(port, buf, i, 1);
862 	if (ret != 0)
863 		dev_dbg(dev, "digi_set_termios: write oob failed, ret=%d\n", ret);
864 	tty_encode_baud_rate(tty, baud, baud);
865 }
866 
867 
868 static void digi_break_ctl(struct tty_struct *tty, int break_state)
869 {
870 	struct usb_serial_port *port = tty->driver_data;
871 	unsigned char buf[4];
872 
873 	buf[0] = DIGI_CMD_BREAK_CONTROL;
874 	buf[1] = 2;				/* length */
875 	buf[2] = break_state ? 1 : 0;
876 	buf[3] = 0;				/* pad */
877 	digi_write_inb_command(port, buf, 4, 0);
878 }
879 
880 
881 static int digi_tiocmget(struct tty_struct *tty)
882 {
883 	struct usb_serial_port *port = tty->driver_data;
884 	struct digi_port *priv = usb_get_serial_port_data(port);
885 	unsigned int val;
886 	unsigned long flags;
887 
888 	spin_lock_irqsave(&priv->dp_port_lock, flags);
889 	val = priv->dp_modem_signals;
890 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
891 	return val;
892 }
893 
894 
895 static int digi_tiocmset(struct tty_struct *tty,
896 					unsigned int set, unsigned int clear)
897 {
898 	struct usb_serial_port *port = tty->driver_data;
899 	struct digi_port *priv = usb_get_serial_port_data(port);
900 	unsigned int val;
901 	unsigned long flags;
902 
903 	spin_lock_irqsave(&priv->dp_port_lock, flags);
904 	val = (priv->dp_modem_signals & ~clear) | set;
905 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
906 	return digi_set_modem_signals(port, val, 1);
907 }
908 
909 
910 static int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
911 					const unsigned char *buf, int count)
912 {
913 
914 	int ret, data_len, new_len;
915 	struct digi_port *priv = usb_get_serial_port_data(port);
916 	unsigned char *data = port->write_urb->transfer_buffer;
917 	unsigned long flags = 0;
918 
919 	dev_dbg(&port->dev,
920 		"digi_write: TOP: port=%d, count=%d, in_interrupt=%ld\n",
921 		priv->dp_port_num, count, in_interrupt());
922 
923 	/* copy user data (which can sleep) before getting spin lock */
924 	count = min(count, port->bulk_out_size-2);
925 	count = min(64, count);
926 
927 	/* be sure only one write proceeds at a time */
928 	/* there are races on the port private buffer */
929 	spin_lock_irqsave(&priv->dp_port_lock, flags);
930 
931 	/* wait for urb status clear to submit another urb */
932 	if (priv->dp_write_urb_in_use) {
933 		/* buffer data if count is 1 (probably put_char) if possible */
934 		if (count == 1 && priv->dp_out_buf_len < DIGI_OUT_BUF_SIZE) {
935 			priv->dp_out_buf[priv->dp_out_buf_len++] = *buf;
936 			new_len = 1;
937 		} else {
938 			new_len = 0;
939 		}
940 		spin_unlock_irqrestore(&priv->dp_port_lock, flags);
941 		return new_len;
942 	}
943 
944 	/* allow space for any buffered data and for new data, up to */
945 	/* transfer buffer size - 2 (for command and length bytes) */
946 	new_len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
947 	data_len = new_len + priv->dp_out_buf_len;
948 
949 	if (data_len == 0) {
950 		spin_unlock_irqrestore(&priv->dp_port_lock, flags);
951 		return 0;
952 	}
953 
954 	port->write_urb->transfer_buffer_length = data_len+2;
955 
956 	*data++ = DIGI_CMD_SEND_DATA;
957 	*data++ = data_len;
958 
959 	/* copy in buffered data first */
960 	memcpy(data, priv->dp_out_buf, priv->dp_out_buf_len);
961 	data += priv->dp_out_buf_len;
962 
963 	/* copy in new data */
964 	memcpy(data, buf, new_len);
965 
966 	ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
967 	if (ret == 0) {
968 		priv->dp_write_urb_in_use = 1;
969 		ret = new_len;
970 		priv->dp_out_buf_len = 0;
971 	}
972 
973 	/* return length of new data written, or error */
974 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
975 	if (ret < 0)
976 		dev_err_console(port,
977 			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
978 			__func__, ret, priv->dp_port_num);
979 	dev_dbg(&port->dev, "digi_write: returning %d\n", ret);
980 	return ret;
981 
982 }
983 
984 static void digi_write_bulk_callback(struct urb *urb)
985 {
986 
987 	struct usb_serial_port *port = urb->context;
988 	struct usb_serial *serial;
989 	struct digi_port *priv;
990 	struct digi_serial *serial_priv;
991 	int ret = 0;
992 	int status = urb->status;
993 
994 	/* port and serial sanity check */
995 	if (port == NULL || (priv = usb_get_serial_port_data(port)) == NULL) {
996 		pr_err("%s: port or port->private is NULL, status=%d\n",
997 			__func__, status);
998 		return;
999 	}
1000 	serial = port->serial;
1001 	if (serial == NULL || (serial_priv = usb_get_serial_data(serial)) == NULL) {
1002 		dev_err(&port->dev,
1003 			"%s: serial or serial->private is NULL, status=%d\n",
1004 			__func__, status);
1005 		return;
1006 	}
1007 
1008 	/* handle oob callback */
1009 	if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
1010 		dev_dbg(&port->dev, "digi_write_bulk_callback: oob callback\n");
1011 		spin_lock(&priv->dp_port_lock);
1012 		priv->dp_write_urb_in_use = 0;
1013 		wake_up_interruptible(&port->write_wait);
1014 		spin_unlock(&priv->dp_port_lock);
1015 		return;
1016 	}
1017 
1018 	/* try to send any buffered data on this port */
1019 	spin_lock(&priv->dp_port_lock);
1020 	priv->dp_write_urb_in_use = 0;
1021 	if (priv->dp_out_buf_len > 0) {
1022 		*((unsigned char *)(port->write_urb->transfer_buffer))
1023 			= (unsigned char)DIGI_CMD_SEND_DATA;
1024 		*((unsigned char *)(port->write_urb->transfer_buffer) + 1)
1025 			= (unsigned char)priv->dp_out_buf_len;
1026 		port->write_urb->transfer_buffer_length =
1027 						priv->dp_out_buf_len + 2;
1028 		memcpy(port->write_urb->transfer_buffer + 2, priv->dp_out_buf,
1029 			priv->dp_out_buf_len);
1030 		ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
1031 		if (ret == 0) {
1032 			priv->dp_write_urb_in_use = 1;
1033 			priv->dp_out_buf_len = 0;
1034 		}
1035 	}
1036 	/* wake up processes sleeping on writes immediately */
1037 	tty_port_tty_wakeup(&port->port);
1038 	/* also queue up a wakeup at scheduler time, in case we */
1039 	/* lost the race in write_chan(). */
1040 	schedule_work(&priv->dp_wakeup_work);
1041 
1042 	spin_unlock(&priv->dp_port_lock);
1043 	if (ret && ret != -EPERM)
1044 		dev_err_console(port,
1045 			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
1046 			__func__, ret, priv->dp_port_num);
1047 }
1048 
1049 static int digi_write_room(struct tty_struct *tty)
1050 {
1051 	struct usb_serial_port *port = tty->driver_data;
1052 	struct digi_port *priv = usb_get_serial_port_data(port);
1053 	int room;
1054 	unsigned long flags = 0;
1055 
1056 	spin_lock_irqsave(&priv->dp_port_lock, flags);
1057 
1058 	if (priv->dp_write_urb_in_use)
1059 		room = 0;
1060 	else
1061 		room = port->bulk_out_size - 2 - priv->dp_out_buf_len;
1062 
1063 	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
1064 	dev_dbg(&port->dev, "digi_write_room: port=%d, room=%d\n", priv->dp_port_num, room);
1065 	return room;
1066 
1067 }
1068 
1069 static int digi_chars_in_buffer(struct tty_struct *tty)
1070 {
1071 	struct usb_serial_port *port = tty->driver_data;
1072 	struct digi_port *priv = usb_get_serial_port_data(port);
1073 
1074 	if (priv->dp_write_urb_in_use) {
1075 		dev_dbg(&port->dev, "digi_chars_in_buffer: port=%d, chars=%d\n",
1076 			priv->dp_port_num, port->bulk_out_size - 2);
1077 		/* return(port->bulk_out_size - 2); */
1078 		return 256;
1079 	} else {
1080 		dev_dbg(&port->dev, "digi_chars_in_buffer: port=%d, chars=%d\n",
1081 			priv->dp_port_num, priv->dp_out_buf_len);
1082 		return priv->dp_out_buf_len;
1083 	}
1084 
1085 }
1086 
1087 static void digi_dtr_rts(struct usb_serial_port *port, int on)
1088 {
1089 	/* Adjust DTR and RTS */
1090 	digi_set_modem_signals(port, on * (TIOCM_DTR|TIOCM_RTS), 1);
1091 }
1092 
1093 static int digi_open(struct tty_struct *tty, struct usb_serial_port *port)
1094 {
1095 	int ret;
1096 	unsigned char buf[32];
1097 	struct digi_port *priv = usb_get_serial_port_data(port);
1098 	struct ktermios not_termios;
1099 
1100 	/* be sure the device is started up */
1101 	if (digi_startup_device(port->serial) != 0)
1102 		return -ENXIO;
1103 
1104 	/* read modem signals automatically whenever they change */
1105 	buf[0] = DIGI_CMD_READ_INPUT_SIGNALS;
1106 	buf[1] = priv->dp_port_num;
1107 	buf[2] = DIGI_ENABLE;
1108 	buf[3] = 0;
1109 
1110 	/* flush fifos */
1111 	buf[4] = DIGI_CMD_IFLUSH_FIFO;
1112 	buf[5] = priv->dp_port_num;
1113 	buf[6] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
1114 	buf[7] = 0;
1115 
1116 	ret = digi_write_oob_command(port, buf, 8, 1);
1117 	if (ret != 0)
1118 		dev_dbg(&port->dev, "digi_open: write oob failed, ret=%d\n", ret);
1119 
1120 	/* set termios settings */
1121 	if (tty) {
1122 		not_termios.c_cflag = ~tty->termios.c_cflag;
1123 		not_termios.c_iflag = ~tty->termios.c_iflag;
1124 		digi_set_termios(tty, port, &not_termios);
1125 	}
1126 	return 0;
1127 }
1128 
1129 
1130 static void digi_close(struct usb_serial_port *port)
1131 {
1132 	DEFINE_WAIT(wait);
1133 	int ret;
1134 	unsigned char buf[32];
1135 	struct digi_port *priv = usb_get_serial_port_data(port);
1136 
1137 	mutex_lock(&port->serial->disc_mutex);
1138 	/* if disconnected, just clear flags */
1139 	if (port->serial->disconnected)
1140 		goto exit;
1141 
1142 	/* FIXME: Transmit idle belongs in the wait_unti_sent path */
1143 	digi_transmit_idle(port, DIGI_CLOSE_TIMEOUT);
1144 
1145 	/* disable input flow control */
1146 	buf[0] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
1147 	buf[1] = priv->dp_port_num;
1148 	buf[2] = DIGI_DISABLE;
1149 	buf[3] = 0;
1150 
1151 	/* disable output flow control */
1152 	buf[4] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
1153 	buf[5] = priv->dp_port_num;
1154 	buf[6] = DIGI_DISABLE;
1155 	buf[7] = 0;
1156 
1157 	/* disable reading modem signals automatically */
1158 	buf[8] = DIGI_CMD_READ_INPUT_SIGNALS;
1159 	buf[9] = priv->dp_port_num;
1160 	buf[10] = DIGI_DISABLE;
1161 	buf[11] = 0;
1162 
1163 	/* disable receive */
1164 	buf[12] = DIGI_CMD_RECEIVE_ENABLE;
1165 	buf[13] = priv->dp_port_num;
1166 	buf[14] = DIGI_DISABLE;
1167 	buf[15] = 0;
1168 
1169 	/* flush fifos */
1170 	buf[16] = DIGI_CMD_IFLUSH_FIFO;
1171 	buf[17] = priv->dp_port_num;
1172 	buf[18] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
1173 	buf[19] = 0;
1174 
1175 	ret = digi_write_oob_command(port, buf, 20, 0);
1176 	if (ret != 0)
1177 		dev_dbg(&port->dev, "digi_close: write oob failed, ret=%d\n",
1178 									ret);
1179 	/* wait for final commands on oob port to complete */
1180 	prepare_to_wait(&priv->dp_flush_wait, &wait,
1181 			TASK_INTERRUPTIBLE);
1182 	schedule_timeout(DIGI_CLOSE_TIMEOUT);
1183 	finish_wait(&priv->dp_flush_wait, &wait);
1184 
1185 	/* shutdown any outstanding bulk writes */
1186 	usb_kill_urb(port->write_urb);
1187 exit:
1188 	spin_lock_irq(&priv->dp_port_lock);
1189 	priv->dp_write_urb_in_use = 0;
1190 	wake_up_interruptible(&priv->dp_close_wait);
1191 	spin_unlock_irq(&priv->dp_port_lock);
1192 	mutex_unlock(&port->serial->disc_mutex);
1193 }
1194 
1195 
1196 /*
1197  *  Digi Startup Device
1198  *
1199  *  Starts reads on all ports.  Must be called AFTER startup, with
1200  *  urbs initialized.  Returns 0 if successful, non-zero error otherwise.
1201  */
1202 
1203 static int digi_startup_device(struct usb_serial *serial)
1204 {
1205 	int i, ret = 0;
1206 	struct digi_serial *serial_priv = usb_get_serial_data(serial);
1207 	struct usb_serial_port *port;
1208 
1209 	/* be sure this happens exactly once */
1210 	spin_lock(&serial_priv->ds_serial_lock);
1211 	if (serial_priv->ds_device_started) {
1212 		spin_unlock(&serial_priv->ds_serial_lock);
1213 		return 0;
1214 	}
1215 	serial_priv->ds_device_started = 1;
1216 	spin_unlock(&serial_priv->ds_serial_lock);
1217 
1218 	/* start reading from each bulk in endpoint for the device */
1219 	/* set USB_DISABLE_SPD flag for write bulk urbs */
1220 	for (i = 0; i < serial->type->num_ports + 1; i++) {
1221 		port = serial->port[i];
1222 		ret = usb_submit_urb(port->read_urb, GFP_KERNEL);
1223 		if (ret != 0) {
1224 			dev_err(&port->dev,
1225 				"%s: usb_submit_urb failed, ret=%d, port=%d\n",
1226 				__func__, ret, i);
1227 			break;
1228 		}
1229 	}
1230 	return ret;
1231 }
1232 
1233 static int digi_port_init(struct usb_serial_port *port, unsigned port_num)
1234 {
1235 	struct digi_port *priv;
1236 
1237 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1238 	if (!priv)
1239 		return -ENOMEM;
1240 
1241 	spin_lock_init(&priv->dp_port_lock);
1242 	priv->dp_port_num = port_num;
1243 	init_waitqueue_head(&priv->dp_transmit_idle_wait);
1244 	init_waitqueue_head(&priv->dp_flush_wait);
1245 	init_waitqueue_head(&priv->dp_close_wait);
1246 	INIT_WORK(&priv->dp_wakeup_work, digi_wakeup_write_lock);
1247 	priv->dp_port = port;
1248 
1249 	init_waitqueue_head(&port->write_wait);
1250 
1251 	usb_set_serial_port_data(port, priv);
1252 
1253 	return 0;
1254 }
1255 
1256 static int digi_startup(struct usb_serial *serial)
1257 {
1258 	struct digi_serial *serial_priv;
1259 	int ret;
1260 
1261 	serial_priv = kzalloc(sizeof(*serial_priv), GFP_KERNEL);
1262 	if (!serial_priv)
1263 		return -ENOMEM;
1264 
1265 	spin_lock_init(&serial_priv->ds_serial_lock);
1266 	serial_priv->ds_oob_port_num = serial->type->num_ports;
1267 	serial_priv->ds_oob_port = serial->port[serial_priv->ds_oob_port_num];
1268 
1269 	ret = digi_port_init(serial_priv->ds_oob_port,
1270 						serial_priv->ds_oob_port_num);
1271 	if (ret) {
1272 		kfree(serial_priv);
1273 		return ret;
1274 	}
1275 
1276 	usb_set_serial_data(serial, serial_priv);
1277 
1278 	return 0;
1279 }
1280 
1281 
1282 static void digi_disconnect(struct usb_serial *serial)
1283 {
1284 	int i;
1285 
1286 	/* stop reads and writes on all ports */
1287 	for (i = 0; i < serial->type->num_ports + 1; i++) {
1288 		usb_kill_urb(serial->port[i]->read_urb);
1289 		usb_kill_urb(serial->port[i]->write_urb);
1290 	}
1291 }
1292 
1293 
1294 static void digi_release(struct usb_serial *serial)
1295 {
1296 	struct digi_serial *serial_priv;
1297 	struct digi_port *priv;
1298 
1299 	serial_priv = usb_get_serial_data(serial);
1300 
1301 	priv = usb_get_serial_port_data(serial_priv->ds_oob_port);
1302 	kfree(priv);
1303 
1304 	kfree(serial_priv);
1305 }
1306 
1307 static int digi_port_probe(struct usb_serial_port *port)
1308 {
1309 	return digi_port_init(port, port->port_number);
1310 }
1311 
1312 static int digi_port_remove(struct usb_serial_port *port)
1313 {
1314 	struct digi_port *priv;
1315 
1316 	priv = usb_get_serial_port_data(port);
1317 	kfree(priv);
1318 
1319 	return 0;
1320 }
1321 
1322 static void digi_read_bulk_callback(struct urb *urb)
1323 {
1324 	struct usb_serial_port *port = urb->context;
1325 	struct digi_port *priv;
1326 	struct digi_serial *serial_priv;
1327 	int ret;
1328 	int status = urb->status;
1329 
1330 	/* port sanity check, do not resubmit if port is not valid */
1331 	if (port == NULL)
1332 		return;
1333 	priv = usb_get_serial_port_data(port);
1334 	if (priv == NULL) {
1335 		dev_err(&port->dev, "%s: port->private is NULL, status=%d\n",
1336 			__func__, status);
1337 		return;
1338 	}
1339 	if (port->serial == NULL ||
1340 		(serial_priv = usb_get_serial_data(port->serial)) == NULL) {
1341 		dev_err(&port->dev, "%s: serial is bad or serial->private "
1342 			"is NULL, status=%d\n", __func__, status);
1343 		return;
1344 	}
1345 
1346 	/* do not resubmit urb if it has any status error */
1347 	if (status) {
1348 		dev_err(&port->dev,
1349 			"%s: nonzero read bulk status: status=%d, port=%d\n",
1350 			__func__, status, priv->dp_port_num);
1351 		return;
1352 	}
1353 
1354 	/* handle oob or inb callback, do not resubmit if error */
1355 	if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
1356 		if (digi_read_oob_callback(urb) != 0)
1357 			return;
1358 	} else {
1359 		if (digi_read_inb_callback(urb) != 0)
1360 			return;
1361 	}
1362 
1363 	/* continue read */
1364 	ret = usb_submit_urb(urb, GFP_ATOMIC);
1365 	if (ret != 0 && ret != -EPERM) {
1366 		dev_err(&port->dev,
1367 			"%s: failed resubmitting urb, ret=%d, port=%d\n",
1368 			__func__, ret, priv->dp_port_num);
1369 	}
1370 
1371 }
1372 
1373 /*
1374  *  Digi Read INB Callback
1375  *
1376  *  Digi Read INB Callback handles reads on the in band ports, sending
1377  *  the data on to the tty subsystem.  When called we know port and
1378  *  port->private are not NULL and port->serial has been validated.
1379  *  It returns 0 if successful, 1 if successful but the port is
1380  *  throttled, and -1 if the sanity checks failed.
1381  */
1382 
1383 static int digi_read_inb_callback(struct urb *urb)
1384 {
1385 	struct usb_serial_port *port = urb->context;
1386 	struct digi_port *priv = usb_get_serial_port_data(port);
1387 	unsigned char *buf = urb->transfer_buffer;
1388 	int opcode;
1389 	int len;
1390 	int port_status;
1391 	unsigned char *data;
1392 	int flag, throttled;
1393 
1394 	/* short/multiple packet check */
1395 	if (urb->actual_length < 2) {
1396 		dev_warn(&port->dev, "short packet received\n");
1397 		return -1;
1398 	}
1399 
1400 	opcode = buf[0];
1401 	len = buf[1];
1402 
1403 	if (urb->actual_length != len + 2) {
1404 		dev_err(&port->dev, "malformed packet received: port=%d, opcode=%d, len=%d, actual_length=%u\n",
1405 			priv->dp_port_num, opcode, len, urb->actual_length);
1406 		return -1;
1407 	}
1408 
1409 	if (opcode == DIGI_CMD_RECEIVE_DATA && len < 1) {
1410 		dev_err(&port->dev, "malformed data packet received\n");
1411 		return -1;
1412 	}
1413 
1414 	spin_lock(&priv->dp_port_lock);
1415 
1416 	/* check for throttle; if set, do not resubmit read urb */
1417 	/* indicate the read chain needs to be restarted on unthrottle */
1418 	throttled = priv->dp_throttled;
1419 	if (throttled)
1420 		priv->dp_throttle_restart = 1;
1421 
1422 	/* receive data */
1423 	if (opcode == DIGI_CMD_RECEIVE_DATA) {
1424 		port_status = buf[2];
1425 		data = &buf[3];
1426 
1427 		/* get flag from port_status */
1428 		flag = 0;
1429 
1430 		/* overrun is special, not associated with a char */
1431 		if (port_status & DIGI_OVERRUN_ERROR)
1432 			tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
1433 
1434 		/* break takes precedence over parity, */
1435 		/* which takes precedence over framing errors */
1436 		if (port_status & DIGI_BREAK_ERROR)
1437 			flag = TTY_BREAK;
1438 		else if (port_status & DIGI_PARITY_ERROR)
1439 			flag = TTY_PARITY;
1440 		else if (port_status & DIGI_FRAMING_ERROR)
1441 			flag = TTY_FRAME;
1442 
1443 		/* data length is len-1 (one byte of len is port_status) */
1444 		--len;
1445 		if (len > 0) {
1446 			tty_insert_flip_string_fixed_flag(&port->port, data,
1447 					flag, len);
1448 			tty_flip_buffer_push(&port->port);
1449 		}
1450 	}
1451 	spin_unlock(&priv->dp_port_lock);
1452 
1453 	if (opcode == DIGI_CMD_RECEIVE_DISABLE)
1454 		dev_dbg(&port->dev, "%s: got RECEIVE_DISABLE\n", __func__);
1455 	else if (opcode != DIGI_CMD_RECEIVE_DATA)
1456 		dev_dbg(&port->dev, "%s: unknown opcode: %d\n", __func__, opcode);
1457 
1458 	return throttled ? 1 : 0;
1459 
1460 }
1461 
1462 
1463 /*
1464  *  Digi Read OOB Callback
1465  *
1466  *  Digi Read OOB Callback handles reads on the out of band port.
1467  *  When called we know port and port->private are not NULL and
1468  *  the port->serial is valid.  It returns 0 if successful, and
1469  *  -1 if the sanity checks failed.
1470  */
1471 
1472 static int digi_read_oob_callback(struct urb *urb)
1473 {
1474 
1475 	struct usb_serial_port *port = urb->context;
1476 	struct usb_serial *serial = port->serial;
1477 	struct tty_struct *tty;
1478 	struct digi_port *priv = usb_get_serial_port_data(port);
1479 	unsigned char *buf = urb->transfer_buffer;
1480 	int opcode, line, status, val;
1481 	int i;
1482 	unsigned int rts;
1483 
1484 	if (urb->actual_length < 4)
1485 		return -1;
1486 
1487 	/* handle each oob command */
1488 	for (i = 0; i < urb->actual_length - 3; i += 4) {
1489 		opcode = buf[i];
1490 		line = buf[i + 1];
1491 		status = buf[i + 2];
1492 		val = buf[i + 3];
1493 
1494 		dev_dbg(&port->dev, "digi_read_oob_callback: opcode=%d, line=%d, status=%d, val=%d\n",
1495 			opcode, line, status, val);
1496 
1497 		if (status != 0 || line >= serial->type->num_ports)
1498 			continue;
1499 
1500 		port = serial->port[line];
1501 
1502 		priv = usb_get_serial_port_data(port);
1503 		if (priv == NULL)
1504 			return -1;
1505 
1506 		tty = tty_port_tty_get(&port->port);
1507 
1508 		rts = 0;
1509 		if (tty)
1510 			rts = C_CRTSCTS(tty);
1511 
1512 		if (tty && opcode == DIGI_CMD_READ_INPUT_SIGNALS) {
1513 			spin_lock(&priv->dp_port_lock);
1514 			/* convert from digi flags to termiox flags */
1515 			if (val & DIGI_READ_INPUT_SIGNALS_CTS) {
1516 				priv->dp_modem_signals |= TIOCM_CTS;
1517 				/* port must be open to use tty struct */
1518 				if (rts)
1519 					tty_port_tty_wakeup(&port->port);
1520 			} else {
1521 				priv->dp_modem_signals &= ~TIOCM_CTS;
1522 				/* port must be open to use tty struct */
1523 			}
1524 			if (val & DIGI_READ_INPUT_SIGNALS_DSR)
1525 				priv->dp_modem_signals |= TIOCM_DSR;
1526 			else
1527 				priv->dp_modem_signals &= ~TIOCM_DSR;
1528 			if (val & DIGI_READ_INPUT_SIGNALS_RI)
1529 				priv->dp_modem_signals |= TIOCM_RI;
1530 			else
1531 				priv->dp_modem_signals &= ~TIOCM_RI;
1532 			if (val & DIGI_READ_INPUT_SIGNALS_DCD)
1533 				priv->dp_modem_signals |= TIOCM_CD;
1534 			else
1535 				priv->dp_modem_signals &= ~TIOCM_CD;
1536 
1537 			spin_unlock(&priv->dp_port_lock);
1538 		} else if (opcode == DIGI_CMD_TRANSMIT_IDLE) {
1539 			spin_lock(&priv->dp_port_lock);
1540 			priv->dp_transmit_idle = 1;
1541 			wake_up_interruptible(&priv->dp_transmit_idle_wait);
1542 			spin_unlock(&priv->dp_port_lock);
1543 		} else if (opcode == DIGI_CMD_IFLUSH_FIFO) {
1544 			wake_up_interruptible(&priv->dp_flush_wait);
1545 		}
1546 		tty_kref_put(tty);
1547 	}
1548 	return 0;
1549 
1550 }
1551 
1552 module_usb_serial_driver(serial_drivers, id_table_combined);
1553 
1554 MODULE_AUTHOR(DRIVER_AUTHOR);
1555 MODULE_DESCRIPTION(DRIVER_DESC);
1556 MODULE_LICENSE("GPL");
1557