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/init.h>
23 #include <linux/jiffies.h>
24 #include <linux/serial.h>
25 #include <linux/serial_reg.h>
26 #include <linux/slab.h>
27 #include <linux/tty.h>
28 #include <linux/tty_driver.h>
29 #include <linux/tty_flip.h>
30 #include <linux/uaccess.h>
31 #include <linux/usb.h>
32 #include <linux/usb/serial.h>
33 #include <asm/unaligned.h>
35 /* Definitions for the vendor ID and device ID */
36 #define MX_USBSERIAL_VID 0x110A
37 #define MX_UPORT1250_PID 0x1250
38 #define MX_UPORT1251_PID 0x1251
39 #define MX_UPORT1410_PID 0x1410
40 #define MX_UPORT1450_PID 0x1450
41 #define MX_UPORT1451_PID 0x1451
42 #define MX_UPORT1618_PID 0x1618
43 #define MX_UPORT1658_PID 0x1658
44 #define MX_UPORT1613_PID 0x1613
45 #define MX_UPORT1653_PID 0x1653
47 /* Definitions for USB info */
49 #define EVENT_LENGTH 8
50 #define DOWN_BLOCK_SIZE 64
52 /* Definitions for firmware info */
53 #define VER_ADDR_1 0x20
54 #define VER_ADDR_2 0x24
55 #define VER_ADDR_3 0x28
57 /* Definitions for USB vendor request */
58 #define RQ_VENDOR_NONE 0x00
59 #define RQ_VENDOR_SET_BAUD 0x01 /* Set baud rate */
60 #define RQ_VENDOR_SET_LINE 0x02 /* Set line status */
61 #define RQ_VENDOR_SET_CHARS 0x03 /* Set Xon/Xoff chars */
62 #define RQ_VENDOR_SET_RTS 0x04 /* Set RTS */
63 #define RQ_VENDOR_SET_DTR 0x05 /* Set DTR */
64 #define RQ_VENDOR_SET_XONXOFF 0x06 /* Set auto Xon/Xoff */
65 #define RQ_VENDOR_SET_RX_HOST_EN 0x07 /* Set RX host enable */
66 #define RQ_VENDOR_SET_OPEN 0x08 /* Set open/close port */
67 #define RQ_VENDOR_PURGE 0x09 /* Purge Rx/Tx buffer */
68 #define RQ_VENDOR_SET_MCR 0x0A /* Set MCR register */
69 #define RQ_VENDOR_SET_BREAK 0x0B /* Set Break signal */
71 #define RQ_VENDOR_START_FW_DOWN 0x0C /* Start firmware download */
72 #define RQ_VENDOR_STOP_FW_DOWN 0x0D /* Stop firmware download */
73 #define RQ_VENDOR_QUERY_FW_READY 0x0E /* Query if new firmware ready */
75 #define RQ_VENDOR_SET_FIFO_DISABLE 0x0F /* Set fifo disable */
76 #define RQ_VENDOR_SET_INTERFACE 0x10 /* Set interface */
77 #define RQ_VENDOR_SET_HIGH_PERFOR 0x11 /* Set hi-performance */
79 #define RQ_VENDOR_ERASE_BLOCK 0x12 /* Erase flash block */
80 #define RQ_VENDOR_WRITE_PAGE 0x13 /* Write flash page */
81 #define RQ_VENDOR_PREPARE_WRITE 0x14 /* Prepare write flash */
82 #define RQ_VENDOR_CONFIRM_WRITE 0x15 /* Confirm write flash */
83 #define RQ_VENDOR_LOCATE 0x16 /* Locate the device */
85 #define RQ_VENDOR_START_ROM_DOWN 0x17 /* Start firmware download */
86 #define RQ_VENDOR_ROM_DATA 0x18 /* Rom file data */
87 #define RQ_VENDOR_STOP_ROM_DOWN 0x19 /* Stop firmware download */
88 #define RQ_VENDOR_FW_DATA 0x20 /* Firmware data */
90 #define RQ_VENDOR_RESET_DEVICE 0x23 /* Try to reset the device */
91 #define RQ_VENDOR_QUERY_FW_CONFIG 0x24
93 #define RQ_VENDOR_GET_VERSION 0x81 /* Get firmware version */
94 #define RQ_VENDOR_GET_PAGE 0x82 /* Read flash page */
95 #define RQ_VENDOR_GET_ROM_PROC 0x83 /* Get ROM process state */
97 #define RQ_VENDOR_GET_INQUEUE 0x84 /* Data in input buffer */
98 #define RQ_VENDOR_GET_OUTQUEUE 0x85 /* Data in output buffer */
100 #define RQ_VENDOR_GET_MSR 0x86 /* Get modem status register */
102 /* Definitions for UPort event type */
103 #define UPORT_EVENT_NONE 0 /* None */
104 #define UPORT_EVENT_TXBUF_THRESHOLD 1 /* Tx buffer threshold */
105 #define UPORT_EVENT_SEND_NEXT 2 /* Send next */
106 #define UPORT_EVENT_MSR 3 /* Modem status */
107 #define UPORT_EVENT_LSR 4 /* Line status */
108 #define UPORT_EVENT_MCR 5 /* Modem control */
110 /* Definitions for serial event type */
111 #define SERIAL_EV_CTS 0x0008 /* CTS changed state */
112 #define SERIAL_EV_DSR 0x0010 /* DSR changed state */
113 #define SERIAL_EV_RLSD 0x0020 /* RLSD changed state */
115 /* Definitions for modem control event type */
116 #define SERIAL_EV_XOFF 0x40 /* XOFF received */
118 /* Definitions for line control of communication */
119 #define MX_WORDLENGTH_5 5
120 #define MX_WORDLENGTH_6 6
121 #define MX_WORDLENGTH_7 7
122 #define MX_WORDLENGTH_8 8
124 #define MX_PARITY_NONE 0
125 #define MX_PARITY_ODD 1
126 #define MX_PARITY_EVEN 2
127 #define MX_PARITY_MARK 3
128 #define MX_PARITY_SPACE 4
130 #define MX_STOP_BITS_1 0
131 #define MX_STOP_BITS_1_5 1
132 #define MX_STOP_BITS_2 2
134 #define MX_RTS_DISABLE 0x0
135 #define MX_RTS_ENABLE 0x1
136 #define MX_RTS_HW 0x2
137 #define MX_RTS_NO_CHANGE 0x3 /* Flag, not valid register value*/
139 #define MX_INT_RS232 0
140 #define MX_INT_2W_RS485 1
141 #define MX_INT_RS422 2
142 #define MX_INT_4W_RS485 3
144 /* Definitions for holding reason */
145 #define MX_WAIT_FOR_CTS 0x0001
146 #define MX_WAIT_FOR_DSR 0x0002
147 #define MX_WAIT_FOR_DCD 0x0004
148 #define MX_WAIT_FOR_XON 0x0008
149 #define MX_WAIT_FOR_START_TX 0x0010
150 #define MX_WAIT_FOR_UNTHROTTLE 0x0020
151 #define MX_WAIT_FOR_LOW_WATER 0x0040
152 #define MX_WAIT_FOR_SEND_NEXT 0x0080
154 #define MX_UPORT_2_PORT BIT(0)
155 #define MX_UPORT_4_PORT BIT(1)
156 #define MX_UPORT_8_PORT BIT(2)
157 #define MX_UPORT_16_PORT BIT(3)
159 /* This structure holds all of the local port information */
160 struct mxuport_port {
161 u8 mcr_state; /* Last MCR state */
162 u8 msr_state; /* Last MSR state */
163 struct mutex mutex; /* Protects mcr_state */
164 spinlock_t spinlock; /* Protects msr_state */
167 /* Table of devices that work with this driver */
168 static const struct usb_device_id mxuport_idtable[] = {
169 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1250_PID),
170 .driver_info = MX_UPORT_2_PORT },
171 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1251_PID),
172 .driver_info = MX_UPORT_2_PORT },
173 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1410_PID),
174 .driver_info = MX_UPORT_4_PORT },
175 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1450_PID),
176 .driver_info = MX_UPORT_4_PORT },
177 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1451_PID),
178 .driver_info = MX_UPORT_4_PORT },
179 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1618_PID),
180 .driver_info = MX_UPORT_8_PORT },
181 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1658_PID),
182 .driver_info = MX_UPORT_8_PORT },
183 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1613_PID),
184 .driver_info = MX_UPORT_16_PORT },
185 { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1653_PID),
186 .driver_info = MX_UPORT_16_PORT },
187 {} /* Terminating entry */
190 MODULE_DEVICE_TABLE(usb, mxuport_idtable);
193 * Add a four byte header containing the port number and the number of
194 * bytes of data in the message. Return the number of bytes in the
197 static int mxuport_prepare_write_buffer(struct usb_serial_port *port,
198 void *dest, size_t size)
203 count = kfifo_out_locked(&port->write_fifo, buf + HEADER_SIZE,
207 put_unaligned_be16(port->port_number, buf);
208 put_unaligned_be16(count, buf + 2);
210 dev_dbg(&port->dev, "%s - size %zd count %d\n", __func__,
213 return count + HEADER_SIZE;
216 /* Read the given buffer in from the control pipe. */
217 static int mxuport_recv_ctrl_urb(struct usb_serial *serial,
218 u8 request, u16 value, u16 index,
219 u8 *data, size_t size)
223 status = usb_control_msg(serial->dev,
224 usb_rcvctrlpipe(serial->dev, 0),
226 (USB_DIR_IN | USB_TYPE_VENDOR |
227 USB_RECIP_DEVICE), value, index,
229 USB_CTRL_GET_TIMEOUT);
231 dev_err(&serial->interface->dev,
232 "%s - usb_control_msg failed (%d)\n",
237 if (status != size) {
238 dev_err(&serial->interface->dev,
239 "%s - short read (%d / %zd)\n",
240 __func__, status, size);
247 /* Write the given buffer out to the control pipe. */
248 static int mxuport_send_ctrl_data_urb(struct usb_serial *serial,
250 u16 value, u16 index,
251 u8 *data, size_t size)
255 status = usb_control_msg(serial->dev,
256 usb_sndctrlpipe(serial->dev, 0),
258 (USB_DIR_OUT | USB_TYPE_VENDOR |
259 USB_RECIP_DEVICE), value, index,
261 USB_CTRL_SET_TIMEOUT);
263 dev_err(&serial->interface->dev,
264 "%s - usb_control_msg failed (%d)\n",
269 if (status != size) {
270 dev_err(&serial->interface->dev,
271 "%s - short write (%d / %zd)\n",
272 __func__, status, size);
279 /* Send a vendor request without any data */
280 static int mxuport_send_ctrl_urb(struct usb_serial *serial,
281 u8 request, u16 value, u16 index)
283 return mxuport_send_ctrl_data_urb(serial, request, value, index,
288 * mxuport_throttle - throttle function of driver
290 * This function is called by the tty driver when it wants to stop the
291 * data being read from the port. Since all the data comes over one
292 * bulk in endpoint, we cannot stop submitting urbs by setting
293 * port->throttle. Instead tell the device to stop sending us data for
296 static void mxuport_throttle(struct tty_struct *tty)
298 struct usb_serial_port *port = tty->driver_data;
299 struct usb_serial *serial = port->serial;
301 dev_dbg(&port->dev, "%s\n", __func__);
303 mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
304 0, port->port_number);
308 * mxuport_unthrottle - unthrottle function of driver
310 * This function is called by the tty driver when it wants to resume
311 * the data being read from the port. Tell the device it can resume
312 * sending us received data from the port.
314 static void mxuport_unthrottle(struct tty_struct *tty)
317 struct usb_serial_port *port = tty->driver_data;
318 struct usb_serial *serial = port->serial;
320 dev_dbg(&port->dev, "%s\n", __func__);
322 mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
323 1, port->port_number);
327 * Processes one chunk of data received for a port. Mostly a copy of
328 * usb_serial_generic_process_read_urb().
330 static void mxuport_process_read_urb_data(struct usb_serial_port *port,
331 char *data, int size)
335 if (!port->port.console || !port->sysrq) {
336 tty_insert_flip_string(&port->port, data, size);
338 for (i = 0; i < size; i++, data++) {
339 if (!usb_serial_handle_sysrq_char(port, *data))
340 tty_insert_flip_char(&port->port, *data,
344 tty_flip_buffer_push(&port->port);
347 static void mxuport_msr_event(struct usb_serial_port *port, u8 buf[4])
349 struct mxuport_port *mxport = usb_get_serial_port_data(port);
350 u8 rcv_msr_hold = buf[2] & 0xF0;
351 u16 rcv_msr_event = get_unaligned_be16(buf);
354 if (rcv_msr_event == 0)
357 /* Update MSR status */
358 spin_lock_irqsave(&mxport->spinlock, flags);
360 dev_dbg(&port->dev, "%s - current MSR status = 0x%x\n",
361 __func__, mxport->msr_state);
363 if (rcv_msr_hold & UART_MSR_CTS) {
364 mxport->msr_state |= UART_MSR_CTS;
365 dev_dbg(&port->dev, "%s - CTS high\n", __func__);
367 mxport->msr_state &= ~UART_MSR_CTS;
368 dev_dbg(&port->dev, "%s - CTS low\n", __func__);
371 if (rcv_msr_hold & UART_MSR_DSR) {
372 mxport->msr_state |= UART_MSR_DSR;
373 dev_dbg(&port->dev, "%s - DSR high\n", __func__);
375 mxport->msr_state &= ~UART_MSR_DSR;
376 dev_dbg(&port->dev, "%s - DSR low\n", __func__);
379 if (rcv_msr_hold & UART_MSR_DCD) {
380 mxport->msr_state |= UART_MSR_DCD;
381 dev_dbg(&port->dev, "%s - DCD high\n", __func__);
383 mxport->msr_state &= ~UART_MSR_DCD;
384 dev_dbg(&port->dev, "%s - DCD low\n", __func__);
386 spin_unlock_irqrestore(&mxport->spinlock, flags);
389 (SERIAL_EV_CTS | SERIAL_EV_DSR | SERIAL_EV_RLSD)) {
391 if (rcv_msr_event & SERIAL_EV_CTS) {
393 dev_dbg(&port->dev, "%s - CTS change\n", __func__);
396 if (rcv_msr_event & SERIAL_EV_DSR) {
398 dev_dbg(&port->dev, "%s - DSR change\n", __func__);
401 if (rcv_msr_event & SERIAL_EV_RLSD) {
403 dev_dbg(&port->dev, "%s - DCD change\n", __func__);
405 wake_up_interruptible(&port->port.delta_msr_wait);
409 static void mxuport_lsr_event(struct usb_serial_port *port, u8 buf[4])
411 u8 lsr_event = buf[2];
413 if (lsr_event & UART_LSR_BI) {
415 dev_dbg(&port->dev, "%s - break error\n", __func__);
418 if (lsr_event & UART_LSR_FE) {
419 port->icount.frame++;
420 dev_dbg(&port->dev, "%s - frame error\n", __func__);
423 if (lsr_event & UART_LSR_PE) {
424 port->icount.parity++;
425 dev_dbg(&port->dev, "%s - parity error\n", __func__);
428 if (lsr_event & UART_LSR_OE) {
429 port->icount.overrun++;
430 dev_dbg(&port->dev, "%s - overrun error\n", __func__);
435 * When something interesting happens, modem control lines XON/XOFF
436 * etc, the device sends an event. Process these events.
438 static void mxuport_process_read_urb_event(struct usb_serial_port *port,
439 u8 buf[4], u32 event)
441 dev_dbg(&port->dev, "%s - receive event : %04x\n", __func__, event);
444 case UPORT_EVENT_SEND_NEXT:
446 * Sent as part of the flow control on device buffers.
447 * Not currently used.
450 case UPORT_EVENT_MSR:
451 mxuport_msr_event(port, buf);
453 case UPORT_EVENT_LSR:
454 mxuport_lsr_event(port, buf);
456 case UPORT_EVENT_MCR:
458 * Event to indicate a change in XON/XOFF from the
459 * peer. Currently not used. We just continue
460 * sending the device data and it will buffer it if
461 * needed. This event could be used for flow control
462 * between the host and the device.
466 dev_dbg(&port->dev, "Unexpected event\n");
472 * One URB can contain data for multiple ports. Demultiplex the data,
473 * checking the port exists, is opened and the message is valid.
475 static void mxuport_process_read_urb_demux_data(struct urb *urb)
477 struct usb_serial_port *port = urb->context;
478 struct usb_serial *serial = port->serial;
479 u8 *data = urb->transfer_buffer;
480 u8 *end = data + urb->actual_length;
481 struct usb_serial_port *demux_port;
487 if (data + HEADER_SIZE > end) {
488 dev_warn(&port->dev, "%s - message with short header\n",
493 rcv_port = get_unaligned_be16(data);
494 if (rcv_port >= serial->num_ports) {
495 dev_warn(&port->dev, "%s - message for invalid port\n",
500 demux_port = serial->port[rcv_port];
501 rcv_len = get_unaligned_be16(data + 2);
502 if (!rcv_len || data + HEADER_SIZE + rcv_len > end) {
503 dev_warn(&port->dev, "%s - short data\n", __func__);
507 if (test_bit(ASYNCB_INITIALIZED, &demux_port->port.flags)) {
508 ch = data + HEADER_SIZE;
509 mxuport_process_read_urb_data(demux_port, ch, rcv_len);
511 dev_dbg(&demux_port->dev, "%s - data for closed port\n",
514 data += HEADER_SIZE + rcv_len;
519 * One URB can contain events for multiple ports. Demultiplex the event,
520 * checking the port exists, and is opened.
522 static void mxuport_process_read_urb_demux_event(struct urb *urb)
524 struct usb_serial_port *port = urb->context;
525 struct usb_serial *serial = port->serial;
526 u8 *data = urb->transfer_buffer;
527 u8 *end = data + urb->actual_length;
528 struct usb_serial_port *demux_port;
534 if (data + EVENT_LENGTH > end) {
535 dev_warn(&port->dev, "%s - message with short event\n",
540 rcv_port = get_unaligned_be16(data);
541 if (rcv_port >= serial->num_ports) {
542 dev_warn(&port->dev, "%s - message for invalid port\n",
547 demux_port = serial->port[rcv_port];
548 if (test_bit(ASYNCB_INITIALIZED, &demux_port->port.flags)) {
549 ch = data + HEADER_SIZE;
550 rcv_event = get_unaligned_be16(data + 2);
551 mxuport_process_read_urb_event(demux_port, ch,
554 dev_dbg(&demux_port->dev,
555 "%s - event for closed port\n", __func__);
557 data += EVENT_LENGTH;
562 * This is called when we have received data on the bulk in
563 * endpoint. Depending on which port it was received on, it can
564 * contain serial data or events.
566 static void mxuport_process_read_urb(struct urb *urb)
568 struct usb_serial_port *port = urb->context;
569 struct usb_serial *serial = port->serial;
571 if (port == serial->port[0])
572 mxuport_process_read_urb_demux_data(urb);
574 if (port == serial->port[1])
575 mxuport_process_read_urb_demux_event(urb);
579 * Ask the device how many bytes it has queued to be sent out. If
580 * there are none, return true.
582 static bool mxuport_tx_empty(struct usb_serial_port *port)
584 struct usb_serial *serial = port->serial;
585 bool is_empty = true;
590 len_buf = kzalloc(4, GFP_KERNEL);
594 err = mxuport_recv_ctrl_urb(serial, RQ_VENDOR_GET_OUTQUEUE, 0,
595 port->port_number, len_buf, 4);
599 txlen = get_unaligned_be32(len_buf);
600 dev_dbg(&port->dev, "%s - tx len = %u\n", __func__, txlen);
610 static int mxuport_set_mcr(struct usb_serial_port *port, u8 mcr_state)
612 struct usb_serial *serial = port->serial;
615 dev_dbg(&port->dev, "%s - %02x\n", __func__, mcr_state);
617 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_MCR,
618 mcr_state, port->port_number);
620 dev_err(&port->dev, "%s - failed to change MCR\n", __func__);
625 static int mxuport_set_dtr(struct usb_serial_port *port, int on)
627 struct mxuport_port *mxport = usb_get_serial_port_data(port);
628 struct usb_serial *serial = port->serial;
631 mutex_lock(&mxport->mutex);
633 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_DTR,
634 !!on, port->port_number);
637 mxport->mcr_state |= UART_MCR_DTR;
639 mxport->mcr_state &= ~UART_MCR_DTR;
642 mutex_unlock(&mxport->mutex);
647 static int mxuport_set_rts(struct usb_serial_port *port, u8 state)
649 struct mxuport_port *mxport = usb_get_serial_port_data(port);
650 struct usb_serial *serial = port->serial;
654 mutex_lock(&mxport->mutex);
655 mcr_state = mxport->mcr_state;
659 mcr_state &= ~UART_MCR_RTS;
662 mcr_state |= UART_MCR_RTS;
666 * Do not update mxport->mcr_state when doing hardware
672 * Should not happen, but somebody might try passing
673 * MX_RTS_NO_CHANGE, which is not valid.
678 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RTS,
679 state, port->port_number);
681 mxport->mcr_state = mcr_state;
684 mutex_unlock(&mxport->mutex);
689 static void mxuport_dtr_rts(struct usb_serial_port *port, int on)
691 struct mxuport_port *mxport = usb_get_serial_port_data(port);
695 mutex_lock(&mxport->mutex);
696 mcr_state = mxport->mcr_state;
699 mcr_state |= (UART_MCR_RTS | UART_MCR_DTR);
701 mcr_state &= ~(UART_MCR_RTS | UART_MCR_DTR);
703 err = mxuport_set_mcr(port, mcr_state);
705 mxport->mcr_state = mcr_state;
707 mutex_unlock(&mxport->mutex);
710 static int mxuport_tiocmset(struct tty_struct *tty, unsigned int set,
713 struct usb_serial_port *port = tty->driver_data;
714 struct mxuport_port *mxport = usb_get_serial_port_data(port);
718 mutex_lock(&mxport->mutex);
719 mcr_state = mxport->mcr_state;
722 mcr_state |= UART_MCR_RTS;
725 mcr_state |= UART_MCR_DTR;
727 if (clear & TIOCM_RTS)
728 mcr_state &= ~UART_MCR_RTS;
730 if (clear & TIOCM_DTR)
731 mcr_state &= ~UART_MCR_DTR;
733 err = mxuport_set_mcr(port, mcr_state);
735 mxport->mcr_state = mcr_state;
737 mutex_unlock(&mxport->mutex);
742 static int mxuport_tiocmget(struct tty_struct *tty)
744 struct mxuport_port *mxport;
745 struct usb_serial_port *port = tty->driver_data;
751 mxport = usb_get_serial_port_data(port);
753 mutex_lock(&mxport->mutex);
754 spin_lock_irqsave(&mxport->spinlock, flags);
756 msr = mxport->msr_state;
757 mcr = mxport->mcr_state;
759 spin_unlock_irqrestore(&mxport->spinlock, flags);
760 mutex_unlock(&mxport->mutex);
762 result = (((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) | /* 0x002 */
763 ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) | /* 0x004 */
764 ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) | /* 0x020 */
765 ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) | /* 0x040 */
766 ((msr & UART_MSR_RI) ? TIOCM_RI : 0) | /* 0x080 */
767 ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0)); /* 0x100 */
769 dev_dbg(&port->dev, "%s - 0x%04x\n", __func__, result);
774 static int mxuport_set_termios_flow(struct tty_struct *tty,
775 struct ktermios *old_termios,
776 struct usb_serial_port *port,
777 struct usb_serial *serial)
779 u8 xon = START_CHAR(tty);
780 u8 xoff = STOP_CHAR(tty);
786 buf = kmalloc(2, GFP_KERNEL);
790 /* S/W flow control settings */
791 if (I_IXOFF(tty) || I_IXON(tty)) {
796 err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_CHARS,
797 0, port->port_number,
802 dev_dbg(&port->dev, "%s - XON = 0x%02x, XOFF = 0x%02x\n",
803 __func__, xon, xoff);
808 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_XONXOFF,
809 enable, port->port_number);
813 rts = MX_RTS_NO_CHANGE;
815 /* H/W flow control settings */
817 C_CRTSCTS(tty) != (old_termios->c_cflag & CRTSCTS)) {
825 if (old_termios && (old_termios->c_cflag & CBAUD) == B0) {
826 /* Raise DTR and RTS */
831 mxuport_set_dtr(port, 1);
834 /* Drop DTR and RTS */
835 rts = MX_RTS_DISABLE;
836 mxuport_set_dtr(port, 0);
839 if (rts != MX_RTS_NO_CHANGE)
840 err = mxuport_set_rts(port, rts);
847 static void mxuport_set_termios(struct tty_struct *tty,
848 struct usb_serial_port *port,
849 struct ktermios *old_termios)
851 struct usb_serial *serial = port->serial;
860 !tty_termios_hw_change(&tty->termios, old_termios) &&
861 tty->termios.c_iflag == old_termios->c_iflag) {
862 dev_dbg(&port->dev, "%s - nothing to change\n", __func__);
866 buf = kmalloc(4, GFP_KERNEL);
870 /* Set data bit of termios */
871 switch (C_CSIZE(tty)) {
873 data_bits = MX_WORDLENGTH_5;
876 data_bits = MX_WORDLENGTH_6;
879 data_bits = MX_WORDLENGTH_7;
883 data_bits = MX_WORDLENGTH_8;
887 /* Set parity of termios */
891 parity = MX_PARITY_MARK;
893 parity = MX_PARITY_SPACE;
896 parity = MX_PARITY_ODD;
898 parity = MX_PARITY_EVEN;
901 parity = MX_PARITY_NONE;
904 /* Set stop bit of termios */
906 stop_bits = MX_STOP_BITS_2;
908 stop_bits = MX_STOP_BITS_1;
915 err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_LINE,
916 0, port->port_number, buf, 4);
920 err = mxuport_set_termios_flow(tty, old_termios, port, serial);
924 baud = tty_get_baud_rate(tty);
928 /* Note: Little Endian */
929 put_unaligned_le32(baud, buf);
931 err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_BAUD,
932 0, port->port_number,
937 dev_dbg(&port->dev, "baud_rate : %d\n", baud);
938 dev_dbg(&port->dev, "data_bits : %d\n", data_bits);
939 dev_dbg(&port->dev, "parity : %d\n", parity);
940 dev_dbg(&port->dev, "stop_bits : %d\n", stop_bits);
947 * Determine how many ports this device has dynamically. It will be
948 * called after the probe() callback is called, but before attach().
950 static int mxuport_calc_num_ports(struct usb_serial *serial)
952 unsigned long features = (unsigned long)usb_get_serial_data(serial);
954 if (features & MX_UPORT_2_PORT)
956 if (features & MX_UPORT_4_PORT)
958 if (features & MX_UPORT_8_PORT)
960 if (features & MX_UPORT_16_PORT)
966 /* Get the version of the firmware currently running. */
967 static int mxuport_get_fw_version(struct usb_serial *serial, u32 *version)
972 ver_buf = kzalloc(4, GFP_KERNEL);
976 /* Get firmware version from SDRAM */
977 err = mxuport_recv_ctrl_urb(serial, RQ_VENDOR_GET_VERSION, 0, 0,
984 *version = (ver_buf[0] << 16) | (ver_buf[1] << 8) | ver_buf[2];
991 /* Given a firmware blob, download it to the device. */
992 static int mxuport_download_fw(struct usb_serial *serial,
993 const struct firmware *fw_p)
1000 fw_buf = kmalloc(DOWN_BLOCK_SIZE, GFP_KERNEL);
1004 dev_dbg(&serial->interface->dev, "Starting firmware download...\n");
1005 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_START_FW_DOWN, 0, 0);
1011 txlen = min_t(size_t, (fw_p->size - fwidx), DOWN_BLOCK_SIZE);
1013 memcpy(fw_buf, &fw_p->data[fwidx], txlen);
1014 err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_FW_DATA,
1015 0, 0, fw_buf, txlen);
1017 mxuport_send_ctrl_urb(serial, RQ_VENDOR_STOP_FW_DOWN,
1023 usleep_range(1000, 2000);
1025 } while (fwidx < fw_p->size);
1028 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_STOP_FW_DOWN, 0, 0);
1033 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_QUERY_FW_READY, 0, 0);
1040 static int mxuport_probe(struct usb_serial *serial,
1041 const struct usb_device_id *id)
1043 u16 productid = le16_to_cpu(serial->dev->descriptor.idProduct);
1044 const struct firmware *fw_p = NULL;
1050 /* Load our firmware */
1051 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_QUERY_FW_CONFIG, 0, 0);
1053 mxuport_send_ctrl_urb(serial, RQ_VENDOR_RESET_DEVICE, 0, 0);
1057 err = mxuport_get_fw_version(serial, &version);
1061 dev_dbg(&serial->interface->dev, "Device firmware version v%x.%x.%x\n",
1062 (version & 0xff0000) >> 16,
1063 (version & 0xff00) >> 8,
1066 snprintf(buf, sizeof(buf) - 1, "moxa/moxa-%04x.fw", productid);
1068 err = request_firmware(&fw_p, buf, &serial->interface->dev);
1070 dev_warn(&serial->interface->dev, "Firmware %s not found\n",
1073 /* Use the firmware already in the device */
1076 local_ver = ((fw_p->data[VER_ADDR_1] << 16) |
1077 (fw_p->data[VER_ADDR_2] << 8) |
1078 fw_p->data[VER_ADDR_3]);
1079 dev_dbg(&serial->interface->dev,
1080 "Available firmware version v%x.%x.%x\n",
1081 fw_p->data[VER_ADDR_1], fw_p->data[VER_ADDR_2],
1082 fw_p->data[VER_ADDR_3]);
1083 if (local_ver > version) {
1084 err = mxuport_download_fw(serial, fw_p);
1087 err = mxuport_get_fw_version(serial, &version);
1093 dev_info(&serial->interface->dev,
1094 "Using device firmware version v%x.%x.%x\n",
1095 (version & 0xff0000) >> 16,
1096 (version & 0xff00) >> 8,
1100 * Contains the features of this hardware. Store away for
1101 * later use, eg, number of ports.
1103 usb_set_serial_data(serial, (void *)id->driver_info);
1106 release_firmware(fw_p);
1111 static int mxuport_port_probe(struct usb_serial_port *port)
1113 struct usb_serial *serial = port->serial;
1114 struct mxuport_port *mxport;
1117 mxport = devm_kzalloc(&port->dev, sizeof(struct mxuport_port),
1122 mutex_init(&mxport->mutex);
1123 spin_lock_init(&mxport->spinlock);
1125 /* Set the port private data */
1126 usb_set_serial_port_data(port, mxport);
1128 /* Set FIFO (Enable) */
1129 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_FIFO_DISABLE,
1130 0, port->port_number);
1134 /* Set transmission mode (Hi-Performance) */
1135 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_HIGH_PERFOR,
1136 0, port->port_number);
1140 /* Set interface (RS-232) */
1141 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_INTERFACE,
1150 static int mxuport_alloc_write_urb(struct usb_serial *serial,
1151 struct usb_serial_port *port,
1152 struct usb_serial_port *port0,
1155 struct usb_device *dev = interface_to_usbdev(serial->interface);
1157 set_bit(j, &port->write_urbs_free);
1158 port->write_urbs[j] = usb_alloc_urb(0, GFP_KERNEL);
1159 if (!port->write_urbs[j])
1162 port->bulk_out_buffers[j] = kmalloc(port0->bulk_out_size, GFP_KERNEL);
1163 if (!port->bulk_out_buffers[j])
1166 usb_fill_bulk_urb(port->write_urbs[j], dev,
1167 usb_sndbulkpipe(dev, port->bulk_out_endpointAddress),
1168 port->bulk_out_buffers[j],
1169 port->bulk_out_size,
1170 serial->type->write_bulk_callback,
1176 static int mxuport_alloc_write_urbs(struct usb_serial *serial,
1177 struct usb_serial_port *port,
1178 struct usb_serial_port *port0)
1183 for (j = 0; j < ARRAY_SIZE(port->write_urbs); ++j) {
1184 ret = mxuport_alloc_write_urb(serial, port, port0, j);
1192 static int mxuport_attach(struct usb_serial *serial)
1194 struct usb_serial_port *port0 = serial->port[0];
1195 struct usb_serial_port *port1 = serial->port[1];
1196 struct usb_serial_port *port;
1202 * Throw away all but the first allocated write URBs so we can
1203 * set them up again to fit the multiplexing scheme.
1205 for (i = 1; i < serial->num_bulk_out; ++i) {
1206 port = serial->port[i];
1207 for (j = 0; j < ARRAY_SIZE(port->write_urbs); ++j) {
1208 usb_free_urb(port->write_urbs[j]);
1209 kfree(port->bulk_out_buffers[j]);
1210 port->write_urbs[j] = NULL;
1211 port->bulk_out_buffers[j] = NULL;
1213 port->write_urbs_free = 0;
1217 * All write data is sent over the first bulk out endpoint,
1218 * with an added header to indicate the port. Allocate URBs
1219 * for each port to the first bulk out endpoint.
1221 for (i = 1; i < serial->num_ports; ++i) {
1222 port = serial->port[i];
1223 port->bulk_out_size = port0->bulk_out_size;
1224 port->bulk_out_endpointAddress =
1225 port0->bulk_out_endpointAddress;
1227 err = mxuport_alloc_write_urbs(serial, port, port0);
1231 port->write_urb = port->write_urbs[0];
1232 port->bulk_out_buffer = port->bulk_out_buffers[0];
1235 * Ensure each port has a fifo. The framework only
1236 * allocates a fifo to ports with a bulk out endpoint,
1237 * where as we need one for every port.
1239 if (!kfifo_initialized(&port->write_fifo)) {
1240 err = kfifo_alloc(&port->write_fifo, PAGE_SIZE,
1248 * All data from the ports is received on the first bulk in
1249 * endpoint, with a multiplex header. The second bulk in is
1252 * Start to read from the device.
1254 err = usb_serial_generic_submit_read_urbs(port0, GFP_KERNEL);
1258 err = usb_serial_generic_submit_read_urbs(port1, GFP_KERNEL);
1260 usb_serial_generic_close(port0);
1267 static int mxuport_open(struct tty_struct *tty, struct usb_serial_port *port)
1269 struct mxuport_port *mxport = usb_get_serial_port_data(port);
1270 struct usb_serial *serial = port->serial;
1273 /* Set receive host (enable) */
1274 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
1275 1, port->port_number);
1279 err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_OPEN,
1280 1, port->port_number);
1282 mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
1283 0, port->port_number);
1287 /* Initial port termios */
1288 mxuport_set_termios(tty, port, NULL);
1291 * TODO: use RQ_VENDOR_GET_MSR, once we know what it
1294 mxport->msr_state = 0;
1299 static void mxuport_close(struct usb_serial_port *port)
1301 struct usb_serial *serial = port->serial;
1303 mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_OPEN, 0,
1306 mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN, 0,
1310 /* Send a break to the port. */
1311 static void mxuport_break_ctl(struct tty_struct *tty, int break_state)
1313 struct usb_serial_port *port = tty->driver_data;
1314 struct usb_serial *serial = port->serial;
1317 if (break_state == -1) {
1319 dev_dbg(&port->dev, "%s - sending break\n", __func__);
1322 dev_dbg(&port->dev, "%s - clearing break\n", __func__);
1325 mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_BREAK,
1326 enable, port->port_number);
1329 static int mxuport_resume(struct usb_serial *serial)
1331 struct usb_serial_port *port;
1336 for (i = 0; i < 2; i++) {
1337 port = serial->port[i];
1339 r = usb_serial_generic_submit_read_urbs(port, GFP_NOIO);
1344 for (i = 0; i < serial->num_ports; i++) {
1345 port = serial->port[i];
1346 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags))
1349 r = usb_serial_generic_write_start(port, GFP_NOIO);
1354 return c ? -EIO : 0;
1357 static struct usb_serial_driver mxuport_device = {
1359 .owner = THIS_MODULE,
1362 .description = "MOXA UPort",
1363 .id_table = mxuport_idtable,
1365 .probe = mxuport_probe,
1366 .port_probe = mxuport_port_probe,
1367 .attach = mxuport_attach,
1368 .calc_num_ports = mxuport_calc_num_ports,
1369 .open = mxuport_open,
1370 .close = mxuport_close,
1371 .set_termios = mxuport_set_termios,
1372 .break_ctl = mxuport_break_ctl,
1373 .tx_empty = mxuport_tx_empty,
1374 .tiocmiwait = usb_serial_generic_tiocmiwait,
1375 .get_icount = usb_serial_generic_get_icount,
1376 .throttle = mxuport_throttle,
1377 .unthrottle = mxuport_unthrottle,
1378 .tiocmget = mxuport_tiocmget,
1379 .tiocmset = mxuport_tiocmset,
1380 .dtr_rts = mxuport_dtr_rts,
1381 .process_read_urb = mxuport_process_read_urb,
1382 .prepare_write_buffer = mxuport_prepare_write_buffer,
1383 .resume = mxuport_resume,
1386 static struct usb_serial_driver *const serial_drivers[] = {
1387 &mxuport_device, NULL
1390 module_usb_serial_driver(serial_drivers, mxuport_idtable);
1392 MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
1393 MODULE_AUTHOR("<support@moxa.com>");
1394 MODULE_LICENSE("GPL");