diff mbox series

platform/x86:lenovo-wmi-camera: Use SW_CAMERA_LENS_COVER instead of KEY_CAMERA_ACESS

Message ID 20241218060341.2496642-1-aichao@kylinos.cn (mailing list archive)
State Changes Requested, archived
Headers show
Series platform/x86:lenovo-wmi-camera: Use SW_CAMERA_LENS_COVER instead of KEY_CAMERA_ACESS | expand

Commit Message

Ai Chao Dec. 18, 2024, 6:03 a.m. UTC
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>
---
 drivers/platform/x86/lenovo-wmi-camera.c | 139 +++++++++++------------
 1 file changed, 66 insertions(+), 73 deletions(-)

Comments

Hans de Goede Dec. 18, 2024, 11:01 a.m. UTC | #1
Hi Ai,

On 18-Dec-24 7:03 AM, Ai Chao wrote:
> 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>

Thank you for your patch, but this patch does a lot more then
just switching to instantiating the input device on the first
event and reporting SW_CAMERA_LENS_COVER.

You are also switching away from using the new WMI bus /
wmi_driver model to using the old deprecated please WMI
functions.

Please post a new version which keeps the wmi_driver and
only delays the input-device registration and switches
to reporting SW_CAMERA_LENS_COVER.

Regards,

Hans



