Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * drivers/usb/core/generic.c - generic driver for USB devices (not interfaces)
0004  *
0005  * (C) Copyright 2005 Greg Kroah-Hartman <gregkh@suse.de>
0006  *
0007  * based on drivers/usb/usb.c which had the following copyrights:
0008  *  (C) Copyright Linus Torvalds 1999
0009  *  (C) Copyright Johannes Erdfelt 1999-2001
0010  *  (C) Copyright Andreas Gal 1999
0011  *  (C) Copyright Gregory P. Smith 1999
0012  *  (C) Copyright Deti Fliegl 1999 (new USB architecture)
0013  *  (C) Copyright Randy Dunlap 2000
0014  *  (C) Copyright David Brownell 2000-2004
0015  *  (C) Copyright Yggdrasil Computing, Inc. 2000
0016  *      (usb_device_id matching changes by Adam J. Richter)
0017  *  (C) Copyright Greg Kroah-Hartman 2002-2003
0018  *
0019  * Released under the GPLv2 only.
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         /* It's possible that a config has no interfaces! */
0073         if (c->desc.bNumInterfaces > 0)
0074             desc = &c->intf_cache[0]->altsetting->desc;
0075 
0076         /*
0077          * HP's USB bus-powered keyboard has only one configuration
0078          * and it claims to be self-powered; other devices may have
0079          * similar errors in their descriptors.  If the next test
0080          * were allowed to execute, such configurations would always
0081          * be rejected and the devices would not work as expected.
0082          * In the meantime, we run the risk of selecting a config
0083          * that requires external power at a time when that power
0084          * isn't available.  It seems to be the lesser of two evils.
0085          *
0086          * Bugzilla #6448 reports a device that appears to crash
0087          * when it receives a GET_DEVICE_STATUS request!  We don't
0088          * have any other way to tell whether a device is self-powered,
0089          * but since we don't use that information anywhere but here,
0090          * the call has been removed.
0091          *
0092          * Maybe the GET_DEVICE_STATUS call and the test below can
0093          * be reinstated when device firmwares become more reliable.
0094          * Don't hold your breath.
0095          */
0096 #if 0
0097         /* Rule out self-powered configs for a bus-powered device */
0098         if (bus_powered && (c->desc.bmAttributes &
0099                     USB_CONFIG_ATT_SELFPOWER))
0100             continue;
0101 #endif
0102 
0103         /*
0104          * The next test may not be as effective as it should be.
0105          * Some hubs have errors in their descriptor, claiming
0106          * to be self-powered when they are really bus-powered.
0107          * We will overestimate the amount of current such hubs
0108          * make available for each port.
0109          *
0110          * This is a fairly benign sort of failure.  It won't
0111          * cause us to reject configurations that we should have
0112          * accepted.
0113          */
0114 
0115         /* Rule out configs that draw too much bus current */
0116         if (usb_get_max_power(udev, c) > udev->bus_mA) {
0117             insufficient_power++;
0118             continue;
0119         }
0120 
0121         /*
0122          * Select first configuration as default for audio so that
0123          * devices that don't comply with UAC3 protocol are supported.
0124          * But, still iterate through other configurations and
0125          * select UAC3 compliant config if present.
0126          */
0127         if (desc && is_audio(desc)) {
0128             /* Always prefer the first found UAC3 config */
0129             if (is_uac3_config(desc)) {
0130                 best = c;
0131                 break;
0132             }
0133 
0134             /* If there is no UAC3 config, prefer the first config */
0135             else if (i == 0)
0136                 best = c;
0137 
0138             /* Unconditional continue, because the rest of the code
0139              * in the loop is irrelevant for audio devices, and
0140              * because it can reassign best, which for audio devices
0141              * we don't want.
0142              */
0143             continue;
0144         }
0145 
0146         /* When the first config's first interface is one of Microsoft's
0147          * pet nonstandard Ethernet-over-USB protocols, ignore it unless
0148          * this kernel has enabled the necessary host side driver.
0149          * But: Don't ignore it if it's the only config.
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         /* From the remaining configs, choose the first one whose
0161          * first interface is for a non-vendor-specific class.
0162          * Reason: Linux is more likely to have a class driver
0163          * than a vendor-specific driver. */
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         /* If all the remaining configs are vendor-specific,
0173          * choose the first one. */
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      * If any other driver wants the device, leave the device to this other
0218      * driver.
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     /* Choose and set the configuration.  This registers the interfaces
0231      * with the driver core and lets interface drivers bind to them.
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                 /* This need not be fatal.  The user can try to
0243                  * set other configurations. */
0244             }
0245         }
0246     }
0247     /* USB device state == configured ... usable */
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     /* if this is only an unbind, not a physical disconnect, then
0258      * unconfigure the device */
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     /* Normal USB devices suspend through their upstream port.
0270      * Root hubs don't have upstream ports to suspend,
0271      * so we have to shut down their downstream HC-to-USB
0272      * interfaces manually by doing a bus (or "global") suspend.
0273      */
0274     if (!udev->parent)
0275         rc = hcd_bus_suspend(udev, msg);
0276 
0277     /*
0278      * Non-root USB2 devices don't need to do anything for FREEZE
0279      * or PRETHAW. USB3 devices don't support global suspend and
0280      * needs to be selectively suspended.
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     /* Normal USB devices resume/reset through their upstream port.
0298      * Root hubs don't have upstream ports to resume or reset,
0299      * so we have to start up their downstream HC-to-USB
0300      * interfaces manually by doing a bus (or "global") resume.
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  /* CONFIG_PM */
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 };