blob: b531a4fd30c228b47fda66611fa304c75657eea2 [file] [log] [blame]
Alan Stern36e56a32006-07-01 22:08:06 -04001/*
2 * drivers/usb/generic.c - generic driver for USB devices (not interfaces)
3 *
4 * (C) Copyright 2005 Greg Kroah-Hartman <gregkh@suse.de>
5 *
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
17 *
18 */
19
Alan Stern36e56a32006-07-01 22:08:06 -040020#include <linux/usb.h>
21#include "usb.h"
22
Alan Stern782da722006-07-01 22:09:35 -040023static inline const char *plural(int n)
24{
25 return (n == 1 ? "" : "s");
26}
27
Ole Andre Vadla Ravnasad55d712006-12-14 16:01:28 -080028static int is_rndis(struct usb_interface_descriptor *desc)
29{
30 return desc->bInterfaceClass == USB_CLASS_COMM
31 && desc->bInterfaceSubClass == 2
32 && desc->bInterfaceProtocol == 0xff;
33}
34
35static int is_activesync(struct usb_interface_descriptor *desc)
36{
37 return desc->bInterfaceClass == USB_CLASS_MISC
38 && desc->bInterfaceSubClass == 1
39 && desc->bInterfaceProtocol == 1;
40}
41
Alan Stern782da722006-07-01 22:09:35 -040042static int choose_configuration(struct usb_device *udev)
43{
44 int i;
45 int num_configs;
46 int insufficient_power = 0;
47 struct usb_host_config *c, *best;
48
49 best = NULL;
50 c = udev->config;
51 num_configs = udev->descriptor.bNumConfigurations;
52 for (i = 0; i < num_configs; (i++, c++)) {
53 struct usb_interface_descriptor *desc = NULL;
54
55 /* It's possible that a config has no interfaces! */
56 if (c->desc.bNumInterfaces > 0)
57 desc = &c->intf_cache[0]->altsetting->desc;
58
59 /*
60 * HP's USB bus-powered keyboard has only one configuration
61 * and it claims to be self-powered; other devices may have
62 * similar errors in their descriptors. If the next test
63 * were allowed to execute, such configurations would always
64 * be rejected and the devices would not work as expected.
65 * In the meantime, we run the risk of selecting a config
66 * that requires external power at a time when that power
67 * isn't available. It seems to be the lesser of two evils.
68 *
69 * Bugzilla #6448 reports a device that appears to crash
70 * when it receives a GET_DEVICE_STATUS request! We don't
71 * have any other way to tell whether a device is self-powered,
72 * but since we don't use that information anywhere but here,
73 * the call has been removed.
74 *
75 * Maybe the GET_DEVICE_STATUS call and the test below can
76 * be reinstated when device firmwares become more reliable.
77 * Don't hold your breath.
78 */
79#if 0
80 /* Rule out self-powered configs for a bus-powered device */
81 if (bus_powered && (c->desc.bmAttributes &
82 USB_CONFIG_ATT_SELFPOWER))
83 continue;
84#endif
85
86 /*
87 * The next test may not be as effective as it should be.
88 * Some hubs have errors in their descriptor, claiming
89 * to be self-powered when they are really bus-powered.
90 * We will overestimate the amount of current such hubs
91 * make available for each port.
92 *
93 * This is a fairly benign sort of failure. It won't
94 * cause us to reject configurations that we should have
95 * accepted.
96 */
97
98 /* Rule out configs that draw too much bus current */
99 if (c->desc.bMaxPower * 2 > udev->bus_mA) {
100 insufficient_power++;
101 continue;
102 }
103
Ole Andre Vadla Ravnasad55d712006-12-14 16:01:28 -0800104 /* When the first config's first interface is one of Microsoft's
105 * pet nonstandard Ethernet-over-USB protocols, ignore it unless
106 * this kernel has enabled the necessary host side driver.
107 */
108 if (i == 0 && desc && (is_rndis(desc) || is_activesync(desc))) {
109#if !defined(CONFIG_USB_NET_RNDIS_HOST) && !defined(CONFIG_USB_NET_RNDIS_HOST_MODULE)
Alan Stern782da722006-07-01 22:09:35 -0400110 continue;
111#else
112 best = c;
113#endif
114 }
115
116 /* From the remaining configs, choose the first one whose
117 * first interface is for a non-vendor-specific class.
118 * Reason: Linux is more likely to have a class driver
119 * than a vendor-specific driver. */
120 else if (udev->descriptor.bDeviceClass !=
121 USB_CLASS_VENDOR_SPEC &&
122 (!desc || desc->bInterfaceClass !=
123 USB_CLASS_VENDOR_SPEC)) {
124 best = c;
125 break;
126 }
127
128 /* If all the remaining configs are vendor-specific,
129 * choose the first one. */
130 else if (!best)
131 best = c;
132 }
133
134 if (insufficient_power > 0)
135 dev_info(&udev->dev, "rejected %d configuration%s "
136 "due to insufficient available bus power\n",
137 insufficient_power, plural(insufficient_power));
138
139 if (best) {
140 i = best->desc.bConfigurationValue;
141 dev_info(&udev->dev,
142 "configuration #%d chosen from %d choice%s\n",
143 i, num_configs, plural(num_configs));
144 } else {
145 i = -1;
146 dev_warn(&udev->dev,
147 "no configuration chosen from %d choice%s\n",
148 num_configs, plural(num_configs));
149 }
150 return i;
151}
152
Alan Stern8bb54ab2006-07-01 22:08:49 -0400153static int generic_probe(struct usb_device *udev)
Alan Stern36e56a32006-07-01 22:08:06 -0400154{
Alan Stern782da722006-07-01 22:09:35 -0400155 int err, c;
156
157 /* put device-specific files into sysfs */
158 usb_create_sysfs_dev_files(udev);
159
160 /* Choose and set the configuration. This registers the interfaces
161 * with the driver core and lets interface drivers bind to them.
162 */
163 c = choose_configuration(udev);
164 if (c >= 0) {
165 err = usb_set_configuration(udev, c);
166 if (err) {
167 dev_err(&udev->dev, "can't set config #%d, error %d\n",
168 c, err);
169 /* This need not be fatal. The user can try to
170 * set other configurations. */
171 }
172 }
173
174 /* USB device state == configured ... usable */
175 usb_notify_add_device(udev);
176
Alan Stern36e56a32006-07-01 22:08:06 -0400177 return 0;
178}
Alan Stern782da722006-07-01 22:09:35 -0400179
Alan Stern8bb54ab2006-07-01 22:08:49 -0400180static void generic_disconnect(struct usb_device *udev)
Alan Stern36e56a32006-07-01 22:08:06 -0400181{
Alan Stern782da722006-07-01 22:09:35 -0400182 usb_notify_remove_device(udev);
183
Alan Stern36e56a32006-07-01 22:08:06 -0400184 /* if this is only an unbind, not a physical disconnect, then
185 * unconfigure the device */
Alan Stern01d883d2006-08-30 15:47:18 -0400186 if (udev->actconfig)
Alan Stern36e56a32006-07-01 22:08:06 -0400187 usb_set_configuration(udev, 0);
188
Alan Stern782da722006-07-01 22:09:35 -0400189 usb_remove_sysfs_dev_files(udev);
Alan Stern36e56a32006-07-01 22:08:06 -0400190}
191
Alan Stern782da722006-07-01 22:09:35 -0400192#ifdef CONFIG_PM
193
Alan Stern782da722006-07-01 22:09:35 -0400194static int generic_suspend(struct usb_device *udev, pm_message_t msg)
195{
Alan Stern782da722006-07-01 22:09:35 -0400196 /* USB devices enter SUSPEND state through their hubs, but can be
197 * marked for FREEZE as soon as their children are already idled.
198 * But those semantics are useless, so we equate the two (sigh).
199 */
200 return usb_port_suspend(udev);
201}
202
203static int generic_resume(struct usb_device *udev)
204{
Alan Stern782da722006-07-01 22:09:35 -0400205 return usb_port_resume(udev);
206}
207
208#endif /* CONFIG_PM */
209
Alan Stern8bb54ab2006-07-01 22:08:49 -0400210struct usb_device_driver usb_generic_driver = {
Alan Stern36e56a32006-07-01 22:08:06 -0400211 .name = "usb",
Alan Stern36e56a32006-07-01 22:08:06 -0400212 .probe = generic_probe,
Alan Stern8bb54ab2006-07-01 22:08:49 -0400213 .disconnect = generic_disconnect,
Alan Stern782da722006-07-01 22:09:35 -0400214#ifdef CONFIG_PM
215 .suspend = generic_suspend,
216 .resume = generic_resume,
217#endif
Alan Stern01d883d2006-08-30 15:47:18 -0400218 .supports_autosuspend = 1,
Alan Stern36e56a32006-07-01 22:08:06 -0400219};