Skip to content

Commit 069e79a

Browse files
author
Jiri Kosina
committed
Merge branch 'for-6.17/battery-timer-fixes' into for-linus
- avoid setting up battery timer for Apple and Magicmouse devices without battery (Aditya Garg)
2 parents 96ba894 + 230cdd8 commit 069e79a

File tree

2 files changed

+54
-33
lines changed

2 files changed

+54
-33
lines changed

drivers/hid/hid-apple.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
#define APPLE_FLAG_TB_FKEY BIT(1)
4949

5050
#define HID_COUNTRY_INTERNATIONAL_ISO 13
51-
#define APPLE_BATTERY_TIMEOUT_MS 60000
51+
#define APPLE_BATTERY_TIMEOUT_SEC 60
5252

5353
#define HID_USAGE_MAGIC_BL 0xff00000f
5454
#define APPLE_MAGIC_REPORT_ID_POWER 3
@@ -645,7 +645,7 @@ static void apple_battery_timer_tick(struct timer_list *t)
645645

646646
if (apple_fetch_battery(hdev) == 0) {
647647
mod_timer(&asc->battery_timer,
648-
jiffies + msecs_to_jiffies(APPLE_BATTERY_TIMEOUT_MS));
648+
jiffies + secs_to_jiffies(APPLE_BATTERY_TIMEOUT_SEC));
649649
}
650650
}
651651

@@ -960,10 +960,12 @@ static int apple_probe(struct hid_device *hdev,
960960
return ret;
961961
}
962962

963-
timer_setup(&asc->battery_timer, apple_battery_timer_tick, 0);
964-
mod_timer(&asc->battery_timer,
965-
jiffies + msecs_to_jiffies(APPLE_BATTERY_TIMEOUT_MS));
966-
apple_fetch_battery(hdev);
963+
if (quirks & APPLE_RDESC_BATTERY) {
964+
timer_setup(&asc->battery_timer, apple_battery_timer_tick, 0);
965+
mod_timer(&asc->battery_timer,
966+
jiffies + secs_to_jiffies(APPLE_BATTERY_TIMEOUT_SEC));
967+
apple_fetch_battery(hdev);
968+
}
967969

968970
if (quirks & APPLE_BACKLIGHT_CTL)
969971
apple_backlight_init(hdev);
@@ -977,7 +979,9 @@ static int apple_probe(struct hid_device *hdev,
977979
return 0;
978980

979981
out_err:
980-
timer_delete_sync(&asc->battery_timer);
982+
if (quirks & APPLE_RDESC_BATTERY)
983+
timer_delete_sync(&asc->battery_timer);
984+
981985
hid_hw_stop(hdev);
982986
return ret;
983987
}
@@ -986,7 +990,8 @@ static void apple_remove(struct hid_device *hdev)
986990
{
987991
struct apple_sc *asc = hid_get_drvdata(hdev);
988992

989-
timer_delete_sync(&asc->battery_timer);
993+
if (asc->quirks & APPLE_RDESC_BATTERY)
994+
timer_delete_sync(&asc->battery_timer);
990995

991996
hid_hw_stop(hdev);
992997
}

drivers/hid/hid-magicmouse.c

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state fie
6060
#define MOUSE_REPORT_ID 0x29
6161
#define MOUSE2_REPORT_ID 0x12
6262
#define DOUBLE_REPORT_ID 0xf7
63-
#define USB_BATTERY_TIMEOUT_MS 60000
63+
#define USB_BATTERY_TIMEOUT_SEC 60
6464

6565
/* These definitions are not precise, but they're close enough. (Bits
6666
* 0x03 seem to indicate the aspect ratio of the touch, bits 0x70 seem
@@ -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];
@@ -827,7 +841,7 @@ static void magicmouse_battery_timer_tick(struct timer_list *t)
827841

828842
if (magicmouse_fetch_battery(hdev) == 0) {
829843
mod_timer(&msc->battery_timer,
830-
jiffies + msecs_to_jiffies(USB_BATTERY_TIMEOUT_MS));
844+
jiffies + secs_to_jiffies(USB_BATTERY_TIMEOUT_SEC));
831845
}
832846
}
833847

@@ -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 + secs_to_jiffies(USB_BATTERY_TIMEOUT_SEC));
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)