Skip to contentSkip to conversation

platform/x86: asus-wmi: fix fn-lock on platforms where WMI DEVS is a no-op

PATCH Superseded Marcus Grenängen 5 participants 16 messages Apr 30 – Jul 28

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 platform/x86: asus-wmi: fix fn-lock on platforms where WMI DEVS is a no-op

Some ASUS laptops (e.g. ProArt P16, N-Key keyboard 0B05:19B6) advertise the WMI fn-lock DEVID (0x00100023) as present but the DEVS call has no effect. On these machines fn-lock must be toggled via a HID feature report sent directly to the N-Key keyboard, which is handled by hid-asus.

Add a DMI quirk flag fnlock_use_hid for such platforms. When set, asus_wmi_has_fnlock_key() checks for a registered hid-asus listener that supports fnlock instead of querying DEVS, and asus_wmi_fnlock_update() dispatches to asus_hid_set_fnlock().

Extend struct asus_hid_listener with a fnlock_set callback and wire it up in hid-asus for QUIRK_ROG_NKEY_KEYBOARD + QUIRK_HID_FN_LOCK devices. Initialize fn_lock_sync_work and register the listener in the probe path for these keyboards (previously the early return for ROG N-Key devices skipped asus_input_configured where this was done).

Also add a fnlock_status sysfs attribute (RW) under the asus-wmi platform device so userspace (e.g. asusctl) can read and set fn-lock state without needing to open /dev/hidraw directly.

Fix a NULL pointer dereference in do_kbd_led_set(): brightness_set was called unconditionally on all listeners, but listeners that only implement fnlock_set leave brightness_set NULL.

---
 drivers/hid/hid-asus.c                     |  24 ++++-
 drivers/platform/x86/asus-nb-wmi.c         |  13 +++
 drivers/platform/x86/asus-wmi.c            | 101 ++++++++++++++++++++-
 drivers/platform/x86/asus-wmi.h            |   5 +
 include/linux/platform_data/x86/asus-wmi.h |   7 +-
 5 files changed, 143 insertions(+), 7 deletions(-)
drivers/hid/hid-asus.c +22 -2
index d34d74df3dc0..71ced03c0f7e 100644
@@ -143,6 +143,7 @@ struct asus_drvdata {
 	unsigned long battery_next_query;
 	struct work_struct fn_lock_sync_work;
 	bool fn_lock;
+	struct asus_hid_listener fn_lock_listener;
 };
 
 static int asus_report_battery(struct asus_drvdata *, u8 *, int);
@@ -584,6 +585,15 @@ static void asus_sync_fn_lock(struct work_struct *work)
 	asus_kbd_set_fn_lock(drvdata->hdev, drvdata->fn_lock);
 }
 
+static void asus_hid_fnlock_set(struct asus_hid_listener *listener, bool enabled)
+{
+	struct asus_drvdata *drvdata =
+		container_of(listener, struct asus_drvdata, fn_lock_listener);
+
+	drvdata->fn_lock = enabled;
+	schedule_work(&drvdata->fn_lock_sync_work);
+}
+
 static void asus_schedule_work(struct asus_kbd_leds *led)
 {
 	unsigned long flags;
@@ -1319,8 +1329,16 @@ static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
 	 * For ROG keyboards, skip rename for consistency and ->input check as
 	 * some devices do not have inputs.
 	 */
-	if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD)
+	if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD) {
+		if (drvdata->quirks & QUIRK_HID_FN_LOCK) {
+			drvdata->fn_lock = true;
+			INIT_WORK(&drvdata->fn_lock_sync_work, asus_sync_fn_lock);
+			asus_kbd_set_fn_lock(hdev, true);
+			drvdata->fn_lock_listener.fnlock_set = asus_hid_fnlock_set;
+			asus_hid_register_listener(&drvdata->fn_lock_listener);
+		}
 		return 0;
+	}
 
 	/*
 	 * Check that input registration succeeded. Checking that
@@ -1362,8 +1380,10 @@ static void asus_remove(struct hid_device *hdev)
 		cancel_work_sync(&drvdata->kbd_backlight->work);
 	}
 
-	if (drvdata->quirks & QUIRK_HID_FN_LOCK)
+	if (drvdata->quirks & QUIRK_HID_FN_LOCK) {
+		asus_hid_unregister_listener(&drvdata->fn_lock_listener);
 		cancel_work_sync(&drvdata->fn_lock_sync_work);
+	}
 
 	hid_hw_stop(hdev);
 }
drivers/platform/x86/asus-nb-wmi.c +13 -0
index b4677c5bba5b..44e4cf68ff70 100644
@@ -155,6 +155,10 @@ static struct quirk_entry quirk_asus_z13 = {
 	.tablet_switch_mode = asus_wmi_kbd_dock_devid,
 };
 
+static struct quirk_entry quirk_asus_proart_p16 = {
+	.fnlock_use_hid = true,
+};
+
 static int dmi_matched(const struct dmi_system_id *dmi)
 {
 	pr_info("Identified laptop model '%s'\n", dmi->ident);
@@ -553,6 +557,15 @@ static const struct dmi_system_id asus_quirks[] = {
 		},
 		.driver_data = &quirk_asus_z13,
 	},
+	{
+		.callback = dmi_matched,
+		.ident = "ASUS ProArt P16",
+		.matches = {
+			DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
+			DMI_MATCH(DMI_PRODUCT_FAMILY, "ProArt P16"),
+		},
+		.driver_data = &quirk_asus_proart_p16,
+	},
 	{},
 };
 
drivers/platform/x86/asus-wmi.c +97 -4

149 lines not on this page · raw message

drivers/platform/x86/asus-wmi.h +5 -0
index 5cd4392b964e..6c50b11860e8 100644
@@ -52,6 +52,11 @@ struct quirk_entry {
 	 */
 	int no_display_toggle;
 	u32 xusb2pr;
