hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
kernel/drivers/usb/gadget/function/u_serial.c
....@@ -16,7 +16,6 @@
1616
1717 #include <linux/kernel.h>
1818 #include <linux/sched.h>
19
-#include <linux/interrupt.h>
2019 #include <linux/device.h>
2120 #include <linux/delay.h>
2221 #include <linux/tty.h>
....@@ -26,6 +25,7 @@
2625 #include <linux/module.h>
2726 #include <linux/console.h>
2827 #include <linux/kthread.h>
28
+#include <linux/workqueue.h>
2929 #include <linux/kfifo.h>
3030
3131 #include "u_serial.h"
....@@ -82,14 +82,13 @@
8282 #define GS_CONSOLE_BUF_SIZE 8192
8383
8484 /* 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;
9392 };
9493
9594 /*
....@@ -101,8 +100,10 @@
101100 spinlock_t port_lock; /* guard port_* access */
102101
103102 struct gserial *port_usb;
103
+#ifdef CONFIG_U_SERIAL_CONSOLE
104
+ struct gs_console *console;
105
+#endif
104106
105
- bool openclose; /* open/close in progress */
106107 u8 port_num;
107108
108109 struct list_head read_pool;
....@@ -110,7 +111,7 @@
110111 int read_allocated;
111112 struct list_head read_queue;
112113 unsigned n_read;
113
- struct tasklet_struct push;
114
+ struct delayed_work push;
114115
115116 struct list_head write_pool;
116117 int write_started;
....@@ -119,6 +120,8 @@
119120 wait_queue_head_t drain_wait; /* wait while writes drain */
120121 bool write_busy;
121122 wait_queue_head_t close_wait;
123
+ bool suspended; /* port suspended */
124
+ bool start_delayed; /* delay start when suspended */
122125
123126 /* REVISIT this state ... */
124127 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
....@@ -255,9 +258,7 @@
255258 list_del(&req->list);
256259 req->zero = kfifo_is_empty(&port->port_write_buf);
257260
258
- pr_vdebug("ttyGS%d: tx len=%d, 0x%02x 0x%02x 0x%02x ...\n",
259
- port->port_num, len, *((u8 *)req->buf),
260
- *((u8 *)req->buf+1), *((u8 *)req->buf+2));
261
+ pr_vdebug("ttyGS%d: tx len=%d, %3ph ...\n", port->port_num, len, req->buf);
261262
262263 /* Drop lock while we call out of driver; completions
263264 * could be issued while we do so. Disconnection may
....@@ -343,7 +344,7 @@
343344 }
344345
345346 /*
346
- * 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
347348 * layer until it refuses to take any more data (or is throttled back).
348349 * Then it issues reads for any further data.
349350 *
....@@ -352,9 +353,10 @@
352353 * So QUEUE_SIZE packets plus however many the FIFO holds (usually two)
353354 * can be buffered before the TTY layer's buffers (currently 64 KB).
354355 */
355
-static void gs_rx_push(unsigned long _port)
356
+static void gs_rx_push(struct work_struct *work)
356357 {
357
- 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);
358360 struct tty_struct *tty;
359361 struct list_head *queue = &port->read_queue;
360362 bool disconnect = false;
....@@ -382,7 +384,7 @@
382384 /* presumably a transient fault */
383385 pr_warn("ttyGS%d: unexpected RX status %d\n",
384386 port->port_num, req->status);
385
- /* FALLTHROUGH */
387
+ fallthrough;
386388 case 0:
387389 /* normal completion */
388390 break;
....@@ -429,21 +431,13 @@
429431
430432 /* We want our data queue to become empty ASAP, keeping data
431433 * in the tty and ldisc (not here). If we couldn't push any
432
- * this time around, there may be trouble unless there's an
433
- * implicit tty_unthrottle() call on its way...
434
+ * this time around, RX may be starved, so wait until next jiffy.
434435 *
435
- * REVISIT we should probably add a timer to keep the tasklet
436
- * 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.
437438 */
438
- if (!list_empty(queue) && tty) {
439
- if (!tty_throttled(tty)) {
440
- if (do_push)
441
- tasklet_schedule(&port->push);
442
- else
443
- pr_warn("ttyGS%d: RX not scheduled?\n",
444
- port->port_num);
445
- }
446
- }
439
+ if (!list_empty(queue) && !tty_throttled(tty))
440
+ schedule_delayed_work(&port->push, 1);
447441
448442 /* If we're still connected, refill the USB RX queue. */
449443 if (!disconnect && port->port_usb)
....@@ -459,7 +453,7 @@
459453 /* Queue all received data until the tty layer is ready for it. */
460454 spin_lock(&port->port_lock);
461455 list_add_tail(&req->list, &port->read_queue);
462
- tasklet_schedule(&port->push);
456
+ schedule_delayed_work(&port->push, 0);
463457 spin_unlock(&port->port_lock);
464458 }
465459
....@@ -476,7 +470,7 @@
476470 /* presumably a transient fault */
477471 pr_warn("%s: unexpected %s status %d\n",
478472 __func__, ep->name, req->status);
479
- /* FALL THROUGH */
473
+ fallthrough;
480474 case 0:
481475 /* normal completion */
482476 gs_start_tx(port);
....@@ -531,7 +525,7 @@
531525
532526 /**
533527 * gs_start_io - start USB I/O streams
534
- * @dev: encapsulates endpoints to use
528
+ * @port: port to use
535529 * Context: holding port_lock; port_tty and port_usb are non-null
536530 *
537531 * We only start I/O when something is connected to both sides of
....@@ -595,109 +589,79 @@
595589 {
596590 int port_num = tty->index;
597591 struct gs_port *port;
598
- int status;
592
+ int status = 0;
599593
600
- do {
601
- mutex_lock(&ports[port_num].lock);
602
- port = ports[port_num].port;
603
- if (!port)
604
- status = -ENODEV;
605
- else {
606
- spin_lock_irq(&port->port_lock);
594
+ mutex_lock(&ports[port_num].lock);
595
+ port = ports[port_num].port;
596
+ if (!port) {
597
+ status = -ENODEV;
598
+ goto out;
599
+ }
607600
608
- /* already open? Great. */
609
- if (port->port.count) {
610
- status = 0;
611
- port->port.count++;
612
-
613
- /* currently opening/closing? wait ... */
614
- } else if (port->openclose) {
615
- status = -EBUSY;
616
-
617
- /* ... else we do the work */
618
- } else {
619
- status = -EAGAIN;
620
- port->openclose = true;
621
- }
622
- spin_unlock_irq(&port->port_lock);
623
- }
624
- mutex_unlock(&ports[port_num].lock);
625
-
626
- switch (status) {
627
- default:
628
- /* fully handled */
629
- return status;
630
- case -EAGAIN:
631
- /* must do the work */
632
- break;
633
- case -EBUSY:
634
- /* wait for EAGAIN task to finish */
635
- msleep(1);
636
- /* REVISIT could have a waitchannel here, if
637
- * concurrent open performance is important
638
- */
639
- break;
640
- }
641
- } while (status != -EAGAIN);
642
-
643
- /* Do the "real open" */
644601 spin_lock_irq(&port->port_lock);
645602
646603 /* allocate circular buffer on first open */
647604 if (!kfifo_initialized(&port->port_write_buf)) {
648605
649606 spin_unlock_irq(&port->port_lock);
607
+
608
+ /*
609
+ * portmaster's mutex still protects from simultaneous open(),
610
+ * and close() can't happen, yet.
611
+ */
612
+
650613 status = kfifo_alloc(&port->port_write_buf,
651614 WRITE_BUF_SIZE, GFP_KERNEL);
652
- spin_lock_irq(&port->port_lock);
653
-
654615 if (status) {
655616 pr_debug("gs_open: ttyGS%d (%p,%p) no buffer\n",
656
- port->port_num, tty, file);
657
- port->openclose = false;
658
- goto exit_unlock_port;
617
+ port_num, tty, file);
618
+ goto out;
659619 }
620
+
621
+ spin_lock_irq(&port->port_lock);
660622 }
661623
662
- /* REVISIT if REMOVED (ports[].port NULL), abort the open
663
- * to let rmmod work faster (but this way isn't wrong).
664
- */
665
-
666
- /* REVISIT maybe wait for "carrier detect" */
624
+ /* already open? Great. */
625
+ if (port->port.count++)
626
+ goto exit_unlock_port;
667627
668628 tty->driver_data = port;
669629 port->port.tty = tty;
670630
671
- port->port.count = 1;
672
- port->openclose = false;
673
-
674631 /* if connected, start the I/O stream */
675632 if (port->port_usb) {
676
- 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;
677636
678
- pr_debug("gs_open: start ttyGS%d\n", port->port_num);
679
- gs_start_io(port);
637
+ pr_debug("gs_open: start ttyGS%d\n", port->port_num);
638
+ gs_start_io(port);
680639
681
- if (gser->connect)
682
- 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
+ }
683646 }
684647
685648 pr_debug("gs_open: ttyGS%d (%p,%p)\n", port->port_num, tty, file);
686649
687
- status = 0;
688
-
689650 exit_unlock_port:
690651 spin_unlock_irq(&port->port_lock);
652
+out:
653
+ mutex_unlock(&ports[port_num].lock);
691654 return status;
692655 }
693656
694
-static int gs_writes_finished(struct gs_port *p)
657
+static int gs_close_flush_done(struct gs_port *p)
695658 {
696659 int cond;
697660
698
- /* return true on disconnect or empty buffer */
661
+ /* return true on disconnect or empty buffer or if raced with open() */
699662 spin_lock_irq(&p->port_lock);
700
- cond = (p->port_usb == NULL) || !kfifo_len(&p->port_write_buf);
663
+ cond = p->port_usb == NULL || !kfifo_len(&p->port_write_buf) ||
664
+ p->port.count > 1;
701665 spin_unlock_irq(&p->port_lock);
702666
703667 return cond;
....@@ -711,6 +675,7 @@
711675 spin_lock_irq(&port->port_lock);
712676
713677 if (port->port.count != 1) {
678
+raced_with_open:
714679 if (port->port.count == 0)
715680 WARN_ON(1);
716681 else
....@@ -720,14 +685,8 @@
720685
721686 pr_debug("gs_close: ttyGS%d (%p,%p) ...\n", port->port_num, tty, file);
722687
723
- /* mark port as closing but in use; we can drop port lock
724
- * and sleep if necessary
725
- */
726
- port->openclose = true;
727
- port->port.count = 0;
728
-
729688 gser = port->port_usb;
730
- if (gser && gser->disconnect)
689
+ if (gser && !port->suspended && gser->disconnect)
731690 gser->disconnect(gser);
732691
733692 /* wait for circular write buffer to drain, disconnect, or at
....@@ -736,24 +695,28 @@
736695 if (kfifo_len(&port->port_write_buf) > 0 && gser) {
737696 spin_unlock_irq(&port->port_lock);
738697 wait_event_interruptible_timeout(port->drain_wait,
739
- gs_writes_finished(port),
698
+ gs_close_flush_done(port),
740699 GS_CLOSE_TIMEOUT * HZ);
741700 spin_lock_irq(&port->port_lock);
701
+
702
+ if (port->port.count != 1)
703
+ goto raced_with_open;
704
+
742705 gser = port->port_usb;
743706 }
744707
745708 /* Iff we're disconnected, there can be no I/O in flight so it's
746709 * ok to free the circular buffer; else just scrub it. And don't
747
- * let the push tasklet fire again until we're re-opened.
710
+ * let the push async work fire again until we're re-opened.
748711 */
749712 if (gser == NULL)
750713 kfifo_free(&port->port_write_buf);
751714 else
752715 kfifo_reset(&port->port_write_buf);
753716
717
+ port->start_delayed = false;
718
+ port->port.count = 0;
754719 port->port.tty = NULL;
755
-
756
- port->openclose = false;
757720
758721 pr_debug("gs_close: ttyGS%d (%p,%p) done!\n",
759722 port->port_num, tty, file);
....@@ -856,8 +819,8 @@
856819 * rts/cts, or other handshaking with the host, but if the
857820 * read queue backs up enough we'll be NAKing OUT packets.
858821 */
859
- tasklet_schedule(&port->push);
860822 pr_vdebug("ttyGS%d: unthrottle\n", port->port_num);
823
+ schedule_delayed_work(&port->push, 0);
861824 }
862825 spin_unlock_irqrestore(&port->port_lock, flags);
863826 }
....@@ -898,50 +861,23 @@
898861
899862 #ifdef CONFIG_U_SERIAL_CONSOLE
900863
901
-static struct gscons_info gscons_info;
902
-static struct console gserial_cons;
903
-
904
-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)
905865 {
906
- struct usb_request *req = usb_ep_alloc_request(ep, GFP_ATOMIC);
907
- if (!req)
908
- return NULL;
909
-
910
- req->buf = kmalloc(ep->maxpacket, GFP_ATOMIC);
911
- if (!req->buf) {
912
- usb_ep_free_request(ep, req);
913
- return NULL;
914
- }
915
-
916
- return req;
917
-}
918
-
919
-static void gs_request_free(struct usb_request *req, struct usb_ep *ep)
920
-{
921
- if (!req)
922
- return;
923
-
924
- kfree(req->buf);
925
- usb_ep_free_request(ep, req);
926
-}
927
-
928
-static void gs_complete_out(struct usb_ep *ep, struct usb_request *req)
929
-{
930
- struct gscons_info *info = &gscons_info;
866
+ struct gs_console *cons = req->context;
931867
932868 switch (req->status) {
933869 default:
934870 pr_warn("%s: unexpected %s status %d\n",
935871 __func__, ep->name, req->status);
936
- /* fall through */
872
+ fallthrough;
937873 case 0:
938874 /* normal completion */
939
- spin_lock(&info->con_lock);
940
- info->req_busy = 0;
941
- spin_unlock(&info->con_lock);
942
-
943
- 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);
944879 break;
880
+ case -ECONNRESET:
945881 case -ESHUTDOWN:
946882 /* disconnect */
947883 pr_vdebug("%s: %s shutdown\n", __func__, ep->name);
....@@ -949,190 +885,250 @@
949885 }
950886 }
951887
952
-static int gs_console_connect(int port_num)
888
+static void __gs_console_push(struct gs_console *cons)
953889 {
954
- struct gscons_info *info = &gscons_info;
955
- struct gs_port *port;
890
+ struct usb_request *req = cons->req;
956891 struct usb_ep *ep;
892
+ size_t size;
957893
958
- if (port_num != gserial_cons.index) {
959
- pr_err("%s: port num [%d] is not support console\n",
960
- __func__, port_num);
961
- 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;
962912 }
963913
964
- port = ports[port_num].port;
965
- ep = port->port_usb->in;
966
- if (!info->console_req) {
967
- info->console_req = gs_request_new(ep);
968
- if (!info->console_req)
969
- return -ENOMEM;
970
- info->console_req->complete = gs_complete_out;
971
- }
972
-
973
- info->port = port;
974
- spin_lock(&info->con_lock);
975
- info->req_busy = 0;
976
- spin_unlock(&info->con_lock);
977
- pr_vdebug("port[%d] console connect!\n", port_num);
978
- return 0;
914
+ req->length = size;
915
+ if (usb_ep_queue(ep, req, GFP_ATOMIC))
916
+ req->length = 0;
979917 }
980918
981
-static void gs_console_disconnect(struct usb_ep *ep)
919
+static void gs_console_work(struct work_struct *work)
982920 {
983
- struct gscons_info *info = &gscons_info;
984
- struct usb_request *req = info->console_req;
921
+ struct gs_console *cons = container_of(work, struct gs_console, work);
985922
986
- gs_request_free(req, ep);
987
- info->console_req = NULL;
988
-}
923
+ spin_lock_irq(&cons->lock);
989924
990
-static int gs_console_thread(void *data)
991
-{
992
- struct gscons_info *info = &gscons_info;
993
- struct gs_port *port;
994
- struct usb_request *req;
995
- struct usb_ep *ep;
996
- int xfer, ret, count, size;
925
+ __gs_console_push(cons);
997926
998
- do {
999
- port = info->port;
1000
- set_current_state(TASK_INTERRUPTIBLE);
1001
- if (!port || !port->port_usb
1002
- || !port->port_usb->in || !info->console_req)
1003
- goto sched;
1004
-
1005
- req = info->console_req;
1006
- ep = port->port_usb->in;
1007
-
1008
- spin_lock_irq(&info->con_lock);
1009
- count = kfifo_len(&info->con_buf);
1010
- size = ep->maxpacket;
1011
-
1012
- if (count > 0 && !info->req_busy) {
1013
- set_current_state(TASK_RUNNING);
1014
- if (count < size)
1015
- size = count;
1016
-
1017
- xfer = kfifo_out(&info->con_buf, req->buf, size);
1018
- req->length = xfer;
1019
-
1020
- spin_unlock(&info->con_lock);
1021
- ret = usb_ep_queue(ep, req, GFP_ATOMIC);
1022
- spin_lock(&info->con_lock);
1023
- if (ret < 0)
1024
- info->req_busy = 0;
1025
- else
1026
- info->req_busy = 1;
1027
-
1028
- spin_unlock_irq(&info->con_lock);
1029
- } else {
1030
- spin_unlock_irq(&info->con_lock);
1031
-sched:
1032
- if (kthread_should_stop()) {
1033
- set_current_state(TASK_RUNNING);
1034
- break;
1035
- }
1036
- schedule();
1037
- }
1038
- } while (1);
1039
-
1040
- return 0;
1041
-}
1042
-
1043
-static int gs_console_setup(struct console *co, char *options)
1044
-{
1045
- struct gscons_info *info = &gscons_info;
1046
- int status;
1047
-
1048
- info->port = NULL;
1049
- info->console_req = NULL;
1050
- info->req_busy = 0;
1051
- spin_lock_init(&info->con_lock);
1052
-
1053
- status = kfifo_alloc(&info->con_buf, GS_CONSOLE_BUF_SIZE, GFP_KERNEL);
1054
- if (status) {
1055
- pr_err("%s: allocate console buffer failed\n", __func__);
1056
- return status;
1057
- }
1058
-
1059
- info->console_thread = kthread_create(gs_console_thread,
1060
- co, "gs_console");
1061
- if (IS_ERR(info->console_thread)) {
1062
- pr_err("%s: cannot create console thread\n", __func__);
1063
- kfifo_free(&info->con_buf);
1064
- return PTR_ERR(info->console_thread);
1065
- }
1066
- wake_up_process(info->console_thread);
1067
-
1068
- return 0;
927
+ spin_unlock_irq(&cons->lock);
1069928 }
1070929
1071930 static void gs_console_write(struct console *co,
1072931 const char *buf, unsigned count)
1073932 {
1074
- struct gscons_info *info = &gscons_info;
933
+ struct gs_console *cons = container_of(co, struct gs_console, console);
1075934 unsigned long flags;
935
+ size_t n;
1076936
1077
- spin_lock_irqsave(&info->con_lock, flags);
1078
- kfifo_in(&info->con_buf, buf, count);
1079
- spin_unlock_irqrestore(&info->con_lock, flags);
937
+ spin_lock_irqsave(&cons->lock, flags);
1080938
1081
- 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);
1082947 }
1083948
1084949 static struct tty_driver *gs_console_device(struct console *co, int *index)
1085950 {
1086
- struct tty_driver **p = (struct tty_driver **)co->data;
1087
-
1088
- if (!*p)
1089
- return NULL;
1090
-
1091951 *index = co->index;
1092
- return *p;
952
+ return gs_tty_driver;
1093953 }
1094954
1095
-static struct console gserial_cons = {
1096
- .name = "ttyGS",
1097
- .write = gs_console_write,
1098
- .device = gs_console_device,
1099
- .setup = gs_console_setup,
1100
- .flags = CON_PRINTBUFFER,
1101
- .index = -1,
1102
- .data = &gs_tty_driver,
1103
-};
1104
-
1105
-static void gserial_console_init(void)
955
+static int gs_console_connect(struct gs_port *port)
1106956 {
1107
- 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;
1108982 }
1109983
1110
-static void gserial_console_exit(void)
984
+static void gs_console_disconnect(struct gs_port *port)
1111985 {
1112
- struct gscons_info *info = &gscons_info;
986
+ struct gs_console *cons = port->console;
987
+ struct usb_request *req;
988
+ struct usb_ep *ep;
1113989
1114
- unregister_console(&gserial_cons);
1115
- if (!IS_ERR_OR_NULL(info->console_thread))
1116
- kthread_stop(info->console_thread);
1117
- 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);
11181006 }
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);
11191114
11201115 #else
11211116
1122
-static int gs_console_connect(int port_num)
1117
+static int gs_console_connect(struct gs_port *port)
11231118 {
11241119 return 0;
11251120 }
11261121
1127
-static void gs_console_disconnect(struct usb_ep *ep)
1122
+static void gs_console_disconnect(struct gs_port *port)
11281123 {
11291124 }
11301125
1131
-static void gserial_console_init(void)
1126
+static int gs_console_init(struct gs_port *port)
11321127 {
1128
+ return -ENOSYS;
11331129 }
11341130
1135
-static void gserial_console_exit(void)
1131
+static void gs_console_exit(struct gs_port *port)
11361132 {
11371133 }
11381134
....@@ -1161,7 +1157,7 @@
11611157 init_waitqueue_head(&port->drain_wait);
11621158 init_waitqueue_head(&port->close_wait);
11631159
1164
- tasklet_init(&port->push, gs_rx_push, (unsigned long) port);
1160
+ INIT_DELAYED_WORK(&port->push, gs_rx_push);
11651161
11661162 INIT_LIST_HEAD(&port->read_pool);
11671163 INIT_LIST_HEAD(&port->read_queue);
....@@ -1181,14 +1177,15 @@
11811177 int cond;
11821178
11831179 spin_lock_irq(&port->port_lock);
1184
- cond = (port->port.count == 0) && !port->openclose;
1180
+ cond = port->port.count == 0;
11851181 spin_unlock_irq(&port->port_lock);
1182
+
11861183 return cond;
11871184 }
11881185
11891186 static void gserial_free_port(struct gs_port *port)
11901187 {
1191
- tasklet_kill(&port->push);
1188
+ cancel_delayed_work_sync(&port->push);
11921189 /* wait for old opens to finish */
11931190 wait_event(port->close_wait, gs_closed(port));
11941191 WARN_ON(port->port_usb != NULL);
....@@ -1206,18 +1203,19 @@
12061203 return;
12071204 }
12081205 port = ports[port_num].port;
1206
+ gs_console_exit(port);
12091207 ports[port_num].port = NULL;
12101208 mutex_unlock(&ports[port_num].lock);
12111209
12121210 gserial_free_port(port);
12131211 tty_unregister_device(gs_tty_driver, port_num);
1214
- gserial_console_exit();
12151212 }
12161213 EXPORT_SYMBOL_GPL(gserial_free_line);
12171214
1218
-int gserial_alloc_line(unsigned char *line_num)
1215
+int gserial_alloc_line_no_console(unsigned char *line_num)
12191216 {
12201217 struct usb_cdc_line_coding coding;
1218
+ struct gs_port *port;
12211219 struct device *tty_dev;
12221220 int ret;
12231221 int port_num;
....@@ -1240,24 +1238,33 @@
12401238
12411239 /* ... and sysfs class devices, so mdev/udev make /dev/ttyGS* */
12421240
1243
- 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,
12441243 gs_tty_driver, port_num, NULL);
12451244 if (IS_ERR(tty_dev)) {
1246
- struct gs_port *port;
12471245 pr_err("%s: failed to register tty for port %d, err %ld\n",
12481246 __func__, port_num, PTR_ERR(tty_dev));
12491247
12501248 ret = PTR_ERR(tty_dev);
12511249 mutex_lock(&ports[port_num].lock);
1252
- port = ports[port_num].port;
12531250 ports[port_num].port = NULL;
12541251 mutex_unlock(&ports[port_num].lock);
12551252 gserial_free_port(port);
12561253 goto err;
12571254 }
12581255 *line_num = port_num;
1259
- gserial_console_init();
12601256 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
+
12611268 return ret;
12621269 }
12631270 EXPORT_SYMBOL_GPL(gserial_alloc_line);
....@@ -1338,7 +1345,7 @@
13381345 gser->disconnect(gser);
13391346 }
13401347
1341
- status = gs_console_connect(port_num);
1348
+ status = gs_console_connect(port);
13421349 spin_unlock_irqrestore(&port->port_lock, flags);
13431350
13441351 return status;
....@@ -1370,16 +1377,19 @@
13701377 /* tell the TTY glue not to do I/O here any more */
13711378 spin_lock_irqsave(&port->port_lock, flags);
13721379
1380
+ gs_console_disconnect(port);
1381
+
13731382 /* REVISIT as above: how best to track this? */
13741383 port->port_line_coding = gser->port_line_coding;
13751384
13761385 port->port_usb = NULL;
13771386 gser->ioport = NULL;
1378
- if (port->port.count > 0 || port->openclose) {
1387
+ if (port->port.count > 0) {
13791388 wake_up_interruptible(&port->drain_wait);
13801389 if (port->port.tty)
13811390 tty_hangup(port->port.tty);
13821391 }
1392
+ port->suspended = false;
13831393 spin_unlock_irqrestore(&port->port_lock, flags);
13841394
13851395 /* disable endpoints, aborting down any active I/O */
....@@ -1388,7 +1398,7 @@
13881398
13891399 /* finally, free any unused/unusable I/O buffers */
13901400 spin_lock_irqsave(&port->port_lock, flags);
1391
- if (port->port.count == 0 && !port->openclose)
1401
+ if (port->port.count == 0)
13921402 kfifo_free(&port->port_write_buf);
13931403 gs_free_requests(gser->out, &port->read_pool, NULL);
13941404 gs_free_requests(gser->out, &port->read_queue, NULL);
....@@ -1397,11 +1407,42 @@
13971407 port->read_allocated = port->read_started =
13981408 port->write_allocated = port->write_started = 0;
13991409
1400
- gs_console_disconnect(gser->in);
14011410 spin_unlock_irqrestore(&port->port_lock, flags);
14021411 }
14031412 EXPORT_SYMBOL_GPL(gserial_disconnect);
14041413
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
+
14051446 static int userial_init(void)
14061447 {
14071448 unsigned i;