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