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