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