mxuport.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  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. return mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_INTERFACE,
  961. MX_INT_RS232,
  962. port->port_number);
  963. }
  964. static int mxuport_alloc_write_urb(struct usb_serial *serial,
  965. struct usb_serial_port *port,
  966. struct usb_serial_port *port0,
  967. int j)
  968. {
  969. struct usb_device *dev = interface_to_usbdev(serial->interface);
  970. set_bit(j, &port->write_urbs_free);
  971. port->write_urbs[j] = usb_alloc_urb(0, GFP_KERNEL);
  972. if (!port->write_urbs[j])
  973. return -ENOMEM;
  974. port->bulk_out_buffers[j] = kmalloc(port0->bulk_out_size, GFP_KERNEL);
  975. if (!port->bulk_out_buffers[j])
  976. return -ENOMEM;
  977. usb_fill_bulk_urb(port->write_urbs[j], dev,
  978. usb_sndbulkpipe(dev, port->bulk_out_endpointAddress),
  979. port->bulk_out_buffers[j],
  980. port->bulk_out_size,
  981. serial->type->write_bulk_callback,
  982. port);
  983. return 0;
  984. }
  985. static int mxuport_alloc_write_urbs(struct usb_serial *serial,
  986. struct usb_serial_port *port,
  987. struct usb_serial_port *port0)
  988. {
  989. int j;
  990. int ret;
  991. for (j = 0; j < ARRAY_SIZE(port->write_urbs); ++j) {
  992. ret = mxuport_alloc_write_urb(serial, port, port0, j);
  993. if (ret)
  994. return ret;
  995. }
  996. return 0;
  997. }
  998. static int mxuport_attach(struct usb_serial *serial)
  999. {
  1000. struct usb_serial_port *port0 = serial->port[0];
  1001. struct usb_serial_port *port1 = serial->port[1];
  1002. struct usb_serial_port *port;
  1003. int err;
  1004. int i;
  1005. int j;
  1006. /*
  1007. * Throw away all but the first allocated write URBs so we can
  1008. * set them up again to fit the multiplexing scheme.
  1009. */
  1010. for (i = 1; i < serial->num_bulk_out; ++i) {
  1011. port = serial->port[i];
  1012. for (j = 0; j < ARRAY_SIZE(port->write_urbs); ++j) {
  1013. usb_free_urb(port->write_urbs[j]);
  1014. kfree(port->bulk_out_buffers[j]);
  1015. port->write_urbs[j] = NULL;
  1016. port->bulk_out_buffers[j] = NULL;
  1017. }
  1018. port->write_urbs_free = 0;
  1019. }
  1020. /*
  1021. * All write data is sent over the first bulk out endpoint,
  1022. * with an added header to indicate the port. Allocate URBs
  1023. * for each port to the first bulk out endpoint.
  1024. */
  1025. for (i = 1; i < serial->num_ports; ++i) {
  1026. port = serial->port[i];
  1027. port->bulk_out_size = port0->bulk_out_size;
  1028. port->bulk_out_endpointAddress =
  1029. port0->bulk_out_endpointAddress;
  1030. err = mxuport_alloc_write_urbs(serial, port, port0);
  1031. if (err)
  1032. return err;
  1033. port->write_urb = port->write_urbs[0];
  1034. port->bulk_out_buffer = port->bulk_out_buffers[0];
  1035. /*
  1036. * Ensure each port has a fifo. The framework only
  1037. * allocates a fifo to ports with a bulk out endpoint,
  1038. * where as we need one for every port.
  1039. */
  1040. if (!kfifo_initialized(&port->write_fifo)) {
  1041. err = kfifo_alloc(&port->write_fifo, PAGE_SIZE,
  1042. GFP_KERNEL);
  1043. if (err)
  1044. return err;
  1045. }
  1046. }
  1047. /*
  1048. * All data from the ports is received on the first bulk in
  1049. * endpoint, with a multiplex header. The second bulk in is
  1050. * used for events.
  1051. *
  1052. * Start to read from the device.
  1053. */
  1054. err = usb_serial_generic_submit_read_urbs(port0, GFP_KERNEL);
  1055. if (err)
  1056. return err;
  1057. err = usb_serial_generic_submit_read_urbs(port1, GFP_KERNEL);
  1058. if (err) {
  1059. usb_serial_generic_close(port0);
  1060. return err;
  1061. }
  1062. return 0;
  1063. }
  1064. static void mxuport_release(struct usb_serial *serial)
  1065. {
  1066. struct usb_serial_port *port0 = serial->port[0];
  1067. struct usb_serial_port *port1 = serial->port[1];
  1068. usb_serial_generic_close(port1);
  1069. usb_serial_generic_close(port0);
  1070. }
  1071. static int mxuport_open(struct tty_struct *tty, struct usb_serial_port *port)
  1072. {
  1073. struct mxuport_port *mxport = usb_get_serial_port_data(port);
  1074. struct usb_serial *serial = port->serial;
  1075. int err;
  1076. /* Set receive host (enable) */
  1077. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
  1078. 1, port->port_number);
  1079. if (err)
  1080. return err;
  1081. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_OPEN,
  1082. 1, port->port_number);
  1083. if (err) {
  1084. mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
  1085. 0, port->port_number);
  1086. return err;
  1087. }
  1088. /* Initial port termios */
  1089. if (tty)
  1090. mxuport_set_termios(tty, port, NULL);
  1091. /*
  1092. * TODO: use RQ_VENDOR_GET_MSR, once we know what it
  1093. * returns.
  1094. */
  1095. mxport->msr_state = 0;
  1096. return err;
  1097. }
  1098. static void mxuport_close(struct usb_serial_port *port)
  1099. {
  1100. struct usb_serial *serial = port->serial;
  1101. mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_OPEN, 0,
  1102. port->port_number);
  1103. mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN, 0,
  1104. port->port_number);
  1105. }
  1106. /* Send a break to the port. */
  1107. static void mxuport_break_ctl(struct tty_struct *tty, int break_state)
  1108. {
  1109. struct usb_serial_port *port = tty->driver_data;
  1110. struct usb_serial *serial = port->serial;
  1111. int enable;
  1112. if (break_state == -1) {
  1113. enable = 1;
  1114. dev_dbg(&port->dev, "%s - sending break\n", __func__);
  1115. } else {
  1116. enable = 0;
  1117. dev_dbg(&port->dev, "%s - clearing break\n", __func__);
  1118. }
  1119. mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_BREAK,
  1120. enable, port->port_number);
  1121. }
  1122. static int mxuport_resume(struct usb_serial *serial)
  1123. {
  1124. struct usb_serial_port *port;
  1125. int c = 0;
  1126. int i;
  1127. int r;
  1128. for (i = 0; i < 2; i++) {
  1129. port = serial->port[i];
  1130. r = usb_serial_generic_submit_read_urbs(port, GFP_NOIO);
  1131. if (r < 0)
  1132. c++;
  1133. }
  1134. for (i = 0; i < serial->num_ports; i++) {
  1135. port = serial->port[i];
  1136. if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
  1137. continue;
  1138. r = usb_serial_generic_write_start(port, GFP_NOIO);
  1139. if (r < 0)
  1140. c++;
  1141. }
  1142. return c ? -EIO : 0;
  1143. }
  1144. static struct usb_serial_driver mxuport_device = {
  1145. .driver = {
  1146. .owner = THIS_MODULE,
  1147. .name = "mxuport",
  1148. },
  1149. .description = "MOXA UPort",
  1150. .id_table = mxuport_idtable,
  1151. .num_ports = 0,
  1152. .probe = mxuport_probe,
  1153. .port_probe = mxuport_port_probe,
  1154. .attach = mxuport_attach,
  1155. .release = mxuport_release,
  1156. .calc_num_ports = mxuport_calc_num_ports,
  1157. .open = mxuport_open,
  1158. .close = mxuport_close,
  1159. .set_termios = mxuport_set_termios,
  1160. .break_ctl = mxuport_break_ctl,
  1161. .tx_empty = mxuport_tx_empty,
  1162. .tiocmiwait = usb_serial_generic_tiocmiwait,
  1163. .get_icount = usb_serial_generic_get_icount,
  1164. .throttle = mxuport_throttle,
  1165. .unthrottle = mxuport_unthrottle,
  1166. .tiocmget = mxuport_tiocmget,
  1167. .tiocmset = mxuport_tiocmset,
  1168. .dtr_rts = mxuport_dtr_rts,
  1169. .process_read_urb = mxuport_process_read_urb,
  1170. .prepare_write_buffer = mxuport_prepare_write_buffer,
  1171. .resume = mxuport_resume,
  1172. };
  1173. static struct usb_serial_driver *const serial_drivers[] = {
  1174. &mxuport_device, NULL
  1175. };
  1176. module_usb_serial_driver(serial_drivers, mxuport_idtable);
  1177. MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
  1178. MODULE_AUTHOR("<support@moxa.com>");
  1179. MODULE_LICENSE("GPL");