1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * u_serial.c - utilities for USB gadget "serial port"/TTY support 4 * 5 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com) 6 * Copyright (C) 2008 David Brownell 7 * Copyright (C) 2008 by Nokia Corporation 8 * 9 * This code also borrows from usbserial.c, which is 10 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com) 11 * Copyright (C) 2000 Peter Berger (pberger@brimson.com) 12 * Copyright (C) 2000 Al Borchers (alborchers@steinerpoint.com) 13 */ 14 15 /* #define VERBOSE_DEBUG */ 16 17 #include <linux/kernel.h> 18 #include <linux/sched.h> 19 #include <linux/device.h> 20 #include <linux/delay.h> 21 #include <linux/tty.h> 22 #include <linux/tty_flip.h> 23 #include <linux/slab.h> 24 #include <linux/export.h> 25 #include <linux/module.h> 26 #include <linux/console.h> 27 #include <linux/kthread.h> 28 #include <linux/workqueue.h> 29 #include <linux/kfifo.h> 30 31 #include "u_serial.h" 32 33 34 /* 35 * This component encapsulates the TTY layer glue needed to provide basic 36 * "serial port" functionality through the USB gadget stack. Each such 37 * port is exposed through a /dev/ttyGS* node. 38 * 39 * After this module has been loaded, the individual TTY port can be requested 40 * (gserial_alloc_line()) and it will stay available until they are removed 41 * (gserial_free_line()). Each one may be connected to a USB function 42 * (gserial_connect), or disconnected (with gserial_disconnect) when the USB 43 * host issues a config change event. Data can only flow when the port is 44 * connected to the host. 45 * 46 * A given TTY port can be made available in multiple configurations. 47 * For example, each one might expose a ttyGS0 node which provides a 48 * login application. In one case that might use CDC ACM interface 0, 49 * while another configuration might use interface 3 for that. The 50 * work to handle that (including descriptor management) is not part 51 * of this component. 52 * 53 * Configurations may expose more than one TTY port. For example, if 54 * ttyGS0 provides login service, then ttyGS1 might provide dialer access 55 * for a telephone or fax link. And ttyGS2 might be something that just 56 * needs a simple byte stream interface for some messaging protocol that 57 * is managed in userspace ... OBEX, PTP, and MTP have been mentioned. 58 * 59 * 60 * gserial is the lifecycle interface, used by USB functions 61 * gs_port is the I/O nexus, used by the tty driver 62 * tty_struct links to the tty/filesystem framework 63 * 64 * gserial <---> gs_port ... links will be null when the USB link is 65 * inactive; managed by gserial_{connect,disconnect}(). each gserial 66 * instance can wrap its own USB control protocol. 67 * gserial->ioport == usb_ep->driver_data ... gs_port 68 * gs_port->port_usb ... gserial 69 * 70 * gs_port <---> tty_struct ... links will be null when the TTY file 71 * isn't opened; managed by gs_open()/gs_close() 72 * gserial->port_tty ... tty_struct 73 * tty_struct->driver_data ... gserial 74 */ 75 76 /* RX and TX queues can buffer QUEUE_SIZE packets before they hit the 77 * next layer of buffering. For TX that's a circular buffer; for RX 78 * consider it a NOP. A third layer is provided by the TTY code. 79 */ 80 #define QUEUE_SIZE 16 81 #define WRITE_BUF_SIZE 8192 /* TX only */ 82 #define GS_CONSOLE_BUF_SIZE 8192 83 84 /* console info */ 85 struct gs_console { 86 struct console console; 87 struct work_struct work; 88 spinlock_t lock; 89 struct usb_request *req; 90 struct kfifo buf; 91 size_t missed; 92 }; 93 94 /* 95 * The port structure holds info for each port, one for each minor number 96 * (and thus for each /dev/ node). 97 */ 98 struct gs_port { 99 struct tty_port port; 100 spinlock_t port_lock; /* guard port_* access */ 101 102 struct gserial *port_usb; 103 #ifdef CONFIG_U_SERIAL_CONSOLE 104 struct gs_console *console; 105 #endif 106 107 bool openclose; /* open/close in progress */ 108 u8 port_num; 109 110 struct list_head read_pool; 111 int read_started; 112 int read_allocated; 113 struct list_head read_queue; 114 unsigned n_read; 115 struct delayed_work push; 116 117 struct list_head write_pool; 118 int write_started; 119 int write_allocated; 120 struct kfifo port_write_buf; 121 wait_queue_head_t drain_wait; /* wait while writes drain */ 122 bool write_busy; 123 wait_queue_head_t close_wait; 124 125 /* REVISIT this state ... */ 126 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */ 127 }; 128 129 static struct portmaster { 130 struct mutex lock; /* protect open/close */ 131 struct gs_port *port; 132 } ports[MAX_U_SERIAL_PORTS]; 133 134 #define GS_CLOSE_TIMEOUT 15 /* seconds */ 135 136 137 138 #ifdef VERBOSE_DEBUG 139 #ifndef pr_vdebug 140 #define pr_vdebug(fmt, arg...) \ 141 pr_debug(fmt, ##arg) 142 #endif /* pr_vdebug */ 143 #else 144 #ifndef pr_vdebug 145 #define pr_vdebug(fmt, arg...) \ 146 ({ if (0) pr_debug(fmt, ##arg); }) 147 #endif /* pr_vdebug */ 148 #endif 149 150 /*-------------------------------------------------------------------------*/ 151 152 /* I/O glue between TTY (upper) and USB function (lower) driver layers */ 153 154 /* 155 * gs_alloc_req 156 * 157 * Allocate a usb_request and its buffer. Returns a pointer to the 158 * usb_request or NULL if there is an error. 159 */ 160 struct usb_request * 161 gs_alloc_req(struct usb_ep *ep, unsigned len, gfp_t kmalloc_flags) 162 { 163 struct usb_request *req; 164 165 req = usb_ep_alloc_request(ep, kmalloc_flags); 166 167 if (req != NULL) { 168 req->length = len; 169 req->buf = kmalloc(len, kmalloc_flags); 170 if (req->buf == NULL) { 171 usb_ep_free_request(ep, req); 172 return NULL; 173 } 174 } 175 176 return req; 177 } 178 EXPORT_SYMBOL_GPL(gs_alloc_req); 179 180 /* 181 * gs_free_req 182 * 183 * Free a usb_request and its buffer. 184 */ 185 void gs_free_req(struct usb_ep *ep, struct usb_request *req) 186 { 187 kfree(req->buf); 188 usb_ep_free_request(ep, req); 189 } 190 EXPORT_SYMBOL_GPL(gs_free_req); 191 192 /* 193 * gs_send_packet 194 * 195 * If there is data to send, a packet is built in the given 196 * buffer and the size is returned. If there is no data to 197 * send, 0 is returned. 198 * 199 * Called with port_lock held. 200 */ 201 static unsigned 202 gs_send_packet(struct gs_port *port, char *packet, unsigned size) 203 { 204 unsigned len; 205 206 len = kfifo_len(&port->port_write_buf); 207 if (len < size) 208 size = len; 209 if (size != 0) 210 size = kfifo_out(&port->port_write_buf, packet, size); 211 return size; 212 } 213 214 /* 215 * gs_start_tx 216 * 217 * This function finds available write requests, calls 218 * gs_send_packet to fill these packets with data, and 219 * continues until either there are no more write requests 220 * available or no more data to send. This function is 221 * run whenever data arrives or write requests are available. 222 * 223 * Context: caller owns port_lock; port_usb is non-null. 224 */ 225 static int gs_start_tx(struct gs_port *port) 226 /* 227 __releases(&port->port_lock) 228 __acquires(&port->port_lock) 229 */ 230 { 231 struct list_head *pool = &port->write_pool; 232 struct usb_ep *in; 233 int status = 0; 234 bool do_tty_wake = false; 235 236 if (!port->port_usb) 237 return status; 238 239 in = port->port_usb->in; 240 241 while (!port->write_busy && !list_empty(pool)) { 242 struct usb_request *req; 243 int len; 244 245 if (port->write_started >= QUEUE_SIZE) 246 break; 247 248 req = list_entry(pool->next, struct usb_request, list); 249 len = gs_send_packet(port, req->buf, in->maxpacket); 250 if (len == 0) { 251 wake_up_interruptible(&port->drain_wait); 252 break; 253 } 254 do_tty_wake = true; 255 256 req->length = len; 257 list_del(&req->list); 258 req->zero = kfifo_is_empty(&port->port_write_buf); 259 260 pr_vdebug("ttyGS%d: tx len=%d, 0x%02x 0x%02x 0x%02x ...\n", 261 port->port_num, len, *((u8 *)req->buf), 262 *((u8 *)req->buf+1), *((u8 *)req->buf+2)); 263 264 /* Drop lock while we call out of driver; completions 265 * could be issued while we do so. Disconnection may 266 * happen too; maybe immediately before we queue this! 267 * 268 * NOTE that we may keep sending data for a while after 269 * the TTY closed (dev->ioport->port_tty is NULL). 270 */ 271 port->write_busy = true; 272 spin_unlock(&port->port_lock); 273 status = usb_ep_queue(in, req, GFP_ATOMIC); 274 spin_lock(&port->port_lock); 275 port->write_busy = false; 276 277 if (status) { 278 pr_debug("%s: %s %s err %d\n", 279 __func__, "queue", in->name, status); 280 list_add(&req->list, pool); 281 break; 282 } 283 284 port->write_started++; 285 286 /* abort immediately after disconnect */ 287 if (!port->port_usb) 288 break; 289 } 290 291 if (do_tty_wake && port->port.tty) 292 tty_wakeup(port->port.tty); 293 return status; 294 } 295 296 /* 297 * Context: caller owns port_lock, and port_usb is set 298 */ 299 static unsigned gs_start_rx(struct gs_port *port) 300 /* 301 __releases(&port->port_lock) 302 __acquires(&port->port_lock) 303 */ 304 { 305 struct list_head *pool = &port->read_pool; 306 struct usb_ep *out = port->port_usb->out; 307 308 while (!list_empty(pool)) { 309 struct usb_request *req; 310 int status; 311 struct tty_struct *tty; 312 313 /* no more rx if closed */ 314 tty = port->port.tty; 315 if (!tty) 316 break; 317 318 if (port->read_started >= QUEUE_SIZE) 319 break; 320 321 req = list_entry(pool->next, struct usb_request, list); 322 list_del(&req->list); 323 req->length = out->maxpacket; 324 325 /* drop lock while we call out; the controller driver 326 * may need to call us back (e.g. for disconnect) 327 */ 328 spin_unlock(&port->port_lock); 329 status = usb_ep_queue(out, req, GFP_ATOMIC); 330 spin_lock(&port->port_lock); 331 332 if (status) { 333 pr_debug("%s: %s %s err %d\n", 334 __func__, "queue", out->name, status); 335 list_add(&req->list, pool); 336 break; 337 } 338 port->read_started++; 339 340 /* abort immediately after disconnect */ 341 if (!port->port_usb) 342 break; 343 } 344 return port->read_started; 345 } 346 347 /* 348 * RX tasklet takes data out of the RX queue and hands it up to the TTY 349 * layer until it refuses to take any more data (or is throttled back). 350 * Then it issues reads for any further data. 351 * 352 * If the RX queue becomes full enough that no usb_request is queued, 353 * the OUT endpoint may begin NAKing as soon as its FIFO fills up. 354 * So QUEUE_SIZE packets plus however many the FIFO holds (usually two) 355 * can be buffered before the TTY layer's buffers (currently 64 KB). 356 */ 357 static void gs_rx_push(struct work_struct *work) 358 { 359 struct delayed_work *w = to_delayed_work(work); 360 struct gs_port *port = container_of(w, struct gs_port, push); 361 struct tty_struct *tty; 362 struct list_head *queue = &port->read_queue; 363 bool disconnect = false; 364 bool do_push = false; 365 366 /* hand any queued data to the tty */ 367 spin_lock_irq(&port->port_lock); 368 tty = port->port.tty; 369 while (!list_empty(queue)) { 370 struct usb_request *req; 371 372 req = list_first_entry(queue, struct usb_request, list); 373 374 /* leave data queued if tty was rx throttled */ 375 if (tty && tty_throttled(tty)) 376 break; 377 378 switch (req->status) { 379 case -ESHUTDOWN: 380 disconnect = true; 381 pr_vdebug("ttyGS%d: shutdown\n", port->port_num); 382 break; 383 384 default: 385 /* presumably a transient fault */ 386 pr_warn("ttyGS%d: unexpected RX status %d\n", 387 port->port_num, req->status); 388 /* FALLTHROUGH */ 389 case 0: 390 /* normal completion */ 391 break; 392 } 393 394 /* push data to (open) tty */ 395 if (req->actual && tty) { 396 char *packet = req->buf; 397 unsigned size = req->actual; 398 unsigned n; 399 int count; 400 401 /* we may have pushed part of this packet already... */ 402 n = port->n_read; 403 if (n) { 404 packet += n; 405 size -= n; 406 } 407 408 count = tty_insert_flip_string(&port->port, packet, 409 size); 410 if (count) 411 do_push = true; 412 if (count != size) { 413 /* stop pushing; TTY layer can't handle more */ 414 port->n_read += count; 415 pr_vdebug("ttyGS%d: rx block %d/%d\n", 416 port->port_num, count, req->actual); 417 break; 418 } 419 port->n_read = 0; 420 } 421 422 list_move(&req->list, &port->read_pool); 423 port->read_started--; 424 } 425 426 /* Push from tty to ldisc; this is handled by a workqueue, 427 * so we won't get callbacks and can hold port_lock 428 */ 429 if (do_push) 430 tty_flip_buffer_push(&port->port); 431 432 433 /* We want our data queue to become empty ASAP, keeping data 434 * in the tty and ldisc (not here). If we couldn't push any 435 * this time around, RX may be starved, so wait until next jiffy. 436 * 437 * We may leave non-empty queue only when there is a tty, and 438 * either it is throttled or there is no more room in flip buffer. 439 */ 440 if (!list_empty(queue) && !tty_throttled(tty)) 441 schedule_delayed_work(&port->push, 1); 442 443 /* If we're still connected, refill the USB RX queue. */ 444 if (!disconnect && port->port_usb) 445 gs_start_rx(port); 446 447 spin_unlock_irq(&port->port_lock); 448 } 449 450 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req) 451 { 452 struct gs_port *port = ep->driver_data; 453 454 /* Queue all received data until the tty layer is ready for it. */ 455 spin_lock(&port->port_lock); 456 list_add_tail(&req->list, &port->read_queue); 457 schedule_delayed_work(&port->push, 0); 458 spin_unlock(&port->port_lock); 459 } 460 461 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req) 462 { 463 struct gs_port *port = ep->driver_data; 464 465 spin_lock(&port->port_lock); 466 list_add(&req->list, &port->write_pool); 467 port->write_started--; 468 469 switch (req->status) { 470 default: 471 /* presumably a transient fault */ 472 pr_warn("%s: unexpected %s status %d\n", 473 __func__, ep->name, req->status); 474 /* FALL THROUGH */ 475 case 0: 476 /* normal completion */ 477 gs_start_tx(port); 478 break; 479 480 case -ESHUTDOWN: 481 /* disconnect */ 482 pr_vdebug("%s: %s shutdown\n", __func__, ep->name); 483 break; 484 } 485 486 spin_unlock(&port->port_lock); 487 } 488 489 static void gs_free_requests(struct usb_ep *ep, struct list_head *head, 490 int *allocated) 491 { 492 struct usb_request *req; 493 494 while (!list_empty(head)) { 495 req = list_entry(head->next, struct usb_request, list); 496 list_del(&req->list); 497 gs_free_req(ep, req); 498 if (allocated) 499 (*allocated)--; 500 } 501 } 502 503 static int gs_alloc_requests(struct usb_ep *ep, struct list_head *head, 504 void (*fn)(struct usb_ep *, struct usb_request *), 505 int *allocated) 506 { 507 int i; 508 struct usb_request *req; 509 int n = allocated ? QUEUE_SIZE - *allocated : QUEUE_SIZE; 510 511 /* Pre-allocate up to QUEUE_SIZE transfers, but if we can't 512 * do quite that many this time, don't fail ... we just won't 513 * be as speedy as we might otherwise be. 514 */ 515 for (i = 0; i < n; i++) { 516 req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC); 517 if (!req) 518 return list_empty(head) ? -ENOMEM : 0; 519 req->complete = fn; 520 list_add_tail(&req->list, head); 521 if (allocated) 522 (*allocated)++; 523 } 524 return 0; 525 } 526 527 /** 528 * gs_start_io - start USB I/O streams 529 * @dev: encapsulates endpoints to use 530 * Context: holding port_lock; port_tty and port_usb are non-null 531 * 532 * We only start I/O when something is connected to both sides of 533 * this port. If nothing is listening on the host side, we may 534 * be pointlessly filling up our TX buffers and FIFO. 535 */ 536 static int gs_start_io(struct gs_port *port) 537 { 538 struct list_head *head = &port->read_pool; 539 struct usb_ep *ep = port->port_usb->out; 540 int status; 541 unsigned started; 542 543 /* Allocate RX and TX I/O buffers. We can't easily do this much 544 * earlier (with GFP_KERNEL) because the requests are coupled to 545 * endpoints, as are the packet sizes we'll be using. Different 546 * configurations may use different endpoints with a given port; 547 * and high speed vs full speed changes packet sizes too. 548 */ 549 status = gs_alloc_requests(ep, head, gs_read_complete, 550 &port->read_allocated); 551 if (status) 552 return status; 553 554 status = gs_alloc_requests(port->port_usb->in, &port->write_pool, 555 gs_write_complete, &port->write_allocated); 556 if (status) { 557 gs_free_requests(ep, head, &port->read_allocated); 558 return status; 559 } 560 561 /* queue read requests */ 562 port->n_read = 0; 563 started = gs_start_rx(port); 564 565 /* unblock any pending writes into our circular buffer */ 566 if (started) { 567 tty_wakeup(port->port.tty); 568 } else { 569 gs_free_requests(ep, head, &port->read_allocated); 570 gs_free_requests(port->port_usb->in, &port->write_pool, 571 &port->write_allocated); 572 status = -EIO; 573 } 574 575 return status; 576 } 577 578 /*-------------------------------------------------------------------------*/ 579 580 /* TTY Driver */ 581 582 /* 583 * gs_open sets up the link between a gs_port and its associated TTY. 584 * That link is broken *only* by TTY close(), and all driver methods 585 * know that. 586 */ 587 static int gs_open(struct tty_struct *tty, struct file *file) 588 { 589 int port_num = tty->index; 590 struct gs_port *port; 591 int status; 592 593 do { 594 mutex_lock(&ports[port_num].lock); 595 port = ports[port_num].port; 596 if (!port) 597 status = -ENODEV; 598 else { 599 spin_lock_irq(&port->port_lock); 600 601 /* already open? Great. */ 602 if (port->port.count) { 603 status = 0; 604 port->port.count++; 605 606 /* currently opening/closing? wait ... */ 607 } else if (port->openclose) { 608 status = -EBUSY; 609 610 /* ... else we do the work */ 611 } else { 612 status = -EAGAIN; 613 port->openclose = true; 614 } 615 spin_unlock_irq(&port->port_lock); 616 } 617 mutex_unlock(&ports[port_num].lock); 618 619 switch (status) { 620 default: 621 /* fully handled */ 622 return status; 623 case -EAGAIN: 624 /* must do the work */ 625 break; 626 case -EBUSY: 627 /* wait for EAGAIN task to finish */ 628 msleep(1); 629 /* REVISIT could have a waitchannel here, if 630 * concurrent open performance is important 631 */ 632 break; 633 } 634 } while (status != -EAGAIN); 635 636 /* Do the "real open" */ 637 spin_lock_irq(&port->port_lock); 638 639 /* allocate circular buffer on first open */ 640 if (!kfifo_initialized(&port->port_write_buf)) { 641 642 spin_unlock_irq(&port->port_lock); 643 status = kfifo_alloc(&port->port_write_buf, 644 WRITE_BUF_SIZE, GFP_KERNEL); 645 spin_lock_irq(&port->port_lock); 646 647 if (status) { 648 pr_debug("gs_open: ttyGS%d (%p,%p) no buffer\n", 649 port->port_num, tty, file); 650 port->openclose = false; 651 goto exit_unlock_port; 652 } 653 } 654 655 /* REVISIT if REMOVED (ports[].port NULL), abort the open 656 * to let rmmod work faster (but this way isn't wrong). 657 */ 658 659 /* REVISIT maybe wait for "carrier detect" */ 660 661 tty->driver_data = port; 662 port->port.tty = tty; 663 664 port->port.count = 1; 665 port->openclose = false; 666 667 /* if connected, start the I/O stream */ 668 if (port->port_usb) { 669 struct gserial *gser = port->port_usb; 670 671 pr_debug("gs_open: start ttyGS%d\n", port->port_num); 672 gs_start_io(port); 673 674 if (gser->connect) 675 gser->connect(gser); 676 } 677 678 pr_debug("gs_open: ttyGS%d (%p,%p)\n", port->port_num, tty, file); 679 680 status = 0; 681 682 exit_unlock_port: 683 spin_unlock_irq(&port->port_lock); 684 return status; 685 } 686 687 static int gs_writes_finished(struct gs_port *p) 688 { 689 int cond; 690 691 /* return true on disconnect or empty buffer */ 692 spin_lock_irq(&p->port_lock); 693 cond = (p->port_usb == NULL) || !kfifo_len(&p->port_write_buf); 694 spin_unlock_irq(&p->port_lock); 695 696 return cond; 697 } 698 699 static void gs_close(struct tty_struct *tty, struct file *file) 700 { 701 struct gs_port *port = tty->driver_data; 702 struct gserial *gser; 703 704 spin_lock_irq(&port->port_lock); 705 706 if (port->port.count != 1) { 707 if (port->port.count == 0) 708 WARN_ON(1); 709 else 710 --port->port.count; 711 goto exit; 712 } 713 714 pr_debug("gs_close: ttyGS%d (%p,%p) ...\n", port->port_num, tty, file); 715 716 /* mark port as closing but in use; we can drop port lock 717 * and sleep if necessary 718 */ 719 port->openclose = true; 720 port->port.count = 0; 721 722 gser = port->port_usb; 723 if (gser && gser->disconnect) 724 gser->disconnect(gser); 725 726 /* wait for circular write buffer to drain, disconnect, or at 727 * most GS_CLOSE_TIMEOUT seconds; then discard the rest 728 */ 729 if (kfifo_len(&port->port_write_buf) > 0 && gser) { 730 spin_unlock_irq(&port->port_lock); 731 wait_event_interruptible_timeout(port->drain_wait, 732 gs_writes_finished(port), 733 GS_CLOSE_TIMEOUT * HZ); 734 spin_lock_irq(&port->port_lock); 735 gser = port->port_usb; 736 } 737 738 /* Iff we're disconnected, there can be no I/O in flight so it's 739 * ok to free the circular buffer; else just scrub it. And don't 740 * let the push tasklet fire again until we're re-opened. 741 */ 742 if (gser == NULL) 743 kfifo_free(&port->port_write_buf); 744 else 745 kfifo_reset(&port->port_write_buf); 746 747 port->port.tty = NULL; 748 749 port->openclose = false; 750 751 pr_debug("gs_close: ttyGS%d (%p,%p) done!\n", 752 port->port_num, tty, file); 753 754 wake_up(&port->close_wait); 755 exit: 756 spin_unlock_irq(&port->port_lock); 757 } 758 759 static int gs_write(struct tty_struct *tty, const unsigned char *buf, int count) 760 { 761 struct gs_port *port = tty->driver_data; 762 unsigned long flags; 763 764 pr_vdebug("gs_write: ttyGS%d (%p) writing %d bytes\n", 765 port->port_num, tty, count); 766 767 spin_lock_irqsave(&port->port_lock, flags); 768 if (count) 769 count = kfifo_in(&port->port_write_buf, buf, count); 770 /* treat count == 0 as flush_chars() */ 771 if (port->port_usb) 772 gs_start_tx(port); 773 spin_unlock_irqrestore(&port->port_lock, flags); 774 775 return count; 776 } 777 778 static int gs_put_char(struct tty_struct *tty, unsigned char ch) 779 { 780 struct gs_port *port = tty->driver_data; 781 unsigned long flags; 782 int status; 783 784 pr_vdebug("gs_put_char: (%d,%p) char=0x%x, called from %ps\n", 785 port->port_num, tty, ch, __builtin_return_address(0)); 786 787 spin_lock_irqsave(&port->port_lock, flags); 788 status = kfifo_put(&port->port_write_buf, ch); 789 spin_unlock_irqrestore(&port->port_lock, flags); 790 791 return status; 792 } 793 794 static void gs_flush_chars(struct tty_struct *tty) 795 { 796 struct gs_port *port = tty->driver_data; 797 unsigned long flags; 798 799 pr_vdebug("gs_flush_chars: (%d,%p)\n", port->port_num, tty); 800 801 spin_lock_irqsave(&port->port_lock, flags); 802 if (port->port_usb) 803 gs_start_tx(port); 804 spin_unlock_irqrestore(&port->port_lock, flags); 805 } 806 807 static int gs_write_room(struct tty_struct *tty) 808 { 809 struct gs_port *port = tty->driver_data; 810 unsigned long flags; 811 int room = 0; 812 813 spin_lock_irqsave(&port->port_lock, flags); 814 if (port->port_usb) 815 room = kfifo_avail(&port->port_write_buf); 816 spin_unlock_irqrestore(&port->port_lock, flags); 817 818 pr_vdebug("gs_write_room: (%d,%p) room=%d\n", 819 port->port_num, tty, room); 820 821 return room; 822 } 823 824 static int gs_chars_in_buffer(struct tty_struct *tty) 825 { 826 struct gs_port *port = tty->driver_data; 827 unsigned long flags; 828 int chars = 0; 829 830 spin_lock_irqsave(&port->port_lock, flags); 831 chars = kfifo_len(&port->port_write_buf); 832 spin_unlock_irqrestore(&port->port_lock, flags); 833 834 pr_vdebug("gs_chars_in_buffer: (%d,%p) chars=%d\n", 835 port->port_num, tty, chars); 836 837 return chars; 838 } 839 840 /* undo side effects of setting TTY_THROTTLED */ 841 static void gs_unthrottle(struct tty_struct *tty) 842 { 843 struct gs_port *port = tty->driver_data; 844 unsigned long flags; 845 846 spin_lock_irqsave(&port->port_lock, flags); 847 if (port->port_usb) { 848 /* Kickstart read queue processing. We don't do xon/xoff, 849 * rts/cts, or other handshaking with the host, but if the 850 * read queue backs up enough we'll be NAKing OUT packets. 851 */ 852 pr_vdebug("ttyGS%d: unthrottle\n", port->port_num); 853 schedule_delayed_work(&port->push, 0); 854 } 855 spin_unlock_irqrestore(&port->port_lock, flags); 856 } 857 858 static int gs_break_ctl(struct tty_struct *tty, int duration) 859 { 860 struct gs_port *port = tty->driver_data; 861 int status = 0; 862 struct gserial *gser; 863 864 pr_vdebug("gs_break_ctl: ttyGS%d, send break (%d) \n", 865 port->port_num, duration); 866 867 spin_lock_irq(&port->port_lock); 868 gser = port->port_usb; 869 if (gser && gser->send_break) 870 status = gser->send_break(gser, duration); 871 spin_unlock_irq(&port->port_lock); 872 873 return status; 874 } 875 876 static const struct tty_operations gs_tty_ops = { 877 .open = gs_open, 878 .close = gs_close, 879 .write = gs_write, 880 .put_char = gs_put_char, 881 .flush_chars = gs_flush_chars, 882 .write_room = gs_write_room, 883 .chars_in_buffer = gs_chars_in_buffer, 884 .unthrottle = gs_unthrottle, 885 .break_ctl = gs_break_ctl, 886 }; 887 888 /*-------------------------------------------------------------------------*/ 889 890 static struct tty_driver *gs_tty_driver; 891 892 #ifdef CONFIG_U_SERIAL_CONSOLE 893 894 static void gs_console_complete_out(struct usb_ep *ep, struct usb_request *req) 895 { 896 struct gs_console *cons = req->context; 897 898 switch (req->status) { 899 default: 900 pr_warn("%s: unexpected %s status %d\n", 901 __func__, ep->name, req->status); 902 /* fall through */ 903 case 0: 904 /* normal completion */ 905 spin_lock(&cons->lock); 906 req->length = 0; 907 schedule_work(&cons->work); 908 spin_unlock(&cons->lock); 909 break; 910 case -ECONNRESET: 911 case -ESHUTDOWN: 912 /* disconnect */ 913 pr_vdebug("%s: %s shutdown\n", __func__, ep->name); 914 break; 915 } 916 } 917 918 static void __gs_console_push(struct gs_console *cons) 919 { 920 struct usb_request *req = cons->req; 921 struct usb_ep *ep; 922 size_t size; 923 924 if (!req) 925 return; /* disconnected */ 926 927 if (req->length) 928 return; /* busy */ 929 930 ep = cons->console.data; 931 size = kfifo_out(&cons->buf, req->buf, ep->maxpacket); 932 if (!size) 933 return; 934 935 if (cons->missed && ep->maxpacket >= 64) { 936 char buf[64]; 937 size_t len; 938 939 len = sprintf(buf, "\n[missed %zu bytes]\n", cons->missed); 940 kfifo_in(&cons->buf, buf, len); 941 cons->missed = 0; 942 } 943 944 req->length = size; 945 if (usb_ep_queue(ep, req, GFP_ATOMIC)) 946 req->length = 0; 947 } 948 949 static void gs_console_work(struct work_struct *work) 950 { 951 struct gs_console *cons = container_of(work, struct gs_console, work); 952 953 spin_lock_irq(&cons->lock); 954 955 __gs_console_push(cons); 956 957 spin_unlock_irq(&cons->lock); 958 } 959 960 static void gs_console_write(struct console *co, 961 const char *buf, unsigned count) 962 { 963 struct gs_console *cons = container_of(co, struct gs_console, console); 964 unsigned long flags; 965 size_t n; 966 967 spin_lock_irqsave(&cons->lock, flags); 968 969 n = kfifo_in(&cons->buf, buf, count); 970 if (n < count) 971 cons->missed += count - n; 972 973 if (cons->req && !cons->req->length) 974 schedule_work(&cons->work); 975 976 spin_unlock_irqrestore(&cons->lock, flags); 977 } 978 979 static struct tty_driver *gs_console_device(struct console *co, int *index) 980 { 981 *index = co->index; 982 return gs_tty_driver; 983 } 984 985 static int gs_console_connect(struct gs_port *port) 986 { 987 struct gs_console *cons = port->console; 988 struct usb_request *req; 989 struct usb_ep *ep; 990 991 if (!cons) 992 return 0; 993 994 ep = port->port_usb->in; 995 req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC); 996 if (!req) 997 return -ENOMEM; 998 req->complete = gs_console_complete_out; 999 req->context = cons; 1000 req->length = 0; 1001 1002 spin_lock(&cons->lock); 1003 cons->req = req; 1004 cons->console.data = ep; 1005 spin_unlock(&cons->lock); 1006 1007 pr_debug("ttyGS%d: console connected!\n", port->port_num); 1008 1009 schedule_work(&cons->work); 1010 1011 return 0; 1012 } 1013 1014 static void gs_console_disconnect(struct gs_port *port) 1015 { 1016 struct gs_console *cons = port->console; 1017 struct usb_request *req; 1018 struct usb_ep *ep; 1019 1020 if (!cons) 1021 return; 1022 1023 spin_lock(&cons->lock); 1024 1025 req = cons->req; 1026 ep = cons->console.data; 1027 cons->req = NULL; 1028 1029 spin_unlock(&cons->lock); 1030 1031 if (!req) 1032 return; 1033 1034 usb_ep_dequeue(ep, req); 1035 gs_free_req(ep, req); 1036 } 1037 1038 static int gs_console_init(struct gs_port *port) 1039 { 1040 struct gs_console *cons; 1041 int err; 1042 1043 if (port->console) 1044 return 0; 1045 1046 cons = kzalloc(sizeof(*port->console), GFP_KERNEL); 1047 if (!cons) 1048 return -ENOMEM; 1049 1050 strcpy(cons->console.name, "ttyGS"); 1051 cons->console.write = gs_console_write; 1052 cons->console.device = gs_console_device; 1053 cons->console.flags = CON_PRINTBUFFER; 1054 cons->console.index = port->port_num; 1055 1056 INIT_WORK(&cons->work, gs_console_work); 1057 spin_lock_init(&cons->lock); 1058 1059 err = kfifo_alloc(&cons->buf, GS_CONSOLE_BUF_SIZE, GFP_KERNEL); 1060 if (err) { 1061 pr_err("ttyGS%d: allocate console buffer failed\n", port->port_num); 1062 kfree(cons); 1063 return err; 1064 } 1065 1066 port->console = cons; 1067 register_console(&cons->console); 1068 1069 spin_lock_irq(&port->port_lock); 1070 if (port->port_usb) 1071 gs_console_connect(port); 1072 spin_unlock_irq(&port->port_lock); 1073 1074 return 0; 1075 } 1076 1077 static void gs_console_exit(struct gs_port *port) 1078 { 1079 struct gs_console *cons = port->console; 1080 1081 if (!cons) 1082 return; 1083 1084 unregister_console(&cons->console); 1085 1086 spin_lock_irq(&port->port_lock); 1087 if (cons->req) 1088 gs_console_disconnect(port); 1089 spin_unlock_irq(&port->port_lock); 1090 1091 cancel_work_sync(&cons->work); 1092 kfifo_free(&cons->buf); 1093 kfree(cons); 1094 port->console = NULL; 1095 } 1096 1097 ssize_t gserial_set_console(unsigned char port_num, const char *page, size_t count) 1098 { 1099 struct gs_port *port; 1100 bool enable; 1101 int ret; 1102 1103 ret = strtobool(page, &enable); 1104 if (ret) 1105 return ret; 1106 1107 mutex_lock(&ports[port_num].lock); 1108 port = ports[port_num].port; 1109 1110 if (WARN_ON(port == NULL)) { 1111 ret = -ENXIO; 1112 goto out; 1113 } 1114 1115 if (enable) 1116 ret = gs_console_init(port); 1117 else 1118 gs_console_exit(port); 1119 out: 1120 mutex_unlock(&ports[port_num].lock); 1121 1122 return ret < 0 ? ret : count; 1123 } 1124 EXPORT_SYMBOL_GPL(gserial_set_console); 1125 1126 ssize_t gserial_get_console(unsigned char port_num, char *page) 1127 { 1128 struct gs_port *port; 1129 ssize_t ret; 1130 1131 mutex_lock(&ports[port_num].lock); 1132 port = ports[port_num].port; 1133 1134 if (WARN_ON(port == NULL)) 1135 ret = -ENXIO; 1136 else 1137 ret = sprintf(page, "%u\n", !!port->console); 1138 1139 mutex_unlock(&ports[port_num].lock); 1140 1141 return ret; 1142 } 1143 EXPORT_SYMBOL_GPL(gserial_get_console); 1144 1145 #else 1146 1147 static int gs_console_connect(struct gs_port *port) 1148 { 1149 return 0; 1150 } 1151 1152 static void gs_console_disconnect(struct gs_port *port) 1153 { 1154 } 1155 1156 static int gs_console_init(struct gs_port *port) 1157 { 1158 return -ENOSYS; 1159 } 1160 1161 static void gs_console_exit(struct gs_port *port) 1162 { 1163 } 1164 1165 #endif 1166 1167 static int 1168 gs_port_alloc(unsigned port_num, struct usb_cdc_line_coding *coding) 1169 { 1170 struct gs_port *port; 1171 int ret = 0; 1172 1173 mutex_lock(&ports[port_num].lock); 1174 if (ports[port_num].port) { 1175 ret = -EBUSY; 1176 goto out; 1177 } 1178 1179 port = kzalloc(sizeof(struct gs_port), GFP_KERNEL); 1180 if (port == NULL) { 1181 ret = -ENOMEM; 1182 goto out; 1183 } 1184 1185 tty_port_init(&port->port); 1186 spin_lock_init(&port->port_lock); 1187 init_waitqueue_head(&port->drain_wait); 1188 init_waitqueue_head(&port->close_wait); 1189 1190 INIT_DELAYED_WORK(&port->push, gs_rx_push); 1191 1192 INIT_LIST_HEAD(&port->read_pool); 1193 INIT_LIST_HEAD(&port->read_queue); 1194 INIT_LIST_HEAD(&port->write_pool); 1195 1196 port->port_num = port_num; 1197 port->port_line_coding = *coding; 1198 1199 ports[port_num].port = port; 1200 out: 1201 mutex_unlock(&ports[port_num].lock); 1202 return ret; 1203 } 1204 1205 static int gs_closed(struct gs_port *port) 1206 { 1207 int cond; 1208 1209 spin_lock_irq(&port->port_lock); 1210 cond = (port->port.count == 0) && !port->openclose; 1211 spin_unlock_irq(&port->port_lock); 1212 return cond; 1213 } 1214 1215 static void gserial_free_port(struct gs_port *port) 1216 { 1217 cancel_delayed_work_sync(&port->push); 1218 /* wait for old opens to finish */ 1219 wait_event(port->close_wait, gs_closed(port)); 1220 WARN_ON(port->port_usb != NULL); 1221 tty_port_destroy(&port->port); 1222 kfree(port); 1223 } 1224 1225 void gserial_free_line(unsigned char port_num) 1226 { 1227 struct gs_port *port; 1228 1229 mutex_lock(&ports[port_num].lock); 1230 if (WARN_ON(!ports[port_num].port)) { 1231 mutex_unlock(&ports[port_num].lock); 1232 return; 1233 } 1234 port = ports[port_num].port; 1235 gs_console_exit(port); 1236 ports[port_num].port = NULL; 1237 mutex_unlock(&ports[port_num].lock); 1238 1239 gserial_free_port(port); 1240 tty_unregister_device(gs_tty_driver, port_num); 1241 } 1242 EXPORT_SYMBOL_GPL(gserial_free_line); 1243 1244 int gserial_alloc_line_no_console(unsigned char *line_num) 1245 { 1246 struct usb_cdc_line_coding coding; 1247 struct gs_port *port; 1248 struct device *tty_dev; 1249 int ret; 1250 int port_num; 1251 1252 coding.dwDTERate = cpu_to_le32(9600); 1253 coding.bCharFormat = 8; 1254 coding.bParityType = USB_CDC_NO_PARITY; 1255 coding.bDataBits = USB_CDC_1_STOP_BITS; 1256 1257 for (port_num = 0; port_num < MAX_U_SERIAL_PORTS; port_num++) { 1258 ret = gs_port_alloc(port_num, &coding); 1259 if (ret == -EBUSY) 1260 continue; 1261 if (ret) 1262 return ret; 1263 break; 1264 } 1265 if (ret) 1266 return ret; 1267 1268 /* ... and sysfs class devices, so mdev/udev make /dev/ttyGS* */ 1269 1270 port = ports[port_num].port; 1271 tty_dev = tty_port_register_device(&port->port, 1272 gs_tty_driver, port_num, NULL); 1273 if (IS_ERR(tty_dev)) { 1274 pr_err("%s: failed to register tty for port %d, err %ld\n", 1275 __func__, port_num, PTR_ERR(tty_dev)); 1276 1277 ret = PTR_ERR(tty_dev); 1278 mutex_lock(&ports[port_num].lock); 1279 ports[port_num].port = NULL; 1280 mutex_unlock(&ports[port_num].lock); 1281 gserial_free_port(port); 1282 goto err; 1283 } 1284 *line_num = port_num; 1285 err: 1286 return ret; 1287 } 1288 EXPORT_SYMBOL_GPL(gserial_alloc_line_no_console); 1289 1290 int gserial_alloc_line(unsigned char *line_num) 1291 { 1292 int ret = gserial_alloc_line_no_console(line_num); 1293 1294 if (!ret && !*line_num) 1295 gs_console_init(ports[*line_num].port); 1296 1297 return ret; 1298 } 1299 EXPORT_SYMBOL_GPL(gserial_alloc_line); 1300 1301 /** 1302 * gserial_connect - notify TTY I/O glue that USB link is active 1303 * @gser: the function, set up with endpoints and descriptors 1304 * @port_num: which port is active 1305 * Context: any (usually from irq) 1306 * 1307 * This is called activate endpoints and let the TTY layer know that 1308 * the connection is active ... not unlike "carrier detect". It won't 1309 * necessarily start I/O queues; unless the TTY is held open by any 1310 * task, there would be no point. However, the endpoints will be 1311 * activated so the USB host can perform I/O, subject to basic USB 1312 * hardware flow control. 1313 * 1314 * Caller needs to have set up the endpoints and USB function in @dev 1315 * before calling this, as well as the appropriate (speed-specific) 1316 * endpoint descriptors, and also have allocate @port_num by calling 1317 * @gserial_alloc_line(). 1318 * 1319 * Returns negative errno or zero. 1320 * On success, ep->driver_data will be overwritten. 1321 */ 1322 int gserial_connect(struct gserial *gser, u8 port_num) 1323 { 1324 struct gs_port *port; 1325 unsigned long flags; 1326 int status; 1327 1328 if (port_num >= MAX_U_SERIAL_PORTS) 1329 return -ENXIO; 1330 1331 port = ports[port_num].port; 1332 if (!port) { 1333 pr_err("serial line %d not allocated.\n", port_num); 1334 return -EINVAL; 1335 } 1336 if (port->port_usb) { 1337 pr_err("serial line %d is in use.\n", port_num); 1338 return -EBUSY; 1339 } 1340 1341 /* activate the endpoints */ 1342 status = usb_ep_enable(gser->in); 1343 if (status < 0) 1344 return status; 1345 gser->in->driver_data = port; 1346 1347 status = usb_ep_enable(gser->out); 1348 if (status < 0) 1349 goto fail_out; 1350 gser->out->driver_data = port; 1351 1352 /* then tell the tty glue that I/O can work */ 1353 spin_lock_irqsave(&port->port_lock, flags); 1354 gser->ioport = port; 1355 port->port_usb = gser; 1356 1357 /* REVISIT unclear how best to handle this state... 1358 * we don't really couple it with the Linux TTY. 1359 */ 1360 gser->port_line_coding = port->port_line_coding; 1361 1362 /* REVISIT if waiting on "carrier detect", signal. */ 1363 1364 /* if it's already open, start I/O ... and notify the serial 1365 * protocol about open/close status (connect/disconnect). 1366 */ 1367 if (port->port.count) { 1368 pr_debug("gserial_connect: start ttyGS%d\n", port->port_num); 1369 gs_start_io(port); 1370 if (gser->connect) 1371 gser->connect(gser); 1372 } else { 1373 if (gser->disconnect) 1374 gser->disconnect(gser); 1375 } 1376 1377 status = gs_console_connect(port); 1378 spin_unlock_irqrestore(&port->port_lock, flags); 1379 1380 return status; 1381 1382 fail_out: 1383 usb_ep_disable(gser->in); 1384 return status; 1385 } 1386 EXPORT_SYMBOL_GPL(gserial_connect); 1387 /** 1388 * gserial_disconnect - notify TTY I/O glue that USB link is inactive 1389 * @gser: the function, on which gserial_connect() was called 1390 * Context: any (usually from irq) 1391 * 1392 * This is called to deactivate endpoints and let the TTY layer know 1393 * that the connection went inactive ... not unlike "hangup". 1394 * 1395 * On return, the state is as if gserial_connect() had never been called; 1396 * there is no active USB I/O on these endpoints. 1397 */ 1398 void gserial_disconnect(struct gserial *gser) 1399 { 1400 struct gs_port *port = gser->ioport; 1401 unsigned long flags; 1402 1403 if (!port) 1404 return; 1405 1406 /* tell the TTY glue not to do I/O here any more */ 1407 spin_lock_irqsave(&port->port_lock, flags); 1408 1409 gs_console_disconnect(port); 1410 1411 /* REVISIT as above: how best to track this? */ 1412 port->port_line_coding = gser->port_line_coding; 1413 1414 port->port_usb = NULL; 1415 gser->ioport = NULL; 1416 if (port->port.count > 0 || port->openclose) { 1417 wake_up_interruptible(&port->drain_wait); 1418 if (port->port.tty) 1419 tty_hangup(port->port.tty); 1420 } 1421 spin_unlock_irqrestore(&port->port_lock, flags); 1422 1423 /* disable endpoints, aborting down any active I/O */ 1424 usb_ep_disable(gser->out); 1425 usb_ep_disable(gser->in); 1426 1427 /* finally, free any unused/unusable I/O buffers */ 1428 spin_lock_irqsave(&port->port_lock, flags); 1429 if (port->port.count == 0 && !port->openclose) 1430 kfifo_free(&port->port_write_buf); 1431 gs_free_requests(gser->out, &port->read_pool, NULL); 1432 gs_free_requests(gser->out, &port->read_queue, NULL); 1433 gs_free_requests(gser->in, &port->write_pool, NULL); 1434 1435 port->read_allocated = port->read_started = 1436 port->write_allocated = port->write_started = 0; 1437 1438 spin_unlock_irqrestore(&port->port_lock, flags); 1439 } 1440 EXPORT_SYMBOL_GPL(gserial_disconnect); 1441 1442 static int userial_init(void) 1443 { 1444 unsigned i; 1445 int status; 1446 1447 gs_tty_driver = alloc_tty_driver(MAX_U_SERIAL_PORTS); 1448 if (!gs_tty_driver) 1449 return -ENOMEM; 1450 1451 gs_tty_driver->driver_name = "g_serial"; 1452 gs_tty_driver->name = "ttyGS"; 1453 /* uses dynamically assigned dev_t values */ 1454 1455 gs_tty_driver->type = TTY_DRIVER_TYPE_SERIAL; 1456 gs_tty_driver->subtype = SERIAL_TYPE_NORMAL; 1457 gs_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; 1458 gs_tty_driver->init_termios = tty_std_termios; 1459 1460 /* 9600-8-N-1 ... matches defaults expected by "usbser.sys" on 1461 * MS-Windows. Otherwise, most of these flags shouldn't affect 1462 * anything unless we were to actually hook up to a serial line. 1463 */ 1464 gs_tty_driver->init_termios.c_cflag = 1465 B9600 | CS8 | CREAD | HUPCL | CLOCAL; 1466 gs_tty_driver->init_termios.c_ispeed = 9600; 1467 gs_tty_driver->init_termios.c_ospeed = 9600; 1468 1469 tty_set_operations(gs_tty_driver, &gs_tty_ops); 1470 for (i = 0; i < MAX_U_SERIAL_PORTS; i++) 1471 mutex_init(&ports[i].lock); 1472 1473 /* export the driver ... */ 1474 status = tty_register_driver(gs_tty_driver); 1475 if (status) { 1476 pr_err("%s: cannot register, err %d\n", 1477 __func__, status); 1478 goto fail; 1479 } 1480 1481 pr_debug("%s: registered %d ttyGS* device%s\n", __func__, 1482 MAX_U_SERIAL_PORTS, 1483 (MAX_U_SERIAL_PORTS == 1) ? "" : "s"); 1484 1485 return status; 1486 fail: 1487 put_tty_driver(gs_tty_driver); 1488 gs_tty_driver = NULL; 1489 return status; 1490 } 1491 module_init(userial_init); 1492 1493 static void userial_cleanup(void) 1494 { 1495 tty_unregister_driver(gs_tty_driver); 1496 put_tty_driver(gs_tty_driver); 1497 gs_tty_driver = NULL; 1498 } 1499 module_exit(userial_cleanup); 1500 1501 MODULE_LICENSE("GPL"); 1502