Signal USB device arrivals to PnP manager
[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 "../miniport/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 id = (struct usb_device_id*)save_id;
408
409 /* proc_connectinfo in devio.c may call us with id == NULL. */
410 if (id == NULL)
411 return NULL;
412
413 intf = &interface->altsetting [interface->act_altsetting];
414 dev = interface_to_usbdev(interface);
415
416 /* It is important to check that id->driver_info is nonzero,
417 since an entry that is all zeroes except for a nonzero
418 id->driver_info is the way to create an entry that
419 indicates that the driver want to examine every
420 device and interface. */
421 for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
422 id->driver_info; id++) {
423
424 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
425 id->idVendor != dev->descriptor.idVendor)
426 continue;
427
428 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
429 id->idProduct != dev->descriptor.idProduct)
430 continue;
431
432 /* No need to test id->bcdDevice_lo != 0, since 0 is never
433 greater than any unsigned number. */
434 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
435 (id->bcdDevice_lo > dev->descriptor.bcdDevice))
436 continue;
437
438 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
439 (id->bcdDevice_hi < dev->descriptor.bcdDevice))
440 continue;
441
442 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
443 (id->bDeviceClass != dev->descriptor.bDeviceClass))
444 continue;
445
446 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
447 (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
448 continue;
449
450 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
451 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
452 continue;
453
454 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
455 (id->bInterfaceClass != intf->desc.bInterfaceClass))
456 continue;
457
458 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
459 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
460 continue;
461
462 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
463 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
464 continue;
465
466 return id;
467 }
468
469 return NULL;
470 }
471
472 /**
473 * usb_find_interface - find usb_interface pointer for driver and device
474 * @drv: the driver whose current configuration is considered
475 * @minor: the minor number of the desired device
476 *
477 * This walks the driver device list and returns a pointer to the interface
478 * with the matching minor. Note, this only works for devices that share the
479 * USB major number.
480 */
481 struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor)
482 {
483 struct list_head *entry;
484 struct device *dev;
485 struct usb_interface *intf;
486
487 list_for_each(entry, &drv->driver.devices) {
488 dev = container_of(entry, struct device, driver_list);
489
490 /* can't look at usb devices, only interfaces */
491 if (dev->driver == &usb_generic_driver)
492 continue;
493
494 intf = to_usb_interface(dev);
495 if (intf->minor == -1)
496 continue;
497 if (intf->minor == minor)
498 return intf;
499 }
500
501 /* no device found that matches */
502 return NULL;
503 }
504
505 static int usb_device_match (struct device *dev, struct device_driver *drv)
506 {
507 struct usb_interface *intf;
508 struct usb_driver *usb_drv;
509 const struct usb_device_id *id;
510
511 /* check for generic driver, which we don't match any device with */
512 if (drv == &usb_generic_driver)
513 return 0;
514
515 intf = to_usb_interface(dev);
516
517 usb_drv = to_usb_driver(drv);
518 id = usb_drv->id_table;
519
520 id = usb_match_id (intf, usb_drv->id_table);
521 if (id)
522 return 1;
523
524 return 0;
525 }
526
527
528 #ifdef CONFIG_HOTPLUG
529
530 /*
531 * USB hotplugging invokes what /proc/sys/kernel/hotplug says
532 * (normally /sbin/hotplug) when USB devices get added or removed.
533 *
534 * This invokes a user mode policy agent, typically helping to load driver
535 * or other modules, configure the device, and more. Drivers can provide
536 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
537 *
538 * We're called either from khubd (the typical case) or from root hub
539 * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle
540 * delays in event delivery. Use sysfs (and DEVPATH) to make sure the
541 * device (and this configuration!) are still present.
542 */
543 static int usb_hotplug (struct device *dev, char **envp, int num_envp,
544 char *buffer, int buffer_size)
545 {
546 struct usb_interface *intf;
547 struct usb_device *usb_dev;
548 char *scratch;
549 int i = 0;
550 int length = 0;
551
552 dbg ("%s", __FUNCTION__);
553
554 if (!dev)
555 return -ENODEV;
556
557 /* Must check driver_data here, as on remove driver is always NULL */
558 if ((dev->driver == &usb_generic_driver) ||
559 (dev->driver_data == &usb_generic_driver_data))
560 return 0;
561
562 intf = to_usb_interface(dev);
563 usb_dev = interface_to_usbdev (intf);
564
565 if (usb_dev->devnum < 0) {
566 dbg ("device already deleted ??");
567 return -ENODEV;
568 }
569 if (!usb_dev->bus) {
570 dbg ("bus already removed?");
571 return -ENODEV;
572 }
573
574 scratch = buffer;
575
576 #ifdef CONFIG_USB_DEVICEFS
577 /* If this is available, userspace programs can directly read
578 * all the device descriptors we don't tell them about. Or
579 * even act as usermode drivers.
580 *
581 * FIXME reduce hardwired intelligence here
582 */
583 envp [i++] = scratch;
584 length += snprintf (scratch, buffer_size - length,
585 "DEVICE=/proc/bus/usb/%03d/%03d",
586 usb_dev->bus->busnum, usb_dev->devnum);
587 if ((buffer_size - length <= 0) || (i >= num_envp))
588 return -ENOMEM;
589 ++length;
590 scratch += length;
591 #endif
592
593 /* per-device configurations are common */
594 envp [i++] = scratch;
595 length += snprintf (scratch, buffer_size - length, "PRODUCT=%x/%x/%x",
596 usb_dev->descriptor.idVendor,
597 usb_dev->descriptor.idProduct,
598 usb_dev->descriptor.bcdDevice);
599 if ((buffer_size - length <= 0) || (i >= num_envp))
600 return -ENOMEM;
601 ++length;
602 scratch += length;
603
604 /* class-based driver binding models */
605 envp [i++] = scratch;
606 length += snprintf (scratch, buffer_size - length, "TYPE=%d/%d/%d",
607 usb_dev->descriptor.bDeviceClass,
608 usb_dev->descriptor.bDeviceSubClass,
609 usb_dev->descriptor.bDeviceProtocol);
610 if ((buffer_size - length <= 0) || (i >= num_envp))
611 return -ENOMEM;
612 ++length;
613 scratch += length;
614
615 if (usb_dev->descriptor.bDeviceClass == 0) {
616 int alt = intf->act_altsetting;
617
618 /* 2.4 only exposed interface zero. in 2.5, hotplug
619 * agents are called for all interfaces, and can use
620 * $DEVPATH/bInterfaceNumber if necessary.
621 */
622 envp [i++] = scratch;
623 length += snprintf (scratch, buffer_size - length,
624 "INTERFACE=%d/%d/%d",
625 intf->altsetting[alt].desc.bInterfaceClass,
626 intf->altsetting[alt].desc.bInterfaceSubClass,
627 intf->altsetting[alt].desc.bInterfaceProtocol);
628 if ((buffer_size - length <= 0) || (i >= num_envp))
629 return -ENOMEM;
630 ++length;
631 scratch += length;
632
633 }
634 envp [i++] = 0;
635
636 return 0;
637 }
638
639 #else
640
641 static int usb_hotplug (struct device *dev, char **envp,
642 int num_envp, char *buffer, int buffer_size)
643 {
644 return -ENODEV;
645 }
646
647 #endif /* CONFIG_HOTPLUG */
648
649 /**
650 * usb_alloc_dev - allocate a usb device structure (usbcore-internal)
651 * @parent: hub to which device is connected
652 * @bus: bus used to access the device
653 * Context: !in_interrupt ()
654 *
655 * Only hub drivers (including virtual root hub drivers for host
656 * controllers) should ever call this.
657 *
658 * This call is synchronous, and may not be used in an interrupt context.
659 */
660 struct usb_device STDCALL *usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus)
661 {
662 struct usb_device *dev;
663
664 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
665 if (!dev)
666 return NULL;
667
668 memset(dev, 0, sizeof(*dev));
669
670 device_initialize(&dev->dev);
671 dev->state = USB_STATE_ATTACHED;
672
673 usb_bus_get(bus);
674
675 if (!parent)
676 dev->devpath [0] = '0';
677 dev->bus = bus;
678 dev->parent = parent;
679 INIT_LIST_HEAD(&dev->filelist);
680
681 init_MUTEX(&dev->serialize);
682
683 if (dev->bus->op->allocate)
684 dev->bus->op->allocate(dev);
685
686 return dev;
687 }
688
689 /**
690 * usb_get_dev - increments the reference count of the usb device structure
691 * @dev: the device being referenced
692 *
693 * Each live reference to a device should be refcounted.
694 *
695 * Drivers for USB interfaces should normally record such references in
696 * their probe() methods, when they bind to an interface, and release
697 * them by calling usb_put_dev(), in their disconnect() methods.
698 *
699 * A pointer to the device with the incremented reference counter is returned.
700 */
701 struct usb_device STDCALL *usb_get_dev (struct usb_device *dev)
702 {
703 struct device *tmp;
704
705 if (!dev)
706 return NULL;
707
708 tmp = get_device(&dev->dev);
709 if (tmp)
710 return to_usb_device(tmp);
711 else
712 return NULL;
713 }
714
715 /**
716 * usb_put_dev - release a use of the usb device structure
717 * @dev: device that's been disconnected
718 *
719 * Must be called when a user of a device is finished with it. When the last
720 * user of the device calls this function, the memory of the device is freed.
721 */
722 void STDCALL usb_put_dev(struct usb_device *dev)
723 {
724 if (dev)
725 put_device(&dev->dev);
726 }
727
728 /**
729 * usb_release_dev - free a usb device structure when all users of it are finished.
730 * @dev: device that's been disconnected
731 *
732 * Will be called only by the device core when all users of this usb device are
733 * done.
734 */
735 static void usb_release_dev(struct device *dev)
736 {
737 struct usb_device *udev;
738
739 udev = to_usb_device(dev);
740
741 if (udev->bus && udev->bus->op && udev->bus->op->deallocate)
742 udev->bus->op->deallocate(udev);
743 usb_destroy_configuration (udev);
744 usb_bus_put (udev->bus);
745 kfree (udev);
746 }
747
748
749 static struct usb_device *match_device(struct usb_device *dev,
750 u16 vendor_id, u16 product_id)
751 {
752 struct usb_device *ret_dev = NULL;
753 int child;
754
755 dbg("looking at vendor %d, product %d",
756 dev->descriptor.idVendor,
757 dev->descriptor.idProduct);
758
759 /* see if this device matches */
760 if ((dev->descriptor.idVendor == vendor_id) &&
761 (dev->descriptor.idProduct == product_id)) {
762 dbg ("found the device!");
763 ret_dev = usb_get_dev(dev);
764 goto exit;
765 }
766
767 /* look through all of the children of this device */
768 for (child = 0; child < dev->maxchild; ++child) {
769 if (dev->children[child]) {
770 ret_dev = match_device(dev->children[child],
771 vendor_id, product_id);
772 if (ret_dev)
773 goto exit;
774 }
775 }
776 exit:
777 return ret_dev;
778 }
779
780 /**
781 * usb_find_device - find a specific usb device in the system
782 * @vendor_id: the vendor id of the device to find
783 * @product_id: the product id of the device to find
784 *
785 * Returns a pointer to a struct usb_device if such a specified usb
786 * device is present in the system currently. The usage count of the
787 * device will be incremented if a device is found. Make sure to call
788 * usb_put_dev() when the caller is finished with the device.
789 *
790 * If a device with the specified vendor and product id is not found,
791 * NULL is returned.
792 */
793 struct usb_device *usb_find_device(u16 vendor_id, u16 product_id)
794 {
795 struct list_head *buslist;
796 struct usb_bus *bus;
797 struct usb_device *dev = NULL;
798
799 down(&usb_bus_list_lock);
800 for (buslist = usb_bus_list.next;
801 buslist != &usb_bus_list;
802 buslist = buslist->next) {
803 bus = container_of(buslist, struct usb_bus, bus_list);
804 dev = match_device(bus->root_hub, vendor_id, product_id);
805 if (dev)
806 goto exit;
807 }
808 exit:
809 up(&usb_bus_list_lock);
810 return dev;
811 }
812
813 /**
814 * usb_get_current_frame_number - return current bus frame number
815 * @dev: the device whose bus is being queried
816 *
817 * Returns the current frame number for the USB host controller
818 * used with the given USB device. This can be used when scheduling
819 * isochronous requests.
820 *
821 * Note that different kinds of host controller have different
822 * "scheduling horizons". While one type might support scheduling only
823 * 32 frames into the future, others could support scheduling up to
824 * 1024 frames into the future.
825 */
826 int usb_get_current_frame_number(struct usb_device *dev)
827 {
828 return dev->bus->op->get_frame_number (dev);
829 }
830
831 /*-------------------------------------------------------------------*/
832 /*
833 * __usb_get_extra_descriptor() finds a descriptor of specific type in the
834 * extra field of the interface and endpoint descriptor structs.
835 */
836
837 int __usb_get_extra_descriptor(char *buffer, unsigned size, unsigned char type, void **ptr)
838 {
839 struct usb_descriptor_header *header;
840
841 while (size >= sizeof(struct usb_descriptor_header)) {
842 header = (struct usb_descriptor_header *)buffer;
843
844 if (header->bLength < 2) {
845 err("invalid descriptor length of %d", header->bLength);
846 return -1;
847 }
848
849 if (header->bDescriptorType == type) {
850 *ptr = header;
851 return 0;
852 }
853
854 buffer += header->bLength;
855 size -= header->bLength;
856 }
857 return -1;
858 }
859
860 /**
861 * usb_disconnect - disconnect a device (usbcore-internal)
862 * @pdev: pointer to device being disconnected
863 * Context: !in_interrupt ()
864 *
865 * Something got disconnected. Get rid of it, and all of its children.
866 *
867 * Only hub drivers (including virtual root hub drivers for host
868 * controllers) should ever call this.
869 *
870 * This call is synchronous, and may not be used in an interrupt context.
871 */
872 void usb_disconnect(struct usb_device **pdev)
873 {
874 struct usb_device *dev = *pdev;
875 struct usb_bus *bus;
876 struct usb_operations *ops;
877 int i;
878
879 might_sleep ();
880
881 if (!dev) {
882 // pr_debug ("%s nodev\n", __FUNCTION__);
883 DPRINT ("%s nodev\n", __FUNCTION__);
884 return;
885 }
886 bus = dev->bus;
887 if (!bus) {
888 // pr_debug ("%s nobus\n", __FUNCTION__);
889 DPRINT ("%s nobus\n", __FUNCTION__);
890 return;
891 }
892 ops = bus->op;
893
894 *pdev = NULL;
895
896 /* mark the device as inactive, so any further urb submissions for
897 * this device will fail.
898 */
899 dev->state = USB_STATE_NOTATTACHED;
900
901 dev_info (&dev->dev, "USB disconnect, address %d\n", dev->devnum);
902
903 /* Free up all the children before we remove this device */
904 for (i = 0; i < USB_MAXCHILDREN; i++) {
905 struct usb_device **child = dev->children + i;
906 if (*child)
907 usb_disconnect(child);
908 }
909
910 /* disconnect() drivers from interfaces (a key side effect) */
911 dev_dbg (&dev->dev, "unregistering interfaces\n");
912 if (dev->actconfig) {
913 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
914 struct usb_interface *interface;
915
916 /* remove this interface */
917 interface = &dev->actconfig->interface[i];
918 device_unregister(&interface->dev);
919 }
920 }
921
922 /* deallocate hcd/hardware state */
923 if (ops->disable) {
924 void (*disable)(struct usb_device *, int) = ops->disable;
925
926 for (i = 0; i < 15; i++) {
927 disable (dev, i);
928 disable (dev, USB_DIR_IN | i);
929 }
930 }
931
932 dev_dbg (&dev->dev, "unregistering device\n");
933 /* Free the device number and remove the /proc/bus/usb entry */
934 if (dev->devnum > 0) {
935 clear_bit(dev->devnum, dev->bus->devmap.devicemap);
936 usbfs_remove_device(dev);
937 }
938 device_unregister(&dev->dev);
939
940 /* Decrement the reference count, it'll auto free everything when */
941 /* it hits 0 which could very well be now */
942 usb_put_dev(dev);
943 }
944
945 /**
946 * usb_connect - pick device address (usbcore-internal)
947 * @dev: newly detected device (in DEFAULT state)
948 *
949 * Picks a device address. It's up to the hub (or root hub) driver
950 * to handle and manage enumeration, starting from the DEFAULT state.
951 * Only hub drivers (including virtual root hub drivers for host
952 * controllers) should ever call this.
953 */
954 void STDCALL usb_connect(struct usb_device *dev)
955 {
956 int devnum;
957 // FIXME needs locking for SMP!!
958 /* why? this is called only from the hub thread,
959 * which hopefully doesn't run on multiple CPU's simultaneously 8-)
960 * ... it's also called from modprobe/rmmod/apmd threads as part
961 * of virtual root hub init/reinit. In the init case, the hub code
962 * won't have seen this, but not so for reinit ...
963 */
964 dev->descriptor.bMaxPacketSize0 = 8; /* Start off at 8 bytes */
965
966 /* Try to allocate the next devnum beginning at bus->devnum_next. */
967 devnum = find_next_zero_bit(dev->bus->devmap.devicemap, 128, dev->bus->devnum_next);
968 if (devnum >= 128)
969 devnum = find_next_zero_bit(dev->bus->devmap.devicemap, 128, 1);
970
971 dev->bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
972
973 if (devnum < 128) {
974 set_bit(devnum, dev->bus->devmap.devicemap);
975 dev->devnum = devnum;
976 }
977 }
978
979 /**
980 * usb_choose_address - pick device address (usbcore-internal)
981 * @dev: newly detected device (in DEFAULT state)
982 *
983 * Picks a device address. It's up to the hub (or root hub) driver
984 * to handle and manage enumeration, starting from the DEFAULT state.
985 * Only hub drivers (but not virtual root hub drivers for host
986 * controllers) should ever call this.
987 */
988 void usb_choose_address(struct usb_device *dev)
989 {
990 int devnum;
991 // FIXME needs locking for SMP!!
992 /* why? this is called only from the hub thread,
993 * which hopefully doesn't run on multiple CPU's simultaneously 8-)
994 */
995
996 /* Try to allocate the next devnum beginning at bus->devnum_next. */
997 devnum = find_next_zero_bit(dev->bus->devmap.devicemap, 128, dev->bus->devnum_next);
998 if (devnum >= 128)
999 devnum = find_next_zero_bit(dev->bus->devmap.devicemap, 128, 1);
1000
1001 dev->bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1002
1003 if (devnum < 128) {
1004 set_bit(devnum, dev->bus->devmap.devicemap);
1005 dev->devnum = devnum;
1006 }
1007 }
1008
1009 // hub-only!! ... and only exported for reset/reinit path.
1010 // otherwise used internally, for usb_new_device()
1011 int usb_set_address(struct usb_device *dev)
1012 {
1013 int retval;
1014
1015 if (dev->devnum == 0)
1016 return -EINVAL;
1017 if (dev->state != USB_STATE_DEFAULT && dev->state != USB_STATE_ADDRESS)
1018 return -EINVAL;
1019 retval = usb_control_msg(dev, usb_snddefctrl(dev), USB_REQ_SET_ADDRESS,
1020 0, dev->devnum, 0, NULL, 0, HZ * USB_CTRL_SET_TIMEOUT);
1021 if (retval == 0)
1022 dev->state = USB_STATE_ADDRESS;
1023 return retval;
1024 }
1025
1026
1027 /* improve on the default device description, if we can ... and
1028 * while we're at it, maybe show the vendor and product strings.
1029 */
1030 static void set_device_description (struct usb_device *dev)
1031 {
1032 void *buf;
1033 int mfgr = dev->descriptor.iManufacturer;
1034 int prod = dev->descriptor.iProduct;
1035 int vendor_id = dev->descriptor.idVendor;
1036 int product_id = dev->descriptor.idProduct;
1037 char *mfgr_str, *prod_str;
1038
1039 /* set default; keep it if there are no strings, or kmalloc fails */
1040 sprintf (dev->dev.name, "USB device %04x:%04x",
1041 vendor_id, product_id);
1042
1043 if (!(buf = kmalloc(256 * 2, GFP_KERNEL)))
1044 return;
1045
1046 prod_str = (char *) buf;
1047 mfgr_str = (char *) buf + 256;
1048
1049 if (prod && usb_string (dev, prod, prod_str, 256) > 0) {
1050 #ifdef DEBUG
1051 dev_printk (KERN_INFO, &dev->dev, "Product: %s\n", prod_str);
1052 #endif
1053 } else {
1054 prod_str = 0;
1055 }
1056
1057 if (mfgr && usb_string (dev, mfgr, mfgr_str, 256) > 0) {
1058 #ifdef DEBUG
1059 dev_printk (KERN_INFO, &dev->dev, "Manufacturer: %s\n", mfgr_str);
1060 #endif
1061 } else {
1062 mfgr_str = 0;
1063 }
1064
1065 /* much like pci ... describe as either:
1066 * - both strings: 'product descr (vendor descr)'
1067 * - product only: 'product descr (USB device vvvv:pppp)'
1068 * - vendor only: 'USB device vvvv:pppp (vendor descr)'
1069 * - neither string: 'USB device vvvv:pppp'
1070 */
1071
1072 if (prod_str && mfgr_str) {
1073
1074 snprintf(dev->dev.name, sizeof dev->dev.name,
1075 "%s (%s)", prod_str, mfgr_str);
1076 } else if (prod_str) {
1077 snprintf(dev->dev.name, sizeof dev->dev.name,
1078 "%s (USB device %04x:%04x)",
1079 prod_str, vendor_id, product_id);
1080
1081 } else if (mfgr_str) {
1082 snprintf(dev->dev.name, sizeof dev->dev.name,
1083 "USB device %04x:%04x (%s)",
1084 vendor_id, product_id, mfgr_str);
1085 }
1086 usbprintk("USB connected: %s\n",dev->dev.name);
1087 kfree(buf);
1088 }
1089
1090 /*
1091 * By the time we get here, we chose a new device address
1092 * and is in the default state. We need to identify the thing and
1093 * get the ball rolling..
1094 *
1095 * Returns 0 for success, != 0 for error.
1096 *
1097 * This call is synchronous, and may not be used in an interrupt context.
1098 *
1099 * Only hub drivers (including virtual root hub drivers for host
1100 * controllers) should ever call this.
1101 */
1102 #define NEW_DEVICE_RETRYS 2
1103 #define SET_ADDRESS_RETRYS 20
1104 int usb_new_device(struct usb_device *dev, struct device *parent)
1105 {
1106 int err = -EINVAL;
1107 int i;
1108 int j;
1109
1110 /*
1111 * Set the driver for the usb device to point to the "generic" driver.
1112 * This prevents the main usb device from being sent to the usb bus
1113 * probe function. Yes, it's a hack, but a nice one :)
1114 *
1115 * Do it asap, so more driver model stuff (like the device.h message
1116 * utilities) can be used in hcd submit/unlink code paths.
1117 */
1118 usb_generic_driver.bus = &usb_bus_type;
1119 dev->dev.parent = parent;
1120 dev->dev.driver = &usb_generic_driver;
1121 dev->dev.bus = &usb_bus_type;
1122 dev->dev.release = usb_release_dev;
1123 dev->dev.driver_data = &usb_generic_driver_data;
1124 usb_get_dev(dev);
1125 if (dev->dev.bus_id[0] == 0)
1126 sprintf (&dev->dev.bus_id[0], "%d-%s",
1127 dev->bus->busnum, dev->devpath);
1128
1129 /* dma masks come from the controller; readonly, except to hcd */
1130 dev->dev.dma_mask = parent->dma_mask;
1131
1132 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
1133 * it's fixed size except for full speed devices.
1134 */
1135 switch (dev->speed) {
1136 case USB_SPEED_HIGH: /* fixed at 64 */
1137 i = 64;
1138 break;
1139 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
1140 /* to determine the ep0 maxpacket size, read the first 8
1141 * bytes from the device descriptor to get bMaxPacketSize0;
1142 * then correct our initial (small) guess.
1143 */
1144 // FALLTHROUGH
1145 case USB_SPEED_LOW: /* fixed at 8 */
1146 i = 8;
1147 break;
1148 default:
1149 goto fail;
1150 }
1151 dev->epmaxpacketin [0] = i;
1152 dev->epmaxpacketout[0] = i;
1153
1154 for (i = 0; i < NEW_DEVICE_RETRYS; ++i) {
1155
1156 for (j = 0; j < SET_ADDRESS_RETRYS; ++j) {
1157 err = usb_set_address(dev);
1158 if (err >= 0)
1159 break;
1160 wait_ms(5);
1161 }
1162 if (err < 0) {
1163 dev_err(&dev->dev, "USB device not accepting new address=%d (error=%d)\n",
1164 dev->devnum, err);
1165 goto fail;
1166 }
1167
1168 wait_ms(10); /* Let the SET_ADDRESS settle */
1169
1170 /* high and low speed devices don't need this... */
1171 err = usb_get_descriptor(dev, USB_DT_DEVICE, 0, &dev->descriptor, 8);
1172 if (err >= 8)
1173 break;
1174 wait_ms(100);
1175 }
1176
1177 if (err < 8) {
1178 dev_err(&dev->dev, "device descriptor read/8, error %d\n", err);
1179 goto fail;
1180 }
1181 if (dev->speed == USB_SPEED_FULL) {
1182 //usb_disable_endpoint(dev, 0);
1183 usb_endpoint_running(dev, 0, 1);
1184 usb_endpoint_running(dev, 0, 0);
1185 dev->epmaxpacketin [0] = dev->descriptor.bMaxPacketSize0;
1186 dev->epmaxpacketout[0] = dev->descriptor.bMaxPacketSize0;
1187 }
1188
1189 /* USB device state == addressed ... still not usable */
1190
1191 err = usb_get_device_descriptor(dev);
1192 if (err < (signed)sizeof(dev->descriptor)) {
1193 dev_err(&dev->dev, "device descriptor read/all, error %d\n", err);
1194 goto fail;
1195 }
1196
1197 err = usb_get_configuration(dev);
1198 if (err < 0) {
1199 dev_err(&dev->dev, "can't read configurations, error %d\n",
1200 err);
1201 goto fail;
1202 }
1203
1204 /* we set the default configuration here */
1205 err = usb_set_configuration(dev, dev->config[0].desc.bConfigurationValue);
1206 if (err) {
1207 dev_err(&dev->dev, "failed to set device %d default configuration (error=%d)\n",
1208 dev->devnum, err);
1209 goto fail;
1210 }
1211
1212 /* USB device state == configured ... tell the world! */
1213
1214 dev_dbg(&dev->dev, "new device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
1215 dev->descriptor.iManufacturer, dev->descriptor.iProduct, dev->descriptor.iSerialNumber);
1216 set_device_description (dev);
1217
1218 #ifdef DEBUG
1219 if (dev->descriptor.iSerialNumber)
1220 usb_show_string(dev, "SerialNumber", dev->descriptor.iSerialNumber);
1221 #endif
1222 /* put into sysfs, with device and config specific files */
1223 err = device_add (&dev->dev);
1224 if (err)
1225 return err;
1226 usb_create_driverfs_dev_files (dev);
1227
1228 /* Register all of the interfaces for this device with the driver core.
1229 * Remember, interfaces get bound to drivers, not devices. */
1230 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1231 struct usb_interface *interface = &dev->actconfig->interface[i];
1232 struct usb_interface_descriptor *desc;
1233
1234 desc = &interface->altsetting [interface->act_altsetting].desc;
1235 interface->dev.parent = &dev->dev;
1236 interface->dev.driver = NULL;
1237 interface->dev.bus = &usb_bus_type;
1238 interface->dev.dma_mask = parent->dma_mask;
1239 sprintf (&interface->dev.bus_id[0], "%d-%s:%d",
1240 dev->bus->busnum, dev->devpath,
1241 desc->bInterfaceNumber);
1242 if (!desc->iInterface
1243 || usb_string (dev, desc->iInterface,
1244 interface->dev.name,
1245 sizeof interface->dev.name) <= 0) {
1246 /* typically devices won't bother with interface
1247 * descriptions; this is the normal case. an
1248 * interface's driver might describe it better.
1249 * (also: iInterface is per-altsetting ...)
1250 */
1251 sprintf (&interface->dev.name[0],
1252 "usb-%s-%s interface %d",
1253 dev->bus->bus_name, dev->devpath,
1254 desc->bInterfaceNumber);
1255 DPRINT1(".........................usb_new_device: %s\n", interface->dev.name);
1256 }
1257 dev_dbg (&dev->dev, "%s - registering interface %s\n", __FUNCTION__, interface->dev.bus_id);
1258 device_add (&interface->dev);
1259 usb_create_driverfs_intf_files (interface);
1260 }
1261 /* add a /proc/bus/usb entry */
1262 //usbfs_add_device(dev);
1263
1264 return 0;
1265 fail:
1266 dev->state = USB_STATE_DEFAULT;
1267 clear_bit(dev->devnum, dev->bus->devmap.devicemap);
1268 dev->devnum = -1;
1269 return err;
1270 }
1271
1272 /**
1273 * usb_buffer_alloc - allocate dma-consistent buffer for URB_NO_DMA_MAP
1274 * @dev: device the buffer will be used with
1275 * @size: requested buffer size
1276 * @mem_flags: affect whether allocation may block
1277 * @dma: used to return DMA address of buffer
1278 *
1279 * Return value is either null (indicating no buffer could be allocated), or
1280 * the cpu-space pointer to a buffer that may be used to perform DMA to the
1281 * specified device. Such cpu-space buffers are returned along with the DMA
1282 * address (through the pointer provided).
1283 *
1284 * These buffers are used with URB_NO_DMA_MAP set in urb->transfer_flags to
1285 * avoid behaviors like using "DMA bounce buffers", or tying down I/O mapping
1286 * hardware for long idle periods. The implementation varies between
1287 * platforms, depending on details of how DMA will work to this device.
1288 * Using these buffers also helps prevent cacheline sharing problems on
1289 * architectures where CPU caches are not DMA-coherent.
1290 *
1291 * When the buffer is no longer used, free it with usb_buffer_free().
1292 */
1293 void *usb_buffer_alloc (
1294 struct usb_device *dev,
1295 size_t size,
1296 int mem_flags,
1297 dma_addr_t *dma
1298 )
1299 {
1300 if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_alloc)
1301 return 0;
1302 return dev->bus->op->buffer_alloc (dev->bus, size, mem_flags, dma);
1303 }
1304
1305 /**
1306 * usb_buffer_free - free memory allocated with usb_buffer_alloc()
1307 * @dev: device the buffer was used with
1308 * @size: requested buffer size
1309 * @addr: CPU address of buffer
1310 * @dma: DMA address of buffer
1311 *
1312 * This reclaims an I/O buffer, letting it be reused. The memory must have
1313 * been allocated using usb_buffer_alloc(), and the parameters must match
1314 * those provided in that allocation request.
1315 */
1316 void usb_buffer_free (
1317 struct usb_device *dev,
1318 size_t size,
1319 void *addr,
1320 dma_addr_t dma
1321 )
1322 {
1323 if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_free)
1324 return;
1325 dev->bus->op->buffer_free (dev->bus, size, addr, dma);
1326 }
1327
1328 /**
1329 * usb_buffer_map - create DMA mapping(s) for an urb
1330 * @urb: urb whose transfer_buffer will be mapped
1331 *
1332 * Return value is either null (indicating no buffer could be mapped), or
1333 * the parameter. URB_NO_DMA_MAP is added to urb->transfer_flags if the
1334 * operation succeeds. If the device is connected to this system through
1335 * a non-DMA controller, this operation always succeeds.
1336 *
1337 * This call would normally be used for an urb which is reused, perhaps
1338 * as the target of a large periodic transfer, with usb_buffer_dmasync()
1339 * calls to synchronize memory and dma state. It may not be used for
1340 * control requests.
1341 *
1342 * Reverse the effect of this call with usb_buffer_unmap().
1343 */
1344 struct urb *usb_buffer_map (struct urb *urb)
1345 {
1346 struct usb_bus *bus;
1347 struct device *controller;
1348
1349 if (!urb
1350 || usb_pipecontrol (urb->pipe)
1351 || !urb->dev
1352 || !(bus = urb->dev->bus)
1353 || !(controller = bus->controller))
1354 return 0;
1355
1356 if (controller->dma_mask) {
1357 urb->transfer_dma = dma_map_single (controller,
1358 urb->transfer_buffer, urb->transfer_buffer_length,
1359 usb_pipein (urb->pipe)
1360 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1361 // FIXME generic api broken like pci, can't report errors
1362 // if (urb->transfer_dma == DMA_ADDR_INVALID) return 0;
1363 } else
1364 urb->transfer_dma = ~0;
1365 urb->transfer_flags |= URB_NO_DMA_MAP;
1366 return urb;
1367 }
1368
1369 /**
1370 * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s)
1371 * @urb: urb whose transfer_buffer will be synchronized
1372 */
1373 void usb_buffer_dmasync (struct urb *urb)
1374 {
1375 struct usb_bus *bus;
1376 struct device *controller;
1377
1378 if (!urb
1379 || !(urb->transfer_flags & URB_NO_DMA_MAP)
1380 || !urb->dev
1381 || !(bus = urb->dev->bus)
1382 || !(controller = bus->controller))
1383 return;
1384
1385 if (controller->dma_mask)
1386 dma_sync_single (controller,
1387 urb->transfer_dma, urb->transfer_buffer_length,
1388 usb_pipein (urb->pipe)
1389 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1390 }
1391
1392 /**
1393 * usb_buffer_unmap - free DMA mapping(s) for an urb
1394 * @urb: urb whose transfer_buffer will be unmapped
1395 *
1396 * Reverses the effect of usb_buffer_map().
1397 */
1398 void usb_buffer_unmap (struct urb *urb)
1399 {
1400 struct usb_bus *bus;
1401 struct device *controller;
1402
1403 if (!urb
1404 || !(urb->transfer_flags & URB_NO_DMA_MAP)
1405 || !urb->dev
1406 || !(bus = urb->dev->bus)
1407 || !(controller = bus->controller))
1408 return;
1409
1410 if (controller->dma_mask)
1411 dma_unmap_single (controller,
1412 urb->transfer_dma, urb->transfer_buffer_length,
1413 usb_pipein (urb->pipe)
1414 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1415 urb->transfer_flags &= ~URB_NO_DMA_MAP;
1416 }
1417
1418 /**
1419 * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
1420 * @dev: device to which the scatterlist will be mapped
1421 * @pipe: endpoint defining the mapping direction
1422 * @sg: the scatterlist to map
1423 * @nents: the number of entries in the scatterlist
1424 *
1425 * Return value is either < 0 (indicating no buffers could be mapped), or
1426 * the number of DMA mapping array entries in the scatterlist.
1427 *
1428 * The caller is responsible for placing the resulting DMA addresses from
1429 * the scatterlist into URB transfer buffer pointers, and for setting the
1430 * URB_NO_DMA_MAP transfer flag in each of those URBs.
1431 *
1432 * Top I/O rates come from queuing URBs, instead of waiting for each one
1433 * to complete before starting the next I/O. This is particularly easy
1434 * to do with scatterlists. Just allocate and submit one URB for each DMA
1435 * mapping entry returned, stopping on the first error or when all succeed.
1436 * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
1437 *
1438 * This call would normally be used when translating scatterlist requests,
1439 * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
1440 * may be able to coalesce mappings for improved I/O efficiency.
1441 *
1442 * Reverse the effect of this call with usb_buffer_unmap_sg().
1443 */
1444 int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
1445 struct scatterlist *sg, int nents)
1446 {
1447 struct usb_bus *bus;
1448 struct device *controller;
1449
1450 if (!dev
1451 || usb_pipecontrol (pipe)
1452 || !(bus = dev->bus)
1453 || !(controller = bus->controller)
1454 || !controller->dma_mask)
1455 return -1;
1456
1457 // FIXME generic api broken like pci, can't report errors
1458 return dma_map_sg (controller, sg, nents,
1459 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1460 }
1461
1462 /**
1463 * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s)
1464 * @dev: device to which the scatterlist will be mapped
1465 * @pipe: endpoint defining the mapping direction
1466 * @sg: the scatterlist to synchronize
1467 * @n_hw_ents: the positive return value from usb_buffer_map_sg
1468 *
1469 * Use this when you are re-using a scatterlist's data buffers for
1470 * another USB request.
1471 */
1472 void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
1473 struct scatterlist *sg, int n_hw_ents)
1474 {
1475 struct usb_bus *bus;
1476 struct device *controller;
1477
1478 if (!dev
1479 || !(bus = dev->bus)
1480 || !(controller = bus->controller)
1481 || !controller->dma_mask)
1482 return;
1483
1484 dma_sync_sg (controller, sg, n_hw_ents,
1485 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1486 }
1487
1488 /**
1489 * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
1490 * @dev: device to which the scatterlist will be mapped
1491 * @pipe: endpoint defining the mapping direction
1492 * @sg: the scatterlist to unmap
1493 * @n_hw_ents: the positive return value from usb_buffer_map_sg
1494 *
1495 * Reverses the effect of usb_buffer_map_sg().
1496 */
1497 void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
1498 struct scatterlist *sg, int n_hw_ents)
1499 {
1500 struct usb_bus *bus;
1501 struct device *controller;
1502
1503 if (!dev
1504 || !(bus = dev->bus)
1505 || !(controller = bus->controller)
1506 || !controller->dma_mask)
1507 return;
1508
1509 dma_unmap_sg (controller, sg, n_hw_ents,
1510 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1511 }
1512
1513
1514 struct bus_type usb_bus_type = {
1515 .name = "usb",
1516 .match = usb_device_match,
1517 .hotplug = usb_hotplug,
1518 };
1519
1520 #ifndef MODULE
1521
1522 static int __init usb_setup_disable(char *str)
1523 {
1524 nousb = 1;
1525 return 1;
1526 }
1527
1528 /* format to disable USB on kernel command line is: nousb */
1529 __setup("nousb", usb_setup_disable);
1530
1531 #endif
1532
1533 /*
1534 * for external read access to <nousb>
1535 */
1536 int STDCALL usb_disabled(void)
1537 {
1538 return nousb;
1539 }
1540
1541 /*
1542 * Init
1543 */
1544 int STDCALL __init usb_init(void)
1545 {
1546 if (nousb) {
1547 info("USB support disabled\n");
1548 return 0;
1549 }
1550
1551 bus_register(&usb_bus_type);
1552 usb_major_init();
1553 usbfs_init();
1554 usb_hub_init();
1555
1556 driver_register(&usb_generic_driver);
1557
1558 return 0;
1559 }
1560
1561 /*
1562 * Cleanup
1563 */
1564 void STDCALL __exit usb_exit(void)
1565 {
1566 /* This will matter if shutdown/reboot does exitcalls. */
1567 if (nousb)
1568 return;
1569
1570 driver_unregister(&usb_generic_driver);
1571 usb_major_cleanup();
1572 usbfs_cleanup();
1573 usb_hub_cleanup();
1574 bus_unregister(&usb_bus_type);
1575 }
1576
1577 subsys_initcall(usb_init);
1578 module_exit(usb_exit);
1579
1580 /*
1581 * USB may be built into the kernel or be built as modules.
1582 * These symbols are exported for device (or host controller)
1583 * driver modules to use.
1584 */
1585 EXPORT_SYMBOL(usb_epnum_to_ep_desc);
1586
1587 EXPORT_SYMBOL(usb_register);
1588 EXPORT_SYMBOL(usb_deregister);
1589 EXPORT_SYMBOL(usb_disabled);
1590
1591 EXPORT_SYMBOL(usb_device_probe);
1592 EXPORT_SYMBOL(usb_device_remove);
1593
1594 EXPORT_SYMBOL(usb_alloc_dev);
1595 EXPORT_SYMBOL(usb_put_dev);
1596 EXPORT_SYMBOL(usb_get_dev);
1597 EXPORT_SYMBOL(usb_hub_tt_clear_buffer);
1598
1599 EXPORT_SYMBOL(usb_driver_claim_interface);
1600 EXPORT_SYMBOL(usb_interface_claimed);
1601 EXPORT_SYMBOL(usb_driver_release_interface);
1602 EXPORT_SYMBOL(usb_match_id);
1603 EXPORT_SYMBOL(usb_find_interface);
1604 EXPORT_SYMBOL(usb_ifnum_to_if);
1605
1606 EXPORT_SYMBOL(usb_new_device);
1607 EXPORT_SYMBOL(usb_reset_device);
1608 EXPORT_SYMBOL(usb_connect);
1609 EXPORT_SYMBOL(usb_disconnect);
1610
1611 EXPORT_SYMBOL(__usb_get_extra_descriptor);
1612
1613 EXPORT_SYMBOL(usb_find_device);
1614 EXPORT_SYMBOL(usb_get_current_frame_number);
1615
1616 EXPORT_SYMBOL (usb_buffer_alloc);
1617 EXPORT_SYMBOL (usb_buffer_free);
1618
1619 EXPORT_SYMBOL (usb_buffer_map);
1620 EXPORT_SYMBOL (usb_buffer_dmasync);
1621 EXPORT_SYMBOL (usb_buffer_unmap);
1622
1623 EXPORT_SYMBOL (usb_buffer_map_sg);
1624 EXPORT_SYMBOL (usb_buffer_dmasync_sg);
1625 EXPORT_SYMBOL (usb_buffer_unmap_sg);
1626
1627 MODULE_LICENSE("GPL");