digi_acceleport.c 46 KB

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