digi_acceleport.c 44 KB

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