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_startup(struct usb_serial
*serial
)
373 struct mxu1_device
*mxdev
;
374 struct usb_device
*dev
= serial
->dev
;
375 struct usb_host_interface
*cur_altsetting
;
377 const struct firmware
*fw_p
= NULL
;
380 dev_dbg(&serial
->interface
->dev
, "%s - product 0x%04X, num configurations %d, configuration value %d\n",
381 __func__
, le16_to_cpu(dev
->descriptor
.idProduct
),
382 dev
->descriptor
.bNumConfigurations
,
383 dev
->actconfig
->desc
.bConfigurationValue
);
385 /* create device structure */
386 mxdev
= kzalloc(sizeof(struct mxu1_device
), GFP_KERNEL
);
390 usb_set_serial_data(serial
, mxdev
);
392 mxdev
->mxd_model
= le16_to_cpu(dev
->descriptor
.idProduct
);
394 cur_altsetting
= serial
->interface
->cur_altsetting
;
396 /* if we have only 1 configuration, download firmware */
397 if (cur_altsetting
->desc
.bNumEndpoints
== 1) {
404 err
= request_firmware(&fw_p
, fw_name
, &serial
->interface
->dev
);
406 dev_err(&serial
->interface
->dev
, "failed to request firmware: %d\n",
411 err
= mxu1_download_firmware(serial
, fw_p
);
413 goto err_release_firmware
;
415 /* device is being reset */
417 goto err_release_firmware
;
422 err_release_firmware
:
423 release_firmware(fw_p
);
430 static int mxu1_write_byte(struct usb_serial_port
*port
, u32 addr
,
435 struct mxu1_write_data_bytes
*data
;
437 dev_dbg(&port
->dev
, "%s - addr 0x%08X, mask 0x%02X, byte 0x%02X\n",
438 __func__
, addr
, mask
, byte
);
440 size
= sizeof(struct mxu1_write_data_bytes
) + 2;
441 data
= kzalloc(size
, GFP_KERNEL
);
445 data
->bAddrType
= MXU1_RW_DATA_ADDR_XDATA
;
446 data
->bDataType
= MXU1_RW_DATA_BYTE
;
447 data
->bDataCounter
= 1;
448 data
->wBaseAddrHi
= cpu_to_be16(addr
>> 16);
449 data
->wBaseAddrLo
= cpu_to_be16(addr
);
450 data
->bData
[0] = mask
;
451 data
->bData
[1] = byte
;
453 status
= mxu1_send_ctrl_data_urb(port
->serial
, MXU1_WRITE_DATA
, 0,
454 MXU1_RAM_PORT
, data
, size
);
456 dev_err(&port
->dev
, "%s - failed: %d\n", __func__
, status
);
463 static int mxu1_set_mcr(struct usb_serial_port
*port
, unsigned int mcr
)
467 status
= mxu1_write_byte(port
,
468 MXU1_UART_BASE_ADDR
+ MXU1_UART_OFFSET_MCR
,
469 MXU1_MCR_RTS
| MXU1_MCR_DTR
| MXU1_MCR_LOOP
,
474 static void mxu1_set_termios(struct tty_struct
*tty
,
475 struct usb_serial_port
*port
,
476 struct ktermios
*old_termios
)
478 struct mxu1_port
*mxport
= usb_get_serial_port_data(port
);
479 struct mxu1_uart_config
*config
;
480 tcflag_t cflag
, iflag
;
485 cflag
= tty
->termios
.c_cflag
;
486 iflag
= tty
->termios
.c_iflag
;
489 !tty_termios_hw_change(&tty
->termios
, old_termios
) &&
490 tty
->termios
.c_iflag
== old_termios
->c_iflag
) {
491 dev_dbg(&port
->dev
, "%s - nothing to change\n", __func__
);
496 "%s - cflag 0x%08x, iflag 0x%08x\n", __func__
, cflag
, iflag
);
499 dev_dbg(&port
->dev
, "%s - old cflag 0x%08x, old iflag 0x%08x\n",
501 old_termios
->c_cflag
,
502 old_termios
->c_iflag
);
505 config
= kzalloc(sizeof(*config
), GFP_KERNEL
);
509 /* these flags must be set */
510 config
->wFlags
|= MXU1_UART_ENABLE_MS_INTS
;
511 config
->wFlags
|= MXU1_UART_ENABLE_AUTO_START_DMA
;
512 if (mxport
->send_break
)
513 config
->wFlags
|= MXU1_UART_SEND_BREAK_SIGNAL
;
514 config
->bUartMode
= mxport
->uart_mode
;
516 switch (C_CSIZE(tty
)) {
518 config
->bDataBits
= MXU1_UART_5_DATA_BITS
;
521 config
->bDataBits
= MXU1_UART_6_DATA_BITS
;
524 config
->bDataBits
= MXU1_UART_7_DATA_BITS
;
528 config
->bDataBits
= MXU1_UART_8_DATA_BITS
;
533 config
->wFlags
|= MXU1_UART_ENABLE_PARITY_CHECKING
;
536 config
->bParity
= MXU1_UART_MARK_PARITY
;
538 config
->bParity
= MXU1_UART_SPACE_PARITY
;
541 config
->bParity
= MXU1_UART_ODD_PARITY
;
543 config
->bParity
= MXU1_UART_EVEN_PARITY
;
546 config
->bParity
= MXU1_UART_NO_PARITY
;
550 config
->bStopBits
= MXU1_UART_2_STOP_BITS
;
552 config
->bStopBits
= MXU1_UART_1_STOP_BITS
;
554 if (C_CRTSCTS(tty
)) {
555 /* RTS flow control must be off to drop RTS for baud rate B0 */
556 if (C_BAUD(tty
) != B0
)
557 config
->wFlags
|= MXU1_UART_ENABLE_RTS_IN
;
558 config
->wFlags
|= MXU1_UART_ENABLE_CTS_OUT
;
561 if (I_IXOFF(tty
) || I_IXON(tty
)) {
562 config
->cXon
= START_CHAR(tty
);
563 config
->cXoff
= STOP_CHAR(tty
);
566 config
->wFlags
|= MXU1_UART_ENABLE_X_IN
;
569 config
->wFlags
|= MXU1_UART_ENABLE_X_OUT
;
572 baud
= tty_get_baud_rate(tty
);
575 config
->wBaudRate
= MXU1_BAUD_BASE
/ baud
;
577 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",
578 __func__
, baud
, config
->wBaudRate
, config
->wFlags
,
579 config
->bDataBits
, config
->bParity
, config
->bStopBits
,
580 config
->cXon
, config
->cXoff
, config
->bUartMode
);
582 cpu_to_be16s(&config
->wBaudRate
);
583 cpu_to_be16s(&config
->wFlags
);
585 status
= mxu1_send_ctrl_data_urb(port
->serial
, MXU1_SET_CONFIG
, 0,
586 MXU1_UART1_PORT
, config
,
589 dev_err(&port
->dev
, "cannot set config: %d\n", status
);
591 mutex_lock(&mxport
->mutex
);
594 if (C_BAUD(tty
) == B0
)
595 mcr
&= ~(MXU1_MCR_DTR
| MXU1_MCR_RTS
);
596 else if (old_termios
&& (old_termios
->c_cflag
& CBAUD
) == B0
)
597 mcr
|= MXU1_MCR_DTR
| MXU1_MCR_RTS
;
599 status
= mxu1_set_mcr(port
, mcr
);
601 dev_err(&port
->dev
, "cannot set modem control: %d\n", status
);
605 mutex_unlock(&mxport
->mutex
);
610 static int mxu1_get_serial_info(struct usb_serial_port
*port
,
611 struct serial_struct __user
*ret_arg
)
613 struct serial_struct ret_serial
;
619 cwait
= port
->port
.closing_wait
;
620 if (cwait
!= ASYNC_CLOSING_WAIT_NONE
)
621 cwait
= jiffies_to_msecs(cwait
) / 10;
623 memset(&ret_serial
, 0, sizeof(ret_serial
));
625 ret_serial
.type
= PORT_16550A
;
626 ret_serial
.line
= port
->minor
;
628 ret_serial
.xmit_fifo_size
= port
->bulk_out_size
;
629 ret_serial
.baud_base
= MXU1_BAUD_BASE
;
630 ret_serial
.close_delay
= 5*HZ
;
631 ret_serial
.closing_wait
= cwait
;
633 if (copy_to_user(ret_arg
, &ret_serial
, sizeof(*ret_arg
)))
640 static int mxu1_set_serial_info(struct usb_serial_port
*port
,
641 struct serial_struct __user
*new_arg
)
643 struct serial_struct new_serial
;
646 if (copy_from_user(&new_serial
, new_arg
, sizeof(new_serial
)))
649 cwait
= new_serial
.closing_wait
;
650 if (cwait
!= ASYNC_CLOSING_WAIT_NONE
)
651 cwait
= msecs_to_jiffies(10 * new_serial
.closing_wait
);
653 port
->port
.closing_wait
= cwait
;
658 static int mxu1_ioctl(struct tty_struct
*tty
,
659 unsigned int cmd
, unsigned long arg
)
661 struct usb_serial_port
*port
= tty
->driver_data
;
665 return mxu1_get_serial_info(port
,
666 (struct serial_struct __user
*)arg
);
668 return mxu1_set_serial_info(port
,
669 (struct serial_struct __user
*)arg
);
675 static int mxu1_tiocmget(struct tty_struct
*tty
)
677 struct usb_serial_port
*port
= tty
->driver_data
;
678 struct mxu1_port
*mxport
= usb_get_serial_port_data(port
);
684 mutex_lock(&mxport
->mutex
);
685 spin_lock_irqsave(&mxport
->spinlock
, flags
);
690 spin_unlock_irqrestore(&mxport
->spinlock
, flags
);
691 mutex_unlock(&mxport
->mutex
);
693 result
= ((mcr
& MXU1_MCR_DTR
) ? TIOCM_DTR
: 0) |
694 ((mcr
& MXU1_MCR_RTS
) ? TIOCM_RTS
: 0) |
695 ((mcr
& MXU1_MCR_LOOP
) ? TIOCM_LOOP
: 0) |
696 ((msr
& MXU1_MSR_CTS
) ? TIOCM_CTS
: 0) |
697 ((msr
& MXU1_MSR_CD
) ? TIOCM_CAR
: 0) |
698 ((msr
& MXU1_MSR_RI
) ? TIOCM_RI
: 0) |
699 ((msr
& MXU1_MSR_DSR
) ? TIOCM_DSR
: 0);
701 dev_dbg(&port
->dev
, "%s - 0x%04X\n", __func__
, result
);
706 static int mxu1_tiocmset(struct tty_struct
*tty
,
707 unsigned int set
, unsigned int clear
)
709 struct usb_serial_port
*port
= tty
->driver_data
;
710 struct mxu1_port
*mxport
= usb_get_serial_port_data(port
);
714 mutex_lock(&mxport
->mutex
);
721 if (set
& TIOCM_LOOP
)
722 mcr
|= MXU1_MCR_LOOP
;
724 if (clear
& TIOCM_RTS
)
725 mcr
&= ~MXU1_MCR_RTS
;
726 if (clear
& TIOCM_DTR
)
727 mcr
&= ~MXU1_MCR_DTR
;
728 if (clear
& TIOCM_LOOP
)
729 mcr
&= ~MXU1_MCR_LOOP
;
731 err
= mxu1_set_mcr(port
, mcr
);
735 mutex_unlock(&mxport
->mutex
);
740 static void mxu1_break(struct tty_struct
*tty
, int break_state
)
742 struct usb_serial_port
*port
= tty
->driver_data
;
743 struct mxu1_port
*mxport
= usb_get_serial_port_data(port
);
745 if (break_state
== -1)
746 mxport
->send_break
= true;
748 mxport
->send_break
= false;
750 mxu1_set_termios(tty
, port
, NULL
);
753 static int mxu1_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
755 struct mxu1_port
*mxport
= usb_get_serial_port_data(port
);
756 struct usb_serial
*serial
= port
->serial
;
760 open_settings
= (MXU1_PIPE_MODE_CONTINUOUS
|
761 MXU1_PIPE_TIMEOUT_ENABLE
|
762 (MXU1_TRANSFER_TIMEOUT
<< 2));
766 status
= usb_submit_urb(port
->interrupt_in_urb
, GFP_KERNEL
);
768 dev_err(&port
->dev
, "failed to submit interrupt urb: %d\n",
774 mxu1_set_termios(tty
, port
, NULL
);
776 status
= mxu1_send_ctrl_urb(serial
, MXU1_OPEN_PORT
,
777 open_settings
, MXU1_UART1_PORT
);
779 dev_err(&port
->dev
, "cannot send open command: %d\n", status
);
783 status
= mxu1_send_ctrl_urb(serial
, MXU1_START_PORT
,
786 dev_err(&port
->dev
, "cannot send start command: %d\n", status
);
790 status
= mxu1_send_ctrl_urb(serial
, MXU1_PURGE_PORT
,
791 MXU1_PURGE_INPUT
, MXU1_UART1_PORT
);
793 dev_err(&port
->dev
, "cannot clear input buffers: %d\n",
799 status
= mxu1_send_ctrl_urb(serial
, MXU1_PURGE_PORT
,
800 MXU1_PURGE_OUTPUT
, MXU1_UART1_PORT
);
802 dev_err(&port
->dev
, "cannot clear output buffers: %d\n",
809 * reset the data toggle on the bulk endpoints to work around bug in
810 * host controllers where things get out of sync some times
812 usb_clear_halt(serial
->dev
, port
->write_urb
->pipe
);
813 usb_clear_halt(serial
->dev
, port
->read_urb
->pipe
);
816 mxu1_set_termios(tty
, port
, NULL
);
818 status
= mxu1_send_ctrl_urb(serial
, MXU1_OPEN_PORT
,
819 open_settings
, MXU1_UART1_PORT
);
821 dev_err(&port
->dev
, "cannot send open command: %d\n", status
);
825 status
= mxu1_send_ctrl_urb(serial
, MXU1_START_PORT
,
828 dev_err(&port
->dev
, "cannot send start command: %d\n", status
);
832 status
= usb_serial_generic_open(tty
, port
);
839 usb_kill_urb(port
->interrupt_in_urb
);
844 static void mxu1_close(struct usb_serial_port
*port
)
848 usb_serial_generic_close(port
);
849 usb_kill_urb(port
->interrupt_in_urb
);
851 status
= mxu1_send_ctrl_urb(port
->serial
, MXU1_CLOSE_PORT
,
854 dev_err(&port
->dev
, "failed to send close port command: %d\n",
859 static void mxu1_handle_new_msr(struct usb_serial_port
*port
, u8 msr
)
861 struct mxu1_port
*mxport
= usb_get_serial_port_data(port
);
862 struct async_icount
*icount
;
865 dev_dbg(&port
->dev
, "%s - msr 0x%02X\n", __func__
, msr
);
867 spin_lock_irqsave(&mxport
->spinlock
, flags
);
868 mxport
->msr
= msr
& MXU1_MSR_MASK
;
869 spin_unlock_irqrestore(&mxport
->spinlock
, flags
);
871 if (msr
& MXU1_MSR_DELTA_MASK
) {
872 icount
= &port
->icount
;
873 if (msr
& MXU1_MSR_DELTA_CTS
)
875 if (msr
& MXU1_MSR_DELTA_DSR
)
877 if (msr
& MXU1_MSR_DELTA_CD
)
879 if (msr
& MXU1_MSR_DELTA_RI
)
882 wake_up_interruptible(&port
->port
.delta_msr_wait
);
886 static void mxu1_interrupt_callback(struct urb
*urb
)
888 struct usb_serial_port
*port
= urb
->context
;
889 unsigned char *data
= urb
->transfer_buffer
;
890 int length
= urb
->actual_length
;
895 switch (urb
->status
) {
901 dev_dbg(&port
->dev
, "%s - urb shutting down: %d\n",
902 __func__
, urb
->status
);
905 dev_dbg(&port
->dev
, "%s - nonzero urb status: %d\n",
906 __func__
, urb
->status
);
911 dev_dbg(&port
->dev
, "%s - bad packet size: %d\n",
916 if (data
[0] == MXU1_CODE_HARDWARE_ERROR
) {
917 dev_err(&port
->dev
, "hardware error: %d\n", data
[1]);
921 function
= mxu1_get_func_from_code(data
[0]);
923 dev_dbg(&port
->dev
, "%s - function %d, data 0x%02X\n",
924 __func__
, function
, data
[1]);
927 case MXU1_CODE_DATA_ERROR
:
928 dev_dbg(&port
->dev
, "%s - DATA ERROR, data 0x%02X\n",
932 case MXU1_CODE_MODEM_STATUS
:
934 mxu1_handle_new_msr(port
, msr
);
938 dev_err(&port
->dev
, "unknown interrupt code: 0x%02X\n",
944 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
946 dev_err(&port
->dev
, "resubmit interrupt urb failed: %d\n",
951 static struct usb_serial_driver mxu11x0_device
= {
953 .owner
= THIS_MODULE
,
956 .description
= "MOXA UPort 11x0",
957 .id_table
= mxu1_idtable
,
959 .port_probe
= mxu1_port_probe
,
960 .attach
= mxu1_startup
,
964 .set_termios
= mxu1_set_termios
,
965 .tiocmget
= mxu1_tiocmget
,
966 .tiocmset
= mxu1_tiocmset
,
967 .tiocmiwait
= usb_serial_generic_tiocmiwait
,
968 .get_icount
= usb_serial_generic_get_icount
,
969 .break_ctl
= mxu1_break
,
970 .read_int_callback
= mxu1_interrupt_callback
,
973 static struct usb_serial_driver
*const serial_drivers
[] = {
974 &mxu11x0_device
, NULL
977 module_usb_serial_driver(serial_drivers
, mxu1_idtable
);
979 MODULE_AUTHOR("Mathieu Othacehe <m.othacehe@gmail.com>");
980 MODULE_DESCRIPTION("MOXA UPort 11x0 USB to Serial Hub Driver");
981 MODULE_LICENSE("GPL");
982 MODULE_FIRMWARE("moxa/moxa-1110.fw");
983 MODULE_FIRMWARE("moxa/moxa-1130.fw");
984 MODULE_FIRMWARE("moxa/moxa-1131.fw");
985 MODULE_FIRMWARE("moxa/moxa-1150.fw");
986 MODULE_FIRMWARE("moxa/moxa-1151.fw");