Skip to contentSkip to conversation

USB: Fix device driver race

PATCH Superseded Bastien Nocera 4 participants 17 messages Jul 22, 2020 – Jul 23, 2020 USB

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 USB: Fix device driver race

When a new device with a specialised device driver is plugged in, the new driver will be modprobe()'d but the driver core will attach the "generic" driver to the device.

After that, nothing will trigger a reprobe when the modprobe()'d device driver has finished initialising, as the device has the "generic" driver attached to it.

Trigger a reprobe ourselves when new specialised drivers get registered.

---
 drivers/usb/core/driver.c | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)
drivers/usb/core/driver.c +29 -2
index f81606c6a35b..a6187dd2186c 100644
@@ -905,6 +905,30 @@ static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
 	return 0;
 }
 
+static int __usb_bus_reprobe_drivers(struct device *dev, void *data)
+{
+	struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
+	struct usb_device *udev = to_usb_device(dev);
+
+	if (udriver == &usb_generic_driver &&
+	    !udev->use_generic_driver)
+		return device_reprobe(dev);
+
+	return 0;
+}
+
+static int __usb_device_driver_added(struct device_driver *drv, void *data)
+{
+	struct usb_device_driver *udrv = to_usb_device_driver(drv);
+
+	if (udrv->match) {
+		bus_for_each_dev(&usb_bus_type, NULL, udrv,
+				 __usb_bus_reprobe_drivers);
+	}
+
+	return 0;
+}
+
 /**
  * usb_register_device_driver - register a USB device (not interface) driver
  * @new_udriver: USB operations for the device driver
@@ -934,13 +958,16 @@ int usb_register_device_driver(struct usb_device_driver *new_udriver,
 
 	retval = driver_register(&new_udriver->drvwrap.driver);
 
-	if (!retval)
+	if (!retval) {
+		bus_for_each_drv(&usb_bus_type, NULL, NULL,
+				 __usb_device_driver_added);
 		pr_info("%s: registered new device driver %s\n",
 			usbcore_name, new_udriver->name);
-	else
+	} else {
 		printk(KERN_ERR "%s: error %d registering device "
 			"	driver %s\n",
 			usbcore_name, retval, new_udriver->name);
+	}
 
 	return retval;
 }
Signature · 1 line
2.26.2
#1 lore raw

PATCH 2/2 USB: Fix source path in header

---
 drivers/usb/core/driver.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/core/driver.c +1 -1
index a6187dd2186c..bd346e683af3 100644
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * drivers/usb/driver.c - most of the driver model stuff for usb
+ * drivers/usb/core/driver.c - most of the driver model stuff for usb
  *
  * (C) Copyright 2005 Greg Kroah-Hartman <[email protected]>
  *
Signature · 1 line
2.26.2
#2 lore raw

Re: [PATCH 2/2] USB: Fix source path in header

Quoted 1 line · Bastien Nocera

I can't take patches without any changelog text at all, sorry.

drivers/usb/core/driver.c line 3

Quoted 13 lines
---
 drivers/usb/core/driver.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/core/driver.c +1 -1
index a6187dd2186c..bd346e683af3 100644
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * drivers/usb/driver.c - most of the driver model stuff for usb
+ * drivers/usb/core/driver.c - most of the driver model stuff for usb

Just drop the whole file name, it's not needed at all.

thanks,

greg k-h

#3 lore raw

Re: [PATCH 2/2] USB: Fix source path in header

Quoted 4 lines · Greg Kroah-Hartman
Quoted 1 line · Bastien Nocera

I can't take patches without any changelog text at all, sorry.

The subject line isn't clear enough?

Quoted 4 lines
Quoted 4 lines
---
 drivers/usb/core/driver.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines
Quoted 10 lines
drivers/usb/core/driver.c +1 -1
index a6187dd2186c..bd346e683af3 100644
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * drivers/usb/driver.c - most of the driver model stuff for usb
+ * drivers/usb/core/driver.c - most of the driver model stuff for

usb

Just drop the whole file name, it's not needed at all.

And the description after or just the filename?

#4 lore raw

Re: [PATCH 2/2] USB: Fix source path in header

Quoted 7 lines · Bastien Nocera
Quoted 4 lines · Greg Kroah-Hartman
Quoted 1 line · Bastien Nocera

I can't take patches without any changelog text at all, sorry.

The subject line isn't clear enough?

Maybe, maybe not, doesn't mean you shouldn't also write some text about why you did what you did.

Quoted 17 lines
Quoted 4 lines
Quoted 4 lines
---
 drivers/usb/core/driver.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 12 lines
Quoted 10 lines
drivers/usb/core/driver.c +1 -1
index a6187dd2186c..bd346e683af3 100644
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * drivers/usb/driver.c - most of the driver model stuff for usb
+ * drivers/usb/core/driver.c - most of the driver model stuff for

usb

Just drop the whole file name, it's not needed at all.

Quoted 1 line

And the description after or just the filename?

The description is fine to keep.

thanks,

greg k-h

#5 lore raw

Re: [PATCH 2/2] USB: Fix source path in header

Quoted 33 lines · Greg Kroah-Hartman
Quoted 7 lines · Bastien Nocera
Quoted 4 lines · Greg Kroah-Hartman
Quoted 1 line · Bastien Nocera

I can't take patches without any changelog text at all, sorry.

The subject line isn't clear enough?

Maybe, maybe not, doesn't mean you shouldn't also write some text about
why you did what you did.

Quoted 20 lines
Quoted 19 lines
Quoted 13 lines
---
 drivers/usb/core/driver.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/core/driver.c +0 -1
index a6187dd2186c..bd346e683af3 100644
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * drivers/usb/driver.c - most of the driver model stuff for
Quoted 4 lines

usb
+ * drivers/usb/core/driver.c - most of the driver model stuff for
usb

Just drop the whole file name, it's not needed at all.

Quoted 3 lines
Quoted 1 line

And the description after or just the filename?

The description is fine to keep.

Looks like there's a lot more cleaning up that would need to be done in the tree, so feel free to drop this patch.

Cheers

#6 lore raw
In reply to Bastien Nocera
Quoted 4 lines · Bastien Nocera

When a new device with a specialised device driver is plugged in, the new driver will be modprobe()'d but the driver core will attach the "generic" driver to the device.

Quoted 8 lines

After that, nothing will trigger a reprobe when the modprobe()'d device driver has finished initialising, as the device has the "generic" driver attached to it.

Trigger a reprobe ourselves when new specialised drivers get registered.

---

What commit id does this fix? And if it's in an older kernel, should it be backported to the stable trees?

thanks,

greg k-h

#7 lore raw
Quoted 14 lines · Greg Kroah-Hartman
Quoted 13 lines · Bastien Nocera

When a new device with a specialised device driver is plugged in, the
new driver will be modprobe()'d but the driver core will attach the "generic" driver to the device.

After that, nothing will trigger a reprobe when the modprobe()'d device
driver has finished initialising, as the device has the "generic" driver attached to it.

Trigger a reprobe ourselves when new specialised drivers get registered.

Quoted 6 lines
Quoted 2 lines

---

What commit id does this fix? And if it's in an older kernel, should it
be backported to the stable trees?

Fixes 88b7381a939de0fa1f1b1629c56b03dca7077309

AFAIK, the apple-mfi-fastcharge driver wasn't backported to stable trees, and it's the only driver that's impacted by this bug, so there shouldn't be any need to backport it there.

Did you want me to respin it with this info?

Cheers

#8 lore raw
In reply to Bastien Nocera
Quoted 32 lines · Bastien Nocera

When a new device with a specialised device driver is plugged in, the new driver will be modprobe()'d but the driver core will attach the "generic" driver to the device.

After that, nothing will trigger a reprobe when the modprobe()'d device driver has finished initialising, as the device has the "generic" driver attached to it.

Trigger a reprobe ourselves when new specialised drivers get registered.

---
 drivers/usb/core/driver.c | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)
drivers/usb/core/driver.c +9 -0
index f81606c6a35b..a6187dd2186c 100644
@@ -905,6 +905,30 @@ static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
 	return 0;
 }
 
+static int __usb_bus_reprobe_drivers(struct device *dev, void *data)
+{
+	struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
+	struct usb_device *udev = to_usb_device(dev);
+
+	if (udriver == &usb_generic_driver &&
+	    !udev->use_generic_driver)
+		return device_reprobe(dev);
+

drivers/usb/core/driver.c static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) · line 926

Quoted 10 lines
drivers/usb/core/driver.c +10 -0
+	return 0;
+}
+
+static int __usb_device_driver_added(struct device_driver *drv, void *data)
+{
+	struct usb_device_driver *udrv = to_usb_device_driver(drv);
+
+	if (udrv->match) {
+		bus_for_each_dev(&usb_bus_type, NULL, udrv,
+				 __usb_bus_reprobe_drivers);

What does udrv get used for here?

Quoted 9 lines
drivers/usb/core/driver.c +5 -0
+	}
+
+	return 0;
+}
+
 /**
  * usb_register_device_driver - register a USB device (not interface) driver
  * @new_udriver: USB operations for the device driver

drivers/usb/core/driver.c int usb_register_device_driver(struct usb_device_driver *new_udriver, · line 963

Quoted 7 lines
drivers/usb/core/driver.c +3 -1
@@ -934,13 +958,16 @@ int usb_register_device_driver(struct usb_device_driver *new_udriver,
 
 	retval = driver_register(&new_udriver->drvwrap.driver);
 
-	if (!retval)
+	if (!retval) {
+		bus_for_each_drv(&usb_bus_type, NULL, NULL,
+				 __usb_device_driver_added);

This looks funny. You're calling both bus_for_each_drv() and bus_for_each_dev(). Can't you skip this iterator and just call bus_for_each_dev() directly?

Alan Stern

#9 lore raw
Quoted 67 lines · Alan Stern
Quoted 43 lines · Bastien Nocera

When a new device with a specialised device driver is plugged in, the
new driver will be modprobe()'d but the driver core will attach the "generic" driver to the device.

After that, nothing will trigger a reprobe when the modprobe()'d device
driver has finished initialising, as the device has the "generic" driver attached to it.

Trigger a reprobe ourselves when new specialised drivers get registered.

---
 drivers/usb/core/driver.c | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)
drivers/usb/core/driver.c +13 -0
index f81606c6a35b..a6187dd2186c 100644
@@ -905,6 +905,30 @@ static int usb_uevent(struct device *dev,
 struct kobj_uevent_env *env)
 	return 0;
 }
 
+static int __usb_bus_reprobe_drivers(struct device *dev, void
 *data)
+{
+	struct usb_device_driver *udriver = to_usb_device_driver(dev-
 >driver);
+	struct usb_device *udev = to_usb_device(dev);
+
+	if (udriver == &usb_generic_driver &&
+	    !udev->use_generic_driver)
+		return device_reprobe(dev);
+
+	return 0;
+}
+
+static int __usb_device_driver_added(struct device_driver *drv,

void *data)

drivers/usb/core/driver.c static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) · line 926

Quoted 6 lines
drivers/usb/core/driver.c +6 -0
+{
+	struct usb_device_driver *udrv = to_usb_device_driver(drv);
+
+	if (udrv->match) {
+		bus_for_each_dev(&usb_bus_type, NULL, udrv,
+				 __usb_bus_reprobe_drivers);

What does udrv get used for here?

Quoted 14 lines
drivers/usb/core/driver.c +5 -0
+	}
+
+	return 0;
+}
+
 /**
  * usb_register_device_driver - register a USB device (not

interface) driver
* @new_udriver: USB operations for the device driver

drivers/usb/core/driver.c +0 -0
@@ -934,13 +958,16 @@ int usb_register_device_driver(struct
 usb_device_driver *new_udriver,
 
 	retval = driver_register(&new_udriver->drvwrap.driver);
 
Quoted 8 lines

drivers/usb/core/driver.c int usb_register_device_driver(struct usb_device_driver *new_udriver, · line 963

Quoted 4 lines
drivers/usb/core/driver.c +3 -1
-	if (!retval)
+	if (!retval) {
+		bus_for_each_drv(&usb_bus_type, NULL, NULL,
+				 __usb_device_driver_added);

This looks funny. You're calling both bus_for_each_drv() and bus_for_each_dev(). Can't you skip this iterator and just call bus_for_each_dev() directly?

You're right, looks like this could be simplified somewhat. I'm building and testing a smaller patch.

Thanks

#10 lore raw
Quoted 77 lines · Bastien Nocera
Quoted 67 lines · Alan Stern
Quoted 43 lines · Bastien Nocera

When a new device with a specialised device driver is plugged in, the
new driver will be modprobe()'d but the driver core will attach the "generic" driver to the device.

After that, nothing will trigger a reprobe when the modprobe()'d device
driver has finished initialising, as the device has the "generic" driver attached to it.

Trigger a reprobe ourselves when new specialised drivers get registered.

---
 drivers/usb/core/driver.c | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)
drivers/usb/core/driver.c +13 -0
index f81606c6a35b..a6187dd2186c 100644
@@ -905,6 +905,30 @@ static int usb_uevent(struct device *dev,
 struct kobj_uevent_env *env)
 	return 0;
 }
 
+static int __usb_bus_reprobe_drivers(struct device *dev, void
 *data)
+{
+	struct usb_device_driver *udriver = to_usb_device_driver(dev-
 >driver);
+	struct usb_device *udev = to_usb_device(dev);
+
+	if (udriver == &usb_generic_driver &&
+	    !udev->use_generic_driver)
+		return device_reprobe(dev);
+
+	return 0;
+}
+
+static int __usb_device_driver_added(struct device_driver *drv,

void *data)

drivers/usb/core/driver.c static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) · line 926

Quoted 6 lines
drivers/usb/core/driver.c +6 -0
+{
+	struct usb_device_driver *udrv = to_usb_device_driver(drv);
+
+	if (udrv->match) {
+		bus_for_each_dev(&usb_bus_type, NULL, udrv,
+				 __usb_bus_reprobe_drivers);

What does udrv get used for here?

Quoted 14 lines
drivers/usb/core/driver.c +5 -0
+	}
+
+	return 0;
+}
+
 /**
  * usb_register_device_driver - register a USB device (not

interface) driver
* @new_udriver: USB operations for the device driver

drivers/usb/core/driver.c +0 -0
@@ -934,13 +958,16 @@ int usb_register_device_driver(struct
 usb_device_driver *new_udriver,
 
 	retval = driver_register(&new_udriver->drvwrap.driver);
 
Quoted 8 lines

drivers/usb/core/driver.c int usb_register_device_driver(struct usb_device_driver *new_udriver, · line 963

Quoted 4 lines
drivers/usb/core/driver.c +3 -1
-	if (!retval)
+	if (!retval) {
+		bus_for_each_drv(&usb_bus_type, NULL, NULL,
+				 __usb_device_driver_added);

This looks funny. You're calling both bus_for_each_drv() and bus_for_each_dev(). Can't you skip this iterator and just call bus_for_each_dev() directly?

Quoted 3 lines

You're right, looks like this could be simplified somewhat. I'm building and testing a smaller patch.

What do you mean "reprobe" for your device? Do you mean the mfi_fc_probe is not called? If it is, Would you please check why usb_device_match at drivers/usb/core/driver.c does not return true for your device?

Signature · 2 lines
Thanks,
Peter Chen
#11 lore raw
Quoted 86 lines · Peter Chen
Quoted 85 lines · Bastien Nocera
Quoted 83 lines · Alan Stern
Quoted 53 lines · Bastien Nocera

When a new device with a specialised device driver is plugged in,
the
new driver will be modprobe()'d but the driver core will attach the
"generic" driver to the device.

After that, nothing will trigger a reprobe when the modprobe()'d
device
driver has finished initialising, as the device has the "generic"
driver attached to it.

Trigger a reprobe ourselves when new specialised drivers get registered.

---
 drivers/usb/core/driver.c | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)
drivers/usb/core/driver.c +3 -0
index f81606c6a35b..a6187dd2186c 100644
@@ -905,6 +905,30 @@ static int usb_uevent(struct device *dev,
 struct kobj_uevent_env *env)
 	return 0;
 }
 
+static int __usb_bus_reprobe_drivers(struct device *dev, void
 *data)
+{
+	struct usb_device_driver *udriver =

to_usb_device_driver(dev-

Quoted 1 line

driver);

drivers/usb/core/driver.c +10 -0
+	struct usb_device *udev = to_usb_device(dev);
+
+	if (udriver == &usb_generic_driver &&
+	    !udev->use_generic_driver)
+		return device_reprobe(dev);
+
+	return 0;
+}
+
+static int __usb_device_driver_added(struct device_driver

*drv,
void *data)

drivers/usb/core/driver.c +2 -0
+{
+	struct usb_device_driver *udrv =

to_usb_device_driver(drv);

drivers/usb/core/driver.c static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) · line 926

Quoted 4 lines
drivers/usb/core/driver.c +4 -0
+
+	if (udrv->match) {
+		bus_for_each_dev(&usb_bus_type, NULL, udrv,
+				 __usb_bus_reprobe_drivers);

What does udrv get used for here?

Quoted 10 lines
drivers/usb/core/driver.c +5 -0
+	}
+
+	return 0;
+}
+
 /**
  * usb_register_device_driver - register a USB device (not

interface) driver
* @new_udriver: USB operations for the device driver

drivers/usb/core/driver.c int usb_register_device_driver(struct · line 964

Quoted 8 lines
drivers/usb/core/driver.c +3 -1
@@ -934,13 +958,16 @@ int usb_register_device_driver(struct
 usb_device_driver *new_udriver,
 
 	retval = driver_register(&new_udriver->drvwrap.driver);
 
-	if (!retval)
+	if (!retval) {
+		bus_for_each_drv(&usb_bus_type, NULL, NULL,
+				 __usb_device_driver_added);

This looks funny. You're calling both bus_for_each_drv() and bus_for_each_dev(). Can't you skip this iterator and just call bus_for_each_dev() directly?

Quoted 8 lines
Quoted 3 lines

You're right, looks like this could be simplified somewhat. I'm building and testing a smaller patch.

What do you mean "reprobe" for your device? Do you mean the mfi_fc_probe
is not called? If it is, Would you please check why usb_device_match at drivers/usb/core/driver.c does not return true for your device?

mfi_fc_probe() isn't called because the device already has the generic driver attached by the time the apple-mfi-fastcharge driver is loaded.

#12 lore raw
Quoted 79 lines
Quoted 79 lines
Quoted 78 lines
Quoted 66 lines
Quoted 45 lines

After that, nothing will trigger a reprobe when the modprobe()'d device driver has finished initialising, as the device has the "generic"
driver attached to it.

Trigger a reprobe ourselves when new specialised drivers get registered.

---
 drivers/usb/core/driver.c | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/drivers/usb/core/driver.c index f81606c6a35b..a6187dd2186c 100644

drivers/usb/core/driver.c +3 -0
@@ -905,6 +905,30 @@ static int usb_uevent(struct device *dev,
 struct kobj_uevent_env *env)
 	return 0;
 }
 
+static int __usb_bus_reprobe_drivers(struct device *dev, void
 *data)
+{
+	struct usb_device_driver *udriver =

to_usb_device_driver(dev-

Quoted 1 line

driver);

drivers/usb/core/driver.c +10 -0
+	struct usb_device *udev = to_usb_device(dev);
+
+	if (udriver == &usb_generic_driver &&
+	    !udev->use_generic_driver)
+		return device_reprobe(dev);
+
+	return 0;
+}
+
+static int __usb_device_driver_added(struct device_driver

*drv,
void *data)

drivers/usb/core/driver.c +2 -0
+{
+	struct usb_device_driver *udrv =

to_usb_device_driver(drv);

drivers/usb/core/driver.c static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) · line 926

Quoted 4 lines
drivers/usb/core/driver.c +4 -0
+
+	if (udrv->match) {
+		bus_for_each_dev(&usb_bus_type, NULL, udrv,
+				 __usb_bus_reprobe_drivers);

What does udrv get used for here?

Quoted 14 lines
drivers/usb/core/driver.c +5 -0
+	}
+
+	return 0;
+}
+
 /**
  * usb_register_device_driver - register a USB device (not

interface) driver
* @new_udriver: USB operations for the device driver @@
-934,13 +958,16 @@ int usb_register_device_driver(struct usb_device_driver *new_udriver,

retval = driver_register(&new_udriver->drvwrap.driver);

Quoted 8 lines

drivers/usb/core/driver.c int usb_register_device_driver(struct usb_device_driver *new_udriver, · line 963

Quoted 4 lines
drivers/usb/core/driver.c +3 -1
-	if (!retval)
+	if (!retval) {
+		bus_for_each_drv(&usb_bus_type, NULL, NULL,
+				 __usb_device_driver_added);

This looks funny. You're calling both bus_for_each_drv() and bus_for_each_dev(). Can't you skip this iterator and just call bus_for_each_dev() directly?

You're right, looks like this could be simplified somewhat. I'm building and testing a smaller patch.

Quoted 7 lines
Quoted 4 lines

What do you mean "reprobe" for your device? Do you mean the mfi_fc_probe is not called? If it is, Would you please check why usb_device_match at drivers/usb/core/driver.c does not return true for your device?

mfi_fc_probe() isn't called because the device already has the generic driver attached by the time the apple-mfi-fastcharge driver is loaded.

Would you please explain more, why only this driver has this issue? Seem you create a struct usb_device_driver, but not struct usb_driver, few drivers do like that. You may see drivers/usb/misc/ehset.c and drivers/usb/misc/appledisplay.c as an example.

Peter

#13 lore raw
Quoted 84 lines · Peter Chen
Quoted 84 lines
Quoted 83 lines
Quoted 75 lines
Quoted 66 lines
Quoted 45 lines

After that, nothing will trigger a reprobe when the modprobe()'d device driver has finished initialising, as the device has the "generic"
driver attached to it.

Trigger a reprobe ourselves when new specialised drivers get registered.

---
 drivers/usb/core/driver.c | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/drivers/usb/core/driver.c index f81606c6a35b..a6187dd2186c 100644

drivers/usb/core/driver.c +3 -0
@@ -905,6 +905,30 @@ static int usb_uevent(struct device *dev,
 struct kobj_uevent_env *env)
 	return 0;
 }
 
+static int __usb_bus_reprobe_drivers(struct device *dev, void
 *data)
+{
+	struct usb_device_driver *udriver =

to_usb_device_driver(dev-

Quoted 1 line

driver);

drivers/usb/core/driver.c +10 -0
+	struct usb_device *udev = to_usb_device(dev);
+
+	if (udriver == &usb_generic_driver &&
+	    !udev->use_generic_driver)
+		return device_reprobe(dev);
+
+	return 0;
+}
+
+static int __usb_device_driver_added(struct device_driver

*drv,
void *data)

drivers/usb/core/driver.c +2 -0
+{
+	struct usb_device_driver *udrv =

to_usb_device_driver(drv);

drivers/usb/core/driver.c static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) · line 926

Quoted 4 lines
drivers/usb/core/driver.c +4 -0
+
+	if (udrv->match) {
+		bus_for_each_dev(&usb_bus_type, NULL, udrv,
+				 __usb_bus_reprobe_drivers);

What does udrv get used for here?

Quoted 14 lines
drivers/usb/core/driver.c +5 -0
+	}
+
+	return 0;
+}
+
 /**
  * usb_register_device_driver - register a USB device (not

interface) driver
* @new_udriver: USB operations for the device driver @@
-934,13 +958,16 @@ int usb_register_device_driver(struct usb_device_driver *new_udriver,

retval = driver_register(&new_udriver->drvwrap.driver);

Quoted 8 lines

drivers/usb/core/driver.c int usb_register_device_driver(struct usb_device_driver *new_udriver, · line 963

Quoted 4 lines
drivers/usb/core/driver.c +3 -1
-	if (!retval)
+	if (!retval) {
+		bus_for_each_drv(&usb_bus_type, NULL, NULL,
+				 __usb_device_driver_added);

This looks funny. You're calling both bus_for_each_drv() and bus_for_each_dev(). Can't you skip this iterator and just call bus_for_each_dev() directly?

Quoted 3 lines

You're right, looks like this could be simplified somewhat. I'm building and testing a smaller patch.

What do you mean "reprobe" for your device? Do you mean the mfi_fc_probe is not called? If it is, Would you please check why usb_device_match at drivers/usb/core/driver.c does not return true for your device?

Quoted 7 lines
Quoted 2 lines

mfi_fc_probe() isn't called because the device already has the generic driver attached by the time the apple-mfi-fastcharge driver is loaded.

Would you please explain more, why only this driver has this issue? Seem you create a struct usb_device_driver, but not struct usb_driver, few drivers do like that. You may see drivers/usb/misc/ehset.c and drivers/usb/misc/appledisplay.c as an example.

This is a different "type" of driver.

#14 lore raw
In reply to Peter Chen
Quoted 97 lines · Peter Chen
Quoted 93 lines
Quoted 87 lines
Quoted 83 lines
Quoted 73 lines
Quoted 52 lines

After that, nothing will trigger a reprobe when the modprobe()'d
device driver has finished initialising, as the device has the
"generic"
driver attached to it.

Trigger a reprobe ourselves when new specialised drivers get
registered.

---
 drivers/usb/core/driver.c | 31
+++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/drivers/usb/core/driver.c index
f81606c6a35b..a6187dd2186c
100644

drivers/usb/core/driver.c +1 -0
@@ -905,6 +905,30 @@ static int usb_uevent(struct device
 *dev,
 struct kobj_uevent_env *env)
 	return 0;
 }
 
+static int __usb_bus_reprobe_drivers(struct device *dev,

void
*data)

drivers/usb/core/driver.c +2 -0
+{
+	struct usb_device_driver *udriver =

to_usb_device_driver(dev-

Quoted 1 line

driver);

drivers/usb/core/driver.c +10 -0
+	struct usb_device *udev = to_usb_device(dev);
+
+	if (udriver == &usb_generic_driver &&
+	    !udev->use_generic_driver)
+		return device_reprobe(dev);
+
+	return 0;
+}
+
+static int __usb_device_driver_added(struct device_driver

*drv,
void *data)

drivers/usb/core/driver.c +2 -0
+{
+	struct usb_device_driver *udrv =

to_usb_device_driver(drv);

drivers/usb/core/driver.c static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) · line 926

Quoted 4 lines
drivers/usb/core/driver.c +4 -0
+
+	if (udrv->match) {
+		bus_for_each_dev(&usb_bus_type, NULL, udrv,
+				 __usb_bus_reprobe_drivers);

What does udrv get used for here?

Quoted 14 lines
drivers/usb/core/driver.c +5 -0
+	}
+
+	return 0;
+}
+
 /**
  * usb_register_device_driver - register a USB device (not

interface) driver
* @new_udriver: USB operations for the device driver @@
-934,13 +958,16 @@ int usb_register_device_driver(struct usb_device_driver *new_udriver,

retval = driver_register(&new_udriver->drvwrap.driver);

Quoted 9 lines

drivers/usb/core/driver.c int usb_register_device_driver(struct usb_device_driver *new_udriver, · line 963

Quoted 4 lines
drivers/usb/core/driver.c +3 -1
-	if (!retval)
+	if (!retval) {
+		bus_for_each_drv(&usb_bus_type, NULL, NULL,
+				 __usb_device_driver_added);

This looks funny. You're calling both bus_for_each_drv() and bus_for_each_dev(). Can't you skip this iterator and just call
bus_for_each_dev() directly?

Quoted 3 lines

You're right, looks like this could be simplified somewhat. I'm building and testing a smaller patch.

Quoted 5 lines

What do you mean "reprobe" for your device? Do you mean the mfi_fc_probe is not called? If it is, Would you please check why usb_device_match at drivers/usb/core/driver.c does not return true for
your device?

Quoted 3 lines

mfi_fc_probe() isn't called because the device already has the generic driver
attached by the time the apple-mfi-fastcharge driver is loaded.

Quoted 7 lines

Would you please explain more, why only this driver has this issue? Seem you
create a struct usb_device_driver, but not struct usb_driver, few drivers do like
that. You may see drivers/usb/misc/ehset.c and drivers/usb/misc/appledisplay.c
as an example.

It's a _device_ driver, not an interface driver. It's the only driver that has the problem because it's the only non-generic device driver :)

#15 lore raw
Quoted 106 lines · Bastien Nocera
Quoted 97 lines · Peter Chen
Quoted 93 lines
Quoted 87 lines
Quoted 83 lines
Quoted 73 lines
Quoted 52 lines

After that, nothing will trigger a reprobe when the modprobe()'d
device driver has finished initialising, as the device has the
"generic"
driver attached to it.

Trigger a reprobe ourselves when new specialised drivers get
registered.

---
 drivers/usb/core/driver.c | 31
+++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/drivers/usb/core/driver.c index
f81606c6a35b..a6187dd2186c
100644

drivers/usb/core/driver.c +1 -0
@@ -905,6 +905,30 @@ static int usb_uevent(struct device
 *dev,
 struct kobj_uevent_env *env)
 	return 0;
 }
 
+static int __usb_bus_reprobe_drivers(struct device *dev,

void
*data)

drivers/usb/core/driver.c +2 -0
+{
+	struct usb_device_driver *udriver =

to_usb_device_driver(dev-

Quoted 1 line

driver);

drivers/usb/core/driver.c +10 -0
+	struct usb_device *udev = to_usb_device(dev);
+
+	if (udriver == &usb_generic_driver &&
+	    !udev->use_generic_driver)
+		return device_reprobe(dev);
+
+	return 0;
+}
+
+static int __usb_device_driver_added(struct device_driver

*drv,
void *data)

drivers/usb/core/driver.c +2 -0
+{
+	struct usb_device_driver *udrv =

to_usb_device_driver(drv);

drivers/usb/core/driver.c

Quoted 4 lines
drivers/usb/core/driver.c +4 -0
+
+	if (udrv->match) {
+		bus_for_each_dev(&usb_bus_type, NULL, udrv,
+				 __usb_bus_reprobe_drivers);

What does udrv get used for here?

Quoted 14 lines
drivers/usb/core/driver.c +5 -0
+	}
+
+	return 0;
+}
+
 /**
  * usb_register_device_driver - register a USB device (not

interface) driver
* @new_udriver: USB operations for the device driver @@
-934,13 +958,16 @@ int usb_register_device_driver(struct usb_device_driver *new_udriver,

retval = driver_register(&new_udriver->drvwrap.driver);

Quoted 9 lines

drivers/usb/core/driver.c

Quoted 4 lines
drivers/usb/core/driver.c +3 -1
-	if (!retval)
+	if (!retval) {
+		bus_for_each_drv(&usb_bus_type, NULL, NULL,
+				 __usb_device_driver_added);

This looks funny. You're calling both bus_for_each_drv() and bus_for_each_dev(). Can't you skip this iterator and just call
bus_for_each_dev() directly?

Quoted 3 lines

You're right, looks like this could be simplified somewhat. I'm building and testing a smaller patch.

Quoted 5 lines

What do you mean "reprobe" for your device? Do you mean the mfi_fc_probe is not called? If it is, Would you please check why usb_device_match at drivers/usb/core/driver.c does not return true for
your device?

Quoted 3 lines

mfi_fc_probe() isn't called because the device already has the generic driver
attached by the time the apple-mfi-fastcharge driver is loaded.

Quoted 7 lines

Would you please explain more, why only this driver has this issue? Seem you
create a struct usb_device_driver, but not struct usb_driver, few drivers do like
that. You may see drivers/usb/misc/ehset.c and drivers/usb/misc/appledisplay.c
as an example.

Quoted 3 lines

It's a _device_ driver, not an interface driver. It's the only driver that has the problem because it's the only non-generic device driver :)

I may clear now, thanks.

So, you mean your device has no interface descriptor, so you can't create a USB interface driver, and have to create non-generic device driver for it. I see there is __check_usb_generic function at generic.c to check if it could use generic driver or not, you may add some condition (eg, no interface descriptor) to avoid using generic driver Is it feasible?

Signature · 2 lines
Thanks,
Peter Chen
#16 lore raw
Quoted 1 line · Peter Chen

<snip>

Quoted 9 lines

I may clear now, thanks.

So, you mean your device has no interface descriptor, so you can't create a USB interface driver, and have to create non-generic device driver for it. I see there is __check_usb_generic function at generic.c
to check if it could use generic driver or not, you may add some condition (eg, no interface descriptor) to avoid using generic driver Is it feasible?

I'm not looking for work-arounds, the driver is meant to be for *the device*, not for interfaces. It does have interfaces, but they're not the target of the calls made.

There's a race between USB device (not interface) drivers on the first plug, which I'm trying to fix.