blob: e89dbd43e952c8a0498301516bec700f7303c50f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/usb/usb.c
3 *
4 * (C) Copyright Linus Torvalds 1999
5 * (C) Copyright Johannes Erdfelt 1999-2001
6 * (C) Copyright Andreas Gal 1999
7 * (C) Copyright Gregory P. Smith 1999
8 * (C) Copyright Deti Fliegl 1999 (new USB architecture)
9 * (C) Copyright Randy Dunlap 2000
10 * (C) Copyright David Brownell 2000-2004
11 * (C) Copyright Yggdrasil Computing, Inc. 2000
12 * (usb_device_id matching changes by Adam J. Richter)
13 * (C) Copyright Greg Kroah-Hartman 2002-2003
14 *
15 * NOTE! This is not actually a driver at all, rather this is
16 * just a collection of helper routines that implement the
17 * generic USB things that the real drivers can use..
18 *
19 * Think of this as a "USB library" rather than anything else.
20 * It should be considered a slave, with no callbacks. Callbacks
21 * are evil.
22 */
23
24#include <linux/config.h>
25
26#ifdef CONFIG_USB_DEBUG
27 #define DEBUG
28#else
29 #undef DEBUG
30#endif
31
32#include <linux/module.h>
33#include <linux/string.h>
34#include <linux/bitops.h>
35#include <linux/slab.h>
36#include <linux/interrupt.h> /* for in_interrupt() */
37#include <linux/kmod.h>
38#include <linux/init.h>
39#include <linux/spinlock.h>
40#include <linux/errno.h>
41#include <linux/smp_lock.h>
42#include <linux/rwsem.h>
43#include <linux/usb.h>
44
45#include <asm/io.h>
46#include <asm/scatterlist.h>
47#include <linux/mm.h>
48#include <linux/dma-mapping.h>
49
50#include "hcd.h"
51#include "usb.h"
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54const char *usbcore_name = "usbcore";
55
56static int nousb; /* Disable USB when built into kernel image */
57 /* Not honored on modular build */
58
59static DECLARE_RWSEM(usb_all_devices_rwsem);
60
61
62static int generic_probe (struct device *dev)
63{
64 return 0;
65}
66static int generic_remove (struct device *dev)
67{
Alan Stern3b4d7f72005-08-11 15:50:32 -040068 struct usb_device *udev = to_usb_device(dev);
69
70 /* if this is only an unbind, not a physical disconnect, then
71 * unconfigure the device */
72 if (udev->state == USB_STATE_CONFIGURED)
73 usb_set_configuration(udev, 0);
74
75 /* in case the call failed or the device was suspended */
76 if (udev->state >= USB_STATE_CONFIGURED)
77 usb_disable_device(udev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return 0;
79}
80
81static struct device_driver usb_generic_driver = {
82 .owner = THIS_MODULE,
83 .name = "usb",
84 .bus = &usb_bus_type,
85 .probe = generic_probe,
86 .remove = generic_remove,
87};
88
89static int usb_generic_driver_data;
90
91/* called from driver core with usb_bus_type.subsys writelock */
92static int usb_probe_interface(struct device *dev)
93{
94 struct usb_interface * intf = to_usb_interface(dev);
95 struct usb_driver * driver = to_usb_driver(dev->driver);
96 const struct usb_device_id *id;
97 int error = -ENODEV;
98
99 dev_dbg(dev, "%s\n", __FUNCTION__);
100
101 if (!driver->probe)
102 return error;
103 /* FIXME we'd much prefer to just resume it ... */
104 if (interface_to_usbdev(intf)->state == USB_STATE_SUSPENDED)
105 return -EHOSTUNREACH;
106
107 id = usb_match_id (intf, driver->id_table);
108 if (id) {
109 dev_dbg (dev, "%s - got id\n", __FUNCTION__);
David Brownelldb690872005-09-13 19:56:33 -0700110
111 /* Interface "power state" doesn't correspond to any hardware
112 * state whatsoever. We use it to record when it's bound to
113 * a driver that may start I/0: it's not frozen/quiesced.
114 */
115 mark_active(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 intf->condition = USB_INTERFACE_BINDING;
117 error = driver->probe (intf, id);
David Brownelldb690872005-09-13 19:56:33 -0700118 if (error) {
119 mark_quiesced(intf);
120 intf->condition = USB_INTERFACE_UNBOUND;
121 } else
122 intf->condition = USB_INTERFACE_BOUND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124
125 return error;
126}
127
128/* called from driver core with usb_bus_type.subsys writelock */
129static int usb_unbind_interface(struct device *dev)
130{
131 struct usb_interface *intf = to_usb_interface(dev);
132 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
133
134 intf->condition = USB_INTERFACE_UNBINDING;
135
136 /* release all urbs for this interface */
137 usb_disable_interface(interface_to_usbdev(intf), intf);
138
139 if (driver && driver->disconnect)
140 driver->disconnect(intf);
141
142 /* reset other interface state */
143 usb_set_interface(interface_to_usbdev(intf),
144 intf->altsetting[0].desc.bInterfaceNumber,
145 0);
146 usb_set_intfdata(intf, NULL);
147 intf->condition = USB_INTERFACE_UNBOUND;
David Brownelldb690872005-09-13 19:56:33 -0700148 mark_quiesced(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 return 0;
151}
152
153/**
154 * usb_register - register a USB driver
155 * @new_driver: USB operations for the driver
156 *
157 * Registers a USB driver with the USB core. The list of unattached
158 * interfaces will be rescanned whenever a new driver is added, allowing
159 * the new driver to attach to any recognized devices.
160 * Returns a negative error code on failure and 0 on success.
161 *
162 * NOTE: if you want your driver to use the USB major number, you must call
163 * usb_register_dev() to enable that functionality. This function no longer
164 * takes care of that.
165 */
166int usb_register(struct usb_driver *new_driver)
167{
168 int retval = 0;
169
170 if (nousb)
171 return -ENODEV;
172
173 new_driver->driver.name = (char *)new_driver->name;
174 new_driver->driver.bus = &usb_bus_type;
175 new_driver->driver.probe = usb_probe_interface;
176 new_driver->driver.remove = usb_unbind_interface;
177 new_driver->driver.owner = new_driver->owner;
178
179 usb_lock_all_devices();
180 retval = driver_register(&new_driver->driver);
181 usb_unlock_all_devices();
182
183 if (!retval) {
184 pr_info("%s: registered new driver %s\n",
185 usbcore_name, new_driver->name);
186 usbfs_update_special();
187 } else {
188 printk(KERN_ERR "%s: error %d registering driver %s\n",
189 usbcore_name, retval, new_driver->name);
190 }
191
192 return retval;
193}
194
195/**
196 * usb_deregister - unregister a USB driver
197 * @driver: USB operations of the driver to unregister
198 * Context: must be able to sleep
199 *
200 * Unlinks the specified driver from the internal USB driver list.
201 *
202 * NOTE: If you called usb_register_dev(), you still need to call
203 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
204 * this * call will no longer do it for you.
205 */
206void usb_deregister(struct usb_driver *driver)
207{
208 pr_info("%s: deregistering driver %s\n", usbcore_name, driver->name);
209
210 usb_lock_all_devices();
211 driver_unregister (&driver->driver);
212 usb_unlock_all_devices();
213
214 usbfs_update_special();
215}
216
217/**
218 * usb_ifnum_to_if - get the interface object with a given interface number
219 * @dev: the device whose current configuration is considered
220 * @ifnum: the desired interface
221 *
222 * This walks the device descriptor for the currently active configuration
223 * and returns a pointer to the interface with that particular interface
224 * number, or null.
225 *
226 * Note that configuration descriptors are not required to assign interface
227 * numbers sequentially, so that it would be incorrect to assume that
228 * the first interface in that descriptor corresponds to interface zero.
229 * This routine helps device drivers avoid such mistakes.
230 * However, you should make sure that you do the right thing with any
231 * alternate settings available for this interfaces.
232 *
233 * Don't call this function unless you are bound to one of the interfaces
234 * on this device or you have locked the device!
235 */
236struct usb_interface *usb_ifnum_to_if(struct usb_device *dev, unsigned ifnum)
237{
238 struct usb_host_config *config = dev->actconfig;
239 int i;
240
241 if (!config)
242 return NULL;
243 for (i = 0; i < config->desc.bNumInterfaces; i++)
244 if (config->interface[i]->altsetting[0]
245 .desc.bInterfaceNumber == ifnum)
246 return config->interface[i];
247
248 return NULL;
249}
250
251/**
252 * usb_altnum_to_altsetting - get the altsetting structure with a given
253 * alternate setting number.
254 * @intf: the interface containing the altsetting in question
255 * @altnum: the desired alternate setting number
256 *
257 * This searches the altsetting array of the specified interface for
258 * an entry with the correct bAlternateSetting value and returns a pointer
259 * to that entry, or null.
260 *
261 * Note that altsettings need not be stored sequentially by number, so
262 * it would be incorrect to assume that the first altsetting entry in
263 * the array corresponds to altsetting zero. This routine helps device
264 * drivers avoid such mistakes.
265 *
266 * Don't call this function unless you are bound to the intf interface
267 * or you have locked the device!
268 */
269struct usb_host_interface *usb_altnum_to_altsetting(struct usb_interface *intf,
270 unsigned int altnum)
271{
272 int i;
273
274 for (i = 0; i < intf->num_altsetting; i++) {
275 if (intf->altsetting[i].desc.bAlternateSetting == altnum)
276 return &intf->altsetting[i];
277 }
278 return NULL;
279}
280
281/**
282 * usb_driver_claim_interface - bind a driver to an interface
283 * @driver: the driver to be bound
284 * @iface: the interface to which it will be bound; must be in the
285 * usb device's active configuration
286 * @priv: driver data associated with that interface
287 *
288 * This is used by usb device drivers that need to claim more than one
289 * interface on a device when probing (audio and acm are current examples).
290 * No device driver should directly modify internal usb_interface or
291 * usb_device structure members.
292 *
293 * Few drivers should need to use this routine, since the most natural
294 * way to bind to an interface is to return the private data from
295 * the driver's probe() method.
296 *
297 * Callers must own the device lock and the driver model's usb_bus_type.subsys
298 * writelock. So driver probe() entries don't need extra locking,
299 * but other call contexts may need to explicitly claim those locks.
300 */
301int usb_driver_claim_interface(struct usb_driver *driver,
302 struct usb_interface *iface, void* priv)
303{
304 struct device *dev = &iface->dev;
305
306 if (dev->driver)
307 return -EBUSY;
308
309 dev->driver = &driver->driver;
310 usb_set_intfdata(iface, priv);
311 iface->condition = USB_INTERFACE_BOUND;
David Brownelldb690872005-09-13 19:56:33 -0700312 mark_active(iface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /* if interface was already added, bind now; else let
315 * the future device_add() bind it, bypassing probe()
316 */
Daniel Ritzd305ef52005-09-22 00:47:24 -0700317 if (device_is_registered(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 device_bind_driver(dev);
319
320 return 0;
321}
322
323/**
324 * usb_driver_release_interface - unbind a driver from an interface
325 * @driver: the driver to be unbound
326 * @iface: the interface from which it will be unbound
327 *
328 * This can be used by drivers to release an interface without waiting
329 * for their disconnect() methods to be called. In typical cases this
330 * also causes the driver disconnect() method to be called.
331 *
332 * This call is synchronous, and may not be used in an interrupt context.
333 * Callers must own the device lock and the driver model's usb_bus_type.subsys
334 * writelock. So driver disconnect() entries don't need extra locking,
335 * but other call contexts may need to explicitly claim those locks.
336 */
337void usb_driver_release_interface(struct usb_driver *driver,
338 struct usb_interface *iface)
339{
340 struct device *dev = &iface->dev;
341
342 /* this should never happen, don't release something that's not ours */
343 if (!dev->driver || dev->driver != &driver->driver)
344 return;
345
Alan Sternf4096612005-05-06 15:41:08 -0400346 /* don't release from within disconnect() */
347 if (iface->condition != USB_INTERFACE_BOUND)
348 return;
349
Daniel Ritzd305ef52005-09-22 00:47:24 -0700350 /* don't release if the interface hasn't been added yet */
351 if (device_is_registered(dev)) {
Alan Sternf4096612005-05-06 15:41:08 -0400352 iface->condition = USB_INTERFACE_UNBINDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 device_release_driver(dev);
Alan Sternf4096612005-05-06 15:41:08 -0400354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 dev->driver = NULL;
357 usb_set_intfdata(iface, NULL);
358 iface->condition = USB_INTERFACE_UNBOUND;
David Brownelldb690872005-09-13 19:56:33 -0700359 mark_quiesced(iface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
362/**
363 * usb_match_id - find first usb_device_id matching device or interface
364 * @interface: the interface of interest
365 * @id: array of usb_device_id structures, terminated by zero entry
366 *
367 * usb_match_id searches an array of usb_device_id's and returns
368 * the first one matching the device or interface, or null.
369 * This is used when binding (or rebinding) a driver to an interface.
370 * Most USB device drivers will use this indirectly, through the usb core,
371 * but some layered driver frameworks use it directly.
372 * These device tables are exported with MODULE_DEVICE_TABLE, through
373 * modutils and "modules.usbmap", to support the driver loading
374 * functionality of USB hotplugging.
375 *
376 * What Matches:
377 *
378 * The "match_flags" element in a usb_device_id controls which
379 * members are used. If the corresponding bit is set, the
380 * value in the device_id must match its corresponding member
381 * in the device or interface descriptor, or else the device_id
382 * does not match.
383 *
384 * "driver_info" is normally used only by device drivers,
385 * but you can create a wildcard "matches anything" usb_device_id
386 * as a driver's "modules.usbmap" entry if you provide an id with
387 * only a nonzero "driver_info" field. If you do this, the USB device
388 * driver's probe() routine should use additional intelligence to
389 * decide whether to bind to the specified interface.
390 *
391 * What Makes Good usb_device_id Tables:
392 *
393 * The match algorithm is very simple, so that intelligence in
394 * driver selection must come from smart driver id records.
395 * Unless you have good reasons to use another selection policy,
396 * provide match elements only in related groups, and order match
397 * specifiers from specific to general. Use the macros provided
398 * for that purpose if you can.
399 *
400 * The most specific match specifiers use device descriptor
401 * data. These are commonly used with product-specific matches;
402 * the USB_DEVICE macro lets you provide vendor and product IDs,
403 * and you can also match against ranges of product revisions.
404 * These are widely used for devices with application or vendor
405 * specific bDeviceClass values.
406 *
407 * Matches based on device class/subclass/protocol specifications
408 * are slightly more general; use the USB_DEVICE_INFO macro, or
409 * its siblings. These are used with single-function devices
410 * where bDeviceClass doesn't specify that each interface has
411 * its own class.
412 *
413 * Matches based on interface class/subclass/protocol are the
414 * most general; they let drivers bind to any interface on a
415 * multiple-function device. Use the USB_INTERFACE_INFO
416 * macro, or its siblings, to match class-per-interface style
417 * devices (as recorded in bDeviceClass).
418 *
419 * Within those groups, remember that not all combinations are
420 * meaningful. For example, don't give a product version range
421 * without vendor and product IDs; or specify a protocol without
422 * its associated class and subclass.
423 */
424const struct usb_device_id *
425usb_match_id(struct usb_interface *interface, const struct usb_device_id *id)
426{
427 struct usb_host_interface *intf;
428 struct usb_device *dev;
429
430 /* proc_connectinfo in devio.c may call us with id == NULL. */
431 if (id == NULL)
432 return NULL;
433
434 intf = interface->cur_altsetting;
435 dev = interface_to_usbdev(interface);
436
437 /* It is important to check that id->driver_info is nonzero,
438 since an entry that is all zeroes except for a nonzero
439 id->driver_info is the way to create an entry that
440 indicates that the driver want to examine every
441 device and interface. */
442 for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
443 id->driver_info; id++) {
444
445 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
446 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
447 continue;
448
449 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
450 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
451 continue;
452
453 /* No need to test id->bcdDevice_lo != 0, since 0 is never
454 greater than any unsigned number. */
455 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
456 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
457 continue;
458
459 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
460 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
461 continue;
462
463 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
464 (id->bDeviceClass != dev->descriptor.bDeviceClass))
465 continue;
466
467 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
468 (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
469 continue;
470
471 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
472 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
473 continue;
474
475 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
476 (id->bInterfaceClass != intf->desc.bInterfaceClass))
477 continue;
478
479 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
480 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
481 continue;
482
483 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
484 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
485 continue;
486
487 return id;
488 }
489
490 return NULL;
491}
492
mochel@digitalimplant.org6034a082005-03-21 11:09:40 -0800493
494static int __find_interface(struct device * dev, void * data)
495{
496 struct usb_interface ** ret = (struct usb_interface **)data;
497 struct usb_interface * intf = *ret;
498 int *minor = (int *)data;
499
500 /* can't look at usb devices, only interfaces */
501 if (dev->driver == &usb_generic_driver)
502 return 0;
503
504 intf = to_usb_interface(dev);
505 if (intf->minor != -1 && intf->minor == *minor) {
506 *ret = intf;
507 return 1;
508 }
509 return 0;
510}
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512/**
513 * usb_find_interface - find usb_interface pointer for driver and device
514 * @drv: the driver whose current configuration is considered
515 * @minor: the minor number of the desired device
516 *
517 * This walks the driver device list and returns a pointer to the interface
518 * with the matching minor. Note, this only works for devices that share the
519 * USB major number.
520 */
521struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor)
522{
gregkh@suse.deff710712005-03-24 00:44:28 -0800523 struct usb_interface *intf = (struct usb_interface *)(long)minor;
mochel@digitalimplant.org6034a082005-03-21 11:09:40 -0800524 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
mochel@digitalimplant.org6034a082005-03-21 11:09:40 -0800526 ret = driver_for_each_device(&drv->driver, NULL, &intf, __find_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
mochel@digitalimplant.org6034a082005-03-21 11:09:40 -0800528 return ret ? intf : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531static int usb_device_match (struct device *dev, struct device_driver *drv)
532{
533 struct usb_interface *intf;
534 struct usb_driver *usb_drv;
535 const struct usb_device_id *id;
536
537 /* check for generic driver, which we don't match any device with */
538 if (drv == &usb_generic_driver)
539 return 0;
540
541 intf = to_usb_interface(dev);
542 usb_drv = to_usb_driver(drv);
543
544 id = usb_match_id (intf, usb_drv->id_table);
545 if (id)
546 return 1;
547
548 return 0;
549}
550
551
552#ifdef CONFIG_HOTPLUG
553
554/*
555 * USB hotplugging invokes what /proc/sys/kernel/hotplug says
556 * (normally /sbin/hotplug) when USB devices get added or removed.
557 *
558 * This invokes a user mode policy agent, typically helping to load driver
559 * or other modules, configure the device, and more. Drivers can provide
560 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
561 *
562 * We're called either from khubd (the typical case) or from root hub
563 * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle
564 * delays in event delivery. Use sysfs (and DEVPATH) to make sure the
565 * device (and this configuration!) are still present.
566 */
567static int usb_hotplug (struct device *dev, char **envp, int num_envp,
568 char *buffer, int buffer_size)
569{
570 struct usb_interface *intf;
571 struct usb_device *usb_dev;
572 int i = 0;
573 int length = 0;
574
575 if (!dev)
576 return -ENODEV;
577
578 /* driver is often null here; dev_dbg() would oops */
579 pr_debug ("usb %s: hotplug\n", dev->bus_id);
580
581 /* Must check driver_data here, as on remove driver is always NULL */
582 if ((dev->driver == &usb_generic_driver) ||
583 (dev->driver_data == &usb_generic_driver_data))
584 return 0;
585
586 intf = to_usb_interface(dev);
587 usb_dev = interface_to_usbdev (intf);
588
589 if (usb_dev->devnum < 0) {
590 pr_debug ("usb %s: already deleted?\n", dev->bus_id);
591 return -ENODEV;
592 }
593 if (!usb_dev->bus) {
594 pr_debug ("usb %s: bus removed?\n", dev->bus_id);
595 return -ENODEV;
596 }
597
598#ifdef CONFIG_USB_DEVICEFS
599 /* If this is available, userspace programs can directly read
600 * all the device descriptors we don't tell them about. Or
601 * even act as usermode drivers.
602 *
603 * FIXME reduce hardwired intelligence here
604 */
605 if (add_hotplug_env_var(envp, num_envp, &i,
606 buffer, buffer_size, &length,
607 "DEVICE=/proc/bus/usb/%03d/%03d",
608 usb_dev->bus->busnum, usb_dev->devnum))
609 return -ENOMEM;
610#endif
611
612 /* per-device configurations are common */
613 if (add_hotplug_env_var(envp, num_envp, &i,
614 buffer, buffer_size, &length,
615 "PRODUCT=%x/%x/%x",
616 le16_to_cpu(usb_dev->descriptor.idVendor),
617 le16_to_cpu(usb_dev->descriptor.idProduct),
618 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
619 return -ENOMEM;
620
621 /* class-based driver binding models */
622 if (add_hotplug_env_var(envp, num_envp, &i,
623 buffer, buffer_size, &length,
624 "TYPE=%d/%d/%d",
625 usb_dev->descriptor.bDeviceClass,
626 usb_dev->descriptor.bDeviceSubClass,
627 usb_dev->descriptor.bDeviceProtocol))
628 return -ENOMEM;
629
630 if (usb_dev->descriptor.bDeviceClass == 0) {
631 struct usb_host_interface *alt = intf->cur_altsetting;
632
633 /* 2.4 only exposed interface zero. in 2.5, hotplug
634 * agents are called for all interfaces, and can use
635 * $DEVPATH/bInterfaceNumber if necessary.
636 */
637 if (add_hotplug_env_var(envp, num_envp, &i,
638 buffer, buffer_size, &length,
639 "INTERFACE=%d/%d/%d",
640 alt->desc.bInterfaceClass,
641 alt->desc.bInterfaceSubClass,
642 alt->desc.bInterfaceProtocol))
643 return -ENOMEM;
644
645 if (add_hotplug_env_var(envp, num_envp, &i,
646 buffer, buffer_size, &length,
Roman Kaganfb3b4eb2005-04-22 15:07:01 -0700647 "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 le16_to_cpu(usb_dev->descriptor.idVendor),
649 le16_to_cpu(usb_dev->descriptor.idProduct),
650 le16_to_cpu(usb_dev->descriptor.bcdDevice),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 usb_dev->descriptor.bDeviceClass,
652 usb_dev->descriptor.bDeviceSubClass,
653 usb_dev->descriptor.bDeviceProtocol,
654 alt->desc.bInterfaceClass,
655 alt->desc.bInterfaceSubClass,
656 alt->desc.bInterfaceProtocol))
657 return -ENOMEM;
658 } else {
659 if (add_hotplug_env_var(envp, num_envp, &i,
660 buffer, buffer_size, &length,
Roman Kaganfb3b4eb2005-04-22 15:07:01 -0700661 "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic*isc*ip*",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 le16_to_cpu(usb_dev->descriptor.idVendor),
663 le16_to_cpu(usb_dev->descriptor.idProduct),
664 le16_to_cpu(usb_dev->descriptor.bcdDevice),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 usb_dev->descriptor.bDeviceClass,
666 usb_dev->descriptor.bDeviceSubClass,
667 usb_dev->descriptor.bDeviceProtocol))
668 return -ENOMEM;
669 }
670
671 envp[i] = NULL;
672
673 return 0;
674}
675
676#else
677
678static int usb_hotplug (struct device *dev, char **envp,
679 int num_envp, char *buffer, int buffer_size)
680{
681 return -ENODEV;
682}
683
684#endif /* CONFIG_HOTPLUG */
685
686/**
687 * usb_release_dev - free a usb device structure when all users of it are finished.
688 * @dev: device that's been disconnected
689 *
690 * Will be called only by the device core when all users of this usb device are
691 * done.
692 */
693static void usb_release_dev(struct device *dev)
694{
695 struct usb_device *udev;
696
697 udev = to_usb_device(dev);
698
699 usb_destroy_configuration(udev);
700 usb_bus_put(udev->bus);
701 kfree(udev->product);
702 kfree(udev->manufacturer);
703 kfree(udev->serial);
704 kfree(udev);
705}
706
707/**
708 * usb_alloc_dev - usb device constructor (usbcore-internal)
709 * @parent: hub to which device is connected; null to allocate a root hub
710 * @bus: bus used to access the device
711 * @port1: one-based index of port; ignored for root hubs
712 * Context: !in_interrupt ()
713 *
714 * Only hub drivers (including virtual root hub drivers for host
715 * controllers) should ever call this.
716 *
717 * This call may not be used in a non-sleeping context.
718 */
719struct usb_device *
720usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port1)
721{
722 struct usb_device *dev;
723
724 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
725 if (!dev)
726 return NULL;
727
728 memset(dev, 0, sizeof(*dev));
729
730 bus = usb_bus_get(bus);
731 if (!bus) {
732 kfree(dev);
733 return NULL;
734 }
735
736 device_initialize(&dev->dev);
737 dev->dev.bus = &usb_bus_type;
738 dev->dev.dma_mask = bus->controller->dma_mask;
739 dev->dev.driver_data = &usb_generic_driver_data;
740 dev->dev.driver = &usb_generic_driver;
741 dev->dev.release = usb_release_dev;
742 dev->state = USB_STATE_ATTACHED;
743
744 INIT_LIST_HEAD(&dev->ep0.urb_list);
745 dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;
746 dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;
747 /* ep0 maxpacket comes later, from device descriptor */
748 dev->ep_in[0] = dev->ep_out[0] = &dev->ep0;
749
750 /* Save readable and stable topology id, distinguishing devices
751 * by location for diagnostics, tools, driver model, etc. The
752 * string is a path along hub ports, from the root. Each device's
753 * dev->devpath will be stable until USB is re-cabled, and hubs
754 * are often labeled with these port numbers. The bus_id isn't
755 * as stable: bus->busnum changes easily from modprobe order,
756 * cardbus or pci hotplugging, and so on.
757 */
758 if (unlikely (!parent)) {
759 dev->devpath [0] = '0';
760
761 dev->dev.parent = bus->controller;
762 sprintf (&dev->dev.bus_id[0], "usb%d", bus->busnum);
763 } else {
764 /* match any labeling on the hubs; it's one-based */
765 if (parent->devpath [0] == '0')
766 snprintf (dev->devpath, sizeof dev->devpath,
767 "%d", port1);
768 else
769 snprintf (dev->devpath, sizeof dev->devpath,
770 "%s.%d", parent->devpath, port1);
771
772 dev->dev.parent = &parent->dev;
773 sprintf (&dev->dev.bus_id[0], "%d-%s",
774 bus->busnum, dev->devpath);
775
776 /* hub driver sets up TT records */
777 }
778
779 dev->bus = bus;
780 dev->parent = parent;
781 INIT_LIST_HEAD(&dev->filelist);
782
783 init_MUTEX(&dev->serialize);
784
785 return dev;
786}
787
788/**
789 * usb_get_dev - increments the reference count of the usb device structure
790 * @dev: the device being referenced
791 *
792 * Each live reference to a device should be refcounted.
793 *
794 * Drivers for USB interfaces should normally record such references in
795 * their probe() methods, when they bind to an interface, and release
796 * them by calling usb_put_dev(), in their disconnect() methods.
797 *
798 * A pointer to the device with the incremented reference counter is returned.
799 */
800struct usb_device *usb_get_dev(struct usb_device *dev)
801{
802 if (dev)
803 get_device(&dev->dev);
804 return dev;
805}
806
807/**
808 * usb_put_dev - release a use of the usb device structure
809 * @dev: device that's been disconnected
810 *
811 * Must be called when a user of a device is finished with it. When the last
812 * user of the device calls this function, the memory of the device is freed.
813 */
814void usb_put_dev(struct usb_device *dev)
815{
816 if (dev)
817 put_device(&dev->dev);
818}
819
820/**
821 * usb_get_intf - increments the reference count of the usb interface structure
822 * @intf: the interface being referenced
823 *
824 * Each live reference to a interface must be refcounted.
825 *
826 * Drivers for USB interfaces should normally record such references in
827 * their probe() methods, when they bind to an interface, and release
828 * them by calling usb_put_intf(), in their disconnect() methods.
829 *
830 * A pointer to the interface with the incremented reference counter is
831 * returned.
832 */
833struct usb_interface *usb_get_intf(struct usb_interface *intf)
834{
835 if (intf)
836 get_device(&intf->dev);
837 return intf;
838}
839
840/**
841 * usb_put_intf - release a use of the usb interface structure
842 * @intf: interface that's been decremented
843 *
844 * Must be called when a user of an interface is finished with it. When the
845 * last user of the interface calls this function, the memory of the interface
846 * is freed.
847 */
848void usb_put_intf(struct usb_interface *intf)
849{
850 if (intf)
851 put_device(&intf->dev);
852}
853
854
855/* USB device locking
856 *
857 * Although locking USB devices should be straightforward, it is
858 * complicated by the way the driver-model core works. When a new USB
859 * driver is registered or unregistered, the core will automatically
860 * probe or disconnect all matching interfaces on all USB devices while
861 * holding the USB subsystem writelock. There's no good way for us to
862 * tell which devices will be used or to lock them beforehand; our only
863 * option is to effectively lock all the USB devices.
864 *
865 * We do that by using a private rw-semaphore, usb_all_devices_rwsem.
866 * When locking an individual device you must first acquire the rwsem's
867 * readlock. When a driver is registered or unregistered the writelock
868 * must be held. These actions are encapsulated in the subroutines
869 * below, so all a driver needs to do is call usb_lock_device() and
870 * usb_unlock_device().
871 *
872 * Complications arise when several devices are to be locked at the same
873 * time. Only hub-aware drivers that are part of usbcore ever have to
874 * do this; nobody else needs to worry about it. The problem is that
875 * usb_lock_device() must not be called to lock a second device since it
876 * would acquire the rwsem's readlock reentrantly, leading to deadlock if
877 * another thread was waiting for the writelock. The solution is simple:
878 *
879 * When locking more than one device, call usb_lock_device()
880 * to lock the first one. Lock the others by calling
881 * down(&udev->serialize) directly.
882 *
883 * When unlocking multiple devices, use up(&udev->serialize)
884 * to unlock all but the last one. Unlock the last one by
885 * calling usb_unlock_device().
886 *
887 * When locking both a device and its parent, always lock the
888 * the parent first.
889 */
890
891/**
892 * usb_lock_device - acquire the lock for a usb device structure
893 * @udev: device that's being locked
894 *
895 * Use this routine when you don't hold any other device locks;
896 * to acquire nested inner locks call down(&udev->serialize) directly.
897 * This is necessary for proper interaction with usb_lock_all_devices().
898 */
899void usb_lock_device(struct usb_device *udev)
900{
901 down_read(&usb_all_devices_rwsem);
902 down(&udev->serialize);
903}
904
905/**
906 * usb_trylock_device - attempt to acquire the lock for a usb device structure
907 * @udev: device that's being locked
908 *
909 * Don't use this routine if you already hold a device lock;
910 * use down_trylock(&udev->serialize) instead.
911 * This is necessary for proper interaction with usb_lock_all_devices().
912 *
913 * Returns 1 if successful, 0 if contention.
914 */
915int usb_trylock_device(struct usb_device *udev)
916{
917 if (!down_read_trylock(&usb_all_devices_rwsem))
918 return 0;
919 if (down_trylock(&udev->serialize)) {
920 up_read(&usb_all_devices_rwsem);
921 return 0;
922 }
923 return 1;
924}
925
926/**
927 * usb_lock_device_for_reset - cautiously acquire the lock for a
928 * usb device structure
929 * @udev: device that's being locked
930 * @iface: interface bound to the driver making the request (optional)
931 *
932 * Attempts to acquire the device lock, but fails if the device is
933 * NOTATTACHED or SUSPENDED, or if iface is specified and the interface
934 * is neither BINDING nor BOUND. Rather than sleeping to wait for the
935 * lock, the routine polls repeatedly. This is to prevent deadlock with
936 * disconnect; in some drivers (such as usb-storage) the disconnect()
Alan Stern3ea15962005-08-11 10:15:39 -0400937 * or suspend() method will block waiting for a device reset to complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 *
939 * Returns a negative error code for failure, otherwise 1 or 0 to indicate
940 * that the device will or will not have to be unlocked. (0 can be
941 * returned when an interface is given and is BINDING, because in that
942 * case the driver already owns the device lock.)
943 */
944int usb_lock_device_for_reset(struct usb_device *udev,
945 struct usb_interface *iface)
946{
Alan Stern3ea15962005-08-11 10:15:39 -0400947 unsigned long jiffies_expire = jiffies + HZ;
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (udev->state == USB_STATE_NOTATTACHED)
950 return -ENODEV;
951 if (udev->state == USB_STATE_SUSPENDED)
952 return -EHOSTUNREACH;
953 if (iface) {
954 switch (iface->condition) {
955 case USB_INTERFACE_BINDING:
956 return 0;
957 case USB_INTERFACE_BOUND:
958 break;
959 default:
960 return -EINTR;
961 }
962 }
963
964 while (!usb_trylock_device(udev)) {
Alan Stern3ea15962005-08-11 10:15:39 -0400965
966 /* If we can't acquire the lock after waiting one second,
967 * we're probably deadlocked */
968 if (time_after(jiffies, jiffies_expire))
969 return -EBUSY;
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 msleep(15);
972 if (udev->state == USB_STATE_NOTATTACHED)
973 return -ENODEV;
974 if (udev->state == USB_STATE_SUSPENDED)
975 return -EHOSTUNREACH;
976 if (iface && iface->condition != USB_INTERFACE_BOUND)
977 return -EINTR;
978 }
979 return 1;
980}
981
982/**
983 * usb_unlock_device - release the lock for a usb device structure
984 * @udev: device that's being unlocked
985 *
986 * Use this routine when releasing the only device lock you hold;
987 * to release inner nested locks call up(&udev->serialize) directly.
988 * This is necessary for proper interaction with usb_lock_all_devices().
989 */
990void usb_unlock_device(struct usb_device *udev)
991{
992 up(&udev->serialize);
993 up_read(&usb_all_devices_rwsem);
994}
995
996/**
997 * usb_lock_all_devices - acquire the lock for all usb device structures
998 *
999 * This is necessary when registering a new driver or probing a bus,
1000 * since the driver-model core may try to use any usb_device.
1001 */
1002void usb_lock_all_devices(void)
1003{
1004 down_write(&usb_all_devices_rwsem);
1005}
1006
1007/**
1008 * usb_unlock_all_devices - release the lock for all usb device structures
1009 */
1010void usb_unlock_all_devices(void)
1011{
1012 up_write(&usb_all_devices_rwsem);
1013}
1014
1015
1016static struct usb_device *match_device(struct usb_device *dev,
1017 u16 vendor_id, u16 product_id)
1018{
1019 struct usb_device *ret_dev = NULL;
1020 int child;
1021
1022 dev_dbg(&dev->dev, "check for vendor %04x, product %04x ...\n",
1023 le16_to_cpu(dev->descriptor.idVendor),
1024 le16_to_cpu(dev->descriptor.idProduct));
1025
1026 /* see if this device matches */
1027 if ((vendor_id == le16_to_cpu(dev->descriptor.idVendor)) &&
1028 (product_id == le16_to_cpu(dev->descriptor.idProduct))) {
1029 dev_dbg (&dev->dev, "matched this device!\n");
1030 ret_dev = usb_get_dev(dev);
1031 goto exit;
1032 }
1033
1034 /* look through all of the children of this device */
1035 for (child = 0; child < dev->maxchild; ++child) {
1036 if (dev->children[child]) {
1037 down(&dev->children[child]->serialize);
1038 ret_dev = match_device(dev->children[child],
1039 vendor_id, product_id);
1040 up(&dev->children[child]->serialize);
1041 if (ret_dev)
1042 goto exit;
1043 }
1044 }
1045exit:
1046 return ret_dev;
1047}
1048
1049/**
1050 * usb_find_device - find a specific usb device in the system
1051 * @vendor_id: the vendor id of the device to find
1052 * @product_id: the product id of the device to find
1053 *
1054 * Returns a pointer to a struct usb_device if such a specified usb
1055 * device is present in the system currently. The usage count of the
1056 * device will be incremented if a device is found. Make sure to call
1057 * usb_put_dev() when the caller is finished with the device.
1058 *
1059 * If a device with the specified vendor and product id is not found,
1060 * NULL is returned.
1061 */
1062struct usb_device *usb_find_device(u16 vendor_id, u16 product_id)
1063{
1064 struct list_head *buslist;
1065 struct usb_bus *bus;
1066 struct usb_device *dev = NULL;
1067
1068 down(&usb_bus_list_lock);
1069 for (buslist = usb_bus_list.next;
1070 buslist != &usb_bus_list;
1071 buslist = buslist->next) {
1072 bus = container_of(buslist, struct usb_bus, bus_list);
1073 if (!bus->root_hub)
1074 continue;
1075 usb_lock_device(bus->root_hub);
1076 dev = match_device(bus->root_hub, vendor_id, product_id);
1077 usb_unlock_device(bus->root_hub);
1078 if (dev)
1079 goto exit;
1080 }
1081exit:
1082 up(&usb_bus_list_lock);
1083 return dev;
1084}
1085
1086/**
1087 * usb_get_current_frame_number - return current bus frame number
1088 * @dev: the device whose bus is being queried
1089 *
1090 * Returns the current frame number for the USB host controller
1091 * used with the given USB device. This can be used when scheduling
1092 * isochronous requests.
1093 *
1094 * Note that different kinds of host controller have different
1095 * "scheduling horizons". While one type might support scheduling only
1096 * 32 frames into the future, others could support scheduling up to
1097 * 1024 frames into the future.
1098 */
1099int usb_get_current_frame_number(struct usb_device *dev)
1100{
1101 return dev->bus->op->get_frame_number (dev);
1102}
1103
1104/*-------------------------------------------------------------------*/
1105/*
1106 * __usb_get_extra_descriptor() finds a descriptor of specific type in the
1107 * extra field of the interface and endpoint descriptor structs.
1108 */
1109
1110int __usb_get_extra_descriptor(char *buffer, unsigned size,
1111 unsigned char type, void **ptr)
1112{
1113 struct usb_descriptor_header *header;
1114
1115 while (size >= sizeof(struct usb_descriptor_header)) {
1116 header = (struct usb_descriptor_header *)buffer;
1117
1118 if (header->bLength < 2) {
1119 printk(KERN_ERR
1120 "%s: bogus descriptor, type %d length %d\n",
1121 usbcore_name,
1122 header->bDescriptorType,
1123 header->bLength);
1124 return -1;
1125 }
1126
1127 if (header->bDescriptorType == type) {
1128 *ptr = header;
1129 return 0;
1130 }
1131
1132 buffer += header->bLength;
1133 size -= header->bLength;
1134 }
1135 return -1;
1136}
1137
1138/**
1139 * usb_buffer_alloc - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
1140 * @dev: device the buffer will be used with
1141 * @size: requested buffer size
1142 * @mem_flags: affect whether allocation may block
1143 * @dma: used to return DMA address of buffer
1144 *
1145 * Return value is either null (indicating no buffer could be allocated), or
1146 * the cpu-space pointer to a buffer that may be used to perform DMA to the
1147 * specified device. Such cpu-space buffers are returned along with the DMA
1148 * address (through the pointer provided).
1149 *
1150 * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
1151 * to avoid behaviors like using "DMA bounce buffers", or tying down I/O
1152 * mapping hardware for long idle periods. The implementation varies between
1153 * platforms, depending on details of how DMA will work to this device.
1154 * Using these buffers also helps prevent cacheline sharing problems on
1155 * architectures where CPU caches are not DMA-coherent.
1156 *
1157 * When the buffer is no longer used, free it with usb_buffer_free().
1158 */
1159void *usb_buffer_alloc (
1160 struct usb_device *dev,
1161 size_t size,
Al Viro55016f12005-10-21 03:21:58 -04001162 gfp_t mem_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 dma_addr_t *dma
1164)
1165{
1166 if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_alloc)
1167 return NULL;
1168 return dev->bus->op->buffer_alloc (dev->bus, size, mem_flags, dma);
1169}
1170
1171/**
1172 * usb_buffer_free - free memory allocated with usb_buffer_alloc()
1173 * @dev: device the buffer was used with
1174 * @size: requested buffer size
1175 * @addr: CPU address of buffer
1176 * @dma: DMA address of buffer
1177 *
1178 * This reclaims an I/O buffer, letting it be reused. The memory must have
1179 * been allocated using usb_buffer_alloc(), and the parameters must match
1180 * those provided in that allocation request.
1181 */
1182void usb_buffer_free (
1183 struct usb_device *dev,
1184 size_t size,
1185 void *addr,
1186 dma_addr_t dma
1187)
1188{
1189 if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_free)
1190 return;
1191 dev->bus->op->buffer_free (dev->bus, size, addr, dma);
1192}
1193
1194/**
1195 * usb_buffer_map - create DMA mapping(s) for an urb
1196 * @urb: urb whose transfer_buffer/setup_packet will be mapped
1197 *
1198 * Return value is either null (indicating no buffer could be mapped), or
1199 * the parameter. URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP are
1200 * added to urb->transfer_flags if the operation succeeds. If the device
1201 * is connected to this system through a non-DMA controller, this operation
1202 * always succeeds.
1203 *
1204 * This call would normally be used for an urb which is reused, perhaps
1205 * as the target of a large periodic transfer, with usb_buffer_dmasync()
1206 * calls to synchronize memory and dma state.
1207 *
1208 * Reverse the effect of this call with usb_buffer_unmap().
1209 */
1210#if 0
1211struct urb *usb_buffer_map (struct urb *urb)
1212{
1213 struct usb_bus *bus;
1214 struct device *controller;
1215
1216 if (!urb
1217 || !urb->dev
1218 || !(bus = urb->dev->bus)
1219 || !(controller = bus->controller))
1220 return NULL;
1221
1222 if (controller->dma_mask) {
1223 urb->transfer_dma = dma_map_single (controller,
1224 urb->transfer_buffer, urb->transfer_buffer_length,
1225 usb_pipein (urb->pipe)
1226 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1227 if (usb_pipecontrol (urb->pipe))
1228 urb->setup_dma = dma_map_single (controller,
1229 urb->setup_packet,
1230 sizeof (struct usb_ctrlrequest),
1231 DMA_TO_DEVICE);
1232 // FIXME generic api broken like pci, can't report errors
1233 // if (urb->transfer_dma == DMA_ADDR_INVALID) return 0;
1234 } else
1235 urb->transfer_dma = ~0;
1236 urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP
1237 | URB_NO_SETUP_DMA_MAP);
1238 return urb;
1239}
1240#endif /* 0 */
1241
1242/* XXX DISABLED, no users currently. If you wish to re-enable this
1243 * XXX please determine whether the sync is to transfer ownership of
1244 * XXX the buffer from device to cpu or vice verse, and thusly use the
1245 * XXX appropriate _for_{cpu,device}() method. -DaveM
1246 */
1247#if 0
1248
1249/**
1250 * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s)
1251 * @urb: urb whose transfer_buffer/setup_packet will be synchronized
1252 */
1253void usb_buffer_dmasync (struct urb *urb)
1254{
1255 struct usb_bus *bus;
1256 struct device *controller;
1257
1258 if (!urb
1259 || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1260 || !urb->dev
1261 || !(bus = urb->dev->bus)
1262 || !(controller = bus->controller))
1263 return;
1264
1265 if (controller->dma_mask) {
1266 dma_sync_single (controller,
1267 urb->transfer_dma, urb->transfer_buffer_length,
1268 usb_pipein (urb->pipe)
1269 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1270 if (usb_pipecontrol (urb->pipe))
1271 dma_sync_single (controller,
1272 urb->setup_dma,
1273 sizeof (struct usb_ctrlrequest),
1274 DMA_TO_DEVICE);
1275 }
1276}
1277#endif
1278
1279/**
1280 * usb_buffer_unmap - free DMA mapping(s) for an urb
1281 * @urb: urb whose transfer_buffer will be unmapped
1282 *
1283 * Reverses the effect of usb_buffer_map().
1284 */
1285#if 0
1286void usb_buffer_unmap (struct urb *urb)
1287{
1288 struct usb_bus *bus;
1289 struct device *controller;
1290
1291 if (!urb
1292 || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1293 || !urb->dev
1294 || !(bus = urb->dev->bus)
1295 || !(controller = bus->controller))
1296 return;
1297
1298 if (controller->dma_mask) {
1299 dma_unmap_single (controller,
1300 urb->transfer_dma, urb->transfer_buffer_length,
1301 usb_pipein (urb->pipe)
1302 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1303 if (usb_pipecontrol (urb->pipe))
1304 dma_unmap_single (controller,
1305 urb->setup_dma,
1306 sizeof (struct usb_ctrlrequest),
1307 DMA_TO_DEVICE);
1308 }
1309 urb->transfer_flags &= ~(URB_NO_TRANSFER_DMA_MAP
1310 | URB_NO_SETUP_DMA_MAP);
1311}
1312#endif /* 0 */
1313
1314/**
1315 * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
1316 * @dev: device to which the scatterlist will be mapped
1317 * @pipe: endpoint defining the mapping direction
1318 * @sg: the scatterlist to map
1319 * @nents: the number of entries in the scatterlist
1320 *
1321 * Return value is either < 0 (indicating no buffers could be mapped), or
1322 * the number of DMA mapping array entries in the scatterlist.
1323 *
1324 * The caller is responsible for placing the resulting DMA addresses from
1325 * the scatterlist into URB transfer buffer pointers, and for setting the
1326 * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs.
1327 *
1328 * Top I/O rates come from queuing URBs, instead of waiting for each one
1329 * to complete before starting the next I/O. This is particularly easy
1330 * to do with scatterlists. Just allocate and submit one URB for each DMA
1331 * mapping entry returned, stopping on the first error or when all succeed.
1332 * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
1333 *
1334 * This call would normally be used when translating scatterlist requests,
1335 * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
1336 * may be able to coalesce mappings for improved I/O efficiency.
1337 *
1338 * Reverse the effect of this call with usb_buffer_unmap_sg().
1339 */
1340int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
1341 struct scatterlist *sg, int nents)
1342{
1343 struct usb_bus *bus;
1344 struct device *controller;
1345
1346 if (!dev
1347 || usb_pipecontrol (pipe)
1348 || !(bus = dev->bus)
1349 || !(controller = bus->controller)
1350 || !controller->dma_mask)
1351 return -1;
1352
1353 // FIXME generic api broken like pci, can't report errors
1354 return dma_map_sg (controller, sg, nents,
1355 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1356}
1357
1358/* XXX DISABLED, no users currently. If you wish to re-enable this
1359 * XXX please determine whether the sync is to transfer ownership of
1360 * XXX the buffer from device to cpu or vice verse, and thusly use the
1361 * XXX appropriate _for_{cpu,device}() method. -DaveM
1362 */
1363#if 0
1364
1365/**
1366 * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s)
1367 * @dev: device to which the scatterlist will be mapped
1368 * @pipe: endpoint defining the mapping direction
1369 * @sg: the scatterlist to synchronize
1370 * @n_hw_ents: the positive return value from usb_buffer_map_sg
1371 *
1372 * Use this when you are re-using a scatterlist's data buffers for
1373 * another USB request.
1374 */
1375void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
1376 struct scatterlist *sg, int n_hw_ents)
1377{
1378 struct usb_bus *bus;
1379 struct device *controller;
1380
1381 if (!dev
1382 || !(bus = dev->bus)
1383 || !(controller = bus->controller)
1384 || !controller->dma_mask)
1385 return;
1386
1387 dma_sync_sg (controller, sg, n_hw_ents,
1388 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1389}
1390#endif
1391
1392/**
1393 * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
1394 * @dev: device to which the scatterlist will be mapped
1395 * @pipe: endpoint defining the mapping direction
1396 * @sg: the scatterlist to unmap
1397 * @n_hw_ents: the positive return value from usb_buffer_map_sg
1398 *
1399 * Reverses the effect of usb_buffer_map_sg().
1400 */
1401void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
1402 struct scatterlist *sg, int n_hw_ents)
1403{
1404 struct usb_bus *bus;
1405 struct device *controller;
1406
1407 if (!dev
1408 || !(bus = dev->bus)
1409 || !(controller = bus->controller)
1410 || !controller->dma_mask)
1411 return;
1412
1413 dma_unmap_sg (controller, sg, n_hw_ents,
1414 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1415}
1416
David Brownell390a8c32005-09-13 19:57:27 -07001417static int verify_suspended(struct device *dev, void *unused)
1418{
1419 return (dev->power.power_state.event == PM_EVENT_ON) ? -EBUSY : 0;
1420}
1421
David Brownell27d72e82005-04-18 17:39:22 -07001422static int usb_generic_suspend(struct device *dev, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
David Brownelldb690872005-09-13 19:56:33 -07001424 struct usb_interface *intf;
1425 struct usb_driver *driver;
1426 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
David Brownell390a8c32005-09-13 19:57:27 -07001428 /* USB devices enter SUSPEND state through their hubs, but can be
1429 * marked for FREEZE as soon as their children are already idled.
1430 */
1431 if (dev->driver == &usb_generic_driver) {
1432 if (dev->power.power_state.event == message.event)
1433 return 0;
1434 /* we need to rule out bogus requests through sysfs */
1435 status = device_for_each_child(dev, NULL, verify_suspended);
1436 if (status)
1437 return status;
1438 if (message.event == PM_EVENT_FREEZE) {
1439 dev->power.power_state = message;
1440 return 0;
1441 }
1442 return usb_suspend_device (to_usb_device(dev));
1443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 if ((dev->driver == NULL) ||
1446 (dev->driver_data == &usb_generic_driver_data))
1447 return 0;
1448
1449 intf = to_usb_interface(dev);
1450 driver = to_usb_driver(dev->driver);
1451
David Brownelldb690872005-09-13 19:56:33 -07001452 /* with no hardware, USB interfaces only use FREEZE and ON states */
1453 if (!is_active(intf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return 0;
1455
David Brownelldb690872005-09-13 19:56:33 -07001456 if (driver->suspend && driver->resume) {
1457 status = driver->suspend(intf, message);
1458 if (status)
1459 dev_err(dev, "%s error %d\n", "suspend", status);
1460 else
1461 mark_quiesced(intf);
1462 } else {
1463 // FIXME else if there's no suspend method, disconnect...
1464 dev_warn(dev, "no %s?\n", "suspend");
1465 status = 0;
1466 }
1467 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
1470static int usb_generic_resume(struct device *dev)
1471{
David Brownelldb690872005-09-13 19:56:33 -07001472 struct usb_interface *intf;
1473 struct usb_driver *driver;
1474 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
David Brownelldb690872005-09-13 19:56:33 -07001476 if (dev->power.power_state.event == PM_EVENT_ON)
1477 return 0;
1478
1479 /* devices resume through their hubs */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 if (dev->driver == &usb_generic_driver)
1481 return usb_resume_device (to_usb_device(dev));
1482
1483 if ((dev->driver == NULL) ||
1484 (dev->driver_data == &usb_generic_driver_data))
1485 return 0;
1486
1487 intf = to_usb_interface(dev);
1488 driver = to_usb_driver(dev->driver);
1489
David Brownelldb690872005-09-13 19:56:33 -07001490 /* if driver was suspended, it has a resume method;
1491 * however, sysfs can wrongly mark things as suspended
1492 * (on the "no suspend method" FIXME path above)
1493 */
1494 mark_active(intf);
1495 if (driver->resume) {
1496 status = driver->resume(intf);
1497 if (status) {
1498 dev_err(dev, "%s error %d\n", "resume", status);
1499 mark_quiesced(intf);
1500 }
1501 } else
1502 dev_warn(dev, "no %s?\n", "resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return 0;
1504}
1505
1506struct bus_type usb_bus_type = {
1507 .name = "usb",
1508 .match = usb_device_match,
1509 .hotplug = usb_hotplug,
1510 .suspend = usb_generic_suspend,
1511 .resume = usb_generic_resume,
1512};
1513
1514#ifndef MODULE
1515
1516static int __init usb_setup_disable(char *str)
1517{
1518 nousb = 1;
1519 return 1;
1520}
1521
1522/* format to disable USB on kernel command line is: nousb */
1523__setup("nousb", usb_setup_disable);
1524
1525#endif
1526
1527/*
1528 * for external read access to <nousb>
1529 */
1530int usb_disabled(void)
1531{
1532 return nousb;
1533}
1534
1535/*
1536 * Init
1537 */
1538static int __init usb_init(void)
1539{
1540 int retval;
1541 if (nousb) {
1542 pr_info ("%s: USB support disabled\n", usbcore_name);
1543 return 0;
1544 }
1545
1546 retval = bus_register(&usb_bus_type);
1547 if (retval)
1548 goto out;
1549 retval = usb_host_init();
1550 if (retval)
1551 goto host_init_failed;
1552 retval = usb_major_init();
1553 if (retval)
1554 goto major_init_failed;
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001555 retval = usb_register(&usbfs_driver);
1556 if (retval)
1557 goto driver_register_failed;
1558 retval = usbdev_init();
1559 if (retval)
1560 goto usbdevice_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 retval = usbfs_init();
1562 if (retval)
1563 goto fs_init_failed;
1564 retval = usb_hub_init();
1565 if (retval)
1566 goto hub_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 retval = driver_register(&usb_generic_driver);
1568 if (!retval)
1569 goto out;
1570
1571 usb_hub_cleanup();
1572hub_init_failed:
1573 usbfs_cleanup();
1574fs_init_failed:
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001575 usbdev_cleanup();
1576usbdevice_init_failed:
1577 usb_deregister(&usbfs_driver);
1578driver_register_failed:
1579 usb_major_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580major_init_failed:
1581 usb_host_cleanup();
1582host_init_failed:
1583 bus_unregister(&usb_bus_type);
1584out:
1585 return retval;
1586}
1587
1588/*
1589 * Cleanup
1590 */
1591static void __exit usb_exit(void)
1592{
1593 /* This will matter if shutdown/reboot does exitcalls. */
1594 if (nousb)
1595 return;
1596
1597 driver_unregister(&usb_generic_driver);
1598 usb_major_cleanup();
1599 usbfs_cleanup();
Kay Sieversfbf82fd2005-07-31 01:05:53 +02001600 usb_deregister(&usbfs_driver);
1601 usbdev_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 usb_hub_cleanup();
1603 usb_host_cleanup();
1604 bus_unregister(&usb_bus_type);
1605}
1606
1607subsys_initcall(usb_init);
1608module_exit(usb_exit);
1609
1610/*
1611 * USB may be built into the kernel or be built as modules.
1612 * These symbols are exported for device (or host controller)
1613 * driver modules to use.
1614 */
1615
1616EXPORT_SYMBOL(usb_register);
1617EXPORT_SYMBOL(usb_deregister);
1618EXPORT_SYMBOL(usb_disabled);
1619
brian@murphy.dka3fdf4e2005-06-29 16:53:29 -07001620EXPORT_SYMBOL_GPL(usb_get_intf);
1621EXPORT_SYMBOL_GPL(usb_put_intf);
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623EXPORT_SYMBOL(usb_alloc_dev);
1624EXPORT_SYMBOL(usb_put_dev);
1625EXPORT_SYMBOL(usb_get_dev);
1626EXPORT_SYMBOL(usb_hub_tt_clear_buffer);
1627
1628EXPORT_SYMBOL(usb_lock_device);
1629EXPORT_SYMBOL(usb_trylock_device);
1630EXPORT_SYMBOL(usb_lock_device_for_reset);
1631EXPORT_SYMBOL(usb_unlock_device);
1632
1633EXPORT_SYMBOL(usb_driver_claim_interface);
1634EXPORT_SYMBOL(usb_driver_release_interface);
1635EXPORT_SYMBOL(usb_match_id);
1636EXPORT_SYMBOL(usb_find_interface);
1637EXPORT_SYMBOL(usb_ifnum_to_if);
1638EXPORT_SYMBOL(usb_altnum_to_altsetting);
1639
1640EXPORT_SYMBOL(usb_reset_device);
1641EXPORT_SYMBOL(usb_disconnect);
1642
1643EXPORT_SYMBOL(__usb_get_extra_descriptor);
1644
1645EXPORT_SYMBOL(usb_find_device);
1646EXPORT_SYMBOL(usb_get_current_frame_number);
1647
1648EXPORT_SYMBOL (usb_buffer_alloc);
1649EXPORT_SYMBOL (usb_buffer_free);
1650
1651#if 0
1652EXPORT_SYMBOL (usb_buffer_map);
1653EXPORT_SYMBOL (usb_buffer_dmasync);
1654EXPORT_SYMBOL (usb_buffer_unmap);
1655#endif
1656
1657EXPORT_SYMBOL (usb_buffer_map_sg);
1658#if 0
1659EXPORT_SYMBOL (usb_buffer_dmasync_sg);
1660#endif
1661EXPORT_SYMBOL (usb_buffer_unmap_sg);
1662
1663MODULE_LICENSE("GPL");