2 * drivers/usb/generic.c - generic driver for USB devices (not interfaces)
4 * (C) Copyright 2005 Greg Kroah-Hartman <gregkh@suse.de>
6 * based on drivers/usb/usb.c which had the following copyrights:
7 * (C) Copyright Linus Torvalds 1999
8 * (C) Copyright Johannes Erdfelt 1999-2001
9 * (C) Copyright Andreas Gal 1999
10 * (C) Copyright Gregory P. Smith 1999
11 * (C) Copyright Deti Fliegl 1999 (new USB architecture)
12 * (C) Copyright Randy Dunlap 2000
13 * (C) Copyright David Brownell 2000-2004
14 * (C) Copyright Yggdrasil Computing, Inc. 2000
15 * (usb_device_id matching changes by Adam J. Richter)
16 * (C) Copyright Greg Kroah-Hartman 2002-2003
20 #include <linux/config.h>
21 #include <linux/usb.h>
24 static inline const char *plural(int n)
26 return (n == 1 ? "" : "s");
29 static int choose_configuration(struct usb_device *udev)
33 int insufficient_power = 0;
34 struct usb_host_config *c, *best;
38 num_configs = udev->descriptor.bNumConfigurations;
39 for (i = 0; i < num_configs; (i++, c++)) {
40 struct usb_interface_descriptor *desc = NULL;
42 /* It's possible that a config has no interfaces! */
43 if (c->desc.bNumInterfaces > 0)
44 desc = &c->intf_cache[0]->altsetting->desc;
47 * HP's USB bus-powered keyboard has only one configuration
48 * and it claims to be self-powered; other devices may have
49 * similar errors in their descriptors. If the next test
50 * were allowed to execute, such configurations would always
51 * be rejected and the devices would not work as expected.
52 * In the meantime, we run the risk of selecting a config
53 * that requires external power at a time when that power
54 * isn't available. It seems to be the lesser of two evils.
56 * Bugzilla #6448 reports a device that appears to crash
57 * when it receives a GET_DEVICE_STATUS request! We don't
58 * have any other way to tell whether a device is self-powered,
59 * but since we don't use that information anywhere but here,
60 * the call has been removed.
62 * Maybe the GET_DEVICE_STATUS call and the test below can
63 * be reinstated when device firmwares become more reliable.
64 * Don't hold your breath.
67 /* Rule out self-powered configs for a bus-powered device */
68 if (bus_powered && (c->desc.bmAttributes &
69 USB_CONFIG_ATT_SELFPOWER))
74 * The next test may not be as effective as it should be.
75 * Some hubs have errors in their descriptor, claiming
76 * to be self-powered when they are really bus-powered.
77 * We will overestimate the amount of current such hubs
78 * make available for each port.
80 * This is a fairly benign sort of failure. It won't
81 * cause us to reject configurations that we should have
85 /* Rule out configs that draw too much bus current */
86 if (c->desc.bMaxPower * 2 > udev->bus_mA) {
91 /* If the first config's first interface is COMM/2/0xff
92 * (MSFT RNDIS), rule it out unless Linux has host-side
95 && desc->bInterfaceClass == USB_CLASS_COMM
96 && desc->bInterfaceSubClass == 2
97 && desc->bInterfaceProtocol == 0xff) {
98 #ifndef CONFIG_USB_NET_RNDIS_HOST
105 /* From the remaining configs, choose the first one whose
106 * first interface is for a non-vendor-specific class.
107 * Reason: Linux is more likely to have a class driver
108 * than a vendor-specific driver. */
109 else if (udev->descriptor.bDeviceClass !=
110 USB_CLASS_VENDOR_SPEC &&
111 (!desc || desc->bInterfaceClass !=
112 USB_CLASS_VENDOR_SPEC)) {
117 /* If all the remaining configs are vendor-specific,
118 * choose the first one. */
123 if (insufficient_power > 0)
124 dev_info(&udev->dev, "rejected %d configuration%s "
125 "due to insufficient available bus power\n",
126 insufficient_power, plural(insufficient_power));
129 i = best->desc.bConfigurationValue;
131 "configuration #%d chosen from %d choice%s\n",
132 i, num_configs, plural(num_configs));
136 "no configuration chosen from %d choice%s\n",
137 num_configs, plural(num_configs));
142 static int generic_probe(struct usb_device *udev)
146 /* put device-specific files into sysfs */
147 usb_create_sysfs_dev_files(udev);
149 /* Choose and set the configuration. This registers the interfaces
150 * with the driver core and lets interface drivers bind to them.
152 c = choose_configuration(udev);
154 err = usb_set_configuration(udev, c);
156 dev_err(&udev->dev, "can't set config #%d, error %d\n",
158 /* This need not be fatal. The user can try to
159 * set other configurations. */
163 /* USB device state == configured ... usable */
164 usb_notify_add_device(udev);
169 static void generic_disconnect(struct usb_device *udev)
171 usb_notify_remove_device(udev);
173 /* if this is only an unbind, not a physical disconnect, then
174 * unconfigure the device */
176 usb_set_configuration(udev, 0);
178 usb_remove_sysfs_dev_files(udev);
183 static int generic_suspend(struct usb_device *udev, pm_message_t msg)
185 /* USB devices enter SUSPEND state through their hubs, but can be
186 * marked for FREEZE as soon as their children are already idled.
187 * But those semantics are useless, so we equate the two (sigh).
189 return usb_port_suspend(udev);
192 static int generic_resume(struct usb_device *udev)
194 return usb_port_resume(udev);
197 #endif /* CONFIG_PM */
199 struct usb_device_driver usb_generic_driver = {
201 .probe = generic_probe,
202 .disconnect = generic_disconnect,
204 .suspend = generic_suspend,
205 .resume = generic_resume,
207 .supports_autosuspend = 1,