digi_acceleport.c 43 KB

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