[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20210522180611.314300-1-jose.exposito89@gmail.com>
Date: Sat, 22 May 2021 20:06:07 +0200
From: José Expósito <jose.exposito89@...il.com>
To: jikos@...nel.org
Cc: benjamin.tissoires@...hat.com, linux-input@...r.kernel.org,
linux-kernel@...r.kernel.org,
José Expósito <jose.exposito89@...il.com>
Subject: [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.
Signed-off-by: José Expósito <jose.exposito89@...il.com>
---
v2: Add depends on USB_HID to Kconfig
---
drivers/hid/hid-magicmouse.c | 90 ++++++++++++++++++++++++++++++++++++
1 file changed, 90 insertions(+)
diff --git a/drivers/hid/hid-magicmouse.c b/drivers/hid/hid-magicmouse.c
index 2bb473d8c424..0f766bce4537 100644
--- a/drivers/hid/hid-magicmouse.c
+++ b/drivers/hid/hid-magicmouse.c
@@ -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);
--
2.25.1
Powered by blists - more mailing lists