digi_acceleport.c 44 KB

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