hc
2024-10-12 a5969cabbb4660eab42b6ef0412cbbd1200cf14d
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"
....@@ -81,15 +81,17 @@
8181 #define WRITE_BUF_SIZE 8192 /* TX only */
8282 #define GS_CONSOLE_BUF_SIZE 8192
8383
84
+/* Prevents race conditions while accessing gser->ioport */
85
+static DEFINE_SPINLOCK(serial_port_lock);
86
+
8487 /* 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;
88
+struct gs_console {
89
+ struct console console;
90
+ struct work_struct work;
91
+ spinlock_t lock;
92
+ struct usb_request *req;
93
+ struct kfifo buf;
94
+ size_t missed;
9395 };
9496
9597 /*
....@@ -101,8 +103,10 @@
101103 spinlock_t port_lock; /* guard port_* access */
102104
103105 struct gserial *port_usb;
106
+#ifdef CONFIG_U_SERIAL_CONSOLE
107
+ struct gs_console *console;
108
+#endif
104109
105
- bool openclose; /* open/close in progress */
106110 u8 port_num;
107111
108112 struct list_head read_pool;
....@@ -110,7 +114,7 @@
110114 int read_allocated;
111115 struct list_head read_queue;
112116 unsigned n_read;
113
- struct tasklet_struct push;
117
+ struct delayed_work push;
114118
115119 struct list_head write_pool;
116120 int write_started;
....@@ -119,6 +123,8 @@
119123 wait_queue_head_t drain_wait; /* wait while writes drain */
120124 bool write_busy;
121125 wait_queue_head_t close_wait;
126
+ bool suspended; /* port suspended */
127
+ bool start_delayed; /* delay start when suspended */
122128
123129 /* REVISIT this state ... */
124130 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
....@@ -255,9 +261,7 @@
255261 list_del(&req->list);
256262 req->zero = kfifo_is_empty(&port->port_write_buf);
257263
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));
264
+ pr_vdebug("ttyGS%d: tx len=%d, %3ph ...\n", port->port_num, len, req->buf);
261265
262266 /* Drop lock while we call out of driver; completions
263267 * could be issued while we do so. Disconnection may
....@@ -343,7 +347,7 @@
343347 }
344348
345349 /*
346
- * RX tasklet takes data out of the RX queue and hands it up to the TTY
350
+ * RX work takes data out of the RX queue and hands it up to the TTY
347351 * layer until it refuses to take any more data (or is throttled back).
348352 * Then it issues reads for any further data.
349353 *
....@@ -352,9 +356,10 @@
352356 * So QUEUE_SIZE packets plus however many the FIFO holds (usually two)
353357 * can be buffered before the TTY layer's buffers (currently 64 KB).
354358 */
355
-static void gs_rx_push(unsigned long _port)
359
+static void gs_rx_push(struct work_struct *work)
356360 {
357
- struct gs_port *port = (void *)_port;
361
+ struct delayed_work *w = to_delayed_work(work);
362
+ struct gs_port *port = container_of(w, struct gs_port, push);
358363 struct tty_struct *tty;
359364 struct list_head *queue = &port->read_queue;
360365 bool disconnect = false;
....@@ -382,7 +387,7 @@
382387 /* presumably a transient fault */
383388 pr_warn("ttyGS%d: unexpected RX status %d\n",
384389 port->port_num, req->status);
385
- /* FALLTHROUGH */
390
+ fallthrough;
386391 case 0:
387392 /* normal completion */
388393 break;
....@@ -429,21 +434,13 @@
429434
430435 /* We want our data queue to become empty ASAP, keeping data
431436 * 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...
437
+ * this time around, RX may be starved, so wait until next jiffy.
434438 *
435
- * REVISIT we should probably add a timer to keep the tasklet
436
- * from starving ... but it's not clear that case ever happens.
439
+ * We may leave non-empty queue only when there is a tty, and
440
+ * either it is throttled or there is no more room in flip buffer.
437441 */
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
- }
442
+ if (!list_empty(queue) && !tty_throttled(tty))
443
+ schedule_delayed_work(&port->push, 1);
447444
448445 /* If we're still connected, refill the USB RX queue. */
449446 if (!disconnect && port->port_usb)
....@@ -459,7 +456,7 @@
459456 /* Queue all received data until the tty layer is ready for it. */
460457 spin_lock(&port->port_lock);
461458 list_add_tail(&req->list, &port->read_queue);
462
- tasklet_schedule(&port->push);
459
+ schedule_delayed_work(&port->push, 0);
463460 spin_unlock(&port->port_lock);
464461 }
465462
....@@ -476,7 +473,7 @@
476473 /* presumably a transient fault */
477474 pr_warn("%s: unexpected %s status %d\n",
478475 __func__, ep->name, req->status);
479
- /* FALL THROUGH */
476
+ fallthrough;
480477 case 0:
481478 /* normal completion */
482479 gs_start_tx(port);
....@@ -531,7 +528,7 @@
531528
532529 /**
533530 * gs_start_io - start USB I/O streams
534
- * @dev: encapsulates endpoints to use
531
+ * @port: port to use
535532 * Context: holding port_lock; port_tty and port_usb are non-null
536533 *
537534 * We only start I/O when something is connected to both sides of
....@@ -595,109 +592,79 @@
595592 {
596593 int port_num = tty->index;
597594 struct gs_port *port;
598
- int status;
595
+ int status = 0;
599596
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);
597
+ mutex_lock(&ports[port_num].lock);
598
+ port = ports[port_num].port;
599
+ if (!port) {
600
+ status = -ENODEV;
601
+ goto out;
602
+ }
607603
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" */
644604 spin_lock_irq(&port->port_lock);
645605
646606 /* allocate circular buffer on first open */
647607 if (!kfifo_initialized(&port->port_write_buf)) {
648608
649609 spin_unlock_irq(&port->port_lock);
610
+
611
+ /*
612
+ * portmaster's mutex still protects from simultaneous open(),
613
+ * and close() can't happen, yet.
614
+ */
615
+
650616 status = kfifo_alloc(&port->port_write_buf,
651617 WRITE_BUF_SIZE, GFP_KERNEL);
652
- spin_lock_irq(&port->port_lock);
653
-
654618 if (status) {
655619 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;
620
+ port_num, tty, file);
621
+ goto out;
659622 }
623
+
624
+ spin_lock_irq(&port->port_lock);
660625 }
661626
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" */
627
+ /* already open? Great. */
628
+ if (port->port.count++)
629
+ goto exit_unlock_port;
667630
668631 tty->driver_data = port;
669632 port->port.tty = tty;
670633
671
- port->port.count = 1;
672
- port->openclose = false;
673
-
674634 /* if connected, start the I/O stream */
675635 if (port->port_usb) {
676
- struct gserial *gser = port->port_usb;
636
+ /* if port is suspended, wait resume to start I/0 stream */
637
+ if (!port->suspended) {
638
+ struct gserial *gser = port->port_usb;
677639
678
- pr_debug("gs_open: start ttyGS%d\n", port->port_num);
679
- gs_start_io(port);
640
+ pr_debug("gs_open: start ttyGS%d\n", port->port_num);
641
+ gs_start_io(port);
680642
681
- if (gser->connect)
682
- gser->connect(gser);
643
+ if (gser->connect)
644
+ gser->connect(gser);
645
+ } else {
646
+ pr_debug("delay start of ttyGS%d\n", port->port_num);
647
+ port->start_delayed = true;
648
+ }
683649 }
684650
685651 pr_debug("gs_open: ttyGS%d (%p,%p)\n", port->port_num, tty, file);
686652
687
- status = 0;
688
-
689653 exit_unlock_port:
690654 spin_unlock_irq(&port->port_lock);
655
+out:
656
+ mutex_unlock(&ports[port_num].lock);
691657 return status;
692658 }
693659
694
-static int gs_writes_finished(struct gs_port *p)
660
+static int gs_close_flush_done(struct gs_port *p)
695661 {
696662 int cond;
697663
698
- /* return true on disconnect or empty buffer */
664
+ /* return true on disconnect or empty buffer or if raced with open() */
699665 spin_lock_irq(&p->port_lock);
700
- cond = (p->port_usb == NULL) || !kfifo_len(&p->port_write_buf);
666
+ cond = p->port_usb == NULL || !kfifo_len(&p->port_write_buf) ||
667
+ p->port.count > 1;
701668 spin_unlock_irq(&p->port_lock);
702669
703670 return cond;
....@@ -711,6 +678,7 @@
711678 spin_lock_irq(&port->port_lock);
712679
713680 if (port->port.count != 1) {
681
+raced_with_open:
714682 if (port->port.count == 0)
715683 WARN_ON(1);
716684 else
....@@ -720,14 +688,8 @@
720688
721689 pr_debug("gs_close: ttyGS%d (%p,%p) ...\n", port->port_num, tty, file);
722690
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
-
729691 gser = port->port_usb;
730
- if (gser && gser->disconnect)
692
+ if (gser && !port->suspended && gser->disconnect)
731693 gser->disconnect(gser);
732694
733695 /* wait for circular write buffer to drain, disconnect, or at
....@@ -736,24 +698,28 @@
736698 if (kfifo_len(&port->port_write_buf) > 0 && gser) {
737699 spin_unlock_irq(&port->port_lock);
738700 wait_event_interruptible_timeout(port->drain_wait,
739
- gs_writes_finished(port),
701
+ gs_close_flush_done(port),
740702 GS_CLOSE_TIMEOUT * HZ);
741703 spin_lock_irq(&port->port_lock);
704
+
705
+ if (port->port.count != 1)
706
+ goto raced_with_open;
707
+
742708 gser = port->port_usb;
743709 }
744710
745711 /* Iff we're disconnected, there can be no I/O in flight so it's
746712 * 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.
713
+ * let the push async work fire again until we're re-opened.
748714 */
749715 if (gser == NULL)
750716 kfifo_free(&port->port_write_buf);
751717 else
752718 kfifo_reset(&port->port_write_buf);
753719
720
+ port->start_delayed = false;
721
+ port->port.count = 0;
754722 port->port.tty = NULL;
755
-
756
- port->openclose = false;
757723
758724 pr_debug("gs_close: ttyGS%d (%p,%p) done!\n",
759725 port->port_num, tty, file);
....@@ -856,8 +822,8 @@
856822 * rts/cts, or other handshaking with the host, but if the
857823 * read queue backs up enough we'll be NAKing OUT packets.
858824 */
859
- tasklet_schedule(&port->push);
860825 pr_vdebug("ttyGS%d: unthrottle\n", port->port_num);
826
+ schedule_delayed_work(&port->push, 0);
861827 }
862828 spin_unlock_irqrestore(&port->port_lock, flags);
863829 }
....@@ -898,50 +864,23 @@
898864
899865 #ifdef CONFIG_U_SERIAL_CONSOLE
900866
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)
867
+static void gs_console_complete_out(struct usb_ep *ep, struct usb_request *req)
905868 {
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;
869
+ struct gs_console *cons = req->context;
931870
932871 switch (req->status) {
933872 default:
934873 pr_warn("%s: unexpected %s status %d\n",
935874 __func__, ep->name, req->status);
936
- /* fall through */
875
+ fallthrough;
937876 case 0:
938877 /* 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);
878
+ spin_lock(&cons->lock);
879
+ req->length = 0;
880
+ schedule_work(&cons->work);
881
+ spin_unlock(&cons->lock);
944882 break;
883
+ case -ECONNRESET:
945884 case -ESHUTDOWN:
946885 /* disconnect */
947886 pr_vdebug("%s: %s shutdown\n", __func__, ep->name);
....@@ -949,190 +888,253 @@
949888 }
950889 }
951890
952
-static int gs_console_connect(int port_num)
891
+static void __gs_console_push(struct gs_console *cons)
953892 {
954
- struct gscons_info *info = &gscons_info;
955
- struct gs_port *port;
893
+ struct usb_request *req = cons->req;
956894 struct usb_ep *ep;
895
+ size_t size;
957896
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;
897
+ if (!req)
898
+ return; /* disconnected */
899
+
900
+ if (req->length)
901
+ return; /* busy */
902
+
903
+ ep = cons->console.data;
904
+ size = kfifo_out(&cons->buf, req->buf, ep->maxpacket);
905
+ if (!size)
906
+ return;
907
+
908
+ if (cons->missed && ep->maxpacket >= 64) {
909
+ char buf[64];
910
+ size_t len;
911
+
912
+ len = sprintf(buf, "\n[missed %zu bytes]\n", cons->missed);
913
+ kfifo_in(&cons->buf, buf, len);
914
+ cons->missed = 0;
962915 }
963916
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
- }
917
+ req->length = size;
972918
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;
919
+ spin_unlock_irq(&cons->lock);
920
+ if (usb_ep_queue(ep, req, GFP_ATOMIC))
921
+ req->length = 0;
922
+ spin_lock_irq(&cons->lock);
979923 }
980924
981
-static void gs_console_disconnect(struct usb_ep *ep)
925
+static void gs_console_work(struct work_struct *work)
982926 {
983
- struct gscons_info *info = &gscons_info;
984
- struct usb_request *req = info->console_req;
927
+ struct gs_console *cons = container_of(work, struct gs_console, work);
985928
986
- gs_request_free(req, ep);
987
- info->console_req = NULL;
988
-}
929
+ spin_lock_irq(&cons->lock);
989930
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;
931
+ __gs_console_push(cons);
997932
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;
933
+ spin_unlock_irq(&cons->lock);
1069934 }
1070935
1071936 static void gs_console_write(struct console *co,
1072937 const char *buf, unsigned count)
1073938 {
1074
- struct gscons_info *info = &gscons_info;
939
+ struct gs_console *cons = container_of(co, struct gs_console, console);
1075940 unsigned long flags;
941
+ size_t n;
1076942
1077
- spin_lock_irqsave(&info->con_lock, flags);
1078
- kfifo_in(&info->con_buf, buf, count);
1079
- spin_unlock_irqrestore(&info->con_lock, flags);
943
+ spin_lock_irqsave(&cons->lock, flags);
1080944
1081
- wake_up_process(info->console_thread);
945
+ n = kfifo_in(&cons->buf, buf, count);
946
+ if (n < count)
947
+ cons->missed += count - n;
948
+
949
+ if (cons->req && !cons->req->length)
950
+ schedule_work(&cons->work);
951
+
952
+ spin_unlock_irqrestore(&cons->lock, flags);
1082953 }
1083954
1084955 static struct tty_driver *gs_console_device(struct console *co, int *index)
1085956 {
1086
- struct tty_driver **p = (struct tty_driver **)co->data;
1087
-
1088
- if (!*p)
1089
- return NULL;
1090
-
1091957 *index = co->index;
1092
- return *p;
958
+ return gs_tty_driver;
1093959 }
1094960
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)
961
+static int gs_console_connect(struct gs_port *port)
1106962 {
1107
- register_console(&gserial_cons);
963
+ struct gs_console *cons = port->console;
964
+ struct usb_request *req;
965
+ struct usb_ep *ep;
966
+
967
+ if (!cons)
968
+ return 0;
969
+
970
+ ep = port->port_usb->in;
971
+ req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC);
972
+ if (!req)
973
+ return -ENOMEM;
974
+ req->complete = gs_console_complete_out;
975
+ req->context = cons;
976
+ req->length = 0;
977
+
978
+ spin_lock(&cons->lock);
979
+ cons->req = req;
980
+ cons->console.data = ep;
981
+ spin_unlock(&cons->lock);
982
+
983
+ pr_debug("ttyGS%d: console connected!\n", port->port_num);
984
+
985
+ schedule_work(&cons->work);
986
+
987
+ return 0;
1108988 }
1109989
1110
-static void gserial_console_exit(void)
990
+static void gs_console_disconnect(struct gs_port *port)
1111991 {
1112
- struct gscons_info *info = &gscons_info;
992
+ struct gs_console *cons = port->console;
993
+ struct usb_request *req;
994
+ struct usb_ep *ep;
1113995
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);
996
+ if (!cons)
997
+ return;
998
+
999
+ spin_lock(&cons->lock);
1000
+
1001
+ req = cons->req;
1002
+ ep = cons->console.data;
1003
+ cons->req = NULL;
1004
+
1005
+ spin_unlock(&cons->lock);
1006
+
1007
+ if (!req)
1008
+ return;
1009
+
1010
+ usb_ep_dequeue(ep, req);
1011
+ gs_free_req(ep, req);
11181012 }
1013
+
1014
+static int gs_console_init(struct gs_port *port)
1015
+{
1016
+ struct gs_console *cons;
1017
+ int err;
1018
+
1019
+ if (port->console)
1020
+ return 0;
1021
+
1022
+ cons = kzalloc(sizeof(*port->console), GFP_KERNEL);
1023
+ if (!cons)
1024
+ return -ENOMEM;
1025
+
1026
+ strcpy(cons->console.name, "ttyGS");
1027
+ cons->console.write = gs_console_write;
1028
+ cons->console.device = gs_console_device;
1029
+ cons->console.flags = CON_PRINTBUFFER;
1030
+ cons->console.index = port->port_num;
1031
+
1032
+ INIT_WORK(&cons->work, gs_console_work);
1033
+ spin_lock_init(&cons->lock);
1034
+
1035
+ err = kfifo_alloc(&cons->buf, GS_CONSOLE_BUF_SIZE, GFP_KERNEL);
1036
+ if (err) {
1037
+ pr_err("ttyGS%d: allocate console buffer failed\n", port->port_num);
1038
+ kfree(cons);
1039
+ return err;
1040
+ }
1041
+
1042
+ port->console = cons;
1043
+ register_console(&cons->console);
1044
+
1045
+ spin_lock_irq(&port->port_lock);
1046
+ if (port->port_usb)
1047
+ gs_console_connect(port);
1048
+ spin_unlock_irq(&port->port_lock);
1049
+
1050
+ return 0;
1051
+}
1052
+
1053
+static void gs_console_exit(struct gs_port *port)
1054
+{
1055
+ struct gs_console *cons = port->console;
1056
+
1057
+ if (!cons)
1058
+ return;
1059
+
1060
+ unregister_console(&cons->console);
1061
+
1062
+ spin_lock_irq(&port->port_lock);
1063
+ if (cons->req)
1064
+ gs_console_disconnect(port);
1065
+ spin_unlock_irq(&port->port_lock);
1066
+
1067
+ cancel_work_sync(&cons->work);
1068
+ kfifo_free(&cons->buf);
1069
+ kfree(cons);
1070
+ port->console = NULL;
1071
+}
1072
+
1073
+ssize_t gserial_set_console(unsigned char port_num, const char *page, size_t count)
1074
+{
1075
+ struct gs_port *port;
1076
+ bool enable;
1077
+ int ret;
1078
+
1079
+ ret = strtobool(page, &enable);
1080
+ if (ret)
1081
+ return ret;
1082
+
1083
+ mutex_lock(&ports[port_num].lock);
1084
+ port = ports[port_num].port;
1085
+
1086
+ if (WARN_ON(port == NULL)) {
1087
+ ret = -ENXIO;
1088
+ goto out;
1089
+ }
1090
+
1091
+ if (enable)
1092
+ ret = gs_console_init(port);
1093
+ else
1094
+ gs_console_exit(port);
1095
+out:
1096
+ mutex_unlock(&ports[port_num].lock);
1097
+
1098
+ return ret < 0 ? ret : count;
1099
+}
1100
+EXPORT_SYMBOL_GPL(gserial_set_console);
1101
+
1102
+ssize_t gserial_get_console(unsigned char port_num, char *page)
1103
+{
1104
+ struct gs_port *port;
1105
+ ssize_t 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
+ else
1113
+ ret = sprintf(page, "%u\n", !!port->console);
1114
+
1115
+ mutex_unlock(&ports[port_num].lock);
1116
+
1117
+ return ret;
1118
+}
1119
+EXPORT_SYMBOL_GPL(gserial_get_console);
11191120
11201121 #else
11211122
1122
-static int gs_console_connect(int port_num)
1123
+static int gs_console_connect(struct gs_port *port)
11231124 {
11241125 return 0;
11251126 }
11261127
1127
-static void gs_console_disconnect(struct usb_ep *ep)
1128
+static void gs_console_disconnect(struct gs_port *port)
11281129 {
11291130 }
11301131
1131
-static void gserial_console_init(void)
1132
+static int gs_console_init(struct gs_port *port)
11321133 {
1134
+ return -ENOSYS;
11331135 }
11341136
1135
-static void gserial_console_exit(void)
1137
+static void gs_console_exit(struct gs_port *port)
11361138 {
11371139 }
11381140
....@@ -1161,7 +1163,7 @@
11611163 init_waitqueue_head(&port->drain_wait);
11621164 init_waitqueue_head(&port->close_wait);
11631165
1164
- tasklet_init(&port->push, gs_rx_push, (unsigned long) port);
1166
+ INIT_DELAYED_WORK(&port->push, gs_rx_push);
11651167
11661168 INIT_LIST_HEAD(&port->read_pool);
11671169 INIT_LIST_HEAD(&port->read_queue);
....@@ -1181,14 +1183,15 @@
11811183 int cond;
11821184
11831185 spin_lock_irq(&port->port_lock);
1184
- cond = (port->port.count == 0) && !port->openclose;
1186
+ cond = port->port.count == 0;
11851187 spin_unlock_irq(&port->port_lock);
1188
+
11861189 return cond;
11871190 }
11881191
11891192 static void gserial_free_port(struct gs_port *port)
11901193 {
1191
- tasklet_kill(&port->push);
1194
+ cancel_delayed_work_sync(&port->push);
11921195 /* wait for old opens to finish */
11931196 wait_event(port->close_wait, gs_closed(port));
11941197 WARN_ON(port->port_usb != NULL);
....@@ -1206,18 +1209,19 @@
12061209 return;
12071210 }
12081211 port = ports[port_num].port;
1212
+ gs_console_exit(port);
12091213 ports[port_num].port = NULL;
12101214 mutex_unlock(&ports[port_num].lock);
12111215
12121216 gserial_free_port(port);
12131217 tty_unregister_device(gs_tty_driver, port_num);
1214
- gserial_console_exit();
12151218 }
12161219 EXPORT_SYMBOL_GPL(gserial_free_line);
12171220
1218
-int gserial_alloc_line(unsigned char *line_num)
1221
+int gserial_alloc_line_no_console(unsigned char *line_num)
12191222 {
12201223 struct usb_cdc_line_coding coding;
1224
+ struct gs_port *port;
12211225 struct device *tty_dev;
12221226 int ret;
12231227 int port_num;
....@@ -1240,24 +1244,33 @@
12401244
12411245 /* ... and sysfs class devices, so mdev/udev make /dev/ttyGS* */
12421246
1243
- tty_dev = tty_port_register_device(&ports[port_num].port->port,
1247
+ port = ports[port_num].port;
1248
+ tty_dev = tty_port_register_device(&port->port,
12441249 gs_tty_driver, port_num, NULL);
12451250 if (IS_ERR(tty_dev)) {
1246
- struct gs_port *port;
12471251 pr_err("%s: failed to register tty for port %d, err %ld\n",
12481252 __func__, port_num, PTR_ERR(tty_dev));
12491253
12501254 ret = PTR_ERR(tty_dev);
12511255 mutex_lock(&ports[port_num].lock);
1252
- port = ports[port_num].port;
12531256 ports[port_num].port = NULL;
12541257 mutex_unlock(&ports[port_num].lock);
12551258 gserial_free_port(port);
12561259 goto err;
12571260 }
12581261 *line_num = port_num;
1259
- gserial_console_init();
12601262 err:
1263
+ return ret;
1264
+}
1265
+EXPORT_SYMBOL_GPL(gserial_alloc_line_no_console);
1266
+
1267
+int gserial_alloc_line(unsigned char *line_num)
1268
+{
1269
+ int ret = gserial_alloc_line_no_console(line_num);
1270
+
1271
+ if (!ret && !*line_num)
1272
+ gs_console_init(ports[*line_num].port);
1273
+
12611274 return ret;
12621275 }
12631276 EXPORT_SYMBOL_GPL(gserial_alloc_line);
....@@ -1338,7 +1351,7 @@
13381351 gser->disconnect(gser);
13391352 }
13401353
1341
- status = gs_console_connect(port_num);
1354
+ status = gs_console_connect(port);
13421355 spin_unlock_irqrestore(&port->port_lock, flags);
13431356
13441357 return status;
....@@ -1367,20 +1380,26 @@
13671380 if (!port)
13681381 return;
13691382
1383
+ spin_lock_irqsave(&serial_port_lock, flags);
1384
+
13701385 /* tell the TTY glue not to do I/O here any more */
1371
- spin_lock_irqsave(&port->port_lock, flags);
1386
+ spin_lock(&port->port_lock);
1387
+
1388
+ gs_console_disconnect(port);
13721389
13731390 /* REVISIT as above: how best to track this? */
13741391 port->port_line_coding = gser->port_line_coding;
13751392
13761393 port->port_usb = NULL;
13771394 gser->ioport = NULL;
1378
- if (port->port.count > 0 || port->openclose) {
1395
+ if (port->port.count > 0) {
13791396 wake_up_interruptible(&port->drain_wait);
13801397 if (port->port.tty)
13811398 tty_hangup(port->port.tty);
13821399 }
1383
- spin_unlock_irqrestore(&port->port_lock, flags);
1400
+ port->suspended = false;
1401
+ spin_unlock(&port->port_lock);
1402
+ spin_unlock_irqrestore(&serial_port_lock, flags);
13841403
13851404 /* disable endpoints, aborting down any active I/O */
13861405 usb_ep_disable(gser->out);
....@@ -1388,7 +1407,7 @@
13881407
13891408 /* finally, free any unused/unusable I/O buffers */
13901409 spin_lock_irqsave(&port->port_lock, flags);
1391
- if (port->port.count == 0 && !port->openclose)
1410
+ if (port->port.count == 0)
13921411 kfifo_free(&port->port_write_buf);
13931412 gs_free_requests(gser->out, &port->read_pool, NULL);
13941413 gs_free_requests(gser->out, &port->read_queue, NULL);
....@@ -1397,11 +1416,60 @@
13971416 port->read_allocated = port->read_started =
13981417 port->write_allocated = port->write_started = 0;
13991418
1400
- gs_console_disconnect(gser->in);
14011419 spin_unlock_irqrestore(&port->port_lock, flags);
14021420 }
14031421 EXPORT_SYMBOL_GPL(gserial_disconnect);
14041422
1423
+void gserial_suspend(struct gserial *gser)
1424
+{
1425
+ struct gs_port *port;
1426
+ unsigned long flags;
1427
+
1428
+ spin_lock_irqsave(&serial_port_lock, flags);
1429
+ port = gser->ioport;
1430
+
1431
+ if (!port) {
1432
+ spin_unlock_irqrestore(&serial_port_lock, flags);
1433
+ return;
1434
+ }
1435
+
1436
+ spin_lock(&port->port_lock);
1437
+ spin_unlock(&serial_port_lock);
1438
+ port->suspended = true;
1439
+ spin_unlock_irqrestore(&port->port_lock, flags);
1440
+}
1441
+EXPORT_SYMBOL_GPL(gserial_suspend);
1442
+
1443
+void gserial_resume(struct gserial *gser)
1444
+{
1445
+ struct gs_port *port;
1446
+ unsigned long flags;
1447
+
1448
+ spin_lock_irqsave(&serial_port_lock, flags);
1449
+ port = gser->ioport;
1450
+
1451
+ if (!port) {
1452
+ spin_unlock_irqrestore(&serial_port_lock, flags);
1453
+ return;
1454
+ }
1455
+
1456
+ spin_lock(&port->port_lock);
1457
+ spin_unlock(&serial_port_lock);
1458
+ port->suspended = false;
1459
+ if (!port->start_delayed) {
1460
+ spin_unlock_irqrestore(&port->port_lock, flags);
1461
+ return;
1462
+ }
1463
+
1464
+ pr_debug("delayed start ttyGS%d\n", port->port_num);
1465
+ gs_start_io(port);
1466
+ if (gser->connect)
1467
+ gser->connect(gser);
1468
+ port->start_delayed = false;
1469
+ spin_unlock_irqrestore(&port->port_lock, flags);
1470
+}
1471
+EXPORT_SYMBOL_GPL(gserial_resume);
1472
+
14051473 static int userial_init(void)
14061474 {
14071475 unsigned i;