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