1 // SPDX-License-Identifier: GPL-2.0+
3 * mxuport.c - MOXA UPort series driver
5 * Copyright (c) 2006 Moxa Technologies Co., Ltd.
6 * Copyright (c) 2013 Andrew Lunn <andrew@lunn.ch>
8 * Supports the following Moxa USB to serial converters:
9 * 2 ports : UPort 1250, UPort 1250I
10 * 4 ports : UPort 1410, UPort 1450, UPort 1450I
11 * 8 ports : UPort 1610-8, UPort 1650-8
12 * 16 ports : UPort 1610-16, UPort 1650-16
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/firmware.h>
18 #include <linux/jiffies.h>
19 #include <linux/serial.h>
20 #include <linux/serial_reg.h>
21 #include <linux/slab.h>
22 #include <linux/tty.h>
23 #include <linux/tty_driver.h>
24 #include <linux/tty_flip.h>
25 #include <linux/uaccess.h>
26 #include <linux/usb.h>
27 #include <linux/usb/serial.h>
28 #include <asm/unaligned.h>
30 /* Definitions for the vendor ID and device ID */
31 #define MX_USBSERIAL_VID 0x110A
32 #define MX_UPORT1250_PID 0x1250
33 #define MX_UPORT1251_PID 0x1251
34 #define MX_UPORT1410_PID 0x1410
35 #define MX_UPORT1450_PID 0x1450
36 #define MX_UPORT1451_PID 0x1451
37 #define MX_UPORT1618_PID 0x1618
38 #define MX_UPORT1658_PID 0x1658
39 #define MX_UPORT1613_PID 0x1613
40 #define MX_UPORT1653_PID 0x1653
42 /* Definitions for USB info */
44 #define EVENT_LENGTH 8
45 #define DOWN_BLOCK_SIZE 64
47 /* Definitions for firmware info */
48 #define VER_ADDR_1 0x20
49 #define VER_ADDR_2 0x24
50 #define VER_ADDR_3 0x28
52 /* Definitions for USB vendor request */
53 #define RQ_VENDOR_NONE 0x00
54 #define RQ_VENDOR_SET_BAUD 0x01 /* Set baud rate */
55 #define RQ_VENDOR_SET_LINE 0x02 /* Set line status */
56 #define RQ_VENDOR_SET_CHARS 0x03 /* Set Xon/Xoff chars */
57 #define RQ_VENDOR_SET_RTS 0x04 /* Set RTS */
58 #define RQ_VENDOR_SET_DTR 0x05 /* Set DTR */
59 #define RQ_VENDOR_SET_XONXOFF 0x06 /* Set auto Xon/Xoff */
60 #define RQ_VENDOR_SET_RX_HOST_EN 0x07 /* Set RX host enable */
61 #define RQ_VENDOR_SET_OPEN 0x08 /* Set open/close port */
62 #define RQ_VENDOR_PURGE 0x09 /* Purge Rx/Tx buffer */
63 #define RQ_VENDOR_SET_MCR 0x0A /* Set MCR register */
64 #define RQ_VENDOR_SET_BREAK 0x0B /* Set Break signal */
66 #define RQ_VENDOR_START_FW_DOWN 0x0C /* Start firmware download */
67 #define RQ_VENDOR_STOP_FW_DOWN 0x0D /* Stop firmware download */
68 #define RQ_VENDOR_QUERY_FW_READY 0x0E /* Query if new firmware ready */
70 #define RQ_VENDOR_SET_FIFO_DISABLE 0x0F /* Set fifo disable */
71 #define RQ_VENDOR_SET_INTERFACE 0x10 /* Set interface */
72 #define RQ_VENDOR_SET_HIGH_PERFOR 0x11 /* Set hi-performance */
74 #define RQ_VENDOR_ERASE_BLOCK 0x12 /* Erase flash block */
75 #define RQ_VENDOR_WRITE_PAGE 0x13 /* Write flash page */
76 #define RQ_VENDOR_PREPARE_WRITE 0x14 /* Prepare write flash */
77 #define RQ_VENDOR_CONFIRM_WRITE 0x15 /* Confirm write flash */
78 #define RQ_VENDOR_LOCATE 0x16 /* Locate the device */
80 #define RQ_VENDOR_START_ROM_DOWN 0x17 /* Start firmware download */
81 #define RQ_VENDOR_ROM_DATA 0x18 /* Rom file data */
82 #define RQ_VENDOR_STOP_ROM_DOWN 0x19 /* Stop firmware download */
83 #define RQ_VENDOR_FW_DATA 0x20 /* Firmware data */
85 #define RQ_VENDOR_RESET_DEVICE 0x23 /* Try to reset the device */
86 #define RQ_VENDOR_QUERY_FW_CONFIG 0x24
88 #define RQ_VENDOR_GET_VERSION 0x81 /* Get firmware version */
89 #define RQ_VENDOR_GET_PAGE 0x82 /* Read flash page */
90 #define RQ_VENDOR_GET_ROM_PROC 0x83 /* Get ROM process state */
92 #define RQ_VENDOR_GET_INQUEUE 0x84 /* Data in input buffer */
93 #define RQ_VENDOR_GET_OUTQUEUE 0x85 /* Data in output buffer */
95 #define RQ_VENDOR_GET_MSR 0x86 /* Get modem status register */
97 /* Definitions for UPort event type */
98 #define UPORT_EVENT_NONE 0 /* None */
99 #define UPORT_EVENT_TXBUF_THRESHOLD 1 /* Tx buffer threshold */
100 #define UPORT_EVENT_SEND_NEXT 2 /* Send next */
101 #define UPORT_EVENT_MSR 3 /* Modem status */
102 #define UPORT_EVENT_LSR 4 /* Line status */
103 #define UPORT_EVENT_MCR 5 /* Modem control */
105 /* Definitions for serial event type */
106 #define SERIAL_EV_CTS 0x0008 /* CTS changed state */
107 #define SERIAL_EV_DSR 0x0010 /* DSR changed state */
108 #define SERIAL_EV_RLSD 0x0020 /* RLSD changed state */
110 /* Definitions for modem control event type */
111 #define SERIAL_EV_XOFF 0x40 /* XOFF received */
113 /* Definitions for line control of communication */
114 #define MX_WORDLENGTH_5 5
115 #define MX_WORDLENGTH_6 6
116 #define MX_WORDLENGTH_7 7
117 #define MX_WORDLENGTH_8 8
119 #define MX_PARITY_NONE 0
120 #define MX_PARITY_ODD 1
121 #define MX_PARITY_EVEN 2
122 #define MX_PARITY_MARK 3
123 #define MX_PARITY_SPACE 4
125 #define MX_STOP_BITS_1 0
126 #define MX_STOP_BITS_1_5 1
127 #define MX_STOP_BITS_2 2
129 #define MX_RTS_DISABLE 0x0
130 #define MX_RTS_ENABLE 0x1
131 #define MX_RTS_HW 0x2
132 #define MX_RTS_NO_CHANGE 0x3 /* Flag, not valid register value*/
134 #define MX_INT_RS232 0
135 #define MX_INT_2W_RS485 1
136 #define MX_INT_RS422 2
137 #define MX_INT_4W_RS485 3
139 /* Definitions for holding reason */
140 #define MX_WAIT_FOR_CTS 0x0001
141 #define MX_WAIT_FOR_DSR 0x0002
142 #define MX_WAIT_FOR_DCD 0x0004
143 #define MX_WAIT_FOR_XON 0x0008
144 #define MX_WAIT_FOR_START_TX 0x0010
145 #define MX_WAIT_FOR_UNTHROTTLE 0x0020
146 #define MX_WAIT_FOR_LOW_WATER 0x0040
147 #define MX_WAIT_FOR_SEND_NEXT 0x0080
149 #define MX_UPORT_2_PORT BIT(0)
150 #define MX_UPORT_4_PORT BIT(1)
151 #define MX_UPORT_8_PORT BIT(2)
152 #define MX_UPORT_16_PORT BIT(3)
154 /* This structure holds all of the local port information */
155 struct mxuport_port
{
156 u8 mcr_state
; /* Last MCR state */
157 u8 msr_state
; /* Last MSR state */
158 struct mutex mutex
; /* Protects mcr_state */
159 spinlock_t spinlock
; /* Protects msr_state */
162 /* Table of devices that work with this driver */
163 static const struct usb_device_id mxuport_idtable
[] = {
164 { USB_DEVICE(MX_USBSERIAL_VID
, MX_UPORT1250_PID
),
165 .driver_info
= MX_UPORT_2_PORT
},
166 { USB_DEVICE(MX_USBSERIAL_VID
, MX_UPORT1251_PID
),
167 .driver_info
= MX_UPORT_2_PORT
},
168 { USB_DEVICE(MX_USBSERIAL_VID
, MX_UPORT1410_PID
),
169 .driver_info
= MX_UPORT_4_PORT
},
170 { USB_DEVICE(MX_USBSERIAL_VID
, MX_UPORT1450_PID
),
171 .driver_info
= MX_UPORT_4_PORT
},
172 { USB_DEVICE(MX_USBSERIAL_VID
, MX_UPORT1451_PID
),
173 .driver_info
= MX_UPORT_4_PORT
},
174 { USB_DEVICE(MX_USBSERIAL_VID
, MX_UPORT1618_PID
),
175 .driver_info
= MX_UPORT_8_PORT
},
176 { USB_DEVICE(MX_USBSERIAL_VID
, MX_UPORT1658_PID
),
177 .driver_info
= MX_UPORT_8_PORT
},
178 { USB_DEVICE(MX_USBSERIAL_VID
, MX_UPORT1613_PID
),
179 .driver_info
= MX_UPORT_16_PORT
},
180 { USB_DEVICE(MX_USBSERIAL_VID
, MX_UPORT1653_PID
),
181 .driver_info
= MX_UPORT_16_PORT
},
182 {} /* Terminating entry */
185 MODULE_DEVICE_TABLE(usb
, mxuport_idtable
);
188 * Add a four byte header containing the port number and the number of
189 * bytes of data in the message. Return the number of bytes in the
192 static int mxuport_prepare_write_buffer(struct usb_serial_port
*port
,
193 void *dest
, size_t size
)
198 count
= kfifo_out_locked(&port
->write_fifo
, buf
+ HEADER_SIZE
,
202 put_unaligned_be16(port
->port_number
, buf
);
203 put_unaligned_be16(count
, buf
+ 2);
205 dev_dbg(&port
->dev
, "%s - size %zd count %d\n", __func__
,
208 return count
+ HEADER_SIZE
;
211 /* Read the given buffer in from the control pipe. */
212 static int mxuport_recv_ctrl_urb(struct usb_serial
*serial
,
213 u8 request
, u16 value
, u16 index
,
214 u8
*data
, size_t size
)
218 status
= usb_control_msg(serial
->dev
,
219 usb_rcvctrlpipe(serial
->dev
, 0),
221 (USB_DIR_IN
| USB_TYPE_VENDOR
|
222 USB_RECIP_DEVICE
), value
, index
,
224 USB_CTRL_GET_TIMEOUT
);
226 dev_err(&serial
->interface
->dev
,
227 "%s - usb_control_msg failed (%d)\n",
232 if (status
!= size
) {
233 dev_err(&serial
->interface
->dev
,
234 "%s - short read (%d / %zd)\n",
235 __func__
, status
, size
);
242 /* Write the given buffer out to the control pipe. */
243 static int mxuport_send_ctrl_data_urb(struct usb_serial
*serial
,
245 u16 value
, u16 index
,
246 u8
*data
, size_t size
)
250 status
= usb_control_msg(serial
->dev
,
251 usb_sndctrlpipe(serial
->dev
, 0),
253 (USB_DIR_OUT
| USB_TYPE_VENDOR
|
254 USB_RECIP_DEVICE
), value
, index
,
256 USB_CTRL_SET_TIMEOUT
);
258 dev_err(&serial
->interface
->dev
,
259 "%s - usb_control_msg failed (%d)\n",
264 if (status
!= size
) {
265 dev_err(&serial
->interface
->dev
,
266 "%s - short write (%d / %zd)\n",
267 __func__
, status
, size
);
274 /* Send a vendor request without any data */
275 static int mxuport_send_ctrl_urb(struct usb_serial
*serial
,
276 u8 request
, u16 value
, u16 index
)
278 return mxuport_send_ctrl_data_urb(serial
, request
, value
, index
,
283 * mxuport_throttle - throttle function of driver
285 * This function is called by the tty driver when it wants to stop the
286 * data being read from the port. Since all the data comes over one
287 * bulk in endpoint, we cannot stop submitting urbs by setting
288 * port->throttle. Instead tell the device to stop sending us data for
291 static void mxuport_throttle(struct tty_struct
*tty
)
293 struct usb_serial_port
*port
= tty
->driver_data
;
294 struct usb_serial
*serial
= port
->serial
;
296 dev_dbg(&port
->dev
, "%s\n", __func__
);
298 mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_RX_HOST_EN
,
299 0, port
->port_number
);
303 * mxuport_unthrottle - unthrottle function of driver
305 * This function is called by the tty driver when it wants to resume
306 * the data being read from the port. Tell the device it can resume
307 * sending us received data from the port.
309 static void mxuport_unthrottle(struct tty_struct
*tty
)
312 struct usb_serial_port
*port
= tty
->driver_data
;
313 struct usb_serial
*serial
= port
->serial
;
315 dev_dbg(&port
->dev
, "%s\n", __func__
);
317 mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_RX_HOST_EN
,
318 1, port
->port_number
);
322 * Processes one chunk of data received for a port. Mostly a copy of
323 * usb_serial_generic_process_read_urb().
325 static void mxuport_process_read_urb_data(struct usb_serial_port
*port
,
326 char *data
, int size
)
331 for (i
= 0; i
< size
; i
++, data
++) {
332 if (!usb_serial_handle_sysrq_char(port
, *data
))
333 tty_insert_flip_char(&port
->port
, *data
,
337 tty_insert_flip_string(&port
->port
, data
, size
);
339 tty_flip_buffer_push(&port
->port
);
342 static void mxuport_msr_event(struct usb_serial_port
*port
, u8 buf
[4])
344 struct mxuport_port
*mxport
= usb_get_serial_port_data(port
);
345 u8 rcv_msr_hold
= buf
[2] & 0xF0;
346 u16 rcv_msr_event
= get_unaligned_be16(buf
);
349 if (rcv_msr_event
== 0)
352 /* Update MSR status */
353 spin_lock_irqsave(&mxport
->spinlock
, flags
);
355 dev_dbg(&port
->dev
, "%s - current MSR status = 0x%x\n",
356 __func__
, mxport
->msr_state
);
358 if (rcv_msr_hold
& UART_MSR_CTS
) {
359 mxport
->msr_state
|= UART_MSR_CTS
;
360 dev_dbg(&port
->dev
, "%s - CTS high\n", __func__
);
362 mxport
->msr_state
&= ~UART_MSR_CTS
;
363 dev_dbg(&port
->dev
, "%s - CTS low\n", __func__
);
366 if (rcv_msr_hold
& UART_MSR_DSR
) {
367 mxport
->msr_state
|= UART_MSR_DSR
;
368 dev_dbg(&port
->dev
, "%s - DSR high\n", __func__
);
370 mxport
->msr_state
&= ~UART_MSR_DSR
;
371 dev_dbg(&port
->dev
, "%s - DSR low\n", __func__
);
374 if (rcv_msr_hold
& UART_MSR_DCD
) {
375 mxport
->msr_state
|= UART_MSR_DCD
;
376 dev_dbg(&port
->dev
, "%s - DCD high\n", __func__
);
378 mxport
->msr_state
&= ~UART_MSR_DCD
;
379 dev_dbg(&port
->dev
, "%s - DCD low\n", __func__
);
381 spin_unlock_irqrestore(&mxport
->spinlock
, flags
);
384 (SERIAL_EV_CTS
| SERIAL_EV_DSR
| SERIAL_EV_RLSD
)) {
386 if (rcv_msr_event
& SERIAL_EV_CTS
) {
388 dev_dbg(&port
->dev
, "%s - CTS change\n", __func__
);
391 if (rcv_msr_event
& SERIAL_EV_DSR
) {
393 dev_dbg(&port
->dev
, "%s - DSR change\n", __func__
);
396 if (rcv_msr_event
& SERIAL_EV_RLSD
) {
398 dev_dbg(&port
->dev
, "%s - DCD change\n", __func__
);
400 wake_up_interruptible(&port
->port
.delta_msr_wait
);
404 static void mxuport_lsr_event(struct usb_serial_port
*port
, u8 buf
[4])
406 u8 lsr_event
= buf
[2];
408 if (lsr_event
& UART_LSR_BI
) {
410 dev_dbg(&port
->dev
, "%s - break error\n", __func__
);
413 if (lsr_event
& UART_LSR_FE
) {
414 port
->icount
.frame
++;
415 dev_dbg(&port
->dev
, "%s - frame error\n", __func__
);
418 if (lsr_event
& UART_LSR_PE
) {
419 port
->icount
.parity
++;
420 dev_dbg(&port
->dev
, "%s - parity error\n", __func__
);
423 if (lsr_event
& UART_LSR_OE
) {
424 port
->icount
.overrun
++;
425 dev_dbg(&port
->dev
, "%s - overrun error\n", __func__
);
430 * When something interesting happens, modem control lines XON/XOFF
431 * etc, the device sends an event. Process these events.
433 static void mxuport_process_read_urb_event(struct usb_serial_port
*port
,
434 u8 buf
[4], u32 event
)
436 dev_dbg(&port
->dev
, "%s - receive event : %04x\n", __func__
, event
);
439 case UPORT_EVENT_SEND_NEXT
:
441 * Sent as part of the flow control on device buffers.
442 * Not currently used.
445 case UPORT_EVENT_MSR
:
446 mxuport_msr_event(port
, buf
);
448 case UPORT_EVENT_LSR
:
449 mxuport_lsr_event(port
, buf
);
451 case UPORT_EVENT_MCR
:
453 * Event to indicate a change in XON/XOFF from the
454 * peer. Currently not used. We just continue
455 * sending the device data and it will buffer it if
456 * needed. This event could be used for flow control
457 * between the host and the device.
461 dev_dbg(&port
->dev
, "Unexpected event\n");
467 * One URB can contain data for multiple ports. Demultiplex the data,
468 * checking the port exists, is opened and the message is valid.
470 static void mxuport_process_read_urb_demux_data(struct urb
*urb
)
472 struct usb_serial_port
*port
= urb
->context
;
473 struct usb_serial
*serial
= port
->serial
;
474 u8
*data
= urb
->transfer_buffer
;
475 u8
*end
= data
+ urb
->actual_length
;
476 struct usb_serial_port
*demux_port
;
482 if (data
+ HEADER_SIZE
> end
) {
483 dev_warn(&port
->dev
, "%s - message with short header\n",
488 rcv_port
= get_unaligned_be16(data
);
489 if (rcv_port
>= serial
->num_ports
) {
490 dev_warn(&port
->dev
, "%s - message for invalid port\n",
495 demux_port
= serial
->port
[rcv_port
];
496 rcv_len
= get_unaligned_be16(data
+ 2);
497 if (!rcv_len
|| data
+ HEADER_SIZE
+ rcv_len
> end
) {
498 dev_warn(&port
->dev
, "%s - short data\n", __func__
);
502 if (tty_port_initialized(&demux_port
->port
)) {
503 ch
= data
+ HEADER_SIZE
;
504 mxuport_process_read_urb_data(demux_port
, ch
, rcv_len
);
506 dev_dbg(&demux_port
->dev
, "%s - data for closed port\n",
509 data
+= HEADER_SIZE
+ rcv_len
;
514 * One URB can contain events for multiple ports. Demultiplex the event,
515 * checking the port exists, and is opened.
517 static void mxuport_process_read_urb_demux_event(struct urb
*urb
)
519 struct usb_serial_port
*port
= urb
->context
;
520 struct usb_serial
*serial
= port
->serial
;
521 u8
*data
= urb
->transfer_buffer
;
522 u8
*end
= data
+ urb
->actual_length
;
523 struct usb_serial_port
*demux_port
;
529 if (data
+ EVENT_LENGTH
> end
) {
530 dev_warn(&port
->dev
, "%s - message with short event\n",
535 rcv_port
= get_unaligned_be16(data
);
536 if (rcv_port
>= serial
->num_ports
) {
537 dev_warn(&port
->dev
, "%s - message for invalid port\n",
542 demux_port
= serial
->port
[rcv_port
];
543 if (tty_port_initialized(&demux_port
->port
)) {
544 ch
= data
+ HEADER_SIZE
;
545 rcv_event
= get_unaligned_be16(data
+ 2);
546 mxuport_process_read_urb_event(demux_port
, ch
,
549 dev_dbg(&demux_port
->dev
,
550 "%s - event for closed port\n", __func__
);
552 data
+= EVENT_LENGTH
;
557 * This is called when we have received data on the bulk in
558 * endpoint. Depending on which port it was received on, it can
559 * contain serial data or events.
561 static void mxuport_process_read_urb(struct urb
*urb
)
563 struct usb_serial_port
*port
= urb
->context
;
564 struct usb_serial
*serial
= port
->serial
;
566 if (port
== serial
->port
[0])
567 mxuport_process_read_urb_demux_data(urb
);
569 if (port
== serial
->port
[1])
570 mxuport_process_read_urb_demux_event(urb
);
574 * Ask the device how many bytes it has queued to be sent out. If
575 * there are none, return true.
577 static bool mxuport_tx_empty(struct usb_serial_port
*port
)
579 struct usb_serial
*serial
= port
->serial
;
580 bool is_empty
= true;
585 len_buf
= kzalloc(4, GFP_KERNEL
);
589 err
= mxuport_recv_ctrl_urb(serial
, RQ_VENDOR_GET_OUTQUEUE
, 0,
590 port
->port_number
, len_buf
, 4);
594 txlen
= get_unaligned_be32(len_buf
);
595 dev_dbg(&port
->dev
, "%s - tx len = %u\n", __func__
, txlen
);
605 static int mxuport_set_mcr(struct usb_serial_port
*port
, u8 mcr_state
)
607 struct usb_serial
*serial
= port
->serial
;
610 dev_dbg(&port
->dev
, "%s - %02x\n", __func__
, mcr_state
);
612 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_MCR
,
613 mcr_state
, port
->port_number
);
615 dev_err(&port
->dev
, "%s - failed to change MCR\n", __func__
);
620 static int mxuport_set_dtr(struct usb_serial_port
*port
, int on
)
622 struct mxuport_port
*mxport
= usb_get_serial_port_data(port
);
623 struct usb_serial
*serial
= port
->serial
;
626 mutex_lock(&mxport
->mutex
);
628 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_DTR
,
629 !!on
, port
->port_number
);
632 mxport
->mcr_state
|= UART_MCR_DTR
;
634 mxport
->mcr_state
&= ~UART_MCR_DTR
;
637 mutex_unlock(&mxport
->mutex
);
642 static int mxuport_set_rts(struct usb_serial_port
*port
, u8 state
)
644 struct mxuport_port
*mxport
= usb_get_serial_port_data(port
);
645 struct usb_serial
*serial
= port
->serial
;
649 mutex_lock(&mxport
->mutex
);
650 mcr_state
= mxport
->mcr_state
;
654 mcr_state
&= ~UART_MCR_RTS
;
657 mcr_state
|= UART_MCR_RTS
;
661 * Do not update mxport->mcr_state when doing hardware
667 * Should not happen, but somebody might try passing
668 * MX_RTS_NO_CHANGE, which is not valid.
673 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_RTS
,
674 state
, port
->port_number
);
676 mxport
->mcr_state
= mcr_state
;
679 mutex_unlock(&mxport
->mutex
);
684 static void mxuport_dtr_rts(struct usb_serial_port
*port
, int on
)
686 struct mxuport_port
*mxport
= usb_get_serial_port_data(port
);
690 mutex_lock(&mxport
->mutex
);
691 mcr_state
= mxport
->mcr_state
;
694 mcr_state
|= (UART_MCR_RTS
| UART_MCR_DTR
);
696 mcr_state
&= ~(UART_MCR_RTS
| UART_MCR_DTR
);
698 err
= mxuport_set_mcr(port
, mcr_state
);
700 mxport
->mcr_state
= mcr_state
;
702 mutex_unlock(&mxport
->mutex
);
705 static int mxuport_tiocmset(struct tty_struct
*tty
, unsigned int set
,
708 struct usb_serial_port
*port
= tty
->driver_data
;
709 struct mxuport_port
*mxport
= usb_get_serial_port_data(port
);
713 mutex_lock(&mxport
->mutex
);
714 mcr_state
= mxport
->mcr_state
;
717 mcr_state
|= UART_MCR_RTS
;
720 mcr_state
|= UART_MCR_DTR
;
722 if (clear
& TIOCM_RTS
)
723 mcr_state
&= ~UART_MCR_RTS
;
725 if (clear
& TIOCM_DTR
)
726 mcr_state
&= ~UART_MCR_DTR
;
728 err
= mxuport_set_mcr(port
, mcr_state
);
730 mxport
->mcr_state
= mcr_state
;
732 mutex_unlock(&mxport
->mutex
);
737 static int mxuport_tiocmget(struct tty_struct
*tty
)
739 struct mxuport_port
*mxport
;
740 struct usb_serial_port
*port
= tty
->driver_data
;
746 mxport
= usb_get_serial_port_data(port
);
748 mutex_lock(&mxport
->mutex
);
749 spin_lock_irqsave(&mxport
->spinlock
, flags
);
751 msr
= mxport
->msr_state
;
752 mcr
= mxport
->mcr_state
;
754 spin_unlock_irqrestore(&mxport
->spinlock
, flags
);
755 mutex_unlock(&mxport
->mutex
);
757 result
= (((mcr
& UART_MCR_DTR
) ? TIOCM_DTR
: 0) | /* 0x002 */
758 ((mcr
& UART_MCR_RTS
) ? TIOCM_RTS
: 0) | /* 0x004 */
759 ((msr
& UART_MSR_CTS
) ? TIOCM_CTS
: 0) | /* 0x020 */
760 ((msr
& UART_MSR_DCD
) ? TIOCM_CAR
: 0) | /* 0x040 */
761 ((msr
& UART_MSR_RI
) ? TIOCM_RI
: 0) | /* 0x080 */
762 ((msr
& UART_MSR_DSR
) ? TIOCM_DSR
: 0)); /* 0x100 */
764 dev_dbg(&port
->dev
, "%s - 0x%04x\n", __func__
, result
);
769 static int mxuport_set_termios_flow(struct tty_struct
*tty
,
770 struct ktermios
*old_termios
,
771 struct usb_serial_port
*port
,
772 struct usb_serial
*serial
)
774 u8 xon
= START_CHAR(tty
);
775 u8 xoff
= STOP_CHAR(tty
);
781 buf
= kmalloc(2, GFP_KERNEL
);
785 /* S/W flow control settings */
786 if (I_IXOFF(tty
) || I_IXON(tty
)) {
791 err
= mxuport_send_ctrl_data_urb(serial
, RQ_VENDOR_SET_CHARS
,
792 0, port
->port_number
,
797 dev_dbg(&port
->dev
, "%s - XON = 0x%02x, XOFF = 0x%02x\n",
798 __func__
, xon
, xoff
);
803 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_XONXOFF
,
804 enable
, port
->port_number
);
808 rts
= MX_RTS_NO_CHANGE
;
810 /* H/W flow control settings */
812 C_CRTSCTS(tty
) != (old_termios
->c_cflag
& CRTSCTS
)) {
820 if (old_termios
&& (old_termios
->c_cflag
& CBAUD
) == B0
) {
821 /* Raise DTR and RTS */
826 mxuport_set_dtr(port
, 1);
829 /* Drop DTR and RTS */
830 rts
= MX_RTS_DISABLE
;
831 mxuport_set_dtr(port
, 0);
834 if (rts
!= MX_RTS_NO_CHANGE
)
835 err
= mxuport_set_rts(port
, rts
);
842 static void mxuport_set_termios(struct tty_struct
*tty
,
843 struct usb_serial_port
*port
,
844 struct ktermios
*old_termios
)
846 struct usb_serial
*serial
= port
->serial
;
855 !tty_termios_hw_change(&tty
->termios
, old_termios
) &&
856 tty
->termios
.c_iflag
== old_termios
->c_iflag
) {
857 dev_dbg(&port
->dev
, "%s - nothing to change\n", __func__
);
861 buf
= kmalloc(4, GFP_KERNEL
);
865 /* Set data bit of termios */
866 switch (C_CSIZE(tty
)) {
868 data_bits
= MX_WORDLENGTH_5
;
871 data_bits
= MX_WORDLENGTH_6
;
874 data_bits
= MX_WORDLENGTH_7
;
878 data_bits
= MX_WORDLENGTH_8
;
882 /* Set parity of termios */
886 parity
= MX_PARITY_MARK
;
888 parity
= MX_PARITY_SPACE
;
891 parity
= MX_PARITY_ODD
;
893 parity
= MX_PARITY_EVEN
;
896 parity
= MX_PARITY_NONE
;
899 /* Set stop bit of termios */
901 stop_bits
= MX_STOP_BITS_2
;
903 stop_bits
= MX_STOP_BITS_1
;
910 err
= mxuport_send_ctrl_data_urb(serial
, RQ_VENDOR_SET_LINE
,
911 0, port
->port_number
, buf
, 4);
915 err
= mxuport_set_termios_flow(tty
, old_termios
, port
, serial
);
919 baud
= tty_get_baud_rate(tty
);
923 /* Note: Little Endian */
924 put_unaligned_le32(baud
, buf
);
926 err
= mxuport_send_ctrl_data_urb(serial
, RQ_VENDOR_SET_BAUD
,
927 0, port
->port_number
,
932 dev_dbg(&port
->dev
, "baud_rate : %d\n", baud
);
933 dev_dbg(&port
->dev
, "data_bits : %d\n", data_bits
);
934 dev_dbg(&port
->dev
, "parity : %d\n", parity
);
935 dev_dbg(&port
->dev
, "stop_bits : %d\n", stop_bits
);
942 * Determine how many ports this device has dynamically. It will be
943 * called after the probe() callback is called, but before attach().
945 static int mxuport_calc_num_ports(struct usb_serial
*serial
,
946 struct usb_serial_endpoints
*epds
)
948 unsigned long features
= (unsigned long)usb_get_serial_data(serial
);
952 if (features
& MX_UPORT_2_PORT
) {
954 } else if (features
& MX_UPORT_4_PORT
) {
956 } else if (features
& MX_UPORT_8_PORT
) {
958 } else if (features
& MX_UPORT_16_PORT
) {
961 dev_warn(&serial
->interface
->dev
,
962 "unknown device, assuming two ports\n");
967 * Setup bulk-out endpoint multiplexing. All ports share the same
970 BUILD_BUG_ON(ARRAY_SIZE(epds
->bulk_out
) < 16);
972 for (i
= 1; i
< num_ports
; ++i
)
973 epds
->bulk_out
[i
] = epds
->bulk_out
[0];
975 epds
->num_bulk_out
= num_ports
;
980 /* Get the version of the firmware currently running. */
981 static int mxuport_get_fw_version(struct usb_serial
*serial
, u32
*version
)
986 ver_buf
= kzalloc(4, GFP_KERNEL
);
990 /* Get firmware version from SDRAM */
991 err
= mxuport_recv_ctrl_urb(serial
, RQ_VENDOR_GET_VERSION
, 0, 0,
998 *version
= (ver_buf
[0] << 16) | (ver_buf
[1] << 8) | ver_buf
[2];
1005 /* Given a firmware blob, download it to the device. */
1006 static int mxuport_download_fw(struct usb_serial
*serial
,
1007 const struct firmware
*fw_p
)
1014 fw_buf
= kmalloc(DOWN_BLOCK_SIZE
, GFP_KERNEL
);
1018 dev_dbg(&serial
->interface
->dev
, "Starting firmware download...\n");
1019 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_START_FW_DOWN
, 0, 0);
1025 txlen
= min_t(size_t, (fw_p
->size
- fwidx
), DOWN_BLOCK_SIZE
);
1027 memcpy(fw_buf
, &fw_p
->data
[fwidx
], txlen
);
1028 err
= mxuport_send_ctrl_data_urb(serial
, RQ_VENDOR_FW_DATA
,
1029 0, 0, fw_buf
, txlen
);
1031 mxuport_send_ctrl_urb(serial
, RQ_VENDOR_STOP_FW_DOWN
,
1037 usleep_range(1000, 2000);
1039 } while (fwidx
< fw_p
->size
);
1042 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_STOP_FW_DOWN
, 0, 0);
1047 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_QUERY_FW_READY
, 0, 0);
1054 static int mxuport_probe(struct usb_serial
*serial
,
1055 const struct usb_device_id
*id
)
1057 u16 productid
= le16_to_cpu(serial
->dev
->descriptor
.idProduct
);
1058 const struct firmware
*fw_p
= NULL
;
1064 /* Load our firmware */
1065 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_QUERY_FW_CONFIG
, 0, 0);
1067 mxuport_send_ctrl_urb(serial
, RQ_VENDOR_RESET_DEVICE
, 0, 0);
1071 err
= mxuport_get_fw_version(serial
, &version
);
1075 dev_dbg(&serial
->interface
->dev
, "Device firmware version v%x.%x.%x\n",
1076 (version
& 0xff0000) >> 16,
1077 (version
& 0xff00) >> 8,
1080 snprintf(buf
, sizeof(buf
) - 1, "moxa/moxa-%04x.fw", productid
);
1082 err
= request_firmware(&fw_p
, buf
, &serial
->interface
->dev
);
1084 dev_warn(&serial
->interface
->dev
, "Firmware %s not found\n",
1087 /* Use the firmware already in the device */
1090 local_ver
= ((fw_p
->data
[VER_ADDR_1
] << 16) |
1091 (fw_p
->data
[VER_ADDR_2
] << 8) |
1092 fw_p
->data
[VER_ADDR_3
]);
1093 dev_dbg(&serial
->interface
->dev
,
1094 "Available firmware version v%x.%x.%x\n",
1095 fw_p
->data
[VER_ADDR_1
], fw_p
->data
[VER_ADDR_2
],
1096 fw_p
->data
[VER_ADDR_3
]);
1097 if (local_ver
> version
) {
1098 err
= mxuport_download_fw(serial
, fw_p
);
1101 err
= mxuport_get_fw_version(serial
, &version
);
1107 dev_info(&serial
->interface
->dev
,
1108 "Using device firmware version v%x.%x.%x\n",
1109 (version
& 0xff0000) >> 16,
1110 (version
& 0xff00) >> 8,
1114 * Contains the features of this hardware. Store away for
1115 * later use, eg, number of ports.
1117 usb_set_serial_data(serial
, (void *)id
->driver_info
);
1120 release_firmware(fw_p
);
1125 static int mxuport_port_probe(struct usb_serial_port
*port
)
1127 struct usb_serial
*serial
= port
->serial
;
1128 struct mxuport_port
*mxport
;
1131 mxport
= devm_kzalloc(&port
->dev
, sizeof(struct mxuport_port
),
1136 mutex_init(&mxport
->mutex
);
1137 spin_lock_init(&mxport
->spinlock
);
1139 /* Set the port private data */
1140 usb_set_serial_port_data(port
, mxport
);
1142 /* Set FIFO (Enable) */
1143 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_FIFO_DISABLE
,
1144 0, port
->port_number
);
1148 /* Set transmission mode (Hi-Performance) */
1149 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_HIGH_PERFOR
,
1150 0, port
->port_number
);
1154 /* Set interface (RS-232) */
1155 return mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_INTERFACE
,
1160 static int mxuport_attach(struct usb_serial
*serial
)
1162 struct usb_serial_port
*port0
= serial
->port
[0];
1163 struct usb_serial_port
*port1
= serial
->port
[1];
1167 * All data from the ports is received on the first bulk in
1168 * endpoint, with a multiplex header. The second bulk in is
1171 * Start to read from the device.
1173 err
= usb_serial_generic_submit_read_urbs(port0
, GFP_KERNEL
);
1177 err
= usb_serial_generic_submit_read_urbs(port1
, GFP_KERNEL
);
1179 usb_serial_generic_close(port0
);
1186 static void mxuport_release(struct usb_serial
*serial
)
1188 struct usb_serial_port
*port0
= serial
->port
[0];
1189 struct usb_serial_port
*port1
= serial
->port
[1];
1191 usb_serial_generic_close(port1
);
1192 usb_serial_generic_close(port0
);
1195 static int mxuport_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
1197 struct mxuport_port
*mxport
= usb_get_serial_port_data(port
);
1198 struct usb_serial
*serial
= port
->serial
;
1201 /* Set receive host (enable) */
1202 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_RX_HOST_EN
,
1203 1, port
->port_number
);
1207 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_OPEN
,
1208 1, port
->port_number
);
1210 mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_RX_HOST_EN
,
1211 0, port
->port_number
);
1215 /* Initial port termios */
1217 mxuport_set_termios(tty
, port
, NULL
);
1220 * TODO: use RQ_VENDOR_GET_MSR, once we know what it
1223 mxport
->msr_state
= 0;
1228 static void mxuport_close(struct usb_serial_port
*port
)
1230 struct usb_serial
*serial
= port
->serial
;
1232 mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_OPEN
, 0,
1235 mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_RX_HOST_EN
, 0,
1239 /* Send a break to the port. */
1240 static void mxuport_break_ctl(struct tty_struct
*tty
, int break_state
)
1242 struct usb_serial_port
*port
= tty
->driver_data
;
1243 struct usb_serial
*serial
= port
->serial
;
1246 if (break_state
== -1) {
1248 dev_dbg(&port
->dev
, "%s - sending break\n", __func__
);
1251 dev_dbg(&port
->dev
, "%s - clearing break\n", __func__
);
1254 mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_BREAK
,
1255 enable
, port
->port_number
);
1258 static int mxuport_resume(struct usb_serial
*serial
)
1260 struct usb_serial_port
*port
;
1265 for (i
= 0; i
< 2; i
++) {
1266 port
= serial
->port
[i
];
1268 r
= usb_serial_generic_submit_read_urbs(port
, GFP_NOIO
);
1273 for (i
= 0; i
< serial
->num_ports
; i
++) {
1274 port
= serial
->port
[i
];
1275 if (!tty_port_initialized(&port
->port
))
1278 r
= usb_serial_generic_write_start(port
, GFP_NOIO
);
1283 return c
? -EIO
: 0;
1286 static struct usb_serial_driver mxuport_device
= {
1288 .owner
= THIS_MODULE
,
1291 .description
= "MOXA UPort",
1292 .id_table
= mxuport_idtable
,
1295 .probe
= mxuport_probe
,
1296 .port_probe
= mxuport_port_probe
,
1297 .attach
= mxuport_attach
,
1298 .release
= mxuport_release
,
1299 .calc_num_ports
= mxuport_calc_num_ports
,
1300 .open
= mxuport_open
,
1301 .close
= mxuport_close
,
1302 .set_termios
= mxuport_set_termios
,
1303 .break_ctl
= mxuport_break_ctl
,
1304 .tx_empty
= mxuport_tx_empty
,
1305 .tiocmiwait
= usb_serial_generic_tiocmiwait
,
1306 .get_icount
= usb_serial_generic_get_icount
,
1307 .throttle
= mxuport_throttle
,
1308 .unthrottle
= mxuport_unthrottle
,
1309 .tiocmget
= mxuport_tiocmget
,
1310 .tiocmset
= mxuport_tiocmset
,
1311 .dtr_rts
= mxuport_dtr_rts
,
1312 .process_read_urb
= mxuport_process_read_urb
,
1313 .prepare_write_buffer
= mxuport_prepare_write_buffer
,
1314 .resume
= mxuport_resume
,
1317 static struct usb_serial_driver
*const serial_drivers
[] = {
1318 &mxuport_device
, NULL
1321 module_usb_serial_driver(serial_drivers
, mxuport_idtable
);
1323 MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
1324 MODULE_AUTHOR("<support@moxa.com>");
1325 MODULE_LICENSE("GPL");