0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #include <linux/usb.h>
0023 #include <linux/usb/hcd.h>
0024 #include <uapi/linux/usb/audio.h>
0025 #include "usb.h"
0026
0027 static inline const char *plural(int n)
0028 {
0029 return (n == 1 ? "" : "s");
0030 }
0031
0032 static int is_rndis(struct usb_interface_descriptor *desc)
0033 {
0034 return desc->bInterfaceClass == USB_CLASS_COMM
0035 && desc->bInterfaceSubClass == 2
0036 && desc->bInterfaceProtocol == 0xff;
0037 }
0038
0039 static int is_activesync(struct usb_interface_descriptor *desc)
0040 {
0041 return desc->bInterfaceClass == USB_CLASS_MISC
0042 && desc->bInterfaceSubClass == 1
0043 && desc->bInterfaceProtocol == 1;
0044 }
0045
0046 static bool is_audio(struct usb_interface_descriptor *desc)
0047 {
0048 return desc->bInterfaceClass == USB_CLASS_AUDIO;
0049 }
0050
0051 static bool is_uac3_config(struct usb_interface_descriptor *desc)
0052 {
0053 return desc->bInterfaceProtocol == UAC_VERSION_3;
0054 }
0055
0056 int usb_choose_configuration(struct usb_device *udev)
0057 {
0058 int i;
0059 int num_configs;
0060 int insufficient_power = 0;
0061 struct usb_host_config *c, *best;
0062
0063 if (usb_device_is_owned(udev))
0064 return 0;
0065
0066 best = NULL;
0067 c = udev->config;
0068 num_configs = udev->descriptor.bNumConfigurations;
0069 for (i = 0; i < num_configs; (i++, c++)) {
0070 struct usb_interface_descriptor *desc = NULL;
0071
0072
0073 if (c->desc.bNumInterfaces > 0)
0074 desc = &c->intf_cache[0]->altsetting->desc;
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096 #if 0
0097
0098 if (bus_powered && (c->desc.bmAttributes &
0099 USB_CONFIG_ATT_SELFPOWER))
0100 continue;
0101 #endif
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116 if (usb_get_max_power(udev, c) > udev->bus_mA) {
0117 insufficient_power++;
0118 continue;
0119 }
0120
0121
0122
0123
0124
0125
0126
0127 if (desc && is_audio(desc)) {
0128
0129 if (is_uac3_config(desc)) {
0130 best = c;
0131 break;
0132 }
0133
0134
0135 else if (i == 0)
0136 best = c;
0137
0138
0139
0140
0141
0142
0143 continue;
0144 }
0145
0146
0147
0148
0149
0150
0151 if (i == 0 && num_configs > 1 && desc &&
0152 (is_rndis(desc) || is_activesync(desc))) {
0153 #if !defined(CONFIG_USB_NET_RNDIS_HOST) && !defined(CONFIG_USB_NET_RNDIS_HOST_MODULE)
0154 continue;
0155 #else
0156 best = c;
0157 #endif
0158 }
0159
0160
0161
0162
0163
0164 else if (udev->descriptor.bDeviceClass !=
0165 USB_CLASS_VENDOR_SPEC &&
0166 (desc && desc->bInterfaceClass !=
0167 USB_CLASS_VENDOR_SPEC)) {
0168 best = c;
0169 break;
0170 }
0171
0172
0173
0174 else if (!best)
0175 best = c;
0176 }
0177
0178 if (insufficient_power > 0)
0179 dev_info(&udev->dev, "rejected %d configuration%s "
0180 "due to insufficient available bus power\n",
0181 insufficient_power, plural(insufficient_power));
0182
0183 if (best) {
0184 i = best->desc.bConfigurationValue;
0185 dev_dbg(&udev->dev,
0186 "configuration #%d chosen from %d choice%s\n",
0187 i, num_configs, plural(num_configs));
0188 } else {
0189 i = -1;
0190 dev_warn(&udev->dev,
0191 "no configuration chosen from %d choice%s\n",
0192 num_configs, plural(num_configs));
0193 }
0194 return i;
0195 }
0196 EXPORT_SYMBOL_GPL(usb_choose_configuration);
0197
0198 static int __check_for_non_generic_match(struct device_driver *drv, void *data)
0199 {
0200 struct usb_device *udev = data;
0201 struct usb_device_driver *udrv;
0202
0203 if (!is_usb_device_driver(drv))
0204 return 0;
0205 udrv = to_usb_device_driver(drv);
0206 if (udrv == &usb_generic_driver)
0207 return 0;
0208 return usb_driver_applicable(udev, udrv);
0209 }
0210
0211 static bool usb_generic_driver_match(struct usb_device *udev)
0212 {
0213 if (udev->use_generic_driver)
0214 return true;
0215
0216
0217
0218
0219
0220 if (bus_for_each_drv(&usb_bus_type, NULL, udev, __check_for_non_generic_match))
0221 return false;
0222
0223 return true;
0224 }
0225
0226 int usb_generic_driver_probe(struct usb_device *udev)
0227 {
0228 int err, c;
0229
0230
0231
0232
0233 if (udev->authorized == 0)
0234 dev_err(&udev->dev, "Device is not authorized for usage\n");
0235 else {
0236 c = usb_choose_configuration(udev);
0237 if (c >= 0) {
0238 err = usb_set_configuration(udev, c);
0239 if (err && err != -ENODEV) {
0240 dev_err(&udev->dev, "can't set config #%d, error %d\n",
0241 c, err);
0242
0243
0244 }
0245 }
0246 }
0247
0248 usb_notify_add_device(udev);
0249
0250 return 0;
0251 }
0252
0253 void usb_generic_driver_disconnect(struct usb_device *udev)
0254 {
0255 usb_notify_remove_device(udev);
0256
0257
0258
0259 if (udev->actconfig)
0260 usb_set_configuration(udev, -1);
0261 }
0262
0263 #ifdef CONFIG_PM
0264
0265 int usb_generic_driver_suspend(struct usb_device *udev, pm_message_t msg)
0266 {
0267 int rc;
0268
0269
0270
0271
0272
0273
0274 if (!udev->parent)
0275 rc = hcd_bus_suspend(udev, msg);
0276
0277
0278
0279
0280
0281
0282 else if ((msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_PRETHAW)
0283 && (udev->speed < USB_SPEED_SUPER))
0284 rc = 0;
0285 else
0286 rc = usb_port_suspend(udev, msg);
0287
0288 if (rc == 0)
0289 usbfs_notify_suspend(udev);
0290 return rc;
0291 }
0292
0293 int usb_generic_driver_resume(struct usb_device *udev, pm_message_t msg)
0294 {
0295 int rc;
0296
0297
0298
0299
0300
0301
0302 if (!udev->parent)
0303 rc = hcd_bus_resume(udev, msg);
0304 else
0305 rc = usb_port_resume(udev, msg);
0306
0307 if (rc == 0)
0308 usbfs_notify_resume(udev);
0309 return rc;
0310 }
0311
0312 #endif
0313
0314 struct usb_device_driver usb_generic_driver = {
0315 .name = "usb",
0316 .match = usb_generic_driver_match,
0317 .probe = usb_generic_driver_probe,
0318 .disconnect = usb_generic_driver_disconnect,
0319 #ifdef CONFIG_PM
0320 .suspend = usb_generic_driver_suspend,
0321 .resume = usb_generic_driver_resume,
0322 #endif
0323 .supports_autosuspend = 1,
0324 };