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