usb.c revision 3ea15966ed59f2bc20928c7b0496b4585f6de206
1/* 2 * drivers/usb/usb.c 3 * 4 * (C) Copyright Linus Torvalds 1999 5 * (C) Copyright Johannes Erdfelt 1999-2001 6 * (C) Copyright Andreas Gal 1999 7 * (C) Copyright Gregory P. Smith 1999 8 * (C) Copyright Deti Fliegl 1999 (new USB architecture) 9 * (C) Copyright Randy Dunlap 2000 10 * (C) Copyright David Brownell 2000-2004 11 * (C) Copyright Yggdrasil Computing, Inc. 2000 12 * (usb_device_id matching changes by Adam J. Richter) 13 * (C) Copyright Greg Kroah-Hartman 2002-2003 14 * 15 * NOTE! This is not actually a driver at all, rather this is 16 * just a collection of helper routines that implement the 17 * generic USB things that the real drivers can use.. 18 * 19 * Think of this as a "USB library" rather than anything else. 20 * It should be considered a slave, with no callbacks. Callbacks 21 * are evil. 22 */ 23 24#include <linux/config.h> 25 26#ifdef CONFIG_USB_DEBUG 27 #define DEBUG 28#else 29 #undef DEBUG 30#endif 31 32#include <linux/module.h> 33#include <linux/string.h> 34#include <linux/bitops.h> 35#include <linux/slab.h> 36#include <linux/interrupt.h> /* for in_interrupt() */ 37#include <linux/kmod.h> 38#include <linux/init.h> 39#include <linux/spinlock.h> 40#include <linux/errno.h> 41#include <linux/smp_lock.h> 42#include <linux/rwsem.h> 43#include <linux/usb.h> 44 45#include <asm/io.h> 46#include <asm/scatterlist.h> 47#include <linux/mm.h> 48#include <linux/dma-mapping.h> 49 50#include "hcd.h" 51#include "usb.h" 52 53 54const char *usbcore_name = "usbcore"; 55 56static int nousb; /* Disable USB when built into kernel image */ 57 /* Not honored on modular build */ 58 59static DECLARE_RWSEM(usb_all_devices_rwsem); 60 61 62static int generic_probe (struct device *dev) 63{ 64 return 0; 65} 66static int generic_remove (struct device *dev) 67{ 68 return 0; 69} 70 71static struct device_driver usb_generic_driver = { 72 .owner = THIS_MODULE, 73 .name = "usb", 74 .bus = &usb_bus_type, 75 .probe = generic_probe, 76 .remove = generic_remove, 77}; 78 79static int usb_generic_driver_data; 80 81/* called from driver core with usb_bus_type.subsys writelock */ 82static int usb_probe_interface(struct device *dev) 83{ 84 struct usb_interface * intf = to_usb_interface(dev); 85 struct usb_driver * driver = to_usb_driver(dev->driver); 86 const struct usb_device_id *id; 87 int error = -ENODEV; 88 89 dev_dbg(dev, "%s\n", __FUNCTION__); 90 91 if (!driver->probe) 92 return error; 93 /* FIXME we'd much prefer to just resume it ... */ 94 if (interface_to_usbdev(intf)->state == USB_STATE_SUSPENDED) 95 return -EHOSTUNREACH; 96 97 id = usb_match_id (intf, driver->id_table); 98 if (id) { 99 dev_dbg (dev, "%s - got id\n", __FUNCTION__); 100 intf->condition = USB_INTERFACE_BINDING; 101 error = driver->probe (intf, id); 102 intf->condition = error ? USB_INTERFACE_UNBOUND : 103 USB_INTERFACE_BOUND; 104 } 105 106 return error; 107} 108 109/* called from driver core with usb_bus_type.subsys writelock */ 110static int usb_unbind_interface(struct device *dev) 111{ 112 struct usb_interface *intf = to_usb_interface(dev); 113 struct usb_driver *driver = to_usb_driver(intf->dev.driver); 114 115 intf->condition = USB_INTERFACE_UNBINDING; 116 117 /* release all urbs for this interface */ 118 usb_disable_interface(interface_to_usbdev(intf), intf); 119 120 if (driver && driver->disconnect) 121 driver->disconnect(intf); 122 123 /* reset other interface state */ 124 usb_set_interface(interface_to_usbdev(intf), 125 intf->altsetting[0].desc.bInterfaceNumber, 126 0); 127 usb_set_intfdata(intf, NULL); 128 intf->condition = USB_INTERFACE_UNBOUND; 129 130 return 0; 131} 132 133/** 134 * usb_register - register a USB driver 135 * @new_driver: USB operations for the driver 136 * 137 * Registers a USB driver with the USB core. The list of unattached 138 * interfaces will be rescanned whenever a new driver is added, allowing 139 * the new driver to attach to any recognized devices. 140 * Returns a negative error code on failure and 0 on success. 141 * 142 * NOTE: if you want your driver to use the USB major number, you must call 143 * usb_register_dev() to enable that functionality. This function no longer 144 * takes care of that. 145 */ 146int usb_register(struct usb_driver *new_driver) 147{ 148 int retval = 0; 149 150 if (nousb) 151 return -ENODEV; 152 153 new_driver->driver.name = (char *)new_driver->name; 154 new_driver->driver.bus = &usb_bus_type; 155 new_driver->driver.probe = usb_probe_interface; 156 new_driver->driver.remove = usb_unbind_interface; 157 new_driver->driver.owner = new_driver->owner; 158 159 usb_lock_all_devices(); 160 retval = driver_register(&new_driver->driver); 161 usb_unlock_all_devices(); 162 163 if (!retval) { 164 pr_info("%s: registered new driver %s\n", 165 usbcore_name, new_driver->name); 166 usbfs_update_special(); 167 } else { 168 printk(KERN_ERR "%s: error %d registering driver %s\n", 169 usbcore_name, retval, new_driver->name); 170 } 171 172 return retval; 173} 174 175/** 176 * usb_deregister - unregister a USB driver 177 * @driver: USB operations of the driver to unregister 178 * Context: must be able to sleep 179 * 180 * Unlinks the specified driver from the internal USB driver list. 181 * 182 * NOTE: If you called usb_register_dev(), you still need to call 183 * usb_deregister_dev() to clean up your driver's allocated minor numbers, 184 * this * call will no longer do it for you. 185 */ 186void usb_deregister(struct usb_driver *driver) 187{ 188 pr_info("%s: deregistering driver %s\n", usbcore_name, driver->name); 189 190 usb_lock_all_devices(); 191 driver_unregister (&driver->driver); 192 usb_unlock_all_devices(); 193 194 usbfs_update_special(); 195} 196 197/** 198 * usb_ifnum_to_if - get the interface object with a given interface number 199 * @dev: the device whose current configuration is considered 200 * @ifnum: the desired interface 201 * 202 * This walks the device descriptor for the currently active configuration 203 * and returns a pointer to the interface with that particular interface 204 * number, or null. 205 * 206 * Note that configuration descriptors are not required to assign interface 207 * numbers sequentially, so that it would be incorrect to assume that 208 * the first interface in that descriptor corresponds to interface zero. 209 * This routine helps device drivers avoid such mistakes. 210 * However, you should make sure that you do the right thing with any 211 * alternate settings available for this interfaces. 212 * 213 * Don't call this function unless you are bound to one of the interfaces 214 * on this device or you have locked the device! 215 */ 216struct usb_interface *usb_ifnum_to_if(struct usb_device *dev, unsigned ifnum) 217{ 218 struct usb_host_config *config = dev->actconfig; 219 int i; 220 221 if (!config) 222 return NULL; 223 for (i = 0; i < config->desc.bNumInterfaces; i++) 224 if (config->interface[i]->altsetting[0] 225 .desc.bInterfaceNumber == ifnum) 226 return config->interface[i]; 227 228 return NULL; 229} 230 231/** 232 * usb_altnum_to_altsetting - get the altsetting structure with a given 233 * alternate setting number. 234 * @intf: the interface containing the altsetting in question 235 * @altnum: the desired alternate setting number 236 * 237 * This searches the altsetting array of the specified interface for 238 * an entry with the correct bAlternateSetting value and returns a pointer 239 * to that entry, or null. 240 * 241 * Note that altsettings need not be stored sequentially by number, so 242 * it would be incorrect to assume that the first altsetting entry in 243 * the array corresponds to altsetting zero. This routine helps device 244 * drivers avoid such mistakes. 245 * 246 * Don't call this function unless you are bound to the intf interface 247 * or you have locked the device! 248 */ 249struct usb_host_interface *usb_altnum_to_altsetting(struct usb_interface *intf, 250 unsigned int altnum) 251{ 252 int i; 253 254 for (i = 0; i < intf->num_altsetting; i++) { 255 if (intf->altsetting[i].desc.bAlternateSetting == altnum) 256 return &intf->altsetting[i]; 257 } 258 return NULL; 259} 260 261/** 262 * usb_driver_claim_interface - bind a driver to an interface 263 * @driver: the driver to be bound 264 * @iface: the interface to which it will be bound; must be in the 265 * usb device's active configuration 266 * @priv: driver data associated with that interface 267 * 268 * This is used by usb device drivers that need to claim more than one 269 * interface on a device when probing (audio and acm are current examples). 270 * No device driver should directly modify internal usb_interface or 271 * usb_device structure members. 272 * 273 * Few drivers should need to use this routine, since the most natural 274 * way to bind to an interface is to return the private data from 275 * the driver's probe() method. 276 * 277 * Callers must own the device lock and the driver model's usb_bus_type.subsys 278 * writelock. So driver probe() entries don't need extra locking, 279 * but other call contexts may need to explicitly claim those locks. 280 */ 281int usb_driver_claim_interface(struct usb_driver *driver, 282 struct usb_interface *iface, void* priv) 283{ 284 struct device *dev = &iface->dev; 285 286 if (dev->driver) 287 return -EBUSY; 288 289 dev->driver = &driver->driver; 290 usb_set_intfdata(iface, priv); 291 iface->condition = USB_INTERFACE_BOUND; 292 293 /* if interface was already added, bind now; else let 294 * the future device_add() bind it, bypassing probe() 295 */ 296 if (klist_node_attached(&dev->knode_bus)) 297 device_bind_driver(dev); 298 299 return 0; 300} 301 302/** 303 * usb_driver_release_interface - unbind a driver from an interface 304 * @driver: the driver to be unbound 305 * @iface: the interface from which it will be unbound 306 * 307 * This can be used by drivers to release an interface without waiting 308 * for their disconnect() methods to be called. In typical cases this 309 * also causes the driver disconnect() method to be called. 310 * 311 * This call is synchronous, and may not be used in an interrupt context. 312 * Callers must own the device lock and the driver model's usb_bus_type.subsys 313 * writelock. So driver disconnect() entries don't need extra locking, 314 * but other call contexts may need to explicitly claim those locks. 315 */ 316void usb_driver_release_interface(struct usb_driver *driver, 317 struct usb_interface *iface) 318{ 319 struct device *dev = &iface->dev; 320 321 /* this should never happen, don't release something that's not ours */ 322 if (!dev->driver || dev->driver != &driver->driver) 323 return; 324 325 /* don't release from within disconnect() */ 326 if (iface->condition != USB_INTERFACE_BOUND) 327 return; 328 329 /* release only after device_add() */ 330 if (klist_node_attached(&dev->knode_bus)) { 331 iface->condition = USB_INTERFACE_UNBINDING; 332 device_release_driver(dev); 333 } 334 335 dev->driver = NULL; 336 usb_set_intfdata(iface, NULL); 337 iface->condition = USB_INTERFACE_UNBOUND; 338} 339 340/** 341 * usb_match_id - find first usb_device_id matching device or interface 342 * @interface: the interface of interest 343 * @id: array of usb_device_id structures, terminated by zero entry 344 * 345 * usb_match_id searches an array of usb_device_id's and returns 346 * the first one matching the device or interface, or null. 347 * This is used when binding (or rebinding) a driver to an interface. 348 * Most USB device drivers will use this indirectly, through the usb core, 349 * but some layered driver frameworks use it directly. 350 * These device tables are exported with MODULE_DEVICE_TABLE, through 351 * modutils and "modules.usbmap", to support the driver loading 352 * functionality of USB hotplugging. 353 * 354 * What Matches: 355 * 356 * The "match_flags" element in a usb_device_id controls which 357 * members are used. If the corresponding bit is set, the 358 * value in the device_id must match its corresponding member 359 * in the device or interface descriptor, or else the device_id 360 * does not match. 361 * 362 * "driver_info" is normally used only by device drivers, 363 * but you can create a wildcard "matches anything" usb_device_id 364 * as a driver's "modules.usbmap" entry if you provide an id with 365 * only a nonzero "driver_info" field. If you do this, the USB device 366 * driver's probe() routine should use additional intelligence to 367 * decide whether to bind to the specified interface. 368 * 369 * What Makes Good usb_device_id Tables: 370 * 371 * The match algorithm is very simple, so that intelligence in 372 * driver selection must come from smart driver id records. 373 * Unless you have good reasons to use another selection policy, 374 * provide match elements only in related groups, and order match 375 * specifiers from specific to general. Use the macros provided 376 * for that purpose if you can. 377 * 378 * The most specific match specifiers use device descriptor 379 * data. These are commonly used with product-specific matches; 380 * the USB_DEVICE macro lets you provide vendor and product IDs, 381 * and you can also match against ranges of product revisions. 382 * These are widely used for devices with application or vendor 383 * specific bDeviceClass values. 384 * 385 * Matches based on device class/subclass/protocol specifications 386 * are slightly more general; use the USB_DEVICE_INFO macro, or 387 * its siblings. These are used with single-function devices 388 * where bDeviceClass doesn't specify that each interface has 389 * its own class. 390 * 391 * Matches based on interface class/subclass/protocol are the 392 * most general; they let drivers bind to any interface on a 393 * multiple-function device. Use the USB_INTERFACE_INFO 394 * macro, or its siblings, to match class-per-interface style 395 * devices (as recorded in bDeviceClass). 396 * 397 * Within those groups, remember that not all combinations are 398 * meaningful. For example, don't give a product version range 399 * without vendor and product IDs; or specify a protocol without 400 * its associated class and subclass. 401 */ 402const struct usb_device_id * 403usb_match_id(struct usb_interface *interface, const struct usb_device_id *id) 404{ 405 struct usb_host_interface *intf; 406 struct usb_device *dev; 407 408 /* proc_connectinfo in devio.c may call us with id == NULL. */ 409 if (id == NULL) 410 return NULL; 411 412 intf = interface->cur_altsetting; 413 dev = interface_to_usbdev(interface); 414 415 /* It is important to check that id->driver_info is nonzero, 416 since an entry that is all zeroes except for a nonzero 417 id->driver_info is the way to create an entry that 418 indicates that the driver want to examine every 419 device and interface. */ 420 for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass || 421 id->driver_info; id++) { 422 423 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && 424 id->idVendor != le16_to_cpu(dev->descriptor.idVendor)) 425 continue; 426 427 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) && 428 id->idProduct != le16_to_cpu(dev->descriptor.idProduct)) 429 continue; 430 431 /* No need to test id->bcdDevice_lo != 0, since 0 is never 432 greater than any unsigned number. */ 433 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) && 434 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice))) 435 continue; 436 437 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) && 438 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice))) 439 continue; 440 441 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) && 442 (id->bDeviceClass != dev->descriptor.bDeviceClass)) 443 continue; 444 445 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) && 446 (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass)) 447 continue; 448 449 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) && 450 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol)) 451 continue; 452 453 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) && 454 (id->bInterfaceClass != intf->desc.bInterfaceClass)) 455 continue; 456 457 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) && 458 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass)) 459 continue; 460 461 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) && 462 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol)) 463 continue; 464 465 return id; 466 } 467 468 return NULL; 469} 470 471 472static int __find_interface(struct device * dev, void * data) 473{ 474 struct usb_interface ** ret = (struct usb_interface **)data; 475 struct usb_interface * intf = *ret; 476 int *minor = (int *)data; 477 478 /* can't look at usb devices, only interfaces */ 479 if (dev->driver == &usb_generic_driver) 480 return 0; 481 482 intf = to_usb_interface(dev); 483 if (intf->minor != -1 && intf->minor == *minor) { 484 *ret = intf; 485 return 1; 486 } 487 return 0; 488} 489 490/** 491 * usb_find_interface - find usb_interface pointer for driver and device 492 * @drv: the driver whose current configuration is considered 493 * @minor: the minor number of the desired device 494 * 495 * This walks the driver device list and returns a pointer to the interface 496 * with the matching minor. Note, this only works for devices that share the 497 * USB major number. 498 */ 499struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor) 500{ 501 struct usb_interface *intf = (struct usb_interface *)(long)minor; 502 int ret; 503 504 ret = driver_for_each_device(&drv->driver, NULL, &intf, __find_interface); 505 506 return ret ? intf : NULL; 507} 508 509static int usb_device_match (struct device *dev, struct device_driver *drv) 510{ 511 struct usb_interface *intf; 512 struct usb_driver *usb_drv; 513 const struct usb_device_id *id; 514 515 /* check for generic driver, which we don't match any device with */ 516 if (drv == &usb_generic_driver) 517 return 0; 518 519 intf = to_usb_interface(dev); 520 usb_drv = to_usb_driver(drv); 521 522 id = usb_match_id (intf, usb_drv->id_table); 523 if (id) 524 return 1; 525 526 return 0; 527} 528 529 530#ifdef CONFIG_HOTPLUG 531 532/* 533 * USB hotplugging invokes what /proc/sys/kernel/hotplug says 534 * (normally /sbin/hotplug) when USB devices get added or removed. 535 * 536 * This invokes a user mode policy agent, typically helping to load driver 537 * or other modules, configure the device, and more. Drivers can provide 538 * a MODULE_DEVICE_TABLE to help with module loading subtasks. 539 * 540 * We're called either from khubd (the typical case) or from root hub 541 * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle 542 * delays in event delivery. Use sysfs (and DEVPATH) to make sure the 543 * device (and this configuration!) are still present. 544 */ 545static int usb_hotplug (struct device *dev, char **envp, int num_envp, 546 char *buffer, int buffer_size) 547{ 548 struct usb_interface *intf; 549 struct usb_device *usb_dev; 550 int i = 0; 551 int length = 0; 552 553 if (!dev) 554 return -ENODEV; 555 556 /* driver is often null here; dev_dbg() would oops */ 557 pr_debug ("usb %s: hotplug\n", dev->bus_id); 558 559 /* Must check driver_data here, as on remove driver is always NULL */ 560 if ((dev->driver == &usb_generic_driver) || 561 (dev->driver_data == &usb_generic_driver_data)) 562 return 0; 563 564 intf = to_usb_interface(dev); 565 usb_dev = interface_to_usbdev (intf); 566 567 if (usb_dev->devnum < 0) { 568 pr_debug ("usb %s: already deleted?\n", dev->bus_id); 569 return -ENODEV; 570 } 571 if (!usb_dev->bus) { 572 pr_debug ("usb %s: bus removed?\n", dev->bus_id); 573 return -ENODEV; 574 } 575 576#ifdef CONFIG_USB_DEVICEFS 577 /* If this is available, userspace programs can directly read 578 * all the device descriptors we don't tell them about. Or 579 * even act as usermode drivers. 580 * 581 * FIXME reduce hardwired intelligence here 582 */ 583 if (add_hotplug_env_var(envp, num_envp, &i, 584 buffer, buffer_size, &length, 585 "DEVICE=/proc/bus/usb/%03d/%03d", 586 usb_dev->bus->busnum, usb_dev->devnum)) 587 return -ENOMEM; 588#endif 589 590 /* per-device configurations are common */ 591 if (add_hotplug_env_var(envp, num_envp, &i, 592 buffer, buffer_size, &length, 593 "PRODUCT=%x/%x/%x", 594 le16_to_cpu(usb_dev->descriptor.idVendor), 595 le16_to_cpu(usb_dev->descriptor.idProduct), 596 le16_to_cpu(usb_dev->descriptor.bcdDevice))) 597 return -ENOMEM; 598 599 /* class-based driver binding models */ 600 if (add_hotplug_env_var(envp, num_envp, &i, 601 buffer, buffer_size, &length, 602 "TYPE=%d/%d/%d", 603 usb_dev->descriptor.bDeviceClass, 604 usb_dev->descriptor.bDeviceSubClass, 605 usb_dev->descriptor.bDeviceProtocol)) 606 return -ENOMEM; 607 608 if (usb_dev->descriptor.bDeviceClass == 0) { 609 struct usb_host_interface *alt = intf->cur_altsetting; 610 611 /* 2.4 only exposed interface zero. in 2.5, hotplug 612 * agents are called for all interfaces, and can use 613 * $DEVPATH/bInterfaceNumber if necessary. 614 */ 615 if (add_hotplug_env_var(envp, num_envp, &i, 616 buffer, buffer_size, &length, 617 "INTERFACE=%d/%d/%d", 618 alt->desc.bInterfaceClass, 619 alt->desc.bInterfaceSubClass, 620 alt->desc.bInterfaceProtocol)) 621 return -ENOMEM; 622 623 if (add_hotplug_env_var(envp, num_envp, &i, 624 buffer, buffer_size, &length, 625 "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X", 626 le16_to_cpu(usb_dev->descriptor.idVendor), 627 le16_to_cpu(usb_dev->descriptor.idProduct), 628 le16_to_cpu(usb_dev->descriptor.bcdDevice), 629 usb_dev->descriptor.bDeviceClass, 630 usb_dev->descriptor.bDeviceSubClass, 631 usb_dev->descriptor.bDeviceProtocol, 632 alt->desc.bInterfaceClass, 633 alt->desc.bInterfaceSubClass, 634 alt->desc.bInterfaceProtocol)) 635 return -ENOMEM; 636 } else { 637 if (add_hotplug_env_var(envp, num_envp, &i, 638 buffer, buffer_size, &length, 639 "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic*isc*ip*", 640 le16_to_cpu(usb_dev->descriptor.idVendor), 641 le16_to_cpu(usb_dev->descriptor.idProduct), 642 le16_to_cpu(usb_dev->descriptor.bcdDevice), 643 usb_dev->descriptor.bDeviceClass, 644 usb_dev->descriptor.bDeviceSubClass, 645 usb_dev->descriptor.bDeviceProtocol)) 646 return -ENOMEM; 647 } 648 649 envp[i] = NULL; 650 651 return 0; 652} 653 654#else 655 656static int usb_hotplug (struct device *dev, char **envp, 657 int num_envp, char *buffer, int buffer_size) 658{ 659 return -ENODEV; 660} 661 662#endif /* CONFIG_HOTPLUG */ 663 664/** 665 * usb_release_dev - free a usb device structure when all users of it are finished. 666 * @dev: device that's been disconnected 667 * 668 * Will be called only by the device core when all users of this usb device are 669 * done. 670 */ 671static void usb_release_dev(struct device *dev) 672{ 673 struct usb_device *udev; 674 675 udev = to_usb_device(dev); 676 677 usb_destroy_configuration(udev); 678 usb_bus_put(udev->bus); 679 kfree(udev->product); 680 kfree(udev->manufacturer); 681 kfree(udev->serial); 682 kfree(udev); 683} 684 685/** 686 * usb_alloc_dev - usb device constructor (usbcore-internal) 687 * @parent: hub to which device is connected; null to allocate a root hub 688 * @bus: bus used to access the device 689 * @port1: one-based index of port; ignored for root hubs 690 * Context: !in_interrupt () 691 * 692 * Only hub drivers (including virtual root hub drivers for host 693 * controllers) should ever call this. 694 * 695 * This call may not be used in a non-sleeping context. 696 */ 697struct usb_device * 698usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port1) 699{ 700 struct usb_device *dev; 701 702 dev = kmalloc(sizeof(*dev), GFP_KERNEL); 703 if (!dev) 704 return NULL; 705 706 memset(dev, 0, sizeof(*dev)); 707 708 bus = usb_bus_get(bus); 709 if (!bus) { 710 kfree(dev); 711 return NULL; 712 } 713 714 device_initialize(&dev->dev); 715 dev->dev.bus = &usb_bus_type; 716 dev->dev.dma_mask = bus->controller->dma_mask; 717 dev->dev.driver_data = &usb_generic_driver_data; 718 dev->dev.driver = &usb_generic_driver; 719 dev->dev.release = usb_release_dev; 720 dev->state = USB_STATE_ATTACHED; 721 722 INIT_LIST_HEAD(&dev->ep0.urb_list); 723 dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE; 724 dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT; 725 /* ep0 maxpacket comes later, from device descriptor */ 726 dev->ep_in[0] = dev->ep_out[0] = &dev->ep0; 727 728 /* Save readable and stable topology id, distinguishing devices 729 * by location for diagnostics, tools, driver model, etc. The 730 * string is a path along hub ports, from the root. Each device's 731 * dev->devpath will be stable until USB is re-cabled, and hubs 732 * are often labeled with these port numbers. The bus_id isn't 733 * as stable: bus->busnum changes easily from modprobe order, 734 * cardbus or pci hotplugging, and so on. 735 */ 736 if (unlikely (!parent)) { 737 dev->devpath [0] = '0'; 738 739 dev->dev.parent = bus->controller; 740 sprintf (&dev->dev.bus_id[0], "usb%d", bus->busnum); 741 } else { 742 /* match any labeling on the hubs; it's one-based */ 743 if (parent->devpath [0] == '0') 744 snprintf (dev->devpath, sizeof dev->devpath, 745 "%d", port1); 746 else 747 snprintf (dev->devpath, sizeof dev->devpath, 748 "%s.%d", parent->devpath, port1); 749 750 dev->dev.parent = &parent->dev; 751 sprintf (&dev->dev.bus_id[0], "%d-%s", 752 bus->busnum, dev->devpath); 753 754 /* hub driver sets up TT records */ 755 } 756 757 dev->bus = bus; 758 dev->parent = parent; 759 INIT_LIST_HEAD(&dev->filelist); 760 761 init_MUTEX(&dev->serialize); 762 763 return dev; 764} 765 766/** 767 * usb_get_dev - increments the reference count of the usb device structure 768 * @dev: the device being referenced 769 * 770 * Each live reference to a device should be refcounted. 771 * 772 * Drivers for USB interfaces should normally record such references in 773 * their probe() methods, when they bind to an interface, and release 774 * them by calling usb_put_dev(), in their disconnect() methods. 775 * 776 * A pointer to the device with the incremented reference counter is returned. 777 */ 778struct usb_device *usb_get_dev(struct usb_device *dev) 779{ 780 if (dev) 781 get_device(&dev->dev); 782 return dev; 783} 784 785/** 786 * usb_put_dev - release a use of the usb device structure 787 * @dev: device that's been disconnected 788 * 789 * Must be called when a user of a device is finished with it. When the last 790 * user of the device calls this function, the memory of the device is freed. 791 */ 792void usb_put_dev(struct usb_device *dev) 793{ 794 if (dev) 795 put_device(&dev->dev); 796} 797 798/** 799 * usb_get_intf - increments the reference count of the usb interface structure 800 * @intf: the interface being referenced 801 * 802 * Each live reference to a interface must be refcounted. 803 * 804 * Drivers for USB interfaces should normally record such references in 805 * their probe() methods, when they bind to an interface, and release 806 * them by calling usb_put_intf(), in their disconnect() methods. 807 * 808 * A pointer to the interface with the incremented reference counter is 809 * returned. 810 */ 811struct usb_interface *usb_get_intf(struct usb_interface *intf) 812{ 813 if (intf) 814 get_device(&intf->dev); 815 return intf; 816} 817 818/** 819 * usb_put_intf - release a use of the usb interface structure 820 * @intf: interface that's been decremented 821 * 822 * Must be called when a user of an interface is finished with it. When the 823 * last user of the interface calls this function, the memory of the interface 824 * is freed. 825 */ 826void usb_put_intf(struct usb_interface *intf) 827{ 828 if (intf) 829 put_device(&intf->dev); 830} 831 832 833/* USB device locking 834 * 835 * Although locking USB devices should be straightforward, it is 836 * complicated by the way the driver-model core works. When a new USB 837 * driver is registered or unregistered, the core will automatically 838 * probe or disconnect all matching interfaces on all USB devices while 839 * holding the USB subsystem writelock. There's no good way for us to 840 * tell which devices will be used or to lock them beforehand; our only 841 * option is to effectively lock all the USB devices. 842 * 843 * We do that by using a private rw-semaphore, usb_all_devices_rwsem. 844 * When locking an individual device you must first acquire the rwsem's 845 * readlock. When a driver is registered or unregistered the writelock 846 * must be held. These actions are encapsulated in the subroutines 847 * below, so all a driver needs to do is call usb_lock_device() and 848 * usb_unlock_device(). 849 * 850 * Complications arise when several devices are to be locked at the same 851 * time. Only hub-aware drivers that are part of usbcore ever have to 852 * do this; nobody else needs to worry about it. The problem is that 853 * usb_lock_device() must not be called to lock a second device since it 854 * would acquire the rwsem's readlock reentrantly, leading to deadlock if 855 * another thread was waiting for the writelock. The solution is simple: 856 * 857 * When locking more than one device, call usb_lock_device() 858 * to lock the first one. Lock the others by calling 859 * down(&udev->serialize) directly. 860 * 861 * When unlocking multiple devices, use up(&udev->serialize) 862 * to unlock all but the last one. Unlock the last one by 863 * calling usb_unlock_device(). 864 * 865 * When locking both a device and its parent, always lock the 866 * the parent first. 867 */ 868 869/** 870 * usb_lock_device - acquire the lock for a usb device structure 871 * @udev: device that's being locked 872 * 873 * Use this routine when you don't hold any other device locks; 874 * to acquire nested inner locks call down(&udev->serialize) directly. 875 * This is necessary for proper interaction with usb_lock_all_devices(). 876 */ 877void usb_lock_device(struct usb_device *udev) 878{ 879 down_read(&usb_all_devices_rwsem); 880 down(&udev->serialize); 881} 882 883/** 884 * usb_trylock_device - attempt to acquire the lock for a usb device structure 885 * @udev: device that's being locked 886 * 887 * Don't use this routine if you already hold a device lock; 888 * use down_trylock(&udev->serialize) instead. 889 * This is necessary for proper interaction with usb_lock_all_devices(). 890 * 891 * Returns 1 if successful, 0 if contention. 892 */ 893int usb_trylock_device(struct usb_device *udev) 894{ 895 if (!down_read_trylock(&usb_all_devices_rwsem)) 896 return 0; 897 if (down_trylock(&udev->serialize)) { 898 up_read(&usb_all_devices_rwsem); 899 return 0; 900 } 901 return 1; 902} 903 904/** 905 * usb_lock_device_for_reset - cautiously acquire the lock for a 906 * usb device structure 907 * @udev: device that's being locked 908 * @iface: interface bound to the driver making the request (optional) 909 * 910 * Attempts to acquire the device lock, but fails if the device is 911 * NOTATTACHED or SUSPENDED, or if iface is specified and the interface 912 * is neither BINDING nor BOUND. Rather than sleeping to wait for the 913 * lock, the routine polls repeatedly. This is to prevent deadlock with 914 * disconnect; in some drivers (such as usb-storage) the disconnect() 915 * or suspend() method will block waiting for a device reset to complete. 916 * 917 * Returns a negative error code for failure, otherwise 1 or 0 to indicate 918 * that the device will or will not have to be unlocked. (0 can be 919 * returned when an interface is given and is BINDING, because in that 920 * case the driver already owns the device lock.) 921 */ 922int usb_lock_device_for_reset(struct usb_device *udev, 923 struct usb_interface *iface) 924{ 925 unsigned long jiffies_expire = jiffies + HZ; 926 927 if (udev->state == USB_STATE_NOTATTACHED) 928 return -ENODEV; 929 if (udev->state == USB_STATE_SUSPENDED) 930 return -EHOSTUNREACH; 931 if (iface) { 932 switch (iface->condition) { 933 case USB_INTERFACE_BINDING: 934 return 0; 935 case USB_INTERFACE_BOUND: 936 break; 937 default: 938 return -EINTR; 939 } 940 } 941 942 while (!usb_trylock_device(udev)) { 943 944 /* If we can't acquire the lock after waiting one second, 945 * we're probably deadlocked */ 946 if (time_after(jiffies, jiffies_expire)) 947 return -EBUSY; 948 949 msleep(15); 950 if (udev->state == USB_STATE_NOTATTACHED) 951 return -ENODEV; 952 if (udev->state == USB_STATE_SUSPENDED) 953 return -EHOSTUNREACH; 954 if (iface && iface->condition != USB_INTERFACE_BOUND) 955 return -EINTR; 956 } 957 return 1; 958} 959 960/** 961 * usb_unlock_device - release the lock for a usb device structure 962 * @udev: device that's being unlocked 963 * 964 * Use this routine when releasing the only device lock you hold; 965 * to release inner nested locks call up(&udev->serialize) directly. 966 * This is necessary for proper interaction with usb_lock_all_devices(). 967 */ 968void usb_unlock_device(struct usb_device *udev) 969{ 970 up(&udev->serialize); 971 up_read(&usb_all_devices_rwsem); 972} 973 974/** 975 * usb_lock_all_devices - acquire the lock for all usb device structures 976 * 977 * This is necessary when registering a new driver or probing a bus, 978 * since the driver-model core may try to use any usb_device. 979 */ 980void usb_lock_all_devices(void) 981{ 982 down_write(&usb_all_devices_rwsem); 983} 984 985/** 986 * usb_unlock_all_devices - release the lock for all usb device structures 987 */ 988void usb_unlock_all_devices(void) 989{ 990 up_write(&usb_all_devices_rwsem); 991} 992 993 994static struct usb_device *match_device(struct usb_device *dev, 995 u16 vendor_id, u16 product_id) 996{ 997 struct usb_device *ret_dev = NULL; 998 int child; 999 1000 dev_dbg(&dev->dev, "check for vendor %04x, product %04x ...\n", 1001 le16_to_cpu(dev->descriptor.idVendor), 1002 le16_to_cpu(dev->descriptor.idProduct)); 1003 1004 /* see if this device matches */ 1005 if ((vendor_id == le16_to_cpu(dev->descriptor.idVendor)) && 1006 (product_id == le16_to_cpu(dev->descriptor.idProduct))) { 1007 dev_dbg (&dev->dev, "matched this device!\n"); 1008 ret_dev = usb_get_dev(dev); 1009 goto exit; 1010 } 1011 1012 /* look through all of the children of this device */ 1013 for (child = 0; child < dev->maxchild; ++child) { 1014 if (dev->children[child]) { 1015 down(&dev->children[child]->serialize); 1016 ret_dev = match_device(dev->children[child], 1017 vendor_id, product_id); 1018 up(&dev->children[child]->serialize); 1019 if (ret_dev) 1020 goto exit; 1021 } 1022 } 1023exit: 1024 return ret_dev; 1025} 1026 1027/** 1028 * usb_find_device - find a specific usb device in the system 1029 * @vendor_id: the vendor id of the device to find 1030 * @product_id: the product id of the device to find 1031 * 1032 * Returns a pointer to a struct usb_device if such a specified usb 1033 * device is present in the system currently. The usage count of the 1034 * device will be incremented if a device is found. Make sure to call 1035 * usb_put_dev() when the caller is finished with the device. 1036 * 1037 * If a device with the specified vendor and product id is not found, 1038 * NULL is returned. 1039 */ 1040struct usb_device *usb_find_device(u16 vendor_id, u16 product_id) 1041{ 1042 struct list_head *buslist; 1043 struct usb_bus *bus; 1044 struct usb_device *dev = NULL; 1045 1046 down(&usb_bus_list_lock); 1047 for (buslist = usb_bus_list.next; 1048 buslist != &usb_bus_list; 1049 buslist = buslist->next) { 1050 bus = container_of(buslist, struct usb_bus, bus_list); 1051 if (!bus->root_hub) 1052 continue; 1053 usb_lock_device(bus->root_hub); 1054 dev = match_device(bus->root_hub, vendor_id, product_id); 1055 usb_unlock_device(bus->root_hub); 1056 if (dev) 1057 goto exit; 1058 } 1059exit: 1060 up(&usb_bus_list_lock); 1061 return dev; 1062} 1063 1064/** 1065 * usb_get_current_frame_number - return current bus frame number 1066 * @dev: the device whose bus is being queried 1067 * 1068 * Returns the current frame number for the USB host controller 1069 * used with the given USB device. This can be used when scheduling 1070 * isochronous requests. 1071 * 1072 * Note that different kinds of host controller have different 1073 * "scheduling horizons". While one type might support scheduling only 1074 * 32 frames into the future, others could support scheduling up to 1075 * 1024 frames into the future. 1076 */ 1077int usb_get_current_frame_number(struct usb_device *dev) 1078{ 1079 return dev->bus->op->get_frame_number (dev); 1080} 1081 1082/*-------------------------------------------------------------------*/ 1083/* 1084 * __usb_get_extra_descriptor() finds a descriptor of specific type in the 1085 * extra field of the interface and endpoint descriptor structs. 1086 */ 1087 1088int __usb_get_extra_descriptor(char *buffer, unsigned size, 1089 unsigned char type, void **ptr) 1090{ 1091 struct usb_descriptor_header *header; 1092 1093 while (size >= sizeof(struct usb_descriptor_header)) { 1094 header = (struct usb_descriptor_header *)buffer; 1095 1096 if (header->bLength < 2) { 1097 printk(KERN_ERR 1098 "%s: bogus descriptor, type %d length %d\n", 1099 usbcore_name, 1100 header->bDescriptorType, 1101 header->bLength); 1102 return -1; 1103 } 1104 1105 if (header->bDescriptorType == type) { 1106 *ptr = header; 1107 return 0; 1108 } 1109 1110 buffer += header->bLength; 1111 size -= header->bLength; 1112 } 1113 return -1; 1114} 1115 1116/** 1117 * usb_buffer_alloc - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP 1118 * @dev: device the buffer will be used with 1119 * @size: requested buffer size 1120 * @mem_flags: affect whether allocation may block 1121 * @dma: used to return DMA address of buffer 1122 * 1123 * Return value is either null (indicating no buffer could be allocated), or 1124 * the cpu-space pointer to a buffer that may be used to perform DMA to the 1125 * specified device. Such cpu-space buffers are returned along with the DMA 1126 * address (through the pointer provided). 1127 * 1128 * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags 1129 * to avoid behaviors like using "DMA bounce buffers", or tying down I/O 1130 * mapping hardware for long idle periods. The implementation varies between 1131 * platforms, depending on details of how DMA will work to this device. 1132 * Using these buffers also helps prevent cacheline sharing problems on 1133 * architectures where CPU caches are not DMA-coherent. 1134 * 1135 * When the buffer is no longer used, free it with usb_buffer_free(). 1136 */ 1137void *usb_buffer_alloc ( 1138 struct usb_device *dev, 1139 size_t size, 1140 unsigned mem_flags, 1141 dma_addr_t *dma 1142) 1143{ 1144 if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_alloc) 1145 return NULL; 1146 return dev->bus->op->buffer_alloc (dev->bus, size, mem_flags, dma); 1147} 1148 1149/** 1150 * usb_buffer_free - free memory allocated with usb_buffer_alloc() 1151 * @dev: device the buffer was used with 1152 * @size: requested buffer size 1153 * @addr: CPU address of buffer 1154 * @dma: DMA address of buffer 1155 * 1156 * This reclaims an I/O buffer, letting it be reused. The memory must have 1157 * been allocated using usb_buffer_alloc(), and the parameters must match 1158 * those provided in that allocation request. 1159 */ 1160void usb_buffer_free ( 1161 struct usb_device *dev, 1162 size_t size, 1163 void *addr, 1164 dma_addr_t dma 1165) 1166{ 1167 if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_free) 1168 return; 1169 dev->bus->op->buffer_free (dev->bus, size, addr, dma); 1170} 1171 1172/** 1173 * usb_buffer_map - create DMA mapping(s) for an urb 1174 * @urb: urb whose transfer_buffer/setup_packet will be mapped 1175 * 1176 * Return value is either null (indicating no buffer could be mapped), or 1177 * the parameter. URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP are 1178 * added to urb->transfer_flags if the operation succeeds. If the device 1179 * is connected to this system through a non-DMA controller, this operation 1180 * always succeeds. 1181 * 1182 * This call would normally be used for an urb which is reused, perhaps 1183 * as the target of a large periodic transfer, with usb_buffer_dmasync() 1184 * calls to synchronize memory and dma state. 1185 * 1186 * Reverse the effect of this call with usb_buffer_unmap(). 1187 */ 1188#if 0 1189struct urb *usb_buffer_map (struct urb *urb) 1190{ 1191 struct usb_bus *bus; 1192 struct device *controller; 1193 1194 if (!urb 1195 || !urb->dev 1196 || !(bus = urb->dev->bus) 1197 || !(controller = bus->controller)) 1198 return NULL; 1199 1200 if (controller->dma_mask) { 1201 urb->transfer_dma = dma_map_single (controller, 1202 urb->transfer_buffer, urb->transfer_buffer_length, 1203 usb_pipein (urb->pipe) 1204 ? DMA_FROM_DEVICE : DMA_TO_DEVICE); 1205 if (usb_pipecontrol (urb->pipe)) 1206 urb->setup_dma = dma_map_single (controller, 1207 urb->setup_packet, 1208 sizeof (struct usb_ctrlrequest), 1209 DMA_TO_DEVICE); 1210 // FIXME generic api broken like pci, can't report errors 1211 // if (urb->transfer_dma == DMA_ADDR_INVALID) return 0; 1212 } else 1213 urb->transfer_dma = ~0; 1214 urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP 1215 | URB_NO_SETUP_DMA_MAP); 1216 return urb; 1217} 1218#endif /* 0 */ 1219 1220/* XXX DISABLED, no users currently. If you wish to re-enable this 1221 * XXX please determine whether the sync is to transfer ownership of 1222 * XXX the buffer from device to cpu or vice verse, and thusly use the 1223 * XXX appropriate _for_{cpu,device}() method. -DaveM 1224 */ 1225#if 0 1226 1227/** 1228 * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s) 1229 * @urb: urb whose transfer_buffer/setup_packet will be synchronized 1230 */ 1231void usb_buffer_dmasync (struct urb *urb) 1232{ 1233 struct usb_bus *bus; 1234 struct device *controller; 1235 1236 if (!urb 1237 || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) 1238 || !urb->dev 1239 || !(bus = urb->dev->bus) 1240 || !(controller = bus->controller)) 1241 return; 1242 1243 if (controller->dma_mask) { 1244 dma_sync_single (controller, 1245 urb->transfer_dma, urb->transfer_buffer_length, 1246 usb_pipein (urb->pipe) 1247 ? DMA_FROM_DEVICE : DMA_TO_DEVICE); 1248 if (usb_pipecontrol (urb->pipe)) 1249 dma_sync_single (controller, 1250 urb->setup_dma, 1251 sizeof (struct usb_ctrlrequest), 1252 DMA_TO_DEVICE); 1253 } 1254} 1255#endif 1256 1257/** 1258 * usb_buffer_unmap - free DMA mapping(s) for an urb 1259 * @urb: urb whose transfer_buffer will be unmapped 1260 * 1261 * Reverses the effect of usb_buffer_map(). 1262 */ 1263#if 0 1264void usb_buffer_unmap (struct urb *urb) 1265{ 1266 struct usb_bus *bus; 1267 struct device *controller; 1268 1269 if (!urb 1270 || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) 1271 || !urb->dev 1272 || !(bus = urb->dev->bus) 1273 || !(controller = bus->controller)) 1274 return; 1275 1276 if (controller->dma_mask) { 1277 dma_unmap_single (controller, 1278 urb->transfer_dma, urb->transfer_buffer_length, 1279 usb_pipein (urb->pipe) 1280 ? DMA_FROM_DEVICE : DMA_TO_DEVICE); 1281 if (usb_pipecontrol (urb->pipe)) 1282 dma_unmap_single (controller, 1283 urb->setup_dma, 1284 sizeof (struct usb_ctrlrequest), 1285 DMA_TO_DEVICE); 1286 } 1287 urb->transfer_flags &= ~(URB_NO_TRANSFER_DMA_MAP 1288 | URB_NO_SETUP_DMA_MAP); 1289} 1290#endif /* 0 */ 1291 1292/** 1293 * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint 1294 * @dev: device to which the scatterlist will be mapped 1295 * @pipe: endpoint defining the mapping direction 1296 * @sg: the scatterlist to map 1297 * @nents: the number of entries in the scatterlist 1298 * 1299 * Return value is either < 0 (indicating no buffers could be mapped), or 1300 * the number of DMA mapping array entries in the scatterlist. 1301 * 1302 * The caller is responsible for placing the resulting DMA addresses from 1303 * the scatterlist into URB transfer buffer pointers, and for setting the 1304 * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs. 1305 * 1306 * Top I/O rates come from queuing URBs, instead of waiting for each one 1307 * to complete before starting the next I/O. This is particularly easy 1308 * to do with scatterlists. Just allocate and submit one URB for each DMA 1309 * mapping entry returned, stopping on the first error or when all succeed. 1310 * Better yet, use the usb_sg_*() calls, which do that (and more) for you. 1311 * 1312 * This call would normally be used when translating scatterlist requests, 1313 * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it 1314 * may be able to coalesce mappings for improved I/O efficiency. 1315 * 1316 * Reverse the effect of this call with usb_buffer_unmap_sg(). 1317 */ 1318int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe, 1319 struct scatterlist *sg, int nents) 1320{ 1321 struct usb_bus *bus; 1322 struct device *controller; 1323 1324 if (!dev 1325 || usb_pipecontrol (pipe) 1326 || !(bus = dev->bus) 1327 || !(controller = bus->controller) 1328 || !controller->dma_mask) 1329 return -1; 1330 1331 // FIXME generic api broken like pci, can't report errors 1332 return dma_map_sg (controller, sg, nents, 1333 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE); 1334} 1335 1336/* XXX DISABLED, no users currently. If you wish to re-enable this 1337 * XXX please determine whether the sync is to transfer ownership of 1338 * XXX the buffer from device to cpu or vice verse, and thusly use the 1339 * XXX appropriate _for_{cpu,device}() method. -DaveM 1340 */ 1341#if 0 1342 1343/** 1344 * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s) 1345 * @dev: device to which the scatterlist will be mapped 1346 * @pipe: endpoint defining the mapping direction 1347 * @sg: the scatterlist to synchronize 1348 * @n_hw_ents: the positive return value from usb_buffer_map_sg 1349 * 1350 * Use this when you are re-using a scatterlist's data buffers for 1351 * another USB request. 1352 */ 1353void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe, 1354 struct scatterlist *sg, int n_hw_ents) 1355{ 1356 struct usb_bus *bus; 1357 struct device *controller; 1358 1359 if (!dev 1360 || !(bus = dev->bus) 1361 || !(controller = bus->controller) 1362 || !controller->dma_mask) 1363 return; 1364 1365 dma_sync_sg (controller, sg, n_hw_ents, 1366 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE); 1367} 1368#endif 1369 1370/** 1371 * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist 1372 * @dev: device to which the scatterlist will be mapped 1373 * @pipe: endpoint defining the mapping direction 1374 * @sg: the scatterlist to unmap 1375 * @n_hw_ents: the positive return value from usb_buffer_map_sg 1376 * 1377 * Reverses the effect of usb_buffer_map_sg(). 1378 */ 1379void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe, 1380 struct scatterlist *sg, int n_hw_ents) 1381{ 1382 struct usb_bus *bus; 1383 struct device *controller; 1384 1385 if (!dev 1386 || !(bus = dev->bus) 1387 || !(controller = bus->controller) 1388 || !controller->dma_mask) 1389 return; 1390 1391 dma_unmap_sg (controller, sg, n_hw_ents, 1392 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE); 1393} 1394 1395static int usb_generic_suspend(struct device *dev, pm_message_t message) 1396{ 1397 struct usb_interface *intf; 1398 struct usb_driver *driver; 1399 1400 if (dev->driver == &usb_generic_driver) 1401 return usb_suspend_device (to_usb_device(dev), message); 1402 1403 if ((dev->driver == NULL) || 1404 (dev->driver_data == &usb_generic_driver_data)) 1405 return 0; 1406 1407 intf = to_usb_interface(dev); 1408 driver = to_usb_driver(dev->driver); 1409 1410 /* there's only one USB suspend state */ 1411 if (intf->dev.power.power_state.event) 1412 return 0; 1413 1414 if (driver->suspend) 1415 return driver->suspend(intf, message); 1416 return 0; 1417} 1418 1419static int usb_generic_resume(struct device *dev) 1420{ 1421 struct usb_interface *intf; 1422 struct usb_driver *driver; 1423 1424 /* devices resume through their hub */ 1425 if (dev->driver == &usb_generic_driver) 1426 return usb_resume_device (to_usb_device(dev)); 1427 1428 if ((dev->driver == NULL) || 1429 (dev->driver_data == &usb_generic_driver_data)) 1430 return 0; 1431 1432 intf = to_usb_interface(dev); 1433 driver = to_usb_driver(dev->driver); 1434 1435 if (driver->resume) 1436 return driver->resume(intf); 1437 return 0; 1438} 1439 1440struct bus_type usb_bus_type = { 1441 .name = "usb", 1442 .match = usb_device_match, 1443 .hotplug = usb_hotplug, 1444 .suspend = usb_generic_suspend, 1445 .resume = usb_generic_resume, 1446}; 1447 1448#ifndef MODULE 1449 1450static int __init usb_setup_disable(char *str) 1451{ 1452 nousb = 1; 1453 return 1; 1454} 1455 1456/* format to disable USB on kernel command line is: nousb */ 1457__setup("nousb", usb_setup_disable); 1458 1459#endif 1460 1461/* 1462 * for external read access to <nousb> 1463 */ 1464int usb_disabled(void) 1465{ 1466 return nousb; 1467} 1468 1469/* 1470 * Init 1471 */ 1472static int __init usb_init(void) 1473{ 1474 int retval; 1475 if (nousb) { 1476 pr_info ("%s: USB support disabled\n", usbcore_name); 1477 return 0; 1478 } 1479 1480 retval = bus_register(&usb_bus_type); 1481 if (retval) 1482 goto out; 1483 retval = usb_host_init(); 1484 if (retval) 1485 goto host_init_failed; 1486 retval = usb_major_init(); 1487 if (retval) 1488 goto major_init_failed; 1489 retval = usb_register(&usbfs_driver); 1490 if (retval) 1491 goto driver_register_failed; 1492 retval = usbdev_init(); 1493 if (retval) 1494 goto usbdevice_init_failed; 1495 retval = usbfs_init(); 1496 if (retval) 1497 goto fs_init_failed; 1498 retval = usb_hub_init(); 1499 if (retval) 1500 goto hub_init_failed; 1501 retval = driver_register(&usb_generic_driver); 1502 if (!retval) 1503 goto out; 1504 1505 usb_hub_cleanup(); 1506hub_init_failed: 1507 usbfs_cleanup(); 1508fs_init_failed: 1509 usbdev_cleanup(); 1510usbdevice_init_failed: 1511 usb_deregister(&usbfs_driver); 1512driver_register_failed: 1513 usb_major_cleanup(); 1514major_init_failed: 1515 usb_host_cleanup(); 1516host_init_failed: 1517 bus_unregister(&usb_bus_type); 1518out: 1519 return retval; 1520} 1521 1522/* 1523 * Cleanup 1524 */ 1525static void __exit usb_exit(void) 1526{ 1527 /* This will matter if shutdown/reboot does exitcalls. */ 1528 if (nousb) 1529 return; 1530 1531 driver_unregister(&usb_generic_driver); 1532 usb_major_cleanup(); 1533 usbfs_cleanup(); 1534 usb_deregister(&usbfs_driver); 1535 usbdev_cleanup(); 1536 usb_hub_cleanup(); 1537 usb_host_cleanup(); 1538 bus_unregister(&usb_bus_type); 1539} 1540 1541subsys_initcall(usb_init); 1542module_exit(usb_exit); 1543 1544/* 1545 * USB may be built into the kernel or be built as modules. 1546 * These symbols are exported for device (or host controller) 1547 * driver modules to use. 1548 */ 1549 1550EXPORT_SYMBOL(usb_register); 1551EXPORT_SYMBOL(usb_deregister); 1552EXPORT_SYMBOL(usb_disabled); 1553 1554EXPORT_SYMBOL_GPL(usb_get_intf); 1555EXPORT_SYMBOL_GPL(usb_put_intf); 1556 1557EXPORT_SYMBOL(usb_alloc_dev); 1558EXPORT_SYMBOL(usb_put_dev); 1559EXPORT_SYMBOL(usb_get_dev); 1560EXPORT_SYMBOL(usb_hub_tt_clear_buffer); 1561 1562EXPORT_SYMBOL(usb_lock_device); 1563EXPORT_SYMBOL(usb_trylock_device); 1564EXPORT_SYMBOL(usb_lock_device_for_reset); 1565EXPORT_SYMBOL(usb_unlock_device); 1566 1567EXPORT_SYMBOL(usb_driver_claim_interface); 1568EXPORT_SYMBOL(usb_driver_release_interface); 1569EXPORT_SYMBOL(usb_match_id); 1570EXPORT_SYMBOL(usb_find_interface); 1571EXPORT_SYMBOL(usb_ifnum_to_if); 1572EXPORT_SYMBOL(usb_altnum_to_altsetting); 1573 1574EXPORT_SYMBOL(usb_reset_device); 1575EXPORT_SYMBOL(usb_disconnect); 1576 1577EXPORT_SYMBOL(__usb_get_extra_descriptor); 1578 1579EXPORT_SYMBOL(usb_find_device); 1580EXPORT_SYMBOL(usb_get_current_frame_number); 1581 1582EXPORT_SYMBOL (usb_buffer_alloc); 1583EXPORT_SYMBOL (usb_buffer_free); 1584 1585#if 0 1586EXPORT_SYMBOL (usb_buffer_map); 1587EXPORT_SYMBOL (usb_buffer_dmasync); 1588EXPORT_SYMBOL (usb_buffer_unmap); 1589#endif 1590 1591EXPORT_SYMBOL (usb_buffer_map_sg); 1592#if 0 1593EXPORT_SYMBOL (usb_buffer_dmasync_sg); 1594#endif 1595EXPORT_SYMBOL (usb_buffer_unmap_sg); 1596 1597MODULE_LICENSE("GPL"); 1598