Commit 0942d59b authored by Bastien Nocera's avatar Bastien Nocera Committed by Greg Kroah-Hartman

usbcore: Check both id_table and match() when both available

From: Bastien Nocera <hadess@hadess.net>

When a USB device driver has both an id_table and a match() function, make
sure to check both to find a match, first matching the id_table, then
checking the match() function.

This makes it possible to have module autoloading done through the
id_table when devices are plugged in, before checking for further
device eligibility in the match() function.

Cc: <stable@vger.kernel.org> # 5.8
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Alan Stern <stern@rowland.harvard.edu>
Co-developed-by: default avatarM. Vefa Bicakci <m.v.b@runbox.com>
Tested-by: default avatarBastien Nocera <hadess@hadess.net>
Signed-off-by: default avatarBastien Nocera <hadess@hadess.net>
Signed-off-by: default avatarM. Vefa Bicakci <m.v.b@runbox.com>
Tested-by: default avatarPan (Pany) YUAN <pany@fedoraproject.org>
Link: https://lore.kernel.org/r/20201022135521.375211-2-m.v.b@runbox.comSigned-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 32d174d2
...@@ -839,6 +839,22 @@ const struct usb_device_id *usb_device_match_id(struct usb_device *udev, ...@@ -839,6 +839,22 @@ const struct usb_device_id *usb_device_match_id(struct usb_device *udev,
return NULL; return NULL;
} }
bool usb_driver_applicable(struct usb_device *udev,
struct usb_device_driver *udrv)
{
if (udrv->id_table && udrv->match)
return usb_device_match_id(udev, udrv->id_table) != NULL &&
udrv->match(udev);
if (udrv->id_table)
return usb_device_match_id(udev, udrv->id_table) != NULL;
if (udrv->match)
return udrv->match(udev);
return false;
}
static int usb_device_match(struct device *dev, struct device_driver *drv) static int usb_device_match(struct device *dev, struct device_driver *drv)
{ {
/* devices and interfaces are handled separately */ /* devices and interfaces are handled separately */
...@@ -853,17 +869,14 @@ static int usb_device_match(struct device *dev, struct device_driver *drv) ...@@ -853,17 +869,14 @@ static int usb_device_match(struct device *dev, struct device_driver *drv)
udev = to_usb_device(dev); udev = to_usb_device(dev);
udrv = to_usb_device_driver(drv); udrv = to_usb_device_driver(drv);
if (udrv->id_table)
return usb_device_match_id(udev, udrv->id_table) != NULL;
if (udrv->match)
return udrv->match(udev);
/* If the device driver under consideration does not have a /* If the device driver under consideration does not have a
* id_table or a match function, then let the driver's probe * id_table or a match function, then let the driver's probe
* function decide. * function decide.
*/ */
return 1; if (!udrv->id_table && !udrv->match)
return 1;
return usb_driver_applicable(udev, udrv);
} else if (is_usb_interface(dev)) { } else if (is_usb_interface(dev)) {
struct usb_interface *intf; struct usb_interface *intf;
...@@ -941,8 +954,7 @@ static int __usb_bus_reprobe_drivers(struct device *dev, void *data) ...@@ -941,8 +954,7 @@ static int __usb_bus_reprobe_drivers(struct device *dev, void *data)
return 0; return 0;
udev = to_usb_device(dev); udev = to_usb_device(dev);
if (usb_device_match_id(udev, new_udriver->id_table) == NULL && if (!usb_driver_applicable(udev, new_udriver))
(!new_udriver->match || new_udriver->match(udev) == 0))
return 0; return 0;
ret = device_reprobe(dev); ret = device_reprobe(dev);
......
...@@ -205,9 +205,7 @@ static int __check_for_non_generic_match(struct device_driver *drv, void *data) ...@@ -205,9 +205,7 @@ static int __check_for_non_generic_match(struct device_driver *drv, void *data)
udrv = to_usb_device_driver(drv); udrv = to_usb_device_driver(drv);
if (udrv == &usb_generic_driver) if (udrv == &usb_generic_driver)
return 0; return 0;
if (usb_device_match_id(udev, udrv->id_table) != NULL) return usb_driver_applicable(udev, udrv);
return 1;
return (udrv->match && udrv->match(udev));
} }
static bool usb_generic_driver_match(struct usb_device *udev) static bool usb_generic_driver_match(struct usb_device *udev)
......
...@@ -74,6 +74,8 @@ extern int usb_match_device(struct usb_device *dev, ...@@ -74,6 +74,8 @@ extern int usb_match_device(struct usb_device *dev,
const struct usb_device_id *id); const struct usb_device_id *id);
extern const struct usb_device_id *usb_device_match_id(struct usb_device *udev, extern const struct usb_device_id *usb_device_match_id(struct usb_device *udev,
const struct usb_device_id *id); const struct usb_device_id *id);
extern bool usb_driver_applicable(struct usb_device *udev,
struct usb_device_driver *udrv);
extern void usb_forced_unbind_intf(struct usb_interface *intf); extern void usb_forced_unbind_intf(struct usb_interface *intf);
extern void usb_unbind_and_rebind_marked_interfaces(struct usb_device *udev); extern void usb_unbind_and_rebind_marked_interfaces(struct usb_device *udev);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment