[PATCH v2 0/4] HID: avoid setting up battery timer when not needed in hid-apple and magicmouse

Aditya Garg posted 4 patches 3 months, 1 week ago
drivers/hid/hid-apple.c      | 21 +++++++-----
drivers/hid/hid-magicmouse.c | 66 ++++++++++++++++++++++--------------
2 files changed, 54 insertions(+), 33 deletions(-)
[PATCH v2 0/4] HID: avoid setting up battery timer when not needed in hid-apple and magicmouse
Posted by Aditya Garg 3 months, 1 week ago
Both hid-apple and hid-magicmouse require set up a battery timer for
certain devices in order to fetch battery status. However, the timer
is being set unconditionally for all devices. This patch series
introduces checks to ensure that the battery timer is only set up for
devices that actually require it.

v2: - Address the cases of out_err and err_stop_hw left in v1
    - Create a function to check if the device is a USB Magic Mouse 2 or Magic Trackpad 2
      to reduce code duplication.
    - Add 2 new patches that convert the battery timeout to use
      secs_to_jiffies() instead of msecs_to_jiffies().

Aditya Garg (4):
  HID: apple: avoid setting up battery timer for devices without battery
  HID: magicmouse: avoid setting up battery timer when not needed
  HID: apple: use secs_to_jiffies() for battery timeout
  HID: magicmouse: use secs_to_jiffies() for battery timeout

 drivers/hid/hid-apple.c      | 21 +++++++-----
 drivers/hid/hid-magicmouse.c | 66 ++++++++++++++++++++++--------------
 2 files changed, 54 insertions(+), 33 deletions(-)

