hc
2024-01-03 2f7c68cb55ecb7331f2381deb497c27155f32faf
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,6 +103,9 @@
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
105110 u8 port_num;
106111
....@@ -109,7 +114,7 @@
109114 int read_allocated;
110115 struct list_head read_queue;
111116 unsigned n_read;
112
- struct tasklet_struct push;
117
+ struct delayed_work push;
113118
114119 struct list_head write_pool;
115120 int write_started;
....@@ -118,6 +123,8 @@
118123 wait_queue_head_t drain_wait; /* wait while writes drain */
119124 bool write_busy;
120125 wait_queue_head_t close_wait;
126
+ bool suspended; /* port suspended */
127
+ bool start_delayed; /* delay start when suspended */
121128
122129 /* REVISIT this state ... */
123130 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
....@@ -254,9 +261,7 @@
254261 list_del(&req->list);
255262 req->zero = kfifo_is_empty(&port->port_write_buf);
256263
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));
264
+ pr_vdebug("ttyGS%d: tx len=%d, %3ph ...\n", port->port_num, len, req->buf);
260265
261266 /* Drop lock while we call out of driver; completions
262267 * could be issued while we do so. Disconnection may
....@@ -342,7 +347,7 @@
342347 }
343348
344349 /*
345
- * 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
346351 * layer until it refuses to take any more data (or is throttled back).
347352 * Then it issues reads for any further data.
348353 *
....@@ -351,9 +356,10 @@
351356 * So QUEUE_SIZE packets plus however many the FIFO holds (usually two)
352357 * can be buffered before the TTY layer's buffers (currently 64 KB).
353358 */
354
-static void gs_rx_push(unsigned long _port)
359
+static void gs_rx_push(struct work_struct *work)
355360 {
356
- 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);
357363 struct tty_struct *tty;
358364 struct list_head *queue = &port->read_queue;
359365 bool disconnect = false;
....@@ -381,7 +387,7 @@
381387 /* presumably a transient fault */
382388 pr_warn("ttyGS%d: unexpected RX status %d\n",
383389 port->port_num, req->status);
384
- /* FALLTHROUGH */
390
+ fallthrough;
385391 case 0:
386392 /* normal completion */
387393 break;
....@@ -428,21 +434,13 @@
428434
429435 /* We want our data queue to become empty ASAP, keeping data
430436 * 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...
437
+ * this time around, RX may be starved, so wait until next jiffy.
433438 *
434
- * REVISIT we should probably add a timer to keep the tasklet
435
- * 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.
436441 */
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
- }
442
+ if (!list_empty(queue) && !tty_throttled(tty))
443
+ schedule_delayed_work(&port->push, 1);
446444
447445 /* If we're still connected, refill the USB RX queue. */
448446 if (!disconnect && port->port_usb)
....@@ -458,7 +456,7 @@
458456 /* Queue all received data until the tty layer is ready for it. */
459457 spin_lock(&port->port_lock);
460458 list_add_tail(&req->list, &port->read_queue);
461
- tasklet_schedule(&port->push);
459
+ schedule_delayed_work(&port->push, 0);
462460 spin_unlock(&port->port_lock);
463461 }
464462
....@@ -475,7 +473,7 @@
475473 /* presumably a transient fault */
476474 pr_warn("%s: unexpected %s status %d\n",
477475 __func__, ep->name, req->status);
478
- /* FALL THROUGH */
476
+ fallthrough;
479477 case 0:
480478 /* normal completion */
481479 gs_start_tx(port);
....@@ -530,7 +528,7 @@
530528
531529 /**
532530 * gs_start_io - start USB I/O streams
533
- * @dev: encapsulates endpoints to use
531
+ * @port: port to use
534532 * Context: holding port_lock; port_tty and port_usb are non-null
535533 *
536534 * We only start I/O when something is connected to both sides of
....@@ -635,13 +633,19 @@
635633
636634 /* if connected, start the I/O stream */
637635 if (port->port_usb) {
638
- 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;
639639
640
- pr_debug("gs_open: start ttyGS%d\n", port->port_num);
641
- gs_start_io(port);
640
+ pr_debug("gs_open: start ttyGS%d\n", port->port_num);
641
+ gs_start_io(port);
642642
643
- if (gser->connect)
644
- 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
+ }
645649 }
646650
647651 pr_debug("gs_open: ttyGS%d (%p,%p)\n", port->port_num, tty, file);
....@@ -685,7 +689,7 @@
685689 pr_debug("gs_close: ttyGS%d (%p,%p) ...\n", port->port_num, tty, file);
686690
687691 gser = port->port_usb;
688
- if (gser && gser->disconnect)
692
+ if (gser && !port->suspended && gser->disconnect)
689693 gser->disconnect(gser);
690694
691695 /* wait for circular write buffer to drain, disconnect, or at
....@@ -706,13 +710,14 @@
706710
707711 /* Iff we're disconnected, there can be no I/O in flight so it's
708712 * 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.
713
+ * let the push async work fire again until we're re-opened.
710714 */
711715 if (gser == NULL)
712716 kfifo_free(&port->port_write_buf);
713717 else
714718 kfifo_reset(&port->port_write_buf);
715719
720
+ port->start_delayed = false;
716721 port->port.count = 0;
717722 port->port.tty = NULL;
718723
....@@ -817,8 +822,8 @@
817822 * rts/cts, or other handshaking with the host, but if the
818823 * read queue backs up enough we'll be NAKing OUT packets.
819824 */
820
- tasklet_schedule(&port->push);
821825 pr_vdebug("ttyGS%d: unthrottle\n", port->port_num);
826
+ schedule_delayed_work(&port->push, 0);
822827 }
823828 spin_unlock_irqrestore(&port->port_lock, flags);
824829 }
....@@ -859,50 +864,23 @@
859864
860865 #ifdef CONFIG_U_SERIAL_CONSOLE
861866
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)
867
+static void gs_console_complete_out(struct usb_ep *ep, struct usb_request *req)
866868 {
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;
869
+ struct gs_console *cons = req->context;
892870
893871 switch (req->status) {
894872 default:
895873 pr_warn("%s: unexpected %s status %d\n",
896874 __func__, ep->name, req->status);
897
- /* fall through */
875
+ fallthrough;
898876 case 0:
899877 /* 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);
878
+ spin_lock(&cons->lock);
879
+ req->length = 0;
880
+ schedule_work(&cons->work);
881
+ spin_unlock(&cons->lock);
905882 break;
883
+ case -ECONNRESET:
906884 case -ESHUTDOWN:
907885 /* disconnect */
908886 pr_vdebug("%s: %s shutdown\n", __func__, ep->name);
....@@ -910,190 +888,253 @@
910888 }
911889 }
912890
913
-static int gs_console_connect(int port_num)
891
+static void __gs_console_push(struct gs_console *cons)
914892 {
915
- struct gscons_info *info = &gscons_info;
916
- struct gs_port *port;
893
+ struct usb_request *req = cons->req;
917894 struct usb_ep *ep;
895
+ size_t size;
918896
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;
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;
923915 }
924916
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
- }
917
+ req->length = size;
933918
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;
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);
940923 }
941924
942
-static void gs_console_disconnect(struct usb_ep *ep)
925
+static void gs_console_work(struct work_struct *work)
943926 {
944
- struct gscons_info *info = &gscons_info;
945
- struct usb_request *req = info->console_req;
927
+ struct gs_console *cons = container_of(work, struct gs_console, work);
946928
947
- gs_request_free(req, ep);
948
- info->console_req = NULL;
949
-}
929
+ spin_lock_irq(&cons->lock);
950930
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;
931
+ __gs_console_push(cons);
958932
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;
933
+ spin_unlock_irq(&cons->lock);
1030934 }
1031935
1032936 static void gs_console_write(struct console *co,
1033937 const char *buf, unsigned count)
1034938 {
1035
- struct gscons_info *info = &gscons_info;
939
+ struct gs_console *cons = container_of(co, struct gs_console, console);
1036940 unsigned long flags;
941
+ size_t n;
1037942
1038
- spin_lock_irqsave(&info->con_lock, flags);
1039
- kfifo_in(&info->con_buf, buf, count);
1040
- spin_unlock_irqrestore(&info->con_lock, flags);
943
+ spin_lock_irqsave(&cons->lock, flags);
1041944
1042
- 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);
1043953 }
1044954
1045955 static struct tty_driver *gs_console_device(struct console *co, int *index)
1046956 {
1047
- struct tty_driver **p = (struct tty_driver **)co->data;
1048
-
1049
- if (!*p)
1050
- return NULL;
1051
-
1052957 *index = co->index;
1053
- return *p;
958
+ return gs_tty_driver;
1054959 }
1055960
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)
961
+static int gs_console_connect(struct gs_port *port)
1067962 {
1068
- 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;
1069988 }
1070989
1071
-static void gserial_console_exit(void)
990
+static void gs_console_disconnect(struct gs_port *port)
1072991 {
1073
- struct gscons_info *info = &gscons_info;
992
+ struct gs_console *cons = port->console;
993
+ struct usb_request *req;
994
+ struct usb_ep *ep;
1074995
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);
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);
10791012 }
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);
10801120
10811121 #else
10821122
1083
-static int gs_console_connect(int port_num)
1123
+static int gs_console_connect(struct gs_port *port)
10841124 {
10851125 return 0;
10861126 }
10871127
1088
-static void gs_console_disconnect(struct usb_ep *ep)
1128
+static void gs_console_disconnect(struct gs_port *port)
10891129 {
10901130 }
10911131
1092
-static void gserial_console_init(void)
1132
+static int gs_console_init(struct gs_port *port)
10931133 {
1134
+ return -ENOSYS;
10941135 }
10951136
1096
-static void gserial_console_exit(void)
1137
+static void gs_console_exit(struct gs_port *port)
10971138 {
10981139 }
10991140
....@@ -1122,7 +1163,7 @@
11221163 init_waitqueue_head(&port->drain_wait);
11231164 init_waitqueue_head(&port->close_wait);
11241165
1125
- tasklet_init(&port->push, gs_rx_push, (unsigned long) port);
1166
+ INIT_DELAYED_WORK(&port->push, gs_rx_push);
11261167
11271168 INIT_LIST_HEAD(&port->read_pool);
11281169 INIT_LIST_HEAD(&port->read_queue);
....@@ -1150,7 +1191,7 @@
11501191
11511192 static void gserial_free_port(struct gs_port *port)
11521193 {
1153
- tasklet_kill(&port->push);
1194
+ cancel_delayed_work_sync(&port->push);
11541195 /* wait for old opens to finish */
11551196 wait_event(port->close_wait, gs_closed(port));
11561197 WARN_ON(port->port_usb != NULL);
....@@ -1168,18 +1209,19 @@
11681209 return;
11691210 }
11701211 port = ports[port_num].port;
1212
+ gs_console_exit(port);
11711213 ports[port_num].port = NULL;
11721214 mutex_unlock(&ports[port_num].lock);
11731215
11741216 gserial_free_port(port);
11751217 tty_unregister_device(gs_tty_driver, port_num);
1176
- gserial_console_exit();
11771218 }
11781219 EXPORT_SYMBOL_GPL(gserial_free_line);
11791220
1180
-int gserial_alloc_line(unsigned char *line_num)
1221
+int gserial_alloc_line_no_console(unsigned char *line_num)
11811222 {
11821223 struct usb_cdc_line_coding coding;
1224
+ struct gs_port *port;
11831225 struct device *tty_dev;
11841226 int ret;
11851227 int port_num;
....@@ -1202,24 +1244,33 @@
12021244
12031245 /* ... and sysfs class devices, so mdev/udev make /dev/ttyGS* */
12041246
1205
- 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,
12061249 gs_tty_driver, port_num, NULL);
12071250 if (IS_ERR(tty_dev)) {
1208
- struct gs_port *port;
12091251 pr_err("%s: failed to register tty for port %d, err %ld\n",
12101252 __func__, port_num, PTR_ERR(tty_dev));
12111253
12121254 ret = PTR_ERR(tty_dev);
12131255 mutex_lock(&ports[port_num].lock);
1214
- port = ports[port_num].port;
12151256 ports[port_num].port = NULL;
12161257 mutex_unlock(&ports[port_num].lock);
12171258 gserial_free_port(port);
12181259 goto err;
12191260 }
12201261 *line_num = port_num;
1221
- gserial_console_init();
12221262 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
+
12231274 return ret;
12241275 }
12251276 EXPORT_SYMBOL_GPL(gserial_alloc_line);
....@@ -1300,7 +1351,7 @@
13001351 gser->disconnect(gser);
13011352 }
13021353
1303
- status = gs_console_connect(port_num);
1354
+ status = gs_console_connect(port);
13041355 spin_unlock_irqrestore(&port->port_lock, flags);
13051356
13061357 return status;
....@@ -1329,8 +1380,12 @@
13291380 if (!port)
13301381 return;
13311382
1383
+ spin_lock_irqsave(&serial_port_lock, flags);
1384
+
13321385 /* tell the TTY glue not to do I/O here any more */
1333
- spin_lock_irqsave(&port->port_lock, flags);
1386
+ spin_lock(&port->port_lock);
1387
+
1388
+ gs_console_disconnect(port);
13341389
13351390 /* REVISIT as above: how best to track this? */
13361391 port->port_line_coding = gser->port_line_coding;
....@@ -1342,7 +1397,9 @@
13421397 if (port->port.tty)
13431398 tty_hangup(port->port.tty);
13441399 }
1345
- 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);
13461403
13471404 /* disable endpoints, aborting down any active I/O */
13481405 usb_ep_disable(gser->out);
....@@ -1359,11 +1416,60 @@
13591416 port->read_allocated = port->read_started =
13601417 port->write_allocated = port->write_started = 0;
13611418
1362
- gs_console_disconnect(gser->in);
13631419 spin_unlock_irqrestore(&port->port_lock, flags);
13641420 }
13651421 EXPORT_SYMBOL_GPL(gserial_disconnect);
13661422
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
+
13671473 static int userial_init(void)
13681474 {
13691475 unsigned i;