Skip to contentSkip to conversation

HID: asus: increase robustness of the driver

PATCH In review Denis Benato 2 participants 9 messages Feb 28 – Mar 10 HID

lore
Share & tools
lore
#0 lore raw

v2 cover HID: asus: increase robustness of the driver

Hi all,

Previous asus-wmi maintainer and asus-linux developer has become less active in the project and left me in charge of advancing the support for ASUS equipement on Linux.

I am preparing to send a patchset of his revised work to support ASUS ROG Ally handhelds devices and since that work is also useful in expanding support for 2025 models it is important to improve the hid-asus driver as future patches will build on top of these changes.

Cheers,
Denis

Changelog:
- v1
- Initial submission
- v2
- Remove another warning about a now unused int ret.
- Fix a regression (already in for-next) about leds not working anymore.

Denis Benato (7):
HID: asus: fix code style of comments and brackets
HID: asus: add xg mobile 2022 external hardware support
HID: asus: fix compiler warning about unused variables
HID: asus: make asus_resume adhere to linux kernel coding standards
HID: asus: simplify and improve asus_kbd_set_report()
HID: asus: do not abort probe when not necessary
HID: asus: do not try to initialize the backlight if the enpoint
doesn't support it

 drivers/hid/hid-asus.c | 74 +++++++++++++++++-------------------------
 drivers/hid/hid-ids.h  |  1 +
 2 files changed, 30 insertions(+), 45 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH v2 1/7 HID: asus: fix code style of comments and brackets

In asus_raw_event there is code that would violate checkpatch script such as unaligned closing comments and superfluous graph parenthesis:
fix these stylistic issues.

Also remove an empty comment spanning two lines.

This commit does not change runtime behavior.

---
 drivers/hid/hid-asus.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
drivers/hid/hid-asus.c +5 -11
index ffbfaff9f117..5fcb06b16167 100644
@@ -20,9 +20,6 @@
  *  Copyright (c) 2016 Frederik Wenigwieser <[email protected]>
  */
 
-/*
- */
-
 #include <linux/acpi.h>
 #include <linux/dmi.h>
 #include <linux/hid.h>
@@ -359,7 +356,7 @@ static int asus_event(struct hid_device *hdev, struct hid_field *field,
 		      struct hid_usage *usage, __s32 value)
 {
 	struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
-	
+
 	if ((usage->hid & HID_USAGE_PAGE) == HID_UP_ASUSVENDOR &&
 	    (usage->hid & HID_USAGE) != 0x00 &&
 	    (usage->hid & HID_USAGE) != 0xff && !usage->type) {
@@ -448,21 +445,18 @@ static int asus_raw_event(struct hid_device *hdev,
 		/*
 		 * G713 and G733 send these codes on some keypresses, depending on
 		 * the key pressed it can trigger a shutdown event if not caught.
-		*/
-		if (data[0] == 0x02 && data[1] == 0x30) {
+		 */
+		if (data[0] == 0x02 && data[1] == 0x30)
 			return -1;
-		}
 	}
 
 	if (drvdata->quirks & QUIRK_ROG_CLAYMORE_II_KEYBOARD) {
 		/*
 		 * CLAYMORE II keyboard sends this packet when it goes to sleep
 		 * this causes the whole system to go into suspend.
-		*/
-
-		if(size == 2 && data[0] == 0x02 && data[1] == 0x00) {
+		 */
+		if (size == 2 && data[0] == 0x02 && data[1] == 0x00)
 			return -1;
-		}
 	}
 
 	return 0;
Signature · 1 line
2.53.0
#2 lore raw

PATCH v2 2/7 HID: asus: add xg mobile 2022 external hardware support

In reply to Denis Benato

XG mobile station 2022 has a different PID than the 2023 model: add it that model to hid-asus.

---
 drivers/hid/hid-asus.c | 3 +++
 drivers/hid/hid-ids.h  | 1 +
 2 files changed, 4 insertions(+)
drivers/hid/hid-asus.c +3 -0
index 5fcb06b16167..fbcf38b15290 100644
@@ -1515,6 +1515,9 @@ static const struct hid_device_id asus_devices[] = {
 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
 	    USB_DEVICE_ID_ASUSTEK_ROG_NKEY_ALLY_X),
 	  QUIRK_USE_KBD_BACKLIGHT | QUIRK_ROG_NKEY_KEYBOARD | QUIRK_ROG_ALLY_XPAD },
+	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
+	    USB_DEVICE_ID_ASUSTEK_XGM_2022),
+	},
 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUSTEK,
 	    USB_DEVICE_ID_ASUSTEK_XGM_2023),
 	},
