Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / usb / core / usb.c
blob1f7ef106d72d3cfcec6c6e62952b3ca8b2e815b0
1 /*
2 * drivers/usb/core/usb.c
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
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..
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.
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>
38 #include <asm/io.h>
39 #include <linux/scatterlist.h>
40 #include <linux/mm.h>
41 #include <linux/dma-mapping.h>
43 #include "hcd.h"
44 #include "usb.h"
47 const char *usbcore_name = "usbcore";
49 static int nousb; /* Disable USB when built into kernel image */
51 /* Workqueue for autosuspend and for remote wakeup of root hubs */
52 struct workqueue_struct *ksuspend_usb_wq;
54 #ifdef CONFIG_USB_SUSPEND
55 static int usb_autosuspend_delay = 2; /* Default delay value,
56 * in seconds */
57 module_param_named(autosuspend, usb_autosuspend_delay, int, 0644);
58 MODULE_PARM_DESC(autosuspend, "default autosuspend delay");
60 #else
61 #define usb_autosuspend_delay 0
62 #endif
65 /**
66 * usb_ifnum_to_if - get the interface object with a given interface number
67 * @dev: the device whose current configuration is considered
68 * @ifnum: the desired interface
70 * This walks the device descriptor for the currently active configuration
71 * and returns a pointer to the interface with that particular interface
72 * number, or null.
74 * Note that configuration descriptors are not required to assign interface
75 * numbers sequentially, so that it would be incorrect to assume that
76 * the first interface in that descriptor corresponds to interface zero.
77 * This routine helps device drivers avoid such mistakes.
78 * However, you should make sure that you do the right thing with any
79 * alternate settings available for this interfaces.
81 * Don't call this function unless you are bound to one of the interfaces
82 * on this device or you have locked the device!
84 struct usb_interface *usb_ifnum_to_if(const struct usb_device *dev,
85 unsigned ifnum)
87 struct usb_host_config *config = dev->actconfig;
88 int i;
90 if (!config)
91 return NULL;
92 for (i = 0; i < config->desc.bNumInterfaces; i++)
93 if (config->interface[i]->altsetting[0]
94 .desc.bInterfaceNumber == ifnum)
95 return config->interface[i];
97 return NULL;
99 EXPORT_SYMBOL_GPL(usb_ifnum_to_if);
102 <<<<<<< HEAD:drivers/usb/core/usb.c
103 * usb_altnum_to_altsetting - get the altsetting structure with a given
104 * alternate setting number.
105 =======
106 * usb_altnum_to_altsetting - get the altsetting structure with a given alternate setting number.
107 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/usb/core/usb.c
108 * @intf: the interface containing the altsetting in question
109 * @altnum: the desired alternate setting number
111 * This searches the altsetting array of the specified interface for
112 * an entry with the correct bAlternateSetting value and returns a pointer
113 * to that entry, or null.
115 * Note that altsettings need not be stored sequentially by number, so
116 * it would be incorrect to assume that the first altsetting entry in
117 * the array corresponds to altsetting zero. This routine helps device
118 * drivers avoid such mistakes.
120 * Don't call this function unless you are bound to the intf interface
121 * or you have locked the device!
123 struct usb_host_interface *usb_altnum_to_altsetting(
124 const struct usb_interface *intf,
125 unsigned int altnum)
127 int i;
129 for (i = 0; i < intf->num_altsetting; i++) {
130 if (intf->altsetting[i].desc.bAlternateSetting == altnum)
131 return &intf->altsetting[i];
133 return NULL;
135 EXPORT_SYMBOL_GPL(usb_altnum_to_altsetting);
137 struct find_interface_arg {
138 int minor;
139 struct usb_interface *interface;
142 static int __find_interface(struct device *dev, void *data)
144 struct find_interface_arg *arg = data;
145 struct usb_interface *intf;
147 /* can't look at usb devices, only interfaces */
148 if (is_usb_device(dev))
149 return 0;
151 intf = to_usb_interface(dev);
152 if (intf->minor != -1 && intf->minor == arg->minor) {
153 arg->interface = intf;
154 return 1;
156 return 0;
160 * usb_find_interface - find usb_interface pointer for driver and device
161 * @drv: the driver whose current configuration is considered
162 * @minor: the minor number of the desired device
164 * This walks the driver device list and returns a pointer to the interface
165 * with the matching minor. Note, this only works for devices that share the
166 * USB major number.
168 struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor)
170 struct find_interface_arg argb;
171 int retval;
173 argb.minor = minor;
174 argb.interface = NULL;
175 /* eat the error, it will be in argb.interface */
176 retval = driver_for_each_device(&drv->drvwrap.driver, NULL, &argb,
177 __find_interface);
178 return argb.interface;
180 EXPORT_SYMBOL_GPL(usb_find_interface);
183 * usb_release_dev - free a usb device structure when all users of it are finished.
184 * @dev: device that's been disconnected
186 * Will be called only by the device core when all users of this usb device are
187 * done.
189 static void usb_release_dev(struct device *dev)
191 struct usb_device *udev;
193 udev = to_usb_device(dev);
195 usb_destroy_configuration(udev);
196 usb_put_hcd(bus_to_hcd(udev->bus));
197 kfree(udev->product);
198 kfree(udev->manufacturer);
199 kfree(udev->serial);
200 kfree(udev);
203 #ifdef CONFIG_HOTPLUG
204 static int usb_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
206 struct usb_device *usb_dev;
208 usb_dev = to_usb_device(dev);
210 if (add_uevent_var(env, "BUSNUM=%03d", usb_dev->bus->busnum))
211 return -ENOMEM;
213 if (add_uevent_var(env, "DEVNUM=%03d", usb_dev->devnum))
214 return -ENOMEM;
216 return 0;
219 #else
221 static int usb_dev_uevent(struct device *dev, struct kobj_uevent_env *env)
223 return -ENODEV;
225 #endif /* CONFIG_HOTPLUG */
227 struct device_type usb_device_type = {
228 .name = "usb_device",
229 .release = usb_release_dev,
230 .uevent = usb_dev_uevent,
233 #ifdef CONFIG_PM
235 static int ksuspend_usb_init(void)
237 /* This workqueue is supposed to be both freezable and
238 * singlethreaded. Its job doesn't justify running on more
239 * than one CPU.
241 <<<<<<< HEAD:drivers/usb/core/usb.c
242 ksuspend_usb_wq = create_singlethread_workqueue("ksuspend_usbd");
243 =======
244 ksuspend_usb_wq = create_freezeable_workqueue("ksuspend_usbd");
245 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/usb/core/usb.c
246 if (!ksuspend_usb_wq)
247 return -ENOMEM;
248 return 0;
251 static void ksuspend_usb_cleanup(void)
253 destroy_workqueue(ksuspend_usb_wq);
256 #else
258 #define ksuspend_usb_init() 0
259 #define ksuspend_usb_cleanup() do {} while (0)
261 #endif /* CONFIG_PM */
264 /* Returns 1 if @usb_bus is WUSB, 0 otherwise */
265 static unsigned usb_bus_is_wusb(struct usb_bus *bus)
267 struct usb_hcd *hcd = container_of(bus, struct usb_hcd, self);
268 return hcd->wireless;
273 * usb_alloc_dev - usb device constructor (usbcore-internal)
274 * @parent: hub to which device is connected; null to allocate a root hub
275 * @bus: bus used to access the device
276 * @port1: one-based index of port; ignored for root hubs
277 * Context: !in_interrupt()
279 * Only hub drivers (including virtual root hub drivers for host
280 * controllers) should ever call this.
282 * This call may not be used in a non-sleeping context.
284 struct usb_device *usb_alloc_dev(struct usb_device *parent,
285 struct usb_bus *bus, unsigned port1)
287 struct usb_device *dev;
288 struct usb_hcd *usb_hcd = container_of(bus, struct usb_hcd, self);
289 unsigned root_hub = 0;
291 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
292 if (!dev)
293 return NULL;
295 if (!usb_get_hcd(bus_to_hcd(bus))) {
296 kfree(dev);
297 return NULL;
300 device_initialize(&dev->dev);
301 dev->dev.bus = &usb_bus_type;
302 dev->dev.type = &usb_device_type;
303 dev->dev.dma_mask = bus->controller->dma_mask;
304 set_dev_node(&dev->dev, dev_to_node(bus->controller));
305 dev->state = USB_STATE_ATTACHED;
306 atomic_set(&dev->urbnum, 0);
308 INIT_LIST_HEAD(&dev->ep0.urb_list);
309 dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;
310 dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;
311 /* ep0 maxpacket comes later, from device descriptor */
312 usb_enable_endpoint(dev, &dev->ep0);
313 dev->can_submit = 1;
315 /* Save readable and stable topology id, distinguishing devices
316 * by location for diagnostics, tools, driver model, etc. The
317 * string is a path along hub ports, from the root. Each device's
318 * dev->devpath will be stable until USB is re-cabled, and hubs
319 * are often labeled with these port numbers. The bus_id isn't
320 * as stable: bus->busnum changes easily from modprobe order,
321 * cardbus or pci hotplugging, and so on.
323 if (unlikely(!parent)) {
324 dev->devpath[0] = '0';
326 dev->dev.parent = bus->controller;
327 sprintf(&dev->dev.bus_id[0], "usb%d", bus->busnum);
328 root_hub = 1;
329 } else {
330 /* match any labeling on the hubs; it's one-based */
331 if (parent->devpath[0] == '0')
332 snprintf(dev->devpath, sizeof dev->devpath,
333 "%d", port1);
334 else
335 snprintf(dev->devpath, sizeof dev->devpath,
336 "%s.%d", parent->devpath, port1);
338 dev->dev.parent = &parent->dev;
339 sprintf(&dev->dev.bus_id[0], "%d-%s",
340 bus->busnum, dev->devpath);
342 /* hub driver sets up TT records */
345 dev->portnum = port1;
346 dev->bus = bus;
347 dev->parent = parent;
348 INIT_LIST_HEAD(&dev->filelist);
350 #ifdef CONFIG_PM
351 mutex_init(&dev->pm_mutex);
352 INIT_DELAYED_WORK(&dev->autosuspend, usb_autosuspend_work);
353 dev->autosuspend_delay = usb_autosuspend_delay * HZ;
354 dev->connect_time = jiffies;
355 dev->active_duration = -jiffies;
356 #endif
357 if (root_hub) /* Root hub always ok [and always wired] */
358 dev->authorized = 1;
359 else {
360 dev->authorized = usb_hcd->authorized_default;
361 dev->wusb = usb_bus_is_wusb(bus)? 1 : 0;
363 return dev;
367 * usb_get_dev - increments the reference count of the usb device structure
368 * @dev: the device being referenced
370 * Each live reference to a device should be refcounted.
372 * Drivers for USB interfaces should normally record such references in
373 * their probe() methods, when they bind to an interface, and release
374 * them by calling usb_put_dev(), in their disconnect() methods.
376 * A pointer to the device with the incremented reference counter is returned.
378 struct usb_device *usb_get_dev(struct usb_device *dev)
380 if (dev)
381 get_device(&dev->dev);
382 return dev;
384 EXPORT_SYMBOL_GPL(usb_get_dev);
387 * usb_put_dev - release a use of the usb device structure
388 * @dev: device that's been disconnected
390 * Must be called when a user of a device is finished with it. When the last
391 * user of the device calls this function, the memory of the device is freed.
393 void usb_put_dev(struct usb_device *dev)
395 if (dev)
396 put_device(&dev->dev);
398 EXPORT_SYMBOL_GPL(usb_put_dev);
401 * usb_get_intf - increments the reference count of the usb interface structure
402 * @intf: the interface being referenced
404 * Each live reference to a interface must be refcounted.
406 * Drivers for USB interfaces should normally record such references in
407 * their probe() methods, when they bind to an interface, and release
408 * them by calling usb_put_intf(), in their disconnect() methods.
410 * A pointer to the interface with the incremented reference counter is
411 * returned.
413 struct usb_interface *usb_get_intf(struct usb_interface *intf)
415 if (intf)
416 get_device(&intf->dev);
417 return intf;
419 EXPORT_SYMBOL_GPL(usb_get_intf);
422 * usb_put_intf - release a use of the usb interface structure
423 * @intf: interface that's been decremented
425 * Must be called when a user of an interface is finished with it. When the
426 * last user of the interface calls this function, the memory of the interface
427 * is freed.
429 void usb_put_intf(struct usb_interface *intf)
431 if (intf)
432 put_device(&intf->dev);
434 EXPORT_SYMBOL_GPL(usb_put_intf);
436 /* USB device locking
438 * USB devices and interfaces are locked using the semaphore in their
439 * embedded struct device. The hub driver guarantees that whenever a
440 * device is connected or disconnected, drivers are called with the
441 * USB device locked as well as their particular interface.
443 * Complications arise when several devices are to be locked at the same
444 * time. Only hub-aware drivers that are part of usbcore ever have to
445 * do this; nobody else needs to worry about it. The rule for locking
446 * is simple:
448 * When locking both a device and its parent, always lock the
449 * the parent first.
453 <<<<<<< HEAD:drivers/usb/core/usb.c
454 * usb_lock_device_for_reset - cautiously acquire the lock for a
455 * usb device structure
456 =======
457 * usb_lock_device_for_reset - cautiously acquire the lock for a usb device structure
458 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/usb/core/usb.c
459 * @udev: device that's being locked
460 * @iface: interface bound to the driver making the request (optional)
462 * Attempts to acquire the device lock, but fails if the device is
463 * NOTATTACHED or SUSPENDED, or if iface is specified and the interface
464 * is neither BINDING nor BOUND. Rather than sleeping to wait for the
465 * lock, the routine polls repeatedly. This is to prevent deadlock with
466 * disconnect; in some drivers (such as usb-storage) the disconnect()
467 * or suspend() method will block waiting for a device reset to complete.
469 * Returns a negative error code for failure, otherwise 1 or 0 to indicate
470 * that the device will or will not have to be unlocked. (0 can be
471 * returned when an interface is given and is BINDING, because in that
472 * case the driver already owns the device lock.)
474 int usb_lock_device_for_reset(struct usb_device *udev,
475 const struct usb_interface *iface)
477 unsigned long jiffies_expire = jiffies + HZ;
479 if (udev->state == USB_STATE_NOTATTACHED)
480 return -ENODEV;
481 if (udev->state == USB_STATE_SUSPENDED)
482 return -EHOSTUNREACH;
483 if (iface) {
484 switch (iface->condition) {
485 case USB_INTERFACE_BINDING:
486 return 0;
487 case USB_INTERFACE_BOUND:
488 break;
489 default:
490 return -EINTR;
494 while (usb_trylock_device(udev) != 0) {
496 /* If we can't acquire the lock after waiting one second,
497 * we're probably deadlocked */
498 if (time_after(jiffies, jiffies_expire))
499 return -EBUSY;
501 msleep(15);
502 if (udev->state == USB_STATE_NOTATTACHED)
503 return -ENODEV;
504 if (udev->state == USB_STATE_SUSPENDED)
505 return -EHOSTUNREACH;
506 if (iface && iface->condition != USB_INTERFACE_BOUND)
507 return -EINTR;
509 return 1;
511 EXPORT_SYMBOL_GPL(usb_lock_device_for_reset);
513 static struct usb_device *match_device(struct usb_device *dev,
514 u16 vendor_id, u16 product_id)
516 struct usb_device *ret_dev = NULL;
517 int child;
519 dev_dbg(&dev->dev, "check for vendor %04x, product %04x ...\n",
520 le16_to_cpu(dev->descriptor.idVendor),
521 le16_to_cpu(dev->descriptor.idProduct));
523 /* see if this device matches */
524 if ((vendor_id == le16_to_cpu(dev->descriptor.idVendor)) &&
525 (product_id == le16_to_cpu(dev->descriptor.idProduct))) {
526 dev_dbg(&dev->dev, "matched this device!\n");
527 ret_dev = usb_get_dev(dev);
528 goto exit;
531 /* look through all of the children of this device */
532 for (child = 0; child < dev->maxchild; ++child) {
533 if (dev->children[child]) {
534 usb_lock_device(dev->children[child]);
535 ret_dev = match_device(dev->children[child],
536 vendor_id, product_id);
537 usb_unlock_device(dev->children[child]);
538 if (ret_dev)
539 goto exit;
542 exit:
543 return ret_dev;
547 * usb_find_device - find a specific usb device in the system
548 * @vendor_id: the vendor id of the device to find
549 * @product_id: the product id of the device to find
551 * Returns a pointer to a struct usb_device if such a specified usb
552 * device is present in the system currently. The usage count of the
553 * device will be incremented if a device is found. Make sure to call
554 * usb_put_dev() when the caller is finished with the device.
556 * If a device with the specified vendor and product id is not found,
557 * NULL is returned.
559 struct usb_device *usb_find_device(u16 vendor_id, u16 product_id)
561 struct list_head *buslist;
562 struct usb_bus *bus;
563 struct usb_device *dev = NULL;
565 mutex_lock(&usb_bus_list_lock);
566 for (buslist = usb_bus_list.next;
567 buslist != &usb_bus_list;
568 buslist = buslist->next) {
569 bus = container_of(buslist, struct usb_bus, bus_list);
570 if (!bus->root_hub)
571 continue;
572 usb_lock_device(bus->root_hub);
573 dev = match_device(bus->root_hub, vendor_id, product_id);
574 usb_unlock_device(bus->root_hub);
575 if (dev)
576 goto exit;
578 exit:
579 mutex_unlock(&usb_bus_list_lock);
580 return dev;
584 * usb_get_current_frame_number - return current bus frame number
585 * @dev: the device whose bus is being queried
587 * Returns the current frame number for the USB host controller
588 * used with the given USB device. This can be used when scheduling
589 * isochronous requests.
591 * Note that different kinds of host controller have different
592 * "scheduling horizons". While one type might support scheduling only
593 * 32 frames into the future, others could support scheduling up to
594 * 1024 frames into the future.
596 int usb_get_current_frame_number(struct usb_device *dev)
598 return usb_hcd_get_frame_number(dev);
600 EXPORT_SYMBOL_GPL(usb_get_current_frame_number);
602 /*-------------------------------------------------------------------*/
604 * __usb_get_extra_descriptor() finds a descriptor of specific type in the
605 * extra field of the interface and endpoint descriptor structs.
608 int __usb_get_extra_descriptor(char *buffer, unsigned size,
609 unsigned char type, void **ptr)
611 struct usb_descriptor_header *header;
613 while (size >= sizeof(struct usb_descriptor_header)) {
614 header = (struct usb_descriptor_header *)buffer;
616 if (header->bLength < 2) {
617 printk(KERN_ERR
618 "%s: bogus descriptor, type %d length %d\n",
619 usbcore_name,
620 header->bDescriptorType,
621 header->bLength);
622 return -1;
625 if (header->bDescriptorType == type) {
626 *ptr = header;
627 return 0;
630 buffer += header->bLength;
631 size -= header->bLength;
633 return -1;
635 EXPORT_SYMBOL_GPL(__usb_get_extra_descriptor);
638 * usb_buffer_alloc - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
639 * @dev: device the buffer will be used with
640 * @size: requested buffer size
641 * @mem_flags: affect whether allocation may block
642 * @dma: used to return DMA address of buffer
644 * Return value is either null (indicating no buffer could be allocated), or
645 * the cpu-space pointer to a buffer that may be used to perform DMA to the
646 * specified device. Such cpu-space buffers are returned along with the DMA
647 * address (through the pointer provided).
649 * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
650 * to avoid behaviors like using "DMA bounce buffers", or thrashing IOMMU
651 * hardware during URB completion/resubmit. The implementation varies between
652 * platforms, depending on details of how DMA will work to this device.
653 * Using these buffers also eliminates cacheline sharing problems on
654 * architectures where CPU caches are not DMA-coherent. On systems without
655 * bus-snooping caches, these buffers are uncached.
657 * When the buffer is no longer used, free it with usb_buffer_free().
659 void *usb_buffer_alloc(struct usb_device *dev, size_t size, gfp_t mem_flags,
660 dma_addr_t *dma)
662 if (!dev || !dev->bus)
663 return NULL;
664 return hcd_buffer_alloc(dev->bus, size, mem_flags, dma);
666 EXPORT_SYMBOL_GPL(usb_buffer_alloc);
669 * usb_buffer_free - free memory allocated with usb_buffer_alloc()
670 * @dev: device the buffer was used with
671 * @size: requested buffer size
672 * @addr: CPU address of buffer
673 * @dma: DMA address of buffer
675 * This reclaims an I/O buffer, letting it be reused. The memory must have
676 * been allocated using usb_buffer_alloc(), and the parameters must match
677 * those provided in that allocation request.
679 void usb_buffer_free(struct usb_device *dev, size_t size, void *addr,
680 dma_addr_t dma)
682 if (!dev || !dev->bus)
683 return;
684 if (!addr)
685 return;
686 hcd_buffer_free(dev->bus, size, addr, dma);
688 EXPORT_SYMBOL_GPL(usb_buffer_free);
691 * usb_buffer_map - create DMA mapping(s) for an urb
692 * @urb: urb whose transfer_buffer/setup_packet will be mapped
694 * Return value is either null (indicating no buffer could be mapped), or
695 * the parameter. URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP are
696 * added to urb->transfer_flags if the operation succeeds. If the device
697 * is connected to this system through a non-DMA controller, this operation
698 * always succeeds.
700 * This call would normally be used for an urb which is reused, perhaps
701 * as the target of a large periodic transfer, with usb_buffer_dmasync()
702 * calls to synchronize memory and dma state.
704 * Reverse the effect of this call with usb_buffer_unmap().
706 #if 0
707 struct urb *usb_buffer_map(struct urb *urb)
709 struct usb_bus *bus;
710 struct device *controller;
712 if (!urb
713 || !urb->dev
714 || !(bus = urb->dev->bus)
715 || !(controller = bus->controller))
716 return NULL;
718 if (controller->dma_mask) {
719 urb->transfer_dma = dma_map_single(controller,
720 urb->transfer_buffer, urb->transfer_buffer_length,
721 usb_pipein(urb->pipe)
722 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
723 if (usb_pipecontrol(urb->pipe))
724 urb->setup_dma = dma_map_single(controller,
725 urb->setup_packet,
726 sizeof(struct usb_ctrlrequest),
727 DMA_TO_DEVICE);
728 /* FIXME generic api broken like pci, can't report errors */
729 /* if (urb->transfer_dma == DMA_ADDR_INVALID) return 0; */
730 } else
731 urb->transfer_dma = ~0;
732 urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP
733 | URB_NO_SETUP_DMA_MAP);
734 return urb;
736 EXPORT_SYMBOL_GPL(usb_buffer_map);
737 #endif /* 0 */
739 /* XXX DISABLED, no users currently. If you wish to re-enable this
740 * XXX please determine whether the sync is to transfer ownership of
741 * XXX the buffer from device to cpu or vice verse, and thusly use the
742 * XXX appropriate _for_{cpu,device}() method. -DaveM
744 #if 0
747 * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s)
748 * @urb: urb whose transfer_buffer/setup_packet will be synchronized
750 void usb_buffer_dmasync(struct urb *urb)
752 struct usb_bus *bus;
753 struct device *controller;
755 if (!urb
756 || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
757 || !urb->dev
758 || !(bus = urb->dev->bus)
759 || !(controller = bus->controller))
760 return;
762 if (controller->dma_mask) {
763 dma_sync_single(controller,
764 urb->transfer_dma, urb->transfer_buffer_length,
765 usb_pipein(urb->pipe)
766 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
767 if (usb_pipecontrol(urb->pipe))
768 dma_sync_single(controller,
769 urb->setup_dma,
770 sizeof(struct usb_ctrlrequest),
771 DMA_TO_DEVICE);
774 EXPORT_SYMBOL_GPL(usb_buffer_dmasync);
775 #endif
778 * usb_buffer_unmap - free DMA mapping(s) for an urb
779 * @urb: urb whose transfer_buffer will be unmapped
781 * Reverses the effect of usb_buffer_map().
783 #if 0
784 void usb_buffer_unmap(struct urb *urb)
786 struct usb_bus *bus;
787 struct device *controller;
789 if (!urb
790 || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
791 || !urb->dev
792 || !(bus = urb->dev->bus)
793 || !(controller = bus->controller))
794 return;
796 if (controller->dma_mask) {
797 dma_unmap_single(controller,
798 urb->transfer_dma, urb->transfer_buffer_length,
799 usb_pipein(urb->pipe)
800 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
801 if (usb_pipecontrol(urb->pipe))
802 dma_unmap_single(controller,
803 urb->setup_dma,
804 sizeof(struct usb_ctrlrequest),
805 DMA_TO_DEVICE);
807 urb->transfer_flags &= ~(URB_NO_TRANSFER_DMA_MAP
808 | URB_NO_SETUP_DMA_MAP);
810 EXPORT_SYMBOL_GPL(usb_buffer_unmap);
811 #endif /* 0 */
814 * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
815 * @dev: device to which the scatterlist will be mapped
816 * @is_in: mapping transfer direction
817 * @sg: the scatterlist to map
818 * @nents: the number of entries in the scatterlist
820 * Return value is either < 0 (indicating no buffers could be mapped), or
821 * the number of DMA mapping array entries in the scatterlist.
823 * The caller is responsible for placing the resulting DMA addresses from
824 * the scatterlist into URB transfer buffer pointers, and for setting the
825 * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs.
827 * Top I/O rates come from queuing URBs, instead of waiting for each one
828 * to complete before starting the next I/O. This is particularly easy
829 * to do with scatterlists. Just allocate and submit one URB for each DMA
830 * mapping entry returned, stopping on the first error or when all succeed.
831 * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
833 * This call would normally be used when translating scatterlist requests,
834 * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
835 * may be able to coalesce mappings for improved I/O efficiency.
837 * Reverse the effect of this call with usb_buffer_unmap_sg().
839 int usb_buffer_map_sg(const struct usb_device *dev, int is_in,
840 struct scatterlist *sg, int nents)
842 struct usb_bus *bus;
843 struct device *controller;
845 if (!dev
846 || !(bus = dev->bus)
847 || !(controller = bus->controller)
848 || !controller->dma_mask)
849 return -1;
851 /* FIXME generic api broken like pci, can't report errors */
852 return dma_map_sg(controller, sg, nents,
853 is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
855 EXPORT_SYMBOL_GPL(usb_buffer_map_sg);
857 /* XXX DISABLED, no users currently. If you wish to re-enable this
858 * XXX please determine whether the sync is to transfer ownership of
859 * XXX the buffer from device to cpu or vice verse, and thusly use the
860 * XXX appropriate _for_{cpu,device}() method. -DaveM
862 #if 0
865 * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s)
866 * @dev: device to which the scatterlist will be mapped
867 * @is_in: mapping transfer direction
868 * @sg: the scatterlist to synchronize
869 * @n_hw_ents: the positive return value from usb_buffer_map_sg
871 * Use this when you are re-using a scatterlist's data buffers for
872 * another USB request.
874 void usb_buffer_dmasync_sg(const struct usb_device *dev, int is_in,
875 struct scatterlist *sg, int n_hw_ents)
877 struct usb_bus *bus;
878 struct device *controller;
880 if (!dev
881 || !(bus = dev->bus)
882 || !(controller = bus->controller)
883 || !controller->dma_mask)
884 return;
886 dma_sync_sg(controller, sg, n_hw_ents,
887 is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
889 EXPORT_SYMBOL_GPL(usb_buffer_dmasync_sg);
890 #endif
893 * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
894 * @dev: device to which the scatterlist will be mapped
895 * @is_in: mapping transfer direction
896 * @sg: the scatterlist to unmap
897 * @n_hw_ents: the positive return value from usb_buffer_map_sg
899 * Reverses the effect of usb_buffer_map_sg().
901 void usb_buffer_unmap_sg(const struct usb_device *dev, int is_in,
902 struct scatterlist *sg, int n_hw_ents)
904 struct usb_bus *bus;
905 struct device *controller;
907 if (!dev
908 || !(bus = dev->bus)
909 || !(controller = bus->controller)
910 || !controller->dma_mask)
911 return;
913 dma_unmap_sg(controller, sg, n_hw_ents,
914 is_in ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
916 EXPORT_SYMBOL_GPL(usb_buffer_unmap_sg);
918 /* format to disable USB on kernel command line is: nousb */
919 __module_param_call("", nousb, param_set_bool, param_get_bool, &nousb, 0444);
922 * for external read access to <nousb>
924 int usb_disabled(void)
926 return nousb;
928 EXPORT_SYMBOL_GPL(usb_disabled);
931 * Init
933 static int __init usb_init(void)
935 int retval;
936 if (nousb) {
937 pr_info("%s: USB support disabled\n", usbcore_name);
938 return 0;
941 retval = ksuspend_usb_init();
942 if (retval)
943 goto out;
944 retval = bus_register(&usb_bus_type);
945 if (retval)
946 goto bus_register_failed;
947 retval = usb_host_init();
948 if (retval)
949 goto host_init_failed;
950 retval = usb_major_init();
951 if (retval)
952 goto major_init_failed;
953 retval = usb_register(&usbfs_driver);
954 if (retval)
955 goto driver_register_failed;
956 retval = usb_devio_init();
957 if (retval)
958 goto usb_devio_init_failed;
959 retval = usbfs_init();
960 if (retval)
961 goto fs_init_failed;
962 retval = usb_hub_init();
963 if (retval)
964 goto hub_init_failed;
965 retval = usb_register_device_driver(&usb_generic_driver, THIS_MODULE);
966 if (!retval)
967 goto out;
969 usb_hub_cleanup();
970 hub_init_failed:
971 usbfs_cleanup();
972 fs_init_failed:
973 usb_devio_cleanup();
974 usb_devio_init_failed:
975 usb_deregister(&usbfs_driver);
976 driver_register_failed:
977 usb_major_cleanup();
978 major_init_failed:
979 usb_host_cleanup();
980 host_init_failed:
981 bus_unregister(&usb_bus_type);
982 bus_register_failed:
983 ksuspend_usb_cleanup();
984 out:
985 return retval;
989 * Cleanup
991 static void __exit usb_exit(void)
993 /* This will matter if shutdown/reboot does exitcalls. */
994 if (nousb)
995 return;
997 usb_deregister_device_driver(&usb_generic_driver);
998 usb_major_cleanup();
999 usbfs_cleanup();
1000 usb_deregister(&usbfs_driver);
1001 usb_devio_cleanup();
1002 usb_hub_cleanup();
1003 usb_host_cleanup();
1004 bus_unregister(&usb_bus_type);
1005 ksuspend_usb_cleanup();
1008 subsys_initcall(usb_init);
1009 module_exit(usb_exit);
1010 MODULE_LICENSE("GPL");