+	/*
+	 * Some platforms report WMI DEVID_FNLOCK as present but the DEVS call
+	 * is a no-op. Force the HID feature report path via hid-asus instead.
+	 */
+	bool fnlock_use_hid;
 };
 
 struct asus_wmi_driver {
include/linux/platform_data/x86/asus-wmi.h +6 -1
index 554f41b827e1..569289417a51 100644
@@ -173,10 +173,11 @@ enum asus_ally_mcu_hack {
 	ASUS_WMI_ALLY_MCU_HACK_DISABLED,
 };
 
-/* Used to notify hid-asus when asus-wmi changes keyboard backlight */
+/* Used to notify hid-asus when asus-wmi changes keyboard backlight or fn-lock */
 struct asus_hid_listener {
 	struct list_head list;
 	void (*brightness_set)(struct asus_hid_listener *listener, int brightness);
+	void (*fnlock_set)(struct asus_hid_listener *listener, bool enabled);
 };
 
 enum asus_hid_event {
@@ -196,6 +197,7 @@ int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval);
 int asus_hid_register_listener(struct asus_hid_listener *cdev);
 void asus_hid_unregister_listener(struct asus_hid_listener *cdev);
 int asus_hid_event(enum asus_hid_event event);
+void asus_hid_set_fnlock(bool enabled);
 #else
 static inline void set_ally_mcu_hack(enum asus_ally_mcu_hack status)
 {
@@ -227,6 +229,9 @@ static inline int asus_hid_event(enum asus_hid_event event)
 {
 	return -ENODEV;
 }
+static inline void asus_hid_set_fnlock(bool enabled)
+{
+}
 #endif
 
 #endif	/* __PLATFORM_DATA_X86_ASUS_WMI_H */
Signature · 1 line
2.54.0
maintainer
#1 lore raw
Quoted 4 lines · Marcus Grenängen

Some ASUS laptops (e.g. ProArt P16, N-Key keyboard 0B05:19B6) advertise the WMI fn-lock DEVID (0x00100023) as present but the DEVS call has no effect. On these machines fn-lock must be toggled via a HID feature report sent directly to the N-Key keyboard, which is handled by hid-asus.

Hi, thanks for your work on this!

Quoted 4 lines

Add a DMI quirk flag fnlock_use_hid for such platforms. When set, asus_wmi_has_fnlock_key() checks for a registered hid-asus listener that supports fnlock instead of querying DEVS, and asus_wmi_fnlock_update() dispatches to asus_hid_set_fnlock().

Do you think there might be something that might make this auto-detected instead of using a quirk?

Quoted 9 lines

Extend struct asus_hid_listener with a fnlock_set callback and wire it up in hid-asus for QUIRK_ROG_NKEY_KEYBOARD + QUIRK_HID_FN_LOCK devices. Initialize fn_lock_sync_work and register the listener in the probe path for these keyboards (previously the early return for ROG N-Key devices skipped asus_input_configured where this was done).

Also add a fnlock_status sysfs attribute (RW) under the asus-wmi platform device so userspace (e.g. asusctl) can read and set fn-lock state without needing to open /dev/hidraw directly.

I believe the place for this is asus-armoury driver not asus-wmi... it's easier too to do it in asus-armoury too.

Quoted 3 lines

Fix a NULL pointer dereference in do_kbd_led_set(): brightness_set was called unconditionally on all listeners, but listeners that only implement fnlock_set leave brightness_set NULL.

This is spelled a bit weird since the issue does not exists until you add another listener type: can you split up this patch in maybe 3? One where you prepare asus-wmi for your additions, one where you add to asus-armoury and one where you actually add the functionality into asus-wmi? (also maybe is worth adding a 4th one with modifications on asus-hid)?

Quoted 308 lines
---
 drivers/hid/hid-asus.c                     |  24 ++++-
 drivers/platform/x86/asus-nb-wmi.c         |  13 +++
 drivers/platform/x86/asus-wmi.c            | 101 ++++++++++++++++++++-
 drivers/platform/x86/asus-wmi.h            |   5 +
 include/linux/platform_data/x86/asus-wmi.h |   7 +-
 5 files changed, 143 insertions(+), 7 deletions(-)
drivers/hid/hid-asus.c +22 -2
index d34d74df3dc0..71ced03c0f7e 100644
@@ -143,6 +143,7 @@ struct asus_drvdata {
 	unsigned long battery_next_query;
 	struct work_struct fn_lock_sync_work;
 	bool fn_lock;
+	struct asus_hid_listener fn_lock_listener;
 };
 
 static int asus_report_battery(struct asus_drvdata *, u8 *, int);
@@ -584,6 +585,15 @@ static void asus_sync_fn_lock(struct work_struct *work)
 	asus_kbd_set_fn_lock(drvdata->hdev, drvdata->fn_lock);
 }
 
+static void asus_hid_fnlock_set(struct asus_hid_listener *listener, bool enabled)
+{
+	struct asus_drvdata *drvdata =
+		container_of(listener, struct asus_drvdata, fn_lock_listener);
+
+	drvdata->fn_lock = enabled;
+	schedule_work(&drvdata->fn_lock_sync_work);
+}
+
 static void asus_schedule_work(struct asus_kbd_leds *led)
 {
 	unsigned long flags;
@@ -1319,8 +1329,16 @@ static int asus_probe(struct hid_device *hdev, const struct hid_device_id *id)
 	 * For ROG keyboards, skip rename for consistency and ->input check as
 	 * some devices do not have inputs.
 	 */
-	if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD)
+	if (drvdata->quirks & QUIRK_ROG_NKEY_KEYBOARD) {
+		if (drvdata->quirks & QUIRK_HID_FN_LOCK) {
+			drvdata->fn_lock = true;
+			INIT_WORK(&drvdata->fn_lock_sync_work, asus_sync_fn_lock);
+			asus_kbd_set_fn_lock(hdev, true);
+			drvdata->fn_lock_listener.fnlock_set = asus_hid_fnlock_set;
+			asus_hid_register_listener(&drvdata->fn_lock_listener);
+		}
 		return 0;
+	}
 
 	/*
 	 * Check that input registration succeeded. Checking that
@@ -1362,8 +1380,10 @@ static void asus_remove(struct hid_device *hdev)
 		cancel_work_sync(&drvdata->kbd_backlight->work);
 	}
 
-	if (drvdata->quirks & QUIRK_HID_FN_LOCK)
+	if (drvdata->quirks & QUIRK_HID_FN_LOCK) {
+		asus_hid_unregister_listener(&drvdata->fn_lock_listener);
 		cancel_work_sync(&drvdata->fn_lock_sync_work);
+	}
 
 	hid_hw_stop(hdev);
 }
drivers/platform/x86/asus-nb-wmi.c +13 -0
index b4677c5bba5b..44e4cf68ff70 100644
@@ -155,6 +155,10 @@ static struct quirk_entry quirk_asus_z13 = {
 	.tablet_switch_mode = asus_wmi_kbd_dock_devid,
 };
 
+static struct quirk_entry quirk_asus_proart_p16 = {
+	.fnlock_use_hid = true,
+};
+
 static int dmi_matched(const struct dmi_system_id *dmi)
 {
 	pr_info("Identified laptop model '%s'\n", dmi->ident);
@@ -553,6 +557,15 @@ static const struct dmi_system_id asus_quirks[] = {
 		},
 		.driver_data = &quirk_asus_z13,
 	},
+	{
+		.callback = dmi_matched,
+		.ident = "ASUS ProArt P16",
+		.matches = {
+			DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
+			DMI_MATCH(DMI_PRODUCT_FAMILY, "ProArt P16"),
+		},
+		.driver_data = &quirk_asus_proart_p16,
+	},
 	{},
 };
 
drivers/platform/x86/asus-wmi.c +97 -4
index 80144c412b90..70758a20551d 100644
@@ -1759,6 +1759,24 @@ int asus_hid_event(enum asus_hid_event event)
 }
 EXPORT_SYMBOL_GPL(asus_hid_event);
 
+/*
+ * Called by asus-wmi to propagate an fn-lock state change to all registered
+ * hid-asus listeners. Used on platforms where the WMI DEVS path for fn-lock
+ * (0x00100023) is a no-operation and the HID feature report path must be used
+ * instead (e.g. ASUS ProArt P16, USB keyboard product ID 0x19B6).
+ */
+void asus_hid_set_fnlock(bool enabled)
+{
+	struct asus_hid_listener *listener;
+
+	guard(spinlock_irqsave)(&asus_ref.lock);
+	list_for_each_entry(listener, &asus_ref.listeners, list) {
+		if (listener->fnlock_set)
+			listener->fnlock_set(listener, enabled);
+	}
+}
+EXPORT_SYMBOL_GPL(asus_hid_set_fnlock);
+
 /*
  * These functions actually update the LED's, and are called from a
  * workqueue. By doing this as separate work rather than when the LED
@@ -1852,7 +1870,8 @@ static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
 
 	scoped_guard(spinlock_irqsave, &asus_ref.lock) {
 		list_for_each_entry(listener, &asus_ref.listeners, list)
-			listener->brightness_set(listener, asus->kbd_led_wk);
+			if (listener->brightness_set)
+				listener->brightness_set(listener, asus->kbd_led_wk);
 	}
 }
 
@@ -4495,21 +4514,64 @@ static void asus_screenpad_exit(struct asus_wmi *asus)
 
 /* Fn-lock ********************************************************************/
 
+static bool asus_hid_has_fnlock_listener(void)
+{
+	struct asus_hid_listener *listener;
+
+	guard(spinlock_irqsave)(&asus_ref.lock);
+	list_for_each_entry(listener, &asus_ref.listeners, list) {
+		if (listener->fnlock_set)
+			return true;
+	}
+	return false;
+}
+
 static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
 {
 	u32 result;
 
+	/* Some platforms have a non-functional WMI path — use HID directly */
+	if (asus->driver->quirks->fnlock_use_hid)
+		return asus_hid_has_fnlock_listener();
+
 	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
 
-	return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
-		!(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
+	if ((result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
+	    !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED))
+		return true;
+
+	/*
+	 * Some platforms (e.g. ASUS ProArt P16) have a non-functional WMI
+	 * DEVS path for fn-lock (DEVS returns One unconditionally with no
+	 * side effects). On these platforms fn-lock is controlled via a HID
+	 * feature report sent directly to the N-Key keyboard. Return true if
+	 * a hid-asus listener with fnlock support has registered, so that
+	 * asus-wmi can manage the fn-lock state and expose the sysfs knob.
+	 */
+	return asus_hid_has_fnlock_listener();
 }
 
 static void asus_wmi_fnlock_update(struct asus_wmi *asus)
 {
 	int mode = asus->fnlock_locked;
+	u32 result;
 
-	asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
+	/* Platform has non-functional WMI DEVS for fn-lock — use HID directly */
+	if (asus->driver->quirks->fnlock_use_hid) {
+		asus_hid_set_fnlock(mode);
+		return;
+	}
+
+	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
+
+	if ((result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
+	    !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED)) {
+		asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
+		return;
+	}
+
+	/* WMI path absent or non-functional — delegate to hid-asus */
+	asus_hid_set_fnlock(mode);
 }
 
 /* WMI events *****************************************************************/
@@ -4690,6 +4752,34 @@ static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
 
 static DEVICE_ATTR_WO(cpufv);
 
+static ssize_t fnlock_status_show(struct device *dev,
+				  struct device_attribute *attr, char *buf)
+{
+	struct asus_wmi *asus = dev_get_drvdata(dev);
+
+	return sysfs_emit(buf, "%d\n", asus->fnlock_locked);
+}
+
+static ssize_t fnlock_status_store(struct device *dev,
+				   struct device_attribute *attr,
+				   const char *buf, size_t count)
+{
+	struct asus_wmi *asus = dev_get_drvdata(dev);
+	bool enable;
+	int err;
+
+	err = kstrtobool(buf, &enable);
+	if (err)
+		return err;
+
+	asus->fnlock_locked = enable;
+	asus_wmi_fnlock_update(asus);
+
+	return count;
+}
+
+static DEVICE_ATTR_RW(fnlock_status);
+
 static struct attribute *platform_attributes[] = {
 	&dev_attr_cpufv.attr,
 	&dev_attr_camera.attr,
@@ -4698,6 +4788,7 @@ static struct attribute *platform_attributes[] = {
 	&dev_attr_lid_resume.attr,
 	&dev_attr_als_enable.attr,
 	&dev_attr_fan_boost_mode.attr,
+	&dev_attr_fnlock_status.attr,
 #if IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS)
 		&dev_attr_charge_mode.attr,
 		&dev_attr_egpu_enable.attr,
@@ -4741,6 +4832,8 @@ static umode_t asus_sysfs_is_visible(struct kobject *kobj,
 		devid = ASUS_WMI_DEVID_ALS_ENABLE;
 	else if (attr == &dev_attr_fan_boost_mode.attr)
 		ok = asus->fan_boost_mode_available;
+	else if (attr == &dev_attr_fnlock_status.attr)
+		ok = asus_wmi_has_fnlock_key(asus);
 
 #if IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS)
 	if (attr == &dev_attr_charge_mode.attr)
drivers/platform/x86/asus-wmi.h +5 -0
index 5cd4392b964e..6c50b11860e8 100644
@@ -52,6 +52,11 @@ struct quirk_entry {
 	 */
 	int no_display_toggle;
 	u32 xusb2pr;
+	/*
+	 * Some platforms report WMI DEVID_FNLOCK as present but the DEVS call
+	 * is a no-op. Force the HID feature report path via hid-asus instead.
+	 */
+	bool fnlock_use_hid;
 };
 
 struct asus_wmi_driver {
include/linux/platform_data/x86/asus-wmi.h +6 -1
index 554f41b827e1..569289417a51 100644
@@ -173,10 +173,11 @@ enum asus_ally_mcu_hack {
 	ASUS_WMI_ALLY_MCU_HACK_DISABLED,
 };
 
-/* Used to notify hid-asus when asus-wmi changes keyboard backlight */
+/* Used to notify hid-asus when asus-wmi changes keyboard backlight or fn-lock */
 struct asus_hid_listener {
 	struct list_head list;
 	void (*brightness_set)(struct asus_hid_listener *listener, int brightness);
+	void (*fnlock_set)(struct asus_hid_listener *listener, bool enabled);
 };
 
 enum asus_hid_event {
@@ -196,6 +197,7 @@ int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval);
 int asus_hid_register_listener(struct asus_hid_listener *cdev);
 void asus_hid_unregister_listener(struct asus_hid_listener *cdev);
 int asus_hid_event(enum asus_hid_event event);
+void asus_hid_set_fnlock(bool enabled);
 #else
 static inline void set_ally_mcu_hack(enum asus_ally_mcu_hack status)
 {
@@ -227,6 +229,9 @@ static inline int asus_hid_event(enum asus_hid_event event)
 {
 	return -ENODEV;
 }
+static inline void asus_hid_set_fnlock(bool enabled)
+{
+}
 #endif
 
 #endif	/* __PLATFORM_DATA_X86_ASUS_WMI_H */
#2 lore raw

v2 cover platform/x86: fix fn-lock on ASUS ProArt P16 (WMI DEVS no-op)

This is v2 of the fn-lock fix for ASUS laptops where the WMI DEVS call for DEVID 0x00100023 is silently non-functional.

Changes since v1:
- Split into three patches as requested: hid-asus export, nb-wmi quirk, asus-armoury attribute (the NULL-ptr fix for brightness_set is folded into patch 1 where the second listener type is introduced)
- Moved the sysfs attribute to asus-armoury's firmware-attributes interface (fn_lock under /sys/class/firmware-attributes/asus-armoury/) instead of asus-wmi, as suggested
- asus-armoury now calls asus_hid_fnlock_notify() directly rather than routing through asus-wmi; this avoids touching asus-wmi.c entirely
- Dropped the asus_hid_listener::fnlock_set callback and the asus_hid_set_fnlock() / asus_hid_has_fnlock_listener() machinery from asus-wmi.c — the direct export from hid-asus is simpler and avoids the module init ordering issue described below

Regarding Denis's question about auto-detection: unfortunately it is not feasible. On the ProArt P16, DSTS reports DEVID 0x00100023 as present (ASUS_WMI_DSTS_PRESENCE_BIT set), so the existing WMI probe path finds it. There is no distinguishing bit in the DSTS result between "WMI works" and "WMI is silently a no-op". Attempting a test write at probe time would be unreliable (no readback available on HID-path platforms — the fn_lock show function intentionally returns -EOPNOTSUPP). A DMI quirk is the cleanest approach.

Note on module init ordering: asus-armoury and asus-nb-wmi are both compiled as loadable modules at the same init level. When asus-armoury initialises it cannot safely dereference asus_ref.asus (set by asus-wmi) to read the quirk flags, because asus-nb-wmi may not have probed yet. asus-armoury therefore uses dmi_match() directly rather than an exported accessor through asus-wmi.

Tested on ASUS ProArt P16 (H7606WI, N-Key keyboard 0B05:19B6):
- fn_lock attribute appears under firmware-attributes/asus-armoury/
- Writing 0/1 to current_value correctly toggles fn-lock state via HID
- asusctl fn-lock -s true/false works end-to-end via asusd

Marcus Grenängen (3):
HID: asus: export asus_hid_fnlock_notify() for direct fn-lock control platform/x86: asus-nb-wmi: add fnlock_use_hid quirk for ProArt P16 platform/x86: asus-armoury: add fn_lock firmware attribute

 drivers/hid/hid-asus.c                     | 43 +++++++++++++-
 drivers/platform/x86/asus-armoury.c        | 69 ++++++++++++++++++++++
 drivers/platform/x86/asus-nb-wmi.c         | 13 ++++
 drivers/platform/x86/asus-wmi.h            |  5 ++
 include/linux/platform_data/x86/asus-wmi.h |  5 ++
 5 files changed, 134 insertions(+), 1 deletion(-)
Signature · 1 line
2.54.0
#3 lore raw

PATCH v2 1/3 HID: asus: export asus_hid_fnlock_notify() for direct fn-lock control

Some ASUS platforms cannot control fn-lock via WMI DEVS and must send a HID feature report directly to the N-Key keyboard device instead.

Add a module-level fnlock_hdev pointer (protected by a mutex) that is set at probe time for devices with QUIRK_HID_FN_LOCK and cleared at remove. Export asus_hid_fnlock_notify(bool) so that asus-armoury can call into hid-asus without a circular dependency.

---
 drivers/hid/hid-asus.c                     | 43 +++++++++++++++++++++-
 include/linux/platform_data/x86/asus-wmi.h |  5 +++
 2 files changed, 47 insertions(+), 1 deletion(-)
drivers/hid/hid-asus.c +42 -1
index d34d74df3dc0..8a51dacf35eb 100644
@@ -584,6 +584,38 @@ static void asus_sync_fn_lock(struct work_struct *work)
 	asus_kbd_set_fn_lock(drvdata->hdev, drvdata->fn_lock);
 }
 
+/*
+ * Module-level reference to the HID device that handles fn-lock via feature
+ * report. Set at probe and cleared at remove for QUIRK_HID_FN_LOCK devices.
+ * Protected by fnlock_hdev_lock.
+ */
+static DEFINE_MUTEX(fnlock_hdev_lock);
+static struct hid_device *fnlock_hdev;
+
+/**
+ * asus_hid_fnlock_notify() - Set fn-lock state directly via HID feature report.
+ * @enabled: true to lock fn (F1-F12 primary), false to unlock.
+ *
+ * Called by asus-armoury on platforms where the WMI DEVS path for fn-lock is
+ * non-functional (e.g. ASUS ProArt P16, N-Key keyboard product ID 0x19B6).
+ *
+ * Returns 0 on success, -ENODEV if no fn-lock capable HID device is present.
+ */
+int asus_hid_fnlock_notify(bool enabled)
+{
+	int ret = -ENODEV;
+
+	guard(mutex)(&fnlock_hdev_lock);
+	if (fnlock_hdev) {
+		ret = asus_kbd_set_fn_lock(fnlock_hdev, enabled);
+		/* hid_hw_raw_request returns byte count on success; normalise to 0 */
+		if (ret > 0)
+			ret = 0;
+	}
+	return ret;
+}
+EXPORT_SYMBOL_GPL(asus_hid_fnlock_notify);
+
 static void asus_schedule_work(struct asus_kbd_leds *led)
 {
 	unsigned long flags;
@@ -969,6 +1001,8 @@ static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
 		drvdata->fn_lock = true;
 		INIT_WORK(&drvdata->fn_lock_sync_work, asus_sync_fn_lock);
 		asus_kbd_set_fn_lock(hdev, true);
+		guard(mutex)(&fnlock_hdev_lock);
+		fnlock_hdev = hdev;
 	}
 
 	if (drvdata->tp) {
@@ -1008,6 +1042,8 @@ static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
 		drvdata->fn_lock = true;
 		INIT_WORK(&drvdata->fn_lock_sync_work, asus_sync_fn_lock);
 		asus_kbd_set_fn_lock(hdev, true);
+		guard(mutex)(&fnlock_hdev_lock);
+		fnlock_hdev = hdev;
 	}
 
 	return 0;
@@ -1362,8 +1398,13 @@ static void asus_remove(struct hid_device *hdev)
 		cancel_work_sync(&drvdata->kbd_backlight->work);
 	}
 
-	if (drvdata->quirks & QUIRK_HID_FN_LOCK)
+	if (drvdata->quirks & QUIRK_HID_FN_LOCK) {
+		scoped_guard(mutex, &fnlock_hdev_lock) {
+			if (fnlock_hdev == hdev)
+				fnlock_hdev = NULL;
+		}
 		cancel_work_sync(&drvdata->fn_lock_sync_work);
+	}
 
 	hid_hw_stop(hdev);
 }
include/linux/platform_data/x86/asus-wmi.h +5 -0
index 554f41b827e1..20facd5da74e 100644
@@ -196,6 +196,7 @@ int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval);
 int asus_hid_register_listener(struct asus_hid_listener *cdev);
 void asus_hid_unregister_listener(struct asus_hid_listener *cdev);
 int asus_hid_event(enum asus_hid_event event);
+int asus_hid_fnlock_notify(bool enabled);
 #else
 static inline void set_ally_mcu_hack(enum asus_ally_mcu_hack status)
 {
@@ -227,6 +228,10 @@ static inline int asus_hid_event(enum asus_hid_event event)
 {
 	return -ENODEV;
 }
+static inline int asus_hid_fnlock_notify(bool enabled)
+{
+	return -ENODEV;
+}
 #endif
 
 #endif	/* __PLATFORM_DATA_X86_ASUS_WMI_H */
Signature · 1 line
2.54.0
#4 lore raw

Re: [PATCH v2 1/3] HID: asus: export asus_hid_fnlock_notify() for direct fn-lock control

Quoted 8 lines · Marcus Grenängen

+/**
+ * asus_hid_fnlock_notify() - Set fn-lock state directly via HID feature report.
+ * @enabled: true to lock fn (F1-F12 primary), false to unlock.
+ *
+ * Called by asus-armoury on platforms where the WMI DEVS path for fn-lock is
+ * non-functional (e.g. ASUS ProArt P16, N-Key keyboard product ID 0x19B6).
+ *
+ * Returns 0 on success, -ENODEV if no fn-lock capable HID device is present.

* Returns: ...

Quoted 3 lines

+ */

drivers/hid/hid-asus.c +2 -0
+int asus_hid_fnlock_notify(bool enabled)
+{
Signature · 1 line
~Randy
maintainer
#5 lore raw

Re: [PATCH v2 1/3] HID: asus: export asus_hid_fnlock_notify() for direct fn-lock control

In reply to Marcus Grenängen
Quoted 30 lines · Marcus Grenängen

Some ASUS platforms cannot control fn-lock via WMI DEVS and must send a HID feature report directly to the N-Key keyboard device instead.

Add a module-level fnlock_hdev pointer (protected by a mutex) that is set at probe time for devices with QUIRK_HID_FN_LOCK and cleared at remove. Export asus_hid_fnlock_notify(bool) so that asus-armoury can call into hid-asus without a circular dependency.

---
 drivers/hid/hid-asus.c                     | 43 +++++++++++++++++++++-
 include/linux/platform_data/x86/asus-wmi.h |  5 +++
 2 files changed, 47 insertions(+), 1 deletion(-)
drivers/hid/hid-asus.c +8 -0
index d34d74df3dc0..8a51dacf35eb 100644
@@ -584,6 +584,38 @@ static void asus_sync_fn_lock(struct work_struct *work)
 	asus_kbd_set_fn_lock(drvdata->hdev, drvdata->fn_lock);
 }
 
+/*
+ * Module-level reference to the HID device that handles fn-lock via feature
+ * report. Set at probe and cleared at remove for QUIRK_HID_FN_LOCK devices.
+ * Protected by fnlock_hdev_lock.
+ */
+static DEFINE_MUTEX(fnlock_hdev_lock);
+static struct hid_device *fnlock_hdev;
+

drivers/hid/hid-asus.c static void asus_sync_fn_lock(struct work_struct *work) · line 604

Quoted 10 lines
drivers/hid/hid-asus.c +10 -0
+/**
+ * asus_hid_fnlock_notify() - Set fn-lock state directly via HID feature report.
+ * @enabled: true to lock fn (F1-F12 primary), false to unlock.
+ *
+ * Called by asus-armoury on platforms where the WMI DEVS path for fn-lock is
+ * non-functional (e.g. ASUS ProArt P16, N-Key keyboard product ID 0x19B6).
+ *
+ * Returns 0 on success, -ENODEV if no fn-lock capable HID device is present.
+ */
+int asus_hid_fnlock_notify(bool enabled)

Generally I see _notify naming being used for internal kernel messaging or to notify the sysfs of a change, can you change the name of this to something like asus_hid_fnlock_enable() or asys_hid_fnlock_set()?

drivers/hid/hid-asus.c static void asus_sync_fn_lock(struct work_struct *work) · line 611

Quoted 7 lines
drivers/hid/hid-asus.c +7 -0
+{
+	int ret = -ENODEV;
+
+	guard(mutex)(&fnlock_hdev_lock);
+	if (fnlock_hdev) {
+		ret = asus_kbd_set_fn_lock(fnlock_hdev, enabled);
+		/* hid_hw_raw_request returns byte count on success; normalise to 0 */

The pattern I see most ofter used is

ret = operation()
if (ret < 0)
   return ret;

return 0;

IMHO much easier to read and doesn't need a comment to explain.

Quoted 66 lines
drivers/hid/hid-asus.c +17 -1
+		if (ret > 0)
+			ret = 0;
+	}
+	return ret;
+}
+EXPORT_SYMBOL_GPL(asus_hid_fnlock_notify);
+
 static void asus_schedule_work(struct asus_kbd_leds *led)
 {
 	unsigned long flags;
@@ -969,6 +1001,8 @@ static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
 		drvdata->fn_lock = true;
 		INIT_WORK(&drvdata->fn_lock_sync_work, asus_sync_fn_lock);
 		asus_kbd_set_fn_lock(hdev, true);
+		guard(mutex)(&fnlock_hdev_lock);
+		fnlock_hdev = hdev;
 	}
 
 	if (drvdata->tp) {
@@ -1008,6 +1042,8 @@ static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
 		drvdata->fn_lock = true;
 		INIT_WORK(&drvdata->fn_lock_sync_work, asus_sync_fn_lock);
 		asus_kbd_set_fn_lock(hdev, true);
+		guard(mutex)(&fnlock_hdev_lock);
+		fnlock_hdev = hdev;
 	}
 
 	return 0;
@@ -1362,8 +1398,13 @@ static void asus_remove(struct hid_device *hdev)
 		cancel_work_sync(&drvdata->kbd_backlight->work);
 	}
 
-	if (drvdata->quirks & QUIRK_HID_FN_LOCK)
+	if (drvdata->quirks & QUIRK_HID_FN_LOCK) {
+		scoped_guard(mutex, &fnlock_hdev_lock) {
+			if (fnlock_hdev == hdev)
+				fnlock_hdev = NULL;
+		}
 		cancel_work_sync(&drvdata->fn_lock_sync_work);
+	}
 
 	hid_hw_stop(hdev);
 }
include/linux/platform_data/x86/asus-wmi.h +5 -0
index 554f41b827e1..20facd5da74e 100644
@@ -196,6 +196,7 @@ int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval);
 int asus_hid_register_listener(struct asus_hid_listener *cdev);
 void asus_hid_unregister_listener(struct asus_hid_listener *cdev);
 int asus_hid_event(enum asus_hid_event event);
+int asus_hid_fnlock_notify(bool enabled);
 #else
 static inline void set_ally_mcu_hack(enum asus_ally_mcu_hack status)
 {
@@ -227,6 +228,10 @@ static inline int asus_hid_event(enum asus_hid_event event)
 {
 	return -ENODEV;
 }
+static inline int asus_hid_fnlock_notify(bool enabled)
+{
+	return -ENODEV;
+}
 #endif
 
 #endif	/* __PLATFORM_DATA_X86_ASUS_WMI_H */
#6 lore raw

v3 cover platform/x86: fix fn-lock on ASUS ProArt P16 (WMI DEVS no-op)

Changes since v2 (addressing Denis's and Randy's review):

Patch 1 (HID: asus):
- Renamed asus_hid_fnlock_notify() to asus_hid_fnlock_set() (Denis)
- Replaced "if (ret > 0) ret = 0" with "if (ret < 0) return ret; return 0;" pattern (Denis)
- Fixed Returns tag format to "Returns:" (Randy)
- Added #if IS_REACHABLE(CONFIG_HID_ASUS) guard for asus_hid_fnlock_set() declaration in asus-wmi.h so asus-wmi.c can call it without a missing prototype warning

Patch 2 (asus-nb-wmi):
- Added asus_wmi_fnlock_use_hid() export so asus-armoury can query the quirk flag without reading the quirk struct directly, keeping DMI and quirk knowledge inside asus-wmi (Denis)
- Fixed Returns tag format to "Returns:" (Randy)

Patch 3 (asus-armoury):
- Replaced the stored fnlock_use_hid flag and dmi_match() call with asus_wmi_fnlock_use_hid(), routing the DMI/quirk check through asus-wmi as Denis suggested
- Added fn_lock_registered bool to properly guard sysfs_remove_group in both the error unwind path and __exit, mirroring the gpu_mux/mini_led pattern (Denis)
- NOTE/Question: Since we have proper fn+esc hardware key handling working now we could eliminate this patch completely if we don't care about being able to control the fn state from user space
eg. asusctl and/or rog control center?
The nice thing of having it controllable via asusctl is the scripting possibilibilities like setting prefered mode when starting a DE as one example.

Regarding the if/else dispatch in fn_lock_current_value_store: Denis suggested routing everything through a single asus_wmi_fnlock_set() exported from asus-wmi. This was implemented but had to be reverted: it introduced a module dependency cycle (hid_asus -> asus_wmi -> hid_asus) that depmod detects and rejects. asus-armoury therefore retains the if/else, calling asus_hid_fnlock_set() on HID-path platforms and armoury_set_devstate() on WMI-path platforms. The asus-armoury -> hid-asus dependency is a soft one (the stub in asus-wmi.h returns -ENODEV when CONFIG_HID_ASUS is not reachable). But since I'm new to this maybe I'm missing something critical here?

Marcus Grenängen (3):
HID: asus: export asus_hid_fnlock_set() for direct fn-lock control platform/x86: asus-nb-wmi: add fnlock_use_hid quirk and asus_wmi_fnlock_use_hid() platform/x86: asus-armoury: add fn_lock firmware attribute

 drivers/hid/hid-asus.c                     | 44 ++++++++++++++++++-
 drivers/platform/x86/asus-armoury.c        | 70 ++++++++++++++++++++++++++++++
 drivers/platform/x86/asus-nb-wmi.c         | 13 ++++++
 drivers/platform/x86/asus-wmi.c            | 22 ++++++++++
 drivers/platform/x86/asus-wmi.h            |  5 +++
 include/linux/platform_data/x86/asus-wmi.h | 15 +++++++
 6 files changed, 168 insertions(+), 1 deletion(-)
Signature · 1 line
2.54.0
#7 lore raw

PATCH v3 1/3 HID: asus: export asus_hid_fnlock_set() for direct fn-lock control

Some ASUS platforms cannot control fn-lock via WMI DEVS and must send a HID feature report directly to the N-Key keyboard device instead.

Add a module-level fnlock_hdev pointer (protected by a mutex) that is set at probe time for devices with QUIRK_HID_FN_LOCK and cleared at remove. Export asus_hid_fnlock_set(bool) so that asus-armoury can call into hid-asus without a circular dependency.

---
 drivers/hid/hid-asus.c                     | 44 +++++++++++++++++++++-
 include/linux/platform_data/x86/asus-wmi.h | 15 ++++++++
 2 files changed, 58 insertions(+), 1 deletion(-)
drivers/hid/hid-asus.c +43 -1
index d34d74df3dc0..402ba9d5e982 100644
@@ -584,6 +584,39 @@ static void asus_sync_fn_lock(struct work_struct *work)
 	asus_kbd_set_fn_lock(drvdata->hdev, drvdata->fn_lock);
 }
 
+/*
+ * Module-level reference to the HID device that handles fn-lock via feature
+ * report. Set at probe and cleared at remove for QUIRK_HID_FN_LOCK devices.
+ * Protected by fnlock_hdev_lock.
+ */
+static DEFINE_MUTEX(fnlock_hdev_lock);
+static struct hid_device *fnlock_hdev;
+
+/**
+ * asus_hid_fnlock_set() - Set fn-lock state directly via HID feature report.
+ * @enabled: true to lock fn (F1-F12 primary), false to unlock.
+ *
+ * Called by asus-armoury on platforms where the WMI DEVS path for fn-lock is
+ * non-functional (e.g. ASUS ProArt P16, N-Key keyboard product ID 0x19B6).
+ *
+ * Returns: 0 on success, -ENODEV if no fn-lock capable HID device is present.
+ */
+int asus_hid_fnlock_set(bool enabled)
+{
+	int ret;
+
+	guard(mutex)(&fnlock_hdev_lock);
+	if (!fnlock_hdev)
+		return -ENODEV;
+
+	ret = asus_kbd_set_fn_lock(fnlock_hdev, enabled);
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(asus_hid_fnlock_set);
+
 static void asus_schedule_work(struct asus_kbd_leds *led)
 {
 	unsigned long flags;
@@ -969,6 +1002,8 @@ static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
 		drvdata->fn_lock = true;
 		INIT_WORK(&drvdata->fn_lock_sync_work, asus_sync_fn_lock);
 		asus_kbd_set_fn_lock(hdev, true);
+		guard(mutex)(&fnlock_hdev_lock);
+		fnlock_hdev = hdev;
 	}
 
 	if (drvdata->tp) {
@@ -1008,6 +1043,8 @@ static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
 		drvdata->fn_lock = true;
 		INIT_WORK(&drvdata->fn_lock_sync_work, asus_sync_fn_lock);
 		asus_kbd_set_fn_lock(hdev, true);
+		guard(mutex)(&fnlock_hdev_lock);
+		fnlock_hdev = hdev;
 	}
 
 	return 0;
@@ -1362,8 +1399,13 @@ static void asus_remove(struct hid_device *hdev)
 		cancel_work_sync(&drvdata->kbd_backlight->work);
 	}
 
-	if (drvdata->quirks & QUIRK_HID_FN_LOCK)
+	if (drvdata->quirks & QUIRK_HID_FN_LOCK) {
+		scoped_guard(mutex, &fnlock_hdev_lock) {
+			if (fnlock_hdev == hdev)
+				fnlock_hdev = NULL;
+		}
 		cancel_work_sync(&drvdata->fn_lock_sync_work);
+	}
 
 	hid_hw_stop(hdev);
 }