drivers/hid/hid-ids.h +1 -0
index 4ab7640b119a..5e85921049e0 100644
@@ -229,6 +229,7 @@
 #define USB_DEVICE_ID_ASUSTEK_ROG_NKEY_ALLY_X		0x1b4c
 #define USB_DEVICE_ID_ASUSTEK_ROG_CLAYMORE_II_KEYBOARD	0x196b
 #define USB_DEVICE_ID_ASUSTEK_FX503VD_KEYBOARD	0x1869
+#define USB_DEVICE_ID_ASUSTEK_XGM_2022	0x1970
 #define USB_DEVICE_ID_ASUSTEK_XGM_2023	0x1a9a
 
 #define USB_VENDOR_ID_ATEN		0x0557
Signature · 1 line
2.53.0
#3 lore raw

PATCH v2 3/7 HID: asus: fix compiler warning about unused variables

In reply to Denis Benato

In the function asus_has_report_id there are 3 unused variables clang warns about: remove them.

Fixes 0919db9f3583 ("HID: asus: always fully initialize devices")
---
 drivers/hid/hid-asus.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/hid/hid-asus.c +1 -2
index fbcf38b15290..2b4411399571 100644
@@ -723,10 +723,9 @@ static void validate_mcu_fw_version(struct hid_device *hdev, int idProduct)
 
 static bool asus_has_report_id(struct hid_device *hdev, u16 report_id)
 {
-	int t, f, u, err = 0;
 	struct hid_report *report;
 
-	for (t = HID_INPUT_REPORT; t <= HID_FEATURE_REPORT; t++) {
+	for (int t = HID_INPUT_REPORT; t <= HID_FEATURE_REPORT; t++) {
 		list_for_each_entry(report, &hdev->report_enum[t].report_list, list) {
 			if (report->id == report_id)
 				return true;
Signature · 1 line
2.53.0
#4 lore raw

PATCH v2 4/7 HID: asus: make asus_resume adhere to linux kernel coding standards

In reply to Denis Benato

Linux kernel coding standars requires functions opening brackets to be in a newline: move the opening bracket of asus_resume in its own line.

Fixes 546edbd26cff ("HID: hid-asus: reset the backlight brightness level on resume")
---
 drivers/hid/hid-asus.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/hid/hid-asus.c +2 -1
index 2b4411399571..48731b48523d 100644
@@ -1171,7 +1171,8 @@ static int asus_start_multitouch(struct hid_device *hdev)
 	return 0;
 }
 
-static int __maybe_unused asus_resume(struct hid_device *hdev) {
+static int __maybe_unused asus_resume(struct hid_device *hdev)
+{
 	struct asus_drvdata *drvdata = hid_get_drvdata(hdev);
 	int ret = 0;
 
Signature · 1 line
2.53.0
#5 lore raw

PATCH v2 5/7 HID: asus: simplify and improve asus_kbd_set_report()

In reply to Denis Benato

Make the function shorter and easier to read using __free, and also fix a misaligned comment closing tag.

The __free macro from cleanup.h is already used in the driver, but its include is missing: add it.

---
 drivers/hid/hid-asus.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
drivers/hid/hid-asus.c +5 -11
index 48731b48523d..4fa8e72c6855 100644
@@ -21,6 +21,7 @@
  */
 
 #include <linux/acpi.h>
+#include <linux/cleanup.h>
 #include <linux/dmi.h>
 #include <linux/hid.h>
 #include <linux/module.h>
@@ -464,23 +465,16 @@ static int asus_raw_event(struct hid_device *hdev,
 
 static int asus_kbd_set_report(struct hid_device *hdev, const u8 *buf, size_t buf_size)
 {
-	unsigned char *dmabuf;
-	int ret;
-
-	dmabuf = kmemdup(buf, buf_size, GFP_KERNEL);
+	u8 *dmabuf __free(kfree) = kmemdup(buf, buf_size, GFP_KERNEL);
 	if (!dmabuf)
 		return -ENOMEM;
 
 	/*
 	 * The report ID should be set from the incoming buffer due to LED and key
 	 * interfaces having different pages
-	*/
-	ret = hid_hw_raw_request(hdev, buf[0], dmabuf,
-				 buf_size, HID_FEATURE_REPORT,
-				 HID_REQ_SET_REPORT);
-	kfree(dmabuf);
-
-	return ret;
+	 */
+	return hid_hw_raw_request(hdev, buf[0], dmabuf, buf_size, HID_FEATURE_REPORT,
+				  HID_REQ_SET_REPORT);
 }
 
 static int asus_kbd_init(struct hid_device *hdev, u8 report_id)
Signature · 1 line
2.53.0
#6 lore raw

PATCH v2 6/7 HID: asus: do not abort probe when not necessary

In reply to Denis Benato

In order to avoid dereferencing a NULL pointer asus_probe is aborted early and control of some asus devices is transferred over hid-generic after erroring out even when such NULL dereference cannot happen: only early abort when the NULL dereference can happen.

Also make the code shorter and more adherent to coding standards removing square brackets enclosing single-line if-else statements.

Fixes d3af6ca9a8c3 ("HID: asus: fix UAF via HID_CLAIMED_INPUT validation")
---
 drivers/hid/hid-asus.c | 25 ++++++++++---------------
 1 file changed, 10 insertions(+), 15 deletions(-)
drivers/hid/hid-asus.c +10 -15
index 4fa8e72c6855..5780e70aebd5 100644
@@ -1323,22 +1323,17 @@ static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
 	 * were freed during registration due to no usages being mapped,
 	 * leaving drvdata->input pointing to freed memory.
 	 */
-	if (!drvdata->input || !(hdev->claimed & HID_CLAIMED_INPUT)) {
-		hid_err(hdev, "Asus input not registered\n");
-		ret = -ENOMEM;
-		goto err_stop_hw;
-	}
-
-	if (drvdata->tp) {
-		drvdata->input->name = "Asus TouchPad";
-	} else {
-		drvdata->input->name = "Asus Keyboard";
-	}
+	if (drvdata->input && (hdev->claimed & HID_CLAIMED_INPUT)) {
+		if (drvdata->tp)
+			drvdata->input->name = "Asus TouchPad";
+		else
+			drvdata->input->name = "Asus Keyboard";
 
-	if (drvdata->tp) {
-		ret = asus_start_multitouch(hdev);
-		if (ret)
-			goto err_stop_hw;
+		if (drvdata->tp) {
+			ret = asus_start_multitouch(hdev);
+			if (ret)
+				goto err_stop_hw;
+		}
 	}
 
 	return 0;
Signature · 1 line
2.53.0
#7 lore raw

PATCH v2 7/7 HID: asus: do not try to initialize the backlight if the enpoint doesn't support it

In reply to Denis Benato

Avoid possibly printing a warning about the inability to initialize the backlight if the hid endpoint doesn't support it.

Also fix and move an incorrect check in asus_kbd_register_leds(): that same check is now used as a precondition to call that function.

Fixes 0919db9f3583 ("HID: asus: always fully initialize devices")
---
 drivers/hid/hid-asus.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/hid/hid-asus.c +3 -5
index 5780e70aebd5..9291f23c10b7 100644
@@ -737,10 +737,6 @@ static int asus_kbd_register_leds(struct hid_device *hdev)
 	unsigned char kbd_func;
 	int ret;
 
-	/* Laptops keyboard backlight is always at 0x5a */
-	if (asus_has_report_id(hdev, FEATURE_KBD_REPORT_ID))
-		return -ENODEV;
-
 	/* Get keyboard functions */
 	ret = asus_kbd_get_functions(hdev, &kbd_func, FEATURE_KBD_REPORT_ID);
 	if (ret < 0)
@@ -1306,8 +1302,10 @@ static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
 		}
 	}
 
+	/* Laptops keyboard backlight is always at 0x5a */
 	if (is_vendor && (drvdata->quirks & QUIRK_USE_KBD_BACKLIGHT) &&
-	    asus_kbd_register_leds(hdev))
+	    (asus_has_report_id(hdev, FEATURE_KBD_REPORT_ID)) &&
+		(asus_kbd_register_leds(hdev)))
 		hid_warn(hdev, "Failed to initialize backlight.\n");
 
 	/*
Signature · 1 line
2.53.0
maintainer
#8 lore raw
In reply to Denis Benato
Quoted 10 lines · Denis Benato

Hi all,

Previous asus-wmi maintainer and asus-linux developer has become less active in the project and left me in charge of advancing the support for ASUS equipement on Linux.

I am preparing to send a patchset of his revised work to support ASUS ROG Ally handhelds devices and since that work is also useful in expanding support for 2025 models it is important to improve the hid-asus driver as future patches will build on top of these changes.

Patches 1, 4, 5, 6 and 7 applied to hid.git#for-7.1/asus.

Patch 2 applied to hid.git#for-7.0/upstream-fixes.

Patch 3 dropped, as it's fixed by 7b2f88cc9dd4c2b9f3d in hid.git#for-7.1/asus.

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs