18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * (C) Copyright Linus Torvalds 1999 48c2ecf20Sopenharmony_ci * (C) Copyright Johannes Erdfelt 1999-2001 58c2ecf20Sopenharmony_ci * (C) Copyright Andreas Gal 1999 68c2ecf20Sopenharmony_ci * (C) Copyright Gregory P. Smith 1999 78c2ecf20Sopenharmony_ci * (C) Copyright Deti Fliegl 1999 88c2ecf20Sopenharmony_ci * (C) Copyright Randy Dunlap 2000 98c2ecf20Sopenharmony_ci * (C) Copyright David Brownell 2000-2002 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <linux/bcd.h> 138c2ecf20Sopenharmony_ci#include <linux/module.h> 148c2ecf20Sopenharmony_ci#include <linux/version.h> 158c2ecf20Sopenharmony_ci#include <linux/kernel.h> 168c2ecf20Sopenharmony_ci#include <linux/sched/task_stack.h> 178c2ecf20Sopenharmony_ci#include <linux/slab.h> 188c2ecf20Sopenharmony_ci#include <linux/completion.h> 198c2ecf20Sopenharmony_ci#include <linux/utsname.h> 208c2ecf20Sopenharmony_ci#include <linux/mm.h> 218c2ecf20Sopenharmony_ci#include <asm/io.h> 228c2ecf20Sopenharmony_ci#include <linux/device.h> 238c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h> 248c2ecf20Sopenharmony_ci#include <linux/mutex.h> 258c2ecf20Sopenharmony_ci#include <asm/irq.h> 268c2ecf20Sopenharmony_ci#include <asm/byteorder.h> 278c2ecf20Sopenharmony_ci#include <asm/unaligned.h> 288c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 298c2ecf20Sopenharmony_ci#include <linux/workqueue.h> 308c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h> 318c2ecf20Sopenharmony_ci#include <linux/types.h> 328c2ecf20Sopenharmony_ci#include <linux/genalloc.h> 338c2ecf20Sopenharmony_ci#include <linux/io.h> 348c2ecf20Sopenharmony_ci#include <linux/kcov.h> 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci#include <linux/phy/phy.h> 378c2ecf20Sopenharmony_ci#include <linux/usb.h> 388c2ecf20Sopenharmony_ci#include <linux/usb/hcd.h> 398c2ecf20Sopenharmony_ci#include <linux/usb/otg.h> 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci#include "usb.h" 428c2ecf20Sopenharmony_ci#include "phy.h" 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci/* 488c2ecf20Sopenharmony_ci * USB Host Controller Driver framework 498c2ecf20Sopenharmony_ci * 508c2ecf20Sopenharmony_ci * Plugs into usbcore (usb_bus) and lets HCDs share code, minimizing 518c2ecf20Sopenharmony_ci * HCD-specific behaviors/bugs. 528c2ecf20Sopenharmony_ci * 538c2ecf20Sopenharmony_ci * This does error checks, tracks devices and urbs, and delegates to a 548c2ecf20Sopenharmony_ci * "hc_driver" only for code (and data) that really needs to know about 558c2ecf20Sopenharmony_ci * hardware differences. That includes root hub registers, i/o queues, 568c2ecf20Sopenharmony_ci * and so on ... but as little else as possible. 578c2ecf20Sopenharmony_ci * 588c2ecf20Sopenharmony_ci * Shared code includes most of the "root hub" code (these are emulated, 598c2ecf20Sopenharmony_ci * though each HC's hardware works differently) and PCI glue, plus request 608c2ecf20Sopenharmony_ci * tracking overhead. The HCD code should only block on spinlocks or on 618c2ecf20Sopenharmony_ci * hardware handshaking; blocking on software events (such as other kernel 628c2ecf20Sopenharmony_ci * threads releasing resources, or completing actions) is all generic. 638c2ecf20Sopenharmony_ci * 648c2ecf20Sopenharmony_ci * Happens the USB 2.0 spec says this would be invisible inside the "USBD", 658c2ecf20Sopenharmony_ci * and includes mostly a "HCDI" (HCD Interface) along with some APIs used 668c2ecf20Sopenharmony_ci * only by the hub driver ... and that neither should be seen or used by 678c2ecf20Sopenharmony_ci * usb client device drivers. 688c2ecf20Sopenharmony_ci * 698c2ecf20Sopenharmony_ci * Contributors of ideas or unattributed patches include: David Brownell, 708c2ecf20Sopenharmony_ci * Roman Weissgaerber, Rory Bolt, Greg Kroah-Hartman, ... 718c2ecf20Sopenharmony_ci * 728c2ecf20Sopenharmony_ci * HISTORY: 738c2ecf20Sopenharmony_ci * 2002-02-21 Pull in most of the usb_bus support from usb.c; some 748c2ecf20Sopenharmony_ci * associated cleanup. "usb_hcd" still != "usb_bus". 758c2ecf20Sopenharmony_ci * 2001-12-12 Initial patch version for Linux 2.5.1 kernel. 768c2ecf20Sopenharmony_ci */ 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci/* Keep track of which host controller drivers are loaded */ 818c2ecf20Sopenharmony_ciunsigned long usb_hcds_loaded; 828c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_hcds_loaded); 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci/* host controllers we manage */ 858c2ecf20Sopenharmony_ciDEFINE_IDR (usb_bus_idr); 868c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL (usb_bus_idr); 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci/* used when allocating bus numbers */ 898c2ecf20Sopenharmony_ci#define USB_MAXBUS 64 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci/* used when updating list of hcds */ 928c2ecf20Sopenharmony_ciDEFINE_MUTEX(usb_bus_idr_lock); /* exported only for usbfs */ 938c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL (usb_bus_idr_lock); 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci/* used for controlling access to virtual root hubs */ 968c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(hcd_root_hub_lock); 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci/* used when updating an endpoint's URB list */ 998c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(hcd_urb_list_lock); 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci/* used to protect against unlinking URBs after the device is gone */ 1028c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(hcd_urb_unlink_lock); 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci/* wait queue for synchronous unlinks */ 1058c2ecf20Sopenharmony_ciDECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue); 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci/* 1108c2ecf20Sopenharmony_ci * Sharable chunks of root hub code. 1118c2ecf20Sopenharmony_ci */ 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 1148c2ecf20Sopenharmony_ci#define KERNEL_REL bin2bcd(((LINUX_VERSION_CODE >> 16) & 0x0ff)) 1158c2ecf20Sopenharmony_ci#define KERNEL_VER bin2bcd(((LINUX_VERSION_CODE >> 8) & 0x0ff)) 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci/* usb 3.1 root hub device descriptor */ 1188c2ecf20Sopenharmony_cistatic const u8 usb31_rh_dev_descriptor[18] = { 1198c2ecf20Sopenharmony_ci 0x12, /* __u8 bLength; */ 1208c2ecf20Sopenharmony_ci USB_DT_DEVICE, /* __u8 bDescriptorType; Device */ 1218c2ecf20Sopenharmony_ci 0x10, 0x03, /* __le16 bcdUSB; v3.1 */ 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ 1248c2ecf20Sopenharmony_ci 0x00, /* __u8 bDeviceSubClass; */ 1258c2ecf20Sopenharmony_ci 0x03, /* __u8 bDeviceProtocol; USB 3 hub */ 1268c2ecf20Sopenharmony_ci 0x09, /* __u8 bMaxPacketSize0; 2^9 = 512 Bytes */ 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */ 1298c2ecf20Sopenharmony_ci 0x03, 0x00, /* __le16 idProduct; device 0x0003 */ 1308c2ecf20Sopenharmony_ci KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */ 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci 0x03, /* __u8 iManufacturer; */ 1338c2ecf20Sopenharmony_ci 0x02, /* __u8 iProduct; */ 1348c2ecf20Sopenharmony_ci 0x01, /* __u8 iSerialNumber; */ 1358c2ecf20Sopenharmony_ci 0x01 /* __u8 bNumConfigurations; */ 1368c2ecf20Sopenharmony_ci}; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci/* usb 3.0 root hub device descriptor */ 1398c2ecf20Sopenharmony_cistatic const u8 usb3_rh_dev_descriptor[18] = { 1408c2ecf20Sopenharmony_ci 0x12, /* __u8 bLength; */ 1418c2ecf20Sopenharmony_ci USB_DT_DEVICE, /* __u8 bDescriptorType; Device */ 1428c2ecf20Sopenharmony_ci 0x00, 0x03, /* __le16 bcdUSB; v3.0 */ 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ 1458c2ecf20Sopenharmony_ci 0x00, /* __u8 bDeviceSubClass; */ 1468c2ecf20Sopenharmony_ci 0x03, /* __u8 bDeviceProtocol; USB 3.0 hub */ 1478c2ecf20Sopenharmony_ci 0x09, /* __u8 bMaxPacketSize0; 2^9 = 512 Bytes */ 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */ 1508c2ecf20Sopenharmony_ci 0x03, 0x00, /* __le16 idProduct; device 0x0003 */ 1518c2ecf20Sopenharmony_ci KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */ 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci 0x03, /* __u8 iManufacturer; */ 1548c2ecf20Sopenharmony_ci 0x02, /* __u8 iProduct; */ 1558c2ecf20Sopenharmony_ci 0x01, /* __u8 iSerialNumber; */ 1568c2ecf20Sopenharmony_ci 0x01 /* __u8 bNumConfigurations; */ 1578c2ecf20Sopenharmony_ci}; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci/* usb 2.5 (wireless USB 1.0) root hub device descriptor */ 1608c2ecf20Sopenharmony_cistatic const u8 usb25_rh_dev_descriptor[18] = { 1618c2ecf20Sopenharmony_ci 0x12, /* __u8 bLength; */ 1628c2ecf20Sopenharmony_ci USB_DT_DEVICE, /* __u8 bDescriptorType; Device */ 1638c2ecf20Sopenharmony_ci 0x50, 0x02, /* __le16 bcdUSB; v2.5 */ 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ 1668c2ecf20Sopenharmony_ci 0x00, /* __u8 bDeviceSubClass; */ 1678c2ecf20Sopenharmony_ci 0x00, /* __u8 bDeviceProtocol; [ usb 2.0 no TT ] */ 1688c2ecf20Sopenharmony_ci 0xFF, /* __u8 bMaxPacketSize0; always 0xFF (WUSB Spec 7.4.1). */ 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */ 1718c2ecf20Sopenharmony_ci 0x02, 0x00, /* __le16 idProduct; device 0x0002 */ 1728c2ecf20Sopenharmony_ci KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */ 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci 0x03, /* __u8 iManufacturer; */ 1758c2ecf20Sopenharmony_ci 0x02, /* __u8 iProduct; */ 1768c2ecf20Sopenharmony_ci 0x01, /* __u8 iSerialNumber; */ 1778c2ecf20Sopenharmony_ci 0x01 /* __u8 bNumConfigurations; */ 1788c2ecf20Sopenharmony_ci}; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci/* usb 2.0 root hub device descriptor */ 1818c2ecf20Sopenharmony_cistatic const u8 usb2_rh_dev_descriptor[18] = { 1828c2ecf20Sopenharmony_ci 0x12, /* __u8 bLength; */ 1838c2ecf20Sopenharmony_ci USB_DT_DEVICE, /* __u8 bDescriptorType; Device */ 1848c2ecf20Sopenharmony_ci 0x00, 0x02, /* __le16 bcdUSB; v2.0 */ 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ 1878c2ecf20Sopenharmony_ci 0x00, /* __u8 bDeviceSubClass; */ 1888c2ecf20Sopenharmony_ci 0x00, /* __u8 bDeviceProtocol; [ usb 2.0 no TT ] */ 1898c2ecf20Sopenharmony_ci 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */ 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */ 1928c2ecf20Sopenharmony_ci 0x02, 0x00, /* __le16 idProduct; device 0x0002 */ 1938c2ecf20Sopenharmony_ci KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */ 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci 0x03, /* __u8 iManufacturer; */ 1968c2ecf20Sopenharmony_ci 0x02, /* __u8 iProduct; */ 1978c2ecf20Sopenharmony_ci 0x01, /* __u8 iSerialNumber; */ 1988c2ecf20Sopenharmony_ci 0x01 /* __u8 bNumConfigurations; */ 1998c2ecf20Sopenharmony_ci}; 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci/* no usb 2.0 root hub "device qualifier" descriptor: one speed only */ 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci/* usb 1.1 root hub device descriptor */ 2048c2ecf20Sopenharmony_cistatic const u8 usb11_rh_dev_descriptor[18] = { 2058c2ecf20Sopenharmony_ci 0x12, /* __u8 bLength; */ 2068c2ecf20Sopenharmony_ci USB_DT_DEVICE, /* __u8 bDescriptorType; Device */ 2078c2ecf20Sopenharmony_ci 0x10, 0x01, /* __le16 bcdUSB; v1.1 */ 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ 2108c2ecf20Sopenharmony_ci 0x00, /* __u8 bDeviceSubClass; */ 2118c2ecf20Sopenharmony_ci 0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */ 2128c2ecf20Sopenharmony_ci 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */ 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */ 2158c2ecf20Sopenharmony_ci 0x01, 0x00, /* __le16 idProduct; device 0x0001 */ 2168c2ecf20Sopenharmony_ci KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */ 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci 0x03, /* __u8 iManufacturer; */ 2198c2ecf20Sopenharmony_ci 0x02, /* __u8 iProduct; */ 2208c2ecf20Sopenharmony_ci 0x01, /* __u8 iSerialNumber; */ 2218c2ecf20Sopenharmony_ci 0x01 /* __u8 bNumConfigurations; */ 2228c2ecf20Sopenharmony_ci}; 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci/* Configuration descriptors for our root hubs */ 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_cistatic const u8 fs_rh_config_descriptor[] = { 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci /* one configuration */ 2328c2ecf20Sopenharmony_ci 0x09, /* __u8 bLength; */ 2338c2ecf20Sopenharmony_ci USB_DT_CONFIG, /* __u8 bDescriptorType; Configuration */ 2348c2ecf20Sopenharmony_ci 0x19, 0x00, /* __le16 wTotalLength; */ 2358c2ecf20Sopenharmony_ci 0x01, /* __u8 bNumInterfaces; (1) */ 2368c2ecf20Sopenharmony_ci 0x01, /* __u8 bConfigurationValue; */ 2378c2ecf20Sopenharmony_ci 0x00, /* __u8 iConfiguration; */ 2388c2ecf20Sopenharmony_ci 0xc0, /* __u8 bmAttributes; 2398c2ecf20Sopenharmony_ci Bit 7: must be set, 2408c2ecf20Sopenharmony_ci 6: Self-powered, 2418c2ecf20Sopenharmony_ci 5: Remote wakeup, 2428c2ecf20Sopenharmony_ci 4..0: resvd */ 2438c2ecf20Sopenharmony_ci 0x00, /* __u8 MaxPower; */ 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci /* USB 1.1: 2468c2ecf20Sopenharmony_ci * USB 2.0, single TT organization (mandatory): 2478c2ecf20Sopenharmony_ci * one interface, protocol 0 2488c2ecf20Sopenharmony_ci * 2498c2ecf20Sopenharmony_ci * USB 2.0, multiple TT organization (optional): 2508c2ecf20Sopenharmony_ci * two interfaces, protocols 1 (like single TT) 2518c2ecf20Sopenharmony_ci * and 2 (multiple TT mode) ... config is 2528c2ecf20Sopenharmony_ci * sometimes settable 2538c2ecf20Sopenharmony_ci * NOT IMPLEMENTED 2548c2ecf20Sopenharmony_ci */ 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_ci /* one interface */ 2578c2ecf20Sopenharmony_ci 0x09, /* __u8 if_bLength; */ 2588c2ecf20Sopenharmony_ci USB_DT_INTERFACE, /* __u8 if_bDescriptorType; Interface */ 2598c2ecf20Sopenharmony_ci 0x00, /* __u8 if_bInterfaceNumber; */ 2608c2ecf20Sopenharmony_ci 0x00, /* __u8 if_bAlternateSetting; */ 2618c2ecf20Sopenharmony_ci 0x01, /* __u8 if_bNumEndpoints; */ 2628c2ecf20Sopenharmony_ci 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */ 2638c2ecf20Sopenharmony_ci 0x00, /* __u8 if_bInterfaceSubClass; */ 2648c2ecf20Sopenharmony_ci 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */ 2658c2ecf20Sopenharmony_ci 0x00, /* __u8 if_iInterface; */ 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci /* one endpoint (status change endpoint) */ 2688c2ecf20Sopenharmony_ci 0x07, /* __u8 ep_bLength; */ 2698c2ecf20Sopenharmony_ci USB_DT_ENDPOINT, /* __u8 ep_bDescriptorType; Endpoint */ 2708c2ecf20Sopenharmony_ci 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */ 2718c2ecf20Sopenharmony_ci 0x03, /* __u8 ep_bmAttributes; Interrupt */ 2728c2ecf20Sopenharmony_ci 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */ 2738c2ecf20Sopenharmony_ci 0xff /* __u8 ep_bInterval; (255ms -- usb 2.0 spec) */ 2748c2ecf20Sopenharmony_ci}; 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_cistatic const u8 hs_rh_config_descriptor[] = { 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_ci /* one configuration */ 2798c2ecf20Sopenharmony_ci 0x09, /* __u8 bLength; */ 2808c2ecf20Sopenharmony_ci USB_DT_CONFIG, /* __u8 bDescriptorType; Configuration */ 2818c2ecf20Sopenharmony_ci 0x19, 0x00, /* __le16 wTotalLength; */ 2828c2ecf20Sopenharmony_ci 0x01, /* __u8 bNumInterfaces; (1) */ 2838c2ecf20Sopenharmony_ci 0x01, /* __u8 bConfigurationValue; */ 2848c2ecf20Sopenharmony_ci 0x00, /* __u8 iConfiguration; */ 2858c2ecf20Sopenharmony_ci 0xc0, /* __u8 bmAttributes; 2868c2ecf20Sopenharmony_ci Bit 7: must be set, 2878c2ecf20Sopenharmony_ci 6: Self-powered, 2888c2ecf20Sopenharmony_ci 5: Remote wakeup, 2898c2ecf20Sopenharmony_ci 4..0: resvd */ 2908c2ecf20Sopenharmony_ci 0x00, /* __u8 MaxPower; */ 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci /* USB 1.1: 2938c2ecf20Sopenharmony_ci * USB 2.0, single TT organization (mandatory): 2948c2ecf20Sopenharmony_ci * one interface, protocol 0 2958c2ecf20Sopenharmony_ci * 2968c2ecf20Sopenharmony_ci * USB 2.0, multiple TT organization (optional): 2978c2ecf20Sopenharmony_ci * two interfaces, protocols 1 (like single TT) 2988c2ecf20Sopenharmony_ci * and 2 (multiple TT mode) ... config is 2998c2ecf20Sopenharmony_ci * sometimes settable 3008c2ecf20Sopenharmony_ci * NOT IMPLEMENTED 3018c2ecf20Sopenharmony_ci */ 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci /* one interface */ 3048c2ecf20Sopenharmony_ci 0x09, /* __u8 if_bLength; */ 3058c2ecf20Sopenharmony_ci USB_DT_INTERFACE, /* __u8 if_bDescriptorType; Interface */ 3068c2ecf20Sopenharmony_ci 0x00, /* __u8 if_bInterfaceNumber; */ 3078c2ecf20Sopenharmony_ci 0x00, /* __u8 if_bAlternateSetting; */ 3088c2ecf20Sopenharmony_ci 0x01, /* __u8 if_bNumEndpoints; */ 3098c2ecf20Sopenharmony_ci 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */ 3108c2ecf20Sopenharmony_ci 0x00, /* __u8 if_bInterfaceSubClass; */ 3118c2ecf20Sopenharmony_ci 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */ 3128c2ecf20Sopenharmony_ci 0x00, /* __u8 if_iInterface; */ 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci /* one endpoint (status change endpoint) */ 3158c2ecf20Sopenharmony_ci 0x07, /* __u8 ep_bLength; */ 3168c2ecf20Sopenharmony_ci USB_DT_ENDPOINT, /* __u8 ep_bDescriptorType; Endpoint */ 3178c2ecf20Sopenharmony_ci 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */ 3188c2ecf20Sopenharmony_ci 0x03, /* __u8 ep_bmAttributes; Interrupt */ 3198c2ecf20Sopenharmony_ci /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) 3208c2ecf20Sopenharmony_ci * see hub.c:hub_configure() for details. */ 3218c2ecf20Sopenharmony_ci (USB_MAXCHILDREN + 1 + 7) / 8, 0x00, 3228c2ecf20Sopenharmony_ci 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */ 3238c2ecf20Sopenharmony_ci}; 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_cistatic const u8 ss_rh_config_descriptor[] = { 3268c2ecf20Sopenharmony_ci /* one configuration */ 3278c2ecf20Sopenharmony_ci 0x09, /* __u8 bLength; */ 3288c2ecf20Sopenharmony_ci USB_DT_CONFIG, /* __u8 bDescriptorType; Configuration */ 3298c2ecf20Sopenharmony_ci 0x1f, 0x00, /* __le16 wTotalLength; */ 3308c2ecf20Sopenharmony_ci 0x01, /* __u8 bNumInterfaces; (1) */ 3318c2ecf20Sopenharmony_ci 0x01, /* __u8 bConfigurationValue; */ 3328c2ecf20Sopenharmony_ci 0x00, /* __u8 iConfiguration; */ 3338c2ecf20Sopenharmony_ci 0xc0, /* __u8 bmAttributes; 3348c2ecf20Sopenharmony_ci Bit 7: must be set, 3358c2ecf20Sopenharmony_ci 6: Self-powered, 3368c2ecf20Sopenharmony_ci 5: Remote wakeup, 3378c2ecf20Sopenharmony_ci 4..0: resvd */ 3388c2ecf20Sopenharmony_ci 0x00, /* __u8 MaxPower; */ 3398c2ecf20Sopenharmony_ci 3408c2ecf20Sopenharmony_ci /* one interface */ 3418c2ecf20Sopenharmony_ci 0x09, /* __u8 if_bLength; */ 3428c2ecf20Sopenharmony_ci USB_DT_INTERFACE, /* __u8 if_bDescriptorType; Interface */ 3438c2ecf20Sopenharmony_ci 0x00, /* __u8 if_bInterfaceNumber; */ 3448c2ecf20Sopenharmony_ci 0x00, /* __u8 if_bAlternateSetting; */ 3458c2ecf20Sopenharmony_ci 0x01, /* __u8 if_bNumEndpoints; */ 3468c2ecf20Sopenharmony_ci 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */ 3478c2ecf20Sopenharmony_ci 0x00, /* __u8 if_bInterfaceSubClass; */ 3488c2ecf20Sopenharmony_ci 0x00, /* __u8 if_bInterfaceProtocol; */ 3498c2ecf20Sopenharmony_ci 0x00, /* __u8 if_iInterface; */ 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci /* one endpoint (status change endpoint) */ 3528c2ecf20Sopenharmony_ci 0x07, /* __u8 ep_bLength; */ 3538c2ecf20Sopenharmony_ci USB_DT_ENDPOINT, /* __u8 ep_bDescriptorType; Endpoint */ 3548c2ecf20Sopenharmony_ci 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */ 3558c2ecf20Sopenharmony_ci 0x03, /* __u8 ep_bmAttributes; Interrupt */ 3568c2ecf20Sopenharmony_ci /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) 3578c2ecf20Sopenharmony_ci * see hub.c:hub_configure() for details. */ 3588c2ecf20Sopenharmony_ci (USB_MAXCHILDREN + 1 + 7) / 8, 0x00, 3598c2ecf20Sopenharmony_ci 0x0c, /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */ 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_ci /* one SuperSpeed endpoint companion descriptor */ 3628c2ecf20Sopenharmony_ci 0x06, /* __u8 ss_bLength */ 3638c2ecf20Sopenharmony_ci USB_DT_SS_ENDPOINT_COMP, /* __u8 ss_bDescriptorType; SuperSpeed EP */ 3648c2ecf20Sopenharmony_ci /* Companion */ 3658c2ecf20Sopenharmony_ci 0x00, /* __u8 ss_bMaxBurst; allows 1 TX between ACKs */ 3668c2ecf20Sopenharmony_ci 0x00, /* __u8 ss_bmAttributes; 1 packet per service interval */ 3678c2ecf20Sopenharmony_ci 0x02, 0x00 /* __le16 ss_wBytesPerInterval; 15 bits for max 15 ports */ 3688c2ecf20Sopenharmony_ci}; 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci/* authorized_default behaviour: 3718c2ecf20Sopenharmony_ci * -1 is authorized for all devices except wireless (old behaviour) 3728c2ecf20Sopenharmony_ci * 0 is unauthorized for all devices 3738c2ecf20Sopenharmony_ci * 1 is authorized for all devices 3748c2ecf20Sopenharmony_ci * 2 is authorized for internal devices 3758c2ecf20Sopenharmony_ci */ 3768c2ecf20Sopenharmony_ci#define USB_AUTHORIZE_WIRED -1 3778c2ecf20Sopenharmony_ci#define USB_AUTHORIZE_NONE 0 3788c2ecf20Sopenharmony_ci#define USB_AUTHORIZE_ALL 1 3798c2ecf20Sopenharmony_ci#define USB_AUTHORIZE_INTERNAL 2 3808c2ecf20Sopenharmony_ci 3818c2ecf20Sopenharmony_cistatic int authorized_default = USB_AUTHORIZE_WIRED; 3828c2ecf20Sopenharmony_cimodule_param(authorized_default, int, S_IRUGO|S_IWUSR); 3838c2ecf20Sopenharmony_ciMODULE_PARM_DESC(authorized_default, 3848c2ecf20Sopenharmony_ci "Default USB device authorization: 0 is not authorized, 1 is " 3858c2ecf20Sopenharmony_ci "authorized, 2 is authorized for internal devices, -1 is " 3868c2ecf20Sopenharmony_ci "authorized except for wireless USB (default, old behaviour)"); 3878c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ci/** 3908c2ecf20Sopenharmony_ci * ascii2desc() - Helper routine for producing UTF-16LE string descriptors 3918c2ecf20Sopenharmony_ci * @s: Null-terminated ASCII (actually ISO-8859-1) string 3928c2ecf20Sopenharmony_ci * @buf: Buffer for USB string descriptor (header + UTF-16LE) 3938c2ecf20Sopenharmony_ci * @len: Length (in bytes; may be odd) of descriptor buffer. 3948c2ecf20Sopenharmony_ci * 3958c2ecf20Sopenharmony_ci * Return: The number of bytes filled in: 2 + 2*strlen(s) or @len, 3968c2ecf20Sopenharmony_ci * whichever is less. 3978c2ecf20Sopenharmony_ci * 3988c2ecf20Sopenharmony_ci * Note: 3998c2ecf20Sopenharmony_ci * USB String descriptors can contain at most 126 characters; input 4008c2ecf20Sopenharmony_ci * strings longer than that are truncated. 4018c2ecf20Sopenharmony_ci */ 4028c2ecf20Sopenharmony_cistatic unsigned 4038c2ecf20Sopenharmony_ciascii2desc(char const *s, u8 *buf, unsigned len) 4048c2ecf20Sopenharmony_ci{ 4058c2ecf20Sopenharmony_ci unsigned n, t = 2 + 2*strlen(s); 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_ci if (t > 254) 4088c2ecf20Sopenharmony_ci t = 254; /* Longest possible UTF string descriptor */ 4098c2ecf20Sopenharmony_ci if (len > t) 4108c2ecf20Sopenharmony_ci len = t; 4118c2ecf20Sopenharmony_ci 4128c2ecf20Sopenharmony_ci t += USB_DT_STRING << 8; /* Now t is first 16 bits to store */ 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci n = len; 4158c2ecf20Sopenharmony_ci while (n--) { 4168c2ecf20Sopenharmony_ci *buf++ = t; 4178c2ecf20Sopenharmony_ci if (!n--) 4188c2ecf20Sopenharmony_ci break; 4198c2ecf20Sopenharmony_ci *buf++ = t >> 8; 4208c2ecf20Sopenharmony_ci t = (unsigned char)*s++; 4218c2ecf20Sopenharmony_ci } 4228c2ecf20Sopenharmony_ci return len; 4238c2ecf20Sopenharmony_ci} 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_ci/** 4268c2ecf20Sopenharmony_ci * rh_string() - provides string descriptors for root hub 4278c2ecf20Sopenharmony_ci * @id: the string ID number (0: langids, 1: serial #, 2: product, 3: vendor) 4288c2ecf20Sopenharmony_ci * @hcd: the host controller for this root hub 4298c2ecf20Sopenharmony_ci * @data: buffer for output packet 4308c2ecf20Sopenharmony_ci * @len: length of the provided buffer 4318c2ecf20Sopenharmony_ci * 4328c2ecf20Sopenharmony_ci * Produces either a manufacturer, product or serial number string for the 4338c2ecf20Sopenharmony_ci * virtual root hub device. 4348c2ecf20Sopenharmony_ci * 4358c2ecf20Sopenharmony_ci * Return: The number of bytes filled in: the length of the descriptor or 4368c2ecf20Sopenharmony_ci * of the provided buffer, whichever is less. 4378c2ecf20Sopenharmony_ci */ 4388c2ecf20Sopenharmony_cistatic unsigned 4398c2ecf20Sopenharmony_cirh_string(int id, struct usb_hcd const *hcd, u8 *data, unsigned len) 4408c2ecf20Sopenharmony_ci{ 4418c2ecf20Sopenharmony_ci char buf[100]; 4428c2ecf20Sopenharmony_ci char const *s; 4438c2ecf20Sopenharmony_ci static char const langids[4] = {4, USB_DT_STRING, 0x09, 0x04}; 4448c2ecf20Sopenharmony_ci 4458c2ecf20Sopenharmony_ci /* language ids */ 4468c2ecf20Sopenharmony_ci switch (id) { 4478c2ecf20Sopenharmony_ci case 0: 4488c2ecf20Sopenharmony_ci /* Array of LANGID codes (0x0409 is MSFT-speak for "en-us") */ 4498c2ecf20Sopenharmony_ci /* See http://www.usb.org/developers/docs/USB_LANGIDs.pdf */ 4508c2ecf20Sopenharmony_ci if (len > 4) 4518c2ecf20Sopenharmony_ci len = 4; 4528c2ecf20Sopenharmony_ci memcpy(data, langids, len); 4538c2ecf20Sopenharmony_ci return len; 4548c2ecf20Sopenharmony_ci case 1: 4558c2ecf20Sopenharmony_ci /* Serial number */ 4568c2ecf20Sopenharmony_ci s = hcd->self.bus_name; 4578c2ecf20Sopenharmony_ci break; 4588c2ecf20Sopenharmony_ci case 2: 4598c2ecf20Sopenharmony_ci /* Product name */ 4608c2ecf20Sopenharmony_ci s = hcd->product_desc; 4618c2ecf20Sopenharmony_ci break; 4628c2ecf20Sopenharmony_ci case 3: 4638c2ecf20Sopenharmony_ci /* Manufacturer */ 4648c2ecf20Sopenharmony_ci snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname, 4658c2ecf20Sopenharmony_ci init_utsname()->release, hcd->driver->description); 4668c2ecf20Sopenharmony_ci s = buf; 4678c2ecf20Sopenharmony_ci break; 4688c2ecf20Sopenharmony_ci default: 4698c2ecf20Sopenharmony_ci /* Can't happen; caller guarantees it */ 4708c2ecf20Sopenharmony_ci return 0; 4718c2ecf20Sopenharmony_ci } 4728c2ecf20Sopenharmony_ci 4738c2ecf20Sopenharmony_ci return ascii2desc(s, data, len); 4748c2ecf20Sopenharmony_ci} 4758c2ecf20Sopenharmony_ci 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_ci/* Root hub control transfers execute synchronously */ 4788c2ecf20Sopenharmony_cistatic int rh_call_control (struct usb_hcd *hcd, struct urb *urb) 4798c2ecf20Sopenharmony_ci{ 4808c2ecf20Sopenharmony_ci struct usb_ctrlrequest *cmd; 4818c2ecf20Sopenharmony_ci u16 typeReq, wValue, wIndex, wLength; 4828c2ecf20Sopenharmony_ci u8 *ubuf = urb->transfer_buffer; 4838c2ecf20Sopenharmony_ci unsigned len = 0; 4848c2ecf20Sopenharmony_ci int status; 4858c2ecf20Sopenharmony_ci u8 patch_wakeup = 0; 4868c2ecf20Sopenharmony_ci u8 patch_protocol = 0; 4878c2ecf20Sopenharmony_ci u16 tbuf_size; 4888c2ecf20Sopenharmony_ci u8 *tbuf = NULL; 4898c2ecf20Sopenharmony_ci const u8 *bufp; 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_ci might_sleep(); 4928c2ecf20Sopenharmony_ci 4938c2ecf20Sopenharmony_ci spin_lock_irq(&hcd_root_hub_lock); 4948c2ecf20Sopenharmony_ci status = usb_hcd_link_urb_to_ep(hcd, urb); 4958c2ecf20Sopenharmony_ci spin_unlock_irq(&hcd_root_hub_lock); 4968c2ecf20Sopenharmony_ci if (status) 4978c2ecf20Sopenharmony_ci return status; 4988c2ecf20Sopenharmony_ci urb->hcpriv = hcd; /* Indicate it's queued */ 4998c2ecf20Sopenharmony_ci 5008c2ecf20Sopenharmony_ci cmd = (struct usb_ctrlrequest *) urb->setup_packet; 5018c2ecf20Sopenharmony_ci typeReq = (cmd->bRequestType << 8) | cmd->bRequest; 5028c2ecf20Sopenharmony_ci wValue = le16_to_cpu (cmd->wValue); 5038c2ecf20Sopenharmony_ci wIndex = le16_to_cpu (cmd->wIndex); 5048c2ecf20Sopenharmony_ci wLength = le16_to_cpu (cmd->wLength); 5058c2ecf20Sopenharmony_ci 5068c2ecf20Sopenharmony_ci if (wLength > urb->transfer_buffer_length) 5078c2ecf20Sopenharmony_ci goto error; 5088c2ecf20Sopenharmony_ci 5098c2ecf20Sopenharmony_ci /* 5108c2ecf20Sopenharmony_ci * tbuf should be at least as big as the 5118c2ecf20Sopenharmony_ci * USB hub descriptor. 5128c2ecf20Sopenharmony_ci */ 5138c2ecf20Sopenharmony_ci tbuf_size = max_t(u16, sizeof(struct usb_hub_descriptor), wLength); 5148c2ecf20Sopenharmony_ci tbuf = kzalloc(tbuf_size, GFP_KERNEL); 5158c2ecf20Sopenharmony_ci if (!tbuf) { 5168c2ecf20Sopenharmony_ci status = -ENOMEM; 5178c2ecf20Sopenharmony_ci goto err_alloc; 5188c2ecf20Sopenharmony_ci } 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_ci bufp = tbuf; 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ci 5238c2ecf20Sopenharmony_ci urb->actual_length = 0; 5248c2ecf20Sopenharmony_ci switch (typeReq) { 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_ci /* DEVICE REQUESTS */ 5278c2ecf20Sopenharmony_ci 5288c2ecf20Sopenharmony_ci /* The root hub's remote wakeup enable bit is implemented using 5298c2ecf20Sopenharmony_ci * driver model wakeup flags. If this system supports wakeup 5308c2ecf20Sopenharmony_ci * through USB, userspace may change the default "allow wakeup" 5318c2ecf20Sopenharmony_ci * policy through sysfs or these calls. 5328c2ecf20Sopenharmony_ci * 5338c2ecf20Sopenharmony_ci * Most root hubs support wakeup from downstream devices, for 5348c2ecf20Sopenharmony_ci * runtime power management (disabling USB clocks and reducing 5358c2ecf20Sopenharmony_ci * VBUS power usage). However, not all of them do so; silicon, 5368c2ecf20Sopenharmony_ci * board, and BIOS bugs here are not uncommon, so these can't 5378c2ecf20Sopenharmony_ci * be treated quite like external hubs. 5388c2ecf20Sopenharmony_ci * 5398c2ecf20Sopenharmony_ci * Likewise, not all root hubs will pass wakeup events upstream, 5408c2ecf20Sopenharmony_ci * to wake up the whole system. So don't assume root hub and 5418c2ecf20Sopenharmony_ci * controller capabilities are identical. 5428c2ecf20Sopenharmony_ci */ 5438c2ecf20Sopenharmony_ci 5448c2ecf20Sopenharmony_ci case DeviceRequest | USB_REQ_GET_STATUS: 5458c2ecf20Sopenharmony_ci tbuf[0] = (device_may_wakeup(&hcd->self.root_hub->dev) 5468c2ecf20Sopenharmony_ci << USB_DEVICE_REMOTE_WAKEUP) 5478c2ecf20Sopenharmony_ci | (1 << USB_DEVICE_SELF_POWERED); 5488c2ecf20Sopenharmony_ci tbuf[1] = 0; 5498c2ecf20Sopenharmony_ci len = 2; 5508c2ecf20Sopenharmony_ci break; 5518c2ecf20Sopenharmony_ci case DeviceOutRequest | USB_REQ_CLEAR_FEATURE: 5528c2ecf20Sopenharmony_ci if (wValue == USB_DEVICE_REMOTE_WAKEUP) 5538c2ecf20Sopenharmony_ci device_set_wakeup_enable(&hcd->self.root_hub->dev, 0); 5548c2ecf20Sopenharmony_ci else 5558c2ecf20Sopenharmony_ci goto error; 5568c2ecf20Sopenharmony_ci break; 5578c2ecf20Sopenharmony_ci case DeviceOutRequest | USB_REQ_SET_FEATURE: 5588c2ecf20Sopenharmony_ci if (device_can_wakeup(&hcd->self.root_hub->dev) 5598c2ecf20Sopenharmony_ci && wValue == USB_DEVICE_REMOTE_WAKEUP) 5608c2ecf20Sopenharmony_ci device_set_wakeup_enable(&hcd->self.root_hub->dev, 1); 5618c2ecf20Sopenharmony_ci else 5628c2ecf20Sopenharmony_ci goto error; 5638c2ecf20Sopenharmony_ci break; 5648c2ecf20Sopenharmony_ci case DeviceRequest | USB_REQ_GET_CONFIGURATION: 5658c2ecf20Sopenharmony_ci tbuf[0] = 1; 5668c2ecf20Sopenharmony_ci len = 1; 5678c2ecf20Sopenharmony_ci fallthrough; 5688c2ecf20Sopenharmony_ci case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: 5698c2ecf20Sopenharmony_ci break; 5708c2ecf20Sopenharmony_ci case DeviceRequest | USB_REQ_GET_DESCRIPTOR: 5718c2ecf20Sopenharmony_ci switch (wValue & 0xff00) { 5728c2ecf20Sopenharmony_ci case USB_DT_DEVICE << 8: 5738c2ecf20Sopenharmony_ci switch (hcd->speed) { 5748c2ecf20Sopenharmony_ci case HCD_USB32: 5758c2ecf20Sopenharmony_ci case HCD_USB31: 5768c2ecf20Sopenharmony_ci bufp = usb31_rh_dev_descriptor; 5778c2ecf20Sopenharmony_ci break; 5788c2ecf20Sopenharmony_ci case HCD_USB3: 5798c2ecf20Sopenharmony_ci bufp = usb3_rh_dev_descriptor; 5808c2ecf20Sopenharmony_ci break; 5818c2ecf20Sopenharmony_ci case HCD_USB25: 5828c2ecf20Sopenharmony_ci bufp = usb25_rh_dev_descriptor; 5838c2ecf20Sopenharmony_ci break; 5848c2ecf20Sopenharmony_ci case HCD_USB2: 5858c2ecf20Sopenharmony_ci bufp = usb2_rh_dev_descriptor; 5868c2ecf20Sopenharmony_ci break; 5878c2ecf20Sopenharmony_ci case HCD_USB11: 5888c2ecf20Sopenharmony_ci bufp = usb11_rh_dev_descriptor; 5898c2ecf20Sopenharmony_ci break; 5908c2ecf20Sopenharmony_ci default: 5918c2ecf20Sopenharmony_ci goto error; 5928c2ecf20Sopenharmony_ci } 5938c2ecf20Sopenharmony_ci len = 18; 5948c2ecf20Sopenharmony_ci if (hcd->has_tt) 5958c2ecf20Sopenharmony_ci patch_protocol = 1; 5968c2ecf20Sopenharmony_ci break; 5978c2ecf20Sopenharmony_ci case USB_DT_CONFIG << 8: 5988c2ecf20Sopenharmony_ci switch (hcd->speed) { 5998c2ecf20Sopenharmony_ci case HCD_USB32: 6008c2ecf20Sopenharmony_ci case HCD_USB31: 6018c2ecf20Sopenharmony_ci case HCD_USB3: 6028c2ecf20Sopenharmony_ci bufp = ss_rh_config_descriptor; 6038c2ecf20Sopenharmony_ci len = sizeof ss_rh_config_descriptor; 6048c2ecf20Sopenharmony_ci break; 6058c2ecf20Sopenharmony_ci case HCD_USB25: 6068c2ecf20Sopenharmony_ci case HCD_USB2: 6078c2ecf20Sopenharmony_ci bufp = hs_rh_config_descriptor; 6088c2ecf20Sopenharmony_ci len = sizeof hs_rh_config_descriptor; 6098c2ecf20Sopenharmony_ci break; 6108c2ecf20Sopenharmony_ci case HCD_USB11: 6118c2ecf20Sopenharmony_ci bufp = fs_rh_config_descriptor; 6128c2ecf20Sopenharmony_ci len = sizeof fs_rh_config_descriptor; 6138c2ecf20Sopenharmony_ci break; 6148c2ecf20Sopenharmony_ci default: 6158c2ecf20Sopenharmony_ci goto error; 6168c2ecf20Sopenharmony_ci } 6178c2ecf20Sopenharmony_ci if (device_can_wakeup(&hcd->self.root_hub->dev)) 6188c2ecf20Sopenharmony_ci patch_wakeup = 1; 6198c2ecf20Sopenharmony_ci break; 6208c2ecf20Sopenharmony_ci case USB_DT_STRING << 8: 6218c2ecf20Sopenharmony_ci if ((wValue & 0xff) < 4) 6228c2ecf20Sopenharmony_ci urb->actual_length = rh_string(wValue & 0xff, 6238c2ecf20Sopenharmony_ci hcd, ubuf, wLength); 6248c2ecf20Sopenharmony_ci else /* unsupported IDs --> "protocol stall" */ 6258c2ecf20Sopenharmony_ci goto error; 6268c2ecf20Sopenharmony_ci break; 6278c2ecf20Sopenharmony_ci case USB_DT_BOS << 8: 6288c2ecf20Sopenharmony_ci goto nongeneric; 6298c2ecf20Sopenharmony_ci default: 6308c2ecf20Sopenharmony_ci goto error; 6318c2ecf20Sopenharmony_ci } 6328c2ecf20Sopenharmony_ci break; 6338c2ecf20Sopenharmony_ci case DeviceRequest | USB_REQ_GET_INTERFACE: 6348c2ecf20Sopenharmony_ci tbuf[0] = 0; 6358c2ecf20Sopenharmony_ci len = 1; 6368c2ecf20Sopenharmony_ci fallthrough; 6378c2ecf20Sopenharmony_ci case DeviceOutRequest | USB_REQ_SET_INTERFACE: 6388c2ecf20Sopenharmony_ci break; 6398c2ecf20Sopenharmony_ci case DeviceOutRequest | USB_REQ_SET_ADDRESS: 6408c2ecf20Sopenharmony_ci /* wValue == urb->dev->devaddr */ 6418c2ecf20Sopenharmony_ci dev_dbg (hcd->self.controller, "root hub device address %d\n", 6428c2ecf20Sopenharmony_ci wValue); 6438c2ecf20Sopenharmony_ci break; 6448c2ecf20Sopenharmony_ci 6458c2ecf20Sopenharmony_ci /* INTERFACE REQUESTS (no defined feature/status flags) */ 6468c2ecf20Sopenharmony_ci 6478c2ecf20Sopenharmony_ci /* ENDPOINT REQUESTS */ 6488c2ecf20Sopenharmony_ci 6498c2ecf20Sopenharmony_ci case EndpointRequest | USB_REQ_GET_STATUS: 6508c2ecf20Sopenharmony_ci /* ENDPOINT_HALT flag */ 6518c2ecf20Sopenharmony_ci tbuf[0] = 0; 6528c2ecf20Sopenharmony_ci tbuf[1] = 0; 6538c2ecf20Sopenharmony_ci len = 2; 6548c2ecf20Sopenharmony_ci fallthrough; 6558c2ecf20Sopenharmony_ci case EndpointOutRequest | USB_REQ_CLEAR_FEATURE: 6568c2ecf20Sopenharmony_ci case EndpointOutRequest | USB_REQ_SET_FEATURE: 6578c2ecf20Sopenharmony_ci dev_dbg (hcd->self.controller, "no endpoint features yet\n"); 6588c2ecf20Sopenharmony_ci break; 6598c2ecf20Sopenharmony_ci 6608c2ecf20Sopenharmony_ci /* CLASS REQUESTS (and errors) */ 6618c2ecf20Sopenharmony_ci 6628c2ecf20Sopenharmony_ci default: 6638c2ecf20Sopenharmony_cinongeneric: 6648c2ecf20Sopenharmony_ci /* non-generic request */ 6658c2ecf20Sopenharmony_ci switch (typeReq) { 6668c2ecf20Sopenharmony_ci case GetHubStatus: 6678c2ecf20Sopenharmony_ci len = 4; 6688c2ecf20Sopenharmony_ci break; 6698c2ecf20Sopenharmony_ci case GetPortStatus: 6708c2ecf20Sopenharmony_ci if (wValue == HUB_PORT_STATUS) 6718c2ecf20Sopenharmony_ci len = 4; 6728c2ecf20Sopenharmony_ci else 6738c2ecf20Sopenharmony_ci /* other port status types return 8 bytes */ 6748c2ecf20Sopenharmony_ci len = 8; 6758c2ecf20Sopenharmony_ci break; 6768c2ecf20Sopenharmony_ci case GetHubDescriptor: 6778c2ecf20Sopenharmony_ci len = sizeof (struct usb_hub_descriptor); 6788c2ecf20Sopenharmony_ci break; 6798c2ecf20Sopenharmony_ci case DeviceRequest | USB_REQ_GET_DESCRIPTOR: 6808c2ecf20Sopenharmony_ci /* len is returned by hub_control */ 6818c2ecf20Sopenharmony_ci break; 6828c2ecf20Sopenharmony_ci } 6838c2ecf20Sopenharmony_ci status = hcd->driver->hub_control (hcd, 6848c2ecf20Sopenharmony_ci typeReq, wValue, wIndex, 6858c2ecf20Sopenharmony_ci tbuf, wLength); 6868c2ecf20Sopenharmony_ci 6878c2ecf20Sopenharmony_ci if (typeReq == GetHubDescriptor) 6888c2ecf20Sopenharmony_ci usb_hub_adjust_deviceremovable(hcd->self.root_hub, 6898c2ecf20Sopenharmony_ci (struct usb_hub_descriptor *)tbuf); 6908c2ecf20Sopenharmony_ci break; 6918c2ecf20Sopenharmony_cierror: 6928c2ecf20Sopenharmony_ci /* "protocol stall" on error */ 6938c2ecf20Sopenharmony_ci status = -EPIPE; 6948c2ecf20Sopenharmony_ci } 6958c2ecf20Sopenharmony_ci 6968c2ecf20Sopenharmony_ci if (status < 0) { 6978c2ecf20Sopenharmony_ci len = 0; 6988c2ecf20Sopenharmony_ci if (status != -EPIPE) { 6998c2ecf20Sopenharmony_ci dev_dbg (hcd->self.controller, 7008c2ecf20Sopenharmony_ci "CTRL: TypeReq=0x%x val=0x%x " 7018c2ecf20Sopenharmony_ci "idx=0x%x len=%d ==> %d\n", 7028c2ecf20Sopenharmony_ci typeReq, wValue, wIndex, 7038c2ecf20Sopenharmony_ci wLength, status); 7048c2ecf20Sopenharmony_ci } 7058c2ecf20Sopenharmony_ci } else if (status > 0) { 7068c2ecf20Sopenharmony_ci /* hub_control may return the length of data copied. */ 7078c2ecf20Sopenharmony_ci len = status; 7088c2ecf20Sopenharmony_ci status = 0; 7098c2ecf20Sopenharmony_ci } 7108c2ecf20Sopenharmony_ci if (len) { 7118c2ecf20Sopenharmony_ci if (urb->transfer_buffer_length < len) 7128c2ecf20Sopenharmony_ci len = urb->transfer_buffer_length; 7138c2ecf20Sopenharmony_ci urb->actual_length = len; 7148c2ecf20Sopenharmony_ci /* always USB_DIR_IN, toward host */ 7158c2ecf20Sopenharmony_ci memcpy (ubuf, bufp, len); 7168c2ecf20Sopenharmony_ci 7178c2ecf20Sopenharmony_ci /* report whether RH hardware supports remote wakeup */ 7188c2ecf20Sopenharmony_ci if (patch_wakeup && 7198c2ecf20Sopenharmony_ci len > offsetof (struct usb_config_descriptor, 7208c2ecf20Sopenharmony_ci bmAttributes)) 7218c2ecf20Sopenharmony_ci ((struct usb_config_descriptor *)ubuf)->bmAttributes 7228c2ecf20Sopenharmony_ci |= USB_CONFIG_ATT_WAKEUP; 7238c2ecf20Sopenharmony_ci 7248c2ecf20Sopenharmony_ci /* report whether RH hardware has an integrated TT */ 7258c2ecf20Sopenharmony_ci if (patch_protocol && 7268c2ecf20Sopenharmony_ci len > offsetof(struct usb_device_descriptor, 7278c2ecf20Sopenharmony_ci bDeviceProtocol)) 7288c2ecf20Sopenharmony_ci ((struct usb_device_descriptor *) ubuf)-> 7298c2ecf20Sopenharmony_ci bDeviceProtocol = USB_HUB_PR_HS_SINGLE_TT; 7308c2ecf20Sopenharmony_ci } 7318c2ecf20Sopenharmony_ci 7328c2ecf20Sopenharmony_ci kfree(tbuf); 7338c2ecf20Sopenharmony_ci err_alloc: 7348c2ecf20Sopenharmony_ci 7358c2ecf20Sopenharmony_ci /* any errors get returned through the urb completion */ 7368c2ecf20Sopenharmony_ci spin_lock_irq(&hcd_root_hub_lock); 7378c2ecf20Sopenharmony_ci usb_hcd_unlink_urb_from_ep(hcd, urb); 7388c2ecf20Sopenharmony_ci usb_hcd_giveback_urb(hcd, urb, status); 7398c2ecf20Sopenharmony_ci spin_unlock_irq(&hcd_root_hub_lock); 7408c2ecf20Sopenharmony_ci return 0; 7418c2ecf20Sopenharmony_ci} 7428c2ecf20Sopenharmony_ci 7438c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 7448c2ecf20Sopenharmony_ci 7458c2ecf20Sopenharmony_ci/* 7468c2ecf20Sopenharmony_ci * Root Hub interrupt transfers are polled using a timer if the 7478c2ecf20Sopenharmony_ci * driver requests it; otherwise the driver is responsible for 7488c2ecf20Sopenharmony_ci * calling usb_hcd_poll_rh_status() when an event occurs. 7498c2ecf20Sopenharmony_ci * 7508c2ecf20Sopenharmony_ci * Completions are called in_interrupt(), but they may or may not 7518c2ecf20Sopenharmony_ci * be in_irq(). 7528c2ecf20Sopenharmony_ci */ 7538c2ecf20Sopenharmony_civoid usb_hcd_poll_rh_status(struct usb_hcd *hcd) 7548c2ecf20Sopenharmony_ci{ 7558c2ecf20Sopenharmony_ci struct urb *urb; 7568c2ecf20Sopenharmony_ci int length; 7578c2ecf20Sopenharmony_ci int status; 7588c2ecf20Sopenharmony_ci unsigned long flags; 7598c2ecf20Sopenharmony_ci char buffer[6]; /* Any root hubs with > 31 ports? */ 7608c2ecf20Sopenharmony_ci 7618c2ecf20Sopenharmony_ci if (unlikely(!hcd->rh_pollable)) 7628c2ecf20Sopenharmony_ci return; 7638c2ecf20Sopenharmony_ci if (!hcd->uses_new_polling && !hcd->status_urb) 7648c2ecf20Sopenharmony_ci return; 7658c2ecf20Sopenharmony_ci 7668c2ecf20Sopenharmony_ci length = hcd->driver->hub_status_data(hcd, buffer); 7678c2ecf20Sopenharmony_ci if (length > 0) { 7688c2ecf20Sopenharmony_ci 7698c2ecf20Sopenharmony_ci /* try to complete the status urb */ 7708c2ecf20Sopenharmony_ci spin_lock_irqsave(&hcd_root_hub_lock, flags); 7718c2ecf20Sopenharmony_ci urb = hcd->status_urb; 7728c2ecf20Sopenharmony_ci if (urb) { 7738c2ecf20Sopenharmony_ci clear_bit(HCD_FLAG_POLL_PENDING, &hcd->flags); 7748c2ecf20Sopenharmony_ci hcd->status_urb = NULL; 7758c2ecf20Sopenharmony_ci if (urb->transfer_buffer_length >= length) { 7768c2ecf20Sopenharmony_ci status = 0; 7778c2ecf20Sopenharmony_ci } else { 7788c2ecf20Sopenharmony_ci status = -EOVERFLOW; 7798c2ecf20Sopenharmony_ci length = urb->transfer_buffer_length; 7808c2ecf20Sopenharmony_ci } 7818c2ecf20Sopenharmony_ci urb->actual_length = length; 7828c2ecf20Sopenharmony_ci memcpy(urb->transfer_buffer, buffer, length); 7838c2ecf20Sopenharmony_ci 7848c2ecf20Sopenharmony_ci usb_hcd_unlink_urb_from_ep(hcd, urb); 7858c2ecf20Sopenharmony_ci usb_hcd_giveback_urb(hcd, urb, status); 7868c2ecf20Sopenharmony_ci } else { 7878c2ecf20Sopenharmony_ci length = 0; 7888c2ecf20Sopenharmony_ci set_bit(HCD_FLAG_POLL_PENDING, &hcd->flags); 7898c2ecf20Sopenharmony_ci } 7908c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&hcd_root_hub_lock, flags); 7918c2ecf20Sopenharmony_ci } 7928c2ecf20Sopenharmony_ci 7938c2ecf20Sopenharmony_ci /* The USB 2.0 spec says 256 ms. This is close enough and won't 7948c2ecf20Sopenharmony_ci * exceed that limit if HZ is 100. The math is more clunky than 7958c2ecf20Sopenharmony_ci * maybe expected, this is to make sure that all timers for USB devices 7968c2ecf20Sopenharmony_ci * fire at the same time to give the CPU a break in between */ 7978c2ecf20Sopenharmony_ci if (hcd->uses_new_polling ? HCD_POLL_RH(hcd) : 7988c2ecf20Sopenharmony_ci (length == 0 && hcd->status_urb != NULL)) 7998c2ecf20Sopenharmony_ci mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4)); 8008c2ecf20Sopenharmony_ci} 8018c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status); 8028c2ecf20Sopenharmony_ci 8038c2ecf20Sopenharmony_ci/* timer callback */ 8048c2ecf20Sopenharmony_cistatic void rh_timer_func (struct timer_list *t) 8058c2ecf20Sopenharmony_ci{ 8068c2ecf20Sopenharmony_ci struct usb_hcd *_hcd = from_timer(_hcd, t, rh_timer); 8078c2ecf20Sopenharmony_ci 8088c2ecf20Sopenharmony_ci usb_hcd_poll_rh_status(_hcd); 8098c2ecf20Sopenharmony_ci} 8108c2ecf20Sopenharmony_ci 8118c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 8128c2ecf20Sopenharmony_ci 8138c2ecf20Sopenharmony_cistatic int rh_queue_status (struct usb_hcd *hcd, struct urb *urb) 8148c2ecf20Sopenharmony_ci{ 8158c2ecf20Sopenharmony_ci int retval; 8168c2ecf20Sopenharmony_ci unsigned long flags; 8178c2ecf20Sopenharmony_ci unsigned len = 1 + (urb->dev->maxchild / 8); 8188c2ecf20Sopenharmony_ci 8198c2ecf20Sopenharmony_ci spin_lock_irqsave (&hcd_root_hub_lock, flags); 8208c2ecf20Sopenharmony_ci if (hcd->status_urb || urb->transfer_buffer_length < len) { 8218c2ecf20Sopenharmony_ci dev_dbg (hcd->self.controller, "not queuing rh status urb\n"); 8228c2ecf20Sopenharmony_ci retval = -EINVAL; 8238c2ecf20Sopenharmony_ci goto done; 8248c2ecf20Sopenharmony_ci } 8258c2ecf20Sopenharmony_ci 8268c2ecf20Sopenharmony_ci retval = usb_hcd_link_urb_to_ep(hcd, urb); 8278c2ecf20Sopenharmony_ci if (retval) 8288c2ecf20Sopenharmony_ci goto done; 8298c2ecf20Sopenharmony_ci 8308c2ecf20Sopenharmony_ci hcd->status_urb = urb; 8318c2ecf20Sopenharmony_ci urb->hcpriv = hcd; /* indicate it's queued */ 8328c2ecf20Sopenharmony_ci if (!hcd->uses_new_polling) 8338c2ecf20Sopenharmony_ci mod_timer(&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4)); 8348c2ecf20Sopenharmony_ci 8358c2ecf20Sopenharmony_ci /* If a status change has already occurred, report it ASAP */ 8368c2ecf20Sopenharmony_ci else if (HCD_POLL_PENDING(hcd)) 8378c2ecf20Sopenharmony_ci mod_timer(&hcd->rh_timer, jiffies); 8388c2ecf20Sopenharmony_ci retval = 0; 8398c2ecf20Sopenharmony_ci done: 8408c2ecf20Sopenharmony_ci spin_unlock_irqrestore (&hcd_root_hub_lock, flags); 8418c2ecf20Sopenharmony_ci return retval; 8428c2ecf20Sopenharmony_ci} 8438c2ecf20Sopenharmony_ci 8448c2ecf20Sopenharmony_cistatic int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb) 8458c2ecf20Sopenharmony_ci{ 8468c2ecf20Sopenharmony_ci if (usb_endpoint_xfer_int(&urb->ep->desc)) 8478c2ecf20Sopenharmony_ci return rh_queue_status (hcd, urb); 8488c2ecf20Sopenharmony_ci if (usb_endpoint_xfer_control(&urb->ep->desc)) 8498c2ecf20Sopenharmony_ci return rh_call_control (hcd, urb); 8508c2ecf20Sopenharmony_ci return -EINVAL; 8518c2ecf20Sopenharmony_ci} 8528c2ecf20Sopenharmony_ci 8538c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 8548c2ecf20Sopenharmony_ci 8558c2ecf20Sopenharmony_ci/* Unlinks of root-hub control URBs are legal, but they don't do anything 8568c2ecf20Sopenharmony_ci * since these URBs always execute synchronously. 8578c2ecf20Sopenharmony_ci */ 8588c2ecf20Sopenharmony_cistatic int usb_rh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) 8598c2ecf20Sopenharmony_ci{ 8608c2ecf20Sopenharmony_ci unsigned long flags; 8618c2ecf20Sopenharmony_ci int rc; 8628c2ecf20Sopenharmony_ci 8638c2ecf20Sopenharmony_ci spin_lock_irqsave(&hcd_root_hub_lock, flags); 8648c2ecf20Sopenharmony_ci rc = usb_hcd_check_unlink_urb(hcd, urb, status); 8658c2ecf20Sopenharmony_ci if (rc) 8668c2ecf20Sopenharmony_ci goto done; 8678c2ecf20Sopenharmony_ci 8688c2ecf20Sopenharmony_ci if (usb_endpoint_num(&urb->ep->desc) == 0) { /* Control URB */ 8698c2ecf20Sopenharmony_ci ; /* Do nothing */ 8708c2ecf20Sopenharmony_ci 8718c2ecf20Sopenharmony_ci } else { /* Status URB */ 8728c2ecf20Sopenharmony_ci if (!hcd->uses_new_polling) 8738c2ecf20Sopenharmony_ci del_timer (&hcd->rh_timer); 8748c2ecf20Sopenharmony_ci if (urb == hcd->status_urb) { 8758c2ecf20Sopenharmony_ci hcd->status_urb = NULL; 8768c2ecf20Sopenharmony_ci usb_hcd_unlink_urb_from_ep(hcd, urb); 8778c2ecf20Sopenharmony_ci usb_hcd_giveback_urb(hcd, urb, status); 8788c2ecf20Sopenharmony_ci } 8798c2ecf20Sopenharmony_ci } 8808c2ecf20Sopenharmony_ci done: 8818c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&hcd_root_hub_lock, flags); 8828c2ecf20Sopenharmony_ci return rc; 8838c2ecf20Sopenharmony_ci} 8848c2ecf20Sopenharmony_ci 8858c2ecf20Sopenharmony_ci 8868c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 8878c2ecf20Sopenharmony_ci 8888c2ecf20Sopenharmony_ci/** 8898c2ecf20Sopenharmony_ci * usb_bus_init - shared initialization code 8908c2ecf20Sopenharmony_ci * @bus: the bus structure being initialized 8918c2ecf20Sopenharmony_ci * 8928c2ecf20Sopenharmony_ci * This code is used to initialize a usb_bus structure, memory for which is 8938c2ecf20Sopenharmony_ci * separately managed. 8948c2ecf20Sopenharmony_ci */ 8958c2ecf20Sopenharmony_cistatic void usb_bus_init (struct usb_bus *bus) 8968c2ecf20Sopenharmony_ci{ 8978c2ecf20Sopenharmony_ci memset (&bus->devmap, 0, sizeof(struct usb_devmap)); 8988c2ecf20Sopenharmony_ci 8998c2ecf20Sopenharmony_ci bus->devnum_next = 1; 9008c2ecf20Sopenharmony_ci 9018c2ecf20Sopenharmony_ci bus->root_hub = NULL; 9028c2ecf20Sopenharmony_ci bus->busnum = -1; 9038c2ecf20Sopenharmony_ci bus->bandwidth_allocated = 0; 9048c2ecf20Sopenharmony_ci bus->bandwidth_int_reqs = 0; 9058c2ecf20Sopenharmony_ci bus->bandwidth_isoc_reqs = 0; 9068c2ecf20Sopenharmony_ci mutex_init(&bus->devnum_next_mutex); 9078c2ecf20Sopenharmony_ci} 9088c2ecf20Sopenharmony_ci 9098c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 9108c2ecf20Sopenharmony_ci 9118c2ecf20Sopenharmony_ci/** 9128c2ecf20Sopenharmony_ci * usb_register_bus - registers the USB host controller with the usb core 9138c2ecf20Sopenharmony_ci * @bus: pointer to the bus to register 9148c2ecf20Sopenharmony_ci * Context: !in_interrupt() 9158c2ecf20Sopenharmony_ci * 9168c2ecf20Sopenharmony_ci * Assigns a bus number, and links the controller into usbcore data 9178c2ecf20Sopenharmony_ci * structures so that it can be seen by scanning the bus list. 9188c2ecf20Sopenharmony_ci * 9198c2ecf20Sopenharmony_ci * Return: 0 if successful. A negative error code otherwise. 9208c2ecf20Sopenharmony_ci */ 9218c2ecf20Sopenharmony_cistatic int usb_register_bus(struct usb_bus *bus) 9228c2ecf20Sopenharmony_ci{ 9238c2ecf20Sopenharmony_ci int result = -E2BIG; 9248c2ecf20Sopenharmony_ci int busnum; 9258c2ecf20Sopenharmony_ci 9268c2ecf20Sopenharmony_ci mutex_lock(&usb_bus_idr_lock); 9278c2ecf20Sopenharmony_ci busnum = idr_alloc(&usb_bus_idr, bus, 1, USB_MAXBUS, GFP_KERNEL); 9288c2ecf20Sopenharmony_ci if (busnum < 0) { 9298c2ecf20Sopenharmony_ci pr_err("%s: failed to get bus number\n", usbcore_name); 9308c2ecf20Sopenharmony_ci goto error_find_busnum; 9318c2ecf20Sopenharmony_ci } 9328c2ecf20Sopenharmony_ci bus->busnum = busnum; 9338c2ecf20Sopenharmony_ci mutex_unlock(&usb_bus_idr_lock); 9348c2ecf20Sopenharmony_ci 9358c2ecf20Sopenharmony_ci usb_notify_add_bus(bus); 9368c2ecf20Sopenharmony_ci 9378c2ecf20Sopenharmony_ci dev_info (bus->controller, "new USB bus registered, assigned bus " 9388c2ecf20Sopenharmony_ci "number %d\n", bus->busnum); 9398c2ecf20Sopenharmony_ci return 0; 9408c2ecf20Sopenharmony_ci 9418c2ecf20Sopenharmony_cierror_find_busnum: 9428c2ecf20Sopenharmony_ci mutex_unlock(&usb_bus_idr_lock); 9438c2ecf20Sopenharmony_ci return result; 9448c2ecf20Sopenharmony_ci} 9458c2ecf20Sopenharmony_ci 9468c2ecf20Sopenharmony_ci/** 9478c2ecf20Sopenharmony_ci * usb_deregister_bus - deregisters the USB host controller 9488c2ecf20Sopenharmony_ci * @bus: pointer to the bus to deregister 9498c2ecf20Sopenharmony_ci * Context: !in_interrupt() 9508c2ecf20Sopenharmony_ci * 9518c2ecf20Sopenharmony_ci * Recycles the bus number, and unlinks the controller from usbcore data 9528c2ecf20Sopenharmony_ci * structures so that it won't be seen by scanning the bus list. 9538c2ecf20Sopenharmony_ci */ 9548c2ecf20Sopenharmony_cistatic void usb_deregister_bus (struct usb_bus *bus) 9558c2ecf20Sopenharmony_ci{ 9568c2ecf20Sopenharmony_ci dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum); 9578c2ecf20Sopenharmony_ci 9588c2ecf20Sopenharmony_ci /* 9598c2ecf20Sopenharmony_ci * NOTE: make sure that all the devices are removed by the 9608c2ecf20Sopenharmony_ci * controller code, as well as having it call this when cleaning 9618c2ecf20Sopenharmony_ci * itself up 9628c2ecf20Sopenharmony_ci */ 9638c2ecf20Sopenharmony_ci mutex_lock(&usb_bus_idr_lock); 9648c2ecf20Sopenharmony_ci idr_remove(&usb_bus_idr, bus->busnum); 9658c2ecf20Sopenharmony_ci mutex_unlock(&usb_bus_idr_lock); 9668c2ecf20Sopenharmony_ci 9678c2ecf20Sopenharmony_ci usb_notify_remove_bus(bus); 9688c2ecf20Sopenharmony_ci} 9698c2ecf20Sopenharmony_ci 9708c2ecf20Sopenharmony_ci/** 9718c2ecf20Sopenharmony_ci * register_root_hub - called by usb_add_hcd() to register a root hub 9728c2ecf20Sopenharmony_ci * @hcd: host controller for this root hub 9738c2ecf20Sopenharmony_ci * 9748c2ecf20Sopenharmony_ci * This function registers the root hub with the USB subsystem. It sets up 9758c2ecf20Sopenharmony_ci * the device properly in the device tree and then calls usb_new_device() 9768c2ecf20Sopenharmony_ci * to register the usb device. It also assigns the root hub's USB address 9778c2ecf20Sopenharmony_ci * (always 1). 9788c2ecf20Sopenharmony_ci * 9798c2ecf20Sopenharmony_ci * Return: 0 if successful. A negative error code otherwise. 9808c2ecf20Sopenharmony_ci */ 9818c2ecf20Sopenharmony_cistatic int register_root_hub(struct usb_hcd *hcd) 9828c2ecf20Sopenharmony_ci{ 9838c2ecf20Sopenharmony_ci struct device *parent_dev = hcd->self.controller; 9848c2ecf20Sopenharmony_ci struct usb_device *usb_dev = hcd->self.root_hub; 9858c2ecf20Sopenharmony_ci struct usb_device_descriptor *descr; 9868c2ecf20Sopenharmony_ci const int devnum = 1; 9878c2ecf20Sopenharmony_ci int retval; 9888c2ecf20Sopenharmony_ci 9898c2ecf20Sopenharmony_ci usb_dev->devnum = devnum; 9908c2ecf20Sopenharmony_ci usb_dev->bus->devnum_next = devnum + 1; 9918c2ecf20Sopenharmony_ci set_bit (devnum, usb_dev->bus->devmap.devicemap); 9928c2ecf20Sopenharmony_ci usb_set_device_state(usb_dev, USB_STATE_ADDRESS); 9938c2ecf20Sopenharmony_ci 9948c2ecf20Sopenharmony_ci mutex_lock(&usb_bus_idr_lock); 9958c2ecf20Sopenharmony_ci 9968c2ecf20Sopenharmony_ci usb_dev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); 9978c2ecf20Sopenharmony_ci descr = usb_get_device_descriptor(usb_dev); 9988c2ecf20Sopenharmony_ci if (IS_ERR(descr)) { 9998c2ecf20Sopenharmony_ci retval = PTR_ERR(descr); 10008c2ecf20Sopenharmony_ci mutex_unlock(&usb_bus_idr_lock); 10018c2ecf20Sopenharmony_ci dev_dbg (parent_dev, "can't read %s device descriptor %d\n", 10028c2ecf20Sopenharmony_ci dev_name(&usb_dev->dev), retval); 10038c2ecf20Sopenharmony_ci return retval; 10048c2ecf20Sopenharmony_ci } 10058c2ecf20Sopenharmony_ci usb_dev->descriptor = *descr; 10068c2ecf20Sopenharmony_ci kfree(descr); 10078c2ecf20Sopenharmony_ci 10088c2ecf20Sopenharmony_ci if (le16_to_cpu(usb_dev->descriptor.bcdUSB) >= 0x0201) { 10098c2ecf20Sopenharmony_ci retval = usb_get_bos_descriptor(usb_dev); 10108c2ecf20Sopenharmony_ci if (!retval) { 10118c2ecf20Sopenharmony_ci usb_dev->lpm_capable = usb_device_supports_lpm(usb_dev); 10128c2ecf20Sopenharmony_ci } else if (usb_dev->speed >= USB_SPEED_SUPER) { 10138c2ecf20Sopenharmony_ci mutex_unlock(&usb_bus_idr_lock); 10148c2ecf20Sopenharmony_ci dev_dbg(parent_dev, "can't read %s bos descriptor %d\n", 10158c2ecf20Sopenharmony_ci dev_name(&usb_dev->dev), retval); 10168c2ecf20Sopenharmony_ci return retval; 10178c2ecf20Sopenharmony_ci } 10188c2ecf20Sopenharmony_ci } 10198c2ecf20Sopenharmony_ci 10208c2ecf20Sopenharmony_ci retval = usb_new_device (usb_dev); 10218c2ecf20Sopenharmony_ci if (retval) { 10228c2ecf20Sopenharmony_ci dev_err (parent_dev, "can't register root hub for %s, %d\n", 10238c2ecf20Sopenharmony_ci dev_name(&usb_dev->dev), retval); 10248c2ecf20Sopenharmony_ci } else { 10258c2ecf20Sopenharmony_ci spin_lock_irq (&hcd_root_hub_lock); 10268c2ecf20Sopenharmony_ci hcd->rh_registered = 1; 10278c2ecf20Sopenharmony_ci spin_unlock_irq (&hcd_root_hub_lock); 10288c2ecf20Sopenharmony_ci 10298c2ecf20Sopenharmony_ci /* Did the HC die before the root hub was registered? */ 10308c2ecf20Sopenharmony_ci if (HCD_DEAD(hcd)) 10318c2ecf20Sopenharmony_ci usb_hc_died (hcd); /* This time clean up */ 10328c2ecf20Sopenharmony_ci } 10338c2ecf20Sopenharmony_ci mutex_unlock(&usb_bus_idr_lock); 10348c2ecf20Sopenharmony_ci 10358c2ecf20Sopenharmony_ci return retval; 10368c2ecf20Sopenharmony_ci} 10378c2ecf20Sopenharmony_ci 10388c2ecf20Sopenharmony_ci/* 10398c2ecf20Sopenharmony_ci * usb_hcd_start_port_resume - a root-hub port is sending a resume signal 10408c2ecf20Sopenharmony_ci * @bus: the bus which the root hub belongs to 10418c2ecf20Sopenharmony_ci * @portnum: the port which is being resumed 10428c2ecf20Sopenharmony_ci * 10438c2ecf20Sopenharmony_ci * HCDs should call this function when they know that a resume signal is 10448c2ecf20Sopenharmony_ci * being sent to a root-hub port. The root hub will be prevented from 10458c2ecf20Sopenharmony_ci * going into autosuspend until usb_hcd_end_port_resume() is called. 10468c2ecf20Sopenharmony_ci * 10478c2ecf20Sopenharmony_ci * The bus's private lock must be held by the caller. 10488c2ecf20Sopenharmony_ci */ 10498c2ecf20Sopenharmony_civoid usb_hcd_start_port_resume(struct usb_bus *bus, int portnum) 10508c2ecf20Sopenharmony_ci{ 10518c2ecf20Sopenharmony_ci unsigned bit = 1 << portnum; 10528c2ecf20Sopenharmony_ci 10538c2ecf20Sopenharmony_ci if (!(bus->resuming_ports & bit)) { 10548c2ecf20Sopenharmony_ci bus->resuming_ports |= bit; 10558c2ecf20Sopenharmony_ci pm_runtime_get_noresume(&bus->root_hub->dev); 10568c2ecf20Sopenharmony_ci } 10578c2ecf20Sopenharmony_ci} 10588c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_hcd_start_port_resume); 10598c2ecf20Sopenharmony_ci 10608c2ecf20Sopenharmony_ci/* 10618c2ecf20Sopenharmony_ci * usb_hcd_end_port_resume - a root-hub port has stopped sending a resume signal 10628c2ecf20Sopenharmony_ci * @bus: the bus which the root hub belongs to 10638c2ecf20Sopenharmony_ci * @portnum: the port which is being resumed 10648c2ecf20Sopenharmony_ci * 10658c2ecf20Sopenharmony_ci * HCDs should call this function when they know that a resume signal has 10668c2ecf20Sopenharmony_ci * stopped being sent to a root-hub port. The root hub will be allowed to 10678c2ecf20Sopenharmony_ci * autosuspend again. 10688c2ecf20Sopenharmony_ci * 10698c2ecf20Sopenharmony_ci * The bus's private lock must be held by the caller. 10708c2ecf20Sopenharmony_ci */ 10718c2ecf20Sopenharmony_civoid usb_hcd_end_port_resume(struct usb_bus *bus, int portnum) 10728c2ecf20Sopenharmony_ci{ 10738c2ecf20Sopenharmony_ci unsigned bit = 1 << portnum; 10748c2ecf20Sopenharmony_ci 10758c2ecf20Sopenharmony_ci if (bus->resuming_ports & bit) { 10768c2ecf20Sopenharmony_ci bus->resuming_ports &= ~bit; 10778c2ecf20Sopenharmony_ci pm_runtime_put_noidle(&bus->root_hub->dev); 10788c2ecf20Sopenharmony_ci } 10798c2ecf20Sopenharmony_ci} 10808c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_hcd_end_port_resume); 10818c2ecf20Sopenharmony_ci 10828c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 10838c2ecf20Sopenharmony_ci 10848c2ecf20Sopenharmony_ci/** 10858c2ecf20Sopenharmony_ci * usb_calc_bus_time - approximate periodic transaction time in nanoseconds 10868c2ecf20Sopenharmony_ci * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH} 10878c2ecf20Sopenharmony_ci * @is_input: true iff the transaction sends data to the host 10888c2ecf20Sopenharmony_ci * @isoc: true for isochronous transactions, false for interrupt ones 10898c2ecf20Sopenharmony_ci * @bytecount: how many bytes in the transaction. 10908c2ecf20Sopenharmony_ci * 10918c2ecf20Sopenharmony_ci * Return: Approximate bus time in nanoseconds for a periodic transaction. 10928c2ecf20Sopenharmony_ci * 10938c2ecf20Sopenharmony_ci * Note: 10948c2ecf20Sopenharmony_ci * See USB 2.0 spec section 5.11.3; only periodic transfers need to be 10958c2ecf20Sopenharmony_ci * scheduled in software, this function is only used for such scheduling. 10968c2ecf20Sopenharmony_ci */ 10978c2ecf20Sopenharmony_cilong usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount) 10988c2ecf20Sopenharmony_ci{ 10998c2ecf20Sopenharmony_ci unsigned long tmp; 11008c2ecf20Sopenharmony_ci 11018c2ecf20Sopenharmony_ci switch (speed) { 11028c2ecf20Sopenharmony_ci case USB_SPEED_LOW: /* INTR only */ 11038c2ecf20Sopenharmony_ci if (is_input) { 11048c2ecf20Sopenharmony_ci tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L; 11058c2ecf20Sopenharmony_ci return 64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp; 11068c2ecf20Sopenharmony_ci } else { 11078c2ecf20Sopenharmony_ci tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L; 11088c2ecf20Sopenharmony_ci return 64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp; 11098c2ecf20Sopenharmony_ci } 11108c2ecf20Sopenharmony_ci case USB_SPEED_FULL: /* ISOC or INTR */ 11118c2ecf20Sopenharmony_ci if (isoc) { 11128c2ecf20Sopenharmony_ci tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L; 11138c2ecf20Sopenharmony_ci return ((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp; 11148c2ecf20Sopenharmony_ci } else { 11158c2ecf20Sopenharmony_ci tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L; 11168c2ecf20Sopenharmony_ci return 9107L + BW_HOST_DELAY + tmp; 11178c2ecf20Sopenharmony_ci } 11188c2ecf20Sopenharmony_ci case USB_SPEED_HIGH: /* ISOC or INTR */ 11198c2ecf20Sopenharmony_ci /* FIXME adjust for input vs output */ 11208c2ecf20Sopenharmony_ci if (isoc) 11218c2ecf20Sopenharmony_ci tmp = HS_NSECS_ISO (bytecount); 11228c2ecf20Sopenharmony_ci else 11238c2ecf20Sopenharmony_ci tmp = HS_NSECS (bytecount); 11248c2ecf20Sopenharmony_ci return tmp; 11258c2ecf20Sopenharmony_ci default: 11268c2ecf20Sopenharmony_ci pr_debug ("%s: bogus device speed!\n", usbcore_name); 11278c2ecf20Sopenharmony_ci return -1; 11288c2ecf20Sopenharmony_ci } 11298c2ecf20Sopenharmony_ci} 11308c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_calc_bus_time); 11318c2ecf20Sopenharmony_ci 11328c2ecf20Sopenharmony_ci 11338c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 11348c2ecf20Sopenharmony_ci 11358c2ecf20Sopenharmony_ci/* 11368c2ecf20Sopenharmony_ci * Generic HC operations. 11378c2ecf20Sopenharmony_ci */ 11388c2ecf20Sopenharmony_ci 11398c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 11408c2ecf20Sopenharmony_ci 11418c2ecf20Sopenharmony_ci/** 11428c2ecf20Sopenharmony_ci * usb_hcd_link_urb_to_ep - add an URB to its endpoint queue 11438c2ecf20Sopenharmony_ci * @hcd: host controller to which @urb was submitted 11448c2ecf20Sopenharmony_ci * @urb: URB being submitted 11458c2ecf20Sopenharmony_ci * 11468c2ecf20Sopenharmony_ci * Host controller drivers should call this routine in their enqueue() 11478c2ecf20Sopenharmony_ci * method. The HCD's private spinlock must be held and interrupts must 11488c2ecf20Sopenharmony_ci * be disabled. The actions carried out here are required for URB 11498c2ecf20Sopenharmony_ci * submission, as well as for endpoint shutdown and for usb_kill_urb. 11508c2ecf20Sopenharmony_ci * 11518c2ecf20Sopenharmony_ci * Return: 0 for no error, otherwise a negative error code (in which case 11528c2ecf20Sopenharmony_ci * the enqueue() method must fail). If no error occurs but enqueue() fails 11538c2ecf20Sopenharmony_ci * anyway, it must call usb_hcd_unlink_urb_from_ep() before releasing 11548c2ecf20Sopenharmony_ci * the private spinlock and returning. 11558c2ecf20Sopenharmony_ci */ 11568c2ecf20Sopenharmony_ciint usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb) 11578c2ecf20Sopenharmony_ci{ 11588c2ecf20Sopenharmony_ci int rc = 0; 11598c2ecf20Sopenharmony_ci 11608c2ecf20Sopenharmony_ci spin_lock(&hcd_urb_list_lock); 11618c2ecf20Sopenharmony_ci 11628c2ecf20Sopenharmony_ci /* Check that the URB isn't being killed */ 11638c2ecf20Sopenharmony_ci if (unlikely(atomic_read(&urb->reject))) { 11648c2ecf20Sopenharmony_ci rc = -EPERM; 11658c2ecf20Sopenharmony_ci goto done; 11668c2ecf20Sopenharmony_ci } 11678c2ecf20Sopenharmony_ci 11688c2ecf20Sopenharmony_ci if (unlikely(!urb->ep->enabled)) { 11698c2ecf20Sopenharmony_ci rc = -ENOENT; 11708c2ecf20Sopenharmony_ci goto done; 11718c2ecf20Sopenharmony_ci } 11728c2ecf20Sopenharmony_ci 11738c2ecf20Sopenharmony_ci if (unlikely(!urb->dev->can_submit)) { 11748c2ecf20Sopenharmony_ci rc = -EHOSTUNREACH; 11758c2ecf20Sopenharmony_ci goto done; 11768c2ecf20Sopenharmony_ci } 11778c2ecf20Sopenharmony_ci 11788c2ecf20Sopenharmony_ci /* 11798c2ecf20Sopenharmony_ci * Check the host controller's state and add the URB to the 11808c2ecf20Sopenharmony_ci * endpoint's queue. 11818c2ecf20Sopenharmony_ci */ 11828c2ecf20Sopenharmony_ci if (HCD_RH_RUNNING(hcd)) { 11838c2ecf20Sopenharmony_ci urb->unlinked = 0; 11848c2ecf20Sopenharmony_ci list_add_tail(&urb->urb_list, &urb->ep->urb_list); 11858c2ecf20Sopenharmony_ci } else { 11868c2ecf20Sopenharmony_ci rc = -ESHUTDOWN; 11878c2ecf20Sopenharmony_ci goto done; 11888c2ecf20Sopenharmony_ci } 11898c2ecf20Sopenharmony_ci done: 11908c2ecf20Sopenharmony_ci spin_unlock(&hcd_urb_list_lock); 11918c2ecf20Sopenharmony_ci return rc; 11928c2ecf20Sopenharmony_ci} 11938c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_hcd_link_urb_to_ep); 11948c2ecf20Sopenharmony_ci 11958c2ecf20Sopenharmony_ci/** 11968c2ecf20Sopenharmony_ci * usb_hcd_check_unlink_urb - check whether an URB may be unlinked 11978c2ecf20Sopenharmony_ci * @hcd: host controller to which @urb was submitted 11988c2ecf20Sopenharmony_ci * @urb: URB being checked for unlinkability 11998c2ecf20Sopenharmony_ci * @status: error code to store in @urb if the unlink succeeds 12008c2ecf20Sopenharmony_ci * 12018c2ecf20Sopenharmony_ci * Host controller drivers should call this routine in their dequeue() 12028c2ecf20Sopenharmony_ci * method. The HCD's private spinlock must be held and interrupts must 12038c2ecf20Sopenharmony_ci * be disabled. The actions carried out here are required for making 12048c2ecf20Sopenharmony_ci * sure than an unlink is valid. 12058c2ecf20Sopenharmony_ci * 12068c2ecf20Sopenharmony_ci * Return: 0 for no error, otherwise a negative error code (in which case 12078c2ecf20Sopenharmony_ci * the dequeue() method must fail). The possible error codes are: 12088c2ecf20Sopenharmony_ci * 12098c2ecf20Sopenharmony_ci * -EIDRM: @urb was not submitted or has already completed. 12108c2ecf20Sopenharmony_ci * The completion function may not have been called yet. 12118c2ecf20Sopenharmony_ci * 12128c2ecf20Sopenharmony_ci * -EBUSY: @urb has already been unlinked. 12138c2ecf20Sopenharmony_ci */ 12148c2ecf20Sopenharmony_ciint usb_hcd_check_unlink_urb(struct usb_hcd *hcd, struct urb *urb, 12158c2ecf20Sopenharmony_ci int status) 12168c2ecf20Sopenharmony_ci{ 12178c2ecf20Sopenharmony_ci struct list_head *tmp; 12188c2ecf20Sopenharmony_ci 12198c2ecf20Sopenharmony_ci /* insist the urb is still queued */ 12208c2ecf20Sopenharmony_ci list_for_each(tmp, &urb->ep->urb_list) { 12218c2ecf20Sopenharmony_ci if (tmp == &urb->urb_list) 12228c2ecf20Sopenharmony_ci break; 12238c2ecf20Sopenharmony_ci } 12248c2ecf20Sopenharmony_ci if (tmp != &urb->urb_list) 12258c2ecf20Sopenharmony_ci return -EIDRM; 12268c2ecf20Sopenharmony_ci 12278c2ecf20Sopenharmony_ci /* Any status except -EINPROGRESS means something already started to 12288c2ecf20Sopenharmony_ci * unlink this URB from the hardware. So there's no more work to do. 12298c2ecf20Sopenharmony_ci */ 12308c2ecf20Sopenharmony_ci if (urb->unlinked) 12318c2ecf20Sopenharmony_ci return -EBUSY; 12328c2ecf20Sopenharmony_ci urb->unlinked = status; 12338c2ecf20Sopenharmony_ci return 0; 12348c2ecf20Sopenharmony_ci} 12358c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_hcd_check_unlink_urb); 12368c2ecf20Sopenharmony_ci 12378c2ecf20Sopenharmony_ci/** 12388c2ecf20Sopenharmony_ci * usb_hcd_unlink_urb_from_ep - remove an URB from its endpoint queue 12398c2ecf20Sopenharmony_ci * @hcd: host controller to which @urb was submitted 12408c2ecf20Sopenharmony_ci * @urb: URB being unlinked 12418c2ecf20Sopenharmony_ci * 12428c2ecf20Sopenharmony_ci * Host controller drivers should call this routine before calling 12438c2ecf20Sopenharmony_ci * usb_hcd_giveback_urb(). The HCD's private spinlock must be held and 12448c2ecf20Sopenharmony_ci * interrupts must be disabled. The actions carried out here are required 12458c2ecf20Sopenharmony_ci * for URB completion. 12468c2ecf20Sopenharmony_ci */ 12478c2ecf20Sopenharmony_civoid usb_hcd_unlink_urb_from_ep(struct usb_hcd *hcd, struct urb *urb) 12488c2ecf20Sopenharmony_ci{ 12498c2ecf20Sopenharmony_ci /* clear all state linking urb to this dev (and hcd) */ 12508c2ecf20Sopenharmony_ci spin_lock(&hcd_urb_list_lock); 12518c2ecf20Sopenharmony_ci list_del_init(&urb->urb_list); 12528c2ecf20Sopenharmony_ci spin_unlock(&hcd_urb_list_lock); 12538c2ecf20Sopenharmony_ci} 12548c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_hcd_unlink_urb_from_ep); 12558c2ecf20Sopenharmony_ci 12568c2ecf20Sopenharmony_ci/* 12578c2ecf20Sopenharmony_ci * Some usb host controllers can only perform dma using a small SRAM area. 12588c2ecf20Sopenharmony_ci * The usb core itself is however optimized for host controllers that can dma 12598c2ecf20Sopenharmony_ci * using regular system memory - like pci devices doing bus mastering. 12608c2ecf20Sopenharmony_ci * 12618c2ecf20Sopenharmony_ci * To support host controllers with limited dma capabilities we provide dma 12628c2ecf20Sopenharmony_ci * bounce buffers. This feature can be enabled by initializing 12638c2ecf20Sopenharmony_ci * hcd->localmem_pool using usb_hcd_setup_local_mem(). 12648c2ecf20Sopenharmony_ci * 12658c2ecf20Sopenharmony_ci * The initialized hcd->localmem_pool then tells the usb code to allocate all 12668c2ecf20Sopenharmony_ci * data for dma using the genalloc API. 12678c2ecf20Sopenharmony_ci * 12688c2ecf20Sopenharmony_ci * So, to summarize... 12698c2ecf20Sopenharmony_ci * 12708c2ecf20Sopenharmony_ci * - We need "local" memory, canonical example being 12718c2ecf20Sopenharmony_ci * a small SRAM on a discrete controller being the 12728c2ecf20Sopenharmony_ci * only memory that the controller can read ... 12738c2ecf20Sopenharmony_ci * (a) "normal" kernel memory is no good, and 12748c2ecf20Sopenharmony_ci * (b) there's not enough to share 12758c2ecf20Sopenharmony_ci * 12768c2ecf20Sopenharmony_ci * - So we use that, even though the primary requirement 12778c2ecf20Sopenharmony_ci * is that the memory be "local" (hence addressable 12788c2ecf20Sopenharmony_ci * by that device), not "coherent". 12798c2ecf20Sopenharmony_ci * 12808c2ecf20Sopenharmony_ci */ 12818c2ecf20Sopenharmony_ci 12828c2ecf20Sopenharmony_cistatic int hcd_alloc_coherent(struct usb_bus *bus, 12838c2ecf20Sopenharmony_ci gfp_t mem_flags, dma_addr_t *dma_handle, 12848c2ecf20Sopenharmony_ci void **vaddr_handle, size_t size, 12858c2ecf20Sopenharmony_ci enum dma_data_direction dir) 12868c2ecf20Sopenharmony_ci{ 12878c2ecf20Sopenharmony_ci unsigned char *vaddr; 12888c2ecf20Sopenharmony_ci 12898c2ecf20Sopenharmony_ci if (*vaddr_handle == NULL) { 12908c2ecf20Sopenharmony_ci WARN_ON_ONCE(1); 12918c2ecf20Sopenharmony_ci return -EFAULT; 12928c2ecf20Sopenharmony_ci } 12938c2ecf20Sopenharmony_ci 12948c2ecf20Sopenharmony_ci vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr), 12958c2ecf20Sopenharmony_ci mem_flags, dma_handle); 12968c2ecf20Sopenharmony_ci if (!vaddr) 12978c2ecf20Sopenharmony_ci return -ENOMEM; 12988c2ecf20Sopenharmony_ci 12998c2ecf20Sopenharmony_ci /* 13008c2ecf20Sopenharmony_ci * Store the virtual address of the buffer at the end 13018c2ecf20Sopenharmony_ci * of the allocated dma buffer. The size of the buffer 13028c2ecf20Sopenharmony_ci * may be uneven so use unaligned functions instead 13038c2ecf20Sopenharmony_ci * of just rounding up. It makes sense to optimize for 13048c2ecf20Sopenharmony_ci * memory footprint over access speed since the amount 13058c2ecf20Sopenharmony_ci * of memory available for dma may be limited. 13068c2ecf20Sopenharmony_ci */ 13078c2ecf20Sopenharmony_ci put_unaligned((unsigned long)*vaddr_handle, 13088c2ecf20Sopenharmony_ci (unsigned long *)(vaddr + size)); 13098c2ecf20Sopenharmony_ci 13108c2ecf20Sopenharmony_ci if (dir == DMA_TO_DEVICE) 13118c2ecf20Sopenharmony_ci memcpy(vaddr, *vaddr_handle, size); 13128c2ecf20Sopenharmony_ci 13138c2ecf20Sopenharmony_ci *vaddr_handle = vaddr; 13148c2ecf20Sopenharmony_ci return 0; 13158c2ecf20Sopenharmony_ci} 13168c2ecf20Sopenharmony_ci 13178c2ecf20Sopenharmony_cistatic void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle, 13188c2ecf20Sopenharmony_ci void **vaddr_handle, size_t size, 13198c2ecf20Sopenharmony_ci enum dma_data_direction dir) 13208c2ecf20Sopenharmony_ci{ 13218c2ecf20Sopenharmony_ci unsigned char *vaddr = *vaddr_handle; 13228c2ecf20Sopenharmony_ci 13238c2ecf20Sopenharmony_ci vaddr = (void *)get_unaligned((unsigned long *)(vaddr + size)); 13248c2ecf20Sopenharmony_ci 13258c2ecf20Sopenharmony_ci if (dir == DMA_FROM_DEVICE) 13268c2ecf20Sopenharmony_ci memcpy(vaddr, *vaddr_handle, size); 13278c2ecf20Sopenharmony_ci 13288c2ecf20Sopenharmony_ci hcd_buffer_free(bus, size + sizeof(vaddr), *vaddr_handle, *dma_handle); 13298c2ecf20Sopenharmony_ci 13308c2ecf20Sopenharmony_ci *vaddr_handle = vaddr; 13318c2ecf20Sopenharmony_ci *dma_handle = 0; 13328c2ecf20Sopenharmony_ci} 13338c2ecf20Sopenharmony_ci 13348c2ecf20Sopenharmony_civoid usb_hcd_unmap_urb_setup_for_dma(struct usb_hcd *hcd, struct urb *urb) 13358c2ecf20Sopenharmony_ci{ 13368c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_HAS_DMA) && 13378c2ecf20Sopenharmony_ci (urb->transfer_flags & URB_SETUP_MAP_SINGLE)) 13388c2ecf20Sopenharmony_ci dma_unmap_single(hcd->self.sysdev, 13398c2ecf20Sopenharmony_ci urb->setup_dma, 13408c2ecf20Sopenharmony_ci sizeof(struct usb_ctrlrequest), 13418c2ecf20Sopenharmony_ci DMA_TO_DEVICE); 13428c2ecf20Sopenharmony_ci else if (urb->transfer_flags & URB_SETUP_MAP_LOCAL) 13438c2ecf20Sopenharmony_ci hcd_free_coherent(urb->dev->bus, 13448c2ecf20Sopenharmony_ci &urb->setup_dma, 13458c2ecf20Sopenharmony_ci (void **) &urb->setup_packet, 13468c2ecf20Sopenharmony_ci sizeof(struct usb_ctrlrequest), 13478c2ecf20Sopenharmony_ci DMA_TO_DEVICE); 13488c2ecf20Sopenharmony_ci 13498c2ecf20Sopenharmony_ci /* Make it safe to call this routine more than once */ 13508c2ecf20Sopenharmony_ci urb->transfer_flags &= ~(URB_SETUP_MAP_SINGLE | URB_SETUP_MAP_LOCAL); 13518c2ecf20Sopenharmony_ci} 13528c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_hcd_unmap_urb_setup_for_dma); 13538c2ecf20Sopenharmony_ci 13548c2ecf20Sopenharmony_cistatic void unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb) 13558c2ecf20Sopenharmony_ci{ 13568c2ecf20Sopenharmony_ci if (hcd->driver->unmap_urb_for_dma) 13578c2ecf20Sopenharmony_ci hcd->driver->unmap_urb_for_dma(hcd, urb); 13588c2ecf20Sopenharmony_ci else 13598c2ecf20Sopenharmony_ci usb_hcd_unmap_urb_for_dma(hcd, urb); 13608c2ecf20Sopenharmony_ci} 13618c2ecf20Sopenharmony_ci 13628c2ecf20Sopenharmony_civoid usb_hcd_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb) 13638c2ecf20Sopenharmony_ci{ 13648c2ecf20Sopenharmony_ci enum dma_data_direction dir; 13658c2ecf20Sopenharmony_ci 13668c2ecf20Sopenharmony_ci usb_hcd_unmap_urb_setup_for_dma(hcd, urb); 13678c2ecf20Sopenharmony_ci 13688c2ecf20Sopenharmony_ci dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; 13698c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_HAS_DMA) && 13708c2ecf20Sopenharmony_ci (urb->transfer_flags & URB_DMA_MAP_SG)) 13718c2ecf20Sopenharmony_ci dma_unmap_sg(hcd->self.sysdev, 13728c2ecf20Sopenharmony_ci urb->sg, 13738c2ecf20Sopenharmony_ci urb->num_sgs, 13748c2ecf20Sopenharmony_ci dir); 13758c2ecf20Sopenharmony_ci else if (IS_ENABLED(CONFIG_HAS_DMA) && 13768c2ecf20Sopenharmony_ci (urb->transfer_flags & URB_DMA_MAP_PAGE)) 13778c2ecf20Sopenharmony_ci dma_unmap_page(hcd->self.sysdev, 13788c2ecf20Sopenharmony_ci urb->transfer_dma, 13798c2ecf20Sopenharmony_ci urb->transfer_buffer_length, 13808c2ecf20Sopenharmony_ci dir); 13818c2ecf20Sopenharmony_ci else if (IS_ENABLED(CONFIG_HAS_DMA) && 13828c2ecf20Sopenharmony_ci (urb->transfer_flags & URB_DMA_MAP_SINGLE)) 13838c2ecf20Sopenharmony_ci dma_unmap_single(hcd->self.sysdev, 13848c2ecf20Sopenharmony_ci urb->transfer_dma, 13858c2ecf20Sopenharmony_ci urb->transfer_buffer_length, 13868c2ecf20Sopenharmony_ci dir); 13878c2ecf20Sopenharmony_ci else if (urb->transfer_flags & URB_MAP_LOCAL) 13888c2ecf20Sopenharmony_ci hcd_free_coherent(urb->dev->bus, 13898c2ecf20Sopenharmony_ci &urb->transfer_dma, 13908c2ecf20Sopenharmony_ci &urb->transfer_buffer, 13918c2ecf20Sopenharmony_ci urb->transfer_buffer_length, 13928c2ecf20Sopenharmony_ci dir); 13938c2ecf20Sopenharmony_ci 13948c2ecf20Sopenharmony_ci /* Make it safe to call this routine more than once */ 13958c2ecf20Sopenharmony_ci urb->transfer_flags &= ~(URB_DMA_MAP_SG | URB_DMA_MAP_PAGE | 13968c2ecf20Sopenharmony_ci URB_DMA_MAP_SINGLE | URB_MAP_LOCAL); 13978c2ecf20Sopenharmony_ci} 13988c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_hcd_unmap_urb_for_dma); 13998c2ecf20Sopenharmony_ci 14008c2ecf20Sopenharmony_cistatic int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, 14018c2ecf20Sopenharmony_ci gfp_t mem_flags) 14028c2ecf20Sopenharmony_ci{ 14038c2ecf20Sopenharmony_ci if (hcd->driver->map_urb_for_dma) 14048c2ecf20Sopenharmony_ci return hcd->driver->map_urb_for_dma(hcd, urb, mem_flags); 14058c2ecf20Sopenharmony_ci else 14068c2ecf20Sopenharmony_ci return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags); 14078c2ecf20Sopenharmony_ci} 14088c2ecf20Sopenharmony_ci 14098c2ecf20Sopenharmony_ciint usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, 14108c2ecf20Sopenharmony_ci gfp_t mem_flags) 14118c2ecf20Sopenharmony_ci{ 14128c2ecf20Sopenharmony_ci enum dma_data_direction dir; 14138c2ecf20Sopenharmony_ci int ret = 0; 14148c2ecf20Sopenharmony_ci 14158c2ecf20Sopenharmony_ci /* Map the URB's buffers for DMA access. 14168c2ecf20Sopenharmony_ci * Lower level HCD code should use *_dma exclusively, 14178c2ecf20Sopenharmony_ci * unless it uses pio or talks to another transport, 14188c2ecf20Sopenharmony_ci * or uses the provided scatter gather list for bulk. 14198c2ecf20Sopenharmony_ci */ 14208c2ecf20Sopenharmony_ci 14218c2ecf20Sopenharmony_ci if (usb_endpoint_xfer_control(&urb->ep->desc)) { 14228c2ecf20Sopenharmony_ci if (hcd->self.uses_pio_for_control) 14238c2ecf20Sopenharmony_ci return ret; 14248c2ecf20Sopenharmony_ci if (hcd->localmem_pool) { 14258c2ecf20Sopenharmony_ci ret = hcd_alloc_coherent( 14268c2ecf20Sopenharmony_ci urb->dev->bus, mem_flags, 14278c2ecf20Sopenharmony_ci &urb->setup_dma, 14288c2ecf20Sopenharmony_ci (void **)&urb->setup_packet, 14298c2ecf20Sopenharmony_ci sizeof(struct usb_ctrlrequest), 14308c2ecf20Sopenharmony_ci DMA_TO_DEVICE); 14318c2ecf20Sopenharmony_ci if (ret) 14328c2ecf20Sopenharmony_ci return ret; 14338c2ecf20Sopenharmony_ci urb->transfer_flags |= URB_SETUP_MAP_LOCAL; 14348c2ecf20Sopenharmony_ci } else if (hcd_uses_dma(hcd)) { 14358c2ecf20Sopenharmony_ci if (object_is_on_stack(urb->setup_packet)) { 14368c2ecf20Sopenharmony_ci WARN_ONCE(1, "setup packet is on stack\n"); 14378c2ecf20Sopenharmony_ci return -EAGAIN; 14388c2ecf20Sopenharmony_ci } 14398c2ecf20Sopenharmony_ci 14408c2ecf20Sopenharmony_ci urb->setup_dma = dma_map_single( 14418c2ecf20Sopenharmony_ci hcd->self.sysdev, 14428c2ecf20Sopenharmony_ci urb->setup_packet, 14438c2ecf20Sopenharmony_ci sizeof(struct usb_ctrlrequest), 14448c2ecf20Sopenharmony_ci DMA_TO_DEVICE); 14458c2ecf20Sopenharmony_ci if (dma_mapping_error(hcd->self.sysdev, 14468c2ecf20Sopenharmony_ci urb->setup_dma)) 14478c2ecf20Sopenharmony_ci return -EAGAIN; 14488c2ecf20Sopenharmony_ci urb->transfer_flags |= URB_SETUP_MAP_SINGLE; 14498c2ecf20Sopenharmony_ci } 14508c2ecf20Sopenharmony_ci } 14518c2ecf20Sopenharmony_ci 14528c2ecf20Sopenharmony_ci dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; 14538c2ecf20Sopenharmony_ci if (urb->transfer_buffer_length != 0 14548c2ecf20Sopenharmony_ci && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) { 14558c2ecf20Sopenharmony_ci if (hcd->localmem_pool) { 14568c2ecf20Sopenharmony_ci ret = hcd_alloc_coherent( 14578c2ecf20Sopenharmony_ci urb->dev->bus, mem_flags, 14588c2ecf20Sopenharmony_ci &urb->transfer_dma, 14598c2ecf20Sopenharmony_ci &urb->transfer_buffer, 14608c2ecf20Sopenharmony_ci urb->transfer_buffer_length, 14618c2ecf20Sopenharmony_ci dir); 14628c2ecf20Sopenharmony_ci if (ret == 0) 14638c2ecf20Sopenharmony_ci urb->transfer_flags |= URB_MAP_LOCAL; 14648c2ecf20Sopenharmony_ci } else if (hcd_uses_dma(hcd)) { 14658c2ecf20Sopenharmony_ci if (urb->num_sgs) { 14668c2ecf20Sopenharmony_ci int n; 14678c2ecf20Sopenharmony_ci 14688c2ecf20Sopenharmony_ci /* We don't support sg for isoc transfers ! */ 14698c2ecf20Sopenharmony_ci if (usb_endpoint_xfer_isoc(&urb->ep->desc)) { 14708c2ecf20Sopenharmony_ci WARN_ON(1); 14718c2ecf20Sopenharmony_ci return -EINVAL; 14728c2ecf20Sopenharmony_ci } 14738c2ecf20Sopenharmony_ci 14748c2ecf20Sopenharmony_ci n = dma_map_sg( 14758c2ecf20Sopenharmony_ci hcd->self.sysdev, 14768c2ecf20Sopenharmony_ci urb->sg, 14778c2ecf20Sopenharmony_ci urb->num_sgs, 14788c2ecf20Sopenharmony_ci dir); 14798c2ecf20Sopenharmony_ci if (n <= 0) 14808c2ecf20Sopenharmony_ci ret = -EAGAIN; 14818c2ecf20Sopenharmony_ci else 14828c2ecf20Sopenharmony_ci urb->transfer_flags |= URB_DMA_MAP_SG; 14838c2ecf20Sopenharmony_ci urb->num_mapped_sgs = n; 14848c2ecf20Sopenharmony_ci if (n != urb->num_sgs) 14858c2ecf20Sopenharmony_ci urb->transfer_flags |= 14868c2ecf20Sopenharmony_ci URB_DMA_SG_COMBINED; 14878c2ecf20Sopenharmony_ci } else if (urb->sg) { 14888c2ecf20Sopenharmony_ci struct scatterlist *sg = urb->sg; 14898c2ecf20Sopenharmony_ci urb->transfer_dma = dma_map_page( 14908c2ecf20Sopenharmony_ci hcd->self.sysdev, 14918c2ecf20Sopenharmony_ci sg_page(sg), 14928c2ecf20Sopenharmony_ci sg->offset, 14938c2ecf20Sopenharmony_ci urb->transfer_buffer_length, 14948c2ecf20Sopenharmony_ci dir); 14958c2ecf20Sopenharmony_ci if (dma_mapping_error(hcd->self.sysdev, 14968c2ecf20Sopenharmony_ci urb->transfer_dma)) 14978c2ecf20Sopenharmony_ci ret = -EAGAIN; 14988c2ecf20Sopenharmony_ci else 14998c2ecf20Sopenharmony_ci urb->transfer_flags |= URB_DMA_MAP_PAGE; 15008c2ecf20Sopenharmony_ci } else if (object_is_on_stack(urb->transfer_buffer)) { 15018c2ecf20Sopenharmony_ci WARN_ONCE(1, "transfer buffer is on stack\n"); 15028c2ecf20Sopenharmony_ci ret = -EAGAIN; 15038c2ecf20Sopenharmony_ci } else { 15048c2ecf20Sopenharmony_ci urb->transfer_dma = dma_map_single( 15058c2ecf20Sopenharmony_ci hcd->self.sysdev, 15068c2ecf20Sopenharmony_ci urb->transfer_buffer, 15078c2ecf20Sopenharmony_ci urb->transfer_buffer_length, 15088c2ecf20Sopenharmony_ci dir); 15098c2ecf20Sopenharmony_ci if (dma_mapping_error(hcd->self.sysdev, 15108c2ecf20Sopenharmony_ci urb->transfer_dma)) 15118c2ecf20Sopenharmony_ci ret = -EAGAIN; 15128c2ecf20Sopenharmony_ci else 15138c2ecf20Sopenharmony_ci urb->transfer_flags |= URB_DMA_MAP_SINGLE; 15148c2ecf20Sopenharmony_ci } 15158c2ecf20Sopenharmony_ci } 15168c2ecf20Sopenharmony_ci if (ret && (urb->transfer_flags & (URB_SETUP_MAP_SINGLE | 15178c2ecf20Sopenharmony_ci URB_SETUP_MAP_LOCAL))) 15188c2ecf20Sopenharmony_ci usb_hcd_unmap_urb_for_dma(hcd, urb); 15198c2ecf20Sopenharmony_ci } 15208c2ecf20Sopenharmony_ci return ret; 15218c2ecf20Sopenharmony_ci} 15228c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_hcd_map_urb_for_dma); 15238c2ecf20Sopenharmony_ci 15248c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 15258c2ecf20Sopenharmony_ci 15268c2ecf20Sopenharmony_ci/* may be called in any context with a valid urb->dev usecount 15278c2ecf20Sopenharmony_ci * caller surrenders "ownership" of urb 15288c2ecf20Sopenharmony_ci * expects usb_submit_urb() to have sanity checked and conditioned all 15298c2ecf20Sopenharmony_ci * inputs in the urb 15308c2ecf20Sopenharmony_ci */ 15318c2ecf20Sopenharmony_ciint usb_hcd_submit_urb (struct urb *urb, gfp_t mem_flags) 15328c2ecf20Sopenharmony_ci{ 15338c2ecf20Sopenharmony_ci int status; 15348c2ecf20Sopenharmony_ci struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus); 15358c2ecf20Sopenharmony_ci 15368c2ecf20Sopenharmony_ci /* increment urb's reference count as part of giving it to the HCD 15378c2ecf20Sopenharmony_ci * (which will control it). HCD guarantees that it either returns 15388c2ecf20Sopenharmony_ci * an error or calls giveback(), but not both. 15398c2ecf20Sopenharmony_ci */ 15408c2ecf20Sopenharmony_ci usb_get_urb(urb); 15418c2ecf20Sopenharmony_ci atomic_inc(&urb->use_count); 15428c2ecf20Sopenharmony_ci atomic_inc(&urb->dev->urbnum); 15438c2ecf20Sopenharmony_ci usbmon_urb_submit(&hcd->self, urb); 15448c2ecf20Sopenharmony_ci 15458c2ecf20Sopenharmony_ci /* NOTE requirements on root-hub callers (usbfs and the hub 15468c2ecf20Sopenharmony_ci * driver, for now): URBs' urb->transfer_buffer must be 15478c2ecf20Sopenharmony_ci * valid and usb_buffer_{sync,unmap}() not be needed, since 15488c2ecf20Sopenharmony_ci * they could clobber root hub response data. Also, control 15498c2ecf20Sopenharmony_ci * URBs must be submitted in process context with interrupts 15508c2ecf20Sopenharmony_ci * enabled. 15518c2ecf20Sopenharmony_ci */ 15528c2ecf20Sopenharmony_ci 15538c2ecf20Sopenharmony_ci if (is_root_hub(urb->dev)) { 15548c2ecf20Sopenharmony_ci status = rh_urb_enqueue(hcd, urb); 15558c2ecf20Sopenharmony_ci } else { 15568c2ecf20Sopenharmony_ci status = map_urb_for_dma(hcd, urb, mem_flags); 15578c2ecf20Sopenharmony_ci if (likely(status == 0)) { 15588c2ecf20Sopenharmony_ci status = hcd->driver->urb_enqueue(hcd, urb, mem_flags); 15598c2ecf20Sopenharmony_ci if (unlikely(status)) 15608c2ecf20Sopenharmony_ci unmap_urb_for_dma(hcd, urb); 15618c2ecf20Sopenharmony_ci } 15628c2ecf20Sopenharmony_ci } 15638c2ecf20Sopenharmony_ci 15648c2ecf20Sopenharmony_ci if (unlikely(status)) { 15658c2ecf20Sopenharmony_ci usbmon_urb_submit_error(&hcd->self, urb, status); 15668c2ecf20Sopenharmony_ci urb->hcpriv = NULL; 15678c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&urb->urb_list); 15688c2ecf20Sopenharmony_ci atomic_dec(&urb->use_count); 15698c2ecf20Sopenharmony_ci /* 15708c2ecf20Sopenharmony_ci * Order the write of urb->use_count above before the read 15718c2ecf20Sopenharmony_ci * of urb->reject below. Pairs with the memory barriers in 15728c2ecf20Sopenharmony_ci * usb_kill_urb() and usb_poison_urb(). 15738c2ecf20Sopenharmony_ci */ 15748c2ecf20Sopenharmony_ci smp_mb__after_atomic(); 15758c2ecf20Sopenharmony_ci 15768c2ecf20Sopenharmony_ci atomic_dec(&urb->dev->urbnum); 15778c2ecf20Sopenharmony_ci if (atomic_read(&urb->reject)) 15788c2ecf20Sopenharmony_ci wake_up(&usb_kill_urb_queue); 15798c2ecf20Sopenharmony_ci usb_put_urb(urb); 15808c2ecf20Sopenharmony_ci } 15818c2ecf20Sopenharmony_ci return status; 15828c2ecf20Sopenharmony_ci} 15838c2ecf20Sopenharmony_ci 15848c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 15858c2ecf20Sopenharmony_ci 15868c2ecf20Sopenharmony_ci/* this makes the hcd giveback() the urb more quickly, by kicking it 15878c2ecf20Sopenharmony_ci * off hardware queues (which may take a while) and returning it as 15888c2ecf20Sopenharmony_ci * soon as practical. we've already set up the urb's return status, 15898c2ecf20Sopenharmony_ci * but we can't know if the callback completed already. 15908c2ecf20Sopenharmony_ci */ 15918c2ecf20Sopenharmony_cistatic int unlink1(struct usb_hcd *hcd, struct urb *urb, int status) 15928c2ecf20Sopenharmony_ci{ 15938c2ecf20Sopenharmony_ci int value; 15948c2ecf20Sopenharmony_ci 15958c2ecf20Sopenharmony_ci if (is_root_hub(urb->dev)) 15968c2ecf20Sopenharmony_ci value = usb_rh_urb_dequeue(hcd, urb, status); 15978c2ecf20Sopenharmony_ci else { 15988c2ecf20Sopenharmony_ci 15998c2ecf20Sopenharmony_ci /* The only reason an HCD might fail this call is if 16008c2ecf20Sopenharmony_ci * it has not yet fully queued the urb to begin with. 16018c2ecf20Sopenharmony_ci * Such failures should be harmless. */ 16028c2ecf20Sopenharmony_ci value = hcd->driver->urb_dequeue(hcd, urb, status); 16038c2ecf20Sopenharmony_ci } 16048c2ecf20Sopenharmony_ci return value; 16058c2ecf20Sopenharmony_ci} 16068c2ecf20Sopenharmony_ci 16078c2ecf20Sopenharmony_ci/* 16088c2ecf20Sopenharmony_ci * called in any context 16098c2ecf20Sopenharmony_ci * 16108c2ecf20Sopenharmony_ci * caller guarantees urb won't be recycled till both unlink() 16118c2ecf20Sopenharmony_ci * and the urb's completion function return 16128c2ecf20Sopenharmony_ci */ 16138c2ecf20Sopenharmony_ciint usb_hcd_unlink_urb (struct urb *urb, int status) 16148c2ecf20Sopenharmony_ci{ 16158c2ecf20Sopenharmony_ci struct usb_hcd *hcd; 16168c2ecf20Sopenharmony_ci struct usb_device *udev = urb->dev; 16178c2ecf20Sopenharmony_ci int retval = -EIDRM; 16188c2ecf20Sopenharmony_ci unsigned long flags; 16198c2ecf20Sopenharmony_ci 16208c2ecf20Sopenharmony_ci /* Prevent the device and bus from going away while 16218c2ecf20Sopenharmony_ci * the unlink is carried out. If they are already gone 16228c2ecf20Sopenharmony_ci * then urb->use_count must be 0, since disconnected 16238c2ecf20Sopenharmony_ci * devices can't have any active URBs. 16248c2ecf20Sopenharmony_ci */ 16258c2ecf20Sopenharmony_ci spin_lock_irqsave(&hcd_urb_unlink_lock, flags); 16268c2ecf20Sopenharmony_ci if (atomic_read(&urb->use_count) > 0) { 16278c2ecf20Sopenharmony_ci retval = 0; 16288c2ecf20Sopenharmony_ci usb_get_dev(udev); 16298c2ecf20Sopenharmony_ci } 16308c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&hcd_urb_unlink_lock, flags); 16318c2ecf20Sopenharmony_ci if (retval == 0) { 16328c2ecf20Sopenharmony_ci hcd = bus_to_hcd(urb->dev->bus); 16338c2ecf20Sopenharmony_ci retval = unlink1(hcd, urb, status); 16348c2ecf20Sopenharmony_ci if (retval == 0) 16358c2ecf20Sopenharmony_ci retval = -EINPROGRESS; 16368c2ecf20Sopenharmony_ci else if (retval != -EIDRM && retval != -EBUSY) 16378c2ecf20Sopenharmony_ci dev_dbg(&udev->dev, "hcd_unlink_urb %pK fail %d\n", 16388c2ecf20Sopenharmony_ci urb, retval); 16398c2ecf20Sopenharmony_ci usb_put_dev(udev); 16408c2ecf20Sopenharmony_ci } 16418c2ecf20Sopenharmony_ci return retval; 16428c2ecf20Sopenharmony_ci} 16438c2ecf20Sopenharmony_ci 16448c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 16458c2ecf20Sopenharmony_ci 16468c2ecf20Sopenharmony_cistatic void __usb_hcd_giveback_urb(struct urb *urb) 16478c2ecf20Sopenharmony_ci{ 16488c2ecf20Sopenharmony_ci struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus); 16498c2ecf20Sopenharmony_ci struct usb_anchor *anchor = urb->anchor; 16508c2ecf20Sopenharmony_ci int status = urb->unlinked; 16518c2ecf20Sopenharmony_ci 16528c2ecf20Sopenharmony_ci urb->hcpriv = NULL; 16538c2ecf20Sopenharmony_ci if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) && 16548c2ecf20Sopenharmony_ci urb->actual_length < urb->transfer_buffer_length && 16558c2ecf20Sopenharmony_ci !status)) 16568c2ecf20Sopenharmony_ci status = -EREMOTEIO; 16578c2ecf20Sopenharmony_ci 16588c2ecf20Sopenharmony_ci unmap_urb_for_dma(hcd, urb); 16598c2ecf20Sopenharmony_ci usbmon_urb_complete(&hcd->self, urb, status); 16608c2ecf20Sopenharmony_ci usb_anchor_suspend_wakeups(anchor); 16618c2ecf20Sopenharmony_ci usb_unanchor_urb(urb); 16628c2ecf20Sopenharmony_ci if (likely(status == 0)) 16638c2ecf20Sopenharmony_ci usb_led_activity(USB_LED_EVENT_HOST); 16648c2ecf20Sopenharmony_ci 16658c2ecf20Sopenharmony_ci /* pass ownership to the completion handler */ 16668c2ecf20Sopenharmony_ci urb->status = status; 16678c2ecf20Sopenharmony_ci /* 16688c2ecf20Sopenharmony_ci * This function can be called in task context inside another remote 16698c2ecf20Sopenharmony_ci * coverage collection section, but KCOV doesn't support that kind of 16708c2ecf20Sopenharmony_ci * recursion yet. Only collect coverage in softirq context for now. 16718c2ecf20Sopenharmony_ci */ 16728c2ecf20Sopenharmony_ci if (in_serving_softirq()) 16738c2ecf20Sopenharmony_ci kcov_remote_start_usb((u64)urb->dev->bus->busnum); 16748c2ecf20Sopenharmony_ci urb->complete(urb); 16758c2ecf20Sopenharmony_ci if (in_serving_softirq()) 16768c2ecf20Sopenharmony_ci kcov_remote_stop(); 16778c2ecf20Sopenharmony_ci 16788c2ecf20Sopenharmony_ci usb_anchor_resume_wakeups(anchor); 16798c2ecf20Sopenharmony_ci atomic_dec(&urb->use_count); 16808c2ecf20Sopenharmony_ci /* 16818c2ecf20Sopenharmony_ci * Order the write of urb->use_count above before the read 16828c2ecf20Sopenharmony_ci * of urb->reject below. Pairs with the memory barriers in 16838c2ecf20Sopenharmony_ci * usb_kill_urb() and usb_poison_urb(). 16848c2ecf20Sopenharmony_ci */ 16858c2ecf20Sopenharmony_ci smp_mb__after_atomic(); 16868c2ecf20Sopenharmony_ci 16878c2ecf20Sopenharmony_ci if (unlikely(atomic_read(&urb->reject))) 16888c2ecf20Sopenharmony_ci wake_up(&usb_kill_urb_queue); 16898c2ecf20Sopenharmony_ci usb_put_urb(urb); 16908c2ecf20Sopenharmony_ci} 16918c2ecf20Sopenharmony_ci 16928c2ecf20Sopenharmony_cistatic void usb_giveback_urb_bh(struct tasklet_struct *t) 16938c2ecf20Sopenharmony_ci{ 16948c2ecf20Sopenharmony_ci struct giveback_urb_bh *bh = from_tasklet(bh, t, bh); 16958c2ecf20Sopenharmony_ci struct list_head local_list; 16968c2ecf20Sopenharmony_ci 16978c2ecf20Sopenharmony_ci spin_lock_irq(&bh->lock); 16988c2ecf20Sopenharmony_ci bh->running = true; 16998c2ecf20Sopenharmony_ci list_replace_init(&bh->head, &local_list); 17008c2ecf20Sopenharmony_ci spin_unlock_irq(&bh->lock); 17018c2ecf20Sopenharmony_ci 17028c2ecf20Sopenharmony_ci while (!list_empty(&local_list)) { 17038c2ecf20Sopenharmony_ci struct urb *urb; 17048c2ecf20Sopenharmony_ci 17058c2ecf20Sopenharmony_ci urb = list_entry(local_list.next, struct urb, urb_list); 17068c2ecf20Sopenharmony_ci list_del_init(&urb->urb_list); 17078c2ecf20Sopenharmony_ci bh->completing_ep = urb->ep; 17088c2ecf20Sopenharmony_ci __usb_hcd_giveback_urb(urb); 17098c2ecf20Sopenharmony_ci bh->completing_ep = NULL; 17108c2ecf20Sopenharmony_ci } 17118c2ecf20Sopenharmony_ci 17128c2ecf20Sopenharmony_ci /* 17138c2ecf20Sopenharmony_ci * giveback new URBs next time to prevent this function 17148c2ecf20Sopenharmony_ci * from not exiting for a long time. 17158c2ecf20Sopenharmony_ci */ 17168c2ecf20Sopenharmony_ci spin_lock_irq(&bh->lock); 17178c2ecf20Sopenharmony_ci if (!list_empty(&bh->head)) { 17188c2ecf20Sopenharmony_ci if (bh->high_prio) 17198c2ecf20Sopenharmony_ci tasklet_hi_schedule(&bh->bh); 17208c2ecf20Sopenharmony_ci else 17218c2ecf20Sopenharmony_ci tasklet_schedule(&bh->bh); 17228c2ecf20Sopenharmony_ci } 17238c2ecf20Sopenharmony_ci bh->running = false; 17248c2ecf20Sopenharmony_ci spin_unlock_irq(&bh->lock); 17258c2ecf20Sopenharmony_ci} 17268c2ecf20Sopenharmony_ci 17278c2ecf20Sopenharmony_ci/** 17288c2ecf20Sopenharmony_ci * usb_hcd_giveback_urb - return URB from HCD to device driver 17298c2ecf20Sopenharmony_ci * @hcd: host controller returning the URB 17308c2ecf20Sopenharmony_ci * @urb: urb being returned to the USB device driver. 17318c2ecf20Sopenharmony_ci * @status: completion status code for the URB. 17328c2ecf20Sopenharmony_ci * Context: in_interrupt() 17338c2ecf20Sopenharmony_ci * 17348c2ecf20Sopenharmony_ci * This hands the URB from HCD to its USB device driver, using its 17358c2ecf20Sopenharmony_ci * completion function. The HCD has freed all per-urb resources 17368c2ecf20Sopenharmony_ci * (and is done using urb->hcpriv). It also released all HCD locks; 17378c2ecf20Sopenharmony_ci * the device driver won't cause problems if it frees, modifies, 17388c2ecf20Sopenharmony_ci * or resubmits this URB. 17398c2ecf20Sopenharmony_ci * 17408c2ecf20Sopenharmony_ci * If @urb was unlinked, the value of @status will be overridden by 17418c2ecf20Sopenharmony_ci * @urb->unlinked. Erroneous short transfers are detected in case 17428c2ecf20Sopenharmony_ci * the HCD hasn't checked for them. 17438c2ecf20Sopenharmony_ci */ 17448c2ecf20Sopenharmony_civoid usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb, int status) 17458c2ecf20Sopenharmony_ci{ 17468c2ecf20Sopenharmony_ci struct giveback_urb_bh *bh; 17478c2ecf20Sopenharmony_ci bool running; 17488c2ecf20Sopenharmony_ci 17498c2ecf20Sopenharmony_ci /* pass status to tasklet via unlinked */ 17508c2ecf20Sopenharmony_ci if (likely(!urb->unlinked)) 17518c2ecf20Sopenharmony_ci urb->unlinked = status; 17528c2ecf20Sopenharmony_ci 17538c2ecf20Sopenharmony_ci if (!hcd_giveback_urb_in_bh(hcd) && !is_root_hub(urb->dev)) { 17548c2ecf20Sopenharmony_ci __usb_hcd_giveback_urb(urb); 17558c2ecf20Sopenharmony_ci return; 17568c2ecf20Sopenharmony_ci } 17578c2ecf20Sopenharmony_ci 17588c2ecf20Sopenharmony_ci if (usb_pipeisoc(urb->pipe) || usb_pipeint(urb->pipe)) 17598c2ecf20Sopenharmony_ci bh = &hcd->high_prio_bh; 17608c2ecf20Sopenharmony_ci else 17618c2ecf20Sopenharmony_ci bh = &hcd->low_prio_bh; 17628c2ecf20Sopenharmony_ci 17638c2ecf20Sopenharmony_ci spin_lock(&bh->lock); 17648c2ecf20Sopenharmony_ci list_add_tail(&urb->urb_list, &bh->head); 17658c2ecf20Sopenharmony_ci running = bh->running; 17668c2ecf20Sopenharmony_ci spin_unlock(&bh->lock); 17678c2ecf20Sopenharmony_ci 17688c2ecf20Sopenharmony_ci if (running) 17698c2ecf20Sopenharmony_ci ; 17708c2ecf20Sopenharmony_ci else if (bh->high_prio) 17718c2ecf20Sopenharmony_ci tasklet_hi_schedule(&bh->bh); 17728c2ecf20Sopenharmony_ci else 17738c2ecf20Sopenharmony_ci tasklet_schedule(&bh->bh); 17748c2ecf20Sopenharmony_ci} 17758c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_hcd_giveback_urb); 17768c2ecf20Sopenharmony_ci 17778c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 17788c2ecf20Sopenharmony_ci 17798c2ecf20Sopenharmony_ci/* Cancel all URBs pending on this endpoint and wait for the endpoint's 17808c2ecf20Sopenharmony_ci * queue to drain completely. The caller must first insure that no more 17818c2ecf20Sopenharmony_ci * URBs can be submitted for this endpoint. 17828c2ecf20Sopenharmony_ci */ 17838c2ecf20Sopenharmony_civoid usb_hcd_flush_endpoint(struct usb_device *udev, 17848c2ecf20Sopenharmony_ci struct usb_host_endpoint *ep) 17858c2ecf20Sopenharmony_ci{ 17868c2ecf20Sopenharmony_ci struct usb_hcd *hcd; 17878c2ecf20Sopenharmony_ci struct urb *urb; 17888c2ecf20Sopenharmony_ci 17898c2ecf20Sopenharmony_ci if (!ep) 17908c2ecf20Sopenharmony_ci return; 17918c2ecf20Sopenharmony_ci might_sleep(); 17928c2ecf20Sopenharmony_ci hcd = bus_to_hcd(udev->bus); 17938c2ecf20Sopenharmony_ci 17948c2ecf20Sopenharmony_ci /* No more submits can occur */ 17958c2ecf20Sopenharmony_ci spin_lock_irq(&hcd_urb_list_lock); 17968c2ecf20Sopenharmony_cirescan: 17978c2ecf20Sopenharmony_ci list_for_each_entry_reverse(urb, &ep->urb_list, urb_list) { 17988c2ecf20Sopenharmony_ci int is_in; 17998c2ecf20Sopenharmony_ci 18008c2ecf20Sopenharmony_ci if (urb->unlinked) 18018c2ecf20Sopenharmony_ci continue; 18028c2ecf20Sopenharmony_ci usb_get_urb (urb); 18038c2ecf20Sopenharmony_ci is_in = usb_urb_dir_in(urb); 18048c2ecf20Sopenharmony_ci spin_unlock(&hcd_urb_list_lock); 18058c2ecf20Sopenharmony_ci 18068c2ecf20Sopenharmony_ci /* kick hcd */ 18078c2ecf20Sopenharmony_ci unlink1(hcd, urb, -ESHUTDOWN); 18088c2ecf20Sopenharmony_ci dev_dbg (hcd->self.controller, 18098c2ecf20Sopenharmony_ci "shutdown urb %pK ep%d%s-%s\n", 18108c2ecf20Sopenharmony_ci urb, usb_endpoint_num(&ep->desc), 18118c2ecf20Sopenharmony_ci is_in ? "in" : "out", 18128c2ecf20Sopenharmony_ci usb_ep_type_string(usb_endpoint_type(&ep->desc))); 18138c2ecf20Sopenharmony_ci usb_put_urb (urb); 18148c2ecf20Sopenharmony_ci 18158c2ecf20Sopenharmony_ci /* list contents may have changed */ 18168c2ecf20Sopenharmony_ci spin_lock(&hcd_urb_list_lock); 18178c2ecf20Sopenharmony_ci goto rescan; 18188c2ecf20Sopenharmony_ci } 18198c2ecf20Sopenharmony_ci spin_unlock_irq(&hcd_urb_list_lock); 18208c2ecf20Sopenharmony_ci 18218c2ecf20Sopenharmony_ci /* Wait until the endpoint queue is completely empty */ 18228c2ecf20Sopenharmony_ci while (!list_empty (&ep->urb_list)) { 18238c2ecf20Sopenharmony_ci spin_lock_irq(&hcd_urb_list_lock); 18248c2ecf20Sopenharmony_ci 18258c2ecf20Sopenharmony_ci /* The list may have changed while we acquired the spinlock */ 18268c2ecf20Sopenharmony_ci urb = NULL; 18278c2ecf20Sopenharmony_ci if (!list_empty (&ep->urb_list)) { 18288c2ecf20Sopenharmony_ci urb = list_entry (ep->urb_list.prev, struct urb, 18298c2ecf20Sopenharmony_ci urb_list); 18308c2ecf20Sopenharmony_ci usb_get_urb (urb); 18318c2ecf20Sopenharmony_ci } 18328c2ecf20Sopenharmony_ci spin_unlock_irq(&hcd_urb_list_lock); 18338c2ecf20Sopenharmony_ci 18348c2ecf20Sopenharmony_ci if (urb) { 18358c2ecf20Sopenharmony_ci usb_kill_urb (urb); 18368c2ecf20Sopenharmony_ci usb_put_urb (urb); 18378c2ecf20Sopenharmony_ci } 18388c2ecf20Sopenharmony_ci } 18398c2ecf20Sopenharmony_ci} 18408c2ecf20Sopenharmony_ci 18418c2ecf20Sopenharmony_ci/** 18428c2ecf20Sopenharmony_ci * usb_hcd_alloc_bandwidth - check whether a new bandwidth setting exceeds 18438c2ecf20Sopenharmony_ci * the bus bandwidth 18448c2ecf20Sopenharmony_ci * @udev: target &usb_device 18458c2ecf20Sopenharmony_ci * @new_config: new configuration to install 18468c2ecf20Sopenharmony_ci * @cur_alt: the current alternate interface setting 18478c2ecf20Sopenharmony_ci * @new_alt: alternate interface setting that is being installed 18488c2ecf20Sopenharmony_ci * 18498c2ecf20Sopenharmony_ci * To change configurations, pass in the new configuration in new_config, 18508c2ecf20Sopenharmony_ci * and pass NULL for cur_alt and new_alt. 18518c2ecf20Sopenharmony_ci * 18528c2ecf20Sopenharmony_ci * To reset a device's configuration (put the device in the ADDRESSED state), 18538c2ecf20Sopenharmony_ci * pass in NULL for new_config, cur_alt, and new_alt. 18548c2ecf20Sopenharmony_ci * 18558c2ecf20Sopenharmony_ci * To change alternate interface settings, pass in NULL for new_config, 18568c2ecf20Sopenharmony_ci * pass in the current alternate interface setting in cur_alt, 18578c2ecf20Sopenharmony_ci * and pass in the new alternate interface setting in new_alt. 18588c2ecf20Sopenharmony_ci * 18598c2ecf20Sopenharmony_ci * Return: An error if the requested bandwidth change exceeds the 18608c2ecf20Sopenharmony_ci * bus bandwidth or host controller internal resources. 18618c2ecf20Sopenharmony_ci */ 18628c2ecf20Sopenharmony_ciint usb_hcd_alloc_bandwidth(struct usb_device *udev, 18638c2ecf20Sopenharmony_ci struct usb_host_config *new_config, 18648c2ecf20Sopenharmony_ci struct usb_host_interface *cur_alt, 18658c2ecf20Sopenharmony_ci struct usb_host_interface *new_alt) 18668c2ecf20Sopenharmony_ci{ 18678c2ecf20Sopenharmony_ci int num_intfs, i, j; 18688c2ecf20Sopenharmony_ci struct usb_host_interface *alt = NULL; 18698c2ecf20Sopenharmony_ci int ret = 0; 18708c2ecf20Sopenharmony_ci struct usb_hcd *hcd; 18718c2ecf20Sopenharmony_ci struct usb_host_endpoint *ep; 18728c2ecf20Sopenharmony_ci 18738c2ecf20Sopenharmony_ci hcd = bus_to_hcd(udev->bus); 18748c2ecf20Sopenharmony_ci if (!hcd->driver->check_bandwidth) 18758c2ecf20Sopenharmony_ci return 0; 18768c2ecf20Sopenharmony_ci 18778c2ecf20Sopenharmony_ci /* Configuration is being removed - set configuration 0 */ 18788c2ecf20Sopenharmony_ci if (!new_config && !cur_alt) { 18798c2ecf20Sopenharmony_ci for (i = 1; i < 16; ++i) { 18808c2ecf20Sopenharmony_ci ep = udev->ep_out[i]; 18818c2ecf20Sopenharmony_ci if (ep) 18828c2ecf20Sopenharmony_ci hcd->driver->drop_endpoint(hcd, udev, ep); 18838c2ecf20Sopenharmony_ci ep = udev->ep_in[i]; 18848c2ecf20Sopenharmony_ci if (ep) 18858c2ecf20Sopenharmony_ci hcd->driver->drop_endpoint(hcd, udev, ep); 18868c2ecf20Sopenharmony_ci } 18878c2ecf20Sopenharmony_ci hcd->driver->check_bandwidth(hcd, udev); 18888c2ecf20Sopenharmony_ci return 0; 18898c2ecf20Sopenharmony_ci } 18908c2ecf20Sopenharmony_ci /* Check if the HCD says there's enough bandwidth. Enable all endpoints 18918c2ecf20Sopenharmony_ci * each interface's alt setting 0 and ask the HCD to check the bandwidth 18928c2ecf20Sopenharmony_ci * of the bus. There will always be bandwidth for endpoint 0, so it's 18938c2ecf20Sopenharmony_ci * ok to exclude it. 18948c2ecf20Sopenharmony_ci */ 18958c2ecf20Sopenharmony_ci if (new_config) { 18968c2ecf20Sopenharmony_ci num_intfs = new_config->desc.bNumInterfaces; 18978c2ecf20Sopenharmony_ci /* Remove endpoints (except endpoint 0, which is always on the 18988c2ecf20Sopenharmony_ci * schedule) from the old config from the schedule 18998c2ecf20Sopenharmony_ci */ 19008c2ecf20Sopenharmony_ci for (i = 1; i < 16; ++i) { 19018c2ecf20Sopenharmony_ci ep = udev->ep_out[i]; 19028c2ecf20Sopenharmony_ci if (ep) { 19038c2ecf20Sopenharmony_ci ret = hcd->driver->drop_endpoint(hcd, udev, ep); 19048c2ecf20Sopenharmony_ci if (ret < 0) 19058c2ecf20Sopenharmony_ci goto reset; 19068c2ecf20Sopenharmony_ci } 19078c2ecf20Sopenharmony_ci ep = udev->ep_in[i]; 19088c2ecf20Sopenharmony_ci if (ep) { 19098c2ecf20Sopenharmony_ci ret = hcd->driver->drop_endpoint(hcd, udev, ep); 19108c2ecf20Sopenharmony_ci if (ret < 0) 19118c2ecf20Sopenharmony_ci goto reset; 19128c2ecf20Sopenharmony_ci } 19138c2ecf20Sopenharmony_ci } 19148c2ecf20Sopenharmony_ci for (i = 0; i < num_intfs; ++i) { 19158c2ecf20Sopenharmony_ci struct usb_host_interface *first_alt; 19168c2ecf20Sopenharmony_ci int iface_num; 19178c2ecf20Sopenharmony_ci 19188c2ecf20Sopenharmony_ci first_alt = &new_config->intf_cache[i]->altsetting[0]; 19198c2ecf20Sopenharmony_ci iface_num = first_alt->desc.bInterfaceNumber; 19208c2ecf20Sopenharmony_ci /* Set up endpoints for alternate interface setting 0 */ 19218c2ecf20Sopenharmony_ci alt = usb_find_alt_setting(new_config, iface_num, 0); 19228c2ecf20Sopenharmony_ci if (!alt) 19238c2ecf20Sopenharmony_ci /* No alt setting 0? Pick the first setting. */ 19248c2ecf20Sopenharmony_ci alt = first_alt; 19258c2ecf20Sopenharmony_ci 19268c2ecf20Sopenharmony_ci for (j = 0; j < alt->desc.bNumEndpoints; j++) { 19278c2ecf20Sopenharmony_ci ret = hcd->driver->add_endpoint(hcd, udev, &alt->endpoint[j]); 19288c2ecf20Sopenharmony_ci if (ret < 0) 19298c2ecf20Sopenharmony_ci goto reset; 19308c2ecf20Sopenharmony_ci } 19318c2ecf20Sopenharmony_ci } 19328c2ecf20Sopenharmony_ci } 19338c2ecf20Sopenharmony_ci if (cur_alt && new_alt) { 19348c2ecf20Sopenharmony_ci struct usb_interface *iface = usb_ifnum_to_if(udev, 19358c2ecf20Sopenharmony_ci cur_alt->desc.bInterfaceNumber); 19368c2ecf20Sopenharmony_ci 19378c2ecf20Sopenharmony_ci if (!iface) 19388c2ecf20Sopenharmony_ci return -EINVAL; 19398c2ecf20Sopenharmony_ci if (iface->resetting_device) { 19408c2ecf20Sopenharmony_ci /* 19418c2ecf20Sopenharmony_ci * The USB core just reset the device, so the xHCI host 19428c2ecf20Sopenharmony_ci * and the device will think alt setting 0 is installed. 19438c2ecf20Sopenharmony_ci * However, the USB core will pass in the alternate 19448c2ecf20Sopenharmony_ci * setting installed before the reset as cur_alt. Dig 19458c2ecf20Sopenharmony_ci * out the alternate setting 0 structure, or the first 19468c2ecf20Sopenharmony_ci * alternate setting if a broken device doesn't have alt 19478c2ecf20Sopenharmony_ci * setting 0. 19488c2ecf20Sopenharmony_ci */ 19498c2ecf20Sopenharmony_ci cur_alt = usb_altnum_to_altsetting(iface, 0); 19508c2ecf20Sopenharmony_ci if (!cur_alt) 19518c2ecf20Sopenharmony_ci cur_alt = &iface->altsetting[0]; 19528c2ecf20Sopenharmony_ci } 19538c2ecf20Sopenharmony_ci 19548c2ecf20Sopenharmony_ci /* Drop all the endpoints in the current alt setting */ 19558c2ecf20Sopenharmony_ci for (i = 0; i < cur_alt->desc.bNumEndpoints; i++) { 19568c2ecf20Sopenharmony_ci ret = hcd->driver->drop_endpoint(hcd, udev, 19578c2ecf20Sopenharmony_ci &cur_alt->endpoint[i]); 19588c2ecf20Sopenharmony_ci if (ret < 0) 19598c2ecf20Sopenharmony_ci goto reset; 19608c2ecf20Sopenharmony_ci } 19618c2ecf20Sopenharmony_ci /* Add all the endpoints in the new alt setting */ 19628c2ecf20Sopenharmony_ci for (i = 0; i < new_alt->desc.bNumEndpoints; i++) { 19638c2ecf20Sopenharmony_ci ret = hcd->driver->add_endpoint(hcd, udev, 19648c2ecf20Sopenharmony_ci &new_alt->endpoint[i]); 19658c2ecf20Sopenharmony_ci if (ret < 0) 19668c2ecf20Sopenharmony_ci goto reset; 19678c2ecf20Sopenharmony_ci } 19688c2ecf20Sopenharmony_ci } 19698c2ecf20Sopenharmony_ci ret = hcd->driver->check_bandwidth(hcd, udev); 19708c2ecf20Sopenharmony_cireset: 19718c2ecf20Sopenharmony_ci if (ret < 0) 19728c2ecf20Sopenharmony_ci hcd->driver->reset_bandwidth(hcd, udev); 19738c2ecf20Sopenharmony_ci return ret; 19748c2ecf20Sopenharmony_ci} 19758c2ecf20Sopenharmony_ci 19768c2ecf20Sopenharmony_ci/* Disables the endpoint: synchronizes with the hcd to make sure all 19778c2ecf20Sopenharmony_ci * endpoint state is gone from hardware. usb_hcd_flush_endpoint() must 19788c2ecf20Sopenharmony_ci * have been called previously. Use for set_configuration, set_interface, 19798c2ecf20Sopenharmony_ci * driver removal, physical disconnect. 19808c2ecf20Sopenharmony_ci * 19818c2ecf20Sopenharmony_ci * example: a qh stored in ep->hcpriv, holding state related to endpoint 19828c2ecf20Sopenharmony_ci * type, maxpacket size, toggle, halt status, and scheduling. 19838c2ecf20Sopenharmony_ci */ 19848c2ecf20Sopenharmony_civoid usb_hcd_disable_endpoint(struct usb_device *udev, 19858c2ecf20Sopenharmony_ci struct usb_host_endpoint *ep) 19868c2ecf20Sopenharmony_ci{ 19878c2ecf20Sopenharmony_ci struct usb_hcd *hcd; 19888c2ecf20Sopenharmony_ci 19898c2ecf20Sopenharmony_ci might_sleep(); 19908c2ecf20Sopenharmony_ci hcd = bus_to_hcd(udev->bus); 19918c2ecf20Sopenharmony_ci if (hcd->driver->endpoint_disable) 19928c2ecf20Sopenharmony_ci hcd->driver->endpoint_disable(hcd, ep); 19938c2ecf20Sopenharmony_ci} 19948c2ecf20Sopenharmony_ci 19958c2ecf20Sopenharmony_ci/** 19968c2ecf20Sopenharmony_ci * usb_hcd_reset_endpoint - reset host endpoint state 19978c2ecf20Sopenharmony_ci * @udev: USB device. 19988c2ecf20Sopenharmony_ci * @ep: the endpoint to reset. 19998c2ecf20Sopenharmony_ci * 20008c2ecf20Sopenharmony_ci * Resets any host endpoint state such as the toggle bit, sequence 20018c2ecf20Sopenharmony_ci * number and current window. 20028c2ecf20Sopenharmony_ci */ 20038c2ecf20Sopenharmony_civoid usb_hcd_reset_endpoint(struct usb_device *udev, 20048c2ecf20Sopenharmony_ci struct usb_host_endpoint *ep) 20058c2ecf20Sopenharmony_ci{ 20068c2ecf20Sopenharmony_ci struct usb_hcd *hcd = bus_to_hcd(udev->bus); 20078c2ecf20Sopenharmony_ci 20088c2ecf20Sopenharmony_ci if (hcd->driver->endpoint_reset) 20098c2ecf20Sopenharmony_ci hcd->driver->endpoint_reset(hcd, ep); 20108c2ecf20Sopenharmony_ci else { 20118c2ecf20Sopenharmony_ci int epnum = usb_endpoint_num(&ep->desc); 20128c2ecf20Sopenharmony_ci int is_out = usb_endpoint_dir_out(&ep->desc); 20138c2ecf20Sopenharmony_ci int is_control = usb_endpoint_xfer_control(&ep->desc); 20148c2ecf20Sopenharmony_ci 20158c2ecf20Sopenharmony_ci usb_settoggle(udev, epnum, is_out, 0); 20168c2ecf20Sopenharmony_ci if (is_control) 20178c2ecf20Sopenharmony_ci usb_settoggle(udev, epnum, !is_out, 0); 20188c2ecf20Sopenharmony_ci } 20198c2ecf20Sopenharmony_ci} 20208c2ecf20Sopenharmony_ci 20218c2ecf20Sopenharmony_ci/** 20228c2ecf20Sopenharmony_ci * usb_alloc_streams - allocate bulk endpoint stream IDs. 20238c2ecf20Sopenharmony_ci * @interface: alternate setting that includes all endpoints. 20248c2ecf20Sopenharmony_ci * @eps: array of endpoints that need streams. 20258c2ecf20Sopenharmony_ci * @num_eps: number of endpoints in the array. 20268c2ecf20Sopenharmony_ci * @num_streams: number of streams to allocate. 20278c2ecf20Sopenharmony_ci * @mem_flags: flags hcd should use to allocate memory. 20288c2ecf20Sopenharmony_ci * 20298c2ecf20Sopenharmony_ci * Sets up a group of bulk endpoints to have @num_streams stream IDs available. 20308c2ecf20Sopenharmony_ci * Drivers may queue multiple transfers to different stream IDs, which may 20318c2ecf20Sopenharmony_ci * complete in a different order than they were queued. 20328c2ecf20Sopenharmony_ci * 20338c2ecf20Sopenharmony_ci * Return: On success, the number of allocated streams. On failure, a negative 20348c2ecf20Sopenharmony_ci * error code. 20358c2ecf20Sopenharmony_ci */ 20368c2ecf20Sopenharmony_ciint usb_alloc_streams(struct usb_interface *interface, 20378c2ecf20Sopenharmony_ci struct usb_host_endpoint **eps, unsigned int num_eps, 20388c2ecf20Sopenharmony_ci unsigned int num_streams, gfp_t mem_flags) 20398c2ecf20Sopenharmony_ci{ 20408c2ecf20Sopenharmony_ci struct usb_hcd *hcd; 20418c2ecf20Sopenharmony_ci struct usb_device *dev; 20428c2ecf20Sopenharmony_ci int i, ret; 20438c2ecf20Sopenharmony_ci 20448c2ecf20Sopenharmony_ci dev = interface_to_usbdev(interface); 20458c2ecf20Sopenharmony_ci hcd = bus_to_hcd(dev->bus); 20468c2ecf20Sopenharmony_ci if (!hcd->driver->alloc_streams || !hcd->driver->free_streams) 20478c2ecf20Sopenharmony_ci return -EINVAL; 20488c2ecf20Sopenharmony_ci if (dev->speed < USB_SPEED_SUPER) 20498c2ecf20Sopenharmony_ci return -EINVAL; 20508c2ecf20Sopenharmony_ci if (dev->state < USB_STATE_CONFIGURED) 20518c2ecf20Sopenharmony_ci return -ENODEV; 20528c2ecf20Sopenharmony_ci 20538c2ecf20Sopenharmony_ci for (i = 0; i < num_eps; i++) { 20548c2ecf20Sopenharmony_ci /* Streams only apply to bulk endpoints. */ 20558c2ecf20Sopenharmony_ci if (!usb_endpoint_xfer_bulk(&eps[i]->desc)) 20568c2ecf20Sopenharmony_ci return -EINVAL; 20578c2ecf20Sopenharmony_ci /* Re-alloc is not allowed */ 20588c2ecf20Sopenharmony_ci if (eps[i]->streams) 20598c2ecf20Sopenharmony_ci return -EINVAL; 20608c2ecf20Sopenharmony_ci } 20618c2ecf20Sopenharmony_ci 20628c2ecf20Sopenharmony_ci ret = hcd->driver->alloc_streams(hcd, dev, eps, num_eps, 20638c2ecf20Sopenharmony_ci num_streams, mem_flags); 20648c2ecf20Sopenharmony_ci if (ret < 0) 20658c2ecf20Sopenharmony_ci return ret; 20668c2ecf20Sopenharmony_ci 20678c2ecf20Sopenharmony_ci for (i = 0; i < num_eps; i++) 20688c2ecf20Sopenharmony_ci eps[i]->streams = ret; 20698c2ecf20Sopenharmony_ci 20708c2ecf20Sopenharmony_ci return ret; 20718c2ecf20Sopenharmony_ci} 20728c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_alloc_streams); 20738c2ecf20Sopenharmony_ci 20748c2ecf20Sopenharmony_ci/** 20758c2ecf20Sopenharmony_ci * usb_free_streams - free bulk endpoint stream IDs. 20768c2ecf20Sopenharmony_ci * @interface: alternate setting that includes all endpoints. 20778c2ecf20Sopenharmony_ci * @eps: array of endpoints to remove streams from. 20788c2ecf20Sopenharmony_ci * @num_eps: number of endpoints in the array. 20798c2ecf20Sopenharmony_ci * @mem_flags: flags hcd should use to allocate memory. 20808c2ecf20Sopenharmony_ci * 20818c2ecf20Sopenharmony_ci * Reverts a group of bulk endpoints back to not using stream IDs. 20828c2ecf20Sopenharmony_ci * Can fail if we are given bad arguments, or HCD is broken. 20838c2ecf20Sopenharmony_ci * 20848c2ecf20Sopenharmony_ci * Return: 0 on success. On failure, a negative error code. 20858c2ecf20Sopenharmony_ci */ 20868c2ecf20Sopenharmony_ciint usb_free_streams(struct usb_interface *interface, 20878c2ecf20Sopenharmony_ci struct usb_host_endpoint **eps, unsigned int num_eps, 20888c2ecf20Sopenharmony_ci gfp_t mem_flags) 20898c2ecf20Sopenharmony_ci{ 20908c2ecf20Sopenharmony_ci struct usb_hcd *hcd; 20918c2ecf20Sopenharmony_ci struct usb_device *dev; 20928c2ecf20Sopenharmony_ci int i, ret; 20938c2ecf20Sopenharmony_ci 20948c2ecf20Sopenharmony_ci dev = interface_to_usbdev(interface); 20958c2ecf20Sopenharmony_ci hcd = bus_to_hcd(dev->bus); 20968c2ecf20Sopenharmony_ci if (dev->speed < USB_SPEED_SUPER) 20978c2ecf20Sopenharmony_ci return -EINVAL; 20988c2ecf20Sopenharmony_ci 20998c2ecf20Sopenharmony_ci /* Double-free is not allowed */ 21008c2ecf20Sopenharmony_ci for (i = 0; i < num_eps; i++) 21018c2ecf20Sopenharmony_ci if (!eps[i] || !eps[i]->streams) 21028c2ecf20Sopenharmony_ci return -EINVAL; 21038c2ecf20Sopenharmony_ci 21048c2ecf20Sopenharmony_ci ret = hcd->driver->free_streams(hcd, dev, eps, num_eps, mem_flags); 21058c2ecf20Sopenharmony_ci if (ret < 0) 21068c2ecf20Sopenharmony_ci return ret; 21078c2ecf20Sopenharmony_ci 21088c2ecf20Sopenharmony_ci for (i = 0; i < num_eps; i++) 21098c2ecf20Sopenharmony_ci eps[i]->streams = 0; 21108c2ecf20Sopenharmony_ci 21118c2ecf20Sopenharmony_ci return ret; 21128c2ecf20Sopenharmony_ci} 21138c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_free_streams); 21148c2ecf20Sopenharmony_ci 21158c2ecf20Sopenharmony_ci/* Protect against drivers that try to unlink URBs after the device 21168c2ecf20Sopenharmony_ci * is gone, by waiting until all unlinks for @udev are finished. 21178c2ecf20Sopenharmony_ci * Since we don't currently track URBs by device, simply wait until 21188c2ecf20Sopenharmony_ci * nothing is running in the locked region of usb_hcd_unlink_urb(). 21198c2ecf20Sopenharmony_ci */ 21208c2ecf20Sopenharmony_civoid usb_hcd_synchronize_unlinks(struct usb_device *udev) 21218c2ecf20Sopenharmony_ci{ 21228c2ecf20Sopenharmony_ci spin_lock_irq(&hcd_urb_unlink_lock); 21238c2ecf20Sopenharmony_ci spin_unlock_irq(&hcd_urb_unlink_lock); 21248c2ecf20Sopenharmony_ci} 21258c2ecf20Sopenharmony_ci 21268c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 21278c2ecf20Sopenharmony_ci 21288c2ecf20Sopenharmony_ci/* called in any context */ 21298c2ecf20Sopenharmony_ciint usb_hcd_get_frame_number (struct usb_device *udev) 21308c2ecf20Sopenharmony_ci{ 21318c2ecf20Sopenharmony_ci struct usb_hcd *hcd = bus_to_hcd(udev->bus); 21328c2ecf20Sopenharmony_ci 21338c2ecf20Sopenharmony_ci if (!HCD_RH_RUNNING(hcd)) 21348c2ecf20Sopenharmony_ci return -ESHUTDOWN; 21358c2ecf20Sopenharmony_ci return hcd->driver->get_frame_number (hcd); 21368c2ecf20Sopenharmony_ci} 21378c2ecf20Sopenharmony_ci 21388c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 21398c2ecf20Sopenharmony_ci 21408c2ecf20Sopenharmony_ci#ifdef CONFIG_PM 21418c2ecf20Sopenharmony_ci 21428c2ecf20Sopenharmony_ciint hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg) 21438c2ecf20Sopenharmony_ci{ 21448c2ecf20Sopenharmony_ci struct usb_hcd *hcd = bus_to_hcd(rhdev->bus); 21458c2ecf20Sopenharmony_ci int status; 21468c2ecf20Sopenharmony_ci int old_state = hcd->state; 21478c2ecf20Sopenharmony_ci 21488c2ecf20Sopenharmony_ci dev_dbg(&rhdev->dev, "bus %ssuspend, wakeup %d\n", 21498c2ecf20Sopenharmony_ci (PMSG_IS_AUTO(msg) ? "auto-" : ""), 21508c2ecf20Sopenharmony_ci rhdev->do_remote_wakeup); 21518c2ecf20Sopenharmony_ci if (HCD_DEAD(hcd)) { 21528c2ecf20Sopenharmony_ci dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "suspend"); 21538c2ecf20Sopenharmony_ci return 0; 21548c2ecf20Sopenharmony_ci } 21558c2ecf20Sopenharmony_ci 21568c2ecf20Sopenharmony_ci if (!hcd->driver->bus_suspend) { 21578c2ecf20Sopenharmony_ci status = -ENOENT; 21588c2ecf20Sopenharmony_ci } else { 21598c2ecf20Sopenharmony_ci clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); 21608c2ecf20Sopenharmony_ci hcd->state = HC_STATE_QUIESCING; 21618c2ecf20Sopenharmony_ci status = hcd->driver->bus_suspend(hcd); 21628c2ecf20Sopenharmony_ci } 21638c2ecf20Sopenharmony_ci if (status == 0) { 21648c2ecf20Sopenharmony_ci usb_set_device_state(rhdev, USB_STATE_SUSPENDED); 21658c2ecf20Sopenharmony_ci hcd->state = HC_STATE_SUSPENDED; 21668c2ecf20Sopenharmony_ci 21678c2ecf20Sopenharmony_ci if (!PMSG_IS_AUTO(msg)) 21688c2ecf20Sopenharmony_ci usb_phy_roothub_suspend(hcd->self.sysdev, 21698c2ecf20Sopenharmony_ci hcd->phy_roothub); 21708c2ecf20Sopenharmony_ci 21718c2ecf20Sopenharmony_ci /* Did we race with a root-hub wakeup event? */ 21728c2ecf20Sopenharmony_ci if (rhdev->do_remote_wakeup) { 21738c2ecf20Sopenharmony_ci char buffer[6]; 21748c2ecf20Sopenharmony_ci 21758c2ecf20Sopenharmony_ci status = hcd->driver->hub_status_data(hcd, buffer); 21768c2ecf20Sopenharmony_ci if (status != 0) { 21778c2ecf20Sopenharmony_ci dev_dbg(&rhdev->dev, "suspend raced with wakeup event\n"); 21788c2ecf20Sopenharmony_ci hcd_bus_resume(rhdev, PMSG_AUTO_RESUME); 21798c2ecf20Sopenharmony_ci status = -EBUSY; 21808c2ecf20Sopenharmony_ci } 21818c2ecf20Sopenharmony_ci } 21828c2ecf20Sopenharmony_ci } else { 21838c2ecf20Sopenharmony_ci spin_lock_irq(&hcd_root_hub_lock); 21848c2ecf20Sopenharmony_ci if (!HCD_DEAD(hcd)) { 21858c2ecf20Sopenharmony_ci set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); 21868c2ecf20Sopenharmony_ci hcd->state = old_state; 21878c2ecf20Sopenharmony_ci } 21888c2ecf20Sopenharmony_ci spin_unlock_irq(&hcd_root_hub_lock); 21898c2ecf20Sopenharmony_ci dev_dbg(&rhdev->dev, "bus %s fail, err %d\n", 21908c2ecf20Sopenharmony_ci "suspend", status); 21918c2ecf20Sopenharmony_ci } 21928c2ecf20Sopenharmony_ci return status; 21938c2ecf20Sopenharmony_ci} 21948c2ecf20Sopenharmony_ci 21958c2ecf20Sopenharmony_ciint hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg) 21968c2ecf20Sopenharmony_ci{ 21978c2ecf20Sopenharmony_ci struct usb_hcd *hcd = bus_to_hcd(rhdev->bus); 21988c2ecf20Sopenharmony_ci int status; 21998c2ecf20Sopenharmony_ci int old_state = hcd->state; 22008c2ecf20Sopenharmony_ci 22018c2ecf20Sopenharmony_ci dev_dbg(&rhdev->dev, "usb %sresume\n", 22028c2ecf20Sopenharmony_ci (PMSG_IS_AUTO(msg) ? "auto-" : "")); 22038c2ecf20Sopenharmony_ci if (HCD_DEAD(hcd)) { 22048c2ecf20Sopenharmony_ci dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "resume"); 22058c2ecf20Sopenharmony_ci return 0; 22068c2ecf20Sopenharmony_ci } 22078c2ecf20Sopenharmony_ci 22088c2ecf20Sopenharmony_ci if (!PMSG_IS_AUTO(msg)) { 22098c2ecf20Sopenharmony_ci status = usb_phy_roothub_resume(hcd->self.sysdev, 22108c2ecf20Sopenharmony_ci hcd->phy_roothub); 22118c2ecf20Sopenharmony_ci if (status) 22128c2ecf20Sopenharmony_ci return status; 22138c2ecf20Sopenharmony_ci } 22148c2ecf20Sopenharmony_ci 22158c2ecf20Sopenharmony_ci if (!hcd->driver->bus_resume) 22168c2ecf20Sopenharmony_ci return -ENOENT; 22178c2ecf20Sopenharmony_ci if (HCD_RH_RUNNING(hcd)) 22188c2ecf20Sopenharmony_ci return 0; 22198c2ecf20Sopenharmony_ci 22208c2ecf20Sopenharmony_ci hcd->state = HC_STATE_RESUMING; 22218c2ecf20Sopenharmony_ci status = hcd->driver->bus_resume(hcd); 22228c2ecf20Sopenharmony_ci clear_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags); 22238c2ecf20Sopenharmony_ci if (status == 0) 22248c2ecf20Sopenharmony_ci status = usb_phy_roothub_calibrate(hcd->phy_roothub); 22258c2ecf20Sopenharmony_ci 22268c2ecf20Sopenharmony_ci if (status == 0) { 22278c2ecf20Sopenharmony_ci struct usb_device *udev; 22288c2ecf20Sopenharmony_ci int port1; 22298c2ecf20Sopenharmony_ci 22308c2ecf20Sopenharmony_ci spin_lock_irq(&hcd_root_hub_lock); 22318c2ecf20Sopenharmony_ci if (!HCD_DEAD(hcd)) { 22328c2ecf20Sopenharmony_ci usb_set_device_state(rhdev, rhdev->actconfig 22338c2ecf20Sopenharmony_ci ? USB_STATE_CONFIGURED 22348c2ecf20Sopenharmony_ci : USB_STATE_ADDRESS); 22358c2ecf20Sopenharmony_ci set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); 22368c2ecf20Sopenharmony_ci hcd->state = HC_STATE_RUNNING; 22378c2ecf20Sopenharmony_ci } 22388c2ecf20Sopenharmony_ci spin_unlock_irq(&hcd_root_hub_lock); 22398c2ecf20Sopenharmony_ci 22408c2ecf20Sopenharmony_ci /* 22418c2ecf20Sopenharmony_ci * Check whether any of the enabled ports on the root hub are 22428c2ecf20Sopenharmony_ci * unsuspended. If they are then a TRSMRCY delay is needed 22438c2ecf20Sopenharmony_ci * (this is what the USB-2 spec calls a "global resume"). 22448c2ecf20Sopenharmony_ci * Otherwise we can skip the delay. 22458c2ecf20Sopenharmony_ci */ 22468c2ecf20Sopenharmony_ci usb_hub_for_each_child(rhdev, port1, udev) { 22478c2ecf20Sopenharmony_ci if (udev->state != USB_STATE_NOTATTACHED && 22488c2ecf20Sopenharmony_ci !udev->port_is_suspended) { 22498c2ecf20Sopenharmony_ci usleep_range(10000, 11000); /* TRSMRCY */ 22508c2ecf20Sopenharmony_ci break; 22518c2ecf20Sopenharmony_ci } 22528c2ecf20Sopenharmony_ci } 22538c2ecf20Sopenharmony_ci } else { 22548c2ecf20Sopenharmony_ci hcd->state = old_state; 22558c2ecf20Sopenharmony_ci usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub); 22568c2ecf20Sopenharmony_ci dev_dbg(&rhdev->dev, "bus %s fail, err %d\n", 22578c2ecf20Sopenharmony_ci "resume", status); 22588c2ecf20Sopenharmony_ci if (status != -ESHUTDOWN) 22598c2ecf20Sopenharmony_ci usb_hc_died(hcd); 22608c2ecf20Sopenharmony_ci } 22618c2ecf20Sopenharmony_ci return status; 22628c2ecf20Sopenharmony_ci} 22638c2ecf20Sopenharmony_ci 22648c2ecf20Sopenharmony_ci/* Workqueue routine for root-hub remote wakeup */ 22658c2ecf20Sopenharmony_cistatic void hcd_resume_work(struct work_struct *work) 22668c2ecf20Sopenharmony_ci{ 22678c2ecf20Sopenharmony_ci struct usb_hcd *hcd = container_of(work, struct usb_hcd, wakeup_work); 22688c2ecf20Sopenharmony_ci struct usb_device *udev = hcd->self.root_hub; 22698c2ecf20Sopenharmony_ci 22708c2ecf20Sopenharmony_ci usb_remote_wakeup(udev); 22718c2ecf20Sopenharmony_ci} 22728c2ecf20Sopenharmony_ci 22738c2ecf20Sopenharmony_ci/** 22748c2ecf20Sopenharmony_ci * usb_hcd_resume_root_hub - called by HCD to resume its root hub 22758c2ecf20Sopenharmony_ci * @hcd: host controller for this root hub 22768c2ecf20Sopenharmony_ci * 22778c2ecf20Sopenharmony_ci * The USB host controller calls this function when its root hub is 22788c2ecf20Sopenharmony_ci * suspended (with the remote wakeup feature enabled) and a remote 22798c2ecf20Sopenharmony_ci * wakeup request is received. The routine submits a workqueue request 22808c2ecf20Sopenharmony_ci * to resume the root hub (that is, manage its downstream ports again). 22818c2ecf20Sopenharmony_ci */ 22828c2ecf20Sopenharmony_civoid usb_hcd_resume_root_hub (struct usb_hcd *hcd) 22838c2ecf20Sopenharmony_ci{ 22848c2ecf20Sopenharmony_ci unsigned long flags; 22858c2ecf20Sopenharmony_ci 22868c2ecf20Sopenharmony_ci spin_lock_irqsave (&hcd_root_hub_lock, flags); 22878c2ecf20Sopenharmony_ci if (hcd->rh_registered) { 22888c2ecf20Sopenharmony_ci pm_wakeup_event(&hcd->self.root_hub->dev, 0); 22898c2ecf20Sopenharmony_ci set_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags); 22908c2ecf20Sopenharmony_ci queue_work(pm_wq, &hcd->wakeup_work); 22918c2ecf20Sopenharmony_ci } 22928c2ecf20Sopenharmony_ci spin_unlock_irqrestore (&hcd_root_hub_lock, flags); 22938c2ecf20Sopenharmony_ci} 22948c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub); 22958c2ecf20Sopenharmony_ci 22968c2ecf20Sopenharmony_ci#endif /* CONFIG_PM */ 22978c2ecf20Sopenharmony_ci 22988c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 22998c2ecf20Sopenharmony_ci 23008c2ecf20Sopenharmony_ci#ifdef CONFIG_USB_OTG 23018c2ecf20Sopenharmony_ci 23028c2ecf20Sopenharmony_ci/** 23038c2ecf20Sopenharmony_ci * usb_bus_start_enum - start immediate enumeration (for OTG) 23048c2ecf20Sopenharmony_ci * @bus: the bus (must use hcd framework) 23058c2ecf20Sopenharmony_ci * @port_num: 1-based number of port; usually bus->otg_port 23068c2ecf20Sopenharmony_ci * Context: in_interrupt() 23078c2ecf20Sopenharmony_ci * 23088c2ecf20Sopenharmony_ci * Starts enumeration, with an immediate reset followed later by 23098c2ecf20Sopenharmony_ci * hub_wq identifying and possibly configuring the device. 23108c2ecf20Sopenharmony_ci * This is needed by OTG controller drivers, where it helps meet 23118c2ecf20Sopenharmony_ci * HNP protocol timing requirements for starting a port reset. 23128c2ecf20Sopenharmony_ci * 23138c2ecf20Sopenharmony_ci * Return: 0 if successful. 23148c2ecf20Sopenharmony_ci */ 23158c2ecf20Sopenharmony_ciint usb_bus_start_enum(struct usb_bus *bus, unsigned port_num) 23168c2ecf20Sopenharmony_ci{ 23178c2ecf20Sopenharmony_ci struct usb_hcd *hcd; 23188c2ecf20Sopenharmony_ci int status = -EOPNOTSUPP; 23198c2ecf20Sopenharmony_ci 23208c2ecf20Sopenharmony_ci /* NOTE: since HNP can't start by grabbing the bus's address0_sem, 23218c2ecf20Sopenharmony_ci * boards with root hubs hooked up to internal devices (instead of 23228c2ecf20Sopenharmony_ci * just the OTG port) may need more attention to resetting... 23238c2ecf20Sopenharmony_ci */ 23248c2ecf20Sopenharmony_ci hcd = bus_to_hcd(bus); 23258c2ecf20Sopenharmony_ci if (port_num && hcd->driver->start_port_reset) 23268c2ecf20Sopenharmony_ci status = hcd->driver->start_port_reset(hcd, port_num); 23278c2ecf20Sopenharmony_ci 23288c2ecf20Sopenharmony_ci /* allocate hub_wq shortly after (first) root port reset finishes; 23298c2ecf20Sopenharmony_ci * it may issue others, until at least 50 msecs have passed. 23308c2ecf20Sopenharmony_ci */ 23318c2ecf20Sopenharmony_ci if (status == 0) 23328c2ecf20Sopenharmony_ci mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10)); 23338c2ecf20Sopenharmony_ci return status; 23348c2ecf20Sopenharmony_ci} 23358c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_bus_start_enum); 23368c2ecf20Sopenharmony_ci 23378c2ecf20Sopenharmony_ci#endif 23388c2ecf20Sopenharmony_ci 23398c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 23408c2ecf20Sopenharmony_ci 23418c2ecf20Sopenharmony_ci/** 23428c2ecf20Sopenharmony_ci * usb_hcd_irq - hook IRQs to HCD framework (bus glue) 23438c2ecf20Sopenharmony_ci * @irq: the IRQ being raised 23448c2ecf20Sopenharmony_ci * @__hcd: pointer to the HCD whose IRQ is being signaled 23458c2ecf20Sopenharmony_ci * 23468c2ecf20Sopenharmony_ci * If the controller isn't HALTed, calls the driver's irq handler. 23478c2ecf20Sopenharmony_ci * Checks whether the controller is now dead. 23488c2ecf20Sopenharmony_ci * 23498c2ecf20Sopenharmony_ci * Return: %IRQ_HANDLED if the IRQ was handled. %IRQ_NONE otherwise. 23508c2ecf20Sopenharmony_ci */ 23518c2ecf20Sopenharmony_ciirqreturn_t usb_hcd_irq (int irq, void *__hcd) 23528c2ecf20Sopenharmony_ci{ 23538c2ecf20Sopenharmony_ci struct usb_hcd *hcd = __hcd; 23548c2ecf20Sopenharmony_ci irqreturn_t rc; 23558c2ecf20Sopenharmony_ci 23568c2ecf20Sopenharmony_ci if (unlikely(HCD_DEAD(hcd) || !HCD_HW_ACCESSIBLE(hcd))) 23578c2ecf20Sopenharmony_ci rc = IRQ_NONE; 23588c2ecf20Sopenharmony_ci else if (hcd->driver->irq(hcd) == IRQ_NONE) 23598c2ecf20Sopenharmony_ci rc = IRQ_NONE; 23608c2ecf20Sopenharmony_ci else 23618c2ecf20Sopenharmony_ci rc = IRQ_HANDLED; 23628c2ecf20Sopenharmony_ci 23638c2ecf20Sopenharmony_ci return rc; 23648c2ecf20Sopenharmony_ci} 23658c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_hcd_irq); 23668c2ecf20Sopenharmony_ci 23678c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 23688c2ecf20Sopenharmony_ci 23698c2ecf20Sopenharmony_ci/* Workqueue routine for when the root-hub has died. */ 23708c2ecf20Sopenharmony_cistatic void hcd_died_work(struct work_struct *work) 23718c2ecf20Sopenharmony_ci{ 23728c2ecf20Sopenharmony_ci struct usb_hcd *hcd = container_of(work, struct usb_hcd, died_work); 23738c2ecf20Sopenharmony_ci static char *env[] = { 23748c2ecf20Sopenharmony_ci "ERROR=DEAD", 23758c2ecf20Sopenharmony_ci NULL 23768c2ecf20Sopenharmony_ci }; 23778c2ecf20Sopenharmony_ci 23788c2ecf20Sopenharmony_ci /* Notify user space that the host controller has died */ 23798c2ecf20Sopenharmony_ci kobject_uevent_env(&hcd->self.root_hub->dev.kobj, KOBJ_OFFLINE, env); 23808c2ecf20Sopenharmony_ci} 23818c2ecf20Sopenharmony_ci 23828c2ecf20Sopenharmony_ci/** 23838c2ecf20Sopenharmony_ci * usb_hc_died - report abnormal shutdown of a host controller (bus glue) 23848c2ecf20Sopenharmony_ci * @hcd: pointer to the HCD representing the controller 23858c2ecf20Sopenharmony_ci * 23868c2ecf20Sopenharmony_ci * This is called by bus glue to report a USB host controller that died 23878c2ecf20Sopenharmony_ci * while operations may still have been pending. It's called automatically 23888c2ecf20Sopenharmony_ci * by the PCI glue, so only glue for non-PCI busses should need to call it. 23898c2ecf20Sopenharmony_ci * 23908c2ecf20Sopenharmony_ci * Only call this function with the primary HCD. 23918c2ecf20Sopenharmony_ci */ 23928c2ecf20Sopenharmony_civoid usb_hc_died (struct usb_hcd *hcd) 23938c2ecf20Sopenharmony_ci{ 23948c2ecf20Sopenharmony_ci unsigned long flags; 23958c2ecf20Sopenharmony_ci 23968c2ecf20Sopenharmony_ci dev_err (hcd->self.controller, "HC died; cleaning up\n"); 23978c2ecf20Sopenharmony_ci 23988c2ecf20Sopenharmony_ci spin_lock_irqsave (&hcd_root_hub_lock, flags); 23998c2ecf20Sopenharmony_ci clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); 24008c2ecf20Sopenharmony_ci set_bit(HCD_FLAG_DEAD, &hcd->flags); 24018c2ecf20Sopenharmony_ci if (hcd->rh_registered) { 24028c2ecf20Sopenharmony_ci clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); 24038c2ecf20Sopenharmony_ci 24048c2ecf20Sopenharmony_ci /* make hub_wq clean up old urbs and devices */ 24058c2ecf20Sopenharmony_ci usb_set_device_state (hcd->self.root_hub, 24068c2ecf20Sopenharmony_ci USB_STATE_NOTATTACHED); 24078c2ecf20Sopenharmony_ci usb_kick_hub_wq(hcd->self.root_hub); 24088c2ecf20Sopenharmony_ci } 24098c2ecf20Sopenharmony_ci if (usb_hcd_is_primary_hcd(hcd) && hcd->shared_hcd) { 24108c2ecf20Sopenharmony_ci hcd = hcd->shared_hcd; 24118c2ecf20Sopenharmony_ci clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); 24128c2ecf20Sopenharmony_ci set_bit(HCD_FLAG_DEAD, &hcd->flags); 24138c2ecf20Sopenharmony_ci if (hcd->rh_registered) { 24148c2ecf20Sopenharmony_ci clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); 24158c2ecf20Sopenharmony_ci 24168c2ecf20Sopenharmony_ci /* make hub_wq clean up old urbs and devices */ 24178c2ecf20Sopenharmony_ci usb_set_device_state(hcd->self.root_hub, 24188c2ecf20Sopenharmony_ci USB_STATE_NOTATTACHED); 24198c2ecf20Sopenharmony_ci usb_kick_hub_wq(hcd->self.root_hub); 24208c2ecf20Sopenharmony_ci } 24218c2ecf20Sopenharmony_ci } 24228c2ecf20Sopenharmony_ci 24238c2ecf20Sopenharmony_ci /* Handle the case where this function gets called with a shared HCD */ 24248c2ecf20Sopenharmony_ci if (usb_hcd_is_primary_hcd(hcd)) 24258c2ecf20Sopenharmony_ci schedule_work(&hcd->died_work); 24268c2ecf20Sopenharmony_ci else 24278c2ecf20Sopenharmony_ci schedule_work(&hcd->primary_hcd->died_work); 24288c2ecf20Sopenharmony_ci 24298c2ecf20Sopenharmony_ci spin_unlock_irqrestore (&hcd_root_hub_lock, flags); 24308c2ecf20Sopenharmony_ci /* Make sure that the other roothub is also deallocated. */ 24318c2ecf20Sopenharmony_ci} 24328c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL (usb_hc_died); 24338c2ecf20Sopenharmony_ci 24348c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 24358c2ecf20Sopenharmony_ci 24368c2ecf20Sopenharmony_cistatic void init_giveback_urb_bh(struct giveback_urb_bh *bh) 24378c2ecf20Sopenharmony_ci{ 24388c2ecf20Sopenharmony_ci 24398c2ecf20Sopenharmony_ci spin_lock_init(&bh->lock); 24408c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&bh->head); 24418c2ecf20Sopenharmony_ci tasklet_setup(&bh->bh, usb_giveback_urb_bh); 24428c2ecf20Sopenharmony_ci} 24438c2ecf20Sopenharmony_ci 24448c2ecf20Sopenharmony_cistruct usb_hcd *__usb_create_hcd(const struct hc_driver *driver, 24458c2ecf20Sopenharmony_ci struct device *sysdev, struct device *dev, const char *bus_name, 24468c2ecf20Sopenharmony_ci struct usb_hcd *primary_hcd) 24478c2ecf20Sopenharmony_ci{ 24488c2ecf20Sopenharmony_ci struct usb_hcd *hcd; 24498c2ecf20Sopenharmony_ci 24508c2ecf20Sopenharmony_ci hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL); 24518c2ecf20Sopenharmony_ci if (!hcd) 24528c2ecf20Sopenharmony_ci return NULL; 24538c2ecf20Sopenharmony_ci if (primary_hcd == NULL) { 24548c2ecf20Sopenharmony_ci hcd->address0_mutex = kmalloc(sizeof(*hcd->address0_mutex), 24558c2ecf20Sopenharmony_ci GFP_KERNEL); 24568c2ecf20Sopenharmony_ci if (!hcd->address0_mutex) { 24578c2ecf20Sopenharmony_ci kfree(hcd); 24588c2ecf20Sopenharmony_ci dev_dbg(dev, "hcd address0 mutex alloc failed\n"); 24598c2ecf20Sopenharmony_ci return NULL; 24608c2ecf20Sopenharmony_ci } 24618c2ecf20Sopenharmony_ci mutex_init(hcd->address0_mutex); 24628c2ecf20Sopenharmony_ci hcd->bandwidth_mutex = kmalloc(sizeof(*hcd->bandwidth_mutex), 24638c2ecf20Sopenharmony_ci GFP_KERNEL); 24648c2ecf20Sopenharmony_ci if (!hcd->bandwidth_mutex) { 24658c2ecf20Sopenharmony_ci kfree(hcd->address0_mutex); 24668c2ecf20Sopenharmony_ci kfree(hcd); 24678c2ecf20Sopenharmony_ci dev_dbg(dev, "hcd bandwidth mutex alloc failed\n"); 24688c2ecf20Sopenharmony_ci return NULL; 24698c2ecf20Sopenharmony_ci } 24708c2ecf20Sopenharmony_ci mutex_init(hcd->bandwidth_mutex); 24718c2ecf20Sopenharmony_ci dev_set_drvdata(dev, hcd); 24728c2ecf20Sopenharmony_ci } else { 24738c2ecf20Sopenharmony_ci mutex_lock(&usb_port_peer_mutex); 24748c2ecf20Sopenharmony_ci hcd->address0_mutex = primary_hcd->address0_mutex; 24758c2ecf20Sopenharmony_ci hcd->bandwidth_mutex = primary_hcd->bandwidth_mutex; 24768c2ecf20Sopenharmony_ci hcd->primary_hcd = primary_hcd; 24778c2ecf20Sopenharmony_ci primary_hcd->primary_hcd = primary_hcd; 24788c2ecf20Sopenharmony_ci hcd->shared_hcd = primary_hcd; 24798c2ecf20Sopenharmony_ci primary_hcd->shared_hcd = hcd; 24808c2ecf20Sopenharmony_ci mutex_unlock(&usb_port_peer_mutex); 24818c2ecf20Sopenharmony_ci } 24828c2ecf20Sopenharmony_ci 24838c2ecf20Sopenharmony_ci kref_init(&hcd->kref); 24848c2ecf20Sopenharmony_ci 24858c2ecf20Sopenharmony_ci usb_bus_init(&hcd->self); 24868c2ecf20Sopenharmony_ci hcd->self.controller = dev; 24878c2ecf20Sopenharmony_ci hcd->self.sysdev = sysdev; 24888c2ecf20Sopenharmony_ci hcd->self.bus_name = bus_name; 24898c2ecf20Sopenharmony_ci 24908c2ecf20Sopenharmony_ci timer_setup(&hcd->rh_timer, rh_timer_func, 0); 24918c2ecf20Sopenharmony_ci#ifdef CONFIG_PM 24928c2ecf20Sopenharmony_ci INIT_WORK(&hcd->wakeup_work, hcd_resume_work); 24938c2ecf20Sopenharmony_ci#endif 24948c2ecf20Sopenharmony_ci 24958c2ecf20Sopenharmony_ci INIT_WORK(&hcd->died_work, hcd_died_work); 24968c2ecf20Sopenharmony_ci 24978c2ecf20Sopenharmony_ci hcd->driver = driver; 24988c2ecf20Sopenharmony_ci hcd->speed = driver->flags & HCD_MASK; 24998c2ecf20Sopenharmony_ci hcd->product_desc = (driver->product_desc) ? driver->product_desc : 25008c2ecf20Sopenharmony_ci "USB Host Controller"; 25018c2ecf20Sopenharmony_ci return hcd; 25028c2ecf20Sopenharmony_ci} 25038c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__usb_create_hcd); 25048c2ecf20Sopenharmony_ci 25058c2ecf20Sopenharmony_ci/** 25068c2ecf20Sopenharmony_ci * usb_create_shared_hcd - create and initialize an HCD structure 25078c2ecf20Sopenharmony_ci * @driver: HC driver that will use this hcd 25088c2ecf20Sopenharmony_ci * @dev: device for this HC, stored in hcd->self.controller 25098c2ecf20Sopenharmony_ci * @bus_name: value to store in hcd->self.bus_name 25108c2ecf20Sopenharmony_ci * @primary_hcd: a pointer to the usb_hcd structure that is sharing the 25118c2ecf20Sopenharmony_ci * PCI device. Only allocate certain resources for the primary HCD 25128c2ecf20Sopenharmony_ci * Context: !in_interrupt() 25138c2ecf20Sopenharmony_ci * 25148c2ecf20Sopenharmony_ci * Allocate a struct usb_hcd, with extra space at the end for the 25158c2ecf20Sopenharmony_ci * HC driver's private data. Initialize the generic members of the 25168c2ecf20Sopenharmony_ci * hcd structure. 25178c2ecf20Sopenharmony_ci * 25188c2ecf20Sopenharmony_ci * Return: On success, a pointer to the created and initialized HCD structure. 25198c2ecf20Sopenharmony_ci * On failure (e.g. if memory is unavailable), %NULL. 25208c2ecf20Sopenharmony_ci */ 25218c2ecf20Sopenharmony_cistruct usb_hcd *usb_create_shared_hcd(const struct hc_driver *driver, 25228c2ecf20Sopenharmony_ci struct device *dev, const char *bus_name, 25238c2ecf20Sopenharmony_ci struct usb_hcd *primary_hcd) 25248c2ecf20Sopenharmony_ci{ 25258c2ecf20Sopenharmony_ci return __usb_create_hcd(driver, dev, dev, bus_name, primary_hcd); 25268c2ecf20Sopenharmony_ci} 25278c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_create_shared_hcd); 25288c2ecf20Sopenharmony_ci 25298c2ecf20Sopenharmony_ci/** 25308c2ecf20Sopenharmony_ci * usb_create_hcd - create and initialize an HCD structure 25318c2ecf20Sopenharmony_ci * @driver: HC driver that will use this hcd 25328c2ecf20Sopenharmony_ci * @dev: device for this HC, stored in hcd->self.controller 25338c2ecf20Sopenharmony_ci * @bus_name: value to store in hcd->self.bus_name 25348c2ecf20Sopenharmony_ci * Context: !in_interrupt() 25358c2ecf20Sopenharmony_ci * 25368c2ecf20Sopenharmony_ci * Allocate a struct usb_hcd, with extra space at the end for the 25378c2ecf20Sopenharmony_ci * HC driver's private data. Initialize the generic members of the 25388c2ecf20Sopenharmony_ci * hcd structure. 25398c2ecf20Sopenharmony_ci * 25408c2ecf20Sopenharmony_ci * Return: On success, a pointer to the created and initialized HCD 25418c2ecf20Sopenharmony_ci * structure. On failure (e.g. if memory is unavailable), %NULL. 25428c2ecf20Sopenharmony_ci */ 25438c2ecf20Sopenharmony_cistruct usb_hcd *usb_create_hcd(const struct hc_driver *driver, 25448c2ecf20Sopenharmony_ci struct device *dev, const char *bus_name) 25458c2ecf20Sopenharmony_ci{ 25468c2ecf20Sopenharmony_ci return __usb_create_hcd(driver, dev, dev, bus_name, NULL); 25478c2ecf20Sopenharmony_ci} 25488c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_create_hcd); 25498c2ecf20Sopenharmony_ci 25508c2ecf20Sopenharmony_ci/* 25518c2ecf20Sopenharmony_ci * Roothubs that share one PCI device must also share the bandwidth mutex. 25528c2ecf20Sopenharmony_ci * Don't deallocate the bandwidth_mutex until the last shared usb_hcd is 25538c2ecf20Sopenharmony_ci * deallocated. 25548c2ecf20Sopenharmony_ci * 25558c2ecf20Sopenharmony_ci * Make sure to deallocate the bandwidth_mutex only when the last HCD is 25568c2ecf20Sopenharmony_ci * freed. When hcd_release() is called for either hcd in a peer set, 25578c2ecf20Sopenharmony_ci * invalidate the peer's ->shared_hcd and ->primary_hcd pointers. 25588c2ecf20Sopenharmony_ci */ 25598c2ecf20Sopenharmony_cistatic void hcd_release(struct kref *kref) 25608c2ecf20Sopenharmony_ci{ 25618c2ecf20Sopenharmony_ci struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref); 25628c2ecf20Sopenharmony_ci 25638c2ecf20Sopenharmony_ci mutex_lock(&usb_port_peer_mutex); 25648c2ecf20Sopenharmony_ci if (hcd->shared_hcd) { 25658c2ecf20Sopenharmony_ci struct usb_hcd *peer = hcd->shared_hcd; 25668c2ecf20Sopenharmony_ci 25678c2ecf20Sopenharmony_ci peer->shared_hcd = NULL; 25688c2ecf20Sopenharmony_ci peer->primary_hcd = NULL; 25698c2ecf20Sopenharmony_ci } else { 25708c2ecf20Sopenharmony_ci kfree(hcd->address0_mutex); 25718c2ecf20Sopenharmony_ci kfree(hcd->bandwidth_mutex); 25728c2ecf20Sopenharmony_ci } 25738c2ecf20Sopenharmony_ci mutex_unlock(&usb_port_peer_mutex); 25748c2ecf20Sopenharmony_ci kfree(hcd); 25758c2ecf20Sopenharmony_ci} 25768c2ecf20Sopenharmony_ci 25778c2ecf20Sopenharmony_cistruct usb_hcd *usb_get_hcd (struct usb_hcd *hcd) 25788c2ecf20Sopenharmony_ci{ 25798c2ecf20Sopenharmony_ci if (hcd) 25808c2ecf20Sopenharmony_ci kref_get (&hcd->kref); 25818c2ecf20Sopenharmony_ci return hcd; 25828c2ecf20Sopenharmony_ci} 25838c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_get_hcd); 25848c2ecf20Sopenharmony_ci 25858c2ecf20Sopenharmony_civoid usb_put_hcd (struct usb_hcd *hcd) 25868c2ecf20Sopenharmony_ci{ 25878c2ecf20Sopenharmony_ci if (hcd) 25888c2ecf20Sopenharmony_ci kref_put (&hcd->kref, hcd_release); 25898c2ecf20Sopenharmony_ci} 25908c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_put_hcd); 25918c2ecf20Sopenharmony_ci 25928c2ecf20Sopenharmony_ciint usb_hcd_is_primary_hcd(struct usb_hcd *hcd) 25938c2ecf20Sopenharmony_ci{ 25948c2ecf20Sopenharmony_ci if (!hcd->primary_hcd) 25958c2ecf20Sopenharmony_ci return 1; 25968c2ecf20Sopenharmony_ci return hcd == hcd->primary_hcd; 25978c2ecf20Sopenharmony_ci} 25988c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_hcd_is_primary_hcd); 25998c2ecf20Sopenharmony_ci 26008c2ecf20Sopenharmony_ciint usb_hcd_find_raw_port_number(struct usb_hcd *hcd, int port1) 26018c2ecf20Sopenharmony_ci{ 26028c2ecf20Sopenharmony_ci if (!hcd->driver->find_raw_port_number) 26038c2ecf20Sopenharmony_ci return port1; 26048c2ecf20Sopenharmony_ci 26058c2ecf20Sopenharmony_ci return hcd->driver->find_raw_port_number(hcd, port1); 26068c2ecf20Sopenharmony_ci} 26078c2ecf20Sopenharmony_ci 26088c2ecf20Sopenharmony_cistatic int usb_hcd_request_irqs(struct usb_hcd *hcd, 26098c2ecf20Sopenharmony_ci unsigned int irqnum, unsigned long irqflags) 26108c2ecf20Sopenharmony_ci{ 26118c2ecf20Sopenharmony_ci int retval; 26128c2ecf20Sopenharmony_ci 26138c2ecf20Sopenharmony_ci if (hcd->driver->irq) { 26148c2ecf20Sopenharmony_ci 26158c2ecf20Sopenharmony_ci snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d", 26168c2ecf20Sopenharmony_ci hcd->driver->description, hcd->self.busnum); 26178c2ecf20Sopenharmony_ci retval = request_irq(irqnum, &usb_hcd_irq, irqflags, 26188c2ecf20Sopenharmony_ci hcd->irq_descr, hcd); 26198c2ecf20Sopenharmony_ci if (retval != 0) { 26208c2ecf20Sopenharmony_ci dev_err(hcd->self.controller, 26218c2ecf20Sopenharmony_ci "request interrupt %d failed\n", 26228c2ecf20Sopenharmony_ci irqnum); 26238c2ecf20Sopenharmony_ci return retval; 26248c2ecf20Sopenharmony_ci } 26258c2ecf20Sopenharmony_ci hcd->irq = irqnum; 26268c2ecf20Sopenharmony_ci dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum, 26278c2ecf20Sopenharmony_ci (hcd->driver->flags & HCD_MEMORY) ? 26288c2ecf20Sopenharmony_ci "io mem" : "io base", 26298c2ecf20Sopenharmony_ci (unsigned long long)hcd->rsrc_start); 26308c2ecf20Sopenharmony_ci } else { 26318c2ecf20Sopenharmony_ci hcd->irq = 0; 26328c2ecf20Sopenharmony_ci if (hcd->rsrc_start) 26338c2ecf20Sopenharmony_ci dev_info(hcd->self.controller, "%s 0x%08llx\n", 26348c2ecf20Sopenharmony_ci (hcd->driver->flags & HCD_MEMORY) ? 26358c2ecf20Sopenharmony_ci "io mem" : "io base", 26368c2ecf20Sopenharmony_ci (unsigned long long)hcd->rsrc_start); 26378c2ecf20Sopenharmony_ci } 26388c2ecf20Sopenharmony_ci return 0; 26398c2ecf20Sopenharmony_ci} 26408c2ecf20Sopenharmony_ci 26418c2ecf20Sopenharmony_ci/* 26428c2ecf20Sopenharmony_ci * Before we free this root hub, flush in-flight peering attempts 26438c2ecf20Sopenharmony_ci * and disable peer lookups 26448c2ecf20Sopenharmony_ci */ 26458c2ecf20Sopenharmony_cistatic void usb_put_invalidate_rhdev(struct usb_hcd *hcd) 26468c2ecf20Sopenharmony_ci{ 26478c2ecf20Sopenharmony_ci struct usb_device *rhdev; 26488c2ecf20Sopenharmony_ci 26498c2ecf20Sopenharmony_ci mutex_lock(&usb_port_peer_mutex); 26508c2ecf20Sopenharmony_ci rhdev = hcd->self.root_hub; 26518c2ecf20Sopenharmony_ci hcd->self.root_hub = NULL; 26528c2ecf20Sopenharmony_ci mutex_unlock(&usb_port_peer_mutex); 26538c2ecf20Sopenharmony_ci usb_put_dev(rhdev); 26548c2ecf20Sopenharmony_ci} 26558c2ecf20Sopenharmony_ci 26568c2ecf20Sopenharmony_ci/** 26578c2ecf20Sopenharmony_ci * usb_add_hcd - finish generic HCD structure initialization and register 26588c2ecf20Sopenharmony_ci * @hcd: the usb_hcd structure to initialize 26598c2ecf20Sopenharmony_ci * @irqnum: Interrupt line to allocate 26608c2ecf20Sopenharmony_ci * @irqflags: Interrupt type flags 26618c2ecf20Sopenharmony_ci * 26628c2ecf20Sopenharmony_ci * Finish the remaining parts of generic HCD initialization: allocate the 26638c2ecf20Sopenharmony_ci * buffers of consistent memory, register the bus, request the IRQ line, 26648c2ecf20Sopenharmony_ci * and call the driver's reset() and start() routines. 26658c2ecf20Sopenharmony_ci */ 26668c2ecf20Sopenharmony_ciint usb_add_hcd(struct usb_hcd *hcd, 26678c2ecf20Sopenharmony_ci unsigned int irqnum, unsigned long irqflags) 26688c2ecf20Sopenharmony_ci{ 26698c2ecf20Sopenharmony_ci int retval; 26708c2ecf20Sopenharmony_ci struct usb_device *rhdev; 26718c2ecf20Sopenharmony_ci struct usb_hcd *shared_hcd; 26728c2ecf20Sopenharmony_ci 26738c2ecf20Sopenharmony_ci if (!hcd->skip_phy_initialization && usb_hcd_is_primary_hcd(hcd)) { 26748c2ecf20Sopenharmony_ci hcd->phy_roothub = usb_phy_roothub_alloc(hcd->self.sysdev); 26758c2ecf20Sopenharmony_ci if (IS_ERR(hcd->phy_roothub)) 26768c2ecf20Sopenharmony_ci return PTR_ERR(hcd->phy_roothub); 26778c2ecf20Sopenharmony_ci 26788c2ecf20Sopenharmony_ci retval = usb_phy_roothub_init(hcd->phy_roothub); 26798c2ecf20Sopenharmony_ci if (retval) 26808c2ecf20Sopenharmony_ci return retval; 26818c2ecf20Sopenharmony_ci 26828c2ecf20Sopenharmony_ci retval = usb_phy_roothub_set_mode(hcd->phy_roothub, 26838c2ecf20Sopenharmony_ci PHY_MODE_USB_HOST_SS); 26848c2ecf20Sopenharmony_ci if (retval) 26858c2ecf20Sopenharmony_ci retval = usb_phy_roothub_set_mode(hcd->phy_roothub, 26868c2ecf20Sopenharmony_ci PHY_MODE_USB_HOST); 26878c2ecf20Sopenharmony_ci if (retval) 26888c2ecf20Sopenharmony_ci goto err_usb_phy_roothub_power_on; 26898c2ecf20Sopenharmony_ci 26908c2ecf20Sopenharmony_ci retval = usb_phy_roothub_power_on(hcd->phy_roothub); 26918c2ecf20Sopenharmony_ci if (retval) 26928c2ecf20Sopenharmony_ci goto err_usb_phy_roothub_power_on; 26938c2ecf20Sopenharmony_ci } 26948c2ecf20Sopenharmony_ci 26958c2ecf20Sopenharmony_ci dev_info(hcd->self.controller, "%s\n", hcd->product_desc); 26968c2ecf20Sopenharmony_ci 26978c2ecf20Sopenharmony_ci switch (authorized_default) { 26988c2ecf20Sopenharmony_ci case USB_AUTHORIZE_NONE: 26998c2ecf20Sopenharmony_ci hcd->dev_policy = USB_DEVICE_AUTHORIZE_NONE; 27008c2ecf20Sopenharmony_ci break; 27018c2ecf20Sopenharmony_ci 27028c2ecf20Sopenharmony_ci case USB_AUTHORIZE_ALL: 27038c2ecf20Sopenharmony_ci hcd->dev_policy = USB_DEVICE_AUTHORIZE_ALL; 27048c2ecf20Sopenharmony_ci break; 27058c2ecf20Sopenharmony_ci 27068c2ecf20Sopenharmony_ci case USB_AUTHORIZE_INTERNAL: 27078c2ecf20Sopenharmony_ci hcd->dev_policy = USB_DEVICE_AUTHORIZE_INTERNAL; 27088c2ecf20Sopenharmony_ci break; 27098c2ecf20Sopenharmony_ci 27108c2ecf20Sopenharmony_ci case USB_AUTHORIZE_WIRED: 27118c2ecf20Sopenharmony_ci default: 27128c2ecf20Sopenharmony_ci hcd->dev_policy = hcd->wireless ? 27138c2ecf20Sopenharmony_ci USB_DEVICE_AUTHORIZE_NONE : USB_DEVICE_AUTHORIZE_ALL; 27148c2ecf20Sopenharmony_ci break; 27158c2ecf20Sopenharmony_ci } 27168c2ecf20Sopenharmony_ci 27178c2ecf20Sopenharmony_ci set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 27188c2ecf20Sopenharmony_ci 27198c2ecf20Sopenharmony_ci /* per default all interfaces are authorized */ 27208c2ecf20Sopenharmony_ci set_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags); 27218c2ecf20Sopenharmony_ci 27228c2ecf20Sopenharmony_ci /* HC is in reset state, but accessible. Now do the one-time init, 27238c2ecf20Sopenharmony_ci * bottom up so that hcds can customize the root hubs before hub_wq 27248c2ecf20Sopenharmony_ci * starts talking to them. (Note, bus id is assigned early too.) 27258c2ecf20Sopenharmony_ci */ 27268c2ecf20Sopenharmony_ci retval = hcd_buffer_create(hcd); 27278c2ecf20Sopenharmony_ci if (retval != 0) { 27288c2ecf20Sopenharmony_ci dev_dbg(hcd->self.sysdev, "pool alloc failed\n"); 27298c2ecf20Sopenharmony_ci goto err_create_buf; 27308c2ecf20Sopenharmony_ci } 27318c2ecf20Sopenharmony_ci 27328c2ecf20Sopenharmony_ci retval = usb_register_bus(&hcd->self); 27338c2ecf20Sopenharmony_ci if (retval < 0) 27348c2ecf20Sopenharmony_ci goto err_register_bus; 27358c2ecf20Sopenharmony_ci 27368c2ecf20Sopenharmony_ci rhdev = usb_alloc_dev(NULL, &hcd->self, 0); 27378c2ecf20Sopenharmony_ci if (rhdev == NULL) { 27388c2ecf20Sopenharmony_ci dev_err(hcd->self.sysdev, "unable to allocate root hub\n"); 27398c2ecf20Sopenharmony_ci retval = -ENOMEM; 27408c2ecf20Sopenharmony_ci goto err_allocate_root_hub; 27418c2ecf20Sopenharmony_ci } 27428c2ecf20Sopenharmony_ci mutex_lock(&usb_port_peer_mutex); 27438c2ecf20Sopenharmony_ci hcd->self.root_hub = rhdev; 27448c2ecf20Sopenharmony_ci mutex_unlock(&usb_port_peer_mutex); 27458c2ecf20Sopenharmony_ci 27468c2ecf20Sopenharmony_ci rhdev->rx_lanes = 1; 27478c2ecf20Sopenharmony_ci rhdev->tx_lanes = 1; 27488c2ecf20Sopenharmony_ci rhdev->ssp_rate = USB_SSP_GEN_UNKNOWN; 27498c2ecf20Sopenharmony_ci 27508c2ecf20Sopenharmony_ci switch (hcd->speed) { 27518c2ecf20Sopenharmony_ci case HCD_USB11: 27528c2ecf20Sopenharmony_ci rhdev->speed = USB_SPEED_FULL; 27538c2ecf20Sopenharmony_ci break; 27548c2ecf20Sopenharmony_ci case HCD_USB2: 27558c2ecf20Sopenharmony_ci rhdev->speed = USB_SPEED_HIGH; 27568c2ecf20Sopenharmony_ci break; 27578c2ecf20Sopenharmony_ci case HCD_USB25: 27588c2ecf20Sopenharmony_ci rhdev->speed = USB_SPEED_WIRELESS; 27598c2ecf20Sopenharmony_ci break; 27608c2ecf20Sopenharmony_ci case HCD_USB3: 27618c2ecf20Sopenharmony_ci rhdev->speed = USB_SPEED_SUPER; 27628c2ecf20Sopenharmony_ci break; 27638c2ecf20Sopenharmony_ci case HCD_USB32: 27648c2ecf20Sopenharmony_ci rhdev->rx_lanes = 2; 27658c2ecf20Sopenharmony_ci rhdev->tx_lanes = 2; 27668c2ecf20Sopenharmony_ci rhdev->ssp_rate = USB_SSP_GEN_2x2; 27678c2ecf20Sopenharmony_ci rhdev->speed = USB_SPEED_SUPER_PLUS; 27688c2ecf20Sopenharmony_ci break; 27698c2ecf20Sopenharmony_ci case HCD_USB31: 27708c2ecf20Sopenharmony_ci rhdev->ssp_rate = USB_SSP_GEN_2x1; 27718c2ecf20Sopenharmony_ci rhdev->speed = USB_SPEED_SUPER_PLUS; 27728c2ecf20Sopenharmony_ci break; 27738c2ecf20Sopenharmony_ci default: 27748c2ecf20Sopenharmony_ci retval = -EINVAL; 27758c2ecf20Sopenharmony_ci goto err_set_rh_speed; 27768c2ecf20Sopenharmony_ci } 27778c2ecf20Sopenharmony_ci 27788c2ecf20Sopenharmony_ci /* wakeup flag init defaults to "everything works" for root hubs, 27798c2ecf20Sopenharmony_ci * but drivers can override it in reset() if needed, along with 27808c2ecf20Sopenharmony_ci * recording the overall controller's system wakeup capability. 27818c2ecf20Sopenharmony_ci */ 27828c2ecf20Sopenharmony_ci device_set_wakeup_capable(&rhdev->dev, 1); 27838c2ecf20Sopenharmony_ci 27848c2ecf20Sopenharmony_ci /* HCD_FLAG_RH_RUNNING doesn't matter until the root hub is 27858c2ecf20Sopenharmony_ci * registered. But since the controller can die at any time, 27868c2ecf20Sopenharmony_ci * let's initialize the flag before touching the hardware. 27878c2ecf20Sopenharmony_ci */ 27888c2ecf20Sopenharmony_ci set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); 27898c2ecf20Sopenharmony_ci 27908c2ecf20Sopenharmony_ci /* "reset" is misnamed; its role is now one-time init. the controller 27918c2ecf20Sopenharmony_ci * should already have been reset (and boot firmware kicked off etc). 27928c2ecf20Sopenharmony_ci */ 27938c2ecf20Sopenharmony_ci if (hcd->driver->reset) { 27948c2ecf20Sopenharmony_ci retval = hcd->driver->reset(hcd); 27958c2ecf20Sopenharmony_ci if (retval < 0) { 27968c2ecf20Sopenharmony_ci dev_err(hcd->self.controller, "can't setup: %d\n", 27978c2ecf20Sopenharmony_ci retval); 27988c2ecf20Sopenharmony_ci goto err_hcd_driver_setup; 27998c2ecf20Sopenharmony_ci } 28008c2ecf20Sopenharmony_ci } 28018c2ecf20Sopenharmony_ci hcd->rh_pollable = 1; 28028c2ecf20Sopenharmony_ci 28038c2ecf20Sopenharmony_ci retval = usb_phy_roothub_calibrate(hcd->phy_roothub); 28048c2ecf20Sopenharmony_ci if (retval) 28058c2ecf20Sopenharmony_ci goto err_hcd_driver_setup; 28068c2ecf20Sopenharmony_ci 28078c2ecf20Sopenharmony_ci /* NOTE: root hub and controller capabilities may not be the same */ 28088c2ecf20Sopenharmony_ci if (device_can_wakeup(hcd->self.controller) 28098c2ecf20Sopenharmony_ci && device_can_wakeup(&hcd->self.root_hub->dev)) 28108c2ecf20Sopenharmony_ci dev_dbg(hcd->self.controller, "supports USB remote wakeup\n"); 28118c2ecf20Sopenharmony_ci 28128c2ecf20Sopenharmony_ci /* initialize tasklets */ 28138c2ecf20Sopenharmony_ci init_giveback_urb_bh(&hcd->high_prio_bh); 28148c2ecf20Sopenharmony_ci hcd->high_prio_bh.high_prio = true; 28158c2ecf20Sopenharmony_ci init_giveback_urb_bh(&hcd->low_prio_bh); 28168c2ecf20Sopenharmony_ci 28178c2ecf20Sopenharmony_ci /* enable irqs just before we start the controller, 28188c2ecf20Sopenharmony_ci * if the BIOS provides legacy PCI irqs. 28198c2ecf20Sopenharmony_ci */ 28208c2ecf20Sopenharmony_ci if (usb_hcd_is_primary_hcd(hcd) && irqnum) { 28218c2ecf20Sopenharmony_ci retval = usb_hcd_request_irqs(hcd, irqnum, irqflags); 28228c2ecf20Sopenharmony_ci if (retval) 28238c2ecf20Sopenharmony_ci goto err_request_irq; 28248c2ecf20Sopenharmony_ci } 28258c2ecf20Sopenharmony_ci 28268c2ecf20Sopenharmony_ci hcd->state = HC_STATE_RUNNING; 28278c2ecf20Sopenharmony_ci retval = hcd->driver->start(hcd); 28288c2ecf20Sopenharmony_ci if (retval < 0) { 28298c2ecf20Sopenharmony_ci dev_err(hcd->self.controller, "startup error %d\n", retval); 28308c2ecf20Sopenharmony_ci goto err_hcd_driver_start; 28318c2ecf20Sopenharmony_ci } 28328c2ecf20Sopenharmony_ci 28338c2ecf20Sopenharmony_ci /* starting here, usbcore will pay attention to the shared HCD roothub */ 28348c2ecf20Sopenharmony_ci shared_hcd = hcd->shared_hcd; 28358c2ecf20Sopenharmony_ci if (!usb_hcd_is_primary_hcd(hcd) && shared_hcd && HCD_DEFER_RH_REGISTER(shared_hcd)) { 28368c2ecf20Sopenharmony_ci retval = register_root_hub(shared_hcd); 28378c2ecf20Sopenharmony_ci if (retval != 0) 28388c2ecf20Sopenharmony_ci goto err_register_root_hub; 28398c2ecf20Sopenharmony_ci 28408c2ecf20Sopenharmony_ci if (shared_hcd->uses_new_polling && HCD_POLL_RH(shared_hcd)) 28418c2ecf20Sopenharmony_ci usb_hcd_poll_rh_status(shared_hcd); 28428c2ecf20Sopenharmony_ci } 28438c2ecf20Sopenharmony_ci 28448c2ecf20Sopenharmony_ci /* starting here, usbcore will pay attention to this root hub */ 28458c2ecf20Sopenharmony_ci if (!HCD_DEFER_RH_REGISTER(hcd)) { 28468c2ecf20Sopenharmony_ci retval = register_root_hub(hcd); 28478c2ecf20Sopenharmony_ci if (retval != 0) 28488c2ecf20Sopenharmony_ci goto err_register_root_hub; 28498c2ecf20Sopenharmony_ci 28508c2ecf20Sopenharmony_ci if (hcd->uses_new_polling && HCD_POLL_RH(hcd)) 28518c2ecf20Sopenharmony_ci usb_hcd_poll_rh_status(hcd); 28528c2ecf20Sopenharmony_ci } 28538c2ecf20Sopenharmony_ci 28548c2ecf20Sopenharmony_ci return retval; 28558c2ecf20Sopenharmony_ci 28568c2ecf20Sopenharmony_cierr_register_root_hub: 28578c2ecf20Sopenharmony_ci hcd->rh_pollable = 0; 28588c2ecf20Sopenharmony_ci clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); 28598c2ecf20Sopenharmony_ci del_timer_sync(&hcd->rh_timer); 28608c2ecf20Sopenharmony_ci hcd->driver->stop(hcd); 28618c2ecf20Sopenharmony_ci hcd->state = HC_STATE_HALT; 28628c2ecf20Sopenharmony_ci clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); 28638c2ecf20Sopenharmony_ci del_timer_sync(&hcd->rh_timer); 28648c2ecf20Sopenharmony_cierr_hcd_driver_start: 28658c2ecf20Sopenharmony_ci if (usb_hcd_is_primary_hcd(hcd) && hcd->irq > 0) 28668c2ecf20Sopenharmony_ci free_irq(irqnum, hcd); 28678c2ecf20Sopenharmony_cierr_request_irq: 28688c2ecf20Sopenharmony_cierr_hcd_driver_setup: 28698c2ecf20Sopenharmony_cierr_set_rh_speed: 28708c2ecf20Sopenharmony_ci usb_put_invalidate_rhdev(hcd); 28718c2ecf20Sopenharmony_cierr_allocate_root_hub: 28728c2ecf20Sopenharmony_ci usb_deregister_bus(&hcd->self); 28738c2ecf20Sopenharmony_cierr_register_bus: 28748c2ecf20Sopenharmony_ci hcd_buffer_destroy(hcd); 28758c2ecf20Sopenharmony_cierr_create_buf: 28768c2ecf20Sopenharmony_ci usb_phy_roothub_power_off(hcd->phy_roothub); 28778c2ecf20Sopenharmony_cierr_usb_phy_roothub_power_on: 28788c2ecf20Sopenharmony_ci usb_phy_roothub_exit(hcd->phy_roothub); 28798c2ecf20Sopenharmony_ci 28808c2ecf20Sopenharmony_ci return retval; 28818c2ecf20Sopenharmony_ci} 28828c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_add_hcd); 28838c2ecf20Sopenharmony_ci 28848c2ecf20Sopenharmony_ci/** 28858c2ecf20Sopenharmony_ci * usb_remove_hcd - shutdown processing for generic HCDs 28868c2ecf20Sopenharmony_ci * @hcd: the usb_hcd structure to remove 28878c2ecf20Sopenharmony_ci * Context: !in_interrupt() 28888c2ecf20Sopenharmony_ci * 28898c2ecf20Sopenharmony_ci * Disconnects the root hub, then reverses the effects of usb_add_hcd(), 28908c2ecf20Sopenharmony_ci * invoking the HCD's stop() method. 28918c2ecf20Sopenharmony_ci */ 28928c2ecf20Sopenharmony_civoid usb_remove_hcd(struct usb_hcd *hcd) 28938c2ecf20Sopenharmony_ci{ 28948c2ecf20Sopenharmony_ci struct usb_device *rhdev = hcd->self.root_hub; 28958c2ecf20Sopenharmony_ci bool rh_registered; 28968c2ecf20Sopenharmony_ci 28978c2ecf20Sopenharmony_ci dev_info(hcd->self.controller, "remove, state %x\n", hcd->state); 28988c2ecf20Sopenharmony_ci 28998c2ecf20Sopenharmony_ci usb_get_dev(rhdev); 29008c2ecf20Sopenharmony_ci clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); 29018c2ecf20Sopenharmony_ci if (HC_IS_RUNNING (hcd->state)) 29028c2ecf20Sopenharmony_ci hcd->state = HC_STATE_QUIESCING; 29038c2ecf20Sopenharmony_ci 29048c2ecf20Sopenharmony_ci dev_dbg(hcd->self.controller, "roothub graceful disconnect\n"); 29058c2ecf20Sopenharmony_ci spin_lock_irq (&hcd_root_hub_lock); 29068c2ecf20Sopenharmony_ci rh_registered = hcd->rh_registered; 29078c2ecf20Sopenharmony_ci hcd->rh_registered = 0; 29088c2ecf20Sopenharmony_ci spin_unlock_irq (&hcd_root_hub_lock); 29098c2ecf20Sopenharmony_ci 29108c2ecf20Sopenharmony_ci#ifdef CONFIG_PM 29118c2ecf20Sopenharmony_ci cancel_work_sync(&hcd->wakeup_work); 29128c2ecf20Sopenharmony_ci#endif 29138c2ecf20Sopenharmony_ci cancel_work_sync(&hcd->died_work); 29148c2ecf20Sopenharmony_ci 29158c2ecf20Sopenharmony_ci mutex_lock(&usb_bus_idr_lock); 29168c2ecf20Sopenharmony_ci if (rh_registered) 29178c2ecf20Sopenharmony_ci usb_disconnect(&rhdev); /* Sets rhdev to NULL */ 29188c2ecf20Sopenharmony_ci mutex_unlock(&usb_bus_idr_lock); 29198c2ecf20Sopenharmony_ci 29208c2ecf20Sopenharmony_ci /* 29218c2ecf20Sopenharmony_ci * tasklet_kill() isn't needed here because: 29228c2ecf20Sopenharmony_ci * - driver's disconnect() called from usb_disconnect() should 29238c2ecf20Sopenharmony_ci * make sure its URBs are completed during the disconnect() 29248c2ecf20Sopenharmony_ci * callback 29258c2ecf20Sopenharmony_ci * 29268c2ecf20Sopenharmony_ci * - it is too late to run complete() here since driver may have 29278c2ecf20Sopenharmony_ci * been removed already now 29288c2ecf20Sopenharmony_ci */ 29298c2ecf20Sopenharmony_ci 29308c2ecf20Sopenharmony_ci /* Prevent any more root-hub status calls from the timer. 29318c2ecf20Sopenharmony_ci * The HCD might still restart the timer (if a port status change 29328c2ecf20Sopenharmony_ci * interrupt occurs), but usb_hcd_poll_rh_status() won't invoke 29338c2ecf20Sopenharmony_ci * the hub_status_data() callback. 29348c2ecf20Sopenharmony_ci */ 29358c2ecf20Sopenharmony_ci hcd->rh_pollable = 0; 29368c2ecf20Sopenharmony_ci clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); 29378c2ecf20Sopenharmony_ci del_timer_sync(&hcd->rh_timer); 29388c2ecf20Sopenharmony_ci 29398c2ecf20Sopenharmony_ci hcd->driver->stop(hcd); 29408c2ecf20Sopenharmony_ci hcd->state = HC_STATE_HALT; 29418c2ecf20Sopenharmony_ci 29428c2ecf20Sopenharmony_ci /* In case the HCD restarted the timer, stop it again. */ 29438c2ecf20Sopenharmony_ci clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); 29448c2ecf20Sopenharmony_ci del_timer_sync(&hcd->rh_timer); 29458c2ecf20Sopenharmony_ci 29468c2ecf20Sopenharmony_ci if (usb_hcd_is_primary_hcd(hcd)) { 29478c2ecf20Sopenharmony_ci if (hcd->irq > 0) 29488c2ecf20Sopenharmony_ci free_irq(hcd->irq, hcd); 29498c2ecf20Sopenharmony_ci } 29508c2ecf20Sopenharmony_ci 29518c2ecf20Sopenharmony_ci usb_deregister_bus(&hcd->self); 29528c2ecf20Sopenharmony_ci hcd_buffer_destroy(hcd); 29538c2ecf20Sopenharmony_ci 29548c2ecf20Sopenharmony_ci usb_phy_roothub_power_off(hcd->phy_roothub); 29558c2ecf20Sopenharmony_ci usb_phy_roothub_exit(hcd->phy_roothub); 29568c2ecf20Sopenharmony_ci 29578c2ecf20Sopenharmony_ci usb_put_invalidate_rhdev(hcd); 29588c2ecf20Sopenharmony_ci hcd->flags = 0; 29598c2ecf20Sopenharmony_ci} 29608c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_remove_hcd); 29618c2ecf20Sopenharmony_ci 29628c2ecf20Sopenharmony_civoid 29638c2ecf20Sopenharmony_ciusb_hcd_platform_shutdown(struct platform_device *dev) 29648c2ecf20Sopenharmony_ci{ 29658c2ecf20Sopenharmony_ci struct usb_hcd *hcd = platform_get_drvdata(dev); 29668c2ecf20Sopenharmony_ci 29678c2ecf20Sopenharmony_ci /* No need for pm_runtime_put(), we're shutting down */ 29688c2ecf20Sopenharmony_ci pm_runtime_get_sync(&dev->dev); 29698c2ecf20Sopenharmony_ci 29708c2ecf20Sopenharmony_ci if (hcd->driver->shutdown) 29718c2ecf20Sopenharmony_ci hcd->driver->shutdown(hcd); 29728c2ecf20Sopenharmony_ci} 29738c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown); 29748c2ecf20Sopenharmony_ci 29758c2ecf20Sopenharmony_ciint usb_hcd_setup_local_mem(struct usb_hcd *hcd, phys_addr_t phys_addr, 29768c2ecf20Sopenharmony_ci dma_addr_t dma, size_t size) 29778c2ecf20Sopenharmony_ci{ 29788c2ecf20Sopenharmony_ci int err; 29798c2ecf20Sopenharmony_ci void *local_mem; 29808c2ecf20Sopenharmony_ci 29818c2ecf20Sopenharmony_ci hcd->localmem_pool = devm_gen_pool_create(hcd->self.sysdev, 4, 29828c2ecf20Sopenharmony_ci dev_to_node(hcd->self.sysdev), 29838c2ecf20Sopenharmony_ci dev_name(hcd->self.sysdev)); 29848c2ecf20Sopenharmony_ci if (IS_ERR(hcd->localmem_pool)) 29858c2ecf20Sopenharmony_ci return PTR_ERR(hcd->localmem_pool); 29868c2ecf20Sopenharmony_ci 29878c2ecf20Sopenharmony_ci local_mem = devm_memremap(hcd->self.sysdev, phys_addr, 29888c2ecf20Sopenharmony_ci size, MEMREMAP_WC); 29898c2ecf20Sopenharmony_ci if (IS_ERR(local_mem)) 29908c2ecf20Sopenharmony_ci return PTR_ERR(local_mem); 29918c2ecf20Sopenharmony_ci 29928c2ecf20Sopenharmony_ci /* 29938c2ecf20Sopenharmony_ci * Here we pass a dma_addr_t but the arg type is a phys_addr_t. 29948c2ecf20Sopenharmony_ci * It's not backed by system memory and thus there's no kernel mapping 29958c2ecf20Sopenharmony_ci * for it. 29968c2ecf20Sopenharmony_ci */ 29978c2ecf20Sopenharmony_ci err = gen_pool_add_virt(hcd->localmem_pool, (unsigned long)local_mem, 29988c2ecf20Sopenharmony_ci dma, size, dev_to_node(hcd->self.sysdev)); 29998c2ecf20Sopenharmony_ci if (err < 0) { 30008c2ecf20Sopenharmony_ci dev_err(hcd->self.sysdev, "gen_pool_add_virt failed with %d\n", 30018c2ecf20Sopenharmony_ci err); 30028c2ecf20Sopenharmony_ci return err; 30038c2ecf20Sopenharmony_ci } 30048c2ecf20Sopenharmony_ci 30058c2ecf20Sopenharmony_ci return 0; 30068c2ecf20Sopenharmony_ci} 30078c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(usb_hcd_setup_local_mem); 30088c2ecf20Sopenharmony_ci 30098c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/ 30108c2ecf20Sopenharmony_ci 30118c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_USB_MON) 30128c2ecf20Sopenharmony_ci 30138c2ecf20Sopenharmony_ciconst struct usb_mon_operations *mon_ops; 30148c2ecf20Sopenharmony_ci 30158c2ecf20Sopenharmony_ci/* 30168c2ecf20Sopenharmony_ci * The registration is unlocked. 30178c2ecf20Sopenharmony_ci * We do it this way because we do not want to lock in hot paths. 30188c2ecf20Sopenharmony_ci * 30198c2ecf20Sopenharmony_ci * Notice that the code is minimally error-proof. Because usbmon needs 30208c2ecf20Sopenharmony_ci * symbols from usbcore, usbcore gets referenced and cannot be unloaded first. 30218c2ecf20Sopenharmony_ci */ 30228c2ecf20Sopenharmony_ci 30238c2ecf20Sopenharmony_ciint usb_mon_register(const struct usb_mon_operations *ops) 30248c2ecf20Sopenharmony_ci{ 30258c2ecf20Sopenharmony_ci 30268c2ecf20Sopenharmony_ci if (mon_ops) 30278c2ecf20Sopenharmony_ci return -EBUSY; 30288c2ecf20Sopenharmony_ci 30298c2ecf20Sopenharmony_ci mon_ops = ops; 30308c2ecf20Sopenharmony_ci mb(); 30318c2ecf20Sopenharmony_ci return 0; 30328c2ecf20Sopenharmony_ci} 30338c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL (usb_mon_register); 30348c2ecf20Sopenharmony_ci 30358c2ecf20Sopenharmony_civoid usb_mon_deregister (void) 30368c2ecf20Sopenharmony_ci{ 30378c2ecf20Sopenharmony_ci 30388c2ecf20Sopenharmony_ci if (mon_ops == NULL) { 30398c2ecf20Sopenharmony_ci printk(KERN_ERR "USB: monitor was not registered\n"); 30408c2ecf20Sopenharmony_ci return; 30418c2ecf20Sopenharmony_ci } 30428c2ecf20Sopenharmony_ci mon_ops = NULL; 30438c2ecf20Sopenharmony_ci mb(); 30448c2ecf20Sopenharmony_ci} 30458c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL (usb_mon_deregister); 30468c2ecf20Sopenharmony_ci 30478c2ecf20Sopenharmony_ci#endif /* CONFIG_USB_MON || CONFIG_USB_MON_MODULE */ 3048