include/linux/platform_data/x86/asus-wmi.h +15 -0
index 554f41b827e1..a88bf03f9c4d 100644
@@ -187,6 +187,15 @@ enum asus_hid_event {
 
 #define ASUS_EV_MAX_BRIGHTNESS 3
 
+#if IS_REACHABLE(CONFIG_HID_ASUS)
+int asus_hid_fnlock_set(bool enabled);
+#else
+static inline int asus_hid_fnlock_set(bool enabled)
+{
+	return -ENODEV;
+}
+#endif
+
 #if IS_REACHABLE(CONFIG_ASUS_WMI)
 void set_ally_mcu_hack(enum asus_ally_mcu_hack status);
 void set_ally_mcu_powersave(bool enabled);
@@ -196,6 +205,7 @@ int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval);
 int asus_hid_register_listener(struct asus_hid_listener *cdev);
 void asus_hid_unregister_listener(struct asus_hid_listener *cdev);
 int asus_hid_event(enum asus_hid_event event);
+int asus_hid_fnlock_set(bool enabled);
 #else
 static inline void set_ally_mcu_hack(enum asus_ally_mcu_hack status)
 {
@@ -227,6 +237,11 @@ static inline int asus_hid_event(enum asus_hid_event event)
 {
 	return -ENODEV;
 }
+
+static inline int asus_hid_fnlock_set(bool enabled)
+{
+	return -ENODEV;
+}
 #endif
 
 #endif	/* __PLATFORM_DATA_X86_ASUS_WMI_H */
Signature · 1 line
2.54.0
maintainer
#8 lore raw

Re: [PATCH v3 1/3] HID: asus: export asus_hid_fnlock_set() for direct fn-lock control

Quoted 68 lines · Marcus Grenängen

Some ASUS platforms cannot control fn-lock via WMI DEVS and must send a HID feature report directly to the N-Key keyboard device instead.

Add a module-level fnlock_hdev pointer (protected by a mutex) that is set at probe time for devices with QUIRK_HID_FN_LOCK and cleared at remove. Export asus_hid_fnlock_set(bool) so that asus-armoury can call into hid-asus without a circular dependency.

---
 drivers/hid/hid-asus.c                     | 44 +++++++++++++++++++++-
 include/linux/platform_data/x86/asus-wmi.h | 15 ++++++++
 2 files changed, 58 insertions(+), 1 deletion(-)
drivers/hid/hid-asus.c +35 -0
index d34d74df3dc0..402ba9d5e982 100644
@@ -584,6 +584,39 @@ static void asus_sync_fn_lock(struct work_struct *work)
 	asus_kbd_set_fn_lock(drvdata->hdev, drvdata->fn_lock);
 }
 
