| .. | .. |
|---|
| 16 | 16 | |
|---|
| 17 | 17 | #include <linux/kernel.h> |
|---|
| 18 | 18 | #include <linux/sched.h> |
|---|
| 19 | | -#include <linux/interrupt.h> |
|---|
| 20 | 19 | #include <linux/device.h> |
|---|
| 21 | 20 | #include <linux/delay.h> |
|---|
| 22 | 21 | #include <linux/tty.h> |
|---|
| .. | .. |
|---|
| 26 | 25 | #include <linux/module.h> |
|---|
| 27 | 26 | #include <linux/console.h> |
|---|
| 28 | 27 | #include <linux/kthread.h> |
|---|
| 28 | +#include <linux/workqueue.h> |
|---|
| 29 | 29 | #include <linux/kfifo.h> |
|---|
| 30 | 30 | |
|---|
| 31 | 31 | #include "u_serial.h" |
|---|
| .. | .. |
|---|
| 82 | 82 | #define GS_CONSOLE_BUF_SIZE 8192 |
|---|
| 83 | 83 | |
|---|
| 84 | 84 | /* console info */ |
|---|
| 85 | | -struct gscons_info { |
|---|
| 86 | | - struct gs_port *port; |
|---|
| 87 | | - struct task_struct *console_thread; |
|---|
| 88 | | - struct kfifo con_buf; |
|---|
| 89 | | - /* protect the buf and busy flag */ |
|---|
| 90 | | - spinlock_t con_lock; |
|---|
| 91 | | - int req_busy; |
|---|
| 92 | | - struct usb_request *console_req; |
|---|
| 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; |
|---|
| 93 | 92 | }; |
|---|
| 94 | 93 | |
|---|
| 95 | 94 | /* |
|---|
| .. | .. |
|---|
| 101 | 100 | spinlock_t port_lock; /* guard port_* access */ |
|---|
| 102 | 101 | |
|---|
| 103 | 102 | struct gserial *port_usb; |
|---|
| 103 | +#ifdef CONFIG_U_SERIAL_CONSOLE |
|---|
| 104 | + struct gs_console *console; |
|---|
| 105 | +#endif |
|---|
| 104 | 106 | |
|---|
| 105 | 107 | u8 port_num; |
|---|
| 106 | 108 | |
|---|
| .. | .. |
|---|
| 109 | 111 | int read_allocated; |
|---|
| 110 | 112 | struct list_head read_queue; |
|---|
| 111 | 113 | unsigned n_read; |
|---|
| 112 | | - struct tasklet_struct push; |
|---|
| 114 | + struct delayed_work push; |
|---|
| 113 | 115 | |
|---|
| 114 | 116 | struct list_head write_pool; |
|---|
| 115 | 117 | int write_started; |
|---|
| .. | .. |
|---|
| 118 | 120 | wait_queue_head_t drain_wait; /* wait while writes drain */ |
|---|
| 119 | 121 | bool write_busy; |
|---|
| 120 | 122 | wait_queue_head_t close_wait; |
|---|
| 123 | + bool suspended; /* port suspended */ |
|---|
| 124 | + bool start_delayed; /* delay start when suspended */ |
|---|
| 121 | 125 | |
|---|
| 122 | 126 | /* REVISIT this state ... */ |
|---|
| 123 | 127 | struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */ |
|---|
| .. | .. |
|---|
| 254 | 258 | list_del(&req->list); |
|---|
| 255 | 259 | req->zero = kfifo_is_empty(&port->port_write_buf); |
|---|
| 256 | 260 | |
|---|
| 257 | | - pr_vdebug("ttyGS%d: tx len=%d, 0x%02x 0x%02x 0x%02x ...\n", |
|---|
| 258 | | - port->port_num, len, *((u8 *)req->buf), |
|---|
| 259 | | - *((u8 *)req->buf+1), *((u8 *)req->buf+2)); |
|---|
| 261 | + pr_vdebug("ttyGS%d: tx len=%d, %3ph ...\n", port->port_num, len, req->buf); |
|---|
| 260 | 262 | |
|---|
| 261 | 263 | /* Drop lock while we call out of driver; completions |
|---|
| 262 | 264 | * could be issued while we do so. Disconnection may |
|---|
| .. | .. |
|---|
| 342 | 344 | } |
|---|
| 343 | 345 | |
|---|
| 344 | 346 | /* |
|---|
| 345 | | - * RX tasklet takes data out of the RX queue and hands it up to the TTY |
|---|
| 347 | + * RX work takes data out of the RX queue and hands it up to the TTY |
|---|
| 346 | 348 | * layer until it refuses to take any more data (or is throttled back). |
|---|
| 347 | 349 | * Then it issues reads for any further data. |
|---|
| 348 | 350 | * |
|---|
| .. | .. |
|---|
| 351 | 353 | * So QUEUE_SIZE packets plus however many the FIFO holds (usually two) |
|---|
| 352 | 354 | * can be buffered before the TTY layer's buffers (currently 64 KB). |
|---|
| 353 | 355 | */ |
|---|
| 354 | | -static void gs_rx_push(unsigned long _port) |
|---|
| 356 | +static void gs_rx_push(struct work_struct *work) |
|---|
| 355 | 357 | { |
|---|
| 356 | | - struct gs_port *port = (void *)_port; |
|---|
| 358 | + struct delayed_work *w = to_delayed_work(work); |
|---|
| 359 | + struct gs_port *port = container_of(w, struct gs_port, push); |
|---|
| 357 | 360 | struct tty_struct *tty; |
|---|
| 358 | 361 | struct list_head *queue = &port->read_queue; |
|---|
| 359 | 362 | bool disconnect = false; |
|---|
| .. | .. |
|---|
| 381 | 384 | /* presumably a transient fault */ |
|---|
| 382 | 385 | pr_warn("ttyGS%d: unexpected RX status %d\n", |
|---|
| 383 | 386 | port->port_num, req->status); |
|---|
| 384 | | - /* FALLTHROUGH */ |
|---|
| 387 | + fallthrough; |
|---|
| 385 | 388 | case 0: |
|---|
| 386 | 389 | /* normal completion */ |
|---|
| 387 | 390 | break; |
|---|
| .. | .. |
|---|
| 428 | 431 | |
|---|
| 429 | 432 | /* We want our data queue to become empty ASAP, keeping data |
|---|
| 430 | 433 | * in the tty and ldisc (not here). If we couldn't push any |
|---|
| 431 | | - * this time around, there may be trouble unless there's an |
|---|
| 432 | | - * implicit tty_unthrottle() call on its way... |
|---|
| 434 | + * this time around, RX may be starved, so wait until next jiffy. |
|---|
| 433 | 435 | * |
|---|
| 434 | | - * REVISIT we should probably add a timer to keep the tasklet |
|---|
| 435 | | - * from starving ... but it's not clear that case ever happens. |
|---|
| 436 | + * We may leave non-empty queue only when there is a tty, and |
|---|
| 437 | + * either it is throttled or there is no more room in flip buffer. |
|---|
| 436 | 438 | */ |
|---|
| 437 | | - if (!list_empty(queue) && tty) { |
|---|
| 438 | | - if (!tty_throttled(tty)) { |
|---|
| 439 | | - if (do_push) |
|---|
| 440 | | - tasklet_schedule(&port->push); |
|---|
| 441 | | - else |
|---|
| 442 | | - pr_warn("ttyGS%d: RX not scheduled?\n", |
|---|
| 443 | | - port->port_num); |
|---|
| 444 | | - } |
|---|
| 445 | | - } |
|---|
| 439 | + if (!list_empty(queue) && !tty_throttled(tty)) |
|---|
| 440 | + schedule_delayed_work(&port->push, 1); |
|---|
| 446 | 441 | |
|---|
| 447 | 442 | /* If we're still connected, refill the USB RX queue. */ |
|---|
| 448 | 443 | if (!disconnect && port->port_usb) |
|---|
| .. | .. |
|---|
| 458 | 453 | /* Queue all received data until the tty layer is ready for it. */ |
|---|
| 459 | 454 | spin_lock(&port->port_lock); |
|---|
| 460 | 455 | list_add_tail(&req->list, &port->read_queue); |
|---|
| 461 | | - tasklet_schedule(&port->push); |
|---|
| 456 | + schedule_delayed_work(&port->push, 0); |
|---|
| 462 | 457 | spin_unlock(&port->port_lock); |
|---|
| 463 | 458 | } |
|---|
| 464 | 459 | |
|---|
| .. | .. |
|---|
| 475 | 470 | /* presumably a transient fault */ |
|---|
| 476 | 471 | pr_warn("%s: unexpected %s status %d\n", |
|---|
| 477 | 472 | __func__, ep->name, req->status); |
|---|
| 478 | | - /* FALL THROUGH */ |
|---|
| 473 | + fallthrough; |
|---|
| 479 | 474 | case 0: |
|---|
| 480 | 475 | /* normal completion */ |
|---|
| 481 | 476 | gs_start_tx(port); |
|---|
| .. | .. |
|---|
| 530 | 525 | |
|---|
| 531 | 526 | /** |
|---|
| 532 | 527 | * gs_start_io - start USB I/O streams |
|---|
| 533 | | - * @dev: encapsulates endpoints to use |
|---|
| 528 | + * @port: port to use |
|---|
| 534 | 529 | * Context: holding port_lock; port_tty and port_usb are non-null |
|---|
| 535 | 530 | * |
|---|
| 536 | 531 | * We only start I/O when something is connected to both sides of |
|---|
| .. | .. |
|---|
| 635 | 630 | |
|---|
| 636 | 631 | /* if connected, start the I/O stream */ |
|---|
| 637 | 632 | if (port->port_usb) { |
|---|
| 638 | | - struct gserial *gser = port->port_usb; |
|---|
| 633 | + /* if port is suspended, wait resume to start I/0 stream */ |
|---|
| 634 | + if (!port->suspended) { |
|---|
| 635 | + struct gserial *gser = port->port_usb; |
|---|
| 639 | 636 | |
|---|
| 640 | | - pr_debug("gs_open: start ttyGS%d\n", port->port_num); |
|---|
| 641 | | - gs_start_io(port); |
|---|
| 637 | + pr_debug("gs_open: start ttyGS%d\n", port->port_num); |
|---|
| 638 | + gs_start_io(port); |
|---|
| 642 | 639 | |
|---|
| 643 | | - if (gser->connect) |
|---|
| 644 | | - gser->connect(gser); |
|---|
| 640 | + if (gser->connect) |
|---|
| 641 | + gser->connect(gser); |
|---|
| 642 | + } else { |
|---|
| 643 | + pr_debug("delay start of ttyGS%d\n", port->port_num); |
|---|
| 644 | + port->start_delayed = true; |
|---|
| 645 | + } |
|---|
| 645 | 646 | } |
|---|
| 646 | 647 | |
|---|
| 647 | 648 | pr_debug("gs_open: ttyGS%d (%p,%p)\n", port->port_num, tty, file); |
|---|
| .. | .. |
|---|
| 685 | 686 | pr_debug("gs_close: ttyGS%d (%p,%p) ...\n", port->port_num, tty, file); |
|---|
| 686 | 687 | |
|---|
| 687 | 688 | gser = port->port_usb; |
|---|
| 688 | | - if (gser && gser->disconnect) |
|---|
| 689 | + if (gser && !port->suspended && gser->disconnect) |
|---|
| 689 | 690 | gser->disconnect(gser); |
|---|
| 690 | 691 | |
|---|
| 691 | 692 | /* wait for circular write buffer to drain, disconnect, or at |
|---|
| .. | .. |
|---|
| 706 | 707 | |
|---|
| 707 | 708 | /* Iff we're disconnected, there can be no I/O in flight so it's |
|---|
| 708 | 709 | * ok to free the circular buffer; else just scrub it. And don't |
|---|
| 709 | | - * let the push tasklet fire again until we're re-opened. |
|---|
| 710 | + * let the push async work fire again until we're re-opened. |
|---|
| 710 | 711 | */ |
|---|
| 711 | 712 | if (gser == NULL) |
|---|
| 712 | 713 | kfifo_free(&port->port_write_buf); |
|---|
| 713 | 714 | else |
|---|
| 714 | 715 | kfifo_reset(&port->port_write_buf); |
|---|
| 715 | 716 | |
|---|
| 717 | + port->start_delayed = false; |
|---|
| 716 | 718 | port->port.count = 0; |
|---|
| 717 | 719 | port->port.tty = NULL; |
|---|
| 718 | 720 | |
|---|
| .. | .. |
|---|
| 817 | 819 | * rts/cts, or other handshaking with the host, but if the |
|---|
| 818 | 820 | * read queue backs up enough we'll be NAKing OUT packets. |
|---|
| 819 | 821 | */ |
|---|
| 820 | | - tasklet_schedule(&port->push); |
|---|
| 821 | 822 | pr_vdebug("ttyGS%d: unthrottle\n", port->port_num); |
|---|
| 823 | + schedule_delayed_work(&port->push, 0); |
|---|
| 822 | 824 | } |
|---|
| 823 | 825 | spin_unlock_irqrestore(&port->port_lock, flags); |
|---|
| 824 | 826 | } |
|---|
| .. | .. |
|---|
| 859 | 861 | |
|---|
| 860 | 862 | #ifdef CONFIG_U_SERIAL_CONSOLE |
|---|
| 861 | 863 | |
|---|
| 862 | | -static struct gscons_info gscons_info; |
|---|
| 863 | | -static struct console gserial_cons; |
|---|
| 864 | | - |
|---|
| 865 | | -static struct usb_request *gs_request_new(struct usb_ep *ep) |
|---|
| 864 | +static void gs_console_complete_out(struct usb_ep *ep, struct usb_request *req) |
|---|
| 866 | 865 | { |
|---|
| 867 | | - struct usb_request *req = usb_ep_alloc_request(ep, GFP_ATOMIC); |
|---|
| 868 | | - if (!req) |
|---|
| 869 | | - return NULL; |
|---|
| 870 | | - |
|---|
| 871 | | - req->buf = kmalloc(ep->maxpacket, GFP_ATOMIC); |
|---|
| 872 | | - if (!req->buf) { |
|---|
| 873 | | - usb_ep_free_request(ep, req); |
|---|
| 874 | | - return NULL; |
|---|
| 875 | | - } |
|---|
| 876 | | - |
|---|
| 877 | | - return req; |
|---|
| 878 | | -} |
|---|
| 879 | | - |
|---|
| 880 | | -static void gs_request_free(struct usb_request *req, struct usb_ep *ep) |
|---|
| 881 | | -{ |
|---|
| 882 | | - if (!req) |
|---|
| 883 | | - return; |
|---|
| 884 | | - |
|---|
| 885 | | - kfree(req->buf); |
|---|
| 886 | | - usb_ep_free_request(ep, req); |
|---|
| 887 | | -} |
|---|
| 888 | | - |
|---|
| 889 | | -static void gs_complete_out(struct usb_ep *ep, struct usb_request *req) |
|---|
| 890 | | -{ |
|---|
| 891 | | - struct gscons_info *info = &gscons_info; |
|---|
| 866 | + struct gs_console *cons = req->context; |
|---|
| 892 | 867 | |
|---|
| 893 | 868 | switch (req->status) { |
|---|
| 894 | 869 | default: |
|---|
| 895 | 870 | pr_warn("%s: unexpected %s status %d\n", |
|---|
| 896 | 871 | __func__, ep->name, req->status); |
|---|
| 897 | | - /* fall through */ |
|---|
| 872 | + fallthrough; |
|---|
| 898 | 873 | case 0: |
|---|
| 899 | 874 | /* normal completion */ |
|---|
| 900 | | - spin_lock(&info->con_lock); |
|---|
| 901 | | - info->req_busy = 0; |
|---|
| 902 | | - spin_unlock(&info->con_lock); |
|---|
| 903 | | - |
|---|
| 904 | | - wake_up_process(info->console_thread); |
|---|
| 875 | + spin_lock(&cons->lock); |
|---|
| 876 | + req->length = 0; |
|---|
| 877 | + schedule_work(&cons->work); |
|---|
| 878 | + spin_unlock(&cons->lock); |
|---|
| 905 | 879 | break; |
|---|
| 880 | + case -ECONNRESET: |
|---|
| 906 | 881 | case -ESHUTDOWN: |
|---|
| 907 | 882 | /* disconnect */ |
|---|
| 908 | 883 | pr_vdebug("%s: %s shutdown\n", __func__, ep->name); |
|---|
| .. | .. |
|---|
| 910 | 885 | } |
|---|
| 911 | 886 | } |
|---|
| 912 | 887 | |
|---|
| 913 | | -static int gs_console_connect(int port_num) |
|---|
| 888 | +static void __gs_console_push(struct gs_console *cons) |
|---|
| 914 | 889 | { |
|---|
| 915 | | - struct gscons_info *info = &gscons_info; |
|---|
| 916 | | - struct gs_port *port; |
|---|
| 890 | + struct usb_request *req = cons->req; |
|---|
| 917 | 891 | struct usb_ep *ep; |
|---|
| 892 | + size_t size; |
|---|
| 918 | 893 | |
|---|
| 919 | | - if (port_num != gserial_cons.index) { |
|---|
| 920 | | - pr_err("%s: port num [%d] is not support console\n", |
|---|
| 921 | | - __func__, port_num); |
|---|
| 922 | | - return -ENXIO; |
|---|
| 894 | + if (!req) |
|---|
| 895 | + return; /* disconnected */ |
|---|
| 896 | + |
|---|
| 897 | + if (req->length) |
|---|
| 898 | + return; /* busy */ |
|---|
| 899 | + |
|---|
| 900 | + ep = cons->console.data; |
|---|
| 901 | + size = kfifo_out(&cons->buf, req->buf, ep->maxpacket); |
|---|
| 902 | + if (!size) |
|---|
| 903 | + return; |
|---|
| 904 | + |
|---|
| 905 | + if (cons->missed && ep->maxpacket >= 64) { |
|---|
| 906 | + char buf[64]; |
|---|
| 907 | + size_t len; |
|---|
| 908 | + |
|---|
| 909 | + len = sprintf(buf, "\n[missed %zu bytes]\n", cons->missed); |
|---|
| 910 | + kfifo_in(&cons->buf, buf, len); |
|---|
| 911 | + cons->missed = 0; |
|---|
| 923 | 912 | } |
|---|
| 924 | 913 | |
|---|
| 925 | | - port = ports[port_num].port; |
|---|
| 926 | | - ep = port->port_usb->in; |
|---|
| 927 | | - if (!info->console_req) { |
|---|
| 928 | | - info->console_req = gs_request_new(ep); |
|---|
| 929 | | - if (!info->console_req) |
|---|
| 930 | | - return -ENOMEM; |
|---|
| 931 | | - info->console_req->complete = gs_complete_out; |
|---|
| 932 | | - } |
|---|
| 933 | | - |
|---|
| 934 | | - info->port = port; |
|---|
| 935 | | - spin_lock(&info->con_lock); |
|---|
| 936 | | - info->req_busy = 0; |
|---|
| 937 | | - spin_unlock(&info->con_lock); |
|---|
| 938 | | - pr_vdebug("port[%d] console connect!\n", port_num); |
|---|
| 939 | | - return 0; |
|---|
| 914 | + req->length = size; |
|---|
| 915 | + if (usb_ep_queue(ep, req, GFP_ATOMIC)) |
|---|
| 916 | + req->length = 0; |
|---|
| 940 | 917 | } |
|---|
| 941 | 918 | |
|---|
| 942 | | -static void gs_console_disconnect(struct usb_ep *ep) |
|---|
| 919 | +static void gs_console_work(struct work_struct *work) |
|---|
| 943 | 920 | { |
|---|
| 944 | | - struct gscons_info *info = &gscons_info; |
|---|
| 945 | | - struct usb_request *req = info->console_req; |
|---|
| 921 | + struct gs_console *cons = container_of(work, struct gs_console, work); |
|---|
| 946 | 922 | |
|---|
| 947 | | - gs_request_free(req, ep); |
|---|
| 948 | | - info->console_req = NULL; |
|---|
| 949 | | -} |
|---|
| 923 | + spin_lock_irq(&cons->lock); |
|---|
| 950 | 924 | |
|---|
| 951 | | -static int gs_console_thread(void *data) |
|---|
| 952 | | -{ |
|---|
| 953 | | - struct gscons_info *info = &gscons_info; |
|---|
| 954 | | - struct gs_port *port; |
|---|
| 955 | | - struct usb_request *req; |
|---|
| 956 | | - struct usb_ep *ep; |
|---|
| 957 | | - int xfer, ret, count, size; |
|---|
| 925 | + __gs_console_push(cons); |
|---|
| 958 | 926 | |
|---|
| 959 | | - do { |
|---|
| 960 | | - port = info->port; |
|---|
| 961 | | - set_current_state(TASK_INTERRUPTIBLE); |
|---|
| 962 | | - if (!port || !port->port_usb |
|---|
| 963 | | - || !port->port_usb->in || !info->console_req) |
|---|
| 964 | | - goto sched; |
|---|
| 965 | | - |
|---|
| 966 | | - req = info->console_req; |
|---|
| 967 | | - ep = port->port_usb->in; |
|---|
| 968 | | - |
|---|
| 969 | | - spin_lock_irq(&info->con_lock); |
|---|
| 970 | | - count = kfifo_len(&info->con_buf); |
|---|
| 971 | | - size = ep->maxpacket; |
|---|
| 972 | | - |
|---|
| 973 | | - if (count > 0 && !info->req_busy) { |
|---|
| 974 | | - set_current_state(TASK_RUNNING); |
|---|
| 975 | | - if (count < size) |
|---|
| 976 | | - size = count; |
|---|
| 977 | | - |
|---|
| 978 | | - xfer = kfifo_out(&info->con_buf, req->buf, size); |
|---|
| 979 | | - req->length = xfer; |
|---|
| 980 | | - |
|---|
| 981 | | - spin_unlock(&info->con_lock); |
|---|
| 982 | | - ret = usb_ep_queue(ep, req, GFP_ATOMIC); |
|---|
| 983 | | - spin_lock(&info->con_lock); |
|---|
| 984 | | - if (ret < 0) |
|---|
| 985 | | - info->req_busy = 0; |
|---|
| 986 | | - else |
|---|
| 987 | | - info->req_busy = 1; |
|---|
| 988 | | - |
|---|
| 989 | | - spin_unlock_irq(&info->con_lock); |
|---|
| 990 | | - } else { |
|---|
| 991 | | - spin_unlock_irq(&info->con_lock); |
|---|
| 992 | | -sched: |
|---|
| 993 | | - if (kthread_should_stop()) { |
|---|
| 994 | | - set_current_state(TASK_RUNNING); |
|---|
| 995 | | - break; |
|---|
| 996 | | - } |
|---|
| 997 | | - schedule(); |
|---|
| 998 | | - } |
|---|
| 999 | | - } while (1); |
|---|
| 1000 | | - |
|---|
| 1001 | | - return 0; |
|---|
| 1002 | | -} |
|---|
| 1003 | | - |
|---|
| 1004 | | -static int gs_console_setup(struct console *co, char *options) |
|---|
| 1005 | | -{ |
|---|
| 1006 | | - struct gscons_info *info = &gscons_info; |
|---|
| 1007 | | - int status; |
|---|
| 1008 | | - |
|---|
| 1009 | | - info->port = NULL; |
|---|
| 1010 | | - info->console_req = NULL; |
|---|
| 1011 | | - info->req_busy = 0; |
|---|
| 1012 | | - spin_lock_init(&info->con_lock); |
|---|
| 1013 | | - |
|---|
| 1014 | | - status = kfifo_alloc(&info->con_buf, GS_CONSOLE_BUF_SIZE, GFP_KERNEL); |
|---|
| 1015 | | - if (status) { |
|---|
| 1016 | | - pr_err("%s: allocate console buffer failed\n", __func__); |
|---|
| 1017 | | - return status; |
|---|
| 1018 | | - } |
|---|
| 1019 | | - |
|---|
| 1020 | | - info->console_thread = kthread_create(gs_console_thread, |
|---|
| 1021 | | - co, "gs_console"); |
|---|
| 1022 | | - if (IS_ERR(info->console_thread)) { |
|---|
| 1023 | | - pr_err("%s: cannot create console thread\n", __func__); |
|---|
| 1024 | | - kfifo_free(&info->con_buf); |
|---|
| 1025 | | - return PTR_ERR(info->console_thread); |
|---|
| 1026 | | - } |
|---|
| 1027 | | - wake_up_process(info->console_thread); |
|---|
| 1028 | | - |
|---|
| 1029 | | - return 0; |
|---|
| 927 | + spin_unlock_irq(&cons->lock); |
|---|
| 1030 | 928 | } |
|---|
| 1031 | 929 | |
|---|
| 1032 | 930 | static void gs_console_write(struct console *co, |
|---|
| 1033 | 931 | const char *buf, unsigned count) |
|---|
| 1034 | 932 | { |
|---|
| 1035 | | - struct gscons_info *info = &gscons_info; |
|---|
| 933 | + struct gs_console *cons = container_of(co, struct gs_console, console); |
|---|
| 1036 | 934 | unsigned long flags; |
|---|
| 935 | + size_t n; |
|---|
| 1037 | 936 | |
|---|
| 1038 | | - spin_lock_irqsave(&info->con_lock, flags); |
|---|
| 1039 | | - kfifo_in(&info->con_buf, buf, count); |
|---|
| 1040 | | - spin_unlock_irqrestore(&info->con_lock, flags); |
|---|
| 937 | + spin_lock_irqsave(&cons->lock, flags); |
|---|
| 1041 | 938 | |
|---|
| 1042 | | - wake_up_process(info->console_thread); |
|---|
| 939 | + n = kfifo_in(&cons->buf, buf, count); |
|---|
| 940 | + if (n < count) |
|---|
| 941 | + cons->missed += count - n; |
|---|
| 942 | + |
|---|
| 943 | + if (cons->req && !cons->req->length) |
|---|
| 944 | + schedule_work(&cons->work); |
|---|
| 945 | + |
|---|
| 946 | + spin_unlock_irqrestore(&cons->lock, flags); |
|---|
| 1043 | 947 | } |
|---|
| 1044 | 948 | |
|---|
| 1045 | 949 | static struct tty_driver *gs_console_device(struct console *co, int *index) |
|---|
| 1046 | 950 | { |
|---|
| 1047 | | - struct tty_driver **p = (struct tty_driver **)co->data; |
|---|
| 1048 | | - |
|---|
| 1049 | | - if (!*p) |
|---|
| 1050 | | - return NULL; |
|---|
| 1051 | | - |
|---|
| 1052 | 951 | *index = co->index; |
|---|
| 1053 | | - return *p; |
|---|
| 952 | + return gs_tty_driver; |
|---|
| 1054 | 953 | } |
|---|
| 1055 | 954 | |
|---|
| 1056 | | -static struct console gserial_cons = { |
|---|
| 1057 | | - .name = "ttyGS", |
|---|
| 1058 | | - .write = gs_console_write, |
|---|
| 1059 | | - .device = gs_console_device, |
|---|
| 1060 | | - .setup = gs_console_setup, |
|---|
| 1061 | | - .flags = CON_PRINTBUFFER, |
|---|
| 1062 | | - .index = -1, |
|---|
| 1063 | | - .data = &gs_tty_driver, |
|---|
| 1064 | | -}; |
|---|
| 1065 | | - |
|---|
| 1066 | | -static void gserial_console_init(void) |
|---|
| 955 | +static int gs_console_connect(struct gs_port *port) |
|---|
| 1067 | 956 | { |
|---|
| 1068 | | - register_console(&gserial_cons); |
|---|
| 957 | + struct gs_console *cons = port->console; |
|---|
| 958 | + struct usb_request *req; |
|---|
| 959 | + struct usb_ep *ep; |
|---|
| 960 | + |
|---|
| 961 | + if (!cons) |
|---|
| 962 | + return 0; |
|---|
| 963 | + |
|---|
| 964 | + ep = port->port_usb->in; |
|---|
| 965 | + req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC); |
|---|
| 966 | + if (!req) |
|---|
| 967 | + return -ENOMEM; |
|---|
| 968 | + req->complete = gs_console_complete_out; |
|---|
| 969 | + req->context = cons; |
|---|
| 970 | + req->length = 0; |
|---|
| 971 | + |
|---|
| 972 | + spin_lock(&cons->lock); |
|---|
| 973 | + cons->req = req; |
|---|
| 974 | + cons->console.data = ep; |
|---|
| 975 | + spin_unlock(&cons->lock); |
|---|
| 976 | + |
|---|
| 977 | + pr_debug("ttyGS%d: console connected!\n", port->port_num); |
|---|
| 978 | + |
|---|
| 979 | + schedule_work(&cons->work); |
|---|
| 980 | + |
|---|
| 981 | + return 0; |
|---|
| 1069 | 982 | } |
|---|
| 1070 | 983 | |
|---|
| 1071 | | -static void gserial_console_exit(void) |
|---|
| 984 | +static void gs_console_disconnect(struct gs_port *port) |
|---|
| 1072 | 985 | { |
|---|
| 1073 | | - struct gscons_info *info = &gscons_info; |
|---|
| 986 | + struct gs_console *cons = port->console; |
|---|
| 987 | + struct usb_request *req; |
|---|
| 988 | + struct usb_ep *ep; |
|---|
| 1074 | 989 | |
|---|
| 1075 | | - unregister_console(&gserial_cons); |
|---|
| 1076 | | - if (!IS_ERR_OR_NULL(info->console_thread)) |
|---|
| 1077 | | - kthread_stop(info->console_thread); |
|---|
| 1078 | | - kfifo_free(&info->con_buf); |
|---|
| 990 | + if (!cons) |
|---|
| 991 | + return; |
|---|
| 992 | + |
|---|
| 993 | + spin_lock(&cons->lock); |
|---|
| 994 | + |
|---|
| 995 | + req = cons->req; |
|---|
| 996 | + ep = cons->console.data; |
|---|
| 997 | + cons->req = NULL; |
|---|
| 998 | + |
|---|
| 999 | + spin_unlock(&cons->lock); |
|---|
| 1000 | + |
|---|
| 1001 | + if (!req) |
|---|
| 1002 | + return; |
|---|
| 1003 | + |
|---|
| 1004 | + usb_ep_dequeue(ep, req); |
|---|
| 1005 | + gs_free_req(ep, req); |
|---|
| 1079 | 1006 | } |
|---|
| 1007 | + |
|---|
| 1008 | +static int gs_console_init(struct gs_port *port) |
|---|
| 1009 | +{ |
|---|
| 1010 | + struct gs_console *cons; |
|---|
| 1011 | + int err; |
|---|
| 1012 | + |
|---|
| 1013 | + if (port->console) |
|---|
| 1014 | + return 0; |
|---|
| 1015 | + |
|---|
| 1016 | + cons = kzalloc(sizeof(*port->console), GFP_KERNEL); |
|---|
| 1017 | + if (!cons) |
|---|
| 1018 | + return -ENOMEM; |
|---|
| 1019 | + |
|---|
| 1020 | + strcpy(cons->console.name, "ttyGS"); |
|---|
| 1021 | + cons->console.write = gs_console_write; |
|---|
| 1022 | + cons->console.device = gs_console_device; |
|---|
| 1023 | + cons->console.flags = CON_PRINTBUFFER; |
|---|
| 1024 | + cons->console.index = port->port_num; |
|---|
| 1025 | + |
|---|
| 1026 | + INIT_WORK(&cons->work, gs_console_work); |
|---|
| 1027 | + spin_lock_init(&cons->lock); |
|---|
| 1028 | + |
|---|
| 1029 | + err = kfifo_alloc(&cons->buf, GS_CONSOLE_BUF_SIZE, GFP_KERNEL); |
|---|
| 1030 | + if (err) { |
|---|
| 1031 | + pr_err("ttyGS%d: allocate console buffer failed\n", port->port_num); |
|---|
| 1032 | + kfree(cons); |
|---|
| 1033 | + return err; |
|---|
| 1034 | + } |
|---|
| 1035 | + |
|---|
| 1036 | + port->console = cons; |
|---|
| 1037 | + register_console(&cons->console); |
|---|
| 1038 | + |
|---|
| 1039 | + spin_lock_irq(&port->port_lock); |
|---|
| 1040 | + if (port->port_usb) |
|---|
| 1041 | + gs_console_connect(port); |
|---|
| 1042 | + spin_unlock_irq(&port->port_lock); |
|---|
| 1043 | + |
|---|
| 1044 | + return 0; |
|---|
| 1045 | +} |
|---|
| 1046 | + |
|---|
| 1047 | +static void gs_console_exit(struct gs_port *port) |
|---|
| 1048 | +{ |
|---|
| 1049 | + struct gs_console *cons = port->console; |
|---|
| 1050 | + |
|---|
| 1051 | + if (!cons) |
|---|
| 1052 | + return; |
|---|
| 1053 | + |
|---|
| 1054 | + unregister_console(&cons->console); |
|---|
| 1055 | + |
|---|
| 1056 | + spin_lock_irq(&port->port_lock); |
|---|
| 1057 | + if (cons->req) |
|---|
| 1058 | + gs_console_disconnect(port); |
|---|
| 1059 | + spin_unlock_irq(&port->port_lock); |
|---|
| 1060 | + |
|---|
| 1061 | + cancel_work_sync(&cons->work); |
|---|
| 1062 | + kfifo_free(&cons->buf); |
|---|
| 1063 | + kfree(cons); |
|---|
| 1064 | + port->console = NULL; |
|---|
| 1065 | +} |
|---|
| 1066 | + |
|---|
| 1067 | +ssize_t gserial_set_console(unsigned char port_num, const char *page, size_t count) |
|---|
| 1068 | +{ |
|---|
| 1069 | + struct gs_port *port; |
|---|
| 1070 | + bool enable; |
|---|
| 1071 | + int ret; |
|---|
| 1072 | + |
|---|
| 1073 | + ret = strtobool(page, &enable); |
|---|
| 1074 | + if (ret) |
|---|
| 1075 | + return ret; |
|---|
| 1076 | + |
|---|
| 1077 | + mutex_lock(&ports[port_num].lock); |
|---|
| 1078 | + port = ports[port_num].port; |
|---|
| 1079 | + |
|---|
| 1080 | + if (WARN_ON(port == NULL)) { |
|---|
| 1081 | + ret = -ENXIO; |
|---|
| 1082 | + goto out; |
|---|
| 1083 | + } |
|---|
| 1084 | + |
|---|
| 1085 | + if (enable) |
|---|
| 1086 | + ret = gs_console_init(port); |
|---|
| 1087 | + else |
|---|
| 1088 | + gs_console_exit(port); |
|---|
| 1089 | +out: |
|---|
| 1090 | + mutex_unlock(&ports[port_num].lock); |
|---|
| 1091 | + |
|---|
| 1092 | + return ret < 0 ? ret : count; |
|---|
| 1093 | +} |
|---|
| 1094 | +EXPORT_SYMBOL_GPL(gserial_set_console); |
|---|
| 1095 | + |
|---|
| 1096 | +ssize_t gserial_get_console(unsigned char port_num, char *page) |
|---|
| 1097 | +{ |
|---|
| 1098 | + struct gs_port *port; |
|---|
| 1099 | + ssize_t ret; |
|---|
| 1100 | + |
|---|
| 1101 | + mutex_lock(&ports[port_num].lock); |
|---|
| 1102 | + port = ports[port_num].port; |
|---|
| 1103 | + |
|---|
| 1104 | + if (WARN_ON(port == NULL)) |
|---|
| 1105 | + ret = -ENXIO; |
|---|
| 1106 | + else |
|---|
| 1107 | + ret = sprintf(page, "%u\n", !!port->console); |
|---|
| 1108 | + |
|---|
| 1109 | + mutex_unlock(&ports[port_num].lock); |
|---|
| 1110 | + |
|---|
| 1111 | + return ret; |
|---|
| 1112 | +} |
|---|
| 1113 | +EXPORT_SYMBOL_GPL(gserial_get_console); |
|---|
| 1080 | 1114 | |
|---|
| 1081 | 1115 | #else |
|---|
| 1082 | 1116 | |
|---|
| 1083 | | -static int gs_console_connect(int port_num) |
|---|
| 1117 | +static int gs_console_connect(struct gs_port *port) |
|---|
| 1084 | 1118 | { |
|---|
| 1085 | 1119 | return 0; |
|---|
| 1086 | 1120 | } |
|---|
| 1087 | 1121 | |
|---|
| 1088 | | -static void gs_console_disconnect(struct usb_ep *ep) |
|---|
| 1122 | +static void gs_console_disconnect(struct gs_port *port) |
|---|
| 1089 | 1123 | { |
|---|
| 1090 | 1124 | } |
|---|
| 1091 | 1125 | |
|---|
| 1092 | | -static void gserial_console_init(void) |
|---|
| 1126 | +static int gs_console_init(struct gs_port *port) |
|---|
| 1093 | 1127 | { |
|---|
| 1128 | + return -ENOSYS; |
|---|
| 1094 | 1129 | } |
|---|
| 1095 | 1130 | |
|---|
| 1096 | | -static void gserial_console_exit(void) |
|---|
| 1131 | +static void gs_console_exit(struct gs_port *port) |
|---|
| 1097 | 1132 | { |
|---|
| 1098 | 1133 | } |
|---|
| 1099 | 1134 | |
|---|
| .. | .. |
|---|
| 1122 | 1157 | init_waitqueue_head(&port->drain_wait); |
|---|
| 1123 | 1158 | init_waitqueue_head(&port->close_wait); |
|---|
| 1124 | 1159 | |
|---|
| 1125 | | - tasklet_init(&port->push, gs_rx_push, (unsigned long) port); |
|---|
| 1160 | + INIT_DELAYED_WORK(&port->push, gs_rx_push); |
|---|
| 1126 | 1161 | |
|---|
| 1127 | 1162 | INIT_LIST_HEAD(&port->read_pool); |
|---|
| 1128 | 1163 | INIT_LIST_HEAD(&port->read_queue); |
|---|
| .. | .. |
|---|
| 1150 | 1185 | |
|---|
| 1151 | 1186 | static void gserial_free_port(struct gs_port *port) |
|---|
| 1152 | 1187 | { |
|---|
| 1153 | | - tasklet_kill(&port->push); |
|---|
| 1188 | + cancel_delayed_work_sync(&port->push); |
|---|
| 1154 | 1189 | /* wait for old opens to finish */ |
|---|
| 1155 | 1190 | wait_event(port->close_wait, gs_closed(port)); |
|---|
| 1156 | 1191 | WARN_ON(port->port_usb != NULL); |
|---|
| .. | .. |
|---|
| 1168 | 1203 | return; |
|---|
| 1169 | 1204 | } |
|---|
| 1170 | 1205 | port = ports[port_num].port; |
|---|
| 1206 | + gs_console_exit(port); |
|---|
| 1171 | 1207 | ports[port_num].port = NULL; |
|---|
| 1172 | 1208 | mutex_unlock(&ports[port_num].lock); |
|---|
| 1173 | 1209 | |
|---|
| 1174 | 1210 | gserial_free_port(port); |
|---|
| 1175 | 1211 | tty_unregister_device(gs_tty_driver, port_num); |
|---|
| 1176 | | - gserial_console_exit(); |
|---|
| 1177 | 1212 | } |
|---|
| 1178 | 1213 | EXPORT_SYMBOL_GPL(gserial_free_line); |
|---|
| 1179 | 1214 | |
|---|
| 1180 | | -int gserial_alloc_line(unsigned char *line_num) |
|---|
| 1215 | +int gserial_alloc_line_no_console(unsigned char *line_num) |
|---|
| 1181 | 1216 | { |
|---|
| 1182 | 1217 | struct usb_cdc_line_coding coding; |
|---|
| 1218 | + struct gs_port *port; |
|---|
| 1183 | 1219 | struct device *tty_dev; |
|---|
| 1184 | 1220 | int ret; |
|---|
| 1185 | 1221 | int port_num; |
|---|
| .. | .. |
|---|
| 1202 | 1238 | |
|---|
| 1203 | 1239 | /* ... and sysfs class devices, so mdev/udev make /dev/ttyGS* */ |
|---|
| 1204 | 1240 | |
|---|
| 1205 | | - tty_dev = tty_port_register_device(&ports[port_num].port->port, |
|---|
| 1241 | + port = ports[port_num].port; |
|---|
| 1242 | + tty_dev = tty_port_register_device(&port->port, |
|---|
| 1206 | 1243 | gs_tty_driver, port_num, NULL); |
|---|
| 1207 | 1244 | if (IS_ERR(tty_dev)) { |
|---|
| 1208 | | - struct gs_port *port; |
|---|
| 1209 | 1245 | pr_err("%s: failed to register tty for port %d, err %ld\n", |
|---|
| 1210 | 1246 | __func__, port_num, PTR_ERR(tty_dev)); |
|---|
| 1211 | 1247 | |
|---|
| 1212 | 1248 | ret = PTR_ERR(tty_dev); |
|---|
| 1213 | 1249 | mutex_lock(&ports[port_num].lock); |
|---|
| 1214 | | - port = ports[port_num].port; |
|---|
| 1215 | 1250 | ports[port_num].port = NULL; |
|---|
| 1216 | 1251 | mutex_unlock(&ports[port_num].lock); |
|---|
| 1217 | 1252 | gserial_free_port(port); |
|---|
| 1218 | 1253 | goto err; |
|---|
| 1219 | 1254 | } |
|---|
| 1220 | 1255 | *line_num = port_num; |
|---|
| 1221 | | - gserial_console_init(); |
|---|
| 1222 | 1256 | err: |
|---|
| 1257 | + return ret; |
|---|
| 1258 | +} |
|---|
| 1259 | +EXPORT_SYMBOL_GPL(gserial_alloc_line_no_console); |
|---|
| 1260 | + |
|---|
| 1261 | +int gserial_alloc_line(unsigned char *line_num) |
|---|
| 1262 | +{ |
|---|
| 1263 | + int ret = gserial_alloc_line_no_console(line_num); |
|---|
| 1264 | + |
|---|
| 1265 | + if (!ret && !*line_num) |
|---|
| 1266 | + gs_console_init(ports[*line_num].port); |
|---|
| 1267 | + |
|---|
| 1223 | 1268 | return ret; |
|---|
| 1224 | 1269 | } |
|---|
| 1225 | 1270 | EXPORT_SYMBOL_GPL(gserial_alloc_line); |
|---|
| .. | .. |
|---|
| 1300 | 1345 | gser->disconnect(gser); |
|---|
| 1301 | 1346 | } |
|---|
| 1302 | 1347 | |
|---|
| 1303 | | - status = gs_console_connect(port_num); |
|---|
| 1348 | + status = gs_console_connect(port); |
|---|
| 1304 | 1349 | spin_unlock_irqrestore(&port->port_lock, flags); |
|---|
| 1305 | 1350 | |
|---|
| 1306 | 1351 | return status; |
|---|
| .. | .. |
|---|
| 1332 | 1377 | /* tell the TTY glue not to do I/O here any more */ |
|---|
| 1333 | 1378 | spin_lock_irqsave(&port->port_lock, flags); |
|---|
| 1334 | 1379 | |
|---|
| 1380 | + gs_console_disconnect(port); |
|---|
| 1381 | + |
|---|
| 1335 | 1382 | /* REVISIT as above: how best to track this? */ |
|---|
| 1336 | 1383 | port->port_line_coding = gser->port_line_coding; |
|---|
| 1337 | 1384 | |
|---|
| .. | .. |
|---|
| 1342 | 1389 | if (port->port.tty) |
|---|
| 1343 | 1390 | tty_hangup(port->port.tty); |
|---|
| 1344 | 1391 | } |
|---|
| 1392 | + port->suspended = false; |
|---|
| 1345 | 1393 | spin_unlock_irqrestore(&port->port_lock, flags); |
|---|
| 1346 | 1394 | |
|---|
| 1347 | 1395 | /* disable endpoints, aborting down any active I/O */ |
|---|
| .. | .. |
|---|
| 1359 | 1407 | port->read_allocated = port->read_started = |
|---|
| 1360 | 1408 | port->write_allocated = port->write_started = 0; |
|---|
| 1361 | 1409 | |
|---|
| 1362 | | - gs_console_disconnect(gser->in); |
|---|
| 1363 | 1410 | spin_unlock_irqrestore(&port->port_lock, flags); |
|---|
| 1364 | 1411 | } |
|---|
| 1365 | 1412 | EXPORT_SYMBOL_GPL(gserial_disconnect); |
|---|
| 1366 | 1413 | |
|---|
| 1414 | +void gserial_suspend(struct gserial *gser) |
|---|
| 1415 | +{ |
|---|
| 1416 | + struct gs_port *port = gser->ioport; |
|---|
| 1417 | + unsigned long flags; |
|---|
| 1418 | + |
|---|
| 1419 | + spin_lock_irqsave(&port->port_lock, flags); |
|---|
| 1420 | + port->suspended = true; |
|---|
| 1421 | + spin_unlock_irqrestore(&port->port_lock, flags); |
|---|
| 1422 | +} |
|---|
| 1423 | +EXPORT_SYMBOL_GPL(gserial_suspend); |
|---|
| 1424 | + |
|---|
| 1425 | +void gserial_resume(struct gserial *gser) |
|---|
| 1426 | +{ |
|---|
| 1427 | + struct gs_port *port = gser->ioport; |
|---|
| 1428 | + unsigned long flags; |
|---|
| 1429 | + |
|---|
| 1430 | + spin_lock_irqsave(&port->port_lock, flags); |
|---|
| 1431 | + port->suspended = false; |
|---|
| 1432 | + if (!port->start_delayed) { |
|---|
| 1433 | + spin_unlock_irqrestore(&port->port_lock, flags); |
|---|
| 1434 | + return; |
|---|
| 1435 | + } |
|---|
| 1436 | + |
|---|
| 1437 | + pr_debug("delayed start ttyGS%d\n", port->port_num); |
|---|
| 1438 | + gs_start_io(port); |
|---|
| 1439 | + if (gser->connect) |
|---|
| 1440 | + gser->connect(gser); |
|---|
| 1441 | + port->start_delayed = false; |
|---|
| 1442 | + spin_unlock_irqrestore(&port->port_lock, flags); |
|---|
| 1443 | +} |
|---|
| 1444 | +EXPORT_SYMBOL_GPL(gserial_resume); |
|---|
| 1445 | + |
|---|
| 1367 | 1446 | static int userial_init(void) |
|---|
| 1368 | 1447 | { |
|---|
| 1369 | 1448 | unsigned i; |
|---|