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