1 /* 2 * USB Serial Converter Generic functions 3 * 4 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com) 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License version 8 * 2 as published by the Free Software Foundation. 9 * 10 */ 11 12 #include <linux/kernel.h> 13 #include <linux/errno.h> 14 #include <linux/slab.h> 15 #include <linux/sysrq.h> 16 #include <linux/tty.h> 17 #include <linux/tty_flip.h> 18 #include <linux/module.h> 19 #include <linux/moduleparam.h> 20 #include <linux/usb.h> 21 #include <linux/usb/serial.h> 22 #include <linux/uaccess.h> 23 #include <linux/kfifo.h> 24 #include <linux/serial.h> 25 26 static int debug; 27 28 #ifdef CONFIG_USB_SERIAL_GENERIC 29 30 static int generic_probe(struct usb_interface *interface, 31 const struct usb_device_id *id); 32 33 static __u16 vendor = 0x05f9; 34 static __u16 product = 0xffff; 35 36 module_param(vendor, ushort, 0); 37 MODULE_PARM_DESC(vendor, "User specified USB idVendor"); 38 39 module_param(product, ushort, 0); 40 MODULE_PARM_DESC(product, "User specified USB idProduct"); 41 42 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */ 43 44 /* we want to look at all devices, as the vendor/product id can change 45 * depending on the command line argument */ 46 static const struct usb_device_id generic_serial_ids[] = { 47 {.driver_info = 42}, 48 {} 49 }; 50 51 static struct usb_driver generic_driver = { 52 .name = "usbserial_generic", 53 .probe = generic_probe, 54 .disconnect = usb_serial_disconnect, 55 .id_table = generic_serial_ids, 56 .no_dynamic_id = 1, 57 }; 58 59 /* All of the device info needed for the Generic Serial Converter */ 60 struct usb_serial_driver usb_serial_generic_device = { 61 .driver = { 62 .owner = THIS_MODULE, 63 .name = "generic", 64 }, 65 .id_table = generic_device_ids, 66 .usb_driver = &generic_driver, 67 .num_ports = 1, 68 .disconnect = usb_serial_generic_disconnect, 69 .release = usb_serial_generic_release, 70 .throttle = usb_serial_generic_throttle, 71 .unthrottle = usb_serial_generic_unthrottle, 72 .resume = usb_serial_generic_resume, 73 }; 74 75 static int generic_probe(struct usb_interface *interface, 76 const struct usb_device_id *id) 77 { 78 const struct usb_device_id *id_pattern; 79 80 id_pattern = usb_match_id(interface, generic_device_ids); 81 if (id_pattern != NULL) 82 return usb_serial_probe(interface, id); 83 return -ENODEV; 84 } 85 #endif 86 87 int usb_serial_generic_register(int _debug) 88 { 89 int retval = 0; 90 91 debug = _debug; 92 #ifdef CONFIG_USB_SERIAL_GENERIC 93 generic_device_ids[0].idVendor = vendor; 94 generic_device_ids[0].idProduct = product; 95 generic_device_ids[0].match_flags = 96 USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT; 97 98 /* register our generic driver with ourselves */ 99 retval = usb_serial_register(&usb_serial_generic_device); 100 if (retval) 101 goto exit; 102 retval = usb_register(&generic_driver); 103 if (retval) 104 usb_serial_deregister(&usb_serial_generic_device); 105 exit: 106 #endif 107 return retval; 108 } 109 110 void usb_serial_generic_deregister(void) 111 { 112 #ifdef CONFIG_USB_SERIAL_GENERIC 113 /* remove our generic driver */ 114 usb_deregister(&generic_driver); 115 usb_serial_deregister(&usb_serial_generic_device); 116 #endif 117 } 118 119 int usb_serial_generic_open(struct tty_struct *tty, struct usb_serial_port *port) 120 { 121 struct usb_serial *serial = port->serial; 122 int result = 0; 123 unsigned long flags; 124 125 dbg("%s - port %d", __func__, port->number); 126 127 /* clear the throttle flags */ 128 spin_lock_irqsave(&port->lock, flags); 129 port->throttled = 0; 130 port->throttle_req = 0; 131 spin_unlock_irqrestore(&port->lock, flags); 132 133 /* if we have a bulk endpoint, start reading from it */ 134 if (port->bulk_in_size) { 135 /* Start reading from the device */ 136 usb_fill_bulk_urb(port->read_urb, serial->dev, 137 usb_rcvbulkpipe(serial->dev, 138 port->bulk_in_endpointAddress), 139 port->read_urb->transfer_buffer, 140 port->read_urb->transfer_buffer_length, 141 ((serial->type->read_bulk_callback) ? 142 serial->type->read_bulk_callback : 143 usb_serial_generic_read_bulk_callback), 144 port); 145 result = usb_submit_urb(port->read_urb, GFP_KERNEL); 146 if (result) 147 dev_err(&port->dev, 148 "%s - failed resubmitting read urb, error %d\n", 149 __func__, result); 150 } 151 152 return result; 153 } 154 EXPORT_SYMBOL_GPL(usb_serial_generic_open); 155 156 static void generic_cleanup(struct usb_serial_port *port) 157 { 158 struct usb_serial *serial = port->serial; 159 unsigned long flags; 160 161 dbg("%s - port %d", __func__, port->number); 162 163 if (serial->dev) { 164 /* shutdown any bulk transfers that might be going on */ 165 if (port->bulk_out_size) { 166 usb_kill_urb(port->write_urb); 167 168 spin_lock_irqsave(&port->lock, flags); 169 kfifo_reset_out(&port->write_fifo); 170 spin_unlock_irqrestore(&port->lock, flags); 171 } 172 if (port->bulk_in_size) 173 usb_kill_urb(port->read_urb); 174 } 175 } 176 177 void usb_serial_generic_close(struct usb_serial_port *port) 178 { 179 dbg("%s - port %d", __func__, port->number); 180 generic_cleanup(port); 181 } 182 EXPORT_SYMBOL_GPL(usb_serial_generic_close); 183 184 static int usb_serial_multi_urb_write(struct tty_struct *tty, 185 struct usb_serial_port *port, const unsigned char *buf, int count) 186 { 187 unsigned long flags; 188 struct urb *urb; 189 unsigned char *buffer; 190 int status; 191 int towrite; 192 int bwrite = 0; 193 194 dbg("%s - port %d", __func__, port->number); 195 196 if (count == 0) 197 dbg("%s - write request of 0 bytes", __func__); 198 199 while (count > 0) { 200 towrite = (count > port->bulk_out_size) ? 201 port->bulk_out_size : count; 202 spin_lock_irqsave(&port->lock, flags); 203 if (port->urbs_in_flight > 204 port->serial->type->max_in_flight_urbs) { 205 spin_unlock_irqrestore(&port->lock, flags); 206 dbg("%s - write limit hit", __func__); 207 return bwrite; 208 } 209 port->tx_bytes_flight += towrite; 210 port->urbs_in_flight++; 211 spin_unlock_irqrestore(&port->lock, flags); 212 213 buffer = kmalloc(towrite, GFP_ATOMIC); 214 if (!buffer) { 215 dev_err(&port->dev, 216 "%s ran out of kernel memory for urb ...\n", __func__); 217 goto error_no_buffer; 218 } 219 220 urb = usb_alloc_urb(0, GFP_ATOMIC); 221 if (!urb) { 222 dev_err(&port->dev, "%s - no more free urbs\n", 223 __func__); 224 goto error_no_urb; 225 } 226 227 /* Copy data */ 228 memcpy(buffer, buf + bwrite, towrite); 229 usb_serial_debug_data(debug, &port->dev, __func__, 230 towrite, buffer); 231 /* fill the buffer and send it */ 232 usb_fill_bulk_urb(urb, port->serial->dev, 233 usb_sndbulkpipe(port->serial->dev, 234 port->bulk_out_endpointAddress), 235 buffer, towrite, 236 usb_serial_generic_write_bulk_callback, port); 237 238 status = usb_submit_urb(urb, GFP_ATOMIC); 239 if (status) { 240 dev_err(&port->dev, 241 "%s - failed submitting write urb, error %d\n", 242 __func__, status); 243 goto error; 244 } 245 246 /* This urb is the responsibility of the host driver now */ 247 usb_free_urb(urb); 248 dbg("%s write: %d", __func__, towrite); 249 count -= towrite; 250 bwrite += towrite; 251 } 252 return bwrite; 253 254 error: 255 usb_free_urb(urb); 256 error_no_urb: 257 kfree(buffer); 258 error_no_buffer: 259 spin_lock_irqsave(&port->lock, flags); 260 port->urbs_in_flight--; 261 port->tx_bytes_flight -= towrite; 262 spin_unlock_irqrestore(&port->lock, flags); 263 return bwrite; 264 } 265 266 /** 267 * usb_serial_generic_write_start - kick off an URB write 268 * @port: Pointer to the &struct usb_serial_port data 269 * 270 * Returns the number of bytes queued on success. This will be zero if there 271 * was nothing to send. Otherwise, it returns a negative errno value 272 */ 273 static int usb_serial_generic_write_start(struct usb_serial_port *port) 274 { 275 struct usb_serial *serial = port->serial; 276 unsigned char *data; 277 int result; 278 int count; 279 unsigned long flags; 280 bool start_io; 281 282 /* Atomically determine whether we can and need to start a USB 283 * operation. */ 284 spin_lock_irqsave(&port->lock, flags); 285 if (port->write_urb_busy) 286 start_io = false; 287 else { 288 start_io = (kfifo_len(&port->write_fifo) != 0); 289 port->write_urb_busy = start_io; 290 } 291 spin_unlock_irqrestore(&port->lock, flags); 292 293 if (!start_io) 294 return 0; 295 296 data = port->write_urb->transfer_buffer; 297 count = kfifo_out_locked(&port->write_fifo, data, port->bulk_out_size, &port->lock); 298 usb_serial_debug_data(debug, &port->dev, __func__, count, data); 299 300 /* set up our urb */ 301 usb_fill_bulk_urb(port->write_urb, serial->dev, 302 usb_sndbulkpipe(serial->dev, 303 port->bulk_out_endpointAddress), 304 port->write_urb->transfer_buffer, count, 305 ((serial->type->write_bulk_callback) ? 306 serial->type->write_bulk_callback : 307 usb_serial_generic_write_bulk_callback), 308 port); 309 310 /* send the data out the bulk port */ 311 result = usb_submit_urb(port->write_urb, GFP_ATOMIC); 312 if (result) { 313 dev_err(&port->dev, 314 "%s - failed submitting write urb, error %d\n", 315 __func__, result); 316 /* don't have to grab the lock here, as we will 317 retry if != 0 */ 318 port->write_urb_busy = 0; 319 return result; 320 } 321 322 spin_lock_irqsave(&port->lock, flags); 323 port->tx_bytes_flight += count; 324 spin_unlock_irqrestore(&port->lock, flags); 325 326 return count; 327 } 328 329 /** 330 * usb_serial_generic_write - generic write function for serial USB devices 331 * @tty: Pointer to &struct tty_struct for the device 332 * @port: Pointer to the &usb_serial_port structure for the device 333 * @buf: Pointer to the data to write 334 * @count: Number of bytes to write 335 * 336 * Returns the number of characters actually written, which may be anything 337 * from zero to @count. If an error occurs, it returns the negative errno 338 * value. 339 */ 340 int usb_serial_generic_write(struct tty_struct *tty, 341 struct usb_serial_port *port, const unsigned char *buf, int count) 342 { 343 struct usb_serial *serial = port->serial; 344 int result; 345 346 dbg("%s - port %d", __func__, port->number); 347 348 /* only do something if we have a bulk out endpoint */ 349 if (!port->bulk_out_size) 350 return -ENODEV; 351 352 if (count == 0) { 353 dbg("%s - write request of 0 bytes", __func__); 354 return 0; 355 } 356 357 if (serial->type->max_in_flight_urbs) 358 return usb_serial_multi_urb_write(tty, port, 359 buf, count); 360 361 count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock); 362 result = usb_serial_generic_write_start(port); 363 364 if (result >= 0) 365 result = count; 366 367 return result; 368 } 369 EXPORT_SYMBOL_GPL(usb_serial_generic_write); 370 371 int usb_serial_generic_write_room(struct tty_struct *tty) 372 { 373 struct usb_serial_port *port = tty->driver_data; 374 struct usb_serial *serial = port->serial; 375 unsigned long flags; 376 int room = 0; 377 378 dbg("%s - port %d", __func__, port->number); 379 380 if (!port->bulk_out_size) 381 return 0; 382 383 spin_lock_irqsave(&port->lock, flags); 384 if (serial->type->max_in_flight_urbs) { 385 if (port->urbs_in_flight < serial->type->max_in_flight_urbs) 386 room = port->bulk_out_size * 387 (serial->type->max_in_flight_urbs - 388 port->urbs_in_flight); 389 } else { 390 room = kfifo_avail(&port->write_fifo); 391 } 392 spin_unlock_irqrestore(&port->lock, flags); 393 394 dbg("%s - returns %d", __func__, room); 395 return room; 396 } 397 398 int usb_serial_generic_chars_in_buffer(struct tty_struct *tty) 399 { 400 struct usb_serial_port *port = tty->driver_data; 401 struct usb_serial *serial = port->serial; 402 unsigned long flags; 403 int chars; 404 405 dbg("%s - port %d", __func__, port->number); 406 407 if (!port->bulk_out_size) 408 return 0; 409 410 spin_lock_irqsave(&port->lock, flags); 411 if (serial->type->max_in_flight_urbs) 412 chars = port->tx_bytes_flight; 413 else 414 chars = kfifo_len(&port->write_fifo) + port->tx_bytes_flight; 415 spin_unlock_irqrestore(&port->lock, flags); 416 417 dbg("%s - returns %d", __func__, chars); 418 return chars; 419 } 420 421 422 void usb_serial_generic_resubmit_read_urb(struct usb_serial_port *port, 423 gfp_t mem_flags) 424 { 425 struct urb *urb = port->read_urb; 426 struct usb_serial *serial = port->serial; 427 int result; 428 429 /* Continue reading from device */ 430 usb_fill_bulk_urb(urb, serial->dev, 431 usb_rcvbulkpipe(serial->dev, 432 port->bulk_in_endpointAddress), 433 urb->transfer_buffer, 434 urb->transfer_buffer_length, 435 ((serial->type->read_bulk_callback) ? 436 serial->type->read_bulk_callback : 437 usb_serial_generic_read_bulk_callback), port); 438 439 result = usb_submit_urb(urb, mem_flags); 440 if (result && result != -EPERM) { 441 dev_err(&port->dev, 442 "%s - failed resubmitting read urb, error %d\n", 443 __func__, result); 444 } 445 } 446 EXPORT_SYMBOL_GPL(usb_serial_generic_resubmit_read_urb); 447 448 /* Push data to tty layer and resubmit the bulk read URB */ 449 static void flush_and_resubmit_read_urb(struct usb_serial_port *port) 450 { 451 struct urb *urb = port->read_urb; 452 struct tty_struct *tty = tty_port_tty_get(&port->port); 453 char *ch = (char *)urb->transfer_buffer; 454 int i; 455 456 if (!tty) 457 goto done; 458 459 /* The per character mucking around with sysrq path it too slow for 460 stuff like 3G modems, so shortcircuit it in the 99.9999999% of cases 461 where the USB serial is not a console anyway */ 462 if (!port->port.console || !port->sysrq) 463 tty_insert_flip_string(tty, ch, urb->actual_length); 464 else { 465 /* Push data to tty */ 466 for (i = 0; i < urb->actual_length; i++, ch++) { 467 if (!usb_serial_handle_sysrq_char(tty, port, *ch)) 468 tty_insert_flip_char(tty, *ch, TTY_NORMAL); 469 } 470 } 471 tty_flip_buffer_push(tty); 472 tty_kref_put(tty); 473 done: 474 usb_serial_generic_resubmit_read_urb(port, GFP_ATOMIC); 475 } 476 477 void usb_serial_generic_read_bulk_callback(struct urb *urb) 478 { 479 struct usb_serial_port *port = urb->context; 480 unsigned char *data = urb->transfer_buffer; 481 int status = urb->status; 482 unsigned long flags; 483 484 dbg("%s - port %d", __func__, port->number); 485 486 if (unlikely(status != 0)) { 487 dbg("%s - nonzero read bulk status received: %d", 488 __func__, status); 489 return; 490 } 491 492 usb_serial_debug_data(debug, &port->dev, __func__, 493 urb->actual_length, data); 494 495 /* Throttle the device if requested by tty */ 496 spin_lock_irqsave(&port->lock, flags); 497 port->throttled = port->throttle_req; 498 if (!port->throttled) { 499 spin_unlock_irqrestore(&port->lock, flags); 500 flush_and_resubmit_read_urb(port); 501 } else 502 spin_unlock_irqrestore(&port->lock, flags); 503 } 504 EXPORT_SYMBOL_GPL(usb_serial_generic_read_bulk_callback); 505 506 void usb_serial_generic_write_bulk_callback(struct urb *urb) 507 { 508 unsigned long flags; 509 struct usb_serial_port *port = urb->context; 510 int status = urb->status; 511 512 dbg("%s - port %d", __func__, port->number); 513 514 if (port->serial->type->max_in_flight_urbs) { 515 kfree(urb->transfer_buffer); 516 517 spin_lock_irqsave(&port->lock, flags); 518 --port->urbs_in_flight; 519 port->tx_bytes_flight -= urb->transfer_buffer_length; 520 if (port->urbs_in_flight < 0) 521 port->urbs_in_flight = 0; 522 spin_unlock_irqrestore(&port->lock, flags); 523 } else { 524 spin_lock_irqsave(&port->lock, flags); 525 port->tx_bytes_flight -= urb->transfer_buffer_length; 526 port->write_urb_busy = 0; 527 spin_unlock_irqrestore(&port->lock, flags); 528 529 if (status) { 530 spin_lock_irqsave(&port->lock, flags); 531 kfifo_reset_out(&port->write_fifo); 532 spin_unlock_irqrestore(&port->lock, flags); 533 } else { 534 usb_serial_generic_write_start(port); 535 } 536 } 537 538 if (status) 539 dbg("%s - non-zero urb status: %d", __func__, status); 540 541 usb_serial_port_softint(port); 542 } 543 EXPORT_SYMBOL_GPL(usb_serial_generic_write_bulk_callback); 544 545 void usb_serial_generic_throttle(struct tty_struct *tty) 546 { 547 struct usb_serial_port *port = tty->driver_data; 548 unsigned long flags; 549 550 dbg("%s - port %d", __func__, port->number); 551 552 /* Set the throttle request flag. It will be picked up 553 * by usb_serial_generic_read_bulk_callback(). */ 554 spin_lock_irqsave(&port->lock, flags); 555 port->throttle_req = 1; 556 spin_unlock_irqrestore(&port->lock, flags); 557 } 558 559 void usb_serial_generic_unthrottle(struct tty_struct *tty) 560 { 561 struct usb_serial_port *port = tty->driver_data; 562 int was_throttled; 563 unsigned long flags; 564 565 dbg("%s - port %d", __func__, port->number); 566 567 /* Clear the throttle flags */ 568 spin_lock_irqsave(&port->lock, flags); 569 was_throttled = port->throttled; 570 port->throttled = port->throttle_req = 0; 571 spin_unlock_irqrestore(&port->lock, flags); 572 573 if (was_throttled) { 574 /* Resume reading from device */ 575 flush_and_resubmit_read_urb(port); 576 } 577 } 578 579 #ifdef CONFIG_MAGIC_SYSRQ 580 int usb_serial_handle_sysrq_char(struct tty_struct *tty, 581 struct usb_serial_port *port, unsigned int ch) 582 { 583 if (port->sysrq && port->port.console) { 584 if (ch && time_before(jiffies, port->sysrq)) { 585 handle_sysrq(ch, tty); 586 port->sysrq = 0; 587 return 1; 588 } 589 port->sysrq = 0; 590 } 591 return 0; 592 } 593 #else 594 int usb_serial_handle_sysrq_char(struct tty_struct *tty, 595 struct usb_serial_port *port, unsigned int ch) 596 { 597 return 0; 598 } 599 #endif 600 EXPORT_SYMBOL_GPL(usb_serial_handle_sysrq_char); 601 602 int usb_serial_handle_break(struct usb_serial_port *port) 603 { 604 if (!port->sysrq) { 605 port->sysrq = jiffies + HZ*5; 606 return 1; 607 } 608 port->sysrq = 0; 609 return 0; 610 } 611 EXPORT_SYMBOL_GPL(usb_serial_handle_break); 612 613 int usb_serial_generic_resume(struct usb_serial *serial) 614 { 615 struct usb_serial_port *port; 616 int i, c = 0, r; 617 618 for (i = 0; i < serial->num_ports; i++) { 619 port = serial->port[i]; 620 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags)) 621 continue; 622 623 if (port->read_urb) { 624 r = usb_submit_urb(port->read_urb, GFP_NOIO); 625 if (r < 0) 626 c++; 627 } 628 629 if (port->write_urb) { 630 r = usb_serial_generic_write_start(port); 631 if (r < 0) 632 c++; 633 } 634 } 635 636 return c ? -EIO : 0; 637 } 638 EXPORT_SYMBOL_GPL(usb_serial_generic_resume); 639 640 void usb_serial_generic_disconnect(struct usb_serial *serial) 641 { 642 int i; 643 644 dbg("%s", __func__); 645 646 /* stop reads and writes on all ports */ 647 for (i = 0; i < serial->num_ports; ++i) 648 generic_cleanup(serial->port[i]); 649 } 650 651 void usb_serial_generic_release(struct usb_serial *serial) 652 { 653 dbg("%s", __func__); 654 } 655