File: /usr/src/linux/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 * (04/08/2001) gb
18 * Identify version on module load.
19 *
20 * (11/01/2000) Adam J. Richter
21 * usb_device_id table support
22 *
23 * (11/01/2000) pberger and borchers
24 * -- Turned off the USB_DISABLE_SPD flag for write bulk urbs--it caused
25 * USB 4 ports to hang on startup.
26 * -- Serialized access to write urbs by adding the dp_write_urb_in_use
27 * flag; otherwise, the driver caused SMP system hangs. Watching the
28 * urb status is not sufficient.
29 *
30 * (10/05/2000) gkh
31 * -- Fixed bug with urb->dev not being set properly, now that the usb
32 * core needs it.
33 *
34 * (8/8/2000) pberger and borchers
35 * -- Fixed close so that
36 * - it can timeout while waiting for transmit idle, if needed;
37 * - it ignores interrupts when flushing the port, turning
38 * of modem signalling, and so on;
39 * - it waits for the flush to really complete before returning.
40 * -- Read_bulk_callback and write_bulk_callback check for a closed
41 * port before using the tty struct or writing to the port.
42 * -- The two changes above fix the oops caused by interrupted closes.
43 * -- Added interruptible args to write_oob_command and set_modem_signals
44 * and added a timeout arg to transmit_idle; needed for fixes to
45 * close.
46 * -- Added code for rx_throttle and rx_unthrottle so that input flow
47 * control works.
48 * -- Added code to set overrun, parity, framing, and break errors
49 * (untested).
50 * -- Set USB_DISABLE_SPD flag for write bulk urbs, so no 0 length
51 * bulk writes are done. These hung the Digi USB device. The
52 * 0 length bulk writes were a new feature of usb-uhci added in
53 * the 2.4.0-test6 kernels.
54 * -- Fixed mod inc race in open; do mod inc before sleeping to wait
55 * for a close to finish.
56 *
57 * (7/31/2000) pberger
58 * -- Fixed bugs with hardware handshaking:
59 * - Added code to set/clear tty->hw_stopped in digi_read_oob_callback()
60 * and digi_set_termios()
61 * -- Added code in digi_set_termios() to
62 * - add conditional in code handling transition from B0 to only
63 * set RTS if RTS/CTS flow control is either not in use or if
64 * the port is not currently throttled.
65 * - handle turning off CRTSCTS.
66 *
67 * (7/30/2000) borchers
68 * -- Added support for more than one Digi USB device by moving
69 * globals to a private structure in the pointed to from the
70 * usb_serial structure.
71 * -- Moved the modem change and transmit idle wait queues into
72 * the port private structure, so each port has its own queue
73 * rather than sharing global queues.
74 * -- Added support for break signals.
75 *
76 * (7/25/2000) pberger
77 * -- Added USB-2 support. Note: the USB-2 supports 3 devices: two
78 * serial and a parallel port. The parallel port is implemented
79 * as a serial-to-parallel converter. That is, the driver actually
80 * presents all three USB-2 interfaces as serial ports, but the third
81 * one physically connects to a parallel device. Thus, for example,
82 * one could plug a parallel printer into the USB-2's third port,
83 * but from the kernel's (and userland's) point of view what's
84 * actually out there is a serial device.
85 *
86 * (7/15/2000) borchers
87 * -- Fixed race in open when a close is in progress.
88 * -- Keep count of opens and dec the module use count for each
89 * outstanding open when shutdown is called (on disconnect).
90 * -- Fixed sanity checks in read_bulk_callback and write_bulk_callback
91 * so pointers are checked before use.
92 * -- Split read bulk callback into in band and out of band
93 * callbacks, and no longer restart read chains if there is
94 * a status error or a sanity error. This fixed the seg
95 * faults and other errors we used to get on disconnect.
96 * -- Port->active is once again a flag as usb-serial intended it
97 * to be, not a count. Since it was only a char it would
98 * have been limited to 256 simultaneous opens. Now the open
99 * count is kept in the port private structure in dp_open_count.
100 * -- Added code for modularization of the digi_acceleport driver.
101 *
102 * (6/27/2000) pberger and borchers
103 * -- Zeroed out sync field in the wakeup_task before first use;
104 * otherwise the uninitialized value might prevent the task from
105 * being scheduled.
106 * -- Initialized ret value to 0 in write_bulk_callback, otherwise
107 * the uninitialized value could cause a spurious debugging message.
108 *
109 * (6/22/2000) pberger and borchers
110 * -- Made cond_wait_... inline--apparently on SPARC the flags arg
111 * to spin_lock_irqsave cannot be passed to another function
112 * to call spin_unlock_irqrestore. Thanks to Pauline Middelink.
113 * -- In digi_set_modem_signals the inner nested spin locks use just
114 * spin_lock() rather than spin_lock_irqsave(). The old code
115 * mistakenly left interrupts off. Thanks to Pauline Middelink.
116 * -- copy_from_user (which can sleep) is no longer called while a
117 * spinlock is held. We copy to a local buffer before getting
118 * the spinlock--don't like the extra copy but the code is simpler.
119 * -- Printk and dbg are no longer called while a spin lock is held.
120 *
121 * (6/4/2000) pberger and borchers
122 * -- Replaced separate calls to spin_unlock_irqrestore and
123 * interruptible_sleep_on_timeout with a new function
124 * cond_wait_interruptible_timeout_irqrestore. This eliminates
125 * the race condition where the wake up could happen after
126 * the unlock and before the sleep.
127 * -- Close now waits for output to drain.
128 * -- Open waits until any close in progress is finished.
129 * -- All out of band responses are now processed, not just the
130 * first in a USB packet.
131 * -- Fixed a bug that prevented the driver from working when the
132 * first Digi port was not the first USB serial port--the driver
133 * was mistakenly using the external USB serial port number to
134 * try to index into its internal ports.
135 * -- Fixed an SMP bug -- write_bulk_callback is called directly from
136 * an interrupt, so spin_lock_irqsave/spin_unlock_irqrestore are
137 * needed for locks outside write_bulk_callback that are also
138 * acquired by write_bulk_callback to prevent deadlocks.
139 * -- Fixed support for select() by making digi_chars_in_buffer()
140 * return 256 when -EINPROGRESS is set, as the line discipline
141 * code in n_tty.c expects.
142 * -- Fixed an include file ordering problem that prevented debugging
143 * messages from working.
144 * -- Fixed an intermittent timeout problem that caused writes to
145 * sometimes get stuck on some machines on some kernels. It turns
146 * out in these circumstances write_chan() (in n_tty.c) was
147 * asleep waiting for our wakeup call. Even though we call
148 * wake_up_interruptible() in digi_write_bulk_callback(), there is
149 * a race condition that could cause the wakeup to fail: if our
150 * wake_up_interruptible() call occurs between the time that our
151 * driver write routine finishes and write_chan() sets current->state
152 * to TASK_INTERRUPTIBLE, the effect of our wakeup setting the state
153 * to TASK_RUNNING will be lost and write_chan's subsequent call to
154 * schedule() will never return (unless it catches a signal).
155 * This race condition occurs because write_bulk_callback() (and thus
156 * the wakeup) are called asynchonously from an interrupt, rather than
157 * from the scheduler. We can avoid the race by calling the wakeup
158 * from the scheduler queue and that's our fix: Now, at the end of
159 * write_bulk_callback() we queue up a wakeup call on the scheduler
160 * task queue. We still also invoke the wakeup directly since that
161 * squeezes a bit more performance out of the driver, and any lost
162 * race conditions will get cleaned up at the next scheduler run.
163 *
164 * NOTE: The problem also goes away if you comment out
165 * the two code lines in write_chan() where current->state
166 * is set to TASK_RUNNING just before calling driver.write() and to
167 * TASK_INTERRUPTIBLE immediately afterwards. This is why the
168 * problem did not show up with the 2.2 kernels -- they do not
169 * include that code.
170 *
171 * (5/16/2000) pberger and borchers
172 * -- Added timeouts to sleeps, to defend against lost wake ups.
173 * -- Handle transition to/from B0 baud rate in digi_set_termios.
174 *
175 * (5/13/2000) pberger and borchers
176 * -- All commands now sent on out of band port, using
177 * digi_write_oob_command.
178 * -- Get modem control signals whenever they change, support TIOCMGET/
179 * SET/BIS/BIC ioctls.
180 * -- digi_set_termios now supports parity, word size, stop bits, and
181 * receive enable.
182 * -- Cleaned up open and close, use digi_set_termios and
183 * digi_write_oob_command to set port parameters.
184 * -- Added digi_startup_device to start read chains on all ports.
185 * -- Write buffer is only used when count==1, to be sure put_char can
186 * write a char (unless the buffer is full).
187 *
188 * (5/10/2000) pberger and borchers
189 * -- Added MOD_INC_USE_COUNT/MOD_DEC_USE_COUNT calls on open/close.
190 * -- Fixed problem where the first incoming character is lost on
191 * port opens after the first close on that port. Now we keep
192 * the read_urb chain open until shutdown.
193 * -- Added more port conditioning calls in digi_open and digi_close.
194 * -- Convert port->active to a use count so that we can deal with multiple
195 * opens and closes properly.
196 * -- Fixed some problems with the locking code.
197 *
198 * (5/3/2000) pberger and borchers
199 * -- First alpha version of the driver--many known limitations and bugs.
200 *
201 *
202 * Locking and SMP
203 *
204 * - Each port, including the out-of-band port, has a lock used to
205 * serialize all access to the port's private structure.
206 * - The port lock is also used to serialize all writes and access to
207 * the port's URB.
208 * - The port lock is also used for the port write_wait condition
209 * variable. Holding the port lock will prevent a wake up on the
210 * port's write_wait; this can be used with cond_wait_... to be sure
211 * the wake up is not lost in a race when dropping the lock and
212 * sleeping waiting for the wakeup.
213 * - digi_write() does not sleep, since it is sometimes called on
214 * interrupt time.
215 * - digi_write_bulk_callback() and digi_read_bulk_callback() are
216 * called directly from interrupts. Hence spin_lock_irqsave()
217 * and spin_lock_irqrestore() are used in the rest of the code
218 * for any locks they acquire.
219 * - digi_write_bulk_callback() gets the port lock before waking up
220 * processes sleeping on the port write_wait. It also schedules
221 * wake ups so they happen from the scheduler, because the tty
222 * system can miss wake ups from interrupts.
223 * - All sleeps use a timeout of DIGI_RETRY_TIMEOUT before looping to
224 * recheck the condition they are sleeping on. This is defensive,
225 * in case a wake up is lost.
226 * - Following Documentation/DocBook/kernel-locking.pdf no spin locks
227 * are held when calling copy_to/from_user or printk.
228 *
229 * $Id: digi_acceleport.c,v 1.80.1.2 2000/11/02 05:45:08 root Exp $
230 */
231
232 #include <linux/config.h>
233 #include <linux/kernel.h>
234 #include <linux/sched.h>
235 #include <linux/signal.h>
236 #include <linux/errno.h>
237 #include <linux/poll.h>
238 #include <linux/init.h>
239 #include <linux/slab.h>
240 #include <linux/fcntl.h>
241 #include <linux/tty.h>
242 #include <linux/tty_driver.h>
243 #include <linux/tty_flip.h>
244 #include <linux/module.h>
245 #include <linux/spinlock.h>
246 #include <linux/tqueue.h>
247 #include <linux/usb.h>
248
249 #ifdef CONFIG_USB_SERIAL_DEBUG
250 static int debug = 1;
251 #else
252 static int debug;
253 #endif
254
255 #include "usb-serial.h"
256
257
258 /* Defines */
259
260 /*
261 * Version Information
262 */
263 #define DRIVER_VERSION "v1.80.1.2"
264 #define DRIVER_AUTHOR "Peter Berger <pberger@brimson.com>, Al Borchers <borchers@steinerpoint.com>"
265 #define DRIVER_DESC "Digi AccelePort USB-2/USB-4 Serial Converter driver"
266
267 /* port output buffer length -- must be <= transfer buffer length - 2 */
268 /* so we can be sure to send the full buffer in one urb */
269 #define DIGI_OUT_BUF_SIZE 8
270
271 /* port input buffer length -- must be >= transfer buffer length - 3 */
272 /* so we can be sure to hold at least one full buffer from one urb */
273 #define DIGI_IN_BUF_SIZE 64
274
275 /* retry timeout while sleeping */
276 #define DIGI_RETRY_TIMEOUT (HZ/10)
277
278 /* timeout while waiting for tty output to drain in close */
279 /* this delay is used twice in close, so the total delay could */
280 /* be twice this value */
281 #define DIGI_CLOSE_TIMEOUT (5*HZ)
282
283
284 /* AccelePort USB Defines */
285
286 /* ids */
287 #define DIGI_VENDOR_ID 0x05c5
288 #define DIGI_2_ID 0x0002 /* USB-2 */
289 #define DIGI_4_ID 0x0004 /* USB-4 */
290
291 /* commands
292 * "INB": can be used on the in-band endpoint
293 * "OOB": can be used on the out-of-band endpoint
294 */
295 #define DIGI_CMD_SET_BAUD_RATE 0 /* INB, OOB */
296 #define DIGI_CMD_SET_WORD_SIZE 1 /* INB, OOB */
297 #define DIGI_CMD_SET_PARITY 2 /* INB, OOB */
298 #define DIGI_CMD_SET_STOP_BITS 3 /* INB, OOB */
299 #define DIGI_CMD_SET_INPUT_FLOW_CONTROL 4 /* INB, OOB */
300 #define DIGI_CMD_SET_OUTPUT_FLOW_CONTROL 5 /* INB, OOB */
301 #define DIGI_CMD_SET_DTR_SIGNAL 6 /* INB, OOB */
302 #define DIGI_CMD_SET_RTS_SIGNAL 7 /* INB, OOB */
303 #define DIGI_CMD_READ_INPUT_SIGNALS 8 /* OOB */
304 #define DIGI_CMD_IFLUSH_FIFO 9 /* OOB */
305 #define DIGI_CMD_RECEIVE_ENABLE 10 /* INB, OOB */
306 #define DIGI_CMD_BREAK_CONTROL 11 /* INB, OOB */
307 #define DIGI_CMD_LOCAL_LOOPBACK 12 /* INB, OOB */
308 #define DIGI_CMD_TRANSMIT_IDLE 13 /* INB, OOB */
309 #define DIGI_CMD_READ_UART_REGISTER 14 /* OOB */
310 #define DIGI_CMD_WRITE_UART_REGISTER 15 /* INB, OOB */
311 #define DIGI_CMD_AND_UART_REGISTER 16 /* INB, OOB */
312 #define DIGI_CMD_OR_UART_REGISTER 17 /* INB, OOB */
313 #define DIGI_CMD_SEND_DATA 18 /* INB */
314 #define DIGI_CMD_RECEIVE_DATA 19 /* INB */
315 #define DIGI_CMD_RECEIVE_DISABLE 20 /* INB */
316 #define DIGI_CMD_GET_PORT_TYPE 21 /* OOB */
317
318 /* baud rates */
319 #define DIGI_BAUD_50 0
320 #define DIGI_BAUD_75 1
321 #define DIGI_BAUD_110 2
322 #define DIGI_BAUD_150 3
323 #define DIGI_BAUD_200 4
324 #define DIGI_BAUD_300 5
325 #define DIGI_BAUD_600 6
326 #define DIGI_BAUD_1200 7
327 #define DIGI_BAUD_1800 8
328 #define DIGI_BAUD_2400 9
329 #define DIGI_BAUD_4800 10
330 #define DIGI_BAUD_7200 11
331 #define DIGI_BAUD_9600 12
332 #define DIGI_BAUD_14400 13
333 #define DIGI_BAUD_19200 14
334 #define DIGI_BAUD_28800 15
335 #define DIGI_BAUD_38400 16
336 #define DIGI_BAUD_57600 17
337 #define DIGI_BAUD_76800 18
338 #define DIGI_BAUD_115200 19
339 #define DIGI_BAUD_153600 20
340 #define DIGI_BAUD_230400 21
341 #define DIGI_BAUD_460800 22
342
343 /* arguments */
344 #define DIGI_WORD_SIZE_5 0
345 #define DIGI_WORD_SIZE_6 1
346 #define DIGI_WORD_SIZE_7 2
347 #define DIGI_WORD_SIZE_8 3
348
349 #define DIGI_PARITY_NONE 0
350 #define DIGI_PARITY_ODD 1
351 #define DIGI_PARITY_EVEN 2
352 #define DIGI_PARITY_MARK 3
353 #define DIGI_PARITY_SPACE 4
354
355 #define DIGI_STOP_BITS_1 0
356 #define DIGI_STOP_BITS_2 1
357
358 #define DIGI_INPUT_FLOW_CONTROL_XON_XOFF 1
359 #define DIGI_INPUT_FLOW_CONTROL_RTS 2
360 #define DIGI_INPUT_FLOW_CONTROL_DTR 4
361
362 #define DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF 1
363 #define DIGI_OUTPUT_FLOW_CONTROL_CTS 2
364 #define DIGI_OUTPUT_FLOW_CONTROL_DSR 4
365
366 #define DIGI_DTR_INACTIVE 0
367 #define DIGI_DTR_ACTIVE 1
368 #define DIGI_DTR_INPUT_FLOW_CONTROL 2
369
370 #define DIGI_RTS_INACTIVE 0
371 #define DIGI_RTS_ACTIVE 1
372 #define DIGI_RTS_INPUT_FLOW_CONTROL 2
373 #define DIGI_RTS_TOGGLE 3
374
375 #define DIGI_FLUSH_TX 1
376 #define DIGI_FLUSH_RX 2
377 #define DIGI_RESUME_TX 4 /* clears xoff condition */
378
379 #define DIGI_TRANSMIT_NOT_IDLE 0
380 #define DIGI_TRANSMIT_IDLE 1
381
382 #define DIGI_DISABLE 0
383 #define DIGI_ENABLE 1
384
385 #define DIGI_DEASSERT 0
386 #define DIGI_ASSERT 1
387
388 /* in band status codes */
389 #define DIGI_OVERRUN_ERROR 4
390 #define DIGI_PARITY_ERROR 8
391 #define DIGI_FRAMING_ERROR 16
392 #define DIGI_BREAK_ERROR 32
393
394 /* out of band status */
395 #define DIGI_NO_ERROR 0
396 #define DIGI_BAD_FIRST_PARAMETER 1
397 #define DIGI_BAD_SECOND_PARAMETER 2
398 #define DIGI_INVALID_LINE 3
399 #define DIGI_INVALID_OPCODE 4
400
401 /* input signals */
402 #define DIGI_READ_INPUT_SIGNALS_SLOT 1
403 #define DIGI_READ_INPUT_SIGNALS_ERR 2
404 #define DIGI_READ_INPUT_SIGNALS_BUSY 4
405 #define DIGI_READ_INPUT_SIGNALS_PE 8
406 #define DIGI_READ_INPUT_SIGNALS_CTS 16
407 #define DIGI_READ_INPUT_SIGNALS_DSR 32
408 #define DIGI_READ_INPUT_SIGNALS_RI 64
409 #define DIGI_READ_INPUT_SIGNALS_DCD 128
410
411
412 /* Structures */
413
414 typedef struct digi_serial {
415 spinlock_t ds_serial_lock;
416 struct usb_serial_port *ds_oob_port; /* out-of-band port */
417 int ds_oob_port_num; /* index of out-of-band port */
418 int ds_device_started;
419 } digi_serial_t;
420
421 typedef struct digi_port {
422 spinlock_t dp_port_lock;
423 int dp_port_num;
424 int dp_out_buf_len;
425 unsigned char dp_out_buf[DIGI_OUT_BUF_SIZE];
426 int dp_in_buf_len;
427 unsigned char dp_in_buf[DIGI_IN_BUF_SIZE];
428 unsigned char dp_in_flag_buf[DIGI_IN_BUF_SIZE];
429 int dp_write_urb_in_use;
430 unsigned int dp_modem_signals;
431 wait_queue_head_t dp_modem_change_wait;
432 int dp_open_count; /* inc on open, dec on close */
433 int dp_transmit_idle;
434 wait_queue_head_t dp_transmit_idle_wait;
435 int dp_throttled;
436 int dp_throttle_restart;
437 wait_queue_head_t dp_flush_wait;
438 int dp_in_close; /* close in progress */
439 wait_queue_head_t dp_close_wait; /* wait queue for close */
440 struct tq_struct dp_wakeup_task;
441 } digi_port_t;
442
443
444 /* Local Function Declarations */
445
446 static void digi_wakeup_write( struct usb_serial_port *port );
447 static void digi_wakeup_write_lock( struct usb_serial_port *port );
448 static int digi_write_oob_command( struct usb_serial_port *port,
449 unsigned char *buf, int count, int interruptible );
450 static int digi_write_inb_command( struct usb_serial_port *port,
451 unsigned char *buf, int count, unsigned long timeout );
452 static int digi_set_modem_signals( struct usb_serial_port *port,
453 unsigned int modem_signals, int interruptible );
454 static int digi_transmit_idle( struct usb_serial_port *port,
455 unsigned long timeout );
456 static void digi_rx_throttle (struct usb_serial_port *port);
457 static void digi_rx_unthrottle (struct usb_serial_port *port);
458 static void digi_set_termios( struct usb_serial_port *port,
459 struct termios *old_termios );
460 static void digi_break_ctl( struct usb_serial_port *port, int break_state );
461 static int digi_ioctl( struct usb_serial_port *port, struct file *file,
462 unsigned int cmd, unsigned long arg );
463 static int digi_write( struct usb_serial_port *port, int from_user,
464 const unsigned char *buf, int count );
465 static void digi_write_bulk_callback( struct urb *urb );
466 static int digi_write_room( struct usb_serial_port *port );
467 static int digi_chars_in_buffer( struct usb_serial_port *port );
468 static int digi_open( struct usb_serial_port *port, struct file *filp );
469 static void digi_close( struct usb_serial_port *port, struct file *filp );
470 static int digi_startup_device( struct usb_serial *serial );
471 static int digi_startup( struct usb_serial *serial );
472 static void digi_shutdown( struct usb_serial *serial );
473 static void digi_read_bulk_callback( struct urb *urb );
474 static int digi_read_inb_callback( struct urb *urb );
475 static int digi_read_oob_callback( struct urb *urb );
476
477
478 /* Statics */
479
480 static __devinitdata struct usb_device_id id_table_combined [] = {
481 { USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
482 { USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
483 { } /* Terminating entry */
484 };
485
486 static __devinitdata struct usb_device_id id_table_2 [] = {
487 { USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
488 { } /* Terminating entry */
489 };
490
491 static __devinitdata struct usb_device_id id_table_4 [] = {
492 { USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
493 { } /* Terminating entry */
494 };
495
496 MODULE_DEVICE_TABLE (usb, id_table_combined);
497
498 /* device info needed for the Digi serial converter */
499
500 static struct usb_serial_device_type digi_acceleport_2_device = {
501 name: "Digi USB",
502 id_table: id_table_2,
503 needs_interrupt_in: DONT_CARE,
504 needs_bulk_in: MUST_HAVE,
505 needs_bulk_out: MUST_HAVE,
506 num_interrupt_in: 0,
507 num_bulk_in: 4,
508 num_bulk_out: 4,
509 num_ports: 3,
510 open: digi_open,
511 close: digi_close,
512 write: digi_write,
513 write_room: digi_write_room,
514 write_bulk_callback: digi_write_bulk_callback,
515 read_bulk_callback: digi_read_bulk_callback,
516 chars_in_buffer: digi_chars_in_buffer,
517 throttle: digi_rx_throttle,
518 unthrottle: digi_rx_unthrottle,
519 ioctl: digi_ioctl,
520 set_termios: digi_set_termios,
521 break_ctl: digi_break_ctl,
522 startup: digi_startup,
523 shutdown: digi_shutdown,
524 };
525
526 static struct usb_serial_device_type digi_acceleport_4_device = {
527 name: "Digi USB",
528 id_table: id_table_4,
529 needs_interrupt_in: DONT_CARE,
530 needs_bulk_in: MUST_HAVE,
531 needs_bulk_out: MUST_HAVE,
532 num_interrupt_in: 0,
533 num_bulk_in: 5,
534 num_bulk_out: 5,
535 num_ports: 4,
536 open: digi_open,
537 close: digi_close,
538 write: digi_write,
539 write_room: digi_write_room,
540 write_bulk_callback: digi_write_bulk_callback,
541 read_bulk_callback: digi_read_bulk_callback,
542 chars_in_buffer: digi_chars_in_buffer,
543 throttle: digi_rx_throttle,
544 unthrottle: digi_rx_unthrottle,
545 ioctl: digi_ioctl,
546 set_termios: digi_set_termios,
547 break_ctl: digi_break_ctl,
548 startup: digi_startup,
549 shutdown: digi_shutdown,
550 };
551
552
553 /* Functions */
554
555 /*
556 * Cond Wait Interruptible Timeout Irqrestore
557 *
558 * Do spin_unlock_irqrestore and interruptible_sleep_on_timeout
559 * so that wake ups are not lost if they occur between the unlock
560 * and the sleep. In other words, spin_lock_irqrestore and
561 * interruptible_sleep_on_timeout are "atomic" with respect to
562 * wake ups. This is used to implement condition variables.
563 */
564
565 static inline long cond_wait_interruptible_timeout_irqrestore(
566 wait_queue_head_t *q, long timeout,
567 spinlock_t *lock, unsigned long flags )
568 {
569
570 wait_queue_t wait;
571
572
573 init_waitqueue_entry( &wait, current );
574
575 set_current_state( TASK_INTERRUPTIBLE );
576
577 add_wait_queue( q, &wait );
578
579 spin_unlock_irqrestore( lock, flags );
580
581 timeout = schedule_timeout(timeout);
582
583 set_current_state( TASK_RUNNING );
584
585 remove_wait_queue( q, &wait );
586
587 return( timeout );
588
589 }
590
591
592 /*
593 * Digi Wakeup Write
594 *
595 * Wake up port, line discipline, and tty processes sleeping
596 * on writes.
597 */
598
599 static void digi_wakeup_write_lock( struct usb_serial_port *port )
600 {
601
602 unsigned long flags;
603 digi_port_t *priv = (digi_port_t *)(port->private);
604
605
606 spin_lock_irqsave( &priv->dp_port_lock, flags );
607 digi_wakeup_write( port );
608 spin_unlock_irqrestore( &priv->dp_port_lock, flags );
609 MOD_DEC_USE_COUNT;
610 }
611
612 static void digi_wakeup_write( struct usb_serial_port *port )
613 {
614
615 struct tty_struct *tty = port->tty;
616
617
618 /* wake up port processes */
619 wake_up_interruptible( &port->write_wait );
620
621 /* wake up line discipline */
622 if( (tty->flags & (1 << TTY_DO_WRITE_WAKEUP))
623 && tty->ldisc.write_wakeup )
624 (tty->ldisc.write_wakeup)(tty);
625
626 /* wake up other tty processes */
627 wake_up_interruptible( &tty->write_wait );
628 /* For 2.2.16 backport -- wake_up_interruptible( &tty->poll_wait ); */
629
630 }
631
632
633 /*
634 * Digi Write OOB Command
635 *
636 * Write commands on the out of band port. Commands are 4
637 * bytes each, multiple commands can be sent at once, and
638 * no command will be split across USB packets. Returns 0
639 * if successful, -EINTR if interrupted while sleeping and
640 * the interruptible flag is true, or a negative error
641 * returned by usb_submit_urb.
642 */
643
644 static int digi_write_oob_command( struct usb_serial_port *port,
645 unsigned char *buf, int count, int interruptible )
646 {
647
648 int ret = 0;
649 int len;
650 struct usb_serial_port *oob_port = (struct usb_serial_port *)((digi_serial_t *)port->serial->private)->ds_oob_port;
651 digi_port_t *oob_priv = (digi_port_t *)oob_port->private;
652 unsigned long flags = 0;
653
654
655 dbg( "digi_write_oob_command: TOP: port=%d, count=%d", oob_priv->dp_port_num, count );
656
657 spin_lock_irqsave( &oob_priv->dp_port_lock, flags );
658
659 while( count > 0 ) {
660
661 while( oob_port->write_urb->status == -EINPROGRESS
662 || oob_priv->dp_write_urb_in_use ) {
663 cond_wait_interruptible_timeout_irqrestore(
664 &oob_port->write_wait, DIGI_RETRY_TIMEOUT,
665 &oob_priv->dp_port_lock, flags );
666 if( interruptible && signal_pending(current) ) {
667 return( -EINTR );
668 }
669 spin_lock_irqsave( &oob_priv->dp_port_lock, flags );
670 }
671
672 /* len must be a multiple of 4, so commands are not split */
673 len = min(count, oob_port->bulk_out_size );
674 if( len > 4 )
675 len &= ~3;
676
677 memcpy( oob_port->write_urb->transfer_buffer, buf, len );
678 oob_port->write_urb->transfer_buffer_length = len;
679 oob_port->write_urb->dev = port->serial->dev;
680
681 if( (ret=usb_submit_urb(oob_port->write_urb)) == 0 ) {
682 oob_priv->dp_write_urb_in_use = 1;
683 count -= len;
684 buf += len;
685 }
686
687 }
688
689 spin_unlock_irqrestore( &oob_priv->dp_port_lock, flags );
690
691 if( ret ) {
692 err( __FUNCTION__ ": usb_submit_urb failed, ret=%d",
693 ret );
694 }
695
696 return( ret );
697
698 }
699
700
701 /*
702 * Digi Write In Band Command
703 *
704 * Write commands on the given port. Commands are 4
705 * bytes each, multiple commands can be sent at once, and
706 * no command will be split across USB packets. If timeout
707 * is non-zero, write in band command will return after
708 * waiting unsuccessfully for the URB status to clear for
709 * timeout ticks. Returns 0 if successful, or a negative
710 * error returned by digi_write.
711 */
712
713 static int digi_write_inb_command( struct usb_serial_port *port,
714 unsigned char *buf, int count, unsigned long timeout )
715 {
716
717 int ret = 0;
718 int len;
719 digi_port_t *priv = (digi_port_t *)(port->private);
720 unsigned char *data = port->write_urb->transfer_buffer;
721 unsigned long flags = 0;
722
723
724 dbg( "digi_write_inb_command: TOP: port=%d, count=%d", priv->dp_port_num,
725 count );
726
727 if( timeout )
728 timeout += jiffies;
729 else
730 timeout = ULONG_MAX;
731
732 spin_lock_irqsave( &priv->dp_port_lock, flags );
733
734 while( count > 0 && ret == 0 ) {
735
736 while( (port->write_urb->status == -EINPROGRESS
737 || priv->dp_write_urb_in_use) && jiffies < timeout ) {
738 cond_wait_interruptible_timeout_irqrestore(
739 &port->write_wait, DIGI_RETRY_TIMEOUT,
740 &priv->dp_port_lock, flags );
741 if( signal_pending(current) ) {
742 return( -EINTR );
743 }
744 spin_lock_irqsave( &priv->dp_port_lock, flags );
745 }
746
747 /* len must be a multiple of 4 and small enough to */
748 /* guarantee the write will send buffered data first, */
749 /* so commands are in order with data and not split */
750 len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len );
751 if( len > 4 )
752 len &= ~3;
753
754 /* write any buffered data first */
755 if( priv->dp_out_buf_len > 0 ) {
756 data[0] = DIGI_CMD_SEND_DATA;
757 data[1] = priv->dp_out_buf_len;
758 memcpy( data+2, priv->dp_out_buf,
759 priv->dp_out_buf_len );
760 memcpy( data+2+priv->dp_out_buf_len, buf, len );
761 port->write_urb->transfer_buffer_length
762 = priv->dp_out_buf_len+2+len;
763 } else {
764 memcpy( data, buf, len );
765 port->write_urb->transfer_buffer_length = len;
766 }
767 port->write_urb->dev = port->serial->dev;
768
769 if( (ret=usb_submit_urb(port->write_urb)) == 0 ) {
770 priv->dp_write_urb_in_use = 1;
771 priv->dp_out_buf_len = 0;
772 count -= len;
773 buf += len;
774 }
775
776 }
777
778 spin_unlock_irqrestore( &priv->dp_port_lock, flags );
779
780 if( ret ) {
781 err( __FUNCTION__ ": usb_submit_urb failed, ret=%d, port=%d",
782 ret, priv->dp_port_num );
783 }
784
785 return( ret );
786
787 }
788
789
790 /*
791 * Digi Set Modem Signals
792 *
793 * Sets or clears DTR and RTS on the port, according to the
794 * modem_signals argument. Use TIOCM_DTR and TIOCM_RTS flags
795 * for the modem_signals argument. Returns 0 if successful,
796 * -EINTR if interrupted while sleeping, or a non-zero error
797 * returned by usb_submit_urb.
798 */
799
800 static int digi_set_modem_signals( struct usb_serial_port *port,
801 unsigned int modem_signals, int interruptible )
802 {
803
804 int ret;
805 digi_port_t *port_priv = (digi_port_t *)port->private;
806 struct usb_serial_port *oob_port = (struct usb_serial_port *)((digi_serial_t *)port->serial->private)->ds_oob_port;
807 digi_port_t *oob_priv = (digi_port_t *)oob_port->private;
808 unsigned char *data = oob_port->write_urb->transfer_buffer;
809 unsigned long flags = 0;
810
811
812 dbg( "digi_set_modem_signals: TOP: port=%d, modem_signals=0x%x",
813 port_priv->dp_port_num, modem_signals );
814
815 spin_lock_irqsave( &oob_priv->dp_port_lock, flags );
816 spin_lock( &port_priv->dp_port_lock );
817
818 while( oob_port->write_urb->status == -EINPROGRESS
819 || oob_priv->dp_write_urb_in_use ) {
820 spin_unlock( &port_priv->dp_port_lock );
821 cond_wait_interruptible_timeout_irqrestore(
822 &oob_port->write_wait, DIGI_RETRY_TIMEOUT,
823 &oob_priv->dp_port_lock, flags );
824 if( interruptible && signal_pending(current) ) {
825 return( -EINTR );
826 }
827 spin_lock_irqsave( &oob_priv->dp_port_lock, flags );
828 spin_lock( &port_priv->dp_port_lock );
829 }
830
831 data[0] = DIGI_CMD_SET_DTR_SIGNAL;
832 data[1] = port_priv->dp_port_num;
833 data[2] = (modem_signals&TIOCM_DTR) ?
834 DIGI_DTR_ACTIVE : DIGI_DTR_INACTIVE;
835 data[3] = 0;
836
837 data[4] = DIGI_CMD_SET_RTS_SIGNAL;
838 data[5] = port_priv->dp_port_num;
839 data[6] = (modem_signals&TIOCM_RTS) ?
840 DIGI_RTS_ACTIVE : DIGI_RTS_INACTIVE;
841 data[7] = 0;
842
843 oob_port->write_urb->transfer_buffer_length = 8;
844 oob_port->write_urb->dev = port->serial->dev;
845
846 if( (ret=usb_submit_urb(oob_port->write_urb)) == 0 ) {
847 oob_priv->dp_write_urb_in_use = 1;
848 port_priv->dp_modem_signals =
849 (port_priv->dp_modem_signals&~(TIOCM_DTR|TIOCM_RTS))
850 | (modem_signals&(TIOCM_DTR|TIOCM_RTS));
851 }
852
853 spin_unlock( &port_priv->dp_port_lock );
854 spin_unlock_irqrestore( &oob_priv->dp_port_lock, flags );
855
856 if( ret ) {
857 err( __FUNCTION__ ": usb_submit_urb failed, ret=%d",
858 ret );
859 }
860
861 return( ret );
862
863 }
864
865
866 /*
867 * Digi Transmit Idle
868 *
869 * Digi transmit idle waits, up to timeout ticks, for the transmitter
870 * to go idle. It returns 0 if successful or a negative error.
871 *
872 * There are race conditions here if more than one process is calling
873 * digi_transmit_idle on the same port at the same time. However, this
874 * is only called from close, and only one process can be in close on a
875 * port at a time, so its ok.
876 */
877
878 static int digi_transmit_idle( struct usb_serial_port *port,
879 unsigned long timeout )
880 {
881
882 int ret;
883 unsigned char buf[2];
884 digi_port_t *priv = (digi_port_t *)(port->private);
885 unsigned long flags = 0;
886
887
888 spin_lock_irqsave( &priv->dp_port_lock, flags );
889 priv->dp_transmit_idle = 0;
890 spin_unlock_irqrestore( &priv->dp_port_lock, flags );
891
892 buf[0] = DIGI_CMD_TRANSMIT_IDLE;
893 buf[1] = 0;
894
895 timeout += jiffies;
896
897 if( (ret=digi_write_inb_command( port, buf, 2, timeout-jiffies )) != 0 )
898 return( ret );
899
900 spin_lock_irqsave( &priv->dp_port_lock, flags );
901
902 while( jiffies < timeout && !priv->dp_transmit_idle ) {
903 cond_wait_interruptible_timeout_irqrestore(
904 &priv->dp_transmit_idle_wait, DIGI_RETRY_TIMEOUT,
905 &priv->dp_port_lock, flags );
906 if( signal_pending(current) ) {
907 return( -EINTR );
908 }
909 spin_lock_irqsave( &priv->dp_port_lock, flags );
910 }
911
912 priv->dp_transmit_idle = 0;
913 spin_unlock_irqrestore( &priv->dp_port_lock, flags );
914
915 return( 0 );
916
917 }
918
919
920 static void digi_rx_throttle( struct usb_serial_port *port )
921 {
922
923 unsigned long flags;
924 digi_port_t *priv = (digi_port_t *)(port->private);
925
926
927 dbg( "digi_rx_throttle: TOP: port=%d", priv->dp_port_num );
928
929 /* stop receiving characters by not resubmitting the read urb */
930 spin_lock_irqsave( &priv->dp_port_lock, flags );
931 priv->dp_throttled = 1;
932 priv->dp_throttle_restart = 0;
933 priv->dp_in_buf_len = 0;
934 spin_unlock_irqrestore( &priv->dp_port_lock, flags );
935
936 }
937
938
939 static void digi_rx_unthrottle( struct usb_serial_port *port )
940 {
941
942 int ret = 0;
943 int len;
944 unsigned long flags;
945 digi_port_t *priv = (digi_port_t *)(port->private);
946 struct tty_struct *tty = port->tty;
947
948
949 dbg( "digi_rx_unthrottle: TOP: port=%d", priv->dp_port_num );
950
951 spin_lock_irqsave( &priv->dp_port_lock, flags );
952
953 /* send any buffered chars from throttle time on to tty subsystem */
954 len = min(priv->dp_in_buf_len, TTY_FLIPBUF_SIZE - tty->flip.count );
955 if( len > 0 ) {
956 memcpy( tty->flip.char_buf_ptr, priv->dp_in_buf, len );
957 memcpy( tty->flip.flag_buf_ptr, priv->dp_in_flag_buf, len );
958 tty->flip.char_buf_ptr += len;
959 tty->flip.flag_buf_ptr += len;
960 tty->flip.count += len;
961 tty_flip_buffer_push( tty );
962 }
963
964 /* restart read chain */
965 if( priv->dp_throttle_restart ) {
966 port->read_urb->dev = port->serial->dev;
967 ret = usb_submit_urb( port->read_urb );
968 }
969
970 /* turn throttle off */
971 priv->dp_throttled = 0;
972 priv->dp_in_buf_len = 0;
973 priv->dp_throttle_restart = 0;
974
975 spin_unlock_irqrestore( &priv->dp_port_lock, flags );
976
977 if( ret ) {
978 err( __FUNCTION__ ": usb_submit_urb failed, ret=%d, port=%d",
979 ret, priv->dp_port_num );
980 }
981
982 }
983
984
985 static void digi_set_termios( struct usb_serial_port *port,
986 struct termios *old_termios )
987 {
988
989 digi_port_t *priv = (digi_port_t *)(port->private);
990 unsigned int iflag = port->tty->termios->c_iflag;
991 unsigned int cflag = port->tty->termios->c_cflag;
992 unsigned int old_iflag = old_termios->c_iflag;
993 unsigned int old_cflag = old_termios->c_cflag;
994 unsigned char buf[32];
995 unsigned int modem_signals;
996 int arg,ret;
997 int i = 0;
998
999
1000 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 );
1001
1002 /* set baud rate */
1003 if( (cflag&CBAUD) != (old_cflag&CBAUD) ) {
1004
1005 arg = -1;
1006
1007 /* reassert DTR and (maybe) RTS on transition from B0 */
1008 if( (old_cflag&CBAUD) == B0 ) {
1009 /* don't set RTS if using hardware flow control */
1010 /* and throttling input */
1011 modem_signals = TIOCM_DTR;
1012 if( !(port->tty->termios->c_cflag & CRTSCTS) ||
1013 !test_bit(TTY_THROTTLED, &port->tty->flags) ) {
1014 modem_signals |= TIOCM_RTS;
1015 }
1016 digi_set_modem_signals( port, modem_signals, 1 );
1017 }
1018
1019 switch( (cflag&CBAUD) ) {
1020 /* drop DTR and RTS on transition to B0 */
1021 case B0: digi_set_modem_signals( port, 0, 1 ); break;
1022 case B50: arg = DIGI_BAUD_50; break;
1023 case B75: arg = DIGI_BAUD_75; break;
1024 case B110: arg = DIGI_BAUD_110; break;
1025 case B150: arg = DIGI_BAUD_150; break;
1026 case B200: arg = DIGI_BAUD_200; break;
1027 case B300: arg = DIGI_BAUD_300; break;
1028 case B600: arg = DIGI_BAUD_600; break;
1029 case B1200: arg = DIGI_BAUD_1200; break;
1030 case B1800: arg = DIGI_BAUD_1800; break;
1031 case B2400: arg = DIGI_BAUD_2400; break;
1032 case B4800: arg = DIGI_BAUD_4800; break;
1033 case B9600: arg = DIGI_BAUD_9600; break;
1034 case B19200: arg = DIGI_BAUD_19200; break;
1035 case B38400: arg = DIGI_BAUD_38400; break;
1036 case B57600: arg = DIGI_BAUD_57600; break;
1037 case B115200: arg = DIGI_BAUD_115200; break;
1038 case B230400: arg = DIGI_BAUD_230400; break;
1039 case B460800: arg = DIGI_BAUD_460800; break;
1040 default:
1041 dbg( "digi_set_termios: can't handle baud rate 0x%x",
1042 (cflag&CBAUD) );
1043 break;
1044 }
1045
1046 if( arg != -1 ) {
1047 buf[i++] = DIGI_CMD_SET_BAUD_RATE;
1048 buf[i++] = priv->dp_port_num;
1049 buf[i++] = arg;
1050 buf[i++] = 0;
1051 }
1052
1053 }
1054
1055 /* set parity */
1056 if( (cflag&(PARENB|PARODD)) != (old_cflag&(PARENB|PARODD)) ) {
1057
1058 if( (cflag&PARENB) ) {
1059 if( (cflag&PARODD) )
1060 arg = DIGI_PARITY_ODD;
1061 else
1062 arg = DIGI_PARITY_EVEN;
1063 } else {
1064 arg = DIGI_PARITY_NONE;
1065 }
1066
1067 buf[i++] = DIGI_CMD_SET_PARITY;
1068 buf[i++] = priv->dp_port_num;
1069 buf[i++] = arg;
1070 buf[i++] = 0;
1071
1072 }
1073
1074 /* set word size */
1075 if( (cflag&CSIZE) != (old_cflag&CSIZE) ) {
1076
1077 arg = -1;
1078
1079 switch( (cflag&CSIZE) ) {
1080 case CS5: arg = DIGI_WORD_SIZE_5; break;
1081 case CS6: arg = DIGI_WORD_SIZE_6; break;
1082 case CS7: arg = DIGI_WORD_SIZE_7; break;
1083 case CS8: arg = DIGI_WORD_SIZE_8; break;
1084 default:
1085 dbg( "digi_set_termios: can't handle word size %d",
1086 (cflag&CSIZE) );
1087 break;
1088 }
1089
1090 if( arg != -1 ) {
1091 buf[i++] = DIGI_CMD_SET_WORD_SIZE;
1092 buf[i++] = priv->dp_port_num;
1093 buf[i++] = arg;
1094 buf[i++] = 0;
1095 }
1096
1097 }
1098
1099 /* set stop bits */
1100 if( (cflag&CSTOPB) != (old_cflag&CSTOPB) ) {
1101
1102 if( (cflag&CSTOPB) )
1103 arg = DIGI_STOP_BITS_2;
1104 else
1105 arg = DIGI_STOP_BITS_1;
1106
1107 buf[i++] = DIGI_CMD_SET_STOP_BITS;
1108 buf[i++] = priv->dp_port_num;
1109 buf[i++] = arg;
1110 buf[i++] = 0;
1111
1112 }
1113
1114 /* set input flow control */
1115 if( (iflag&IXOFF) != (old_iflag&IXOFF)
1116 || (cflag&CRTSCTS) != (old_cflag&CRTSCTS) ) {
1117
1118 arg = 0;
1119
1120 if( (iflag&IXOFF) )
1121 arg |= DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
1122 else
1123 arg &= ~DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
1124
1125 if( (cflag&CRTSCTS) ) {
1126
1127 arg |= DIGI_INPUT_FLOW_CONTROL_RTS;
1128
1129 /* On USB-4 it is necessary to assert RTS prior */
1130 /* to selecting RTS input flow control. */
1131 buf[i++] = DIGI_CMD_SET_RTS_SIGNAL;
1132 buf[i++] = priv->dp_port_num;
1133 buf[i++] = DIGI_RTS_ACTIVE;
1134 buf[i++] = 0;
1135
1136 } else {
1137 arg &= ~DIGI_INPUT_FLOW_CONTROL_RTS;
1138 }
1139
1140 buf[i++] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
1141 buf[i++] = priv->dp_port_num;
1142 buf[i++] = arg;
1143 buf[i++] = 0;
1144
1145 }
1146
1147 /* set output flow control */
1148 if( (iflag&IXON) != (old_iflag&IXON)
1149 || (cflag&CRTSCTS) != (old_cflag&CRTSCTS) ) {
1150
1151 arg = 0;
1152
1153 if( (iflag&IXON) )
1154 arg |= DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
1155 else
1156 arg &= ~DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
1157
1158 if( (cflag&CRTSCTS) ) {
1159 arg |= DIGI_OUTPUT_FLOW_CONTROL_CTS;
1160 } else {
1161 arg &= ~DIGI_OUTPUT_FLOW_CONTROL_CTS;
1162 port->tty->hw_stopped = 0;
1163 }
1164
1165 buf[i++] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
1166 buf[i++] = priv->dp_port_num;
1167 buf[i++] = arg;
1168 buf[i++] = 0;
1169
1170 }
1171
1172 /* set receive enable/disable */
1173 if( (cflag&CREAD) != (old_cflag&CREAD) ) {
1174
1175 if( (cflag&CREAD) )
1176 arg = DIGI_ENABLE;
1177 else
1178 arg = DIGI_DISABLE;
1179
1180 buf[i++] = DIGI_CMD_RECEIVE_ENABLE;
1181 buf[i++] = priv->dp_port_num;
1182 buf[i++] = arg;
1183 buf[i++] = 0;
1184
1185 }
1186
1187 if( (ret=digi_write_oob_command( port, buf, i, 1 )) != 0 )
1188 dbg( "digi_set_termios: write oob failed, ret=%d", ret );
1189
1190 }
1191
1192
1193 static void digi_break_ctl( struct usb_serial_port *port, int break_state )
1194 {
1195
1196 unsigned char buf[4];
1197
1198
1199 buf[0] = DIGI_CMD_BREAK_CONTROL;
1200 buf[1] = 2; /* length */
1201 buf[2] = break_state ? 1 : 0;
1202 buf[3] = 0; /* pad */
1203
1204 digi_write_inb_command( port, buf, 4, 0 );
1205
1206 }
1207
1208
1209 static int digi_ioctl( struct usb_serial_port *port, struct file *file,
1210 unsigned int cmd, unsigned long arg )
1211 {
1212
1213 digi_port_t *priv = (digi_port_t *)(port->private);
1214 unsigned int val;
1215 unsigned long flags = 0;
1216
1217
1218 dbg( "digi_ioctl: TOP: port=%d, cmd=0x%x", priv->dp_port_num, cmd );
1219
1220 switch (cmd) {
1221
1222 case TIOCMGET:
1223 spin_lock_irqsave( &priv->dp_port_lock, flags );
1224 val = priv->dp_modem_signals;
1225 spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1226 if( copy_to_user((unsigned int *)arg, &val, sizeof(int)) )
1227 return( -EFAULT );
1228 return( 0 );
1229
1230 case TIOCMSET:
1231 case TIOCMBIS:
1232 case TIOCMBIC:
1233 if( copy_from_user(&val, (unsigned int *)arg, sizeof(int)) )
1234 return( -EFAULT );
1235 spin_lock_irqsave( &priv->dp_port_lock, flags );
1236 if( cmd == TIOCMBIS )
1237 val = priv->dp_modem_signals | val;
1238 else if( cmd == TIOCMBIC )
1239 val = priv->dp_modem_signals & ~val;
1240 spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1241 return( digi_set_modem_signals( port, val, 1 ) );
1242
1243 case TIOCMIWAIT:
1244 /* wait for any of the 4 modem inputs (DCD,RI,DSR,CTS)*/
1245 /* TODO */
1246 return( 0 );
1247
1248 case TIOCGICOUNT:
1249 /* return count of modemline transitions */
1250 /* TODO */
1251 return 0;
1252
1253 }
1254
1255 return( -ENOIOCTLCMD );
1256
1257 }
1258
1259
1260 static int digi_write( struct usb_serial_port *port, int from_user,
1261 const unsigned char *buf, int count )
1262 {
1263
1264 int ret,data_len,new_len;
1265 digi_port_t *priv = (digi_port_t *)(port->private);
1266 unsigned char *data = port->write_urb->transfer_buffer;
1267 unsigned char user_buf[64]; /* 64 bytes is max USB bulk packet */
1268 unsigned long flags = 0;
1269
1270
1271 dbg( "digi_write: TOP: port=%d, count=%d, from_user=%d, in_interrupt=%d",
1272 priv->dp_port_num, count, from_user, in_interrupt() );
1273
1274 /* copy user data (which can sleep) before getting spin lock */
1275 count = min( count, port->bulk_out_size-2 );
1276 count = min( 64, count);
1277 if( from_user && copy_from_user( user_buf, buf, count ) ) {
1278 return( -EFAULT );
1279 }
1280
1281 /* be sure only one write proceeds at a time */
1282 /* there are races on the port private buffer */
1283 /* and races to check write_urb->status */
1284 spin_lock_irqsave( &priv->dp_port_lock, flags );
1285
1286 /* wait for urb status clear to submit another urb */
1287 if( port->write_urb->status == -EINPROGRESS
1288 || priv->dp_write_urb_in_use ) {
1289
1290 /* buffer data if count is 1 (probably put_char) if possible */
1291 if( count == 1 && priv->dp_out_buf_len < DIGI_OUT_BUF_SIZE ) {
1292 priv->dp_out_buf[priv->dp_out_buf_len++]
1293 = *(from_user ? user_buf : buf);
1294 new_len = 1;
1295 } else {
1296 new_len = 0;
1297 }
1298
1299 spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1300
1301 return( new_len );
1302
1303 }
1304
1305 /* allow space for any buffered data and for new data, up to */
1306 /* transfer buffer size - 2 (for command and length bytes) */
1307 new_len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
1308 data_len = new_len + priv->dp_out_buf_len;
1309
1310 if( data_len == 0 ) {
1311 spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1312 return( 0 );
1313 }
1314
1315 port->write_urb->transfer_buffer_length = data_len+2;
1316 port->write_urb->dev = port->serial->dev;
1317
1318 *data++ = DIGI_CMD_SEND_DATA;
1319 *data++ = data_len;
1320
1321 /* copy in buffered data first */
1322 memcpy( data, priv->dp_out_buf, priv->dp_out_buf_len );
1323 data += priv->dp_out_buf_len;
1324
1325 /* copy in new data */
1326 memcpy( data, from_user ? user_buf : buf, new_len );
1327
1328 if( (ret=usb_submit_urb(port->write_urb)) == 0 ) {
1329 priv->dp_write_urb_in_use = 1;
1330 ret = new_len;
1331 priv->dp_out_buf_len = 0;
1332 }
1333
1334 /* return length of new data written, or error */
1335 spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1336 if( ret < 0 ) {
1337 err( __FUNCTION__ ": usb_submit_urb failed, ret=%d, port=%d",
1338 ret, priv->dp_port_num );
1339 }
1340
1341 dbg( "digi_write: returning %d", ret );
1342 return( ret );
1343
1344 }
1345
1346
1347 static void digi_write_bulk_callback( struct urb *urb )
1348 {
1349
1350 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1351 struct usb_serial *serial;
1352 digi_port_t *priv;
1353 int ret = 0;
1354
1355
1356 dbg( "digi_write_bulk_callback: TOP, urb->status=%d", urb->status );
1357
1358 /* port and serial sanity check */
1359 if( port == NULL || (priv=(digi_port_t *)(port->private)) == NULL ) {
1360 err( __FUNCTION__ ": port or port->private is NULL, status=%d",
1361 urb->status );
1362 return;
1363 }
1364 serial = port->serial;
1365 if( serial == NULL || serial->private == NULL ) {
1366 err( __FUNCTION__ ": serial or serial->private is NULL, status=%d", urb->status );
1367 return;
1368 }
1369
1370 /* handle oob callback */
1371 if( priv->dp_port_num
1372 == ((digi_serial_t *)(serial->private))->ds_oob_port_num ) {
1373 dbg( "digi_write_bulk_callback: oob callback" );
1374 spin_lock( &priv->dp_port_lock );
1375 priv->dp_write_urb_in_use = 0;
1376 wake_up_interruptible( &port->write_wait );
1377 spin_unlock( &priv->dp_port_lock );
1378 return;
1379 }
1380
1381 /* further sanity checks */
1382 if( port_paranoia_check( port, __FUNCTION__ )
1383 || serial_paranoia_check( serial, __FUNCTION__ ) )
1384 return;
1385
1386 /* try to send any buffered data on this port, if it is open */
1387 spin_lock( &priv->dp_port_lock );
1388 priv->dp_write_urb_in_use = 0;
1389 if( priv->dp_open_count && port->write_urb->status != -EINPROGRESS
1390 && priv->dp_out_buf_len > 0 ) {
1391
1392 *((unsigned char *)(port->write_urb->transfer_buffer))
1393 = (unsigned char)DIGI_CMD_SEND_DATA;
1394 *((unsigned char *)(port->write_urb->transfer_buffer)+1)
1395 = (unsigned char)priv->dp_out_buf_len;
1396
1397 port->write_urb->transfer_buffer_length
1398 = priv->dp_out_buf_len+2;
1399 port->write_urb->dev = serial->dev;
1400
1401 memcpy( port->write_urb->transfer_buffer+2, priv->dp_out_buf,
1402 priv->dp_out_buf_len );
1403
1404 if( (ret=usb_submit_urb(port->write_urb)) == 0 ) {
1405 priv->dp_write_urb_in_use = 1;
1406 priv->dp_out_buf_len = 0;
1407 }
1408
1409 }
1410
1411 /* wake up processes sleeping on writes immediately */
1412 digi_wakeup_write( port );
1413
1414 /* also queue up a wakeup at scheduler time, in case we */
1415 /* lost the race in write_chan(). */
1416 MOD_INC_USE_COUNT;
1417 if (schedule_task(&priv->dp_wakeup_task) == 0)
1418 MOD_DEC_USE_COUNT;
1419
1420 spin_unlock( &priv->dp_port_lock );
1421
1422 if( ret ) {
1423 err( __FUNCTION__ ": usb_submit_urb failed, ret=%d, port=%d",
1424 ret, priv->dp_port_num );
1425 }
1426
1427 }
1428
1429
1430 static int digi_write_room( struct usb_serial_port *port )
1431 {
1432
1433 int room;
1434 digi_port_t *priv = (digi_port_t *)(port->private);
1435 unsigned long flags = 0;
1436
1437
1438 spin_lock_irqsave( &priv->dp_port_lock, flags );
1439
1440 if( port->write_urb->status == -EINPROGRESS
1441 || priv->dp_write_urb_in_use )
1442 room = 0;
1443 else
1444 room = port->bulk_out_size - 2 - priv->dp_out_buf_len;
1445
1446 spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1447
1448 dbg( "digi_write_room: port=%d, room=%d", priv->dp_port_num, room );
1449 return( room );
1450
1451 }
1452
1453
1454 static int digi_chars_in_buffer( struct usb_serial_port *port )
1455 {
1456
1457 digi_port_t *priv = (digi_port_t *)(port->private);
1458
1459
1460 if( port->write_urb->status == -EINPROGRESS
1461 || priv->dp_write_urb_in_use ) {
1462 dbg( "digi_chars_in_buffer: port=%d, chars=%d", priv->dp_port_num, port->bulk_out_size - 2 );
1463 /* return( port->bulk_out_size - 2 ); */
1464 return( 256 );
1465 } else {
1466 dbg( "digi_chars_in_buffer: port=%d, chars=%d", priv->dp_port_num, priv->dp_out_buf_len );
1467 return( priv->dp_out_buf_len );
1468 }
1469
1470 }
1471
1472
1473 static int digi_open( struct usb_serial_port *port, struct file *filp )
1474 {
1475
1476 int ret;
1477 unsigned char buf[32];
1478 digi_port_t *priv = (digi_port_t *)(port->private);
1479 struct termios not_termios;
1480 unsigned long flags = 0;
1481
1482
1483 dbg( "digi_open: TOP: port=%d, active=%d, open_count=%d", priv->dp_port_num, port->active, priv->dp_open_count );
1484
1485 /* be sure the device is started up */
1486 if( digi_startup_device( port->serial ) != 0 )
1487 return( -ENXIO );
1488
1489 spin_lock_irqsave( &priv->dp_port_lock, flags );
1490
1491 /* don't wait on a close in progress for non-blocking opens */
1492 if( priv->dp_in_close && (filp->f_flags&(O_NDELAY|O_NONBLOCK)) == 0 ) {
1493 spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1494 return( -EAGAIN );
1495 }
1496
1497 /* inc module use count before sleeping to wait for closes */
1498 ++priv->dp_open_count;
1499 MOD_INC_USE_COUNT;
1500
1501 /* wait for a close in progress to finish */
1502 while( priv->dp_in_close ) {
1503 cond_wait_interruptible_timeout_irqrestore(
1504 &priv->dp_close_wait, DIGI_RETRY_TIMEOUT,
1505 &priv->dp_port_lock, flags );
1506 if( signal_pending(current) ) {
1507 --priv->dp_open_count;
1508 MOD_DEC_USE_COUNT;
1509 return( -EINTR );
1510 }
1511 spin_lock_irqsave( &priv->dp_port_lock, flags );
1512 }
1513
1514 /* if port is already open, just return */
1515 /* be sure exactly one open proceeds */
1516 if( port->active ) {
1517 spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1518 return( 0 );
1519 }
1520
1521 /* first open, mark port as active */
1522 port->active = 1;
1523 spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1524
1525 /* read modem signals automatically whenever they change */
1526 buf[0] = DIGI_CMD_READ_INPUT_SIGNALS;
1527 buf[1] = priv->dp_port_num;
1528 buf[2] = DIGI_ENABLE;
1529 buf[3] = 0;
1530
1531 /* flush fifos */
1532 buf[4] = DIGI_CMD_IFLUSH_FIFO;
1533 buf[5] = priv->dp_port_num;
1534 buf[6] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
1535 buf[7] = 0;
1536
1537 if( (ret=digi_write_oob_command( port, buf, 8, 1 )) != 0 )
1538 dbg( "digi_open: write oob failed, ret=%d", ret );
1539
1540 /* set termios settings */
1541 not_termios.c_cflag = ~port->tty->termios->c_cflag;
1542 not_termios.c_iflag = ~port->tty->termios->c_iflag;
1543 digi_set_termios( port, ¬_termios );
1544
1545 /* set DTR and RTS */
1546 digi_set_modem_signals( port, TIOCM_DTR|TIOCM_RTS, 1 );
1547
1548 return( 0 );
1549
1550 }
1551
1552
1553 static void digi_close( struct usb_serial_port *port, struct file *filp )
1554 {
1555
1556 int ret;
1557 unsigned char buf[32];
1558 struct tty_struct *tty = port->tty;
1559 digi_port_t *priv = (digi_port_t *)port->private;
1560 unsigned long flags = 0;
1561
1562
1563 dbg( "digi_close: TOP: port=%d, active=%d, open_count=%d", priv->dp_port_num, port->active, priv->dp_open_count );
1564
1565
1566 /* do cleanup only after final close on this port */
1567 spin_lock_irqsave( &priv->dp_port_lock, flags );
1568 if( priv->dp_open_count > 1 ) {
1569 --priv->dp_open_count;
1570 MOD_DEC_USE_COUNT;
1571 spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1572 return;
1573 } else if( priv->dp_open_count <= 0 ) {
1574 spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1575 return;
1576 }
1577 priv->dp_in_close = 1;
1578 spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1579
1580 /* tell line discipline to process only XON/XOFF */
1581 tty->closing = 1;
1582
1583 /* wait for output to drain */
1584 if( (filp->f_flags&(O_NDELAY|O_NONBLOCK)) == 0 ) {
1585 tty_wait_until_sent( tty, DIGI_CLOSE_TIMEOUT );
1586 }
1587
1588 /* flush driver and line discipline buffers */
1589 if( tty->driver.flush_buffer )
1590 tty->driver.flush_buffer( tty );
1591 if( tty->ldisc.flush_buffer )
1592 tty->ldisc.flush_buffer( tty );
1593
1594 /* wait for transmit idle */
1595 if( (filp->f_flags&(O_NDELAY|O_NONBLOCK)) == 0 ) {
1596 digi_transmit_idle( port, DIGI_CLOSE_TIMEOUT );
1597 }
1598
1599 /* drop DTR and RTS */
1600 digi_set_modem_signals( port, 0, 0 );
1601
1602 /* disable input flow control */
1603 buf[0] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
1604 buf[1] = priv->dp_port_num;
1605 buf[2] = DIGI_DISABLE;
1606 buf[3] = 0;
1607
1608 /* disable output flow control */
1609 buf[4] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
1610 buf[5] = priv->dp_port_num;
1611 buf[6] = DIGI_DISABLE;
1612 buf[7] = 0;
1613
1614 /* disable reading modem signals automatically */
1615 buf[8] = DIGI_CMD_READ_INPUT_SIGNALS;
1616 buf[9] = priv->dp_port_num;
1617 buf[10] = DIGI_DISABLE;
1618 buf[11] = 0;
1619
1620 /* disable receive */
1621 buf[12] = DIGI_CMD_RECEIVE_ENABLE;
1622 buf[13] = priv->dp_port_num;
1623 buf[14] = DIGI_DISABLE;
1624 buf[15] = 0;
1625
1626 /* flush fifos */
1627 buf[16] = DIGI_CMD_IFLUSH_FIFO;
1628 buf[17] = priv->dp_port_num;
1629 buf[18] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
1630 buf[19] = 0;
1631
1632 if( (ret=digi_write_oob_command( port, buf, 20, 0 )) != 0 )
1633 dbg( "digi_close: write oob failed, ret=%d", ret );
1634
1635 /* wait for final commands on oob port to complete */
1636 interruptible_sleep_on_timeout( &priv->dp_flush_wait,
1637 DIGI_CLOSE_TIMEOUT );
1638
1639 /* shutdown any outstanding bulk writes */
1640 usb_unlink_urb (port->write_urb);
1641
1642 tty->closing = 0;
1643
1644 spin_lock_irqsave( &priv->dp_port_lock, flags );
1645 port->active = 0;
1646 priv->dp_write_urb_in_use = 0;
1647 priv->dp_in_close = 0;
1648 --priv->dp_open_count;
1649 MOD_DEC_USE_COUNT;
1650 wake_up_interruptible( &priv->dp_close_wait );
1651 spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1652
1653 dbg( "digi_close: done" );
1654 }
1655
1656
1657 /*
1658 * Digi Startup Device
1659 *
1660 * Starts reads on all ports. Must be called AFTER startup, with
1661 * urbs initialized. Returns 0 if successful, non-zero error otherwise.
1662 */
1663
1664 static int digi_startup_device( struct usb_serial *serial )
1665 {
1666
1667 int i,ret = 0;
1668 digi_serial_t *serial_priv = (digi_serial_t *)serial->private;
1669 struct usb_serial_port *port;
1670
1671
1672 /* be sure this happens exactly once */
1673 spin_lock( &serial_priv->ds_serial_lock );
1674 if( serial_priv->ds_device_started ) {
1675 spin_unlock( &serial_priv->ds_serial_lock );
1676 return( 0 );
1677 }
1678 serial_priv->ds_device_started = 1;
1679 spin_unlock( &serial_priv->ds_serial_lock );
1680
1681 /* start reading from each bulk in endpoint for the device */
1682 /* set USB_DISABLE_SPD flag for write bulk urbs */
1683 for( i=0; i<serial->type->num_ports+1; i++ ) {
1684
1685 port = &serial->port[i];
1686
1687 port->write_urb->dev = port->serial->dev;
1688
1689 if( (ret=usb_submit_urb(port->read_urb)) != 0 ) {
1690 err(
1691 __FUNCTION__ ": usb_submit_urb failed, ret=%d, port=%d",
1692 ret, i );
1693 break;
1694 }
1695
1696 }
1697
1698 return( ret );
1699
1700 }
1701
1702
1703 static int digi_startup( struct usb_serial *serial )
1704 {
1705
1706 int i;
1707 digi_port_t *priv;
1708 digi_serial_t *serial_priv;
1709
1710
1711 dbg( "digi_startup: TOP" );
1712
1713 /* allocate the private data structures for all ports */
1714 /* number of regular ports + 1 for the out-of-band port */
1715 for( i=0; i<serial->type->num_ports+1; i++ ) {
1716
1717 serial->port[i].active = 0;
1718
1719 /* allocate port private structure */
1720 priv = serial->port[i].private =
1721 (digi_port_t *)kmalloc( sizeof(digi_port_t),
1722 GFP_KERNEL );
1723 if( priv == (digi_port_t *)0 ) {
1724 while( --i >= 0 )
1725 kfree( serial->port[i].private );
1726 return( 1 ); /* error */
1727 }
1728
1729 /* initialize port private structure */
1730 spin_lock_init( &priv->dp_port_lock );
1731 priv->dp_port_num = i;
1732 priv->dp_out_buf_len = 0;
1733 priv->dp_in_buf_len = 0;
1734 priv->dp_write_urb_in_use = 0;
1735 priv->dp_modem_signals = 0;
1736 init_waitqueue_head( &priv->dp_modem_change_wait );
1737 priv->dp_open_count = 0;
1738 priv->dp_transmit_idle = 0;
1739 init_waitqueue_head( &priv->dp_transmit_idle_wait );
1740 priv->dp_throttled = 0;
1741 priv->dp_throttle_restart = 0;
1742 init_waitqueue_head( &priv->dp_flush_wait );
1743 priv->dp_in_close = 0;
1744 init_waitqueue_head( &priv->dp_close_wait );
1745 INIT_LIST_HEAD(&priv->dp_wakeup_task.list);
1746 priv->dp_wakeup_task.sync = 0;
1747 priv->dp_wakeup_task.routine = (void *)digi_wakeup_write_lock;
1748 priv->dp_wakeup_task.data = (void *)(&serial->port[i]);
1749
1750 /* initialize write wait queue for this port */
1751 init_waitqueue_head( &serial->port[i].write_wait );
1752
1753 }
1754
1755 /* allocate serial private structure */
1756 serial_priv = serial->private =
1757 (digi_serial_t *)kmalloc( sizeof(digi_serial_t),
1758 GFP_KERNEL );
1759 if( serial_priv == (digi_serial_t *)0 ) {
1760 for( i=0; i<serial->type->num_ports+1; i++ )
1761 kfree( serial->port[i].private );
1762 return( 1 ); /* error */
1763 }
1764
1765 /* initialize serial private structure */
1766 spin_lock_init( &serial_priv->ds_serial_lock );
1767 serial_priv->ds_oob_port_num = serial->type->num_ports;
1768 serial_priv->ds_oob_port = &serial->port[serial_priv->ds_oob_port_num];
1769 serial_priv->ds_device_started = 0;
1770
1771 return( 0 );
1772
1773 }
1774
1775
1776 static void digi_shutdown( struct usb_serial *serial )
1777 {
1778
1779 int i;
1780 digi_port_t *priv;
1781 unsigned long flags;
1782
1783
1784 dbg( "digi_shutdown: TOP, in_interrupt()=%d", in_interrupt() );
1785
1786 /* stop reads and writes on all ports */
1787 for( i=0; i<serial->type->num_ports+1; i++ ) {
1788 usb_unlink_urb( serial->port[i].read_urb );
1789 usb_unlink_urb( serial->port[i].write_urb );
1790 }
1791
1792 /* dec module use count */
1793 for( i=0; i<serial->type->num_ports; i++ ) {
1794 priv = serial->port[i].private;
1795 spin_lock_irqsave( &priv->dp_port_lock, flags );
1796 while( priv->dp_open_count > 0 ) {
1797 MOD_DEC_USE_COUNT;
1798 --priv->dp_open_count;
1799 }
1800 spin_unlock_irqrestore( &priv->dp_port_lock, flags );
1801 }
1802
1803 /* free the private data structures for all ports */
1804 /* number of regular ports + 1 for the out-of-band port */
1805 for( i=0; i<serial->type->num_ports+1; i++ )
1806 kfree( serial->port[i].private );
1807 kfree( serial->private );
1808
1809 }
1810
1811
1812 static void digi_read_bulk_callback( struct urb *urb )
1813 {
1814
1815 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1816 digi_port_t *priv;
1817 int ret;
1818
1819
1820 dbg( "digi_read_bulk_callback: TOP" );
1821
1822 /* port sanity check, do not resubmit if port is not valid */
1823 if( port == NULL || (priv=(digi_port_t *)(port->private)) == NULL ) {
1824 err( __FUNCTION__ ": port or port->private is NULL, status=%d",
1825 urb->status );
1826 return;
1827 }
1828 if( port->serial == NULL
1829 || serial_paranoia_check( port->serial, __FUNCTION__ )
1830 || port->serial->private == NULL ) {
1831 err( __FUNCTION__ ": serial is bad or serial->private is NULL, status=%d", urb->status );
1832 return;
1833 }
1834
1835 /* do not resubmit urb if it has any status error */
1836 if( urb->status ) {
1837 err( __FUNCTION__ ": nonzero read bulk status: status=%d, port=%d", urb->status, priv->dp_port_num );
1838 return;
1839 }
1840
1841 /* handle oob or inb callback, do not resubmit if error */
1842 if( priv->dp_port_num
1843 == ((digi_serial_t *)(port->serial->private))->ds_oob_port_num ) {
1844 if( digi_read_oob_callback( urb ) != 0 )
1845 return;
1846 } else {
1847 if( digi_read_inb_callback( urb ) != 0 )
1848 return;
1849 }
1850
1851 /* continue read */
1852 urb->dev = port->serial->dev;
1853 if( (ret=usb_submit_urb(urb)) != 0 ) {
1854 err( __FUNCTION__ ": failed resubmitting urb, ret=%d, port=%d",
1855 ret, priv->dp_port_num );
1856 }
1857
1858 }
1859
1860
1861 /*
1862 * Digi Read INB Callback
1863 *
1864 * Digi Read INB Callback handles reads on the in band ports, sending
1865 * the data on to the tty subsystem. When called we know port and
1866 * port->private are not NULL and port->serial has been validated.
1867 * It returns 0 if successful, 1 if successful but the port is
1868 * throttled, and -1 if the sanity checks failed.
1869 */
1870
1871 static int digi_read_inb_callback( struct urb *urb )
1872 {
1873
1874 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1875 struct tty_struct *tty = port->tty;
1876 digi_port_t *priv = (digi_port_t *)(port->private);
1877 int opcode = ((unsigned char *)urb->transfer_buffer)[0];
1878 int len = ((unsigned char *)urb->transfer_buffer)[1];
1879 int status = ((unsigned char *)urb->transfer_buffer)[2];
1880 unsigned char *data = ((unsigned char *)urb->transfer_buffer)+3;
1881 int flag,throttled;
1882
1883
1884 /* sanity check */
1885 if( port_paranoia_check( port, __FUNCTION__ ) )
1886 return( -1 );
1887
1888 /* do not process callbacks on closed ports */
1889 /* but do continue the read chain */
1890 if( priv->dp_open_count == 0 )
1891 return( 0 );
1892
1893 /* short/multiple packet check */
1894 if( urb->actual_length != len + 2 ) {
1895 err( __FUNCTION__ ": INCOMPLETE OR MULTIPLE PACKET, urb->status=%d, port=%d, opcode=%d, len=%d, actual_length=%d, status=%d", urb->status, priv->dp_port_num, opcode, len, urb->actual_length, status );
1896 return( -1 );
1897 }
1898
1899 spin_lock( &priv->dp_port_lock );
1900
1901 /* check for throttle; if set, do not resubmit read urb */
1902 /* indicate the read chain needs to be restarted on unthrottle */
1903 throttled = priv->dp_throttled;
1904 if( throttled )
1905 priv->dp_throttle_restart = 1;
1906
1907 /* receive data */
1908 if( opcode == DIGI_CMD_RECEIVE_DATA ) {
1909
1910 /* get flag from status */
1911 flag = 0;
1912
1913 /* overrun is special, not associated with a char */
1914 if( status & DIGI_OVERRUN_ERROR ) {
1915 tty_insert_flip_char( tty, 0, TTY_OVERRUN );
1916 }
1917
1918 /* break takes precedence over parity, */
1919 /* which takes precedence over framing errors */
1920 if( status & DIGI_BREAK_ERROR ) {
1921 flag = TTY_BREAK;
1922 } else if( status & DIGI_PARITY_ERROR ) {
1923 flag = TTY_PARITY;
1924 } else if( status & DIGI_FRAMING_ERROR ) {
1925 flag = TTY_FRAME;
1926 }
1927
1928 /* data length is len-1 (one byte of len is status) */
1929 --len;
1930
1931 if( throttled ) {
1932
1933 len = min( len,
1934 DIGI_IN_BUF_SIZE - priv->dp_in_buf_len );
1935
1936 if( len > 0 ) {
1937 memcpy( priv->dp_in_buf + priv->dp_in_buf_len,
1938 data, len );
1939 memset( priv->dp_in_flag_buf
1940 + priv->dp_in_buf_len, flag, len );
1941 priv->dp_in_buf_len += len;
1942 }
1943
1944 } else {
1945
1946 len = min( len, TTY_FLIPBUF_SIZE - tty->flip.count );
1947
1948 if( len > 0 ) {
1949 memcpy( tty->flip.char_buf_ptr, data, len );
1950 memset( tty->flip.flag_buf_ptr, flag, len );
1951 tty->flip.char_buf_ptr += len;
1952 tty->flip.flag_buf_ptr += len;
1953 tty->flip.count += len;
1954 tty_flip_buffer_push( tty );
1955 }
1956
1957 }
1958
1959 }
1960
1961 spin_unlock( &priv->dp_port_lock );
1962
1963 if( opcode == DIGI_CMD_RECEIVE_DISABLE ) {
1964 dbg( __FUNCTION__ ": got RECEIVE_DISABLE" );
1965 } else if( opcode != DIGI_CMD_RECEIVE_DATA ) {
1966 dbg( __FUNCTION__ ": unknown opcode: %d", opcode );
1967 }
1968
1969 return( throttled ? 1 : 0 );
1970
1971 }
1972
1973
1974 /*
1975 * Digi Read OOB Callback
1976 *
1977 * Digi Read OOB Callback handles reads on the out of band port.
1978 * When called we know port and port->private are not NULL and
1979 * the port->serial is valid. It returns 0 if successful, and
1980 * -1 if the sanity checks failed.
1981 */
1982
1983 static int digi_read_oob_callback( struct urb *urb )
1984 {
1985
1986 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1987 struct usb_serial *serial = port->serial;
1988 digi_port_t *priv = (digi_port_t *)(port->private);
1989 int opcode, line, status, val;
1990 int i;
1991
1992
1993 dbg( "digi_read_oob_callback: port=%d, len=%d", priv->dp_port_num,
1994 urb->actual_length );
1995
1996 /* handle each oob command */
1997 for( i=0; i<urb->actual_length-3; ) {
1998
1999 opcode = ((unsigned char *)urb->transfer_buffer)[i++];
2000 line = ((unsigned char *)urb->transfer_buffer)[i++];
2001 status = ((unsigned char *)urb->transfer_buffer)[i++];
2002 val = ((unsigned char *)urb->transfer_buffer)[i++];
2003
2004 dbg( "digi_read_oob_callback: opcode=%d, line=%d, status=%d, val=%d",
2005 opcode, line, status, val );
2006
2007 if( status != 0 || line >= serial->type->num_ports )
2008 continue;
2009
2010 port = &serial->port[line];
2011
2012 if( port_paranoia_check( port, __FUNCTION__ )
2013 || (priv=port->private) == NULL )
2014 return( -1 );
2015
2016 if( opcode == DIGI_CMD_READ_INPUT_SIGNALS ) {
2017
2018 spin_lock( &priv->dp_port_lock );
2019
2020 /* convert from digi flags to termiox flags */
2021 if( val & DIGI_READ_INPUT_SIGNALS_CTS ) {
2022 priv->dp_modem_signals |= TIOCM_CTS;
2023 /* port must be open to use tty struct */
2024 if( priv->dp_open_count
2025 && port->tty->termios->c_cflag & CRTSCTS ) {
2026 port->tty->hw_stopped = 0;
2027 digi_wakeup_write( port );
2028 }
2029 } else {
2030 priv->dp_modem_signals &= ~TIOCM_CTS;
2031 /* port must be open to use tty struct */
2032 if( priv->dp_open_count
2033 && port->tty->termios->c_cflag & CRTSCTS ) {
2034 port->tty->hw_stopped = 1;
2035 }
2036 }
2037 if( val & DIGI_READ_INPUT_SIGNALS_DSR )
2038 priv->dp_modem_signals |= TIOCM_DSR;
2039 else
2040 priv->dp_modem_signals &= ~TIOCM_DSR;
2041 if( val & DIGI_READ_INPUT_SIGNALS_RI )
2042 priv->dp_modem_signals |= TIOCM_RI;
2043 else
2044 priv->dp_modem_signals &= ~TIOCM_RI;
2045 if( val & DIGI_READ_INPUT_SIGNALS_DCD )
2046 priv->dp_modem_signals |= TIOCM_CD;
2047 else
2048 priv->dp_modem_signals &= ~TIOCM_CD;
2049
2050 wake_up_interruptible( &priv->dp_modem_change_wait );
2051 spin_unlock( &priv->dp_port_lock );
2052
2053 } else if( opcode == DIGI_CMD_TRANSMIT_IDLE ) {
2054
2055 spin_lock( &priv->dp_port_lock );
2056 priv->dp_transmit_idle = 1;
2057 wake_up_interruptible( &priv->dp_transmit_idle_wait );
2058 spin_unlock( &priv->dp_port_lock );
2059
2060 } else if( opcode == DIGI_CMD_IFLUSH_FIFO ) {
2061
2062 wake_up_interruptible( &priv->dp_flush_wait );
2063
2064 }
2065
2066 }
2067
2068 return( 0 );
2069
2070 }
2071
2072
2073 static int __init digi_init (void)
2074 {
2075 usb_serial_register (&digi_acceleport_2_device);
2076 usb_serial_register (&digi_acceleport_4_device);
2077 info(DRIVER_VERSION ":" DRIVER_DESC);
2078 return 0;
2079 }
2080
2081
2082 static void __exit digi_exit (void)
2083 {
2084 usb_serial_deregister (&digi_acceleport_2_device);
2085 usb_serial_deregister (&digi_acceleport_4_device);
2086 }
2087
2088
2089 module_init(digi_init);
2090 module_exit(digi_exit);
2091
2092
2093 MODULE_AUTHOR( DRIVER_AUTHOR );
2094 MODULE_DESCRIPTION( DRIVER_DESC );
2095 MODULE_LICENSE("GPL");
2096
2097 MODULE_PARM(debug, "i");
2098 MODULE_PARM_DESC(debug, "Debug enabled or not");
2099
2100