+/*
+ * Module-level reference to the HID device that handles fn-lock via feature
+ * report. Set at probe and cleared at remove for QUIRK_HID_FN_LOCK devices.
+ * Protected by fnlock_hdev_lock.
+ */
+static DEFINE_MUTEX(fnlock_hdev_lock);
+static struct hid_device *fnlock_hdev;
+
+/**
+ * asus_hid_fnlock_set() - Set fn-lock state directly via HID feature report.
+ * @enabled: true to lock fn (F1-F12 primary), false to unlock.
+ *
+ * Called by asus-armoury on platforms where the WMI DEVS path for fn-lock is
+ * non-functional (e.g. ASUS ProArt P16, N-Key keyboard product ID 0x19B6).
+ *
+ * Returns: 0 on success, -ENODEV if no fn-lock capable HID device is present.
+ */
+int asus_hid_fnlock_set(bool enabled)
+{
+	int ret;
+
+	guard(mutex)(&fnlock_hdev_lock);
+	if (!fnlock_hdev)
+		return -ENODEV;
+
+	ret = asus_kbd_set_fn_lock(fnlock_hdev, enabled);
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(asus_hid_fnlock_set);
+
 static void asus_schedule_work(struct asus_kbd_leds *led)
 {
 	unsigned long flags;
@@ -969,6 +1002,8 @@ static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
 		drvdata->fn_lock = true;
 		INIT_WORK(&drvdata->fn_lock_sync_work, asus_sync_fn_lock);
 		asus_kbd_set_fn_lock(hdev, true);
+		guard(mutex)(&fnlock_hdev_lock);
+		fnlock_hdev = hdev;
 	}
 
 	if (drvdata->tp) {

drivers/hid/hid-asus.c static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi) · line 1047

Quoted 5 lines
drivers/hid/hid-asus.c +2 -0
@@ -1008,6 +1043,8 @@ static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
 		drvdata->fn_lock = true;
 		INIT_WORK(&drvdata->fn_lock_sync_work, asus_sync_fn_lock);
 		asus_kbd_set_fn_lock(hdev, true);
+		guard(mutex)(&fnlock_hdev_lock);
+		fnlock_hdev = hdev;

What's going on with this patch? Why this context appears more than once on different lines???

I even wasted my time on checking what hid tree has queued, and came up zero explanation (as expected).

--
i.

maintainer
#9 lore raw

Re: [PATCH v3 1/3] HID: asus: export asus_hid_fnlock_set() for direct fn-lock control

Quoted 68 lines · Ilpo Järvinen
Quoted 66 lines · Marcus Grenängen

Some ASUS platforms cannot control fn-lock via WMI DEVS and must send a HID feature report directly to the N-Key keyboard device instead.

Add a module-level fnlock_hdev pointer (protected by a mutex) that is set at probe time for devices with QUIRK_HID_FN_LOCK and cleared at remove. Export asus_hid_fnlock_set(bool) so that asus-armoury can call into hid-asus without a circular dependency.

---
 drivers/hid/hid-asus.c                     | 44 +++++++++++++++++++++-
 include/linux/platform_data/x86/asus-wmi.h | 15 ++++++++
 2 files changed, 58 insertions(+), 1 deletion(-)
drivers/hid/hid-asus.c +35 -0
index d34d74df3dc0..402ba9d5e982 100644
@@ -584,6 +584,39 @@ static void asus_sync_fn_lock(struct work_struct *work)
 	asus_kbd_set_fn_lock(drvdata->hdev, drvdata->fn_lock);
 }
 
