digi_acceleport.c 45 KB

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