USB: Select better matching USB drivers when available
authorBastien Nocera <hadess@hadess.net>
Wed, 16 Oct 2019 09:39:31 +0000 (11:39 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 12 Feb 2020 19:01:55 +0000 (11:01 -0800)
Now that USB device drivers can reuse code from the generic USB device
driver, we need to make sure that they get selected rather than the
generic driver. Add an id_table and match vfunc to the usb_device_driver
struct, which will get used to select a better matching driver at
->probe time.

This is a similar mechanism to that used in the HID drivers, with the
generic driver being selected unless there's a better matching one found
in the registered drivers (see hid_generic_match() in
drivers/hid/hid-generic.c).

Signed-off-by: Bastien Nocera <hadess@hadess.net>
Acked-by: Alan Stern <stern@rowland.harvard.edu>
Link: https://lore.kernel.org/r/20191016093933.693-5-hadess@hadess.net
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/usb/core/driver.c
drivers/usb/core/generic.c
include/linux/usb.h

index 697898327b44b118ea007b98aa77bf9c4b1636c0..9d1502a9571dd013f5bca492410c57189d081d1c 100644 (file)
@@ -818,13 +818,24 @@ static int usb_device_match(struct device *dev, struct device_driver *drv)
 {
        /* devices and interfaces are handled separately */
        if (is_usb_device(dev)) {
+               struct usb_device *udev;
+               struct usb_device_driver *udrv;
 
                /* interface drivers never match devices */
                if (!is_usb_device_driver(drv))
                        return 0;
 
-               /* TODO: Add real matching code */
-               return 1;
+               udev = to_usb_device(dev);
+               udrv = to_usb_device_driver(drv);
+
+               if (udrv->id_table &&
+                   usb_device_match_id(udev, udrv->id_table) != NULL) {
+                       return 1;
+               }
+
+               if (udrv->match)
+                       return udrv->match(udev);
+               return 0;
 
        } else if (is_usb_interface(dev)) {
                struct usb_interface *intf;
index 28ece4d777499d235d0f3ca642ba3700f71bd02c..84da85c13825d1496bf57f3f3bb12186c8ef19c6 100644 (file)
@@ -195,6 +195,34 @@ int usb_choose_configuration(struct usb_device *udev)
 }
 EXPORT_SYMBOL_GPL(usb_choose_configuration);
 
+static int __check_usb_generic(struct device_driver *drv, void *data)
+{
+       struct usb_device *udev = data;
+       struct usb_device_driver *udrv;
+
+       if (!is_usb_device_driver(drv))
+               return 0;
+       udrv = to_usb_device_driver(drv);
+       if (udrv == &usb_generic_driver)
+               return 0;
+       if (!udrv->id_table)
+               return 0;
+
+       return usb_device_match_id(udev, udrv->id_table) != NULL;
+}
+
+static bool usb_generic_driver_match(struct usb_device *udev)
+{
+       /*
+        * If any other driver wants the device, leave the device to this other
+        * driver.
+        */
+       if (bus_for_each_drv(&usb_bus_type, NULL, udev, __check_usb_generic))
+               return false;
+
+       return true;
+}
+
 int usb_generic_driver_probe(struct usb_device *udev)
 {
        int err, c;
@@ -285,6 +313,7 @@ int usb_generic_driver_resume(struct usb_device *udev, pm_message_t msg)
 
 struct usb_device_driver usb_generic_driver = {
        .name = "usb",
+       .match = usb_generic_driver_match,
        .probe = usb_generic_driver_probe,
        .disconnect = usb_generic_driver_disconnect,
 #ifdef CONFIG_PM
index 94bd3b48a485c4724e017811faccd7627a130525..3663bd7b1fa4fa466c83470756fbf285bbc546bb 100644 (file)
@@ -1237,6 +1237,7 @@ struct usb_driver {
 struct usb_device_driver {
        const char *name;
 
+       bool (*match) (struct usb_device *udev);
        int (*probe) (struct usb_device *udev);
        void (*disconnect) (struct usb_device *udev);
 
@@ -1244,6 +1245,7 @@ struct usb_device_driver {
        int (*resume) (struct usb_device *udev, pm_message_t message);
        const struct attribute_group **dev_groups;
        struct usbdrv_wrap drvwrap;
+       const struct usb_device_id *id_table;
        unsigned int supports_autosuspend:1;
        unsigned int generic_subclass:1;
 };