mxuport.c 34 KB

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