2 * mxuport.c - MOXA UPort series driver
4 * Copyright (c) 2006 Moxa Technologies Co., Ltd.
5 * Copyright (c) 2013 Andrew Lunn <andrew@lunn.ch>
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.
12 * Supports the following Moxa USB to serial converters:
13 * 2 ports : UPort 1250, UPort 1250I
14 * 4 ports : UPort 1410, UPort 1450, UPort 1450I
15 * 8 ports : UPort 1610-8, UPort 1650-8
16 * 16 ports : UPort 1610-16, UPort 1650-16
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/firmware.h>
22 #include <linux/jiffies.h>
23 #include <linux/serial.h>
24 #include <linux/serial_reg.h>
25 #include <linux/slab.h>
26 #include <linux/tty.h>
27 #include <linux/tty_driver.h>
28 #include <linux/tty_flip.h>
29 #include <linux/uaccess.h>
30 #include <linux/usb.h>
31 #include <linux/usb/serial.h>
32 #include <asm/unaligned.h>
34 /* Definitions for the vendor ID and device ID */
35 #define MX_USBSERIAL_VID 0x110A
36 #define MX_UPORT1250_PID 0x1250
37 #define MX_UPORT1251_PID 0x1251
38 #define MX_UPORT1410_PID 0x1410
39 #define MX_UPORT1450_PID 0x1450
40 #define MX_UPORT1451_PID 0x1451
41 #define MX_UPORT1618_PID 0x1618
42 #define MX_UPORT1658_PID 0x1658
43 #define MX_UPORT1613_PID 0x1613
44 #define MX_UPORT1653_PID 0x1653
46 /* Definitions for USB info */
48 #define EVENT_LENGTH 8
49 #define DOWN_BLOCK_SIZE 64
51 /* Definitions for firmware info */
52 #define VER_ADDR_1 0x20
53 #define VER_ADDR_2 0x24
54 #define VER_ADDR_3 0x28
56 /* Definitions for USB vendor request */
57 #define RQ_VENDOR_NONE 0x00
58 #define RQ_VENDOR_SET_BAUD 0x01 /* Set baud rate */
59 #define RQ_VENDOR_SET_LINE 0x02 /* Set line status */
60 #define RQ_VENDOR_SET_CHARS 0x03 /* Set Xon/Xoff chars */
61 #define RQ_VENDOR_SET_RTS 0x04 /* Set RTS */
62 #define RQ_VENDOR_SET_DTR 0x05 /* Set DTR */
63 #define RQ_VENDOR_SET_XONXOFF 0x06 /* Set auto Xon/Xoff */
64 #define RQ_VENDOR_SET_RX_HOST_EN 0x07 /* Set RX host enable */
65 #define RQ_VENDOR_SET_OPEN 0x08 /* Set open/close port */
66 #define RQ_VENDOR_PURGE 0x09 /* Purge Rx/Tx buffer */
67 #define RQ_VENDOR_SET_MCR 0x0A /* Set MCR register */
68 #define RQ_VENDOR_SET_BREAK 0x0B /* Set Break signal */
70 #define RQ_VENDOR_START_FW_DOWN 0x0C /* Start firmware download */
71 #define RQ_VENDOR_STOP_FW_DOWN 0x0D /* Stop firmware download */
72 #define RQ_VENDOR_QUERY_FW_READY 0x0E /* Query if new firmware ready */
74 #define RQ_VENDOR_SET_FIFO_DISABLE 0x0F /* Set fifo disable */
75 #define RQ_VENDOR_SET_INTERFACE 0x10 /* Set interface */
76 #define RQ_VENDOR_SET_HIGH_PERFOR 0x11 /* Set hi-performance */
78 #define RQ_VENDOR_ERASE_BLOCK 0x12 /* Erase flash block */
79 #define RQ_VENDOR_WRITE_PAGE 0x13 /* Write flash page */
80 #define RQ_VENDOR_PREPARE_WRITE 0x14 /* Prepare write flash */
81 #define RQ_VENDOR_CONFIRM_WRITE 0x15 /* Confirm write flash */
82 #define RQ_VENDOR_LOCATE 0x16 /* Locate the device */
84 #define RQ_VENDOR_START_ROM_DOWN 0x17 /* Start firmware download */
85 #define RQ_VENDOR_ROM_DATA 0x18 /* Rom file data */
86 #define RQ_VENDOR_STOP_ROM_DOWN 0x19 /* Stop firmware download */
87 #define RQ_VENDOR_FW_DATA 0x20 /* Firmware data */
89 #define RQ_VENDOR_RESET_DEVICE 0x23 /* Try to reset the device */
90 #define RQ_VENDOR_QUERY_FW_CONFIG 0x24
92 #define RQ_VENDOR_GET_VERSION 0x81 /* Get firmware version */
93 #define RQ_VENDOR_GET_PAGE 0x82 /* Read flash page */
94 #define RQ_VENDOR_GET_ROM_PROC 0x83 /* Get ROM process state */
96 #define RQ_VENDOR_GET_INQUEUE 0x84 /* Data in input buffer */
97 #define RQ_VENDOR_GET_OUTQUEUE 0x85 /* Data in output buffer */
99 #define RQ_VENDOR_GET_MSR 0x86 /* Get modem status register */
101 /* Definitions for UPort event type */
102 #define UPORT_EVENT_NONE 0 /* None */
103 #define UPORT_EVENT_TXBUF_THRESHOLD 1 /* Tx buffer threshold */
104 #define UPORT_EVENT_SEND_NEXT 2 /* Send next */
105 #define UPORT_EVENT_MSR 3 /* Modem status */
106 #define UPORT_EVENT_LSR 4 /* Line status */
107 #define UPORT_EVENT_MCR 5 /* Modem control */
109 /* Definitions for serial event type */
110 #define SERIAL_EV_CTS 0x0008 /* CTS changed state */
111 #define SERIAL_EV_DSR 0x0010 /* DSR changed state */
112 #define SERIAL_EV_RLSD 0x0020 /* RLSD changed state */
114 /* Definitions for modem control event type */
115 #define SERIAL_EV_XOFF 0x40 /* XOFF received */
117 /* Definitions for line control of communication */
118 #define MX_WORDLENGTH_5 5
119 #define MX_WORDLENGTH_6 6
120 #define MX_WORDLENGTH_7 7
121 #define MX_WORDLENGTH_8 8
123 #define MX_PARITY_NONE 0
124 #define MX_PARITY_ODD 1
125 #define MX_PARITY_EVEN 2
126 #define MX_PARITY_MARK 3
127 #define MX_PARITY_SPACE 4
129 #define MX_STOP_BITS_1 0
130 #define MX_STOP_BITS_1_5 1
131 #define MX_STOP_BITS_2 2
133 #define MX_RTS_DISABLE 0x0
134 #define MX_RTS_ENABLE 0x1
135 #define MX_RTS_HW 0x2
136 #define MX_RTS_NO_CHANGE 0x3 /* Flag, not valid register value*/
138 #define MX_INT_RS232 0
139 #define MX_INT_2W_RS485 1
140 #define MX_INT_RS422 2
141 #define MX_INT_4W_RS485 3
143 /* Definitions for holding reason */
144 #define MX_WAIT_FOR_CTS 0x0001
145 #define MX_WAIT_FOR_DSR 0x0002
146 #define MX_WAIT_FOR_DCD 0x0004
147 #define MX_WAIT_FOR_XON 0x0008
148 #define MX_WAIT_FOR_START_TX 0x0010
149 #define MX_WAIT_FOR_UNTHROTTLE 0x0020
150 #define MX_WAIT_FOR_LOW_WATER 0x0040
151 #define MX_WAIT_FOR_SEND_NEXT 0x0080
153 #define MX_UPORT_2_PORT BIT(0)
154 #define MX_UPORT_4_PORT BIT(1)
155 #define MX_UPORT_8_PORT BIT(2)
156 #define MX_UPORT_16_PORT BIT(3)
158 /* This structure holds all of the local port information */
159 struct mxuport_port
{
160 u8 mcr_state
; /* Last MCR state */
161 u8 msr_state
; /* Last MSR state */
162 struct mutex mutex
; /* Protects mcr_state */
163 spinlock_t spinlock
; /* Protects msr_state */
166 /* Table of devices that work with this driver */
167 static const struct usb_device_id mxuport_idtable
[] = {
168 { USB_DEVICE(MX_USBSERIAL_VID
, MX_UPORT1250_PID
),
169 .driver_info
= MX_UPORT_2_PORT
},
170 { USB_DEVICE(MX_USBSERIAL_VID
, MX_UPORT1251_PID
),
171 .driver_info
= MX_UPORT_2_PORT
},
172 { USB_DEVICE(MX_USBSERIAL_VID
, MX_UPORT1410_PID
),
173 .driver_info
= MX_UPORT_4_PORT
},
174 { USB_DEVICE(MX_USBSERIAL_VID
, MX_UPORT1450_PID
),
175 .driver_info
= MX_UPORT_4_PORT
},
176 { USB_DEVICE(MX_USBSERIAL_VID
, MX_UPORT1451_PID
),
177 .driver_info
= MX_UPORT_4_PORT
},
178 { USB_DEVICE(MX_USBSERIAL_VID
, MX_UPORT1618_PID
),
179 .driver_info
= MX_UPORT_8_PORT
},
180 { USB_DEVICE(MX_USBSERIAL_VID
, MX_UPORT1658_PID
),
181 .driver_info
= MX_UPORT_8_PORT
},
182 { USB_DEVICE(MX_USBSERIAL_VID
, MX_UPORT1613_PID
),
183 .driver_info
= MX_UPORT_16_PORT
},
184 { USB_DEVICE(MX_USBSERIAL_VID
, MX_UPORT1653_PID
),
185 .driver_info
= MX_UPORT_16_PORT
},
186 {} /* Terminating entry */
189 MODULE_DEVICE_TABLE(usb
, mxuport_idtable
);
192 * Add a four byte header containing the port number and the number of
193 * bytes of data in the message. Return the number of bytes in the
196 static int mxuport_prepare_write_buffer(struct usb_serial_port
*port
,
197 void *dest
, size_t size
)
202 count
= kfifo_out_locked(&port
->write_fifo
, buf
+ HEADER_SIZE
,
206 put_unaligned_be16(port
->port_number
, buf
);
207 put_unaligned_be16(count
, buf
+ 2);
209 dev_dbg(&port
->dev
, "%s - size %zd count %d\n", __func__
,
212 return count
+ HEADER_SIZE
;
215 /* Read the given buffer in from the control pipe. */
216 static int mxuport_recv_ctrl_urb(struct usb_serial
*serial
,
217 u8 request
, u16 value
, u16 index
,
218 u8
*data
, size_t size
)
222 status
= usb_control_msg(serial
->dev
,
223 usb_rcvctrlpipe(serial
->dev
, 0),
225 (USB_DIR_IN
| USB_TYPE_VENDOR
|
226 USB_RECIP_DEVICE
), value
, index
,
228 USB_CTRL_GET_TIMEOUT
);
230 dev_err(&serial
->interface
->dev
,
231 "%s - usb_control_msg failed (%d)\n",
236 if (status
!= size
) {
237 dev_err(&serial
->interface
->dev
,
238 "%s - short read (%d / %zd)\n",
239 __func__
, status
, size
);
246 /* Write the given buffer out to the control pipe. */
247 static int mxuport_send_ctrl_data_urb(struct usb_serial
*serial
,
249 u16 value
, u16 index
,
250 u8
*data
, size_t size
)
254 status
= usb_control_msg(serial
->dev
,
255 usb_sndctrlpipe(serial
->dev
, 0),
257 (USB_DIR_OUT
| USB_TYPE_VENDOR
|
258 USB_RECIP_DEVICE
), value
, index
,
260 USB_CTRL_SET_TIMEOUT
);
262 dev_err(&serial
->interface
->dev
,
263 "%s - usb_control_msg failed (%d)\n",
268 if (status
!= size
) {
269 dev_err(&serial
->interface
->dev
,
270 "%s - short write (%d / %zd)\n",
271 __func__
, status
, size
);
278 /* Send a vendor request without any data */
279 static int mxuport_send_ctrl_urb(struct usb_serial
*serial
,
280 u8 request
, u16 value
, u16 index
)
282 return mxuport_send_ctrl_data_urb(serial
, request
, value
, index
,
287 * mxuport_throttle - throttle function of driver
289 * This function is called by the tty driver when it wants to stop the
290 * data being read from the port. Since all the data comes over one
291 * bulk in endpoint, we cannot stop submitting urbs by setting
292 * port->throttle. Instead tell the device to stop sending us data for
295 static void mxuport_throttle(struct tty_struct
*tty
)
297 struct usb_serial_port
*port
= tty
->driver_data
;
298 struct usb_serial
*serial
= port
->serial
;
300 dev_dbg(&port
->dev
, "%s\n", __func__
);
302 mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_RX_HOST_EN
,
303 0, port
->port_number
);
307 * mxuport_unthrottle - unthrottle function of driver
309 * This function is called by the tty driver when it wants to resume
310 * the data being read from the port. Tell the device it can resume
311 * sending us received data from the port.
313 static void mxuport_unthrottle(struct tty_struct
*tty
)
316 struct usb_serial_port
*port
= tty
->driver_data
;
317 struct usb_serial
*serial
= port
->serial
;
319 dev_dbg(&port
->dev
, "%s\n", __func__
);
321 mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_RX_HOST_EN
,
322 1, port
->port_number
);
326 * Processes one chunk of data received for a port. Mostly a copy of
327 * usb_serial_generic_process_read_urb().
329 static void mxuport_process_read_urb_data(struct usb_serial_port
*port
,
330 char *data
, int size
)
334 if (!port
->port
.console
|| !port
->sysrq
) {
335 tty_insert_flip_string(&port
->port
, data
, size
);
337 for (i
= 0; i
< size
; i
++, data
++) {
338 if (!usb_serial_handle_sysrq_char(port
, *data
))
339 tty_insert_flip_char(&port
->port
, *data
,
343 tty_flip_buffer_push(&port
->port
);
346 static void mxuport_msr_event(struct usb_serial_port
*port
, u8 buf
[4])
348 struct mxuport_port
*mxport
= usb_get_serial_port_data(port
);
349 u8 rcv_msr_hold
= buf
[2] & 0xF0;
350 u16 rcv_msr_event
= get_unaligned_be16(buf
);
353 if (rcv_msr_event
== 0)
356 /* Update MSR status */
357 spin_lock_irqsave(&mxport
->spinlock
, flags
);
359 dev_dbg(&port
->dev
, "%s - current MSR status = 0x%x\n",
360 __func__
, mxport
->msr_state
);
362 if (rcv_msr_hold
& UART_MSR_CTS
) {
363 mxport
->msr_state
|= UART_MSR_CTS
;
364 dev_dbg(&port
->dev
, "%s - CTS high\n", __func__
);
366 mxport
->msr_state
&= ~UART_MSR_CTS
;
367 dev_dbg(&port
->dev
, "%s - CTS low\n", __func__
);
370 if (rcv_msr_hold
& UART_MSR_DSR
) {
371 mxport
->msr_state
|= UART_MSR_DSR
;
372 dev_dbg(&port
->dev
, "%s - DSR high\n", __func__
);
374 mxport
->msr_state
&= ~UART_MSR_DSR
;
375 dev_dbg(&port
->dev
, "%s - DSR low\n", __func__
);
378 if (rcv_msr_hold
& UART_MSR_DCD
) {
379 mxport
->msr_state
|= UART_MSR_DCD
;
380 dev_dbg(&port
->dev
, "%s - DCD high\n", __func__
);
382 mxport
->msr_state
&= ~UART_MSR_DCD
;
383 dev_dbg(&port
->dev
, "%s - DCD low\n", __func__
);
385 spin_unlock_irqrestore(&mxport
->spinlock
, flags
);
388 (SERIAL_EV_CTS
| SERIAL_EV_DSR
| SERIAL_EV_RLSD
)) {
390 if (rcv_msr_event
& SERIAL_EV_CTS
) {
392 dev_dbg(&port
->dev
, "%s - CTS change\n", __func__
);
395 if (rcv_msr_event
& SERIAL_EV_DSR
) {
397 dev_dbg(&port
->dev
, "%s - DSR change\n", __func__
);
400 if (rcv_msr_event
& SERIAL_EV_RLSD
) {
402 dev_dbg(&port
->dev
, "%s - DCD change\n", __func__
);
404 wake_up_interruptible(&port
->port
.delta_msr_wait
);
408 static void mxuport_lsr_event(struct usb_serial_port
*port
, u8 buf
[4])
410 u8 lsr_event
= buf
[2];
412 if (lsr_event
& UART_LSR_BI
) {
414 dev_dbg(&port
->dev
, "%s - break error\n", __func__
);
417 if (lsr_event
& UART_LSR_FE
) {
418 port
->icount
.frame
++;
419 dev_dbg(&port
->dev
, "%s - frame error\n", __func__
);
422 if (lsr_event
& UART_LSR_PE
) {
423 port
->icount
.parity
++;
424 dev_dbg(&port
->dev
, "%s - parity error\n", __func__
);
427 if (lsr_event
& UART_LSR_OE
) {
428 port
->icount
.overrun
++;
429 dev_dbg(&port
->dev
, "%s - overrun error\n", __func__
);
434 * When something interesting happens, modem control lines XON/XOFF
435 * etc, the device sends an event. Process these events.
437 static void mxuport_process_read_urb_event(struct usb_serial_port
*port
,
438 u8 buf
[4], u32 event
)
440 dev_dbg(&port
->dev
, "%s - receive event : %04x\n", __func__
, event
);
443 case UPORT_EVENT_SEND_NEXT
:
445 * Sent as part of the flow control on device buffers.
446 * Not currently used.
449 case UPORT_EVENT_MSR
:
450 mxuport_msr_event(port
, buf
);
452 case UPORT_EVENT_LSR
:
453 mxuport_lsr_event(port
, buf
);
455 case UPORT_EVENT_MCR
:
457 * Event to indicate a change in XON/XOFF from the
458 * peer. Currently not used. We just continue
459 * sending the device data and it will buffer it if
460 * needed. This event could be used for flow control
461 * between the host and the device.
465 dev_dbg(&port
->dev
, "Unexpected event\n");
471 * One URB can contain data for multiple ports. Demultiplex the data,
472 * checking the port exists, is opened and the message is valid.
474 static void mxuport_process_read_urb_demux_data(struct urb
*urb
)
476 struct usb_serial_port
*port
= urb
->context
;
477 struct usb_serial
*serial
= port
->serial
;
478 u8
*data
= urb
->transfer_buffer
;
479 u8
*end
= data
+ urb
->actual_length
;
480 struct usb_serial_port
*demux_port
;
486 if (data
+ HEADER_SIZE
> end
) {
487 dev_warn(&port
->dev
, "%s - message with short header\n",
492 rcv_port
= get_unaligned_be16(data
);
493 if (rcv_port
>= serial
->num_ports
) {
494 dev_warn(&port
->dev
, "%s - message for invalid port\n",
499 demux_port
= serial
->port
[rcv_port
];
500 rcv_len
= get_unaligned_be16(data
+ 2);
501 if (!rcv_len
|| data
+ HEADER_SIZE
+ rcv_len
> end
) {
502 dev_warn(&port
->dev
, "%s - short data\n", __func__
);
506 if (test_bit(ASYNCB_INITIALIZED
, &demux_port
->port
.flags
)) {
507 ch
= data
+ HEADER_SIZE
;
508 mxuport_process_read_urb_data(demux_port
, ch
, rcv_len
);
510 dev_dbg(&demux_port
->dev
, "%s - data for closed port\n",
513 data
+= HEADER_SIZE
+ rcv_len
;
518 * One URB can contain events for multiple ports. Demultiplex the event,
519 * checking the port exists, and is opened.
521 static void mxuport_process_read_urb_demux_event(struct urb
*urb
)
523 struct usb_serial_port
*port
= urb
->context
;
524 struct usb_serial
*serial
= port
->serial
;
525 u8
*data
= urb
->transfer_buffer
;
526 u8
*end
= data
+ urb
->actual_length
;
527 struct usb_serial_port
*demux_port
;
533 if (data
+ EVENT_LENGTH
> end
) {
534 dev_warn(&port
->dev
, "%s - message with short event\n",
539 rcv_port
= get_unaligned_be16(data
);
540 if (rcv_port
>= serial
->num_ports
) {
541 dev_warn(&port
->dev
, "%s - message for invalid port\n",
546 demux_port
= serial
->port
[rcv_port
];
547 if (test_bit(ASYNCB_INITIALIZED
, &demux_port
->port
.flags
)) {
548 ch
= data
+ HEADER_SIZE
;
549 rcv_event
= get_unaligned_be16(data
+ 2);
550 mxuport_process_read_urb_event(demux_port
, ch
,
553 dev_dbg(&demux_port
->dev
,
554 "%s - event for closed port\n", __func__
);
556 data
+= EVENT_LENGTH
;
561 * This is called when we have received data on the bulk in
562 * endpoint. Depending on which port it was received on, it can
563 * contain serial data or events.
565 static void mxuport_process_read_urb(struct urb
*urb
)
567 struct usb_serial_port
*port
= urb
->context
;
568 struct usb_serial
*serial
= port
->serial
;
570 if (port
== serial
->port
[0])
571 mxuport_process_read_urb_demux_data(urb
);
573 if (port
== serial
->port
[1])
574 mxuport_process_read_urb_demux_event(urb
);
578 * Ask the device how many bytes it has queued to be sent out. If
579 * there are none, return true.
581 static bool mxuport_tx_empty(struct usb_serial_port
*port
)
583 struct usb_serial
*serial
= port
->serial
;
584 bool is_empty
= true;
589 len_buf
= kzalloc(4, GFP_KERNEL
);
593 err
= mxuport_recv_ctrl_urb(serial
, RQ_VENDOR_GET_OUTQUEUE
, 0,
594 port
->port_number
, len_buf
, 4);
598 txlen
= get_unaligned_be32(len_buf
);
599 dev_dbg(&port
->dev
, "%s - tx len = %u\n", __func__
, txlen
);
609 static int mxuport_set_mcr(struct usb_serial_port
*port
, u8 mcr_state
)
611 struct usb_serial
*serial
= port
->serial
;
614 dev_dbg(&port
->dev
, "%s - %02x\n", __func__
, mcr_state
);
616 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_MCR
,
617 mcr_state
, port
->port_number
);
619 dev_err(&port
->dev
, "%s - failed to change MCR\n", __func__
);
624 static int mxuport_set_dtr(struct usb_serial_port
*port
, int on
)
626 struct mxuport_port
*mxport
= usb_get_serial_port_data(port
);
627 struct usb_serial
*serial
= port
->serial
;
630 mutex_lock(&mxport
->mutex
);
632 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_DTR
,
633 !!on
, port
->port_number
);
636 mxport
->mcr_state
|= UART_MCR_DTR
;
638 mxport
->mcr_state
&= ~UART_MCR_DTR
;
641 mutex_unlock(&mxport
->mutex
);
646 static int mxuport_set_rts(struct usb_serial_port
*port
, u8 state
)
648 struct mxuport_port
*mxport
= usb_get_serial_port_data(port
);
649 struct usb_serial
*serial
= port
->serial
;
653 mutex_lock(&mxport
->mutex
);
654 mcr_state
= mxport
->mcr_state
;
658 mcr_state
&= ~UART_MCR_RTS
;
661 mcr_state
|= UART_MCR_RTS
;
665 * Do not update mxport->mcr_state when doing hardware
671 * Should not happen, but somebody might try passing
672 * MX_RTS_NO_CHANGE, which is not valid.
677 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_RTS
,
678 state
, port
->port_number
);
680 mxport
->mcr_state
= mcr_state
;
683 mutex_unlock(&mxport
->mutex
);
688 static void mxuport_dtr_rts(struct usb_serial_port
*port
, int on
)
690 struct mxuport_port
*mxport
= usb_get_serial_port_data(port
);
694 mutex_lock(&mxport
->mutex
);
695 mcr_state
= mxport
->mcr_state
;
698 mcr_state
|= (UART_MCR_RTS
| UART_MCR_DTR
);
700 mcr_state
&= ~(UART_MCR_RTS
| UART_MCR_DTR
);
702 err
= mxuport_set_mcr(port
, mcr_state
);
704 mxport
->mcr_state
= mcr_state
;
706 mutex_unlock(&mxport
->mutex
);
709 static int mxuport_tiocmset(struct tty_struct
*tty
, unsigned int set
,
712 struct usb_serial_port
*port
= tty
->driver_data
;
713 struct mxuport_port
*mxport
= usb_get_serial_port_data(port
);
717 mutex_lock(&mxport
->mutex
);
718 mcr_state
= mxport
->mcr_state
;
721 mcr_state
|= UART_MCR_RTS
;
724 mcr_state
|= UART_MCR_DTR
;
726 if (clear
& TIOCM_RTS
)
727 mcr_state
&= ~UART_MCR_RTS
;
729 if (clear
& TIOCM_DTR
)
730 mcr_state
&= ~UART_MCR_DTR
;
732 err
= mxuport_set_mcr(port
, mcr_state
);
734 mxport
->mcr_state
= mcr_state
;
736 mutex_unlock(&mxport
->mutex
);
741 static int mxuport_tiocmget(struct tty_struct
*tty
)
743 struct mxuport_port
*mxport
;
744 struct usb_serial_port
*port
= tty
->driver_data
;
750 mxport
= usb_get_serial_port_data(port
);
752 mutex_lock(&mxport
->mutex
);
753 spin_lock_irqsave(&mxport
->spinlock
, flags
);
755 msr
= mxport
->msr_state
;
756 mcr
= mxport
->mcr_state
;
758 spin_unlock_irqrestore(&mxport
->spinlock
, flags
);
759 mutex_unlock(&mxport
->mutex
);
761 result
= (((mcr
& UART_MCR_DTR
) ? TIOCM_DTR
: 0) | /* 0x002 */
762 ((mcr
& UART_MCR_RTS
) ? TIOCM_RTS
: 0) | /* 0x004 */
763 ((msr
& UART_MSR_CTS
) ? TIOCM_CTS
: 0) | /* 0x020 */
764 ((msr
& UART_MSR_DCD
) ? TIOCM_CAR
: 0) | /* 0x040 */
765 ((msr
& UART_MSR_RI
) ? TIOCM_RI
: 0) | /* 0x080 */
766 ((msr
& UART_MSR_DSR
) ? TIOCM_DSR
: 0)); /* 0x100 */
768 dev_dbg(&port
->dev
, "%s - 0x%04x\n", __func__
, result
);
773 static int mxuport_set_termios_flow(struct tty_struct
*tty
,
774 struct ktermios
*old_termios
,
775 struct usb_serial_port
*port
,
776 struct usb_serial
*serial
)
778 u8 xon
= START_CHAR(tty
);
779 u8 xoff
= STOP_CHAR(tty
);
785 buf
= kmalloc(2, GFP_KERNEL
);
789 /* S/W flow control settings */
790 if (I_IXOFF(tty
) || I_IXON(tty
)) {
795 err
= mxuport_send_ctrl_data_urb(serial
, RQ_VENDOR_SET_CHARS
,
796 0, port
->port_number
,
801 dev_dbg(&port
->dev
, "%s - XON = 0x%02x, XOFF = 0x%02x\n",
802 __func__
, xon
, xoff
);
807 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_XONXOFF
,
808 enable
, port
->port_number
);
812 rts
= MX_RTS_NO_CHANGE
;
814 /* H/W flow control settings */
816 C_CRTSCTS(tty
) != (old_termios
->c_cflag
& CRTSCTS
)) {
824 if (old_termios
&& (old_termios
->c_cflag
& CBAUD
) == B0
) {
825 /* Raise DTR and RTS */
830 mxuport_set_dtr(port
, 1);
833 /* Drop DTR and RTS */
834 rts
= MX_RTS_DISABLE
;
835 mxuport_set_dtr(port
, 0);
838 if (rts
!= MX_RTS_NO_CHANGE
)
839 err
= mxuport_set_rts(port
, rts
);
846 static void mxuport_set_termios(struct tty_struct
*tty
,
847 struct usb_serial_port
*port
,
848 struct ktermios
*old_termios
)
850 struct usb_serial
*serial
= port
->serial
;
859 !tty_termios_hw_change(&tty
->termios
, old_termios
) &&
860 tty
->termios
.c_iflag
== old_termios
->c_iflag
) {
861 dev_dbg(&port
->dev
, "%s - nothing to change\n", __func__
);
865 buf
= kmalloc(4, GFP_KERNEL
);
869 /* Set data bit of termios */
870 switch (C_CSIZE(tty
)) {
872 data_bits
= MX_WORDLENGTH_5
;
875 data_bits
= MX_WORDLENGTH_6
;
878 data_bits
= MX_WORDLENGTH_7
;
882 data_bits
= MX_WORDLENGTH_8
;
886 /* Set parity of termios */
890 parity
= MX_PARITY_MARK
;
892 parity
= MX_PARITY_SPACE
;
895 parity
= MX_PARITY_ODD
;
897 parity
= MX_PARITY_EVEN
;
900 parity
= MX_PARITY_NONE
;
903 /* Set stop bit of termios */
905 stop_bits
= MX_STOP_BITS_2
;
907 stop_bits
= MX_STOP_BITS_1
;
914 err
= mxuport_send_ctrl_data_urb(serial
, RQ_VENDOR_SET_LINE
,
915 0, port
->port_number
, buf
, 4);
919 err
= mxuport_set_termios_flow(tty
, old_termios
, port
, serial
);
923 baud
= tty_get_baud_rate(tty
);
927 /* Note: Little Endian */
928 put_unaligned_le32(baud
, buf
);
930 err
= mxuport_send_ctrl_data_urb(serial
, RQ_VENDOR_SET_BAUD
,
931 0, port
->port_number
,
936 dev_dbg(&port
->dev
, "baud_rate : %d\n", baud
);
937 dev_dbg(&port
->dev
, "data_bits : %d\n", data_bits
);
938 dev_dbg(&port
->dev
, "parity : %d\n", parity
);
939 dev_dbg(&port
->dev
, "stop_bits : %d\n", stop_bits
);
946 * Determine how many ports this device has dynamically. It will be
947 * called after the probe() callback is called, but before attach().
949 static int mxuport_calc_num_ports(struct usb_serial
*serial
)
951 unsigned long features
= (unsigned long)usb_get_serial_data(serial
);
953 if (features
& MX_UPORT_2_PORT
)
955 if (features
& MX_UPORT_4_PORT
)
957 if (features
& MX_UPORT_8_PORT
)
959 if (features
& MX_UPORT_16_PORT
)
965 /* Get the version of the firmware currently running. */
966 static int mxuport_get_fw_version(struct usb_serial
*serial
, u32
*version
)
971 ver_buf
= kzalloc(4, GFP_KERNEL
);
975 /* Get firmware version from SDRAM */
976 err
= mxuport_recv_ctrl_urb(serial
, RQ_VENDOR_GET_VERSION
, 0, 0,
983 *version
= (ver_buf
[0] << 16) | (ver_buf
[1] << 8) | ver_buf
[2];
990 /* Given a firmware blob, download it to the device. */
991 static int mxuport_download_fw(struct usb_serial
*serial
,
992 const struct firmware
*fw_p
)
999 fw_buf
= kmalloc(DOWN_BLOCK_SIZE
, GFP_KERNEL
);
1003 dev_dbg(&serial
->interface
->dev
, "Starting firmware download...\n");
1004 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_START_FW_DOWN
, 0, 0);
1010 txlen
= min_t(size_t, (fw_p
->size
- fwidx
), DOWN_BLOCK_SIZE
);
1012 memcpy(fw_buf
, &fw_p
->data
[fwidx
], txlen
);
1013 err
= mxuport_send_ctrl_data_urb(serial
, RQ_VENDOR_FW_DATA
,
1014 0, 0, fw_buf
, txlen
);
1016 mxuport_send_ctrl_urb(serial
, RQ_VENDOR_STOP_FW_DOWN
,
1022 usleep_range(1000, 2000);
1024 } while (fwidx
< fw_p
->size
);
1027 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_STOP_FW_DOWN
, 0, 0);
1032 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_QUERY_FW_READY
, 0, 0);
1039 static int mxuport_probe(struct usb_serial
*serial
,
1040 const struct usb_device_id
*id
)
1042 u16 productid
= le16_to_cpu(serial
->dev
->descriptor
.idProduct
);
1043 const struct firmware
*fw_p
= NULL
;
1049 /* Load our firmware */
1050 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_QUERY_FW_CONFIG
, 0, 0);
1052 mxuport_send_ctrl_urb(serial
, RQ_VENDOR_RESET_DEVICE
, 0, 0);
1056 err
= mxuport_get_fw_version(serial
, &version
);
1060 dev_dbg(&serial
->interface
->dev
, "Device firmware version v%x.%x.%x\n",
1061 (version
& 0xff0000) >> 16,
1062 (version
& 0xff00) >> 8,
1065 snprintf(buf
, sizeof(buf
) - 1, "moxa/moxa-%04x.fw", productid
);
1067 err
= request_firmware(&fw_p
, buf
, &serial
->interface
->dev
);
1069 dev_warn(&serial
->interface
->dev
, "Firmware %s not found\n",
1072 /* Use the firmware already in the device */
1075 local_ver
= ((fw_p
->data
[VER_ADDR_1
] << 16) |
1076 (fw_p
->data
[VER_ADDR_2
] << 8) |
1077 fw_p
->data
[VER_ADDR_3
]);
1078 dev_dbg(&serial
->interface
->dev
,
1079 "Available firmware version v%x.%x.%x\n",
1080 fw_p
->data
[VER_ADDR_1
], fw_p
->data
[VER_ADDR_2
],
1081 fw_p
->data
[VER_ADDR_3
]);
1082 if (local_ver
> version
) {
1083 err
= mxuport_download_fw(serial
, fw_p
);
1086 err
= mxuport_get_fw_version(serial
, &version
);
1092 dev_info(&serial
->interface
->dev
,
1093 "Using device firmware version v%x.%x.%x\n",
1094 (version
& 0xff0000) >> 16,
1095 (version
& 0xff00) >> 8,
1099 * Contains the features of this hardware. Store away for
1100 * later use, eg, number of ports.
1102 usb_set_serial_data(serial
, (void *)id
->driver_info
);
1105 release_firmware(fw_p
);
1110 static int mxuport_port_probe(struct usb_serial_port
*port
)
1112 struct usb_serial
*serial
= port
->serial
;
1113 struct mxuport_port
*mxport
;
1116 mxport
= devm_kzalloc(&port
->dev
, sizeof(struct mxuport_port
),
1121 mutex_init(&mxport
->mutex
);
1122 spin_lock_init(&mxport
->spinlock
);
1124 /* Set the port private data */
1125 usb_set_serial_port_data(port
, mxport
);
1127 /* Set FIFO (Enable) */
1128 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_FIFO_DISABLE
,
1129 0, port
->port_number
);
1133 /* Set transmission mode (Hi-Performance) */
1134 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_HIGH_PERFOR
,
1135 0, port
->port_number
);
1139 /* Set interface (RS-232) */
1140 return mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_INTERFACE
,
1145 static int mxuport_alloc_write_urb(struct usb_serial
*serial
,
1146 struct usb_serial_port
*port
,
1147 struct usb_serial_port
*port0
,
1150 struct usb_device
*dev
= interface_to_usbdev(serial
->interface
);
1152 set_bit(j
, &port
->write_urbs_free
);
1153 port
->write_urbs
[j
] = usb_alloc_urb(0, GFP_KERNEL
);
1154 if (!port
->write_urbs
[j
])
1157 port
->bulk_out_buffers
[j
] = kmalloc(port0
->bulk_out_size
, GFP_KERNEL
);
1158 if (!port
->bulk_out_buffers
[j
])
1161 usb_fill_bulk_urb(port
->write_urbs
[j
], dev
,
1162 usb_sndbulkpipe(dev
, port
->bulk_out_endpointAddress
),
1163 port
->bulk_out_buffers
[j
],
1164 port
->bulk_out_size
,
1165 serial
->type
->write_bulk_callback
,
1171 static int mxuport_alloc_write_urbs(struct usb_serial
*serial
,
1172 struct usb_serial_port
*port
,
1173 struct usb_serial_port
*port0
)
1178 for (j
= 0; j
< ARRAY_SIZE(port
->write_urbs
); ++j
) {
1179 ret
= mxuport_alloc_write_urb(serial
, port
, port0
, j
);
1187 static int mxuport_attach(struct usb_serial
*serial
)
1189 struct usb_serial_port
*port0
= serial
->port
[0];
1190 struct usb_serial_port
*port1
= serial
->port
[1];
1191 struct usb_serial_port
*port
;
1197 * Throw away all but the first allocated write URBs so we can
1198 * set them up again to fit the multiplexing scheme.
1200 for (i
= 1; i
< serial
->num_bulk_out
; ++i
) {
1201 port
= serial
->port
[i
];
1202 for (j
= 0; j
< ARRAY_SIZE(port
->write_urbs
); ++j
) {
1203 usb_free_urb(port
->write_urbs
[j
]);
1204 kfree(port
->bulk_out_buffers
[j
]);
1205 port
->write_urbs
[j
] = NULL
;
1206 port
->bulk_out_buffers
[j
] = NULL
;
1208 port
->write_urbs_free
= 0;
1212 * All write data is sent over the first bulk out endpoint,
1213 * with an added header to indicate the port. Allocate URBs
1214 * for each port to the first bulk out endpoint.
1216 for (i
= 1; i
< serial
->num_ports
; ++i
) {
1217 port
= serial
->port
[i
];
1218 port
->bulk_out_size
= port0
->bulk_out_size
;
1219 port
->bulk_out_endpointAddress
=
1220 port0
->bulk_out_endpointAddress
;
1222 err
= mxuport_alloc_write_urbs(serial
, port
, port0
);
1226 port
->write_urb
= port
->write_urbs
[0];
1227 port
->bulk_out_buffer
= port
->bulk_out_buffers
[0];
1230 * Ensure each port has a fifo. The framework only
1231 * allocates a fifo to ports with a bulk out endpoint,
1232 * where as we need one for every port.
1234 if (!kfifo_initialized(&port
->write_fifo
)) {
1235 err
= kfifo_alloc(&port
->write_fifo
, PAGE_SIZE
,
1243 * All data from the ports is received on the first bulk in
1244 * endpoint, with a multiplex header. The second bulk in is
1247 * Start to read from the device.
1249 err
= usb_serial_generic_submit_read_urbs(port0
, GFP_KERNEL
);
1253 err
= usb_serial_generic_submit_read_urbs(port1
, GFP_KERNEL
);
1255 usb_serial_generic_close(port0
);
1262 static void mxuport_release(struct usb_serial
*serial
)
1264 struct usb_serial_port
*port0
= serial
->port
[0];
1265 struct usb_serial_port
*port1
= serial
->port
[1];
1267 usb_serial_generic_close(port1
);
1268 usb_serial_generic_close(port0
);
1271 static int mxuport_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
1273 struct mxuport_port
*mxport
= usb_get_serial_port_data(port
);
1274 struct usb_serial
*serial
= port
->serial
;
1277 /* Set receive host (enable) */
1278 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_RX_HOST_EN
,
1279 1, port
->port_number
);
1283 err
= mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_OPEN
,
1284 1, port
->port_number
);
1286 mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_RX_HOST_EN
,
1287 0, port
->port_number
);
1291 /* Initial port termios */
1293 mxuport_set_termios(tty
, port
, NULL
);
1296 * TODO: use RQ_VENDOR_GET_MSR, once we know what it
1299 mxport
->msr_state
= 0;
1304 static void mxuport_close(struct usb_serial_port
*port
)
1306 struct usb_serial
*serial
= port
->serial
;
1308 mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_OPEN
, 0,
1311 mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_RX_HOST_EN
, 0,
1315 /* Send a break to the port. */
1316 static void mxuport_break_ctl(struct tty_struct
*tty
, int break_state
)
1318 struct usb_serial_port
*port
= tty
->driver_data
;
1319 struct usb_serial
*serial
= port
->serial
;
1322 if (break_state
== -1) {
1324 dev_dbg(&port
->dev
, "%s - sending break\n", __func__
);
1327 dev_dbg(&port
->dev
, "%s - clearing break\n", __func__
);
1330 mxuport_send_ctrl_urb(serial
, RQ_VENDOR_SET_BREAK
,
1331 enable
, port
->port_number
);
1334 static int mxuport_resume(struct usb_serial
*serial
)
1336 struct usb_serial_port
*port
;
1341 for (i
= 0; i
< 2; i
++) {
1342 port
= serial
->port
[i
];
1344 r
= usb_serial_generic_submit_read_urbs(port
, GFP_NOIO
);
1349 for (i
= 0; i
< serial
->num_ports
; i
++) {
1350 port
= serial
->port
[i
];
1351 if (!test_bit(ASYNCB_INITIALIZED
, &port
->port
.flags
))
1354 r
= usb_serial_generic_write_start(port
, GFP_NOIO
);
1359 return c
? -EIO
: 0;
1362 static struct usb_serial_driver mxuport_device
= {
1364 .owner
= THIS_MODULE
,
1367 .description
= "MOXA UPort",
1368 .id_table
= mxuport_idtable
,
1370 .probe
= mxuport_probe
,
1371 .port_probe
= mxuport_port_probe
,
1372 .attach
= mxuport_attach
,
1373 .release
= mxuport_release
,
1374 .calc_num_ports
= mxuport_calc_num_ports
,
1375 .open
= mxuport_open
,
1376 .close
= mxuport_close
,
1377 .set_termios
= mxuport_set_termios
,
1378 .break_ctl
= mxuport_break_ctl
,
1379 .tx_empty
= mxuport_tx_empty
,
1380 .tiocmiwait
= usb_serial_generic_tiocmiwait
,
1381 .get_icount
= usb_serial_generic_get_icount
,
1382 .throttle
= mxuport_throttle
,
1383 .unthrottle
= mxuport_unthrottle
,
1384 .tiocmget
= mxuport_tiocmget
,
1385 .tiocmset
= mxuport_tiocmset
,
1386 .dtr_rts
= mxuport_dtr_rts
,
1387 .process_read_urb
= mxuport_process_read_urb
,
1388 .prepare_write_buffer
= mxuport_prepare_write_buffer
,
1389 .resume
= mxuport_resume
,
1392 static struct usb_serial_driver
*const serial_drivers
[] = {
1393 &mxuport_device
, NULL
1396 module_usb_serial_driver(serial_drivers
, mxuport_idtable
);
1398 MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
1399 MODULE_AUTHOR("<support@moxa.com>");
1400 MODULE_LICENSE("GPL");