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