mxuport.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * mxuport.c - MOXA UPort series driver
  4. *
  5. * Copyright (c) 2006 Moxa Technologies Co., Ltd.
  6. * Copyright (c) 2013 Andrew Lunn <[email protected]>
  7. *
  8. * Supports the following Moxa USB to serial converters:
  9. * 2 ports : UPort 1250, UPort 1250I
  10. * 4 ports : UPort 1410, UPort 1450, UPort 1450I
  11. * 8 ports : UPort 1610-8, UPort 1650-8
  12. * 16 ports : UPort 1610-16, UPort 1650-16
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/firmware.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/serial.h>
  19. #include <linux/serial_reg.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/uaccess.h>
  25. #include <linux/usb.h>
  26. #include <linux/usb/serial.h>
  27. #include <asm/unaligned.h>
  28. /* Definitions for the vendor ID and device ID */
  29. #define MX_USBSERIAL_VID 0x110A
  30. #define MX_UPORT1250_PID 0x1250
  31. #define MX_UPORT1251_PID 0x1251
  32. #define MX_UPORT1410_PID 0x1410
  33. #define MX_UPORT1450_PID 0x1450
  34. #define MX_UPORT1451_PID 0x1451
  35. #define MX_UPORT1618_PID 0x1618
  36. #define MX_UPORT1658_PID 0x1658
  37. #define MX_UPORT1613_PID 0x1613
  38. #define MX_UPORT1653_PID 0x1653
  39. /* Definitions for USB info */
  40. #define HEADER_SIZE 4
  41. #define EVENT_LENGTH 8
  42. #define DOWN_BLOCK_SIZE 64
  43. /* Definitions for firmware info */
  44. #define VER_ADDR_1 0x20
  45. #define VER_ADDR_2 0x24
  46. #define VER_ADDR_3 0x28
  47. /* Definitions for USB vendor request */
  48. #define RQ_VENDOR_NONE 0x00
  49. #define RQ_VENDOR_SET_BAUD 0x01 /* Set baud rate */
  50. #define RQ_VENDOR_SET_LINE 0x02 /* Set line status */
  51. #define RQ_VENDOR_SET_CHARS 0x03 /* Set Xon/Xoff chars */
  52. #define RQ_VENDOR_SET_RTS 0x04 /* Set RTS */
  53. #define RQ_VENDOR_SET_DTR 0x05 /* Set DTR */
  54. #define RQ_VENDOR_SET_XONXOFF 0x06 /* Set auto Xon/Xoff */
  55. #define RQ_VENDOR_SET_RX_HOST_EN 0x07 /* Set RX host enable */
  56. #define RQ_VENDOR_SET_OPEN 0x08 /* Set open/close port */
  57. #define RQ_VENDOR_PURGE 0x09 /* Purge Rx/Tx buffer */
  58. #define RQ_VENDOR_SET_MCR 0x0A /* Set MCR register */
  59. #define RQ_VENDOR_SET_BREAK 0x0B /* Set Break signal */
  60. #define RQ_VENDOR_START_FW_DOWN 0x0C /* Start firmware download */
  61. #define RQ_VENDOR_STOP_FW_DOWN 0x0D /* Stop firmware download */
  62. #define RQ_VENDOR_QUERY_FW_READY 0x0E /* Query if new firmware ready */
  63. #define RQ_VENDOR_SET_FIFO_DISABLE 0x0F /* Set fifo disable */
  64. #define RQ_VENDOR_SET_INTERFACE 0x10 /* Set interface */
  65. #define RQ_VENDOR_SET_HIGH_PERFOR 0x11 /* Set hi-performance */
  66. #define RQ_VENDOR_ERASE_BLOCK 0x12 /* Erase flash block */
  67. #define RQ_VENDOR_WRITE_PAGE 0x13 /* Write flash page */
  68. #define RQ_VENDOR_PREPARE_WRITE 0x14 /* Prepare write flash */
  69. #define RQ_VENDOR_CONFIRM_WRITE 0x15 /* Confirm write flash */
  70. #define RQ_VENDOR_LOCATE 0x16 /* Locate the device */
  71. #define RQ_VENDOR_START_ROM_DOWN 0x17 /* Start firmware download */
  72. #define RQ_VENDOR_ROM_DATA 0x18 /* Rom file data */
  73. #define RQ_VENDOR_STOP_ROM_DOWN 0x19 /* Stop firmware download */
  74. #define RQ_VENDOR_FW_DATA 0x20 /* Firmware data */
  75. #define RQ_VENDOR_RESET_DEVICE 0x23 /* Try to reset the device */
  76. #define RQ_VENDOR_QUERY_FW_CONFIG 0x24
  77. #define RQ_VENDOR_GET_VERSION 0x81 /* Get firmware version */
  78. #define RQ_VENDOR_GET_PAGE 0x82 /* Read flash page */
  79. #define RQ_VENDOR_GET_ROM_PROC 0x83 /* Get ROM process state */
  80. #define RQ_VENDOR_GET_INQUEUE 0x84 /* Data in input buffer */
  81. #define RQ_VENDOR_GET_OUTQUEUE 0x85 /* Data in output buffer */
  82. #define RQ_VENDOR_GET_MSR 0x86 /* Get modem status register */
  83. /* Definitions for UPort event type */
  84. #define UPORT_EVENT_NONE 0 /* None */
  85. #define UPORT_EVENT_TXBUF_THRESHOLD 1 /* Tx buffer threshold */
  86. #define UPORT_EVENT_SEND_NEXT 2 /* Send next */
  87. #define UPORT_EVENT_MSR 3 /* Modem status */
  88. #define UPORT_EVENT_LSR 4 /* Line status */
  89. #define UPORT_EVENT_MCR 5 /* Modem control */
  90. /* Definitions for serial event type */
  91. #define SERIAL_EV_CTS 0x0008 /* CTS changed state */
  92. #define SERIAL_EV_DSR 0x0010 /* DSR changed state */
  93. #define SERIAL_EV_RLSD 0x0020 /* RLSD changed state */
  94. /* Definitions for modem control event type */
  95. #define SERIAL_EV_XOFF 0x40 /* XOFF received */
  96. /* Definitions for line control of communication */
  97. #define MX_WORDLENGTH_5 5
  98. #define MX_WORDLENGTH_6 6
  99. #define MX_WORDLENGTH_7 7
  100. #define MX_WORDLENGTH_8 8
  101. #define MX_PARITY_NONE 0
  102. #define MX_PARITY_ODD 1
  103. #define MX_PARITY_EVEN 2
  104. #define MX_PARITY_MARK 3
  105. #define MX_PARITY_SPACE 4
  106. #define MX_STOP_BITS_1 0
  107. #define MX_STOP_BITS_1_5 1
  108. #define MX_STOP_BITS_2 2
  109. #define MX_RTS_DISABLE 0x0
  110. #define MX_RTS_ENABLE 0x1
  111. #define MX_RTS_HW 0x2
  112. #define MX_RTS_NO_CHANGE 0x3 /* Flag, not valid register value*/
  113. #define MX_INT_RS232 0
  114. #define MX_INT_2W_RS485 1
  115. #define MX_INT_RS422 2
  116. #define MX_INT_4W_RS485 3
  117. /* Definitions for holding reason */
  118. #define MX_WAIT_FOR_CTS 0x0001
  119. #define MX_WAIT_FOR_DSR 0x0002
  120. #define MX_WAIT_FOR_DCD 0x0004
  121. #define MX_WAIT_FOR_XON 0x0008
  122. #define MX_WAIT_FOR_START_TX 0x0010
  123. #define MX_WAIT_FOR_UNTHROTTLE 0x0020
  124. #define MX_WAIT_FOR_LOW_WATER 0x0040
  125. #define MX_WAIT_FOR_SEND_NEXT 0x0080
  126. #define MX_UPORT_2_PORT BIT(0)
  127. #define MX_UPORT_4_PORT BIT(1)
  128. #define MX_UPORT_8_PORT BIT(2)
  129. #define MX_UPORT_16_PORT BIT(3)
  130. /* This structure holds all of the local port information */
  131. struct mxuport_port {
  132. u8 mcr_state; /* Last MCR state */
  133. u8 msr_state; /* Last MSR state */
  134. struct mutex mutex; /* Protects mcr_state */
  135. spinlock_t spinlock; /* Protects msr_state */
  136. };
  137. /* Table of devices that work with this driver */
  138. static const struct usb_device_id mxuport_idtable[] = {
  139. { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1250_PID),
  140. .driver_info = MX_UPORT_2_PORT },
  141. { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1251_PID),
  142. .driver_info = MX_UPORT_2_PORT },
  143. { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1410_PID),
  144. .driver_info = MX_UPORT_4_PORT },
  145. { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1450_PID),
  146. .driver_info = MX_UPORT_4_PORT },
  147. { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1451_PID),
  148. .driver_info = MX_UPORT_4_PORT },
  149. { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1618_PID),
  150. .driver_info = MX_UPORT_8_PORT },
  151. { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1658_PID),
  152. .driver_info = MX_UPORT_8_PORT },
  153. { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1613_PID),
  154. .driver_info = MX_UPORT_16_PORT },
  155. { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1653_PID),
  156. .driver_info = MX_UPORT_16_PORT },
  157. {} /* Terminating entry */
  158. };
  159. MODULE_DEVICE_TABLE(usb, mxuport_idtable);
  160. /*
  161. * Add a four byte header containing the port number and the number of
  162. * bytes of data in the message. Return the number of bytes in the
  163. * buffer.
  164. */
  165. static int mxuport_prepare_write_buffer(struct usb_serial_port *port,
  166. void *dest, size_t size)
  167. {
  168. u8 *buf = dest;
  169. int count;
  170. count = kfifo_out_locked(&port->write_fifo, buf + HEADER_SIZE,
  171. size - HEADER_SIZE,
  172. &port->lock);
  173. put_unaligned_be16(port->port_number, buf);
  174. put_unaligned_be16(count, buf + 2);
  175. dev_dbg(&port->dev, "%s - size %zd count %d\n", __func__,
  176. size, count);
  177. return count + HEADER_SIZE;
  178. }
  179. /* Read the given buffer in from the control pipe. */
  180. static int mxuport_recv_ctrl_urb(struct usb_serial *serial,
  181. u8 request, u16 value, u16 index,
  182. u8 *data, size_t size)
  183. {
  184. int status;
  185. status = usb_control_msg(serial->dev,
  186. usb_rcvctrlpipe(serial->dev, 0),
  187. request,
  188. (USB_DIR_IN | USB_TYPE_VENDOR |
  189. USB_RECIP_DEVICE), value, index,
  190. data, size,
  191. USB_CTRL_GET_TIMEOUT);
  192. if (status < 0) {
  193. dev_err(&serial->interface->dev,
  194. "%s - usb_control_msg failed (%d)\n",
  195. __func__, status);
  196. return status;
  197. }
  198. if (status != size) {
  199. dev_err(&serial->interface->dev,
  200. "%s - short read (%d / %zd)\n",
  201. __func__, status, size);
  202. return -EIO;
  203. }
  204. return status;
  205. }
  206. /* Write the given buffer out to the control pipe. */
  207. static int mxuport_send_ctrl_data_urb(struct usb_serial *serial,
  208. u8 request,
  209. u16 value, u16 index,
  210. u8 *data, size_t size)
  211. {
  212. int status;
  213. status = usb_control_msg(serial->dev,
  214. usb_sndctrlpipe(serial->dev, 0),
  215. request,
  216. (USB_DIR_OUT | USB_TYPE_VENDOR |
  217. USB_RECIP_DEVICE), value, index,
  218. data, size,
  219. USB_CTRL_SET_TIMEOUT);
  220. if (status < 0) {
  221. dev_err(&serial->interface->dev,
  222. "%s - usb_control_msg failed (%d)\n",
  223. __func__, status);
  224. return status;
  225. }
  226. return 0;
  227. }
  228. /* Send a vendor request without any data */
  229. static int mxuport_send_ctrl_urb(struct usb_serial *serial,
  230. u8 request, u16 value, u16 index)
  231. {
  232. return mxuport_send_ctrl_data_urb(serial, request, value, index,
  233. NULL, 0);
  234. }
  235. /*
  236. * mxuport_throttle - throttle function of driver
  237. *
  238. * This function is called by the tty driver when it wants to stop the
  239. * data being read from the port. Since all the data comes over one
  240. * bulk in endpoint, we cannot stop submitting urbs by setting
  241. * port->throttle. Instead tell the device to stop sending us data for
  242. * the port.
  243. */
  244. static void mxuport_throttle(struct tty_struct *tty)
  245. {
  246. struct usb_serial_port *port = tty->driver_data;
  247. struct usb_serial *serial = port->serial;
  248. dev_dbg(&port->dev, "%s\n", __func__);
  249. mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
  250. 0, port->port_number);
  251. }
  252. /*
  253. * mxuport_unthrottle - unthrottle function of driver
  254. *
  255. * This function is called by the tty driver when it wants to resume
  256. * the data being read from the port. Tell the device it can resume
  257. * sending us received data from the port.
  258. */
  259. static void mxuport_unthrottle(struct tty_struct *tty)
  260. {
  261. struct usb_serial_port *port = tty->driver_data;
  262. struct usb_serial *serial = port->serial;
  263. dev_dbg(&port->dev, "%s\n", __func__);
  264. mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
  265. 1, port->port_number);
  266. }
  267. /*
  268. * Processes one chunk of data received for a port. Mostly a copy of
  269. * usb_serial_generic_process_read_urb().
  270. */
  271. static void mxuport_process_read_urb_data(struct usb_serial_port *port,
  272. char *data, int size)
  273. {
  274. int i;
  275. if (port->sysrq) {
  276. for (i = 0; i < size; i++, data++) {
  277. if (!usb_serial_handle_sysrq_char(port, *data))
  278. tty_insert_flip_char(&port->port, *data,
  279. TTY_NORMAL);
  280. }
  281. } else {
  282. tty_insert_flip_string(&port->port, data, size);
  283. }
  284. tty_flip_buffer_push(&port->port);
  285. }
  286. static void mxuport_msr_event(struct usb_serial_port *port, u8 buf[4])
  287. {
  288. struct mxuport_port *mxport = usb_get_serial_port_data(port);
  289. u8 rcv_msr_hold = buf[2] & 0xF0;
  290. u16 rcv_msr_event = get_unaligned_be16(buf);
  291. unsigned long flags;
  292. if (rcv_msr_event == 0)
  293. return;
  294. /* Update MSR status */
  295. spin_lock_irqsave(&mxport->spinlock, flags);
  296. dev_dbg(&port->dev, "%s - current MSR status = 0x%x\n",
  297. __func__, mxport->msr_state);
  298. if (rcv_msr_hold & UART_MSR_CTS) {
  299. mxport->msr_state |= UART_MSR_CTS;
  300. dev_dbg(&port->dev, "%s - CTS high\n", __func__);
  301. } else {
  302. mxport->msr_state &= ~UART_MSR_CTS;
  303. dev_dbg(&port->dev, "%s - CTS low\n", __func__);
  304. }
  305. if (rcv_msr_hold & UART_MSR_DSR) {
  306. mxport->msr_state |= UART_MSR_DSR;
  307. dev_dbg(&port->dev, "%s - DSR high\n", __func__);
  308. } else {
  309. mxport->msr_state &= ~UART_MSR_DSR;
  310. dev_dbg(&port->dev, "%s - DSR low\n", __func__);
  311. }
  312. if (rcv_msr_hold & UART_MSR_DCD) {
  313. mxport->msr_state |= UART_MSR_DCD;
  314. dev_dbg(&port->dev, "%s - DCD high\n", __func__);
  315. } else {
  316. mxport->msr_state &= ~UART_MSR_DCD;
  317. dev_dbg(&port->dev, "%s - DCD low\n", __func__);
  318. }
  319. spin_unlock_irqrestore(&mxport->spinlock, flags);
  320. if (rcv_msr_event &
  321. (SERIAL_EV_CTS | SERIAL_EV_DSR | SERIAL_EV_RLSD)) {
  322. if (rcv_msr_event & SERIAL_EV_CTS) {
  323. port->icount.cts++;
  324. dev_dbg(&port->dev, "%s - CTS change\n", __func__);
  325. }
  326. if (rcv_msr_event & SERIAL_EV_DSR) {
  327. port->icount.dsr++;
  328. dev_dbg(&port->dev, "%s - DSR change\n", __func__);
  329. }
  330. if (rcv_msr_event & SERIAL_EV_RLSD) {
  331. port->icount.dcd++;
  332. dev_dbg(&port->dev, "%s - DCD change\n", __func__);
  333. }
  334. wake_up_interruptible(&port->port.delta_msr_wait);
  335. }
  336. }
  337. static void mxuport_lsr_event(struct usb_serial_port *port, u8 buf[4])
  338. {
  339. u8 lsr_event = buf[2];
  340. if (lsr_event & UART_LSR_BI) {
  341. port->icount.brk++;
  342. dev_dbg(&port->dev, "%s - break error\n", __func__);
  343. }
  344. if (lsr_event & UART_LSR_FE) {
  345. port->icount.frame++;
  346. dev_dbg(&port->dev, "%s - frame error\n", __func__);
  347. }
  348. if (lsr_event & UART_LSR_PE) {
  349. port->icount.parity++;
  350. dev_dbg(&port->dev, "%s - parity error\n", __func__);
  351. }
  352. if (lsr_event & UART_LSR_OE) {
  353. port->icount.overrun++;
  354. dev_dbg(&port->dev, "%s - overrun error\n", __func__);
  355. }
  356. }
  357. /*
  358. * When something interesting happens, modem control lines XON/XOFF
  359. * etc, the device sends an event. Process these events.
  360. */
  361. static void mxuport_process_read_urb_event(struct usb_serial_port *port,
  362. u8 buf[4], u32 event)
  363. {
  364. dev_dbg(&port->dev, "%s - receive event : %04x\n", __func__, event);
  365. switch (event) {
  366. case UPORT_EVENT_SEND_NEXT:
  367. /*
  368. * Sent as part of the flow control on device buffers.
  369. * Not currently used.
  370. */
  371. break;
  372. case UPORT_EVENT_MSR:
  373. mxuport_msr_event(port, buf);
  374. break;
  375. case UPORT_EVENT_LSR:
  376. mxuport_lsr_event(port, buf);
  377. break;
  378. case UPORT_EVENT_MCR:
  379. /*
  380. * Event to indicate a change in XON/XOFF from the
  381. * peer. Currently not used. We just continue
  382. * sending the device data and it will buffer it if
  383. * needed. This event could be used for flow control
  384. * between the host and the device.
  385. */
  386. break;
  387. default:
  388. dev_dbg(&port->dev, "Unexpected event\n");
  389. break;
  390. }
  391. }
  392. /*
  393. * One URB can contain data for multiple ports. Demultiplex the data,
  394. * checking the port exists, is opened and the message is valid.
  395. */
  396. static void mxuport_process_read_urb_demux_data(struct urb *urb)
  397. {
  398. struct usb_serial_port *port = urb->context;
  399. struct usb_serial *serial = port->serial;
  400. u8 *data = urb->transfer_buffer;
  401. u8 *end = data + urb->actual_length;
  402. struct usb_serial_port *demux_port;
  403. u8 *ch;
  404. u16 rcv_port;
  405. u16 rcv_len;
  406. while (data < end) {
  407. if (data + HEADER_SIZE > end) {
  408. dev_warn(&port->dev, "%s - message with short header\n",
  409. __func__);
  410. return;
  411. }
  412. rcv_port = get_unaligned_be16(data);
  413. if (rcv_port >= serial->num_ports) {
  414. dev_warn(&port->dev, "%s - message for invalid port\n",
  415. __func__);
  416. return;
  417. }
  418. demux_port = serial->port[rcv_port];
  419. rcv_len = get_unaligned_be16(data + 2);
  420. if (!rcv_len || data + HEADER_SIZE + rcv_len > end) {
  421. dev_warn(&port->dev, "%s - short data\n", __func__);
  422. return;
  423. }
  424. if (tty_port_initialized(&demux_port->port)) {
  425. ch = data + HEADER_SIZE;
  426. mxuport_process_read_urb_data(demux_port, ch, rcv_len);
  427. } else {
  428. dev_dbg(&demux_port->dev, "%s - data for closed port\n",
  429. __func__);
  430. }
  431. data += HEADER_SIZE + rcv_len;
  432. }
  433. }
  434. /*
  435. * One URB can contain events for multiple ports. Demultiplex the event,
  436. * checking the port exists, and is opened.
  437. */
  438. static void mxuport_process_read_urb_demux_event(struct urb *urb)
  439. {
  440. struct usb_serial_port *port = urb->context;
  441. struct usb_serial *serial = port->serial;
  442. u8 *data = urb->transfer_buffer;
  443. u8 *end = data + urb->actual_length;
  444. struct usb_serial_port *demux_port;
  445. u8 *ch;
  446. u16 rcv_port;
  447. u16 rcv_event;
  448. while (data < end) {
  449. if (data + EVENT_LENGTH > end) {
  450. dev_warn(&port->dev, "%s - message with short event\n",
  451. __func__);
  452. return;
  453. }
  454. rcv_port = get_unaligned_be16(data);
  455. if (rcv_port >= serial->num_ports) {
  456. dev_warn(&port->dev, "%s - message for invalid port\n",
  457. __func__);
  458. return;
  459. }
  460. demux_port = serial->port[rcv_port];
  461. if (tty_port_initialized(&demux_port->port)) {
  462. ch = data + HEADER_SIZE;
  463. rcv_event = get_unaligned_be16(data + 2);
  464. mxuport_process_read_urb_event(demux_port, ch,
  465. rcv_event);
  466. } else {
  467. dev_dbg(&demux_port->dev,
  468. "%s - event for closed port\n", __func__);
  469. }
  470. data += EVENT_LENGTH;
  471. }
  472. }
  473. /*
  474. * This is called when we have received data on the bulk in
  475. * endpoint. Depending on which port it was received on, it can
  476. * contain serial data or events.
  477. */
  478. static void mxuport_process_read_urb(struct urb *urb)
  479. {
  480. struct usb_serial_port *port = urb->context;
  481. struct usb_serial *serial = port->serial;
  482. if (port == serial->port[0])
  483. mxuport_process_read_urb_demux_data(urb);
  484. if (port == serial->port[1])
  485. mxuport_process_read_urb_demux_event(urb);
  486. }
  487. /*
  488. * Ask the device how many bytes it has queued to be sent out. If
  489. * there are none, return true.
  490. */
  491. static bool mxuport_tx_empty(struct usb_serial_port *port)
  492. {
  493. struct usb_serial *serial = port->serial;
  494. bool is_empty = true;
  495. u32 txlen;
  496. u8 *len_buf;
  497. int err;
  498. len_buf = kzalloc(4, GFP_KERNEL);
  499. if (!len_buf)
  500. goto out;
  501. err = mxuport_recv_ctrl_urb(serial, RQ_VENDOR_GET_OUTQUEUE, 0,
  502. port->port_number, len_buf, 4);
  503. if (err < 0)
  504. goto out;
  505. txlen = get_unaligned_be32(len_buf);
  506. dev_dbg(&port->dev, "%s - tx len = %u\n", __func__, txlen);
  507. if (txlen != 0)
  508. is_empty = false;
  509. out:
  510. kfree(len_buf);
  511. return is_empty;
  512. }
  513. static int mxuport_set_mcr(struct usb_serial_port *port, u8 mcr_state)
  514. {
  515. struct usb_serial *serial = port->serial;
  516. int err;
  517. dev_dbg(&port->dev, "%s - %02x\n", __func__, mcr_state);
  518. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_MCR,
  519. mcr_state, port->port_number);
  520. if (err)
  521. dev_err(&port->dev, "%s - failed to change MCR\n", __func__);
  522. return err;
  523. }
  524. static int mxuport_set_dtr(struct usb_serial_port *port, int on)
  525. {
  526. struct mxuport_port *mxport = usb_get_serial_port_data(port);
  527. struct usb_serial *serial = port->serial;
  528. int err;
  529. mutex_lock(&mxport->mutex);
  530. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_DTR,
  531. !!on, port->port_number);
  532. if (!err) {
  533. if (on)
  534. mxport->mcr_state |= UART_MCR_DTR;
  535. else
  536. mxport->mcr_state &= ~UART_MCR_DTR;
  537. }
  538. mutex_unlock(&mxport->mutex);
  539. return err;
  540. }
  541. static int mxuport_set_rts(struct usb_serial_port *port, u8 state)
  542. {
  543. struct mxuport_port *mxport = usb_get_serial_port_data(port);
  544. struct usb_serial *serial = port->serial;
  545. int err;
  546. u8 mcr_state;
  547. mutex_lock(&mxport->mutex);
  548. mcr_state = mxport->mcr_state;
  549. switch (state) {
  550. case MX_RTS_DISABLE:
  551. mcr_state &= ~UART_MCR_RTS;
  552. break;
  553. case MX_RTS_ENABLE:
  554. mcr_state |= UART_MCR_RTS;
  555. break;
  556. case MX_RTS_HW:
  557. /*
  558. * Do not update mxport->mcr_state when doing hardware
  559. * flow control.
  560. */
  561. break;
  562. default:
  563. /*
  564. * Should not happen, but somebody might try passing
  565. * MX_RTS_NO_CHANGE, which is not valid.
  566. */
  567. err = -EINVAL;
  568. goto out;
  569. }
  570. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RTS,
  571. state, port->port_number);
  572. if (!err)
  573. mxport->mcr_state = mcr_state;
  574. out:
  575. mutex_unlock(&mxport->mutex);
  576. return err;
  577. }
  578. static void mxuport_dtr_rts(struct usb_serial_port *port, int on)
  579. {
  580. struct mxuport_port *mxport = usb_get_serial_port_data(port);
  581. u8 mcr_state;
  582. int err;
  583. mutex_lock(&mxport->mutex);
  584. mcr_state = mxport->mcr_state;
  585. if (on)
  586. mcr_state |= (UART_MCR_RTS | UART_MCR_DTR);
  587. else
  588. mcr_state &= ~(UART_MCR_RTS | UART_MCR_DTR);
  589. err = mxuport_set_mcr(port, mcr_state);
  590. if (!err)
  591. mxport->mcr_state = mcr_state;
  592. mutex_unlock(&mxport->mutex);
  593. }
  594. static int mxuport_tiocmset(struct tty_struct *tty, unsigned int set,
  595. unsigned int clear)
  596. {
  597. struct usb_serial_port *port = tty->driver_data;
  598. struct mxuport_port *mxport = usb_get_serial_port_data(port);
  599. int err;
  600. u8 mcr_state;
  601. mutex_lock(&mxport->mutex);
  602. mcr_state = mxport->mcr_state;
  603. if (set & TIOCM_RTS)
  604. mcr_state |= UART_MCR_RTS;
  605. if (set & TIOCM_DTR)
  606. mcr_state |= UART_MCR_DTR;
  607. if (clear & TIOCM_RTS)
  608. mcr_state &= ~UART_MCR_RTS;
  609. if (clear & TIOCM_DTR)
  610. mcr_state &= ~UART_MCR_DTR;
  611. err = mxuport_set_mcr(port, mcr_state);
  612. if (!err)
  613. mxport->mcr_state = mcr_state;
  614. mutex_unlock(&mxport->mutex);
  615. return err;
  616. }
  617. static int mxuport_tiocmget(struct tty_struct *tty)
  618. {
  619. struct mxuport_port *mxport;
  620. struct usb_serial_port *port = tty->driver_data;
  621. unsigned int result;
  622. unsigned long flags;
  623. unsigned int msr;
  624. unsigned int mcr;
  625. mxport = usb_get_serial_port_data(port);
  626. mutex_lock(&mxport->mutex);
  627. spin_lock_irqsave(&mxport->spinlock, flags);
  628. msr = mxport->msr_state;
  629. mcr = mxport->mcr_state;
  630. spin_unlock_irqrestore(&mxport->spinlock, flags);
  631. mutex_unlock(&mxport->mutex);
  632. result = (((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) | /* 0x002 */
  633. ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) | /* 0x004 */
  634. ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) | /* 0x020 */
  635. ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) | /* 0x040 */
  636. ((msr & UART_MSR_RI) ? TIOCM_RI : 0) | /* 0x080 */
  637. ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0)); /* 0x100 */
  638. dev_dbg(&port->dev, "%s - 0x%04x\n", __func__, result);
  639. return result;
  640. }
  641. static int mxuport_set_termios_flow(struct tty_struct *tty,
  642. const struct ktermios *old_termios,
  643. struct usb_serial_port *port,
  644. struct usb_serial *serial)
  645. {
  646. u8 xon = START_CHAR(tty);
  647. u8 xoff = STOP_CHAR(tty);
  648. int enable;
  649. int err;
  650. u8 *buf;
  651. u8 rts;
  652. buf = kmalloc(2, GFP_KERNEL);
  653. if (!buf)
  654. return -ENOMEM;
  655. /* S/W flow control settings */
  656. if (I_IXOFF(tty) || I_IXON(tty)) {
  657. enable = 1;
  658. buf[0] = xon;
  659. buf[1] = xoff;
  660. err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_CHARS,
  661. 0, port->port_number,
  662. buf, 2);
  663. if (err)
  664. goto out;
  665. dev_dbg(&port->dev, "%s - XON = 0x%02x, XOFF = 0x%02x\n",
  666. __func__, xon, xoff);
  667. } else {
  668. enable = 0;
  669. }
  670. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_XONXOFF,
  671. enable, port->port_number);
  672. if (err)
  673. goto out;
  674. rts = MX_RTS_NO_CHANGE;
  675. /* H/W flow control settings */
  676. if (!old_termios ||
  677. C_CRTSCTS(tty) != (old_termios->c_cflag & CRTSCTS)) {
  678. if (C_CRTSCTS(tty))
  679. rts = MX_RTS_HW;
  680. else
  681. rts = MX_RTS_ENABLE;
  682. }
  683. if (C_BAUD(tty)) {
  684. if (old_termios && (old_termios->c_cflag & CBAUD) == B0) {
  685. /* Raise DTR and RTS */
  686. if (C_CRTSCTS(tty))
  687. rts = MX_RTS_HW;
  688. else
  689. rts = MX_RTS_ENABLE;
  690. mxuport_set_dtr(port, 1);
  691. }
  692. } else {
  693. /* Drop DTR and RTS */
  694. rts = MX_RTS_DISABLE;
  695. mxuport_set_dtr(port, 0);
  696. }
  697. if (rts != MX_RTS_NO_CHANGE)
  698. err = mxuport_set_rts(port, rts);
  699. out:
  700. kfree(buf);
  701. return err;
  702. }
  703. static void mxuport_set_termios(struct tty_struct *tty,
  704. struct usb_serial_port *port,
  705. const struct ktermios *old_termios)
  706. {
  707. struct usb_serial *serial = port->serial;
  708. u8 *buf;
  709. u8 data_bits;
  710. u8 stop_bits;
  711. u8 parity;
  712. int baud;
  713. int err;
  714. if (old_termios &&
  715. !tty_termios_hw_change(&tty->termios, old_termios) &&
  716. tty->termios.c_iflag == old_termios->c_iflag) {
  717. dev_dbg(&port->dev, "%s - nothing to change\n", __func__);
  718. return;
  719. }
  720. buf = kmalloc(4, GFP_KERNEL);
  721. if (!buf)
  722. return;
  723. /* Set data bit of termios */
  724. switch (C_CSIZE(tty)) {
  725. case CS5:
  726. data_bits = MX_WORDLENGTH_5;
  727. break;
  728. case CS6:
  729. data_bits = MX_WORDLENGTH_6;
  730. break;
  731. case CS7:
  732. data_bits = MX_WORDLENGTH_7;
  733. break;
  734. case CS8:
  735. default:
  736. data_bits = MX_WORDLENGTH_8;
  737. break;
  738. }
  739. /* Set parity of termios */
  740. if (C_PARENB(tty)) {
  741. if (C_CMSPAR(tty)) {
  742. if (C_PARODD(tty))
  743. parity = MX_PARITY_MARK;
  744. else
  745. parity = MX_PARITY_SPACE;
  746. } else {
  747. if (C_PARODD(tty))
  748. parity = MX_PARITY_ODD;
  749. else
  750. parity = MX_PARITY_EVEN;
  751. }
  752. } else {
  753. parity = MX_PARITY_NONE;
  754. }
  755. /* Set stop bit of termios */
  756. if (C_CSTOPB(tty))
  757. stop_bits = MX_STOP_BITS_2;
  758. else
  759. stop_bits = MX_STOP_BITS_1;
  760. buf[0] = data_bits;
  761. buf[1] = parity;
  762. buf[2] = stop_bits;
  763. buf[3] = 0;
  764. err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_LINE,
  765. 0, port->port_number, buf, 4);
  766. if (err)
  767. goto out;
  768. err = mxuport_set_termios_flow(tty, old_termios, port, serial);
  769. if (err)
  770. goto out;
  771. baud = tty_get_baud_rate(tty);
  772. if (!baud)
  773. baud = 9600;
  774. /* Note: Little Endian */
  775. put_unaligned_le32(baud, buf);
  776. err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_BAUD,
  777. 0, port->port_number,
  778. buf, 4);
  779. if (err)
  780. goto out;
  781. dev_dbg(&port->dev, "baud_rate : %d\n", baud);
  782. dev_dbg(&port->dev, "data_bits : %d\n", data_bits);
  783. dev_dbg(&port->dev, "parity : %d\n", parity);
  784. dev_dbg(&port->dev, "stop_bits : %d\n", stop_bits);
  785. out:
  786. kfree(buf);
  787. }
  788. /*
  789. * Determine how many ports this device has dynamically. It will be
  790. * called after the probe() callback is called, but before attach().
  791. */
  792. static int mxuport_calc_num_ports(struct usb_serial *serial,
  793. struct usb_serial_endpoints *epds)
  794. {
  795. unsigned long features = (unsigned long)usb_get_serial_data(serial);
  796. int num_ports;
  797. int i;
  798. if (features & MX_UPORT_2_PORT) {
  799. num_ports = 2;
  800. } else if (features & MX_UPORT_4_PORT) {
  801. num_ports = 4;
  802. } else if (features & MX_UPORT_8_PORT) {
  803. num_ports = 8;
  804. } else if (features & MX_UPORT_16_PORT) {
  805. num_ports = 16;
  806. } else {
  807. dev_warn(&serial->interface->dev,
  808. "unknown device, assuming two ports\n");
  809. num_ports = 2;
  810. }
  811. /*
  812. * Setup bulk-out endpoint multiplexing. All ports share the same
  813. * bulk-out endpoint.
  814. */
  815. BUILD_BUG_ON(ARRAY_SIZE(epds->bulk_out) < 16);
  816. for (i = 1; i < num_ports; ++i)
  817. epds->bulk_out[i] = epds->bulk_out[0];
  818. epds->num_bulk_out = num_ports;
  819. return num_ports;
  820. }
  821. /* Get the version of the firmware currently running. */
  822. static int mxuport_get_fw_version(struct usb_serial *serial, u32 *version)
  823. {
  824. u8 *ver_buf;
  825. int err;
  826. ver_buf = kzalloc(4, GFP_KERNEL);
  827. if (!ver_buf)
  828. return -ENOMEM;
  829. /* Get firmware version from SDRAM */
  830. err = mxuport_recv_ctrl_urb(serial, RQ_VENDOR_GET_VERSION, 0, 0,
  831. ver_buf, 4);
  832. if (err != 4) {
  833. err = -EIO;
  834. goto out;
  835. }
  836. *version = (ver_buf[0] << 16) | (ver_buf[1] << 8) | ver_buf[2];
  837. err = 0;
  838. out:
  839. kfree(ver_buf);
  840. return err;
  841. }
  842. /* Given a firmware blob, download it to the device. */
  843. static int mxuport_download_fw(struct usb_serial *serial,
  844. const struct firmware *fw_p)
  845. {
  846. u8 *fw_buf;
  847. size_t txlen;
  848. size_t fwidx;
  849. int err;
  850. fw_buf = kmalloc(DOWN_BLOCK_SIZE, GFP_KERNEL);
  851. if (!fw_buf)
  852. return -ENOMEM;
  853. dev_dbg(&serial->interface->dev, "Starting firmware download...\n");
  854. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_START_FW_DOWN, 0, 0);
  855. if (err)
  856. goto out;
  857. fwidx = 0;
  858. do {
  859. txlen = min_t(size_t, (fw_p->size - fwidx), DOWN_BLOCK_SIZE);
  860. memcpy(fw_buf, &fw_p->data[fwidx], txlen);
  861. err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_FW_DATA,
  862. 0, 0, fw_buf, txlen);
  863. if (err) {
  864. mxuport_send_ctrl_urb(serial, RQ_VENDOR_STOP_FW_DOWN,
  865. 0, 0);
  866. goto out;
  867. }
  868. fwidx += txlen;
  869. usleep_range(1000, 2000);
  870. } while (fwidx < fw_p->size);
  871. msleep(1000);
  872. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_STOP_FW_DOWN, 0, 0);
  873. if (err)
  874. goto out;
  875. msleep(1000);
  876. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_QUERY_FW_READY, 0, 0);
  877. out:
  878. kfree(fw_buf);
  879. return err;
  880. }
  881. static int mxuport_probe(struct usb_serial *serial,
  882. const struct usb_device_id *id)
  883. {
  884. u16 productid = le16_to_cpu(serial->dev->descriptor.idProduct);
  885. const struct firmware *fw_p = NULL;
  886. u32 version;
  887. int local_ver;
  888. char buf[32];
  889. int err;
  890. /* Load our firmware */
  891. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_QUERY_FW_CONFIG, 0, 0);
  892. if (err) {
  893. mxuport_send_ctrl_urb(serial, RQ_VENDOR_RESET_DEVICE, 0, 0);
  894. return err;
  895. }
  896. err = mxuport_get_fw_version(serial, &version);
  897. if (err < 0)
  898. return err;
  899. dev_dbg(&serial->interface->dev, "Device firmware version v%x.%x.%x\n",
  900. (version & 0xff0000) >> 16,
  901. (version & 0xff00) >> 8,
  902. (version & 0xff));
  903. snprintf(buf, sizeof(buf) - 1, "moxa/moxa-%04x.fw", productid);
  904. err = request_firmware(&fw_p, buf, &serial->interface->dev);
  905. if (err) {
  906. dev_warn(&serial->interface->dev, "Firmware %s not found\n",
  907. buf);
  908. /* Use the firmware already in the device */
  909. err = 0;
  910. } else {
  911. local_ver = ((fw_p->data[VER_ADDR_1] << 16) |
  912. (fw_p->data[VER_ADDR_2] << 8) |
  913. fw_p->data[VER_ADDR_3]);
  914. dev_dbg(&serial->interface->dev,
  915. "Available firmware version v%x.%x.%x\n",
  916. fw_p->data[VER_ADDR_1], fw_p->data[VER_ADDR_2],
  917. fw_p->data[VER_ADDR_3]);
  918. if (local_ver > version) {
  919. err = mxuport_download_fw(serial, fw_p);
  920. if (err)
  921. goto out;
  922. err = mxuport_get_fw_version(serial, &version);
  923. if (err < 0)
  924. goto out;
  925. }
  926. }
  927. dev_info(&serial->interface->dev,
  928. "Using device firmware version v%x.%x.%x\n",
  929. (version & 0xff0000) >> 16,
  930. (version & 0xff00) >> 8,
  931. (version & 0xff));
  932. /*
  933. * Contains the features of this hardware. Store away for
  934. * later use, eg, number of ports.
  935. */
  936. usb_set_serial_data(serial, (void *)id->driver_info);
  937. out:
  938. if (fw_p)
  939. release_firmware(fw_p);
  940. return err;
  941. }
  942. static int mxuport_port_probe(struct usb_serial_port *port)
  943. {
  944. struct usb_serial *serial = port->serial;
  945. struct mxuport_port *mxport;
  946. int err;
  947. mxport = devm_kzalloc(&port->dev, sizeof(struct mxuport_port),
  948. GFP_KERNEL);
  949. if (!mxport)
  950. return -ENOMEM;
  951. mutex_init(&mxport->mutex);
  952. spin_lock_init(&mxport->spinlock);
  953. /* Set the port private data */
  954. usb_set_serial_port_data(port, mxport);
  955. /* Set FIFO (Enable) */
  956. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_FIFO_DISABLE,
  957. 0, port->port_number);
  958. if (err)
  959. return err;
  960. /* Set transmission mode (Hi-Performance) */
  961. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_HIGH_PERFOR,
  962. 0, port->port_number);
  963. if (err)
  964. return err;
  965. /* Set interface (RS-232) */
  966. return mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_INTERFACE,
  967. MX_INT_RS232,
  968. port->port_number);
  969. }
  970. static int mxuport_attach(struct usb_serial *serial)
  971. {
  972. struct usb_serial_port *port0 = serial->port[0];
  973. struct usb_serial_port *port1 = serial->port[1];
  974. int err;
  975. /*
  976. * All data from the ports is received on the first bulk in
  977. * endpoint, with a multiplex header. The second bulk in is
  978. * used for events.
  979. *
  980. * Start to read from the device.
  981. */
  982. err = usb_serial_generic_submit_read_urbs(port0, GFP_KERNEL);
  983. if (err)
  984. return err;
  985. err = usb_serial_generic_submit_read_urbs(port1, GFP_KERNEL);
  986. if (err) {
  987. usb_serial_generic_close(port0);
  988. return err;
  989. }
  990. return 0;
  991. }
  992. static void mxuport_release(struct usb_serial *serial)
  993. {
  994. struct usb_serial_port *port0 = serial->port[0];
  995. struct usb_serial_port *port1 = serial->port[1];
  996. usb_serial_generic_close(port1);
  997. usb_serial_generic_close(port0);
  998. }
  999. static int mxuport_open(struct tty_struct *tty, struct usb_serial_port *port)
  1000. {
  1001. struct mxuport_port *mxport = usb_get_serial_port_data(port);
  1002. struct usb_serial *serial = port->serial;
  1003. int err;
  1004. /* Set receive host (enable) */
  1005. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
  1006. 1, port->port_number);
  1007. if (err)
  1008. return err;
  1009. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_OPEN,
  1010. 1, port->port_number);
  1011. if (err) {
  1012. mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
  1013. 0, port->port_number);
  1014. return err;
  1015. }
  1016. /* Initial port termios */
  1017. if (tty)
  1018. mxuport_set_termios(tty, port, NULL);
  1019. /*
  1020. * TODO: use RQ_VENDOR_GET_MSR, once we know what it
  1021. * returns.
  1022. */
  1023. mxport->msr_state = 0;
  1024. return err;
  1025. }
  1026. static void mxuport_close(struct usb_serial_port *port)
  1027. {
  1028. struct usb_serial *serial = port->serial;
  1029. mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_OPEN, 0,
  1030. port->port_number);
  1031. mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN, 0,
  1032. port->port_number);
  1033. }
  1034. /* Send a break to the port. */
  1035. static void mxuport_break_ctl(struct tty_struct *tty, int break_state)
  1036. {
  1037. struct usb_serial_port *port = tty->driver_data;
  1038. struct usb_serial *serial = port->serial;
  1039. int enable;
  1040. if (break_state == -1) {
  1041. enable = 1;
  1042. dev_dbg(&port->dev, "%s - sending break\n", __func__);
  1043. } else {
  1044. enable = 0;
  1045. dev_dbg(&port->dev, "%s - clearing break\n", __func__);
  1046. }
  1047. mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_BREAK,
  1048. enable, port->port_number);
  1049. }
  1050. static int mxuport_resume(struct usb_serial *serial)
  1051. {
  1052. struct usb_serial_port *port;
  1053. int c = 0;
  1054. int i;
  1055. int r;
  1056. for (i = 0; i < 2; i++) {
  1057. port = serial->port[i];
  1058. r = usb_serial_generic_submit_read_urbs(port, GFP_NOIO);
  1059. if (r < 0)
  1060. c++;
  1061. }
  1062. for (i = 0; i < serial->num_ports; i++) {
  1063. port = serial->port[i];
  1064. if (!tty_port_initialized(&port->port))
  1065. continue;
  1066. r = usb_serial_generic_write_start(port, GFP_NOIO);
  1067. if (r < 0)
  1068. c++;
  1069. }
  1070. return c ? -EIO : 0;
  1071. }
  1072. static struct usb_serial_driver mxuport_device = {
  1073. .driver = {
  1074. .owner = THIS_MODULE,
  1075. .name = "mxuport",
  1076. },
  1077. .description = "MOXA UPort",
  1078. .id_table = mxuport_idtable,
  1079. .num_bulk_in = 2,
  1080. .num_bulk_out = 1,
  1081. .probe = mxuport_probe,
  1082. .port_probe = mxuport_port_probe,
  1083. .attach = mxuport_attach,
  1084. .release = mxuport_release,
  1085. .calc_num_ports = mxuport_calc_num_ports,
  1086. .open = mxuport_open,
  1087. .close = mxuport_close,
  1088. .set_termios = mxuport_set_termios,
  1089. .break_ctl = mxuport_break_ctl,
  1090. .tx_empty = mxuport_tx_empty,
  1091. .tiocmiwait = usb_serial_generic_tiocmiwait,
  1092. .get_icount = usb_serial_generic_get_icount,
  1093. .throttle = mxuport_throttle,
  1094. .unthrottle = mxuport_unthrottle,
  1095. .tiocmget = mxuport_tiocmget,
  1096. .tiocmset = mxuport_tiocmset,
  1097. .dtr_rts = mxuport_dtr_rts,
  1098. .process_read_urb = mxuport_process_read_urb,
  1099. .prepare_write_buffer = mxuport_prepare_write_buffer,
  1100. .resume = mxuport_resume,
  1101. };
  1102. static struct usb_serial_driver *const serial_drivers[] = {
  1103. &mxuport_device, NULL
  1104. };
  1105. module_usb_serial_driver(serial_drivers, mxuport_idtable);
  1106. MODULE_AUTHOR("Andrew Lunn <[email protected]>");
  1107. MODULE_AUTHOR("<[email protected]>");
  1108. MODULE_LICENSE("GPL");