Skip to content

Commit 9bdc30e

Browse files
AdityaGarg8Jiri Kosina
authored andcommitted
HID: magicmouse: avoid setting up battery timer when not needed
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: 0b91b4e ("HID: magicmouse: Report battery level over USB") Cc: [email protected] Signed-off-by: Aditya Garg <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent c061046 commit 9bdc30e

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

drivers/hid/hid-magicmouse.c

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -791,17 +791,31 @@ static void magicmouse_enable_mt_work(struct work_struct *work)
791791
hid_err(msc->hdev, "unable to request touch data (%d)\n", ret);
792792
}
793793

794+
static bool is_usb_magicmouse2(__u32 vendor, __u32 product)
795+
{
796+
if (vendor != USB_VENDOR_ID_APPLE)
797+
return false;
798+
return product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
799+
product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC;
800+
}
801+
802+
static bool is_usb_magictrackpad2(__u32 vendor, __u32 product)
803+
{
804+
if (vendor != USB_VENDOR_ID_APPLE)
805+
return false;
806+
return product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
807+
product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC;
808+
}
809+
794810
static int magicmouse_fetch_battery(struct hid_device *hdev)
795811
{
796812
#ifdef CONFIG_HID_BATTERY_STRENGTH
797813
struct hid_report_enum *report_enum;
798814
struct hid_report *report;
799815

800-
if (!hdev->battery || hdev->vendor != USB_VENDOR_ID_APPLE ||
801-
(hdev->product != USB_DEVICE_ID_APPLE_MAGICMOUSE2 &&
802-
hdev->product != USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC &&
803-
hdev->product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 &&
804-
hdev->product != USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC))
816+
if (!hdev->battery ||
817+
(!is_usb_magicmouse2(hdev->vendor, hdev->product) &&
818+
!is_usb_magictrackpad2(hdev->vendor, hdev->product)))
805819
return -1;
806820

807821
report_enum = &hdev->report_enum[hdev->battery_report_type];
@@ -863,17 +877,17 @@ static int magicmouse_probe(struct hid_device *hdev,
863877
return ret;
864878
}
865879

866-
timer_setup(&msc->battery_timer, magicmouse_battery_timer_tick, 0);
867-
mod_timer(&msc->battery_timer,
868-
jiffies + msecs_to_jiffies(USB_BATTERY_TIMEOUT_MS));
869-
magicmouse_fetch_battery(hdev);
870-
871-
if (id->vendor == USB_VENDOR_ID_APPLE &&
872-
(id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
873-
id->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC ||
874-
((id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
875-
id->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) &&
876-
hdev->type != HID_TYPE_USBMOUSE)))
880+
if (is_usb_magicmouse2(id->vendor, id->product) ||
881+
is_usb_magictrackpad2(id->vendor, id->product)) {
882+
timer_setup(&msc->battery_timer, magicmouse_battery_timer_tick, 0);
883+
mod_timer(&msc->battery_timer,
884+
jiffies + msecs_to_jiffies(USB_BATTERY_TIMEOUT_MS));
885+
magicmouse_fetch_battery(hdev);
886+
}
887+
888+
if (is_usb_magicmouse2(id->vendor, id->product) ||
889+
(is_usb_magictrackpad2(id->vendor, id->product) &&
890+
hdev->type != HID_TYPE_USBMOUSE))
877891
return 0;
878892

879893
if (!msc->input) {
@@ -936,7 +950,10 @@ static int magicmouse_probe(struct hid_device *hdev,
936950

937951
return 0;
938952
err_stop_hw:
939-
timer_delete_sync(&msc->battery_timer);
953+
if (is_usb_magicmouse2(id->vendor, id->product) ||
954+
is_usb_magictrackpad2(id->vendor, id->product))
955+
timer_delete_sync(&msc->battery_timer);
956+
940957
hid_hw_stop(hdev);
941958
return ret;
942959
}
@@ -947,7 +964,9 @@ static void magicmouse_remove(struct hid_device *hdev)
947964

948965
if (msc) {
949966
cancel_delayed_work_sync(&msc->work);
950-
timer_delete_sync(&msc->battery_timer);
967+
if (is_usb_magicmouse2(hdev->vendor, hdev->product) ||
968+
is_usb_magictrackpad2(hdev->vendor, hdev->product))
969+
timer_delete_sync(&msc->battery_timer);
951970
}
952971

953972
hid_hw_stop(hdev);
@@ -964,11 +983,8 @@ static const __u8 *magicmouse_report_fixup(struct hid_device *hdev, __u8 *rdesc,
964983
* 0x05, 0x01, // Usage Page (Generic Desktop) 0
965984
* 0x09, 0x02, // Usage (Mouse) 2
966985
*/
967-
if (hdev->vendor == USB_VENDOR_ID_APPLE &&
968-
(hdev->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2 ||
969-
hdev->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2_USBC ||
970-
hdev->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2 ||
971-
hdev->product == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC) &&
986+
if ((is_usb_magicmouse2(hdev->vendor, hdev->product) ||
987+
is_usb_magictrackpad2(hdev->vendor, hdev->product)) &&
972988
*rsize == 83 && rdesc[46] == 0x84 && rdesc[58] == 0x85) {
973989
hid_info(hdev,
974990
"fixing up magicmouse battery report descriptor\n");

0 commit comments

Comments
 (0)