Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /*
  2  * drivers/usb/core/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/module.h>
 25 #include <linux/moduleparam.h>
 26 #include <linux/string.h>
 27 #include <linux/bitops.h>
 28 #include <linux/slab.h>
 29 #include <linux/interrupt.h>  /* for in_interrupt() */
 30 #include <linux/kmod.h>
 31 #include <linux/init.h>
 32 #include <linux/spinlock.h>
 33 #include <linux/errno.h>
 34 #include <linux/usb.h>
 35 #include <linux/mutex.h>
 36 #include <linux/workqueue.h>
 37 #include <linux/debugfs.h>
 38 
 39 #include <asm/io.h>
 40 #include <linux/scatterlist.h>
 41 #include <linux/mm.h>
 42 #include <linux/dma-mapping.h>
 43 
 44 #include "hcd.h"
 45 #include "usb.h"
 46 
 47 
 48 const char *usbcore_name = "usbcore";
 49 
 50 static int nousb;       /* Disable USB when built into kernel image */
 51 
 52 /* Workqueue for autosuspend and for remote wakeup of root hubs */
 53 struct workqueue_struct *ksuspend_usb_wq;
 54 
 55 #ifdef  CONFIG_USB_SUSPEND
 56 static int usb_autosuspend_delay = 2;           /* Default delay value,
 57                                                  * in seconds */
 58 module_param_named(autosuspend, usb_autosuspend_delay, int, 0644);
 59 MODULE_PARM_DESC(autosuspend, "default autosuspend delay");
 60 
 61 #else
 62 #define usb_autosuspend_delay           0
 63 #endif
 64 
 65 
 66 /**
 67  * usb_ifnum_to_if - get the interface object with a given interface number
 68  * @dev: the device whose current configuration is considered
 69  * @ifnum: the desired interface
 70  *
 71  * This walks the device descriptor for the currently active configuration
 72  * and returns a pointer to the interface with that particular interface
 73  * number, or null.
 74  *
 75  * Note that configuration descriptors are not required to assign interface
 76  * numbers sequentially, so that it would be incorrect to assume that
 77  * the first interface in that descriptor corresponds to interface zero.
 78  * This routine helps device drivers avoid such mistakes.
 79  * However, you should make sure that you do the right thing with any
 80  * alternate settings available for this interfaces.
 81  *
 82  * Don't call this function unless you are bound to one of the interfaces
 83  * on this device or you have locked the device!
 84  */
 85 struct usb_interface *usb_ifnum_to_if(const struct usb_device *dev,
 86                                       unsigned ifnum)
 87 {
 88         struct usb_host_config *config = dev->actconfig;
 89         int i;
 90 
 91         if (!config)
 92                 return NULL;
 93         for (i = 0; i < config->desc.bNumInterfaces; i++)
 94                 if (config->interface[i]->altsetting[0]
 95                                 .desc.bInterfaceNumber == ifnum)
 96                         return config->interface[i];
 97 
 98         return NULL;
 99 }
