digi_acceleport.c 44 KB

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