aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/core/generic.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/core/generic.c')
-rw-r--r--drivers/usb/core/generic.c184
1 files changed, 184 insertions, 0 deletions
diff --git a/drivers/usb/core/generic.c b/drivers/usb/core/generic.c
index fa6f34a12b4b..1522195de715 100644
--- a/drivers/usb/core/generic.c
+++ b/drivers/usb/core/generic.c
@@ -21,24 +21,208 @@
21#include <linux/usb.h> 21#include <linux/usb.h>
22#include "usb.h" 22#include "usb.h"
23 23
24static inline const char *plural(int n)
25{
26 return (n == 1 ? "" : "s");
27}
28
29static int choose_configuration(struct usb_device *udev)
30{
31 int i;
32 int num_configs;
33 int insufficient_power = 0;
34 struct usb_host_config *c, *best;
35
36 best = NULL;
37 c = udev->config;
38 num_configs = udev->descriptor.bNumConfigurations;
39 for (i = 0; i < num_configs; (i++, c++)) {
40 struct usb_interface_descriptor *desc = NULL;
41
42 /* It's possible that a config has no interfaces! */
43 if (c->desc.bNumInterfaces > 0)
44 desc = &c->intf_cache[0]->altsetting->desc;
45
46 /*
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.
55 *
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.
61 *
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.
65 */
66#if 0
67 /* Rule out self-powered configs for a bus-powered device */
68 if (bus_powered && (c->desc.bmAttributes &
69 USB_CONFIG_ATT_SELFPOWER))
70 continue;
71#endif
72
73 /*
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.
79 *
80 * This is a fairly benign sort of failure. It won't
81 * cause us to reject configurations that we should have
82 * accepted.
83 */
84
85 /* Rule out configs that draw too much bus current */
86 if (c->desc.bMaxPower * 2 > udev->bus_mA) {
87 insufficient_power++;
88 continue;
89 }
90
91 /* If the first config's first interface is COMM/2/0xff
92 * (MSFT RNDIS), rule it out unless Linux has host-side
93 * RNDIS support. */
94 if (i == 0 && desc
95 && desc->bInterfaceClass == USB_CLASS_COMM
96 && desc->bInterfaceSubClass == 2
97 && desc->bInterfaceProtocol == 0xff) {
98#ifndef CONFIG_USB_NET_RNDIS_HOST
99 continue;
100#else
101 best = c;
102#endif
103 }
104
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)) {
113 best = c;
114 break;
115 }
116
117 /* If all the remaining configs are vendor-specific,
118 * choose the first one. */
119 else if (!best)
120 best = c;
121 }
122
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));
127
128 if (best) {
129 i = best->desc.bConfigurationValue;
130 dev_info(&udev->dev,
131 "configuration #%d chosen from %d choice%s\n",
132 i, num_configs, plural(num_configs));
133 } else {
134 i = -1;
135 dev_warn(&udev->dev,
136 "no configuration chosen from %d choice%s\n",
137 num_configs, plural(num_configs));
138 }
139 return i;
140}
141
24static int generic_probe(struct usb_device *udev) 142static int generic_probe(struct usb_device *udev)
25{ 143{
144 int err, c;
145
146 /* put device-specific files into sysfs */
147 usb_create_sysfs_dev_files(udev);
148
149 /* Choose and set the configuration. This registers the interfaces
150 * with the driver core and lets interface drivers bind to them.
151 */
152 c = choose_configuration(udev);
153 if (c >= 0) {
154 err = usb_set_configuration(udev, c);
155 if (err) {
156 dev_err(&udev->dev, "can't set config #%d, error %d\n",
157 c, err);
158 /* This need not be fatal. The user can try to
159 * set other configurations. */
160 }
161 }
162
163 /* USB device state == configured ... usable */
164 usb_notify_add_device(udev);
165
26 return 0; 166 return 0;
27} 167}
168
28static void generic_disconnect(struct usb_device *udev) 169static void generic_disconnect(struct usb_device *udev)
29{ 170{
171 usb_notify_remove_device(udev);
172
30 /* if this is only an unbind, not a physical disconnect, then 173 /* if this is only an unbind, not a physical disconnect, then
31 * unconfigure the device */ 174 * unconfigure the device */
32 if (udev->state == USB_STATE_CONFIGURED) 175 if (udev->state == USB_STATE_CONFIGURED)
33 usb_set_configuration(udev, 0); 176 usb_set_configuration(udev, 0);
34 177
178 usb_remove_sysfs_dev_files(udev);
179
35 /* in case the call failed or the device was suspended */ 180 /* in case the call failed or the device was suspended */
36 if (udev->state >= USB_STATE_CONFIGURED) 181 if (udev->state >= USB_STATE_CONFIGURED)
37 usb_disable_device(udev, 0); 182 usb_disable_device(udev, 0);
38} 183}
39 184
185#ifdef CONFIG_PM
186
187static int verify_suspended(struct device *dev, void *unused)
188{
189 if (dev->driver == NULL)
190 return 0;
191 return (dev->power.power_state.event == PM_EVENT_ON) ? -EBUSY : 0;
192}
193
194static int generic_suspend(struct usb_device *udev, pm_message_t msg)
195{
196 int status;
197
198 /* rule out bogus requests through sysfs */
199 status = device_for_each_child(&udev->dev, NULL, verify_suspended);
200 if (status)
201 return status;
202
203 /* USB devices enter SUSPEND state through their hubs, but can be
204 * marked for FREEZE as soon as their children are already idled.
205 * But those semantics are useless, so we equate the two (sigh).
206 */
207 return usb_port_suspend(udev);
208}
209
210static int generic_resume(struct usb_device *udev)
211{
212 if (udev->state == USB_STATE_NOTATTACHED)
213 return 0;
214
215 return usb_port_resume(udev);
216}
217
218#endif /* CONFIG_PM */
219
40struct usb_device_driver usb_generic_driver = { 220struct usb_device_driver usb_generic_driver = {
41 .name = "usb", 221 .name = "usb",
42 .probe = generic_probe, 222 .probe = generic_probe,
43 .disconnect = generic_disconnect, 223 .disconnect = generic_disconnect,
224#ifdef CONFIG_PM
225 .suspend = generic_suspend,
226 .resume = generic_resume,
227#endif
44}; 228};