Skip to contentSkip to conversation

platform/x86: asus-wmi: fix camera key led on Zenbook S14

PATCH In review José Guilherme de Castro Rodrigues 2 participants 6 messages Apr 9 – May 11

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

PATCH v2 1/1 platform/x86: asus-wmi: fix camera key led on Zenbook S14

Fix key led not turning on/off when the camera key is pressed. The asus_wmi_dev_is_present(ASUS_WMI_DEVID_CAMERA_LED) check fails on Zenbook S14, and no sysfs attribute for the camera key led is created.

The check succeeds for ASUS_WMI_DEVID_CAMERA_LED_NEG, though, and when it is read, the led state is synchronized. By doing a read after every key press, the led is guaranteed to stay in sync with the actual camera state.

This change is not visible to userspace, as no sysfs attribute for ASUS_WMI_DEVID_CAMERA_LED_NEG exists.

Tested on an ASUS Zenbook S14 with BIOS version UX5406SA.309.

---
Changes in v2:
- Do not return after triggering the update.
- Use tabs for indentation on new defines.

---
 drivers/platform/x86/asus-wmi.c            | 19 +++++++++++++++++++
 include/linux/platform_data/x86/asus-wmi.h |  2 +-
 2 files changed, 20 insertions(+), 1 deletion(-)
drivers/platform/x86/asus-wmi.c +19 -0
index 80144c412b90..32d0d4ce60ff 100644
@@ -70,6 +70,8 @@ module_param(fnlock_default, bool, 0444);
 #define NOTIFY_KBD_TTP			0xae
 #define NOTIFY_LID_FLIP			0xfa
 #define NOTIFY_LID_FLIP_ROG		0xbd
+#define NOTIFY_CAMERA_TOGGLE_1		0x82
+#define NOTIFY_CAMERA_TOGGLE_2		0x85
 
 #define ASUS_WMI_FNLOCK_BIOS_DISABLED	BIT(0)
 
@@ -307,6 +309,7 @@ struct asus_wmi {
 	u32 kbd_rgb_dev;
 	bool kbd_rgb_state_available;
 	bool oobe_state_available;
+	bool camera_neg_led_available;
 
 	u8 throttle_thermal_policy_mode;
 	u32 throttle_thermal_policy_dev;
@@ -1982,6 +1985,18 @@ static int micmute_led_set(struct led_classdev *led_cdev,
 	return err < 0 ? err : 0;
 }
 
+static bool is_camera_toggle(int code)
+{
+	return code == NOTIFY_CAMERA_TOGGLE_1 || code == NOTIFY_CAMERA_TOGGLE_2;
+}
+
+static void camera_neg_led_trigger_update(struct asus_wmi *asus)
+{
+	u32 result;
+
+	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CAMERA_LED_NEG, &result);
+}
+
 static enum led_brightness camera_led_get(struct led_classdev *led_cdev)
 {
 	struct asus_wmi *asus;
@@ -4587,6 +4602,9 @@ static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
 	if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle)
 		return;
 
+	if (is_camera_toggle(code) && asus->camera_neg_led_available)
+		camera_neg_led_trigger_update(asus);
+
 	if (!sparse_keymap_report_event(asus->inputdev, code,
 					key_value, autorelease))
 		pr_info("Unknown key code 0x%x\n", code);
@@ -5062,6 +5080,7 @@ static int asus_wmi_add(struct platform_device *pdev)
 #endif /* IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS) */
 
 	asus->oobe_state_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_OOBE);
+	asus->camera_neg_led_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CAMERA_LED_NEG);
 
 	if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY))
 		asus->throttle_thermal_policy_dev = ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY;
include/linux/platform_data/x86/asus-wmi.h +1 -1
index 554f41b827e1..1a9349da6069 100644
@@ -56,7 +56,7 @@
 #define ASUS_WMI_DEVID_MICMUTE_LED		0x00040017
 
 /* Disable Camera LED */
-#define ASUS_WMI_DEVID_CAMERA_LED_NEG	0x00060078 /* 0 = on (unused) */
+#define ASUS_WMI_DEVID_CAMERA_LED_NEG	0x00060078 /* 0 = on */
 #define ASUS_WMI_DEVID_CAMERA_LED	0x00060079 /* 1 = on */
 
 /* Backlight and Brightness */
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 5 lines · José Guilherme de Castro Rodrigues

Fix key led not turning on/off when the camera key is pressed. The asus_wmi_dev_is_present(ASUS_WMI_DEVID_CAMERA_LED) check fails on Zenbook S14, and no sysfs attribute for the camera key led is created.

Quoted 7 lines

