| 1 |
// SPDX-License-Identifier: GPL-2.0+ |
| 2 |
/* |
| 3 |
* (C) Copyright Linus Torvalds 1999 |
| 4 |
* (C) Copyright Johannes Erdfelt 1999-2001 |
| 5 |
* (C) Copyright Andreas Gal 1999 |
| 6 |
* (C) Copyright Gregory P. Smith 1999 |
| 7 |
* (C) Copyright Deti Fliegl 1999 |
| 8 |
* (C) Copyright Randy Dunlap 2000 |
| 9 |
* (C) Copyright David Brownell 2000-2002 |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <linux/bcd.h> |
| 13 |
#include <linux/module.h> |
| 14 |
#include <linux/version.h> |
| 15 |
#include <linux/kernel.h> |
| 16 |
#include <linux/sched/task_stack.h> |
| 17 |
#include <linux/slab.h> |
| 18 |
#include <linux/completion.h> |
| 19 |
#include <linux/utsname.h> |
| 20 |
#include <linux/mm.h> |
| 21 |
#include <asm/io.h> |
| 22 |
#include <linux/device.h> |
| 23 |
#include <linux/dma-mapping.h> |
| 24 |
#include <linux/mutex.h> |
| 25 |
#include <asm/irq.h> |
| 26 |
#include <asm/byteorder.h> |
| 27 |
#include <linux/unaligned.h> |
| 28 |
#include <linux/platform_device.h> |
| 29 |
#include <linux/workqueue.h> |
| 30 |
#include <linux/pm_runtime.h> |
| 31 |
#include <linux/types.h> |
| 32 |
#include <linux/genalloc.h> |
| 33 |
#include <linux/io.h> |
| 34 |
#include <linux/kcov.h> |
| 35 |
|
| 36 |
#include <linux/phy/phy.h> |
| 37 |
#include <linux/usb.h> |
| 38 |
#include <linux/usb/hcd.h> |
| 39 |
#include <linux/usb/otg.h> |
| 40 |
|
| 41 |
#include "usb.h" |
| 42 |
#include "phy.h" |
| 43 |
|
| 44 |
|
| 45 |
/*-------------------------------------------------------------------------*/ |
| 46 |
|
| 47 |
/* |
| 48 |
* USB Host Controller Driver framework |
| 49 |
* |
| 50 |
* Plugs into usbcore (usb_bus) and lets HCDs share code, minimizing |
| 51 |
* HCD-specific behaviors/bugs. |
| 52 |
* |
| 53 |
* This does error checks, tracks devices and urbs, and delegates to a |
| 54 |
* "hc_driver" only for code (and data) that really needs to know about |
| 55 |
* hardware differences. That includes root hub registers, i/o queues, |
| 56 |
* and so on ... but as little else as possible. |
| 57 |
* |
| 58 |
* Shared code includes most of the "root hub" code (these are emulated, |
| 59 |
* though each HC's hardware works differently) and PCI glue, plus request |
| 60 |
* tracking overhead. The HCD code should only block on spinlocks or on |
| 61 |
* hardware handshaking; blocking on software events (such as other kernel |
| 62 |
* threads releasing resources, or completing actions) is all generic. |
| 63 |
* |
| 64 |
* Happens the USB 2.0 spec says this would be invisible inside the "USBD", |
| 65 |
* and includes mostly a "HCDI" (HCD Interface) along with some APIs used |
| 66 |
* only by the hub driver ... and that neither should be seen or used by |
| 67 |
* usb client device drivers. |
| 68 |
* |
| 69 |
* Contributors of ideas or unattributed patches include: David Brownell, |
| 70 |
* Roman Weissgaerber, Rory Bolt, Greg Kroah-Hartman, ... |
| 71 |
* |
| 72 |
* HISTORY: |
| 73 |
* 2002-02-21 Pull in most of the usb_bus support from usb.c; some |
| 74 |
* associated cleanup. "usb_hcd" still != "usb_bus". |
| 75 |
* 2001-12-12 Initial patch version for Linux 2.5.1 kernel. |
| 76 |
*/ |
| 77 |
|
| 78 |
/*-------------------------------------------------------------------------*/ |
| 79 |
|
| 80 |
/* host controllers we manage */ |
| 81 |
DEFINE_IDR (usb_bus_idr); |
| 82 |
EXPORT_SYMBOL_GPL (usb_bus_idr); |
| 83 |
|
| 84 |
/* used when allocating bus numbers */ |
| 85 |
#define USB_MAXBUS 64 |
| 86 |
|
| 87 |
/* used when updating list of hcds */ |
| 88 |
DEFINE_MUTEX(usb_bus_idr_lock); /* exported only for usbfs */ |
| 89 |
EXPORT_SYMBOL_GPL (usb_bus_idr_lock); |
| 90 |
|
| 91 |
/* used for controlling access to virtual root hubs */ |
| 92 |
static DEFINE_SPINLOCK(hcd_root_hub_lock); |
| 93 |
|
| 94 |
/* used when updating an endpoint's URB list */ |
| 95 |
static DEFINE_SPINLOCK(hcd_urb_list_lock); |
| 96 |
|
| 97 |
/* used to protect against unlinking URBs after the device is gone */ |
| 98 |
static DEFINE_SPINLOCK(hcd_urb_unlink_lock); |
| 99 |
|
| 100 |
/* wait queue for synchronous unlinks */ |
| 101 |
DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue); |
| 102 |
|
| 103 |
/*-------------------------------------------------------------------------*/ |
| 104 |
|
| 105 |
/* |
| 106 |
* Sharable chunks of root hub code. |
| 107 |
*/ |
| 108 |
|
| 109 |
/*-------------------------------------------------------------------------*/ |
| 110 |
#define KERNEL_REL bin2bcd(LINUX_VERSION_MAJOR) |
| 111 |
#define KERNEL_VER bin2bcd(LINUX_VERSION_PATCHLEVEL) |
| 112 |
|
| 113 |
/* usb 3.1 root hub device descriptor */ |
| 114 |
static const u8 usb31_rh_dev_descriptor[18] = { |
| 115 |
0x12, /* __u8 bLength; */ |
| 116 |
USB_DT_DEVICE, /* __u8 bDescriptorType; Device */ |
| 117 |
0x10, 0x03, /* __le16 bcdUSB; v3.1 */ |
| 118 |
|
| 119 |
0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ |
| 120 |
0x00, /* __u8 bDeviceSubClass; */ |
| 121 |
0x03, /* __u8 bDeviceProtocol; USB 3 hub */ |
| 122 |
0x09, /* __u8 bMaxPacketSize0; 2^9 = 512 Bytes */ |
| 123 |
|
| 124 |
0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */ |
| 125 |
0x03, 0x00, /* __le16 idProduct; device 0x0003 */ |
| 126 |
KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */ |
| 127 |
|
| 128 |
0x03, /* __u8 iManufacturer; */ |
| 129 |
0x02, /* __u8 iProduct; */ |
| 130 |
0x01, /* __u8 iSerialNumber; */ |
| 131 |
0x01 /* __u8 bNumConfigurations; */ |
| 132 |
}; |
| 133 |
|
| 134 |
/* usb 3.0 root hub device descriptor */ |
| 135 |
static const u8 usb3_rh_dev_descriptor[18] = { |
| 136 |
0x12, /* __u8 bLength; */ |
| 137 |
USB_DT_DEVICE, /* __u8 bDescriptorType; Device */ |
| 138 |
0x00, 0x03, /* __le16 bcdUSB; v3.0 */ |
| 139 |
|
| 140 |
0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ |
| 141 |
0x00, /* __u8 bDeviceSubClass; */ |
| 142 |
0x03, /* __u8 bDeviceProtocol; USB 3.0 hub */ |
| 143 |
0x09, /* __u8 bMaxPacketSize0; 2^9 = 512 Bytes */ |
| 144 |
|
| 145 |
0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */ |
| 146 |
0x03, 0x00, /* __le16 idProduct; device 0x0003 */ |
| 147 |
KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */ |
| 148 |
|
| 149 |
0x03, /* __u8 iManufacturer; */ |
| 150 |
0x02, /* __u8 iProduct; */ |
| 151 |
0x01, /* __u8 iSerialNumber; */ |
| 152 |
0x01 /* __u8 bNumConfigurations; */ |
| 153 |
}; |
| 154 |
|
| 155 |
/* usb 2.0 root hub device descriptor */ |
| 156 |
static const u8 usb2_rh_dev_descriptor[18] = { |
| 157 |
0x12, /* __u8 bLength; */ |
| 158 |
USB_DT_DEVICE, /* __u8 bDescriptorType; Device */ |
| 159 |
0x00, 0x02, /* __le16 bcdUSB; v2.0 */ |
| 160 |
|
| 161 |
0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ |
| 162 |
0x00, /* __u8 bDeviceSubClass; */ |
| 163 |
0x00, /* __u8 bDeviceProtocol; [ usb 2.0 no TT ] */ |
| 164 |
0x40, /* __u8 bMaxPacketSize0; 64 Bytes */ |
| 165 |
|
| 166 |
0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */ |
| 167 |
0x02, 0x00, /* __le16 idProduct; device 0x0002 */ |
| 168 |
KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */ |
| 169 |
|
| 170 |
0x03, /* __u8 iManufacturer; */ |
| 171 |
0x02, /* __u8 iProduct; */ |
| 172 |
0x01, /* __u8 iSerialNumber; */ |
| 173 |
0x01 /* __u8 bNumConfigurations; */ |
| 174 |
}; |
| 175 |
|
| 176 |
/* no usb 2.0 root hub "device qualifier" descriptor: one speed only */ |
| 177 |
|
| 178 |
/* usb 1.1 root hub device descriptor */ |
| 179 |
static const u8 usb11_rh_dev_descriptor[18] = { |
| 180 |
0x12, /* __u8 bLength; */ |
| 181 |
USB_DT_DEVICE, /* __u8 bDescriptorType; Device */ |
| 182 |
0x10, 0x01, /* __le16 bcdUSB; v1.1 */ |
| 183 |
|
| 184 |
0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ |
| 185 |
0x00, /* __u8 bDeviceSubClass; */ |
| 186 |
0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */ |
| 187 |
0x40, /* __u8 bMaxPacketSize0; 64 Bytes */ |
| 188 |
|
| 189 |
0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */ |
| 190 |
0x01, 0x00, /* __le16 idProduct; device 0x0001 */ |
| 191 |
KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */ |
| 192 |
|
| 193 |
0x03, /* __u8 iManufacturer; */ |
| 194 |
0x02, /* __u8 iProduct; */ |
| 195 |
0x01, /* __u8 iSerialNumber; */ |
| 196 |
0x01 /* __u8 bNumConfigurations; */ |
| 197 |
}; |
| 198 |
|
| 199 |
|
| 200 |
/*-------------------------------------------------------------------------*/ |
| 201 |
|
| 202 |
/* Configuration descriptors for our root hubs */ |
| 203 |
|
| 204 |
static const u8 fs_rh_config_descriptor[] = { |
| 205 |
|
| 206 |
/* one configuration */ |
| 207 |
0x09, /* __u8 bLength; */ |
| 208 |
USB_DT_CONFIG, /* __u8 bDescriptorType; Configuration */ |
| 209 |
0x19, 0x00, /* __le16 wTotalLength; */ |
| 210 |
0x01, /* __u8 bNumInterfaces; (1) */ |
| 211 |
0x01, /* __u8 bConfigurationValue; */ |
| 212 |
0x00, /* __u8 iConfiguration; */ |
| 213 |
0xc0, /* __u8 bmAttributes; |
| 214 |
Bit 7: must be set, |
| 215 |
6: Self-powered, |
| 216 |
5: Remote wakeup, |
| 217 |
4..0: resvd */ |
| 218 |
0x00, /* __u8 MaxPower; */ |
| 219 |
|
| 220 |
/* USB 1.1: |
| 221 |
* USB 2.0, single TT organization (mandatory): |
| 222 |
* one interface, protocol 0 |
| 223 |
* |
| 224 |
* USB 2.0, multiple TT organization (optional): |
| 225 |
* two interfaces, protocols 1 (like single TT) |
| 226 |
* and 2 (multiple TT mode) ... config is |
| 227 |
* sometimes settable |
| 228 |
* NOT IMPLEMENTED |
| 229 |
*/ |
| 230 |
|
| 231 |
/* one interface */ |
| 232 |
0x09, /* __u8 if_bLength; */ |
| 233 |
USB_DT_INTERFACE, /* __u8 if_bDescriptorType; Interface */ |
| 234 |
0x00, /* __u8 if_bInterfaceNumber; */ |
| 235 |
0x00, /* __u8 if_bAlternateSetting; */ |
| 236 |
0x01, /* __u8 if_bNumEndpoints; */ |
| 237 |
0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */ |
| 238 |
0x00, /* __u8 if_bInterfaceSubClass; */ |
| 239 |
0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */ |
| 240 |
0x00, /* __u8 if_iInterface; */ |
| 241 |
|
| 242 |
/* one endpoint (status change endpoint) */ |
| 243 |
0x07, /* __u8 ep_bLength; */ |
| 244 |
USB_DT_ENDPOINT, /* __u8 ep_bDescriptorType; Endpoint */ |
| 245 |
0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */ |
| 246 |
0x03, /* __u8 ep_bmAttributes; Interrupt */ |
| 247 |
0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */ |
| 248 |
0xff /* __u8 ep_bInterval; (255ms -- usb 2.0 spec) */ |
| 249 |
}; |
| 250 |
|
| 251 |
static const u8 hs_rh_config_descriptor[] = { |
| 252 |
|
| 253 |
/* one configuration */ |
| 254 |
0x09, /* __u8 bLength; */ |
| 255 |
USB_DT_CONFIG, /* __u8 bDescriptorType; Configuration */ |
| 256 |
0x19, 0x00, /* __le16 wTotalLength; */ |
| 257 |
0x01, /* __u8 bNumInterfaces; (1) */ |
| 258 |
0x01, /* __u8 bConfigurationValue; */ |
| 259 |
0x00, /* __u8 iConfiguration; */ |
| 260 |
0xc0, /* __u8 bmAttributes; |
| 261 |
Bit 7: must be set, |
| 262 |
6: Self-powered, |
| 263 |
5: Remote wakeup, |
| 264 |
4..0: resvd */ |
| 265 |
0x00, /* __u8 MaxPower; */ |
| 266 |
|
| 267 |
/* USB 1.1: |
| 268 |
* USB 2.0, single TT organization (mandatory): |
| 269 |
* one interface, protocol 0 |
| 270 |
* |
| 271 |
* USB 2.0, multiple TT organization (optional): |
| 272 |
* two interfaces, protocols 1 (like single TT) |
| 273 |
* and 2 (multiple TT mode) ... config is |
| 274 |
* sometimes settable |
| 275 |
* NOT IMPLEMENTED |
| 276 |
*/ |
| 277 |
|
| 278 |
/* one interface */ |
| 279 |
0x09, /* __u8 if_bLength; */ |
| 280 |
USB_DT_INTERFACE, /* __u8 if_bDescriptorType; Interface */ |
| 281 |
0x00, /* __u8 if_bInterfaceNumber; */ |
| 282 |
0x00, /* __u8 if_bAlternateSetting; */ |
| 283 |
0x01, /* __u8 if_bNumEndpoints; */ |
| 284 |
0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */ |
| 285 |
0x00, /* __u8 if_bInterfaceSubClass; */ |
| 286 |
0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */ |
| 287 |
0x00, /* __u8 if_iInterface; */ |
| 288 |
|
| 289 |
/* one endpoint (status change endpoint) */ |
| 290 |
0x07, /* __u8 ep_bLength; */ |
| 291 |
USB_DT_ENDPOINT, /* __u8 ep_bDescriptorType; Endpoint */ |
| 292 |
0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */ |
| 293 |
0x03, /* __u8 ep_bmAttributes; Interrupt */ |
| 294 |
/* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) |
| 295 |
* see hub.c:hub_configure() for details. */ |
| 296 |
(USB_MAXCHILDREN + 1 + 7) / 8, 0x00, |
| 297 |
0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */ |
| 298 |
}; |
| 299 |
|
| 300 |
static const u8 ss_rh_config_descriptor[] = { |
| 301 |
/* one configuration */ |
| 302 |
0x09, /* __u8 bLength; */ |
| 303 |
USB_DT_CONFIG, /* __u8 bDescriptorType; Configuration */ |
| 304 |
0x1f, 0x00, /* __le16 wTotalLength; */ |
| 305 |
0x01, /* __u8 bNumInterfaces; (1) */ |
| 306 |
0x01, /* __u8 bConfigurationValue; */ |
| 307 |
0x00, /* __u8 iConfiguration; */ |
| 308 |
0xc0, /* __u8 bmAttributes; |
| 309 |
Bit 7: must be set, |
| 310 |
6: Self-powered, |
| 311 |
5: Remote wakeup, |
| 312 |
4..0: resvd */ |
| 313 |
0x00, /* __u8 MaxPower; */ |
| 314 |
|
| 315 |
/* one interface */ |
| 316 |
0x09, /* __u8 if_bLength; */ |
| 317 |
USB_DT_INTERFACE, /* __u8 if_bDescriptorType; Interface */ |
| 318 |
0x00, /* __u8 if_bInterfaceNumber; */ |
| 319 |
0x00, /* __u8 if_bAlternateSetting; */ |
| 320 |
0x01, /* __u8 if_bNumEndpoints; */ |
| 321 |
0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */ |
| 322 |
0x00, /* __u8 if_bInterfaceSubClass; */ |
| 323 |
0x00, /* __u8 if_bInterfaceProtocol; */ |
| 324 |
0x00, /* __u8 if_iInterface; */ |
| 325 |
|
| 326 |
/* one endpoint (status change endpoint) */ |
| 327 |
0x07, /* __u8 ep_bLength; */ |
| 328 |
USB_DT_ENDPOINT, /* __u8 ep_bDescriptorType; Endpoint */ |
| 329 |
0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */ |
| 330 |
0x03, /* __u8 ep_bmAttributes; Interrupt */ |
| 331 |
0x02, 0x00, /* __le16 ep_wMaxPacketSize; 2 bytes per USB3 10.15.1 */ |
| 332 |
0x0c, /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */ |
| 333 |
|
| 334 |
/* one SuperSpeed endpoint companion descriptor */ |
| 335 |
0x06, /* __u8 ss_bLength */ |
| 336 |
USB_DT_SS_ENDPOINT_COMP, /* __u8 ss_bDescriptorType; SuperSpeed EP */ |
| 337 |
/* Companion */ |
| 338 |
0x00, /* __u8 ss_bMaxBurst; allows 1 TX between ACKs */ |
| 339 |
0x00, /* __u8 ss_bmAttributes; 1 packet per service interval */ |
| 340 |
0x02, 0x00 /* __le16 ss_wBytesPerInterval; 15 bits for max 15 ports */ |
| 341 |
}; |
| 342 |
|
| 343 |
/* authorized_default behaviour: |
| 344 |
* -1 is authorized for all devices (leftover from wireless USB) |
| 345 |
* 0 is unauthorized for all devices |
| 346 |
* 1 is authorized for all devices |
| 347 |
* 2 is authorized for internal devices |
| 348 |
*/ |
| 349 |
#define USB_AUTHORIZE_WIRED -1 |
| 350 |
#define USB_AUTHORIZE_NONE 0 |
| 351 |
#define USB_AUTHORIZE_ALL 1 |
| 352 |
#define USB_AUTHORIZE_INTERNAL 2 |
| 353 |
|
| 354 |
static int authorized_default = CONFIG_USB_DEFAULT_AUTHORIZATION_MODE; |
| 355 |
module_param(authorized_default, int, S_IRUGO|S_IWUSR); |
| 356 |
MODULE_PARM_DESC(authorized_default, |
| 357 |
"Default USB device authorization: 0 is not authorized, 1 is authorized (default), 2 is authorized for internal devices, -1 is authorized (same as 1)"); |
| 358 |
/*-------------------------------------------------------------------------*/ |
| 359 |
|
| 360 |
/** |
| 361 |
* ascii2desc() - Helper routine for producing UTF-16LE string descriptors |
| 362 |
* @s: Null-terminated ASCII (actually ISO-8859-1) string |
| 363 |
* @buf: Buffer for USB string descriptor (header + UTF-16LE) |
| 364 |
* @len: Length (in bytes; may be odd) of descriptor buffer. |
| 365 |
* |
| 366 |
* Return: The number of bytes filled in: 2 + 2*strlen(s) or @len, |
| 367 |
* whichever is less. |
| 368 |
* |
| 369 |
* Note: |
| 370 |
* USB String descriptors can contain at most 126 characters; input |
| 371 |
* strings longer than that are truncated. |
| 372 |
*/ |
| 373 |
static unsigned |
| 374 |
ascii2desc(char const *s, u8 *buf, unsigned len) |
| 375 |
{ |
| 376 |
unsigned n, t = 2 + 2*strlen(s); |
| 377 |
|
| 378 |
if (t > 254) |
| 379 |
t = 254; /* Longest possible UTF string descriptor */ |
| 380 |
if (len > t) |
| 381 |
len = t; |
| 382 |
|
| 383 |
t += USB_DT_STRING << 8; /* Now t is first 16 bits to store */ |
| 384 |
|
| 385 |
n = len; |
| 386 |
while (n--) { |
| 387 |
*buf++ = t; |
| 388 |
if (!n--) |
| 389 |
break; |
| 390 |
*buf++ = t >> 8; |
| 391 |
t = (unsigned char)*s++; |
| 392 |
} |
| 393 |
return len; |
| 394 |
} |
| 395 |
|
| 396 |
/** |
| 397 |
* rh_string() - provides string descriptors for root hub |
| 398 |
* @id: the string ID number (0: langids, 1: serial #, 2: product, 3: vendor) |
| 399 |
* @hcd: the host controller for this root hub |
| 400 |
* @data: buffer for output packet |
| 401 |
* @len: length of the provided buffer |
| 402 |
* |
| 403 |
* Produces either a manufacturer, product or serial number string for the |
| 404 |
* virtual root hub device. |
| 405 |
* |
| 406 |
* Return: The number of bytes filled in: the length of the descriptor or |
| 407 |
* of the provided buffer, whichever is less. |
| 408 |
*/ |
| 409 |
static unsigned |
| 410 |
rh_string(int id, struct usb_hcd const *hcd, u8 *data, unsigned len) |
| 411 |
{ |
| 412 |
char buf[160]; |
| 413 |
char const *s; |
| 414 |
static char const langids[4] = {4, USB_DT_STRING, 0x09, 0x04}; |
| 415 |
|
| 416 |
/* language ids */ |
| 417 |
switch (id) { |
| 418 |
case 0: |
| 419 |
/* Array of LANGID codes (0x0409 is MSFT-speak for "en-us") */ |
| 420 |
/* See http://www.usb.org/developers/docs/USB_LANGIDs.pdf */ |
| 421 |
if (len > 4) |
| 422 |
len = 4; |
| 423 |
memcpy(data, langids, len); |
| 424 |
return len; |
| 425 |
case 1: |
| 426 |
/* Serial number */ |
| 427 |
s = hcd->self.bus_name; |
| 428 |
break; |
| 429 |
case 2: |
| 430 |
/* Product name */ |
| 431 |
s = hcd->product_desc; |
| 432 |
break; |
| 433 |
case 3: |
| 434 |
/* Manufacturer */ |
| 435 |
snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname, |
| 436 |
init_utsname()->release, hcd->driver->description); |
| 437 |
s = buf; |
| 438 |
break; |
| 439 |
default: |
| 440 |
/* Can't happen; caller guarantees it */ |
| 441 |
return 0; |
| 442 |
} |
| 443 |
|
| 444 |
return ascii2desc(s, data, len); |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
/* Root hub control transfers execute synchronously */ |
| 449 |
static int rh_call_control (struct usb_hcd *hcd, struct urb *urb) |
| 450 |
{ |
| 451 |
struct usb_ctrlrequest *cmd; |
| 452 |
u16 typeReq, wValue, wIndex, wLength; |
| 453 |
u8 *ubuf = urb->transfer_buffer; |
| 454 |
unsigned len = 0; |
| 455 |
int status; |
| 456 |
u8 patch_wakeup = 0; |
| 457 |
u8 patch_protocol = 0; |
| 458 |
u16 tbuf_size; |
| 459 |
u8 *tbuf = NULL; |
| 460 |
const u8 *bufp; |
| 461 |
|
| 462 |
might_sleep(); |
| 463 |
|
| 464 |
spin_lock_irq(&hcd_root_hub_lock); |
| 465 |
status = usb_hcd_link_urb_to_ep(hcd, urb); |
| 466 |
spin_unlock_irq(&hcd_root_hub_lock); |
| 467 |
if (status) |
| 468 |
return status; |
| 469 |
urb->hcpriv = hcd; /* Indicate it's queued */ |
| 470 |
|
| 471 |
cmd = (struct usb_ctrlrequest *) urb->setup_packet; |
| 472 |
typeReq = (cmd->bRequestType << 8) | cmd->bRequest; |
| 473 |
wValue = le16_to_cpu (cmd->wValue); |
| 474 |
wIndex = le16_to_cpu (cmd->wIndex); |
| 475 |
wLength = le16_to_cpu (cmd->wLength); |
| 476 |
|
| 477 |
if (wLength > urb->transfer_buffer_length) |
| 478 |
goto error; |
| 479 |
|
| 480 |
/* |
| 481 |
* tbuf should be at least as big as the |
| 482 |
* USB hub descriptor. |
| 483 |
*/ |
| 484 |
tbuf_size = max_t(u16, sizeof(struct usb_hub_descriptor), wLength); |
| 485 |
tbuf = kzalloc(tbuf_size, GFP_KERNEL); |
| 486 |
if (!tbuf) { |
| 487 |
status = -ENOMEM; |
| 488 |
goto err_alloc; |
| 489 |
} |
| 490 |
|
| 491 |
bufp = tbuf; |
| 492 |
|
| 493 |
|
| 494 |
urb->actual_length = 0; |
| 495 |
switch (typeReq) { |
| 496 |
|
| 497 |
/* DEVICE REQUESTS */ |
| 498 |
|
| 499 |
/* The root hub's remote wakeup enable bit is implemented using |
| 500 |
* driver model wakeup flags. If this system supports wakeup |
| 501 |
* through USB, userspace may change the default "allow wakeup" |
| 502 |
* policy through sysfs or these calls. |
| 503 |
* |
| 504 |
* Most root hubs support wakeup from downstream devices, for |
| 505 |
* runtime power management (disabling USB clocks and reducing |
| 506 |
* VBUS power usage). However, not all of them do so; silicon, |
| 507 |
* board, and BIOS bugs here are not uncommon, so these can't |
| 508 |
* be treated quite like external hubs. |
| 509 |
* |
| 510 |
* Likewise, not all root hubs will pass wakeup events upstream, |
| 511 |
* to wake up the whole system. So don't assume root hub and |
| 512 |
* controller capabilities are identical. |
| 513 |
*/ |
| 514 |
|
| 515 |
case DeviceRequest | USB_REQ_GET_STATUS: |
| 516 |
tbuf[0] = (device_may_wakeup(&hcd->self.root_hub->dev) |
| 517 |
<< USB_DEVICE_REMOTE_WAKEUP) |
| 518 |
| (1 << USB_DEVICE_SELF_POWERED); |
| 519 |
tbuf[1] = 0; |
| 520 |
len = 2; |
| 521 |
break; |
| 522 |
case DeviceOutRequest | USB_REQ_CLEAR_FEATURE: |
| 523 |
if (wValue == USB_DEVICE_REMOTE_WAKEUP) |
| 524 |
device_set_wakeup_enable(&hcd->self.root_hub->dev, 0); |
| 525 |
else |
| 526 |
goto error; |
| 527 |
break; |
| 528 |
case DeviceOutRequest | USB_REQ_SET_FEATURE: |
| 529 |
if (device_can_wakeup(&hcd->self.root_hub->dev) |
| 530 |
&& wValue == USB_DEVICE_REMOTE_WAKEUP) |
| 531 |
device_set_wakeup_enable(&hcd->self.root_hub->dev, 1); |
| 532 |
else |
| 533 |
goto error; |
| 534 |
break; |
| 535 |
case DeviceRequest | USB_REQ_GET_CONFIGURATION: |
| 536 |
tbuf[0] = 1; |
| 537 |
len = 1; |
| 538 |
fallthrough; |
| 539 |
case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: |
| 540 |
break; |
| 541 |
case DeviceRequest | USB_REQ_GET_DESCRIPTOR: |
| 542 |
switch (wValue & 0xff00) { |
| 543 |
case USB_DT_DEVICE << 8: |
| 544 |
switch (hcd->speed) { |
| 545 |
case HCD_USB32: |
| 546 |
case HCD_USB31: |
| 547 |
bufp = usb31_rh_dev_descriptor; |
| 548 |
break; |
| 549 |
case HCD_USB3: |
| 550 |
bufp = usb3_rh_dev_descriptor; |
| 551 |
break; |
| 552 |
case HCD_USB2: |
| 553 |
bufp = usb2_rh_dev_descriptor; |
| 554 |
break; |
| 555 |
case HCD_USB11: |
| 556 |
bufp = usb11_rh_dev_descriptor; |
| 557 |
break; |
| 558 |
default: |
| 559 |
goto error; |
| 560 |
} |
| 561 |
len = 18; |
| 562 |
if (hcd->has_tt) |
| 563 |
patch_protocol = 1; |
| 564 |
break; |
| 565 |
case USB_DT_CONFIG << 8: |
| 566 |
switch (hcd->speed) { |
| 567 |
case HCD_USB32: |
| 568 |
case HCD_USB31: |
| 569 |
case HCD_USB3: |
| 570 |
bufp = ss_rh_config_descriptor; |
| 571 |
len = sizeof ss_rh_config_descriptor; |
| 572 |
break; |
| 573 |
case HCD_USB2: |
| 574 |
bufp = hs_rh_config_descriptor; |
| 575 |
len = sizeof hs_rh_config_descriptor; |
| 576 |
break; |
| 577 |
case HCD_USB11: |
| 578 |
bufp = fs_rh_config_descriptor; |
| 579 |
len = sizeof fs_rh_config_descriptor; |
| 580 |
break; |
| 581 |
default: |
| 582 |
goto error; |
| 583 |
} |
| 584 |
if (device_can_wakeup(&hcd->self.root_hub->dev)) |
| 585 |
patch_wakeup = 1; |
| 586 |
break; |
| 587 |
case USB_DT_STRING << 8: |
| 588 |
if ((wValue & 0xff) < 4) |
| 589 |
urb->actual_length = rh_string(wValue & 0xff, |
| 590 |
hcd, ubuf, wLength); |
| 591 |
else /* unsupported IDs --> "protocol stall" */ |
| 592 |
goto error; |
| 593 |
break; |
| 594 |
case USB_DT_BOS << 8: |
| 595 |
goto nongeneric; |
| 596 |
default: |
| 597 |
goto error; |
| 598 |
} |
| 599 |
break; |
| 600 |
case DeviceRequest | USB_REQ_GET_INTERFACE: |
| 601 |
tbuf[0] = 0; |
| 602 |
len = 1; |
| 603 |
fallthrough; |
| 604 |
case DeviceOutRequest | USB_REQ_SET_INTERFACE: |
| 605 |
break; |
| 606 |
case DeviceOutRequest | USB_REQ_SET_ADDRESS: |
| 607 |
/* wValue == urb->dev->devaddr */ |
| 608 |
dev_dbg (hcd->self.controller, "root hub device address %d\n", |
| 609 |
wValue); |
| 610 |
break; |
| 611 |
|
| 612 |
/* INTERFACE REQUESTS (no defined feature/status flags) */ |
| 613 |
|
| 614 |
/* ENDPOINT REQUESTS */ |
| 615 |
|
| 616 |
case EndpointRequest | USB_REQ_GET_STATUS: |
| 617 |
/* ENDPOINT_HALT flag */ |
| 618 |
tbuf[0] = 0; |
| 619 |
tbuf[1] = 0; |
| 620 |
len = 2; |
| 621 |
fallthrough; |
| 622 |
case EndpointOutRequest | USB_REQ_CLEAR_FEATURE: |
| 623 |
case EndpointOutRequest | USB_REQ_SET_FEATURE: |
| 624 |
dev_dbg (hcd->self.controller, "no endpoint features yet\n"); |
| 625 |
break; |
| 626 |
|
| 627 |
/* CLASS REQUESTS (and errors) */ |
| 628 |
|
| 629 |
default: |
| 630 |
nongeneric: |
| 631 |
/* non-generic request */ |
| 632 |
switch (typeReq) { |
| 633 |
case GetHubStatus: |
| 634 |
len = 4; |
| 635 |
break; |
| 636 |
case GetPortStatus: |
| 637 |
if (wValue == HUB_PORT_STATUS) |
| 638 |
len = 4; |
| 639 |
else |
| 640 |
/* other port status types return 8 bytes */ |
| 641 |
len = 8; |
| 642 |
break; |
| 643 |
case GetHubDescriptor: |
| 644 |
len = sizeof (struct usb_hub_descriptor); |
| 645 |
break; |
| 646 |
case DeviceRequest | USB_REQ_GET_DESCRIPTOR: |
| 647 |
/* len is returned by hub_control */ |
| 648 |
break; |
| 649 |
} |
| 650 |
status = hcd->driver->hub_control (hcd, |
| 651 |
typeReq, wValue, wIndex, |
| 652 |
tbuf, wLength); |
| 653 |
|
| 654 |
if (typeReq == GetHubDescriptor) |
| 655 |
usb_hub_adjust_deviceremovable(hcd->self.root_hub, |
| 656 |
(struct usb_hub_descriptor *)tbuf); |
| 657 |
break; |
| 658 |
error: |
| 659 |
/* "protocol stall" on error */ |
| 660 |
status = -EPIPE; |
| 661 |
} |
| 662 |
|
| 663 |
if (status < 0) { |
| 664 |
len = 0; |
| 665 |
if (status != -EPIPE) { |
| 666 |
dev_dbg (hcd->self.controller, |
| 667 |
"CTRL: TypeReq=0x%x val=0x%x " |
| 668 |
"idx=0x%x len=%d ==> %d\n", |
| 669 |
typeReq, wValue, wIndex, |
| 670 |
wLength, status); |
| 671 |
} |
| 672 |
} else if (status > 0) { |
| 673 |
/* hub_control may return the length of data copied. */ |
| 674 |
len = status; |
| 675 |
status = 0; |
| 676 |
} |
| 677 |
if (len) { |
| 678 |
if (urb->transfer_buffer_length < len) |
| 679 |
len = urb->transfer_buffer_length; |
| 680 |
urb->actual_length = len; |
| 681 |
/* always USB_DIR_IN, toward host */ |
| 682 |
memcpy (ubuf, bufp, len); |
| 683 |
|
| 684 |
/* report whether RH hardware supports remote wakeup */ |
| 685 |
if (patch_wakeup && |
| 686 |
len > offsetof (struct usb_config_descriptor, |
| 687 |
bmAttributes)) |
| 688 |
((struct usb_config_descriptor *)ubuf)->bmAttributes |
| 689 |
|= USB_CONFIG_ATT_WAKEUP; |
| 690 |
|
| 691 |
/* report whether RH hardware has an integrated TT */ |
| 692 |
if (patch_protocol && |
| 693 |
len > offsetof(struct usb_device_descriptor, |
| 694 |
bDeviceProtocol)) |
| 695 |
((struct usb_device_descriptor *) ubuf)-> |
| 696 |
bDeviceProtocol = USB_HUB_PR_HS_SINGLE_TT; |
| 697 |
} |
| 698 |
|
| 699 |
kfree(tbuf); |
| 700 |
err_alloc: |
| 701 |
|
| 702 |
/* any errors get returned through the urb completion */ |
| 703 |
spin_lock_irq(&hcd_root_hub_lock); |
| 704 |
usb_hcd_unlink_urb_from_ep(hcd, urb); |
| 705 |
usb_hcd_giveback_urb(hcd, urb, status); |
| 706 |
spin_unlock_irq(&hcd_root_hub_lock); |
| 707 |
return 0; |
| 708 |
} |
| 709 |
|
| 710 |
/*-------------------------------------------------------------------------*/ |
| 711 |
|
| 712 |
/* |
| 713 |
* Root Hub interrupt transfers are polled using a timer if the |
| 714 |
* driver requests it; otherwise the driver is responsible for |
| 715 |
* calling usb_hcd_poll_rh_status() when an event occurs. |
| 716 |
* |
| 717 |
* Completion handler may not sleep. See usb_hcd_giveback_urb() for details. |
| 718 |
*/ |
| 719 |
void usb_hcd_poll_rh_status(struct usb_hcd *hcd) |
| 720 |
{ |
| 721 |
struct urb *urb; |
| 722 |
int length; |
| 723 |
int status; |
| 724 |
unsigned long flags; |
| 725 |
char buffer[6]; /* Any root hubs with > 31 ports? */ |
| 726 |
|
| 727 |
if (unlikely(!hcd->rh_pollable)) |
| 728 |
return; |
| 729 |
if (!hcd->uses_new_polling && !hcd->status_urb) |
| 730 |
return; |
| 731 |
|
| 732 |
length = hcd->driver->hub_status_data(hcd, buffer); |
| 733 |
if (length > 0) { |
| 734 |
|
| 735 |
/* try to complete the status urb */ |
| 736 |
spin_lock_irqsave(&hcd_root_hub_lock, flags); |
| 737 |
urb = hcd->status_urb; |
| 738 |
if (urb) { |
| 739 |
clear_bit(HCD_FLAG_POLL_PENDING, &hcd->flags); |
| 740 |
hcd->status_urb = NULL; |
| 741 |
if (urb->transfer_buffer_length >= length) { |
| 742 |
status = 0; |
| 743 |
} else { |
| 744 |
status = -EOVERFLOW; |
| 745 |
length = urb->transfer_buffer_length; |
| 746 |
} |
| 747 |
urb->actual_length = length; |
| 748 |
memcpy(urb->transfer_buffer, buffer, length); |
| 749 |
|
| 750 |
usb_hcd_unlink_urb_from_ep(hcd, urb); |
| 751 |
usb_hcd_giveback_urb(hcd, urb, status); |
| 752 |
} else { |
| 753 |
length = 0; |
| 754 |
set_bit(HCD_FLAG_POLL_PENDING, &hcd->flags); |
| 755 |
} |
| 756 |
spin_unlock_irqrestore(&hcd_root_hub_lock, flags); |
| 757 |
} |
| 758 |
|
| 759 |
/* The USB 2.0 spec says 256 ms. This is close enough and won't |
| 760 |
* exceed that limit if HZ is 100. The math is more clunky than |
| 761 |
* maybe expected, this is to make sure that all timers for USB devices |
| 762 |
* fire at the same time to give the CPU a break in between */ |
| 763 |
if (hcd->uses_new_polling ? HCD_POLL_RH(hcd) : |
| 764 |
(length == 0 && hcd->status_urb != NULL)) |
| 765 |
mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4)); |
| 766 |
} |
| 767 |
EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status); |
| 768 |
|
| 769 |
/* timer callback */ |
| 770 |
static void rh_timer_func (struct timer_list *t) |
| 771 |
{ |
| 772 |
struct usb_hcd *_hcd = timer_container_of(_hcd, t, rh_timer); |
| 773 |
|
| 774 |
usb_hcd_poll_rh_status(_hcd); |
| 775 |
} |
| 776 |
|
| 777 |
/*-------------------------------------------------------------------------*/ |
| 778 |
|
| 779 |
static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb) |
| 780 |
{ |
| 781 |
int retval; |
| 782 |
unsigned long flags; |
| 783 |
unsigned len = 1 + (urb->dev->maxchild / 8); |
| 784 |
|
| 785 |
spin_lock_irqsave (&hcd_root_hub_lock, flags); |
| 786 |
if (hcd->status_urb || urb->transfer_buffer_length < len) { |
| 787 |
dev_dbg (hcd->self.controller, "not queuing rh status urb\n"); |
| 788 |
retval = -EINVAL; |
| 789 |
goto done; |
| 790 |
} |
| 791 |
|
| 792 |
retval = usb_hcd_link_urb_to_ep(hcd, urb); |
| 793 |
if (retval) |
| 794 |
goto done; |
| 795 |
|
| 796 |
hcd->status_urb = urb; |
| 797 |
urb->hcpriv = hcd; /* indicate it's queued */ |
| 798 |
if (!hcd->uses_new_polling) |
| 799 |
mod_timer(&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4)); |
| 800 |
|
| 801 |
/* If a status change has already occurred, report it ASAP */ |
| 802 |
else if (HCD_POLL_PENDING(hcd)) |
| 803 |
mod_timer(&hcd->rh_timer, jiffies); |
| 804 |
retval = 0; |
| 805 |
done: |
| 806 |
spin_unlock_irqrestore (&hcd_root_hub_lock, flags); |
| 807 |
return retval; |
| 808 |
} |
| 809 |
|
| 810 |
static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb) |
| 811 |
{ |
| 812 |
if (usb_endpoint_xfer_int(&urb->ep->desc)) |
| 813 |
return rh_queue_status (hcd, urb); |
| 814 |
if (usb_endpoint_xfer_control(&urb->ep->desc)) |
| 815 |
return rh_call_control (hcd, urb); |
| 816 |
return -EINVAL; |
| 817 |
} |
| 818 |
|
| 819 |
/*-------------------------------------------------------------------------*/ |
| 820 |
|
| 821 |
/* Unlinks of root-hub control URBs are legal, but they don't do anything |
| 822 |
* since these URBs always execute synchronously. |
| 823 |
*/ |
| 824 |
static int usb_rh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) |
| 825 |
{ |
| 826 |
unsigned long flags; |
| 827 |
int rc; |
| 828 |
|
| 829 |
spin_lock_irqsave(&hcd_root_hub_lock, flags); |
| 830 |
rc = usb_hcd_check_unlink_urb(hcd, urb, status); |
| 831 |
if (rc) |
| 832 |
goto done; |
| 833 |
|
| 834 |
if (usb_endpoint_num(&urb->ep->desc) == 0) { /* Control URB */ |
| 835 |
; /* Do nothing */ |
| 836 |
|
| 837 |
} else { /* Status URB */ |
| 838 |
if (!hcd->uses_new_polling) |
| 839 |
timer_delete(&hcd->rh_timer); |
| 840 |
if (urb == hcd->status_urb) { |
| 841 |
hcd->status_urb = NULL; |
| 842 |
usb_hcd_unlink_urb_from_ep(hcd, urb); |
| 843 |
usb_hcd_giveback_urb(hcd, urb, status); |
| 844 |
} |
| 845 |
} |
| 846 |
done: |
| 847 |
spin_unlock_irqrestore(&hcd_root_hub_lock, flags); |
| 848 |
return rc; |
| 849 |
} |
| 850 |
|
| 851 |
|
| 852 |
/*-------------------------------------------------------------------------*/ |
| 853 |
|
| 854 |
/** |
| 855 |
* usb_bus_init - shared initialization code |
| 856 |
* @bus: the bus structure being initialized |
| 857 |
* |
| 858 |
* This code is used to initialize a usb_bus structure, memory for which is |
| 859 |
* separately managed. |
| 860 |
*/ |
| 861 |
static void usb_bus_init (struct usb_bus *bus) |
| 862 |
{ |
| 863 |
memset(&bus->devmap, 0, sizeof(bus->devmap)); |
| 864 |
|
| 865 |
bus->devnum_next = 1; |
| 866 |
|
| 867 |
bus->root_hub = NULL; |
| 868 |
bus->busnum = -1; |
| 869 |
bus->bandwidth_allocated = 0; |
| 870 |
bus->bandwidth_int_reqs = 0; |
| 871 |
bus->bandwidth_isoc_reqs = 0; |
| 872 |
mutex_init(&bus->devnum_next_mutex); |
| 873 |
} |
| 874 |
|
| 875 |
/*-------------------------------------------------------------------------*/ |
| 876 |
|
| 877 |
/** |
| 878 |
* usb_register_bus - registers the USB host controller with the usb core |
| 879 |
* @bus: pointer to the bus to register |
| 880 |
* |
| 881 |
* Context: task context, might sleep. |
| 882 |
* |
| 883 |
* Assigns a bus number, and links the controller into usbcore data |
| 884 |
* structures so that it can be seen by scanning the bus list. |
| 885 |
* |
| 886 |
* Return: 0 if successful. A negative error code otherwise. |
| 887 |
*/ |
| 888 |
static int usb_register_bus(struct usb_bus *bus) |
| 889 |
{ |
| 890 |
int result = -E2BIG; |
| 891 |
int busnum; |
| 892 |
|
| 893 |
mutex_lock(&usb_bus_idr_lock); |
| 894 |
busnum = idr_alloc(&usb_bus_idr, bus, 1, USB_MAXBUS, GFP_KERNEL); |
| 895 |
if (busnum < 0) { |
| 896 |
pr_err("%s: failed to get bus number\n", usbcore_name); |
| 897 |
goto error_find_busnum; |
| 898 |
} |
| 899 |
bus->busnum = busnum; |
| 900 |
mutex_unlock(&usb_bus_idr_lock); |
| 901 |
|
| 902 |
usb_notify_add_bus(bus); |
| 903 |
|
| 904 |
dev_info (bus->controller, "new USB bus registered, assigned bus " |
| 905 |
"number %d\n", bus->busnum); |
| 906 |
return 0; |
| 907 |
|
| 908 |
error_find_busnum: |
| 909 |
mutex_unlock(&usb_bus_idr_lock); |
| 910 |
return result; |
| 911 |
} |
| 912 |
|
| 913 |
/** |
| 914 |
* usb_deregister_bus - deregisters the USB host controller |
| 915 |
* @bus: pointer to the bus to deregister |
| 916 |
* |
| 917 |
* Context: task context, might sleep. |
| 918 |
* |
| 919 |
* Recycles the bus number, and unlinks the controller from usbcore data |
| 920 |
* structures so that it won't be seen by scanning the bus list. |
| 921 |
*/ |
| 922 |
static void usb_deregister_bus (struct usb_bus *bus) |
| 923 |
{ |
| 924 |
dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum); |
| 925 |
|
| 926 |
/* |
| 927 |
* NOTE: make sure that all the devices are removed by the |
| 928 |
* controller code, as well as having it call this when cleaning |
| 929 |
* itself up |
| 930 |
*/ |
| 931 |
mutex_lock(&usb_bus_idr_lock); |
| 932 |
idr_remove(&usb_bus_idr, bus->busnum); |
| 933 |
mutex_unlock(&usb_bus_idr_lock); |
| 934 |
|
| 935 |
usb_notify_remove_bus(bus); |
| 936 |
} |
| 937 |
|
| 938 |
/** |
| 939 |
* register_root_hub - called by usb_add_hcd() to register a root hub |
| 940 |
* @hcd: host controller for this root hub |
| 941 |
* |
| 942 |
* This function registers the root hub with the USB subsystem. It sets up |
| 943 |
* the device properly in the device tree and then calls usb_new_device() |
| 944 |
* to register the usb device. It also assigns the root hub's USB address |
| 945 |
* (always 1). |
| 946 |
* |
| 947 |
* Return: 0 if successful. A negative error code otherwise. |
| 948 |
*/ |
| 949 |
static int register_root_hub(struct usb_hcd *hcd) |
| 950 |
{ |
| 951 |
struct device *parent_dev = hcd->self.controller; |
| 952 |
struct usb_device *usb_dev = hcd->self.root_hub; |
| 953 |
struct usb_device_descriptor *descr; |
| 954 |
const int devnum = 1; |
| 955 |
int retval; |
| 956 |
|
| 957 |
usb_dev->devnum = devnum; |
| 958 |
usb_dev->bus->devnum_next = devnum + 1; |
| 959 |
set_bit(devnum, usb_dev->bus->devmap); |
| 960 |
usb_set_device_state(usb_dev, USB_STATE_ADDRESS); |
| 961 |
|
| 962 |
mutex_lock(&usb_bus_idr_lock); |
| 963 |
|
| 964 |
usb_dev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); |
| 965 |
descr = usb_get_device_descriptor(usb_dev); |
| 966 |
if (IS_ERR(descr)) { |
| 967 |
retval = PTR_ERR(descr); |
| 968 |
mutex_unlock(&usb_bus_idr_lock); |
| 969 |
dev_dbg (parent_dev, "can't read %s device descriptor %d\n", |
| 970 |
dev_name(&usb_dev->dev), retval); |
| 971 |
return retval; |
| 972 |
} |
| 973 |
usb_dev->descriptor = *descr; |
| 974 |
kfree(descr); |
| 975 |
|
| 976 |
if (le16_to_cpu(usb_dev->descriptor.bcdUSB) >= 0x0201) { |
| 977 |
retval = usb_get_bos_descriptor(usb_dev); |
| 978 |
if (!retval) { |
| 979 |
usb_dev->lpm_capable = usb_device_supports_lpm(usb_dev); |
| 980 |
} else if (usb_dev->speed >= USB_SPEED_SUPER) { |
| 981 |
mutex_unlock(&usb_bus_idr_lock); |
| 982 |
dev_dbg(parent_dev, "can't read %s bos descriptor %d\n", |
| 983 |
dev_name(&usb_dev->dev), retval); |
| 984 |
return retval; |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
retval = usb_new_device (usb_dev); |
| 989 |
if (retval) { |
| 990 |
dev_err (parent_dev, "can't register root hub for %s, %d\n", |
| 991 |
dev_name(&usb_dev->dev), retval); |
| 992 |
} else { |
| 993 |
spin_lock_irq (&hcd_root_hub_lock); |
| 994 |
hcd->rh_registered = 1; |
| 995 |
spin_unlock_irq (&hcd_root_hub_lock); |
| 996 |
|
| 997 |
/* Did the HC die before the root hub was registered? */ |
| 998 |
if (HCD_DEAD(hcd)) |
| 999 |
usb_hc_died (hcd); /* This time clean up */ |
| 1000 |
} |
| 1001 |
mutex_unlock(&usb_bus_idr_lock); |
| 1002 |
|
| 1003 |
return retval; |
| 1004 |
} |
| 1005 |
|
| 1006 |
/* |
| 1007 |
* usb_hcd_start_port_resume - a root-hub port is sending a resume signal |
| 1008 |
* @bus: the bus which the root hub belongs to |
| 1009 |
* @portnum: the port which is being resumed |
| 1010 |
* |
| 1011 |
* HCDs should call this function when they know that a resume signal is |
| 1012 |
* being sent to a root-hub port. The root hub will be prevented from |
| 1013 |
* going into autosuspend until usb_hcd_end_port_resume() is called. |
| 1014 |
* |
| 1015 |
* The bus's private lock must be held by the caller. |
| 1016 |
*/ |
| 1017 |
void usb_hcd_start_port_resume(struct usb_bus *bus, int portnum) |
| 1018 |
{ |
| 1019 |
unsigned bit = 1 << portnum; |
| 1020 |
|
| 1021 |
if (!(bus->resuming_ports & bit)) { |
| 1022 |
bus->resuming_ports |= bit; |
| 1023 |
pm_runtime_get_noresume(&bus->root_hub->dev); |
| 1024 |
} |
| 1025 |
} |
| 1026 |
EXPORT_SYMBOL_GPL(usb_hcd_start_port_resume); |
| 1027 |
|
| 1028 |
/* |
| 1029 |
* usb_hcd_end_port_resume - a root-hub port has stopped sending a resume signal |
| 1030 |
* @bus: the bus which the root hub belongs to |
| 1031 |
* @portnum: the port which is being resumed |
| 1032 |
* |
| 1033 |
* HCDs should call this function when they know that a resume signal has |
| 1034 |
* stopped being sent to a root-hub port. The root hub will be allowed to |
| 1035 |
* autosuspend again. |
| 1036 |
* |
| 1037 |
* The bus's private lock must be held by the caller. |
| 1038 |
*/ |
| 1039 |
void usb_hcd_end_port_resume(struct usb_bus *bus, int portnum) |
| 1040 |
{ |
| 1041 |
unsigned bit = 1 << portnum; |
| 1042 |
|
| 1043 |
if (bus->resuming_ports & bit) { |
| 1044 |
bus->resuming_ports &= ~bit; |
| 1045 |
pm_runtime_put_noidle(&bus->root_hub->dev); |
| 1046 |
} |
| 1047 |
} |
| 1048 |
EXPORT_SYMBOL_GPL(usb_hcd_end_port_resume); |
| 1049 |
|
| 1050 |
/*-------------------------------------------------------------------------*/ |
| 1051 |
|
| 1052 |
/** |
| 1053 |
* usb_calc_bus_time - approximate periodic transaction time in nanoseconds |
| 1054 |
* @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH} |
| 1055 |
* @is_input: true iff the transaction sends data to the host |
| 1056 |
* @isoc: true for isochronous transactions, false for interrupt ones |
| 1057 |
* @bytecount: how many bytes in the transaction. |
| 1058 |
* |
| 1059 |
* Return: Approximate bus time in nanoseconds for a periodic transaction. |
| 1060 |
* |
| 1061 |
* Note: |
| 1062 |
* See USB 2.0 spec section 5.11.3; only periodic transfers need to be |
| 1063 |
* scheduled in software, this function is only used for such scheduling. |
| 1064 |
*/ |
| 1065 |
long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount) |
| 1066 |
{ |
| 1067 |
unsigned long tmp; |
| 1068 |
|
| 1069 |
switch (speed) { |
| 1070 |
case USB_SPEED_LOW: /* INTR only */ |
| 1071 |
if (is_input) { |
| 1072 |
tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L; |
| 1073 |
return 64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp; |
| 1074 |
} else { |
| 1075 |
tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L; |
| 1076 |
return 64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp; |
| 1077 |
} |
| 1078 |
case USB_SPEED_FULL: /* ISOC or INTR */ |
| 1079 |
if (isoc) { |
| 1080 |
tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L; |
| 1081 |
return ((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp; |
| 1082 |
} else { |
| 1083 |
tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L; |
| 1084 |
return 9107L + BW_HOST_DELAY + tmp; |
| 1085 |
} |
| 1086 |
case USB_SPEED_HIGH: /* ISOC or INTR */ |
| 1087 |
/* FIXME adjust for input vs output */ |
| 1088 |
if (isoc) |
| 1089 |
tmp = HS_NSECS_ISO (bytecount); |
| 1090 |
else |
| 1091 |
tmp = HS_NSECS (bytecount); |
| 1092 |
return tmp; |
| 1093 |
default: |
| 1094 |
pr_debug ("%s: bogus device speed!\n", usbcore_name); |
| 1095 |
return -1; |
| 1096 |
} |
| 1097 |
} |
| 1098 |
EXPORT_SYMBOL_GPL(usb_calc_bus_time); |
| 1099 |
|
| 1100 |
|
| 1101 |
/*-------------------------------------------------------------------------*/ |
| 1102 |
|
| 1103 |
/* |
| 1104 |
* Generic HC operations. |
| 1105 |
*/ |
| 1106 |
|
| 1107 |
/*-------------------------------------------------------------------------*/ |
| 1108 |
|
| 1109 |
/** |
| 1110 |
* usb_hcd_link_urb_to_ep - add an URB to its endpoint queue |
| 1111 |
* @hcd: host controller to which @urb was submitted |
| 1112 |
* @urb: URB being submitted |
| 1113 |
* |
| 1114 |
* Host controller drivers should call this routine in their enqueue() |
| 1115 |
* method. The HCD's private spinlock must be held and interrupts must |
| 1116 |
* be disabled. The actions carried out here are required for URB |
| 1117 |
* submission, as well as for endpoint shutdown and for usb_kill_urb. |
| 1118 |
* |
| 1119 |
* Return: 0 for no error, otherwise a negative error code (in which case |
| 1120 |
* the enqueue() method must fail). If no error occurs but enqueue() fails |
| 1121 |
* anyway, it must call usb_hcd_unlink_urb_from_ep() before releasing |
| 1122 |
* the private spinlock and returning. |
| 1123 |
*/ |
| 1124 |
int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb) |
| 1125 |
{ |
| 1126 |
int rc = 0; |
| 1127 |
|
| 1128 |
spin_lock(&hcd_urb_list_lock); |
| 1129 |
|
| 1130 |
/* Check that the URB isn't being killed */ |
| 1131 |
if (unlikely(atomic_read(&urb->reject))) { |
| 1132 |
rc = -EPERM; |
| 1133 |
goto done; |
| 1134 |
} |
| 1135 |
|
| 1136 |
if (unlikely(!urb->ep->enabled)) { |
| 1137 |
rc = -ENOENT; |
| 1138 |
goto done; |
| 1139 |
} |
| 1140 |
|
| 1141 |
if (unlikely(!urb->dev->can_submit)) { |
| 1142 |
rc = -EHOSTUNREACH; |
| 1143 |
goto done; |
| 1144 |
} |
| 1145 |
|
| 1146 |
/* |
| 1147 |
* Check the host controller's state and add the URB to the |
| 1148 |
* endpoint's queue. |
| 1149 |
*/ |
| 1150 |
if (HCD_RH_RUNNING(hcd)) { |
| 1151 |
urb->unlinked = 0; |
| 1152 |
list_add_tail(&urb->urb_list, &urb->ep->urb_list); |
| 1153 |
} else { |
| 1154 |
rc = -ESHUTDOWN; |
| 1155 |
goto done; |
| 1156 |
} |
| 1157 |
done: |
| 1158 |
spin_unlock(&hcd_urb_list_lock); |
| 1159 |
return rc; |
| 1160 |
} |
| 1161 |
EXPORT_SYMBOL_GPL(usb_hcd_link_urb_to_ep); |
| 1162 |
|
| 1163 |
/** |
| 1164 |
* usb_hcd_check_unlink_urb - check whether an URB may be unlinked |
| 1165 |
* @hcd: host controller to which @urb was submitted |
| 1166 |
* @urb: URB being checked for unlinkability |
| 1167 |
* @status: error code to store in @urb if the unlink succeeds |
| 1168 |
* |
| 1169 |
* Host controller drivers should call this routine in their dequeue() |
| 1170 |
* method. The HCD's private spinlock must be held and interrupts must |
| 1171 |
* be disabled. The actions carried out here are required for making |
| 1172 |
* sure than an unlink is valid. |
| 1173 |
* |
| 1174 |
* Return: 0 for no error, otherwise a negative error code (in which case |
| 1175 |
* the dequeue() method must fail). The possible error codes are: |
| 1176 |
* |
| 1177 |
* -EIDRM: @urb was not submitted or has already completed. |
| 1178 |
* The completion function may not have been called yet. |
| 1179 |
* |
| 1180 |
* -EBUSY: @urb has already been unlinked. |
| 1181 |
*/ |
| 1182 |
int usb_hcd_check_unlink_urb(struct usb_hcd *hcd, struct urb *urb, |
| 1183 |
int status) |
| 1184 |
{ |
| 1185 |
struct list_head *tmp; |
| 1186 |
|
| 1187 |
/* insist the urb is still queued */ |
| 1188 |
list_for_each(tmp, &urb->ep->urb_list) { |
| 1189 |
if (tmp == &urb->urb_list) |
| 1190 |
break; |
| 1191 |
} |
| 1192 |
if (tmp != &urb->urb_list) |
| 1193 |
return -EIDRM; |
| 1194 |
|
| 1195 |
/* Any status except -EINPROGRESS means something already started to |
| 1196 |
* unlink this URB from the hardware. So there's no more work to do. |
| 1197 |
*/ |
| 1198 |
if (urb->unlinked) |
| 1199 |
return -EBUSY; |
| 1200 |
urb->unlinked = status; |
| 1201 |
return 0; |
| 1202 |
} |
| 1203 |
EXPORT_SYMBOL_GPL(usb_hcd_check_unlink_urb); |
| 1204 |
|
| 1205 |
/** |
| 1206 |
* usb_hcd_unlink_urb_from_ep - remove an URB from its endpoint queue |
| 1207 |
* @hcd: host controller to which @urb was submitted |
| 1208 |
* @urb: URB being unlinked |
| 1209 |
* |
| 1210 |
* Host controller drivers should call this routine before calling |
| 1211 |
* usb_hcd_giveback_urb(). The HCD's private spinlock must be held and |
| 1212 |
* interrupts must be disabled. The actions carried out here are required |
| 1213 |
* for URB completion. |
| 1214 |
*/ |
| 1215 |
void usb_hcd_unlink_urb_from_ep(struct usb_hcd *hcd, struct urb *urb) |
| 1216 |
{ |
| 1217 |
/* clear all state linking urb to this dev (and hcd) */ |
| 1218 |
spin_lock(&hcd_urb_list_lock); |
| 1219 |
list_del_init(&urb->urb_list); |
| 1220 |
spin_unlock(&hcd_urb_list_lock); |
| 1221 |
} |
| 1222 |
EXPORT_SYMBOL_GPL(usb_hcd_unlink_urb_from_ep); |
| 1223 |
|
| 1224 |
/* |
| 1225 |
* Some usb host controllers can only perform dma using a small SRAM area, |
| 1226 |
* or have restrictions on addressable DRAM. |
| 1227 |
* The usb core itself is however optimized for host controllers that can dma |
| 1228 |
* using regular system memory - like pci devices doing bus mastering. |
| 1229 |
* |
| 1230 |
* To support host controllers with limited dma capabilities we provide dma |
| 1231 |
* bounce buffers. This feature can be enabled by initializing |
| 1232 |
* hcd->localmem_pool using usb_hcd_setup_local_mem(). |
| 1233 |
* |
| 1234 |
* The initialized hcd->localmem_pool then tells the usb code to allocate all |
| 1235 |
* data for dma using the genalloc API. |
| 1236 |
* |
| 1237 |
* So, to summarize... |
| 1238 |
* |
| 1239 |
* - We need "local" memory, canonical example being |
| 1240 |
* a small SRAM on a discrete controller being the |
| 1241 |
* only memory that the controller can read ... |
| 1242 |
* (a) "normal" kernel memory is no good, and |
| 1243 |
* (b) there's not enough to share |
| 1244 |
* |
| 1245 |
* - So we use that, even though the primary requirement |
| 1246 |
* is that the memory be "local" (hence addressable |
| 1247 |
* by that device), not "coherent". |
| 1248 |
* |
| 1249 |
*/ |
| 1250 |
|
| 1251 |
static int hcd_alloc_coherent(struct usb_bus *bus, |
| 1252 |
gfp_t mem_flags, dma_addr_t *dma_handle, |
| 1253 |
void **vaddr_handle, size_t size, |
| 1254 |
enum dma_data_direction dir) |
| 1255 |
{ |
| 1256 |
unsigned char *vaddr; |
| 1257 |
|
| 1258 |
if (*vaddr_handle == NULL) { |
| 1259 |
WARN_ON_ONCE(1); |
| 1260 |
return -EFAULT; |
| 1261 |
} |
| 1262 |
|
| 1263 |
vaddr = hcd_buffer_alloc(bus, size + sizeof(unsigned long), |
| 1264 |
mem_flags, dma_handle); |
| 1265 |
if (!vaddr) |
| 1266 |
return -ENOMEM; |
| 1267 |
|
| 1268 |
/* |
| 1269 |
* Store the virtual address of the buffer at the end |
| 1270 |
* of the allocated dma buffer. The size of the buffer |
| 1271 |
* may be uneven so use unaligned functions instead |
| 1272 |
* of just rounding up. It makes sense to optimize for |
| 1273 |
* memory footprint over access speed since the amount |
| 1274 |
* of memory available for dma may be limited. |
| 1275 |
*/ |
| 1276 |
put_unaligned((unsigned long)*vaddr_handle, |
| 1277 |
(unsigned long *)(vaddr + size)); |
| 1278 |
|
| 1279 |
if (dir == DMA_TO_DEVICE) |
| 1280 |
memcpy(vaddr, *vaddr_handle, size); |
| 1281 |
|
| 1282 |
*vaddr_handle = vaddr; |
| 1283 |
return 0; |
| 1284 |
} |
| 1285 |
|
| 1286 |
static void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle, |
| 1287 |
void **vaddr_handle, size_t size, |
| 1288 |
enum dma_data_direction dir) |
| 1289 |
{ |
| 1290 |
unsigned char *vaddr = *vaddr_handle; |
| 1291 |
|
| 1292 |
vaddr = (void *)get_unaligned((unsigned long *)(vaddr + size)); |
| 1293 |
|
| 1294 |
if (dir == DMA_FROM_DEVICE) |
| 1295 |
memcpy(vaddr, *vaddr_handle, size); |
| 1296 |
|
| 1297 |
hcd_buffer_free(bus, size + sizeof(vaddr), *vaddr_handle, *dma_handle); |
| 1298 |
|
| 1299 |
*vaddr_handle = vaddr; |
| 1300 |
*dma_handle = 0; |
| 1301 |
} |
| 1302 |
|
| 1303 |
void usb_hcd_unmap_urb_setup_for_dma(struct usb_hcd *hcd, struct urb *urb) |
| 1304 |
{ |
| 1305 |
if (IS_ENABLED(CONFIG_HAS_DMA) && |
| 1306 |
(urb->transfer_flags & URB_SETUP_MAP_SINGLE)) |
| 1307 |
dma_unmap_single(hcd->self.sysdev, |
| 1308 |
urb->setup_dma, |
| 1309 |
sizeof(struct usb_ctrlrequest), |
| 1310 |
DMA_TO_DEVICE); |
| 1311 |
else if (urb->transfer_flags & URB_SETUP_MAP_LOCAL) |
| 1312 |
hcd_free_coherent(urb->dev->bus, |
| 1313 |
&urb->setup_dma, |
| 1314 |
(void **) &urb->setup_packet, |
| 1315 |
sizeof(struct usb_ctrlrequest), |
| 1316 |
DMA_TO_DEVICE); |
| 1317 |
|
| 1318 |
/* Make it safe to call this routine more than once */ |
| 1319 |
urb->transfer_flags &= ~(URB_SETUP_MAP_SINGLE | URB_SETUP_MAP_LOCAL); |
| 1320 |
} |
| 1321 |
EXPORT_SYMBOL_GPL(usb_hcd_unmap_urb_setup_for_dma); |
| 1322 |
|
| 1323 |
static void unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb) |
| 1324 |
{ |
| 1325 |
if (hcd->driver->unmap_urb_for_dma) |
| 1326 |
hcd->driver->unmap_urb_for_dma(hcd, urb); |
| 1327 |
else |
| 1328 |
usb_hcd_unmap_urb_for_dma(hcd, urb); |
| 1329 |
} |
| 1330 |
|
| 1331 |
void usb_hcd_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb) |
| 1332 |
{ |
| 1333 |
enum dma_data_direction dir; |
| 1334 |
|
| 1335 |
usb_hcd_unmap_urb_setup_for_dma(hcd, urb); |
| 1336 |
|
| 1337 |
dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; |
| 1338 |
if (IS_ENABLED(CONFIG_HAS_DMA) && |
| 1339 |
(urb->transfer_flags & URB_DMA_MAP_SG)) { |
| 1340 |
dma_unmap_sg(hcd->self.sysdev, |
| 1341 |
urb->sg, |
| 1342 |
urb->num_sgs, |
| 1343 |
dir); |
| 1344 |
} else if (IS_ENABLED(CONFIG_HAS_DMA) && |
| 1345 |
(urb->transfer_flags & URB_DMA_MAP_PAGE)) { |
| 1346 |
dma_unmap_page(hcd->self.sysdev, |
| 1347 |
urb->transfer_dma, |
| 1348 |
urb->transfer_buffer_length, |
| 1349 |
dir); |
| 1350 |
} else if (IS_ENABLED(CONFIG_HAS_DMA) && |
| 1351 |
(urb->transfer_flags & URB_DMA_MAP_SINGLE)) { |
| 1352 |
dma_unmap_single(hcd->self.sysdev, |
| 1353 |
urb->transfer_dma, |
| 1354 |
urb->transfer_buffer_length, |
| 1355 |
dir); |
| 1356 |
} else if (urb->transfer_flags & URB_MAP_LOCAL) { |
| 1357 |
hcd_free_coherent(urb->dev->bus, |
| 1358 |
&urb->transfer_dma, |
| 1359 |
&urb->transfer_buffer, |
| 1360 |
urb->transfer_buffer_length, |
| 1361 |
dir); |
| 1362 |
} else if ((urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) && urb->sgt) { |
| 1363 |
dma_sync_sgtable_for_cpu(hcd->self.sysdev, urb->sgt, dir); |
| 1364 |
if (dir == DMA_FROM_DEVICE) |
| 1365 |
invalidate_kernel_vmap_range(urb->transfer_buffer, |
| 1366 |
urb->transfer_buffer_length); |
| 1367 |
} |
| 1368 |
|
| 1369 |
/* Make it safe to call this routine more than once */ |
| 1370 |
urb->transfer_flags &= ~(URB_DMA_MAP_SG | URB_DMA_MAP_PAGE | |
| 1371 |
URB_DMA_MAP_SINGLE | URB_MAP_LOCAL); |
| 1372 |
} |
| 1373 |
EXPORT_SYMBOL_GPL(usb_hcd_unmap_urb_for_dma); |
| 1374 |
|
| 1375 |
static int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, |
| 1376 |
gfp_t mem_flags) |
| 1377 |
{ |
| 1378 |
if (hcd->driver->map_urb_for_dma) |
| 1379 |
return hcd->driver->map_urb_for_dma(hcd, urb, mem_flags); |
| 1380 |
else |
| 1381 |
return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags); |
| 1382 |
} |
| 1383 |
|
| 1384 |
int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, |
| 1385 |
gfp_t mem_flags) |
| 1386 |
{ |
| 1387 |
enum dma_data_direction dir; |
| 1388 |
int ret = 0; |
| 1389 |
|
| 1390 |
/* Map the URB's buffers for DMA access. |
| 1391 |
* Lower level HCD code should use *_dma exclusively, |
| 1392 |
* unless it uses pio or talks to another transport, |
| 1393 |
* or uses the provided scatter gather list for bulk. |
| 1394 |
*/ |
| 1395 |
|
| 1396 |
if (usb_endpoint_xfer_control(&urb->ep->desc)) { |
| 1397 |
if (hcd->self.uses_pio_for_control) |
| 1398 |
return ret; |
| 1399 |
if (hcd->localmem_pool) { |
| 1400 |
ret = hcd_alloc_coherent( |
| 1401 |
urb->dev->bus, mem_flags, |
| 1402 |
&urb->setup_dma, |
| 1403 |
(void **)&urb->setup_packet, |
| 1404 |
sizeof(struct usb_ctrlrequest), |
| 1405 |
DMA_TO_DEVICE); |
| 1406 |
if (ret) |
| 1407 |
return ret; |
| 1408 |
urb->transfer_flags |= URB_SETUP_MAP_LOCAL; |
| 1409 |
} else if (hcd_uses_dma(hcd)) { |
| 1410 |
if (object_is_on_stack(urb->setup_packet)) { |
| 1411 |
WARN_ONCE(1, "setup packet is on stack\n"); |
| 1412 |
return -EAGAIN; |
| 1413 |
} |
| 1414 |
|
| 1415 |
urb->setup_dma = dma_map_single( |
| 1416 |
hcd->self.sysdev, |
| 1417 |
urb->setup_packet, |
| 1418 |
sizeof(struct usb_ctrlrequest), |
| 1419 |
DMA_TO_DEVICE); |
| 1420 |
if (dma_mapping_error(hcd->self.sysdev, |
| 1421 |
urb->setup_dma)) |
| 1422 |
return -EAGAIN; |
| 1423 |
urb->transfer_flags |= URB_SETUP_MAP_SINGLE; |
| 1424 |
} |
| 1425 |
} |
| 1426 |
|
| 1427 |
dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; |
| 1428 |
if (urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) { |
| 1429 |
if (!urb->sgt) |
| 1430 |
return 0; |
| 1431 |
|
| 1432 |
if (dir == DMA_TO_DEVICE) |
| 1433 |
flush_kernel_vmap_range(urb->transfer_buffer, |
| 1434 |
urb->transfer_buffer_length); |
| 1435 |
dma_sync_sgtable_for_device(hcd->self.sysdev, urb->sgt, dir); |
| 1436 |
} else if (urb->transfer_buffer_length != 0) { |
| 1437 |
if (hcd->localmem_pool) { |
| 1438 |
ret = hcd_alloc_coherent( |
| 1439 |
urb->dev->bus, mem_flags, |
| 1440 |
&urb->transfer_dma, |
| 1441 |
&urb->transfer_buffer, |
| 1442 |
urb->transfer_buffer_length, |
| 1443 |
dir); |
| 1444 |
if (ret == 0) |
| 1445 |
urb->transfer_flags |= URB_MAP_LOCAL; |
| 1446 |
} else if (hcd_uses_dma(hcd)) { |
| 1447 |
if (urb->num_sgs) { |
| 1448 |
int n; |
| 1449 |
|
| 1450 |
/* We don't support sg for isoc transfers ! */ |
| 1451 |
if (usb_endpoint_xfer_isoc(&urb->ep->desc)) { |
| 1452 |
WARN_ON(1); |
| 1453 |
return -EINVAL; |
| 1454 |
} |
| 1455 |
|
| 1456 |
n = dma_map_sg( |
| 1457 |
hcd->self.sysdev, |
| 1458 |
urb->sg, |
| 1459 |
urb->num_sgs, |
| 1460 |
dir); |
| 1461 |
if (!n) |
| 1462 |
ret = -EAGAIN; |
| 1463 |
else |
| 1464 |
urb->transfer_flags |= URB_DMA_MAP_SG; |
| 1465 |
urb->num_mapped_sgs = n; |
| 1466 |
if (n != urb->num_sgs) |
| 1467 |
urb->transfer_flags |= |
| 1468 |
URB_DMA_SG_COMBINED; |
| 1469 |
} else if (urb->sg) { |
| 1470 |
struct scatterlist *sg = urb->sg; |
| 1471 |
urb->transfer_dma = dma_map_page( |
| 1472 |
hcd->self.sysdev, |
| 1473 |
sg_page(sg), |
| 1474 |
sg->offset, |
| 1475 |
urb->transfer_buffer_length, |
| 1476 |
dir); |
| 1477 |
if (dma_mapping_error(hcd->self.sysdev, |
| 1478 |
urb->transfer_dma)) |
| 1479 |
ret = -EAGAIN; |
| 1480 |
else |
| 1481 |
urb->transfer_flags |= URB_DMA_MAP_PAGE; |
| 1482 |
} else if (object_is_on_stack(urb->transfer_buffer)) { |
| 1483 |
WARN_ONCE(1, "transfer buffer is on stack\n"); |
| 1484 |
ret = -EAGAIN; |
| 1485 |
} else { |
| 1486 |
urb->transfer_dma = dma_map_single( |
| 1487 |
hcd->self.sysdev, |
| 1488 |
urb->transfer_buffer, |
| 1489 |
urb->transfer_buffer_length, |
| 1490 |
dir); |
| 1491 |
if (dma_mapping_error(hcd->self.sysdev, |
| 1492 |
urb->transfer_dma)) |
| 1493 |
ret = -EAGAIN; |
| 1494 |
else |
| 1495 |
urb->transfer_flags |= URB_DMA_MAP_SINGLE; |
| 1496 |
} |
| 1497 |
} |
| 1498 |
if (ret && (urb->transfer_flags & (URB_SETUP_MAP_SINGLE | |
| 1499 |
URB_SETUP_MAP_LOCAL))) |
| 1500 |
usb_hcd_unmap_urb_for_dma(hcd, urb); |
| 1501 |
} |
| 1502 |
return ret; |
| 1503 |
} |
| 1504 |
EXPORT_SYMBOL_GPL(usb_hcd_map_urb_for_dma); |
| 1505 |
|
| 1506 |
/*-------------------------------------------------------------------------*/ |
| 1507 |
|
| 1508 |
/* may be called in any context with a valid urb->dev usecount |
| 1509 |
* caller surrenders "ownership" of urb |
| 1510 |
* expects usb_submit_urb() to have sanity checked and conditioned all |
| 1511 |
* inputs in the urb |
| 1512 |
*/ |
| 1513 |
int usb_hcd_submit_urb (struct urb *urb, gfp_t mem_flags) |
| 1514 |
{ |
| 1515 |
int status; |
| 1516 |
struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus); |
| 1517 |
|
| 1518 |
/* increment urb's reference count as part of giving it to the HCD |
| 1519 |
* (which will control it). HCD guarantees that it either returns |
| 1520 |
* an error or calls giveback(), but not both. |
| 1521 |
*/ |
| 1522 |
usb_get_urb(urb); |
| 1523 |
atomic_inc(&urb->use_count); |
| 1524 |
atomic_inc(&urb->dev->urbnum); |
| 1525 |
usbmon_urb_submit(&hcd->self, urb); |
| 1526 |
|
| 1527 |
/* NOTE requirements on root-hub callers (usbfs and the hub |
| 1528 |
* driver, for now): URBs' urb->transfer_buffer must be |
| 1529 |
* valid and usb_buffer_{sync,unmap}() not be needed, since |
| 1530 |
* they could clobber root hub response data. Also, control |
| 1531 |
* URBs must be submitted in process context with interrupts |
| 1532 |
* enabled. |
| 1533 |
*/ |
| 1534 |
|
| 1535 |
if (is_root_hub(urb->dev)) { |
| 1536 |
status = rh_urb_enqueue(hcd, urb); |
| 1537 |
} else { |
| 1538 |
status = map_urb_for_dma(hcd, urb, mem_flags); |
| 1539 |
if (likely(status == 0)) { |
| 1540 |
status = hcd->driver->urb_enqueue(hcd, urb, mem_flags); |
| 1541 |
if (unlikely(status)) |
| 1542 |
unmap_urb_for_dma(hcd, urb); |
| 1543 |
} |
| 1544 |
} |
| 1545 |
|
| 1546 |
if (unlikely(status)) { |
| 1547 |
usbmon_urb_submit_error(&hcd->self, urb, status); |
| 1548 |
urb->hcpriv = NULL; |
| 1549 |
INIT_LIST_HEAD(&urb->urb_list); |
| 1550 |
atomic_dec(&urb->use_count); |
| 1551 |
/* |
| 1552 |
* Order the write of urb->use_count above before the read |
| 1553 |
* of urb->reject below. Pairs with the memory barriers in |
| 1554 |
* usb_kill_urb() and usb_poison_urb(). |
| 1555 |
*/ |
| 1556 |
smp_mb__after_atomic(); |
| 1557 |
|
| 1558 |
atomic_dec(&urb->dev->urbnum); |
| 1559 |
if (atomic_read(&urb->reject)) |
| 1560 |
wake_up(&usb_kill_urb_queue); |
| 1561 |
usb_put_urb(urb); |
| 1562 |
} |
| 1563 |
return status; |
| 1564 |
} |
| 1565 |
|
| 1566 |
/*-------------------------------------------------------------------------*/ |
| 1567 |
|
| 1568 |
/* this makes the hcd giveback() the urb more quickly, by kicking it |
| 1569 |
* off hardware queues (which may take a while) and returning it as |
| 1570 |
* soon as practical. we've already set up the urb's return status, |
| 1571 |
* but we can't know if the callback completed already. |
| 1572 |
*/ |
| 1573 |
static int unlink1(struct usb_hcd *hcd, struct urb *urb, int status) |
| 1574 |
{ |
| 1575 |
int value; |
| 1576 |
|
| 1577 |
if (is_root_hub(urb->dev)) |
| 1578 |
value = usb_rh_urb_dequeue(hcd, urb, status); |
| 1579 |
else { |
| 1580 |
|
| 1581 |
/* The only reason an HCD might fail this call is if |
| 1582 |
* it has not yet fully queued the urb to begin with. |
| 1583 |
* Such failures should be harmless. */ |
| 1584 |
value = hcd->driver->urb_dequeue(hcd, urb, status); |
| 1585 |
} |
| 1586 |
return value; |
| 1587 |
} |
| 1588 |
|
| 1589 |
/* |
| 1590 |
* called in any context |
| 1591 |
* |
| 1592 |
* caller guarantees urb won't be recycled till both unlink() |
| 1593 |
* and the urb's completion function return |
| 1594 |
*/ |
| 1595 |
int usb_hcd_unlink_urb (struct urb *urb, int status) |
| 1596 |
{ |
| 1597 |
struct usb_hcd *hcd; |
| 1598 |
struct usb_device *udev = urb->dev; |
| 1599 |
int retval = -EIDRM; |
| 1600 |
unsigned long flags; |
| 1601 |
|
| 1602 |
/* Prevent the device and bus from going away while |
| 1603 |
* the unlink is carried out. If they are already gone |
| 1604 |
* then urb->use_count must be 0, since disconnected |
| 1605 |
* devices can't have any active URBs. |
| 1606 |
*/ |
| 1607 |
spin_lock_irqsave(&hcd_urb_unlink_lock, flags); |
| 1608 |
if (atomic_read(&urb->use_count) > 0) { |
| 1609 |
retval = 0; |
| 1610 |
usb_get_dev(udev); |
| 1611 |
} |
| 1612 |
spin_unlock_irqrestore(&hcd_urb_unlink_lock, flags); |
| 1613 |
if (retval == 0) { |
| 1614 |
hcd = bus_to_hcd(urb->dev->bus); |
| 1615 |
retval = unlink1(hcd, urb, status); |
| 1616 |
if (retval == 0) |
| 1617 |
retval = -EINPROGRESS; |
| 1618 |
else if (retval != -EIDRM && retval != -EBUSY) |
| 1619 |
dev_dbg(&udev->dev, "hcd_unlink_urb %p fail %d\n", |
| 1620 |
urb, retval); |
| 1621 |
usb_put_dev(udev); |
| 1622 |
} |
| 1623 |
return retval; |
| 1624 |
} |
| 1625 |
|
| 1626 |
/*-------------------------------------------------------------------------*/ |
| 1627 |
|
| 1628 |
static void __usb_hcd_giveback_urb(struct urb *urb) |
| 1629 |
{ |
| 1630 |
struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus); |
| 1631 |
struct usb_anchor *anchor = urb->anchor; |
| 1632 |
int status = urb->unlinked; |
| 1633 |
|
| 1634 |
urb->hcpriv = NULL; |
| 1635 |
if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) && |
| 1636 |
urb->actual_length < urb->transfer_buffer_length && |
| 1637 |
!status)) |
| 1638 |
status = -EREMOTEIO; |
| 1639 |
|
| 1640 |
unmap_urb_for_dma(hcd, urb); |
| 1641 |
usbmon_urb_complete(&hcd->self, urb, status); |
| 1642 |
usb_anchor_suspend_wakeups(anchor); |
| 1643 |
usb_unanchor_urb(urb); |
| 1644 |
if (likely(status == 0)) |
| 1645 |
usb_led_activity(USB_LED_EVENT_HOST); |
| 1646 |
|
| 1647 |
/* pass ownership to the completion handler */ |
| 1648 |
urb->status = status; |
| 1649 |
/* |
| 1650 |
* This function can be called in task context inside another remote |
| 1651 |
* coverage collection section, but kcov doesn't support that kind of |
| 1652 |
* recursion yet. Only collect coverage in softirq context for now. |
| 1653 |
*/ |
| 1654 |
kcov_remote_start_usb_softirq((u64)urb->dev->bus->busnum); |
| 1655 |
urb->complete(urb); |
| 1656 |
kcov_remote_stop_softirq(); |
| 1657 |
|
| 1658 |
usb_anchor_resume_wakeups(anchor); |
| 1659 |
atomic_dec(&urb->use_count); |
| 1660 |
/* |
| 1661 |
* Order the write of urb->use_count above before the read |
| 1662 |
* of urb->reject below. Pairs with the memory barriers in |
| 1663 |
* usb_kill_urb() and usb_poison_urb(). |
| 1664 |
*/ |
| 1665 |
smp_mb__after_atomic(); |
| 1666 |
|
| 1667 |
if (unlikely(atomic_read(&urb->reject))) |
| 1668 |
wake_up(&usb_kill_urb_queue); |
| 1669 |
usb_put_urb(urb); |
| 1670 |
} |
| 1671 |
|
| 1672 |
static void usb_giveback_urb_bh(struct work_struct *work) |
| 1673 |
{ |
| 1674 |
struct giveback_urb_bh *bh = |
| 1675 |
container_of(work, struct giveback_urb_bh, bh); |
| 1676 |
struct list_head local_list; |
| 1677 |
|
| 1678 |
spin_lock_irq(&bh->lock); |
| 1679 |
bh->running = true; |
| 1680 |
list_replace_init(&bh->head, &local_list); |
| 1681 |
spin_unlock_irq(&bh->lock); |
| 1682 |
|
| 1683 |
while (!list_empty(&local_list)) { |
| 1684 |
struct urb *urb; |
| 1685 |
|
| 1686 |
urb = list_entry(local_list.next, struct urb, urb_list); |
| 1687 |
list_del_init(&urb->urb_list); |
| 1688 |
bh->completing_ep = urb->ep; |
| 1689 |
__usb_hcd_giveback_urb(urb); |
| 1690 |
bh->completing_ep = NULL; |
| 1691 |
} |
| 1692 |
|
| 1693 |
/* |
| 1694 |
* giveback new URBs next time to prevent this function |
| 1695 |
* from not exiting for a long time. |
| 1696 |
*/ |
| 1697 |
spin_lock_irq(&bh->lock); |
| 1698 |
if (!list_empty(&bh->head)) { |
| 1699 |
if (bh->high_prio) |
| 1700 |
queue_work(system_bh_highpri_wq, &bh->bh); |
| 1701 |
else |
| 1702 |
queue_work(system_bh_wq, &bh->bh); |
| 1703 |
} |
| 1704 |
bh->running = false; |
| 1705 |
spin_unlock_irq(&bh->lock); |
| 1706 |
} |
| 1707 |
|
| 1708 |
/** |
| 1709 |
* usb_hcd_giveback_urb - return URB from HCD to device driver |
| 1710 |
* @hcd: host controller returning the URB |
| 1711 |
* @urb: urb being returned to the USB device driver. |
| 1712 |
* @status: completion status code for the URB. |
| 1713 |
* |
| 1714 |
* Context: atomic. The completion callback is invoked either in a work queue |
| 1715 |
* (BH) context or in the caller's context, depending on whether the HCD_BH |
| 1716 |
* flag is set in the @hcd structure, except that URBs submitted to the |
| 1717 |
* root hub always complete in BH context. |
| 1718 |
* |
| 1719 |
* This hands the URB from HCD to its USB device driver, using its |
| 1720 |
* completion function. The HCD has freed all per-urb resources |
| 1721 |
* (and is done using urb->hcpriv). It also released all HCD locks; |
| 1722 |
* the device driver won't cause problems if it frees, modifies, |
| 1723 |
* or resubmits this URB. |
| 1724 |
* |
| 1725 |
* If @urb was unlinked, the value of @status will be overridden by |
| 1726 |
* @urb->unlinked. Erroneous short transfers are detected in case |
| 1727 |
* the HCD hasn't checked for them. |
| 1728 |
*/ |
| 1729 |
void usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb, int status) |
| 1730 |
{ |
| 1731 |
struct giveback_urb_bh *bh; |
| 1732 |
bool running; |
| 1733 |
|
| 1734 |
/* pass status to BH via unlinked */ |
| 1735 |
if (likely(!urb->unlinked)) |
| 1736 |
urb->unlinked = status; |
| 1737 |
|
| 1738 |
if (!hcd_giveback_urb_in_bh(hcd) && !is_root_hub(urb->dev)) { |
| 1739 |
__usb_hcd_giveback_urb(urb); |
| 1740 |
return; |
| 1741 |
} |
| 1742 |
|
| 1743 |
if (usb_pipeisoc(urb->pipe) || usb_pipeint(urb->pipe)) |
| 1744 |
bh = &hcd->high_prio_bh; |
| 1745 |
else |
| 1746 |
bh = &hcd->low_prio_bh; |
| 1747 |
|
| 1748 |
spin_lock(&bh->lock); |
| 1749 |
list_add_tail(&urb->urb_list, &bh->head); |
| 1750 |
running = bh->running; |
| 1751 |
spin_unlock(&bh->lock); |
| 1752 |
|
| 1753 |
if (running) |
| 1754 |
; |
| 1755 |
else if (bh->high_prio) |
| 1756 |
queue_work(system_bh_highpri_wq, &bh->bh); |
| 1757 |
else |
| 1758 |
queue_work(system_bh_wq, &bh->bh); |
| 1759 |
} |
| 1760 |
EXPORT_SYMBOL_GPL(usb_hcd_giveback_urb); |
| 1761 |
|
| 1762 |
/*-------------------------------------------------------------------------*/ |
| 1763 |
|
| 1764 |
/* Cancel all URBs pending on this endpoint and wait for the endpoint's |
| 1765 |
* queue to drain completely. The caller must first insure that no more |
| 1766 |
* URBs can be submitted for this endpoint. |
| 1767 |
*/ |
| 1768 |
void usb_hcd_flush_endpoint(struct usb_device *udev, |
| 1769 |
struct usb_host_endpoint *ep) |
| 1770 |
{ |
| 1771 |
struct usb_hcd *hcd; |
| 1772 |
struct urb *urb; |
| 1773 |
|
| 1774 |
if (!ep) |
| 1775 |
return; |
| 1776 |
might_sleep(); |
| 1777 |
hcd = bus_to_hcd(udev->bus); |
| 1778 |
|
| 1779 |
/* No more submits can occur */ |
| 1780 |
spin_lock_irq(&hcd_urb_list_lock); |
| 1781 |
rescan: |
| 1782 |
list_for_each_entry_reverse(urb, &ep->urb_list, urb_list) { |
| 1783 |
int is_in; |
| 1784 |
|
| 1785 |
if (urb->unlinked) |
| 1786 |
continue; |
| 1787 |
usb_get_urb (urb); |
| 1788 |
is_in = usb_urb_dir_in(urb); |
| 1789 |
spin_unlock(&hcd_urb_list_lock); |
| 1790 |
|
| 1791 |
/* kick hcd */ |
| 1792 |
unlink1(hcd, urb, -ESHUTDOWN); |
| 1793 |
dev_dbg (hcd->self.controller, |
| 1794 |
"shutdown urb %p ep%d%s-%s\n", |
| 1795 |
urb, usb_endpoint_num(&ep->desc), |
| 1796 |
is_in ? "in" : "out", |
| 1797 |
usb_ep_type_string(usb_endpoint_type(&ep->desc))); |
| 1798 |
usb_put_urb (urb); |
| 1799 |
|
| 1800 |
/* list contents may have changed */ |
| 1801 |
spin_lock(&hcd_urb_list_lock); |
| 1802 |
goto rescan; |
| 1803 |
} |
| 1804 |
spin_unlock_irq(&hcd_urb_list_lock); |
| 1805 |
|
| 1806 |
/* Wait until the endpoint queue is completely empty */ |
| 1807 |
while (!list_empty (&ep->urb_list)) { |
| 1808 |
spin_lock_irq(&hcd_urb_list_lock); |
| 1809 |
|
| 1810 |
/* The list may have changed while we acquired the spinlock */ |
| 1811 |
urb = NULL; |
| 1812 |
if (!list_empty (&ep->urb_list)) { |
| 1813 |
urb = list_entry (ep->urb_list.prev, struct urb, |
| 1814 |
urb_list); |
| 1815 |
usb_get_urb (urb); |
| 1816 |
} |
| 1817 |
spin_unlock_irq(&hcd_urb_list_lock); |
| 1818 |
|
| 1819 |
if (urb) { |
| 1820 |
usb_kill_urb (urb); |
| 1821 |
usb_put_urb (urb); |
| 1822 |
} |
| 1823 |
} |
| 1824 |
} |
| 1825 |
|
| 1826 |
/** |
| 1827 |
* usb_hcd_alloc_bandwidth - check whether a new bandwidth setting exceeds |
| 1828 |
* the bus bandwidth |
| 1829 |
* @udev: target &usb_device |
| 1830 |
* @new_config: new configuration to install |
| 1831 |
* @cur_alt: the current alternate interface setting |
| 1832 |
* @new_alt: alternate interface setting that is being installed |
| 1833 |
* |
| 1834 |
* To change configurations, pass in the new configuration in new_config, |
| 1835 |
* and pass NULL for cur_alt and new_alt. |
| 1836 |
* |
| 1837 |
* To reset a device's configuration (put the device in the ADDRESSED state), |
| 1838 |
* pass in NULL for new_config, cur_alt, and new_alt. |
| 1839 |
* |
| 1840 |
* To change alternate interface settings, pass in NULL for new_config, |
| 1841 |
* pass in the current alternate interface setting in cur_alt, |
| 1842 |
* and pass in the new alternate interface setting in new_alt. |
| 1843 |
* |
| 1844 |
* Return: An error if the requested bandwidth change exceeds the |
| 1845 |
* bus bandwidth or host controller internal resources. |
| 1846 |
*/ |
| 1847 |
int usb_hcd_alloc_bandwidth(struct usb_device *udev, |
| 1848 |
struct usb_host_config *new_config, |
| 1849 |
struct usb_host_interface *cur_alt, |
| 1850 |
struct usb_host_interface *new_alt) |
| 1851 |
{ |
| 1852 |
int num_intfs, i, j; |
| 1853 |
struct usb_host_interface *alt = NULL; |
| 1854 |
int ret = 0; |
| 1855 |
struct usb_hcd *hcd; |
| 1856 |
struct usb_host_endpoint *ep; |
| 1857 |
|
| 1858 |
hcd = bus_to_hcd(udev->bus); |
| 1859 |
if (!hcd->driver->check_bandwidth) |
| 1860 |
return 0; |
| 1861 |
|
| 1862 |
/* Configuration is being removed - set configuration 0 */ |
| 1863 |
if (!new_config && !cur_alt) { |
| 1864 |
for (i = 1; i < 16; ++i) { |
| 1865 |
ep = udev->ep_out[i]; |
| 1866 |
if (ep) |
| 1867 |
hcd->driver->drop_endpoint(hcd, udev, ep); |
| 1868 |
ep = udev->ep_in[i]; |
| 1869 |
if (ep) |
| 1870 |
hcd->driver->drop_endpoint(hcd, udev, ep); |
| 1871 |
} |
| 1872 |
hcd->driver->check_bandwidth(hcd, udev); |
| 1873 |
return 0; |
| 1874 |
} |
| 1875 |
/* Check if the HCD says there's enough bandwidth. Enable all endpoints |
| 1876 |
* each interface's alt setting 0 and ask the HCD to check the bandwidth |
| 1877 |
* of the bus. There will always be bandwidth for endpoint 0, so it's |
| 1878 |
* ok to exclude it. |
| 1879 |
*/ |
| 1880 |
if (new_config) { |
| 1881 |
num_intfs = new_config->desc.bNumInterfaces; |
| 1882 |
/* Remove endpoints (except endpoint 0, which is always on the |
| 1883 |
* schedule) from the old config from the schedule |
| 1884 |
*/ |
| 1885 |
for (i = 1; i < 16; ++i) { |
| 1886 |
ep = udev->ep_out[i]; |
| 1887 |
if (ep) { |
| 1888 |
ret = hcd->driver->drop_endpoint(hcd, udev, ep); |
| 1889 |
if (ret < 0) |
| 1890 |
goto reset; |
| 1891 |
} |
| 1892 |
ep = udev->ep_in[i]; |
| 1893 |
if (ep) { |
| 1894 |
ret = hcd->driver->drop_endpoint(hcd, udev, ep); |
| 1895 |
if (ret < 0) |
| 1896 |
goto reset; |
| 1897 |
} |
| 1898 |
} |
| 1899 |
for (i = 0; i < num_intfs; ++i) { |
| 1900 |
struct usb_host_interface *first_alt; |
| 1901 |
int iface_num; |
| 1902 |
|
| 1903 |
first_alt = &new_config->intf_cache[i]->altsetting[0]; |
| 1904 |
iface_num = first_alt->desc.bInterfaceNumber; |
| 1905 |
/* Set up endpoints for alternate interface setting 0 */ |
| 1906 |
alt = usb_find_alt_setting(new_config, iface_num, 0); |
| 1907 |
if (!alt) |
| 1908 |
/* No alt setting 0? Pick the first setting. */ |
| 1909 |
alt = first_alt; |
| 1910 |
|
| 1911 |
for (j = 0; j < alt->desc.bNumEndpoints; j++) { |
| 1912 |
ret = hcd->driver->add_endpoint(hcd, udev, &alt->endpoint[j]); |
| 1913 |
if (ret < 0) |
| 1914 |
goto reset; |
| 1915 |
} |
| 1916 |
} |
| 1917 |
} |
| 1918 |
if (cur_alt && new_alt) { |
| 1919 |
struct usb_interface *iface = usb_ifnum_to_if(udev, |
| 1920 |
cur_alt->desc.bInterfaceNumber); |
| 1921 |
|
| 1922 |
if (!iface) |
| 1923 |
return -EINVAL; |
| 1924 |
if (iface->resetting_device) { |
| 1925 |
/* |
| 1926 |
* The USB core just reset the device, so the xHCI host |
| 1927 |
* and the device will think alt setting 0 is installed. |
| 1928 |
* However, the USB core will pass in the alternate |
| 1929 |
* setting installed before the reset as cur_alt. Dig |
| 1930 |
* out the alternate setting 0 structure, or the first |
| 1931 |
* alternate setting if a broken device doesn't have alt |
| 1932 |
* setting 0. |
| 1933 |
*/ |
| 1934 |
cur_alt = usb_altnum_to_altsetting(iface, 0); |
| 1935 |
if (!cur_alt) |
| 1936 |
cur_alt = &iface->altsetting[0]; |
| 1937 |
} |
| 1938 |
|
| 1939 |
/* Drop all the endpoints in the current alt setting */ |
| 1940 |
for (i = 0; i < cur_alt->desc.bNumEndpoints; i++) { |
| 1941 |
ret = hcd->driver->drop_endpoint(hcd, udev, |
| 1942 |
&cur_alt->endpoint[i]); |
| 1943 |
if (ret < 0) |
| 1944 |
goto reset; |
| 1945 |
} |
| 1946 |
/* Add all the endpoints in the new alt setting */ |
| 1947 |
for (i = 0; i < new_alt->desc.bNumEndpoints; i++) { |
| 1948 |
ret = hcd->driver->add_endpoint(hcd, udev, |
| 1949 |
&new_alt->endpoint[i]); |
| 1950 |
if (ret < 0) |
| 1951 |
goto reset; |
| 1952 |
} |
| 1953 |
} |
| 1954 |
ret = hcd->driver->check_bandwidth(hcd, udev); |
| 1955 |
reset: |
| 1956 |
if (ret < 0) |
| 1957 |
hcd->driver->reset_bandwidth(hcd, udev); |
| 1958 |
return ret; |
| 1959 |
} |
| 1960 |
|
| 1961 |
/* Disables the endpoint: synchronizes with the hcd to make sure all |
| 1962 |
* endpoint state is gone from hardware. usb_hcd_flush_endpoint() must |
| 1963 |
* have been called previously. Use for set_configuration, set_interface, |
| 1964 |
* driver removal, physical disconnect. |
| 1965 |
* |
| 1966 |
* example: a qh stored in ep->hcpriv, holding state related to endpoint |
| 1967 |
* type, maxpacket size, toggle, halt status, and scheduling. |
| 1968 |
*/ |
| 1969 |
void usb_hcd_disable_endpoint(struct usb_device *udev, |
| 1970 |
struct usb_host_endpoint *ep) |
| 1971 |
{ |
| 1972 |
struct usb_hcd *hcd; |
| 1973 |
|
| 1974 |
might_sleep(); |
| 1975 |
hcd = bus_to_hcd(udev->bus); |
| 1976 |
if (hcd->driver->endpoint_disable) |
| 1977 |
hcd->driver->endpoint_disable(hcd, ep); |
| 1978 |
} |
| 1979 |
|
| 1980 |
/** |
| 1981 |
* usb_hcd_reset_endpoint - reset host endpoint state |
| 1982 |
* @udev: USB device. |
| 1983 |
* @ep: the endpoint to reset. |
| 1984 |
* |
| 1985 |
* Resets any host endpoint state such as the toggle bit, sequence |
| 1986 |
* number and current window. |
| 1987 |
*/ |
| 1988 |
void usb_hcd_reset_endpoint(struct usb_device *udev, |
| 1989 |
struct usb_host_endpoint *ep) |
| 1990 |
{ |
| 1991 |
struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
| 1992 |
|
| 1993 |
if (hcd->driver->endpoint_reset) |
| 1994 |
hcd->driver->endpoint_reset(hcd, ep); |
| 1995 |
else { |
| 1996 |
int epnum = usb_endpoint_num(&ep->desc); |
| 1997 |
int is_out = usb_endpoint_dir_out(&ep->desc); |
| 1998 |
int is_control = usb_endpoint_xfer_control(&ep->desc); |
| 1999 |
|
| 2000 |
usb_settoggle(udev, epnum, is_out, 0); |
| 2001 |
if (is_control) |
| 2002 |
usb_settoggle(udev, epnum, !is_out, 0); |
| 2003 |
} |
| 2004 |
} |
| 2005 |
|
| 2006 |
/** |
| 2007 |
* usb_alloc_streams - allocate bulk endpoint stream IDs. |
| 2008 |
* @interface: alternate setting that includes all endpoints. |
| 2009 |
* @eps: array of endpoints that need streams. |
| 2010 |
* @num_eps: number of endpoints in the array. |
| 2011 |
* @num_streams: number of streams to allocate. |
| 2012 |
* @mem_flags: flags hcd should use to allocate memory. |
| 2013 |
* |
| 2014 |
* Sets up a group of bulk endpoints to have @num_streams stream IDs available. |
| 2015 |
* Drivers may queue multiple transfers to different stream IDs, which may |
| 2016 |
* complete in a different order than they were queued. |
| 2017 |
* |
| 2018 |
* Return: On success, the number of allocated streams. On failure, a negative |
| 2019 |
* error code. |
| 2020 |
*/ |
| 2021 |
int usb_alloc_streams(struct usb_interface *interface, |
| 2022 |
struct usb_host_endpoint **eps, unsigned int num_eps, |
| 2023 |
unsigned int num_streams, gfp_t mem_flags) |
| 2024 |
{ |
| 2025 |
struct usb_hcd *hcd; |
| 2026 |
struct usb_device *dev; |
| 2027 |
int i, ret; |
| 2028 |
|
| 2029 |
dev = interface_to_usbdev(interface); |
| 2030 |
hcd = bus_to_hcd(dev->bus); |
| 2031 |
if (!hcd->driver->alloc_streams || !hcd->driver->free_streams) |
| 2032 |
return -EINVAL; |
| 2033 |
if (dev->speed < USB_SPEED_SUPER) |
| 2034 |
return -EINVAL; |
| 2035 |
if (dev->state < USB_STATE_CONFIGURED) |
| 2036 |
return -ENODEV; |
| 2037 |
|
| 2038 |
for (i = 0; i < num_eps; i++) { |
| 2039 |
/* Streams only apply to bulk endpoints. */ |
| 2040 |
if (!usb_endpoint_xfer_bulk(&eps[i]->desc)) |
| 2041 |
return -EINVAL; |
| 2042 |
/* Re-alloc is not allowed */ |
| 2043 |
if (eps[i]->streams) |
| 2044 |
return -EINVAL; |
| 2045 |
} |
| 2046 |
|
| 2047 |
ret = hcd->driver->alloc_streams(hcd, dev, eps, num_eps, |
| 2048 |
num_streams, mem_flags); |
| 2049 |
if (ret < 0) |
| 2050 |
return ret; |
| 2051 |
|
| 2052 |
for (i = 0; i < num_eps; i++) |
| 2053 |
eps[i]->streams = ret; |
| 2054 |
|
| 2055 |
return ret; |
| 2056 |
} |
| 2057 |
EXPORT_SYMBOL_GPL(usb_alloc_streams); |
| 2058 |
|
| 2059 |
/** |
| 2060 |
* usb_free_streams - free bulk endpoint stream IDs. |
| 2061 |
* @interface: alternate setting that includes all endpoints. |
| 2062 |
* @eps: array of endpoints to remove streams from. |
| 2063 |
* @num_eps: number of endpoints in the array. |
| 2064 |
* @mem_flags: flags hcd should use to allocate memory. |
| 2065 |
* |
| 2066 |
* Reverts a group of bulk endpoints back to not using stream IDs. |
| 2067 |
* Can fail if we are given bad arguments, or HCD is broken. |
| 2068 |
* |
| 2069 |
* Return: 0 on success. On failure, a negative error code. |
| 2070 |
*/ |
| 2071 |
int usb_free_streams(struct usb_interface *interface, |
| 2072 |
struct usb_host_endpoint **eps, unsigned int num_eps, |
| 2073 |
gfp_t mem_flags) |
| 2074 |
{ |
| 2075 |
struct usb_hcd *hcd; |
| 2076 |
struct usb_device *dev; |
| 2077 |
int i, ret; |
| 2078 |
|
| 2079 |
dev = interface_to_usbdev(interface); |
| 2080 |
hcd = bus_to_hcd(dev->bus); |
| 2081 |
if (dev->speed < USB_SPEED_SUPER) |
| 2082 |
return -EINVAL; |
| 2083 |
|
| 2084 |
/* Double-free is not allowed */ |
| 2085 |
for (i = 0; i < num_eps; i++) |
| 2086 |
if (!eps[i] || !eps[i]->streams) |
| 2087 |
return -EINVAL; |
| 2088 |
|
| 2089 |
ret = hcd->driver->free_streams(hcd, dev, eps, num_eps, mem_flags); |
| 2090 |
if (ret < 0) |
| 2091 |
return ret; |
| 2092 |
|
| 2093 |
for (i = 0; i < num_eps; i++) |
| 2094 |
eps[i]->streams = 0; |
| 2095 |
|
| 2096 |
return ret; |
| 2097 |
} |
| 2098 |
EXPORT_SYMBOL_GPL(usb_free_streams); |
| 2099 |
|
| 2100 |
/* Protect against drivers that try to unlink URBs after the device |
| 2101 |
* is gone, by waiting until all unlinks for @udev are finished. |
| 2102 |
* Since we don't currently track URBs by device, simply wait until |
| 2103 |
* nothing is running in the locked region of usb_hcd_unlink_urb(). |
| 2104 |
*/ |
| 2105 |
void usb_hcd_synchronize_unlinks(struct usb_device *udev) |
| 2106 |
{ |
| 2107 |
spin_lock_irq(&hcd_urb_unlink_lock); |
| 2108 |
spin_unlock_irq(&hcd_urb_unlink_lock); |
| 2109 |
} |
| 2110 |
|
| 2111 |
/*-------------------------------------------------------------------------*/ |
| 2112 |
|
| 2113 |
/* called in any context */ |
| 2114 |
int usb_hcd_get_frame_number (struct usb_device *udev) |
| 2115 |
{ |
| 2116 |
struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
| 2117 |
|
| 2118 |
if (!HCD_RH_RUNNING(hcd)) |
| 2119 |
return -ESHUTDOWN; |
| 2120 |
return hcd->driver->get_frame_number (hcd); |
| 2121 |
} |
| 2122 |
|
| 2123 |
/*-------------------------------------------------------------------------*/ |
| 2124 |
#ifdef CONFIG_USB_HCD_TEST_MODE |
| 2125 |
|
| 2126 |
static void usb_ehset_completion(struct urb *urb) |
| 2127 |
{ |
| 2128 |
struct completion *done = urb->context; |
| 2129 |
|
| 2130 |
complete(done); |
| 2131 |
} |
| 2132 |
/* |
| 2133 |
* Allocate and initialize a control URB. This request will be used by the |
| 2134 |
* EHSET SINGLE_STEP_SET_FEATURE test in which the DATA and STATUS stages |
| 2135 |
* of the GetDescriptor request are sent 15 seconds after the SETUP stage. |
| 2136 |
* Return NULL if failed. |
| 2137 |
*/ |
| 2138 |
static struct urb *request_single_step_set_feature_urb( |
| 2139 |
struct usb_device *udev, |
| 2140 |
void *dr, |
| 2141 |
void *buf, |
| 2142 |
struct completion *done) |
| 2143 |
{ |
| 2144 |
struct urb *urb; |
| 2145 |
struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
| 2146 |
|
| 2147 |
urb = usb_alloc_urb(0, GFP_KERNEL); |
| 2148 |
if (!urb) |
| 2149 |
return NULL; |
| 2150 |
|
| 2151 |
urb->pipe = usb_rcvctrlpipe(udev, 0); |
| 2152 |
|
| 2153 |
urb->ep = &udev->ep0; |
| 2154 |
urb->dev = udev; |
| 2155 |
urb->setup_packet = (void *)dr; |
| 2156 |
urb->transfer_buffer = buf; |
| 2157 |
urb->transfer_buffer_length = USB_DT_DEVICE_SIZE; |
| 2158 |
urb->complete = usb_ehset_completion; |
| 2159 |
urb->status = -EINPROGRESS; |
| 2160 |
urb->actual_length = 0; |
| 2161 |
urb->transfer_flags = URB_DIR_IN | URB_NO_TRANSFER_DMA_MAP; |
| 2162 |
usb_get_urb(urb); |
| 2163 |
atomic_inc(&urb->use_count); |
| 2164 |
atomic_inc(&urb->dev->urbnum); |
| 2165 |
if (map_urb_for_dma(hcd, urb, GFP_KERNEL)) { |
| 2166 |
usb_put_urb(urb); |
| 2167 |
usb_free_urb(urb); |
| 2168 |
return NULL; |
| 2169 |
} |
| 2170 |
|
| 2171 |
urb->context = done; |
| 2172 |
return urb; |
| 2173 |
} |
| 2174 |
|
| 2175 |
int ehset_single_step_set_feature(struct usb_hcd *hcd, int port) |
| 2176 |
{ |
| 2177 |
int retval = -ENOMEM; |
| 2178 |
struct usb_ctrlrequest *dr; |
| 2179 |
struct urb *urb; |
| 2180 |
struct usb_device *udev; |
| 2181 |
struct usb_device_descriptor *buf; |
| 2182 |
DECLARE_COMPLETION_ONSTACK(done); |
| 2183 |
|
| 2184 |
/* Obtain udev of the rhub's child port */ |
| 2185 |
udev = usb_hub_find_child(hcd->self.root_hub, port); |
| 2186 |
if (!udev) { |
| 2187 |
dev_err(hcd->self.controller, "No device attached to the RootHub\n"); |
| 2188 |
return -ENODEV; |
| 2189 |
} |
| 2190 |
buf = kmalloc(USB_DT_DEVICE_SIZE, GFP_KERNEL); |
| 2191 |
if (!buf) |
| 2192 |
return -ENOMEM; |
| 2193 |
|
| 2194 |
dr = kmalloc_obj(struct usb_ctrlrequest); |
| 2195 |
if (!dr) { |
| 2196 |
kfree(buf); |
| 2197 |
return -ENOMEM; |
| 2198 |
} |
| 2199 |
|
| 2200 |
/* Fill Setup packet for GetDescriptor */ |
| 2201 |
dr->bRequestType = USB_DIR_IN; |
| 2202 |
dr->bRequest = USB_REQ_GET_DESCRIPTOR; |
| 2203 |
dr->wValue = cpu_to_le16(USB_DT_DEVICE << 8); |
| 2204 |
dr->wIndex = 0; |
| 2205 |
dr->wLength = cpu_to_le16(USB_DT_DEVICE_SIZE); |
| 2206 |
urb = request_single_step_set_feature_urb(udev, dr, buf, &done); |
| 2207 |
if (!urb) |
| 2208 |
goto cleanup; |
| 2209 |
|
| 2210 |
/* Submit just the SETUP stage */ |
| 2211 |
retval = hcd->driver->submit_single_step_set_feature(hcd, urb, 1); |
| 2212 |
if (retval) |
| 2213 |
goto out1; |
| 2214 |
if (!wait_for_completion_timeout(&done, msecs_to_jiffies(2000))) { |
| 2215 |
usb_kill_urb(urb); |
| 2216 |
retval = -ETIMEDOUT; |
| 2217 |
dev_err(hcd->self.controller, |
| 2218 |
"%s SETUP stage timed out on ep0\n", __func__); |
| 2219 |
goto out1; |
| 2220 |
} |
| 2221 |
msleep(15 * 1000); |
| 2222 |
|
| 2223 |
/* Complete remaining DATA and STATUS stages using the same URB */ |
| 2224 |
urb->status = -EINPROGRESS; |
| 2225 |
urb->transfer_flags &= ~URB_NO_TRANSFER_DMA_MAP; |
| 2226 |
usb_get_urb(urb); |
| 2227 |
atomic_inc(&urb->use_count); |
| 2228 |
atomic_inc(&urb->dev->urbnum); |
| 2229 |
if (map_urb_for_dma(hcd, urb, GFP_KERNEL)) { |
| 2230 |
usb_put_urb(urb); |
| 2231 |
goto out1; |
| 2232 |
} |
| 2233 |
|
| 2234 |
retval = hcd->driver->submit_single_step_set_feature(hcd, urb, 0); |
| 2235 |
if (!retval && !wait_for_completion_timeout(&done, |
| 2236 |
msecs_to_jiffies(2000))) { |
| 2237 |
usb_kill_urb(urb); |
| 2238 |
retval = -ETIMEDOUT; |
| 2239 |
dev_err(hcd->self.controller, |
| 2240 |
"%s IN stage timed out on ep0\n", __func__); |
| 2241 |
} |
| 2242 |
out1: |
| 2243 |
usb_free_urb(urb); |
| 2244 |
cleanup: |
| 2245 |
kfree(dr); |
| 2246 |
kfree(buf); |
| 2247 |
return retval; |
| 2248 |
} |
| 2249 |
EXPORT_SYMBOL_GPL(ehset_single_step_set_feature); |
| 2250 |
#endif /* CONFIG_USB_HCD_TEST_MODE */ |
| 2251 |
|
| 2252 |
/*-------------------------------------------------------------------------*/ |
| 2253 |
|
| 2254 |
#ifdef CONFIG_PM |
| 2255 |
|
| 2256 |
int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg) |
| 2257 |
{ |
| 2258 |
struct usb_hcd *hcd = bus_to_hcd(rhdev->bus); |
| 2259 |
int status; |
| 2260 |
int old_state = hcd->state; |
| 2261 |
|
| 2262 |
dev_dbg(&rhdev->dev, "bus %ssuspend, wakeup %d\n", |
| 2263 |
(PMSG_IS_AUTO(msg) ? "auto-" : ""), |
| 2264 |
rhdev->do_remote_wakeup); |
| 2265 |
if (HCD_DEAD(hcd)) { |
| 2266 |
dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "suspend"); |
| 2267 |
return 0; |
| 2268 |
} |
| 2269 |
|
| 2270 |
if (!hcd->driver->bus_suspend) { |
| 2271 |
status = -ENOENT; |
| 2272 |
} else { |
| 2273 |
clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); |
| 2274 |
hcd->state = HC_STATE_QUIESCING; |
| 2275 |
status = hcd->driver->bus_suspend(hcd); |
| 2276 |
} |
| 2277 |
if (status == 0) { |
| 2278 |
usb_set_device_state(rhdev, USB_STATE_SUSPENDED); |
| 2279 |
hcd->state = HC_STATE_SUSPENDED; |
| 2280 |
|
| 2281 |
if (!PMSG_IS_AUTO(msg)) |
| 2282 |
usb_phy_roothub_suspend(hcd->self.sysdev, |
| 2283 |
hcd->phy_roothub); |
| 2284 |
|
| 2285 |
/* Did we race with a root-hub wakeup event? */ |
| 2286 |
if (rhdev->do_remote_wakeup) { |
| 2287 |
char buffer[6]; |
| 2288 |
|
| 2289 |
status = hcd->driver->hub_status_data(hcd, buffer); |
| 2290 |
if (status != 0) { |
| 2291 |
dev_dbg(&rhdev->dev, "suspend raced with wakeup event\n"); |
| 2292 |
hcd_bus_resume(rhdev, PMSG_AUTO_RESUME); |
| 2293 |
status = -EBUSY; |
| 2294 |
} |
| 2295 |
} |
| 2296 |
} else { |
| 2297 |
spin_lock_irq(&hcd_root_hub_lock); |
| 2298 |
if (!HCD_DEAD(hcd)) { |
| 2299 |
set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); |
| 2300 |
hcd->state = old_state; |
| 2301 |
} |
| 2302 |
spin_unlock_irq(&hcd_root_hub_lock); |
| 2303 |
dev_dbg(&rhdev->dev, "bus %s fail, err %d\n", |
| 2304 |
"suspend", status); |
| 2305 |
} |
| 2306 |
return status; |
| 2307 |
} |
| 2308 |
|
| 2309 |
int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg) |
| 2310 |
{ |
| 2311 |
struct usb_hcd *hcd = bus_to_hcd(rhdev->bus); |
| 2312 |
int status; |
| 2313 |
int old_state = hcd->state; |
| 2314 |
|
| 2315 |
dev_dbg(&rhdev->dev, "usb %sresume\n", |
| 2316 |
(PMSG_IS_AUTO(msg) ? "auto-" : "")); |
| 2317 |
if (HCD_DEAD(hcd)) { |
| 2318 |
dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "resume"); |
| 2319 |
return 0; |
| 2320 |
} |
| 2321 |
|
| 2322 |
if (!PMSG_IS_AUTO(msg)) { |
| 2323 |
status = usb_phy_roothub_resume(hcd->self.sysdev, |
| 2324 |
hcd->phy_roothub); |
| 2325 |
if (status) |
| 2326 |
return status; |
| 2327 |
} |
| 2328 |
|
| 2329 |
if (!hcd->driver->bus_resume) |
| 2330 |
return -ENOENT; |
| 2331 |
if (HCD_RH_RUNNING(hcd)) |
| 2332 |
return 0; |
| 2333 |
|
| 2334 |
hcd->state = HC_STATE_RESUMING; |
| 2335 |
status = hcd->driver->bus_resume(hcd); |
| 2336 |
clear_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags); |
| 2337 |
if (status == 0) |
| 2338 |
status = usb_phy_roothub_calibrate(hcd->phy_roothub); |
| 2339 |
|
| 2340 |
if (status == 0) { |
| 2341 |
struct usb_device *udev; |
| 2342 |
int port1; |
| 2343 |
|
| 2344 |
spin_lock_irq(&hcd_root_hub_lock); |
| 2345 |
if (!HCD_DEAD(hcd)) { |
| 2346 |
usb_set_device_state(rhdev, rhdev->actconfig |
| 2347 |
? USB_STATE_CONFIGURED |
| 2348 |
: USB_STATE_ADDRESS); |
| 2349 |
set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); |
| 2350 |
hcd->state = HC_STATE_RUNNING; |
| 2351 |
} |
| 2352 |
spin_unlock_irq(&hcd_root_hub_lock); |
| 2353 |
|
| 2354 |
/* |
| 2355 |
* Check whether any of the enabled ports on the root hub are |
| 2356 |
* unsuspended. If they are then a TRSMRCY delay is needed |
| 2357 |
* (this is what the USB-2 spec calls a "global resume"). |
| 2358 |
* Otherwise we can skip the delay. |
| 2359 |
*/ |
| 2360 |
usb_hub_for_each_child(rhdev, port1, udev) { |
| 2361 |
if (udev->state != USB_STATE_NOTATTACHED && |
| 2362 |
!udev->port_is_suspended) { |
| 2363 |
usleep_range(10000, 11000); /* TRSMRCY */ |
| 2364 |
break; |
| 2365 |
} |
| 2366 |
} |
| 2367 |
} else { |
| 2368 |
hcd->state = old_state; |
| 2369 |
usb_phy_roothub_suspend(hcd->self.sysdev, hcd->phy_roothub); |
| 2370 |
dev_dbg(&rhdev->dev, "bus %s fail, err %d\n", |
| 2371 |
"resume", status); |
| 2372 |
if (status != -ESHUTDOWN) |
| 2373 |
usb_hc_died(hcd); |
| 2374 |
} |
| 2375 |
return status; |
| 2376 |
} |
| 2377 |
|
| 2378 |
/* Workqueue routine for root-hub remote wakeup */ |
| 2379 |
static void hcd_resume_work(struct work_struct *work) |
| 2380 |
{ |
| 2381 |
struct usb_hcd *hcd = container_of(work, struct usb_hcd, wakeup_work); |
| 2382 |
struct usb_device *udev = hcd->self.root_hub; |
| 2383 |
|
| 2384 |
usb_remote_wakeup(udev); |
| 2385 |
} |
| 2386 |
|
| 2387 |
/** |
| 2388 |
* usb_hcd_resume_root_hub - called by HCD to resume its root hub |
| 2389 |
* @hcd: host controller for this root hub |
| 2390 |
* |
| 2391 |
* The USB host controller calls this function when its root hub is |
| 2392 |
* suspended (with the remote wakeup feature enabled) and a remote |
| 2393 |
* wakeup request is received. The routine submits a workqueue request |
| 2394 |
* to resume the root hub (that is, manage its downstream ports again). |
| 2395 |
*/ |
| 2396 |
void usb_hcd_resume_root_hub (struct usb_hcd *hcd) |
| 2397 |
{ |
| 2398 |
unsigned long flags; |
| 2399 |
|
| 2400 |
spin_lock_irqsave (&hcd_root_hub_lock, flags); |
| 2401 |
if (hcd->rh_registered) { |
| 2402 |
pm_wakeup_event(&hcd->self.root_hub->dev, 0); |
| 2403 |
set_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags); |
| 2404 |
queue_work(system_freezable_wq, &hcd->wakeup_work); |
| 2405 |
} |
| 2406 |
spin_unlock_irqrestore (&hcd_root_hub_lock, flags); |
| 2407 |
} |
| 2408 |
EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub); |
| 2409 |
|
| 2410 |
#endif /* CONFIG_PM */ |
| 2411 |
|
| 2412 |
/*-------------------------------------------------------------------------*/ |
| 2413 |
|
| 2414 |
#ifdef CONFIG_USB_OTG |
| 2415 |
|
| 2416 |
/** |
| 2417 |
* usb_bus_start_enum - start immediate enumeration (for OTG) |
| 2418 |
* @bus: the bus (must use hcd framework) |
| 2419 |
* @port_num: 1-based number of port; usually bus->otg_port |
| 2420 |
* Context: atomic |
| 2421 |
* |
| 2422 |
* Starts enumeration, with an immediate reset followed later by |
| 2423 |
* hub_wq identifying and possibly configuring the device. |
| 2424 |
* This is needed by OTG controller drivers, where it helps meet |
| 2425 |
* HNP protocol timing requirements for starting a port reset. |
| 2426 |
* |
| 2427 |
* Return: 0 if successful. |
| 2428 |
*/ |
| 2429 |
int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num) |
| 2430 |
{ |
| 2431 |
struct usb_hcd *hcd; |
| 2432 |
int status = -EOPNOTSUPP; |
| 2433 |
|
| 2434 |
/* NOTE: since HNP can't start by grabbing the bus's address0_sem, |
| 2435 |
* boards with root hubs hooked up to internal devices (instead of |
| 2436 |
* just the OTG port) may need more attention to resetting... |
| 2437 |
*/ |
| 2438 |
hcd = bus_to_hcd(bus); |
| 2439 |
if (port_num && hcd->driver->start_port_reset) |
| 2440 |
status = hcd->driver->start_port_reset(hcd, port_num); |
| 2441 |
|
| 2442 |
/* allocate hub_wq shortly after (first) root port reset finishes; |
| 2443 |
* it may issue others, until at least 50 msecs have passed. |
| 2444 |
*/ |
| 2445 |
if (status == 0) |
| 2446 |
mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10)); |
| 2447 |
return status; |
| 2448 |
} |
| 2449 |
EXPORT_SYMBOL_GPL(usb_bus_start_enum); |
| 2450 |
|
| 2451 |
#endif |
| 2452 |
|
| 2453 |
/*-------------------------------------------------------------------------*/ |
| 2454 |
|
| 2455 |
/** |
| 2456 |
* usb_hcd_irq - hook IRQs to HCD framework (bus glue) |
| 2457 |
* @irq: the IRQ being raised |
| 2458 |
* @__hcd: pointer to the HCD whose IRQ is being signaled |
| 2459 |
* |
| 2460 |
* If the controller isn't HALTed, calls the driver's irq handler. |
| 2461 |
* Checks whether the controller is now dead. |
| 2462 |
* |
| 2463 |
* Return: %IRQ_HANDLED if the IRQ was handled. %IRQ_NONE otherwise. |
| 2464 |
*/ |
| 2465 |
irqreturn_t usb_hcd_irq (int irq, void *__hcd) |
| 2466 |
{ |
| 2467 |
struct usb_hcd *hcd = __hcd; |
| 2468 |
irqreturn_t rc; |
| 2469 |
|
| 2470 |
if (unlikely(HCD_DEAD(hcd) || !HCD_HW_ACCESSIBLE(hcd))) |
| 2471 |
rc = IRQ_NONE; |
| 2472 |
else if (hcd->driver->irq(hcd) == IRQ_NONE) |
| 2473 |
rc = IRQ_NONE; |
| 2474 |
else |
| 2475 |
rc = IRQ_HANDLED; |
| 2476 |
|
| 2477 |
return rc; |
| 2478 |
} |
| 2479 |
EXPORT_SYMBOL_GPL(usb_hcd_irq); |
| 2480 |
|
| 2481 |
/*-------------------------------------------------------------------------*/ |
| 2482 |
|
| 2483 |
/* Workqueue routine for when the root-hub has died. */ |
| 2484 |
static void hcd_died_work(struct work_struct *work) |
| 2485 |
{ |
| 2486 |
struct usb_hcd *hcd = container_of(work, struct usb_hcd, died_work); |
| 2487 |
static char *env[] = { |
| 2488 |
"ERROR=DEAD", |
| 2489 |
NULL |
| 2490 |
}; |
| 2491 |
|
| 2492 |
/* Notify user space that the host controller has died */ |
| 2493 |
kobject_uevent_env(&hcd->self.root_hub->dev.kobj, KOBJ_OFFLINE, env); |
| 2494 |
} |
| 2495 |
|
| 2496 |
/** |
| 2497 |
* usb_hc_died - report abnormal shutdown of a host controller (bus glue) |
| 2498 |
* @hcd: pointer to the HCD representing the controller |
| 2499 |
* |
| 2500 |
* This is called by bus glue to report a USB host controller that died |
| 2501 |
* while operations may still have been pending. It's called automatically |
| 2502 |
* by the PCI glue, so only glue for non-PCI busses should need to call it. |
| 2503 |
* |
| 2504 |
* Only call this function with the primary HCD. |
| 2505 |
*/ |
| 2506 |
void usb_hc_died (struct usb_hcd *hcd) |
| 2507 |
{ |
| 2508 |
unsigned long flags; |
| 2509 |
|
| 2510 |
dev_err (hcd->self.controller, "HC died; cleaning up\n"); |
| 2511 |
|
| 2512 |
spin_lock_irqsave (&hcd_root_hub_lock, flags); |
| 2513 |
clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); |
| 2514 |
set_bit(HCD_FLAG_DEAD, &hcd->flags); |
| 2515 |
if (hcd->rh_registered) { |
| 2516 |
clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); |
| 2517 |
|
| 2518 |
/* make hub_wq clean up old urbs and devices */ |
| 2519 |
usb_set_device_state (hcd->self.root_hub, |
| 2520 |
USB_STATE_NOTATTACHED); |
| 2521 |
usb_kick_hub_wq(hcd->self.root_hub); |
| 2522 |
} |
| 2523 |
if (usb_hcd_is_primary_hcd(hcd) && hcd->shared_hcd) { |
| 2524 |
hcd = hcd->shared_hcd; |
| 2525 |
clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); |
| 2526 |
set_bit(HCD_FLAG_DEAD, &hcd->flags); |
| 2527 |
if (hcd->rh_registered) { |
| 2528 |
clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); |
| 2529 |
|
| 2530 |
/* make hub_wq clean up old urbs and devices */ |
| 2531 |
usb_set_device_state(hcd->self.root_hub, |
| 2532 |
USB_STATE_NOTATTACHED); |
| 2533 |
usb_kick_hub_wq(hcd->self.root_hub); |
| 2534 |
} |
| 2535 |
} |
| 2536 |
|
| 2537 |
/* Handle the case where this function gets called with a shared HCD */ |
| 2538 |
if (usb_hcd_is_primary_hcd(hcd)) |
| 2539 |
schedule_work(&hcd->died_work); |
| 2540 |
else |
| 2541 |
schedule_work(&hcd->primary_hcd->died_work); |
| 2542 |
|
| 2543 |
spin_unlock_irqrestore (&hcd_root_hub_lock, flags); |
| 2544 |
/* Make sure that the other roothub is also deallocated. */ |
| 2545 |
} |
| 2546 |
EXPORT_SYMBOL_GPL (usb_hc_died); |
| 2547 |
|
| 2548 |
/*-------------------------------------------------------------------------*/ |
| 2549 |
|
| 2550 |
static void init_giveback_urb_bh(struct giveback_urb_bh *bh) |
| 2551 |
{ |
| 2552 |
|
| 2553 |
spin_lock_init(&bh->lock); |
| 2554 |
INIT_LIST_HEAD(&bh->head); |
| 2555 |
INIT_WORK(&bh->bh, usb_giveback_urb_bh); |
| 2556 |
} |
| 2557 |
|
| 2558 |
struct usb_hcd *__usb_create_hcd(const struct hc_driver *driver, |
| 2559 |
struct device *sysdev, struct device *dev, const char *bus_name, |
| 2560 |
struct usb_hcd *primary_hcd) |
| 2561 |
{ |
| 2562 |
struct usb_hcd *hcd; |
| 2563 |
|
| 2564 |
hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL); |
| 2565 |
if (!hcd) |
| 2566 |
return NULL; |
| 2567 |
if (primary_hcd == NULL) { |
| 2568 |
hcd->address0_mutex = kmalloc_obj(*hcd->address0_mutex); |
| 2569 |
if (!hcd->address0_mutex) { |
| 2570 |
kfree(hcd); |
| 2571 |
dev_dbg(dev, "hcd address0 mutex alloc failed\n"); |
| 2572 |
return NULL; |
| 2573 |
} |
| 2574 |
mutex_init(hcd->address0_mutex); |
| 2575 |
hcd->bandwidth_mutex = kmalloc_obj(*hcd->bandwidth_mutex); |
| 2576 |
if (!hcd->bandwidth_mutex) { |
| 2577 |
kfree(hcd->address0_mutex); |
| 2578 |
kfree(hcd); |
| 2579 |
dev_dbg(dev, "hcd bandwidth mutex alloc failed\n"); |
| 2580 |
return NULL; |
| 2581 |
} |
| 2582 |
mutex_init(hcd->bandwidth_mutex); |
| 2583 |
dev_set_drvdata(dev, hcd); |
| 2584 |
} else { |
| 2585 |
mutex_lock(&usb_port_peer_mutex); |
| 2586 |
hcd->address0_mutex = primary_hcd->address0_mutex; |
| 2587 |
hcd->bandwidth_mutex = primary_hcd->bandwidth_mutex; |
| 2588 |
hcd->primary_hcd = primary_hcd; |
| 2589 |
primary_hcd->primary_hcd = primary_hcd; |
| 2590 |
hcd->shared_hcd = primary_hcd; |
| 2591 |
primary_hcd->shared_hcd = hcd; |
| 2592 |
mutex_unlock(&usb_port_peer_mutex); |
| 2593 |
} |
| 2594 |
|
| 2595 |
kref_init(&hcd->kref); |
| 2596 |
|
| 2597 |
usb_bus_init(&hcd->self); |
| 2598 |
hcd->self.controller = dev; |
| 2599 |
hcd->self.sysdev = sysdev; |
| 2600 |
hcd->self.bus_name = bus_name; |
| 2601 |
|
| 2602 |
timer_setup(&hcd->rh_timer, rh_timer_func, 0); |
| 2603 |
#ifdef CONFIG_PM |
| 2604 |
INIT_WORK(&hcd->wakeup_work, hcd_resume_work); |
| 2605 |
#endif |
| 2606 |
|
| 2607 |
INIT_WORK(&hcd->died_work, hcd_died_work); |
| 2608 |
|
| 2609 |
hcd->driver = driver; |
| 2610 |
hcd->speed = driver->flags & HCD_MASK; |
| 2611 |
hcd->product_desc = (driver->product_desc) ? driver->product_desc : |
| 2612 |
"USB Host Controller"; |
| 2613 |
return hcd; |
| 2614 |
} |
| 2615 |
EXPORT_SYMBOL_GPL(__usb_create_hcd); |
| 2616 |
|
| 2617 |
/** |
| 2618 |
* usb_create_shared_hcd - create and initialize an HCD structure |
| 2619 |
* @driver: HC driver that will use this hcd |
| 2620 |
* @dev: device for this HC, stored in hcd->self.controller |
| 2621 |
* @bus_name: value to store in hcd->self.bus_name |
| 2622 |
* @primary_hcd: a pointer to the usb_hcd structure that is sharing the |
| 2623 |
* PCI device. Only allocate certain resources for the primary HCD |
| 2624 |
* |
| 2625 |
* Context: task context, might sleep. |
| 2626 |
* |
| 2627 |
* Allocate a struct usb_hcd, with extra space at the end for the |
| 2628 |
* HC driver's private data. Initialize the generic members of the |
| 2629 |
* hcd structure. |
| 2630 |
* |
| 2631 |
* Return: On success, a pointer to the created and initialized HCD structure. |
| 2632 |
* On failure (e.g. if memory is unavailable), %NULL. |
| 2633 |
*/ |
| 2634 |
struct usb_hcd *usb_create_shared_hcd(const struct hc_driver *driver, |
| 2635 |
struct device *dev, const char *bus_name, |
| 2636 |
struct usb_hcd *primary_hcd) |
| 2637 |
{ |
| 2638 |
return __usb_create_hcd(driver, dev, dev, bus_name, primary_hcd); |
| 2639 |
} |
| 2640 |
EXPORT_SYMBOL_GPL(usb_create_shared_hcd); |
| 2641 |
|
| 2642 |
/** |
| 2643 |
* usb_create_hcd - create and initialize an HCD structure |
| 2644 |
* @driver: HC driver that will use this hcd |
| 2645 |
* @dev: device for this HC, stored in hcd->self.controller |
| 2646 |
* @bus_name: value to store in hcd->self.bus_name |
| 2647 |
* |
| 2648 |
* Context: task context, might sleep. |
| 2649 |
* |
| 2650 |
* Allocate a struct usb_hcd, with extra space at the end for the |
| 2651 |
* HC driver's private data. Initialize the generic members of the |
| 2652 |
* hcd structure. |
| 2653 |
* |
| 2654 |
* Return: On success, a pointer to the created and initialized HCD |
| 2655 |
* structure. On failure (e.g. if memory is unavailable), %NULL. |
| 2656 |
*/ |
| 2657 |
struct usb_hcd *usb_create_hcd(const struct hc_driver *driver, |
| 2658 |
struct device *dev, const char *bus_name) |
| 2659 |
{ |
| 2660 |
return __usb_create_hcd(driver, dev, dev, bus_name, NULL); |
| 2661 |
} |
| 2662 |
EXPORT_SYMBOL_GPL(usb_create_hcd); |
| 2663 |
|
| 2664 |
/* |
| 2665 |
* Roothubs that share one PCI device must also share the bandwidth mutex. |
| 2666 |
* Don't deallocate the bandwidth_mutex until the last shared usb_hcd is |
| 2667 |
* deallocated. |
| 2668 |
* |
| 2669 |
* Make sure to deallocate the bandwidth_mutex only when the last HCD is |
| 2670 |
* freed. When hcd_release() is called for either hcd in a peer set, |
| 2671 |
* invalidate the peer's ->shared_hcd and ->primary_hcd pointers. |
| 2672 |
*/ |
| 2673 |
static void hcd_release(struct kref *kref) |
| 2674 |
{ |
| 2675 |
struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref); |
| 2676 |
|
| 2677 |
mutex_lock(&usb_port_peer_mutex); |
| 2678 |
if (hcd->shared_hcd) { |
| 2679 |
struct usb_hcd *peer = hcd->shared_hcd; |
| 2680 |
|
| 2681 |
peer->shared_hcd = NULL; |
| 2682 |
peer->primary_hcd = NULL; |
| 2683 |
} else { |
| 2684 |
kfree(hcd->address0_mutex); |
| 2685 |
kfree(hcd->bandwidth_mutex); |
| 2686 |
} |
| 2687 |
mutex_unlock(&usb_port_peer_mutex); |
| 2688 |
kfree(hcd); |
| 2689 |
} |
| 2690 |
|
| 2691 |
struct usb_hcd *usb_get_hcd(struct usb_hcd *hcd) |
| 2692 |
{ |
| 2693 |
if (hcd) |
| 2694 |
kref_get(&hcd->kref); |
| 2695 |
return hcd; |
| 2696 |
} |
| 2697 |
EXPORT_SYMBOL_GPL(usb_get_hcd); |
| 2698 |
|
| 2699 |
void usb_put_hcd(struct usb_hcd *hcd) |
| 2700 |
{ |
| 2701 |
if (hcd) |
| 2702 |
kref_put(&hcd->kref, hcd_release); |
| 2703 |
} |
| 2704 |
EXPORT_SYMBOL_GPL(usb_put_hcd); |
| 2705 |
|
| 2706 |
int usb_hcd_is_primary_hcd(struct usb_hcd *hcd) |
| 2707 |
{ |
| 2708 |
if (!hcd->primary_hcd) |
| 2709 |
return 1; |
| 2710 |
return hcd == hcd->primary_hcd; |
| 2711 |
} |
| 2712 |
EXPORT_SYMBOL_GPL(usb_hcd_is_primary_hcd); |
| 2713 |
|
| 2714 |
int usb_hcd_find_raw_port_number(struct usb_hcd *hcd, int port1) |
| 2715 |
{ |
| 2716 |
if (!hcd->driver->find_raw_port_number) |
| 2717 |
return port1; |
| 2718 |
|
| 2719 |
return hcd->driver->find_raw_port_number(hcd, port1); |
| 2720 |
} |
| 2721 |
|
| 2722 |
static int usb_hcd_request_irqs(struct usb_hcd *hcd, |
| 2723 |
unsigned int irqnum, unsigned long irqflags) |
| 2724 |
{ |
| 2725 |
int retval; |
| 2726 |
|
| 2727 |
if (hcd->driver->irq) { |
| 2728 |
|
| 2729 |
snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d", |
| 2730 |
hcd->driver->description, hcd->self.busnum); |
| 2731 |
retval = request_irq(irqnum, &usb_hcd_irq, irqflags, |
| 2732 |
hcd->irq_descr, hcd); |
| 2733 |
if (retval != 0) { |
| 2734 |
dev_err(hcd->self.controller, |
| 2735 |
"request interrupt %d failed\n", |
| 2736 |
irqnum); |
| 2737 |
return retval; |
| 2738 |
} |
| 2739 |
hcd->irq = irqnum; |
| 2740 |
dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum, |
| 2741 |
(hcd->driver->flags & HCD_MEMORY) ? |
| 2742 |
"io mem" : "io port", |
| 2743 |
(unsigned long long)hcd->rsrc_start); |
| 2744 |
} else { |
| 2745 |
hcd->irq = 0; |
| 2746 |
if (hcd->rsrc_start) |
| 2747 |
dev_info(hcd->self.controller, "%s 0x%08llx\n", |
| 2748 |
(hcd->driver->flags & HCD_MEMORY) ? |
| 2749 |
"io mem" : "io port", |
| 2750 |
(unsigned long long)hcd->rsrc_start); |
| 2751 |
} |
| 2752 |
return 0; |
| 2753 |
} |
| 2754 |
|
| 2755 |
/* |
| 2756 |
* Before we free this root hub, flush in-flight peering attempts |
| 2757 |
* and disable peer lookups |
| 2758 |
*/ |
| 2759 |
static void usb_put_invalidate_rhdev(struct usb_hcd *hcd) |
| 2760 |
{ |
| 2761 |
struct usb_device *rhdev; |
| 2762 |
|
| 2763 |
mutex_lock(&usb_port_peer_mutex); |
| 2764 |
rhdev = hcd->self.root_hub; |
| 2765 |
hcd->self.root_hub = NULL; |
| 2766 |
mutex_unlock(&usb_port_peer_mutex); |
| 2767 |
usb_put_dev(rhdev); |
| 2768 |
} |
| 2769 |
|
| 2770 |
/** |
| 2771 |
* usb_stop_hcd - Halt the HCD |
| 2772 |
* @hcd: the usb_hcd that has to be halted |
| 2773 |
* |
| 2774 |
* Stop the root-hub polling timer and invoke the HCD's ->stop callback. |
| 2775 |
*/ |
| 2776 |
static void usb_stop_hcd(struct usb_hcd *hcd) |
| 2777 |
{ |
| 2778 |
hcd->rh_pollable = 0; |
| 2779 |
clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); |
| 2780 |
timer_delete_sync(&hcd->rh_timer); |
| 2781 |
|
| 2782 |
hcd->driver->stop(hcd); |
| 2783 |
hcd->state = HC_STATE_HALT; |
| 2784 |
|
| 2785 |
/* In case the HCD restarted the timer, stop it again. */ |
| 2786 |
clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); |
| 2787 |
timer_delete_sync(&hcd->rh_timer); |
| 2788 |
} |
| 2789 |
|
| 2790 |
/** |
| 2791 |
* usb_add_hcd - finish generic HCD structure initialization and register |
| 2792 |
* @hcd: the usb_hcd structure to initialize |
| 2793 |
* @irqnum: Interrupt line to allocate |
| 2794 |
* @irqflags: Interrupt type flags |
| 2795 |
* |
| 2796 |
* Finish the remaining parts of generic HCD initialization: allocate the |
| 2797 |
* buffers of consistent memory, register the bus, request the IRQ line, |
| 2798 |
* and call the driver's reset() and start() routines. |
| 2799 |
*/ |
| 2800 |
int usb_add_hcd(struct usb_hcd *hcd, |
| 2801 |
unsigned int irqnum, unsigned long irqflags) |
| 2802 |
{ |
| 2803 |
int retval; |
| 2804 |
struct usb_device *rhdev; |
| 2805 |
struct usb_hcd *shared_hcd; |
| 2806 |
int skip_phy_initialization; |
| 2807 |
|
| 2808 |
if (usb_hcd_is_primary_hcd(hcd)) |
| 2809 |
skip_phy_initialization = hcd->skip_phy_initialization; |
| 2810 |
else |
| 2811 |
skip_phy_initialization = hcd->primary_hcd->skip_phy_initialization; |
| 2812 |
|
| 2813 |
if (!skip_phy_initialization) { |
| 2814 |
if (usb_hcd_is_primary_hcd(hcd)) { |
| 2815 |
hcd->phy_roothub = usb_phy_roothub_alloc(hcd->self.sysdev); |
| 2816 |
if (IS_ERR(hcd->phy_roothub)) |
| 2817 |
return PTR_ERR(hcd->phy_roothub); |
| 2818 |
} else { |
| 2819 |
hcd->phy_roothub = usb_phy_roothub_alloc_usb3_phy(hcd->self.sysdev); |
| 2820 |
if (IS_ERR(hcd->phy_roothub)) |
| 2821 |
return PTR_ERR(hcd->phy_roothub); |
| 2822 |
} |
| 2823 |
|
| 2824 |
retval = usb_phy_roothub_init(hcd->phy_roothub); |
| 2825 |
if (retval) |
| 2826 |
return retval; |
| 2827 |
|
| 2828 |
retval = usb_phy_roothub_set_mode(hcd->phy_roothub, |
| 2829 |
PHY_MODE_USB_HOST_SS); |
| 2830 |
if (retval) |
| 2831 |
retval = usb_phy_roothub_set_mode(hcd->phy_roothub, |
| 2832 |
PHY_MODE_USB_HOST); |
| 2833 |
if (retval) |
| 2834 |
goto err_usb_phy_roothub_power_on; |
| 2835 |
|
| 2836 |
retval = usb_phy_roothub_power_on(hcd->phy_roothub); |
| 2837 |
if (retval) |
| 2838 |
goto err_usb_phy_roothub_power_on; |
| 2839 |
} |
| 2840 |
|
| 2841 |
dev_info(hcd->self.controller, "%s\n", hcd->product_desc); |
| 2842 |
|
| 2843 |
switch (authorized_default) { |
| 2844 |
case USB_AUTHORIZE_NONE: |
| 2845 |
hcd->dev_policy = USB_DEVICE_AUTHORIZE_NONE; |
| 2846 |
break; |
| 2847 |
|
| 2848 |
case USB_AUTHORIZE_INTERNAL: |
| 2849 |
hcd->dev_policy = USB_DEVICE_AUTHORIZE_INTERNAL; |
| 2850 |
break; |
| 2851 |
|
| 2852 |
case USB_AUTHORIZE_ALL: |
| 2853 |
case USB_AUTHORIZE_WIRED: |
| 2854 |
default: |
| 2855 |
hcd->dev_policy = USB_DEVICE_AUTHORIZE_ALL; |
| 2856 |
break; |
| 2857 |
} |
| 2858 |
|
| 2859 |
set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); |
| 2860 |
|
| 2861 |
/* per default all interfaces are authorized */ |
| 2862 |
set_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags); |
| 2863 |
|
| 2864 |
/* HC is in reset state, but accessible. Now do the one-time init, |
| 2865 |
* bottom up so that hcds can customize the root hubs before hub_wq |
| 2866 |
* starts talking to them. (Note, bus id is assigned early too.) |
| 2867 |
*/ |
| 2868 |
retval = hcd_buffer_create(hcd); |
| 2869 |
if (retval != 0) { |
| 2870 |
dev_dbg(hcd->self.sysdev, "pool alloc failed\n"); |
| 2871 |
goto err_create_buf; |
| 2872 |
} |
| 2873 |
|
| 2874 |
retval = usb_register_bus(&hcd->self); |
| 2875 |
if (retval < 0) |
| 2876 |
goto err_register_bus; |
| 2877 |
|
| 2878 |
rhdev = usb_alloc_dev(NULL, &hcd->self, 0); |
| 2879 |
if (rhdev == NULL) { |
| 2880 |
dev_err(hcd->self.sysdev, "unable to allocate root hub\n"); |
| 2881 |
retval = -ENOMEM; |
| 2882 |
goto err_allocate_root_hub; |
| 2883 |
} |
| 2884 |
mutex_lock(&usb_port_peer_mutex); |
| 2885 |
hcd->self.root_hub = rhdev; |
| 2886 |
mutex_unlock(&usb_port_peer_mutex); |
| 2887 |
|
| 2888 |
rhdev->rx_lanes = 1; |
| 2889 |
rhdev->tx_lanes = 1; |
| 2890 |
rhdev->ssp_rate = USB_SSP_GEN_UNKNOWN; |
| 2891 |
|
| 2892 |
switch (hcd->speed) { |
| 2893 |
case HCD_USB11: |
| 2894 |
rhdev->speed = USB_SPEED_FULL; |
| 2895 |
break; |
| 2896 |
case HCD_USB2: |
| 2897 |
rhdev->speed = USB_SPEED_HIGH; |
| 2898 |
break; |
| 2899 |
case HCD_USB3: |
| 2900 |
rhdev->speed = USB_SPEED_SUPER; |
| 2901 |
break; |
| 2902 |
case HCD_USB32: |
| 2903 |
rhdev->rx_lanes = 2; |
| 2904 |
rhdev->tx_lanes = 2; |
| 2905 |
rhdev->ssp_rate = USB_SSP_GEN_2x2; |
| 2906 |
rhdev->speed = USB_SPEED_SUPER_PLUS; |
| 2907 |
break; |
| 2908 |
case HCD_USB31: |
| 2909 |
rhdev->ssp_rate = USB_SSP_GEN_2x1; |
| 2910 |
rhdev->speed = USB_SPEED_SUPER_PLUS; |
| 2911 |
break; |
| 2912 |
default: |
| 2913 |
retval = -EINVAL; |
| 2914 |
goto err_set_rh_speed; |
| 2915 |
} |
| 2916 |
|
| 2917 |
/* wakeup flag init defaults to "everything works" for root hubs, |
| 2918 |
* but drivers can override it in reset() if needed, along with |
| 2919 |
* recording the overall controller's system wakeup capability. |
| 2920 |
*/ |
| 2921 |
device_set_wakeup_capable(&rhdev->dev, 1); |
| 2922 |
|
| 2923 |
/* HCD_FLAG_RH_RUNNING doesn't matter until the root hub is |
| 2924 |
* registered. But since the controller can die at any time, |
| 2925 |
* let's initialize the flag before touching the hardware. |
| 2926 |
*/ |
| 2927 |
set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); |
| 2928 |
|
| 2929 |
/* "reset" is misnamed; its role is now one-time init. the controller |
| 2930 |
* should already have been reset (and boot firmware kicked off etc). |
| 2931 |
*/ |
| 2932 |
if (hcd->driver->reset) { |
| 2933 |
retval = hcd->driver->reset(hcd); |
| 2934 |
if (retval < 0) { |
| 2935 |
dev_err(hcd->self.controller, "can't setup: %d\n", |
| 2936 |
retval); |
| 2937 |
goto err_hcd_driver_setup; |
| 2938 |
} |
| 2939 |
} |
| 2940 |
hcd->rh_pollable = 1; |
| 2941 |
|
| 2942 |
retval = usb_phy_roothub_calibrate(hcd->phy_roothub); |
| 2943 |
if (retval) |
| 2944 |
goto err_hcd_driver_setup; |
| 2945 |
|
| 2946 |
/* NOTE: root hub and controller capabilities may not be the same */ |
| 2947 |
if (device_can_wakeup(hcd->self.controller) |
| 2948 |
&& device_can_wakeup(&hcd->self.root_hub->dev)) |
| 2949 |
dev_dbg(hcd->self.controller, "supports USB remote wakeup\n"); |
| 2950 |
|
| 2951 |
/* initialize BHs */ |
| 2952 |
init_giveback_urb_bh(&hcd->high_prio_bh); |
| 2953 |
hcd->high_prio_bh.high_prio = true; |
| 2954 |
init_giveback_urb_bh(&hcd->low_prio_bh); |
| 2955 |
|
| 2956 |
/* enable irqs just before we start the controller, |
| 2957 |
* if the BIOS provides legacy PCI irqs. |
| 2958 |
*/ |
| 2959 |
if (usb_hcd_is_primary_hcd(hcd) && irqnum) { |
| 2960 |
retval = usb_hcd_request_irqs(hcd, irqnum, irqflags); |
| 2961 |
if (retval) |
| 2962 |
goto err_request_irq; |
| 2963 |
} |
| 2964 |
|
| 2965 |
hcd->state = HC_STATE_RUNNING; |
| 2966 |
retval = hcd->driver->start(hcd); |
| 2967 |
if (retval < 0) { |
| 2968 |
dev_err(hcd->self.controller, "startup error %d\n", retval); |
| 2969 |
goto err_hcd_driver_start; |
| 2970 |
} |
| 2971 |
|
| 2972 |
/* starting here, usbcore will pay attention to the shared HCD roothub */ |
| 2973 |
shared_hcd = hcd->shared_hcd; |
| 2974 |
if (!usb_hcd_is_primary_hcd(hcd) && shared_hcd && HCD_DEFER_RH_REGISTER(shared_hcd)) { |
| 2975 |
retval = register_root_hub(shared_hcd); |
| 2976 |
if (retval != 0) |
| 2977 |
goto err_register_root_hub; |
| 2978 |
|
| 2979 |
if (shared_hcd->uses_new_polling && HCD_POLL_RH(shared_hcd)) |
| 2980 |
usb_hcd_poll_rh_status(shared_hcd); |
| 2981 |
} |
| 2982 |
|
| 2983 |
/* starting here, usbcore will pay attention to this root hub */ |
| 2984 |
if (!HCD_DEFER_RH_REGISTER(hcd)) { |
| 2985 |
retval = register_root_hub(hcd); |
| 2986 |
if (retval != 0) |
| 2987 |
goto err_register_root_hub; |
| 2988 |
|
| 2989 |
if (hcd->uses_new_polling && HCD_POLL_RH(hcd)) |
| 2990 |
usb_hcd_poll_rh_status(hcd); |
| 2991 |
} |
| 2992 |
|
| 2993 |
return retval; |
| 2994 |
|
| 2995 |
err_register_root_hub: |
| 2996 |
usb_stop_hcd(hcd); |
| 2997 |
err_hcd_driver_start: |
| 2998 |
if (usb_hcd_is_primary_hcd(hcd) && hcd->irq > 0) |
| 2999 |
free_irq(irqnum, hcd); |
| 3000 |
err_request_irq: |
| 3001 |
err_hcd_driver_setup: |
| 3002 |
err_set_rh_speed: |
| 3003 |
usb_put_invalidate_rhdev(hcd); |
| 3004 |
err_allocate_root_hub: |
| 3005 |
usb_deregister_bus(&hcd->self); |
| 3006 |
err_register_bus: |
| 3007 |
hcd_buffer_destroy(hcd); |
| 3008 |
err_create_buf: |
| 3009 |
usb_phy_roothub_power_off(hcd->phy_roothub); |
| 3010 |
err_usb_phy_roothub_power_on: |
| 3011 |
usb_phy_roothub_exit(hcd->phy_roothub); |
| 3012 |
|
| 3013 |
return retval; |
| 3014 |
} |
| 3015 |
EXPORT_SYMBOL_GPL(usb_add_hcd); |
| 3016 |
|
| 3017 |
/** |
| 3018 |
* usb_remove_hcd - shutdown processing for generic HCDs |
| 3019 |
* @hcd: the usb_hcd structure to remove |
| 3020 |
* |
| 3021 |
* Context: task context, might sleep. |
| 3022 |
* |
| 3023 |
* Disconnects the root hub, then reverses the effects of usb_add_hcd(), |
| 3024 |
* invoking the HCD's stop() method. |
| 3025 |
*/ |
| 3026 |
void usb_remove_hcd(struct usb_hcd *hcd) |
| 3027 |
{ |
| 3028 |
struct usb_device *rhdev; |
| 3029 |
bool rh_registered; |
| 3030 |
|
| 3031 |
if (!hcd) { |
| 3032 |
pr_debug("%s: hcd is NULL\n", __func__); |
| 3033 |
return; |
| 3034 |
} |
| 3035 |
rhdev = hcd->self.root_hub; |
| 3036 |
|
| 3037 |
dev_info(hcd->self.controller, "remove, state %x\n", hcd->state); |
| 3038 |
|
| 3039 |
usb_get_dev(rhdev); |
| 3040 |
clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); |
| 3041 |
if (HC_IS_RUNNING (hcd->state)) |
| 3042 |
hcd->state = HC_STATE_QUIESCING; |
| 3043 |
|
| 3044 |
dev_dbg(hcd->self.controller, "roothub graceful disconnect\n"); |
| 3045 |
spin_lock_irq (&hcd_root_hub_lock); |
| 3046 |
rh_registered = hcd->rh_registered; |
| 3047 |
hcd->rh_registered = 0; |
| 3048 |
spin_unlock_irq (&hcd_root_hub_lock); |
| 3049 |
|
| 3050 |
#ifdef CONFIG_PM |
| 3051 |
cancel_work_sync(&hcd->wakeup_work); |
| 3052 |
#endif |
| 3053 |
cancel_work_sync(&hcd->died_work); |
| 3054 |
|
| 3055 |
mutex_lock(&usb_bus_idr_lock); |
| 3056 |
if (rh_registered) |
| 3057 |
usb_disconnect(&rhdev); /* Sets rhdev to NULL */ |
| 3058 |
mutex_unlock(&usb_bus_idr_lock); |
| 3059 |
|
| 3060 |
/* |
| 3061 |
* flush_work() isn't needed here because: |
| 3062 |
* - driver's disconnect() called from usb_disconnect() should |
| 3063 |
* make sure its URBs are completed during the disconnect() |
| 3064 |
* callback |
| 3065 |
* |
| 3066 |
* - it is too late to run complete() here since driver may have |
| 3067 |
* been removed already now |
| 3068 |
*/ |
| 3069 |
|
| 3070 |
/* Prevent any more root-hub status calls from the timer. |
| 3071 |
* The HCD might still restart the timer (if a port status change |
| 3072 |
* interrupt occurs), but usb_hcd_poll_rh_status() won't invoke |
| 3073 |
* the hub_status_data() callback. |
| 3074 |
*/ |
| 3075 |
usb_stop_hcd(hcd); |
| 3076 |
|
| 3077 |
if (usb_hcd_is_primary_hcd(hcd)) { |
| 3078 |
if (hcd->irq > 0) |
| 3079 |
free_irq(hcd->irq, hcd); |
| 3080 |
} |
| 3081 |
|
| 3082 |
usb_deregister_bus(&hcd->self); |
| 3083 |
hcd_buffer_destroy(hcd); |
| 3084 |
|
| 3085 |
usb_phy_roothub_power_off(hcd->phy_roothub); |
| 3086 |
usb_phy_roothub_exit(hcd->phy_roothub); |
| 3087 |
|
| 3088 |
usb_put_invalidate_rhdev(hcd); |
| 3089 |
hcd->flags = 0; |
| 3090 |
} |
| 3091 |
EXPORT_SYMBOL_GPL(usb_remove_hcd); |
| 3092 |
|
| 3093 |
void |
| 3094 |
usb_hcd_platform_shutdown(struct platform_device *dev) |
| 3095 |
{ |
| 3096 |
struct usb_hcd *hcd = platform_get_drvdata(dev); |
| 3097 |
|
| 3098 |
/* No need for pm_runtime_put(), we're shutting down */ |
| 3099 |
pm_runtime_get_sync(&dev->dev); |
| 3100 |
|
| 3101 |
if (hcd->driver->shutdown) |
| 3102 |
hcd->driver->shutdown(hcd); |
| 3103 |
} |
| 3104 |
EXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown); |
| 3105 |
|
| 3106 |
int usb_hcd_setup_local_mem(struct usb_hcd *hcd, phys_addr_t phys_addr, |
| 3107 |
dma_addr_t dma, size_t size) |
| 3108 |
{ |
| 3109 |
int err; |
| 3110 |
void *local_mem; |
| 3111 |
|
| 3112 |
hcd->localmem_pool = devm_gen_pool_create(hcd->self.sysdev, 4, |
| 3113 |
dev_to_node(hcd->self.sysdev), |
| 3114 |
dev_name(hcd->self.sysdev)); |
| 3115 |
if (IS_ERR(hcd->localmem_pool)) |
| 3116 |
return PTR_ERR(hcd->localmem_pool); |
| 3117 |
|
| 3118 |
/* |
| 3119 |
* if a physical SRAM address was passed, map it, otherwise |
| 3120 |
* allocate system memory as a buffer. |
| 3121 |
*/ |
| 3122 |
if (phys_addr) |
| 3123 |
local_mem = devm_memremap(hcd->self.sysdev, phys_addr, |
| 3124 |
size, MEMREMAP_WC); |
| 3125 |
else |
| 3126 |
local_mem = dmam_alloc_attrs(hcd->self.sysdev, size, &dma, |
| 3127 |
GFP_KERNEL, |
| 3128 |
DMA_ATTR_WRITE_COMBINE); |
| 3129 |
|
| 3130 |
if (IS_ERR_OR_NULL(local_mem)) { |
| 3131 |
if (!local_mem) |
| 3132 |
return -ENOMEM; |
| 3133 |
|
| 3134 |
return PTR_ERR(local_mem); |
| 3135 |
} |
| 3136 |
|
| 3137 |
/* |
| 3138 |
* Here we pass a dma_addr_t but the arg type is a phys_addr_t. |
| 3139 |
* It's not backed by system memory and thus there's no kernel mapping |
| 3140 |
* for it. |
| 3141 |
*/ |
| 3142 |
err = gen_pool_add_virt(hcd->localmem_pool, (unsigned long)local_mem, |
| 3143 |
dma, size, dev_to_node(hcd->self.sysdev)); |
| 3144 |
if (err < 0) { |
| 3145 |
dev_err(hcd->self.sysdev, "gen_pool_add_virt failed with %d\n", |
| 3146 |
err); |
| 3147 |
return err; |
| 3148 |
} |
| 3149 |
|
| 3150 |
return 0; |
| 3151 |
} |
| 3152 |
EXPORT_SYMBOL_GPL(usb_hcd_setup_local_mem); |
| 3153 |
|
| 3154 |
/*-------------------------------------------------------------------------*/ |
| 3155 |
|
| 3156 |
#if IS_ENABLED(CONFIG_USB_MON) |
| 3157 |
|
| 3158 |
const struct usb_mon_operations *mon_ops; |
| 3159 |
|
| 3160 |
/* |
| 3161 |
* The registration is unlocked. |
| 3162 |
* We do it this way because we do not want to lock in hot paths. |
| 3163 |
* |
| 3164 |
* Notice that the code is minimally error-proof. Because usbmon needs |
| 3165 |
* symbols from usbcore, usbcore gets referenced and cannot be unloaded first. |
| 3166 |
*/ |
| 3167 |
|
| 3168 |
int usb_mon_register(const struct usb_mon_operations *ops) |
| 3169 |
{ |
| 3170 |
|
| 3171 |
if (mon_ops) |
| 3172 |
return -EBUSY; |
| 3173 |
|
| 3174 |
mon_ops = ops; |
| 3175 |
mb(); |
| 3176 |
return 0; |
| 3177 |
} |
| 3178 |
EXPORT_SYMBOL_GPL (usb_mon_register); |
| 3179 |
|
| 3180 |
void usb_mon_deregister (void) |
| 3181 |
{ |
| 3182 |
|
| 3183 |
if (mon_ops == NULL) { |
| 3184 |
printk(KERN_ERR "USB: monitor was not registered\n"); |
| 3185 |
return; |
| 3186 |
} |
| 3187 |
mon_ops = NULL; |
| 3188 |
mb(); |
| 3189 |
} |
| 3190 |
EXPORT_SYMBOL_GPL (usb_mon_deregister); |
| 3191 |
|
| 3192 |
#endif /* CONFIG_USB_MON || CONFIG_USB_MON_MODULE */ |