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