2 * USB Moxa UPORT 11x0 Serial Driver
4 * Copyright (C) 2007 MOXA Technologies Co., Ltd.
5 * Copyright (C) 2015 Mathieu Othacehe <m.othacehe@gmail.com>
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.
13 * Supports the following Moxa USB to serial converters:
14 * UPort 1110, 1 port RS-232 USB to Serial Hub.
15 * UPort 1130, 1 port RS-422/485 USB to Serial Hub.
16 * UPort 1130I, 1 port RS-422/485 USB to Serial Hub with isolation
18 * UPort 1150, 1 port RS-232/422/485 USB to Serial Hub.
19 * UPort 1150I, 1 port RS-232/422/485 USB to Serial Hub with isolation
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/firmware.h>
26 #include <linux/jiffies.h>
27 #include <linux/serial.h>
28 #include <linux/serial_reg.h>
29 #include <linux/slab.h>
30 #include <linux/spinlock.h>
31 #include <linux/mutex.h>
32 #include <linux/tty.h>
33 #include <linux/tty_driver.h>
34 #include <linux/tty_flip.h>
35 #include <linux/uaccess.h>
36 #include <linux/usb.h>
37 #include <linux/usb/serial.h>
39 /* Vendor and product ids */
40 #define MXU1_VENDOR_ID 0x110a
41 #define MXU1_1110_PRODUCT_ID 0x1110
42 #define MXU1_1130_PRODUCT_ID 0x1130
43 #define MXU1_1150_PRODUCT_ID 0x1150
44 #define MXU1_1151_PRODUCT_ID 0x1151
45 #define MXU1_1131_PRODUCT_ID 0x1131
48 #define MXU1_GET_VERSION 0x01
49 #define MXU1_GET_PORT_STATUS 0x02
50 #define MXU1_GET_PORT_DEV_INFO 0x03
51 #define MXU1_GET_CONFIG 0x04
52 #define MXU1_SET_CONFIG 0x05
53 #define MXU1_OPEN_PORT 0x06
54 #define MXU1_CLOSE_PORT 0x07
55 #define MXU1_START_PORT 0x08
56 #define MXU1_STOP_PORT 0x09
57 #define MXU1_TEST_PORT 0x0A
58 #define MXU1_PURGE_PORT 0x0B
59 #define MXU1_RESET_EXT_DEVICE 0x0C
60 #define MXU1_GET_OUTQUEUE 0x0D
61 #define MXU1_WRITE_DATA 0x80
62 #define MXU1_READ_DATA 0x81
63 #define MXU1_REQ_TYPE_CLASS 0x82
65 /* Module identifiers */
66 #define MXU1_I2C_PORT 0x01
67 #define MXU1_IEEE1284_PORT 0x02
68 #define MXU1_UART1_PORT 0x03
69 #define MXU1_UART2_PORT 0x04
70 #define MXU1_RAM_PORT 0x05
73 #define MXU1_MSR_DELTA_CTS 0x01
74 #define MXU1_MSR_DELTA_DSR 0x02
75 #define MXU1_MSR_DELTA_RI 0x04
76 #define MXU1_MSR_DELTA_CD 0x08
77 #define MXU1_MSR_CTS 0x10
78 #define MXU1_MSR_DSR 0x20
79 #define MXU1_MSR_RI 0x40
80 #define MXU1_MSR_CD 0x80
81 #define MXU1_MSR_DELTA_MASK 0x0F
82 #define MXU1_MSR_MASK 0xF0
85 #define MXU1_LSR_OVERRUN_ERROR 0x01
86 #define MXU1_LSR_PARITY_ERROR 0x02
87 #define MXU1_LSR_FRAMING_ERROR 0x04
88 #define MXU1_LSR_BREAK 0x08
89 #define MXU1_LSR_ERROR 0x0F
90 #define MXU1_LSR_RX_FULL 0x10
91 #define MXU1_LSR_TX_EMPTY 0x20
94 #define MXU1_MCR_LOOP 0x04
95 #define MXU1_MCR_DTR 0x10
96 #define MXU1_MCR_RTS 0x20
99 #define MXU1_UART_ENABLE_RTS_IN 0x0001
100 #define MXU1_UART_DISABLE_RTS 0x0002
101 #define MXU1_UART_ENABLE_PARITY_CHECKING 0x0008
102 #define MXU1_UART_ENABLE_DSR_OUT 0x0010
103 #define MXU1_UART_ENABLE_CTS_OUT 0x0020
104 #define MXU1_UART_ENABLE_X_OUT 0x0040
105 #define MXU1_UART_ENABLE_XA_OUT 0x0080
106 #define MXU1_UART_ENABLE_X_IN 0x0100
107 #define MXU1_UART_ENABLE_DTR_IN 0x0800
108 #define MXU1_UART_DISABLE_DTR 0x1000
109 #define MXU1_UART_ENABLE_MS_INTS 0x2000
110 #define MXU1_UART_ENABLE_AUTO_START_DMA 0x4000
111 #define MXU1_UART_SEND_BREAK_SIGNAL 0x8000
114 #define MXU1_UART_NO_PARITY 0x00
115 #define MXU1_UART_ODD_PARITY 0x01
116 #define MXU1_UART_EVEN_PARITY 0x02
117 #define MXU1_UART_MARK_PARITY 0x03
118 #define MXU1_UART_SPACE_PARITY 0x04
121 #define MXU1_UART_1_STOP_BITS 0x00
122 #define MXU1_UART_1_5_STOP_BITS 0x01
123 #define MXU1_UART_2_STOP_BITS 0x02
125 /* Bits per character */
126 #define MXU1_UART_5_DATA_BITS 0x00
127 #define MXU1_UART_6_DATA_BITS 0x01
128 #define MXU1_UART_7_DATA_BITS 0x02
129 #define MXU1_UART_8_DATA_BITS 0x03
131 /* Operation modes */
132 #define MXU1_UART_232 0x00
133 #define MXU1_UART_485_RECEIVER_DISABLED 0x01
134 #define MXU1_UART_485_RECEIVER_ENABLED 0x02
136 /* Pipe transfer mode and timeout */
137 #define MXU1_PIPE_MODE_CONTINUOUS 0x01
138 #define MXU1_PIPE_MODE_MASK 0x03
139 #define MXU1_PIPE_TIMEOUT_MASK 0x7C
140 #define MXU1_PIPE_TIMEOUT_ENABLE 0x80
143 struct mxu1_uart_config {
155 #define MXU1_PURGE_OUTPUT 0x00
156 #define MXU1_PURGE_INPUT 0x80
158 /* Read/Write data */
159 #define MXU1_RW_DATA_ADDR_SFR 0x10
160 #define MXU1_RW_DATA_ADDR_IDATA 0x20
161 #define MXU1_RW_DATA_ADDR_XDATA 0x30
162 #define MXU1_RW_DATA_ADDR_CODE 0x40
163 #define MXU1_RW_DATA_ADDR_GPIO 0x50
164 #define MXU1_RW_DATA_ADDR_I2C 0x60
165 #define MXU1_RW_DATA_ADDR_FLASH 0x70
166 #define MXU1_RW_DATA_ADDR_DSP 0x80
168 #define MXU1_RW_DATA_UNSPECIFIED 0x00
169 #define MXU1_RW_DATA_BYTE 0x01
170 #define MXU1_RW_DATA_WORD 0x02
171 #define MXU1_RW_DATA_DOUBLE_WORD 0x04
173 struct mxu1_write_data_bytes {
182 /* Interrupt codes */
183 #define MXU1_CODE_HARDWARE_ERROR 0xFF
184 #define MXU1_CODE_DATA_ERROR 0x03
185 #define MXU1_CODE_MODEM_STATUS 0x04
187 static inline int mxu1_get_func_from_code(unsigned char code)
192 /* Download firmware max packet size */
193 #define MXU1_DOWNLOAD_MAX_PACKET_SIZE 64
195 /* Firmware image header */
196 struct mxu1_firmware_header {
201 #define MXU1_UART_BASE_ADDR 0xFFA0
202 #define MXU1_UART_OFFSET_MCR 0x0004
204 #define MXU1_BAUD_BASE 923077
206 #define MXU1_TRANSFER_TIMEOUT 2
207 #define MXU1_DOWNLOAD_TIMEOUT 1000
208 #define MXU1_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */
214 spinlock_t spinlock; /* Protects msr */
215 struct mutex mutex; /* Protects mcr */
223 static const struct usb_device_id mxu1_idtable[] = {
224 { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1110_PRODUCT_ID) },
225 { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1130_PRODUCT_ID) },
226 { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1150_PRODUCT_ID) },
227 { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1151_PRODUCT_ID) },
228 { USB_DEVICE(MXU1_VENDOR_ID, MXU1_1131_PRODUCT_ID) },
232 MODULE_DEVICE_TABLE(usb, mxu1_idtable);
234 /* Write the given buffer out to the control pipe. */
235 static int mxu1_send_ctrl_data_urb(struct usb_serial *serial,
237 u16 value, u16 index,
238 void *data, size_t size)
242 status = usb_control_msg(serial->dev,
243 usb_sndctrlpipe(serial->dev, 0),
245 (USB_DIR_OUT | USB_TYPE_VENDOR |
246 USB_RECIP_DEVICE), value, index,
248 USB_CTRL_SET_TIMEOUT);
250 dev_err(&serial->interface->dev,
251 "%s - usb_control_msg failed: %d\n",
256 if (status != size) {
257 dev_err(&serial->interface->dev,
258 "%s - short write (%d / %zd)\n",
259 __func__, status, size);
266 /* Send a vendor request without any data */
267 static int mxu1_send_ctrl_urb(struct usb_serial *serial,
268 u8 request, u16 value, u16 index)
270 return mxu1_send_ctrl_data_urb(serial, request, value, index,
274 static int mxu1_download_firmware(struct usb_serial *serial,
275 const struct firmware *fw_p)
284 struct usb_device *dev = serial->dev;
285 struct mxu1_firmware_header *header;
288 pipe = usb_sndbulkpipe(dev, serial->port[0]->bulk_out_endpointAddress);
290 buffer_size = fw_p->size + sizeof(*header);
291 buffer = kmalloc(buffer_size, GFP_KERNEL);
295 memcpy(buffer, fw_p->data, fw_p->size);
296 memset(buffer + fw_p->size, 0xff, buffer_size - fw_p->size);
298 for (pos = sizeof(*header); pos < buffer_size; pos++)
299 cs = (u8)(cs + buffer[pos]);
301 header = (struct mxu1_firmware_header *)buffer;
302 header->wLength = cpu_to_le16(buffer_size - sizeof(*header));
303 header->bCheckSum = cs;
305 dev_dbg(&dev->dev, "%s - downloading firmware\n", __func__);
307 for (pos = 0; pos < buffer_size; pos += done) {
308 len = min(buffer_size - pos, MXU1_DOWNLOAD_MAX_PACKET_SIZE);
310 status = usb_bulk_msg(dev, pipe, buffer + pos, len, &done,
311 MXU1_DOWNLOAD_TIMEOUT);
319 dev_err(&dev->dev, "failed to download firmware: %d\n", status);
323 msleep_interruptible(100);
324 usb_reset_device(dev);
326 dev_dbg(&dev->dev, "%s - download successful\n", __func__);
331 static int mxu1_port_probe(struct usb_serial_port *port)
333 struct mxu1_port *mxport;
334 struct mxu1_device *mxdev;
336 if (!port->interrupt_in_urb) {
337 dev_err(&port->dev, "no interrupt urb\n");
341 mxport = kzalloc(sizeof(struct mxu1_port), GFP_KERNEL);
345 spin_lock_init(&mxport->spinlock);
346 mutex_init(&mxport->mutex);
348 mxdev = usb_get_serial_data(port->serial);
350 switch (mxdev->mxd_model) {
351 case MXU1_1110_PRODUCT_ID:
352 case MXU1_1150_PRODUCT_ID:
353 case MXU1_1151_PRODUCT_ID:
354 mxport->uart_mode = MXU1_UART_232;
356 case MXU1_1130_PRODUCT_ID:
357 case MXU1_1131_PRODUCT_ID:
358 mxport->uart_mode = MXU1_UART_485_RECEIVER_DISABLED;
362 usb_set_serial_port_data(port, mxport);
364 port->port.closing_wait =
365 msecs_to_jiffies(MXU1_DEFAULT_CLOSING_WAIT * 10);
366 port->port.drain_delay = 1;
371 static int mxu1_port_remove(struct usb_serial_port *port)
373 struct mxu1_port *mxport;
375 mxport = usb_get_serial_port_data(port);
381 static int mxu1_startup(struct usb_serial *serial)
383 struct mxu1_device *mxdev;
384 struct usb_device *dev = serial->dev;
385 struct usb_host_interface *cur_altsetting;
387 const struct firmware *fw_p = NULL;
390 dev_dbg(&serial->interface->dev, "%s - product 0x%04X, num configurations %d, configuration value %d\n",
391 __func__, le16_to_cpu(dev->descriptor.idProduct),
392 dev->descriptor.bNumConfigurations,
393 dev->actconfig->desc.bConfigurationValue);
395 /* create device structure */
396 mxdev = kzalloc(sizeof(struct mxu1_device), GFP_KERNEL);
400 usb_set_serial_data(serial, mxdev);
402 mxdev->mxd_model = le16_to_cpu(dev->descriptor.idProduct);
404 cur_altsetting = serial->interface->cur_altsetting;
406 /* if we have only 1 configuration, download firmware */
407 if (cur_altsetting->desc.bNumEndpoints == 1) {
414 err = request_firmware(&fw_p, fw_name, &serial->interface->dev);
416 dev_err(&serial->interface->dev, "failed to request firmware: %d\n",
421 err = mxu1_download_firmware(serial, fw_p);
423 goto err_release_firmware;
425 /* device is being reset */
427 goto err_release_firmware;
432 err_release_firmware:
433 release_firmware(fw_p);
440 static void mxu1_release(struct usb_serial *serial)
442 struct mxu1_device *mxdev;
444 mxdev = usb_get_serial_data(serial);
448 static int mxu1_write_byte(struct usb_serial_port *port, u32 addr,
453 struct mxu1_write_data_bytes *data;
455 dev_dbg(&port->dev, "%s - addr 0x%08X, mask 0x%02X, byte 0x%02X\n",
456 __func__, addr, mask, byte);
458 size = sizeof(struct mxu1_write_data_bytes) + 2;
459 data = kzalloc(size, GFP_KERNEL);
463 data->bAddrType = MXU1_RW_DATA_ADDR_XDATA;
464 data->bDataType = MXU1_RW_DATA_BYTE;
465 data->bDataCounter = 1;
466 data->wBaseAddrHi = cpu_to_be16(addr >> 16);
467 data->wBaseAddrLo = cpu_to_be16(addr);
468 data->bData[0] = mask;
469 data->bData[1] = byte;
471 status = mxu1_send_ctrl_data_urb(port->serial, MXU1_WRITE_DATA, 0,
472 MXU1_RAM_PORT, data, size);
474 dev_err(&port->dev, "%s - failed: %d\n", __func__, status);
481 static int mxu1_set_mcr(struct usb_serial_port *port, unsigned int mcr)
485 status = mxu1_write_byte(port,
486 MXU1_UART_BASE_ADDR + MXU1_UART_OFFSET_MCR,
487 MXU1_MCR_RTS | MXU1_MCR_DTR | MXU1_MCR_LOOP,
492 static void mxu1_set_termios(struct tty_struct *tty,
493 struct usb_serial_port *port,
494 struct ktermios *old_termios)
496 struct mxu1_port *mxport = usb_get_serial_port_data(port);
497 struct mxu1_uart_config *config;
498 tcflag_t cflag, iflag;
503 cflag = tty->termios.c_cflag;
504 iflag = tty->termios.c_iflag;
507 !tty_termios_hw_change(&tty->termios, old_termios) &&
508 tty->termios.c_iflag == old_termios->c_iflag) {
509 dev_dbg(&port->dev, "%s - nothing to change\n", __func__);
514 "%s - cflag 0x%08x, iflag 0x%08x\n", __func__, cflag, iflag);
517 dev_dbg(&port->dev, "%s - old cflag 0x%08x, old iflag 0x%08x\n",
519 old_termios->c_cflag,
520 old_termios->c_iflag);
523 config = kzalloc(sizeof(*config), GFP_KERNEL);
527 /* these flags must be set */
528 config->wFlags |= MXU1_UART_ENABLE_MS_INTS;
529 config->wFlags |= MXU1_UART_ENABLE_AUTO_START_DMA;
530 if (mxport->send_break)
531 config->wFlags |= MXU1_UART_SEND_BREAK_SIGNAL;
532 config->bUartMode = mxport->uart_mode;
534 switch (C_CSIZE(tty)) {
536 config->bDataBits = MXU1_UART_5_DATA_BITS;
539 config->bDataBits = MXU1_UART_6_DATA_BITS;
542 config->bDataBits = MXU1_UART_7_DATA_BITS;
546 config->bDataBits = MXU1_UART_8_DATA_BITS;
551 config->wFlags |= MXU1_UART_ENABLE_PARITY_CHECKING;
554 config->bParity = MXU1_UART_MARK_PARITY;
556 config->bParity = MXU1_UART_SPACE_PARITY;
559 config->bParity = MXU1_UART_ODD_PARITY;
561 config->bParity = MXU1_UART_EVEN_PARITY;
564 config->bParity = MXU1_UART_NO_PARITY;
568 config->bStopBits = MXU1_UART_2_STOP_BITS;
570 config->bStopBits = MXU1_UART_1_STOP_BITS;
572 if (C_CRTSCTS(tty)) {
573 /* RTS flow control must be off to drop RTS for baud rate B0 */
574 if (C_BAUD(tty) != B0)
575 config->wFlags |= MXU1_UART_ENABLE_RTS_IN;
576 config->wFlags |= MXU1_UART_ENABLE_CTS_OUT;
579 if (I_IXOFF(tty) || I_IXON(tty)) {
580 config->cXon = START_CHAR(tty);
581 config->cXoff = STOP_CHAR(tty);
584 config->wFlags |= MXU1_UART_ENABLE_X_IN;
587 config->wFlags |= MXU1_UART_ENABLE_X_OUT;
590 baud = tty_get_baud_rate(tty);
593 config->wBaudRate = MXU1_BAUD_BASE / baud;
595 dev_dbg(&port->dev, "%s - BaudRate=%d, wBaudRate=%d, wFlags=0x%04X, bDataBits=%d, bParity=%d, bStopBits=%d, cXon=%d, cXoff=%d, bUartMode=%d\n",
596 __func__, baud, config->wBaudRate, config->wFlags,
597 config->bDataBits, config->bParity, config->bStopBits,
598 config->cXon, config->cXoff, config->bUartMode);
600 cpu_to_be16s(&config->wBaudRate);
601 cpu_to_be16s(&config->wFlags);
603 status = mxu1_send_ctrl_data_urb(port->serial, MXU1_SET_CONFIG, 0,
604 MXU1_UART1_PORT, config,
607 dev_err(&port->dev, "cannot set config: %d\n", status);
609 mutex_lock(&mxport->mutex);
612 if (C_BAUD(tty) == B0)
613 mcr &= ~(MXU1_MCR_DTR | MXU1_MCR_RTS);
614 else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
615 mcr |= MXU1_MCR_DTR | MXU1_MCR_RTS;
617 status = mxu1_set_mcr(port, mcr);
619 dev_err(&port->dev, "cannot set modem control: %d\n", status);
623 mutex_unlock(&mxport->mutex);
628 static int mxu1_get_serial_info(struct usb_serial_port *port,
629 struct serial_struct __user *ret_arg)
631 struct serial_struct ret_serial;
637 cwait = port->port.closing_wait;
638 if (cwait != ASYNC_CLOSING_WAIT_NONE)
639 cwait = jiffies_to_msecs(cwait) / 10;
641 memset(&ret_serial, 0, sizeof(ret_serial));
643 ret_serial.type = PORT_16550A;
644 ret_serial.line = port->minor;
646 ret_serial.xmit_fifo_size = port->bulk_out_size;
647 ret_serial.baud_base = MXU1_BAUD_BASE;
648 ret_serial.close_delay = 5*HZ;
649 ret_serial.closing_wait = cwait;
651 if (copy_to_user(ret_arg, &ret_serial, sizeof(*ret_arg)))
658 static int mxu1_set_serial_info(struct usb_serial_port *port,
659 struct serial_struct __user *new_arg)
661 struct serial_struct new_serial;
664 if (copy_from_user(&new_serial, new_arg, sizeof(new_serial)))
667 cwait = new_serial.closing_wait;
668 if (cwait != ASYNC_CLOSING_WAIT_NONE)
669 cwait = msecs_to_jiffies(10 * new_serial.closing_wait);
671 port->port.closing_wait = cwait;
676 static int mxu1_ioctl(struct tty_struct *tty,
677 unsigned int cmd, unsigned long arg)
679 struct usb_serial_port *port = tty->driver_data;
683 return mxu1_get_serial_info(port,
684 (struct serial_struct __user *)arg);
686 return mxu1_set_serial_info(port,
687 (struct serial_struct __user *)arg);
693 static int mxu1_tiocmget(struct tty_struct *tty)
695 struct usb_serial_port *port = tty->driver_data;
696 struct mxu1_port *mxport = usb_get_serial_port_data(port);
702 mutex_lock(&mxport->mutex);
703 spin_lock_irqsave(&mxport->spinlock, flags);
708 spin_unlock_irqrestore(&mxport->spinlock, flags);
709 mutex_unlock(&mxport->mutex);
711 result = ((mcr & MXU1_MCR_DTR) ? TIOCM_DTR : 0) |
712 ((mcr & MXU1_MCR_RTS) ? TIOCM_RTS : 0) |
713 ((mcr & MXU1_MCR_LOOP) ? TIOCM_LOOP : 0) |
714 ((msr & MXU1_MSR_CTS) ? TIOCM_CTS : 0) |
715 ((msr & MXU1_MSR_CD) ? TIOCM_CAR : 0) |
716 ((msr & MXU1_MSR_RI) ? TIOCM_RI : 0) |
717 ((msr & MXU1_MSR_DSR) ? TIOCM_DSR : 0);
719 dev_dbg(&port->dev, "%s - 0x%04X\n", __func__, result);
724 static int mxu1_tiocmset(struct tty_struct *tty,
725 unsigned int set, unsigned int clear)
727 struct usb_serial_port *port = tty->driver_data;
728 struct mxu1_port *mxport = usb_get_serial_port_data(port);
732 mutex_lock(&mxport->mutex);
739 if (set & TIOCM_LOOP)
740 mcr |= MXU1_MCR_LOOP;
742 if (clear & TIOCM_RTS)
743 mcr &= ~MXU1_MCR_RTS;
744 if (clear & TIOCM_DTR)
745 mcr &= ~MXU1_MCR_DTR;
746 if (clear & TIOCM_LOOP)
747 mcr &= ~MXU1_MCR_LOOP;
749 err = mxu1_set_mcr(port, mcr);
753 mutex_unlock(&mxport->mutex);
758 static void mxu1_break(struct tty_struct *tty, int break_state)
760 struct usb_serial_port *port = tty->driver_data;
761 struct mxu1_port *mxport = usb_get_serial_port_data(port);
763 if (break_state == -1)
764 mxport->send_break = true;
766 mxport->send_break = false;
768 mxu1_set_termios(tty, port, NULL);
771 static int mxu1_open(struct tty_struct *tty, struct usb_serial_port *port)
773 struct mxu1_port *mxport = usb_get_serial_port_data(port);
774 struct usb_serial *serial = port->serial;
778 open_settings = (MXU1_PIPE_MODE_CONTINUOUS |
779 MXU1_PIPE_TIMEOUT_ENABLE |
780 (MXU1_TRANSFER_TIMEOUT << 2));
784 status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
786 dev_err(&port->dev, "failed to submit interrupt urb: %d\n",
792 mxu1_set_termios(tty, port, NULL);
794 status = mxu1_send_ctrl_urb(serial, MXU1_OPEN_PORT,
795 open_settings, MXU1_UART1_PORT);
797 dev_err(&port->dev, "cannot send open command: %d\n", status);
801 status = mxu1_send_ctrl_urb(serial, MXU1_START_PORT,
804 dev_err(&port->dev, "cannot send start command: %d\n", status);
808 status = mxu1_send_ctrl_urb(serial, MXU1_PURGE_PORT,
809 MXU1_PURGE_INPUT, MXU1_UART1_PORT);
811 dev_err(&port->dev, "cannot clear input buffers: %d\n",
817 status = mxu1_send_ctrl_urb(serial, MXU1_PURGE_PORT,
818 MXU1_PURGE_OUTPUT, MXU1_UART1_PORT);
820 dev_err(&port->dev, "cannot clear output buffers: %d\n",
827 * reset the data toggle on the bulk endpoints to work around bug in
828 * host controllers where things get out of sync some times
830 usb_clear_halt(serial->dev, port->write_urb->pipe);
831 usb_clear_halt(serial->dev, port->read_urb->pipe);
834 mxu1_set_termios(tty, port, NULL);
836 status = mxu1_send_ctrl_urb(serial, MXU1_OPEN_PORT,
837 open_settings, MXU1_UART1_PORT);
839 dev_err(&port->dev, "cannot send open command: %d\n", status);
843 status = mxu1_send_ctrl_urb(serial, MXU1_START_PORT,
846 dev_err(&port->dev, "cannot send start command: %d\n", status);
850 status = usb_serial_generic_open(tty, port);
857 usb_kill_urb(port->interrupt_in_urb);
862 static void mxu1_close(struct usb_serial_port *port)
866 usb_serial_generic_close(port);
867 usb_kill_urb(port->interrupt_in_urb);
869 status = mxu1_send_ctrl_urb(port->serial, MXU1_CLOSE_PORT,
872 dev_err(&port->dev, "failed to send close port command: %d\n",
877 static void mxu1_handle_new_msr(struct usb_serial_port *port, u8 msr)
879 struct mxu1_port *mxport = usb_get_serial_port_data(port);
880 struct async_icount *icount;
883 dev_dbg(&port->dev, "%s - msr 0x%02X\n", __func__, msr);
885 spin_lock_irqsave(&mxport->spinlock, flags);
886 mxport->msr = msr & MXU1_MSR_MASK;
887 spin_unlock_irqrestore(&mxport->spinlock, flags);
889 if (msr & MXU1_MSR_DELTA_MASK) {
890 icount = &port->icount;
891 if (msr & MXU1_MSR_DELTA_CTS)
893 if (msr & MXU1_MSR_DELTA_DSR)
895 if (msr & MXU1_MSR_DELTA_CD)
897 if (msr & MXU1_MSR_DELTA_RI)
900 wake_up_interruptible(&port->port.delta_msr_wait);
904 static void mxu1_interrupt_callback(struct urb *urb)
906 struct usb_serial_port *port = urb->context;
907 unsigned char *data = urb->transfer_buffer;
908 int length = urb->actual_length;
913 switch (urb->status) {
919 dev_dbg(&port->dev, "%s - urb shutting down: %d\n",
920 __func__, urb->status);
923 dev_dbg(&port->dev, "%s - nonzero urb status: %d\n",
924 __func__, urb->status);
929 dev_dbg(&port->dev, "%s - bad packet size: %d\n",
934 if (data[0] == MXU1_CODE_HARDWARE_ERROR) {
935 dev_err(&port->dev, "hardware error: %d\n", data[1]);
939 function = mxu1_get_func_from_code(data[0]);
941 dev_dbg(&port->dev, "%s - function %d, data 0x%02X\n",
942 __func__, function, data[1]);
945 case MXU1_CODE_DATA_ERROR:
946 dev_dbg(&port->dev, "%s - DATA ERROR, data 0x%02X\n",
950 case MXU1_CODE_MODEM_STATUS:
952 mxu1_handle_new_msr(port, msr);
956 dev_err(&port->dev, "unknown interrupt code: 0x%02X\n",
962 status = usb_submit_urb(urb, GFP_ATOMIC);
964 dev_err(&port->dev, "resubmit interrupt urb failed: %d\n",
969 static struct usb_serial_driver mxu11x0_device = {
971 .owner = THIS_MODULE,
974 .description = "MOXA UPort 11x0",
975 .id_table = mxu1_idtable,
977 .port_probe = mxu1_port_probe,
978 .port_remove = mxu1_port_remove,
979 .attach = mxu1_startup,
980 .release = mxu1_release,
984 .set_termios = mxu1_set_termios,
985 .tiocmget = mxu1_tiocmget,
986 .tiocmset = mxu1_tiocmset,
987 .tiocmiwait = usb_serial_generic_tiocmiwait,
988 .get_icount = usb_serial_generic_get_icount,
989 .break_ctl = mxu1_break,
990 .read_int_callback = mxu1_interrupt_callback,
993 static struct usb_serial_driver *const serial_drivers[] = {
994 &mxu11x0_device, NULL
997 module_usb_serial_driver(serial_drivers, mxu1_idtable);
999 MODULE_AUTHOR("Mathieu Othacehe <m.othacehe@gmail.com>");
1000 MODULE_DESCRIPTION("MOXA UPort 11x0 USB to Serial Hub Driver");
1001 MODULE_LICENSE("GPL");
1002 MODULE_FIRMWARE("moxa/moxa-1110.fw");
1003 MODULE_FIRMWARE("moxa/moxa-1130.fw");
1004 MODULE_FIRMWARE("moxa/moxa-1131.fw");
1005 MODULE_FIRMWARE("moxa/moxa-1150.fw");
1006 MODULE_FIRMWARE("moxa/moxa-1151.fw");