+/*
+ * Module-level reference to the HID device that handles fn-lock via feature
+ * report. Set at probe and cleared at remove for QUIRK_HID_FN_LOCK devices.
+ * Protected by fnlock_hdev_lock.
+ */
+static DEFINE_MUTEX(fnlock_hdev_lock);
+static struct hid_device *fnlock_hdev;
+
+/**
+ * asus_hid_fnlock_set() - Set fn-lock state directly via HID feature report.
+ * @enabled: true to lock fn (F1-F12 primary), false to unlock.
+ *
+ * Called by asus-armoury on platforms where the WMI DEVS path for fn-lock is
+ * non-functional (e.g. ASUS ProArt P16, N-Key keyboard product ID 0x19B6).
+ *
+ * Returns: 0 on success, -ENODEV if no fn-lock capable HID device is present.
+ */
+int asus_hid_fnlock_set(bool enabled)
+{
+	int ret;
+
+	guard(mutex)(&fnlock_hdev_lock);
+	if (!fnlock_hdev)
+		return -ENODEV;
+
+	ret = asus_kbd_set_fn_lock(fnlock_hdev, enabled);
+	if (ret < 0)
+		return ret;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(asus_hid_fnlock_set);
+
 static void asus_schedule_work(struct asus_kbd_leds *led)
 {
 	unsigned long flags;
@@ -969,6 +1002,8 @@ static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
 		drvdata->fn_lock = true;
 		INIT_WORK(&drvdata->fn_lock_sync_work, asus_sync_fn_lock);
 		asus_kbd_set_fn_lock(hdev, true);
+		guard(mutex)(&fnlock_hdev_lock);
+		fnlock_hdev = hdev;
 	}
 
Quoted 9 lines

drivers/hid/hid-asus.c static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi) · line 1047

