From patchwork Sat May 22 18:06:07 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: 446064 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.7 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 012BDC2B9F2 for ; Sat, 22 May 2021 18:06:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C6DC46102A for ; Sat, 22 May 2021 18:06:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231146AbhEVSHm (ORCPT ); Sat, 22 May 2021 14:07:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230497AbhEVSHl (ORCPT ); Sat, 22 May 2021 14:07:41 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD647C061574; Sat, 22 May 2021 11:06:16 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id f6-20020a1c1f060000b0290175ca89f698so9114704wmf.5; Sat, 22 May 2021 11:06:16 -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=PdFjx680hDLKYhZTSS1Guwd8pcDuscxe0HRr3gILf8o=; b=A39KiAyYjelaEcQ79m/x3fciVw/rhCbwPQdbdMQ2FfuuVDJVLSs6LLhVYv/1SVsP57 UgUk4yrhf9Vpd2k0j0lgdyqJYvTdQFBow4xqv+wKc2wlkw1MgniZm5gSZv/Buekr79IA oV1oJZboQEk87pn1vEBEXdi4r917R4FqtznvzIVNODAk9Slp+YrL8gjJ6EZ5OdrHAARa Q1ZASIYneTQFE/gML4pqq01HCXsMPfzl37VpuLzTx+kjBbCM9tNP0BbFj9YF8x/3Wvrh wGaObo9zEcXXAgXZ35fvvL1amWzUP/zlWQsVvGoUAL53znJx+5YM+ONBufLzTMtZ5fBC SIvQ== 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=PdFjx680hDLKYhZTSS1Guwd8pcDuscxe0HRr3gILf8o=; b=lVGg6+KXwRIiEFi/LzgOtnd9K7wa1GsCim39xbpBFo1W7RX1WaPag2R2HNr0hXjJTa IaJ4B6PrGJ36O7HYrFg8ltBzh6HYuAm5K6fbok+PdTSIgBpsdcWVa+laJSSeglWxbd/+ EjCtdwohi6hfp1qN+obTqZLzz14qTyNzmM02zo6xIi2JSidP3B0f6Za+R2DVfjHqVAXd Q6LvTVDIRaOqYI5W1jb9ZVaMwtb7URXIS1ybP3oyFv7MKJZiSbmeMxRbtmssBaw536XE GsB/4gvYEnc21HIMurgz/4CR0W9qlHiNl8P4s862lKgQ+O3Mf2DPdWiPdRsYJk0gQYkJ IIOQ== X-Gm-Message-State: AOAM530rAYST+DLohK79jsT3tgTG/1pyzffCQ8Cy4zr68HWm0iwGU9FU C8fDUBlAyB/kyALiRpIgy0U= X-Google-Smtp-Source: ABdhPJyo+3t36ZZ6jzAGBJ97JSOBr88XANmvxxgDzk+n74H2zGGxXbJZysJSwhTApWGuaR91GSL/yQ== X-Received: by 2002:a05:600c:4f48:: with SMTP id m8mr13935940wmq.169.1621706775244; Sat, 22 May 2021 11:06:15 -0700 (PDT) Received: from localhost.localdomain ([94.73.38.147]) by smtp.gmail.com with ESMTPSA id v12sm5913217wrv.76.2021.05.22.11.06.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 May 2021 11:06:14 -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 v2 1/5] HID: magicmouse: register power supply Date: Sat, 22 May 2021 20:06:07 +0200 Message-Id: <20210522180611.314300-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 --- 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); From patchwork Sat May 22 18:06:08 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: 446317 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.7 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 DF244C2B9F2 for ; Sat, 22 May 2021 18:06:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BF2D761182 for ; Sat, 22 May 2021 18:06:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231340AbhEVSHp (ORCPT ); Sat, 22 May 2021 14:07:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231316AbhEVSHo (ORCPT ); Sat, 22 May 2021 14:07:44 -0400 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC978C061574; Sat, 22 May 2021 11:06:17 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id c14so22365377wrx.3; Sat, 22 May 2021 11:06:17 -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=vQRaJRfbPbdAGEJBzqPuJXJakMKu+r1s68xgDWZGh0RkrzLgRPgRusCXLhEITcdHyv L0DkmYWnGfBKjUoBO+2WmhQcUZXx42d3xlAFb68tTEuxwIS10XwFBWAZuHlPDKxeCj46 CwCmsdijo0MzofR6f3OT4I54pg5nJuQauDZ6v5k8UXjnjKp0MsdVep0Gc5gmTttfdJuw +Tsmg5fFuS4txhKBWR4+9DAYEKdjN2/LUEKtbbsqBkMLDRFk57uzAD4fN6x93gp5lPhl 8JeycI0nlP/HjWnih8m2PoPloWKb00qwmf8Ip/39cIT2ijpcc0nh+P+uNDGikIJhat9m jyaQ== 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=XlJpuHPfyPyfRD6BznKO0iGPA6pBALp+Yzc+RrYIIZM58nrgVbb48KvhK05cxJ3E1j oMs8gwTPbymMKYZkzR0903z9uRas2wV1Ukh90rOhWJU12m6t0FbhHtbbYHnHF4AnJXgF LcjtKB5q/VxLQv0+OFglKE0X24YiOsNTtQ81RUVe3O6FeMyIWDkHNdFttv1VbRwH59zp ohB3Kkyx6jUYswdbk8HFTV4XtLPy9Y60f2O9obCTtVBK97Yk0As0g9iZPaeekK5XkWOS /HVHzHQHzso8CxZmCFWPUTr1WBSXwadLnAu7ZGuoQ3+TzO1Ygp7sqO3m/kAawTPgnBX8 DvLA== X-Gm-Message-State: AOAM530sbnlykzk6B8NVSRu2tzWM0NE91LKAePdKVtJGWHPWlEQ4MKZm /BsP4DtKflycfPDomelHDd0gnIvWay7EEA== X-Google-Smtp-Source: ABdhPJwyo8xPhgskFs4G7N0xAGxaYufSswon1nj8sXwnGyUL57kpvr8ITB+QmUIiQhQAusw+ib/fkg== X-Received: by 2002:adf:f7cf:: with SMTP id a15mr14601539wrq.184.1621706776382; Sat, 22 May 2021 11:06:16 -0700 (PDT) Received: from localhost.localdomain ([94.73.38.147]) by smtp.gmail.com with ESMTPSA id v12sm5913217wrv.76.2021.05.22.11.06.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 May 2021 11:06:15 -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 v2 2/5] HID: magicmouse: report battery capacity over bluetooth Date: Sat, 22 May 2021 20:06:08 +0200 Message-Id: <20210522180611.314300-2-jose.exposito89@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210522180611.314300-1-jose.exposito89@gmail.com> References: <20210522180611.314300-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 Sat May 22 18:06:09 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: 446316 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.7 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 8155DC2B9F2 for ; Sat, 22 May 2021 18:06:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 620916102A for ; Sat, 22 May 2021 18:06:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231370AbhEVSHt (ORCPT ); Sat, 22 May 2021 14:07:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40760 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231342AbhEVSHp (ORCPT ); Sat, 22 May 2021 14:07:45 -0400 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C37A6C06174A; Sat, 22 May 2021 11:06:18 -0700 (PDT) Received: by mail-wm1-x331.google.com with SMTP id u5-20020a7bc0450000b02901480e40338bso8192904wmc.1; Sat, 22 May 2021 11:06:18 -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=tvQkJA45dcnDhgUgMx0vMPfrn39cI+zkH5R9jIgqU68=; b=PsQ+cdJzZiglWQZ9C5mjX1BI5wFjif4GcbAGBC9hpjefTRbC6E8eYWsHIxWXX9JdYJ qoBLltW3bDowuM+Uc9iKcYgsZNeWq8SZiLo58Jp6ZCy4pLi1WBCdhTqtaKGpPNDAclZt sURjbCRUqQC5EZ2dhpItSMU2vBJc/f71HPe0EMB8qURat42D2jBbQcM70IhFpZAE8ymt bFsB6XlaUwx0FvaB3JX2tSJE666UWlUEhkmYXQCfj4RVi3SJVHZNJ7dgVzWFEnqEgdSu 5v8XXrJkvTvu2EZVih64nOGzX82YbmUgFu7rZR/pkjY/dHaUbDnUIP2vwOzIcRuk4SRy mImg== 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=tvQkJA45dcnDhgUgMx0vMPfrn39cI+zkH5R9jIgqU68=; b=g9qzAyVHfUnFG4VstmF0loXOtVm1rkvVKYSe9s1IdXRV4fY0MYGpJlp+kbS/z1radu me3xWskr+sO7Ef3el95mIo1KTWCKYtBVXox9BZyVwF7adPRseNuweSpTOuad8tFQL+h6 jXPxTfzXKI+04XheBf2e5Mgva9VN/PHR8pkP9ROceSAFlkADHRi4W9LpSveHoD2DBZOs q3nh2HC7DEshP6VCa5A8PZWDCes6vqGNNVsz3JoZSWAct55/ob+4euR1kbvDZkhkPZ97 nF7KxvaguhDSii11XOXquAal4x619mZpzfYzO92+ueoJAA6EQmdvn1wF8Mj08aFXHAZ9 Q0jQ== X-Gm-Message-State: AOAM533fB6aKaaiUYhtqmVsvgTY5BqhM+gMf2IMR7mo1jVfroIgdKTxl b1U1/fxsHKXuZYXSi8kdvN8= X-Google-Smtp-Source: ABdhPJyg9eEetn5/JE63jwsuleitlk0zssXyJ1gSTKVV37ZYnLabg3vncKl6jxji7BBlVaB+6R94GQ== X-Received: by 2002:a05:600c:2909:: with SMTP id i9mr13674853wmd.116.1621706777371; Sat, 22 May 2021 11:06:17 -0700 (PDT) Received: from localhost.localdomain ([94.73.38.147]) by smtp.gmail.com with ESMTPSA id v12sm5913217wrv.76.2021.05.22.11.06.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 May 2021 11:06:17 -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 v2 3/5] HID: magicmouse: Magic Trackpad 2 USB battery capacity Date: Sat, 22 May 2021 20:06:09 +0200 Message-Id: <20210522180611.314300-3-jose.exposito89@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210522180611.314300-1-jose.exposito89@gmail.com> References: <20210522180611.314300-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 --- drivers/hid/Kconfig | 2 +- drivers/hid/hid-magicmouse.c | 136 +++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 1 deletion(-) diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig index 4bf263c2d61a..f4856e5f5aa4 100644 --- a/drivers/hid/Kconfig +++ b/drivers/hid/Kconfig @@ -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. 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 Sat May 22 18:06:10 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: 446063 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.7 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 224A7C2B9FB for ; Sat, 22 May 2021 18:06:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F3C0D6115C for ; Sat, 22 May 2021 18:06:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230497AbhEVSHq (ORCPT ); Sat, 22 May 2021 14:07:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231332AbhEVSHo (ORCPT ); Sat, 22 May 2021 14:07:44 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87BEEC0613ED; Sat, 22 May 2021 11:06:19 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id z17so24126780wrq.7; Sat, 22 May 2021 11:06:19 -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=OB9iwCSxWplJKOrP02WjDrcvsvBmgA1sLphFCFfydPlAYGS3GDczOrzWEF/DjyR50g WRtYfxG+940RZJ4CkWVfRy6Ubj1FeuGZZyes1E4S4A9lvL0zDHomGpsF+pQ9ckw31ZMt JZNs2FRepQKgOS6Sasc1sgLnbNiwjLtD2e+QWEFtlrHOABSZRd1Eicx1fn+22haw0Mvh UhmJa//a6jD9a8Jmw9naSMDHoejGZF8nV4ZdFF0U+DCz1djtAZLpQah6p4SW+DPVYHow NJJEGFZejMatdvaNfnYiVC+OkOUjSS9+d7V3TONAKbiY8kkPMBHP4W1ah9fUB2Op/V6g hAiA== 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=CliqfDeTcbsRdzxOcj2VrKCQuBIQA84sIYDkkQRDzRkS2/xeAfzJd7U/dzu/C5OynU 72zQSTSlydazDj1DZGng4Ii1pF2bCTtzWHkqE7b5ZO+fpBgrU295KRmDvmdXQGY0CQ7l CxF3BHHWXREWdiuOApIHJSePT1lFAXKOfR/0qD1NFkULrYDMYRgCbonTurYw1I/4Vm5t iGmTNiH9kY3ak6TrUveyqLw769hUCxcXPlsyPU2k072PKAOHHLWkb26lTJAT9kmQRYIZ XghiUwqqZWFNko8yg/OZAB0rxA31/jOzjmzG3+8BM2sNTXqvhpj/ZDH4Su8ub/gkXFBK XhOA== X-Gm-Message-State: AOAM532j1yx3VRY4+l24cfvM9MO7ep65gop2kQfNf2gHzhUHdP4YVbCe B0magFexFhKXEc87oo5VlcQ= X-Google-Smtp-Source: ABdhPJzBD0uew326CWxWknYulddmvtJRc3WE77Gm+LSm5nO+lCaqToZIK1a03hIdq2pGCZ1+zlNOMQ== X-Received: by 2002:a05:6000:1561:: with SMTP id 1mr15244540wrz.284.1621706778180; Sat, 22 May 2021 11:06:18 -0700 (PDT) Received: from localhost.localdomain ([94.73.38.147]) by smtp.gmail.com with ESMTPSA id v12sm5913217wrv.76.2021.05.22.11.06.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 May 2021 11:06:17 -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 v2 4/5] HID: magicmouse: Magic Mouse 2 USB battery capacity Date: Sat, 22 May 2021 20:06:10 +0200 Message-Id: <20210522180611.314300-4-jose.exposito89@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210522180611.314300-1-jose.exposito89@gmail.com> References: <20210522180611.314300-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 Sat May 22 18:06:11 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: 446062 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.7 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 337B4C2B9FB for ; Sat, 22 May 2021 18:06:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0FF0861139 for ; Sat, 22 May 2021 18:06:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231348AbhEVSHu (ORCPT ); Sat, 22 May 2021 14:07:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231345AbhEVSHq (ORCPT ); Sat, 22 May 2021 14:07:46 -0400 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96FD0C061574; Sat, 22 May 2021 11:06:20 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id c14so22365447wrx.3; Sat, 22 May 2021 11:06:20 -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=BLxfvyEUa9gSXWp4fZAIG0gTEEmd51FDW5MbBQoN0XwZtp0cRoAr8ixIdvPP2HrYJ8 Ly+N+JCOiHN3QytZbLTE9+eopYDiAK7QwpPGAymDJDvBuVl2OgPfCLsp8sCvmkYuneH6 1rWRcKvDi6KkrUjJb1uUeMgxw+0ZCevDGO17hQSiDd8DSTRfQBUuTRjM9FIAchpAdqID 3mJ2w4mMboEH9pgrmfEMwu9yvvwplYwdSQDP+nEF0EtFGgH/BfHdERkXWOERD974vDzB hiUyX6Pin1VYi1W+PGgUC3FUNNnbJfiQ0jxUB7rbU6nca0U5AkSOQjt9YpGVlmz3ia/5 lKcQ== 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=aV98OYfxCmguFtSpslddpuE5sO9gJxnCq4rQJ3sRSVJlrrRLWYBZlx7rggcSGWPCak ZWU7bgFn28O8ynMw/HUKf7Bpn3Hc2YfidRVXyLSVP002K+qaBQdpqCAqFpa9Vcz2Ioxu ANqJAEWl3EMqQlUktFcEgN+xXwULJRXZ7IFphNyD8tDAevBXZHlF6deSKLMUF8kOp1Al 0EoBRdhPEU4vLUjjbFve+5vVHy7VLmKxfKSp3o6FeIu5TIgApl9VhXHcs7N/EbHqK50Q 8H0+cZCXMEYbvhuYr+9Hq0QK9ORG1wj2t8Hzc3KLzY6Pyf8OobzUyXQlx4USeebZCCXZ qSUA== X-Gm-Message-State: AOAM532uOjPe9b85ur+ezfc7hcg8l9FSA1qoV6CVDFeczfvgNMG12rKB Aft2FAE2A4qYZygxWOB0zvA= X-Google-Smtp-Source: ABdhPJxPbGRlXNSjFslrW+hBeVFVFncyVs//DCdQBT2rh/+Y58Eowdni0mcGMgkmiPt3pshM9wPlnA== X-Received: by 2002:a5d:5409:: with SMTP id g9mr14358025wrv.321.1621706779212; Sat, 22 May 2021 11:06:19 -0700 (PDT) Received: from localhost.localdomain ([94.73.38.147]) by smtp.gmail.com with ESMTPSA id v12sm5913217wrv.76.2021.05.22.11.06.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 22 May 2021 11:06:18 -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 v2 5/5] HID: magicmouse: report battery status Date: Sat, 22 May 2021 20:06:11 +0200 Message-Id: <20210522180611.314300-5-jose.exposito89@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210522180611.314300-1-jose.exposito89@gmail.com> References: <20210522180611.314300-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;