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