| 1 |
// SPDX-License-Identifier: GPL-2.0+ |
| 2 |
/* |
| 3 |
* mxuport.c - MOXA UPort series driver |
| 4 |
* |
| 5 |
* Copyright (c) 2006 Moxa Technologies Co., Ltd. |
| 6 |
* Copyright (c) 2013 Andrew Lunn <[email protected]> |
| 7 |
* |
| 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 |
| 13 |
*/ |
| 14 |
|
| 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 <linux/unaligned.h> |
| 29 |
|
| 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 |
| 41 |
|
| 42 |
/* Definitions for USB info */ |
| 43 |
#define HEADER_SIZE 4 |
| 44 |
#define EVENT_LENGTH 8 |
| 45 |
#define DOWN_BLOCK_SIZE 64 |
| 46 |
|
| 47 |
/* Definitions for firmware info */ |
| 48 |
#define VER_ADDR_1 0x20 |
| 49 |
#define VER_ADDR_2 0x24 |
| 50 |
#define VER_ADDR_3 0x28 |
| 51 |
|
| 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 */ |
| 65 |
|
| 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 */ |
| 69 |
|
| 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 */ |
| 73 |
|
| 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 */ |
| 79 |
|
| 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 */ |
| 84 |
|
| 85 |
#define RQ_VENDOR_RESET_DEVICE 0x23 /* Try to reset the device */ |
| 86 |
#define RQ_VENDOR_QUERY_FW_CONFIG 0x24 |
| 87 |
|
| 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 */ |
| 91 |
|
| 92 |
#define RQ_VENDOR_GET_INQUEUE 0x84 /* Data in input buffer */ |
| 93 |
#define RQ_VENDOR_GET_OUTQUEUE 0x85 /* Data in output buffer */ |
| 94 |
|
| 95 |
#define RQ_VENDOR_GET_MSR 0x86 /* Get modem status register */ |
| 96 |
|
| 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 */ |
| 104 |
|
| 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 */ |
| 109 |
|
| 110 |
/* Definitions for modem control event type */ |
| 111 |
#define SERIAL_EV_XOFF 0x40 /* XOFF received */ |
| 112 |
|
| 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 |
| 118 |
|
| 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 |
| 124 |
|
| 125 |
#define MX_STOP_BITS_1 0 |
| 126 |
#define MX_STOP_BITS_1_5 1 |
| 127 |
#define MX_STOP_BITS_2 2 |
| 128 |
|
| 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*/ |
| 133 |
|
| 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 |
| 138 |
|
| 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 |
| 148 |
|
| 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) |
| 153 |
|
| 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 */ |
| 160 |
}; |
| 161 |
|
| 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 */ |
| 183 |
}; |
| 184 |
|
| 185 |
MODULE_DEVICE_TABLE(usb, mxuport_idtable); |
| 186 |
|
| 187 |
/* |
| 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 |
| 190 |
* buffer. |
| 191 |
*/ |
| 192 |
static int mxuport_prepare_write_buffer(struct usb_serial_port *port, |
| 193 |
void *dest, size_t size) |
| 194 |
{ |
| 195 |
u8 *buf = dest; |
| 196 |
int count; |
| 197 |
|
| 198 |
count = kfifo_out_locked(&port->write_fifo, buf + HEADER_SIZE, |
| 199 |
size - HEADER_SIZE, |
| 200 |
&port->lock); |
| 201 |
|
| 202 |
put_unaligned_be16(port->port_number, buf); |
| 203 |
put_unaligned_be16(count, buf + 2); |
| 204 |
|
| 205 |
dev_dbg(&port->dev, "%s - size %zd count %d\n", __func__, |
| 206 |
size, count); |
| 207 |
|
| 208 |
return count + HEADER_SIZE; |
| 209 |
} |
| 210 |
|
| 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) |
| 215 |
{ |
| 216 |
int status; |
| 217 |
|
| 218 |
status = usb_control_msg(serial->dev, |
| 219 |
usb_rcvctrlpipe(serial->dev, 0), |
| 220 |
request, |
| 221 |
(USB_DIR_IN | USB_TYPE_VENDOR | |
| 222 |
USB_RECIP_DEVICE), value, index, |
| 223 |
data, size, |
| 224 |
USB_CTRL_GET_TIMEOUT); |
| 225 |
if (status < 0) { |
| 226 |
dev_err(&serial->interface->dev, |
| 227 |
"%s - usb_control_msg failed (%d)\n", |
| 228 |
__func__, status); |
| 229 |
return status; |
| 230 |
} |
| 231 |
|
| 232 |
if (status != size) { |
| 233 |
dev_err(&serial->interface->dev, |
| 234 |
"%s - short read (%d / %zd)\n", |
| 235 |
__func__, status, size); |
| 236 |
return -EIO; |
| 237 |
} |
| 238 |
|
| 239 |
return status; |
| 240 |
} |
| 241 |
|
| 242 |
/* Write the given buffer out to the control pipe. */ |
| 243 |
static int mxuport_send_ctrl_data_urb(struct usb_serial *serial, |
| 244 |
u8 request, |
| 245 |
u16 value, u16 index, |
| 246 |
u8 *data, size_t size) |
| 247 |
{ |
| 248 |
int status; |
| 249 |
|
| 250 |
status = usb_control_msg(serial->dev, |
| 251 |
usb_sndctrlpipe(serial->dev, 0), |
| 252 |
request, |
| 253 |
(USB_DIR_OUT | USB_TYPE_VENDOR | |
| 254 |
USB_RECIP_DEVICE), value, index, |
| 255 |
data, size, |
| 256 |
USB_CTRL_SET_TIMEOUT); |
| 257 |
if (status < 0) { |
| 258 |
dev_err(&serial->interface->dev, |
| 259 |
"%s - usb_control_msg failed (%d)\n", |
| 260 |
__func__, status); |
| 261 |
return status; |
| 262 |
} |
| 263 |
|
| 264 |
return 0; |
| 265 |
} |
| 266 |
|
| 267 |
/* Send a vendor request without any data */ |
| 268 |
static int mxuport_send_ctrl_urb(struct usb_serial *serial, |
| 269 |
u8 request, u16 value, u16 index) |
| 270 |
{ |
| 271 |
return mxuport_send_ctrl_data_urb(serial, request, value, index, |
| 272 |
NULL, 0); |
| 273 |
} |
| 274 |
|
| 275 |
/* |
| 276 |
* mxuport_throttle - throttle function of driver |
| 277 |
* |
| 278 |
* This function is called by the tty driver when it wants to stop the |
| 279 |
* data being read from the port. Since all the data comes over one |
| 280 |
* bulk in endpoint, we cannot stop submitting urbs by setting |
| 281 |
* port->throttle. Instead tell the device to stop sending us data for |
| 282 |
* the port. |
| 283 |
*/ |
| 284 |
static void mxuport_throttle(struct tty_struct *tty) |
| 285 |
{ |
| 286 |
struct usb_serial_port *port = tty->driver_data; |
| 287 |
struct usb_serial *serial = port->serial; |
| 288 |
|
| 289 |
dev_dbg(&port->dev, "%s\n", __func__); |
| 290 |
|
| 291 |
mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN, |
| 292 |
0, port->port_number); |
| 293 |
} |
| 294 |
|
| 295 |
/* |
| 296 |
* mxuport_unthrottle - unthrottle function of driver |
| 297 |
* |
| 298 |
* This function is called by the tty driver when it wants to resume |
| 299 |
* the data being read from the port. Tell the device it can resume |
| 300 |
* sending us received data from the port. |
| 301 |
*/ |
| 302 |
static void mxuport_unthrottle(struct tty_struct *tty) |
| 303 |
{ |
| 304 |
|
| 305 |
struct usb_serial_port *port = tty->driver_data; |
| 306 |
struct usb_serial *serial = port->serial; |
| 307 |
|
| 308 |
dev_dbg(&port->dev, "%s\n", __func__); |
| 309 |
|
| 310 |
mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN, |
| 311 |
1, port->port_number); |
| 312 |
} |
| 313 |
|
| 314 |
/* |
| 315 |
* Processes one chunk of data received for a port. Mostly a copy of |
| 316 |
* usb_serial_generic_process_read_urb(). |
| 317 |
*/ |
| 318 |
static void mxuport_process_read_urb_data(struct usb_serial_port *port, |
| 319 |
char *data, int size) |
| 320 |
{ |
| 321 |
int i; |
| 322 |
|
| 323 |
if (port->sysrq) { |
| 324 |
for (i = 0; i < size; i++, data++) { |
| 325 |
if (!usb_serial_handle_sysrq_char(port, *data)) |
| 326 |
tty_insert_flip_char(&port->port, *data, |
| 327 |
TTY_NORMAL); |
| 328 |
} |
| 329 |
} else { |
| 330 |
tty_insert_flip_string(&port->port, data, size); |
| 331 |
} |
| 332 |
tty_flip_buffer_push(&port->port); |
| 333 |
} |
| 334 |
|
| 335 |
static void mxuport_msr_event(struct usb_serial_port *port, u8 buf[4]) |
| 336 |
{ |
| 337 |
struct mxuport_port *mxport = usb_get_serial_port_data(port); |
| 338 |
u8 rcv_msr_hold = buf[2] & 0xF0; |
| 339 |
u16 rcv_msr_event = get_unaligned_be16(buf); |
| 340 |
unsigned long flags; |
| 341 |
|
| 342 |
if (rcv_msr_event == 0) |
| 343 |
return; |
| 344 |
|
| 345 |
/* Update MSR status */ |
| 346 |
spin_lock_irqsave(&mxport->spinlock, flags); |
| 347 |
|
| 348 |
dev_dbg(&port->dev, "%s - current MSR status = 0x%x\n", |
| 349 |
__func__, mxport->msr_state); |
| 350 |
|
| 351 |
if (rcv_msr_hold & UART_MSR_CTS) { |
| 352 |
mxport->msr_state |= UART_MSR_CTS; |
| 353 |
dev_dbg(&port->dev, "%s - CTS high\n", __func__); |
| 354 |
} else { |
| 355 |
mxport->msr_state &= ~UART_MSR_CTS; |
| 356 |
dev_dbg(&port->dev, "%s - CTS low\n", __func__); |
| 357 |
} |
| 358 |
|
| 359 |
if (rcv_msr_hold & UART_MSR_DSR) { |
| 360 |
mxport->msr_state |= UART_MSR_DSR; |
| 361 |
dev_dbg(&port->dev, "%s - DSR high\n", __func__); |
| 362 |
} else { |
| 363 |
mxport->msr_state &= ~UART_MSR_DSR; |
| 364 |
dev_dbg(&port->dev, "%s - DSR low\n", __func__); |
| 365 |
} |
| 366 |
|
| 367 |
if (rcv_msr_hold & UART_MSR_DCD) { |
| 368 |
mxport->msr_state |= UART_MSR_DCD; |
| 369 |
dev_dbg(&port->dev, "%s - DCD high\n", __func__); |
| 370 |
} else { |
| 371 |
mxport->msr_state &= ~UART_MSR_DCD; |
| 372 |
dev_dbg(&port->dev, "%s - DCD low\n", __func__); |
| 373 |
} |
| 374 |
spin_unlock_irqrestore(&mxport->spinlock, flags); |
| 375 |
|
| 376 |
if (rcv_msr_event & |
| 377 |
(SERIAL_EV_CTS | SERIAL_EV_DSR | SERIAL_EV_RLSD)) { |
| 378 |
|
| 379 |
if (rcv_msr_event & SERIAL_EV_CTS) { |
| 380 |
port->icount.cts++; |
| 381 |
dev_dbg(&port->dev, "%s - CTS change\n", __func__); |
| 382 |
} |
| 383 |
|
| 384 |
if (rcv_msr_event & SERIAL_EV_DSR) { |
| 385 |
port->icount.dsr++; |
| 386 |
dev_dbg(&port->dev, "%s - DSR change\n", __func__); |
| 387 |
} |
| 388 |
|
| 389 |
if (rcv_msr_event & SERIAL_EV_RLSD) { |
| 390 |
port->icount.dcd++; |
| 391 |
dev_dbg(&port->dev, "%s - DCD change\n", __func__); |
| 392 |
} |
| 393 |
wake_up_interruptible(&port->port.delta_msr_wait); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
static void mxuport_lsr_event(struct usb_serial_port *port, u8 buf[4]) |
| 398 |
{ |
| 399 |
u8 lsr_event = buf[2]; |
| 400 |
|
| 401 |
if (lsr_event & UART_LSR_BI) { |
| 402 |
port->icount.brk++; |
| 403 |
dev_dbg(&port->dev, "%s - break error\n", __func__); |
| 404 |
} |
| 405 |
|
| 406 |
if (lsr_event & UART_LSR_FE) { |
| 407 |
port->icount.frame++; |
| 408 |
dev_dbg(&port->dev, "%s - frame error\n", __func__); |
| 409 |
} |
| 410 |
|
| 411 |
if (lsr_event & UART_LSR_PE) { |
| 412 |
port->icount.parity++; |
| 413 |
dev_dbg(&port->dev, "%s - parity error\n", __func__); |
| 414 |
} |
| 415 |
|
| 416 |
if (lsr_event & UART_LSR_OE) { |
| 417 |
port->icount.overrun++; |
| 418 |
dev_dbg(&port->dev, "%s - overrun error\n", __func__); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
/* |
| 423 |
* When something interesting happens, modem control lines XON/XOFF |
| 424 |
* etc, the device sends an event. Process these events. |
| 425 |
*/ |
| 426 |
static void mxuport_process_read_urb_event(struct usb_serial_port *port, |
| 427 |
u8 buf[4], u32 event) |
| 428 |
{ |
| 429 |
dev_dbg(&port->dev, "%s - receive event : %04x\n", __func__, event); |
| 430 |
|
| 431 |
switch (event) { |
| 432 |
case UPORT_EVENT_SEND_NEXT: |
| 433 |
/* |
| 434 |
* Sent as part of the flow control on device buffers. |
| 435 |
* Not currently used. |
| 436 |
*/ |
| 437 |
break; |
| 438 |
case UPORT_EVENT_MSR: |
| 439 |
mxuport_msr_event(port, buf); |
| 440 |
break; |
| 441 |
case UPORT_EVENT_LSR: |
| 442 |
mxuport_lsr_event(port, buf); |
| 443 |
break; |
| 444 |
case UPORT_EVENT_MCR: |
| 445 |
/* |
| 446 |
* Event to indicate a change in XON/XOFF from the |
| 447 |
* peer. Currently not used. We just continue |
| 448 |
* sending the device data and it will buffer it if |
| 449 |
* needed. This event could be used for flow control |
| 450 |
* between the host and the device. |
| 451 |
*/ |
| 452 |
break; |
| 453 |
default: |
| 454 |
dev_dbg(&port->dev, "Unexpected event\n"); |
| 455 |
break; |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
/* |
| 460 |
* One URB can contain data for multiple ports. Demultiplex the data, |
| 461 |
* checking the port exists, is opened and the message is valid. |
| 462 |
*/ |
| 463 |
static void mxuport_process_read_urb_demux_data(struct urb *urb) |
| 464 |
{ |
| 465 |
struct usb_serial_port *port = urb->context; |
| 466 |
struct usb_serial *serial = port->serial; |
| 467 |
u8 *data = urb->transfer_buffer; |
| 468 |
u8 *end = data + urb->actual_length; |
| 469 |
struct usb_serial_port *demux_port; |
| 470 |
u8 *ch; |
| 471 |
u16 rcv_port; |
| 472 |
u16 rcv_len; |
| 473 |
|
| 474 |
while (data < end) { |
| 475 |
if (data + HEADER_SIZE > end) { |
| 476 |
dev_warn(&port->dev, "%s - message with short header\n", |
| 477 |
__func__); |
| 478 |
return; |
| 479 |
} |
| 480 |
|
| 481 |
rcv_port = get_unaligned_be16(data); |
| 482 |
if (rcv_port >= serial->num_ports) { |
| 483 |
dev_warn(&port->dev, "%s - message for invalid port\n", |
| 484 |
__func__); |
| 485 |
return; |
| 486 |
} |
| 487 |
|
| 488 |
demux_port = serial->port[rcv_port]; |
| 489 |
rcv_len = get_unaligned_be16(data + 2); |
| 490 |
if (!rcv_len || data + HEADER_SIZE + rcv_len > end) { |
| 491 |
dev_warn(&port->dev, "%s - short data\n", __func__); |
| 492 |
return; |
| 493 |
} |
| 494 |
|
| 495 |
if (tty_port_initialized(&demux_port->port)) { |
| 496 |
ch = data + HEADER_SIZE; |
| 497 |
mxuport_process_read_urb_data(demux_port, ch, rcv_len); |
| 498 |
} else { |
| 499 |
dev_dbg(&demux_port->dev, "%s - data for closed port\n", |
| 500 |
__func__); |
| 501 |
} |
| 502 |
data += HEADER_SIZE + rcv_len; |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
/* |
| 507 |
* One URB can contain events for multiple ports. Demultiplex the event, |
| 508 |
* checking the port exists, and is opened. |
| 509 |
*/ |
| 510 |
static void mxuport_process_read_urb_demux_event(struct urb *urb) |
| 511 |
{ |
| 512 |
struct usb_serial_port *port = urb->context; |
| 513 |
struct usb_serial *serial = port->serial; |
| 514 |
u8 *data = urb->transfer_buffer; |
| 515 |
u8 *end = data + urb->actual_length; |
| 516 |
struct usb_serial_port *demux_port; |
| 517 |
u8 *ch; |
| 518 |
u16 rcv_port; |
| 519 |
u16 rcv_event; |
| 520 |
|
| 521 |
while (data < end) { |
| 522 |
if (data + EVENT_LENGTH > end) { |
| 523 |
dev_warn(&port->dev, "%s - message with short event\n", |
| 524 |
__func__); |
| 525 |
return; |
| 526 |
} |
| 527 |
|
| 528 |
rcv_port = get_unaligned_be16(data); |
| 529 |
if (rcv_port >= serial->num_ports) { |
| 530 |
dev_warn(&port->dev, "%s - message for invalid port\n", |
| 531 |
__func__); |
| 532 |
return; |
| 533 |
} |
| 534 |
|
| 535 |
demux_port = serial->port[rcv_port]; |
| 536 |
if (tty_port_initialized(&demux_port->port)) { |
| 537 |
ch = data + HEADER_SIZE; |
| 538 |
rcv_event = get_unaligned_be16(data + 2); |
| 539 |
mxuport_process_read_urb_event(demux_port, ch, |
| 540 |
rcv_event); |
| 541 |
} else { |
| 542 |
dev_dbg(&demux_port->dev, |
| 543 |
"%s - event for closed port\n", __func__); |
| 544 |
} |
| 545 |
data += EVENT_LENGTH; |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
/* |
| 550 |
* This is called when we have received data on the bulk in |
| 551 |
* endpoint. Depending on which port it was received on, it can |
| 552 |
* contain serial data or events. |
| 553 |
*/ |
| 554 |
static void mxuport_process_read_urb(struct urb *urb) |
| 555 |
{ |
| 556 |
struct usb_serial_port *port = urb->context; |
| 557 |
struct usb_serial *serial = port->serial; |
| 558 |
|
| 559 |
if (port == serial->port[0]) |
| 560 |
mxuport_process_read_urb_demux_data(urb); |
| 561 |
|
| 562 |
if (port == serial->port[1]) |
| 563 |
mxuport_process_read_urb_demux_event(urb); |
| 564 |
} |
| 565 |
|
| 566 |
/* |
| 567 |
* Ask the device how many bytes it has queued to be sent out. If |
| 568 |
* there are none, return true. |
| 569 |
*/ |
| 570 |
static bool mxuport_tx_empty(struct usb_serial_port *port) |
| 571 |
{ |
| 572 |
struct usb_serial *serial = port->serial; |
| 573 |
bool is_empty = true; |
| 574 |
u32 txlen; |
| 575 |
u8 *len_buf; |
| 576 |
int err; |
| 577 |
|
| 578 |
len_buf = kzalloc(4, GFP_KERNEL); |
| 579 |
if (!len_buf) |
| 580 |
goto out; |
| 581 |
|
| 582 |
err = mxuport_recv_ctrl_urb(serial, RQ_VENDOR_GET_OUTQUEUE, 0, |
| 583 |
port->port_number, len_buf, 4); |
| 584 |
if (err < 0) |
| 585 |
goto out; |
| 586 |
|
| 587 |
txlen = get_unaligned_be32(len_buf); |
| 588 |
dev_dbg(&port->dev, "%s - tx len = %u\n", __func__, txlen); |
| 589 |
|
| 590 |
if (txlen != 0) |
| 591 |
is_empty = false; |
| 592 |
|
| 593 |
out: |
| 594 |
kfree(len_buf); |
| 595 |
return is_empty; |
| 596 |
} |
| 597 |
|
| 598 |
static int mxuport_set_mcr(struct usb_serial_port *port, u8 mcr_state) |
| 599 |
{ |
| 600 |
struct usb_serial *serial = port->serial; |
| 601 |
int err; |
| 602 |
|
| 603 |
dev_dbg(&port->dev, "%s - %02x\n", __func__, mcr_state); |
| 604 |
|
| 605 |
err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_MCR, |
| 606 |
mcr_state, port->port_number); |
| 607 |
if (err) |
| 608 |
dev_err(&port->dev, "%s - failed to change MCR\n", __func__); |
| 609 |
|
| 610 |
return err; |
| 611 |
} |
| 612 |
|
| 613 |
static int mxuport_set_dtr(struct usb_serial_port *port, int on) |
| 614 |
{ |
| 615 |
struct mxuport_port *mxport = usb_get_serial_port_data(port); |
| 616 |
struct usb_serial *serial = port->serial; |
| 617 |
int err; |
| 618 |
|
| 619 |
mutex_lock(&mxport->mutex); |
| 620 |
|
| 621 |
err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_DTR, |
| 622 |
!!on, port->port_number); |
| 623 |
if (!err) { |
| 624 |
if (on) |
| 625 |
mxport->mcr_state |= UART_MCR_DTR; |
| 626 |
else |
| 627 |
mxport->mcr_state &= ~UART_MCR_DTR; |
| 628 |
} |
| 629 |
|
| 630 |
mutex_unlock(&mxport->mutex); |
| 631 |
|
| 632 |
return err; |
| 633 |
} |
| 634 |
|
| 635 |
static int mxuport_set_rts(struct usb_serial_port *port, u8 state) |
| 636 |
{ |
| 637 |
struct mxuport_port *mxport = usb_get_serial_port_data(port); |
| 638 |
struct usb_serial *serial = port->serial; |
| 639 |
int err; |
| 640 |
u8 mcr_state; |
| 641 |
|
| 642 |
mutex_lock(&mxport->mutex); |
| 643 |
mcr_state = mxport->mcr_state; |
| 644 |
|
| 645 |
switch (state) { |
| 646 |
case MX_RTS_DISABLE: |
| 647 |
mcr_state &= ~UART_MCR_RTS; |
| 648 |
break; |
| 649 |
case MX_RTS_ENABLE: |
| 650 |
mcr_state |= UART_MCR_RTS; |
| 651 |
break; |
| 652 |
case MX_RTS_HW: |
| 653 |
/* |
| 654 |
* Do not update mxport->mcr_state when doing hardware |
| 655 |
* flow control. |
| 656 |
*/ |
| 657 |
break; |
| 658 |
default: |
| 659 |
/* |
| 660 |
* Should not happen, but somebody might try passing |
| 661 |
* MX_RTS_NO_CHANGE, which is not valid. |
| 662 |
*/ |
| 663 |
err = -EINVAL; |
| 664 |
goto out; |
| 665 |
} |
| 666 |
err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RTS, |
| 667 |
state, port->port_number); |
| 668 |
if (!err) |
| 669 |
mxport->mcr_state = mcr_state; |
| 670 |
|
| 671 |
out: |
| 672 |
mutex_unlock(&mxport->mutex); |
| 673 |
|
| 674 |
return err; |
| 675 |
} |
| 676 |
|
| 677 |
static void mxuport_dtr_rts(struct usb_serial_port *port, int on) |
| 678 |
{ |
| 679 |
struct mxuport_port *mxport = usb_get_serial_port_data(port); |
| 680 |
u8 mcr_state; |
| 681 |
int err; |
| 682 |
|
| 683 |
mutex_lock(&mxport->mutex); |
| 684 |
mcr_state = mxport->mcr_state; |
| 685 |
|
| 686 |
if (on) |
| 687 |
mcr_state |= (UART_MCR_RTS | UART_MCR_DTR); |
| 688 |
else |
| 689 |
mcr_state &= ~(UART_MCR_RTS | UART_MCR_DTR); |
| 690 |
|
| 691 |
err = mxuport_set_mcr(port, mcr_state); |
| 692 |
if (!err) |
| 693 |
mxport->mcr_state = mcr_state; |
| 694 |
|
| 695 |
mutex_unlock(&mxport->mutex); |
| 696 |
} |
| 697 |
|
| 698 |
static int mxuport_tiocmset(struct tty_struct *tty, unsigned int set, |
| 699 |
unsigned int clear) |
| 700 |
{ |
| 701 |
struct usb_serial_port *port = tty->driver_data; |
| 702 |
struct mxuport_port *mxport = usb_get_serial_port_data(port); |
| 703 |
int err; |
| 704 |
u8 mcr_state; |
| 705 |
|
| 706 |
mutex_lock(&mxport->mutex); |
| 707 |
mcr_state = mxport->mcr_state; |
| 708 |
|
| 709 |
if (set & TIOCM_RTS) |
| 710 |
mcr_state |= UART_MCR_RTS; |
| 711 |
|
| 712 |
if (set & TIOCM_DTR) |
| 713 |
mcr_state |= UART_MCR_DTR; |
| 714 |
|
| 715 |
if (clear & TIOCM_RTS) |
| 716 |
mcr_state &= ~UART_MCR_RTS; |
| 717 |
|
| 718 |
if (clear & TIOCM_DTR) |
| 719 |
mcr_state &= ~UART_MCR_DTR; |
| 720 |
|
| 721 |
err = mxuport_set_mcr(port, mcr_state); |
| 722 |
if (!err) |
| 723 |
mxport->mcr_state = mcr_state; |
| 724 |
|
| 725 |
mutex_unlock(&mxport->mutex); |
| 726 |
|
| 727 |
return err; |
| 728 |
} |
| 729 |
|
| 730 |
static int mxuport_tiocmget(struct tty_struct *tty) |
| 731 |
{ |
| 732 |
struct mxuport_port *mxport; |
| 733 |
struct usb_serial_port *port = tty->driver_data; |
| 734 |
unsigned int result; |
| 735 |
unsigned long flags; |
| 736 |
unsigned int msr; |
| 737 |
unsigned int mcr; |
| 738 |
|
| 739 |
mxport = usb_get_serial_port_data(port); |
| 740 |
|
| 741 |
mutex_lock(&mxport->mutex); |
| 742 |
spin_lock_irqsave(&mxport->spinlock, flags); |
| 743 |
|
| 744 |
msr = mxport->msr_state; |
| 745 |
mcr = mxport->mcr_state; |
| 746 |
|
| 747 |
spin_unlock_irqrestore(&mxport->spinlock, flags); |
| 748 |
mutex_unlock(&mxport->mutex); |
| 749 |
|
| 750 |
result = (((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) | /* 0x002 */ |
| 751 |
((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) | /* 0x004 */ |
| 752 |
((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) | /* 0x020 */ |
| 753 |
((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) | /* 0x040 */ |
| 754 |
((msr & UART_MSR_RI) ? TIOCM_RI : 0) | /* 0x080 */ |
| 755 |
((msr & UART_MSR_DSR) ? TIOCM_DSR : 0)); /* 0x100 */ |
| 756 |
|
| 757 |
dev_dbg(&port->dev, "%s - 0x%04x\n", __func__, result); |
| 758 |
|
| 759 |
return result; |
| 760 |
} |
| 761 |
|
| 762 |
static int mxuport_set_termios_flow(struct tty_struct *tty, |
| 763 |
const struct ktermios *old_termios, |
| 764 |
struct usb_serial_port *port, |
| 765 |
struct usb_serial *serial) |
| 766 |
{ |
| 767 |
u8 xon = START_CHAR(tty); |
| 768 |
u8 xoff = STOP_CHAR(tty); |
| 769 |
int enable; |
| 770 |
int err; |
| 771 |
u8 *buf; |
| 772 |
u8 rts; |
| 773 |
|
| 774 |
buf = kmalloc(2, GFP_KERNEL); |
| 775 |
if (!buf) |
| 776 |
return -ENOMEM; |
| 777 |
|
| 778 |
/* S/W flow control settings */ |
| 779 |
if (I_IXOFF(tty) || I_IXON(tty)) { |
| 780 |
enable = 1; |
| 781 |
buf[0] = xon; |
| 782 |
buf[1] = xoff; |
| 783 |
|
| 784 |
err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_CHARS, |
| 785 |
0, port->port_number, |
| 786 |
buf, 2); |
| 787 |
if (err) |
| 788 |
goto out; |
| 789 |
|
| 790 |
dev_dbg(&port->dev, "%s - XON = 0x%02x, XOFF = 0x%02x\n", |
| 791 |
__func__, xon, xoff); |
| 792 |
} else { |
| 793 |
enable = 0; |
| 794 |
} |
| 795 |
|
| 796 |
err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_XONXOFF, |
| 797 |
enable, port->port_number); |
| 798 |
if (err) |
| 799 |
goto out; |
| 800 |
|
| 801 |
rts = MX_RTS_NO_CHANGE; |
| 802 |
|
| 803 |
/* H/W flow control settings */ |
| 804 |
if (!old_termios || |
| 805 |
C_CRTSCTS(tty) != (old_termios->c_cflag & CRTSCTS)) { |
| 806 |
if (C_CRTSCTS(tty)) |
| 807 |
rts = MX_RTS_HW; |
| 808 |
else |
| 809 |
rts = MX_RTS_ENABLE; |
| 810 |
} |
| 811 |
|
| 812 |
if (C_BAUD(tty)) { |
| 813 |
if (old_termios && (old_termios->c_cflag & CBAUD) == B0) { |
| 814 |
/* Raise DTR and RTS */ |
| 815 |
if (C_CRTSCTS(tty)) |
| 816 |
rts = MX_RTS_HW; |
| 817 |
else |
| 818 |
rts = MX_RTS_ENABLE; |
| 819 |
mxuport_set_dtr(port, 1); |
| 820 |
} |
| 821 |
} else { |
| 822 |
/* Drop DTR and RTS */ |
| 823 |
rts = MX_RTS_DISABLE; |
| 824 |
mxuport_set_dtr(port, 0); |
| 825 |
} |
| 826 |
|
| 827 |
if (rts != MX_RTS_NO_CHANGE) |
| 828 |
err = mxuport_set_rts(port, rts); |
| 829 |
|
| 830 |
out: |
| 831 |
kfree(buf); |
| 832 |
return err; |
| 833 |
} |
| 834 |
|
| 835 |
static void mxuport_set_termios(struct tty_struct *tty, |
| 836 |
struct usb_serial_port *port, |
| 837 |
const struct ktermios *old_termios) |
| 838 |
{ |
| 839 |
struct usb_serial *serial = port->serial; |
| 840 |
u8 *buf; |
| 841 |
u8 data_bits; |
| 842 |
u8 stop_bits; |
| 843 |
u8 parity; |
| 844 |
int baud; |
| 845 |
int err; |
| 846 |
|
| 847 |
if (old_termios && |
| 848 |
!tty_termios_hw_change(&tty->termios, old_termios) && |
| 849 |
tty->termios.c_iflag == old_termios->c_iflag) { |
| 850 |
dev_dbg(&port->dev, "%s - nothing to change\n", __func__); |
| 851 |
return; |
| 852 |
} |
| 853 |
|
| 854 |
buf = kmalloc(4, GFP_KERNEL); |
| 855 |
if (!buf) |
| 856 |
return; |
| 857 |
|
| 858 |
/* Set data bit of termios */ |
| 859 |
switch (C_CSIZE(tty)) { |
| 860 |
case CS5: |
| 861 |
data_bits = MX_WORDLENGTH_5; |
| 862 |
break; |
| 863 |
case CS6: |
| 864 |
data_bits = MX_WORDLENGTH_6; |
| 865 |
break; |
| 866 |
case CS7: |
| 867 |
data_bits = MX_WORDLENGTH_7; |
| 868 |
break; |
| 869 |
case CS8: |
| 870 |
default: |
| 871 |
data_bits = MX_WORDLENGTH_8; |
| 872 |
break; |
| 873 |
} |
| 874 |
|
| 875 |
/* Set parity of termios */ |
| 876 |
if (C_PARENB(tty)) { |
| 877 |
if (C_CMSPAR(tty)) { |
| 878 |
if (C_PARODD(tty)) |
| 879 |
parity = MX_PARITY_MARK; |
| 880 |
else |
| 881 |
parity = MX_PARITY_SPACE; |
| 882 |
} else { |
| 883 |
if (C_PARODD(tty)) |
| 884 |
parity = MX_PARITY_ODD; |
| 885 |
else |
| 886 |
parity = MX_PARITY_EVEN; |
| 887 |
} |
| 888 |
} else { |
| 889 |
parity = MX_PARITY_NONE; |
| 890 |
} |
| 891 |
|
| 892 |
/* Set stop bit of termios */ |
| 893 |
if (C_CSTOPB(tty)) |
| 894 |
stop_bits = MX_STOP_BITS_2; |
| 895 |
else |
| 896 |
stop_bits = MX_STOP_BITS_1; |
| 897 |
|
| 898 |
buf[0] = data_bits; |
| 899 |
buf[1] = parity; |
| 900 |
buf[2] = stop_bits; |
| 901 |
buf[3] = 0; |
| 902 |
|
| 903 |
err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_LINE, |
| 904 |
0, port->port_number, buf, 4); |
| 905 |
if (err) |
| 906 |
goto out; |
| 907 |
|
| 908 |
err = mxuport_set_termios_flow(tty, old_termios, port, serial); |
| 909 |
if (err) |
| 910 |
goto out; |
| 911 |
|
| 912 |
baud = tty_get_baud_rate(tty); |
| 913 |
if (!baud) |
| 914 |
baud = 9600; |
| 915 |
|
| 916 |
/* Note: Little Endian */ |
| 917 |
put_unaligned_le32(baud, buf); |
| 918 |
|
| 919 |
err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_BAUD, |
| 920 |
0, port->port_number, |
| 921 |
buf, 4); |
| 922 |
if (err) |
| 923 |
goto out; |
| 924 |
|
| 925 |
dev_dbg(&port->dev, "baud_rate : %d\n", baud); |
| 926 |
dev_dbg(&port->dev, "data_bits : %d\n", data_bits); |
| 927 |
dev_dbg(&port->dev, "parity : %d\n", parity); |
| 928 |
dev_dbg(&port->dev, "stop_bits : %d\n", stop_bits); |
| 929 |
|
| 930 |
out: |
| 931 |
kfree(buf); |
| 932 |
} |
| 933 |
|
| 934 |
/* |
| 935 |
* Determine how many ports this device has dynamically. It will be |
| 936 |
* called after the probe() callback is called, but before attach(). |
| 937 |
*/ |
| 938 |
static int mxuport_calc_num_ports(struct usb_serial *serial, |
| 939 |
struct usb_serial_endpoints *epds) |
| 940 |
{ |
| 941 |
unsigned long features = (unsigned long)usb_get_serial_data(serial); |
| 942 |
int num_ports; |
| 943 |
int i; |
| 944 |
|
| 945 |
if (features & MX_UPORT_2_PORT) { |
| 946 |
num_ports = 2; |
| 947 |
} else if (features & MX_UPORT_4_PORT) { |
| 948 |
num_ports = 4; |
| 949 |
} else if (features & MX_UPORT_8_PORT) { |
| 950 |
num_ports = 8; |
| 951 |
} else if (features & MX_UPORT_16_PORT) { |
| 952 |
num_ports = 16; |
| 953 |
} else { |
| 954 |
dev_warn(&serial->interface->dev, |
| 955 |
"unknown device, assuming two ports\n"); |
| 956 |
num_ports = 2; |
| 957 |
} |
| 958 |
|
| 959 |
/* |
| 960 |
* Setup bulk-out endpoint multiplexing. All ports share the same |
| 961 |
* bulk-out endpoint. |
| 962 |
*/ |
| 963 |
BUILD_BUG_ON(ARRAY_SIZE(epds->bulk_out) < 16); |
| 964 |
|
| 965 |
/* |
| 966 |
* The bulk-out buffers must be large enough for the four-byte header |
| 967 |
* (and following data), but assume anything smaller than eight bytes |
| 968 |
* is broken. |
| 969 |
*/ |
| 970 |
if (usb_endpoint_maxp(epds->bulk_out[0]) < 8) |
| 971 |
return -EINVAL; |
| 972 |
|
| 973 |
for (i = 1; i < num_ports; ++i) |
| 974 |
epds->bulk_out[i] = epds->bulk_out[0]; |
| 975 |
|
| 976 |
epds->num_bulk_out = num_ports; |
| 977 |
|
| 978 |
return num_ports; |
| 979 |
} |
| 980 |
|
| 981 |
/* Get the version of the firmware currently running. */ |
| 982 |
static int mxuport_get_fw_version(struct usb_serial *serial, u32 *version) |
| 983 |
{ |
| 984 |
u8 *ver_buf; |
| 985 |
int err; |
| 986 |
|
| 987 |
ver_buf = kzalloc(4, GFP_KERNEL); |
| 988 |
if (!ver_buf) |
| 989 |
return -ENOMEM; |
| 990 |
|
| 991 |
/* Get firmware version from SDRAM */ |
| 992 |
err = mxuport_recv_ctrl_urb(serial, RQ_VENDOR_GET_VERSION, 0, 0, |
| 993 |
ver_buf, 4); |
| 994 |
if (err != 4) { |
| 995 |
err = -EIO; |
| 996 |
goto out; |
| 997 |
} |
| 998 |
|
| 999 |
*version = (ver_buf[0] << 16) | (ver_buf[1] << 8) | ver_buf[2]; |
| 1000 |
err = 0; |
| 1001 |
out: |
| 1002 |
kfree(ver_buf); |
| 1003 |
return err; |
| 1004 |
} |
| 1005 |
|
| 1006 |
/* Given a firmware blob, download it to the device. */ |
| 1007 |
static int mxuport_download_fw(struct usb_serial *serial, |
| 1008 |
const struct firmware *fw_p) |
| 1009 |
{ |
| 1010 |
u8 *fw_buf; |
| 1011 |
size_t txlen; |
| 1012 |
size_t fwidx; |
| 1013 |
int err; |
| 1014 |
|
| 1015 |
fw_buf = kmalloc(DOWN_BLOCK_SIZE, GFP_KERNEL); |
| 1016 |
if (!fw_buf) |
| 1017 |
return -ENOMEM; |
| 1018 |
|
| 1019 |
dev_dbg(&serial->interface->dev, "Starting firmware download...\n"); |
| 1020 |
err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_START_FW_DOWN, 0, 0); |
| 1021 |
if (err) |
| 1022 |
goto out; |
| 1023 |
|
| 1024 |
fwidx = 0; |
| 1025 |
do { |
| 1026 |
txlen = min_t(size_t, (fw_p->size - fwidx), DOWN_BLOCK_SIZE); |
| 1027 |
|
| 1028 |
memcpy(fw_buf, &fw_p->data[fwidx], txlen); |
| 1029 |
err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_FW_DATA, |
| 1030 |
0, 0, fw_buf, txlen); |
| 1031 |
if (err) { |
| 1032 |
mxuport_send_ctrl_urb(serial, RQ_VENDOR_STOP_FW_DOWN, |
| 1033 |
0, 0); |
| 1034 |
goto out; |
| 1035 |
} |
| 1036 |
|
| 1037 |
fwidx += txlen; |
| 1038 |
usleep_range(1000, 2000); |
| 1039 |
|
| 1040 |
} while (fwidx < fw_p->size); |
| 1041 |
|
| 1042 |
msleep(1000); |
| 1043 |
err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_STOP_FW_DOWN, 0, 0); |
| 1044 |
if (err) |
| 1045 |
goto out; |
| 1046 |
|
| 1047 |
msleep(1000); |
| 1048 |
err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_QUERY_FW_READY, 0, 0); |
| 1049 |
|
| 1050 |
out: |
| 1051 |
kfree(fw_buf); |
| 1052 |
return err; |
| 1053 |
} |
| 1054 |
|
| 1055 |
static int mxuport_probe(struct usb_serial *serial, |
| 1056 |
const struct usb_device_id *id) |
| 1057 |
{ |
| 1058 |
u16 productid = le16_to_cpu(serial->dev->descriptor.idProduct); |
| 1059 |
const struct firmware *fw_p = NULL; |
| 1060 |
u32 version; |
| 1061 |
int local_ver; |
| 1062 |
char buf[32]; |
| 1063 |
int err; |
| 1064 |
|
| 1065 |
/* Load our firmware */ |
| 1066 |
err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_QUERY_FW_CONFIG, 0, 0); |
| 1067 |
if (err) { |
| 1068 |
mxuport_send_ctrl_urb(serial, RQ_VENDOR_RESET_DEVICE, 0, 0); |
| 1069 |
return err; |
| 1070 |
} |
| 1071 |
|
| 1072 |
err = mxuport_get_fw_version(serial, &version); |
| 1073 |
if (err < 0) |
| 1074 |
return err; |
| 1075 |
|
| 1076 |
dev_dbg(&serial->interface->dev, "Device firmware version v%x.%x.%x\n", |
| 1077 |
(version & 0xff0000) >> 16, |
| 1078 |
(version & 0xff00) >> 8, |
| 1079 |
(version & 0xff)); |
| 1080 |
|
| 1081 |
snprintf(buf, sizeof(buf) - 1, "moxa/moxa-%04x.fw", productid); |
| 1082 |
|
| 1083 |
err = request_firmware(&fw_p, buf, &serial->interface->dev); |
| 1084 |
if (err) { |
| 1085 |
dev_warn(&serial->interface->dev, "Firmware %s not found\n", |
| 1086 |
buf); |
| 1087 |
|
| 1088 |
/* Use the firmware already in the device */ |
| 1089 |
err = 0; |
| 1090 |
} else { |
| 1091 |
local_ver = ((fw_p->data[VER_ADDR_1] << 16) | |
| 1092 |
(fw_p->data[VER_ADDR_2] << 8) | |
| 1093 |
fw_p->data[VER_ADDR_3]); |
| 1094 |
dev_dbg(&serial->interface->dev, |
| 1095 |
"Available firmware version v%x.%x.%x\n", |
| 1096 |
fw_p->data[VER_ADDR_1], fw_p->data[VER_ADDR_2], |
| 1097 |
fw_p->data[VER_ADDR_3]); |
| 1098 |
if (local_ver > version) { |
| 1099 |
err = mxuport_download_fw(serial, fw_p); |
| 1100 |
if (err) |
| 1101 |
goto out; |
| 1102 |
err = mxuport_get_fw_version(serial, &version); |
| 1103 |
if (err < 0) |
| 1104 |
goto out; |
| 1105 |
} |
| 1106 |
} |
| 1107 |
|
| 1108 |
dev_info(&serial->interface->dev, |
| 1109 |
"Using device firmware version v%x.%x.%x\n", |
| 1110 |
(version & 0xff0000) >> 16, |
| 1111 |
(version & 0xff00) >> 8, |
| 1112 |
(version & 0xff)); |
| 1113 |
|
| 1114 |
/* |
| 1115 |
* Contains the features of this hardware. Store away for |
| 1116 |
* later use, eg, number of ports. |
| 1117 |
*/ |
| 1118 |
usb_set_serial_data(serial, (void *)id->driver_info); |
| 1119 |
out: |
| 1120 |
if (fw_p) |
| 1121 |
release_firmware(fw_p); |
| 1122 |
return err; |
| 1123 |
} |
| 1124 |
|
| 1125 |
|
| 1126 |
static int mxuport_port_probe(struct usb_serial_port *port) |
| 1127 |
{ |
| 1128 |
struct usb_serial *serial = port->serial; |
| 1129 |
struct mxuport_port *mxport; |
| 1130 |
int err; |
| 1131 |
|
| 1132 |
mxport = devm_kzalloc(&port->dev, sizeof(struct mxuport_port), |
| 1133 |
GFP_KERNEL); |
| 1134 |
if (!mxport) |
| 1135 |
return -ENOMEM; |
| 1136 |
|
| 1137 |
mutex_init(&mxport->mutex); |
| 1138 |
spin_lock_init(&mxport->spinlock); |
| 1139 |
|
| 1140 |
/* Set the port private data */ |
| 1141 |
usb_set_serial_port_data(port, mxport); |
| 1142 |
|
| 1143 |
/* Set FIFO (Enable) */ |
| 1144 |
err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_FIFO_DISABLE, |
| 1145 |
0, port->port_number); |
| 1146 |
if (err) |
| 1147 |
return err; |
| 1148 |
|
| 1149 |
/* Set transmission mode (Hi-Performance) */ |
| 1150 |
err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_HIGH_PERFOR, |
| 1151 |
0, port->port_number); |
| 1152 |
if (err) |
| 1153 |
return err; |
| 1154 |
|
| 1155 |
/* Set interface (RS-232) */ |
| 1156 |
return mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_INTERFACE, |
| 1157 |
MX_INT_RS232, |
| 1158 |
port->port_number); |
| 1159 |
} |
| 1160 |
|
| 1161 |
static int mxuport_attach(struct usb_serial *serial) |
| 1162 |
{ |
| 1163 |
struct usb_serial_port *port0 = serial->port[0]; |
| 1164 |
struct usb_serial_port *port1 = serial->port[1]; |
| 1165 |
int err; |
| 1166 |
|
| 1167 |
/* |
| 1168 |
* All data from the ports is received on the first bulk in |
| 1169 |
* endpoint, with a multiplex header. The second bulk in is |
| 1170 |
* used for events. |
| 1171 |
* |
| 1172 |
* Start to read from the device. |
| 1173 |
*/ |
| 1174 |
err = usb_serial_generic_submit_read_urbs(port0, GFP_KERNEL); |
| 1175 |
if (err) |
| 1176 |
return err; |
| 1177 |
|
| 1178 |
err = usb_serial_generic_submit_read_urbs(port1, GFP_KERNEL); |
| 1179 |
if (err) { |
| 1180 |
usb_serial_generic_close(port0); |
| 1181 |
return err; |
| 1182 |
} |
| 1183 |
|
| 1184 |
return 0; |
| 1185 |
} |
| 1186 |
|
| 1187 |
static void mxuport_release(struct usb_serial *serial) |
| 1188 |
{ |
| 1189 |
struct usb_serial_port *port0 = serial->port[0]; |
| 1190 |
struct usb_serial_port *port1 = serial->port[1]; |
| 1191 |
|
| 1192 |
usb_serial_generic_close(port1); |
| 1193 |
usb_serial_generic_close(port0); |
| 1194 |
} |
| 1195 |
|
| 1196 |
static int mxuport_open(struct tty_struct *tty, struct usb_serial_port *port) |
| 1197 |
{ |
| 1198 |
struct mxuport_port *mxport = usb_get_serial_port_data(port); |
| 1199 |
struct usb_serial *serial = port->serial; |
| 1200 |
int err; |
| 1201 |
|
| 1202 |
/* Set receive host (enable) */ |
| 1203 |
err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN, |
| 1204 |
1, port->port_number); |
| 1205 |
if (err) |
| 1206 |
return err; |
| 1207 |
|
| 1208 |
err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_OPEN, |
| 1209 |
1, port->port_number); |
| 1210 |
if (err) { |
| 1211 |
mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN, |
| 1212 |
0, port->port_number); |
| 1213 |
return err; |
| 1214 |
} |
| 1215 |
|
| 1216 |
/* Initial port termios */ |
| 1217 |
if (tty) |
| 1218 |
mxuport_set_termios(tty, port, NULL); |
| 1219 |
|
| 1220 |
/* |
| 1221 |
* TODO: use RQ_VENDOR_GET_MSR, once we know what it |
| 1222 |
* returns. |
| 1223 |
*/ |
| 1224 |
mxport->msr_state = 0; |
| 1225 |
|
| 1226 |
return err; |
| 1227 |
} |
| 1228 |
|
| 1229 |
static void mxuport_close(struct usb_serial_port *port) |
| 1230 |
{ |
| 1231 |
struct usb_serial *serial = port->serial; |
| 1232 |
|
| 1233 |
mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_OPEN, 0, |
| 1234 |
port->port_number); |
| 1235 |
|
| 1236 |
mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN, 0, |
| 1237 |
port->port_number); |
| 1238 |
} |
| 1239 |
|
| 1240 |
/* Send a break to the port. */ |
| 1241 |
static int mxuport_break_ctl(struct tty_struct *tty, int break_state) |
| 1242 |
{ |
| 1243 |
struct usb_serial_port *port = tty->driver_data; |
| 1244 |
struct usb_serial *serial = port->serial; |
| 1245 |
int enable; |
| 1246 |
|
| 1247 |
if (break_state == -1) { |
| 1248 |
enable = 1; |
| 1249 |
dev_dbg(&port->dev, "%s - sending break\n", __func__); |
| 1250 |
} else { |
| 1251 |
enable = 0; |
| 1252 |
dev_dbg(&port->dev, "%s - clearing break\n", __func__); |
| 1253 |
} |
| 1254 |
|
| 1255 |
return mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_BREAK, |
| 1256 |
enable, port->port_number); |
| 1257 |
} |
| 1258 |
|
| 1259 |
static int mxuport_resume(struct usb_serial *serial) |
| 1260 |
{ |
| 1261 |
struct usb_serial_port *port; |
| 1262 |
int c = 0; |
| 1263 |
int i; |
| 1264 |
int r; |
| 1265 |
|
| 1266 |
for (i = 0; i < 2; i++) { |
| 1267 |
port = serial->port[i]; |
| 1268 |
|
| 1269 |
r = usb_serial_generic_submit_read_urbs(port, GFP_NOIO); |
| 1270 |
if (r < 0) |
| 1271 |
c++; |
| 1272 |
} |
| 1273 |
|
| 1274 |
for (i = 0; i < serial->num_ports; i++) { |
| 1275 |
port = serial->port[i]; |
| 1276 |
if (!tty_port_initialized(&port->port)) |
| 1277 |
continue; |
| 1278 |
|
| 1279 |
r = usb_serial_generic_write_start(port, GFP_NOIO); |
| 1280 |
if (r < 0) |
| 1281 |
c++; |
| 1282 |
} |
| 1283 |
|
| 1284 |
return c ? -EIO : 0; |
| 1285 |
} |
| 1286 |
|
| 1287 |
static struct usb_serial_driver mxuport_device = { |
| 1288 |
.driver = { |
| 1289 |
.name = "mxuport", |
| 1290 |
}, |
| 1291 |
.description = "MOXA UPort", |
| 1292 |
.id_table = mxuport_idtable, |
| 1293 |
.num_bulk_in = 2, |
| 1294 |
.num_bulk_out = 1, |
| 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, |
| 1315 |
}; |
| 1316 |
|
| 1317 |
static struct usb_serial_driver *const serial_drivers[] = { |
| 1318 |
&mxuport_device, NULL |
| 1319 |
}; |
| 1320 |
|
| 1321 |
module_usb_serial_driver(serial_drivers, mxuport_idtable); |
| 1322 |
|
| 1323 |
MODULE_AUTHOR("Andrew Lunn <[email protected]>"); |
| 1324 |
MODULE_AUTHOR("<[email protected]>"); |
| 1325 |
MODULE_DESCRIPTION("Moxa UPORT USB Serial driver"); |
| 1326 |
MODULE_LICENSE("GPL"); |