digi_acceleport.c 42 KB

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