[PATCH v2] platform/x86: lenovo-wmi-camera: Use SW_CAMERA_LENS_COVER instead of KEY_CAMERA_ACESS

Ai Chao posted 1 patch 1 year, 1 month ago
There is a newer version of this series
drivers/platform/x86/lenovo-wmi-camera.c | 55 ++++++++++++++----------
1 file changed, 32 insertions(+), 23 deletions(-)
[PATCH v2] platform/x86: lenovo-wmi-camera: Use SW_CAMERA_LENS_COVER instead of KEY_CAMERA_ACESS
Posted by Ai Chao 1 year, 1 month ago
Use SW_CAMERA_LENS_COVER instead of KEY_CAMERA_ACESS_ENABLE and
KEY_CAMERA_ACESS_DISABLE. When the camera toggle switch was hit,
the lenovo-wmi-camera driver would report an event code.

Signed-off-by: Ai Chao <aichao@kylinos.cn>
---
change for v2
-Only delays the input-device registration and switches to reporting SW_CAMERA_LENS_COVER.

 drivers/platform/x86/lenovo-wmi-camera.c | 55 ++++++++++++++----------
 1 file changed, 32 insertions(+), 23 deletions(-)

diff --git a/drivers/platform/x86/lenovo-wmi-camera.c b/drivers/platform/x86/lenovo-wmi-camera.c
index 0c0bedaf7407..ad296acaf562 100644
--- a/drivers/platform/x86/lenovo-wmi-camera.c
+++ b/drivers/platform/x86/lenovo-wmi-camera.c
@@ -26,10 +26,33 @@ enum {
 	SW_CAMERA_ON	= 1,
 };
 