The check succeeds for ASUS_WMI_DEVID_CAMERA_LED_NEG, though, and when it is read, the led state is synchronized. By doing a read after every key press, the led is guaranteed to stay in sync with the actual camera state.

This change is not visible to userspace, as no sysfs attribute for ASUS_WMI_DEVID_CAMERA_LED_NEG exists.

While I believe you are "correct" that userspace won't see a thing, is this really correct way to implement support for this device?

I mean the camera led init in asus_wmi_led_init() is gated by asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CAMERA_LED) but should that be gated by either CAMERA_LED or CAMERA_LED_NEG and the related code in *_camera_led_*() that now only uses ASUS_WMI_DEVID_CAMERA_LED should use which ever of them is available?

Quoted 84 lines

Tested on an ASUS Zenbook S14 with BIOS version UX5406SA.309.

---
Changes in v2:
- Do not return after triggering the update.
- Use tabs for indentation on new defines.

---
 drivers/platform/x86/asus-wmi.c            | 19 +++++++++++++++++++
 include/linux/platform_data/x86/asus-wmi.h |  2 +-
 2 files changed, 20 insertions(+), 1 deletion(-)
drivers/platform/x86/asus-wmi.c +19 -0
index 80144c412b90..32d0d4ce60ff 100644
@@ -70,6 +70,8 @@ module_param(fnlock_default, bool, 0444);
 #define NOTIFY_KBD_TTP			0xae
 #define NOTIFY_LID_FLIP			0xfa
 #define NOTIFY_LID_FLIP_ROG		0xbd
+#define NOTIFY_CAMERA_TOGGLE_1		0x82
+#define NOTIFY_CAMERA_TOGGLE_2		0x85
 
 #define ASUS_WMI_FNLOCK_BIOS_DISABLED	BIT(0)
 
@@ -307,6 +309,7 @@ struct asus_wmi {
 	u32 kbd_rgb_dev;
 	bool kbd_rgb_state_available;
 	bool oobe_state_available;
+	bool camera_neg_led_available;
 
 	u8 throttle_thermal_policy_mode;
 	u32 throttle_thermal_policy_dev;
@@ -1982,6 +1985,18 @@ static int micmute_led_set(struct led_classdev *led_cdev,
 	return err < 0 ? err : 0;
 }
 
+static bool is_camera_toggle(int code)
+{
+	return code == NOTIFY_CAMERA_TOGGLE_1 || code == NOTIFY_CAMERA_TOGGLE_2;
+}
+
+static void camera_neg_led_trigger_update(struct asus_wmi *asus)
+{
+	u32 result;
+
+	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CAMERA_LED_NEG, &result);
+}
+
 static enum led_brightness camera_led_get(struct led_classdev *led_cdev)
 {
 	struct asus_wmi *asus;
@@ -4587,6 +4602,9 @@ static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
 	if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle)
 		return;
 
+	if (is_camera_toggle(code) && asus->camera_neg_led_available)
+		camera_neg_led_trigger_update(asus);
+
 	if (!sparse_keymap_report_event(asus->inputdev, code,
 					key_value, autorelease))
 		pr_info("Unknown key code 0x%x\n", code);
@@ -5062,6 +5080,7 @@ static int asus_wmi_add(struct platform_device *pdev)
 #endif /* IS_ENABLED(CONFIG_ASUS_WMI_DEPRECATED_ATTRS) */
 
 	asus->oobe_state_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_OOBE);
+	asus->camera_neg_led_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CAMERA_LED_NEG);
 
 	if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY))
 		asus->throttle_thermal_policy_dev = ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY;
include/linux/platform_data/x86/asus-wmi.h +1 -1
index 554f41b827e1..1a9349da6069 100644
@@ -56,7 +56,7 @@
 #define ASUS_WMI_DEVID_MICMUTE_LED		0x00040017
 
 /* Disable Camera LED */
-#define ASUS_WMI_DEVID_CAMERA_LED_NEG	0x00060078 /* 0 = on (unused) */
+#define ASUS_WMI_DEVID_CAMERA_LED_NEG	0x00060078 /* 0 = on */
 #define ASUS_WMI_DEVID_CAMERA_LED	0x00060079 /* 1 = on */
 
 /* Backlight and Brightness */
Signature · 1 line
 i.
#2 lore raw

Thanks for the comment, Ilpo.

Quoted 2 lines · Ilpo Järvinen

While I believe you are "correct" that userspace won't see a thing, is this really correct way to implement support for this device?

At first, I created another sysfs attribute that basically duplicated the logic that is used for the "asus::camera" attribute, with the exception being the value that was written to it, of course. I noticed, however, that calling asus_wmi_set_devstate() had not effect on the LED state. The LED only toggled when I read the attribute, which then called asus_wmi_get_devstate().

