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