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