digi_acceleport.c 43 KB

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