xref: /linux/drivers/usb/gadget/function/u_serial.c (revision c0c9379f235df33a12ceae94370ad80c5278324d)
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 
gserial_wakeup_host(struct gserial * gser)595 static int gserial_wakeup_host(struct gserial *gser)
596 {
597 	struct usb_function	*func = &gser->func;
598 	struct usb_gadget	*gadget = func->config->cdev->gadget;
599 
600 	if (func->func_suspended)
601 		return usb_func_wakeup(func);
602 	else
603 		return usb_gadget_wakeup(gadget);
604 }
605 
606 /*-------------------------------------------------------------------------*/
607 
608 /* TTY Driver */
609 
610 /*
611  * gs_open sets up the link between a gs_port and its associated TTY.
612  * That link is broken *only* by TTY close(), and all driver methods
613  * know that.
614  */
gs_open(struct tty_struct * tty,struct file * file)615 static int gs_open(struct tty_struct *tty, struct file *file)
616 {
617 	int		port_num = tty->index;
618 	struct gs_port	*port;
619 	int		status = 0;
620 
621 	mutex_lock(&ports[port_num].lock);
622 	port = ports[port_num].port;
623 	if (!port) {
624 		status = -ENODEV;
625 		goto out;
626 	}
627 
628 	spin_lock_irq(&port->port_lock);
629 
630 	/* allocate circular buffer on first open */
631 	if (!kfifo_initialized(&port->port_write_buf)) {
632 
633 		spin_unlock_irq(&port->port_lock);
634 
635 		/*
636 		 * portmaster's mutex still protects from simultaneous open(),
637 		 * and close() can't happen, yet.
638 		 */
639 
640 		status = kfifo_alloc(&port->port_write_buf,
641 				     WRITE_BUF_SIZE, GFP_KERNEL);
642 		if (status) {
643 			pr_debug("gs_open: ttyGS%d (%p,%p) no buffer\n",
644 				 port_num, tty, file);
645 			goto out;
646 		}
647 
648 		spin_lock_irq(&port->port_lock);
649 	}
650 
651 	/* already open?  Great. */
652 	if (port->port.count++)
653 		goto exit_unlock_port;
654 
655 	tty->driver_data = port;
656 	port->port.tty = tty;
657 
658 	/* if connected, start the I/O stream */
659 	if (port->port_usb) {
660 		/* if port is suspended, wait resume to start I/0 stream */
661 		if (!port->suspended) {
662 			struct gserial	*gser = port->port_usb;
663 
664 			pr_debug("gs_open: start ttyGS%d\n", port->port_num);
665 			gs_start_io(port);
666 
667 			if (gser->connect)
668 				gser->connect(gser);
669 		} else {
670 			pr_debug("delay start of ttyGS%d\n", port->port_num);
671 			port->start_delayed = true;
672 		}
673 	}
674 
675 	pr_debug("gs_open: ttyGS%d (%p,%p)\n", port->port_num, tty, file);
676 
677 exit_unlock_port:
678 	spin_unlock_irq(&port->port_lock);
679 out:
680 	mutex_unlock(&ports[port_num].lock);
681 	return status;
682 }
683 
gs_close_flush_done(struct gs_port * p)684 static int gs_close_flush_done(struct gs_port *p)
685 {
686 	int cond;
687 
688 	/* return true on disconnect or empty buffer or if raced with open() */
689 	spin_lock_irq(&p->port_lock);
690 	cond = p->port_usb == NULL || !kfifo_len(&p->port_write_buf) ||
691 		p->port.count > 1;
692 	spin_unlock_irq(&p->port_lock);
693 
694 	return cond;
695 }
696 
gs_close(struct tty_struct * tty,struct file * file)697 static void gs_close(struct tty_struct *tty, struct file *file)
698 {
699 	struct gs_port *port = tty->driver_data;
700 	struct gserial	*gser;
701 
702 	spin_lock_irq(&port->port_lock);
703 
704 	if (port->port.count != 1) {
705 raced_with_open:
706 		if (port->port.count == 0)
707 			WARN_ON(1);
708 		else
709 			--port->port.count;
710 		goto exit;
711 	}
712 
713 	pr_debug("gs_close: ttyGS%d (%p,%p) ...\n", port->port_num, tty, file);
714 
715 	gser = port->port_usb;
716 	if (gser && !port->suspended && gser->disconnect)
717 		gser->disconnect(gser);
718 
719 	/* wait for circular write buffer to drain, disconnect, or at
720 	 * most GS_CLOSE_TIMEOUT seconds; then discard the rest
721 	 */
722 	if (kfifo_len(&port->port_write_buf) > 0 && gser) {
723 		spin_unlock_irq(&port->port_lock);
724 		wait_event_interruptible_timeout(port->drain_wait,
725 					gs_close_flush_done(port),
726 					GS_CLOSE_TIMEOUT * HZ);
727 		spin_lock_irq(&port->port_lock);
728 
729 		if (port->port.count != 1)
730 			goto raced_with_open;
731 
732 		gser = port->port_usb;
733 	}
734 
735 	/* Iff we're disconnected, there can be no I/O in flight so it's
736 	 * ok to free the circular buffer; else just scrub it.  And don't
737 	 * let the push async work fire again until we're re-opened.
738 	 */
739 	if (gser == NULL)
740 		kfifo_free(&port->port_write_buf);
741 	else
742 		kfifo_reset(&port->port_write_buf);
743 
744 	port->start_delayed = false;
745 	port->port.count = 0;
746 	port->port.tty = NULL;
747 
748 	pr_debug("gs_close: ttyGS%d (%p,%p) done!\n",
749 			port->port_num, tty, file);
750 
751 	wake_up(&port->close_wait);
752 exit:
753 	spin_unlock_irq(&port->port_lock);
754 }
755 
gs_write(struct tty_struct * tty,const u8 * buf,size_t count)756 static ssize_t gs_write(struct tty_struct *tty, const u8 *buf, size_t count)
757 {
758 	struct gs_port	*port = tty->driver_data;
759 	unsigned long	flags;
760 	int ret = 0;
761 	struct gserial  *gser = port->port_usb;
762 
763 	pr_vdebug("gs_write: ttyGS%d (%p) writing %zu bytes\n",
764 			port->port_num, tty, count);
765 
766 	spin_lock_irqsave(&port->port_lock, flags);
767 	if (count)
768 		count = kfifo_in(&port->port_write_buf, buf, count);
769 
770 	if (port->suspended) {
771 		spin_unlock_irqrestore(&port->port_lock, flags);
772 		ret = gserial_wakeup_host(gser);
773 		if (ret) {
774 			pr_debug("ttyGS%d: Remote wakeup failed:%d\n", port->port_num, ret);
775 			return count;
776 		}
777 		spin_lock_irqsave(&port->port_lock, flags);
778 	}
779 
780 	/* treat count == 0 as flush_chars() */
781 	if (port->port_usb)
782 		gs_start_tx(port);
783 	spin_unlock_irqrestore(&port->port_lock, flags);
784 
785 	return count;
786 }
787 
gs_put_char(struct tty_struct * tty,u8 ch)788 static int gs_put_char(struct tty_struct *tty, u8 ch)
789 {
790 	struct gs_port	*port = tty->driver_data;
791 	unsigned long	flags;
792 	int		status;
793 
794 	pr_vdebug("gs_put_char: (%d,%p) char=0x%x, called from %ps\n",
795 		port->port_num, tty, ch, __builtin_return_address(0));
796 
797 	spin_lock_irqsave(&port->port_lock, flags);
798 	status = kfifo_put(&port->port_write_buf, ch);
799 	spin_unlock_irqrestore(&port->port_lock, flags);
800 
801 	return status;
802 }
803 
gs_flush_chars(struct tty_struct * tty)804 static void gs_flush_chars(struct tty_struct *tty)
805 {
806 	struct gs_port	*port = tty->driver_data;
807 	unsigned long	flags;
808 	int ret = 0;
809 	struct gserial  *gser = port->port_usb;
810 
811 	pr_vdebug("gs_flush_chars: (%d,%p)\n", port->port_num, tty);
812 
813 	spin_lock_irqsave(&port->port_lock, flags);
814 	if (port->suspended) {
815 		spin_unlock_irqrestore(&port->port_lock, flags);
816 		ret = gserial_wakeup_host(gser);
817 		if (ret) {
818 			pr_debug("ttyGS%d: Remote wakeup failed:%d\n", port->port_num, ret);
819 			return;
820 		}
821 		spin_lock_irqsave(&port->port_lock, flags);
822 	}
823 
824 	if (port->port_usb)
825 		gs_start_tx(port);
826 	spin_unlock_irqrestore(&port->port_lock, flags);
827 }
828 
gs_write_room(struct tty_struct * tty)829 static unsigned int gs_write_room(struct tty_struct *tty)
830 {
831 	struct gs_port	*port = tty->driver_data;
832 	unsigned long	flags;
833 	unsigned int room = 0;
834 
835 	spin_lock_irqsave(&port->port_lock, flags);
836 	if (port->port_usb)
837 		room = kfifo_avail(&port->port_write_buf);
838 	spin_unlock_irqrestore(&port->port_lock, flags);
839 
840 	pr_vdebug("gs_write_room: (%d,%p) room=%u\n",
841 		port->port_num, tty, room);
842 
843 	return room;
844 }
845 
gs_chars_in_buffer(struct tty_struct * tty)846 static unsigned int gs_chars_in_buffer(struct tty_struct *tty)
847 {
848 	struct gs_port	*port = tty->driver_data;
849 	unsigned long	flags;
850 	unsigned int	chars;
851 
852 	spin_lock_irqsave(&port->port_lock, flags);
853 	chars = kfifo_len(&port->port_write_buf);
854 	spin_unlock_irqrestore(&port->port_lock, flags);
855 
856 	pr_vdebug("gs_chars_in_buffer: (%d,%p) chars=%u\n",
857 		port->port_num, tty, chars);
858 
859 	return chars;
860 }
861 
862 /* undo side effects of setting TTY_THROTTLED */
gs_unthrottle(struct tty_struct * tty)863 static void gs_unthrottle(struct tty_struct *tty)
864 {
865 	struct gs_port		*port = tty->driver_data;
866 	unsigned long		flags;
867 
868 	spin_lock_irqsave(&port->port_lock, flags);
869 	if (port->port_usb) {
870 		/* Kickstart read queue processing.  We don't do xon/xoff,
871 		 * rts/cts, or other handshaking with the host, but if the
872 		 * read queue backs up enough we'll be NAKing OUT packets.
873 		 */
874 		pr_vdebug("ttyGS%d: unthrottle\n", port->port_num);
875 		schedule_delayed_work(&port->push, 0);
876 	}
877 	spin_unlock_irqrestore(&port->port_lock, flags);
878 }
879 
gs_break_ctl(struct tty_struct * tty,int duration)880 static int gs_break_ctl(struct tty_struct *tty, int duration)
881 {
882 	struct gs_port	*port = tty->driver_data;
883 	int		status = 0;
884 	struct gserial	*gser;
885 
886 	pr_vdebug("gs_break_ctl: ttyGS%d, send break (%d) \n",
887 			port->port_num, duration);
888 
889 	spin_lock_irq(&port->port_lock);
890 	gser = port->port_usb;
891 	if (gser && gser->send_break)
892 		status = gser->send_break(gser, duration);
893 	spin_unlock_irq(&port->port_lock);
894 
895 	return status;
896 }
897 
gs_get_icount(struct tty_struct * tty,struct serial_icounter_struct * icount)898 static int gs_get_icount(struct tty_struct *tty,
899 			 struct serial_icounter_struct *icount)
900 {
901 	struct gs_port *port = tty->driver_data;
902 	struct async_icount cnow;
903 	unsigned long flags;
904 
905 	spin_lock_irqsave(&port->port_lock, flags);
906 	cnow = port->icount;
907 	spin_unlock_irqrestore(&port->port_lock, flags);
908 
909 	icount->rx = cnow.rx;
910 	icount->tx = cnow.tx;
911 
912 	return 0;
913 }
914 
915 static const struct tty_operations gs_tty_ops = {
916 	.open =			gs_open,
917 	.close =		gs_close,
918 	.write =		gs_write,
919 	.put_char =		gs_put_char,
920 	.flush_chars =		gs_flush_chars,
921 	.write_room =		gs_write_room,
922 	.chars_in_buffer =	gs_chars_in_buffer,
923 	.unthrottle =		gs_unthrottle,
924 	.break_ctl =		gs_break_ctl,
925 	.get_icount =		gs_get_icount,
926 };
927 
928 /*-------------------------------------------------------------------------*/
929 
930 static struct tty_driver *gs_tty_driver;
931 
932 #ifdef CONFIG_U_SERIAL_CONSOLE
933 
gs_console_complete_out(struct usb_ep * ep,struct usb_request * req)934 static void gs_console_complete_out(struct usb_ep *ep, struct usb_request *req)
935 {
936 	struct gs_console *cons = req->context;
937 
938 	switch (req->status) {
939 	default:
940 		pr_warn("%s: unexpected %s status %d\n",
941 			__func__, ep->name, req->status);
942 		fallthrough;
943 	case 0:
944 		/* normal completion */
945 		spin_lock(&cons->lock);
946 		req->length = 0;
947 		schedule_work(&cons->work);
948 		spin_unlock(&cons->lock);
949 		break;
950 	case -ECONNRESET:
951 	case -ESHUTDOWN:
952 		/* disconnect */
953 		pr_vdebug("%s: %s shutdown\n", __func__, ep->name);
954 		break;
955 	}
956 }
957 
__gs_console_push(struct gs_console * cons)958 static void __gs_console_push(struct gs_console *cons)
959 {
960 	struct usb_request *req = cons->req;
961 	struct usb_ep *ep;
962 	size_t size;
963 
964 	if (!req)
965 		return;	/* disconnected */
966 
967 	if (req->length)
968 		return;	/* busy */
969 
970 	ep = cons->console.data;
971 	size = kfifo_out(&cons->buf, req->buf, ep->maxpacket);
972 	if (!size)
973 		return;
974 
975 	if (cons->missed && ep->maxpacket >= 64) {
976 		char buf[64];
977 		size_t len;
978 
979 		len = sprintf(buf, "\n[missed %zu bytes]\n", cons->missed);
980 		kfifo_in(&cons->buf, buf, len);
981 		cons->missed = 0;
982 	}
983 
984 	req->length = size;
985 
986 	spin_unlock_irq(&cons->lock);
987 	if (usb_ep_queue(ep, req, GFP_ATOMIC))
988 		req->length = 0;
989 	spin_lock_irq(&cons->lock);
990 }
991 
gs_console_work(struct work_struct * work)992 static void gs_console_work(struct work_struct *work)
993 {
994 	struct gs_console *cons = container_of(work, struct gs_console, work);
995 
996 	spin_lock_irq(&cons->lock);
997 
998 	__gs_console_push(cons);
999 
1000 	spin_unlock_irq(&cons->lock);
1001 }
1002 
gs_console_write(struct console * co,const char * buf,unsigned count)1003 static void gs_console_write(struct console *co,
1004 			     const char *buf, unsigned count)
1005 {
1006 	struct gs_console *cons = container_of(co, struct gs_console, console);
1007 	unsigned long flags;
1008 	size_t n;
1009 
1010 	spin_lock_irqsave(&cons->lock, flags);
1011 
1012 	n = kfifo_in(&cons->buf, buf, count);
1013 	if (n < count)
1014 		cons->missed += count - n;
1015 
1016 	if (cons->req && !cons->req->length)
1017 		schedule_work(&cons->work);
1018 
1019 	spin_unlock_irqrestore(&cons->lock, flags);
1020 }
1021 
gs_console_device(struct console * co,int * index)1022 static struct tty_driver *gs_console_device(struct console *co, int *index)
1023 {
1024 	*index = co->index;
1025 	return gs_tty_driver;
1026 }
1027 
gs_console_connect(struct gs_port * port)1028 static int gs_console_connect(struct gs_port *port)
1029 {
1030 	struct gs_console *cons = port->console;
1031 	struct usb_request *req;
1032 	struct usb_ep *ep;
1033 
1034 	if (!cons)
1035 		return 0;
1036 
1037 	ep = port->port_usb->in;
1038 	req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC);
1039 	if (!req)
1040 		return -ENOMEM;
1041 	req->complete = gs_console_complete_out;
1042 	req->context = cons;
1043 	req->length = 0;
1044 
1045 	spin_lock(&cons->lock);
1046 	cons->req = req;
1047 	cons->console.data = ep;
1048 	spin_unlock(&cons->lock);
1049 
1050 	pr_debug("ttyGS%d: console connected!\n", port->port_num);
1051 
1052 	schedule_work(&cons->work);
1053 
1054 	return 0;
1055 }
1056 
gs_console_disconnect(struct gs_port * port)1057 static void gs_console_disconnect(struct gs_port *port)
1058 {
1059 	struct gs_console *cons = port->console;
1060 	struct usb_request *req;
1061 	struct usb_ep *ep;
1062 
1063 	if (!cons)
1064 		return;
1065 
1066 	spin_lock(&cons->lock);
1067 
1068 	req = cons->req;
1069 	ep = cons->console.data;
1070 	cons->req = NULL;
1071 
1072 	spin_unlock(&cons->lock);
1073 
1074 	if (!req)
1075 		return;
1076 
1077 	usb_ep_dequeue(ep, req);
1078 	gs_free_req(ep, req);
1079 }
1080 
gs_console_init(struct gs_port * port)1081 static int gs_console_init(struct gs_port *port)
1082 {
1083 	struct gs_console *cons;
1084 	int err;
1085 
1086 	if (port->console)
1087 		return 0;
1088 
1089 	cons = kzalloc(sizeof(*port->console), GFP_KERNEL);
1090 	if (!cons)
1091 		return -ENOMEM;
1092 
1093 	strcpy(cons->console.name, "ttyGS");
1094 	cons->console.write = gs_console_write;
1095 	cons->console.device = gs_console_device;
1096 	cons->console.flags = CON_PRINTBUFFER;
1097 	cons->console.index = port->port_num;
1098 
1099 	INIT_WORK(&cons->work, gs_console_work);
1100 	spin_lock_init(&cons->lock);
1101 
1102 	err = kfifo_alloc(&cons->buf, GS_CONSOLE_BUF_SIZE, GFP_KERNEL);
1103 	if (err) {
1104 		pr_err("ttyGS%d: allocate console buffer failed\n", port->port_num);
1105 		kfree(cons);
1106 		return err;
1107 	}
1108 
1109 	port->console = cons;
1110 	register_console(&cons->console);
1111 
1112 	spin_lock_irq(&port->port_lock);
1113 	if (port->port_usb)
1114 		gs_console_connect(port);
1115 	spin_unlock_irq(&port->port_lock);
1116 
1117 	return 0;
1118 }
1119 
gs_console_exit(struct gs_port * port)1120 static void gs_console_exit(struct gs_port *port)
1121 {
1122 	struct gs_console *cons = port->console;
1123 
1124 	if (!cons)
1125 		return;
1126 
1127 	unregister_console(&cons->console);
1128 
1129 	spin_lock_irq(&port->port_lock);
1130 	if (cons->req)
1131 		gs_console_disconnect(port);
1132 	spin_unlock_irq(&port->port_lock);
1133 
1134 	cancel_work_sync(&cons->work);
1135 	kfifo_free(&cons->buf);
1136 	kfree(cons);
1137 	port->console = NULL;
1138 }
1139 
gserial_set_console(unsigned char port_num,const char * page,size_t count)1140 ssize_t gserial_set_console(unsigned char port_num, const char *page, size_t count)
1141 {
1142 	struct gs_port *port;
1143 	bool enable;
1144 	int ret;
1145 
1146 	ret = kstrtobool(page, &enable);
1147 	if (ret)
1148 		return ret;
1149 
1150 	mutex_lock(&ports[port_num].lock);
1151 	port = ports[port_num].port;
1152 
1153 	if (WARN_ON(port == NULL)) {
1154 		ret = -ENXIO;
1155 		goto out;
1156 	}
1157 
1158 	if (enable)
1159 		ret = gs_console_init(port);
1160 	else
1161 		gs_console_exit(port);
1162 out:
1163 	mutex_unlock(&ports[port_num].lock);
1164 
1165 	return ret < 0 ? ret : count;
1166 }
1167 EXPORT_SYMBOL_GPL(gserial_set_console);
1168 
gserial_get_console(unsigned char port_num,char * page)1169 ssize_t gserial_get_console(unsigned char port_num, char *page)
1170 {
1171 	struct gs_port *port;
1172 	ssize_t ret;
1173 
1174 	mutex_lock(&ports[port_num].lock);
1175 	port = ports[port_num].port;
1176 
1177 	if (WARN_ON(port == NULL))
1178 		ret = -ENXIO;
1179 	else
1180 		ret = sprintf(page, "%u\n", !!port->console);
1181 
1182 	mutex_unlock(&ports[port_num].lock);
1183 
1184 	return ret;
1185 }
1186 EXPORT_SYMBOL_GPL(gserial_get_console);
1187 
1188 #else
1189 
gs_console_connect(struct gs_port * port)1190 static int gs_console_connect(struct gs_port *port)
1191 {
1192 	return 0;
1193 }
1194 
gs_console_disconnect(struct gs_port * port)1195 static void gs_console_disconnect(struct gs_port *port)
1196 {
1197 }
1198 
gs_console_init(struct gs_port * port)1199 static int gs_console_init(struct gs_port *port)
1200 {
1201 	return -ENOSYS;
1202 }
1203 
gs_console_exit(struct gs_port * port)1204 static void gs_console_exit(struct gs_port *port)
1205 {
1206 }
1207 
1208 #endif
1209 
1210 static int
gs_port_alloc(unsigned port_num,struct usb_cdc_line_coding * coding)1211 gs_port_alloc(unsigned port_num, struct usb_cdc_line_coding *coding)
1212 {
1213 	struct gs_port	*port;
1214 	int		ret = 0;
1215 
1216 	mutex_lock(&ports[port_num].lock);
1217 	if (ports[port_num].port) {
1218 		ret = -EBUSY;
1219 		goto out;
1220 	}
1221 
1222 	port = kzalloc(sizeof(struct gs_port), GFP_KERNEL);
1223 	if (port == NULL) {
1224 		ret = -ENOMEM;
1225 		goto out;
1226 	}
1227 
1228 	tty_port_init(&port->port);
1229 	spin_lock_init(&port->port_lock);
1230 	init_waitqueue_head(&port->drain_wait);
1231 	init_waitqueue_head(&port->close_wait);
1232 
1233 	INIT_DELAYED_WORK(&port->push, gs_rx_push);
1234 
1235 	INIT_LIST_HEAD(&port->read_pool);
1236 	INIT_LIST_HEAD(&port->read_queue);
1237 	INIT_LIST_HEAD(&port->write_pool);
1238 
1239 	port->port_num = port_num;
1240 	port->port_line_coding = *coding;
1241 
1242 	ports[port_num].port = port;
1243 out:
1244 	mutex_unlock(&ports[port_num].lock);
1245 	return ret;
1246 }
1247 
gs_closed(struct gs_port * port)1248 static int gs_closed(struct gs_port *port)
1249 {
1250 	int cond;
1251 
1252 	spin_lock_irq(&port->port_lock);
1253 	cond = port->port.count == 0;
1254 	spin_unlock_irq(&port->port_lock);
1255 
1256 	return cond;
1257 }
1258 
gserial_free_port(struct gs_port * port)1259 static void gserial_free_port(struct gs_port *port)
1260 {
1261 	cancel_delayed_work_sync(&port->push);
1262 	/* wait for old opens to finish */
1263 	wait_event(port->close_wait, gs_closed(port));
1264 	WARN_ON(port->port_usb != NULL);
1265 	tty_port_destroy(&port->port);
1266 	kfree(port);
1267 }
1268 
gserial_free_line(unsigned char port_num)1269 void gserial_free_line(unsigned char port_num)
1270 {
1271 	struct gs_port	*port;
1272 
1273 	mutex_lock(&ports[port_num].lock);
1274 	if (!ports[port_num].port) {
1275 		mutex_unlock(&ports[port_num].lock);
1276 		return;
1277 	}
1278 	port = ports[port_num].port;
1279 	gs_console_exit(port);
1280 	ports[port_num].port = NULL;
1281 	mutex_unlock(&ports[port_num].lock);
1282 
1283 	gserial_free_port(port);
1284 	tty_unregister_device(gs_tty_driver, port_num);
1285 }
1286 EXPORT_SYMBOL_GPL(gserial_free_line);
1287 
gserial_alloc_line_no_console(unsigned char * line_num)1288 int gserial_alloc_line_no_console(unsigned char *line_num)
1289 {
1290 	struct usb_cdc_line_coding	coding;
1291 	struct gs_port			*port;
1292 	struct device			*tty_dev;
1293 	int				ret;
1294 	int				port_num;
1295 
1296 	coding.dwDTERate = cpu_to_le32(9600);
1297 	coding.bCharFormat = 8;
1298 	coding.bParityType = USB_CDC_NO_PARITY;
1299 	coding.bDataBits = USB_CDC_1_STOP_BITS;
1300 
1301 	for (port_num = 0; port_num < MAX_U_SERIAL_PORTS; port_num++) {
1302 		ret = gs_port_alloc(port_num, &coding);
1303 		if (ret == -EBUSY)
1304 			continue;
1305 		if (ret)
1306 			return ret;
1307 		break;
1308 	}
1309 	if (ret)
1310 		return ret;
1311 
1312 	/* ... and sysfs class devices, so mdev/udev make /dev/ttyGS* */
1313 
1314 	port = ports[port_num].port;
1315 	tty_dev = tty_port_register_device(&port->port,
1316 			gs_tty_driver, port_num, NULL);
1317 	if (IS_ERR(tty_dev)) {
1318 		pr_err("%s: failed to register tty for port %d, err %ld\n",
1319 				__func__, port_num, PTR_ERR(tty_dev));
1320 
1321 		ret = PTR_ERR(tty_dev);
1322 		mutex_lock(&ports[port_num].lock);
1323 		ports[port_num].port = NULL;
1324 		mutex_unlock(&ports[port_num].lock);
1325 		gserial_free_port(port);
1326 		goto err;
1327 	}
1328 	*line_num = port_num;
1329 err:
1330 	return ret;
1331 }
1332 EXPORT_SYMBOL_GPL(gserial_alloc_line_no_console);
1333 
gserial_alloc_line(unsigned char * line_num)1334 int gserial_alloc_line(unsigned char *line_num)
1335 {
1336 	int ret = gserial_alloc_line_no_console(line_num);
1337 
1338 	if (!ret && !*line_num)
1339 		gs_console_init(ports[*line_num].port);
1340 
1341 	return ret;
1342 }
1343 EXPORT_SYMBOL_GPL(gserial_alloc_line);
1344 
1345 /**
1346  * gserial_connect - notify TTY I/O glue that USB link is active
1347  * @gser: the function, set up with endpoints and descriptors
1348  * @port_num: which port is active
1349  * Context: any (usually from irq)
1350  *
1351  * This is called activate endpoints and let the TTY layer know that
1352  * the connection is active ... not unlike "carrier detect".  It won't
1353  * necessarily start I/O queues; unless the TTY is held open by any
1354  * task, there would be no point.  However, the endpoints will be
1355  * activated so the USB host can perform I/O, subject to basic USB
1356  * hardware flow control.
1357  *
1358  * Caller needs to have set up the endpoints and USB function in @dev
1359  * before calling this, as well as the appropriate (speed-specific)
1360  * endpoint descriptors, and also have allocate @port_num by calling
1361  * @gserial_alloc_line().
1362  *
1363  * Returns negative errno or zero.
1364  * On success, ep->driver_data will be overwritten.
1365  */
gserial_connect(struct gserial * gser,u8 port_num)1366 int gserial_connect(struct gserial *gser, u8 port_num)
1367 {
1368 	struct gs_port	*port;
1369 	unsigned long	flags;
1370 	int		status;
1371 
1372 	if (port_num >= MAX_U_SERIAL_PORTS)
1373 		return -ENXIO;
1374 
1375 	port = ports[port_num].port;
1376 	if (!port) {
1377 		pr_err("serial line %d not allocated.\n", port_num);
1378 		return -EINVAL;
1379 	}
1380 	if (port->port_usb) {
1381 		pr_err("serial line %d is in use.\n", port_num);
1382 		return -EBUSY;
1383 	}
1384 
1385 	/* activate the endpoints */
1386 	status = usb_ep_enable(gser->in);
1387 	if (status < 0)
1388 		return status;
1389 	gser->in->driver_data = port;
1390 
1391 	status = usb_ep_enable(gser->out);
1392 	if (status < 0)
1393 		goto fail_out;
1394 	gser->out->driver_data = port;
1395 
1396 	/* then tell the tty glue that I/O can work */
1397 	spin_lock_irqsave(&port->port_lock, flags);
1398 	gser->ioport = port;
1399 	port->port_usb = gser;
1400 
1401 	/* REVISIT unclear how best to handle this state...
1402 	 * we don't really couple it with the Linux TTY.
1403 	 */
1404 	gser->port_line_coding = port->port_line_coding;
1405 
1406 	/* REVISIT if waiting on "carrier detect", signal. */
1407 
1408 	/* if it's already open, start I/O ... and notify the serial
1409 	 * protocol about open/close status (connect/disconnect).
1410 	 */
1411 	if (port->port.count) {
1412 		pr_debug("gserial_connect: start ttyGS%d\n", port->port_num);
1413 		gs_start_io(port);
1414 		if (gser->connect)
1415 			gser->connect(gser);
1416 	} else {
1417 		if (gser->disconnect)
1418 			gser->disconnect(gser);
1419 	}
1420 
1421 	status = gs_console_connect(port);
1422 	spin_unlock_irqrestore(&port->port_lock, flags);
1423 
1424 	return status;
1425 
1426 fail_out:
1427 	usb_ep_disable(gser->in);
1428 	return status;
1429 }
1430 EXPORT_SYMBOL_GPL(gserial_connect);
1431 /**
1432  * gserial_disconnect - notify TTY I/O glue that USB link is inactive
1433  * @gser: the function, on which gserial_connect() was called
1434  * Context: any (usually from irq)
1435  *
1436  * This is called to deactivate endpoints and let the TTY layer know
1437  * that the connection went inactive ... not unlike "hangup".
1438  *
1439  * On return, the state is as if gserial_connect() had never been called;
1440  * there is no active USB I/O on these endpoints.
1441  */
gserial_disconnect(struct gserial * gser)1442 void gserial_disconnect(struct gserial *gser)
1443 {
1444 	struct gs_port	*port = gser->ioport;
1445 	unsigned long	flags;
1446 
1447 	if (!port)
1448 		return;
1449 
1450 	spin_lock_irqsave(&serial_port_lock, flags);
1451 
1452 	/* tell the TTY glue not to do I/O here any more */
1453 	spin_lock(&port->port_lock);
1454 
1455 	gs_console_disconnect(port);
1456 
1457 	/* REVISIT as above: how best to track this? */
1458 	port->port_line_coding = gser->port_line_coding;
1459 
1460 	port->port_usb = NULL;
1461 	gser->ioport = NULL;
1462 	if (port->port.count > 0) {
1463 		wake_up_interruptible(&port->drain_wait);
1464 		if (port->port.tty)
1465 			tty_hangup(port->port.tty);
1466 	}
1467 	port->suspended = false;
1468 	spin_unlock(&port->port_lock);
1469 	spin_unlock_irqrestore(&serial_port_lock, flags);
1470 
1471 	/* disable endpoints, aborting down any active I/O */
1472 	usb_ep_disable(gser->out);
1473 	usb_ep_disable(gser->in);
1474 
1475 	/* finally, free any unused/unusable I/O buffers */
1476 	spin_lock_irqsave(&port->port_lock, flags);
1477 	if (port->port.count == 0)
1478 		kfifo_free(&port->port_write_buf);
1479 	gs_free_requests(gser->out, &port->read_pool, NULL);
1480 	gs_free_requests(gser->out, &port->read_queue, NULL);
1481 	gs_free_requests(gser->in, &port->write_pool, NULL);
1482 
1483 	port->read_allocated = port->read_started =
1484 		port->write_allocated = port->write_started = 0;
1485 
1486 	spin_unlock_irqrestore(&port->port_lock, flags);
1487 }
1488 EXPORT_SYMBOL_GPL(gserial_disconnect);
1489 
gserial_suspend(struct gserial * gser)1490 void gserial_suspend(struct gserial *gser)
1491 {
1492 	struct gs_port	*port;
1493 	unsigned long	flags;
1494 
1495 	spin_lock_irqsave(&serial_port_lock, flags);
1496 	port = gser->ioport;
1497 
1498 	if (!port) {
1499 		spin_unlock_irqrestore(&serial_port_lock, flags);
1500 		return;
1501 	}
1502 
1503 	if (port->write_busy || port->write_started) {
1504 		/* Wakeup to host if there are ongoing transfers */
1505 		spin_unlock_irqrestore(&serial_port_lock, flags);
1506 		if (!gserial_wakeup_host(gser))
1507 			return;
1508 
1509 		/* Check if port is valid after acquiring lock back */
1510 		spin_lock_irqsave(&serial_port_lock, flags);
1511 		if (!port) {
1512 			spin_unlock_irqrestore(&serial_port_lock, flags);
1513 			return;
1514 		}
1515 	}
1516 
1517 	spin_lock(&port->port_lock);
1518 	spin_unlock(&serial_port_lock);
1519 	port->suspended = true;
1520 	port->start_delayed = true;
1521 	spin_unlock_irqrestore(&port->port_lock, flags);
1522 }
1523 EXPORT_SYMBOL_GPL(gserial_suspend);
1524 
gserial_resume(struct gserial * gser)1525 void gserial_resume(struct gserial *gser)
1526 {
1527 	struct gs_port *port;
1528 	unsigned long	flags;
1529 
1530 	spin_lock_irqsave(&serial_port_lock, flags);
1531 	port = gser->ioport;
1532 
1533 	if (!port) {
1534 		spin_unlock_irqrestore(&serial_port_lock, flags);
1535 		return;
1536 	}
1537 
1538 	spin_lock(&port->port_lock);
1539 	spin_unlock(&serial_port_lock);
1540 	port->suspended = false;
1541 	if (!port->start_delayed) {
1542 		spin_unlock_irqrestore(&port->port_lock, flags);
1543 		return;
1544 	}
1545 
1546 	pr_debug("delayed start ttyGS%d\n", port->port_num);
1547 	gs_start_io(port);
1548 	if (gser->connect)
1549 		gser->connect(gser);
1550 	port->start_delayed = false;
1551 	spin_unlock_irqrestore(&port->port_lock, flags);
1552 }
1553 EXPORT_SYMBOL_GPL(gserial_resume);
1554 
userial_init(void)1555 static int __init userial_init(void)
1556 {
1557 	struct tty_driver *driver;
1558 	unsigned			i;
1559 	int				status;
1560 
1561 	driver = tty_alloc_driver(MAX_U_SERIAL_PORTS, TTY_DRIVER_REAL_RAW |
1562 			TTY_DRIVER_DYNAMIC_DEV);
1563 	if (IS_ERR(driver))
1564 		return PTR_ERR(driver);
1565 
1566 	driver->driver_name = "g_serial";
1567 	driver->name = "ttyGS";
1568 	/* uses dynamically assigned dev_t values */
1569 
1570 	driver->type = TTY_DRIVER_TYPE_SERIAL;
1571 	driver->subtype = SERIAL_TYPE_NORMAL;
1572 	driver->init_termios = tty_std_termios;
1573 
1574 	/* 9600-8-N-1 ... matches defaults expected by "usbser.sys" on
1575 	 * MS-Windows.  Otherwise, most of these flags shouldn't affect
1576 	 * anything unless we were to actually hook up to a serial line.
1577 	 */
1578 	driver->init_termios.c_cflag =
1579 			B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1580 	driver->init_termios.c_ispeed = 9600;
1581 	driver->init_termios.c_ospeed = 9600;
1582 
1583 	tty_set_operations(driver, &gs_tty_ops);
1584 	for (i = 0; i < MAX_U_SERIAL_PORTS; i++)
1585 		mutex_init(&ports[i].lock);
1586 
1587 	/* export the driver ... */
1588 	status = tty_register_driver(driver);
1589 	if (status) {
1590 		pr_err("%s: cannot register, err %d\n",
1591 				__func__, status);
1592 		goto fail;
1593 	}
1594 
1595 	gs_tty_driver = driver;
1596 
1597 	pr_debug("%s: registered %d ttyGS* device%s\n", __func__,
1598 			MAX_U_SERIAL_PORTS,
1599 			str_plural(MAX_U_SERIAL_PORTS));
1600 
1601 	return status;
1602 fail:
1603 	tty_driver_kref_put(driver);
1604 	return status;
1605 }
1606 module_init(userial_init);
1607 
userial_cleanup(void)1608 static void __exit userial_cleanup(void)
1609 {
1610 	tty_unregister_driver(gs_tty_driver);
1611 	tty_driver_kref_put(gs_tty_driver);
1612 	gs_tty_driver = NULL;
1613 }
1614 module_exit(userial_cleanup);
1615 
1616 MODULE_DESCRIPTION("utilities for USB gadget \"serial port\"/TTY support");
1617 MODULE_LICENSE("GPL");
1618