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