+static int camera_shutter_input_setup(struct wmi_device *wdev)
+{
+	struct lenovo_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
+	int err;
+
+	priv->idev = devm_input_allocate_device(&wdev->dev);
+	if (!priv->idev)
+		return -ENOMEM;
+
+	priv->idev->name = "Lenovo WMI Camera Button";
+	priv->idev->phys = "wmi/input0";
+	priv->idev->id.bustype = BUS_HOST;
+	priv->idev->dev.parent = &wdev->dev;
+
+	__set_bit(EV_SW, priv->idev->evbit);
+	__set_bit(SW_CAMERA_LENS_COVER, priv->idev->swbit);
+
+	err = input_register_device(priv->idev);
+	if (err)
+		return err;
+
+	return 0;
+}
+
 static void lenovo_wmi_notify(struct wmi_device *wdev, union acpi_object *obj)
 {
 	struct lenovo_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
-	unsigned int keycode;
 	u8 camera_mode;
 
 	if (obj->type != ACPI_TYPE_BUFFER) {
@@ -53,13 +76,16 @@ static void lenovo_wmi_notify(struct wmi_device *wdev, union acpi_object *obj)
 		return;
 	}
 
+	if (!priv->idev)
+		if (camera_shutter_input_setup(wdev))
+			return;
+
 	mutex_lock(&priv->notify_lock);
 
-	keycode = camera_mode == SW_CAMERA_ON ?
-		   KEY_CAMERA_ACCESS_ENABLE : KEY_CAMERA_ACCESS_DISABLE;
-	input_report_key(priv->idev, keycode, 1);
-	input_sync(priv->idev);
-	input_report_key(priv->idev, keycode, 0);
+	if (camera_mode == SW_CAMERA_ON)
+		input_report_switch(priv->idev, SW_CAMERA_LENS_COVER, 1);
+	else if (camera_mode == SW_CAMERA_OFF)
+		input_report_switch(priv->idev, SW_CAMERA_LENS_COVER, 0);
 	input_sync(priv->idev);
 
 	mutex_unlock(&priv->notify_lock);
@@ -68,29 +94,12 @@ static void lenovo_wmi_notify(struct wmi_device *wdev, union acpi_object *obj)
 static int lenovo_wmi_probe(struct wmi_device *wdev, const void *context)
 {
 	struct lenovo_wmi_priv *priv;
-	int ret;
 
 	priv = devm_kzalloc(&wdev->dev, sizeof(*priv), GFP_KERNEL);
 	if (!priv)
 		return -ENOMEM;
 
 	dev_set_drvdata(&wdev->dev, priv);
-
-	priv->idev = devm_input_allocate_device(&wdev->dev);
-	if (!priv->idev)
-		return -ENOMEM;
-
-	priv->idev->name = "Lenovo WMI Camera Button";
-	priv->idev->phys = "wmi/input0";
-	priv->idev->id.bustype = BUS_HOST;
-	priv->idev->dev.parent = &wdev->dev;
-	input_set_capability(priv->idev, EV_KEY, KEY_CAMERA_ACCESS_ENABLE);
-	input_set_capability(priv->idev, EV_KEY, KEY_CAMERA_ACCESS_DISABLE);
-
-	ret = input_register_device(priv->idev);
-	if (ret)
-		return ret;
-
 	mutex_init(&priv->notify_lock);
 
 	return 0;
-- 
2.25.1
Re: [PATCH v2] platform/x86: lenovo-wmi-camera: Use SW_CAMERA_LENS_COVER instead of KEY_CAMERA_ACESS
Posted by Armin Wolf 1 year, 1 month ago
Am 27.12.24 um 10:10 schrieb Ai Chao:

> Use SW_CAMERA_LENS_COVER instead of KEY_CAMERA_ACESS_ENABLE and
> KEY_CAMERA_ACESS_DISABLE. When the camera toggle switch was hit,
> the lenovo-wmi-camera driver would report an event code.
>
> Signed-off-by: Ai Chao <aichao@kylinos.cn>
> ---
> change for v2
> -Only delays the input-device registration and switches to reporting SW_CAMERA_LENS_COVER.
>
>   drivers/platform/x86/lenovo-wmi-camera.c | 55 ++++++++++++++----------
>   1 file changed, 32 insertions(+), 23 deletions(-)
>
> diff --git a/drivers/platform/x86/lenovo-wmi-camera.c b/drivers/platform/x86/lenovo-wmi-camera.c
> index 0c0bedaf7407..ad296acaf562 100644
> --- a/drivers/platform/x86/lenovo-wmi-camera.c
> +++ b/drivers/platform/x86/lenovo-wmi-camera.c
> @@ -26,10 +26,33 @@ enum {
>   	SW_CAMERA_ON	= 1,
>   };
>
> +static int camera_shutter_input_setup(struct wmi_device *wdev)
> +{
> +	struct lenovo_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
> +	int err;
> +
> +	priv->idev = devm_input_allocate_device(&wdev->dev);
> +	if (!priv->idev)
> +		return -ENOMEM;
> +
> +	priv->idev->name = "Lenovo WMI Camera Button";
> +	priv->idev->phys = "wmi/input0";
> +	priv->idev->id.bustype = BUS_HOST;
> +	priv->idev->dev.parent = &wdev->dev;
> +
> +	__set_bit(EV_SW, priv->idev->evbit);
> +	__set_bit(SW_CAMERA_LENS_COVER, priv->idev->swbit);
> +

Please use input_set_capability() here.

> +	err = input_register_device(priv->idev);
> +	if (err)
> +		return err;

Please set priv->idev back to NULL if input_device_register() fails, or else
lenovo_wmi_notify() will assume that a valid input device was registered when
the next WMI event is received.

Also i suggest that you do not use devm_input_allocate_device() since you need
to be able to free the input device should an error occur during registration.
Better use input_device_allocate() and remove the input device manually inside
lenovo_wmi_remove().

> +
> +	return 0;
> +}
> +
>   static void lenovo_wmi_notify(struct wmi_device *wdev, union acpi_object *obj)
>   {
>   	struct lenovo_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
> -	unsigned int keycode;
>   	u8 camera_mode;
>
>   	if (obj->type != ACPI_TYPE_BUFFER) {
> @@ -53,13 +76,16 @@ static void lenovo_wmi_notify(struct wmi_device *wdev, union acpi_object *obj)
>   		return;
>   	}
>
> +	if (!priv->idev)
> +		if (camera_shutter_input_setup(wdev))
> +			return;

Please move this piece of code below "mutex_lock(&priv->notify_lock)" to make
sure that only a single thread tries to register a input device at the same time.

> +
>   	mutex_lock(&priv->notify_lock);
>
> -	keycode = camera_mode == SW_CAMERA_ON ?
> -		   KEY_CAMERA_ACCESS_ENABLE : KEY_CAMERA_ACCESS_DISABLE;
> -	input_report_key(priv->idev, keycode, 1);
> -	input_sync(priv->idev);
> -	input_report_key(priv->idev, keycode, 0);
> +	if (camera_mode == SW_CAMERA_ON)
> +		input_report_switch(priv->idev, SW_CAMERA_LENS_COVER, 1);
> +	else if (camera_mode == SW_CAMERA_OFF)
> +		input_report_switch(priv->idev, SW_CAMERA_LENS_COVER, 0);

The second if statement is necessary, we already validated camera_mode before.

Thanks,
Armin Wolf

>   	input_sync(priv->idev);
>
>   	mutex_unlock(&priv->notify_lock);
> @@ -68,29 +94,12 @@ static void lenovo_wmi_notify(struct wmi_device *wdev, union acpi_object *obj)
>   static int lenovo_wmi_probe(struct wmi_device *wdev, const void *context)
>   {
>   	struct lenovo_wmi_priv *priv;
> -	int ret;
>
>   	priv = devm_kzalloc(&wdev->dev, sizeof(*priv), GFP_KERNEL);
>   	if (!priv)
>   		return -ENOMEM;
>
>   	dev_set_drvdata(&wdev->dev, priv);
> -
> -	priv->idev = devm_input_allocate_device(&wdev->dev);
> -	if (!priv->idev)
> -		return -ENOMEM;
> -
> -	priv->idev->name = "Lenovo WMI Camera Button";
> -	priv->idev->phys = "wmi/input0";
> -	priv->idev->id.bustype = BUS_HOST;
> -	priv->idev->dev.parent = &wdev->dev;
> -	input_set_capability(priv->idev, EV_KEY, KEY_CAMERA_ACCESS_ENABLE);
> -	input_set_capability(priv->idev, EV_KEY, KEY_CAMERA_ACCESS_DISABLE);
> -
> -	ret = input_register_device(priv->idev);
> -	if (ret)
> -		return ret;
> -
>   	mutex_init(&priv->notify_lock);
>
>   	return 0;