digi_acceleport.c 42 KB

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