Quoted 7 lines
drivers/hid/hid-asus.c +2 -0
 	if (drvdata->tp) {
@@ -1008,6 +1043,8 @@ static int asus_input_configured(struct hid_device *hdev, struct hid_input *hi)
 		drvdata->fn_lock = true;
 		INIT_WORK(&drvdata->fn_lock_sync_work, asus_sync_fn_lock);
 		asus_kbd_set_fn_lock(hdev, true);
+		guard(mutex)(&fnlock_hdev_lock);
+		fnlock_hdev = hdev;

What's going on with this patch? Why this context appears more than once on different lines???

Hi Ilpo,

I think this patch has been taken by HID already as I had to resolve another issue with it:
mutex usage in atomic context; you can read out more here:
https://lore.kernel.org/all/[email protected]/

The main idea as I understood it is that to activate the fn key, do things, and then deactivate it: the problem in this logic comes from the fact that using true both time is very likely an oversight and it should have been true -> false.

I requested clarification too but still haven't got any response yet.

Let me know if I can do something.

Thanks,
Denis

Quoted 5 lines

I even wasted my time on checking what hid tree has queued, and came up zero explanation (as expected).

--
i.

#10 lore raw

Re: [PATCH v3 1/3] HID: asus: export asus_hid_fnlock_set() for direct fn-lock control

In reply to Ilpo Järvinen
#11 lore raw

PATCH v3 2/3 platform/x86: asus-nb-wmi: add fnlock_use_hid quirk and asus_wmi_fnlock_use_hid()

In reply to Marcus Grenängen

The ASUS ProArt P16 (N-Key keyboard 0B05:19B6) advertises the WMI fn-lock DEVID (0x00100023) as present via DSTS, but the DEVS call has no effect. Fn-lock must instead be toggled via a HID feature report sent to the N-Key keyboard (handled by hid-asus).

Add a fnlock_use_hid flag to struct quirk_entry and set it for the ProArt P16 via a DMI match on DMI_PRODUCT_FAMILY.

Export asus_wmi_fnlock_use_hid() so that asus-armoury can query whether the HID path is required without reading the quirk struct directly. This keeps the DMI and quirk knowledge inside asus-wmi.

---
 drivers/platform/x86/asus-nb-wmi.c         | 13 +++++++++++++
 drivers/platform/x86/asus-wmi.c            | 22 ++++++++++++++++++++++
 drivers/platform/x86/asus-wmi.h            |  5 +++++
 include/linux/platform_data/x86/asus-wmi.h |  6 +++---
 4 files changed, 43 insertions(+), 3 deletions(-)
drivers/platform/x86/asus-nb-wmi.c +13 -0
index b4677c5bba5b..44e4cf68ff70 100644
@@ -155,6 +155,10 @@ static struct quirk_entry quirk_asus_z13 = {
 	.tablet_switch_mode = asus_wmi_kbd_dock_devid,
 };
 
+static struct quirk_entry quirk_asus_proart_p16 = {
+	.fnlock_use_hid = true,
+};
+
 static int dmi_matched(const struct dmi_system_id *dmi)
 {
 	pr_info("Identified laptop model '%s'\n", dmi->ident);
@@ -553,6 +557,15 @@ static const struct dmi_system_id asus_quirks[] = {
 		},
 		.driver_data = &quirk_asus_z13,
 	},
+	{
+		.callback = dmi_matched,
+		.ident = "ASUS ProArt P16",
+		.matches = {
+			DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
+			DMI_MATCH(DMI_PRODUCT_FAMILY, "ProArt P16"),
+		},
+		.driver_data = &quirk_asus_proart_p16,
+	},
 	{},
 };
 
drivers/platform/x86/asus-wmi.c +22 -0
index 80144c412b90..d4d742b9983d 100644
@@ -1759,6 +1759,28 @@ int asus_hid_event(enum asus_hid_event event)
 }
 EXPORT_SYMBOL_GPL(asus_hid_event);
 
+/**
+ * asus_wmi_fnlock_use_hid() - Return true if fn-lock must use the HID path.
+ *
+ * On some platforms (e.g. ASUS ProArt P16) the WMI DEVS call for fn-lock is
+ * silently a no-op. The fnlock_use_hid quirk flag marks these platforms so
+ * that callers can select the HID feature-report path instead.
+ *
+ * Returns: true if the HID path should be used, false otherwise.
+ */
+bool asus_wmi_fnlock_use_hid(void)
+{
+	struct asus_wmi *asus;
+
+	guard(spinlock_irqsave)(&asus_ref.lock);
+	asus = asus_ref.asus;
+	if (!asus)
+		return false;
+
+	return asus->driver->quirks->fnlock_use_hid;
+}
+EXPORT_SYMBOL_NS_GPL(asus_wmi_fnlock_use_hid, "ASUS_WMI");
+
 /*
  * These functions actually update the LED's, and are called from a
  * workqueue. By doing this as separate work rather than when the LED
drivers/platform/x86/asus-wmi.h +5 -0
index 5cd4392b964e..6c50b11860e8 100644
@@ -52,6 +52,11 @@ struct quirk_entry {
 	 */
 	int no_display_toggle;
 	u32 xusb2pr;
+	/*
+	 * Some platforms report WMI DEVID_FNLOCK as present but the DEVS call
+	 * is a no-op. Force the HID feature report path via hid-asus instead.
+	 */
+	bool fnlock_use_hid;
 };
 
 struct asus_wmi_driver {
include/linux/platform_data/x86/asus-wmi.h +3 -3
index a88bf03f9c4d..199179266363 100644
@@ -205,7 +205,7 @@ int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval);
 int asus_hid_register_listener(struct asus_hid_listener *cdev);
 void asus_hid_unregister_listener(struct asus_hid_listener *cdev);
 int asus_hid_event(enum asus_hid_event event);
-int asus_hid_fnlock_set(bool enabled);
+bool asus_wmi_fnlock_use_hid(void);
 #else
 static inline void set_ally_mcu_hack(enum asus_ally_mcu_hack status)
 {
@@ -238,9 +238,9 @@ static inline int asus_hid_event(enum asus_hid_event event)
 	return -ENODEV;
 }
 
-static inline int asus_hid_fnlock_set(bool enabled)
+static inline bool asus_wmi_fnlock_use_hid(void)
 {
-	return -ENODEV;
+	return false;
 }
 #endif
 
Signature · 1 line
2.54.0
#12 lore raw

PATCH v3 3/3 platform/x86: asus-armoury: add fn_lock firmware attribute

In reply to Marcus Grenängen

Add a fn_lock attribute to the asus-armoury firmware-attributes interface, allowing userspace to read and set the Fn-lock state (whether F1-F12 keys are primary or media/system keys are primary).

On most ASUS laptops fn-lock is backed by WMI DEVID 0x00100023. On platforms where that DEVS call is a no-op (fnlock_use_hid quirk), the store path dispatches via asus_wmi_fnlock_set(), which selects the HID feature-report path internally. The show path returns -EOPNOTSUPP on such platforms as the hardware provides no readback.

The attribute is only registered when the platform actually supports fn-lock, either via the HID quirk (asus_wmi_fnlock_use_hid()) or a functional WMI DEVID (armoury_has_devstate(ASUS_WMI_DEVID_FNLOCK)). Registration state is tracked in fn_lock_registered so that removal in the exit path is symmetric.

---
 drivers/platform/x86/asus-armoury.c | 70 +++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)
