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