Greg Kroah-HartmanMerge tag 'v7.1-rc6' into usb-next55311a95 months agoHistory
3194 lines · 90.0 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0+/* * (C) Copyright Linus Torvalds 1999 * (C) Copyright Johannes Erdfelt 1999-2001 * (C) Copyright Andreas Gal 1999 * (C) Copyright Gregory P. Smith 1999 * (C) Copyright Deti Fliegl 1999 * (C) Copyright Randy Dunlap 2000 * (C) Copyright David Brownell 2000-2002 */ #include <linux/bcd.h>#include <linux/module.h>#include <linux/version.h>#include <linux/kernel.h>#include <linux/sched/task_stack.h>#include <linux/slab.h>#include <linux/completion.h>#include <linux/utsname.h>#include <linux/mm.h>#include <asm/io.h>#include <linux/device.h>#include <linux/dma-mapping.h>#include <linux/mutex.h>#include <asm/irq.h>#include <asm/byteorder.h>#include <linux/unaligned.h>#include <linux/platform_device.h>#include <linux/workqueue.h>#include <linux/pm_runtime.h>#include <linux/types.h>#include <linux/genalloc.h>#include <linux/io.h>#include <linux/kcov.h> #include <linux/phy/phy.h>#include <linux/usb.h>#include <linux/usb/hcd.h>#include <linux/usb/otg.h> #include "usb.h"#include "phy.h"  /*-------------------------------------------------------------------------*/ /* * USB Host Controller Driver framework * * Plugs into usbcore (usb_bus) and lets HCDs share code, minimizing * HCD-specific behaviors/bugs. * * This does error checks, tracks devices and urbs, and delegates to a * "hc_driver" only for code (and data) that really needs to know about * hardware differences.  That includes root hub registers, i/o queues, * and so on ... but as little else as possible. * * Shared code includes most of the "root hub" code (these are emulated, * though each HC's hardware works differently) and PCI glue, plus request * tracking overhead.  The HCD code should only block on spinlocks or on * hardware handshaking; blocking on software events (such as other kernel * threads releasing resources, or completing actions) is all generic. * * Happens the USB 2.0 spec says this would be invisible inside the "USBD", * and includes mostly a "HCDI" (HCD Interface) along with some APIs used * only by the hub driver ... and that neither should be seen or used by * usb client device drivers. * * Contributors of ideas or unattributed patches include: David Brownell, * Roman Weissgaerber, Rory Bolt, Greg Kroah-Hartman, ... * * HISTORY: * 2002-02-21	Pull in most of the usb_bus support from usb.c; some *		associated cleanup.  "usb_hcd" still != "usb_bus". * 2001-12-12	Initial patch version for Linux 2.5.1 kernel. */ /*-------------------------------------------------------------------------*/ /* host controllers we manage */DEFINE_IDR (usb_bus_idr);EXPORT_SYMBOL_GPL (usb_bus_idr); /* used when allocating bus numbers */#define USB_MAXBUS		64 /* used when updating list of hcds */DEFINE_MUTEX(usb_bus_idr_lock);	/* exported only for usbfs */EXPORT_SYMBOL_GPL (usb_bus_idr_lock); /* used for controlling access to virtual root hubs */static DEFINE_SPINLOCK(hcd_root_hub_lock); /* used when updating an endpoint's URB list */static DEFINE_SPINLOCK(hcd_urb_list_lock); /* used to protect against unlinking URBs after the device is gone */static DEFINE_SPINLOCK(hcd_urb_unlink_lock); /* wait queue for synchronous unlinks */DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue); /*-------------------------------------------------------------------------*/ /* * Sharable chunks of root hub code. */ /*-------------------------------------------------------------------------*/#define KERNEL_REL	bin2bcd(LINUX_VERSION_MAJOR)#define KERNEL_VER	bin2bcd(LINUX_VERSION_PATCHLEVEL) /* usb 3.1 root hub device descriptor */static const u8 usb31_rh_dev_descriptor[18] = {	0x12,       /*  __u8  bLength; */	USB_DT_DEVICE, /* __u8 bDescriptorType; Device */	0x10, 0x03, /*  __le16 bcdUSB; v3.1 */ 	0x09,	    /*  __u8  bDeviceClass; HUB_CLASSCODE */	0x00,	    /*  __u8  bDeviceSubClass; */	0x03,       /*  __u8  bDeviceProtocol; USB 3 hub */	0x09,       /*  __u8  bMaxPacketSize0; 2^9 = 512 Bytes */ 	0x6b, 0x1d, /*  __le16 idVendor; Linux Foundation 0x1d6b */	0x03, 0x00, /*  __le16 idProduct; device 0x0003 */	KERNEL_VER, KERNEL_REL, /*  __le16 bcdDevice */ 	0x03,       /*  __u8  iManufacturer; */	0x02,       /*  __u8  iProduct; */	0x01,       /*  __u8  iSerialNumber; */	0x01        /*  __u8  bNumConfigurations; */}; /* usb 3.0 root hub device descriptor */static const u8 usb3_rh_dev_descriptor[18] = {	0x12,       /*  __u8  bLength; */	USB_DT_DEVICE, /* __u8 bDescriptorType; Device */	0x00, 0x03, /*  __le16 bcdUSB; v3.0 */ 	0x09,	    /*  __u8  bDeviceClass; HUB_CLASSCODE */	0x00,	    /*  __u8  bDeviceSubClass; */	0x03,       /*  __u8  bDeviceProtocol; USB 3.0 hub */	0x09,       /*  __u8  bMaxPacketSize0; 2^9 = 512 Bytes */ 	0x6b, 0x1d, /*  __le16 idVendor; Linux Foundation 0x1d6b */	0x03, 0x00, /*  __le16 idProduct; device 0x0003 */	KERNEL_VER, KERNEL_REL, /*  __le16 bcdDevice */ 	0x03,       /*  __u8  iManufacturer; */	0x02,       /*  __u8  iProduct; */	0x01,       /*  __u8  iSerialNumber; */	0x01        /*  __u8  bNumConfigurations; */}; /* usb 2.0 root hub device descriptor */static const u8 usb2_rh_dev_descriptor[18] = {	0x12,       /*  __u8  bLength; */	USB_DT_DEVICE, /* __u8 bDescriptorType; Device */	0x00, 0x02, /*  __le16 bcdUSB; v2.0 */ 	0x09,	    /*  __u8  bDeviceClass; HUB_CLASSCODE */	0x00,	    /*  __u8  bDeviceSubClass; */	0x00,       /*  __u8  bDeviceProtocol; [ usb 2.0 no TT ] */	0x40,       /*  __u8  bMaxPacketSize0; 64 Bytes */ 	0x6b, 0x1d, /*  __le16 idVendor; Linux Foundation 0x1d6b */	0x02, 0x00, /*  __le16 idProduct; device 0x0002 */	KERNEL_VER, KERNEL_REL, /*  __le16 bcdDevice */ 	0x03,       /*  __u8  iManufacturer; */	0x02,       /*  __u8  iProduct; */	0x01,       /*  __u8  iSerialNumber; */	0x01        /*  __u8  bNumConfigurations; */}; /* no usb 2.0 root hub "device qualifier" descriptor: one speed only */ /* usb 1.1 root hub device descriptor */static const u8 usb11_rh_dev_descriptor[18] = {	0x12,       /*  __u8  bLength; */	USB_DT_DEVICE, /* __u8 bDescriptorType; Device */	0x10, 0x01, /*  __le16 bcdUSB; v1.1 */ 	0x09,	    /*  __u8  bDeviceClass; HUB_CLASSCODE */	0x00,	    /*  __u8  bDeviceSubClass; */	0x00,       /*  __u8  bDeviceProtocol; [ low/full speeds only ] */	0x40,       /*  __u8  bMaxPacketSize0; 64 Bytes */ 	0x6b, 0x1d, /*  __le16 idVendor; Linux Foundation 0x1d6b */	0x01, 0x00, /*  __le16 idProduct; device 0x0001 */	KERNEL_VER, KERNEL_REL, /*  __le16 bcdDevice */ 	0x03,       /*  __u8  iManufacturer; */	0x02,       /*  __u8  iProduct; */	0x01,       /*  __u8  iSerialNumber; */	0x01        /*  __u8  bNumConfigurations; */};  /*-------------------------------------------------------------------------*/ /* Configuration descriptors for our root hubs */ static const u8 fs_rh_config_descriptor[] = { 	/* one configuration */	0x09,       /*  __u8  bLength; */	USB_DT_CONFIG, /* __u8 bDescriptorType; Configuration */	0x19, 0x00, /*  __le16 wTotalLength; */	0x01,       /*  __u8  bNumInterfaces; (1) */	0x01,       /*  __u8  bConfigurationValue; */	0x00,       /*  __u8  iConfiguration; */	0xc0,       /*  __u8  bmAttributes;				 Bit 7: must be set,				     6: Self-powered,				     5: Remote wakeup,				     4..0: resvd */	0x00,       /*  __u8  MaxPower; */ 	/* USB 1.1:	 * USB 2.0, single TT organization (mandatory):	 *	one interface, protocol 0	 *	 * USB 2.0, multiple TT organization (optional):	 *	two interfaces, protocols 1 (like single TT)	 *	and 2 (multiple TT mode) ... config is	 *	sometimes settable	 *	NOT IMPLEMENTED	 */ 	/* one interface */	0x09,       /*  __u8  if_bLength; */	USB_DT_INTERFACE,  /* __u8 if_bDescriptorType; Interface */	0x00,       /*  __u8  if_bInterfaceNumber; */	0x00,       /*  __u8  if_bAlternateSetting; */	0x01,       /*  __u8  if_bNumEndpoints; */	0x09,       /*  __u8  if_bInterfaceClass; HUB_CLASSCODE */	0x00,       /*  __u8  if_bInterfaceSubClass; */	0x00,       /*  __u8  if_bInterfaceProtocol; [usb1.1 or single tt] */	0x00,       /*  __u8  if_iInterface; */ 	/* one endpoint (status change endpoint) */	0x07,       /*  __u8  ep_bLength; */	USB_DT_ENDPOINT, /* __u8 ep_bDescriptorType; Endpoint */	0x81,       /*  __u8  ep_bEndpointAddress; IN Endpoint 1 */	0x03,       /*  __u8  ep_bmAttributes; Interrupt */	0x02, 0x00, /*  __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */	0xff        /*  __u8  ep_bInterval; (255ms -- usb 2.0 spec) */}; static const u8 hs_rh_config_descriptor[] = { 	/* one configuration */	0x09,       /*  __u8  bLength; */	USB_DT_CONFIG, /* __u8 bDescriptorType; Configuration */	0x19, 0x00, /*  __le16 wTotalLength; */	0x01,       /*  __u8  bNumInterfaces; (1) */	0x01,       /*  __u8  bConfigurationValue; */	0x00,       /*  __u8  iConfiguration; */	0xc0,       /*  __u8  bmAttributes;				 Bit 7: must be set,				     6: Self-powered,				     5: Remote wakeup,				     4..0: resvd */	0x00,       /*  __u8  MaxPower; */ 	/* USB 1.1:	 * USB 2.0, single TT organization (mandatory):	 *	one interface, protocol 0	 *	 * USB 2.0, multiple TT organization (optional):	 *	two interfaces, protocols 1 (like single TT)	 *	and 2 (multiple TT mode) ... config is	 *	sometimes settable	 *	NOT IMPLEMENTED	 */ 	/* one interface */	0x09,       /*  __u8  if_bLength; */	USB_DT_INTERFACE, /* __u8 if_bDescriptorType; Interface */	0x00,       /*  __u8  if_bInterfaceNumber; */	0x00,       /*  __u8  if_bAlternateSetting; */	0x01,       /*  __u8  if_bNumEndpoints; */	0x09,       /*  __u8  if_bInterfaceClass; HUB_CLASSCODE */	0x00,       /*  __u8  if_bInterfaceSubClass; */	0x00,       /*  __u8  if_bInterfaceProtocol; [usb1.1 or single tt] */	0x00,       /*  __u8  if_iInterface; */ 	/* one endpoint (status change endpoint) */	0x07,       /*  __u8  ep_bLength; */	USB_DT_ENDPOINT, /* __u8 ep_bDescriptorType; Endpoint */	0x81,       /*  __u8  ep_bEndpointAddress; IN Endpoint 1 */	0x03,       /*  __u8  ep_bmAttributes; Interrupt */		    /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8)		     * see hub.c:hub_configure() for details. */	(USB_MAXCHILDREN + 1 + 7) / 8, 0x00,	0x0c        /*  __u8  ep_bInterval; (256ms -- usb 2.0 spec) */}; static const u8 ss_rh_config_descriptor[] = {	/* one configuration */	0x09,       /*  __u8  bLength; */	USB_DT_CONFIG, /* __u8 bDescriptorType; Configuration */	0x1f, 0x00, /*  __le16 wTotalLength; */	0x01,       /*  __u8  bNumInterfaces; (1) */	0x01,       /*  __u8  bConfigurationValue; */	0x00,       /*  __u8  iConfiguration; */	0xc0,       /*  __u8  bmAttributes;				 Bit 7: must be set,				     6: Self-powered,				     5: Remote wakeup,				     4..0: resvd */	0x00,       /*  __u8  MaxPower; */ 	/* one interface */	0x09,       /*  __u8  if_bLength; */	USB_DT_INTERFACE, /* __u8 if_bDescriptorType; Interface */	0x00,       /*  __u8  if_bInterfaceNumber; */	0x00,       /*  __u8  if_bAlternateSetting; */	0x01,       /*  __u8  if_bNumEndpoints; */	0x09,       /*  __u8  if_bInterfaceClass; HUB_CLASSCODE */	0x00,       /*  __u8  if_bInterfaceSubClass; */	0x00,       /*  __u8  if_bInterfaceProtocol; */	0x00,       /*  __u8  if_iInterface; */ 	/* one endpoint (status change endpoint) */	0x07,       /*  __u8  ep_bLength; */	USB_DT_ENDPOINT, /* __u8 ep_bDescriptorType; Endpoint */	0x81,       /*  __u8  ep_bEndpointAddress; IN Endpoint 1 */	0x03,       /*  __u8  ep_bmAttributes; Interrupt */	0x02, 0x00, /* __le16 ep_wMaxPacketSize; 2 bytes per USB3 10.15.1 */	0x0c,       /*  __u8  ep_bInterval; (256ms -- usb 2.0 spec) */ 	/* one SuperSpeed endpoint companion descriptor */	0x06,        /* __u8 ss_bLength */	USB_DT_SS_ENDPOINT_COMP, /* __u8 ss_bDescriptorType; SuperSpeed EP */		     /* Companion */	0x00,        /* __u8 ss_bMaxBurst; allows 1 TX between ACKs */	0x00,        /* __u8 ss_bmAttributes; 1 packet per service interval */	0x02, 0x00   /* __le16 ss_wBytesPerInterval; 15 bits for max 15 ports */}; /* authorized_default behaviour: * -1 is authorized for all devices (leftover from wireless USB) * 0 is unauthorized for all devices * 1 is authorized for all devices * 2 is authorized for internal devices */#define USB_AUTHORIZE_WIRED	-1#define USB_AUTHORIZE_NONE	0#define USB_AUTHORIZE_ALL	1#define USB_AUTHORIZE_INTERNAL	2 static int authorized_default = CONFIG_USB_DEFAULT_AUTHORIZATION_MODE;module_param(authorized_default, int, S_IRUGO|S_IWUSR);MODULE_PARM_DESC(authorized_default,		"Default USB device authorization: 0 is not authorized, 1 is authorized (default), 2 is authorized for internal devices, -1 is authorized (same as 1)");/*-------------------------------------------------------------------------*/ /** * ascii2desc() - Helper routine for producing UTF-16LE string descriptors * @s: Null-terminated ASCII (actually ISO-8859-1) string * @buf: Buffer for USB string descriptor (header + UTF-16LE) * @len: Length (in bytes; may be odd) of descriptor buffer. * * Return: The number of bytes filled in: 2 + 2*strlen(s) or @len, * whichever is less. * * Note: * USB String descriptors can contain at most 126 characters; input * strings longer than that are truncated. */static unsignedascii2desc(char const *s, u8 *buf, unsigned len){	unsigned n, t = 2 + 2*strlen(s); 	if (t > 254)		t = 254;	/* Longest possible UTF string descriptor */	if (len > t)		len = t; 	t += USB_DT_STRING << 8;	/* Now t is first 16 bits to store */ 	n = len;	while (n--) {		*buf++ = t;		if (!n--)			break;		*buf++ = t >> 8;		t = (unsigned char)*s++;	}	return len;} /** * rh_string() - provides string descriptors for root hub * @id: the string ID number (0: langids, 1: serial #, 2: product, 3: vendor) * @hcd: the host controller for this root hub * @data: buffer for output packet * @len: length of the provided buffer * * Produces either a manufacturer, product or serial number string for the * virtual root hub device. * * Return: The number of bytes filled in: the length of the descriptor or * of the provided buffer, whichever is less. */static unsignedrh_string(int id, struct usb_hcd const *hcd, u8 *data, unsigned len){	char buf[160];	char const *s;	static char const langids[4] = {4, USB_DT_STRING, 0x09, 0x04}; 	/* language ids */	switch (id) {	case 0:		/* Array of LANGID codes (0x0409 is MSFT-speak for "en-us") */		/* See http://www.usb.org/developers/docs/USB_LANGIDs.pdf */		if (len > 4)			len = 4;		memcpy(data, langids, len);		return len;	case 1:		/* Serial number */		s = hcd->self.bus_name;		break;	case 2:		/* Product name */		s = hcd->product_desc;		break;	case 3:		/* Manufacturer */		snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname,			init_utsname()->release, hcd->driver->description);		s = buf;		break;	default:		/* Can't happen; caller guarantees it */		return 0;	} 	return ascii2desc(s, data, len);}  /* Root hub control transfers execute synchronously */static int rh_call_control(struct usb_hcd *hcd,		struct urb *urb, gfp_t mem_flags){	struct usb_ctrlrequest *cmd;	u16		typeReq, wValue, wIndex, wLength;	u8		*ubuf = urb->transfer_buffer;	unsigned	len = 0;	int		status;	u8		patch_wakeup = 0;	u8		patch_protocol = 0;	u16		tbuf_size;	u8		*tbuf = NULL;	const u8	*bufp; 	might_sleep(); 	spin_lock_irq(&hcd_root_hub_lock);	status = usb_hcd_link_urb_to_ep(hcd, urb);	spin_unlock_irq(&hcd_root_hub_lock);	if (status)		return status;	urb->hcpriv = hcd;	/* Indicate it's queued */ 	cmd = (struct usb_ctrlrequest *) urb->setup_packet;	typeReq  = (cmd->bRequestType << 8) | cmd->bRequest;	wValue   = le16_to_cpu (cmd->wValue);	wIndex   = le16_to_cpu (cmd->wIndex);	wLength  = le16_to_cpu (cmd->wLength); 	if (wLength > urb->transfer_buffer_length)		goto error; 	/*	 * tbuf should be at least as big as the	 * USB hub descriptor.	 */	tbuf_size = max_t(u16, sizeof(struct usb_hub_descriptor), wLength);	tbuf = kzalloc(tbuf_size, mem_flags);	if (!tbuf) {		status = -ENOMEM;		goto err_alloc;	} 	bufp = tbuf;  	urb->actual_length = 0;	switch (typeReq) { 	/* DEVICE REQUESTS */ 	/* The root hub's remote wakeup enable bit is implemented using

Lines 1–500 of 3194.