Range-diff against v1:
1:  05b6ac964 ! 1:  41c49e1d6 HID: apple: avoid setting up battery timer for devices without battery
    @@ drivers/hid/hid-apple.c: static int apple_probe(struct hid_device *hdev,
      
      	if (quirks & APPLE_BACKLIGHT_CTL)
      		apple_backlight_init(hdev);
    +@@ drivers/hid/hid-apple.c: static int apple_probe(struct hid_device *hdev,
    + 	return 0;
    + 
    + out_err:
    +-	timer_delete_sync(&asc->battery_timer);
    ++	if (quirks & APPLE_RDESC_BATTERY)
    ++		timer_delete_sync(&asc->battery_timer);
    ++
    + 	hid_hw_stop(hdev);
    + 	return ret;
    + }
     @@ drivers/hid/hid-apple.c: static void apple_remove(struct hid_device *hdev)
      {
      	struct apple_sc *asc = hid_get_drvdata(hdev);
2:  25b52facf ! 2:  a1617042f HID: magicmouse: avoid setting up battery timer when not needed
    @@ Commit message
         Signed-off-by: Aditya Garg <gargaditya08@live.com>
     
      ## drivers/hid/hid-magicmouse.c ##
    +@@ drivers/hid/hid-magicmouse.c: static void magicmouse_enable_mt_work(struct work_struct *work)
    + 		hid_err(msc->hdev, "unable to request touch data (%d)\n", ret);
    + }
    + 
    ++static bool is_usb_magicmouse2(__u32 vendor, __u32 product)
    ++{
    ++	if (vendor != USB_VENDOR_ID_APPLE)
    ++		return false;
    ++	return product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
    ++	       product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC;
    ++}
    ++
    ++static bool is_usb_magictrackpad2(__u32 vendor, __u32 product)
    ++{
    ++	if (vendor != USB_VENDOR_ID_APPLE)
    ++		return false;
    ++	return product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
    ++	       product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC;
    ++}
    ++
    + static int magicmouse_fetch_battery(struct hid_device *hdev)
    + {
    + #ifdef CONFIG_HID_BATTERY_STRENGTH
    + 	struct hid_report_enum *report_enum;
    + 	struct hid_report *report;
    + 
    +-	if (!hdev->battery || 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))
    ++	if (!hdev->battery ||
    ++	    (!is_usb_magicmouse2(hdev->vendor, hdev->product) &&
    ++	     !is_usb_magictrackpad2(hdev->vendor, hdev->product)))
    + 		return -1;
    + 
    + 	report_enum = &hdev->report_enum[hdev->battery_report_type];
     @@ drivers/hid/hid-magicmouse.c: static int magicmouse_probe(struct hid_device *hdev,
      		return ret;
      	}
    @@ drivers/hid/hid-magicmouse.c: static int magicmouse_probe(struct hid_device *hde
     -	     ((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;
    ++	if (is_usb_magicmouse2(id->vendor, id->product) ||
    ++	    is_usb_magictrackpad2(id->vendor, id->product)) {
    ++		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_usb_magicmouse2(id->vendor, id->product) ||
    ++	    (is_usb_magictrackpad2(id->vendor, id->product) &&
    ++	     hdev->type != HID_TYPE_USBMOUSE))
    + 		return 0;
      
      	if (!msc->input) {
    - 		hid_err(hdev, "magicmouse input not registered\n");
    +@@ drivers/hid/hid-magicmouse.c: static int magicmouse_probe(struct hid_device *hdev,
    + 
    + 	return 0;
    + err_stop_hw:
    +-	timer_delete_sync(&msc->battery_timer);
    ++	if (is_usb_magicmouse2(id->vendor, id->product) ||
    ++	    is_usb_magictrackpad2(id->vendor, id->product))
    ++		timer_delete_sync(&msc->battery_timer);
    ++
    + 	hid_hw_stop(hdev);
    + 	return ret;
    + }
     @@ drivers/hid/hid-magicmouse.c: 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))
    -+
    ++		if (is_usb_magicmouse2(hdev->vendor, hdev->product) ||
    ++		    is_usb_magictrackpad2(hdev->vendor, hdev->product))
     +			timer_delete_sync(&msc->battery_timer);
      	}
      
      	hid_hw_stop(hdev);
    +@@ drivers/hid/hid-magicmouse.c: static const __u8 *magicmouse_report_fixup(struct hid_device *hdev, __u8 *rdesc,
    + 	 *   0x05, 0x01,       // Usage Page (Generic Desktop)        0
    + 	 *   0x09, 0x02,       // Usage (Mouse)                       2
    + 	 */
    +-	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) &&
    ++	if ((is_usb_magicmouse2(hdev->vendor, hdev->product) ||
    ++	     is_usb_magictrackpad2(hdev->vendor, hdev->product)) &&
    + 	    *rsize == 83 && rdesc[46] == 0x84 && rdesc[58] == 0x85) {
    + 		hid_info(hdev,
    + 			 "fixing up magicmouse battery report descriptor\n");
-:  --------- > 3:  3ebe25998 HID: apple: use secs_to_jiffies() for battery timeout
-:  --------- > 4:  1d3400714 HID: magicmouse: use secs_to_jiffies() for battery timeout
-- 
2.49.0
Re: [PATCH v2 0/4] HID: avoid setting up battery timer when not needed in hid-apple and magicmouse
Posted by Jiri Kosina 3 months, 1 week ago
On Mon, 30 Jun 2025, Aditya Garg wrote:

> Both hid-apple and hid-magicmouse require set up a battery timer for
> certain devices in order to fetch battery status. However, the timer
> is being set unconditionally for all devices. This patch series
> introduces checks to ensure that the battery timer is only set up for
> devices that actually require it.
> 
> v2: - Address the cases of out_err and err_stop_hw left in v1
>     - Create a function to check if the device is a USB Magic Mouse 2 or Magic Trackpad 2
>       to reduce code duplication.
>     - Add 2 new patches that convert the battery timeout to use
>       secs_to_jiffies() instead of msecs_to_jiffies().
> 
> Aditya Garg (4):
>   HID: apple: avoid setting up battery timer for devices without battery
>   HID: magicmouse: avoid setting up battery timer when not needed
>   HID: apple: use secs_to_jiffies() for battery timeout
>   HID: magicmouse: use secs_to_jiffies() for battery timeout
> 
>  drivers/hid/hid-apple.c      | 21 +++++++-----
>  drivers/hid/hid-magicmouse.c | 66 ++++++++++++++++++++++--------------
>  2 files changed, 54 insertions(+), 33 deletions(-)

Now queued in hid.git#for-6.17/battery-timer-fixes.

Thanks,

-- 
Jiri Kosina
SUSE Labs