linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "José Expósito" <jose.exposito89@gmail.com>
To: jikos@kernel.org
Cc: benjamin.tissoires@redhat.com, linux-input@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	"José Expósito" <jose.exposito89@gmail.com>
Subject: [PATCH 2/5] HID: magicmouse: report battery capacity over bluetooth
Date: Tue, 11 May 2021 20:20:20 +0200	[thread overview]
Message-ID: <20210511182023.730524-2-jose.exposito89@gmail.com> (raw)
In-Reply-To: <20210511182023.730524-1-jose.exposito89@gmail.com>

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

Signed-off-by: José Expósito <jose.exposito89@gmail.com>
---
 drivers/hid/hid-magicmouse.c | 54 ++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)

diff --git a/drivers/hid/hid-magicmouse.c b/drivers/hid/hid-magicmouse.c
index 0f766bce4537..d4a58dd6d2b8 100644
--- a/drivers/hid/hid-magicmouse.c
+++ b/drivers/hid/hid-magicmouse.c
@@ -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);
-- 
2.25.1


  reply	other threads:[~2021-05-11 18:20 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-11 18:20 [PATCH 1/5] HID: magicmouse: register power supply José Expósito
2021-05-11 18:20 ` José Expósito [this message]
2021-05-11 18:20 ` [PATCH 3/5] HID: magicmouse: Magic Trackpad 2 USB battery capacity José Expósito
2021-05-12  9:39   ` kernel test robot
2021-05-15 18:50     ` José Expósito
2021-05-20  9:18       ` [kbuild-all] " Rong Chen
2021-05-22 17:41         ` José Expósito
2021-05-12 11:28   ` kernel test robot
2021-05-11 18:20 ` [PATCH 4/5] HID: magicmouse: Magic Mouse " José Expósito
2021-05-11 18:20 ` [PATCH 5/5] HID: magicmouse: report battery status José Expósito

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210511182023.730524-2-jose.exposito89@gmail.com \
    --to=jose.exposito89@gmail.com \
    --cc=benjamin.tissoires@redhat.com \
    --cc=jikos@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).