1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * usb-serial driver for Quatech USB 2 devices 4 * 5 * Copyright (C) 2012 Bill Pemberton (wfp5p@virginia.edu) 6 * 7 * These devices all have only 1 bulk in and 1 bulk out that is shared 8 * for all serial ports. 9 * 10 */ 11 12 #include <linux/unaligned.h> 13 #include <linux/errno.h> 14 #include <linux/slab.h> 15 #include <linux/tty.h> 16 #include <linux/tty_flip.h> 17 #include <linux/module.h> 18 #include <linux/serial.h> 19 #include <linux/usb.h> 20 #include <linux/usb/serial.h> 21 #include <linux/serial_reg.h> 22 23 /* default urb timeout for usb operations */ 24 #define QT2_USB_TIMEOUT USB_CTRL_SET_TIMEOUT 25 26 #define QT_OPEN_CLOSE_CHANNEL 0xca 27 #define QT_SET_GET_DEVICE 0xc2 28 #define QT_SET_GET_REGISTER 0xc0 29 #define QT_GET_SET_PREBUF_TRIG_LVL 0xcc 30 #define QT_SET_ATF 0xcd 31 #define QT_TRANSFER_IN 0xc0 32 #define QT_HW_FLOW_CONTROL_MASK 0xc5 33 #define QT_SW_FLOW_CONTROL_MASK 0xc6 34 #define QT2_BREAK_CONTROL 0xc8 35 #define QT2_GET_SET_UART 0xc1 36 #define QT2_FLUSH_DEVICE 0xc4 37 #define QT2_GET_SET_QMCR 0xe1 38 #define QT2_QMCR_RS232 0x40 39 #define QT2_QMCR_RS422 0x10 40 41 #define SERIAL_CRTSCTS ((UART_MCR_RTS << 8) | UART_MSR_CTS) 42 43 #define SERIAL_EVEN_PARITY (UART_LCR_PARITY | UART_LCR_EPAR) 44 45 /* status bytes for the device */ 46 #define QT2_CONTROL_BYTE 0x1b 47 #define QT2_LINE_STATUS 0x00 /* following 1 byte is line status */ 48 #define QT2_MODEM_STATUS 0x01 /* following 1 byte is modem status */ 49 #define QT2_XMIT_HOLD 0x02 /* following 2 bytes are ?? */ 50 #define QT2_CHANGE_PORT 0x03 /* following 1 byte is port to change to */ 51 #define QT2_REC_FLUSH 0x04 /* no following info */ 52 #define QT2_XMIT_FLUSH 0x05 /* no following info */ 53 #define QT2_CONTROL_ESCAPE 0xff /* pass through previous 2 control bytes */ 54 55 #define MAX_BAUD_RATE 921600 56 #define DEFAULT_BAUD_RATE 9600 57 58 #define QT2_READ_BUFFER_SIZE 512 /* size of read buffer */ 59 #define QT2_WRITE_BUFFER_SIZE 512 /* size of write buffer */ 60 #define QT2_WRITE_CONTROL_SIZE 5 /* control bytes used for a write */ 61 62 #define DRIVER_DESC "Quatech 2nd gen USB to Serial Driver" 63 64 #define USB_VENDOR_ID_QUATECH 0x061d 65 #define QUATECH_SSU2_100 0xC120 /* RS232 single port */ 66 #define QUATECH_DSU2_100 0xC140 /* RS232 dual port */ 67 #define QUATECH_DSU2_400 0xC150 /* RS232/422/485 dual port */ 68 #define QUATECH_QSU2_100 0xC160 /* RS232 four port */ 69 #define QUATECH_QSU2_400 0xC170 /* RS232/422/485 four port */ 70 #define QUATECH_ESU2_100 0xC1A0 /* RS232 eight port */ 71 #define QUATECH_ESU2_400 0xC180 /* RS232/422/485 eight port */ 72 73 struct qt2_device_detail { 74 int product_id; 75 int num_ports; 76 }; 77 78 #define QT_DETAILS(prod, ports) \ 79 .product_id = (prod), \ 80 .num_ports = (ports) 81 82 static const struct qt2_device_detail qt2_device_details[] = { 83 {QT_DETAILS(QUATECH_SSU2_100, 1)}, 84 {QT_DETAILS(QUATECH_DSU2_400, 2)}, 85 {QT_DETAILS(QUATECH_DSU2_100, 2)}, 86 {QT_DETAILS(QUATECH_QSU2_400, 4)}, 87 {QT_DETAILS(QUATECH_QSU2_100, 4)}, 88 {QT_DETAILS(QUATECH_ESU2_400, 8)}, 89 {QT_DETAILS(QUATECH_ESU2_100, 8)}, 90 {QT_DETAILS(0, 0)} /* Terminating entry */ 91 }; 92 93 static const struct usb_device_id id_table[] = { 94 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU2_100)}, 95 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU2_100)}, 96 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU2_400)}, 97 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU2_100)}, 98 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU2_400)}, 99 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU2_100)}, 100 {USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU2_400)}, 101 {} /* Terminating entry */ 102 }; 103 MODULE_DEVICE_TABLE(usb, id_table); 104 105 struct qt2_serial_private { 106 unsigned char current_port; /* current port for incoming data */ 107 108 struct urb *read_urb; /* shared among all ports */ 109 char *read_buffer; 110 }; 111 112 struct qt2_port_private { 113 u8 device_port; 114 115 spinlock_t urb_lock; 116 bool urb_in_use; 117 struct urb *write_urb; 118 char *write_buffer; 119 120 spinlock_t lock; 121 u8 shadowLSR; 122 u8 shadowMSR; 123 124 struct usb_serial_port *port; 125 }; 126 127 static void qt2_update_lsr(struct usb_serial_port *port, unsigned char *ch); 128 static void qt2_update_msr(struct usb_serial_port *port, unsigned char *ch); 129 static void qt2_write_bulk_callback(struct urb *urb); 130 static void qt2_read_bulk_callback(struct urb *urb); 131 132 static void qt2_release(struct usb_serial *serial) 133 { 134 struct qt2_serial_private *serial_priv; 135 136 serial_priv = usb_get_serial_data(serial); 137 138 usb_kill_urb(serial_priv->read_urb); 139 usb_free_urb(serial_priv->read_urb); 140 kfree(serial_priv->read_buffer); 141 kfree(serial_priv); 142 } 143 144 static inline int calc_baud_divisor(int baudrate) 145 { 146 int divisor, rem; 147 148 divisor = MAX_BAUD_RATE / baudrate; 149 rem = MAX_BAUD_RATE % baudrate; 150 /* Round to nearest divisor */ 151 if (((rem * 2) >= baudrate) && (baudrate != 110)) 152 divisor++; 153 154 return divisor; 155 } 156 157 static inline int qt2_set_port_config(struct usb_device *dev, 158 unsigned char port_number, 159 u16 baudrate, u16 lcr) 160 { 161 int divisor = calc_baud_divisor(baudrate); 162 u16 index = ((u16) (lcr << 8) | (u16) (port_number)); 163 164 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 165 QT2_GET_SET_UART, 0x40, 166 divisor, index, NULL, 0, QT2_USB_TIMEOUT); 167 } 168 169 static inline int qt2_control_msg(struct usb_device *dev, 170 u8 request, u16 data, u16 index) 171 { 172 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 173 request, 0x40, data, index, 174 NULL, 0, QT2_USB_TIMEOUT); 175 } 176 177 static inline int qt2_getregister(struct usb_device *dev, 178 u8 uart, 179 u8 reg, 180 u8 *data) 181 { 182 int ret; 183 184 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), 185 QT_SET_GET_REGISTER, 0xc0, reg, 186 uart, data, sizeof(*data), QT2_USB_TIMEOUT); 187 if (ret < (int)sizeof(*data)) { 188 if (ret >= 0) 189 ret = -EIO; 190 } 191 192 return ret; 193 } 194 195 static inline int qt2_setregister(struct usb_device *dev, 196 u8 uart, u8 reg, u16 data) 197 { 198 u16 value = (data << 8) | reg; 199 200 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 201 QT_SET_GET_REGISTER, 0x40, value, uart, 202 NULL, 0, QT2_USB_TIMEOUT); 203 } 204 205 static inline int update_mctrl(struct qt2_port_private *port_priv, 206 unsigned int set, unsigned int clear) 207 { 208 struct usb_serial_port *port = port_priv->port; 209 struct usb_device *dev = port->serial->dev; 210 unsigned urb_value; 211 int status; 212 213 if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) { 214 dev_dbg(&port->dev, 215 "update_mctrl - DTR|RTS not being set|cleared\n"); 216 return 0; /* no change */ 217 } 218 219 clear &= ~set; /* 'set' takes precedence over 'clear' */ 220 urb_value = 0; 221 if (set & TIOCM_DTR) 222 urb_value |= UART_MCR_DTR; 223 if (set & TIOCM_RTS) 224 urb_value |= UART_MCR_RTS; 225 226 status = qt2_setregister(dev, port_priv->device_port, UART_MCR, 227 urb_value); 228 if (status < 0) 229 dev_err(&port->dev, 230 "update_mctrl - Error from MODEM_CTRL urb: %i\n", 231 status); 232 return status; 233 } 234 235 static int qt2_calc_num_ports(struct usb_serial *serial, 236 struct usb_serial_endpoints *epds) 237 { 238 struct qt2_device_detail d; 239 int i; 240 241 for (i = 0; d = qt2_device_details[i], d.product_id != 0; i++) { 242 if (d.product_id == le16_to_cpu(serial->dev->descriptor.idProduct)) 243 return d.num_ports; 244 } 245 246 /* we didn't recognize the device */ 247 dev_err(&serial->dev->dev, 248 "don't know the number of ports, assuming 1\n"); 249 250 return 1; 251 } 252 253 static void qt2_set_termios(struct tty_struct *tty, 254 struct usb_serial_port *port, 255 const struct ktermios *old_termios) 256 { 257 struct usb_device *dev = port->serial->dev; 258 struct qt2_port_private *port_priv; 259 struct ktermios *termios = &tty->termios; 260 u16 baud; 261 unsigned int cflag = termios->c_cflag; 262 u16 new_lcr = 0; 263 int status; 264 265 port_priv = usb_get_serial_port_data(port); 266 267 if (cflag & PARENB) { 268 if (cflag & PARODD) 269 new_lcr |= UART_LCR_PARITY; 270 else 271 new_lcr |= SERIAL_EVEN_PARITY; 272 } 273 274 new_lcr |= UART_LCR_WLEN(tty_get_char_size(cflag)); 275 276 baud = tty_get_baud_rate(tty); 277 if (!baud) 278 baud = 9600; 279 280 status = qt2_set_port_config(dev, port_priv->device_port, baud, 281 new_lcr); 282 if (status < 0) 283 dev_err(&port->dev, "%s - qt2_set_port_config failed: %i\n", 284 __func__, status); 285 286 if (cflag & CRTSCTS) 287 status = qt2_control_msg(dev, QT_HW_FLOW_CONTROL_MASK, 288 SERIAL_CRTSCTS, 289 port_priv->device_port); 290 else 291 status = qt2_control_msg(dev, QT_HW_FLOW_CONTROL_MASK, 292 0, port_priv->device_port); 293 if (status < 0) 294 dev_err(&port->dev, "%s - set HW flow control failed: %i\n", 295 __func__, status); 296 297 if (I_IXOFF(tty) || I_IXON(tty)) { 298 u16 x = ((u16) (START_CHAR(tty) << 8) | (u16) (STOP_CHAR(tty))); 299 300 status = qt2_control_msg(dev, QT_SW_FLOW_CONTROL_MASK, 301 x, port_priv->device_port); 302 } else 303 status = qt2_control_msg(dev, QT_SW_FLOW_CONTROL_MASK, 304 0, port_priv->device_port); 305 306 if (status < 0) 307 dev_err(&port->dev, "%s - set SW flow control failed: %i\n", 308 __func__, status); 309 310 } 311 312 static int qt2_open(struct tty_struct *tty, struct usb_serial_port *port) 313 { 314 struct usb_serial *serial; 315 struct qt2_port_private *port_priv; 316 u8 *data; 317 u16 device_port; 318 int status; 319 unsigned long flags; 320 321 device_port = port->port_number; 322 323 serial = port->serial; 324 325 port_priv = usb_get_serial_port_data(port); 326 327 /* set the port to RS232 mode */ 328 status = qt2_control_msg(serial->dev, QT2_GET_SET_QMCR, 329 QT2_QMCR_RS232, device_port); 330 if (status < 0) { 331 dev_err(&port->dev, 332 "%s failed to set RS232 mode for port %i error %i\n", 333 __func__, device_port, status); 334 return status; 335 } 336 337 data = kzalloc(2, GFP_KERNEL); 338 if (!data) 339 return -ENOMEM; 340 341 /* open the port */ 342 status = usb_control_msg(serial->dev, 343 usb_rcvctrlpipe(serial->dev, 0), 344 QT_OPEN_CLOSE_CHANNEL, 345 0xc0, 0, 346 device_port, data, 2, QT2_USB_TIMEOUT); 347 348 if (status < 2) { 349 dev_err(&port->dev, "%s - open port failed %i\n", __func__, 350 status); 351 if (status >= 0) 352 status = -EIO; 353 kfree(data); 354 return status; 355 } 356 357 spin_lock_irqsave(&port_priv->lock, flags); 358 port_priv->shadowLSR = data[0]; 359 port_priv->shadowMSR = data[1]; 360 spin_unlock_irqrestore(&port_priv->lock, flags); 361 362 kfree(data); 363 364 /* set to default speed and 8bit word size */ 365 status = qt2_set_port_config(serial->dev, device_port, 366 DEFAULT_BAUD_RATE, UART_LCR_WLEN8); 367 if (status < 0) { 368 dev_err(&port->dev, "%s - initial setup failed (%i)\n", 369 __func__, device_port); 370 return status; 371 } 372 373 port_priv->device_port = (u8) device_port; 374 375 if (tty) 376 qt2_set_termios(tty, port, &tty->termios); 377 378 return 0; 379 380 } 381 382 static void qt2_close(struct usb_serial_port *port) 383 { 384 struct usb_serial *serial; 385 struct qt2_port_private *port_priv; 386 int i; 387 388 serial = port->serial; 389 port_priv = usb_get_serial_port_data(port); 390 391 usb_kill_urb(port_priv->write_urb); 392 393 /* flush the port transmit buffer */ 394 i = usb_control_msg(serial->dev, 395 usb_sndctrlpipe(serial->dev, 0), 396 QT2_FLUSH_DEVICE, 0x40, 1, 397 port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT); 398 399 if (i < 0) 400 dev_err(&port->dev, "%s - transmit buffer flush failed: %i\n", 401 __func__, i); 402 403 /* flush the port receive buffer */ 404 i = usb_control_msg(serial->dev, 405 usb_sndctrlpipe(serial->dev, 0), 406 QT2_FLUSH_DEVICE, 0x40, 0, 407 port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT); 408 409 if (i < 0) 410 dev_err(&port->dev, "%s - receive buffer flush failed: %i\n", 411 __func__, i); 412 413 /* close the port */ 414 i = usb_control_msg(serial->dev, 415 usb_sndctrlpipe(serial->dev, 0), 416 QT_OPEN_CLOSE_CHANNEL, 417 0x40, 0, 418 port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT); 419 420 if (i < 0) 421 dev_err(&port->dev, "%s - close port failed %i\n", 422 __func__, i); 423 } 424 425 static void qt2_disconnect(struct usb_serial *serial) 426 { 427 struct qt2_serial_private *serial_priv = usb_get_serial_data(serial); 428 429 usb_kill_urb(serial_priv->read_urb); 430 } 431 432 static void qt2_process_status(struct usb_serial_port *port, unsigned char *ch) 433 { 434 switch (*ch) { 435 case QT2_LINE_STATUS: 436 qt2_update_lsr(port, ch + 1); 437 break; 438 case QT2_MODEM_STATUS: 439 qt2_update_msr(port, ch + 1); 440 break; 441 } 442 } 443 444 static void qt2_process_read_urb(struct urb *urb) 445 { 446 struct usb_serial *serial; 447 struct qt2_serial_private *serial_priv; 448 struct usb_serial_port *port; 449 bool escapeflag; 450 unsigned char *ch; 451 int i; 452 unsigned char newport; 453 int len = urb->actual_length; 454 455 if (!len) 456 return; 457 458 ch = urb->transfer_buffer; 459 serial = urb->context; 460 serial_priv = usb_get_serial_data(serial); 461 port = serial->port[serial_priv->current_port]; 462 463 for (i = 0; i < urb->actual_length; i++) { 464 ch = (unsigned char *)urb->transfer_buffer + i; 465 if ((i <= (len - 3)) && 466 (*ch == QT2_CONTROL_BYTE) && 467 (*(ch + 1) == QT2_CONTROL_BYTE)) { 468 escapeflag = false; 469 switch (*(ch + 2)) { 470 case QT2_LINE_STATUS: 471 case QT2_MODEM_STATUS: 472 if (i > (len - 4)) { 473 dev_warn(&port->dev, 474 "%s - status message too short\n", 475 __func__); 476 break; 477 } 478 qt2_process_status(port, ch + 2); 479 i += 3; 480 escapeflag = true; 481 break; 482 case QT2_XMIT_HOLD: 483 if (i > (len - 5)) { 484 dev_warn(&port->dev, 485 "%s - xmit_empty message too short\n", 486 __func__); 487 break; 488 } 489 /* bytes_written = (ch[1] << 4) + ch[0]; */ 490 i += 4; 491 escapeflag = true; 492 break; 493 case QT2_CHANGE_PORT: 494 if (i > (len - 4)) { 495 dev_warn(&port->dev, 496 "%s - change_port message too short\n", 497 __func__); 498 break; 499 } 500 tty_flip_buffer_push(&port->port); 501 502 newport = *(ch + 3); 503 504 if (newport >= serial->num_ports) { 505 dev_err(&port->dev, 506 "%s - port change to invalid port: %i\n", 507 __func__, newport); 508 break; 509 } 510 511 serial_priv->current_port = newport; 512 port = serial->port[serial_priv->current_port]; 513 i += 3; 514 escapeflag = true; 515 break; 516 case QT2_REC_FLUSH: 517 case QT2_XMIT_FLUSH: 518 i += 2; 519 escapeflag = true; 520 break; 521 case QT2_CONTROL_ESCAPE: 522 tty_insert_flip_string(&port->port, ch, 2); 523 i += 2; 524 escapeflag = true; 525 break; 526 default: 527 dev_warn(&port->dev, 528 "%s - unsupported command %i\n", 529 __func__, *(ch + 2)); 530 break; 531 } 532 if (escapeflag) 533 continue; 534 } 535 536 tty_insert_flip_char(&port->port, *ch, TTY_NORMAL); 537 } 538 539 tty_flip_buffer_push(&port->port); 540 } 541 542 static void qt2_write_bulk_callback(struct urb *urb) 543 { 544 struct usb_serial_port *port; 545 struct qt2_port_private *port_priv; 546 unsigned long flags; 547 548 port = urb->context; 549 port_priv = usb_get_serial_port_data(port); 550 551 spin_lock_irqsave(&port_priv->urb_lock, flags); 552 553 port_priv->urb_in_use = false; 554 usb_serial_port_softint(port); 555 556 spin_unlock_irqrestore(&port_priv->urb_lock, flags); 557 558 } 559 560 static void qt2_read_bulk_callback(struct urb *urb) 561 { 562 struct usb_serial *serial = urb->context; 563 int status; 564 565 if (urb->status) { 566 dev_warn(&serial->dev->dev, 567 "%s - non-zero urb status: %i\n", __func__, 568 urb->status); 569 return; 570 } 571 572 qt2_process_read_urb(urb); 573 574 status = usb_submit_urb(urb, GFP_ATOMIC); 575 if (status != 0) 576 dev_err(&serial->dev->dev, 577 "%s - resubmit read urb failed: %i\n", 578 __func__, status); 579 } 580 581 static int qt2_setup_urbs(struct usb_serial *serial) 582 { 583 struct usb_serial_port *port0; 584 struct qt2_serial_private *serial_priv; 585 int status; 586 587 port0 = serial->port[0]; 588 589 serial_priv = usb_get_serial_data(serial); 590 serial_priv->read_urb = usb_alloc_urb(0, GFP_KERNEL); 591 if (!serial_priv->read_urb) 592 return -ENOMEM; 593 594 usb_fill_bulk_urb(serial_priv->read_urb, serial->dev, 595 usb_rcvbulkpipe(serial->dev, 596 port0->bulk_in_endpointAddress), 597 serial_priv->read_buffer, 598 QT2_READ_BUFFER_SIZE, 599 qt2_read_bulk_callback, serial); 600 601 status = usb_submit_urb(serial_priv->read_urb, GFP_KERNEL); 602 if (status != 0) { 603 dev_err(&serial->dev->dev, 604 "%s - submit read urb failed %i\n", __func__, status); 605 usb_free_urb(serial_priv->read_urb); 606 return status; 607 } 608 609 return 0; 610 } 611 612 static int qt2_attach(struct usb_serial *serial) 613 { 614 struct qt2_serial_private *serial_priv; 615 int status; 616 617 /* power on unit */ 618 status = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), 619 0xc2, 0x40, 0x8000, 0, NULL, 0, 620 QT2_USB_TIMEOUT); 621 if (status < 0) { 622 dev_err(&serial->dev->dev, 623 "%s - failed to power on unit: %i\n", __func__, status); 624 return status; 625 } 626 627 serial_priv = kzalloc_obj(*serial_priv); 628 if (!serial_priv) 629 return -ENOMEM; 630 631 serial_priv->read_buffer = kmalloc(QT2_READ_BUFFER_SIZE, GFP_KERNEL); 632 if (!serial_priv->read_buffer) { 633 status = -ENOMEM; 634 goto err_buf; 635 } 636 637 usb_set_serial_data(serial, serial_priv); 638 639 status = qt2_setup_urbs(serial); 640 if (status != 0) 641 goto attach_failed; 642 643 return 0; 644 645 attach_failed: 646 kfree(serial_priv->read_buffer); 647 err_buf: 648 kfree(serial_priv); 649 return status; 650 } 651 652 static int qt2_port_probe(struct usb_serial_port *port) 653 { 654 struct usb_serial *serial = port->serial; 655 struct qt2_port_private *port_priv; 656 u8 bEndpointAddress; 657 658 port_priv = kzalloc_obj(*port_priv); 659 if (!port_priv) 660 return -ENOMEM; 661 662 spin_lock_init(&port_priv->lock); 663 spin_lock_init(&port_priv->urb_lock); 664 port_priv->port = port; 665 666 port_priv->write_buffer = kmalloc(QT2_WRITE_BUFFER_SIZE, GFP_KERNEL); 667 if (!port_priv->write_buffer) 668 goto err_buf; 669 670 port_priv->write_urb = usb_alloc_urb(0, GFP_KERNEL); 671 if (!port_priv->write_urb) 672 goto err_urb; 673 674 bEndpointAddress = serial->port[0]->bulk_out_endpointAddress; 675 usb_fill_bulk_urb(port_priv->write_urb, serial->dev, 676 usb_sndbulkpipe(serial->dev, bEndpointAddress), 677 port_priv->write_buffer, 678 QT2_WRITE_BUFFER_SIZE, 679 qt2_write_bulk_callback, port); 680 681 usb_set_serial_port_data(port, port_priv); 682 683 return 0; 684 err_urb: 685 kfree(port_priv->write_buffer); 686 err_buf: 687 kfree(port_priv); 688 return -ENOMEM; 689 } 690 691 static void qt2_port_remove(struct usb_serial_port *port) 692 { 693 struct qt2_port_private *port_priv; 694 695 port_priv = usb_get_serial_port_data(port); 696 usb_free_urb(port_priv->write_urb); 697 kfree(port_priv->write_buffer); 698 kfree(port_priv); 699 } 700 701 static int qt2_tiocmget(struct tty_struct *tty) 702 { 703 struct usb_serial_port *port = tty->driver_data; 704 struct usb_device *dev = port->serial->dev; 705 struct qt2_port_private *port_priv = usb_get_serial_port_data(port); 706 u8 *d; 707 int r; 708 709 d = kzalloc(2, GFP_KERNEL); 710 if (!d) 711 return -ENOMEM; 712 713 r = qt2_getregister(dev, port_priv->device_port, UART_MCR, d); 714 if (r < 0) 715 goto mget_out; 716 717 r = qt2_getregister(dev, port_priv->device_port, UART_MSR, d + 1); 718 if (r < 0) 719 goto mget_out; 720 721 r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) | 722 (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) | 723 (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) | 724 (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) | 725 (d[1] & UART_MSR_RI ? TIOCM_RI : 0) | 726 (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0); 727 728 mget_out: 729 kfree(d); 730 return r; 731 } 732 733 static int qt2_tiocmset(struct tty_struct *tty, 734 unsigned int set, unsigned int clear) 735 { 736 struct qt2_port_private *port_priv; 737 738 port_priv = usb_get_serial_port_data(tty->driver_data); 739 return update_mctrl(port_priv, set, clear); 740 } 741 742 static int qt2_break_ctl(struct tty_struct *tty, int break_state) 743 { 744 struct usb_serial_port *port = tty->driver_data; 745 struct qt2_port_private *port_priv; 746 int status; 747 u16 val; 748 749 port_priv = usb_get_serial_port_data(port); 750 751 val = (break_state == -1) ? 1 : 0; 752 753 status = qt2_control_msg(port->serial->dev, QT2_BREAK_CONTROL, 754 val, port_priv->device_port); 755 if (status < 0) { 756 dev_warn(&port->dev, 757 "%s - failed to send control message: %i\n", __func__, 758 status); 759 return status; 760 } 761 762 return 0; 763 } 764 765 766 767 static void qt2_dtr_rts(struct usb_serial_port *port, int on) 768 { 769 struct usb_device *dev = port->serial->dev; 770 struct qt2_port_private *port_priv = usb_get_serial_port_data(port); 771 772 /* Disable flow control */ 773 if (!on) { 774 if (qt2_setregister(dev, port_priv->device_port, 775 UART_MCR, 0) < 0) 776 dev_warn(&port->dev, "error from flowcontrol urb\n"); 777 } 778 /* drop RTS and DTR */ 779 if (on) 780 update_mctrl(port_priv, TIOCM_DTR | TIOCM_RTS, 0); 781 else 782 update_mctrl(port_priv, 0, TIOCM_DTR | TIOCM_RTS); 783 } 784 785 static void qt2_update_msr(struct usb_serial_port *port, unsigned char *ch) 786 { 787 struct qt2_port_private *port_priv; 788 u8 newMSR = (u8) *ch; 789 unsigned long flags; 790 791 /* May be called from qt2_process_read_urb() for an unbound port. */ 792 port_priv = usb_get_serial_port_data(port); 793 if (!port_priv) 794 return; 795 796 spin_lock_irqsave(&port_priv->lock, flags); 797 port_priv->shadowMSR = newMSR; 798 spin_unlock_irqrestore(&port_priv->lock, flags); 799 800 if (newMSR & UART_MSR_ANY_DELTA) { 801 /* update input line counters */ 802 if (newMSR & UART_MSR_DCTS) 803 port->icount.cts++; 804 if (newMSR & UART_MSR_DDSR) 805 port->icount.dsr++; 806 if (newMSR & UART_MSR_DDCD) 807 port->icount.dcd++; 808 if (newMSR & UART_MSR_TERI) 809 port->icount.rng++; 810 811 wake_up_interruptible(&port->port.delta_msr_wait); 812 } 813 } 814 815 static void qt2_update_lsr(struct usb_serial_port *port, unsigned char *ch) 816 { 817 struct qt2_port_private *port_priv; 818 struct async_icount *icount; 819 unsigned long flags; 820 u8 newLSR = (u8) *ch; 821 822 /* May be called from qt2_process_read_urb() for an unbound port. */ 823 port_priv = usb_get_serial_port_data(port); 824 if (!port_priv) 825 return; 826 827 if (newLSR & UART_LSR_BI) 828 newLSR &= (u8) (UART_LSR_OE | UART_LSR_BI); 829 830 spin_lock_irqsave(&port_priv->lock, flags); 831 port_priv->shadowLSR = newLSR; 832 spin_unlock_irqrestore(&port_priv->lock, flags); 833 834 icount = &port->icount; 835 836 if (newLSR & UART_LSR_BRK_ERROR_BITS) { 837 838 if (newLSR & UART_LSR_BI) 839 icount->brk++; 840 841 if (newLSR & UART_LSR_OE) 842 icount->overrun++; 843 844 if (newLSR & UART_LSR_PE) 845 icount->parity++; 846 847 if (newLSR & UART_LSR_FE) 848 icount->frame++; 849 } 850 851 } 852 853 static unsigned int qt2_write_room(struct tty_struct *tty) 854 { 855 struct usb_serial_port *port = tty->driver_data; 856 struct qt2_port_private *port_priv; 857 unsigned long flags; 858 unsigned int r; 859 860 port_priv = usb_get_serial_port_data(port); 861 862 spin_lock_irqsave(&port_priv->urb_lock, flags); 863 864 if (port_priv->urb_in_use) 865 r = 0; 866 else 867 r = QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE; 868 869 spin_unlock_irqrestore(&port_priv->urb_lock, flags); 870 871 return r; 872 } 873 874 static int qt2_write(struct tty_struct *tty, 875 struct usb_serial_port *port, 876 const unsigned char *buf, int count) 877 { 878 struct qt2_port_private *port_priv; 879 struct urb *write_urb; 880 unsigned char *data; 881 unsigned long flags; 882 int status; 883 int bytes_out = 0; 884 885 port_priv = usb_get_serial_port_data(port); 886 887 if (port_priv->write_urb == NULL) { 888 dev_err(&port->dev, "%s - no output urb\n", __func__); 889 return 0; 890 } 891 write_urb = port_priv->write_urb; 892 893 count = min(count, QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE); 894 895 data = write_urb->transfer_buffer; 896 spin_lock_irqsave(&port_priv->urb_lock, flags); 897 if (port_priv->urb_in_use) { 898 dev_err(&port->dev, "qt2_write - urb is in use\n"); 899 goto write_out; 900 } 901 902 *data++ = QT2_CONTROL_BYTE; 903 *data++ = QT2_CONTROL_BYTE; 904 *data++ = port_priv->device_port; 905 put_unaligned_le16(count, data); 906 data += 2; 907 memcpy(data, buf, count); 908 909 write_urb->transfer_buffer_length = count + QT2_WRITE_CONTROL_SIZE; 910 911 status = usb_submit_urb(write_urb, GFP_ATOMIC); 912 if (status == 0) { 913 port_priv->urb_in_use = true; 914 bytes_out += count; 915 } 916 917 write_out: 918 spin_unlock_irqrestore(&port_priv->urb_lock, flags); 919 return bytes_out; 920 } 921 922 923 static struct usb_serial_driver qt2_device = { 924 .driver = { 925 .name = "quatech-serial", 926 }, 927 .description = DRIVER_DESC, 928 .id_table = id_table, 929 .open = qt2_open, 930 .close = qt2_close, 931 .write = qt2_write, 932 .write_room = qt2_write_room, 933 .calc_num_ports = qt2_calc_num_ports, 934 .attach = qt2_attach, 935 .release = qt2_release, 936 .disconnect = qt2_disconnect, 937 .port_probe = qt2_port_probe, 938 .port_remove = qt2_port_remove, 939 .dtr_rts = qt2_dtr_rts, 940 .break_ctl = qt2_break_ctl, 941 .tiocmget = qt2_tiocmget, 942 .tiocmset = qt2_tiocmset, 943 .tiocmiwait = usb_serial_generic_tiocmiwait, 944 .get_icount = usb_serial_generic_get_icount, 945 .set_termios = qt2_set_termios, 946 }; 947 948 static struct usb_serial_driver *const serial_drivers[] = { 949 &qt2_device, NULL 950 }; 951 952 module_usb_serial_driver(serial_drivers, id_table); 953 954 MODULE_DESCRIPTION(DRIVER_DESC); 955 MODULE_LICENSE("GPL v2"); 956