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