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