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