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