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