Skip to contentSkip to conversation

platform/x86: hp-wmi: Add HP WMI camera switch

PATCH Superseded Jonathan Singer 3 participants 7 messages Apr 26, 2023 x86 platform drivers

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 platform/x86: hp-wmi: Add HP WMI camera switch

Previously, when the camera toggle switch was hit, the hp-wmi driver would report an invalid event code. By adding a case for that in the event handling switch statement we can eliminate that error code and enable a framework for potential further kernel handling of that key. This change was tested on my HP Envy x360 15-ey0023dx laptop, but it would likely work for any HP laptop with a camera toggle button. Now we emit an SW_CAMERA_LENS_COVER event, on a device that gets created on the first such event so as to not report incorrectly the state of the camera shutter before we can know its state.

---
 drivers/platform/x86/hp/hp-wmi.c | 50 ++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
drivers/platform/x86/hp/hp-wmi.c +50 -0
index 873f59c3e280..a7fb33ac49b8 100644
@@ -90,6 +90,7 @@ enum hp_wmi_event_ids {
 	HPWMI_PEAKSHIFT_PERIOD		= 0x0F,
 	HPWMI_BATTERY_CHARGE_PERIOD	= 0x10,
 	HPWMI_SANITIZATION_MODE		= 0x17,
+	HPWMI_CAMERA_TOGGLE		= 0x1A,
 	HPWMI_OMEN_KEY			= 0x1D,
 	HPWMI_SMART_EXPERIENCE_APP	= 0x21,
 };
@@ -228,6 +229,7 @@ static const struct key_entry hp_wmi_keymap[] = {
 };
 
 static struct input_dev *hp_wmi_input_dev;
+static struct input_dev *camera_shutter_input_dev;
 static struct platform_device *hp_wmi_platform_dev;
 static struct platform_profile_handler platform_profile_handler;
 static bool platform_profile_support;
@@ -739,6 +741,36 @@ static ssize_t postcode_store(struct device *dev, struct device_attribute *attr,
 	return count;
 }
 
+static int camera_shutter_input_setup(void)
+{
+	int err;
+
+	camera_shutter_input_dev = input_allocate_device();
+	if (!camera_shutter_input_dev)
+		return -ENOMEM;
+
+	camera_shutter_input_dev->name = "HP WMI camera shutter";
+	camera_shutter_input_dev->phys = "wmi/input1";
+	camera_shutter_input_dev->id.bustype = BUS_HOST;
+
+	__set_bit(EV_SW, camera_shutter_input_dev->evbit);
+	__set_bit(SW_CAMERA_LENS_COVER, camera_shutter_input_dev->swbit);
+
+	/* Set initial hardware state */
+	input_sync(camera_shutter_input_dev);
+
+	err = input_register_device(camera_shutter_input_dev);
+	if (err)
+		goto err_free_dev;
+
+	return 0;
+
+ err_free_dev:
+	input_free_device(camera_shutter_input_dev);
+	camera_shutter_input_dev = NULL;
+	return err;
+}
+
 static DEVICE_ATTR_RO(display);
 static DEVICE_ATTR_RO(hddtemp);
 static DEVICE_ATTR_RW(als);
@@ -866,6 +898,20 @@ static void hp_wmi_notify(u32 value, void *context)
 		break;
 	case HPWMI_SANITIZATION_MODE:
 		break;
+	case HPWMI_CAMERA_TOGGLE:
+		if (!camera_shutter_input_dev)
+			if (camera_shutter_input_setup()) {
+				pr_info("Failed to setup camera shutter input device\n");
+				break;
+			}
+		if (event_data == 0xff)
+			input_report_switch(camera_shutter_input_dev, SW_CAMERA_LENS_COVER, 1);
+		else if (event_data == 0xfe)
+			input_report_switch(camera_shutter_input_dev, SW_CAMERA_LENS_COVER, 0);
+		else
+			pr_info("Unknown camera shutter state - 0x%x\n", event_data);
+		input_sync(camera_shutter_input_dev);
+		break;
 	case HPWMI_SMART_EXPERIENCE_APP:
 		break;
 	default:
