digi_acceleport.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:59k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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