drivers/platform/x86/asus-armoury.c +70 -0
index 5b0987ccc270..fb8ad3b14aad 100644
@@ -93,6 +93,7 @@ struct asus_armoury_priv {
 
 	u32 mini_led_dev_id;
 	u32 gpu_mux_dev_id;
+	bool fn_lock_registered;
 };
 
 static struct asus_armoury_priv asus_armoury = {
@@ -778,6 +779,58 @@ ASUS_ATTR_GROUP_ROG_TUNABLE(nv_tgp, "nv_tgp", ASUS_WMI_DEVID_DGPU_SET_TGP,
 ASUS_ATTR_GROUP_INT_VALUE_ONLY_RO(nv_base_tgp, ATTR_NV_BASE_TGP, ASUS_WMI_DEVID_DGPU_BASE_TGP,
 				  "Read the base TGP value");
 
+/*
+ * fn_lock: toggle whether Fn key is locked (F1-F12 primary) or unlocked
+ * (media/system keys primary).
+ *
+ * On most ASUS laptops this is backed by WMI DEVID 0x00100023. On some
+ * platforms (e.g. ProArt P16) that DEVS call is a no-op and the state must
+ * be sent as a HID feature report to the N-Key keyboard via hid-asus.
+ */
+static ssize_t fn_lock_current_value_show(struct kobject *kobj,
+					  struct kobj_attribute *attr, char *buf)
+{
+	u32 result;
+	int err;
+
+	if (asus_wmi_fnlock_use_hid())
+		return -EOPNOTSUPP;
+
+	err = armoury_get_devstate(attr, &result, ASUS_WMI_DEVID_FNLOCK);
+	if (err)
+		return err;
+
+	return sysfs_emit(buf, "%u\n", result & 1);
+}
+
+static ssize_t fn_lock_current_value_store(struct kobject *kobj,
+					   struct kobj_attribute *attr,
+					   const char *buf, size_t count)
+{
+	bool enable;
+	int err;
+
+	err = kstrtobool(buf, &enable);
+	if (err)
+		return err;
+
+	if (asus_wmi_fnlock_use_hid()) {
+		err = asus_hid_fnlock_set(enable);
+		if (err)
+			return err;
+	} else {
+		err = armoury_set_devstate(attr, enable ? 1 : 0, NULL,
+					   ASUS_WMI_DEVID_FNLOCK);
+		if (err)
+			return err;
+	}
+
+	sysfs_notify(kobj, NULL, attr->attr.name);
+	return count;
+}
+
+ASUS_ATTR_GROUP_BOOL(fn_lock, "fn_lock", "Set the Fn-lock state");
+
 /* If an attribute does not require any special case handling add it here */
 static const struct asus_attr_group armoury_attr_groups[] = {
 	{ &egpu_connected_attr_group, ASUS_WMI_DEVID_EGPU_CONNECTED },
@@ -926,6 +979,17 @@ static int asus_fw_attr_add(void)
 		}
 	}
 
+	if (asus_wmi_fnlock_use_hid() ||
+	    armoury_has_devstate(ASUS_WMI_DEVID_FNLOCK)) {
+		err = sysfs_create_group(&asus_armoury.fw_attr_kset->kobj,
+					 &fn_lock_attr_group);
+		if (err) {
+			pr_err("Failed to create sysfs-group for fn_lock\n");
+			goto err_remove_gpu_mux_group;
+		}
+		asus_armoury.fn_lock_registered = true;
+	}
+
 	for (i = 0; i < ARRAY_SIZE(armoury_attr_groups); i++) {
 		if (!armoury_has_devstate(armoury_attr_groups[i].wmi_devid))
 			continue;
@@ -963,6 +1027,9 @@ static int asus_fw_attr_add(void)
 			sysfs_remove_group(&asus_armoury.fw_attr_kset->kobj,
 					   armoury_attr_groups[i].attr_group);
 	}
+	if (asus_armoury.fn_lock_registered)
+		sysfs_remove_group(&asus_armoury.fw_attr_kset->kobj, &fn_lock_attr_group);
+err_remove_gpu_mux_group:
 	if (asus_armoury.gpu_mux_dev_id)
 		sysfs_remove_group(&asus_armoury.fw_attr_kset->kobj, &gpu_mux_mode_attr_group);
 err_remove_mini_led_group:
@@ -1138,6 +1205,9 @@ static void __exit asus_fw_exit(void)
 	if (asus_armoury.gpu_mux_dev_id)
 		sysfs_remove_group(&asus_armoury.fw_attr_kset->kobj, &gpu_mux_mode_attr_group);
 
+	if (asus_armoury.fn_lock_registered)
+		sysfs_remove_group(&asus_armoury.fw_attr_kset->kobj, &fn_lock_attr_group);
+
 	if (asus_armoury.mini_led_dev_id)
 		sysfs_remove_group(&asus_armoury.fw_attr_kset->kobj, &mini_led_mode_attr_group);
 
Signature · 1 line
2.54.0
#13 lore raw

PATCH v2 2/3 platform/x86: asus-nb-wmi: add fnlock_use_hid quirk for ProArt P16

In reply to Marcus Grenängen

The ASUS ProArt P16 (N-Key keyboard 0B05:19B6) advertises the WMI fn-lock DEVID (0x00100023) as present via DSTS, but the DEVS call has no effect. Fn-lock must instead be toggled via a HID feature report sent to the N-Key keyboard (handled by hid-asus).

Add a fnlock_use_hid flag to struct quirk_entry and set it for the ProArt P16 via a DMI match on DMI_PRODUCT_FAMILY. This flag is consumed by asus-armoury to select the HID path instead of WMI DEVS.

---
 drivers/platform/x86/asus-nb-wmi.c | 13 +++++++++++++
 drivers/platform/x86/asus-wmi.h    |  5 +++++
 2 files changed, 18 insertions(+)
drivers/platform/x86/asus-nb-wmi.c +13 -0
index b4677c5bba5b..44e4cf68ff70 100644
@@ -155,6 +155,10 @@ static struct quirk_entry quirk_asus_z13 = {
 	.tablet_switch_mode = asus_wmi_kbd_dock_devid,
 };
 
+static struct quirk_entry quirk_asus_proart_p16 = {
+	.fnlock_use_hid = true,
+};
+
 static int dmi_matched(const struct dmi_system_id *dmi)
 {
 	pr_info("Identified laptop model '%s'\n", dmi->ident);
@@ -553,6 +557,15 @@ static const struct dmi_system_id asus_quirks[] = {
 		},
 		.driver_data = &quirk_asus_z13,
 	},
+	{
+		.callback = dmi_matched,
+		.ident = "ASUS ProArt P16",
+		.matches = {
+			DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
+			DMI_MATCH(DMI_PRODUCT_FAMILY, "ProArt P16"),
+		},
+		.driver_data = &quirk_asus_proart_p16,
+	},
 	{},
 };
 
drivers/platform/x86/asus-wmi.h +5 -0
index 5cd4392b964e..6c50b11860e8 100644
@@ -52,6 +52,11 @@ struct quirk_entry {
 	 */
 	int no_display_toggle;
 	u32 xusb2pr;
+	/*
+	 * Some platforms report WMI DEVID_FNLOCK as present but the DEVS call
+	 * is a no-op. Force the HID feature report path via hid-asus instead.
+	 */
+	bool fnlock_use_hid;
 };
 
 struct asus_wmi_driver {
Signature · 1 line
2.54.0
#14 lore raw

PATCH v2 3/3 platform/x86: asus-armoury: add fn_lock firmware attribute

In reply to Marcus Grenängen

Add a fn_lock attribute to the asus-armoury firmware-attributes interface, allowing userspace to read and set the Fn-lock state (whether F1-F12 keys are primary or media/system keys are primary).

On most ASUS laptops fn-lock is backed by WMI DEVID 0x00100023 and the attribute uses armoury_get/set_devstate() as normal. On platforms where the WMI DEVS call is a no-op (fnlock_use_hid quirk, e.g. ProArt P16), the store path calls asus_hid_fnlock_notify() to send the feature report directly to the N-Key keyboard via hid-asus. The show path returns -EOPNOTSUPP on such platforms as the hardware provides no readback.

The fnlock_use_hid flag is detected at init time via dmi_match() on DMI_PRODUCT_FAMILY. A direct DMI check is used rather than reading the asus-nb-wmi quirk flag because asus-armoury and asus-nb-wmi are both loadable modules at the same init level, so the asus_ref pointer set by asus-wmi may not yet be valid when asus-armoury initialises.

---
 drivers/platform/x86/asus-armoury.c | 69 +++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)
drivers/platform/x86/asus-armoury.c +69 -0
index 5b0987ccc270..9d7646eff944 100644
@@ -93,6 +93,7 @@ struct asus_armoury_priv {
 
 	u32 mini_led_dev_id;
 	u32 gpu_mux_dev_id;
+	bool fnlock_use_hid;
 };
 
 static struct asus_armoury_priv asus_armoury = {
@@ -778,6 +779,58 @@ ASUS_ATTR_GROUP_ROG_TUNABLE(nv_tgp, "nv_tgp", ASUS_WMI_DEVID_DGPU_SET_TGP,
 ASUS_ATTR_GROUP_INT_VALUE_ONLY_RO(nv_base_tgp, ATTR_NV_BASE_TGP, ASUS_WMI_DEVID_DGPU_BASE_TGP,
 				  "Read the base TGP value");
 
+/*
+ * fn_lock: toggle whether Fn key is locked (F1-F12 primary) or unlocked
+ * (media/system keys primary).
+ *
+ * On most ASUS laptops this is backed by WMI DEVID 0x00100023. On some
+ * platforms (e.g. ProArt P16) that DEVS call is a no-op and the state must
+ * be sent as a HID feature report to the N-Key keyboard via hid-asus.
+ */
+static ssize_t fn_lock_current_value_show(struct kobject *kobj,
+					  struct kobj_attribute *attr, char *buf)
+{
+	u32 result;
+	int err;
+
+	if (asus_armoury.fnlock_use_hid)
+		return -EOPNOTSUPP;
+
+	err = armoury_get_devstate(attr, &result, ASUS_WMI_DEVID_FNLOCK);
+	if (err)
+		return err;
+
+	return sysfs_emit(buf, "%u\n", result & 1);
+}
+
+static ssize_t fn_lock_current_value_store(struct kobject *kobj,
+					   struct kobj_attribute *attr,
+					   const char *buf, size_t count)
+{
+	bool enable;
+	int err;
+
+	err = kstrtobool(buf, &enable);
+	if (err)
+		return err;
+
+	if (asus_armoury.fnlock_use_hid) {
+		err = asus_hid_fnlock_notify(enable);
+		if (err)
+			return err;
+	} else {
+		err = armoury_set_devstate(attr, enable ? 1 : 0, NULL,
+					   ASUS_WMI_DEVID_FNLOCK);
+		if (err)
+			return err;
+	}
+
+	sysfs_notify(kobj, NULL, attr->attr.name);
+	return count;
+}
+
+ASUS_ATTR_GROUP_BOOL(fn_lock, "fn_lock", "Set the Fn-lock state");
+
 /* If an attribute does not require any special case handling add it here */
 static const struct asus_attr_group armoury_attr_groups[] = {
 	{ &egpu_connected_attr_group, ASUS_WMI_DEVID_EGPU_CONNECTED },
@@ -926,6 +979,16 @@ static int asus_fw_attr_add(void)
 		}
 	}
 
+	if (asus_armoury.fnlock_use_hid ||
+	    armoury_has_devstate(ASUS_WMI_DEVID_FNLOCK)) {
+		err = sysfs_create_group(&asus_armoury.fw_attr_kset->kobj,
+					 &fn_lock_attr_group);
+		if (err) {
+			pr_err("Failed to create sysfs-group for fn_lock\n");
+			goto err_remove_gpu_mux_group;
+		}
+	}
+
 	for (i = 0; i < ARRAY_SIZE(armoury_attr_groups); i++) {
 		if (!armoury_has_devstate(armoury_attr_groups[i].wmi_devid))
 			continue;
@@ -963,6 +1026,8 @@ static int asus_fw_attr_add(void)
 			sysfs_remove_group(&asus_armoury.fw_attr_kset->kobj,
 					   armoury_attr_groups[i].attr_group);
 	}
+	sysfs_remove_group(&asus_armoury.fw_attr_kset->kobj, &fn_lock_attr_group);
+err_remove_gpu_mux_group:
 	if (asus_armoury.gpu_mux_dev_id)
 		sysfs_remove_group(&asus_armoury.fw_attr_kset->kobj, &gpu_mux_mode_attr_group);
 err_remove_mini_led_group:
@@ -1121,6 +1186,8 @@ static int __init asus_fw_init(void)
 
 	init_rog_tunables();
 
+	asus_armoury.fnlock_use_hid = dmi_match(DMI_PRODUCT_FAMILY, "ProArt P16");
+
 	/* Must always be last step to ensure data is available */
 	return asus_fw_attr_add();
 }
