mirror of https://lore.kernel.org/lkml/
 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
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ 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] 13+ 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  2:44 ` [PATCH 02/12] usb: apple-mfi-fastcharge: Use pm_runtime_resume_and_get() Rosen Penev
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ 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	[flat|nested] 13+ 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
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ 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	[flat|nested] 13+ 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
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ 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	[flat|nested] 13+ 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  2:44 ` [PATCH 05/12] usb: apple-mfi-fastcharge: Constify power_supply_property array Rosen Penev
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ 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	[flat|nested] 13+ 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
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ 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	[flat|nested] 13+ 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
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ 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	[flat|nested] 13+ 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  2:44 ` [PATCH 08/12] usb: apple-mfi-fastcharge: Serialize charge_type access with a mutex Rosen Penev
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ 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	[flat|nested] 13+ 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  2:44 ` [PATCH 09/12] usb: apple-mfi-fastcharge: Configure runtime PM autosuspend Rosen Penev
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ 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	[flat|nested] 13+ 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  2:44 ` [PATCH 10/12] usb: apple-mfi-fastcharge: Name the vendor power request Rosen Penev
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ 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	[flat|nested] 13+ 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
  2026-12-06  2:44 ` [PATCH 12/12] usb: apple-mfi-fastcharge: Simplify match and property_is_writeable Rosen Penev
  11 siblings, 0 replies; 13+ 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	[flat|nested] 13+ 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:44 ` [PATCH 12/12] usb: apple-mfi-fastcharge: Simplify match and property_is_writeable Rosen Penev
  11 siblings, 0 replies; 13+ 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	[flat|nested] 13+ 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
  11 siblings, 0 replies; 13+ 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	[flat|nested] 13+ messages in thread

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

Thread overview: 13+ 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  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  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  2:44 ` [PATCH 08/12] usb: apple-mfi-fastcharge: Serialize charge_type access with a mutex Rosen Penev
2026-12-06  2:44 ` [PATCH 09/12] usb: apple-mfi-fastcharge: Configure runtime PM autosuspend Rosen Penev
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:44 ` [PATCH 12/12] usb: apple-mfi-fastcharge: Simplify match and property_is_writeable Rosen Penev

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

all inboxes | Powered by JetHome®