> ---
>  drivers/platform/x86/lenovo-wmi-camera.c | 139 +++++++++++------------
>  1 file changed, 66 insertions(+), 73 deletions(-)
> 
> diff --git a/drivers/platform/x86/lenovo-wmi-camera.c b/drivers/platform/x86/lenovo-wmi-camera.c
> index 0c0bedaf7407..fc9efd5beda8 100644
> --- a/drivers/platform/x86/lenovo-wmi-camera.c
> +++ b/drivers/platform/x86/lenovo-wmi-camera.c
> @@ -16,29 +16,52 @@
>  
>  #define WMI_LENOVO_CAMERABUTTON_EVENT_GUID "50C76F1F-D8E4-D895-0A3D-62F4EA400013"
>  
> -struct lenovo_wmi_priv {
> -	struct input_dev *idev;
> -	struct mutex notify_lock;	/* lenovo WMI camera button notify lock */
> -};
> +static struct input_dev *camera_shutter_input_dev;
> +static struct mutex notify_lock; /* lenovo WMI camera button notify lock */
>  
>  enum {
>  	SW_CAMERA_OFF	= 0,
>  	SW_CAMERA_ON	= 1,
>  };
>  
> -static void lenovo_wmi_notify(struct wmi_device *wdev, union acpi_object *obj)
> +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 = "Lenovo WMI Camera Button";
> +	camera_shutter_input_dev->phys = "wmi/input0";
> +	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);
> +
> +	err = input_register_device(camera_shutter_input_dev);
> +	if (err) {
> +		input_free_device(camera_shutter_input_dev);
> +		camera_shutter_input_dev = NULL;
> +		return err;
> +	}
> +
> +	return 0;
> +}
> +
> +static void lenovo_wmi_notify(union acpi_object *obj, void *context)
>  {
> -	struct lenovo_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
> -	unsigned int keycode;
> -	u8 camera_mode;
> +	u32 event_data;
>  
> +	if (!obj)
> +		return;
>  	if (obj->type != ACPI_TYPE_BUFFER) {
> -		dev_err(&wdev->dev, "Bad response type %u\n", obj->type);
> +		pr_info("Unknown response received %d\n", obj->type);
>  		return;
>  	}
>  
>  	if (obj->buffer.length != 1) {
> -		dev_err(&wdev->dev, "Invalid buffer length %u\n", obj->buffer.length);
> +		pr_info("Invalid buffer length %u\n", obj->buffer.length);
>  		return;
>  	}
>  
> @@ -47,80 +70,50 @@ static void lenovo_wmi_notify(struct wmi_device *wdev, union acpi_object *obj)
>  	 *      0 camera close
>  	 *      1 camera open
>  	 */
> -	camera_mode = obj->buffer.pointer[0];
> -	if (camera_mode > SW_CAMERA_ON) {
> -		dev_err(&wdev->dev, "Unknown camera mode %u\n", camera_mode);
> -		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);
> -	input_sync(priv->idev);
> -
> -	mutex_unlock(&priv->notify_lock);
> +	event_data = obj->buffer.pointer[0];
> +
> +	mutex_lock(&notify_lock);
> +	if (!camera_shutter_input_dev)
> +		if (camera_shutter_input_setup()) {
> +			pr_err("Failed to setup camera shutter input device\n");
> +			mutex_unlock(&notify_lock);
> +			return;
> +		}
> +
> +	if (event_data == SW_CAMERA_ON)
> +		input_report_switch(camera_shutter_input_dev, SW_CAMERA_LENS_COVER, 1);
> +	else if (event_data == SW_CAMERA_OFF)
> +		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);
> +
> +	mutex_unlock(&notify_lock);
>  }
>  
> -static int lenovo_wmi_probe(struct wmi_device *wdev, const void *context)
> +static __init int lenovo_wmi_init(void)
>  {
> -	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);
> +	int event_capable = wmi_has_guid(WMI_LENOVO_CAMERABUTTON_EVENT_GUID);
> +	acpi_status status;
>  
> -	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);
> +	if (!event_capable)
> +		return -ENODEV;
>  
> -	ret = input_register_device(priv->idev);
> -	if (ret)
> -		return ret;
> -
> -	mutex_init(&priv->notify_lock);
> +	status = wmi_install_notify_handler(WMI_LENOVO_CAMERABUTTON_EVENT_GUID,
> +					    lenovo_wmi_notify, NULL);
> +	if (ACPI_FAILURE(status))
> +		return -EIO;
>  
>  	return 0;
>  }
> +module_init(lenovo_wmi_init);
>  
> -static void lenovo_wmi_remove(struct wmi_device *wdev)
> +static __exit void lenovo_wmi_exit(void)
>  {
> -	struct lenovo_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
> -
> -	mutex_destroy(&priv->notify_lock);
> +	if (camera_shutter_input_dev)
> +		input_unregister_device(camera_shutter_input_dev);
>  }
> -
> -static const struct wmi_device_id lenovo_wmi_id_table[] = {
> -	{ .guid_string = WMI_LENOVO_CAMERABUTTON_EVENT_GUID },
> -	{  }
> -};
> -MODULE_DEVICE_TABLE(wmi, lenovo_wmi_id_table);
> -
> -static struct wmi_driver lenovo_wmi_driver = {
> -	.driver = {
> -		.name = "lenovo-wmi-camera",
> -		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
> -	},
> -	.id_table = lenovo_wmi_id_table,
> -	.no_singleton = true,
> -	.probe = lenovo_wmi_probe,
> -	.notify = lenovo_wmi_notify,
> -	.remove = lenovo_wmi_remove,
> -};
> -module_wmi_driver(lenovo_wmi_driver);
> +module_exit(lenovo_wmi_exit);
>  
>  MODULE_AUTHOR("Ai Chao <aichao@kylinos.cn>");
>  MODULE_DESCRIPTION("Lenovo WMI Camera Button Driver");
diff mbox series

Patch

diff --git a/drivers/platform/x86/lenovo-wmi-camera.c b/drivers/platform/x86/lenovo-wmi-camera.c
index 0c0bedaf7407..fc9efd5beda8 100644
--- a/drivers/platform/x86/lenovo-wmi-camera.c
+++ b/drivers/platform/x86/lenovo-wmi-camera.c
@@ -16,29 +16,52 @@ 
 
 #define WMI_LENOVO_CAMERABUTTON_EVENT_GUID "50C76F1F-D8E4-D895-0A3D-62F4EA400013"
 
-struct lenovo_wmi_priv {
-	struct input_dev *idev;
-	struct mutex notify_lock;	/* lenovo WMI camera button notify lock */
-};
+static struct input_dev *camera_shutter_input_dev;
+static struct mutex notify_lock; /* lenovo WMI camera button notify lock */
 
 enum {
 	SW_CAMERA_OFF	= 0,
 	SW_CAMERA_ON	= 1,
 };
 