@@ -1138,6 +1205,8 @@ static void __exit asus_fw_exit(void)
 	if (asus_armoury.gpu_mux_dev_id)
 		sysfs_remove_group(&asus_armoury.fw_attr_kset->kobj, &gpu_mux_mode_attr_group);
 
+	sysfs_remove_group(&asus_armoury.fw_attr_kset->kobj, &fn_lock_attr_group);
+
 	if (asus_armoury.mini_led_dev_id)
 		sysfs_remove_group(&asus_armoury.fw_attr_kset->kobj, &mini_led_mode_attr_group);
 
Signature · 1 line
2.54.0
maintainer
#15 lore raw

Re: [PATCH v2 3/3] platform/x86: asus-armoury: add fn_lock firmware attribute

Quoted 65 lines · Marcus Grenängen

Add a fn_lock attribute to the asus-armoury firmware-attributes interface, allowing userspace to read and set the Fn-lock state (whether F1-F12 keys are primary or media/system keys are primary).

On most ASUS laptops fn-lock is backed by WMI DEVID 0x00100023 and the attribute uses armoury_get/set_devstate() as normal. On platforms where the WMI DEVS call is a no-op (fnlock_use_hid quirk, e.g. ProArt P16), the store path calls asus_hid_fnlock_notify() to send the feature report directly to the N-Key keyboard via hid-asus. The show path returns -EOPNOTSUPP on such platforms as the hardware provides no readback.

The fnlock_use_hid flag is detected at init time via dmi_match() on DMI_PRODUCT_FAMILY. A direct DMI check is used rather than reading the asus-nb-wmi quirk flag because asus-armoury and asus-nb-wmi are both loadable modules at the same init level, so the asus_ref pointer set by asus-wmi may not yet be valid when asus-armoury initialises.

---
 drivers/platform/x86/asus-armoury.c | 69 +++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)
drivers/platform/x86/asus-armoury.c +28 -0
index 5b0987ccc270..9d7646eff944 100644
@@ -93,6 +93,7 @@ struct asus_armoury_priv {
 
 	u32 mini_led_dev_id;
 	u32 gpu_mux_dev_id;
+	bool fnlock_use_hid;
 };
 
 static struct asus_armoury_priv asus_armoury = {
@@ -778,6 +779,58 @@ ASUS_ATTR_GROUP_ROG_TUNABLE(nv_tgp, "nv_tgp", ASUS_WMI_DEVID_DGPU_SET_TGP,
 ASUS_ATTR_GROUP_INT_VALUE_ONLY_RO(nv_base_tgp, ATTR_NV_BASE_TGP, ASUS_WMI_DEVID_DGPU_BASE_TGP,
 				  "Read the base TGP value");
 
+/*
+ * fn_lock: toggle whether Fn key is locked (F1-F12 primary) or unlocked
+ * (media/system keys primary).
+ *
+ * On most ASUS laptops this is backed by WMI DEVID 0x00100023. On some
+ * platforms (e.g. ProArt P16) that DEVS call is a no-op and the state must
+ * be sent as a HID feature report to the N-Key keyboard via hid-asus.
+ */
+static ssize_t fn_lock_current_value_show(struct kobject *kobj,
+					  struct kobj_attribute *attr, char *buf)
+{
+	u32 result;
+	int err;
+
+	if (asus_armoury.fnlock_use_hid)
+		return -EOPNOTSUPP;
+
+	err = armoury_get_devstate(attr, &result, ASUS_WMI_DEVID_FNLOCK);
+	if (err)
+		return err;
+
+	return sysfs_emit(buf, "%u\n", result & 1);
+}
+
+static ssize_t fn_lock_current_value_store(struct kobject *kobj,
+					   struct kobj_attribute *attr,
+					   const char *buf, size_t count)

drivers/platform/x86/asus-armoury.c ASUS_ATTR_GROUP_ROG_TUNABLE(nv_tgp, "nv_tgp", ASUS_WMI_DEVID_DGPU_SET_TGP, · line 818

Quoted 10 lines
drivers/platform/x86/asus-armoury.c +10 -0
+{
+	bool enable;
+	int err;
+
+	err = kstrtobool(buf, &enable);
+	if (err)
+		return err;
+
+	if (asus_armoury.fnlock_use_hid) {
+		err = asus_hid_fnlock_notify(enable);

Doing this would introduce a dependency from asus-armoury to hid-asus, let's not do that.

Instead only show this attribute if it's actually doing something, you can check it from asus-wmi: asus-armoury already depends on it.

Edit: you are actually already registering this only if fnlock_use_hid is true, so the else looks dead code to me.

I think there is something not right here.

Quoted 45 lines
drivers/platform/x86/asus-armoury.c +27 -0
+		if (err)
+			return err;
+	} else {
+		err = armoury_set_devstate(attr, enable ? 1 : 0, NULL,
+					   ASUS_WMI_DEVID_FNLOCK);
+		if (err)
+			return err;
+	}
+
+	sysfs_notify(kobj, NULL, attr->attr.name);
+	return count;
+}
+
+ASUS_ATTR_GROUP_BOOL(fn_lock, "fn_lock", "Set the Fn-lock state");
+
 /* If an attribute does not require any special case handling add it here */
 static const struct asus_attr_group armoury_attr_groups[] = {
 	{ &egpu_connected_attr_group, ASUS_WMI_DEVID_EGPU_CONNECTED },
@@ -926,6 +979,16 @@ static int asus_fw_attr_add(void)
 		}
 	}
 
+	if (asus_armoury.fnlock_use_hid ||
+	    armoury_has_devstate(ASUS_WMI_DEVID_FNLOCK)) {
+		err = sysfs_create_group(&asus_armoury.fw_attr_kset->kobj,
+					 &fn_lock_attr_group);
+		if (err) {
+			pr_err("Failed to create sysfs-group for fn_lock\n");
+			goto err_remove_gpu_mux_group;
+		}
+	}
+
 	for (i = 0; i < ARRAY_SIZE(armoury_attr_groups); i++) {
 		if (!armoury_has_devstate(armoury_attr_groups[i].wmi_devid))
 			continue;
@@ -963,6 +1026,8 @@ static int asus_fw_attr_add(void)
 			sysfs_remove_group(&asus_armoury.fw_attr_kset->kobj,
 					   armoury_attr_groups[i].attr_group);
 	}
+	sysfs_remove_group(&asus_armoury.fw_attr_kset->kobj, &fn_lock_attr_group);
+err_remove_gpu_mux_group:
 	if (asus_armoury.gpu_mux_dev_id)
 		sysfs_remove_group(&asus_armoury.fw_attr_kset->kobj, &gpu_mux_mode_attr_group);
 err_remove_mini_led_group:

drivers/platform/x86/asus-armoury.c static int __init asus_fw_init(void) · line 1189

Quoted 4 lines
drivers/platform/x86/asus-armoury.c +1 -0
@@ -1121,6 +1186,8 @@ static int __init asus_fw_init(void)
 
 	init_rog_tunables();
 
+	asus_armoury.fnlock_use_hid = dmi_match(DMI_PRODUCT_FAMILY, "ProArt P16");

Perhaps you can reuse something from asus-wmi instead of re-doing the dmi_match again in this driver.

drivers/platform/x86/asus-armoury.c static void __exit asus_fw_exit(void) · line 1208

Quoted 9 lines
drivers/platform/x86/asus-armoury.c +2 -0
+
 	/* Must always be last step to ensure data is available */
 	return asus_fw_attr_add();
 }
@@ -1138,6 +1205,8 @@ static void __exit asus_fw_exit(void)
 	if (asus_armoury.gpu_mux_dev_id)
 		sysfs_remove_group(&asus_armoury.fw_attr_kset->kobj, &gpu_mux_mode_attr_group);
 
+	sysfs_remove_group(&asus_armoury.fw_attr_kset->kobj, &fn_lock_attr_group);

Not guarded the same way as the sysfs_create_group therefore will trigger on hardware that doesn't need this.

Quoted 4 lines
drivers/platform/x86/asus-armoury.c +1 -0
+
 	if (asus_armoury.mini_led_dev_id)
 		sysfs_remove_group(&asus_armoury.fw_attr_kset->kobj, &mini_led_mode_attr_group);