Linux USB
 help / color / mirror / Atom feed
* [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups
@ 2026-12-06  2:44 Rosen Penev
  2026-12-06  2:44 ` [PATCH 01/12] usb: apple-mfi-fastcharge: Convert to devm resources Rosen Penev
                   ` (12 more replies)
  0 siblings, 13 replies; 21+ messages in thread
From: Rosen Penev @ 2026-12-06  2:44 UTC (permalink / raw)
  To: Bastien Nocera, Greg Kroah-Hartman; +Cc: linux-usb, linux-kernel

This series fixes several problems in the Apple MFi fast-charge
driver and modernizes it.

Patches 1-6 convert the driver to devm resources, use
pm_runtime_resume_and_get(), fix the usb_control_msg() error check and
timeout, register the power supply as POWER_SUPPLY_TYPE_USB instead of
BATTERY, constify the property array, and name the power supply after
the USB topology (busnum-devpath) instead of the device number, which
changes on every re-enumeration. Patches 4 and 6 change what userspace
sees: the power supply type and its name.

Patch 7 re-sends the fast-charge request on resume. A failure there is
logged but not returned, since an error from a usb_device_driver's
resume makes the USB core skip resuming the device's interfaces.

Patch 8 adds a mutex held across the USB control transfer, so that
concurrent writes to charge_type cannot leave the cached value
different from what was last sent to the device.

Patch 9 enables autosuspend. The device keeps charging at the
fast-charge current while it is USB-suspended.

Patches 10-12 are small cleanups.

Tested on an iPhone (05ac:12a8) connected to an x86 host:
- Fast/Trickle switch the current the phone reports for its USB
  charger between 2400 mA and 500 mA. Invalid values are rejected
  with -EINVAL.
- Two writers sending opposite values plus a reader, for 3 rounds
  (about 670 writes): afterwards the cached value always matched what
  the phone reported.
- With usbmuxd stopped and ipheth unbound, the phone autosuspends
  2 s after its last use. It kept charging while suspended (34% to
  43% in 433 s). Writes to charge_type wake the phone, and it suspends
  again afterwards.
System suspend and unplugging during a write were not tested.

Claude Code (Opus) was used to help prepare these patches.

Rosen Penev (12):
  usb: apple-mfi-fastcharge: Convert to devm resources
  usb: apple-mfi-fastcharge: Use pm_runtime_resume_and_get()
  usb: apple-mfi-fastcharge: Fix usb_control_msg() return check and
    timeout
  usb: apple-mfi-fastcharge: Use POWER_SUPPLY_TYPE_USB instead of
    BATTERY
  usb: apple-mfi-fastcharge: Constify power_supply_property array
  usb: apple-mfi-fastcharge: Use topology-based device name
  usb: apple-mfi-fastcharge: Restore charge current on resume
  usb: apple-mfi-fastcharge: Serialize charge_type access with a mutex
  usb: apple-mfi-fastcharge: Configure runtime PM autosuspend
  usb: apple-mfi-fastcharge: Name the vendor power request
  usb: apple-mfi-fastcharge: Rename battery variables to psy
  usb: apple-mfi-fastcharge: Simplify match and property_is_writeable

 drivers/usb/misc/apple-mfi-fastcharge.c | 171 ++++++++++++------------
 1 file changed, 85 insertions(+), 86 deletions(-)


base-commit: 29beb21c21ada7207692b2d69fc58dcd15dab6e9
-- 
2.56.1


^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH 01/12] usb: apple-mfi-fastcharge: Convert to devm resources
  2026-12-06  2:44 [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups Rosen Penev
@ 2026-12-06  2:44 ` Rosen Penev
  2026-12-06  3:00   ` sashiko-bot
  2026-12-06  2:44 ` [PATCH 02/12] usb: apple-mfi-fastcharge: Use pm_runtime_resume_and_get() Rosen Penev
                   ` (11 subsequent siblings)
  12 siblings, 1 reply; 21+ messages in thread
From: Rosen Penev @ 2026-12-06  2:44 UTC (permalink / raw)
  To: Bastien Nocera, Greg Kroah-Hartman; +Cc: linux-usb, linux-kernel

Use devm_kzalloc_obj(), devm_kasprintf(), and
devm_power_supply_register() to let the driver core handle resource
cleanup. This allows removing the mfi_fc_disconnect() function and
the error labels in probe.

Remove the redundant mfi_fc_match() check from probe since the
.match callback already handles filtering.

Also fix the module init/exit: module_usb_driver() is for struct
usb_driver *, not struct usb_device_driver *. Use the correct
usb_register_device_driver() / usb_deregister_device_driver() calls
instead.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/usb/misc/apple-mfi-fastcharge.c | 76 +++++++------------------
 1 file changed, 20 insertions(+), 56 deletions(-)

diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
index 02adec28d9e6..133dde848dd1 100644
--- a/drivers/usb/misc/apple-mfi-fastcharge.c
+++ b/drivers/usb/misc/apple-mfi-fastcharge.c
@@ -43,9 +43,8 @@ MODULE_DEVICE_TABLE(usb, mfi_fc_id_table);
 /* Driver-local specific stuff */
 struct mfi_device {
 	struct usb_device *udev;
-	struct power_supply *battery;
-	struct power_supply_desc battery_desc;
 	int charge_type;
+	struct power_supply_desc battery_desc;
 };
 
 static int apple_mfi_fc_set_charge_type(struct mfi_device *mfi,
@@ -155,16 +154,6 @@ static enum power_supply_property apple_mfi_fc_properties[] = {
 	POWER_SUPPLY_PROP_SCOPE
 };
 
-static const struct power_supply_desc apple_mfi_fc_desc = {
-	.name                   = "apple_mfi_fastcharge",
-	.type                   = POWER_SUPPLY_TYPE_BATTERY,
-	.properties             = apple_mfi_fc_properties,
-	.num_properties         = ARRAY_SIZE(apple_mfi_fc_properties),
-	.get_property           = apple_mfi_fc_get_property,
-	.set_property           = apple_mfi_fc_set_property,
-	.property_is_writeable  = apple_mfi_fc_property_is_writeable
-};
-
 static bool mfi_fc_match(struct usb_device *udev)
 {
 	int idProduct;
@@ -177,67 +166,42 @@ static bool mfi_fc_match(struct usb_device *udev)
 static int mfi_fc_probe(struct usb_device *udev)
 {
 	struct power_supply_config battery_cfg = {};
-	struct mfi_device *mfi = NULL;
-	char *battery_name;
-	int err;
-
-	if (!mfi_fc_match(udev))
-		return -ENODEV;
+	struct power_supply_desc *battery_desc;
+	struct power_supply *battery;
+	struct mfi_device *mfi;
 
-	mfi = kzalloc_obj(struct mfi_device);
+	mfi = devm_kzalloc_obj(&udev->dev, *mfi);
 	if (!mfi)
 		return -ENOMEM;
 
-	battery_name = kasprintf(GFP_KERNEL, "apple_mfi_fastcharge_%d-%d",
-				 udev->bus->busnum, udev->devnum);
-	if (!battery_name) {
-		err = -ENOMEM;
-		goto err_free_mfi;
-	}
+	battery_desc = &mfi->battery_desc;
+	battery_desc->name = devm_kasprintf(&udev->dev, GFP_KERNEL, "apple_mfi_fastcharge_%d-%d",
+					    udev->bus->busnum, udev->devnum);
+	if (!battery_desc->name)
+		return -ENOMEM;
 
-	mfi->battery_desc = apple_mfi_fc_desc;
-	mfi->battery_desc.name = battery_name;
+	battery_desc->type = POWER_SUPPLY_TYPE_BATTERY;
+	battery_desc->properties = apple_mfi_fc_properties;
+	battery_desc->num_properties = ARRAY_SIZE(apple_mfi_fc_properties);
+	battery_desc->get_property = apple_mfi_fc_get_property;
+	battery_desc->set_property = apple_mfi_fc_set_property;
+	battery_desc->property_is_writeable = apple_mfi_fc_property_is_writeable;
 
 	battery_cfg.drv_data = mfi;
 
 	mfi->charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
-	mfi->battery = power_supply_register(&udev->dev,
-						&mfi->battery_desc,
-						&battery_cfg);
-	if (IS_ERR(mfi->battery)) {
-		dev_err(&udev->dev, "Can't register battery\n");
-		err = PTR_ERR(mfi->battery);
-		goto err_free_name;
-	}
-
 	mfi->udev = udev;
-	dev_set_drvdata(&udev->dev, mfi);
 
-	return 0;
-
-err_free_name:
-	kfree(battery_name);
-err_free_mfi:
-	kfree(mfi);
-	return err;
-}
+	battery = devm_power_supply_register(&udev->dev, battery_desc, &battery_cfg);
+	if (IS_ERR(battery))
+		return PTR_ERR(battery);
 
-static void mfi_fc_disconnect(struct usb_device *udev)
-{
-	struct mfi_device *mfi;
-
-	mfi = dev_get_drvdata(&udev->dev);
-	if (mfi->battery)
-		power_supply_unregister(mfi->battery);
-	kfree(mfi->battery_desc.name);
-	dev_set_drvdata(&udev->dev, NULL);
-	kfree(mfi);
+	return 0;
 }
 
 static struct usb_device_driver mfi_fc_driver = {
 	.name =		"apple-mfi-fastcharge",
 	.probe =	mfi_fc_probe,
-	.disconnect =	mfi_fc_disconnect,
 	.id_table =	mfi_fc_id_table,
 	.match =	mfi_fc_match,
 	.generic_subclass = 1,
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH 02/12] usb: apple-mfi-fastcharge: Use pm_runtime_resume_and_get()
  2026-12-06  2:44 [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups Rosen Penev
  2026-12-06  2:44 ` [PATCH 01/12] usb: apple-mfi-fastcharge: Convert to devm resources Rosen Penev
@ 2026-12-06  2:44 ` Rosen Penev
  2026-12-06  2:44 ` [PATCH 03/12] usb: apple-mfi-fastcharge: Fix usb_control_msg() return check and timeout Rosen Penev
                   ` (10 subsequent siblings)
  12 siblings, 0 replies; 21+ messages in thread
From: Rosen Penev @ 2026-12-06  2:44 UTC (permalink / raw)
  To: Bastien Nocera, Greg Kroah-Hartman; +Cc: linux-usb, linux-kernel

Replace pm_runtime_get_sync() with pm_runtime_resume_and_get(), which
handles the pm_runtime_put_noidle() on error internally.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/usb/misc/apple-mfi-fastcharge.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
index 133dde848dd1..81138e9df9ad 100644
--- a/drivers/usb/misc/apple-mfi-fastcharge.c
+++ b/drivers/usb/misc/apple-mfi-fastcharge.c
@@ -119,11 +119,9 @@ static int apple_mfi_fc_set_property(struct power_supply *psy,
 
 	dev_dbg(&mfi->udev->dev, "prop: %d\n", psp);
 
-	ret = pm_runtime_get_sync(&mfi->udev->dev);
-	if (ret < 0) {
-		pm_runtime_put_noidle(&mfi->udev->dev);
+	ret = pm_runtime_resume_and_get(&mfi->udev->dev);
+	if (ret)
 		return ret;
-	}
 
 	switch (psp) {
 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH 03/12] usb: apple-mfi-fastcharge: Fix usb_control_msg() return check and timeout
  2026-12-06  2:44 [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups Rosen Penev
  2026-12-06  2:44 ` [PATCH 01/12] usb: apple-mfi-fastcharge: Convert to devm resources Rosen Penev
  2026-12-06  2:44 ` [PATCH 02/12] usb: apple-mfi-fastcharge: Use pm_runtime_resume_and_get() Rosen Penev
@ 2026-12-06  2:44 ` Rosen Penev
  2026-12-06  2:44 ` [PATCH 04/12] usb: apple-mfi-fastcharge: Use POWER_SUPPLY_TYPE_USB instead of BATTERY Rosen Penev
                   ` (9 subsequent siblings)
  12 siblings, 0 replies; 21+ messages in thread
From: Rosen Penev @ 2026-12-06  2:44 UTC (permalink / raw)
  To: Bastien Nocera, Greg Kroah-Hartman; +Cc: linux-usb, linux-kernel

Fix the return value check from if (retval) to if (retval < 0), since
usb_control_msg() returns the number of bytes transferred on success
and a negative errno on failure.

Also use USB_CTRL_SET_TIMEOUT instead of USB_CTRL_GET_TIMEOUT since
this is a control OUT message.

Upgrade the error message from dev_dbg to dev_err, since this is a
real error path that should be visible.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/usb/misc/apple-mfi-fastcharge.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
index 81138e9df9ad..9ec4bac60395 100644
--- a/drivers/usb/misc/apple-mfi-fastcharge.c
+++ b/drivers/usb/misc/apple-mfi-fastcharge.c
@@ -77,9 +77,9 @@ static int apple_mfi_fc_set_charge_type(struct mfi_device *mfi,
 				 request_type,
 				 current_ma, /* wValue, current offset */
 				 current_ma, /* wIndex, current offset */
-				 NULL, 0, USB_CTRL_GET_TIMEOUT);
-	if (retval) {
-		dev_dbg(&mfi->udev->dev, "retval = %d\n", retval);
+				 NULL, 0, USB_CTRL_SET_TIMEOUT);
+	if (retval < 0) {
+		dev_err(&mfi->udev->dev, "Failed to set charge type: %d\n", retval);
 		return retval;
 	}
 
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH 04/12] usb: apple-mfi-fastcharge: Use POWER_SUPPLY_TYPE_USB instead of BATTERY
  2026-12-06  2:44 [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups Rosen Penev
                   ` (2 preceding siblings ...)
  2026-12-06  2:44 ` [PATCH 03/12] usb: apple-mfi-fastcharge: Fix usb_control_msg() return check and timeout Rosen Penev
@ 2026-12-06  2:44 ` Rosen Penev
  2026-12-06  3:04   ` sashiko-bot
  2026-12-06  2:44 ` [PATCH 05/12] usb: apple-mfi-fastcharge: Constify power_supply_property array Rosen Penev
                   ` (8 subsequent siblings)
  12 siblings, 1 reply; 21+ messages in thread
From: Rosen Penev @ 2026-12-06  2:44 UTC (permalink / raw)
  To: Bastien Nocera, Greg Kroah-Hartman; +Cc: linux-usb, linux-kernel

This device is a charging control interface, not a battery. Using
POWER_SUPPLY_TYPE_BATTERY would confuse userspace.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/usb/misc/apple-mfi-fastcharge.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
index 9ec4bac60395..63ceec5c2c3f 100644
--- a/drivers/usb/misc/apple-mfi-fastcharge.c
+++ b/drivers/usb/misc/apple-mfi-fastcharge.c
@@ -178,7 +178,7 @@ static int mfi_fc_probe(struct usb_device *udev)
 	if (!battery_desc->name)
 		return -ENOMEM;
 
-	battery_desc->type = POWER_SUPPLY_TYPE_BATTERY;
+	battery_desc->type = POWER_SUPPLY_TYPE_USB;
 	battery_desc->properties = apple_mfi_fc_properties;
 	battery_desc->num_properties = ARRAY_SIZE(apple_mfi_fc_properties);
 	battery_desc->get_property = apple_mfi_fc_get_property;
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH 05/12] usb: apple-mfi-fastcharge: Constify power_supply_property array
  2026-12-06  2:44 [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups Rosen Penev
                   ` (3 preceding siblings ...)
  2026-12-06  2:44 ` [PATCH 04/12] usb: apple-mfi-fastcharge: Use POWER_SUPPLY_TYPE_USB instead of BATTERY Rosen Penev
@ 2026-12-06  2:44 ` Rosen Penev
  2026-12-06  2:44 ` [PATCH 06/12] usb: apple-mfi-fastcharge: Use topology-based device name Rosen Penev
                   ` (7 subsequent siblings)
  12 siblings, 0 replies; 21+ messages in thread
From: Rosen Penev @ 2026-12-06  2:44 UTC (permalink / raw)
  To: Bastien Nocera, Greg Kroah-Hartman; +Cc: linux-usb, linux-kernel

The property list is never modified at runtime, so make it const so
that it can be placed in read-only memory.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/usb/misc/apple-mfi-fastcharge.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
index 63ceec5c2c3f..24d6a11cb5e0 100644
--- a/drivers/usb/misc/apple-mfi-fastcharge.c
+++ b/drivers/usb/misc/apple-mfi-fastcharge.c
@@ -147,7 +147,7 @@ static int apple_mfi_fc_property_is_writeable(struct power_supply *psy,
 	}
 }
 
-static enum power_supply_property apple_mfi_fc_properties[] = {
+static const enum power_supply_property apple_mfi_fc_properties[] = {
 	POWER_SUPPLY_PROP_CHARGE_TYPE,
 	POWER_SUPPLY_PROP_SCOPE
 };
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH 06/12] usb: apple-mfi-fastcharge: Use topology-based device name
  2026-12-06  2:44 [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups Rosen Penev
                   ` (4 preceding siblings ...)
  2026-12-06  2:44 ` [PATCH 05/12] usb: apple-mfi-fastcharge: Constify power_supply_property array Rosen Penev
@ 2026-12-06  2:44 ` Rosen Penev
  2026-12-06  2:44 ` [PATCH 07/12] usb: apple-mfi-fastcharge: Restore charge current on resume Rosen Penev
                   ` (6 subsequent siblings)
  12 siblings, 0 replies; 21+ messages in thread
From: Rosen Penev @ 2026-12-06  2:44 UTC (permalink / raw)
  To: Bastien Nocera, Greg Kroah-Hartman; +Cc: linux-usb, linux-kernel

The device name used devnum which changes across re-enumeration,
making the power supply name unstable for userspace. Use
dev_name() instead, which gives a topology-based identifier
(busnum-devpath) that is stable across re-enumeration.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/usb/misc/apple-mfi-fastcharge.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
index 24d6a11cb5e0..49644922c5b5 100644
--- a/drivers/usb/misc/apple-mfi-fastcharge.c
+++ b/drivers/usb/misc/apple-mfi-fastcharge.c
@@ -173,8 +173,8 @@ static int mfi_fc_probe(struct usb_device *udev)
 		return -ENOMEM;
 
 	battery_desc = &mfi->battery_desc;
-	battery_desc->name = devm_kasprintf(&udev->dev, GFP_KERNEL, "apple_mfi_fastcharge_%d-%d",
-					    udev->bus->busnum, udev->devnum);
+	battery_desc->name = devm_kasprintf(&udev->dev, GFP_KERNEL, "apple_mfi_fastcharge_%s",
+						dev_name(&udev->dev));
 	if (!battery_desc->name)
 		return -ENOMEM;
 
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH 07/12] usb: apple-mfi-fastcharge: Restore charge current on resume
  2026-12-06  2:44 [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups Rosen Penev
                   ` (5 preceding siblings ...)
  2026-12-06  2:44 ` [PATCH 06/12] usb: apple-mfi-fastcharge: Use topology-based device name Rosen Penev
@ 2026-12-06  2:44 ` Rosen Penev
  2026-12-06  3:05   ` sashiko-bot
  2026-12-06  2:44 ` [PATCH 08/12] usb: apple-mfi-fastcharge: Serialize charge_type access with a mutex Rosen Penev
                   ` (5 subsequent siblings)
  12 siblings, 1 reply; 21+ messages in thread
From: Rosen Penev @ 2026-12-06  2:44 UTC (permalink / raw)
  To: Bastien Nocera, Greg Kroah-Hartman; +Cc: linux-usb, linux-kernel

If the device loses its charging state across suspend/resume, the
cached charge_type will diverge from the hardware. Add a resume
callback that reprograms the current if it was set to FAST.

If reprogramming fails, log the error, fall back to TRICKLE so a
later write of "Fast" is sent again, and still return 0: an error
from a usb_device_driver's resume makes the USB core skip resuming
the device's interfaces.

Refactor the USB control message into apple_mfi_fc_send_current()
to share the code between set_charge_type and resume.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/usb/misc/apple-mfi-fastcharge.c | 66 ++++++++++++++++++-------
 1 file changed, 47 insertions(+), 19 deletions(-)

diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
index 49644922c5b5..85cf970e5f5e 100644
--- a/drivers/usb/misc/apple-mfi-fastcharge.c
+++ b/drivers/usb/misc/apple-mfi-fastcharge.c
@@ -47,18 +47,28 @@ struct mfi_device {
 	struct power_supply_desc battery_desc;
 };
 
+static int apple_mfi_fc_send_current(struct mfi_device *mfi, int current_ma)
+{
+	int retval;
+
+	retval = usb_control_msg(mfi->udev, usb_sndctrlpipe(mfi->udev, 0),
+				 0x40, /* Vendor‐defined power request */
+				 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
+				 current_ma, /* wValue, current offset */
+				 current_ma, /* wIndex, current offset */
+				 NULL, 0, USB_CTRL_SET_TIMEOUT);
+	if (retval < 0)
+		dev_err(&mfi->udev->dev, "Failed to set charge current: %d\n", retval);
+
+	return retval;
+}
+
 static int apple_mfi_fc_set_charge_type(struct mfi_device *mfi,
 					const union power_supply_propval *val)
 {
 	int current_ma;
-	int retval;
-	__u8 request_type;
-
-	if (mfi->charge_type == val->intval) {
-		dev_dbg(&mfi->udev->dev, "charge type %d already set\n",
-				mfi->charge_type);
-		return 0;
-	}
+	int old_type;
+	int ret;
 
 	switch (val->intval) {
 	case POWER_SUPPLY_CHARGE_TYPE_TRICKLE:
@@ -71,21 +81,20 @@ static int apple_mfi_fc_set_charge_type(struct mfi_device *mfi,
 		return -EINVAL;
 	}
 
-	request_type = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
-	retval = usb_control_msg(mfi->udev, usb_sndctrlpipe(mfi->udev, 0),
-				 0x40, /* Vendor‐defined power request */
-				 request_type,
-				 current_ma, /* wValue, current offset */
-				 current_ma, /* wIndex, current offset */
-				 NULL, 0, USB_CTRL_SET_TIMEOUT);
-	if (retval < 0) {
-		dev_err(&mfi->udev->dev, "Failed to set charge type: %d\n", retval);
-		return retval;
+	if (mfi->charge_type == val->intval) {
+		dev_dbg(&mfi->udev->dev, "charge type %d already set\n",
+			mfi->charge_type);
+		return 0;
 	}
 
+	old_type = mfi->charge_type;
 	mfi->charge_type = val->intval;
 
-	return 0;
+	ret = apple_mfi_fc_send_current(mfi, current_ma);
+	if (ret < 0)
+		mfi->charge_type = old_type;
+
+	return ret;
 }
 
 static int apple_mfi_fc_get_property(struct power_supply *psy,
@@ -194,12 +203,31 @@ static int mfi_fc_probe(struct usb_device *udev)
 	if (IS_ERR(battery))
 		return PTR_ERR(battery);
 
+	dev_set_drvdata(&udev->dev, mfi);
+
+	return 0;
+}
+
+static int mfi_fc_resume(struct usb_device *udev, pm_message_t message)
+{
+	struct mfi_device *mfi = dev_get_drvdata(&udev->dev);
+
+	/*
+	 * A failure here must not fail the resume: the USB core would then
+	 * skip resuming the device's interfaces. Fall back to trickle so a
+	 * later write of "Fast" is sent to the device again.
+	 */
+	if (mfi->charge_type == POWER_SUPPLY_CHARGE_TYPE_FAST &&
+	    apple_mfi_fc_send_current(mfi, FAST_CURRENT_MA) < 0)
+		mfi->charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
+
 	return 0;
 }
 
 static struct usb_device_driver mfi_fc_driver = {
 	.name =		"apple-mfi-fastcharge",
 	.probe =	mfi_fc_probe,
+	.resume =	mfi_fc_resume,
 	.id_table =	mfi_fc_id_table,
 	.match =	mfi_fc_match,
 	.generic_subclass = 1,
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH 08/12] usb: apple-mfi-fastcharge: Serialize charge_type access with a mutex
  2026-12-06  2:44 [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups Rosen Penev
                   ` (6 preceding siblings ...)
  2026-12-06  2:44 ` [PATCH 07/12] usb: apple-mfi-fastcharge: Restore charge current on resume Rosen Penev
@ 2026-12-06  2:44 ` Rosen Penev
  2026-12-06  3:07   ` sashiko-bot
  2026-12-06  2:44 ` [PATCH 09/12] usb: apple-mfi-fastcharge: Configure runtime PM autosuspend Rosen Penev
                   ` (4 subsequent siblings)
  12 siblings, 1 reply; 21+ messages in thread
From: Rosen Penev @ 2026-12-06  2:44 UTC (permalink / raw)
  To: Bastien Nocera, Greg Kroah-Hartman; +Cc: linux-usb, linux-kernel

Concurrent writes to charge_type from userspace can race: both
threads may read the old value, send their USB command, and write
the software state, leaving the cached charge_type mismatched with
the actual hardware state.

Add a per-device mutex and hold it across the USB control transfer
in set_charge_type, so the cached value always matches the last
current sent to the device. Update the cache only after the transfer
succeeds, which removes the need to roll it back on error. Take the
same lock in get_property and in the resume path. Initialize it with
devm_mutex_init() and use guard(mutex) for scoped locking.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/usb/misc/apple-mfi-fastcharge.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
index 85cf970e5f5e..fb4b975a62e9 100644
--- a/drivers/usb/misc/apple-mfi-fastcharge.c
+++ b/drivers/usb/misc/apple-mfi-fastcharge.c
@@ -7,6 +7,7 @@
 
 /* Standard include files */
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/power_supply.h>
 #include <linux/slab.h>
 #include <linux/usb.h>
@@ -44,6 +45,7 @@ MODULE_DEVICE_TABLE(usb, mfi_fc_id_table);
 struct mfi_device {
 	struct usb_device *udev;
 	int charge_type;
+	struct mutex lock;
 	struct power_supply_desc battery_desc;
 };
 
@@ -67,7 +69,6 @@ static int apple_mfi_fc_set_charge_type(struct mfi_device *mfi,
 					const union power_supply_propval *val)
 {
 	int current_ma;
-	int old_type;
 	int ret;
 
 	switch (val->intval) {
@@ -81,20 +82,20 @@ static int apple_mfi_fc_set_charge_type(struct mfi_device *mfi,
 		return -EINVAL;
 	}
 
+	guard(mutex)(&mfi->lock);
 	if (mfi->charge_type == val->intval) {
 		dev_dbg(&mfi->udev->dev, "charge type %d already set\n",
 			mfi->charge_type);
 		return 0;
 	}
 
-	old_type = mfi->charge_type;
-	mfi->charge_type = val->intval;
-
 	ret = apple_mfi_fc_send_current(mfi, current_ma);
 	if (ret < 0)
-		mfi->charge_type = old_type;
+		return ret;
 
-	return ret;
+	mfi->charge_type = val->intval;
+
+	return 0;
 }
 
 static int apple_mfi_fc_get_property(struct power_supply *psy,
@@ -106,9 +107,11 @@ static int apple_mfi_fc_get_property(struct power_supply *psy,
 	dev_dbg(&mfi->udev->dev, "prop: %d\n", psp);
 
 	switch (psp) {
-	case POWER_SUPPLY_PROP_CHARGE_TYPE:
+	case POWER_SUPPLY_PROP_CHARGE_TYPE: {
+		guard(mutex)(&mfi->lock);
 		val->intval = mfi->charge_type;
 		break;
+	}
 	case POWER_SUPPLY_PROP_SCOPE:
 		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
 		break;
@@ -176,11 +179,16 @@ static int mfi_fc_probe(struct usb_device *udev)
 	struct power_supply_desc *battery_desc;
 	struct power_supply *battery;
 	struct mfi_device *mfi;
+	int ret;
 
 	mfi = devm_kzalloc_obj(&udev->dev, *mfi);
 	if (!mfi)
 		return -ENOMEM;
 
+	ret = devm_mutex_init(&udev->dev, &mfi->lock);
+	if (ret)
+		return ret;
+
 	battery_desc = &mfi->battery_desc;
 	battery_desc->name = devm_kasprintf(&udev->dev, GFP_KERNEL, "apple_mfi_fastcharge_%s",
 						dev_name(&udev->dev));
@@ -212,6 +220,8 @@ static int mfi_fc_resume(struct usb_device *udev, pm_message_t message)
 {
 	struct mfi_device *mfi = dev_get_drvdata(&udev->dev);
 
+	guard(mutex)(&mfi->lock);
+
 	/*
 	 * A failure here must not fail the resume: the USB core would then
 	 * skip resuming the device's interfaces. Fall back to trickle so a
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH 09/12] usb: apple-mfi-fastcharge: Configure runtime PM autosuspend
  2026-12-06  2:44 [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups Rosen Penev
                   ` (7 preceding siblings ...)
  2026-12-06  2:44 ` [PATCH 08/12] usb: apple-mfi-fastcharge: Serialize charge_type access with a mutex Rosen Penev
@ 2026-12-06  2:44 ` Rosen Penev
  2026-12-06  3:05   ` sashiko-bot
  2026-12-06  2:44 ` [PATCH 10/12] usb: apple-mfi-fastcharge: Name the vendor power request Rosen Penev
                   ` (3 subsequent siblings)
  12 siblings, 1 reply; 21+ messages in thread
From: Rosen Penev @ 2026-12-06  2:44 UTC (permalink / raw)
  To: Bastien Nocera, Greg Kroah-Hartman; +Cc: linux-usb, linux-kernel

The USB core disables autosuspend for all USB devices by default.
The driver called pm_runtime_put_autosuspend() without ever enabling
autosuspend, making those calls ineffective.

Add supports_autosuspend to the driver struct so the USB core does
not force-resume the device on probe, and call
usb_enable_autosuspend() after probe succeeds to allow the device
to autosuspend when idle.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/usb/misc/apple-mfi-fastcharge.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
index fb4b975a62e9..83009b601da1 100644
--- a/drivers/usb/misc/apple-mfi-fastcharge.c
+++ b/drivers/usb/misc/apple-mfi-fastcharge.c
@@ -212,6 +212,7 @@ static int mfi_fc_probe(struct usb_device *udev)
 		return PTR_ERR(battery);
 
 	dev_set_drvdata(&udev->dev, mfi);
+	usb_enable_autosuspend(udev);
 
 	return 0;
 }
@@ -241,6 +242,7 @@ static struct usb_device_driver mfi_fc_driver = {
 	.id_table =	mfi_fc_id_table,
 	.match =	mfi_fc_match,
 	.generic_subclass = 1,
+	.supports_autosuspend = 1,
 };
 
 static int __init mfi_fc_driver_init(void)
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH 10/12] usb: apple-mfi-fastcharge: Name the vendor power request
  2026-12-06  2:44 [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups Rosen Penev
                   ` (8 preceding siblings ...)
  2026-12-06  2:44 ` [PATCH 09/12] usb: apple-mfi-fastcharge: Configure runtime PM autosuspend Rosen Penev
@ 2026-12-06  2:44 ` Rosen Penev
  2026-12-06  2:44 ` [PATCH 11/12] usb: apple-mfi-fastcharge: Rename battery variables to psy Rosen Penev
                   ` (2 subsequent siblings)
  12 siblings, 0 replies; 21+ messages in thread
From: Rosen Penev @ 2026-12-06  2:44 UTC (permalink / raw)
  To: Bastien Nocera, Greg Kroah-Hartman; +Cc: linux-usb, linux-kernel

Replace the bare 0x40 request code with APPLE_MFI_REQ_SET_CURRENT.
This also removes the inline comment, which used a non-ASCII hyphen
(U+2010).

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/usb/misc/apple-mfi-fastcharge.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
index 83009b601da1..71dcf5f0bdc4 100644
--- a/drivers/usb/misc/apple-mfi-fastcharge.c
+++ b/drivers/usb/misc/apple-mfi-fastcharge.c
@@ -21,6 +21,9 @@ MODULE_LICENSE("GPL");
 
 #define APPLE_VENDOR_ID			0x05ac	/* Apple */
 
+/* Vendor-defined request that sets the current the device may draw */
+#define APPLE_MFI_REQ_SET_CURRENT	0x40
+
 /* The product ID is defined as starting with 0x12nn, as per the
  * "Choosing an Apple Device USB Configuration" section in
  * release R9 (2012) of the "MFi Accessory Hardware Specification"
@@ -54,7 +57,7 @@ static int apple_mfi_fc_send_current(struct mfi_device *mfi, int current_ma)
 	int retval;
 
 	retval = usb_control_msg(mfi->udev, usb_sndctrlpipe(mfi->udev, 0),
-				 0x40, /* Vendor‐defined power request */
+				 APPLE_MFI_REQ_SET_CURRENT,
 				 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
 				 current_ma, /* wValue, current offset */
 				 current_ma, /* wIndex, current offset */
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH 11/12] usb: apple-mfi-fastcharge: Rename battery variables to psy
  2026-12-06  2:44 [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups Rosen Penev
                   ` (9 preceding siblings ...)
  2026-12-06  2:44 ` [PATCH 10/12] usb: apple-mfi-fastcharge: Name the vendor power request Rosen Penev
@ 2026-12-06  2:44 ` Rosen Penev
  2026-12-06  2:58   ` sashiko-bot
  2026-12-06  2:44 ` [PATCH 12/12] usb: apple-mfi-fastcharge: Simplify match and property_is_writeable Rosen Penev
  2026-12-06  3:07 ` [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups sashiko-bot
  12 siblings, 1 reply; 21+ messages in thread
From: Rosen Penev @ 2026-12-06  2:44 UTC (permalink / raw)
  To: Bastien Nocera, Greg Kroah-Hartman; +Cc: linux-usb, linux-kernel

The power supply is registered as POWER_SUPPLY_TYPE_USB, not as a
battery. Rename battery, battery_desc and battery_cfg to psy,
psy_desc and psy_cfg to match.

No functional change.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/usb/misc/apple-mfi-fastcharge.c | 34 ++++++++++++-------------
 1 file changed, 17 insertions(+), 17 deletions(-)

diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
index 71dcf5f0bdc4..f6355db6ec99 100644
--- a/drivers/usb/misc/apple-mfi-fastcharge.c
+++ b/drivers/usb/misc/apple-mfi-fastcharge.c
@@ -49,7 +49,7 @@ struct mfi_device {
 	struct usb_device *udev;
 	int charge_type;
 	struct mutex lock;
-	struct power_supply_desc battery_desc;
+	struct power_supply_desc psy_desc;
 };
 
 static int apple_mfi_fc_send_current(struct mfi_device *mfi, int current_ma)
@@ -178,9 +178,9 @@ static bool mfi_fc_match(struct usb_device *udev)
 
 static int mfi_fc_probe(struct usb_device *udev)
 {
-	struct power_supply_config battery_cfg = {};
-	struct power_supply_desc *battery_desc;
-	struct power_supply *battery;
+	struct power_supply_config psy_cfg = {};
+	struct power_supply_desc *psy_desc;
+	struct power_supply *psy;
 	struct mfi_device *mfi;
 	int ret;
 
@@ -192,27 +192,27 @@ static int mfi_fc_probe(struct usb_device *udev)
 	if (ret)
 		return ret;
 
-	battery_desc = &mfi->battery_desc;
-	battery_desc->name = devm_kasprintf(&udev->dev, GFP_KERNEL, "apple_mfi_fastcharge_%s",
+	psy_desc = &mfi->psy_desc;
+	psy_desc->name = devm_kasprintf(&udev->dev, GFP_KERNEL, "apple_mfi_fastcharge_%s",
 						dev_name(&udev->dev));
-	if (!battery_desc->name)
+	if (!psy_desc->name)
 		return -ENOMEM;
 
-	battery_desc->type = POWER_SUPPLY_TYPE_USB;
-	battery_desc->properties = apple_mfi_fc_properties;
-	battery_desc->num_properties = ARRAY_SIZE(apple_mfi_fc_properties);
-	battery_desc->get_property = apple_mfi_fc_get_property;
-	battery_desc->set_property = apple_mfi_fc_set_property;
-	battery_desc->property_is_writeable = apple_mfi_fc_property_is_writeable;
+	psy_desc->type = POWER_SUPPLY_TYPE_USB;
+	psy_desc->properties = apple_mfi_fc_properties;
+	psy_desc->num_properties = ARRAY_SIZE(apple_mfi_fc_properties);
+	psy_desc->get_property = apple_mfi_fc_get_property;
+	psy_desc->set_property = apple_mfi_fc_set_property;
+	psy_desc->property_is_writeable = apple_mfi_fc_property_is_writeable;
 
-	battery_cfg.drv_data = mfi;
+	psy_cfg.drv_data = mfi;
 
 	mfi->charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
 	mfi->udev = udev;
 
-	battery = devm_power_supply_register(&udev->dev, battery_desc, &battery_cfg);
-	if (IS_ERR(battery))
-		return PTR_ERR(battery);
+	psy = devm_power_supply_register(&udev->dev, psy_desc, &psy_cfg);
+	if (IS_ERR(psy))
+		return PTR_ERR(psy);
 
 	dev_set_drvdata(&udev->dev, mfi);
 	usb_enable_autosuspend(udev);
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH 12/12] usb: apple-mfi-fastcharge: Simplify match and property_is_writeable
  2026-12-06  2:44 [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups Rosen Penev
                   ` (10 preceding siblings ...)
  2026-12-06  2:44 ` [PATCH 11/12] usb: apple-mfi-fastcharge: Rename battery variables to psy Rosen Penev
@ 2026-12-06  2:44 ` Rosen Penev
  2026-12-06  3:06   ` sashiko-bot
  2026-12-06  3:07 ` [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups sashiko-bot
  12 siblings, 1 reply; 21+ messages in thread
From: Rosen Penev @ 2026-12-06  2:44 UTC (permalink / raw)
  To: Bastien Nocera, Greg Kroah-Hartman; +Cc: linux-usb, linux-kernel

Check the 0x12nn product ID range with a mask on a u16, matching the
type of the descriptor field, and reduce property_is_writeable() to a
single comparison.

No functional change.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/usb/misc/apple-mfi-fastcharge.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)

diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
index f6355db6ec99..41eb0d56f9e0 100644
--- a/drivers/usb/misc/apple-mfi-fastcharge.c
+++ b/drivers/usb/misc/apple-mfi-fastcharge.c
@@ -154,12 +154,7 @@ static int apple_mfi_fc_set_property(struct power_supply *psy,
 static int apple_mfi_fc_property_is_writeable(struct power_supply *psy,
 					      enum power_supply_property psp)
 {
-	switch (psp) {
-	case POWER_SUPPLY_PROP_CHARGE_TYPE:
-		return 1;
-	default:
-		return 0;
-	}
+	return psp == POWER_SUPPLY_PROP_CHARGE_TYPE;
 }
 
 static const enum power_supply_property apple_mfi_fc_properties[] = {
@@ -169,11 +164,10 @@ static const enum power_supply_property apple_mfi_fc_properties[] = {
 
 static bool mfi_fc_match(struct usb_device *udev)
 {
-	int idProduct;
+	u16 idProduct = le16_to_cpu(udev->descriptor.idProduct);
 
-	idProduct = le16_to_cpu(udev->descriptor.idProduct);
 	/* See comment above mfi_fc_id_table[] */
-	return (idProduct >= 0x1200 && idProduct <= 0x12ff);
+	return (idProduct & 0xff00) == 0x1200;
 }
 
 static int mfi_fc_probe(struct usb_device *udev)
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* Re: [PATCH 11/12] usb: apple-mfi-fastcharge: Rename battery variables to psy
  2026-12-06  2:44 ` [PATCH 11/12] usb: apple-mfi-fastcharge: Rename battery variables to psy Rosen Penev
@ 2026-12-06  2:58   ` sashiko-bot
  0 siblings, 0 replies; 21+ messages in thread
From: sashiko-bot @ 2026-12-06  2:58 UTC (permalink / raw)
  To: Rosen Penev; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] usb: apple-mfi-fastcharge: NULL deref in mfi_fc_resume() during
  probe
--

commit 4d0b06a77285f110f6866355c9ab47b9f51b4b31
Author: Rosen Penev <rosenp@gmail.com>

usb: apple-mfi-fastcharge: Rename battery variables to psy

This commit renames battery, battery_desc, and battery_cfg to psy,
psy_desc, and psy_cfg in apple-mfi-fastcharge to match its
POWER_SUPPLY_TYPE_USB registration.

> diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
> index 71dcf5f0bdc45..f6355db6ec99d 100644
> --- a/drivers/usb/misc/apple-mfi-fastcharge.c
> +++ b/drivers/usb/misc/apple-mfi-fastcharge.c

[ ... ]

> @@ -192,27 +192,27 @@ static int mfi_fc_probe(struct usb_device *udev)

[ ... ]

> -	battery = devm_power_supply_register(&udev->dev, battery_desc, &battery_cfg);
> -	if (IS_ERR(battery))
> -		return PTR_ERR(battery);
> +	psy = devm_power_supply_register(&udev->dev, psy_desc, &psy_cfg);
> +	if (IS_ERR(psy))
> +		return PTR_ERR(psy);
>  
>  	dev_set_drvdata(&udev->dev, mfi);
>  	usb_enable_autosuspend(udev);

[Severity: High]
Can mfi_fc_resume() dereference a NULL mfi pointer during probe?

mfi_fc_probe() enables autosuspend without disabling it on disconnect.
After unbinding the driver (e.g. rmmod or sysfs unbind), dev->driver_data
is cleared to NULL while runtime PM stays enabled, so the unbound device
can autosuspend.

On rebind, really_probe() sets dev->driver before usb_probe_device().
With generic_subclass = 1, usb_probe_device() calls
usb_generic_driver_probe() -> usb_set_configuration() ->
usb_autoresume_device() -> mfi_fc_resume() before mfi_fc_probe() runs:

drivers/usb/misc/apple-mfi-fastcharge.c:mfi_fc_resume() {
	struct mfi_device *mfi = dev_get_drvdata(&udev->dev);

	guard(mutex)(&mfi->lock);
	...
}

At that point dev_get_drvdata(&udev->dev) returns NULL, causing a NULL
pointer dereference when locking &mfi->lock.

Separately, mfi_fc_probe() registers the power supply before calling
dev_set_drvdata(&udev->dev, mfi), so a concurrent write to charge_type on
a suspended device in that window triggers the same NULL pointer
dereference via apple_mfi_fc_set_property() -> pm_runtime_resume_and_get().

Checking if (!mfi) return 0 in mfi_fc_resume() and moving
dev_set_drvdata(&udev->dev, mfi) before devm_power_supply_register()
avoids both paths.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796524906.git.rosenp@gmail.com?part=11

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 01/12] usb: apple-mfi-fastcharge: Convert to devm resources
  2026-12-06  2:44 ` [PATCH 01/12] usb: apple-mfi-fastcharge: Convert to devm resources Rosen Penev
@ 2026-12-06  3:00   ` sashiko-bot
  0 siblings, 0 replies; 21+ messages in thread
From: sashiko-bot @ 2026-12-06  3:00 UTC (permalink / raw)
  To: Rosen Penev; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] usb: apple-mfi-fastcharge: devm power supply unregister races
  with USB unbind
- [Medium] usb: apple-mfi-fastcharge: commit message describes absent
  module init fix
- [Low] usb: apple-mfi-fastcharge: stale comment still mentions .probe()
  ID check
--

commit 427d84edebb61420afc7208c728895fb30a00bfa
Author: Rosen Penev <rosenp@gmail.com>

usb: apple-mfi-fastcharge: Convert to devm resources

This commit converts apple-mfi-fastcharge to device-managed allocations and
power supply registration, removes mfi_fc_disconnect(), and drops the
mfi_fc_match() call from mfi_fc_probe().

> Also fix the module init/exit: module_usb_driver() is for struct
> usb_driver *, not struct usb_device_driver *. Use the correct
> usb_register_device_driver() / usb_deregister_device_driver() calls
> instead.

[Severity: Medium]
Should this paragraph be dropped from the commit message?

mfi_fc_driver_init() and mfi_fc_driver_exit() already call
usb_register_device_driver() and usb_deregister_device_driver(), and this
patch does not touch them.

Separately, the patch moves mfi->udev = udev before power supply
registration and drops the dev_err() on registration failure without
mentioning either change in the changelog.

> diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
> index 02adec28d9e69..133dde848dd12 100644
> --- a/drivers/usb/misc/apple-mfi-fastcharge.c
> +++ b/drivers/usb/misc/apple-mfi-fastcharge.c

[ ... ]

> @@ -177,67 +166,42 @@ static int mfi_fc_probe(struct usb_device *udev)
>  {
>  	struct power_supply_config battery_cfg = {};
> -	struct mfi_device *mfi = NULL;
> -	char *battery_name;
> -	int err;
> -
> -	if (!mfi_fc_match(udev))
> -		return -ENODEV;

[Severity: Low]
With mfi_fc_match() removed from mfi_fc_probe(), the comment above
mfi_fc_id_table[] still says "Those checks will be done in .match() and
.probe()."  Should that comment be updated?

[ ... ]

> +	battery = devm_power_supply_register(&udev->dev, battery_desc, &battery_cfg);
> +	if (IS_ERR(battery))
> +		return PTR_ERR(battery);
>  
> -static void mfi_fc_disconnect(struct usb_device *udev)
> -{
> -	struct mfi_device *mfi;
> -
> -	mfi = dev_get_drvdata(&udev->dev);
> -	if (mfi->battery)
> -		power_supply_unregister(mfi->battery);
> -	kfree(mfi->battery_desc.name);
> -	dev_set_drvdata(&udev->dev, NULL);
> -	kfree(mfi);
> +	return 0;
>  }
>  
>  static struct usb_device_driver mfi_fc_driver = {
>  	.name =		"apple-mfi-fastcharge",
>  	.probe =	mfi_fc_probe,
> -	.disconnect =	mfi_fc_disconnect,

[Severity: Medium]
Can switching to devm_power_supply_register() and removing
mfi_fc_disconnect() race with a concurrent sysfs write to charge_type
during USB unbind?

__device_release_driver() runs device_remove() before
device_unbind_cleanup().  Because mfi_fc_driver has no .disconnect callback
now, usb_unbind_device() runs usb_generic_driver_disconnect() and
usb_autosuspend_device() while the power supply is still registered:

CPU0                              CPU1
__device_release_driver()
  device_lock(&udev->dev)
  usb_unbind_device()
    usb_generic_driver_disconnect()
      usb_set_configuration(..., -1)
    usb_autosuspend_device()
                                  apple_mfi_fc_set_property()
                                    pm_runtime_get_sync(&udev->dev)
                                      usb_runtime_resume() <-- no device_lock
                                    usb_control_msg(...)   <-- unconfigured
  device_unbind_cleanup()
    power_supply_unregister()

A concurrent write to charge_type during rmmod or sysfs unbind can submit
vendor control request 0x40 to an unconfigured device or trigger
usb_runtime_resume() -> usb_resume_both() -> finish_port_resume() without
holding device_lock(&udev->dev) while unbind holds it.  Keeping
mfi_fc_disconnect() to call power_supply_unregister() synchronously before
usb_generic_driver_disconnect() runs avoids this window.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796524906.git.rosenp@gmail.com?part=1

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 04/12] usb: apple-mfi-fastcharge: Use POWER_SUPPLY_TYPE_USB instead of BATTERY
  2026-12-06  2:44 ` [PATCH 04/12] usb: apple-mfi-fastcharge: Use POWER_SUPPLY_TYPE_USB instead of BATTERY Rosen Penev
@ 2026-12-06  3:04   ` sashiko-bot
  0 siblings, 0 replies; 21+ messages in thread
From: sashiko-bot @ 2026-12-06  3:04 UTC (permalink / raw)
  To: Rosen Penev; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] usb: apple-mfi-fastcharge: changing type to USB breaks
  sysfs/uevent ABI
--

commit 0d3db2f7dca0ee3251630abcee5663e509cef35a
Author: Rosen Penev <rosenp@gmail.com>

usb: apple-mfi-fastcharge: Use POWER_SUPPLY_TYPE_USB instead of BATTERY

This commit changes the power_supply_desc type registered by
apple-mfi-fastcharge from POWER_SUPPLY_TYPE_BATTERY to
POWER_SUPPLY_TYPE_USB.

> diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
> index 9ec4bac603951..63ceec5c2c3fe 100644
> --- a/drivers/usb/misc/apple-mfi-fastcharge.c
> +++ b/drivers/usb/misc/apple-mfi-fastcharge.c
> @@ -178,7 +178,7 @@ static int mfi_fc_probe(struct usb_device *udev)
>  	if (!battery_desc->name)
>  		return -ENOMEM;
>  
> -	battery_desc->type = POWER_SUPPLY_TYPE_BATTERY;
> +	battery_desc->type = POWER_SUPPLY_TYPE_USB;
>  	battery_desc->properties = apple_mfi_fc_properties;
>  	battery_desc->num_properties = ARRAY_SIZE(apple_mfi_fc_properties);
>  	battery_desc->get_property = apple_mfi_fc_get_property;

[Severity: High]
Does changing battery_desc->type to POWER_SUPPLY_TYPE_USB break the
userspace ABI?

Since commit 249fa8217b84 ("USB: Add driver to control USB fast charge for
iOS devices"), this driver has reported "Battery" via sysfs type and
POWER_SUPPLY_TYPE uevents.  Switching it to "USB" breaks existing
userspace power managers and udev rules matching Battery.

apple_mfi_fc_get_property() already returns POWER_SUPPLY_SCOPE_DEVICE for
POWER_SUPPLY_PROP_SCOPE, which is what power_supply_is_system_battery() and
userspace use to avoid treating an attached peripheral as a host battery.

POWER_SUPPLY_TYPE_USB also denotes a USB power source (typically exposing
online or usb_type), whereas this driver only exposes
POWER_SUPPLY_PROP_CHARGE_TYPE and POWER_SUPPLY_PROP_SCOPE, matching other
device-scoped peripheral charger drivers such as cros_peripheral_charger.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796524906.git.rosenp@gmail.com?part=4

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 09/12] usb: apple-mfi-fastcharge: Configure runtime PM autosuspend
  2026-12-06  2:44 ` [PATCH 09/12] usb: apple-mfi-fastcharge: Configure runtime PM autosuspend Rosen Penev
@ 2026-12-06  3:05   ` sashiko-bot
  0 siblings, 0 replies; 21+ messages in thread
From: sashiko-bot @ 2026-12-06  3:05 UTC (permalink / raw)
  To: Rosen Penev; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] usb: apple-mfi-fastcharge: NULL deref in mfi_fc_resume() before
  drvdata is set
- [High] usb: apple-mfi-fastcharge: usb_enable_autosuspend() breaks fast
  charging
- [Medium] usb: apple-mfi-fastcharge: missing usb_disable_autosuspend()
  on unbind
- [Medium] usb: apple-mfi-fastcharge: unpaired usb_enable_autosuspend()
  races resume
--

commit 52cde79222ebee5d1af8ec77b3372e905516d988
Author: Rosen Penev <rosenp@gmail.com>

usb: apple-mfi-fastcharge: Configure runtime PM autosuspend

This commit sets supports_autosuspend on mfi_fc_driver and calls
usb_enable_autosuspend() at the end of mfi_fc_probe() so the USB device
can runtime-autosuspend when idle.

> diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
> index fb4b975a62e94..83009b601da14 100644
> --- a/drivers/usb/misc/apple-mfi-fastcharge.c
> +++ b/drivers/usb/misc/apple-mfi-fastcharge.c
> @@ -212,6 +212,7 @@ static int mfi_fc_probe(struct usb_device *udev)
>  		return PTR_ERR(battery);
>  
>  	dev_set_drvdata(&udev->dev, mfi);
> +	usb_enable_autosuspend(udev);
>  
>  	return 0;
>  }

[Severity: High]
Does calling usb_enable_autosuspend() here break fast charging after the
default 2-second autosuspend delay?

After setting charge_type to Fast, apple_mfi_fc_set_property() immediately
calls pm_runtime_put_autosuspend().  With no runtime PM reference held and
no suspend callback to veto PMSG_AUTO_SUSPEND, an idle device enters USB
Suspend and loses its fast-charge state while charge_type stays Fast.

Unconditionally enabling autosuspend also overrides USB core's default
policy across all 0x05ac:0x1200..0x12ff devices (unlike other
generic_subclass drivers that only set supports_autosuspend) and adds a
40 ms usb_port_resume() stall on intermittent wakeups.

Dropping usb_enable_autosuspend() and preventing autosuspend while
POWER_SUPPLY_CHARGE_TYPE_FAST is active would avoid this regression.

[Severity: Medium]
Does mfi_fc_probe() leave autosuspend permanently enabled on udev after the
driver unbinds?

mfi_fc_driver defines no disconnect callback or devm action to call
usb_disable_autosuspend(udev).  Since pm_runtime_reinit() in
device_unbind_cleanup() returns early while pm_runtime_enabled() is true,
runtime_auto stays enabled after module unload or sysfs unbind.

> @@ -241,6 +242,7 @@ static struct usb_device_driver mfi_fc_driver = {
>  	.id_table =	mfi_fc_id_table,
>  	.match =	mfi_fc_match,
>  	.generic_subclass = 1,
> +	.supports_autosuspend = 1,
>  };

[Severity: High]
Can setting supports_autosuspend trigger a NULL pointer dereference in
mfi_fc_resume() before mfi_fc_probe() sets drvdata?

When binding to a runtime-suspended device (such as on rebind after unbind
leaves autosuspend enabled), really_probe() sets dev->driver before calling
usb_probe_device().  With supports_autosuspend and generic_subclass set,
usb_probe_device() no longer resumes udev before usb_generic_driver_probe().

Instead, usb_generic_driver_probe() -> usb_set_configuration() ->
usb_autoresume_device() -> usb_resume_device() invokes mfi_fc_resume()
before mfi_fc_probe() runs.  dev_get_drvdata(&udev->dev) is still NULL, so
guard(mutex)(&mfi->lock) dereferences NULL.

A concurrent charge_type write during probe can hit the same crash because
mfi_fc_probe() registers the power supply before calling dev_set_drvdata().

Checking for NULL in mfi_fc_resume() and setting drvdata before
devm_power_supply_register() would prevent both crashes.

[Severity: Medium]
Can an asynchronous runtime resume race with driver unbind and cause a
use-after-free of mfi in mfi_fc_resume()?

During unbind or rmmod while the device remains attached,
usb_generic_driver_disconnect() calls usb_autosuspend_device().  Without a
disconnect callback disabling autosuspend, udev can autosuspend before
device_unbind_cleanup() frees mfi and clears dev->driver:

CPU0 (unbind)                     CPU1 (pm_wq)
__device_release_driver()
  usb_unbind_device()
    usb_set_configuration(..., -1)
      usb_autosuspend_device()
        ... -> usb_port_suspend()
  device_unbind_cleanup()
    devres_release_all()
      mutex_destroy(&mfi->lock)
      kfree(mfi)
                                  usb_port_runtime_resume()
                                    pm_request_resume(&udev->dev)
                                  usb_runtime_resume()
                                    ... -> mfi_fc_resume()
                                      guard(mutex)(&mfi->lock)
                                        <-- use-after-free
    device_set_driver(dev, NULL)
    dev_set_drvdata(dev, NULL)

Because usb_runtime_resume() does not acquire device_lock(&udev->dev),
mfi_fc_resume() can touch freed mfi or sleep in usb_control_msg() across
devres_release_all() and module unload.

Calling usb_disable_autosuspend(udev) in a disconnect callback or devm
action before mfi is freed would synchronize runtime PM during unbind.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796524906.git.rosenp@gmail.com?part=9

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 07/12] usb: apple-mfi-fastcharge: Restore charge current on resume
  2026-12-06  2:44 ` [PATCH 07/12] usb: apple-mfi-fastcharge: Restore charge current on resume Rosen Penev
@ 2026-12-06  3:05   ` sashiko-bot
  0 siblings, 0 replies; 21+ messages in thread
From: sashiko-bot @ 2026-12-06  3:05 UTC (permalink / raw)
  To: Rosen Penev; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] usb: apple-mfi-fastcharge: NULL deref in mfi_fc_resume() on
  probe autoresume
- [Medium] usb: apple-mfi-fastcharge: mfi_fc_resume() races with unbind
  teardown
--

commit 86e6119152426dd019ad71e148c2e276d8b2358f
Author: Rosen Penev <rosenp@gmail.com>

usb: apple-mfi-fastcharge: Restore charge current on resume

This commit adds a resume callback to reprogram the fast charge current
after suspend/resume and refactors the USB control transfer into
apple_mfi_fc_send_current().

> diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
> index 49644922c5b51..85cf970e5f5eb 100644
> --- a/drivers/usb/misc/apple-mfi-fastcharge.c
> +++ b/drivers/usb/misc/apple-mfi-fastcharge.c

[ ... ]

> @@ -194,12 +203,31 @@ static int mfi_fc_probe(struct usb_device *udev)
>  	if (IS_ERR(battery))
>  		return PTR_ERR(battery);
>  
> +	dev_set_drvdata(&udev->dev, mfi);
> +
> +	return 0;
> +}
> +
> +static int mfi_fc_resume(struct usb_device *udev, pm_message_t message)
> +{
> +	struct mfi_device *mfi = dev_get_drvdata(&udev->dev);
> +
> +	/*
> +	 * A failure here must not fail the resume: the USB core would then
> +	 * skip resuming the device's interfaces. Fall back to trickle so a
> +	 * later write of "Fast" is sent to the device again.
> +	 */
> +	if (mfi->charge_type == POWER_SUPPLY_CHARGE_TYPE_FAST &&
> +	    apple_mfi_fc_send_current(mfi, FAST_CURRENT_MA) < 0)
> +		mfi->charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
> +
>  	return 0;
>  }

[Severity: High]
Can mfi be NULL here if the device is runtime-suspended when the driver
binds?

really_probe() sets udev->dev.driver before usb_probe_device() runs,
which calls usb_autoresume_device() (directly and via
usb_generic_driver_probe() -> usb_set_configuration()) before
mfi_fc_probe() sets drvdata.

When udev is runtime-suspended, usb_resume_device() invokes
mfi_fc_resume() with NULL drvdata, causing a NULL pointer dereference on
mfi->charge_type.  Returning 0 when !mfi avoids the crash.

[Severity: Medium]
Can mfi_fc_resume() race with driver unbind and dereference a freed mfi?

During unbind, __device_release_driver() drops its runtime PM reference
before device_remove() and device_unbind_cleanup(), and
usb_unbind_device() autosuspends udev without disabling runtime PM.

A concurrent port runtime resume can then queue
pm_request_resume(&udev->dev) without device_lock(&udev->dev), letting
usb_resume_device() call mfi_fc_resume() during teardown:

CPU0 (unbind)                     CPU1
__device_release_driver()
  pm_runtime_put_sync(dev)
  usb_unbind_device()
    usb_autosuspend_device(udev)
                                  usb_port_runtime_resume()
                                    pm_request_resume(&udev->dev)
                                  usb_resume_device()
                                    udriver = udev->dev.driver
                                    usb_generic_driver_resume()
  device_unbind_cleanup()
    devres_release_all(dev)
                                    mfi_fc_resume()
                                      mfi = dev_get_drvdata(...)
                                      mfi->charge_type  <-- use-after-free
    device_set_driver(dev, NULL)
    dev_set_drvdata(dev, NULL)

devres_release_all() frees mfi before dev->driver and dev->driver_data
are cleared, and pm_runtime_reinit() returns early while runtime PM is
enabled.  Depending on timing, mfi_fc_resume() hits a use-after-free, a
NULL pointer dereference, or unloaded module text during rmmod.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796524906.git.rosenp@gmail.com?part=7

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 12/12] usb: apple-mfi-fastcharge: Simplify match and property_is_writeable
  2026-12-06  2:44 ` [PATCH 12/12] usb: apple-mfi-fastcharge: Simplify match and property_is_writeable Rosen Penev
@ 2026-12-06  3:06   ` sashiko-bot
  0 siblings, 0 replies; 21+ messages in thread
From: sashiko-bot @ 2026-12-06  3:06 UTC (permalink / raw)
  To: Rosen Penev; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] usb: apple-mfi-fastcharge: NULL deref in mfi_fc_resume() during
  probe
- [Medium] usb: apple-mfi-fastcharge: UAF in mfi_fc_resume() during
  unbind
--

commit 69d314052e91b1c0b04c5d6cf67281bfa7bec29d
Author: Rosen Penev <rosenp@gmail.com>

usb: apple-mfi-fastcharge: Simplify match and property_is_writeable

This commit simplifies mfi_fc_match() to check the 0x12nn product ID range
with a u16 mask and reduces apple_mfi_fc_property_is_writeable() to a single
comparison, concluding the apple-mfi-fastcharge cleanup and runtime PM
series.

> diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
> index 02adec28d9e69..41eb0d56f9e01 100644
> --- a/drivers/usb/misc/apple-mfi-fastcharge.c
> +++ b/drivers/usb/misc/apple-mfi-fastcharge.c

[ ... ]

>  	mfi->udev = udev;
> +
> +	psy = devm_power_supply_register(&udev->dev, psy_desc, &psy_cfg);
> +	if (IS_ERR(psy))
> +		return PTR_ERR(psy);
> +
>  	dev_set_drvdata(&udev->dev, mfi);
> +	usb_enable_autosuspend(udev);
>  
>  	return 0;
> -
> -err_free_name:
> -	kfree(battery_name);
> -err_free_mfi:
> -	kfree(mfi);
> -	return err;
>  }
>  
> -static void mfi_fc_disconnect(struct usb_device *udev)
> +static int mfi_fc_resume(struct usb_device *udev, pm_message_t message)
>  {
> -	struct mfi_device *mfi;
> +	struct mfi_device *mfi = dev_get_drvdata(&udev->dev);
>  
> -	mfi = dev_get_drvdata(&udev->dev);
> -	if (mfi->battery)
> -		power_supply_unregister(mfi->battery);
> -	kfree(mfi->battery_desc.name);
> -	dev_set_drvdata(&udev->dev, NULL);
> -	kfree(mfi);
> +	guard(mutex)(&mfi->lock);

[Severity: High]
Can mfi_fc_resume() dereference a NULL mfi pointer during probe?

Earlier in this series, commits "usb: apple-mfi-fastcharge: Restore charge
current on resume", "usb: apple-mfi-fastcharge: Serialize charge_type access
with a mutex", and "usb: apple-mfi-fastcharge: Configure runtime PM
autosuspend" added mfi_fc_resume() with guard(mutex)(&mfi->lock) and enabled
autosuspend.

When binding to a runtime-suspended device (for instance on rebind after an
unbind leaves autosuspend enabled), really_probe() sets dev->driver before
usb_probe_device() runs usb_generic_driver_probe() ->
usb_set_configuration() -> usb_autoresume_device() -> usb_resume_device().
Because udev->dev.driver is already set while drvdata is still NULL,
usb_resume_device() calls mfi_fc_resume() before mfi_fc_probe(), causing a
NULL pointer dereference on &mfi->lock.

In addition, mfi_fc_probe() registers the power_supply device before calling
dev_set_drvdata(&udev->dev, mfi), so a concurrent write to charge_type can
also trigger pm_runtime_resume_and_get() -> mfi_fc_resume() while drvdata is
still NULL.

Returning 0 when mfi is NULL in mfi_fc_resume() and calling dev_set_drvdata()
before devm_power_supply_register() would close both windows.

[ ... ]

>  static struct usb_device_driver mfi_fc_driver = {
>  	.name =		"apple-mfi-fastcharge",
>  	.probe =	mfi_fc_probe,
> -	.disconnect =	mfi_fc_disconnect,
> +	.resume =	mfi_fc_resume,
>  	.id_table =	mfi_fc_id_table,
>  	.match =	mfi_fc_match,
>  	.generic_subclass = 1,
> +	.supports_autosuspend = 1,
>  };

[Severity: Medium]
Without a .disconnect callback, can mfi_fc_resume() race with devm teardown
during unbind and trigger a use-after-free on mfi?

Earlier in this series, commit "usb: apple-mfi-fastcharge: Convert to devm
resources" removed mfi_fc_disconnect() and moved mfi and mfi->lock to devm.
During unbind, usb_generic_driver_disconnect() calls
usb_set_configuration(udev, -1) -> usb_autosuspend_device(udev), allowing
udev and its port to runtime-suspend while dev->driver is still set.

Because usb_port_runtime_resume() and usb_runtime_resume() do not hold
device_lock(&udev->dev), an asynchronous runtime resume can run after
devres_release_all() frees mfi and destroys mfi->lock, but before
device_unbind_cleanup() clears dev->driver and drvdata:

CPU0                                CPU1
__device_release_driver()
  usb_unbind_device()
    usb_set_configuration(..., -1)
      usb_autosuspend_device()
                                    usb_port_runtime_resume()
                                      pm_request_resume(&udev->dev)
  device_unbind_cleanup()
    devres_release_all()
      mutex_destroy(&mfi->lock)
      kfree(mfi)
                                    usb_runtime_resume()
                                      usb_resume_device()
                                        mfi_fc_resume()
                                          guard(mutex)(&mfi->lock)
                                            <-- use-after-free
    device_set_driver(dev, NULL)
    dev_set_drvdata(dev, NULL)

mfi_fc_resume() then locks a destroyed mutex in freed memory (or hits a NULL
pointer dereference if drvdata was just cleared), risking memory corruption
or an Oops on unbind or module removal.  Adding a .disconnect callback that
disables autosuspend, synchronizes runtime PM, and clears drvdata before
devm teardown would prevent this race.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796524906.git.rosenp@gmail.com?part=12

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 08/12] usb: apple-mfi-fastcharge: Serialize charge_type access with a mutex
  2026-12-06  2:44 ` [PATCH 08/12] usb: apple-mfi-fastcharge: Serialize charge_type access with a mutex Rosen Penev
@ 2026-12-06  3:07   ` sashiko-bot
  0 siblings, 0 replies; 21+ messages in thread
From: sashiko-bot @ 2026-12-06  3:07 UTC (permalink / raw)
  To: Rosen Penev; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] usb: apple-mfi-fastcharge: NULL dereference in mfi_fc_resume()
  during probe
- [Medium] usb: apple-mfi-fastcharge: unbind races mfi_fc_resume() via
  devm cleanup
- [Low] usb: apple-mfi-fastcharge: missing Fixes tag for charge_type
  race fix
--

commit b8bcac8cd75839545033e3e164816c2f61377563
Author: Rosen Penev <rosenp@gmail.com>

> usb: apple-mfi-fastcharge: Serialize charge_type access with a mutex
>
> Concurrent writes to charge_type from userspace can race: both
> threads may read the old value, send their USB command, and write
> the software state, leaving the cached charge_type mismatched with
> the actual hardware state.
>
> Add a per-device mutex and hold it across the USB control transfer
> in set_charge_type, so the cached value always matches the last
> current sent to the device. Update the cache only after the transfer
> succeeds, which removes the need to roll it back on error. Take the
> same lock in get_property and in the resume path. Initialize it with
> devm_mutex_init() and use guard(mutex) for scoped locking.
>
> Signed-off-by: Rosen Penev <rosenp@gmail.com>

[Severity: Low]
This fixes a concurrency bug on mfi->charge_type that has been present
since the driver was added.  Should this include:

  Fixes: 249fa8217b84 ("USB: Add driver to control USB fast charge for iOS devices")

> diff --git a/drivers/usb/misc/apple-mfi-fastcharge.c b/drivers/usb/misc/apple-mfi-fastcharge.c
> index 85cf970e5f5eb..fb4b975a62e94 100644
> --- a/drivers/usb/misc/apple-mfi-fastcharge.c
> +++ b/drivers/usb/misc/apple-mfi-fastcharge.c

[ ... ]

> @@ -176,11 +179,16 @@ static int mfi_fc_probe(struct usb_device *udev)
>  	struct power_supply_desc *battery_desc;
>  	struct power_supply *battery;
>  	struct mfi_device *mfi;
> +	int ret;
>  
>  	mfi = devm_kzalloc_obj(&udev->dev, *mfi);
>  	if (!mfi)
>  		return -ENOMEM;
>  
> +	ret = devm_mutex_init(&udev->dev, &mfi->lock);
> +	if (ret)
> +		return ret;

[Severity: Medium]
Can driver unbind race with mfi_fc_resume() and trigger a use-after-free
on mfi->lock?

During unbind while the device remains attached (such as sysfs unbind or
rmmod), usb_unbind_device() drops the runtime PM usage count so udev can
runtime-suspend while udev->dev.driver is still set.  A concurrent
lockless runtime resume, such as writing a negative delay to
power/autosuspend, can then observe udev->dev.driver before
device_unbind_cleanup() clears it:

CPU0 (unbind)                         CPU1 (autosuspend_store)
__device_release_driver()
  usb_unbind_device()
    usb_autosuspend_device()
      ... -> usb_runtime_suspend()
                                      pm_runtime_set_autosuspend_delay()
                                        rpm_resume() -> usb_resume_device()
                                          reads udev->dev.driver
                                          usb_generic_driver_resume()
  device_unbind_cleanup()
    devres_release_all()
      mutex_destroy(&mfi->lock)
      kfree(mfi)
                                          mfi_fc_resume()
                                            guard(mutex)(&mfi->lock)
                                              <-- use-after-free
    device_set_driver(dev, NULL)
    dev_set_drvdata(dev, NULL)

Because devres_release_all() destroys mfi->lock and frees mfi before
dev->driver and drvdata are cleared and without a pm_runtime_barrier(),
mfi_fc_resume() can lock freed memory, dereference a NULL mfi, or have
mfi->lock destroyed while sleeping in usb_control_msg().

[ ... ]

> @@ -212,6 +220,8 @@ static int mfi_fc_resume(struct usb_device *udev, pm_message_t message)
>  {
>  	struct mfi_device *mfi = dev_get_drvdata(&udev->dev);
>  
> +	guard(mutex)(&mfi->lock);

[Severity: High]
Will guard(mutex)(&mfi->lock) dereference a NULL mfi when probing a
runtime-suspended device?

really_probe() binds udev->dev.driver via device_set_driver() before
calling usb_probe_device(), which calls usb_autoresume_device() (both
directly and via usb_generic_driver_probe() -> usb_set_configuration())
before mfi_fc_probe() runs.  If the device is runtime-suspended at probe
time, usb_resume_device() invokes mfi_fc_resume() while
dev_get_drvdata(&udev->dev) is still NULL, causing a NULL pointer
dereference in mutex_lock().

Returning 0 early when !mfi avoids the crash.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796524906.git.rosenp@gmail.com?part=8

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups
  2026-12-06  2:44 [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups Rosen Penev
                   ` (11 preceding siblings ...)
  2026-12-06  2:44 ` [PATCH 12/12] usb: apple-mfi-fastcharge: Simplify match and property_is_writeable Rosen Penev
@ 2026-12-06  3:07 ` sashiko-bot
  12 siblings, 0 replies; 21+ messages in thread
From: sashiko-bot @ 2026-12-06  3:07 UTC (permalink / raw)
  To: Rosen Penev; +Cc: linux-usb

Sashiko has finished reviewing this 12-patch series:

- Patches 2, 3, 5, 6, and 10: no issues found.
- Patch 1 ("usb: apple-mfi-fastcharge: Convert to devm resources"): 3
  potential issues (2 Medium, 1 Low)
- Patch 4 ("usb: apple-mfi-fastcharge: Use POWER_SUPPLY_TYPE_USB..."): 1
  potential issue (1 High)
- Patch 7 ("usb: apple-mfi-fastcharge: Restore charge current on..."): 2
  potential issues (1 High, 1 Medium)
- Patch 8 ("usb: apple-mfi-fastcharge: Serialize charge_type acc..."): 3
  potential issues (1 High, 1 Medium, 1 Low)
- Patch 9 ("usb: apple-mfi-fastcharge: Configure runtime PM auto..."): 4
  potential issues (2 High, 2 Medium)
- Patch 11 ("usb: apple-mfi-fastcharge: Rename battery variables..."): 1
  potential issue (1 High)
- Patch 12 ("usb: apple-mfi-fastcharge: Simplify match and proper..."):
  2 potential issues (1 High, 1 Medium)

Detailed reviews have been sent as separate replies to patches 1, 4,
7-9, 11, and 12.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1796524906.git.rosenp@gmail.com

^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2026-12-06  3:07 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06  2:44 [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups Rosen Penev
2026-12-06  2:44 ` [PATCH 01/12] usb: apple-mfi-fastcharge: Convert to devm resources Rosen Penev
2026-12-06  3:00   ` sashiko-bot
2026-12-06  2:44 ` [PATCH 02/12] usb: apple-mfi-fastcharge: Use pm_runtime_resume_and_get() Rosen Penev
2026-12-06  2:44 ` [PATCH 03/12] usb: apple-mfi-fastcharge: Fix usb_control_msg() return check and timeout Rosen Penev
2026-12-06  2:44 ` [PATCH 04/12] usb: apple-mfi-fastcharge: Use POWER_SUPPLY_TYPE_USB instead of BATTERY Rosen Penev
2026-12-06  3:04   ` sashiko-bot
2026-12-06  2:44 ` [PATCH 05/12] usb: apple-mfi-fastcharge: Constify power_supply_property array Rosen Penev
2026-12-06  2:44 ` [PATCH 06/12] usb: apple-mfi-fastcharge: Use topology-based device name Rosen Penev
2026-12-06  2:44 ` [PATCH 07/12] usb: apple-mfi-fastcharge: Restore charge current on resume Rosen Penev
2026-12-06  3:05   ` sashiko-bot
2026-12-06  2:44 ` [PATCH 08/12] usb: apple-mfi-fastcharge: Serialize charge_type access with a mutex Rosen Penev
2026-12-06  3:07   ` sashiko-bot
2026-12-06  2:44 ` [PATCH 09/12] usb: apple-mfi-fastcharge: Configure runtime PM autosuspend Rosen Penev
2026-12-06  3:05   ` sashiko-bot
2026-12-06  2:44 ` [PATCH 10/12] usb: apple-mfi-fastcharge: Name the vendor power request Rosen Penev
2026-12-06  2:44 ` [PATCH 11/12] usb: apple-mfi-fastcharge: Rename battery variables to psy Rosen Penev
2026-12-06  2:58   ` sashiko-bot
2026-12-06  2:44 ` [PATCH 12/12] usb: apple-mfi-fastcharge: Simplify match and property_is_writeable Rosen Penev
2026-12-06  3:06   ` sashiko-bot
2026-12-06  3:07 ` [PATCH 00/12] usb: apple-mfi-fastcharge: Fixes and cleanups sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox