diff mbox series

[4/5] USB: Select better matching USB drivers when available

Message ID 20191009134342.6476-5-hadess@hadess.net (mailing list archive)
State Superseded
Headers show
Series Add Apple MFi fastcharge USB device driver | expand

Commit Message

Bastien Nocera Oct. 9, 2019, 1:43 p.m. UTC
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>
---
 drivers/usb/core/driver.c  | 15 +++++++++++++--
 drivers/usb/core/generic.c | 29 +++++++++++++++++++++++++++++
 include/linux/usb.h        |  2 ++
 3 files changed, 44 insertions(+), 2 deletions(-)

Comments

Alan Stern Oct. 9, 2019, 2:43 p.m. UTC | #1
On Wed, 9 Oct 2019, Bastien Nocera wrote:

> 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>
> ---
>  drivers/usb/core/driver.c  | 15 +++++++++++++--
>  drivers/usb/core/generic.c | 29 +++++++++++++++++++++++++++++
>  include/linux/usb.h        |  2 ++
>  3 files changed, 44 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
> index 50f92da8afcf..27ce63ed902d 100644
> --- a/drivers/usb/core/driver.c
> +++ b/drivers/usb/core/driver.c
> @@ -819,13 +819,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;

What happens if the subclass driver's probe routine returns an error?  
Don't you still want the device to be bound to the generic driver?

Alan Stern
Bastien Nocera Oct. 9, 2019, 3:35 p.m. UTC | #2
On Wed, 2019-10-09 at 10:43 -0400, Alan Stern wrote:
> On Wed, 9 Oct 2019, Bastien Nocera wrote:
> 
> > 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>
> > ---
> >  drivers/usb/core/driver.c  | 15 +++++++++++++--
> >  drivers/usb/core/generic.c | 29 +++++++++++++++++++++++++++++
> >  include/linux/usb.h        |  2 ++
> >  3 files changed, 44 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
> > index 50f92da8afcf..27ce63ed902d 100644
> > --- a/drivers/usb/core/driver.c
> > +++ b/drivers/usb/core/driver.c
> > @@ -819,13 +819,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;
> 
> What happens if the subclass driver's probe routine returns an
> error?  
> Don't you still want the device to be bound to the generic driver?

I don't know whether that's what you'd want to do. But if we did,
that'd only be for devices which have "generic_init" set.

We'd need to remember the result of the ->probe() call at the end of
usb_probe_device() (as modified in patch 2), and only call the generic
driver (not the specific device driver)'s functions in later usage.

Is that what you would expect?
Alan Stern Oct. 9, 2019, 5:28 p.m. UTC | #3
On Wed, 9 Oct 2019, Bastien Nocera wrote:

> On Wed, 2019-10-09 at 10:43 -0400, Alan Stern wrote:
> > On Wed, 9 Oct 2019, Bastien Nocera wrote:
> > 
> > > 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>
> > > ---
> > >  drivers/usb/core/driver.c  | 15 +++++++++++++--
> > >  drivers/usb/core/generic.c | 29 +++++++++++++++++++++++++++++
> > >  include/linux/usb.h        |  2 ++
> > >  3 files changed, 44 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
> > > index 50f92da8afcf..27ce63ed902d 100644
> > > --- a/drivers/usb/core/driver.c
> > > +++ b/drivers/usb/core/driver.c
> > > @@ -819,13 +819,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;
> > 
> > What happens if the subclass driver's probe routine returns an
> > error?  
> > Don't you still want the device to be bound to the generic driver?
> 
> I don't know whether that's what you'd want to do. But if we did,
> that'd only be for devices which have "generic_init" set.
> 
> We'd need to remember the result of the ->probe() call at the end of
> usb_probe_device() (as modified in patch 2), and only call the generic
> driver (not the specific device driver)'s functions in later usage.
> 
> Is that what you would expect?

No, that's not quite it.

Here's what should happen when the subclass driver is being probed:
First, call the generic_probe routine, and return immediately if that
fails.  Then call the subclass driver's probe routine.  If that gets an
error, fail the probe call but tell the device core that the device is
now bound to the generic driver, not to the subclass driver.

Alan Stern
Bastien Nocera Oct. 9, 2019, 6:24 p.m. UTC | #4
On Wed, 2019-10-09 at 13:28 -0400, Alan Stern wrote:
<snip>
> No, that's not quite it.
> 
> Here's what should happen when the subclass driver is being probed:
> First, call the generic_probe routine, and return immediately if that
> fails.  Then call the subclass driver's probe routine.  If that gets
> an
> error, fail the probe call but tell the device core that the device
> is
> now bound to the generic driver, not to the subclass driver.

So, something like that, on top of the existing patches? (I'm not sure
whether device_driver_attach is the correct call to use here).

