3 * USB Moxa UPORT 11x0 Serial Driver
5 * Copyright (C) 2007 MOXA Technologies Co., Ltd.
6 * Copyright (C) 2015 Mathieu Othacehe <m.othacehe@gmail.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 * Supports the following Moxa USB to serial converters:
15 * UPort 1110, 1 port RS-232 USB to Serial Hub.
16 * UPort 1130, 1 port RS-422/485 USB to Serial Hub.
17 * UPort 1130I, 1 port RS-422/485 USB to Serial Hub with isolation
19 * UPort 1150, 1 port RS-232/422/485 USB to Serial Hub.
20 * UPort 1150I, 1 port RS-232/422/485 USB to Serial Hub with isolation
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/firmware.h>
27 #include <linux/jiffies.h>
28 #include <linux/serial.h>
29 #include <linux/serial_reg.h>
30 #include <linux/slab.h>
31 #include <linux/spinlock.h>
32 #include <linux/mutex.h>
33 #include <linux/tty.h>
34 #include <linux/tty_driver.h>
35 #include <linux/tty_flip.h>
36 #include <linux/uaccess.h>
37 #include <linux/usb.h>
38 #include <linux/usb/serial.h>
40 /* Vendor and product ids */
41 #define MXU1_VENDOR_ID 0x110a
42 #define MXU1_1110_PRODUCT_ID 0x1110
43 #define MXU1_1130_PRODUCT_ID 0x1130
44 #define MXU1_1150_PRODUCT_ID 0x1150
45 #define MXU1_1151_PRODUCT_ID 0x1151
46 #define MXU1_1131_PRODUCT_ID 0x1131
49 #define MXU1_GET_VERSION 0x01
50 #define MXU1_GET_PORT_STATUS 0x02
51 #define MXU1_GET_PORT_DEV_INFO 0x03
52 #define MXU1_GET_CONFIG 0x04
53 #define MXU1_SET_CONFIG 0x05
54 #define MXU1_OPEN_PORT 0x06
55 #define MXU1_CLOSE_PORT 0x07
56 #define MXU1_START_PORT 0x08
57 #define MXU1_STOP_PORT 0x09
58 #define MXU1_TEST_PORT 0x0A
59 #define MXU1_PURGE_PORT 0x0B
60 #define MXU1_RESET_EXT_DEVICE 0x0C
61 #define MXU1_GET_OUTQUEUE 0x0D
62 #define MXU1_WRITE_DATA 0x80
63 #define MXU1_READ_DATA 0x81
64 #define MXU1_REQ_TYPE_CLASS 0x82
66 /* Module identifiers */
67 #define MXU1_I2C_PORT 0x01
68 #define MXU1_IEEE1284_PORT 0x02
69 #define MXU1_UART1_PORT 0x03
70 #define MXU1_UART2_PORT 0x04
71 #define MXU1_RAM_PORT 0x05
74 #define MXU1_MSR_DELTA_CTS 0x01
75 #define MXU1_MSR_DELTA_DSR 0x02
76 #define MXU1_MSR_DELTA_RI 0x04
77 #define MXU1_MSR_DELTA_CD 0x08
78 #define MXU1_MSR_CTS 0x10
79 #define MXU1_MSR_DSR 0x20
80 #define MXU1_MSR_RI 0x40
81 #define MXU1_MSR_CD 0x80
82 #define MXU1_MSR_DELTA_MASK 0x0F
83 #define MXU1_MSR_MASK 0xF0
86 #define MXU1_LSR_OVERRUN_ERROR 0x01
87 #define MXU1_LSR_PARITY_ERROR 0x02
88 #define MXU1_LSR_FRAMING_ERROR 0x04
89 #define MXU1_LSR_BREAK 0x08
90 #define MXU1_LSR_ERROR 0x0F
91 #define MXU1_LSR_RX_FULL 0x10
92 #define MXU1_LSR_TX_EMPTY 0x20
95 #define MXU1_MCR_LOOP 0x04
96 #define MXU1_MCR_DTR 0x10
97 #define MXU1_MCR_RTS 0x20
100 #define MXU1_UART_ENABLE_RTS_IN 0x0001
101 #define MXU1_UART_DISABLE_RTS 0x0002
102 #define MXU1_UART_ENABLE_PARITY_CHECKING 0x0008
103 #define MXU1_UART_ENABLE_DSR_OUT 0x0010
104 #define MXU1_UART_ENABLE_CTS_OUT 0x0020
105 #define MXU1_UART_ENABLE_X_OUT 0x0040
106 #define MXU1_UART_ENABLE_XA_OUT 0x0080
107 #define MXU1_UART_ENABLE_X_IN 0x0100
108 #define MXU1_UART_ENABLE_DTR_IN 0x0800
109 #define MXU1_UART_DISABLE_DTR 0x1000
110 #define MXU1_UART_ENABLE_MS_INTS 0x2000
111 #define MXU1_UART_ENABLE_AUTO_START_DMA 0x4000
112 #define MXU1_UART_SEND_BREAK_SIGNAL 0x8000
115 #define MXU1_UART_NO_PARITY 0x00
116 #define MXU1_UART_ODD_PARITY 0x01
117 #define MXU1_UART_EVEN_PARITY 0x02
118 #define MXU1_UART_MARK_PARITY 0x03
119 #define MXU1_UART_SPACE_PARITY 0x04
122 #define MXU1_UART_1_STOP_BITS 0x00
123 #define MXU1_UART_1_5_STOP_BITS 0x01
124 #define MXU1_UART_2_STOP_BITS 0x02
126 /* Bits per character */
127 #define MXU1_UART_5_DATA_BITS 0x00
128 #define MXU1_UART_6_DATA_BITS 0x01
129 #define MXU1_UART_7_DATA_BITS 0x02
130 #define MXU1_UART_8_DATA_BITS 0x03
132 /* Operation modes */
133 #define MXU1_UART_232 0x00
134 #define MXU1_UART_485_RECEIVER_DISABLED 0x01
135 #define MXU1_UART_485_RECEIVER_ENABLED 0x02
137 /* Pipe transfer mode and timeout */
138 #define MXU1_PIPE_MODE_CONTINUOUS 0x01
139 #define MXU1_PIPE_MODE_MASK 0x03
140 #define MXU1_PIPE_TIMEOUT_MASK 0x7C
141 #define MXU1_PIPE_TIMEOUT_ENABLE 0x80
144 struct mxu1_uart_config
{
156 #define MXU1_PURGE_OUTPUT 0x00
157 #define MXU1_PURGE_INPUT 0x80
159 /* Read/Write data */
160 #define MXU1_RW_DATA_ADDR_SFR 0x10
161 #define MXU1_RW_DATA_ADDR_IDATA 0x20
162 #define MXU1_RW_DATA_ADDR_XDATA 0x30
163 #define MXU1_RW_DATA_ADDR_CODE 0x40
164 #define MXU1_RW_DATA_ADDR_GPIO 0x50
165 #define MXU1_RW_DATA_ADDR_I2C 0x60
166 #define MXU1_RW_DATA_ADDR_FLASH 0x70
167 #define MXU1_RW_DATA_ADDR_DSP 0x80
169 #define MXU1_RW_DATA_UNSPECIFIED 0x00
170 #define MXU1_RW_DATA_BYTE 0x01
171 #define MXU1_RW_DATA_WORD 0x02
172 #define MXU1_RW_DATA_DOUBLE_WORD 0x04
174 struct mxu1_write_data_bytes
{
183 /* Interrupt codes */
184 #define MXU1_CODE_HARDWARE_ERROR 0xFF
185 #define MXU1_CODE_DATA_ERROR 0x03
186 #define MXU1_CODE_MODEM_STATUS 0x04
188 static inline int mxu1_get_func_from_code(unsigned char code
)
193 /* Download firmware max packet size */
194 #define MXU1_DOWNLOAD_MAX_PACKET_SIZE 64
196 /* Firmware image header */
197 struct mxu1_firmware_header
{
202 #define MXU1_UART_BASE_ADDR 0xFFA0
203 #define MXU1_UART_OFFSET_MCR 0x0004
205 #define MXU1_BAUD_BASE 923077
207 #define MXU1_TRANSFER_TIMEOUT 2
208 #define MXU1_DOWNLOAD_TIMEOUT 1000
209 #define MXU1_DEFAULT_CLOSING_WAIT 4000 /* in .01 secs */
215 spinlock_t spinlock
; /* Protects msr */
216 struct mutex mutex
; /* Protects mcr */
224 static const struct usb_device_id mxu1_idtable
[] = {
225 { USB_DEVICE(MXU1_VENDOR_ID
, MXU1_1110_PRODUCT_ID
) },
226 { USB_DEVICE(MXU1_VENDOR_ID
, MXU1_1130_PRODUCT_ID
) },
227 { USB_DEVICE(MXU1_VENDOR_ID
, MXU1_1150_PRODUCT_ID
) },
228 { USB_DEVICE(MXU1_VENDOR_ID
, MXU1_1151_PRODUCT_ID
) },
229 { USB_DEVICE(MXU1_VENDOR_ID
, MXU1_1131_PRODUCT_ID
) },
233 MODULE_DEVICE_TABLE(usb
, mxu1_idtable
);
235 /* Write the given buffer out to the control pipe. */
236 static int mxu1_send_ctrl_data_urb(struct usb_serial
*serial
,
238 u16 value
, u16 index
,
239 void *data
, size_t size
)
243 status
= usb_control_msg(serial
->dev
,
244 usb_sndctrlpipe(serial
->dev
, 0),
246 (USB_DIR_OUT
| USB_TYPE_VENDOR
|
247 USB_RECIP_DEVICE
), value
, index
,
249 USB_CTRL_SET_TIMEOUT
);
251 dev_err(&serial
->interface
->dev
,
252 "%s - usb_control_msg failed: %d\n",
257 if (status
!= size
) {
258 dev_err(&serial
->interface
->dev
,
259 "%s - short write (%d / %zd)\n",
260 __func__
, status
, size
);
267 /* Send a vendor request without any data */
268 static int mxu1_send_ctrl_urb(struct usb_serial
*serial
,
269 u8 request
, u16 value
, u16 index
)
271 return mxu1_send_ctrl_data_urb(serial
, request
, value
, index
,
275 static int mxu1_download_firmware(struct usb_serial
*serial
,
276 const struct firmware
*fw_p
)
285 struct usb_device
*dev
= serial
->dev
;
286 struct mxu1_firmware_header
*header
;
289 pipe
= usb_sndbulkpipe(dev
, serial
->port
[0]->bulk_out_endpointAddress
);
291 buffer_size
= fw_p
->size
+ sizeof(*header
);
292 buffer
= kmalloc(buffer_size
, GFP_KERNEL
);
296 memcpy(buffer
, fw_p
->data
, fw_p
->size
);
297 memset(buffer
+ fw_p
->size
, 0xff, buffer_size
- fw_p
->size
);
299 for (pos
= sizeof(*header
); pos
< buffer_size
; pos
++)
300 cs
= (u8
)(cs
+ buffer
[pos
]);
302 header
= (struct mxu1_firmware_header
*)buffer
;
303 header
->wLength
= cpu_to_le16(buffer_size
- sizeof(*header
));
304 header
->bCheckSum
= cs
;
306 dev_dbg(&dev
->dev
, "%s - downloading firmware\n", __func__
);
308 for (pos
= 0; pos
< buffer_size
; pos
+= done
) {
309 len
= min(buffer_size
- pos
, MXU1_DOWNLOAD_MAX_PACKET_SIZE
);
311 status
= usb_bulk_msg(dev
, pipe
, buffer
+ pos
, len
, &done
,
312 MXU1_DOWNLOAD_TIMEOUT
);
320 dev_err(&dev
->dev
, "failed to download firmware: %d\n", status
);
324 msleep_interruptible(100);
325 usb_reset_device(dev
);
327 dev_dbg(&dev
->dev
, "%s - download successful\n", __func__
);
332 static int mxu1_port_probe(struct usb_serial_port
*port
)
334 struct mxu1_port
*mxport
;
335 struct mxu1_device
*mxdev
;
337 if (!port
->interrupt_in_urb
) {
338 dev_err(&port
->dev
, "no interrupt urb\n");
342 mxport
= kzalloc(sizeof(struct mxu1_port
), GFP_KERNEL
);
346 spin_lock_init(&mxport
->spinlock
);
347 mutex_init(&mxport
->mutex
);
349 mxdev
= usb_get_serial_data(port
->serial
);
351 switch (mxdev
->mxd_model
) {
352 case MXU1_1110_PRODUCT_ID
:
353 case MXU1_1150_PRODUCT_ID
:
354 case MXU1_1151_PRODUCT_ID
:
355 mxport
->uart_mode
= MXU1_UART_232
;
357 case MXU1_1130_PRODUCT_ID
:
358 case MXU1_1131_PRODUCT_ID
:
359 mxport
->uart_mode
= MXU1_UART_485_RECEIVER_DISABLED
;
363 usb_set_serial_port_data(port
, mxport
);
365 port
->port
.closing_wait
=
366 msecs_to_jiffies(MXU1_DEFAULT_CLOSING_WAIT
* 10);
367 port
->port
.drain_delay
= 1;
372 static int mxu1_startup(struct usb_serial
*serial
)
374 struct mxu1_device
*mxdev
;
375 struct usb_device
*dev
= serial
->dev
;
376 struct usb_host_interface
*cur_altsetting
;
378 const struct firmware
*fw_p
= NULL
;
381 dev_dbg(&serial
->interface
->dev
, "%s - product 0x%04X, num configurations %d, configuration value %d\n",
382 __func__
, le16_to_cpu(dev
->descriptor
.idProduct
),
383 dev
->descriptor
.bNumConfigurations
,
384 dev
->actconfig
->desc
.bConfigurationValue
);
386 /* create device structure */
387 mxdev
= kzalloc(sizeof(struct mxu1_device
), GFP_KERNEL
);
391 usb_set_serial_data(serial
, mxdev
);
393 mxdev
->mxd_model
= le16_to_cpu(dev
->descriptor
.idProduct
);
395 cur_altsetting
= serial
->interface
->cur_altsetting
;
397 /* if we have only 1 configuration, download firmware */
398 if (cur_altsetting
->desc
.bNumEndpoints
== 1) {
405 err
= request_firmware(&fw_p
, fw_name
, &serial
->interface
->dev
);
407 dev_err(&serial
->interface
->dev
, "failed to request firmware: %d\n",
412 err
= mxu1_download_firmware(serial
, fw_p
);
414 goto err_release_firmware
;
416 /* device is being reset */
418 goto err_release_firmware
;
423 err_release_firmware
:
424 release_firmware(fw_p
);
431 static int mxu1_write_byte(struct usb_serial_port
*port
, u32 addr
,
436 struct mxu1_write_data_bytes
*data
;
438 dev_dbg(&port
->dev
, "%s - addr 0x%08X, mask 0x%02X, byte 0x%02X\n",
439 __func__
, addr
, mask
, byte
);
441 size
= sizeof(struct mxu1_write_data_bytes
) + 2;
442 data
= kzalloc(size
, GFP_KERNEL
);
446 data
->bAddrType
= MXU1_RW_DATA_ADDR_XDATA
;
447 data
->bDataType
= MXU1_RW_DATA_BYTE
;
448 data
->bDataCounter
= 1;
449 data
->wBaseAddrHi
= cpu_to_be16(addr
>> 16);
450 data
->wBaseAddrLo
= cpu_to_be16(addr
);
451 data
->bData
[0] = mask
;
452 data
->bData
[1] = byte
;
454 status
= mxu1_send_ctrl_data_urb(port
->serial
, MXU1_WRITE_DATA
, 0,
455 MXU1_RAM_PORT
, data
, size
);
457 dev_err(&port
->dev
, "%s - failed: %d\n", __func__
, status
);
464 static int mxu1_set_mcr(struct usb_serial_port
*port
, unsigned int mcr
)
468 status
= mxu1_write_byte(port
,
469 MXU1_UART_BASE_ADDR
+ MXU1_UART_OFFSET_MCR
,
470 MXU1_MCR_RTS
| MXU1_MCR_DTR
| MXU1_MCR_LOOP
,
475 static void mxu1_set_termios(struct tty_struct
*tty
,
476 struct usb_serial_port
*port
,
477 struct ktermios
*old_termios
)
479 struct mxu1_port
*mxport
= usb_get_serial_port_data(port
);
480 struct mxu1_uart_config
*config
;
481 tcflag_t cflag
, iflag
;
486 cflag
= tty
->termios
.c_cflag
;
487 iflag
= tty
->termios
.c_iflag
;
490 !tty_termios_hw_change(&tty
->termios
, old_termios
) &&
491 tty
->termios
.c_iflag
== old_termios
->c_iflag
) {
492 dev_dbg(&port
->dev
, "%s - nothing to change\n", __func__
);
497 "%s - clfag %08x, iflag %08x\n", __func__
, cflag
, iflag
);
500 dev_dbg(&port
->dev
, "%s - old clfag %08x, old iflag %08x\n",
502 old_termios
->c_cflag
,
503 old_termios
->c_iflag
);
506 config
= kzalloc(sizeof(*config
), GFP_KERNEL
);
510 /* these flags must be set */
511 config
->wFlags
|= MXU1_UART_ENABLE_MS_INTS
;
512 config
->wFlags
|= MXU1_UART_ENABLE_AUTO_START_DMA
;
513 if (mxport
->send_break
)
514 config
->wFlags
|= MXU1_UART_SEND_BREAK_SIGNAL
;
515 config
->bUartMode
= mxport
->uart_mode
;
517 switch (C_CSIZE(tty
)) {
519 config
->bDataBits
= MXU1_UART_5_DATA_BITS
;
522 config
->bDataBits
= MXU1_UART_6_DATA_BITS
;
525 config
->bDataBits
= MXU1_UART_7_DATA_BITS
;
529 config
->bDataBits
= MXU1_UART_8_DATA_BITS
;
534 config
->wFlags
|= MXU1_UART_ENABLE_PARITY_CHECKING
;
537 config
->bParity
= MXU1_UART_MARK_PARITY
;
539 config
->bParity
= MXU1_UART_SPACE_PARITY
;
542 config
->bParity
= MXU1_UART_ODD_PARITY
;
544 config
->bParity
= MXU1_UART_EVEN_PARITY
;
547 config
->bParity
= MXU1_UART_NO_PARITY
;
551 config
->bStopBits
= MXU1_UART_2_STOP_BITS
;
553 config
->bStopBits
= MXU1_UART_1_STOP_BITS
;
555 if (C_CRTSCTS(tty
)) {
556 /* RTS flow control must be off to drop RTS for baud rate B0 */
557 if (C_BAUD(tty
) != B0
)
558 config
->wFlags
|= MXU1_UART_ENABLE_RTS_IN
;
559 config
->wFlags
|= MXU1_UART_ENABLE_CTS_OUT
;
562 if (I_IXOFF(tty
) || I_IXON(tty
)) {
563 config
->cXon
= START_CHAR(tty
);
564 config
->cXoff
= STOP_CHAR(tty
);
567 config
->wFlags
|= MXU1_UART_ENABLE_X_IN
;
570 config
->wFlags
|= MXU1_UART_ENABLE_X_OUT
;
573 baud
= tty_get_baud_rate(tty
);
576 config
->wBaudRate
= MXU1_BAUD_BASE
/ baud
;
578 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",
579 __func__
, baud
, config
->wBaudRate
, config
->wFlags
,
580 config
->bDataBits
, config
->bParity
, config
->bStopBits
,
581 config
->cXon
, config
->cXoff
, config
->bUartMode
);
583 cpu_to_be16s(&config
->wBaudRate
);
584 cpu_to_be16s(&config
->wFlags
);
586 status
= mxu1_send_ctrl_data_urb(port
->serial
, MXU1_SET_CONFIG
, 0,
587 MXU1_UART1_PORT
, config
,
590 dev_err(&port
->dev
, "cannot set config: %d\n", status
);
592 mutex_lock(&mxport
->mutex
);
595 if (C_BAUD(tty
) == B0
)
596 mcr
&= ~(MXU1_MCR_DTR
| MXU1_MCR_RTS
);
597 else if (old_termios
&& (old_termios
->c_cflag
& CBAUD
) == B0
)
598 mcr
|= MXU1_MCR_DTR
| MXU1_MCR_RTS
;
600 status
= mxu1_set_mcr(port
, mcr
);
602 dev_err(&port
->dev
, "cannot set modem control: %d\n", status
);
606 mutex_unlock(&mxport
->mutex
);
611 static int mxu1_get_serial_info(struct usb_serial_port
*port
,
612 struct serial_struct __user
*ret_arg
)
614 struct serial_struct ret_serial
;
620 cwait
= port
->port
.closing_wait
;
621 if (cwait
!= ASYNC_CLOSING_WAIT_NONE
)
622 cwait
= jiffies_to_msecs(cwait
) / 10;
624 memset(&ret_serial
, 0, sizeof(ret_serial
));
626 ret_serial
.type
= PORT_16550A
;
627 ret_serial
.line
= port
->minor
;
629 ret_serial
.xmit_fifo_size
= port
->bulk_out_size
;
630 ret_serial
.baud_base
= MXU1_BAUD_BASE
;
631 ret_serial
.close_delay
= 5*HZ
;
632 ret_serial
.closing_wait
= cwait
;
634 if (copy_to_user(ret_arg
, &ret_serial
, sizeof(*ret_arg
)))
641 static int mxu1_set_serial_info(struct usb_serial_port
*port
,
642 struct serial_struct __user
*new_arg
)
644 struct serial_struct new_serial
;
647 if (copy_from_user(&new_serial
, new_arg
, sizeof(new_serial
)))
650 cwait
= new_serial
.closing_wait
;
651 if (cwait
!= ASYNC_CLOSING_WAIT_NONE
)
652 cwait
= msecs_to_jiffies(10 * new_serial
.closing_wait
);
654 port
->port
.closing_wait
= cwait
;
659 static int mxu1_ioctl(struct tty_struct
*tty
,
660 unsigned int cmd
, unsigned long arg
)
662 struct usb_serial_port
*port
= tty
->driver_data
;
666 return mxu1_get_serial_info(port
,
667 (struct serial_struct __user
*)arg
);
669 return mxu1_set_serial_info(port
,
670 (struct serial_struct __user
*)arg
);
676 static int mxu1_tiocmget(struct tty_struct
*tty
)
678 struct usb_serial_port
*port
= tty
->driver_data
;
679 struct mxu1_port
*mxport
= usb_get_serial_port_data(port
);
685 mutex_lock(&mxport
->mutex
);
686 spin_lock_irqsave(&mxport
->spinlock
, flags
);
691 spin_unlock_irqrestore(&mxport
->spinlock
, flags
);
692 mutex_unlock(&mxport
->mutex
);
694 result
= ((mcr
& MXU1_MCR_DTR
) ? TIOCM_DTR
: 0) |
695 ((mcr
& MXU1_MCR_RTS
) ? TIOCM_RTS
: 0) |
696 ((mcr
& MXU1_MCR_LOOP
) ? TIOCM_LOOP
: 0) |
697 ((msr
& MXU1_MSR_CTS
) ? TIOCM_CTS
: 0) |
698 ((msr
& MXU1_MSR_CD
) ? TIOCM_CAR
: 0) |
699 ((msr
& MXU1_MSR_RI
) ? TIOCM_RI
: 0) |
700 ((msr
& MXU1_MSR_DSR
) ? TIOCM_DSR
: 0);
702 dev_dbg(&port
->dev
, "%s - 0x%04X\n", __func__
, result
);
707 static int mxu1_tiocmset(struct tty_struct
*tty
,
708 unsigned int set
, unsigned int clear
)
710 struct usb_serial_port
*port
= tty
->driver_data
;
711 struct mxu1_port
*mxport
= usb_get_serial_port_data(port
);
715 mutex_lock(&mxport
->mutex
);
722 if (set
& TIOCM_LOOP
)
723 mcr
|= MXU1_MCR_LOOP
;
725 if (clear
& TIOCM_RTS
)
726 mcr
&= ~MXU1_MCR_RTS
;
727 if (clear
& TIOCM_DTR
)
728 mcr
&= ~MXU1_MCR_DTR
;
729 if (clear
& TIOCM_LOOP
)
730 mcr
&= ~MXU1_MCR_LOOP
;
732 err
= mxu1_set_mcr(port
, mcr
);
736 mutex_unlock(&mxport
->mutex
);
741 static void mxu1_break(struct tty_struct
*tty
, int break_state
)
743 struct usb_serial_port
*port
= tty
->driver_data
;
744 struct mxu1_port
*mxport
= usb_get_serial_port_data(port
);
746 if (break_state
== -1)
747 mxport
->send_break
= true;
749 mxport
->send_break
= false;
751 mxu1_set_termios(tty
, port
, NULL
);
754 static int mxu1_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
756 struct mxu1_port
*mxport
= usb_get_serial_port_data(port
);
757 struct usb_serial
*serial
= port
->serial
;
761 open_settings
= (MXU1_PIPE_MODE_CONTINUOUS
|
762 MXU1_PIPE_TIMEOUT_ENABLE
|
763 (MXU1_TRANSFER_TIMEOUT
<< 2));
767 dev_dbg(&port
->dev
, "%s - start interrupt in urb\n", __func__
);
768 status
= usb_submit_urb(port
->interrupt_in_urb
, GFP_KERNEL
);
770 dev_err(&port
->dev
, "failed to submit interrupt urb: %d\n",
776 mxu1_set_termios(tty
, port
, NULL
);
778 status
= mxu1_send_ctrl_urb(serial
, MXU1_OPEN_PORT
,
779 open_settings
, MXU1_UART1_PORT
);
781 dev_err(&port
->dev
, "%s - cannot send open command: %d\n",
786 status
= mxu1_send_ctrl_urb(serial
, MXU1_START_PORT
,
789 dev_err(&port
->dev
, "%s - cannot send start command: %d\n",
794 status
= mxu1_send_ctrl_urb(serial
, MXU1_PURGE_PORT
,
795 MXU1_PURGE_INPUT
, MXU1_UART1_PORT
);
797 dev_err(&port
->dev
, "%s - cannot clear input buffers: %d\n",
803 status
= mxu1_send_ctrl_urb(serial
, MXU1_PURGE_PORT
,
804 MXU1_PURGE_OUTPUT
, MXU1_UART1_PORT
);
806 dev_err(&port
->dev
, "%s - cannot clear output buffers: %d\n",
813 * reset the data toggle on the bulk endpoints to work around bug in
814 * host controllers where things get out of sync some times
816 usb_clear_halt(serial
->dev
, port
->write_urb
->pipe
);
817 usb_clear_halt(serial
->dev
, port
->read_urb
->pipe
);
820 mxu1_set_termios(tty
, port
, NULL
);
822 status
= mxu1_send_ctrl_urb(serial
, MXU1_OPEN_PORT
,
823 open_settings
, MXU1_UART1_PORT
);
825 dev_err(&port
->dev
, "%s - cannot send open command: %d\n",
830 status
= mxu1_send_ctrl_urb(serial
, MXU1_START_PORT
,
833 dev_err(&port
->dev
, "%s - cannot send start command: %d\n",
838 status
= usb_serial_generic_open(tty
, port
);
840 dev_err(&port
->dev
, "%s - submit read urb failed: %d\n",
848 usb_kill_urb(port
->interrupt_in_urb
);
853 static void mxu1_close(struct usb_serial_port
*port
)
857 usb_serial_generic_close(port
);
858 usb_kill_urb(port
->interrupt_in_urb
);
860 status
= mxu1_send_ctrl_urb(port
->serial
, MXU1_CLOSE_PORT
,
863 dev_err(&port
->dev
, "failed to send close port command: %d\n",
867 static void mxu1_handle_new_msr(struct usb_serial_port
*port
, u8 msr
)
869 struct async_icount
*icount
;
870 struct mxu1_port
*mxport
;
873 dev_dbg(&port
->dev
, "%s - msr 0x%02X\n", __func__
, msr
);
875 mxport
= usb_get_serial_port_data(port
);
877 spin_lock_irqsave(&mxport
->spinlock
, flags
);
878 mxport
->msr
= msr
& MXU1_MSR_MASK
;
879 spin_unlock_irqrestore(&mxport
->spinlock
, flags
);
881 if (msr
& MXU1_MSR_DELTA_MASK
) {
882 icount
= &port
->icount
;
883 if (msr
& MXU1_MSR_DELTA_CTS
)
885 if (msr
& MXU1_MSR_DELTA_DSR
)
887 if (msr
& MXU1_MSR_DELTA_CD
)
889 if (msr
& MXU1_MSR_DELTA_RI
)
892 wake_up_interruptible(&port
->port
.delta_msr_wait
);
896 static void mxu1_interrupt_callback(struct urb
*urb
)
898 struct usb_serial_port
*port
= urb
->context
;
899 unsigned char *data
= urb
->transfer_buffer
;
900 int length
= urb
->actual_length
;
905 switch (urb
->status
) {
911 dev_dbg(&port
->dev
, "%s - urb shutting down: %d\n",
912 __func__
, urb
->status
);
915 dev_dbg(&port
->dev
, "%s - nonzero urb status: %d\n",
916 __func__
, urb
->status
);
921 dev_dbg(&port
->dev
, "%s - bad packet size: %d\n",
926 if (data
[0] == MXU1_CODE_HARDWARE_ERROR
) {
927 dev_err(&port
->dev
, "%s - hardware error: %d\n",
932 function
= mxu1_get_func_from_code(data
[0]);
934 dev_dbg(&port
->dev
, "%s - function %d, data 0x%02X\n",
935 __func__
, function
, data
[1]);
938 case MXU1_CODE_DATA_ERROR
:
939 dev_dbg(&port
->dev
, "%s - DATA ERROR, data 0x%02X\n",
943 case MXU1_CODE_MODEM_STATUS
:
945 mxu1_handle_new_msr(port
, msr
);
949 dev_err(&port
->dev
, "%s - unknown interrupt code: 0x%02X\n",
955 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
957 dev_err(&port
->dev
, "resubmit interrupt urb failed: %d\n",
961 static struct usb_serial_driver mxu11x0_device
= {
963 .owner
= THIS_MODULE
,
966 .description
= "MOXA UPort 11x0",
967 .id_table
= mxu1_idtable
,
969 .port_probe
= mxu1_port_probe
,
970 .attach
= mxu1_startup
,
974 .set_termios
= mxu1_set_termios
,
975 .tiocmget
= mxu1_tiocmget
,
976 .tiocmset
= mxu1_tiocmset
,
977 .tiocmiwait
= usb_serial_generic_tiocmiwait
,
978 .get_icount
= usb_serial_generic_get_icount
,
979 .break_ctl
= mxu1_break
,
980 .read_int_callback
= mxu1_interrupt_callback
,
983 static struct usb_serial_driver
*const serial_drivers
[] = {
984 &mxu11x0_device
, NULL
987 module_usb_serial_driver(serial_drivers
, mxu1_idtable
);
989 MODULE_AUTHOR("Mathieu Othacehe <m.othacehe@gmail.com>");
990 MODULE_DESCRIPTION("MOXA UPort 11x0 USB to Serial Hub Driver");
991 MODULE_LICENSE("GPL");
992 MODULE_FIRMWARE("moxa/moxa-1110.fw");
993 MODULE_FIRMWARE("moxa/moxa-1130.fw");
994 MODULE_FIRMWARE("moxa/moxa-1131.fw");
995 MODULE_FIRMWARE("moxa/moxa-1150.fw");
996 MODULE_FIRMWARE("moxa/moxa-1151.fw");