1607 lines · 38.5 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0+/* * u_serial.c - utilities for USB gadget "serial port"/TTY support * * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com) * Copyright (C) 2008 David Brownell * Copyright (C) 2008 by Nokia Corporation * * This code also borrows from usbserial.c, which is * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com) * Copyright (C) 2000 Peter Berger (pberger@brimson.com) * Copyright (C) 2000 Al Borchers (alborchers@steinerpoint.com) */ /* #define VERBOSE_DEBUG */ #include <linux/kernel.h>#include <linux/sched.h>#include <linux/device.h>#include <linux/delay.h>#include <linux/tty.h>#include <linux/tty_flip.h>#include <linux/slab.h>#include <linux/string_choices.h>#include <linux/export.h>#include <linux/module.h>#include <linux/console.h>#include <linux/kstrtox.h>#include <linux/kthread.h>#include <linux/workqueue.h>#include <linux/kfifo.h>#include <linux/serial.h> #include "u_serial.h"  /* * This component encapsulates the TTY layer glue needed to provide basic * "serial port" functionality through the USB gadget stack.  Each such * port is exposed through a /dev/ttyGS* node. * * After this module has been loaded, the individual TTY port can be requested * (gserial_alloc_line()) and it will stay available until they are removed * (gserial_free_line()). Each one may be connected to a USB function * (gserial_connect), or disconnected (with gserial_disconnect) when the USB * host issues a config change event. Data can only flow when the port is * connected to the host. * * A given TTY port can be made available in multiple configurations. * For example, each one might expose a ttyGS0 node which provides a * login application.  In one case that might use CDC ACM interface 0, * while another configuration might use interface 3 for that.  The * work to handle that (including descriptor management) is not part * of this component. * * Configurations may expose more than one TTY port.  For example, if * ttyGS0 provides login service, then ttyGS1 might provide dialer access * for a telephone or fax link.  And ttyGS2 might be something that just * needs a simple byte stream interface for some messaging protocol that * is managed in userspace ... OBEX, PTP, and MTP have been mentioned. * * * gserial is the lifecycle interface, used by USB functions * gs_port is the I/O nexus, used by the tty driver * tty_struct links to the tty/filesystem framework * * gserial <---> gs_port ... links will be null when the USB link is * inactive; managed by gserial_{connect,disconnect}().  each gserial * instance can wrap its own USB control protocol. *	gserial->ioport == usb_ep->driver_data ... gs_port *	gs_port->port_usb ... gserial * * gs_port <---> tty_struct ... links will be null when the TTY file * isn't opened; managed by gs_open()/gs_close() *	gserial->port_tty ... tty_struct *	tty_struct->driver_data ... gserial */ /* RX and TX queues can buffer QUEUE_SIZE packets before they hit the * next layer of buffering.  For TX that's a circular buffer; for RX * consider it a NOP.  A third layer is provided by the TTY code. */#define QUEUE_SIZE		16#define WRITE_BUF_SIZE		8192		/* TX only */#define GS_CONSOLE_BUF_SIZE	8192 /* Prevents race conditions while accessing gser->ioport */static DEFINE_SPINLOCK(serial_port_lock); /* console info */struct gs_console {	struct console		console;	struct work_struct	work;	spinlock_t		lock;	struct usb_request	*req;	struct kfifo		buf;	size_t			missed;}; /* * The port structure holds info for each port, one for each minor number * (and thus for each /dev/ node). */struct gs_port {	struct tty_port		port;	spinlock_t		port_lock;	/* guard port_* access */ 	struct gserial		*port_usb;#ifdef CONFIG_U_SERIAL_CONSOLE	struct gs_console	*console;#endif 	u8			port_num; 	struct list_head	read_pool;	int read_started;	int read_allocated;	struct list_head	read_queue;	unsigned		n_read;	struct delayed_work	push; 	struct list_head	write_pool;	int write_started;	int write_allocated;	struct kfifo		port_write_buf;	wait_queue_head_t	drain_wait;	/* wait while writes drain */	bool                    write_busy;	wait_queue_head_t	close_wait;	bool			suspended;	/* port suspended */	bool			start_delayed;	/* delay start when suspended */	struct async_icount	icount; 	/* REVISIT this state ... */	struct usb_cdc_line_coding port_line_coding;	/* 8-N-1 etc */}; static struct portmaster {	struct mutex	lock;			/* protect open/close */	struct gs_port	*port;} ports[MAX_U_SERIAL_PORTS]; #define GS_CLOSE_TIMEOUT		15		/* seconds */   #ifdef VERBOSE_DEBUG#ifndef pr_vdebug#define pr_vdebug(fmt, arg...) \	pr_debug(fmt, ##arg)#endif /* pr_vdebug */#else#ifndef pr_vdebug#define pr_vdebug(fmt, arg...) \	({ if (0) pr_debug(fmt, ##arg); })#endif /* pr_vdebug */#endif /*-------------------------------------------------------------------------*/ /* I/O glue between TTY (upper) and USB function (lower) driver layers */ /* * gs_alloc_req * * Allocate a usb_request and its buffer.  Returns a pointer to the * usb_request or NULL if there is an error. */struct usb_request *gs_alloc_req(struct usb_ep *ep, unsigned len, gfp_t kmalloc_flags){	struct usb_request *req; 	req = usb_ep_alloc_request(ep, kmalloc_flags); 	if (req != NULL) {		req->length = len;		req->buf = kmalloc(len, kmalloc_flags);		if (req->buf == NULL) {			usb_ep_free_request(ep, req);			return NULL;		}	} 	return req;}EXPORT_SYMBOL_GPL(gs_alloc_req); /* * gs_free_req * * Free a usb_request and its buffer. */void gs_free_req(struct usb_ep *ep, struct usb_request *req){	kfree(req->buf);	usb_ep_free_request(ep, req);}EXPORT_SYMBOL_GPL(gs_free_req); /* * gs_send_packet * * If there is data to send, a packet is built in the given * buffer and the size is returned.  If there is no data to * send, 0 is returned. * * Called with port_lock held. */static unsignedgs_send_packet(struct gs_port *port, char *packet, unsigned size){	unsigned len; 	len = kfifo_len(&port->port_write_buf);	if (len < size)		size = len;	if (size != 0)		size = kfifo_out(&port->port_write_buf, packet, size);	return size;} /* * gs_start_tx * * This function finds available write requests, calls * gs_send_packet to fill these packets with data, and * continues until either there are no more write requests * available or no more data to send.  This function is * run whenever data arrives or write requests are available. * * Context: caller owns port_lock; port_usb is non-null. */static int gs_start_tx(struct gs_port *port)/*__releases(&port->port_lock)__acquires(&port->port_lock)*/{	struct list_head	*pool = &port->write_pool;	struct usb_ep		*in;	int			status = 0;	bool			do_tty_wake = false; 	if (!port->port_usb)		return status; 	in = port->port_usb->in; 	while (!port->write_busy && !list_empty(pool)) {		struct usb_request	*req;		int			len; 		if (port->write_started >= QUEUE_SIZE)			break; 		req = list_entry(pool->next, struct usb_request, list);		len = gs_send_packet(port, req->buf, in->maxpacket);		if (len == 0) {			wake_up_interruptible(&port->drain_wait);			break;		}		do_tty_wake = true;		port->icount.tx += len; 		req->length = len;		list_del(&req->list);		req->zero = kfifo_is_empty(&port->port_write_buf); 		pr_vdebug("ttyGS%d: tx len=%d, %3ph ...\n", port->port_num, len, req->buf); 		/* Drop lock while we call out of driver; completions		 * could be issued while we do so.  Disconnection may		 * happen too; maybe immediately before we queue this!		 *		 * NOTE that we may keep sending data for a while after		 * the TTY closed (dev->ioport->port_tty is NULL).		 */		port->write_busy = true;		spin_unlock(&port->port_lock);		status = usb_ep_queue(in, req, GFP_ATOMIC);		spin_lock(&port->port_lock);		port->write_busy = false; 		if (status) {			pr_debug("%s: %s %s err %d\n",					__func__, "queue", in->name, status);			list_add(&req->list, pool);			break;		} 		port->write_started++; 		/* abort immediately after disconnect */		if (!port->port_usb)			break;	} 	if (do_tty_wake)		tty_port_tty_wakeup(&port->port);	return status;} /* * Context: caller owns port_lock, and port_usb is set */static unsigned gs_start_rx(struct gs_port *port)/*__releases(&port->port_lock)__acquires(&port->port_lock)*/{	struct list_head	*pool = &port->read_pool;	struct usb_ep		*out = port->port_usb->out; 	while (!list_empty(pool)) {		struct usb_request	*req;		int			status;		struct tty_struct	*tty; 		/* no more rx if closed */		tty = port->port.tty;		if (!tty)			break; 		if (port->read_started >= QUEUE_SIZE)			break; 		req = list_entry(pool->next, struct usb_request, list);		list_del(&req->list);		req->length = out->maxpacket; 		/* drop lock while we call out; the controller driver		 * may need to call us back (e.g. for disconnect)		 */		spin_unlock(&port->port_lock);		status = usb_ep_queue(out, req, GFP_ATOMIC);		spin_lock(&port->port_lock); 		if (status) {			pr_debug("%s: %s %s err %d\n",					__func__, "queue", out->name, status);			list_add(&req->list, pool);			break;		}		port->read_started++; 		/* abort immediately after disconnect */		if (!port->port_usb)			break;	}	return port->read_started;} /* * RX work takes data out of the RX queue and hands it up to the TTY * layer until it refuses to take any more data (or is throttled back). * Then it issues reads for any further data. * * If the RX queue becomes full enough that no usb_request is queued, * the OUT endpoint may begin NAKing as soon as its FIFO fills up. * So QUEUE_SIZE packets plus however many the FIFO holds (usually two) * can be buffered before the TTY layer's buffers (currently 64 KB). */static void gs_rx_push(struct work_struct *work){	struct delayed_work	*w = to_delayed_work(work);	struct gs_port		*port = container_of(w, struct gs_port, push);	struct tty_struct	*tty;	struct list_head	*queue = &port->read_queue;	bool			disconnect = false;	bool			do_push = false; 	/* hand any queued data to the tty */	spin_lock_irq(&port->port_lock);	tty = port->port.tty;	while (!list_empty(queue)) {		struct usb_request	*req; 		req = list_first_entry(queue, struct usb_request, list); 		/* leave data queued if tty was rx throttled */		if (tty && tty_throttled(tty))			break; 		switch (req->status) {		case -ESHUTDOWN:			disconnect = true;			pr_vdebug("ttyGS%d: shutdown\n", port->port_num);			break; 		default:			/* presumably a transient fault */			pr_warn("ttyGS%d: unexpected RX status %d\n",				port->port_num, req->status);			fallthrough;		case 0:			/* normal completion */			break;		} 		/* push data to (open) tty */		if (req->actual && tty) {			char		*packet = req->buf;			unsigned	size = req->actual;			unsigned	n;			int		count; 			/* we may have pushed part of this packet already... */			n = port->n_read;			if (n) {				packet += n;				size -= n;			} 			port->icount.rx += size;			count = tty_insert_flip_string(&port->port, packet,					size);			if (count)				do_push = true;			if (count != size) {				/* stop pushing; TTY layer can't handle more */				port->n_read += count;				pr_vdebug("ttyGS%d: rx block %d/%d\n",					  port->port_num, count, req->actual);				break;			}			port->n_read = 0;		} 		list_move(&req->list, &port->read_pool);		port->read_started--;	} 	/* Push from tty to ldisc; this is handled by a workqueue,	 * so we won't get callbacks and can hold port_lock	 */	if (do_push)		tty_flip_buffer_push(&port->port);  	/* We want our data queue to become empty ASAP, keeping data	 * in the tty and ldisc (not here).  If we couldn't push any	 * this time around, RX may be starved, so wait until next jiffy.	 *	 * We may leave non-empty queue only when there is a tty, and	 * either it is throttled or there is no more room in flip buffer.	 */	if (!list_empty(queue) && !tty_throttled(tty))		schedule_delayed_work(&port->push, 1); 	/* If we're still connected, refill the USB RX queue. */	if (!disconnect && port->port_usb)		gs_start_rx(port); 	spin_unlock_irq(&port->port_lock);} static void gs_read_complete(struct usb_ep *ep, struct usb_request *req){	struct gs_port	*port = ep->driver_data; 	/* Queue all received data until the tty layer is ready for it. */	spin_lock(&port->port_lock);	list_add_tail(&req->list, &port->read_queue);	schedule_delayed_work(&port->push, 0);	spin_unlock(&port->port_lock);} static void gs_write_complete(struct usb_ep *ep, struct usb_request *req){	struct gs_port	*port = ep->driver_data; 	spin_lock(&port->port_lock);	list_add(&req->list, &port->write_pool);	port->write_started--; 	switch (req->status) {	default:		/* presumably a transient fault */		pr_warn("%s: unexpected %s status %d\n",			__func__, ep->name, req->status);		fallthrough;	case 0:		/* normal completion */		gs_start_tx(port);		break; 	case -ESHUTDOWN:		/* disconnect */		pr_vdebug("%s: %s shutdown\n", __func__, ep->name);		break;	} 	spin_unlock(&port->port_lock);} static void gs_free_requests(struct usb_ep *ep, struct list_head *head,							 int *allocated){	struct usb_request	*req;

Lines 1–500 of 1607.