@@ -1564,9 +1610,13 @@ static void __exit hp_wmi_exit(void)
 	if (wmi_has_guid(HPWMI_EVENT_GUID))
 		hp_wmi_input_destroy();
 
+	if (camera_shutter_input_dev)
+		input_unregister_device(camera_shutter_input_dev);
+
 	if (hp_wmi_platform_dev) {
 		platform_device_unregister(hp_wmi_platform_dev);
 		platform_driver_unregister(&hp_wmi_driver);
 	}
+
 }
 module_exit(hp_wmi_exit);
Signature · 1 line
2.40.0
#1 lore raw

PATCH v2 2/2 platform/x86: hp-wmi: Add HP Envy special key support

Previously, some support for certain keys on the HP keyboard has been added already in commit 3ee5447b2048 ("platform/x86: hp-wmi: Handle Omen Key event"), however this as tested did not allow even the fn+esc key on my HP Envy which uses the same keycode on my HP Envy x360 laptop to work --the keycode rather than being passed in as a separate int from WMI, was being passed in as the event_data for the HPWMI_OMEN_KEY event.

This patch, as tested was able to properly get the keycode for fn+esc, and for fn+f12 which is supposed to be a programmable key according to HP's keyboard diagram and is thus mapped to KEY_PROG2. The fn+f8 key combination (mute microphone) was a standard HPWMI_BEZEL_BUTTON key, however it did not previously have an entry in the sparse keymap. This patch preserves the original HPWMI_OMEN_KEY behavior for laptops that use it by only taking the keycode from the event_data only when the event_data is nonzero.

---
 drivers/platform/x86/hp/hp-wmi.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/platform/x86/hp/hp-wmi.c +12 -1
