Skip to contentSkip to conversation

HID: magicmouse: register power supply

PATCH In review José Expósito 2 participants 10 messages May 22, 2021 – Jul 28, 2021 HID

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/5 HID: magicmouse: register power supply

Unlike the Apple Magic Mouse 1 and the Apple Magic Trackpad 1, the second generation of the devices don't report their battery status automatically.

This patchset adds support for reporting the battery capacity and charging status for the Apple Magic Mouse 2 and Apple Magic Trackpad 2 both over bluetooth and USB.

This patch:

Register the required power supply structs for the Apple Magic Mouse 2 and the Apple Magic Trackpad 2 to be able to report battery capacity and status in future patches.

---

v2: Add depends on USB_HID to Kconfig

---
 drivers/hid/hid-magicmouse.c | 90 ++++++++++++++++++++++++++++++++++++
 1 file changed, 90 insertions(+)
drivers/hid/hid-magicmouse.c +90 -0
index 2bb473d8c424..0f766bce4537 100644
@@ -112,6 +112,9 @@ MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state fie
  * @scroll_jiffies: Time of last scroll motion.
  * @touches: Most recent data for a touch, indexed by tracking ID.
  * @tracking_ids: Mapping of current touch input data to @touches.
+ * @battery: Required data to report the battery status of the Apple Magic
+ * Mouse 2 and Apple Magic Trackpad 2. Battery is reported automatically on the
+ * first generation of the devices.
  */
 struct magicmouse_sc {
 	struct input_dev *input;
@@ -132,8 +135,89 @@ struct magicmouse_sc {
 
 	struct hid_device *hdev;
 	struct delayed_work work;
+
+	struct {
+		struct power_supply *ps;
+		struct power_supply_desc ps_desc;
+	} battery;
+};
+
+static enum power_supply_property magicmouse_ps_props[] = {
+	POWER_SUPPLY_PROP_PRESENT,
+	POWER_SUPPLY_PROP_SCOPE,
 };
 
+static bool magicmouse_can_report_battery(struct magicmouse_sc *msc)
+{
+	return (msc->input->id.product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) ||
+	       (msc->input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2);
+}
+
+static int magicmouse_battery_get_property(struct power_supply *psy,
+					   enum power_supply_property psp,
+					   union power_supply_propval *val)
+{
+	struct magicmouse_sc *msc = power_supply_get_drvdata(psy);
+	int ret = 0;
+
+	if (!magicmouse_can_report_battery(msc))
+		return -EINVAL;
+
+	switch (psp) {
+	case POWER_SUPPLY_PROP_PRESENT:
+		val->intval = 1;
+		break;
+	case POWER_SUPPLY_PROP_SCOPE:
+		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
+		break;
+	default:
+		ret = -EINVAL;
+		break;
+	}
+
+	return ret;
+}
+
+static int magicmouse_battery_probe(struct hid_device *hdev)
+{
+	struct magicmouse_sc *msc = hid_get_drvdata(hdev);
+	struct power_supply *ps = NULL;
+	struct power_supply_config ps_cfg = { .drv_data = msc };
+	int ret;
+
+	if (!magicmouse_can_report_battery(msc))
+		return 0;
+
+	msc->battery.ps_desc.type = POWER_SUPPLY_TYPE_BATTERY;
+	msc->battery.ps_desc.properties = magicmouse_ps_props;
+	msc->battery.ps_desc.num_properties = ARRAY_SIZE(magicmouse_ps_props);
+	msc->battery.ps_desc.get_property = magicmouse_battery_get_property;
+	msc->battery.ps_desc.name = kasprintf(GFP_KERNEL, "magic_trackpad_2_%s",
+					      msc->input->uniq);
+	if (!msc->battery.ps_desc.name) {
+		hid_err(hdev, "unable to register ps_desc name, ENOMEM\n");
+		return -ENOMEM;
+	}
+
+	ps = devm_power_supply_register(&hdev->dev, &msc->battery.ps_desc,
+					&ps_cfg);
+	if (IS_ERR(ps)) {
+		ret = PTR_ERR(ps);
+		hid_err(hdev, "unable to register battery device: %d\n", ret);
+		return ret;
+	}
+
+	msc->battery.ps = ps;
+
+	ret = power_supply_powers(msc->battery.ps, &hdev->dev);
+	if (ret) {
+		hid_err(hdev, "unable to activate battery device: %d\n", ret);
+		return ret;
+	}
+
+	return 0;
+}
+
 static int magicmouse_firm_touch(struct magicmouse_sc *msc)
 {
 	int touch = -1;
@@ -726,6 +810,12 @@ static int magicmouse_probe(struct hid_device *hdev,
 		goto err_stop_hw;
 	}
 
+	ret = magicmouse_battery_probe(hdev);
+	if (ret) {
+		hid_err(hdev, "battery not registered\n");
+		goto err_stop_hw;
+	}
+
 	if (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE)
 		report = hid_register_report(hdev, HID_INPUT_REPORT,
 			MOUSE_REPORT_ID, 0);
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 2/5 HID: magicmouse: report battery capacity over bluetooth

Report the battery capacity percentage for the Apple Magic Mouse 2 and the Apple Magic Trackpad 2 when they are connected over bluetooth.

---
 drivers/hid/hid-magicmouse.c | 54 ++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
drivers/hid/hid-magicmouse.c +54 -0
index 0f766bce4537..d4a58dd6d2b8 100644
@@ -57,6 +57,8 @@ MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state fie
 #define MOUSE_REPORT_ID    0x29
 #define MOUSE2_REPORT_ID   0x12
 #define DOUBLE_REPORT_ID   0xf7
+#define BT_BATTERY_REPORT_ID 0x90
+
 /* These definitions are not precise, but they're close enough.  (Bits
  * 0x03 seem to indicate the aspect ratio of the touch, bits 0x70 seem
  * to be some kind of bit mask -- 0x20 may be a near-field reading,
@@ -139,12 +141,14 @@ struct magicmouse_sc {
 	struct {
 		struct power_supply *ps;
 		struct power_supply_desc ps_desc;
+		int capacity;
 	} battery;
 };
 
 static enum power_supply_property magicmouse_ps_props[] = {
 	POWER_SUPPLY_PROP_PRESENT,
 	POWER_SUPPLY_PROP_SCOPE,
+	POWER_SUPPLY_PROP_CAPACITY,
 };
 
 static bool magicmouse_can_report_battery(struct magicmouse_sc *msc)
@@ -153,6 +157,49 @@ static bool magicmouse_can_report_battery(struct magicmouse_sc *msc)
 	       (msc->input->id.product == USB_DEVICE_ID_APPLE_MAGICMOUSE2);
 }
 
+static bool magicmouse_can_report_battery_vendor(struct magicmouse_sc *msc,
+						 unsigned short vendor)
+{
+	return magicmouse_can_report_battery(msc) &&
+	       (msc->input->id.vendor == vendor);
+}
+
+static int magicmouse_battery_bt_get_capacity(struct magicmouse_sc *msc)
+{
+	struct hid_report_enum report_enum;
+	struct hid_report *report;
+	int ret;
+
+	if (!magicmouse_can_report_battery_vendor(msc, BT_VENDOR_ID_APPLE))
+		return -EINVAL;
+
+	report_enum = msc->hdev->report_enum[HID_INPUT_REPORT];
+	report = report_enum.report_id_hash[BT_BATTERY_REPORT_ID];
+
+	if (!report || report->maxfield < 1) {
+		hid_err(msc->hdev, "failed to retrieve report with ID %d\n",
+			BT_BATTERY_REPORT_ID);
+		return -EINVAL;
+	}
+
+	hid_hw_request(msc->hdev, report, HID_REQ_GET_REPORT);
+
+	if (!report || report->maxfield < 2) {
+		hid_err(msc->hdev, "invalid report->maxfield: %d\n",
+			report->maxfield);
+		return -EINVAL;
+	}
+
+	ret = report->field[0]->value[0];
+	if (ret < 0) {
+		hid_err(msc->hdev, "invalid report status %d\n", ret);
+		return ret;
+	}
+
+	msc->battery.capacity = report->field[1]->value[0];
+	return 0;
+}
+
 static int magicmouse_battery_get_property(struct power_supply *psy,
 					   enum power_supply_property psp,
 					   union power_supply_propval *val)
@@ -170,6 +217,12 @@ static int magicmouse_battery_get_property(struct power_supply *psy,
 	case POWER_SUPPLY_PROP_SCOPE:
 		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
 		break;
+	case POWER_SUPPLY_PROP_CAPACITY:
+		if (msc->input->id.vendor == BT_VENDOR_ID_APPLE)
+			magicmouse_battery_bt_get_capacity(msc);
+
+		val->intval = msc->battery.capacity;
+		break;
 	default:
 		ret = -EINVAL;
 		break;
@@ -188,6 +241,7 @@ static int magicmouse_battery_probe(struct hid_device *hdev)
 	if (!magicmouse_can_report_battery(msc))
 		return 0;
 
+	msc->battery.capacity = 100;
 	msc->battery.ps_desc.type = POWER_SUPPLY_TYPE_BATTERY;
 	msc->battery.ps_desc.properties = magicmouse_ps_props;
 	msc->battery.ps_desc.num_properties = ARRAY_SIZE(magicmouse_ps_props);
Signature · 1 line
2.25.1
#2 lore raw

PATCH v2 3/5 HID: magicmouse: Magic Trackpad 2 USB battery capacity

In reply to José Expósito

Report the battery capacity percentage for the Apple Magic Trackpad 2 when it is connected over USB.

---
 drivers/hid/Kconfig          |   2 +-
 drivers/hid/hid-magicmouse.c | 136 +++++++++++++++++++++++++++++++++++
 2 files changed, 137 insertions(+), 1 deletion(-)
drivers/hid/Kconfig +1 -1
index 4bf263c2d61a..f4856e5f5aa4 100644
@@ -640,7 +640,7 @@ config LOGIWHEELS_FF
 
 config HID_MAGICMOUSE
 	tristate "Apple Magic Mouse/Trackpad multi-touch support"
-	depends on HID
+	depends on USB_HID
 	help
 	Support for the Apple Magic Mouse/Trackpad multi-touch.
 
drivers/hid/hid-magicmouse.c +136 -0

173 lines not on this page · raw message

Signature · 1 line
2.25.1
#3 lore raw

PATCH v2 4/5 HID: magicmouse: Magic Mouse 2 USB battery capacity

In reply to José Expósito

Report the battery capacity percentage for the Apple Magic Mouse 2 when it is connected over USB.

---
 drivers/hid/hid-magicmouse.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
drivers/hid/hid-magicmouse.c +15 -3
index ea8a85767c39..53e8a10f0551 100644
@@ -911,8 +911,17 @@ static int magicmouse_enable_multitouch(struct hid_device *hdev)
 			feature = feature_mt_trackpad2_usb;
 		}
 	} else if (hdev->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
-		feature_size = sizeof(feature_mt_mouse2);
-		feature = feature_mt_mouse2;
+		if (hdev->vendor == BT_VENDOR_ID_APPLE) {
+			feature_size = sizeof(feature_mt_mouse2);
+			feature = feature_mt_mouse2;
+		} else { /* USB_VENDOR_ID_APPLE */
+			/*
+			 * The Magic Mouse 2 has the lightning connector on the
+			 * bottom, making impossible to use it when it is
+			 * charging.
+			 */
+			return 0;
+		}
 	} else {
 		feature_size = sizeof(feature_mt);
 		feature = feature_mt;
@@ -947,7 +956,8 @@ static int magicmouse_probe(struct hid_device *hdev,
 	int ret;
 
 	if (id->vendor == USB_VENDOR_ID_APPLE &&
-	    id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 &&
+	    (id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
+	     id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) &&
 	    hdev->type != HID_TYPE_USBMOUSE)
 		return 0;
 
@@ -1068,6 +1078,8 @@ static const struct hid_device_id magic_mice[] = {
 		USB_DEVICE_ID_APPLE_MAGICMOUSE), .driver_data = 0 },
 	{ HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
 		USB_DEVICE_ID_APPLE_MAGICMOUSE2), .driver_data = 0 },
