digi_acceleport.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  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. #include <linux/kernel.h>
  18. #include <linux/errno.h>
  19. #include <linux/init.h>
  20. #include <linux/slab.h>
  21. #include <linux/tty.h>
  22. #include <linux/tty_driver.h>
  23. #include <linux/tty_flip.h>
  24. #include <linux/module.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/usb.h>
  29. #include <linux/wait.h>
  30. #include <linux/usb/serial.h>
  31. /* Defines */
  32. /*
  33. * Version Information
  34. */
  35. #define DRIVER_VERSION "v1.80.1.2"
  36. #define DRIVER_AUTHOR "Peter Berger <pberger@brimson.com>, Al Borchers <borchers@steinerpoint.com>"
  37. #define DRIVER_DESC "Digi AccelePort USB-2/USB-4 Serial Converter driver"
  38. /* port output buffer length -- must be <= transfer buffer length - 2 */
  39. /* so we can be sure to send the full buffer in one urb */
  40. #define DIGI_OUT_BUF_SIZE 8
  41. /* port input buffer length -- must be >= transfer buffer length - 3 */
  42. /* so we can be sure to hold at least one full buffer from one urb */
  43. #define DIGI_IN_BUF_SIZE 64
  44. /* retry timeout while sleeping */
  45. #define DIGI_RETRY_TIMEOUT (HZ/10)
  46. /* timeout while waiting for tty output to drain in close */
  47. /* this delay is used twice in close, so the total delay could */
  48. /* be twice this value */
  49. #define DIGI_CLOSE_TIMEOUT (5*HZ)
  50. /* AccelePort USB Defines */
  51. /* ids */
  52. #define DIGI_VENDOR_ID 0x05c5
  53. #define DIGI_2_ID 0x0002 /* USB-2 */
  54. #define DIGI_4_ID 0x0004 /* USB-4 */
  55. /* commands
  56. * "INB": can be used on the in-band endpoint
  57. * "OOB": can be used on the out-of-band endpoint
  58. */
  59. #define DIGI_CMD_SET_BAUD_RATE 0 /* INB, OOB */
  60. #define DIGI_CMD_SET_WORD_SIZE 1 /* INB, OOB */
  61. #define DIGI_CMD_SET_PARITY 2 /* INB, OOB */
  62. #define DIGI_CMD_SET_STOP_BITS 3 /* INB, OOB */
  63. #define DIGI_CMD_SET_INPUT_FLOW_CONTROL 4 /* INB, OOB */
  64. #define DIGI_CMD_SET_OUTPUT_FLOW_CONTROL 5 /* INB, OOB */
  65. #define DIGI_CMD_SET_DTR_SIGNAL 6 /* INB, OOB */
  66. #define DIGI_CMD_SET_RTS_SIGNAL 7 /* INB, OOB */
  67. #define DIGI_CMD_READ_INPUT_SIGNALS 8 /* OOB */
  68. #define DIGI_CMD_IFLUSH_FIFO 9 /* OOB */
  69. #define DIGI_CMD_RECEIVE_ENABLE 10 /* INB, OOB */
  70. #define DIGI_CMD_BREAK_CONTROL 11 /* INB, OOB */
  71. #define DIGI_CMD_LOCAL_LOOPBACK 12 /* INB, OOB */
  72. #define DIGI_CMD_TRANSMIT_IDLE 13 /* INB, OOB */
  73. #define DIGI_CMD_READ_UART_REGISTER 14 /* OOB */
  74. #define DIGI_CMD_WRITE_UART_REGISTER 15 /* INB, OOB */
  75. #define DIGI_CMD_AND_UART_REGISTER 16 /* INB, OOB */
  76. #define DIGI_CMD_OR_UART_REGISTER 17 /* INB, OOB */
  77. #define DIGI_CMD_SEND_DATA 18 /* INB */
  78. #define DIGI_CMD_RECEIVE_DATA 19 /* INB */
  79. #define DIGI_CMD_RECEIVE_DISABLE 20 /* INB */
  80. #define DIGI_CMD_GET_PORT_TYPE 21 /* OOB */
  81. /* baud rates */
  82. #define DIGI_BAUD_50 0
  83. #define DIGI_BAUD_75 1
  84. #define DIGI_BAUD_110 2
  85. #define DIGI_BAUD_150 3
  86. #define DIGI_BAUD_200 4
  87. #define DIGI_BAUD_300 5
  88. #define DIGI_BAUD_600 6
  89. #define DIGI_BAUD_1200 7
  90. #define DIGI_BAUD_1800 8
  91. #define DIGI_BAUD_2400 9
  92. #define DIGI_BAUD_4800 10
  93. #define DIGI_BAUD_7200 11
  94. #define DIGI_BAUD_9600 12
  95. #define DIGI_BAUD_14400 13
  96. #define DIGI_BAUD_19200 14
  97. #define DIGI_BAUD_28800 15
  98. #define DIGI_BAUD_38400 16
  99. #define DIGI_BAUD_57600 17
  100. #define DIGI_BAUD_76800 18
  101. #define DIGI_BAUD_115200 19
  102. #define DIGI_BAUD_153600 20
  103. #define DIGI_BAUD_230400 21
  104. #define DIGI_BAUD_460800 22
  105. /* arguments */
  106. #define DIGI_WORD_SIZE_5 0
  107. #define DIGI_WORD_SIZE_6 1
  108. #define DIGI_WORD_SIZE_7 2
  109. #define DIGI_WORD_SIZE_8 3
  110. #define DIGI_PARITY_NONE 0
  111. #define DIGI_PARITY_ODD 1
  112. #define DIGI_PARITY_EVEN 2
  113. #define DIGI_PARITY_MARK 3
  114. #define DIGI_PARITY_SPACE 4
  115. #define DIGI_STOP_BITS_1 0
  116. #define DIGI_STOP_BITS_2 1
  117. #define DIGI_INPUT_FLOW_CONTROL_XON_XOFF 1
  118. #define DIGI_INPUT_FLOW_CONTROL_RTS 2
  119. #define DIGI_INPUT_FLOW_CONTROL_DTR 4
  120. #define DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF 1
  121. #define DIGI_OUTPUT_FLOW_CONTROL_CTS 2
  122. #define DIGI_OUTPUT_FLOW_CONTROL_DSR 4
  123. #define DIGI_DTR_INACTIVE 0
  124. #define DIGI_DTR_ACTIVE 1
  125. #define DIGI_DTR_INPUT_FLOW_CONTROL 2
  126. #define DIGI_RTS_INACTIVE 0
  127. #define DIGI_RTS_ACTIVE 1
  128. #define DIGI_RTS_INPUT_FLOW_CONTROL 2
  129. #define DIGI_RTS_TOGGLE 3
  130. #define DIGI_FLUSH_TX 1
  131. #define DIGI_FLUSH_RX 2
  132. #define DIGI_RESUME_TX 4 /* clears xoff condition */
  133. #define DIGI_TRANSMIT_NOT_IDLE 0
  134. #define DIGI_TRANSMIT_IDLE 1
  135. #define DIGI_DISABLE 0
  136. #define DIGI_ENABLE 1
  137. #define DIGI_DEASSERT 0
  138. #define DIGI_ASSERT 1
  139. /* in band status codes */
  140. #define DIGI_OVERRUN_ERROR 4
  141. #define DIGI_PARITY_ERROR 8
  142. #define DIGI_FRAMING_ERROR 16
  143. #define DIGI_BREAK_ERROR 32
  144. /* out of band status */
  145. #define DIGI_NO_ERROR 0
  146. #define DIGI_BAD_FIRST_PARAMETER 1
  147. #define DIGI_BAD_SECOND_PARAMETER 2
  148. #define DIGI_INVALID_LINE 3
  149. #define DIGI_INVALID_OPCODE 4
  150. /* input signals */
  151. #define DIGI_READ_INPUT_SIGNALS_SLOT 1
  152. #define DIGI_READ_INPUT_SIGNALS_ERR 2
  153. #define DIGI_READ_INPUT_SIGNALS_BUSY 4
  154. #define DIGI_READ_INPUT_SIGNALS_PE 8
  155. #define DIGI_READ_INPUT_SIGNALS_CTS 16
  156. #define DIGI_READ_INPUT_SIGNALS_DSR 32
  157. #define DIGI_READ_INPUT_SIGNALS_RI 64
  158. #define DIGI_READ_INPUT_SIGNALS_DCD 128
  159. /* Structures */
  160. struct digi_serial {
  161. spinlock_t ds_serial_lock;
  162. struct usb_serial_port *ds_oob_port; /* out-of-band port */
  163. int ds_oob_port_num; /* index of out-of-band port */
  164. int ds_device_started;
  165. };
  166. struct digi_port {
  167. spinlock_t dp_port_lock;
  168. int dp_port_num;
  169. int dp_out_buf_len;
  170. unsigned char dp_out_buf[DIGI_OUT_BUF_SIZE];
  171. int dp_write_urb_in_use;
  172. unsigned int dp_modem_signals;
  173. wait_queue_head_t dp_modem_change_wait;
  174. int dp_transmit_idle;
  175. wait_queue_head_t dp_transmit_idle_wait;
  176. int dp_throttled;
  177. int dp_throttle_restart;
  178. wait_queue_head_t dp_flush_wait;
  179. wait_queue_head_t dp_close_wait; /* wait queue for close */
  180. struct work_struct dp_wakeup_work;
  181. struct usb_serial_port *dp_port;
  182. };
  183. /* Local Function Declarations */
  184. static void digi_wakeup_write(struct usb_serial_port *port);
  185. static void digi_wakeup_write_lock(struct work_struct *work);
  186. static int digi_write_oob_command(struct usb_serial_port *port,
  187. unsigned char *buf, int count, int interruptible);
  188. static int digi_write_inb_command(struct usb_serial_port *port,
  189. unsigned char *buf, int count, unsigned long timeout);
  190. static int digi_set_modem_signals(struct usb_serial_port *port,
  191. unsigned int modem_signals, int interruptible);
  192. static int digi_transmit_idle(struct usb_serial_port *port,
  193. unsigned long timeout);
  194. static void digi_rx_throttle(struct tty_struct *tty);
  195. static void digi_rx_unthrottle(struct tty_struct *tty);
  196. static void digi_set_termios(struct tty_struct *tty,
  197. struct usb_serial_port *port, struct ktermios *old_termios);
  198. static void digi_break_ctl(struct tty_struct *tty, int break_state);
  199. static int digi_tiocmget(struct tty_struct *tty);
  200. static int digi_tiocmset(struct tty_struct *tty, unsigned int set,
  201. unsigned int clear);
  202. static int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
  203. const unsigned char *buf, int count);
  204. static void digi_write_bulk_callback(struct urb *urb);
  205. static int digi_write_room(struct tty_struct *tty);
  206. static int digi_chars_in_buffer(struct tty_struct *tty);
  207. static int digi_open(struct tty_struct *tty, struct usb_serial_port *port);
  208. static void digi_close(struct usb_serial_port *port);
  209. static void digi_dtr_rts(struct usb_serial_port *port, int on);
  210. static int digi_startup_device(struct usb_serial *serial);
  211. static int digi_startup(struct usb_serial *serial);
  212. static void digi_disconnect(struct usb_serial *serial);
  213. static void digi_release(struct usb_serial *serial);
  214. static void digi_read_bulk_callback(struct urb *urb);
  215. static int digi_read_inb_callback(struct urb *urb);
  216. static int digi_read_oob_callback(struct urb *urb);
  217. static const struct usb_device_id id_table_combined[] = {
  218. { USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
  219. { USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
  220. { } /* Terminating entry */
  221. };
  222. static const struct usb_device_id id_table_2[] = {
  223. { USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
  224. { } /* Terminating entry */
  225. };
  226. static const struct usb_device_id id_table_4[] = {
  227. { USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
  228. { } /* Terminating entry */
  229. };
  230. MODULE_DEVICE_TABLE(usb, id_table_combined);
  231. /* device info needed for the Digi serial converter */
  232. static struct usb_serial_driver digi_acceleport_2_device = {
  233. .driver = {
  234. .owner = THIS_MODULE,
  235. .name = "digi_2",
  236. },
  237. .description = "Digi 2 port USB adapter",
  238. .id_table = id_table_2,
  239. .num_ports = 3,
  240. .open = digi_open,
  241. .close = digi_close,
  242. .dtr_rts = digi_dtr_rts,
  243. .write = digi_write,
  244. .write_room = digi_write_room,
  245. .write_bulk_callback = digi_write_bulk_callback,
  246. .read_bulk_callback = digi_read_bulk_callback,
  247. .chars_in_buffer = digi_chars_in_buffer,
  248. .throttle = digi_rx_throttle,
  249. .unthrottle = digi_rx_unthrottle,
  250. .set_termios = digi_set_termios,
  251. .break_ctl = digi_break_ctl,
  252. .tiocmget = digi_tiocmget,
  253. .tiocmset = digi_tiocmset,
  254. .attach = digi_startup,
  255. .disconnect = digi_disconnect,
  256. .release = digi_release,
  257. };
  258. static struct usb_serial_driver digi_acceleport_4_device = {
  259. .driver = {
  260. .owner = THIS_MODULE,
  261. .name = "digi_4",
  262. },
  263. .description = "Digi 4 port USB adapter",
  264. .id_table = id_table_4,
  265. .num_ports = 4,
  266. .open = digi_open,
  267. .close = digi_close,
  268. .write = digi_write,
  269. .write_room = digi_write_room,
  270. .write_bulk_callback = digi_write_bulk_callback,
  271. .read_bulk_callback = digi_read_bulk_callback,
  272. .chars_in_buffer = digi_chars_in_buffer,
  273. .throttle = digi_rx_throttle,
  274. .unthrottle = digi_rx_unthrottle,
  275. .set_termios = digi_set_termios,
  276. .break_ctl = digi_break_ctl,
  277. .tiocmget = digi_tiocmget,
  278. .tiocmset = digi_tiocmset,
  279. .attach = digi_startup,
  280. .disconnect = digi_disconnect,
  281. .release = digi_release,
  282. };
  283. static struct usb_serial_driver * const serial_drivers[] = {
  284. &digi_acceleport_2_device, &digi_acceleport_4_device, NULL
  285. };
  286. /* Functions */
  287. /*
  288. * Cond Wait Interruptible Timeout Irqrestore
  289. *
  290. * Do spin_unlock_irqrestore and interruptible_sleep_on_timeout
  291. * so that wake ups are not lost if they occur between the unlock
  292. * and the sleep. In other words, spin_unlock_irqrestore and
  293. * interruptible_sleep_on_timeout are "atomic" with respect to
  294. * wake ups. This is used to implement condition variables.
  295. *
  296. * interruptible_sleep_on_timeout is deprecated and has been replaced
  297. * with the equivalent code.
  298. */
  299. static long cond_wait_interruptible_timeout_irqrestore(
  300. wait_queue_head_t *q, long timeout,
  301. spinlock_t *lock, unsigned long flags)
  302. __releases(lock)
  303. {
  304. DEFINE_WAIT(wait);
  305. prepare_to_wait(q, &wait, TASK_INTERRUPTIBLE);
  306. spin_unlock_irqrestore(lock, flags);
  307. timeout = schedule_timeout(timeout);
  308. finish_wait(q, &wait);
  309. return timeout;
  310. }
  311. /*
  312. * Digi Wakeup Write
  313. *
  314. * Wake up port, line discipline, and tty processes sleeping
  315. * on writes.
  316. */
  317. static void digi_wakeup_write_lock(struct work_struct *work)
  318. {
  319. struct digi_port *priv =
  320. container_of(work, struct digi_port, dp_wakeup_work);
  321. struct usb_serial_port *port = priv->dp_port;
  322. unsigned long flags;
  323. spin_lock_irqsave(&priv->dp_port_lock, flags);
  324. digi_wakeup_write(port);
  325. spin_unlock_irqrestore(&priv->dp_port_lock, flags);
  326. }
  327. static void digi_wakeup_write(struct usb_serial_port *port)
  328. {
  329. struct tty_struct *tty = tty_port_tty_get(&port->port);
  330. if (tty) {
  331. tty_wakeup(tty);
  332. tty_kref_put(tty);
  333. }
  334. }
  335. /*
  336. * Digi Write OOB Command
  337. *
  338. * Write commands on the out of band port. Commands are 4
  339. * bytes each, multiple commands can be sent at once, and
  340. * no command will be split across USB packets. Returns 0
  341. * if successful, -EINTR if interrupted while sleeping and
  342. * the interruptible flag is true, or a negative error
  343. * returned by usb_submit_urb.
  344. */
  345. static int digi_write_oob_command(struct usb_serial_port *port,
  346. unsigned char *buf, int count, int interruptible)
  347. {
  348. int ret = 0;
  349. int len;
  350. struct usb_serial_port *oob_port = (struct usb_serial_port *)((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
  351. struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
  352. unsigned long flags = 0;
  353. dev_dbg(&port->dev,
  354. "digi_write_oob_command: TOP: port=%d, count=%d\n",
  355. oob_priv->dp_port_num, count);
  356. spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
  357. while (count > 0) {
  358. while (oob_priv->dp_write_urb_in_use) {
  359. cond_wait_interruptible_timeout_irqrestore(
  360. &oob_port->write_wait, DIGI_RETRY_TIMEOUT,
  361. &oob_priv->dp_port_lock, flags);
  362. if (interruptible && signal_pending(current))
  363. return -EINTR;
  364. spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
  365. }
  366. /* len must be a multiple of 4, so commands are not split */
  367. len = min(count, oob_port->bulk_out_size);
  368. if (len > 4)
  369. len &= ~3;
  370. memcpy(oob_port->write_urb->transfer_buffer, buf, len);
  371. oob_port->write_urb->transfer_buffer_length = len;
  372. ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
  373. if (ret == 0) {
  374. oob_priv->dp_write_urb_in_use = 1;
  375. count -= len;
  376. buf += len;
  377. }
  378. }
  379. spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
  380. if (ret)
  381. dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n",
  382. __func__, ret);
  383. return ret;
  384. }
  385. /*
  386. * Digi Write In Band Command
  387. *
  388. * Write commands on the given port. Commands are 4
  389. * bytes each, multiple commands can be sent at once, and
  390. * no command will be split across USB packets. If timeout
  391. * is non-zero, write in band command will return after
  392. * waiting unsuccessfully for the URB status to clear for
  393. * timeout ticks. Returns 0 if successful, or a negative
  394. * error returned by digi_write.
  395. */
  396. static int digi_write_inb_command(struct usb_serial_port *port,
  397. unsigned char *buf, int count, unsigned long timeout)
  398. {
  399. int ret = 0;
  400. int len;
  401. struct digi_port *priv = usb_get_serial_port_data(port);
  402. unsigned char *data = port->write_urb->transfer_buffer;
  403. unsigned long flags = 0;
  404. dev_dbg(&port->dev, "digi_write_inb_command: TOP: port=%d, count=%d\n",
  405. priv->dp_port_num, count);
  406. if (timeout)
  407. timeout += jiffies;
  408. else
  409. timeout = ULONG_MAX;
  410. spin_lock_irqsave(&priv->dp_port_lock, flags);
  411. while (count > 0 && ret == 0) {
  412. while (priv->dp_write_urb_in_use &&
  413. time_before(jiffies, timeout)) {
  414. cond_wait_interruptible_timeout_irqrestore(
  415. &port->write_wait, DIGI_RETRY_TIMEOUT,
  416. &priv->dp_port_lock, flags);
  417. if (signal_pending(current))
  418. return -EINTR;
  419. spin_lock_irqsave(&priv->dp_port_lock, flags);
  420. }
  421. /* len must be a multiple of 4 and small enough to */
  422. /* guarantee the write will send buffered data first, */
  423. /* so commands are in order with data and not split */
  424. len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
  425. if (len > 4)
  426. len &= ~3;
  427. /* write any buffered data first */
  428. if (priv->dp_out_buf_len > 0) {
  429. data[0] = DIGI_CMD_SEND_DATA;
  430. data[1] = priv->dp_out_buf_len;
  431. memcpy(data + 2, priv->dp_out_buf,
  432. priv->dp_out_buf_len);
  433. memcpy(data + 2 + priv->dp_out_buf_len, buf, len);
  434. port->write_urb->transfer_buffer_length
  435. = priv->dp_out_buf_len + 2 + len;
  436. } else {
  437. memcpy(data, buf, len);
  438. port->write_urb->transfer_buffer_length = len;
  439. }
  440. ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  441. if (ret == 0) {
  442. priv->dp_write_urb_in_use = 1;
  443. priv->dp_out_buf_len = 0;
  444. count -= len;
  445. buf += len;
  446. }
  447. }
  448. spin_unlock_irqrestore(&priv->dp_port_lock, flags);
  449. if (ret)
  450. dev_err(&port->dev,
  451. "%s: usb_submit_urb failed, ret=%d, port=%d\n",
  452. __func__, ret, priv->dp_port_num);
  453. return ret;
  454. }
  455. /*
  456. * Digi Set Modem Signals
  457. *
  458. * Sets or clears DTR and RTS on the port, according to the
  459. * modem_signals argument. Use TIOCM_DTR and TIOCM_RTS flags
  460. * for the modem_signals argument. Returns 0 if successful,
  461. * -EINTR if interrupted while sleeping, or a non-zero error
  462. * returned by usb_submit_urb.
  463. */
  464. static int digi_set_modem_signals(struct usb_serial_port *port,
  465. unsigned int modem_signals, int interruptible)
  466. {
  467. int ret;
  468. struct digi_port *port_priv = usb_get_serial_port_data(port);
  469. struct usb_serial_port *oob_port = (struct usb_serial_port *) ((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
  470. struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
  471. unsigned char *data = oob_port->write_urb->transfer_buffer;
  472. unsigned long flags = 0;
  473. dev_dbg(&port->dev,
  474. "digi_set_modem_signals: TOP: port=%d, modem_signals=0x%x\n",
  475. port_priv->dp_port_num, modem_signals);
  476. spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
  477. spin_lock(&port_priv->dp_port_lock);
  478. while (oob_priv->dp_write_urb_in_use) {
  479. spin_unlock(&port_priv->dp_port_lock);
  480. cond_wait_interruptible_timeout_irqrestore(
  481. &oob_port->write_wait, DIGI_RETRY_TIMEOUT,
  482. &oob_priv->dp_port_lock, flags);
  483. if (interruptible && signal_pending(current))
  484. return -EINTR;
  485. spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
  486. spin_lock(&port_priv->dp_port_lock);
  487. }
  488. data[0] = DIGI_CMD_SET_DTR_SIGNAL;
  489. data[1] = port_priv->dp_port_num;
  490. data[2] = (modem_signals & TIOCM_DTR) ?
  491. DIGI_DTR_ACTIVE : DIGI_DTR_INACTIVE;
  492. data[3] = 0;
  493. data[4] = DIGI_CMD_SET_RTS_SIGNAL;
  494. data[5] = port_priv->dp_port_num;
  495. data[6] = (modem_signals & TIOCM_RTS) ?
  496. DIGI_RTS_ACTIVE : DIGI_RTS_INACTIVE;
  497. data[7] = 0;
  498. oob_port->write_urb->transfer_buffer_length = 8;
  499. ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
  500. if (ret == 0) {
  501. oob_priv->dp_write_urb_in_use = 1;
  502. port_priv->dp_modem_signals =
  503. (port_priv->dp_modem_signals&~(TIOCM_DTR|TIOCM_RTS))
  504. | (modem_signals&(TIOCM_DTR|TIOCM_RTS));
  505. }
  506. spin_unlock(&port_priv->dp_port_lock);
  507. spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
  508. if (ret)
  509. dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n",
  510. __func__, ret);
  511. return ret;
  512. }
  513. /*
  514. * Digi Transmit Idle
  515. *
  516. * Digi transmit idle waits, up to timeout ticks, for the transmitter
  517. * to go idle. It returns 0 if successful or a negative error.
  518. *
  519. * There are race conditions here if more than one process is calling
  520. * digi_transmit_idle on the same port at the same time. However, this
  521. * is only called from close, and only one process can be in close on a
  522. * port at a time, so its ok.
  523. */
  524. static int digi_transmit_idle(struct usb_serial_port *port,
  525. unsigned long timeout)
  526. {
  527. int ret;
  528. unsigned char buf[2];
  529. struct digi_port *priv = usb_get_serial_port_data(port);
  530. unsigned long flags = 0;
  531. spin_lock_irqsave(&priv->dp_port_lock, flags);
  532. priv->dp_transmit_idle = 0;
  533. spin_unlock_irqrestore(&priv->dp_port_lock, flags);
  534. buf[0] = DIGI_CMD_TRANSMIT_IDLE;
  535. buf[1] = 0;
  536. timeout += jiffies;
  537. ret = digi_write_inb_command(port, buf, 2, timeout - jiffies);
  538. if (ret != 0)
  539. return ret;
  540. spin_lock_irqsave(&priv->dp_port_lock, flags);
  541. while (time_before(jiffies, timeout) && !priv->dp_transmit_idle) {
  542. cond_wait_interruptible_timeout_irqrestore(
  543. &priv->dp_transmit_idle_wait, DIGI_RETRY_TIMEOUT,
  544. &priv->dp_port_lock, flags);
  545. if (signal_pending(current))
  546. return -EINTR;
  547. spin_lock_irqsave(&priv->dp_port_lock, flags);
  548. }
  549. priv->dp_transmit_idle = 0;
  550. spin_unlock_irqrestore(&priv->dp_port_lock, flags);
  551. return 0;
  552. }
  553. static void digi_rx_throttle(struct tty_struct *tty)
  554. {
  555. unsigned long flags;
  556. struct usb_serial_port *port = tty->driver_data;
  557. struct digi_port *priv = usb_get_serial_port_data(port);
  558. /* stop receiving characters by not resubmitting the read urb */
  559. spin_lock_irqsave(&priv->dp_port_lock, flags);
  560. priv->dp_throttled = 1;
  561. priv->dp_throttle_restart = 0;
  562. spin_unlock_irqrestore(&priv->dp_port_lock, flags);
  563. }
  564. static void digi_rx_unthrottle(struct tty_struct *tty)
  565. {
  566. int ret = 0;
  567. unsigned long flags;
  568. struct usb_serial_port *port = tty->driver_data;
  569. struct digi_port *priv = usb_get_serial_port_data(port);
  570. spin_lock_irqsave(&priv->dp_port_lock, flags);
  571. /* restart read chain */
  572. if (priv->dp_throttle_restart)
  573. ret = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  574. /* turn throttle off */
  575. priv->dp_throttled = 0;
  576. priv->dp_throttle_restart = 0;
  577. spin_unlock_irqrestore(&priv->dp_port_lock, flags);
  578. if (ret)
  579. dev_err(&port->dev,
  580. "%s: usb_submit_urb failed, ret=%d, port=%d\n",
  581. __func__, ret, priv->dp_port_num);
  582. }
  583. static void digi_set_termios(struct tty_struct *tty,
  584. struct usb_serial_port *port, struct ktermios *old_termios)
  585. {
  586. struct digi_port *priv = usb_get_serial_port_data(port);
  587. struct device *dev = &port->dev;
  588. unsigned int iflag = tty->termios->c_iflag;
  589. unsigned int cflag = tty->termios->c_cflag;
  590. unsigned int old_iflag = old_termios->c_iflag;
  591. unsigned int old_cflag = old_termios->c_cflag;
  592. unsigned char buf[32];
  593. unsigned int modem_signals;
  594. int arg, ret;
  595. int i = 0;
  596. speed_t baud;
  597. dev_dbg(dev,
  598. "digi_set_termios: TOP: port=%d, iflag=0x%x, old_iflag=0x%x, cflag=0x%x, old_cflag=0x%x\n",
  599. priv->dp_port_num, iflag, old_iflag, cflag, old_cflag);
  600. /* set baud rate */
  601. baud = tty_get_baud_rate(tty);
  602. if (baud != tty_termios_baud_rate(old_termios)) {
  603. arg = -1;
  604. /* reassert DTR and (maybe) RTS on transition from B0 */
  605. if ((old_cflag&CBAUD) == B0) {
  606. /* don't set RTS if using hardware flow control */
  607. /* and throttling input */
  608. modem_signals = TIOCM_DTR;
  609. if (!(tty->termios->c_cflag & CRTSCTS) ||
  610. !test_bit(TTY_THROTTLED, &tty->flags))
  611. modem_signals |= TIOCM_RTS;
  612. digi_set_modem_signals(port, modem_signals, 1);
  613. }
  614. switch (baud) {
  615. /* drop DTR and RTS on transition to B0 */
  616. case 0: digi_set_modem_signals(port, 0, 1); break;
  617. case 50: arg = DIGI_BAUD_50; break;
  618. case 75: arg = DIGI_BAUD_75; break;
  619. case 110: arg = DIGI_BAUD_110; break;
  620. case 150: arg = DIGI_BAUD_150; break;
  621. case 200: arg = DIGI_BAUD_200; break;
  622. case 300: arg = DIGI_BAUD_300; break;
  623. case 600: arg = DIGI_BAUD_600; break;
  624. case 1200: arg = DIGI_BAUD_1200; break;
  625. case 1800: arg = DIGI_BAUD_1800; break;
  626. case 2400: arg = DIGI_BAUD_2400; break;
  627. case 4800: arg = DIGI_BAUD_4800; break;
  628. case 9600: arg = DIGI_BAUD_9600; break;
  629. case 19200: arg = DIGI_BAUD_19200; break;
  630. case 38400: arg = DIGI_BAUD_38400; break;
  631. case 57600: arg = DIGI_BAUD_57600; break;
  632. case 115200: arg = DIGI_BAUD_115200; break;
  633. case 230400: arg = DIGI_BAUD_230400; break;
  634. case 460800: arg = DIGI_BAUD_460800; break;
  635. default:
  636. arg = DIGI_BAUD_9600;
  637. baud = 9600;
  638. break;
  639. }
  640. if (arg != -1) {
  641. buf[i++] = DIGI_CMD_SET_BAUD_RATE;
  642. buf[i++] = priv->dp_port_num;
  643. buf[i++] = arg;
  644. buf[i++] = 0;
  645. }
  646. }
  647. /* set parity */
  648. tty->termios->c_cflag &= ~CMSPAR;
  649. if ((cflag&(PARENB|PARODD)) != (old_cflag&(PARENB|PARODD))) {
  650. if (cflag&PARENB) {
  651. if (cflag&PARODD)
  652. arg = DIGI_PARITY_ODD;
  653. else
  654. arg = DIGI_PARITY_EVEN;
  655. } else {
  656. arg = DIGI_PARITY_NONE;
  657. }
  658. buf[i++] = DIGI_CMD_SET_PARITY;
  659. buf[i++] = priv->dp_port_num;
  660. buf[i++] = arg;
  661. buf[i++] = 0;
  662. }
  663. /* set word size */
  664. if ((cflag&CSIZE) != (old_cflag&CSIZE)) {
  665. arg = -1;
  666. switch (cflag&CSIZE) {
  667. case CS5: arg = DIGI_WORD_SIZE_5; break;
  668. case CS6: arg = DIGI_WORD_SIZE_6; break;
  669. case CS7: arg = DIGI_WORD_SIZE_7; break;
  670. case CS8: arg = DIGI_WORD_SIZE_8; break;
  671. default:
  672. dev_dbg(dev,
  673. "digi_set_termios: can't handle word size %d\n",
  674. (cflag&CSIZE));
  675. break;
  676. }
  677. if (arg != -1) {
  678. buf[i++] = DIGI_CMD_SET_WORD_SIZE;
  679. buf[i++] = priv->dp_port_num;
  680. buf[i++] = arg;
  681. buf[i++] = 0;
  682. }
  683. }
  684. /* set stop bits */
  685. if ((cflag&CSTOPB) != (old_cflag&CSTOPB)) {
  686. if ((cflag&CSTOPB))
  687. arg = DIGI_STOP_BITS_2;
  688. else
  689. arg = DIGI_STOP_BITS_1;
  690. buf[i++] = DIGI_CMD_SET_STOP_BITS;
  691. buf[i++] = priv->dp_port_num;
  692. buf[i++] = arg;
  693. buf[i++] = 0;
  694. }
  695. /* set input flow control */
  696. if ((iflag&IXOFF) != (old_iflag&IXOFF)
  697. || (cflag&CRTSCTS) != (old_cflag&CRTSCTS)) {
  698. arg = 0;
  699. if (iflag&IXOFF)
  700. arg |= DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
  701. else
  702. arg &= ~DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
  703. if (cflag&CRTSCTS) {
  704. arg |= DIGI_INPUT_FLOW_CONTROL_RTS;
  705. /* On USB-4 it is necessary to assert RTS prior */
  706. /* to selecting RTS input flow control. */
  707. buf[i++] = DIGI_CMD_SET_RTS_SIGNAL;
  708. buf[i++] = priv->dp_port_num;
  709. buf[i++] = DIGI_RTS_ACTIVE;
  710. buf[i++] = 0;
  711. } else {
  712. arg &= ~DIGI_INPUT_FLOW_CONTROL_RTS;
  713. }
  714. buf[i++] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
  715. buf[i++] = priv->dp_port_num;
  716. buf[i++] = arg;
  717. buf[i++] = 0;
  718. }
  719. /* set output flow control */
  720. if ((iflag & IXON) != (old_iflag & IXON)
  721. || (cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
  722. arg = 0;
  723. if (iflag & IXON)
  724. arg |= DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
  725. else
  726. arg &= ~DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
  727. if (cflag & CRTSCTS) {
  728. arg |= DIGI_OUTPUT_FLOW_CONTROL_CTS;
  729. } else {
  730. arg &= ~DIGI_OUTPUT_FLOW_CONTROL_CTS;
  731. tty->hw_stopped = 0;
  732. }
  733. buf[i++] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
  734. buf[i++] = priv->dp_port_num;
  735. buf[i++] = arg;
  736. buf[i++] = 0;
  737. }
  738. /* set receive enable/disable */
  739. if ((cflag & CREAD) != (old_cflag & CREAD)) {
  740. if (cflag & CREAD)
  741. arg = DIGI_ENABLE;
  742. else
  743. arg = DIGI_DISABLE;
  744. buf[i++] = DIGI_CMD_RECEIVE_ENABLE;
  745. buf[i++] = priv->dp_port_num;
  746. buf[i++] = arg;
  747. buf[i++] = 0;
  748. }
  749. ret = digi_write_oob_command(port, buf, i, 1);
  750. if (ret != 0)
  751. dev_dbg(dev, "digi_set_termios: write oob failed, ret=%d\n", ret);
  752. tty_encode_baud_rate(tty, baud, baud);
  753. }
  754. static void digi_break_ctl(struct tty_struct *tty, int break_state)
  755. {
  756. struct usb_serial_port *port = tty->driver_data;
  757. unsigned char buf[4];
  758. buf[0] = DIGI_CMD_BREAK_CONTROL;
  759. buf[1] = 2; /* length */
  760. buf[2] = break_state ? 1 : 0;
  761. buf[3] = 0; /* pad */
  762. digi_write_inb_command(port, buf, 4, 0);
  763. }
  764. static int digi_tiocmget(struct tty_struct *tty)
  765. {
  766. struct usb_serial_port *port = tty->driver_data;
  767. struct digi_port *priv = usb_get_serial_port_data(port);
  768. unsigned int val;
  769. unsigned long flags;
  770. spin_lock_irqsave(&priv->dp_port_lock, flags);
  771. val = priv->dp_modem_signals;
  772. spin_unlock_irqrestore(&priv->dp_port_lock, flags);
  773. return val;
  774. }
  775. static int digi_tiocmset(struct tty_struct *tty,
  776. unsigned int set, unsigned int clear)
  777. {
  778. struct usb_serial_port *port = tty->driver_data;
  779. struct digi_port *priv = usb_get_serial_port_data(port);
  780. unsigned int val;
  781. unsigned long flags;
  782. spin_lock_irqsave(&priv->dp_port_lock, flags);
  783. val = (priv->dp_modem_signals & ~clear) | set;
  784. spin_unlock_irqrestore(&priv->dp_port_lock, flags);
  785. return digi_set_modem_signals(port, val, 1);
  786. }
  787. static int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
  788. const unsigned char *buf, int count)
  789. {
  790. int ret, data_len, new_len;
  791. struct digi_port *priv = usb_get_serial_port_data(port);
  792. unsigned char *data = port->write_urb->transfer_buffer;
  793. unsigned long flags = 0;
  794. dev_dbg(&port->dev,
  795. "digi_write: TOP: port=%d, count=%d, in_interrupt=%ld\n",
  796. priv->dp_port_num, count, in_interrupt());
  797. /* copy user data (which can sleep) before getting spin lock */
  798. count = min(count, port->bulk_out_size-2);
  799. count = min(64, count);
  800. /* be sure only one write proceeds at a time */
  801. /* there are races on the port private buffer */
  802. spin_lock_irqsave(&priv->dp_port_lock, flags);
  803. /* wait for urb status clear to submit another urb */
  804. if (priv->dp_write_urb_in_use) {
  805. /* buffer data if count is 1 (probably put_char) if possible */
  806. if (count == 1 && priv->dp_out_buf_len < DIGI_OUT_BUF_SIZE) {
  807. priv->dp_out_buf[priv->dp_out_buf_len++] = *buf;
  808. new_len = 1;
  809. } else {
  810. new_len = 0;
  811. }
  812. spin_unlock_irqrestore(&priv->dp_port_lock, flags);
  813. return new_len;
  814. }
  815. /* allow space for any buffered data and for new data, up to */
  816. /* transfer buffer size - 2 (for command and length bytes) */
  817. new_len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
  818. data_len = new_len + priv->dp_out_buf_len;
  819. if (data_len == 0) {
  820. spin_unlock_irqrestore(&priv->dp_port_lock, flags);
  821. return 0;
  822. }
  823. port->write_urb->transfer_buffer_length = data_len+2;
  824. *data++ = DIGI_CMD_SEND_DATA;
  825. *data++ = data_len;
  826. /* copy in buffered data first */
  827. memcpy(data, priv->dp_out_buf, priv->dp_out_buf_len);
  828. data += priv->dp_out_buf_len;
  829. /* copy in new data */
  830. memcpy(data, buf, new_len);
  831. ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  832. if (ret == 0) {
  833. priv->dp_write_urb_in_use = 1;
  834. ret = new_len;
  835. priv->dp_out_buf_len = 0;
  836. }
  837. /* return length of new data written, or error */
  838. spin_unlock_irqrestore(&priv->dp_port_lock, flags);
  839. if (ret < 0)
  840. dev_err_console(port,
  841. "%s: usb_submit_urb failed, ret=%d, port=%d\n",
  842. __func__, ret, priv->dp_port_num);
  843. dev_dbg(&port->dev, "digi_write: returning %d\n", ret);
  844. return ret;
  845. }
  846. static void digi_write_bulk_callback(struct urb *urb)
  847. {
  848. struct usb_serial_port *port = urb->context;
  849. struct usb_serial *serial;
  850. struct digi_port *priv;
  851. struct digi_serial *serial_priv;
  852. int ret = 0;
  853. int status = urb->status;
  854. /* port and serial sanity check */
  855. if (port == NULL || (priv = usb_get_serial_port_data(port)) == NULL) {
  856. pr_err("%s: port or port->private is NULL, status=%d\n",
  857. __func__, status);
  858. return;
  859. }
  860. serial = port->serial;
  861. if (serial == NULL || (serial_priv = usb_get_serial_data(serial)) == NULL) {
  862. dev_err(&port->dev,
  863. "%s: serial or serial->private is NULL, status=%d\n",
  864. __func__, status);
  865. return;
  866. }
  867. /* handle oob callback */
  868. if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
  869. dev_dbg(&port->dev, "digi_write_bulk_callback: oob callback\n");
  870. spin_lock(&priv->dp_port_lock);
  871. priv->dp_write_urb_in_use = 0;
  872. wake_up_interruptible(&port->write_wait);
  873. spin_unlock(&priv->dp_port_lock);
  874. return;
  875. }
  876. /* try to send any buffered data on this port */
  877. spin_lock(&priv->dp_port_lock);
  878. priv->dp_write_urb_in_use = 0;
  879. if (priv->dp_out_buf_len > 0) {
  880. *((unsigned char *)(port->write_urb->transfer_buffer))
  881. = (unsigned char)DIGI_CMD_SEND_DATA;
  882. *((unsigned char *)(port->write_urb->transfer_buffer) + 1)
  883. = (unsigned char)priv->dp_out_buf_len;
  884. port->write_urb->transfer_buffer_length =
  885. priv->dp_out_buf_len + 2;
  886. memcpy(port->write_urb->transfer_buffer + 2, priv->dp_out_buf,
  887. priv->dp_out_buf_len);
  888. ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  889. if (ret == 0) {
  890. priv->dp_write_urb_in_use = 1;
  891. priv->dp_out_buf_len = 0;
  892. }
  893. }
  894. /* wake up processes sleeping on writes immediately */
  895. digi_wakeup_write(port);
  896. /* also queue up a wakeup at scheduler time, in case we */
  897. /* lost the race in write_chan(). */
  898. schedule_work(&priv->dp_wakeup_work);
  899. spin_unlock(&priv->dp_port_lock);
  900. if (ret && ret != -EPERM)
  901. dev_err_console(port,
  902. "%s: usb_submit_urb failed, ret=%d, port=%d\n",
  903. __func__, ret, priv->dp_port_num);
  904. }
  905. static int digi_write_room(struct tty_struct *tty)
  906. {
  907. struct usb_serial_port *port = tty->driver_data;
  908. struct digi_port *priv = usb_get_serial_port_data(port);
  909. int room;
  910. unsigned long flags = 0;
  911. spin_lock_irqsave(&priv->dp_port_lock, flags);
  912. if (priv->dp_write_urb_in_use)
  913. room = 0;
  914. else
  915. room = port->bulk_out_size - 2 - priv->dp_out_buf_len;
  916. spin_unlock_irqrestore(&priv->dp_port_lock, flags);
  917. dev_dbg(&port->dev, "digi_write_room: port=%d, room=%d\n", priv->dp_port_num, room);
  918. return room;
  919. }
  920. static int digi_chars_in_buffer(struct tty_struct *tty)
  921. {
  922. struct usb_serial_port *port = tty->driver_data;
  923. struct digi_port *priv = usb_get_serial_port_data(port);
  924. if (priv->dp_write_urb_in_use) {
  925. dev_dbg(&port->dev, "digi_chars_in_buffer: port=%d, chars=%d\n",
  926. priv->dp_port_num, port->bulk_out_size - 2);
  927. /* return(port->bulk_out_size - 2); */
  928. return 256;
  929. } else {
  930. dev_dbg(&port->dev, "digi_chars_in_buffer: port=%d, chars=%d\n",
  931. priv->dp_port_num, priv->dp_out_buf_len);
  932. return priv->dp_out_buf_len;
  933. }
  934. }
  935. static void digi_dtr_rts(struct usb_serial_port *port, int on)
  936. {
  937. /* Adjust DTR and RTS */
  938. digi_set_modem_signals(port, on * (TIOCM_DTR|TIOCM_RTS), 1);
  939. }
  940. static int digi_open(struct tty_struct *tty, struct usb_serial_port *port)
  941. {
  942. int ret;
  943. unsigned char buf[32];
  944. struct digi_port *priv = usb_get_serial_port_data(port);
  945. struct ktermios not_termios;
  946. /* be sure the device is started up */
  947. if (digi_startup_device(port->serial) != 0)
  948. return -ENXIO;
  949. /* read modem signals automatically whenever they change */
  950. buf[0] = DIGI_CMD_READ_INPUT_SIGNALS;
  951. buf[1] = priv->dp_port_num;
  952. buf[2] = DIGI_ENABLE;
  953. buf[3] = 0;
  954. /* flush fifos */
  955. buf[4] = DIGI_CMD_IFLUSH_FIFO;
  956. buf[5] = priv->dp_port_num;
  957. buf[6] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
  958. buf[7] = 0;
  959. ret = digi_write_oob_command(port, buf, 8, 1);
  960. if (ret != 0)
  961. dev_dbg(&port->dev, "digi_open: write oob failed, ret=%d\n", ret);
  962. /* set termios settings */
  963. if (tty) {
  964. not_termios.c_cflag = ~tty->termios->c_cflag;
  965. not_termios.c_iflag = ~tty->termios->c_iflag;
  966. digi_set_termios(tty, port, &not_termios);
  967. }
  968. return 0;
  969. }
  970. static void digi_close(struct usb_serial_port *port)
  971. {
  972. DEFINE_WAIT(wait);
  973. int ret;
  974. unsigned char buf[32];
  975. struct digi_port *priv = usb_get_serial_port_data(port);
  976. mutex_lock(&port->serial->disc_mutex);
  977. /* if disconnected, just clear flags */
  978. if (port->serial->disconnected)
  979. goto exit;
  980. if (port->serial->dev) {
  981. /* FIXME: Transmit idle belongs in the wait_unti_sent path */
  982. digi_transmit_idle(port, DIGI_CLOSE_TIMEOUT);
  983. /* disable input flow control */
  984. buf[0] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
  985. buf[1] = priv->dp_port_num;
  986. buf[2] = DIGI_DISABLE;
  987. buf[3] = 0;
  988. /* disable output flow control */
  989. buf[4] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
  990. buf[5] = priv->dp_port_num;
  991. buf[6] = DIGI_DISABLE;
  992. buf[7] = 0;
  993. /* disable reading modem signals automatically */
  994. buf[8] = DIGI_CMD_READ_INPUT_SIGNALS;
  995. buf[9] = priv->dp_port_num;
  996. buf[10] = DIGI_DISABLE;
  997. buf[11] = 0;
  998. /* disable receive */
  999. buf[12] = DIGI_CMD_RECEIVE_ENABLE;
  1000. buf[13] = priv->dp_port_num;
  1001. buf[14] = DIGI_DISABLE;
  1002. buf[15] = 0;
  1003. /* flush fifos */
  1004. buf[16] = DIGI_CMD_IFLUSH_FIFO;
  1005. buf[17] = priv->dp_port_num;
  1006. buf[18] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
  1007. buf[19] = 0;
  1008. ret = digi_write_oob_command(port, buf, 20, 0);
  1009. if (ret != 0)
  1010. dev_dbg(&port->dev, "digi_close: write oob failed, ret=%d\n", ret);
  1011. /* wait for final commands on oob port to complete */
  1012. prepare_to_wait(&priv->dp_flush_wait, &wait,
  1013. TASK_INTERRUPTIBLE);
  1014. schedule_timeout(DIGI_CLOSE_TIMEOUT);
  1015. finish_wait(&priv->dp_flush_wait, &wait);
  1016. /* shutdown any outstanding bulk writes */
  1017. usb_kill_urb(port->write_urb);
  1018. }
  1019. exit:
  1020. spin_lock_irq(&priv->dp_port_lock);
  1021. priv->dp_write_urb_in_use = 0;
  1022. wake_up_interruptible(&priv->dp_close_wait);
  1023. spin_unlock_irq(&priv->dp_port_lock);
  1024. mutex_unlock(&port->serial->disc_mutex);
  1025. }
  1026. /*
  1027. * Digi Startup Device
  1028. *
  1029. * Starts reads on all ports. Must be called AFTER startup, with
  1030. * urbs initialized. Returns 0 if successful, non-zero error otherwise.
  1031. */
  1032. static int digi_startup_device(struct usb_serial *serial)
  1033. {
  1034. int i, ret = 0;
  1035. struct digi_serial *serial_priv = usb_get_serial_data(serial);
  1036. struct usb_serial_port *port;
  1037. /* be sure this happens exactly once */
  1038. spin_lock(&serial_priv->ds_serial_lock);
  1039. if (serial_priv->ds_device_started) {
  1040. spin_unlock(&serial_priv->ds_serial_lock);
  1041. return 0;
  1042. }
  1043. serial_priv->ds_device_started = 1;
  1044. spin_unlock(&serial_priv->ds_serial_lock);
  1045. /* start reading from each bulk in endpoint for the device */
  1046. /* set USB_DISABLE_SPD flag for write bulk urbs */
  1047. for (i = 0; i < serial->type->num_ports + 1; i++) {
  1048. port = serial->port[i];
  1049. ret = usb_submit_urb(port->read_urb, GFP_KERNEL);
  1050. if (ret != 0) {
  1051. dev_err(&port->dev,
  1052. "%s: usb_submit_urb failed, ret=%d, port=%d\n",
  1053. __func__, ret, i);
  1054. break;
  1055. }
  1056. }
  1057. return ret;
  1058. }
  1059. static int digi_startup(struct usb_serial *serial)
  1060. {
  1061. int i;
  1062. struct digi_port *priv;
  1063. struct digi_serial *serial_priv;
  1064. /* allocate the private data structures for all ports */
  1065. /* number of regular ports + 1 for the out-of-band port */
  1066. for (i = 0; i < serial->type->num_ports + 1; i++) {
  1067. /* allocate port private structure */
  1068. priv = kmalloc(sizeof(struct digi_port), GFP_KERNEL);
  1069. if (priv == NULL) {
  1070. while (--i >= 0)
  1071. kfree(usb_get_serial_port_data(serial->port[i]));
  1072. return 1; /* error */
  1073. }
  1074. /* initialize port private structure */
  1075. spin_lock_init(&priv->dp_port_lock);
  1076. priv->dp_port_num = i;
  1077. priv->dp_out_buf_len = 0;
  1078. priv->dp_write_urb_in_use = 0;
  1079. priv->dp_modem_signals = 0;
  1080. init_waitqueue_head(&priv->dp_modem_change_wait);
  1081. priv->dp_transmit_idle = 0;
  1082. init_waitqueue_head(&priv->dp_transmit_idle_wait);
  1083. priv->dp_throttled = 0;
  1084. priv->dp_throttle_restart = 0;
  1085. init_waitqueue_head(&priv->dp_flush_wait);
  1086. init_waitqueue_head(&priv->dp_close_wait);
  1087. INIT_WORK(&priv->dp_wakeup_work, digi_wakeup_write_lock);
  1088. priv->dp_port = serial->port[i];
  1089. /* initialize write wait queue for this port */
  1090. init_waitqueue_head(&serial->port[i]->write_wait);
  1091. usb_set_serial_port_data(serial->port[i], priv);
  1092. }
  1093. /* allocate serial private structure */
  1094. serial_priv = kmalloc(sizeof(struct digi_serial), GFP_KERNEL);
  1095. if (serial_priv == NULL) {
  1096. for (i = 0; i < serial->type->num_ports + 1; i++)
  1097. kfree(usb_get_serial_port_data(serial->port[i]));
  1098. return 1; /* error */
  1099. }
  1100. /* initialize serial private structure */
  1101. spin_lock_init(&serial_priv->ds_serial_lock);
  1102. serial_priv->ds_oob_port_num = serial->type->num_ports;
  1103. serial_priv->ds_oob_port = serial->port[serial_priv->ds_oob_port_num];
  1104. serial_priv->ds_device_started = 0;
  1105. usb_set_serial_data(serial, serial_priv);
  1106. return 0;
  1107. }
  1108. static void digi_disconnect(struct usb_serial *serial)
  1109. {
  1110. int i;
  1111. /* stop reads and writes on all ports */
  1112. for (i = 0; i < serial->type->num_ports + 1; i++) {
  1113. usb_kill_urb(serial->port[i]->read_urb);
  1114. usb_kill_urb(serial->port[i]->write_urb);
  1115. }
  1116. }
  1117. static void digi_release(struct usb_serial *serial)
  1118. {
  1119. int i;
  1120. /* free the private data structures for all ports */
  1121. /* number of regular ports + 1 for the out-of-band port */
  1122. for (i = 0; i < serial->type->num_ports + 1; i++)
  1123. kfree(usb_get_serial_port_data(serial->port[i]));
  1124. kfree(usb_get_serial_data(serial));
  1125. }
  1126. static void digi_read_bulk_callback(struct urb *urb)
  1127. {
  1128. struct usb_serial_port *port = urb->context;
  1129. struct digi_port *priv;
  1130. struct digi_serial *serial_priv;
  1131. int ret;
  1132. int status = urb->status;
  1133. /* port sanity check, do not resubmit if port is not valid */
  1134. if (port == NULL)
  1135. return;
  1136. priv = usb_get_serial_port_data(port);
  1137. if (priv == NULL) {
  1138. dev_err(&port->dev, "%s: port->private is NULL, status=%d\n",
  1139. __func__, status);
  1140. return;
  1141. }
  1142. if (port->serial == NULL ||
  1143. (serial_priv = usb_get_serial_data(port->serial)) == NULL) {
  1144. dev_err(&port->dev, "%s: serial is bad or serial->private "
  1145. "is NULL, status=%d\n", __func__, status);
  1146. return;
  1147. }
  1148. /* do not resubmit urb if it has any status error */
  1149. if (status) {
  1150. dev_err(&port->dev,
  1151. "%s: nonzero read bulk status: status=%d, port=%d\n",
  1152. __func__, status, priv->dp_port_num);
  1153. return;
  1154. }
  1155. /* handle oob or inb callback, do not resubmit if error */
  1156. if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
  1157. if (digi_read_oob_callback(urb) != 0)
  1158. return;
  1159. } else {
  1160. if (digi_read_inb_callback(urb) != 0)
  1161. return;
  1162. }
  1163. /* continue read */
  1164. ret = usb_submit_urb(urb, GFP_ATOMIC);
  1165. if (ret != 0 && ret != -EPERM) {
  1166. dev_err(&port->dev,
  1167. "%s: failed resubmitting urb, ret=%d, port=%d\n",
  1168. __func__, ret, priv->dp_port_num);
  1169. }
  1170. }
  1171. /*
  1172. * Digi Read INB Callback
  1173. *
  1174. * Digi Read INB Callback handles reads on the in band ports, sending
  1175. * the data on to the tty subsystem. When called we know port and
  1176. * port->private are not NULL and port->serial has been validated.
  1177. * It returns 0 if successful, 1 if successful but the port is
  1178. * throttled, and -1 if the sanity checks failed.
  1179. */
  1180. static int digi_read_inb_callback(struct urb *urb)
  1181. {
  1182. struct usb_serial_port *port = urb->context;
  1183. struct tty_struct *tty;
  1184. struct digi_port *priv = usb_get_serial_port_data(port);
  1185. int opcode = ((unsigned char *)urb->transfer_buffer)[0];
  1186. int len = ((unsigned char *)urb->transfer_buffer)[1];
  1187. int port_status = ((unsigned char *)urb->transfer_buffer)[2];
  1188. unsigned char *data = ((unsigned char *)urb->transfer_buffer) + 3;
  1189. int flag, throttled;
  1190. int status = urb->status;
  1191. /* do not process callbacks on closed ports */
  1192. /* but do continue the read chain */
  1193. if (urb->status == -ENOENT)
  1194. return 0;
  1195. /* short/multiple packet check */
  1196. if (urb->actual_length != len + 2) {
  1197. dev_err(&port->dev, "%s: INCOMPLETE OR MULTIPLE PACKET, "
  1198. "status=%d, port=%d, opcode=%d, len=%d, "
  1199. "actual_length=%d, status=%d\n", __func__, status,
  1200. priv->dp_port_num, opcode, len, urb->actual_length,
  1201. port_status);
  1202. return -1;
  1203. }
  1204. tty = tty_port_tty_get(&port->port);
  1205. spin_lock(&priv->dp_port_lock);
  1206. /* check for throttle; if set, do not resubmit read urb */
  1207. /* indicate the read chain needs to be restarted on unthrottle */
  1208. throttled = priv->dp_throttled;
  1209. if (throttled)
  1210. priv->dp_throttle_restart = 1;
  1211. /* receive data */
  1212. if (tty && opcode == DIGI_CMD_RECEIVE_DATA) {
  1213. /* get flag from port_status */
  1214. flag = 0;
  1215. /* overrun is special, not associated with a char */
  1216. if (port_status & DIGI_OVERRUN_ERROR)
  1217. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  1218. /* break takes precedence over parity, */
  1219. /* which takes precedence over framing errors */
  1220. if (port_status & DIGI_BREAK_ERROR)
  1221. flag = TTY_BREAK;
  1222. else if (port_status & DIGI_PARITY_ERROR)
  1223. flag = TTY_PARITY;
  1224. else if (port_status & DIGI_FRAMING_ERROR)
  1225. flag = TTY_FRAME;
  1226. /* data length is len-1 (one byte of len is port_status) */
  1227. --len;
  1228. if (len > 0) {
  1229. tty_insert_flip_string_fixed_flag(tty, data, flag,
  1230. len);
  1231. tty_flip_buffer_push(tty);
  1232. }
  1233. }
  1234. spin_unlock(&priv->dp_port_lock);
  1235. tty_kref_put(tty);
  1236. if (opcode == DIGI_CMD_RECEIVE_DISABLE)
  1237. dev_dbg(&port->dev, "%s: got RECEIVE_DISABLE\n", __func__);
  1238. else if (opcode != DIGI_CMD_RECEIVE_DATA)
  1239. dev_dbg(&port->dev, "%s: unknown opcode: %d\n", __func__, opcode);
  1240. return throttled ? 1 : 0;
  1241. }
  1242. /*
  1243. * Digi Read OOB Callback
  1244. *
  1245. * Digi Read OOB Callback handles reads on the out of band port.
  1246. * When called we know port and port->private are not NULL and
  1247. * the port->serial is valid. It returns 0 if successful, and
  1248. * -1 if the sanity checks failed.
  1249. */
  1250. static int digi_read_oob_callback(struct urb *urb)
  1251. {
  1252. struct usb_serial_port *port = urb->context;
  1253. struct usb_serial *serial = port->serial;
  1254. struct tty_struct *tty;
  1255. struct digi_port *priv = usb_get_serial_port_data(port);
  1256. int opcode, line, status, val;
  1257. int i;
  1258. unsigned int rts;
  1259. /* handle each oob command */
  1260. for (i = 0; i < urb->actual_length - 3;) {
  1261. opcode = ((unsigned char *)urb->transfer_buffer)[i++];
  1262. line = ((unsigned char *)urb->transfer_buffer)[i++];
  1263. status = ((unsigned char *)urb->transfer_buffer)[i++];
  1264. val = ((unsigned char *)urb->transfer_buffer)[i++];
  1265. dev_dbg(&port->dev, "digi_read_oob_callback: opcode=%d, line=%d, status=%d, val=%d\n",
  1266. opcode, line, status, val);
  1267. if (status != 0 || line >= serial->type->num_ports)
  1268. continue;
  1269. port = serial->port[line];
  1270. priv = usb_get_serial_port_data(port);
  1271. if (priv == NULL)
  1272. return -1;
  1273. tty = tty_port_tty_get(&port->port);
  1274. rts = 0;
  1275. if (tty)
  1276. rts = tty->termios->c_cflag & CRTSCTS;
  1277. if (tty && opcode == DIGI_CMD_READ_INPUT_SIGNALS) {
  1278. spin_lock(&priv->dp_port_lock);
  1279. /* convert from digi flags to termiox flags */
  1280. if (val & DIGI_READ_INPUT_SIGNALS_CTS) {
  1281. priv->dp_modem_signals |= TIOCM_CTS;
  1282. /* port must be open to use tty struct */
  1283. if (rts) {
  1284. tty->hw_stopped = 0;
  1285. digi_wakeup_write(port);
  1286. }
  1287. } else {
  1288. priv->dp_modem_signals &= ~TIOCM_CTS;
  1289. /* port must be open to use tty struct */
  1290. if (rts)
  1291. tty->hw_stopped = 1;
  1292. }
  1293. if (val & DIGI_READ_INPUT_SIGNALS_DSR)
  1294. priv->dp_modem_signals |= TIOCM_DSR;
  1295. else
  1296. priv->dp_modem_signals &= ~TIOCM_DSR;
  1297. if (val & DIGI_READ_INPUT_SIGNALS_RI)
  1298. priv->dp_modem_signals |= TIOCM_RI;
  1299. else
  1300. priv->dp_modem_signals &= ~TIOCM_RI;
  1301. if (val & DIGI_READ_INPUT_SIGNALS_DCD)
  1302. priv->dp_modem_signals |= TIOCM_CD;
  1303. else
  1304. priv->dp_modem_signals &= ~TIOCM_CD;
  1305. wake_up_interruptible(&priv->dp_modem_change_wait);
  1306. spin_unlock(&priv->dp_port_lock);
  1307. } else if (opcode == DIGI_CMD_TRANSMIT_IDLE) {
  1308. spin_lock(&priv->dp_port_lock);
  1309. priv->dp_transmit_idle = 1;
  1310. wake_up_interruptible(&priv->dp_transmit_idle_wait);
  1311. spin_unlock(&priv->dp_port_lock);
  1312. } else if (opcode == DIGI_CMD_IFLUSH_FIFO) {
  1313. wake_up_interruptible(&priv->dp_flush_wait);
  1314. }
  1315. tty_kref_put(tty);
  1316. }
  1317. return 0;
  1318. }
  1319. module_usb_serial_driver(serial_drivers, id_table_combined);
  1320. MODULE_AUTHOR(DRIVER_AUTHOR);
  1321. MODULE_DESCRIPTION(DRIVER_DESC);
  1322. MODULE_LICENSE("GPL");