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,6 +100,9 @@
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
105107 u8 port_num;
106108
....@@ -109,7 +111,7 @@
109111 int read_allocated;
110112 struct list_head read_queue;
111113 unsigned n_read;
112
- struct tasklet_struct push;
114
+ struct delayed_work push;
113115
114116 struct list_head write_pool;
115117 int write_started;
....@@ -118,6 +120,8 @@
118120 wait_queue_head_t drain_wait; /* wait while writes drain */
119121 bool write_busy;
120122 wait_queue_head_t close_wait;
123
+ bool suspended; /* port suspended */
124
+ bool start_delayed; /* delay start when suspended */
121125
122126 /* REVISIT this state ... */
123127 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
....@@ -254,9 +258,7 @@
254258 list_del(&req->list);
255259 req->zero = kfifo_is_empty(&port->port_write_buf);
256260
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);
260262
261263 /* Drop lock while we call out of driver; completions
262264 * could be issued while we do so. Disconnection may
....@@ -342,7 +344,7 @@
342344 }
343345
344346 /*
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
346348 * layer until it refuses to take any more data (or is throttled back).
347349 * Then it issues reads for any further data.
348350 *
....@@ -351,9 +353,10 @@
351353 * So QUEUE_SIZE packets plus however many the FIFO holds (usually two)
352354 * can be buffered before the TTY layer's buffers (currently 64 KB).
353355 */
354
-static void gs_rx_push(unsigned long _port)
356
+static void gs_rx_push(struct work_struct *work)
355357 {
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);
357360 struct tty_struct *tty;
358361 struct list_head *queue = &port->read_queue;
359362 bool disconnect = false;
....@@ -381,7 +384,7 @@
381384 /* presumably a transient fault */
382385 pr_warn("ttyGS%d: unexpected RX status %d\n",
383386 port->port_num, req->status);
384
- /* FALLTHROUGH */
387
+ fallthrough;
385388 case 0:
386389 /* normal completion */
387390 break;
....@@ -428,21 +431,13 @@
428431
429432 /* We want our data queue to become empty ASAP, keeping data
430433 * 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.
433435 *
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.
436438 */
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);
446441
447442 /* If we're still connected, refill the USB RX queue. */
448443 if (!disconnect && port->port_usb)
....@@ -458,7 +453,7 @@
458453 /* Queue all received data until the tty layer is ready for it. */
459454 spin_lock(&port->port_lock);
460455 list_add_tail(&req->list, &port->read_queue);
461
- tasklet_schedule(&port->push);
456
+ schedule_delayed_work(&port->push, 0);
462457 spin_unlock(&port->port_lock);
463458 }
464459
....@@ -475,7 +470,7 @@
475470 /* presumably a transient fault */
476471 pr_warn("%s: unexpected %s status %d\n",
477472 __func__, ep->name, req->status);
478
- /* FALL THROUGH */
473
+ fallthrough;
479474 case 0:
480475 /* normal completion */
481476 gs_start_tx(port);
....@@ -530,7 +525,7 @@
530525
531526 /**
532527 * gs_start_io - start USB I/O streams
533
- * @dev: encapsulates endpoints to use
528
+ * @port: port to use
534529 * Context: holding port_lock; port_tty and port_usb are non-null
535530 *
536531 * We only start I/O when something is connected to both sides of
....@@ -635,13 +630,19 @@
635630
636631 /* if connected, start the I/O stream */
637632 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;
639636
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);
642639
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
+ }
645646 }
646647
647648 pr_debug("gs_open: ttyGS%d (%p,%p)\n", port->port_num, tty, file);
....@@ -685,7 +686,7 @@
685686 pr_debug("gs_close: ttyGS%d (%p,%p) ...\n", port->port_num, tty, file);
686687
687688 gser = port->port_usb;
688
- if (gser && gser->disconnect)
689
+ if (gser && !port->suspended && gser->disconnect)
689690 gser->disconnect(gser);
690691
691692 /* wait for circular write buffer to drain, disconnect, or at
....@@ -706,13 +707,14 @@
706707
707708 /* Iff we're disconnected, there can be no I/O in flight so it's
708709 * 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.
710711 */
711712 if (gser == NULL)
712713 kfifo_free(&port->port_write_buf);
713714 else
714715 kfifo_reset(&port->port_write_buf);
715716
717
+ port->start_delayed = false;
716718 port->port.count = 0;
717719 port->port.tty = NULL;
718720
....@@ -817,8 +819,8 @@
817819 * rts/cts, or other handshaking with the host, but if the
818820 * read queue backs up enough we'll be NAKing OUT packets.
819821 */
820
- tasklet_schedule(&port->push);
821822 pr_vdebug("ttyGS%d: unthrottle\n", port->port_num);
823
+ schedule_delayed_work(&port->push, 0);
822824 }
823825 spin_unlock_irqrestore(&port->port_lock, flags);
824826 }
....@@ -859,50 +861,23 @@
859861
860862 #ifdef CONFIG_U_SERIAL_CONSOLE
861863
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)
866865 {
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;
892867
893868 switch (req->status) {
894869 default:
895870 pr_warn("%s: unexpected %s status %d\n",
896871 __func__, ep->name, req->status);
897
- /* fall through */
872
+ fallthrough;
898873 case 0:
899874 /* 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);
905879 break;
880
+ case -ECONNRESET:
906881 case -ESHUTDOWN:
907882 /* disconnect */
908883 pr_vdebug("%s: %s shutdown\n", __func__, ep->name);
....@@ -910,190 +885,250 @@
910885 }
911886 }
912887
913
-static int gs_console_connect(int port_num)
888
+static void __gs_console_push(struct gs_console *cons)
914889 {
915
- struct gscons_info *info = &gscons_info;
916
- struct gs_port *port;
890
+ struct usb_request *req = cons->req;
917891 struct usb_ep *ep;
892
+ size_t size;
918893
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;
923912 }
924913
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;
940917 }
941918
942
-static void gs_console_disconnect(struct usb_ep *ep)
919
+static void gs_console_work(struct work_struct *work)
943920 {
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);
946922
947
- gs_request_free(req, ep);
948
- info->console_req = NULL;
949
-}
923
+ spin_lock_irq(&cons->lock);
950924
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);
958926
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);
1030928 }
1031929
1032930 static void gs_console_write(struct console *co,
1033931 const char *buf, unsigned count)
1034932 {
1035
- struct gscons_info *info = &gscons_info;
933
+ struct gs_console *cons = container_of(co, struct gs_console, console);
1036934 unsigned long flags;
935
+ size_t n;
1037936
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);
1041938
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);
1043947 }
1044948
1045949 static struct tty_driver *gs_console_device(struct console *co, int *index)
1046950 {
1047
- struct tty_driver **p = (struct tty_driver **)co->data;
1048
-
1049
- if (!*p)
1050
- return NULL;
1051
-
1052951 *index = co->index;
1053
- return *p;
952
+ return gs_tty_driver;
1054953 }
1055954
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)
1067956 {
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;
1069982 }
1070983
1071
-static void gserial_console_exit(void)
984
+static void gs_console_disconnect(struct gs_port *port)
1072985 {
1073
- struct gscons_info *info = &gscons_info;
986
+ struct gs_console *cons = port->console;
987
+ struct usb_request *req;
988
+ struct usb_ep *ep;
1074989
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);
10791006 }
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);
10801114
10811115 #else
10821116
1083
-static int gs_console_connect(int port_num)
1117
+static int gs_console_connect(struct gs_port *port)
10841118 {
10851119 return 0;
10861120 }
10871121
1088
-static void gs_console_disconnect(struct usb_ep *ep)
1122
+static void gs_console_disconnect(struct gs_port *port)
10891123 {
10901124 }
10911125
1092
-static void gserial_console_init(void)
1126
+static int gs_console_init(struct gs_port *port)
10931127 {
1128
+ return -ENOSYS;
10941129 }
10951130
1096
-static void gserial_console_exit(void)
1131
+static void gs_console_exit(struct gs_port *port)
10971132 {
10981133 }
10991134
....@@ -1122,7 +1157,7 @@
11221157 init_waitqueue_head(&port->drain_wait);
11231158 init_waitqueue_head(&port->close_wait);
11241159
1125
- tasklet_init(&port->push, gs_rx_push, (unsigned long) port);
1160
+ INIT_DELAYED_WORK(&port->push, gs_rx_push);
11261161
11271162 INIT_LIST_HEAD(&port->read_pool);
11281163 INIT_LIST_HEAD(&port->read_queue);
....@@ -1150,7 +1185,7 @@
11501185
11511186 static void gserial_free_port(struct gs_port *port)
11521187 {
1153
- tasklet_kill(&port->push);
1188
+ cancel_delayed_work_sync(&port->push);
11541189 /* wait for old opens to finish */
11551190 wait_event(port->close_wait, gs_closed(port));
11561191 WARN_ON(port->port_usb != NULL);
....@@ -1168,18 +1203,19 @@
11681203 return;
11691204 }
11701205 port = ports[port_num].port;
1206
+ gs_console_exit(port);
11711207 ports[port_num].port = NULL;
11721208 mutex_unlock(&ports[port_num].lock);
11731209
11741210 gserial_free_port(port);
11751211 tty_unregister_device(gs_tty_driver, port_num);
1176
- gserial_console_exit();
11771212 }
11781213 EXPORT_SYMBOL_GPL(gserial_free_line);
11791214
1180
-int gserial_alloc_line(unsigned char *line_num)
1215
+int gserial_alloc_line_no_console(unsigned char *line_num)
11811216 {
11821217 struct usb_cdc_line_coding coding;
1218
+ struct gs_port *port;
11831219 struct device *tty_dev;
11841220 int ret;
11851221 int port_num;
....@@ -1202,24 +1238,33 @@
12021238
12031239 /* ... and sysfs class devices, so mdev/udev make /dev/ttyGS* */
12041240
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,
12061243 gs_tty_driver, port_num, NULL);
12071244 if (IS_ERR(tty_dev)) {
1208
- struct gs_port *port;
12091245 pr_err("%s: failed to register tty for port %d, err %ld\n",
12101246 __func__, port_num, PTR_ERR(tty_dev));
12111247
12121248 ret = PTR_ERR(tty_dev);
12131249 mutex_lock(&ports[port_num].lock);
1214
- port = ports[port_num].port;
12151250 ports[port_num].port = NULL;
12161251 mutex_unlock(&ports[port_num].lock);
12171252 gserial_free_port(port);
12181253 goto err;
12191254 }
12201255 *line_num = port_num;
1221
- gserial_console_init();
12221256 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
+
12231268 return ret;
12241269 }
12251270 EXPORT_SYMBOL_GPL(gserial_alloc_line);
....@@ -1300,7 +1345,7 @@
13001345 gser->disconnect(gser);
13011346 }
13021347
1303
- status = gs_console_connect(port_num);
1348
+ status = gs_console_connect(port);
13041349 spin_unlock_irqrestore(&port->port_lock, flags);
13051350
13061351 return status;
....@@ -1332,6 +1377,8 @@
13321377 /* tell the TTY glue not to do I/O here any more */
13331378 spin_lock_irqsave(&port->port_lock, flags);
13341379
1380
+ gs_console_disconnect(port);
1381
+
13351382 /* REVISIT as above: how best to track this? */
13361383 port->port_line_coding = gser->port_line_coding;
13371384
....@@ -1342,6 +1389,7 @@
13421389 if (port->port.tty)
13431390 tty_hangup(port->port.tty);
13441391 }
1392
+ port->suspended = false;
13451393 spin_unlock_irqrestore(&port->port_lock, flags);
13461394
13471395 /* disable endpoints, aborting down any active I/O */
....@@ -1359,11 +1407,42 @@
13591407 port->read_allocated = port->read_started =
13601408 port->write_allocated = port->write_started = 0;
13611409
1362
- gs_console_disconnect(gser->in);
13631410 spin_unlock_irqrestore(&port->port_lock, flags);
13641411 }
13651412 EXPORT_SYMBOL_GPL(gserial_disconnect);
13661413
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
+
13671446 static int userial_init(void)
13681447 {
13691448 unsigned i;