+	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE,
+		USB_DEVICE_ID_APPLE_MAGICMOUSE2), .driver_data = 0 },
 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE,
 		USB_DEVICE_ID_APPLE_MAGICTRACKPAD), .driver_data = 0 },
 	{ HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE,
Signature · 1 line
2.25.1
#4 lore raw

PATCH v2 5/5 HID: magicmouse: report battery status

In reply to José Expósito

Report the battery charging status for the Apple Magic Mouse 2 and the Apple Magic Trackpad 2.

---
 drivers/hid/hid-magicmouse.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/hid/hid-magicmouse.c +10 -0
index 53e8a10f0551..4085b6698f2c 100644
@@ -155,6 +155,7 @@ static enum power_supply_property magicmouse_ps_props[] = {
 	POWER_SUPPLY_PROP_PRESENT,
 	POWER_SUPPLY_PROP_SCOPE,
 	POWER_SUPPLY_PROP_CAPACITY,
+	POWER_SUPPLY_PROP_STATUS,
 };
 
 static bool magicmouse_can_report_battery(struct magicmouse_sc *msc)
@@ -229,6 +230,15 @@ static int magicmouse_battery_get_property(struct power_supply *psy,
 
 		val->intval = msc->battery.capacity;
 		break;
+	case POWER_SUPPLY_PROP_STATUS:
+		if (msc->input->id.vendor == BT_VENDOR_ID_APPLE) {
+			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
+		} else { /* USB_VENDOR_ID_APPLE */
+			val->intval = (msc->battery.capacity == 100) ?
+				      POWER_SUPPLY_STATUS_FULL :
+				      POWER_SUPPLY_STATUS_CHARGING;
+		}
+		break;
 	default:
 		ret = -EINVAL;
 		break;
Signature · 1 line
2.25.1
maintainer
#5 lore raw
In reply to José Expósito
Quoted 14 lines · José Expósito

Unlike the Apple Magic Mouse 1 and the Apple Magic Trackpad 1, the second generation of the devices don't report their battery status automatically.

This patchset adds support for reporting the battery capacity and charging status for the Apple Magic Mouse 2 and Apple Magic Trackpad 2 both over bluetooth and USB.

This patch:

Register the required power supply structs for the Apple Magic Mouse 2 and the Apple Magic Trackpad 2 to be able to report battery capacity and status in future patches.

Quoted 5 lines

---

v2: Add depends on USB_HID to Kconfig

Hmm, why is this dependency needed in the first place, please? I think trying to keep the drivers independent on transport drivers (especially in cases like this, where more variants of physical transports actually really do exist) is worth trying.

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs
#6 lore raw

Hi Jiri,

First of all, thank you for taking the time to review my patches.

Quoted 9 lines · Jiri Kosina
Quoted 2 lines · José Expósito

[...]
v2: Add depends on USB_HID to Kconfig

Hmm, why is this dependency needed in the first place, please? I think trying to keep the drivers independent on transport drivers (especially in cases like this, where more variants of physical transports actually really do exist) is worth trying.

Sorry, that's something I should have explained in the changelog.

Intel's test bot reported compilation errors on the first version of the patch when USB support wasn't configured:
https://lore.kernel.org/patchwork/patch/1425313/

I was kindly pointed to a similar error and its fix, but, maybe in this case this is not the right fix?
Maybe there is a macro that I can use to wrap the USB related code in an #ifdef?

Thanks,
Jose

maintainer
#7 lore raw
Quoted 10 lines · José Expósito
Quoted 7 lines
Quoted 2 lines

[...]
v2: Add depends on USB_HID to Kconfig

Hmm, why is this dependency needed in the first place, please? I think trying to keep the drivers independent on transport drivers (especially in cases like this, where more variants of physical transports actually really do exist) is worth trying.

Sorry, that's something I should have explained in the changelog.

Quoted 7 lines

Intel's test bot reported compilation errors on the first version of the patch when USB support wasn't configured:
https://lore.kernel.org/patchwork/patch/1425313/

I was kindly pointed to a similar error and its fix, but, maybe in this case this is not the right fix?
Maybe there is a macro that I can use to wrap the USB related code in an #ifdef?

It can certainly be wrapped, but looking into the code now, it probably wouldn't really bring more clarity. I will apply the series with adding the USB_HID dependency for now.

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs
#8 lore raw
Quoted 16 lines · Jiri Kosina
Quoted 14 lines · José Expósito
Quoted 7 lines
Quoted 2 lines

[...]
v2: Add depends on USB_HID to Kconfig

Hmm, why is this dependency needed in the first place, please? I think trying to keep the drivers independent on transport drivers (especially in cases like this, where more variants of physical transports actually really do exist) is worth trying.

Sorry, that's something I should have explained in the changelog.

Intel's test bot reported compilation errors on the first version of the patch when USB support wasn't configured:
https://lore.kernel.org/patchwork/patch/1425313/

Quoted 7 lines
Quoted 3 lines

I was kindly pointed to a similar error and its fix, but, maybe in this case this is not the right fix?
Maybe there is a macro that I can use to wrap the USB related code in an #ifdef?

It can certainly be wrapped, but looking into the code now, it probably wouldn't really bring more clarity. I will apply the series with adding the USB_HID dependency for now.

Hi Jiri,

I've been investigating a bit about this issue and I think this might not be the righ solution for the problem.

John Chen's patch (9de07a4e8d4cb269f9876b2ffa282b5ffd09e05b):
https://lore.kernel.org/lkml/[email protected]/

Already adds battery reporting over bluetooth, so my patch is redundant... And worse than his, I should add.

I was investigating how to do something similar over USB, but I couldn't finish a patch yet.

So, if you don't mind, I'd prefer not to apply this patchset yet until I figure out a better solution on v3.

Thanks,
Jose

maintainer
#9 lore raw
Quoted 2 lines · José Expósito

So, if you don't mind, I'd prefer not to apply this patchset yet until I figure out a better solution on v3.

Thanks for the heads up. I am dropping it for now.

Signature · 2 lines
Jiri Kosina
SUSE Labs