From patchwork Tue May 11 18:20:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?Sm9zw6kgRXhww7NzaXRv?= X-Patchwork-Id: 435946 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A62CFC433B4 for ; Tue, 11 May 2021 18:20:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7C13C6190A for ; Tue, 11 May 2021 18:20:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231454AbhEKSVv (ORCPT ); Tue, 11 May 2021 14:21:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231329AbhEKSVv (ORCPT ); Tue, 11 May 2021 14:21:51 -0400 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D443C061574; Tue, 11 May 2021 11:20:44 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id s5-20020a7bc0c50000b0290147d0c21c51so1744969wmh.4; Tue, 11 May 2021 11:20:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ftGwwdNlFP05uVc9KwVMdLS0pcb01gK3FHHMMiTD974=; b=Ld7Ty0gR7mB0MC6BwFBIXZ1oMjm0Vx3JhPA88ybxJEZ8SnAyqUdtpDYwU52MFzpSu+ HYIOMHM7OLkCChoZETc+D1EYGqRE5YKKxBq+JGeqDivGOXrm/wXDXrFhUgF3RMbCUeqA 9a/281b7bndzRt+2+dyQz7GQ/AMJl4OCoB39Qba3UmfMXUE/XtspKhDls1kp265NnBPi oKU7nmAcTfx+7r9TxGxFMHhhgZjG7P2Pxumz0QCfiel2D/C1V6EjsWocDtplB2CCBBAM PEt6q9RyEsTw78SHQ9av3ATRjWAVxulV+YvOR0p7sf8bmcfpvPesfe4qPh6q+Dvrfkby 92Ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ftGwwdNlFP05uVc9KwVMdLS0pcb01gK3FHHMMiTD974=; b=ZpRfc1Q8SwBEQjJEVsMpfzhGSLMYfAuwTe8KXrsq6PA4k/UinPKpumBQaGrriAs8pJ bSyCBEykzkFMitnGCUIJOa56T61bEsjp33JwpQK+SVtM3mWwwzHvEtUWpgf2hdwbwNy9 RSLFXkMiLLqVPo+Js2CE9PidGD4NwBLQ+9ezUT6HO/HkqiU2mRZ9VX17tjU5Ley29hdQ QPjxUZP1SfQHTHFyZJOoLj7pbo//OJsrqUWA9/gWN+hNJ886/KTzpyHRf73UY0XxzGRf Qgj+bCEu3NCvN/zufKvxjYyGlArbmNngEFtf31L9OewfcpxQ1dAuSjJVnfw+c43TXNd3 dM3Q== X-Gm-Message-State: AOAM532j14ILRJpnCA4Wh2PiDoKcCdykGzWdCAuXVyNANDFIW+HhKoqv AvLpkr3zV/jgiNlMbVD8hiJmJVE2/Gs5UG5p X-Google-Smtp-Source: ABdhPJzipI5ebtz47j0YHilq4cAeh11Vl1vmdK2VpW48eBO3BK6g0jUi56HbdgF94xivljSxcw/g3A== X-Received: by 2002:a7b:ce19:: with SMTP id m25mr33961850wmc.137.1620757243076; Tue, 11 May 2021 11:20:43 -0700 (PDT) Received: from localhost.localdomain ([94.73.38.147]) by smtp.gmail.com with ESMTPSA id d15sm28012133wrw.71.2021.05.11.11.20.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 May 2021 11:20:42 -0700 (PDT) From: =?utf-8?b?Sm9zw6kgRXhww7NzaXRv?= To: jikos@kernel.org Cc: benjamin.tissoires@redhat.com, linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?b?Sm9zw6kgRXhww7NzaXRv?= Subject: [PATCH 1/5] HID: magicmouse: register power supply Date: Tue, 11 May 2021 20:20:19 +0200 Message-Id: <20210511182023.730524-1-jose.exposito89@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org 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 --- 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); From patchwork Tue May 11 18:20:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?Sm9zw6kgRXhww7NzaXRv?= X-Patchwork-Id: 435945 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B56F7C43460 for ; Tue, 11 May 2021 18:20:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7DE276191A for ; Tue, 11 May 2021 18:20:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232052AbhEKSVy (ORCPT ); Tue, 11 May 2021 14:21:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45274 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232031AbhEKSVx (ORCPT ); Tue, 11 May 2021 14:21:53 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 508FBC061574; Tue, 11 May 2021 11:20:45 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id o6-20020a05600c4fc6b029015ec06d5269so1758007wmq.0; Tue, 11 May 2021 11:20:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rxhjwjEE7ZOo0E56gyS1tUalNN5E638sE9uVTQlGAvw=; b=RIU+PKozwvWFleDMQpKHa1Q78FIGrLTB7y1MNXyWoFlHR7iKXW75pwBHqu5eN34HzE hcwkiMQtv+FZ1qtWPmMkwEBkYjQSQXjnB99/FY8/Wu4kQ88Fu4ErD2TgcnKiAp+8791z kscAFZagle3TkMaXTRLVOk6QnEB3Wn2GdSM2kr/jALxqzDP1AvyRhyH6wGzublU5r93B yYN68J2mV9UidxYlG5GilecKm3x+N/Vosp5t60t3DTyje9oW8aL5OmC20uFkqNsR3kfJ TffJUI/Fqf7u1qnNiCCHPXdo4aJlNpOxWv4ZjzcumC5z6bytxFkwWZQrQIZAac5fQ5s6 drkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rxhjwjEE7ZOo0E56gyS1tUalNN5E638sE9uVTQlGAvw=; b=eqANTbcbKHX8BZg29V1FKX//5F4TzOewbMkwIZ1wqTTK/Z+PlyujH2/mgWNR5cJDSu pEONCBJGTeUKCAUYDPgHBy3Bn+RnFRELT7mtb/zVgH3XNE/kZIDP8n5T1WbaGZHG+pUd kZTjMt4YpHjXcX5v1c2UcQ8XMy8l4dsx9P3uFxyD/vsGZN8WlX+ESxKC1EmGGAwscsnL 3vNmFKjcsbgmi7WFCd9BZa4+8TA4DS0M2FSXYBN34FQcuwG1dzRsjRrI0jGaEgTIy2I5 r5e9bV1SrGWkmuMjDHglY/YFWwRPJ0KnORqRUhAoZcpID7kYqtBR/Tqi4gAzAU6WH3Tg 7Z4Q== X-Gm-Message-State: AOAM533nTOUahVUvIFbvMpHCio2u3tHKlBlpplRNq8YuTx+jgm6MR0VP re+0GF1wLMe4U8noh4AxuH++qJHrKrLsVJST X-Google-Smtp-Source: ABdhPJxlHz3W4OP1o1atRrXr/qej7ER1C97nuVypUrzSzijhHT5P/Wl7ROivEScHNYsbp5pDlNaaoQ== X-Received: by 2002:a1c:193:: with SMTP id 141mr34268922wmb.99.1620757244081; Tue, 11 May 2021 11:20:44 -0700 (PDT) Received: from localhost.localdomain ([94.73.38.147]) by smtp.gmail.com with ESMTPSA id d15sm28012133wrw.71.2021.05.11.11.20.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 May 2021 11:20:43 -0700 (PDT) From: =?utf-8?b?Sm9zw6kgRXhww7NzaXRv?= To: jikos@kernel.org Cc: benjamin.tissoires@redhat.com, linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?b?Sm9zw6kgRXhww7NzaXRv?= Subject: [PATCH 2/5] HID: magicmouse: report battery capacity over bluetooth Date: Tue, 11 May 2021 20:20:20 +0200 Message-Id: <20210511182023.730524-2-jose.exposito89@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210511182023.730524-1-jose.exposito89@gmail.com> References: <20210511182023.730524-1-jose.exposito89@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org 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 --- 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); From patchwork Tue May 11 18:20:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?Sm9zw6kgRXhww7NzaXRv?= X-Patchwork-Id: 434519 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6C6BAC433B4 for ; Tue, 11 May 2021 18:20:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 388AE6190A for ; Tue, 11 May 2021 18:20:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232048AbhEKSVx (ORCPT ); Tue, 11 May 2021 14:21:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231329AbhEKSVx (ORCPT ); Tue, 11 May 2021 14:21:53 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 50F51C06174A; Tue, 11 May 2021 11:20:46 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id e7so2550806wrc.11; Tue, 11 May 2021 11:20:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=c3iaH3y/FZoaNpVFta2w69W8hOQ3/2OA7g9Y/AFoXo8=; b=lFKWrtI/Qq/pS2K02NGYIJDsqyB1Op2vp8CB+1iBEc+Bc5tL6Cxu4g96ztcSR3SSRN nsXCPrXI7ovYD8j6T+RZsxuPbtA7WJOd0ve+YiC3S5IFwPum903+ayyVL+TJzY7Qzf8B tWaam2y2aMX2Wv/EcK4t7u+aYC4vmYNn6nJLqNGCtkL5utiJ174BmPQKyj3vIuO46RRI p1P50dIG59nfPlrdi38qCsTkxtNHc/swv2Jk9jBiGkX1j6FfMT/7QDKFnYISLHP06U+m O0vsM4ejC6w/3zfwuZIHaHlbn8RTTvs2cBjzidfbTTw3YslaQR9osoKxE2eOgWvb1Tpy 1EsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=c3iaH3y/FZoaNpVFta2w69W8hOQ3/2OA7g9Y/AFoXo8=; b=kXzwUFnY1UR417zZHObY2c3bNYzu/r9jaObDFVbfiAVdsTEUN5sWHhmDykFK3UaKTg +3MhZBYVHm5vJOUGOuGOlf8ZTnsyjinu9hU76Yn3Rsj2vo0EIYAeHsj2qW1u3KgBjAHS XO8WQyjOoDPwvJ8+CdKgr2G/vwtAz4VtFYZnGrOZf3QaqrUgpDEk5tsAkq9zXdTCKt8l fotCbGp1LAcObBD592CAUl7p/CfUq1dYLYsYJoHxRF0/tSHL8hpxNhWpg4ya9yzAenc3 QnDxNaqXW2i7ZZo8UmBDxsvVI9Bj+F/klGj+dgyX7QEPN8mWZUI0l/5L27lpuzm2r5xr BP7A== X-Gm-Message-State: AOAM532ZlfJpuCLKSvqKVosdiNvLqnah59LA6YjjDlkD4BYBsk9zkPj6 Q0XRpeIwQGvp+OdVyi3qgGk= X-Google-Smtp-Source: ABdhPJwPFDK4Msv4z0+ZQwxLFA87V0cw+uujSfYn5M5PwJ0U1/1g9/ifAO9FbpUETNek+9OlCL6zGw== X-Received: by 2002:a5d:40c9:: with SMTP id b9mr39344824wrq.59.1620757245088; Tue, 11 May 2021 11:20:45 -0700 (PDT) Received: from localhost.localdomain ([94.73.38.147]) by smtp.gmail.com with ESMTPSA id d15sm28012133wrw.71.2021.05.11.11.20.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 May 2021 11:20:44 -0700 (PDT) From: =?utf-8?b?Sm9zw6kgRXhww7NzaXRv?= To: jikos@kernel.org Cc: benjamin.tissoires@redhat.com, linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?b?Sm9zw6kgRXhww7NzaXRv?= Subject: [PATCH 3/5] HID: magicmouse: Magic Trackpad 2 USB battery capacity Date: Tue, 11 May 2021 20:20:21 +0200 Message-Id: <20210511182023.730524-3-jose.exposito89@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210511182023.730524-1-jose.exposito89@gmail.com> References: <20210511182023.730524-1-jose.exposito89@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org Report the battery capacity percentage for the Apple Magic Trackpad 2 when it is connected over USB. Signed-off-by: José Expósito Reported-by: kernel test robot Reported-by: kernel test robot --- drivers/hid/hid-magicmouse.c | 136 +++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) diff --git a/drivers/hid/hid-magicmouse.c b/drivers/hid/hid-magicmouse.c index d4a58dd6d2b8..ea8a85767c39 100644 --- a/drivers/hid/hid-magicmouse.c +++ b/drivers/hid/hid-magicmouse.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include "hid-ids.h" @@ -58,6 +59,7 @@ MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state fie #define MOUSE2_REPORT_ID 0x12 #define DOUBLE_REPORT_ID 0xf7 #define BT_BATTERY_REPORT_ID 0x90 +#define USB_BATTERY_EP_ADDR 0x81 /* These definitions are not precise, but they're close enough. (Bits * 0x03 seem to indicate the aspect ratio of the touch, bits 0x70 seem @@ -142,6 +144,10 @@ struct magicmouse_sc { struct power_supply *ps; struct power_supply_desc ps_desc; int capacity; + struct urb *urb; + u8 *urb_buf; + int urb_buf_size; + dma_addr_t urb_buf_dma; } battery; }; @@ -231,6 +237,112 @@ static int magicmouse_battery_get_property(struct power_supply *psy, return ret; } +static void magicmouse_battery_usb_urb_complete(struct urb *urb) +{ + struct magicmouse_sc *msc = urb->context; + int ret; + + switch (urb->status) { + case 0: + msc->battery.capacity = msc->battery.urb_buf[2]; + break; + case -EOVERFLOW: + hid_err(msc->hdev, "URB overflow\n"); + fallthrough; + case -ECONNRESET: + case -ENOENT: + case -ESHUTDOWN: + return; + default: + break; + } + + ret = usb_submit_urb(msc->battery.urb, GFP_ATOMIC); + if (ret) + hid_err(msc->hdev, "unable to submit URB, %d\n", ret); +} + +static int magicmouse_battery_usb_probe(struct magicmouse_sc *msc) +{ + struct usb_interface *iface = to_usb_interface(msc->hdev->dev.parent); + struct usb_device *usbdev = interface_to_usbdev(iface); + struct usb_host_endpoint *endpoint = NULL; + u8 ep_address; + unsigned int pipe = 0; + int i, ret; + + if (!magicmouse_can_report_battery_vendor(msc, USB_VENDOR_ID_APPLE)) + return -EINVAL; + + for (i = 0; i < sizeof(usbdev->ep_in); i++) { + endpoint = usbdev->ep_in[i]; + if (endpoint) { + ep_address = endpoint->desc.bEndpointAddress; + if (ep_address == USB_BATTERY_EP_ADDR) + break; + } + } + + if (!endpoint) { + hid_err(msc->hdev, "endpoint with address %d not found\n", + USB_BATTERY_EP_ADDR); + ret = -EIO; + goto exit; + } + + msc->battery.urb = usb_alloc_urb(0, GFP_ATOMIC); + if (!msc->battery.urb) { + hid_err(msc->hdev, "unable to alloc URB, ENOMEM\n"); + ret = -ENOMEM; + goto exit; + } + + pipe = usb_rcvintpipe(usbdev, endpoint->desc.bEndpointAddress); + if (pipe == 0) { + hid_err(msc->hdev, "unable to create USB rcvintpipe\n"); + ret = -EIO; + goto err_free_urb; + } + + msc->battery.urb_buf_size = endpoint->desc.wMaxPacketSize; + msc->battery.urb_buf_dma = msc->battery.urb->transfer_dma; + msc->battery.urb_buf = usb_alloc_coherent(usbdev, + msc->battery.urb_buf_size, GFP_ATOMIC, + &msc->battery.urb_buf_dma); + if (!msc->battery.urb_buf) { + hid_err(msc->hdev, "unable to alloc URB buffer, ENOMEM\n"); + ret = -ENOMEM; + goto err_free_urb; + } + + usb_fill_int_urb(msc->battery.urb, usbdev, pipe, msc->battery.urb_buf, + msc->battery.urb_buf_size, + magicmouse_battery_usb_urb_complete, msc, + endpoint->desc.bInterval); + + ret = usb_submit_urb(msc->battery.urb, GFP_ATOMIC); + if (ret) { + hid_err(msc->hdev, "unable to submit URB, %d\n", ret); + goto err_free_urb_buf; + } + + return 0; + +err_free_urb_buf: + usb_free_coherent(usbdev, msc->battery.urb_buf_size, + msc->battery.urb_buf, msc->battery.urb_buf_dma); + +err_free_urb: + usb_free_urb(msc->battery.urb); + +exit: + msc->battery.urb = NULL; + msc->battery.urb_buf = NULL; + msc->battery.urb_buf_size = 0; + + return ret; +} + static int magicmouse_battery_probe(struct hid_device *hdev) { struct magicmouse_sc *msc = hid_get_drvdata(hdev); @@ -269,6 +381,12 @@ static int magicmouse_battery_probe(struct hid_device *hdev) return ret; } + if (msc->input->id.vendor == USB_VENDOR_ID_APPLE) { + ret = magicmouse_battery_usb_probe(msc); + if (ret) + return ret; + } + return 0; } @@ -923,7 +1041,25 @@ static int magicmouse_probe(struct hid_device *hdev, static void magicmouse_remove(struct hid_device *hdev) { struct magicmouse_sc *msc = hid_get_drvdata(hdev); + struct usb_interface *iface; + struct usb_device *usbdev; cancel_delayed_work_sync(&msc->work); + + if (msc && + magicmouse_can_report_battery_vendor(msc, USB_VENDOR_ID_APPLE) && + msc->battery.urb && msc->battery.urb_buf) { + iface = to_usb_interface(hdev->dev.parent); + usbdev = interface_to_usbdev(iface); + + usb_kill_urb(msc->battery.urb); + + usb_free_coherent(usbdev, msc->battery.urb_buf_size, + msc->battery.urb_buf, + msc->battery.urb_buf_dma); + + usb_free_urb(msc->battery.urb); + } + hid_hw_stop(hdev); } From patchwork Tue May 11 18:20:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?Sm9zw6kgRXhww7NzaXRv?= X-Patchwork-Id: 434518 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 37D5AC433ED for ; Tue, 11 May 2021 18:20:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 01813617ED for ; Tue, 11 May 2021 18:20:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232065AbhEKSVy (ORCPT ); Tue, 11 May 2021 14:21:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232053AbhEKSVy (ORCPT ); Tue, 11 May 2021 14:21:54 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4FF27C061574; Tue, 11 May 2021 11:20:47 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id a10-20020a05600c068ab029014dcda1971aso92814wmn.3; Tue, 11 May 2021 11:20:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=gmaW3DHknv/IqorFvXkou48CAqTVgYoEed8/ElnIKvQ=; b=kBKU6eTdDt6NG/MYmpYulXFHqIqyOlUO4nayE4hvFFLtceQ2Dm6w72XYzOe79NFq/o FjGtl9a1NY1MKxlZKckPt+V3+y6OUFcY/uLKq3xuhP5nO+CT7Hmc1oS2MBR8AfWVpqF8 5XTqlf0DEN2Wa0T/VAkkqo+/eR0SIzr2WgNTtlekdnmlN6s50cqCBF6LBS/moDTcjL+U oSOdDUHOjnK3eUtPDKU2f6cUdVys0Yk2KOIXMv+VZplNd7iD4XVg7KL2z+/qeavQfde/ mcViMDagvkcicIEhEap8RofDajiptb12pv53oAiVdJkI1C6ulh+0KpGBvMK9n295+SGi q0DQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=gmaW3DHknv/IqorFvXkou48CAqTVgYoEed8/ElnIKvQ=; b=HlMcOdU37Tq+6GgWyNskHlk9pPJj1pGKA06SFSImLYnOK50fu2aQogM8xA2zkko1pZ x9MCJG3LkSRoi6szuW6cNGWm7wwLFPfFS2zNRmevvIWkLPgH/ehrt37TQLaiCs4OMeHC DxknPZ+z5L9UjhRmg9G815j4R7kMN+xhf8MTEwS+JjYqHxpaBYU4n3XLX8opXKzNY5+e +NQfLYgtJi6O/2eWEbI4w+8teBUXWGMM1mkM9O47yri285IhbIx8zzEIZTY0aqAga9/8 tjJ73PEtT+2vIpb4chOOM3PXSmIDvNIeGIkmmEN/vjhWEUc03LHim/ZUQFGpNkAytLXY gC9g== X-Gm-Message-State: AOAM53112h2pSwKkJiOwop+qx5KRg18O6xica+S3n05l4pSfbLGzV1wA LtLRYujD78OIPTvzub2mSRg= X-Google-Smtp-Source: ABdhPJxe9X8zjmSjBAuV2MC6xu4NXcTX7FN9uu8/IVV6Nnu7+fYm34yvw6k1BQHcHmENuJCXSDjUiw== X-Received: by 2002:a05:600c:4982:: with SMTP id h2mr33751845wmp.108.1620757246118; Tue, 11 May 2021 11:20:46 -0700 (PDT) Received: from localhost.localdomain ([94.73.38.147]) by smtp.gmail.com with ESMTPSA id d15sm28012133wrw.71.2021.05.11.11.20.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 May 2021 11:20:45 -0700 (PDT) From: =?utf-8?b?Sm9zw6kgRXhww7NzaXRv?= To: jikos@kernel.org Cc: benjamin.tissoires@redhat.com, linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?b?Sm9zw6kgRXhww7NzaXRv?= Subject: [PATCH 4/5] HID: magicmouse: Magic Mouse 2 USB battery capacity Date: Tue, 11 May 2021 20:20:22 +0200 Message-Id: <20210511182023.730524-4-jose.exposito89@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210511182023.730524-1-jose.exposito89@gmail.com> References: <20210511182023.730524-1-jose.exposito89@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org Report the battery capacity percentage for the Apple Magic Mouse 2 when it is connected over USB. Signed-off-by: José Expósito --- drivers/hid/hid-magicmouse.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/drivers/hid/hid-magicmouse.c b/drivers/hid/hid-magicmouse.c index ea8a85767c39..53e8a10f0551 100644 --- a/drivers/hid/hid-magicmouse.c +++ b/drivers/hid/hid-magicmouse.c @@ -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, From patchwork Tue May 11 18:20:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?Sm9zw6kgRXhww7NzaXRv?= X-Patchwork-Id: 435944 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E6891C433ED for ; Tue, 11 May 2021 18:20:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CC81C617ED for ; Tue, 11 May 2021 18:20:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232084AbhEKSV6 (ORCPT ); Tue, 11 May 2021 14:21:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232019AbhEKSVz (ORCPT ); Tue, 11 May 2021 14:21:55 -0400 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E1AAC061574; Tue, 11 May 2021 11:20:48 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id o6-20020a05600c4fc6b029015ec06d5269so1758099wmq.0; Tue, 11 May 2021 11:20:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=DXAhBfbLQ+Z6qa68rOJY6vHkUQpdl/o/mBrv046HmPE=; b=aN9uGxsVCwSGaaMATi9By/8HDdoqPOl0q3lr3cIzdtbfu/G/eNywekhLkna127VCEt tZE3NE6TAu9KsM/jlbwp3k1l/Tme4wHoarmv/dBahLYoiZ4Z3UHRSmlNrfJq1Be+F7mC Crr01OCpUEiz2VLgF3B7HVTViQXg3r0iBnHVsBsvQKMB6n3W4pGIX7NE0STtN7flNZO0 OL4jN4QzJ3SYcRtQcy15+SLA61tzqOf4IziNuXew0YgLKH82KFCEAu9WytWtAqgrnQ7q CBeL309KqrkyZh16Vomg8ESDCKwEHHxpLujOcfZxjnV8ukFV/EkHkorJ07kzxGlVkwol 8E7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=DXAhBfbLQ+Z6qa68rOJY6vHkUQpdl/o/mBrv046HmPE=; b=d1rndeaMIlnhuyPBa0yczMhYr89DczuAEArd2zBsbYttsu4ibFwqfy95UHFY8cpfHD GPrFFxsnanYShQuHPznoHAaA6zSA+UJNeT4t/zeRqMMiUlPZ/i4TSH+i3RPUnpg+tVGy Gb5oYD3/4eS3C9ex3OdMkp4+icDs1ko12eDjd+UP+8pW2e+bz5atKtA6uHyKEurMLiAc 56Gb1f/QeWVT3H6puBipGt1/OoH4K6lNrR+1TOneiDNK1tQEHoxPjh/doAidT0XXx6Qw JYVX45ERB9nFs+v52+Pq/OAhWr3yoyA9sN8tag2Mz8Lhr3gxRgc0GnHZtK0ve8ebC2Bl xQoQ== X-Gm-Message-State: AOAM532i1cAGNzLAR3/fuvX0BYeEof4lLiFEDUO4JqXpLsbygO4RwX9Q z09UprtmmGspR7pigaH2WOE= X-Google-Smtp-Source: ABdhPJxV18DCIwXmjWUt0yrboA4C4tGwbySkgdDTrM7UyarjQvbbUVLI4Dd1XWDU8CIePuUZeXs8pA== X-Received: by 2002:a05:600c:4f04:: with SMTP id l4mr33852177wmq.18.1620757247138; Tue, 11 May 2021 11:20:47 -0700 (PDT) Received: from localhost.localdomain ([94.73.38.147]) by smtp.gmail.com with ESMTPSA id d15sm28012133wrw.71.2021.05.11.11.20.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 May 2021 11:20:46 -0700 (PDT) From: =?utf-8?b?Sm9zw6kgRXhww7NzaXRv?= To: jikos@kernel.org Cc: benjamin.tissoires@redhat.com, linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?b?Sm9zw6kgRXhww7NzaXRv?= Subject: [PATCH 5/5] HID: magicmouse: report battery status Date: Tue, 11 May 2021 20:20:23 +0200 Message-Id: <20210511182023.730524-5-jose.exposito89@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210511182023.730524-1-jose.exposito89@gmail.com> References: <20210511182023.730524-1-jose.exposito89@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org Report the battery charging status for the Apple Magic Mouse 2 and the Apple Magic Trackpad 2. Signed-off-by: José Expósito --- drivers/hid/hid-magicmouse.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/drivers/hid/hid-magicmouse.c b/drivers/hid/hid-magicmouse.c index 53e8a10f0551..4085b6698f2c 100644 --- a/drivers/hid/hid-magicmouse.c +++ b/drivers/hid/hid-magicmouse.c @@ -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;