100 EXPORT_SYMBOL_GPL(usb_ifnum_to_if);
101 
102 /**
103  * usb_altnum_to_altsetting - get the altsetting structure with a given alternate setting number.
104  * @intf: the interface containing the altsetting in question
105  * @altnum: the desired alternate setting number
106  *
107  * This searches the altsetting array of the specified interface for
108  * an entry with the correct bAlternateSetting value and returns a pointer
109  * to that entry, or null.
110  *
111  * Note that altsettings need not be stored sequentially by number, so
112  * it would be incorrect to assume that the first altsetting entry in
113  * the array corresponds to altsetting zero.  This routine helps device
114  * drivers avoid such mistakes.
115  *
116  * Don't call this function unless you are bound to the intf interface
117  * or you have locked the device!
118  */
119 struct usb_host_interface *usb_altnum_to_altsetting(
120                                         const struct usb_interface *intf,
121                                         unsigned int altnum)
122 {
123         int i;
124 
125         for (i = 0; i < intf->num_altsetting; i++) {
126                 if (intf->altsetting[i].desc.bAlternateSetting == altnum)
127                         return &intf->altsetting[i];
128         }
129         return NULL;
130 }
131 EXPORT_SYMBOL_GPL(usb_altnum_to_altsetting);
132 
133 struct find_interface_arg {
134         int minor;
135         struct device_driver *drv;
136 };
137 
138 static int __find_interface(struct device *dev, void *data)
139 {
140         struct find_interface_arg *arg = data;
141         struct usb_interface *intf;
142 
143         if (!is_usb_interface(dev))
144                 return 0;
145 
146         if (dev->driver != arg->drv)
147                 return 0;
148         intf = to_usb_interface(dev);
149         return intf->minor == arg->minor;
150 }
151 
152 /**
153  * usb_find_interface - find usb_interface pointer for driver and device
154  * @drv: the driver whose current configuration is considered
155  * @minor: the minor number of the desired device
156  *
157  * This walks the bus device list and returns a pointer to the interface
158  * with the matching minor and driver.  Note, this only works for devices
159  * that share the USB major number.
160  */
161 struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor)
162 {
163         struct find_interface_arg argb;
164         struct device *dev;
165 
166         argb.minor = minor;
167         argb.drv = &drv->drvwrap.driver;
168 
169         dev = bus_find_device(&usb_bus_type, NULL, &argb, __find_interface);
170 
171         /* Drop reference count from bus_find_device */
172         put_device(dev);
173 
174         return dev ? to_usb_interface(dev) : NULL;
175 }
176 EXPORT_SYMBOL_GPL(usb_find_interface);
177 
178 /**
179  * usb_release_dev - free a usb device structure when all users of it are finished.
180  * @dev: device that's been disconnected
181  *
182  * Will be called only by the device core when all users of this usb device are
183  * done.
184  */
185 static void usb_release_dev(struct device *dev)
186 {
187         struct usb_device *udev;
188         struct usb_hcd *hcd;
189 
190         udev = to_usb_device(dev);
191         hcd = bus_to_hcd(udev->bus);
192 
193         usb_destroy_configuration(udev);
194         /* Root hubs aren't real devices, so don't free HCD resources */
195         if (hcd->driver->free_dev && udev->parent)
196                 hcd->driver->free_dev(hcd, udev);
197         usb_put_hcd(hcd);
198         kfree(udev->product);
199         kfree(udev->manufacturer);
200         kfree(udev->serial);
201         kfree(udev);
202 }
203 
204 #ifdef  CONFIG_HOTPLUG
205 static int usb_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
206 {
207         struct usb_device *usb_dev;
208 
209         usb_dev = to_usb_device(dev);
210 
211         if (add_uevent_var(env, "BUSNUM=%03d", usb_dev->bus->busnum))
212                 return -ENOMEM;
213 
214         if (add_uevent_var(env, "DEVNUM=%03d", usb_dev->devnum))
215                 return -ENOMEM;
216 
217         return 0;
218 }
219 
220 #else
221 
222 static int usb_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
223 {
224         return -ENODEV;
225 }
226 #endif  /* CONFIG_HOTPLUG */
227 
228 #ifdef  CONFIG_PM
229 
230 static int ksuspend_usb_init(void)
231 {
232         /* This workqueue is supposed to be both freezable and
233          * singlethreaded.  Its job doesn't justify running on more
234          * than one CPU.
235          */
236         ksuspend_usb_wq = create_freezeable_workqueue("ksuspend_usbd");
237         if (!ksuspend_usb_wq)
238                 return -ENOMEM;
239         return 0;
240 }
241 
242 static void ksuspend_usb_cleanup(void)
243 {
244         destroy_workqueue(ksuspend_usb_wq);
245 }
246 
247 /* USB device Power-Management thunks.
248  * There's no need to distinguish here between quiescing a USB device
249  * and powering it down; the generic_suspend() routine takes care of
250  * it by skipping the usb_port_suspend() call for a quiesce.  And for
251  * USB interfaces there's no difference at all.
252  */
253 
254 static int usb_dev_prepare(struct device *dev)
255 {
256         return 0;               /* Implement eventually? */
257 }
258 
259 static void usb_dev_complete(struct device *dev)
260 {
261         /* Currently used only for rebinding interfaces */
262         usb_resume(dev, PMSG_RESUME);   /* Message event is meaningless */
263 }
264 
265 static int usb_dev_suspend(struct device *dev)
266 {
267         return usb_suspend(dev, PMSG_SUSPEND);
268 }
269 
270 static int usb_dev_resume(struct device *dev)
271 {
272         return usb_resume(dev, PMSG_RESUME);
273 }
274 
275 static int usb_dev_freeze(struct device *dev)
276 {
277         return usb_suspend(dev, PMSG_FREEZE);
278 }
279 
280 static int usb_dev_thaw(struct device *dev)
281 {
282         return usb_resume(dev, PMSG_THAW);
283 }
284 
285 static int usb_dev_poweroff(struct device *dev)
286 {
287         return usb_suspend(dev, PMSG_HIBERNATE);
288 }
289 
290 static int usb_dev_restore(struct device *dev)
291 {
292         return usb_resume(dev, PMSG_RESTORE);
293 }
294 
295 static struct dev_pm_ops usb_device_pm_ops = {
296         .prepare =      usb_dev_prepare,
297         .complete =     usb_dev_complete,
298         .suspend =      usb_dev_suspend,
299         .resume =       usb_dev_resume,
300         .freeze =       usb_dev_freeze,
301         .thaw =         usb_dev_thaw,
302         .poweroff =     usb_dev_poweroff,
303         .restore =      usb_dev_restore,
304 };
305 
306 #else
307 
308 #define ksuspend_usb_init()     0
309 #define ksuspend_usb_cleanup()  do {} while (0)
310 #define usb_device_pm_ops       (*(struct dev_pm_ops *)0)
311 
312 #endif  /* CONFIG_PM */
313 
314 
315 static char *usb_nodename(struct device *dev)
316 {
317         struct usb_device *usb_dev;
318 
319         usb_dev = to_usb_device(dev);
320         return kasprintf(GFP_KERNEL, "bus/usb/%03d/%03d",
321                          usb_dev->bus->busnum, usb_dev->devnum);
322 }
323 
324 struct device_type usb_device_type = {
325         .name =         "usb_device",
326         .release =      usb_release_dev,
327         .uevent =       usb_dev_uevent,
328         .nodename =     usb_nodename,
329         .pm =           &usb_device_pm_ops,
330 };
331 
332 
333 /* Returns 1 if @usb_bus is WUSB, 0 otherwise */
334 static unsigned usb_bus_is_wusb(struct usb_bus *bus)
335 {
336         struct usb_hcd *hcd = container_of(bus, struct usb_hcd, self);
337         return hcd->wireless;
338 }
339 
340 
341 /**
342  * usb_alloc_dev - usb device constructor (usbcore-internal)
343  * @parent: hub to which device is connected; null to allocate a root hub
344  * @bus: bus used to access the device
345  * @port1: one-based index of port; ignored for root hubs
346  * Context: !in_interrupt()
347  *
348  * Only hub drivers (including virtual root hub drivers for host
349  * controllers) should ever call this.
350  *
351  * This call may not be used in a non-sleeping context.
352  */
353 struct usb_device *usb_alloc_dev(struct usb_device *parent,
354                                  struct usb_bus *bus, unsigned port1)
355 {
356         struct usb_device *dev;
357         struct usb_hcd *usb_hcd = container_of(bus, struct usb_hcd, self);
358         unsigned root_hub = 0;
359 
360         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
361         if (!dev)
362                 return NULL;
363 
364         if (!usb_get_hcd(bus_to_hcd(bus))) {
365                 kfree(dev);
366                 return NULL;
367         }
368         /* Root hubs aren't true devices, so don't allocate HCD resources */
369         if (usb_hcd->driver->alloc_dev && parent &&
370                 !usb_hcd->driver->alloc_dev(usb_hcd, dev)) {
371                 usb_put_hcd(bus_to_hcd(bus));
372                 kfree(dev);
373                 return NULL;
374         }
375 
376         device_initialize(&dev->dev);
377         dev->dev.bus = &usb_bus_type;
378         dev->dev.type = &usb_device_type;
379         dev->dev.groups = usb_device_groups;
380         dev->dev.dma_mask = bus->controller->dma_mask;
381         set_dev_node(&dev->dev, dev_to_node(bus->controller));
382         dev->state = USB_STATE_ATTACHED;
383         atomic_set(&dev->urbnum, 0);
384 
385         INIT_LIST_HEAD(&dev->ep0.urb_list);
386         dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;
387         dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;
388         /* ep0 maxpacket comes later, from device descriptor */
389         usb_enable_endpoint(dev, &dev->ep0, false);
390         dev->can_submit = 1;
391 
392         /* Save readable and stable topology id, distinguishing devices
393          * by location for diagnostics, tools, driver model, etc.  The
394          * string is a path along hub ports, from the root.  Each device's
395          * dev->devpath will be stable until USB is re-cabled, and hubs
396          * are often labeled with these port numbers.  The name isn't
397          * as stable:  bus->busnum changes easily from modprobe order,
398          * cardbus or pci hotplugging, and so on.
399          */
400         if (unlikely(!parent)) {
401                 dev->devpath[0] = '';
402                 dev->route = 0;
403 
404                 dev->dev.parent = bus->controller;
405                 dev_set_name(&dev->dev, "usb%d", bus->busnum);
406                 root_hub = 1;
407         } else {
408                 /* match any labeling on the hubs; it's one-based */
409                 if (parent->devpath[0] == '') {
410                         snprintf(dev->devpath, sizeof dev->devpath,
411                                 "%d", port1);
412                         /* Root ports are not counted in route string */
413                         dev->route = 0;
414                 } else {
415                         snprintf(dev->devpath, sizeof dev->devpath,
416                                 "%s.%d", parent->devpath, port1);
417                         dev->route = parent->route +
418                                 (port1 << ((parent->level - 1)*4));
419                 }
420 
421                 dev->dev.parent = &parent->dev;
422                 dev_set_name(&dev->dev, "%d-%s", bus->busnum, dev->devpath);
423 
424                 /* hub driver sets up TT records */
425         }
426 
427         dev->portnum = port1;
428         dev->bus = bus;
429         dev->parent = parent;
430         INIT_LIST_HEAD(&dev->filelist);
431 
432 #ifdef  CONFIG_PM
433         mutex_init(&dev->pm_mutex);
434         INIT_DELAYED_WORK(&dev->autosuspend, usb_autosuspend_work);
435         INIT_WORK(&dev->autoresume, usb_autoresume_work);
436         dev->autosuspend_delay = usb_autosuspend_delay * HZ;
437         dev->connect_time = jiffies;
438         dev->active_duration = -jiffies;
439 #endif
440         if (root_hub)   /* Root hub always ok [and always wired] */
441                 dev->authorized = 1;
442         else {
443                 dev->authorized = usb_hcd->authorized_default;
444                 dev->wusb = usb_bus_is_wusb(bus)? 1 : 0;
445         }
446         return dev;
447 }
448 
449 /**
450  * usb_get_dev - increments the reference count of the usb device structure
451  * @dev: the device being referenced
452  *
453  * Each live reference to a device should be refcounted.
454  *
455  * Drivers for USB interfaces should normally record such references in
456  * their probe() methods, when they bind to an interface, and release
457  * them by calling usb_put_dev(), in their disconnect() methods.
458  *
459  * A pointer to the device with the incremented reference counter is returned.
460  */
461 struct usb_device *usb_get_dev(struct usb_device *dev)
462 {
463         if (dev)
464                 get_device(&dev->dev);
465         return dev;
466 }
467 EXPORT_SYMBOL_GPL(usb_get_dev);
468 
469 /**
470  * usb_put_dev - release a use of the usb device structure
471  * @dev: device that's been disconnected
472  *
473  * Must be called when a user of a device is finished with it.  When the last
474  * user of the device calls this function, the memory of the device is freed.
475  */
476 void usb_put_dev(struct usb_device *dev)
477 {
478         if (dev)
479                 put_device(&dev->dev);
480 }
481 EXPORT_SYMBOL_GPL(usb_put_dev);
482 
483 /**
484  * usb_get_intf - increments the reference count of the usb interface structure
485  * @intf: the interface being referenced
486  *
487  * Each live reference to a interface must be refcounted.
488  *
489  * Drivers for USB interfaces should normally record such references in
490  * their probe() methods, when they bind to an interface, and release
491  * them by calling usb_put_intf(), in their disconnect() methods.
492  *
493  * A pointer to the interface with the incremented reference counter is
494  * returned.
495  */
496 struct usb_interface *usb_get_intf(struct usb_interface *intf)
497 {
498         if (intf)
499                 get_device(&intf->dev);
500         return intf;
501 }
502 EXPORT_SYMBOL_GPL(usb_get_intf);
503 
504 /**
505  * usb_put_intf - release a use of the usb interface structure
506  * @intf: interface that's been decremented
507  *
508  * Must be called when a user of an interface is finished with it.  When the
509  * last user of the interface calls this function, the memory of the interface
510  * is freed.
511  */
512 void usb_put_intf(struct usb_interface *intf)
513 {
514         if (intf)
515                 put_device(&intf->dev);
516 }
517 EXPORT_SYMBOL_GPL(usb_put_intf);
518 
519 /*                      USB device locking
520  *
521  * USB devices and interfaces are locked using the semaphore in their
522  * embedded struct device.  The hub driver guarantees that whenever a
523  * device is connected or disconnected, drivers are called with the
524  * USB device locked as well as their particular interface.
525  *
526  * Complications arise when several devices are to be locked at the same
527  * time.  Only hub-aware drivers that are part of usbcore ever have to
528  * do this; nobody else needs to worry about it.  The rule for locking
529  * is simple:
530  *
531  *      When locking both a device and its parent, always lock the
532  *      the parent first.
533  */
534 
535 /**
536  * usb_lock_device_for_reset - cautiously acquire the lock for a usb device structure
537  * @udev: device that's being locked
538  * @iface: interface bound to the driver making the request (optional)
539  *
540  * Attempts to acquire the device lock, but fails if the device is
541  * NOTATTACHED or SUSPENDED, or if iface is specified and the interface
542  * is neither BINDING nor BOUND.  Rather than sleeping to wait for the
543  * lock, the routine polls repeatedly.  This is to prevent deadlock with
544  * disconnect; in some drivers (such as usb-storage) the disconnect()
545  * or suspend() method will block waiting for a device reset to complete.
546  *
547  * Returns a negative error code for failure, otherwise 0.
548  */
549 int usb_lock_device_for_reset(struct usb_device *udev,
550                               const struct usb_interface *iface)
551 {
552         unsigned long jiffies_expire = jiffies + HZ;
553 
554         if (udev->state == USB_STATE_NOTATTACHED)
555                 return -ENODEV;
556         if (udev->state == USB_STATE_SUSPENDED)
557                 return -EHOSTUNREACH;
558         if (iface && (iface->condition == USB_INTERFACE_UNBINDING ||
559                         iface->condition == USB_INTERFACE_UNBOUND))
560                 return -EINTR;
561 
562         while (usb_trylock_device(udev) != 0) {
563 
564                 /* If we can't acquire the lock after waiting one second,
565                  * we're probably deadlocked */
566                 if (time_after(jiffies, jiffies_expire))
567                         return -EBUSY;
568 
569                 msleep(15);
570                 if (udev->state == USB_STATE_NOTATTACHED)
571                         return -ENODEV;
572                 if (udev->state == USB_STATE_SUSPENDED)
573                         return -EHOSTUNREACH;
574                 if (iface && (iface->condition == USB_INTERFACE_UNBINDING ||
575                                 iface->condition == USB_INTERFACE_UNBOUND))
576                         return -EINTR;
577         }
578         return 0;
579 }
580 EXPORT_SYMBOL_GPL(usb_lock_device_for_reset);
581 
582 static struct usb_device *match_device(struct usb_device *dev,
583                                        u16 vendor_id, u16 product_id)
584 {
585         struct usb_device *ret_dev = NULL;
586         int child;
587 
588         dev_dbg(&dev->dev, "check for vendor %04x, product %04x ...\n",
589             le16_to_cpu(dev->descriptor.idVendor),
590             le16_to_cpu(dev->descriptor.idProduct));
591 
592         /* see if this device matches */
593         if ((vendor_id == le16_to_cpu(dev->descriptor.idVendor)) &&
594             (product_id == le16_to_cpu(dev->descriptor.idProduct))) {
595                 dev_dbg(&dev->dev, "matched this device!\n");
596                 ret_dev = usb_get_dev(dev);
597                 goto exit;
598         }
599 
600         /* look through all of the children of this device */
601         for (child = 0; child < dev->maxchild; ++child) {
602                 if (dev->children[child]) {
603                         usb_lock_device(dev->children[child]);
604                         ret_dev = match_device(dev->children[child],
605                                                vendor_id, product_id);
606                         usb_unlock_device(dev->children[child]);
607                         if (ret_dev)
608                                 goto exit;
609                 }
610         }
611 exit:
612         return ret_dev;
613 }
614 
615 /**
616  * usb_find_device - find a specific usb device in the system
617  * @vendor_id: the vendor id of the device to find
618  * @product_id: the product id of the device to find
619  *
620  * Returns a pointer to a struct usb_device if such a specified usb
621  * device is present in the system currently.  The usage count of the
622  * device will be incremented if a device is found.  Make sure to call
623  * usb_put_dev() when the caller is finished with the device.
624  *
625  * If a device with the specified vendor and product id is not found,
626  * NULL is returned.
627  */
628 struct usb_device *usb_find_device(u16 vendor_id, u16 product_id)
629 {
630         struct list_head *buslist;
631         struct usb_bus *bus;
632         struct usb_device *dev = NULL;
633 
634         mutex_lock(&usb_bus_list_lock);
635         for (buslist = usb_bus_list.next;
636              buslist != &usb_bus_list;
637              buslist = buslist->next) {
638                 bus = container_of(buslist, struct usb_bus, bus_list);
639                 if (!bus->root_hub)
640                         continue;
641                 usb_lock_device(bus->root_hub);
642                 dev = match_device(bus->root_hub, vendor_id, product_id);
643                 usb_unlock_device(bus->root_hub);
644                 if (dev)
645                         goto exit;
646         }
647 exit:
648         mutex_unlock(&usb_bus_list_lock);
649         return dev;
650 }
651 
652 /**
653  * usb_get_current_frame_number - return current bus frame number
654  * @dev: the device whose bus is being queried
655  *
656  * Returns the current frame number for the USB host controller
657  * used with the given USB device.  This can be used when scheduling
658  * isochronous requests.
659  *
660  * Note that different kinds of host controller have different
661  * "scheduling horizons".  While one type might support scheduling only
662  * 32 frames into the future, others could support scheduling up to
663  * 1024 frames into the future.
664  */
665 int usb_get_current_frame_number(struct usb_device *dev)
666 {
667         return usb_hcd_get_frame_number(dev);
668 }
669 EXPORT_SYMBOL_GPL(usb_get_current_frame_number);
670 
671 /*-------------------------------------------------------------------*/
672 /*
673  * __usb_get_extra_descriptor() finds a descriptor of specific type in the
674  * extra field of the interface and endpoint descriptor structs.
675  */
676 
677 int __usb_get_extra_descriptor(char *buffer, unsigned size,
678                                unsigned char type, void **ptr)
679 {
680         struct usb_descriptor_header *header;
681 
682         while (size >= sizeof(struct usb_descriptor_header)) {
683                 header = (struct usb_descriptor_header *)buffer;
684 
685                 if (header->bLength < 2) {
686                         printk(KERN_ERR
687                                 "%s: bogus descriptor, type %d length %d\n",
688                                 usbcore_name,
689                                 header->bDescriptorType,
690                                 header->bLength);
691                         return -1;
692                 }
693 
694                 if (header->bDescriptorType == type) {
695                         *ptr = header;
696                         return 0;
697                 }
698 
699                 buffer += header->bLength;
700                 size -= header->bLength;
701         }
702         return -1;
703 }
704 EXPORT_SYMBOL_GPL(__usb_get_extra_descriptor);
705 
706 /**
707  * usb_buffer_alloc - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
708  * @dev: device the buffer will be used with
709  * @size: requested buffer size
710  * @mem_flags: affect whether allocation may block
711  * @dma: used to return DMA address of buffer
712  *
713  * Return value is either null (indicating no buffer could be allocated), or
714  * the cpu-space pointer to a buffer that may be used to perform DMA to the
715  * specified device.  Such cpu-space buffers are returned along with the DMA
716  * address (through the pointer provided).
717  *
718  * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
719  * to avoid behaviors like using "DMA bounce buffers", or thrashing IOMMU
720  * hardware during URB completion/resubmit.  The implementation varies between
721  * platforms, depending on details of how DMA will work to this device.
722  * Using these buffers also eliminates cacheline sharing problems on
723  * architectures where CPU caches are not DMA-coherent.  On systems without
724  * bus-snooping caches, these buffers are uncached.
725  *
726  * When the buffer is no longer used, free it with usb_buffer_free().
727  */
728 void *usb_buffer_alloc(struct usb_device *dev, size_t size, gfp_t mem_flags,
729                        dma_addr_t *dma)
730 {
731         if (!dev || !dev->bus)
732                 return NULL;
733         return hcd_buffer_alloc(dev->bus, size, mem_flags, dma);
734 }
735 EXPORT_SYMBOL_GPL(usb_buffer_alloc);
736 
737 /**
738  * usb_buffer_free - free memory allocated with usb_buffer_alloc()
739  * @dev: device the buffer was used with
740  * @size: requested buffer size
741  * @addr: CPU address of buffer
742  * @dma: DMA address of buffer
743  *
744  * This reclaims an I/O buffer, letting it be reused.  The memory must have
745  * been allocated using usb_buffer_alloc(), and the parameters must match
746  * those provided in that allocation request.
747  */
748 void usb_buffer_free(struct usb_device *dev, size_t size, void *addr,
749                      dma_addr_t dma)
750 {
751         if (!dev || !dev->bus)
752                 return;
753         if (!addr)
754                 return;
755         hcd_buffer_free(dev->bus, size, addr, dma);
756 }
757 EXPORT_SYMBOL_GPL(usb_buffer_free);
758 
759 /**
760  * usb_buffer_map - create DMA mapping(s) for an urb
761  * @urb: urb whose transfer_buffer/setup_packet will be mapped
762  *
763  * Return value is either null (indicating no buffer could be mapped), or
764  * the parameter.  URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP are
765  * added to urb->transfer_flags if the operation succeeds.  If the device
766  * is connected to this system through a non-DMA controller, this operation
767  * always succeeds.
768  *
769  * This call would normally be used for an urb which is reused, perhaps
770  * as the target of a large periodic transfer, with usb_buffer_dmasync()
771  * calls to synchronize memory and dma state.
772  *
773  * Reverse the effect of this call with usb_buffer_unmap().
774  */
775 #if 0
776 struct urb *usb_buffer_map(struct urb *urb)
777 {
778         struct usb_bus          *bus;
779         struct device           *controller;
780 
781         if (!urb
782                         || !urb->dev
783                         || !(bus = urb->dev->bus)
784                         || !(controller = bus->controller))
785                 return NULL;
786 
787         if (controller->dma_mask) {
788                 urb->transfer_dma = dma_map_single(controller,
789                         urb->transfer_buffer, urb->transfer_buffer_length,
790                         usb_pipein(urb->pipe)
791                                 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
792                 if (usb_pipecontrol(urb->pipe))
793                         urb->setup_dma = dma_map_single(controller,
794                                         urb->setup_packet,
795                                         sizeof(struct usb_ctrlrequest),
796                                         DMA_TO_DEVICE);
797         /* FIXME generic api broken like pci, can't report errors */
798         /* if (urb->transfer_dma == DMA_ADDR_INVALID) return 0; */
799         } else
800                 urb->transfer_dma = ~0;
801         urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP
802                                 | URB_NO_SETUP_DMA_MAP);
803         return urb;
804 }
805 EXPORT_SYMBOL_GPL(usb_buffer_map);
806 #endif  /*  0  */
807 
808 /* XXX DISABLED, no users currently.  If you wish to re-enable this
809  * XXX please determine whether the sync is to transfer ownership of
810  * XXX the buffer from device to cpu or vice verse, and thusly use the
811  * XXX appropriate _for_{cpu,device}() method.  -DaveM
812  */
813 #if 0
814 
815 /**
816  * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s)
817  * @urb: urb whose transfer_buffer/setup_packet will be synchronized
818  */
819 void usb_buffer_dmasync(struct urb *urb)
820 {
821         struct usb_bus          *bus;
822         struct device           *controller;
823 
824         if (!urb
825                         || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
826                         || !urb->dev
827                         || !(bus = urb->dev->bus)
828                         || !(controller = bus->controller))
829                 return;
830 
831         if (controller->dma_mask) {
832                 dma_sync_single_for_cpu(controller,
833                         urb->transfer_dma, urb->transfer_buffer_length,
834                         usb_pipein(urb->pipe)
835                                 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
836                 if (usb_pipecontrol(urb->pipe))
837                         dma_sync_single_for_cpu(controller,
838                                         urb->setup_dma,
839                                         sizeof(struct usb_ctrlrequest),
840                                         DMA_TO_DEVICE);
841         }
842 }
843 EXPORT_SYMBOL_GPL(usb_buffer_dmasync);
844 #endif
845 
846 /**
847  * usb_buffer_unmap - free DMA mapping(s) for an urb
848  * @urb: urb whose transfer_buffer will be unmapped
849  *
850  * Reverses the effect of usb_buffer_map().
851  */
852 #if 0
853 void usb_buffer_unmap(struct urb *urb)
854 {
855         struct usb_bus          *bus;
856         struct device           *controller;
857 
858         if (!urb
859                         || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
860                         || !urb->dev
861                         || !(bus = urb->dev->bus)
862                         || !(controller = bus->controller))
863                 return;
864 
865         if (controller->dma_mask) {
866                 dma_unmap_single(controller,
867                         urb->transfer_dma, urb->transfer_buffer_length,
868                         usb_pipein(urb->pipe)
869                                 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
870                 if (usb_pipecontrol(urb->pipe))
871                         dma_unmap_single(controller,
872                                         urb->setup_dma,
873                                         sizeof(struct usb_ctrlrequest),
874                                         DMA_TO_DEVICE);
875         }
876         urb->transfer_flags &= ~(URB_NO_TRANSFER_DMA_MAP
877                                 | URB_NO_SETUP_DMA_MAP);
878 }
879 EXPORT_SYMBOL_GPL(usb_buffer_unmap);
880 #endif  /*  0  */
881 
882 /**
883  * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
884  * @dev: device to which the scatterlist will be mapped
885  * @is_in: mapping transfer direction
886  * @sg: the scatterlist to map
887  * @nents: the number of entries in the scatterlist
888  *
889  * Return value is either < 0 (indicating no buffers could be mapped), or
890  * the number of DMA mapping array entries in the scatterlist.
891  *
892  * The caller is responsible for placing the resulting DMA addresses from
893  * the scatterlist into URB transfer buffer pointers, and for setting the
894  * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs.
895  *
896  * Top I/O rates come from queuing URBs, instead of waiting for each one
897  * to complete before starting the next I/O.   This is particularly easy
898  * to do with scatterlists.  Just allocate and submit one URB for each DMA
899  * mapping entry returned, stopping on the first error or when all succeed.
900  * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
901  *
902  * This call would normally be used when translating scatterlist requests,
903  * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
904  * may be able to coalesce mappings for improved I/O efficiency.
905  *
906  * Reverse the effect of this call with usb_buffer_unmap_sg().
907  */
908 int usb_buffer_map_sg(const struct usb_device *dev, int is_in,
909                       struct scatterlist *sg, int nents)
910 {
911         struct usb_bus          *bus;
912         struct device           *controller;
913 
914         if (!dev
915                         || !(bus = dev->bus)
916                         || !(controller = bus->controller)
917                         || !controller->dma_mask)
918                 return -1;
919 
920         /* FIXME generic api broken like pci, can't report errors */
921         return dma_map_sg(controller, sg, nents,
922                         is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
923 }
924 EXPORT_SYMBOL_GPL(usb_buffer_map_sg);
925 
926 /* XXX DISABLED, no users currently.  If you wish to re-enable this
927  * XXX please determine whether the sync is to transfer ownership of
928  * XXX the buffer from device to cpu or vice verse, and thusly use the
929  * XXX appropriate _for_{cpu,device}() method.  -DaveM
930  */
931 #if 0
932 
933 /**
934  * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s)
935  * @dev: device to which the scatterlist will be mapped
936  * @is_in: mapping transfer direction
937  * @sg: the scatterlist to synchronize
938  * @n_hw_ents: the positive return value from usb_buffer_map_sg
939  *
940  * Use this when you are re-using a scatterlist's data buffers for
941  * another USB request.
942  */
943 void usb_buffer_dmasync_sg(const struct usb_device *dev, int is_in,
944                            struct scatterlist *sg, int n_hw_ents)
945 {
946         struct usb_bus          *bus;
947         struct device           *controller;
948 
949         if (!dev
950                         || !(bus = dev->bus)
951                         || !(controller = bus->controller)
952                         || !controller->dma_mask)
953                 return;
954 
955         dma_sync_sg_for_cpu(controller, sg, n_hw_ents,
956                             is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
957 }
958 EXPORT_SYMBOL_GPL(usb_buffer_dmasync_sg);
959 #endif
960 
961 /**
962  * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
963  * @dev: device to which the scatterlist will be mapped
964  * @is_in: mapping transfer direction
965  * @sg: the scatterlist to unmap
966  * @n_hw_ents: the positive return value from usb_buffer_map_sg
967  *
968  * Reverses the effect of usb_buffer_map_sg().
969  */
970 void usb_buffer_unmap_sg(const struct usb_device *dev, int is_in,
971                          struct scatterlist *sg, int n_hw_ents)
972 {
973         struct usb_bus          *bus;
974         struct device           *controller;
975 
976         if (!dev
977                         || !(bus = dev->bus)
978                         || !(controller = bus->controller)
979                         || !controller->dma_mask)
980                 return;
981 
982         dma_unmap_sg(controller, sg, n_hw_ents,
983                         is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
984 }
985 EXPORT_SYMBOL_GPL(usb_buffer_unmap_sg);
986 
987 /* To disable USB, kernel command line is 'nousb' not 'usbcore.nousb' */
988 #ifdef MODULE
989 module_param(nousb, bool, 0444);
990 #else
991 core_param(nousb, nousb, bool, 0444);
992 #endif
993 
994 /*
995  * for external read access to <nousb>
996  */
997 int usb_disabled(void)
998 {
999         return nousb;
1000 }
1001 EXPORT_SYMBOL_GPL(usb_disabled);
1002 
1003 /*
1004  * Notifications of device and interface registration
1005  */
1006 static int usb_bus_notify(struct notifier_block *nb, unsigned long action,
1007                 void *data)
1008 {
1009         struct device *dev = data;
1010 
1011         switch (action) {
1012         case BUS_NOTIFY_ADD_DEVICE:
1013                 if (dev->type == &usb_device_type)
1014                         (void) usb_create_sysfs_dev_files(to_usb_device(dev));
1015                 else if (dev->type == &usb_if_device_type)
1016                         (void) usb_create_sysfs_intf_files(
1017                                         to_usb_interface(dev));
1018                 break;
1019 
1020         case BUS_NOTIFY_DEL_DEVICE:
1021                 if (dev->type == &usb_device_type)
1022                         usb_remove_sysfs_dev_files(to_usb_device(dev));
1023                 else if (dev->type == &usb_if_device_type)
1024                         usb_remove_sysfs_intf_files(to_usb_interface(dev));
1025                 break;
1026         }
1027         return 0;
1028 }
1029 
1030 static struct notifier_block usb_bus_nb = {
1031         .notifier_call = usb_bus_notify,
1032 };
1033 
1034 struct dentry *usb_debug_root;
1035 EXPORT_SYMBOL_GPL(usb_debug_root);
1036 
1037 struct dentry *usb_debug_devices;
1038 
1039 static int usb_debugfs_init(void)
1040 {
1041         usb_debug_root = debugfs_create_dir("usb", NULL);
1042         if (!usb_debug_root)
1043                 return -ENOENT;
1044 
1045         usb_debug_devices = debugfs_create_file("devices", 0444,
1046                                                 usb_debug_root, NULL,
1047                                                 &usbfs_devices_fops);
1048         if (!usb_debug_devices) {
1049                 debugfs_remove(usb_debug_root);
1050                 usb_debug_root = NULL;
1051                 return -ENOENT;
1052         }
1053 
1054         return 0;
1055 }
1056 
1057 static void usb_debugfs_cleanup(void)
1058 {
1059         debugfs_remove(usb_debug_devices);
1060         debugfs_remove(usb_debug_root);
1061 }
1062 
1063 /*
1064  * Init
1065  */
1066 static int __init usb_init(void)
1067 {
1068         int retval;
1069         if (nousb) {
1070                 pr_info("%s: USB support disabled\n", usbcore_name);
1071                 return 0;
1072         }
1073 
1074         retval = usb_debugfs_init();
1075         if (retval)
1076                 goto out;
1077 
1078         retval = ksuspend_usb_init();
1079         if (retval)
1080                 goto out;
1081         retval = bus_register(&usb_bus_type);
1082         if (retval)
1083                 goto bus_register_failed;
1084         retval = bus_register_notifier(&usb_bus_type, &usb_bus_nb);
1085         if (retval)
1086                 goto bus_notifier_failed;
1087         retval = usb_major_init();
1088         if (retval)
1089                 goto major_init_failed;
1090         retval = usb_register(&usbfs_driver);
1091         if (retval)
1092                 goto driver_register_failed;
1093         retval = usb_devio_init();
1094         if (retval)
1095                 goto usb_devio_init_failed;
1096         retval = usbfs_init();
1097         if (retval)
1098                 goto fs_init_failed;
1099         retval = usb_hub_init();
1100         if (retval)
1101                 goto hub_init_failed;
1102         retval = usb_register_device_driver(&usb_generic_driver, THIS_MODULE);
1103         if (!retval)
1104                 goto out;
1105 
1106         usb_hub_cleanup();
1107 hub_init_failed:
1108         usbfs_cleanup();
1109 fs_init_failed:
1110         usb_devio_cleanup();
1111 usb_devio_init_failed:
1112         usb_deregister(&usbfs_driver);
1113 driver_register_failed:
1114         usb_major_cleanup();
1115 major_init_failed:
1116         bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
1117 bus_notifier_failed:
1118         bus_unregister(&usb_bus_type);
1119 bus_register_failed:
1120         ksuspend_usb_cleanup();
1121 out:
1122         return retval;
1123 }
1124 
1125 /*
1126  * Cleanup
1127  */
1128 static void __exit usb_exit(void)
1129 {
1130         /* This will matter if shutdown/reboot does exitcalls. */
1131         if (nousb)
1132                 return;
1133 
1134         usb_deregister_device_driver(&usb_generic_driver);
1135         usb_major_cleanup();
1136         usbfs_cleanup();
1137         usb_deregister(&usbfs_driver);
1138         usb_devio_cleanup();
1139         usb_hub_cleanup();
1140         bus_unregister_notifier(&usb_bus_type, &usb_bus_nb);
1141         bus_unregister(&usb_bus_type);
1142         ksuspend_usb_cleanup();
1143         usb_debugfs_cleanup();
1144 }
1145 
1146 subsys_initcall(usb_init);
1147 module_exit(usb_exit);
1148 MODULE_LICENSE("GPL");
1149 
  This page was automatically generated by the LXR engine.