mxuport.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  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 <andrew@lunn.ch>
  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. if (status != size) {
  227. dev_err(&serial->interface->dev,
  228. "%s - short write (%d / %zd)\n",
  229. __func__, status, size);
  230. return -EIO;
  231. }
  232. return 0;
  233. }
  234. /* Send a vendor request without any data */
  235. static int mxuport_send_ctrl_urb(struct usb_serial *serial,
  236. u8 request, u16 value, u16 index)
  237. {
  238. return mxuport_send_ctrl_data_urb(serial, request, value, index,
  239. NULL, 0);
  240. }
  241. /*
  242. * mxuport_throttle - throttle function of driver
  243. *
  244. * This function is called by the tty driver when it wants to stop the
  245. * data being read from the port. Since all the data comes over one
  246. * bulk in endpoint, we cannot stop submitting urbs by setting
  247. * port->throttle. Instead tell the device to stop sending us data for
  248. * the port.
  249. */
  250. static void mxuport_throttle(struct tty_struct *tty)
  251. {
  252. struct usb_serial_port *port = tty->driver_data;
  253. struct usb_serial *serial = port->serial;
  254. dev_dbg(&port->dev, "%s\n", __func__);
  255. mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
  256. 0, port->port_number);
  257. }
  258. /*
  259. * mxuport_unthrottle - unthrottle function of driver
  260. *
  261. * This function is called by the tty driver when it wants to resume
  262. * the data being read from the port. Tell the device it can resume
  263. * sending us received data from the port.
  264. */
  265. static void mxuport_unthrottle(struct tty_struct *tty)
  266. {
  267. struct usb_serial_port *port = tty->driver_data;
  268. struct usb_serial *serial = port->serial;
  269. dev_dbg(&port->dev, "%s\n", __func__);
  270. mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
  271. 1, port->port_number);
  272. }
  273. /*
  274. * Processes one chunk of data received for a port. Mostly a copy of
  275. * usb_serial_generic_process_read_urb().
  276. */
  277. static void mxuport_process_read_urb_data(struct usb_serial_port *port,
  278. char *data, int size)
  279. {
  280. int i;
  281. if (!port->port.console || !port->sysrq) {
  282. tty_insert_flip_string(&port->port, data, size);
  283. } else {
  284. for (i = 0; i < size; i++, data++) {
  285. if (!usb_serial_handle_sysrq_char(port, *data))
  286. tty_insert_flip_char(&port->port, *data,
  287. TTY_NORMAL);
  288. }
  289. }
  290. tty_flip_buffer_push(&port->port);
  291. }
  292. static void mxuport_msr_event(struct usb_serial_port *port, u8 buf[4])
  293. {
  294. struct mxuport_port *mxport = usb_get_serial_port_data(port);
  295. u8 rcv_msr_hold = buf[2] & 0xF0;
  296. u16 rcv_msr_event = get_unaligned_be16(buf);
  297. unsigned long flags;
  298. if (rcv_msr_event == 0)
  299. return;
  300. /* Update MSR status */
  301. spin_lock_irqsave(&mxport->spinlock, flags);
  302. dev_dbg(&port->dev, "%s - current MSR status = 0x%x\n",
  303. __func__, mxport->msr_state);
  304. if (rcv_msr_hold & UART_MSR_CTS) {
  305. mxport->msr_state |= UART_MSR_CTS;
  306. dev_dbg(&port->dev, "%s - CTS high\n", __func__);
  307. } else {
  308. mxport->msr_state &= ~UART_MSR_CTS;
  309. dev_dbg(&port->dev, "%s - CTS low\n", __func__);
  310. }
  311. if (rcv_msr_hold & UART_MSR_DSR) {
  312. mxport->msr_state |= UART_MSR_DSR;
  313. dev_dbg(&port->dev, "%s - DSR high\n", __func__);
  314. } else {
  315. mxport->msr_state &= ~UART_MSR_DSR;
  316. dev_dbg(&port->dev, "%s - DSR low\n", __func__);
  317. }
  318. if (rcv_msr_hold & UART_MSR_DCD) {
  319. mxport->msr_state |= UART_MSR_DCD;
  320. dev_dbg(&port->dev, "%s - DCD high\n", __func__);
  321. } else {
  322. mxport->msr_state &= ~UART_MSR_DCD;
  323. dev_dbg(&port->dev, "%s - DCD low\n", __func__);
  324. }
  325. spin_unlock_irqrestore(&mxport->spinlock, flags);
  326. if (rcv_msr_event &
  327. (SERIAL_EV_CTS | SERIAL_EV_DSR | SERIAL_EV_RLSD)) {
  328. if (rcv_msr_event & SERIAL_EV_CTS) {
  329. port->icount.cts++;
  330. dev_dbg(&port->dev, "%s - CTS change\n", __func__);
  331. }
  332. if (rcv_msr_event & SERIAL_EV_DSR) {
  333. port->icount.dsr++;
  334. dev_dbg(&port->dev, "%s - DSR change\n", __func__);
  335. }
  336. if (rcv_msr_event & SERIAL_EV_RLSD) {
  337. port->icount.dcd++;
  338. dev_dbg(&port->dev, "%s - DCD change\n", __func__);
  339. }
  340. wake_up_interruptible(&port->port.delta_msr_wait);
  341. }
  342. }
  343. static void mxuport_lsr_event(struct usb_serial_port *port, u8 buf[4])
  344. {
  345. u8 lsr_event = buf[2];
  346. if (lsr_event & UART_LSR_BI) {
  347. port->icount.brk++;
  348. dev_dbg(&port->dev, "%s - break error\n", __func__);
  349. }
  350. if (lsr_event & UART_LSR_FE) {
  351. port->icount.frame++;
  352. dev_dbg(&port->dev, "%s - frame error\n", __func__);
  353. }
  354. if (lsr_event & UART_LSR_PE) {
  355. port->icount.parity++;
  356. dev_dbg(&port->dev, "%s - parity error\n", __func__);
  357. }
  358. if (lsr_event & UART_LSR_OE) {
  359. port->icount.overrun++;
  360. dev_dbg(&port->dev, "%s - overrun error\n", __func__);
  361. }
  362. }
  363. /*
  364. * When something interesting happens, modem control lines XON/XOFF
  365. * etc, the device sends an event. Process these events.
  366. */
  367. static void mxuport_process_read_urb_event(struct usb_serial_port *port,
  368. u8 buf[4], u32 event)
  369. {
  370. dev_dbg(&port->dev, "%s - receive event : %04x\n", __func__, event);
  371. switch (event) {
  372. case UPORT_EVENT_SEND_NEXT:
  373. /*
  374. * Sent as part of the flow control on device buffers.
  375. * Not currently used.
  376. */
  377. break;
  378. case UPORT_EVENT_MSR:
  379. mxuport_msr_event(port, buf);
  380. break;
  381. case UPORT_EVENT_LSR:
  382. mxuport_lsr_event(port, buf);
  383. break;
  384. case UPORT_EVENT_MCR:
  385. /*
  386. * Event to indicate a change in XON/XOFF from the
  387. * peer. Currently not used. We just continue
  388. * sending the device data and it will buffer it if
  389. * needed. This event could be used for flow control
  390. * between the host and the device.
  391. */
  392. break;
  393. default:
  394. dev_dbg(&port->dev, "Unexpected event\n");
  395. break;
  396. }
  397. }
  398. /*
  399. * One URB can contain data for multiple ports. Demultiplex the data,
  400. * checking the port exists, is opened and the message is valid.
  401. */
  402. static void mxuport_process_read_urb_demux_data(struct urb *urb)
  403. {
  404. struct usb_serial_port *port = urb->context;
  405. struct usb_serial *serial = port->serial;
  406. u8 *data = urb->transfer_buffer;
  407. u8 *end = data + urb->actual_length;
  408. struct usb_serial_port *demux_port;
  409. u8 *ch;
  410. u16 rcv_port;
  411. u16 rcv_len;
  412. while (data < end) {
  413. if (data + HEADER_SIZE > end) {
  414. dev_warn(&port->dev, "%s - message with short header\n",
  415. __func__);
  416. return;
  417. }
  418. rcv_port = get_unaligned_be16(data);
  419. if (rcv_port >= serial->num_ports) {
  420. dev_warn(&port->dev, "%s - message for invalid port\n",
  421. __func__);
  422. return;
  423. }
  424. demux_port = serial->port[rcv_port];
  425. rcv_len = get_unaligned_be16(data + 2);
  426. if (!rcv_len || data + HEADER_SIZE + rcv_len > end) {
  427. dev_warn(&port->dev, "%s - short data\n", __func__);
  428. return;
  429. }
  430. if (tty_port_initialized(&demux_port->port)) {
  431. ch = data + HEADER_SIZE;
  432. mxuport_process_read_urb_data(demux_port, ch, rcv_len);
  433. } else {
  434. dev_dbg(&demux_port->dev, "%s - data for closed port\n",
  435. __func__);
  436. }
  437. data += HEADER_SIZE + rcv_len;
  438. }
  439. }
  440. /*
  441. * One URB can contain events for multiple ports. Demultiplex the event,
  442. * checking the port exists, and is opened.
  443. */
  444. static void mxuport_process_read_urb_demux_event(struct urb *urb)
  445. {
  446. struct usb_serial_port *port = urb->context;
  447. struct usb_serial *serial = port->serial;
  448. u8 *data = urb->transfer_buffer;
  449. u8 *end = data + urb->actual_length;
  450. struct usb_serial_port *demux_port;
  451. u8 *ch;
  452. u16 rcv_port;
  453. u16 rcv_event;
  454. while (data < end) {
  455. if (data + EVENT_LENGTH > end) {
  456. dev_warn(&port->dev, "%s - message with short event\n",
  457. __func__);
  458. return;
  459. }
  460. rcv_port = get_unaligned_be16(data);
  461. if (rcv_port >= serial->num_ports) {
  462. dev_warn(&port->dev, "%s - message for invalid port\n",
  463. __func__);
  464. return;
  465. }
  466. demux_port = serial->port[rcv_port];
  467. if (tty_port_initialized(&demux_port->port)) {
  468. ch = data + HEADER_SIZE;
  469. rcv_event = get_unaligned_be16(data + 2);
  470. mxuport_process_read_urb_event(demux_port, ch,
  471. rcv_event);
  472. } else {
  473. dev_dbg(&demux_port->dev,
  474. "%s - event for closed port\n", __func__);
  475. }
  476. data += EVENT_LENGTH;
  477. }
  478. }
  479. /*
  480. * This is called when we have received data on the bulk in
  481. * endpoint. Depending on which port it was received on, it can
  482. * contain serial data or events.
  483. */
  484. static void mxuport_process_read_urb(struct urb *urb)
  485. {
  486. struct usb_serial_port *port = urb->context;
  487. struct usb_serial *serial = port->serial;
  488. if (port == serial->port[0])
  489. mxuport_process_read_urb_demux_data(urb);
  490. if (port == serial->port[1])
  491. mxuport_process_read_urb_demux_event(urb);
  492. }
  493. /*
  494. * Ask the device how many bytes it has queued to be sent out. If
  495. * there are none, return true.
  496. */
  497. static bool mxuport_tx_empty(struct usb_serial_port *port)
  498. {
  499. struct usb_serial *serial = port->serial;
  500. bool is_empty = true;
  501. u32 txlen;
  502. u8 *len_buf;
  503. int err;
  504. len_buf = kzalloc(4, GFP_KERNEL);
  505. if (!len_buf)
  506. goto out;
  507. err = mxuport_recv_ctrl_urb(serial, RQ_VENDOR_GET_OUTQUEUE, 0,
  508. port->port_number, len_buf, 4);
  509. if (err < 0)
  510. goto out;
  511. txlen = get_unaligned_be32(len_buf);
  512. dev_dbg(&port->dev, "%s - tx len = %u\n", __func__, txlen);
  513. if (txlen != 0)
  514. is_empty = false;
  515. out:
  516. kfree(len_buf);
  517. return is_empty;
  518. }
  519. static int mxuport_set_mcr(struct usb_serial_port *port, u8 mcr_state)
  520. {
  521. struct usb_serial *serial = port->serial;
  522. int err;
  523. dev_dbg(&port->dev, "%s - %02x\n", __func__, mcr_state);
  524. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_MCR,
  525. mcr_state, port->port_number);
  526. if (err)
  527. dev_err(&port->dev, "%s - failed to change MCR\n", __func__);
  528. return err;
  529. }
  530. static int mxuport_set_dtr(struct usb_serial_port *port, int on)
  531. {
  532. struct mxuport_port *mxport = usb_get_serial_port_data(port);
  533. struct usb_serial *serial = port->serial;
  534. int err;
  535. mutex_lock(&mxport->mutex);
  536. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_DTR,
  537. !!on, port->port_number);
  538. if (!err) {
  539. if (on)
  540. mxport->mcr_state |= UART_MCR_DTR;
  541. else
  542. mxport->mcr_state &= ~UART_MCR_DTR;
  543. }
  544. mutex_unlock(&mxport->mutex);
  545. return err;
  546. }
  547. static int mxuport_set_rts(struct usb_serial_port *port, u8 state)
  548. {
  549. struct mxuport_port *mxport = usb_get_serial_port_data(port);
  550. struct usb_serial *serial = port->serial;
  551. int err;
  552. u8 mcr_state;
  553. mutex_lock(&mxport->mutex);
  554. mcr_state = mxport->mcr_state;
  555. switch (state) {
  556. case MX_RTS_DISABLE:
  557. mcr_state &= ~UART_MCR_RTS;
  558. break;
  559. case MX_RTS_ENABLE:
  560. mcr_state |= UART_MCR_RTS;
  561. break;
  562. case MX_RTS_HW:
  563. /*
  564. * Do not update mxport->mcr_state when doing hardware
  565. * flow control.
  566. */
  567. break;
  568. default:
  569. /*
  570. * Should not happen, but somebody might try passing
  571. * MX_RTS_NO_CHANGE, which is not valid.
  572. */
  573. err = -EINVAL;
  574. goto out;
  575. }
  576. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RTS,
  577. state, port->port_number);
  578. if (!err)
  579. mxport->mcr_state = mcr_state;
  580. out:
  581. mutex_unlock(&mxport->mutex);
  582. return err;
  583. }
  584. static void mxuport_dtr_rts(struct usb_serial_port *port, int on)
  585. {
  586. struct mxuport_port *mxport = usb_get_serial_port_data(port);
  587. u8 mcr_state;
  588. int err;
  589. mutex_lock(&mxport->mutex);
  590. mcr_state = mxport->mcr_state;
  591. if (on)
  592. mcr_state |= (UART_MCR_RTS | UART_MCR_DTR);
  593. else
  594. mcr_state &= ~(UART_MCR_RTS | UART_MCR_DTR);
  595. err = mxuport_set_mcr(port, mcr_state);
  596. if (!err)
  597. mxport->mcr_state = mcr_state;
  598. mutex_unlock(&mxport->mutex);
  599. }
  600. static int mxuport_tiocmset(struct tty_struct *tty, unsigned int set,
  601. unsigned int clear)
  602. {
  603. struct usb_serial_port *port = tty->driver_data;
  604. struct mxuport_port *mxport = usb_get_serial_port_data(port);
  605. int err;
  606. u8 mcr_state;
  607. mutex_lock(&mxport->mutex);
  608. mcr_state = mxport->mcr_state;
  609. if (set & TIOCM_RTS)
  610. mcr_state |= UART_MCR_RTS;
  611. if (set & TIOCM_DTR)
  612. mcr_state |= UART_MCR_DTR;
  613. if (clear & TIOCM_RTS)
  614. mcr_state &= ~UART_MCR_RTS;
  615. if (clear & TIOCM_DTR)
  616. mcr_state &= ~UART_MCR_DTR;
  617. err = mxuport_set_mcr(port, mcr_state);
  618. if (!err)
  619. mxport->mcr_state = mcr_state;
  620. mutex_unlock(&mxport->mutex);
  621. return err;
  622. }
  623. static int mxuport_tiocmget(struct tty_struct *tty)
  624. {
  625. struct mxuport_port *mxport;
  626. struct usb_serial_port *port = tty->driver_data;
  627. unsigned int result;
  628. unsigned long flags;
  629. unsigned int msr;
  630. unsigned int mcr;
  631. mxport = usb_get_serial_port_data(port);
  632. mutex_lock(&mxport->mutex);
  633. spin_lock_irqsave(&mxport->spinlock, flags);
  634. msr = mxport->msr_state;
  635. mcr = mxport->mcr_state;
  636. spin_unlock_irqrestore(&mxport->spinlock, flags);
  637. mutex_unlock(&mxport->mutex);
  638. result = (((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) | /* 0x002 */
  639. ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) | /* 0x004 */
  640. ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) | /* 0x020 */
  641. ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) | /* 0x040 */
  642. ((msr & UART_MSR_RI) ? TIOCM_RI : 0) | /* 0x080 */
  643. ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0)); /* 0x100 */
  644. dev_dbg(&port->dev, "%s - 0x%04x\n", __func__, result);
  645. return result;
  646. }
  647. static int mxuport_set_termios_flow(struct tty_struct *tty,
  648. struct ktermios *old_termios,
  649. struct usb_serial_port *port,
  650. struct usb_serial *serial)
  651. {
  652. u8 xon = START_CHAR(tty);
  653. u8 xoff = STOP_CHAR(tty);
  654. int enable;
  655. int err;
  656. u8 *buf;
  657. u8 rts;
  658. buf = kmalloc(2, GFP_KERNEL);
  659. if (!buf)
  660. return -ENOMEM;
  661. /* S/W flow control settings */
  662. if (I_IXOFF(tty) || I_IXON(tty)) {
  663. enable = 1;
  664. buf[0] = xon;
  665. buf[1] = xoff;
  666. err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_CHARS,
  667. 0, port->port_number,
  668. buf, 2);
  669. if (err)
  670. goto out;
  671. dev_dbg(&port->dev, "%s - XON = 0x%02x, XOFF = 0x%02x\n",
  672. __func__, xon, xoff);
  673. } else {
  674. enable = 0;
  675. }
  676. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_XONXOFF,
  677. enable, port->port_number);
  678. if (err)
  679. goto out;
  680. rts = MX_RTS_NO_CHANGE;
  681. /* H/W flow control settings */
  682. if (!old_termios ||
  683. C_CRTSCTS(tty) != (old_termios->c_cflag & CRTSCTS)) {
  684. if (C_CRTSCTS(tty))
  685. rts = MX_RTS_HW;
  686. else
  687. rts = MX_RTS_ENABLE;
  688. }
  689. if (C_BAUD(tty)) {
  690. if (old_termios && (old_termios->c_cflag & CBAUD) == B0) {
  691. /* Raise DTR and RTS */
  692. if (C_CRTSCTS(tty))
  693. rts = MX_RTS_HW;
  694. else
  695. rts = MX_RTS_ENABLE;
  696. mxuport_set_dtr(port, 1);
  697. }
  698. } else {
  699. /* Drop DTR and RTS */
  700. rts = MX_RTS_DISABLE;
  701. mxuport_set_dtr(port, 0);
  702. }
  703. if (rts != MX_RTS_NO_CHANGE)
  704. err = mxuport_set_rts(port, rts);
  705. out:
  706. kfree(buf);
  707. return err;
  708. }
  709. static void mxuport_set_termios(struct tty_struct *tty,
  710. struct usb_serial_port *port,
  711. struct ktermios *old_termios)
  712. {
  713. struct usb_serial *serial = port->serial;
  714. u8 *buf;
  715. u8 data_bits;
  716. u8 stop_bits;
  717. u8 parity;
  718. int baud;
  719. int err;
  720. if (old_termios &&
  721. !tty_termios_hw_change(&tty->termios, old_termios) &&
  722. tty->termios.c_iflag == old_termios->c_iflag) {
  723. dev_dbg(&port->dev, "%s - nothing to change\n", __func__);
  724. return;
  725. }
  726. buf = kmalloc(4, GFP_KERNEL);
  727. if (!buf)
  728. return;
  729. /* Set data bit of termios */
  730. switch (C_CSIZE(tty)) {
  731. case CS5:
  732. data_bits = MX_WORDLENGTH_5;
  733. break;
  734. case CS6:
  735. data_bits = MX_WORDLENGTH_6;
  736. break;
  737. case CS7:
  738. data_bits = MX_WORDLENGTH_7;
  739. break;
  740. case CS8:
  741. default:
  742. data_bits = MX_WORDLENGTH_8;
  743. break;
  744. }
  745. /* Set parity of termios */
  746. if (C_PARENB(tty)) {
  747. if (C_CMSPAR(tty)) {
  748. if (C_PARODD(tty))
  749. parity = MX_PARITY_MARK;
  750. else
  751. parity = MX_PARITY_SPACE;
  752. } else {
  753. if (C_PARODD(tty))
  754. parity = MX_PARITY_ODD;
  755. else
  756. parity = MX_PARITY_EVEN;
  757. }
  758. } else {
  759. parity = MX_PARITY_NONE;
  760. }
  761. /* Set stop bit of termios */
  762. if (C_CSTOPB(tty))
  763. stop_bits = MX_STOP_BITS_2;
  764. else
  765. stop_bits = MX_STOP_BITS_1;
  766. buf[0] = data_bits;
  767. buf[1] = parity;
  768. buf[2] = stop_bits;
  769. buf[3] = 0;
  770. err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_LINE,
  771. 0, port->port_number, buf, 4);
  772. if (err)
  773. goto out;
  774. err = mxuport_set_termios_flow(tty, old_termios, port, serial);
  775. if (err)
  776. goto out;
  777. baud = tty_get_baud_rate(tty);
  778. if (!baud)
  779. baud = 9600;
  780. /* Note: Little Endian */
  781. put_unaligned_le32(baud, buf);
  782. err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_BAUD,
  783. 0, port->port_number,
  784. buf, 4);
  785. if (err)
  786. goto out;
  787. dev_dbg(&port->dev, "baud_rate : %d\n", baud);
  788. dev_dbg(&port->dev, "data_bits : %d\n", data_bits);
  789. dev_dbg(&port->dev, "parity : %d\n", parity);
  790. dev_dbg(&port->dev, "stop_bits : %d\n", stop_bits);
  791. out:
  792. kfree(buf);
  793. }
  794. /*
  795. * Determine how many ports this device has dynamically. It will be
  796. * called after the probe() callback is called, but before attach().
  797. */
  798. static int mxuport_calc_num_ports(struct usb_serial *serial,
  799. struct usb_serial_endpoints *epds)
  800. {
  801. unsigned long features = (unsigned long)usb_get_serial_data(serial);
  802. int num_ports;
  803. int i;
  804. if (features & MX_UPORT_2_PORT) {
  805. num_ports = 2;
  806. } else if (features & MX_UPORT_4_PORT) {
  807. num_ports = 4;
  808. } else if (features & MX_UPORT_8_PORT) {
  809. num_ports = 8;
  810. } else if (features & MX_UPORT_16_PORT) {
  811. num_ports = 16;
  812. } else {
  813. dev_warn(&serial->interface->dev,
  814. "unknown device, assuming two ports\n");
  815. num_ports = 2;
  816. }
  817. /*
  818. * Setup bulk-out endpoint multiplexing. All ports share the same
  819. * bulk-out endpoint.
  820. */
  821. BUILD_BUG_ON(ARRAY_SIZE(epds->bulk_out) < 16);
  822. for (i = 1; i < num_ports; ++i)
  823. epds->bulk_out[i] = epds->bulk_out[0];
  824. epds->num_bulk_out = num_ports;
  825. return num_ports;
  826. }
  827. /* Get the version of the firmware currently running. */
  828. static int mxuport_get_fw_version(struct usb_serial *serial, u32 *version)
  829. {
  830. u8 *ver_buf;
  831. int err;
  832. ver_buf = kzalloc(4, GFP_KERNEL);
  833. if (!ver_buf)
  834. return -ENOMEM;
  835. /* Get firmware version from SDRAM */
  836. err = mxuport_recv_ctrl_urb(serial, RQ_VENDOR_GET_VERSION, 0, 0,
  837. ver_buf, 4);
  838. if (err != 4) {
  839. err = -EIO;
  840. goto out;
  841. }
  842. *version = (ver_buf[0] << 16) | (ver_buf[1] << 8) | ver_buf[2];
  843. err = 0;
  844. out:
  845. kfree(ver_buf);
  846. return err;
  847. }
  848. /* Given a firmware blob, download it to the device. */
  849. static int mxuport_download_fw(struct usb_serial *serial,
  850. const struct firmware *fw_p)
  851. {
  852. u8 *fw_buf;
  853. size_t txlen;
  854. size_t fwidx;
  855. int err;
  856. fw_buf = kmalloc(DOWN_BLOCK_SIZE, GFP_KERNEL);
  857. if (!fw_buf)
  858. return -ENOMEM;
  859. dev_dbg(&serial->interface->dev, "Starting firmware download...\n");
  860. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_START_FW_DOWN, 0, 0);
  861. if (err)
  862. goto out;
  863. fwidx = 0;
  864. do {
  865. txlen = min_t(size_t, (fw_p->size - fwidx), DOWN_BLOCK_SIZE);
  866. memcpy(fw_buf, &fw_p->data[fwidx], txlen);
  867. err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_FW_DATA,
  868. 0, 0, fw_buf, txlen);
  869. if (err) {
  870. mxuport_send_ctrl_urb(serial, RQ_VENDOR_STOP_FW_DOWN,
  871. 0, 0);
  872. goto out;
  873. }
  874. fwidx += txlen;
  875. usleep_range(1000, 2000);
  876. } while (fwidx < fw_p->size);
  877. msleep(1000);
  878. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_STOP_FW_DOWN, 0, 0);
  879. if (err)
  880. goto out;
  881. msleep(1000);
  882. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_QUERY_FW_READY, 0, 0);
  883. out:
  884. kfree(fw_buf);
  885. return err;
  886. }
  887. static int mxuport_probe(struct usb_serial *serial,
  888. const struct usb_device_id *id)
  889. {
  890. u16 productid = le16_to_cpu(serial->dev->descriptor.idProduct);
  891. const struct firmware *fw_p = NULL;
  892. u32 version;
  893. int local_ver;
  894. char buf[32];
  895. int err;
  896. /* Load our firmware */
  897. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_QUERY_FW_CONFIG, 0, 0);
  898. if (err) {
  899. mxuport_send_ctrl_urb(serial, RQ_VENDOR_RESET_DEVICE, 0, 0);
  900. return err;
  901. }
  902. err = mxuport_get_fw_version(serial, &version);
  903. if (err < 0)
  904. return err;
  905. dev_dbg(&serial->interface->dev, "Device firmware version v%x.%x.%x\n",
  906. (version & 0xff0000) >> 16,
  907. (version & 0xff00) >> 8,
  908. (version & 0xff));
  909. snprintf(buf, sizeof(buf) - 1, "moxa/moxa-%04x.fw", productid);
  910. err = request_firmware(&fw_p, buf, &serial->interface->dev);
  911. if (err) {
  912. dev_warn(&serial->interface->dev, "Firmware %s not found\n",
  913. buf);
  914. /* Use the firmware already in the device */
  915. err = 0;
  916. } else {
  917. local_ver = ((fw_p->data[VER_ADDR_1] << 16) |
  918. (fw_p->data[VER_ADDR_2] << 8) |
  919. fw_p->data[VER_ADDR_3]);
  920. dev_dbg(&serial->interface->dev,
  921. "Available firmware version v%x.%x.%x\n",
  922. fw_p->data[VER_ADDR_1], fw_p->data[VER_ADDR_2],
  923. fw_p->data[VER_ADDR_3]);
  924. if (local_ver > version) {
  925. err = mxuport_download_fw(serial, fw_p);
  926. if (err)
  927. goto out;
  928. err = mxuport_get_fw_version(serial, &version);
  929. if (err < 0)
  930. goto out;
  931. }
  932. }
  933. dev_info(&serial->interface->dev,
  934. "Using device firmware version v%x.%x.%x\n",
  935. (version & 0xff0000) >> 16,
  936. (version & 0xff00) >> 8,
  937. (version & 0xff));
  938. /*
  939. * Contains the features of this hardware. Store away for
  940. * later use, eg, number of ports.
  941. */
  942. usb_set_serial_data(serial, (void *)id->driver_info);
  943. out:
  944. if (fw_p)
  945. release_firmware(fw_p);
  946. return err;
  947. }
  948. static int mxuport_port_probe(struct usb_serial_port *port)
  949. {
  950. struct usb_serial *serial = port->serial;
  951. struct mxuport_port *mxport;
  952. int err;
  953. mxport = devm_kzalloc(&port->dev, sizeof(struct mxuport_port),
  954. GFP_KERNEL);
  955. if (!mxport)
  956. return -ENOMEM;
  957. mutex_init(&mxport->mutex);
  958. spin_lock_init(&mxport->spinlock);
  959. /* Set the port private data */
  960. usb_set_serial_port_data(port, mxport);
  961. /* Set FIFO (Enable) */
  962. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_FIFO_DISABLE,
  963. 0, port->port_number);
  964. if (err)
  965. return err;
  966. /* Set transmission mode (Hi-Performance) */
  967. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_HIGH_PERFOR,
  968. 0, port->port_number);
  969. if (err)
  970. return err;
  971. /* Set interface (RS-232) */
  972. return mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_INTERFACE,
  973. MX_INT_RS232,
  974. port->port_number);
  975. }
  976. static int mxuport_attach(struct usb_serial *serial)
  977. {
  978. struct usb_serial_port *port0 = serial->port[0];
  979. struct usb_serial_port *port1 = serial->port[1];
  980. int err;
  981. /*
  982. * All data from the ports is received on the first bulk in
  983. * endpoint, with a multiplex header. The second bulk in is
  984. * used for events.
  985. *
  986. * Start to read from the device.
  987. */
  988. err = usb_serial_generic_submit_read_urbs(port0, GFP_KERNEL);
  989. if (err)
  990. return err;
  991. err = usb_serial_generic_submit_read_urbs(port1, GFP_KERNEL);
  992. if (err) {
  993. usb_serial_generic_close(port0);
  994. return err;
  995. }
  996. return 0;
  997. }
  998. static void mxuport_release(struct usb_serial *serial)
  999. {
  1000. struct usb_serial_port *port0 = serial->port[0];
  1001. struct usb_serial_port *port1 = serial->port[1];
  1002. usb_serial_generic_close(port1);
  1003. usb_serial_generic_close(port0);
  1004. }
  1005. static int mxuport_open(struct tty_struct *tty, struct usb_serial_port *port)
  1006. {
  1007. struct mxuport_port *mxport = usb_get_serial_port_data(port);
  1008. struct usb_serial *serial = port->serial;
  1009. int err;
  1010. /* Set receive host (enable) */
  1011. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
  1012. 1, port->port_number);
  1013. if (err)
  1014. return err;
  1015. err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_OPEN,
  1016. 1, port->port_number);
  1017. if (err) {
  1018. mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
  1019. 0, port->port_number);
  1020. return err;
  1021. }
  1022. /* Initial port termios */
  1023. if (tty)
  1024. mxuport_set_termios(tty, port, NULL);
  1025. /*
  1026. * TODO: use RQ_VENDOR_GET_MSR, once we know what it
  1027. * returns.
  1028. */
  1029. mxport->msr_state = 0;
  1030. return err;
  1031. }
  1032. static void mxuport_close(struct usb_serial_port *port)
  1033. {
  1034. struct usb_serial *serial = port->serial;
  1035. mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_OPEN, 0,
  1036. port->port_number);
  1037. mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN, 0,
  1038. port->port_number);
  1039. }
  1040. /* Send a break to the port. */
  1041. static void mxuport_break_ctl(struct tty_struct *tty, int break_state)
  1042. {
  1043. struct usb_serial_port *port = tty->driver_data;
  1044. struct usb_serial *serial = port->serial;
  1045. int enable;
  1046. if (break_state == -1) {
  1047. enable = 1;
  1048. dev_dbg(&port->dev, "%s - sending break\n", __func__);
  1049. } else {
  1050. enable = 0;
  1051. dev_dbg(&port->dev, "%s - clearing break\n", __func__);
  1052. }
  1053. mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_BREAK,
  1054. enable, port->port_number);
  1055. }
  1056. static int mxuport_resume(struct usb_serial *serial)
  1057. {
  1058. struct usb_serial_port *port;
  1059. int c = 0;
  1060. int i;
  1061. int r;
  1062. for (i = 0; i < 2; i++) {
  1063. port = serial->port[i];
  1064. r = usb_serial_generic_submit_read_urbs(port, GFP_NOIO);
  1065. if (r < 0)
  1066. c++;
  1067. }
  1068. for (i = 0; i < serial->num_ports; i++) {
  1069. port = serial->port[i];
  1070. if (!tty_port_initialized(&port->port))
  1071. continue;
  1072. r = usb_serial_generic_write_start(port, GFP_NOIO);
  1073. if (r < 0)
  1074. c++;
  1075. }
  1076. return c ? -EIO : 0;
  1077. }
  1078. static struct usb_serial_driver mxuport_device = {
  1079. .driver = {
  1080. .owner = THIS_MODULE,
  1081. .name = "mxuport",
  1082. },
  1083. .description = "MOXA UPort",
  1084. .id_table = mxuport_idtable,
  1085. .num_bulk_in = 2,
  1086. .num_bulk_out = 1,
  1087. .probe = mxuport_probe,
  1088. .port_probe = mxuport_port_probe,
  1089. .attach = mxuport_attach,
  1090. .release = mxuport_release,
  1091. .calc_num_ports = mxuport_calc_num_ports,
  1092. .open = mxuport_open,
  1093. .close = mxuport_close,
  1094. .set_termios = mxuport_set_termios,
  1095. .break_ctl = mxuport_break_ctl,
  1096. .tx_empty = mxuport_tx_empty,
  1097. .tiocmiwait = usb_serial_generic_tiocmiwait,
  1098. .get_icount = usb_serial_generic_get_icount,
  1099. .throttle = mxuport_throttle,
  1100. .unthrottle = mxuport_unthrottle,
  1101. .tiocmget = mxuport_tiocmget,
  1102. .tiocmset = mxuport_tiocmset,
  1103. .dtr_rts = mxuport_dtr_rts,
  1104. .process_read_urb = mxuport_process_read_urb,
  1105. .prepare_write_buffer = mxuport_prepare_write_buffer,
  1106. .resume = mxuport_resume,
  1107. };
  1108. static struct usb_serial_driver *const serial_drivers[] = {
  1109. &mxuport_device, NULL
  1110. };
  1111. module_usb_serial_driver(serial_drivers, mxuport_idtable);
  1112. MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
  1113. MODULE_AUTHOR("<support@moxa.com>");
  1114. MODULE_LICENSE("GPL");