I don't know if this is a quirk for this device, or if all devices that expose ASUS_WMI_DEVID_CAMERA_LED_NEG behave the same, but the LED is not toggled until I call asus_wmi_get_devstate() - be it when detecting a key press (hardware change) or after the sysfs attribute is written to (software change).

Since in this case the LED state would not change after a write to the sysfs attribute, I went in the "do not make it visible to userspace at all" direction.

Quoted 5 lines

I mean the camera led init in asus_wmi_led_init() is gated by asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CAMERA_LED) but should that be gated by either CAMERA_LED or CAMERA_LED_NEG and the related code in *_camera_led_*() that now only uses ASUS_WMI_DEVID_CAMERA_LED should use which ever of them is available?

Yes, I think what you said makes sense as it would give userspace access to the resource without creating a new attribute.

In this case, I feel like a good option would be to gate the camera led initialization by both IDs, and assume that the write and read works as they probably should for CAMERA_LED_NEG. Then, somehow add a quirk for this specific device that performs a read when the key is pressed, and right after the sysfs attribute is written to. If we can confirm that this behavior is expected when writing to CAMERA_LED_NEG, it might not need to be a quirk. Unfortunately, I am not able to try this on other devices.

What do you think about it?

Best regards,
José Rodrigues

maintainer
#3 lore raw
Quoted 41 lines · José Guilherme de Castro Rodrigues

Thanks for the comment, Ilpo.

Quoted 2 lines · Ilpo Järvinen

While I believe you are "correct" that userspace won't see a thing, is this really correct way to implement support for this device?

At first, I created another sysfs attribute that basically duplicated the logic that is used for the "asus::camera" attribute, with the exception being the value that was written to it, of course. I noticed, however, that calling asus_wmi_set_devstate() had not effect on the LED state. The LED only toggled when I read the attribute, which then called asus_wmi_get_devstate().

I don't know if this is a quirk for this device, or if all devices that expose ASUS_WMI_DEVID_CAMERA_LED_NEG behave the same, but the LED is not toggled until I call asus_wmi_get_devstate() - be it when detecting a key press (hardware change) or after the sysfs attribute is written to (software change).

Since in this case the LED state would not change after a write to the sysfs attribute, I went in the "do not make it visible to userspace at all" direction.

Quoted 5 lines

I mean the camera led init in asus_wmi_led_init() is gated by asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CAMERA_LED) but should that be gated by either CAMERA_LED or CAMERA_LED_NEG and the related code in *_camera_led_*() that now only uses ASUS_WMI_DEVID_CAMERA_LED should use which ever of them is available?

Yes, I think what you said makes sense as it would give userspace access to the resource without creating a new attribute.

In this case, I feel like a good option would be to gate the camera led initialization by both IDs, and assume that the write and read works as they probably should for CAMERA_LED_NEG. Then, somehow add a quirk for this specific device that performs a read when the key is pressed, and right after the sysfs attribute is written to. If we can confirm that this behavior is expected when writing to CAMERA_LED_NEG, it might not need to be a quirk. Unfortunately, I am not able to try this on other devices.

Quoted 1 line

What do you think about it?

So if I understood you correctly with LED_NEG (unknown if it's just on your device or generally with it),

This doesn't work: set
This works: set+get

The extra get shouldn't be that harmful so doing that always if CAMERA_LED_NEG is being used is okay at least with me (make sure to add add a comment though so it can be known later why it's there even 10 years from now).

Signature · 1 line
 i.
#4 lore raw
Quoted 6 lines · Ilpo Järvinen

So if I understood you correctly with LED_NEG (unknown if it's just on your device or generally with it),

This doesn't work: set
This works: set+get

Yes, that is correct.

Quoted 4 lines

The extra get shouldn't be that harmful so doing that always if CAMERA_LED_NEG is being used is okay at least with me (make sure to add add a comment though so it can be known later why it's there even 10 years from now).

Ok! Once again, thanks for the direction. I'll work on this as soon as I can and then send a v3.

Best Regards,
José Rodrigues

#5 lore raw
Quoted 10 lines · José Guilherme de Castro Rodrigues
Quoted 6 lines · Ilpo Järvinen

So if I understood you correctly with LED_NEG (unknown if it's just on your device or generally with it),

This doesn't work: set
This works: set+get

Yes, that is correct.

Actually, sorry. No, that is not correct. I was pretty sure that did work, but I must've mixed something up... probably because I tried that only before I sent the first patch.

I can't get the set operation to work at all. So I end up only being able to read the brightness in the device exposed to userspace.

I don't really have the knowledge to debug the wmi call, so that is definitely a dead end for me.

Best Regards,
José Rodrigues