index a7fb33ac49b8..399163b4aca0 100644
@@ -212,6 +212,7 @@ struct bios_rfkill2_state {
 static const struct key_entry hp_wmi_keymap[] = {
 	{ KE_KEY, 0x02,    { KEY_BRIGHTNESSUP } },
 	{ KE_KEY, 0x03,    { KEY_BRIGHTNESSDOWN } },
+	{ KE_KEY, 0x270,   { KEY_MICMUTE } },
 	{ KE_KEY, 0x20e6,  { KEY_PROG1 } },
 	{ KE_KEY, 0x20e8,  { KEY_MEDIA } },
 	{ KE_KEY, 0x2142,  { KEY_MEDIA } },
@@ -222,6 +223,7 @@ static const struct key_entry hp_wmi_keymap[] = {
 	{ KE_IGNORE, 0x121a4, }, /* Win Lock Off */
 	{ KE_KEY, 0x21a5,  { KEY_PROG2 } }, /* HP Omen Key */
 	{ KE_KEY, 0x21a7,  { KEY_FN_ESC } },
+	{ KE_KEY, 0x21a8,  { KEY_PROG2 } }, /* HP Envy x360 programmable key */
 	{ KE_KEY, 0x21a9,  { KEY_TOUCHPAD_OFF } },
 	{ KE_KEY, 0x121a9, { KEY_TOUCHPAD_ON } },
 	{ KE_KEY, 0x231b,  { KEY_HELP } },
@@ -847,11 +849,20 @@ static void hp_wmi_notify(u32 value, void *context)
 	case HPWMI_SMART_ADAPTER:
 		break;
 	case HPWMI_BEZEL_BUTTON:
-	case HPWMI_OMEN_KEY:
 		key_code = hp_wmi_read_int(HPWMI_HOTKEY_QUERY);
 		if (key_code < 0)
 			break;
 
+		if (!sparse_keymap_report_event(hp_wmi_input_dev,
+						key_code, 1, true))
+			pr_info("Unknown key code - 0x%x\n", key_code);
+		break;
+	case HPWMI_OMEN_KEY:
+		if (event_data) /* Only should be true for HP Omen */
+			key_code = event_data;
+		else
+			key_code = hp_wmi_read_int(HPWMI_HOTKEY_QUERY);
+
 		if (!sparse_keymap_report_event(hp_wmi_input_dev,
 						key_code, 1, true))
 			pr_info("Unknown key code - 0x%x\n", key_code);
Signature · 1 line
2.40.0
Reviewed-by on v2 2/2
#2 lore raw

Re: [PATCH v2 2/2] platform/x86: hp-wmi: Add HP Envy special key support

Quoted 16 lines · Jonathan Singer

Previously, some support for certain keys on the HP keyboard has been added already in commit 3ee5447b2048 ("platform/x86: hp-wmi: Handle Omen Key event"), however this as tested did not allow even the fn+esc key on my HP Envy which uses the same keycode on my HP Envy x360 laptop to work --the keycode rather than being passed in as a separate int from WMI, was being passed in as the event_data for the HPWMI_OMEN_KEY event.

This patch, as tested was able to properly get the keycode for fn+esc, and for fn+f12 which is supposed to be a programmable key according to HP's keyboard diagram and is thus mapped to KEY_PROG2. The fn+f8 key combination (mute microphone) was a standard HPWMI_BEZEL_BUTTON key, however it did not previously have an entry in the sparse keymap. This patch preserves the original HPWMI_OMEN_KEY behavior for laptops that use it by only taking the keycode from the event_data only when the event_data is nonzero.

Quoted 1 line
Quoted 46 lines
---
  drivers/platform/x86/hp/hp-wmi.c | 13 ++++++++++++-
  1 file changed, 12 insertions(+), 1 deletion(-)
drivers/platform/x86/hp/hp-wmi.c +12 -1
index a7fb33ac49b8..399163b4aca0 100644
@@ -212,6 +212,7 @@ struct bios_rfkill2_state {
  static const struct key_entry hp_wmi_keymap[] = {
  	{ KE_KEY, 0x02,    { KEY_BRIGHTNESSUP } },
  	{ KE_KEY, 0x03,    { KEY_BRIGHTNESSDOWN } },
+	{ KE_KEY, 0x270,   { KEY_MICMUTE } },
  	{ KE_KEY, 0x20e6,  { KEY_PROG1 } },
  	{ KE_KEY, 0x20e8,  { KEY_MEDIA } },
  	{ KE_KEY, 0x2142,  { KEY_MEDIA } },
@@ -222,6 +223,7 @@ static const struct key_entry hp_wmi_keymap[] = {
  	{ KE_IGNORE, 0x121a4, }, /* Win Lock Off */
  	{ KE_KEY, 0x21a5,  { KEY_PROG2 } }, /* HP Omen Key */
  	{ KE_KEY, 0x21a7,  { KEY_FN_ESC } },
+	{ KE_KEY, 0x21a8,  { KEY_PROG2 } }, /* HP Envy x360 programmable key */
  	{ KE_KEY, 0x21a9,  { KEY_TOUCHPAD_OFF } },
  	{ KE_KEY, 0x121a9, { KEY_TOUCHPAD_ON } },
  	{ KE_KEY, 0x231b,  { KEY_HELP } },
@@ -847,11 +849,20 @@ static void hp_wmi_notify(u32 value, void *context)
  	case HPWMI_SMART_ADAPTER:
  		break;
  	case HPWMI_BEZEL_BUTTON:
-	case HPWMI_OMEN_KEY:
  		key_code = hp_wmi_read_int(HPWMI_HOTKEY_QUERY);
  		if (key_code < 0)
  			break;
 
+		if (!sparse_keymap_report_event(hp_wmi_input_dev,
+						key_code, 1, true))
+			pr_info("Unknown key code - 0x%x\n", key_code);
+		break;
+	case HPWMI_OMEN_KEY:
+		if (event_data) /* Only should be true for HP Omen */
+			key_code = event_data;
+		else
+			key_code = hp_wmi_read_int(HPWMI_HOTKEY_QUERY);
+
  		if (!sparse_keymap_report_event(hp_wmi_input_dev,
  						key_code, 1, true))
  			pr_info("Unknown key code - 0x%x\n", key_code);
#3 lore raw
In reply to Jonathan Singer
Quoted 73 lines · Jonathan Singer

Previously, when the camera toggle switch was hit, the hp-wmi driver would report an invalid event code. By adding a case for that in the event handling switch statement we can eliminate that error code and enable a framework for potential further kernel handling of that key. This change was tested on my HP Envy x360 15-ey0023dx laptop, but it would likely work for any HP laptop with a camera toggle button. Now we emit an SW_CAMERA_LENS_COVER event, on a device that gets created on the first such event so as to not report incorrectly the state of the camera shutter before we can know its state.

---
  drivers/platform/x86/hp/hp-wmi.c | 50 ++++++++++++++++++++++++++++++++
  1 file changed, 50 insertions(+)
drivers/platform/x86/hp/hp-wmi.c +32 -0
index 873f59c3e280..a7fb33ac49b8 100644
@@ -90,6 +90,7 @@ enum hp_wmi_event_ids {
  	HPWMI_PEAKSHIFT_PERIOD		= 0x0F,
  	HPWMI_BATTERY_CHARGE_PERIOD	= 0x10,
  	HPWMI_SANITIZATION_MODE		= 0x17,
+	HPWMI_CAMERA_TOGGLE		= 0x1A,
  	HPWMI_OMEN_KEY			= 0x1D,
  	HPWMI_SMART_EXPERIENCE_APP	= 0x21,
  };
@@ -228,6 +229,7 @@ static const struct key_entry hp_wmi_keymap[] = {
  };
 
  static struct input_dev *hp_wmi_input_dev;
+static struct input_dev *camera_shutter_input_dev;
  static struct platform_device *hp_wmi_platform_dev;
  static struct platform_profile_handler platform_profile_handler;
  static bool platform_profile_support;
@@ -739,6 +741,36 @@ static ssize_t postcode_store(struct device *dev, struct device_attribute *attr,
  	return count;
  }
 
+static int camera_shutter_input_setup(void)
+{
+	int err;
+
+	camera_shutter_input_dev = input_allocate_device();
+	if (!camera_shutter_input_dev)
+		return -ENOMEM;
+
+	camera_shutter_input_dev->name = "HP WMI camera shutter";
+	camera_shutter_input_dev->phys = "wmi/input1";
+	camera_shutter_input_dev->id.bustype = BUS_HOST;
+
+	__set_bit(EV_SW, camera_shutter_input_dev->evbit);
+	__set_bit(SW_CAMERA_LENS_COVER, camera_shutter_input_dev->swbit);
+
+	/* Set initial hardware state */
+	input_sync(camera_shutter_input_dev);
+
+	err = input_register_device(camera_shutter_input_dev);
+	if (err)
+		goto err_free_dev;
+
+	return 0;
+
+ err_free_dev:
+	input_free_device(camera_shutter_input_dev);
+	camera_shutter_input_dev = NULL;
+	return err;
+}
+
  static DEVICE_ATTR_RO(display);
  static DEVICE_ATTR_RO(hddtemp);
  static DEVICE_ATTR_RW(als);

drivers/platform/x86/hp/hp-wmi.c static void hp_wmi_notify(u32 value, void *context) · line 904

Quoted 7 lines
drivers/platform/x86/hp/hp-wmi.c +4 -0
@@ -866,6 +898,20 @@ static void hp_wmi_notify(u32 value, void *context)
  		break;
  	case HPWMI_SANITIZATION_MODE:
  		break;
+	case HPWMI_CAMERA_TOGGLE:
+		if (!camera_shutter_input_dev)
+			if (camera_shutter_input_setup()) {
+				pr_info("Failed to setup camera shutter input device\n");

I think this should be pr_err.

drivers/platform/x86/hp/hp-wmi.c static void hp_wmi_notify(u32 value, void *context) · line 912

Quoted 8 lines
drivers/platform/x86/hp/hp-wmi.c +8 -0
+				break;
+			}
+		if (event_data == 0xff)
+			input_report_switch(camera_shutter_input_dev, SW_CAMERA_LENS_COVER, 1);
+		else if (event_data == 0xfe)
+			input_report_switch(camera_shutter_input_dev, SW_CAMERA_LENS_COVER, 0);
+		else
+			pr_info("Unknown camera shutter state - 0x%x\n", event_data);

I think this should be warn.

Quoted 19 lines
drivers/platform/x86/hp/hp-wmi.c +6 -0
+		input_sync(camera_shutter_input_dev);
+		break;
  	case HPWMI_SMART_EXPERIENCE_APP:
  		break;
  	default:
@@ -1564,9 +1610,13 @@ static void __exit hp_wmi_exit(void)
  	if (wmi_has_guid(HPWMI_EVENT_GUID))
  		hp_wmi_input_destroy();
 
+	if (camera_shutter_input_dev)
+		input_unregister_device(camera_shutter_input_dev);
+
  	if (hp_wmi_platform_dev) {
  		platform_device_unregister(hp_wmi_platform_dev);
  		platform_driver_unregister(&hp_wmi_driver);
  	}
+
  }
  module_exit(hp_wmi_exit);
#4 lore raw
In reply to Jonathan Singer

Hi Jonathan,

Quoted 46 lines · Jonathan Singer

Previously, when the camera toggle switch was hit, the hp-wmi driver would report an invalid event code. By adding a case for that in the event handling switch statement we can eliminate that error code and enable a framework for potential further kernel handling of that key. This change was tested on my HP Envy x360 15-ey0023dx laptop, but it would likely work for any HP laptop with a camera toggle button. Now we emit an SW_CAMERA_LENS_COVER event, on a device that gets created on the first such event so as to not report incorrectly the state of the camera shutter before we can know its state.

---
 drivers/platform/x86/hp/hp-wmi.c | 50 ++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
drivers/platform/x86/hp/hp-wmi.c +9 -0
index 873f59c3e280..a7fb33ac49b8 100644
@@ -90,6 +90,7 @@ enum hp_wmi_event_ids {
 	HPWMI_PEAKSHIFT_PERIOD		= 0x0F,
 	HPWMI_BATTERY_CHARGE_PERIOD	= 0x10,
 	HPWMI_SANITIZATION_MODE		= 0x17,
+	HPWMI_CAMERA_TOGGLE		= 0x1A,
 	HPWMI_OMEN_KEY			= 0x1D,
 	HPWMI_SMART_EXPERIENCE_APP	= 0x21,
 };
@@ -228,6 +229,7 @@ static const struct key_entry hp_wmi_keymap[] = {
 };
 
 static struct input_dev *hp_wmi_input_dev;
+static struct input_dev *camera_shutter_input_dev;
 static struct platform_device *hp_wmi_platform_dev;
 static struct platform_profile_handler platform_profile_handler;
 static bool platform_profile_support;
@@ -739,6 +741,36 @@ static ssize_t postcode_store(struct device *dev, struct device_attribute *attr,
 	return count;
 }
 
+static int camera_shutter_input_setup(void)
+{
+	int err;
+
+	camera_shutter_input_dev = input_allocate_device();
+	if (!camera_shutter_input_dev)
+		return -ENOMEM;

drivers/platform/x86/hp/hp-wmi.c static ssize_t postcode_store(struct device *dev, struct device_attribute *attr, · line 760

Quoted 10 lines
drivers/platform/x86/hp/hp-wmi.c +10 -0
+
+	camera_shutter_input_dev->name = "HP WMI camera shutter";
+	camera_shutter_input_dev->phys = "wmi/input1";
+	camera_shutter_input_dev->id.bustype = BUS_HOST;
+
+	__set_bit(EV_SW, camera_shutter_input_dev->evbit);
+	__set_bit(SW_CAMERA_LENS_COVER, camera_shutter_input_dev->swbit);
+
+	/* Set initial hardware state */
+	input_sync(camera_shutter_input_dev);

This initial sync is only necessary if you have actually read + set an initial state, which is not happening here, so please drop this.

The caller of camera_shutter_input_setup() will set a state + call input_sync() immediately afterwards anyways, so there is no need to set an initial state.

Otherwise this looks good to me, thank you for working on this.

Regards,

Hans

Quoted 51 lines
drivers/platform/x86/hp/hp-wmi.c +31 -0
+
+	err = input_register_device(camera_shutter_input_dev);
+	if (err)
+		goto err_free_dev;
+
+	return 0;
+
+ err_free_dev:
+	input_free_device(camera_shutter_input_dev);
+	camera_shutter_input_dev = NULL;
+	return err;
+}
+
 static DEVICE_ATTR_RO(display);
 static DEVICE_ATTR_RO(hddtemp);
 static DEVICE_ATTR_RW(als);
@@ -866,6 +898,20 @@ static void hp_wmi_notify(u32 value, void *context)
 		break;
 	case HPWMI_SANITIZATION_MODE:
 		break;
+	case HPWMI_CAMERA_TOGGLE:
+		if (!camera_shutter_input_dev)
+			if (camera_shutter_input_setup()) {
+				pr_info("Failed to setup camera shutter input device\n");
+				break;
+			}
+		if (event_data == 0xff)
+			input_report_switch(camera_shutter_input_dev, SW_CAMERA_LENS_COVER, 1);
+		else if (event_data == 0xfe)
+			input_report_switch(camera_shutter_input_dev, SW_CAMERA_LENS_COVER, 0);
+		else
+			pr_info("Unknown camera shutter state - 0x%x\n", event_data);
+		input_sync(camera_shutter_input_dev);
+		break;
 	case HPWMI_SMART_EXPERIENCE_APP:
 		break;
 	default:
@@ -1564,9 +1610,13 @@ static void __exit hp_wmi_exit(void)
 	if (wmi_has_guid(HPWMI_EVENT_GUID))
 		hp_wmi_input_destroy();
 
+	if (camera_shutter_input_dev)
+		input_unregister_device(camera_shutter_input_dev);
+
 	if (hp_wmi_platform_dev) {
 		platform_device_unregister(hp_wmi_platform_dev);
 		platform_driver_unregister(&hp_wmi_driver);
 	}
+
 }
 module_exit(hp_wmi_exit);
#5 lore raw
In reply to Jonathan Singer

Hi again,

Quoted 11 lines · Jonathan Singer

Previously, when the camera toggle switch was hit, the hp-wmi driver would report an invalid event code. By adding a case for that in the event handling switch statement we can eliminate that error code and enable a framework for potential further kernel handling of that key. This change was tested on my HP Envy x360 15-ey0023dx laptop, but it would likely work for any HP laptop with a camera toggle button. Now we emit an SW_CAMERA_LENS_COVER event, on a device that gets created on the first such event so as to not report incorrectly the state of the camera shutter before we can know its state.

p.s.

drivers/platform/x86/hp/hp-wmi.c static void hp_wmi_notify(u32 value, void *context) · line 908

Quoted 2 lines
drivers/platform/x86/hp/hp-wmi.c +2 -0
+		if (event_data == 0xff)
+			input_report_switch(camera_shutter_input_dev, SW_CAMERA_LENS_COVER, 1);

I assume that event_data = 0xff happens when disabling the camera, so we report SW_CAMERA_LENS_COVER 1 when the camera is disabled, right ?

Regards,

Hans

#6 lore raw

Hi Hans,

On Wed, 26 Apr 2023 18:09:13 +0200, Hans de Goede <[email protected]> wrote

Quoted 2 lines

I assume that event_data = 0xff happens when disabling the camera, so we report SW_CAMERA_LENS_COVER 1 when the camera is disabled, right ?

Correct, the 0xff event_data happens when the camera shutter is closed and the 0xfe event happens when the camera shutter is opened.

Regards,
Jonathan