-       if (udriver->probe)
-               return udriver->probe(udev);
-       return 0;
+       if (!udriver->probe)
+               return 0;
+       error = udriver->probe(udev);
+       if (error == -ENODEV &&
+           udrv != &usb_generic_driver)
+               return device_driver_attach(usb_generic_driver.drvwrap.driver, dev);
+       return error;

Anything else in this patch series? I was concerned about the naming
for "generic_init" in patch 2 ("subclass").

If there's nothing, I'll test and respin the patchset with the above
changes tomorrow.

Cheers
Alan Stern Oct. 9, 2019, 6:45 p.m. UTC | #5
On Wed, 9 Oct 2019, Bastien Nocera wrote:

> On Wed, 2019-10-09 at 13:28 -0400, Alan Stern wrote:
> <snip>
> > No, that's not quite it.
> > 
> > Here's what should happen when the subclass driver is being probed:
> > First, call the generic_probe routine, and return immediately if that
> > fails.  Then call the subclass driver's probe routine.  If that gets
> > an
> > error, fail the probe call but tell the device core that the device
> > is
> > now bound to the generic driver, not to the subclass driver.
> 
> So, something like that, on top of the existing patches? (I'm not sure
> whether device_driver_attach is the correct call to use here).
> 
> -       if (udriver->probe)
> -               return udriver->probe(udev);
> -       return 0;
> +       if (!udriver->probe)
> +               return 0;

This test is unnecessary; all drivers must have a probe routine.  
Otherwise how would they know when they get bound to a device?

> +       error = udriver->probe(udev);
> +       if (error == -ENODEV &&
> +           udrv != &usb_generic_driver)

No need to test for usb_generic_driver; its probe routine always 
returns 0.  But if you want to include the test anyway, at least don't 
split the line -- it will all fit in under 80 columns.

> +               return device_driver_attach(usb_generic_driver.drvwrap.driver, dev);
> +       return error;

I think that's right.  A little testing wouldn't hurt.

> Anything else in this patch series? I was concerned about the naming
> for "generic_init" in patch 2 ("subclass").

Yes; see the suggestions in

	https://marc.info/?l=linux-usb&m=157063168632242&w=2

Also (I didn't notice this earlier), in patch 1/5 it's not necessary to 
EXPORT the usb_generic_* routines.  They don't get used in the subclass 
driver, only in usbcore.

> If there's nothing, I'll test and respin the patchset with the above
> changes tomorrow.

Okay, good.

Alan Stern
Bastien Nocera Oct. 10, 2019, 8:26 a.m. UTC | #6
On Wed, 2019-10-09 at 14:45 -0400, Alan Stern wrote:
> 
On Wed, 9 Oct 2019, Bastien Nocera wrote:
> 
> <snip>
> > +               return
> device_driver_attach(usb_generic_driver.drvwrap.driver, dev);
> > +       return error;
> 
> I think that's right.  A little testing wouldn't hurt.

device_driver_attach() isn't available to this part of the code.

I think the only way to do things here might be to set status bit for
the usb_device and launch device_reprobe(). The second time around, we
wouldn't match or probe the specific driver.
Alan Stern Oct. 10, 2019, 2:19 p.m. UTC | #7
On Thu, 10 Oct 2019, Bastien Nocera wrote:

> On Wed, 2019-10-09 at 14:45 -0400, Alan Stern wrote:
> > 
> On Wed, 9 Oct 2019, Bastien Nocera wrote:
> > 
> > <snip>
> > > +               return
> > device_driver_attach(usb_generic_driver.drvwrap.driver, dev);
> > > +       return error;
> > 
> > I think that's right.  A little testing wouldn't hurt.
> 
> device_driver_attach() isn't available to this part of the code.
> 
> I think the only way to do things here might be to set status bit for
> the usb_device and launch device_reprobe(). The second time around, we
> wouldn't match or probe the specific driver.

That would mean probing generic_driver twice, right?  You probably
should call its disconnect routine in between.

That sounds pretty awkward, but if there's no other way then go ahead 
and do it.

Ala Stern
diff mbox series

Patch

diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index 50f92da8afcf..27ce63ed902d 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -819,13 +819,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;
diff --git a/drivers/usb/core/generic.c b/drivers/usb/core/generic.c
index 7454c74d43ee..89f9c026a4d1 100644
--- a/drivers/usb/core/generic.c
+++ b/drivers/usb/core/generic.c
@@ -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;
@@ -289,6 +317,7 @@  EXPORT_SYMBOL_GPL(usb_generic_driver_resume);
 
 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
diff --git a/include/linux/usb.h b/include/linux/usb.h
index 66bd4344e298..df5604f41118 100644
--- a/include/linux/usb.h
+++ b/include/linux/usb.h
@@ -1236,6 +1236,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);
 
@@ -1243,6 +1244,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_init:1;
 };