-static void lenovo_wmi_notify(struct wmi_device *wdev, union acpi_object *obj)
+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 = "Lenovo WMI Camera Button";
+	camera_shutter_input_dev->phys = "wmi/input0";
+	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);
+
+	err = input_register_device(camera_shutter_input_dev);
+	if (err) {
+		input_free_device(camera_shutter_input_dev);
+		camera_shutter_input_dev = NULL;
+		return err;
+	}
+
+	return 0;
+}
+
+static void lenovo_wmi_notify(union acpi_object *obj, void *context)
 {
-	struct lenovo_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
-	unsigned int keycode;
-	u8 camera_mode;
+	u32 event_data;
 
+	if (!obj)
+		return;
 	if (obj->type != ACPI_TYPE_BUFFER) {
-		dev_err(&wdev->dev, "Bad response type %u\n", obj->type);
+		pr_info("Unknown response received %d\n", obj->type);
 		return;
 	}
 
 	if (obj->buffer.length != 1) {
-		dev_err(&wdev->dev, "Invalid buffer length %u\n", obj->buffer.length);
+		pr_info("Invalid buffer length %u\n", obj->buffer.length);
 		return;
 	}
 
@@ -47,80 +70,50 @@  static void lenovo_wmi_notify(struct wmi_device *wdev, union acpi_object *obj)
 	 *      0 camera close
 	 *      1 camera open
 	 */
-	camera_mode = obj->buffer.pointer[0];
-	if (camera_mode > SW_CAMERA_ON) {
-		dev_err(&wdev->dev, "Unknown camera mode %u\n", camera_mode);
-		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);
-	input_sync(priv->idev);
-
-	mutex_unlock(&priv->notify_lock);
+	event_data = obj->buffer.pointer[0];
+
+	mutex_lock(&notify_lock);
+	if (!camera_shutter_input_dev)
+		if (camera_shutter_input_setup()) {
+			pr_err("Failed to setup camera shutter input device\n");
+			mutex_unlock(&notify_lock);
+			return;
+		}
+
+	if (event_data == SW_CAMERA_ON)
+		input_report_switch(camera_shutter_input_dev, SW_CAMERA_LENS_COVER, 1);
+	else if (event_data == SW_CAMERA_OFF)
+		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);
+
+	mutex_unlock(&notify_lock);
 }
 
-static int lenovo_wmi_probe(struct wmi_device *wdev, const void *context)
+static __init int lenovo_wmi_init(void)
 {
-	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);
+	int event_capable = wmi_has_guid(WMI_LENOVO_CAMERABUTTON_EVENT_GUID);
+	acpi_status status;
 
-	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);
+	if (!event_capable)
+		return -ENODEV;
 
-	ret = input_register_device(priv->idev);
-	if (ret)
-		return ret;
-
-	mutex_init(&priv->notify_lock);
+	status = wmi_install_notify_handler(WMI_LENOVO_CAMERABUTTON_EVENT_GUID,
+					    lenovo_wmi_notify, NULL);
+	if (ACPI_FAILURE(status))
+		return -EIO;
 
 	return 0;
 }
+module_init(lenovo_wmi_init);
 
-static void lenovo_wmi_remove(struct wmi_device *wdev)
+static __exit void lenovo_wmi_exit(void)
 {
-	struct lenovo_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
-
-	mutex_destroy(&priv->notify_lock);
+	if (camera_shutter_input_dev)
+		input_unregister_device(camera_shutter_input_dev);
 }
-
-static const struct wmi_device_id lenovo_wmi_id_table[] = {
-	{ .guid_string = WMI_LENOVO_CAMERABUTTON_EVENT_GUID },
-	{  }
-};
-MODULE_DEVICE_TABLE(wmi, lenovo_wmi_id_table);
-
-static struct wmi_driver lenovo_wmi_driver = {
-	.driver = {
-		.name = "lenovo-wmi-camera",
-		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
-	},
-	.id_table = lenovo_wmi_id_table,
-	.no_singleton = true,
-	.probe = lenovo_wmi_probe,
-	.notify = lenovo_wmi_notify,
-	.remove = lenovo_wmi_remove,
-};
-module_wmi_driver(lenovo_wmi_driver);
+module_exit(lenovo_wmi_exit);
 
 MODULE_AUTHOR("Ai Chao <aichao@kylinos.cn>");
 MODULE_DESCRIPTION("Lenovo WMI Camera Button Driver");