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