1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * u_serial.c - utilities for USB gadget "serial port"/TTY support
4 *
5 * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
6 * Copyright (C) 2008 David Brownell
7 * Copyright (C) 2008 by Nokia Corporation
8 *
9 * This code also borrows from usbserial.c, which is
10 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
11 * Copyright (C) 2000 Peter Berger (pberger@brimson.com)
12 * Copyright (C) 2000 Al Borchers (alborchers@steinerpoint.com)
13 */
14
15 /* #define VERBOSE_DEBUG */
16
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/device.h>
20 #include <linux/delay.h>
21 #include <linux/tty.h>
22 #include <linux/tty_flip.h>
23 #include <linux/slab.h>
24 #include <linux/string_choices.h>
25 #include <linux/export.h>
26 #include <linux/module.h>
27 #include <linux/console.h>
28 #include <linux/kstrtox.h>
29 #include <linux/kthread.h>
30 #include <linux/workqueue.h>
31 #include <linux/kfifo.h>
32 #include <linux/serial.h>
33
34 #include "u_serial.h"
35
36
37 /*
38 * This component encapsulates the TTY layer glue needed to provide basic
39 * "serial port" functionality through the USB gadget stack. Each such
40 * port is exposed through a /dev/ttyGS* node.
41 *
42 * After this module has been loaded, the individual TTY port can be requested
43 * (gserial_alloc_line()) and it will stay available until they are removed
44 * (gserial_free_line()). Each one may be connected to a USB function
45 * (gserial_connect), or disconnected (with gserial_disconnect) when the USB
46 * host issues a config change event. Data can only flow when the port is
47 * connected to the host.
48 *
49 * A given TTY port can be made available in multiple configurations.
50 * For example, each one might expose a ttyGS0 node which provides a
51 * login application. In one case that might use CDC ACM interface 0,
52 * while another configuration might use interface 3 for that. The
53 * work to handle that (including descriptor management) is not part
54 * of this component.
55 *
56 * Configurations may expose more than one TTY port. For example, if
57 * ttyGS0 provides login service, then ttyGS1 might provide dialer access
58 * for a telephone or fax link. And ttyGS2 might be something that just
59 * needs a simple byte stream interface for some messaging protocol that
60 * is managed in userspace ... OBEX, PTP, and MTP have been mentioned.
61 *
62 *
63 * gserial is the lifecycle interface, used by USB functions
64 * gs_port is the I/O nexus, used by the tty driver
65 * tty_struct links to the tty/filesystem framework
66 *
67 * gserial <---> gs_port ... links will be null when the USB link is
68 * inactive; managed by gserial_{connect,disconnect}(). each gserial
69 * instance can wrap its own USB control protocol.
70 * gserial->ioport == usb_ep->driver_data ... gs_port
71 * gs_port->port_usb ... gserial
72 *
73 * gs_port <---> tty_struct ... links will be null when the TTY file
74 * isn't opened; managed by gs_open()/gs_close()
75 * gserial->port_tty ... tty_struct
76 * tty_struct->driver_data ... gserial
77 */
78
79 /* RX and TX queues can buffer QUEUE_SIZE packets before they hit the
80 * next layer of buffering. For TX that's a circular buffer; for RX
81 * consider it a NOP. A third layer is provided by the TTY code.
82 */
83 #define QUEUE_SIZE 16
84 #define WRITE_BUF_SIZE 8192 /* TX only */
85 #define GS_CONSOLE_BUF_SIZE 8192
86
87 /* Prevents race conditions while accessing gser->ioport */
88 static DEFINE_SPINLOCK(serial_port_lock);
89
90 /* console info */
91 struct gs_console {
92 struct console console;
93 struct work_struct work;
94 spinlock_t lock;
95 struct usb_request *req;
96 struct kfifo buf;
97 size_t missed;
98 };
99
100 /*
101 * The port structure holds info for each port, one for each minor number
102 * (and thus for each /dev/ node).
103 */
104 struct gs_port {
105 struct tty_port port;
106 spinlock_t port_lock; /* guard port_* access */
107
108 struct gserial *port_usb;
109 #ifdef CONFIG_U_SERIAL_CONSOLE
110 struct gs_console *console;
111 #endif
112
113 u8 port_num;
114
115 struct list_head read_pool;
116 int read_started;
117 int read_allocated;
118 struct list_head read_queue;
119 unsigned n_read;
120 struct delayed_work push;
121
122 struct list_head write_pool;
123 int write_started;
124 int write_allocated;
125 struct kfifo port_write_buf;
126 wait_queue_head_t drain_wait; /* wait while writes drain */
127 bool write_busy;
128 wait_queue_head_t close_wait;
129 bool suspended; /* port suspended */
130 bool start_delayed; /* delay start when suspended */
131 struct async_icount icount;
132
133 /* REVISIT this state ... */
134 struct usb_cdc_line_coding port_line_coding; /* 8-N-1 etc */
135 };
136
137 static struct portmaster {
138 struct mutex lock; /* protect open/close */
139 struct gs_port *port;
140 } ports[MAX_U_SERIAL_PORTS];
141
142 #define GS_CLOSE_TIMEOUT 15 /* seconds */
143
144
145
146 #ifdef VERBOSE_DEBUG
147 #ifndef pr_vdebug
148 #define pr_vdebug(fmt, arg...) \
149 pr_debug(fmt, ##arg)
150 #endif /* pr_vdebug */
151 #else
152 #ifndef pr_vdebug
153 #define pr_vdebug(fmt, arg...) \
154 ({ if (0) pr_debug(fmt, ##arg); })
155 #endif /* pr_vdebug */
156 #endif
157
158 /*-------------------------------------------------------------------------*/
159
160 /* I/O glue between TTY (upper) and USB function (lower) driver layers */
161
162 /*
163 * gs_alloc_req
164 *
165 * Allocate a usb_request and its buffer. Returns a pointer to the
166 * usb_request or NULL if there is an error.
167 */
168 struct usb_request *
gs_alloc_req(struct usb_ep * ep,unsigned len,gfp_t kmalloc_flags)169 gs_alloc_req(struct usb_ep *ep, unsigned len, gfp_t kmalloc_flags)
170 {
171 struct usb_request *req;
172
173 req = usb_ep_alloc_request(ep, kmalloc_flags);
174
175 if (req != NULL) {
176 req->length = len;
177 req->buf = kmalloc(len, kmalloc_flags);
178 if (req->buf == NULL) {
179 usb_ep_free_request(ep, req);
180 return NULL;
181 }
182 }
183
184 return req;
185 }
186 EXPORT_SYMBOL_GPL(gs_alloc_req);
187
188 /*
189 * gs_free_req
190 *
191 * Free a usb_request and its buffer.
192 */
gs_free_req(struct usb_ep * ep,struct usb_request * req)193 void gs_free_req(struct usb_ep *ep, struct usb_request *req)
194 {
195 kfree(req->buf);
196 usb_ep_free_request(ep, req);
197 }
198 EXPORT_SYMBOL_GPL(gs_free_req);
199
200 /*
201 * gs_send_packet
202 *
203 * If there is data to send, a packet is built in the given
204 * buffer and the size is returned. If there is no data to
205 * send, 0 is returned.
206 *
207 * Called with port_lock held.
208 */
209 static unsigned
gs_send_packet(struct gs_port * port,char * packet,unsigned size)210 gs_send_packet(struct gs_port *port, char *packet, unsigned size)
211 {
212 unsigned len;
213
214 len = kfifo_len(&port->port_write_buf);
215 if (len < size)
216 size = len;
217 if (size != 0)
218 size = kfifo_out(&port->port_write_buf, packet, size);
219 return size;
220 }
221
222 /*
223 * gs_start_tx
224 *
225 * This function finds available write requests, calls
226 * gs_send_packet to fill these packets with data, and
227 * continues until either there are no more write requests
228 * available or no more data to send. This function is
229 * run whenever data arrives or write requests are available.
230 *
231 * Context: caller owns port_lock; port_usb is non-null.
232 */
gs_start_tx(struct gs_port * port)233 static int gs_start_tx(struct gs_port *port)
234 /*
235 __releases(&port->port_lock)
236 __acquires(&port->port_lock)
237 */
238 {
239 struct list_head *pool = &port->write_pool;
240 struct usb_ep *in;
241 int status = 0;
242 bool do_tty_wake = false;
243
244 if (!port->port_usb)
245 return status;
246
247 in = port->port_usb->in;
248
249 while (!port->write_busy && !list_empty(pool)) {
250 struct usb_request *req;
251 int len;
252
253 if (port->write_started >= QUEUE_SIZE)
254 break;
255
256 req = list_entry(pool->next, struct usb_request, list);
257 len = gs_send_packet(port, req->buf, in->maxpacket);
258 if (len == 0) {
259 wake_up_interruptible(&port->drain_wait);
260 break;
261 }
262 do_tty_wake = true;
263 port->icount.tx += len;
264
265 req->length = len;
266 list_del(&req->list);
267 req->zero = kfifo_is_empty(&port->port_write_buf);
268
269 pr_vdebug("ttyGS%d: tx len=%d, %3ph ...\n", port->port_num, len, req->buf);
270
271 /* Drop lock while we call out of driver; completions
272 * could be issued while we do so. Disconnection may
273 * happen too; maybe immediately before we queue this!
274 *
275 * NOTE that we may keep sending data for a while after
276 * the TTY closed (dev->ioport->port_tty is NULL).
277 */
278 port->write_busy = true;
279 spin_unlock(&port->port_lock);
280 status = usb_ep_queue(in, req, GFP_ATOMIC);
281 spin_lock(&port->port_lock);
282 port->write_busy = false;
283
284 if (status) {
285 pr_debug("%s: %s %s err %d\n",
286 __func__, "queue", in->name, status);
287 list_add(&req->list, pool);
288 break;
289 }
290
291 port->write_started++;
292
293 /* abort immediately after disconnect */
294 if (!port->port_usb)
295 break;
296 }
297
298 if (do_tty_wake && port->port.tty)
299 tty_wakeup(port->port.tty);
300 return status;
301 }
302
303 /*
304 * Context: caller owns port_lock, and port_usb is set
305 */
gs_start_rx(struct gs_port * port)306 static unsigned gs_start_rx(struct gs_port *port)
307 /*
308 __releases(&port->port_lock)
309 __acquires(&port->port_lock)
310 */
311 {
312 struct list_head *pool = &port->read_pool;
313 struct usb_ep *out = port->port_usb->out;
314
315 while (!list_empty(pool)) {
316 struct usb_request *req;
317 int status;
318 struct tty_struct *tty;
319
320 /* no more rx if closed */
321 tty = port->port.tty;
322 if (!tty)
323 break;
324
325 if (port->read_started >= QUEUE_SIZE)
326 break;
327
328 req = list_entry(pool->next, struct usb_request, list);
329 list_del(&req->list);
330 req->length = out->maxpacket;
331
332 /* drop lock while we call out; the controller driver
333 * may need to call us back (e.g. for disconnect)
334 */
335 spin_unlock(&port->port_lock);
336 status = usb_ep_queue(out, req, GFP_ATOMIC);
337 spin_lock(&port->port_lock);
338
339 if (status) {
340 pr_debug("%s: %s %s err %d\n",
341 __func__, "queue", out->name, status);
342 list_add(&req->list, pool);
343 break;
344 }
345 port->read_started++;
346
347 /* abort immediately after disconnect */
348 if (!port->port_usb)
349 break;
350 }
351 return port->read_started;
352 }
353
354 /*
355 * RX work takes data out of the RX queue and hands it up to the TTY
356 * layer until it refuses to take any more data (or is throttled back).
357 * Then it issues reads for any further data.
358 *
359 * If the RX queue becomes full enough that no usb_request is queued,
360 * the OUT endpoint may begin NAKing as soon as its FIFO fills up.
361 * So QUEUE_SIZE packets plus however many the FIFO holds (usually two)
362 * can be buffered before the TTY layer's buffers (currently 64 KB).
363 */
gs_rx_push(struct work_struct * work)364 static void gs_rx_push(struct work_struct *work)
365 {
366 struct delayed_work *w = to_delayed_work(work);
367 struct gs_port *port = container_of(w, struct gs_port, push);
368 struct tty_struct *tty;
369 struct list_head *queue = &port->read_queue;
370 bool disconnect = false;
371 bool do_push = false;
372
373 /* hand any queued data to the tty */
374 spin_lock_irq(&port->port_lock);
375 tty = port->port.tty;
376 while (!list_empty(queue)) {
377 struct usb_request *req;
378
379 req = list_first_entry(queue, struct usb_request, list);
380
381 /* leave data queued if tty was rx throttled */
382 if (tty && tty_throttled(tty))
383 break;
384
385 switch (req->status) {
386 case -ESHUTDOWN:
387 disconnect = true;
388 pr_vdebug("ttyGS%d: shutdown\n", port->port_num);
389 break;
390
391 default:
392 /* presumably a transient fault */
393 pr_warn("ttyGS%d: unexpected RX status %d\n",
394 port->port_num, req->status);
395 fallthrough;
396 case 0:
397 /* normal completion */
398 break;
399 }
400
401 /* push data to (open) tty */
402 if (req->actual && tty) {
403 char *packet = req->buf;
404 unsigned size = req->actual;
405 unsigned n;
406 int count;
407
408 /* we may have pushed part of this packet already... */
409 n = port->n_read;
410 if (n) {
411 packet += n;
412 size -= n;
413 }
414
415 port->icount.rx += size;
416 count = tty_insert_flip_string(&port->port, packet,
417 size);
418 if (count)
419 do_push = true;
420 if (count != size) {
421 /* stop pushing; TTY layer can't handle more */
422 port->n_read += count;
423 pr_vdebug("ttyGS%d: rx block %d/%d\n",
424 port->port_num, count, req->actual);
425 break;
426 }
427 port->n_read = 0;
428 }
429
430 list_move(&req->list, &port->read_pool);
431 port->read_started--;
432 }
433
434 /* Push from tty to ldisc; this is handled by a workqueue,
435 * so we won't get callbacks and can hold port_lock
436 */
437 if (do_push)
438 tty_flip_buffer_push(&port->port);
439
440
441 /* We want our data queue to become empty ASAP, keeping data
442 * in the tty and ldisc (not here). If we couldn't push any
443 * this time around, RX may be starved, so wait until next jiffy.
444 *
445 * We may leave non-empty queue only when there is a tty, and
446 * either it is throttled or there is no more room in flip buffer.
447 */
448 if (!list_empty(queue) && !tty_throttled(tty))
449 schedule_delayed_work(&port->push, 1);
450
451 /* If we're still connected, refill the USB RX queue. */
452 if (!disconnect && port->port_usb)
453 gs_start_rx(port);
454
455 spin_unlock_irq(&port->port_lock);
456 }
457
gs_read_complete(struct usb_ep * ep,struct usb_request * req)458 static void gs_read_complete(struct usb_ep *ep, struct usb_request *req)
459 {
460 struct gs_port *port = ep->driver_data;
461
462 /* Queue all received data until the tty layer is ready for it. */
463 spin_lock(&port->port_lock);
464 list_add_tail(&req->list, &port->read_queue);
465 schedule_delayed_work(&port->push, 0);
466 spin_unlock(&port->port_lock);
467 }
468
gs_write_complete(struct usb_ep * ep,struct usb_request * req)469 static void gs_write_complete(struct usb_ep *ep, struct usb_request *req)
470 {
471 struct gs_port *port = ep->driver_data;
472
473 spin_lock(&port->port_lock);
474 list_add(&req->list, &port->write_pool);
475 port->write_started--;
476
477 switch (req->status) {
478 default:
479 /* presumably a transient fault */
480 pr_warn("%s: unexpected %s status %d\n",
481 __func__, ep->name, req->status);
482 fallthrough;
483 case 0:
484 /* normal completion */
485 gs_start_tx(port);
486 break;
487
488 case -ESHUTDOWN:
489 /* disconnect */
490 pr_vdebug("%s: %s shutdown\n", __func__, ep->name);
491 break;
492 }
493
494 spin_unlock(&port->port_lock);
495 }
496
gs_free_requests(struct usb_ep * ep,struct list_head * head,int * allocated)497 static void gs_free_requests(struct usb_ep *ep, struct list_head *head,
498 int *allocated)
499 {
500 struct usb_request *req;
501
502 while (!list_empty(head)) {
503 req = list_entry(head->next, struct usb_request, list);
504 list_del(&req->list);
505 gs_free_req(ep, req);
506 if (allocated)
507 (*allocated)--;
508 }
509 }
510
gs_alloc_requests(struct usb_ep * ep,struct list_head * head,void (* fn)(struct usb_ep *,struct usb_request *),int * allocated)511 static int gs_alloc_requests(struct usb_ep *ep, struct list_head *head,
512 void (*fn)(struct usb_ep *, struct usb_request *),
513 int *allocated)
514 {
515 int i;
516 struct usb_request *req;
517 int n = allocated ? QUEUE_SIZE - *allocated : QUEUE_SIZE;
518
519 /* Pre-allocate up to QUEUE_SIZE transfers, but if we can't
520 * do quite that many this time, don't fail ... we just won't
521 * be as speedy as we might otherwise be.
522 */
523 for (i = 0; i < n; i++) {
524 req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC);
525 if (!req)
526 return list_empty(head) ? -ENOMEM : 0;
527 req->complete = fn;
528 list_add_tail(&req->list, head);
529 if (allocated)
530 (*allocated)++;
531 }
532 return 0;
533 }
534
535 /**
536 * gs_start_io - start USB I/O streams
537 * @port: port to use
538 * Context: holding port_lock; port_tty and port_usb are non-null
539 *
540 * We only start I/O when something is connected to both sides of
541 * this port. If nothing is listening on the host side, we may
542 * be pointlessly filling up our TX buffers and FIFO.
543 */
gs_start_io(struct gs_port * port)544 static int gs_start_io(struct gs_port *port)
545 {
546 struct list_head *head = &port->read_pool;
547 struct usb_ep *ep;
548 int status;
549 unsigned started;
550
551 if (!port->port_usb || !port->port.tty)
552 return -EIO;
553
554 /* Allocate RX and TX I/O buffers. We can't easily do this much
555 * earlier (with GFP_KERNEL) because the requests are coupled to
556 * endpoints, as are the packet sizes we'll be using. Different
557 * configurations may use different endpoints with a given port;
558 * and high speed vs full speed changes packet sizes too.
559 */
560 ep = port->port_usb->out;
561 status = gs_alloc_requests(ep, head, gs_read_complete,
562 &port->read_allocated);
563 if (status)
564 return status;
565
566 status = gs_alloc_requests(port->port_usb->in, &port->write_pool,
567 gs_write_complete, &port->write_allocated);
568 if (status) {
569 gs_free_requests(ep, head, &port->read_allocated);
570 return status;
571 }
572
573 /* queue read requests */
574 port->n_read = 0;
575 started = gs_start_rx(port);
576
577 if (started) {
578 gs_start_tx(port);
579 /* Unblock any pending writes into our circular buffer, in case
580 * we didn't in gs_start_tx() */
581 tty_wakeup(port->port.tty);
582 } else {
583 /* Free reqs only if we are still connected */
584 if (port->port_usb) {
585 gs_free_requests(ep, head, &port->read_allocated);
586 gs_free_requests(port->port_usb->in, &port->write_pool,
587 &port->write_allocated);
588 }
589 status = -EIO;
590 }
591
592 return status;
593 }
594
595 /*-------------------------------------------------------------------------*/
596
597 /* TTY Driver */
598
599 /*
600 * gs_open sets up the link between a gs_port and its associated TTY.
601 * That link is broken *only* by TTY close(), and all driver methods
602 * know that.
603 */
gs_open(struct tty_struct * tty,struct file * file)604 static int gs_open(struct tty_struct *tty, struct file *file)
605 {
606 int port_num = tty->index;
607 struct gs_port *port;
608 int status = 0;
609
610 mutex_lock(&ports[port_num].lock);
611 port = ports[port_num].port;
612 if (!port) {
613 status = -ENODEV;
614 goto out;
615 }
616
617 spin_lock_irq(&port->port_lock);
618
619 /* allocate circular buffer on first open */
620 if (!kfifo_initialized(&port->port_write_buf)) {
621
622 spin_unlock_irq(&port->port_lock);
623
624 /*
625 * portmaster's mutex still protects from simultaneous open(),
626 * and close() can't happen, yet.
627 */
628
629 status = kfifo_alloc(&port->port_write_buf,
630 WRITE_BUF_SIZE, GFP_KERNEL);
631 if (status) {
632 pr_debug("gs_open: ttyGS%d (%p,%p) no buffer\n",
633 port_num, tty, file);
634 goto out;
635 }
636
637 spin_lock_irq(&port->port_lock);
638 }
639
640 /* already open? Great. */
641 if (port->port.count++)
642 goto exit_unlock_port;
643
644 tty->driver_data = port;
645 port->port.tty = tty;
646
647 /* if connected, start the I/O stream */
648 if (port->port_usb) {
649 /* if port is suspended, wait resume to start I/0 stream */
650 if (!port->suspended) {
651 struct gserial *gser = port->port_usb;
652
653 pr_debug("gs_open: start ttyGS%d\n", port->port_num);
654 gs_start_io(port);
655
656 if (gser->connect)
657 gser->connect(gser);
658 } else {
659 pr_debug("delay start of ttyGS%d\n", port->port_num);
660 port->start_delayed = true;
661 }
662 }
663
664 pr_debug("gs_open: ttyGS%d (%p,%p)\n", port->port_num, tty, file);
665
666 exit_unlock_port:
667 spin_unlock_irq(&port->port_lock);
668 out:
669 mutex_unlock(&ports[port_num].lock);
670 return status;
671 }
672
gs_close_flush_done(struct gs_port * p)673 static int gs_close_flush_done(struct gs_port *p)
674 {
675 int cond;
676
677 /* return true on disconnect or empty buffer or if raced with open() */
678 spin_lock_irq(&p->port_lock);
679 cond = p->port_usb == NULL || !kfifo_len(&p->port_write_buf) ||
680 p->port.count > 1;
681 spin_unlock_irq(&p->port_lock);
682
683 return cond;
684 }
685
gs_close(struct tty_struct * tty,struct file * file)686 static void gs_close(struct tty_struct *tty, struct file *file)
687 {
688 struct gs_port *port = tty->driver_data;
689 struct gserial *gser;
690
691 spin_lock_irq(&port->port_lock);
692
693 if (port->port.count != 1) {
694 raced_with_open:
695 if (port->port.count == 0)
696 WARN_ON(1);
697 else
698 --port->port.count;
699 goto exit;
700 }
701
702 pr_debug("gs_close: ttyGS%d (%p,%p) ...\n", port->port_num, tty, file);
703
704 gser = port->port_usb;
705 if (gser && !port->suspended && gser->disconnect)
706 gser->disconnect(gser);
707
708 /* wait for circular write buffer to drain, disconnect, or at
709 * most GS_CLOSE_TIMEOUT seconds; then discard the rest
710 */
711 if (kfifo_len(&port->port_write_buf) > 0 && gser) {
712 spin_unlock_irq(&port->port_lock);
713 wait_event_interruptible_timeout(port->drain_wait,
714 gs_close_flush_done(port),
715 GS_CLOSE_TIMEOUT * HZ);
716 spin_lock_irq(&port->port_lock);
717
718 if (port->port.count != 1)
719 goto raced_with_open;
720
721 gser = port->port_usb;
722 }
723
724 /* Iff we're disconnected, there can be no I/O in flight so it's
725 * ok to free the circular buffer; else just scrub it. And don't
726 * let the push async work fire again until we're re-opened.
727 */
728 if (gser == NULL)
729 kfifo_free(&port->port_write_buf);
730 else
731 kfifo_reset(&port->port_write_buf);
732
733 port->start_delayed = false;
734 port->port.count = 0;
735 port->port.tty = NULL;
736
737 pr_debug("gs_close: ttyGS%d (%p,%p) done!\n",
738 port->port_num, tty, file);
739
740 wake_up(&port->close_wait);
741 exit:
742 spin_unlock_irq(&port->port_lock);
743 }
744
gs_write(struct tty_struct * tty,const u8 * buf,size_t count)745 static ssize_t gs_write(struct tty_struct *tty, const u8 *buf, size_t count)
746 {
747 struct gs_port *port = tty->driver_data;
748 unsigned long flags;
749
750 pr_vdebug("gs_write: ttyGS%d (%p) writing %zu bytes\n",
751 port->port_num, tty, count);
752
753 spin_lock_irqsave(&port->port_lock, flags);
754 if (count)
755 count = kfifo_in(&port->port_write_buf, buf, count);
756 /* treat count == 0 as flush_chars() */
757 if (port->port_usb)
758 gs_start_tx(port);
759 spin_unlock_irqrestore(&port->port_lock, flags);
760
761 return count;
762 }
763
gs_put_char(struct tty_struct * tty,u8 ch)764 static int gs_put_char(struct tty_struct *tty, u8 ch)
765 {
766 struct gs_port *port = tty->driver_data;
767 unsigned long flags;
768 int status;
769
770 pr_vdebug("gs_put_char: (%d,%p) char=0x%x, called from %ps\n",
771 port->port_num, tty, ch, __builtin_return_address(0));
772
773 spin_lock_irqsave(&port->port_lock, flags);
774 status = kfifo_put(&port->port_write_buf, ch);
775 spin_unlock_irqrestore(&port->port_lock, flags);
776
777 return status;
778 }
779
gs_flush_chars(struct tty_struct * tty)780 static void gs_flush_chars(struct tty_struct *tty)
781 {
782 struct gs_port *port = tty->driver_data;
783 unsigned long flags;
784
785 pr_vdebug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
786
787 spin_lock_irqsave(&port->port_lock, flags);
788 if (port->port_usb)
789 gs_start_tx(port);
790 spin_unlock_irqrestore(&port->port_lock, flags);
791 }
792
gs_write_room(struct tty_struct * tty)793 static unsigned int gs_write_room(struct tty_struct *tty)
794 {
795 struct gs_port *port = tty->driver_data;
796 unsigned long flags;
797 unsigned int room = 0;
798
799 spin_lock_irqsave(&port->port_lock, flags);
800 if (port->port_usb)
801 room = kfifo_avail(&port->port_write_buf);
802 spin_unlock_irqrestore(&port->port_lock, flags);
803
804 pr_vdebug("gs_write_room: (%d,%p) room=%u\n",
805 port->port_num, tty, room);
806
807 return room;
808 }
809
gs_chars_in_buffer(struct tty_struct * tty)810 static unsigned int gs_chars_in_buffer(struct tty_struct *tty)
811 {
812 struct gs_port *port = tty->driver_data;
813 unsigned long flags;
814 unsigned int chars;
815
816 spin_lock_irqsave(&port->port_lock, flags);
817 chars = kfifo_len(&port->port_write_buf);
818 spin_unlock_irqrestore(&port->port_lock, flags);
819
820 pr_vdebug("gs_chars_in_buffer: (%d,%p) chars=%u\n",
821 port->port_num, tty, chars);
822
823 return chars;
824 }
825
826 /* undo side effects of setting TTY_THROTTLED */
gs_unthrottle(struct tty_struct * tty)827 static void gs_unthrottle(struct tty_struct *tty)
828 {
829 struct gs_port *port = tty->driver_data;
830 unsigned long flags;
831
832 spin_lock_irqsave(&port->port_lock, flags);
833 if (port->port_usb) {
834 /* Kickstart read queue processing. We don't do xon/xoff,
835 * rts/cts, or other handshaking with the host, but if the
836 * read queue backs up enough we'll be NAKing OUT packets.
837 */
838 pr_vdebug("ttyGS%d: unthrottle\n", port->port_num);
839 schedule_delayed_work(&port->push, 0);
840 }
841 spin_unlock_irqrestore(&port->port_lock, flags);
842 }
843
gs_break_ctl(struct tty_struct * tty,int duration)844 static int gs_break_ctl(struct tty_struct *tty, int duration)
845 {
846 struct gs_port *port = tty->driver_data;
847 int status = 0;
848 struct gserial *gser;
849
850 pr_vdebug("gs_break_ctl: ttyGS%d, send break (%d) \n",
851 port->port_num, duration);
852
853 spin_lock_irq(&port->port_lock);
854 gser = port->port_usb;
855 if (gser && gser->send_break)
856 status = gser->send_break(gser, duration);
857 spin_unlock_irq(&port->port_lock);
858
859 return status;
860 }
861
gs_get_icount(struct tty_struct * tty,struct serial_icounter_struct * icount)862 static int gs_get_icount(struct tty_struct *tty,
863 struct serial_icounter_struct *icount)
864 {
865 struct gs_port *port = tty->driver_data;
866 struct async_icount cnow;
867 unsigned long flags;
868
869 spin_lock_irqsave(&port->port_lock, flags);
870 cnow = port->icount;
871 spin_unlock_irqrestore(&port->port_lock, flags);
872
873 icount->rx = cnow.rx;
874 icount->tx = cnow.tx;
875
876 return 0;
877 }
878
879 static const struct tty_operations gs_tty_ops = {
880 .open = gs_open,
881 .close = gs_close,
882 .write = gs_write,
883 .put_char = gs_put_char,
884 .flush_chars = gs_flush_chars,
885 .write_room = gs_write_room,
886 .chars_in_buffer = gs_chars_in_buffer,
887 .unthrottle = gs_unthrottle,
888 .break_ctl = gs_break_ctl,
889 .get_icount = gs_get_icount,
890 };
891
892 /*-------------------------------------------------------------------------*/
893
894 static struct tty_driver *gs_tty_driver;
895
896 #ifdef CONFIG_U_SERIAL_CONSOLE
897
gs_console_complete_out(struct usb_ep * ep,struct usb_request * req)898 static void gs_console_complete_out(struct usb_ep *ep, struct usb_request *req)
899 {
900 struct gs_console *cons = req->context;
901
902 switch (req->status) {
903 default:
904 pr_warn("%s: unexpected %s status %d\n",
905 __func__, ep->name, req->status);
906 fallthrough;
907 case 0:
908 /* normal completion */
909 spin_lock(&cons->lock);
910 req->length = 0;
911 schedule_work(&cons->work);
912 spin_unlock(&cons->lock);
913 break;
914 case -ECONNRESET:
915 case -ESHUTDOWN:
916 /* disconnect */
917 pr_vdebug("%s: %s shutdown\n", __func__, ep->name);
918 break;
919 }
920 }
921
__gs_console_push(struct gs_console * cons)922 static void __gs_console_push(struct gs_console *cons)
923 {
924 struct usb_request *req = cons->req;
925 struct usb_ep *ep;
926 size_t size;
927
928 if (!req)
929 return; /* disconnected */
930
931 if (req->length)
932 return; /* busy */
933
934 ep = cons->console.data;
935 size = kfifo_out(&cons->buf, req->buf, ep->maxpacket);
936 if (!size)
937 return;
938
939 if (cons->missed && ep->maxpacket >= 64) {
940 char buf[64];
941 size_t len;
942
943 len = sprintf(buf, "\n[missed %zu bytes]\n", cons->missed);
944 kfifo_in(&cons->buf, buf, len);
945 cons->missed = 0;
946 }
947
948 req->length = size;
949
950 spin_unlock_irq(&cons->lock);
951 if (usb_ep_queue(ep, req, GFP_ATOMIC))
952 req->length = 0;
953 spin_lock_irq(&cons->lock);
954 }
955
gs_console_work(struct work_struct * work)956 static void gs_console_work(struct work_struct *work)
957 {
958 struct gs_console *cons = container_of(work, struct gs_console, work);
959
960 spin_lock_irq(&cons->lock);
961
962 __gs_console_push(cons);
963
964 spin_unlock_irq(&cons->lock);
965 }
966
gs_console_write(struct console * co,const char * buf,unsigned count)967 static void gs_console_write(struct console *co,
968 const char *buf, unsigned count)
969 {
970 struct gs_console *cons = container_of(co, struct gs_console, console);
971 unsigned long flags;
972 size_t n;
973
974 spin_lock_irqsave(&cons->lock, flags);
975
976 n = kfifo_in(&cons->buf, buf, count);
977 if (n < count)
978 cons->missed += count - n;
979
980 if (cons->req && !cons->req->length)
981 schedule_work(&cons->work);
982
983 spin_unlock_irqrestore(&cons->lock, flags);
984 }
985
gs_console_device(struct console * co,int * index)986 static struct tty_driver *gs_console_device(struct console *co, int *index)
987 {
988 *index = co->index;
989 return gs_tty_driver;
990 }
991
gs_console_connect(struct gs_port * port)992 static int gs_console_connect(struct gs_port *port)
993 {
994 struct gs_console *cons = port->console;
995 struct usb_request *req;
996 struct usb_ep *ep;
997
998 if (!cons)
999 return 0;
1000
1001 ep = port->port_usb->in;
1002 req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC);
1003 if (!req)
1004 return -ENOMEM;
1005 req->complete = gs_console_complete_out;
1006 req->context = cons;
1007 req->length = 0;
1008
1009 spin_lock(&cons->lock);
1010 cons->req = req;
1011 cons->console.data = ep;
1012 spin_unlock(&cons->lock);
1013
1014 pr_debug("ttyGS%d: console connected!\n", port->port_num);
1015
1016 schedule_work(&cons->work);
1017
1018 return 0;
1019 }
1020
gs_console_disconnect(struct gs_port * port)1021 static void gs_console_disconnect(struct gs_port *port)
1022 {
1023 struct gs_console *cons = port->console;
1024 struct usb_request *req;
1025 struct usb_ep *ep;
1026
1027 if (!cons)
1028 return;
1029
1030 spin_lock(&cons->lock);
1031
1032 req = cons->req;
1033 ep = cons->console.data;
1034 cons->req = NULL;
1035
1036 spin_unlock(&cons->lock);
1037
1038 if (!req)
1039 return;
1040
1041 usb_ep_dequeue(ep, req);
1042 gs_free_req(ep, req);
1043 }
1044
gs_console_init(struct gs_port * port)1045 static int gs_console_init(struct gs_port *port)
1046 {
1047 struct gs_console *cons;
1048 int err;
1049
1050 if (port->console)
1051 return 0;
1052
1053 cons = kzalloc(sizeof(*port->console), GFP_KERNEL);
1054 if (!cons)
1055 return -ENOMEM;
1056
1057 strcpy(cons->console.name, "ttyGS");
1058 cons->console.write = gs_console_write;
1059 cons->console.device = gs_console_device;
1060 cons->console.flags = CON_PRINTBUFFER;
1061 cons->console.index = port->port_num;
1062
1063 INIT_WORK(&cons->work, gs_console_work);
1064 spin_lock_init(&cons->lock);
1065
1066 err = kfifo_alloc(&cons->buf, GS_CONSOLE_BUF_SIZE, GFP_KERNEL);
1067 if (err) {
1068 pr_err("ttyGS%d: allocate console buffer failed\n", port->port_num);
1069 kfree(cons);
1070 return err;
1071 }
1072
1073 port->console = cons;
1074 register_console(&cons->console);
1075
1076 spin_lock_irq(&port->port_lock);
1077 if (port->port_usb)
1078 gs_console_connect(port);
1079 spin_unlock_irq(&port->port_lock);
1080
1081 return 0;
1082 }
1083
gs_console_exit(struct gs_port * port)1084 static void gs_console_exit(struct gs_port *port)
1085 {
1086 struct gs_console *cons = port->console;
1087
1088 if (!cons)
1089 return;
1090
1091 unregister_console(&cons->console);
1092
1093 spin_lock_irq(&port->port_lock);
1094 if (cons->req)
1095 gs_console_disconnect(port);
1096 spin_unlock_irq(&port->port_lock);
1097
1098 cancel_work_sync(&cons->work);
1099 kfifo_free(&cons->buf);
1100 kfree(cons);
1101 port->console = NULL;
1102 }
1103
gserial_set_console(unsigned char port_num,const char * page,size_t count)1104 ssize_t gserial_set_console(unsigned char port_num, const char *page, size_t count)
1105 {
1106 struct gs_port *port;
1107 bool enable;
1108 int ret;
1109
1110 ret = kstrtobool(page, &enable);
1111 if (ret)
1112 return ret;
1113
1114 mutex_lock(&ports[port_num].lock);
1115 port = ports[port_num].port;
1116
1117 if (WARN_ON(port == NULL)) {
1118 ret = -ENXIO;
1119 goto out;
1120 }
1121
1122 if (enable)
1123 ret = gs_console_init(port);
1124 else
1125 gs_console_exit(port);
1126 out:
1127 mutex_unlock(&ports[port_num].lock);
1128
1129 return ret < 0 ? ret : count;
1130 }
1131 EXPORT_SYMBOL_GPL(gserial_set_console);
1132
gserial_get_console(unsigned char port_num,char * page)1133 ssize_t gserial_get_console(unsigned char port_num, char *page)
1134 {
1135 struct gs_port *port;
1136 ssize_t ret;
1137
1138 mutex_lock(&ports[port_num].lock);
1139 port = ports[port_num].port;
1140
1141 if (WARN_ON(port == NULL))
1142 ret = -ENXIO;
1143 else
1144 ret = sprintf(page, "%u\n", !!port->console);
1145
1146 mutex_unlock(&ports[port_num].lock);
1147
1148 return ret;
1149 }
1150 EXPORT_SYMBOL_GPL(gserial_get_console);
1151
1152 #else
1153
gs_console_connect(struct gs_port * port)1154 static int gs_console_connect(struct gs_port *port)
1155 {
1156 return 0;
1157 }
1158
gs_console_disconnect(struct gs_port * port)1159 static void gs_console_disconnect(struct gs_port *port)
1160 {
1161 }
1162
gs_console_init(struct gs_port * port)1163 static int gs_console_init(struct gs_port *port)
1164 {
1165 return -ENOSYS;
1166 }
1167
gs_console_exit(struct gs_port * port)1168 static void gs_console_exit(struct gs_port *port)
1169 {
1170 }
1171
1172 #endif
1173
1174 static int
gs_port_alloc(unsigned port_num,struct usb_cdc_line_coding * coding)1175 gs_port_alloc(unsigned port_num, struct usb_cdc_line_coding *coding)
1176 {
1177 struct gs_port *port;
1178 int ret = 0;
1179
1180 mutex_lock(&ports[port_num].lock);
1181 if (ports[port_num].port) {
1182 ret = -EBUSY;
1183 goto out;
1184 }
1185
1186 port = kzalloc(sizeof(struct gs_port), GFP_KERNEL);
1187 if (port == NULL) {
1188 ret = -ENOMEM;
1189 goto out;
1190 }
1191
1192 tty_port_init(&port->port);
1193 spin_lock_init(&port->port_lock);
1194 init_waitqueue_head(&port->drain_wait);
1195 init_waitqueue_head(&port->close_wait);
1196
1197 INIT_DELAYED_WORK(&port->push, gs_rx_push);
1198
1199 INIT_LIST_HEAD(&port->read_pool);
1200 INIT_LIST_HEAD(&port->read_queue);
1201 INIT_LIST_HEAD(&port->write_pool);
1202
1203 port->port_num = port_num;
1204 port->port_line_coding = *coding;
1205
1206 ports[port_num].port = port;
1207 out:
1208 mutex_unlock(&ports[port_num].lock);
1209 return ret;
1210 }
1211
gs_closed(struct gs_port * port)1212 static int gs_closed(struct gs_port *port)
1213 {
1214 int cond;
1215
1216 spin_lock_irq(&port->port_lock);
1217 cond = port->port.count == 0;
1218 spin_unlock_irq(&port->port_lock);
1219
1220 return cond;
1221 }
1222
gserial_free_port(struct gs_port * port)1223 static void gserial_free_port(struct gs_port *port)
1224 {
1225 cancel_delayed_work_sync(&port->push);
1226 /* wait for old opens to finish */
1227 wait_event(port->close_wait, gs_closed(port));
1228 WARN_ON(port->port_usb != NULL);
1229 tty_port_destroy(&port->port);
1230 kfree(port);
1231 }
1232
gserial_free_line(unsigned char port_num)1233 void gserial_free_line(unsigned char port_num)
1234 {
1235 struct gs_port *port;
1236
1237 mutex_lock(&ports[port_num].lock);
1238 if (!ports[port_num].port) {
1239 mutex_unlock(&ports[port_num].lock);
1240 return;
1241 }
1242 port = ports[port_num].port;
1243 gs_console_exit(port);
1244 ports[port_num].port = NULL;
1245 mutex_unlock(&ports[port_num].lock);
1246
1247 gserial_free_port(port);
1248 tty_unregister_device(gs_tty_driver, port_num);
1249 }
1250 EXPORT_SYMBOL_GPL(gserial_free_line);
1251
gserial_alloc_line_no_console(unsigned char * line_num)1252 int gserial_alloc_line_no_console(unsigned char *line_num)
1253 {
1254 struct usb_cdc_line_coding coding;
1255 struct gs_port *port;
1256 struct device *tty_dev;
1257 int ret;
1258 int port_num;
1259
1260 coding.dwDTERate = cpu_to_le32(9600);
1261 coding.bCharFormat = 8;
1262 coding.bParityType = USB_CDC_NO_PARITY;
1263 coding.bDataBits = USB_CDC_1_STOP_BITS;
1264
1265 for (port_num = 0; port_num < MAX_U_SERIAL_PORTS; port_num++) {
1266 ret = gs_port_alloc(port_num, &coding);
1267 if (ret == -EBUSY)
1268 continue;
1269 if (ret)
1270 return ret;
1271 break;
1272 }
1273 if (ret)
1274 return ret;
1275
1276 /* ... and sysfs class devices, so mdev/udev make /dev/ttyGS* */
1277
1278 port = ports[port_num].port;
1279 tty_dev = tty_port_register_device(&port->port,
1280 gs_tty_driver, port_num, NULL);
1281 if (IS_ERR(tty_dev)) {
1282 pr_err("%s: failed to register tty for port %d, err %ld\n",
1283 __func__, port_num, PTR_ERR(tty_dev));
1284
1285 ret = PTR_ERR(tty_dev);
1286 mutex_lock(&ports[port_num].lock);
1287 ports[port_num].port = NULL;
1288 mutex_unlock(&ports[port_num].lock);
1289 gserial_free_port(port);
1290 goto err;
1291 }
1292 *line_num = port_num;
1293 err:
1294 return ret;
1295 }
1296 EXPORT_SYMBOL_GPL(gserial_alloc_line_no_console);
1297
gserial_alloc_line(unsigned char * line_num)1298 int gserial_alloc_line(unsigned char *line_num)
1299 {
1300 int ret = gserial_alloc_line_no_console(line_num);
1301
1302 if (!ret && !*line_num)
1303 gs_console_init(ports[*line_num].port);
1304
1305 return ret;
1306 }
1307 EXPORT_SYMBOL_GPL(gserial_alloc_line);
1308
1309 /**
1310 * gserial_connect - notify TTY I/O glue that USB link is active
1311 * @gser: the function, set up with endpoints and descriptors
1312 * @port_num: which port is active
1313 * Context: any (usually from irq)
1314 *
1315 * This is called activate endpoints and let the TTY layer know that
1316 * the connection is active ... not unlike "carrier detect". It won't
1317 * necessarily start I/O queues; unless the TTY is held open by any
1318 * task, there would be no point. However, the endpoints will be
1319 * activated so the USB host can perform I/O, subject to basic USB
1320 * hardware flow control.
1321 *
1322 * Caller needs to have set up the endpoints and USB function in @dev
1323 * before calling this, as well as the appropriate (speed-specific)
1324 * endpoint descriptors, and also have allocate @port_num by calling
1325 * @gserial_alloc_line().
1326 *
1327 * Returns negative errno or zero.
1328 * On success, ep->driver_data will be overwritten.
1329 */
gserial_connect(struct gserial * gser,u8 port_num)1330 int gserial_connect(struct gserial *gser, u8 port_num)
1331 {
1332 struct gs_port *port;
1333 unsigned long flags;
1334 int status;
1335
1336 if (port_num >= MAX_U_SERIAL_PORTS)
1337 return -ENXIO;
1338
1339 port = ports[port_num].port;
1340 if (!port) {
1341 pr_err("serial line %d not allocated.\n", port_num);
1342 return -EINVAL;
1343 }
1344 if (port->port_usb) {
1345 pr_err("serial line %d is in use.\n", port_num);
1346 return -EBUSY;
1347 }
1348
1349 /* activate the endpoints */
1350 status = usb_ep_enable(gser->in);
1351 if (status < 0)
1352 return status;
1353 gser->in->driver_data = port;
1354
1355 status = usb_ep_enable(gser->out);
1356 if (status < 0)
1357 goto fail_out;
1358 gser->out->driver_data = port;
1359
1360 /* then tell the tty glue that I/O can work */
1361 spin_lock_irqsave(&port->port_lock, flags);
1362 gser->ioport = port;
1363 port->port_usb = gser;
1364
1365 /* REVISIT unclear how best to handle this state...
1366 * we don't really couple it with the Linux TTY.
1367 */
1368 gser->port_line_coding = port->port_line_coding;
1369
1370 /* REVISIT if waiting on "carrier detect", signal. */
1371
1372 /* if it's already open, start I/O ... and notify the serial
1373 * protocol about open/close status (connect/disconnect).
1374 */
1375 if (port->port.count) {
1376 pr_debug("gserial_connect: start ttyGS%d\n", port->port_num);
1377 gs_start_io(port);
1378 if (gser->connect)
1379 gser->connect(gser);
1380 } else {
1381 if (gser->disconnect)
1382 gser->disconnect(gser);
1383 }
1384
1385 status = gs_console_connect(port);
1386 spin_unlock_irqrestore(&port->port_lock, flags);
1387
1388 return status;
1389
1390 fail_out:
1391 usb_ep_disable(gser->in);
1392 return status;
1393 }
1394 EXPORT_SYMBOL_GPL(gserial_connect);
1395 /**
1396 * gserial_disconnect - notify TTY I/O glue that USB link is inactive
1397 * @gser: the function, on which gserial_connect() was called
1398 * Context: any (usually from irq)
1399 *
1400 * This is called to deactivate endpoints and let the TTY layer know
1401 * that the connection went inactive ... not unlike "hangup".
1402 *
1403 * On return, the state is as if gserial_connect() had never been called;
1404 * there is no active USB I/O on these endpoints.
1405 */
gserial_disconnect(struct gserial * gser)1406 void gserial_disconnect(struct gserial *gser)
1407 {
1408 struct gs_port *port = gser->ioport;
1409 unsigned long flags;
1410
1411 if (!port)
1412 return;
1413
1414 spin_lock_irqsave(&serial_port_lock, flags);
1415
1416 /* tell the TTY glue not to do I/O here any more */
1417 spin_lock(&port->port_lock);
1418
1419 gs_console_disconnect(port);
1420
1421 /* REVISIT as above: how best to track this? */
1422 port->port_line_coding = gser->port_line_coding;
1423
1424 port->port_usb = NULL;
1425 gser->ioport = NULL;
1426 if (port->port.count > 0) {
1427 wake_up_interruptible(&port->drain_wait);
1428 if (port->port.tty)
1429 tty_hangup(port->port.tty);
1430 }
1431 port->suspended = false;
1432 spin_unlock(&port->port_lock);
1433 spin_unlock_irqrestore(&serial_port_lock, flags);
1434
1435 /* disable endpoints, aborting down any active I/O */
1436 usb_ep_disable(gser->out);
1437 usb_ep_disable(gser->in);
1438
1439 /* finally, free any unused/unusable I/O buffers */
1440 spin_lock_irqsave(&port->port_lock, flags);
1441 if (port->port.count == 0)
1442 kfifo_free(&port->port_write_buf);
1443 gs_free_requests(gser->out, &port->read_pool, NULL);
1444 gs_free_requests(gser->out, &port->read_queue, NULL);
1445 gs_free_requests(gser->in, &port->write_pool, NULL);
1446
1447 port->read_allocated = port->read_started =
1448 port->write_allocated = port->write_started = 0;
1449
1450 spin_unlock_irqrestore(&port->port_lock, flags);
1451 }
1452 EXPORT_SYMBOL_GPL(gserial_disconnect);
1453
gserial_suspend(struct gserial * gser)1454 void gserial_suspend(struct gserial *gser)
1455 {
1456 struct gs_port *port;
1457 unsigned long flags;
1458
1459 spin_lock_irqsave(&serial_port_lock, flags);
1460 port = gser->ioport;
1461
1462 if (!port) {
1463 spin_unlock_irqrestore(&serial_port_lock, flags);
1464 return;
1465 }
1466
1467 spin_lock(&port->port_lock);
1468 spin_unlock(&serial_port_lock);
1469 port->suspended = true;
1470 port->start_delayed = true;
1471 spin_unlock_irqrestore(&port->port_lock, flags);
1472 }
1473 EXPORT_SYMBOL_GPL(gserial_suspend);
1474
gserial_resume(struct gserial * gser)1475 void gserial_resume(struct gserial *gser)
1476 {
1477 struct gs_port *port;
1478 unsigned long flags;
1479
1480 spin_lock_irqsave(&serial_port_lock, flags);
1481 port = gser->ioport;
1482
1483 if (!port) {
1484 spin_unlock_irqrestore(&serial_port_lock, flags);
1485 return;
1486 }
1487
1488 spin_lock(&port->port_lock);
1489 spin_unlock(&serial_port_lock);
1490 port->suspended = false;
1491 if (!port->start_delayed) {
1492 spin_unlock_irqrestore(&port->port_lock, flags);
1493 return;
1494 }
1495
1496 pr_debug("delayed start ttyGS%d\n", port->port_num);
1497 gs_start_io(port);
1498 if (gser->connect)
1499 gser->connect(gser);
1500 port->start_delayed = false;
1501 spin_unlock_irqrestore(&port->port_lock, flags);
1502 }
1503 EXPORT_SYMBOL_GPL(gserial_resume);
1504
userial_init(void)1505 static int __init userial_init(void)
1506 {
1507 struct tty_driver *driver;
1508 unsigned i;
1509 int status;
1510
1511 driver = tty_alloc_driver(MAX_U_SERIAL_PORTS, TTY_DRIVER_REAL_RAW |
1512 TTY_DRIVER_DYNAMIC_DEV);
1513 if (IS_ERR(driver))
1514 return PTR_ERR(driver);
1515
1516 driver->driver_name = "g_serial";
1517 driver->name = "ttyGS";
1518 /* uses dynamically assigned dev_t values */
1519
1520 driver->type = TTY_DRIVER_TYPE_SERIAL;
1521 driver->subtype = SERIAL_TYPE_NORMAL;
1522 driver->init_termios = tty_std_termios;
1523
1524 /* 9600-8-N-1 ... matches defaults expected by "usbser.sys" on
1525 * MS-Windows. Otherwise, most of these flags shouldn't affect
1526 * anything unless we were to actually hook up to a serial line.
1527 */
1528 driver->init_termios.c_cflag =
1529 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1530 driver->init_termios.c_ispeed = 9600;
1531 driver->init_termios.c_ospeed = 9600;
1532
1533 tty_set_operations(driver, &gs_tty_ops);
1534 for (i = 0; i < MAX_U_SERIAL_PORTS; i++)
1535 mutex_init(&ports[i].lock);
1536
1537 /* export the driver ... */
1538 status = tty_register_driver(driver);
1539 if (status) {
1540 pr_err("%s: cannot register, err %d\n",
1541 __func__, status);
1542 goto fail;
1543 }
1544
1545 gs_tty_driver = driver;
1546
1547 pr_debug("%s: registered %d ttyGS* device%s\n", __func__,
1548 MAX_U_SERIAL_PORTS,
1549 str_plural(MAX_U_SERIAL_PORTS));
1550
1551 return status;
1552 fail:
1553 tty_driver_kref_put(driver);
1554 return status;
1555 }
1556 module_init(userial_init);
1557
userial_cleanup(void)1558 static void __exit userial_cleanup(void)
1559 {
1560 tty_unregister_driver(gs_tty_driver);
1561 tty_driver_kref_put(gs_tty_driver);
1562 gs_tty_driver = NULL;
1563 }
1564 module_exit(userial_cleanup);
1565
1566 MODULE_DESCRIPTION("utilities for USB gadget \"serial port\"/TTY support");
1567 MODULE_LICENSE("GPL");
1568