public inbox for linux-input@vger.kernel.org
 help / color / mirror / Atom feed
From: "José Expósito" <jose.exposito89@gmail.com>
To: Aditya Garg <gargaditya08@live.com>
Cc: Jiri Kosina <jikos@kernel.org>,
	Benjamin Tissoires <bentiss@kernel.org>,
	linux-input@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/2] HID: magicmouse: avoid setting up battery timer when not needed
Date: Mon, 30 Jun 2025 11:32:54 +0200	[thread overview]
Message-ID: <aGJZxlV-Vyi0EDN7@fedora> (raw)
In-Reply-To: <PN3PR01MB95970C5D46483D0367C1D63CB87BA@PN3PR01MB9597.INDPRD01.PROD.OUTLOOK.COM>

Hi Aditya,

On Wed, Jun 25, 2025 at 07:46:04PM +0530, Aditya Garg wrote:
> Currently, the battery timer is set up for all devices using
> hid-magicmouse, irrespective of whether they actually need it or not.
> 
> The current implementation requires the battery timer for Magic Mouse 2
> and Magic Trackpad 2 when connected via USB only. Add checks to ensure
> that the battery timer is only set up when they are connected via USB.
> 
> Fixes: 0b91b4e4dae6 ("HID: magicmouse: Report battery level over USB")
> Cc: stable@vger.kernel.org
> Signed-off-by: Aditya Garg <gargaditya08@live.com>
> ---
>  drivers/hid/hid-magicmouse.c | 36 +++++++++++++++++++++++-------------
>  1 file changed, 23 insertions(+), 13 deletions(-)
> 
> diff --git a/drivers/hid/hid-magicmouse.c b/drivers/hid/hid-magicmouse.c
> index f49405d38..3e531905b 100644
> --- a/drivers/hid/hid-magicmouse.c
> +++ b/drivers/hid/hid-magicmouse.c
> @@ -863,18 +863,22 @@ static int magicmouse_probe(struct hid_device *hdev,
>  		return ret;
>  	}
>  
> -	timer_setup(&msc->battery_timer, magicmouse_battery_timer_tick, 0);
> -	mod_timer(&msc->battery_timer,
> -		  jiffies + msecs_to_jiffies(USB_BATTERY_TIMEOUT_MS));
> -	magicmouse_fetch_battery(hdev);
> -
> -	if (id->vendor == USB_VENDOR_ID_APPLE &&
> -	    (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
> -	     id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC ||
> -	     ((id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
> -	       id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) &&
> -	      hdev->type != HID_TYPE_USBMOUSE)))
> -		return 0;
> +	if (id->vendor == USB_VENDOR_ID_APPLE) {
> +		bool is_mouse2 = (id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
> +				  id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC);
> +		bool is_trackpad2 = (id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
> +				     id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC);
> +
> +		if (is_mouse2 || is_trackpad2) {
> +			timer_setup(&msc->battery_timer, magicmouse_battery_timer_tick, 0);
> +			mod_timer(&msc->battery_timer,
> +				  jiffies + msecs_to_jiffies(USB_BATTERY_TIMEOUT_MS));
> +			magicmouse_fetch_battery(hdev);
> +		}
> +
> +		if (is_mouse2 || (is_trackpad2 && hdev->type != HID_TYPE_USBMOUSE))
> +			return 0;
> +	}

Instead of duplicating these conditions here and in magicmouse_remove(),
you could move them into a helper function.

Also, watch out the `err_stop_hw:` error case, the timer could be used
there uninitialized.

Jose

>  	if (!msc->input) {
>  		hid_err(hdev, "magicmouse input not registered\n");
> @@ -947,7 +951,13 @@ static void magicmouse_remove(struct hid_device *hdev)
>  
>  	if (msc) {
>  		cancel_delayed_work_sync(&msc->work);
> -		timer_delete_sync(&msc->battery_timer);
> +		if (hdev->vendor == USB_VENDOR_ID_APPLE &&
> +		    (hdev->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
> +		     hdev->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC ||
> +		     hdev->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
> +		     hdev->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC))
> +
> +			timer_delete_sync(&msc->battery_timer);
>  	}
>  
>  	hid_hw_stop(hdev);
> -- 
> 2.43.0
> 

      reply	other threads:[~2025-06-30  9:32 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-25 14:16 [PATCH 0/2] HID: avoid setting up battery timer when not needed in hid-apple and magicmouse Aditya Garg
2025-06-25 14:16 ` [PATCH 1/2] HID: apple: avoid setting up battery timer for devices without battery Aditya Garg
2025-06-30  9:33   ` José Expósito
2025-06-30 10:10     ` Aditya Garg
2025-06-25 14:16 ` [PATCH 2/2] HID: magicmouse: avoid setting up battery timer when not needed Aditya Garg
2025-06-30  9:32   ` José Expósito [this message]

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=aGJZxlV-Vyi0EDN7@fedora \
    --to=jose.exposito89@gmail.com \
    --cc=bentiss@kernel.org \
    --cc=gargaditya08@live.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