digi_acceleport.c 43 KB

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