2024-02-29 05:16:50

by Ai Chao

[permalink] [raw]
Subject: [PATCH] platform/x86: add lenovo generic wmi driver

Add lenovo generic wmi driver to support camera button.

Signed-off-by: Ai Chao <[email protected]>
---
drivers/platform/x86/Kconfig | 10 +++
drivers/platform/x86/Makefile | 1 +
drivers/platform/x86/lenovo-wmi.c | 121 ++++++++++++++++++++++++++++++
3 files changed, 132 insertions(+)
create mode 100644 drivers/platform/x86/lenovo-wmi.c

diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
index bdd302274b9a..fbbb8fb843d7 100644
--- a/drivers/platform/x86/Kconfig
+++ b/drivers/platform/x86/Kconfig
@@ -1001,6 +1001,16 @@ config INSPUR_PLATFORM_PROFILE
To compile this driver as a module, choose M here: the module
will be called inspur-platform-profile.

+config LENOVO_WMI
+ tristate "Lenovo Geneirc WMI driver"
+ depends on ACPI_WMI
+ depends on INPUT
+ help
+ This driver provides support for Lenovo WMI driver.
+
+ To compile this driver as a module, choose M here: the module
+ will be called lenovo-wmi.
+
source "drivers/platform/x86/x86-android-tablets/Kconfig"

config FW_ATTR_CLASS
diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
index 1de432e8861e..d51086552192 100644
--- a/drivers/platform/x86/Makefile
+++ b/drivers/platform/x86/Makefile
@@ -66,6 +66,7 @@ obj-$(CONFIG_SENSORS_HDAPS) += hdaps.o
obj-$(CONFIG_THINKPAD_ACPI) += thinkpad_acpi.o
obj-$(CONFIG_THINKPAD_LMI) += think-lmi.o
obj-$(CONFIG_YOGABOOK) += lenovo-yogabook.o
+obj-$(CONFIG_LENOVO_WMI) += lenovo-wmi.o

# Intel
obj-y += intel/
diff --git a/drivers/platform/x86/lenovo-wmi.c b/drivers/platform/x86/lenovo-wmi.c
new file mode 100644
index 000000000000..e8b1c401b53e
--- /dev/null
+++ b/drivers/platform/x86/lenovo-wmi.c
@@ -0,0 +1,121 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Lenovo Generic WMI Driver
+ *
+ * Copyright (C) 2018 Ai Chao <[email protected]>
+ */
+
+#include <linux/acpi.h>
+#include <linux/device.h>
+#include <linux/input.h>
+#include <linux/module.h>
+#include <linux/wmi.h>
+
+#define WMI_LENOVO_CAMERABUTTON_EVENT_GUID "50C76F1F-D8E4-D895-0A3D-62F4EA400013"
+
+static u8 camera_mode;
+
+struct lenovo_wmi_priv {
+ struct input_dev *idev;
+};
+
+static ssize_t camerabutton_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ return sprintf(buf, "%u\n", camera_mode);
+}
+
+DEVICE_ATTR_RO(camerabutton);
+
+static struct attribute *lenovo_wmi_attrs[] = {
+ &dev_attr_camerabutton.attr,
+ NULL,
+};
+
+static const struct attribute_group lenovo_wmi_group = {
+ .attrs = lenovo_wmi_attrs,
+};
+
+const struct attribute_group *lenovo_wmi_groups[] = {
+ &lenovo_wmi_group,
+ NULL,
+};
+
+static void lenovo_wmi_notify(struct wmi_device *wdev, union acpi_object *obj)
+{
+ struct lenovo_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
+
+ if (obj->type == ACPI_TYPE_BUFFER) {
+ camera_mode = obj->buffer.pointer[0];
+ input_report_key(priv->idev, KEY_CAMERA, 1);
+ input_sync(priv->idev);
+ input_report_key(priv->idev, KEY_CAMERA, 0);
+ input_sync(priv->idev);
+ } else {
+ dev_info(&wdev->dev, "Bad response type %d\n", obj->type);
+ }
+}
+
+static int lenovo_wmi_input_setup(struct wmi_device *wdev)
+{
+ struct lenovo_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
+
+ 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;
+ priv->idev->evbit[0] = BIT_MASK(EV_KEY);
+ priv->idev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
+
+ return input_register_device(priv->idev);
+}
+
+static int lenovo_wmi_probe(struct wmi_device *wdev, const void *context)
+{
+ struct lenovo_wmi_priv *priv;
+ int err;
+
+ priv = devm_kzalloc(&wdev->dev, sizeof(struct lenovo_wmi_priv),
+ GFP_KERNEL);
+ if (!priv)
+ return -ENOMEM;
+
+ dev_set_drvdata(&wdev->dev, priv);
+
+ err = lenovo_wmi_input_setup(wdev);
+ return err;
+}
+
+static void lenovo_wmi_remove(struct wmi_device *wdev)
+{
+ struct lenovo_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
+
+ input_unregister_device(priv->idev);
+}
+
+static const struct wmi_device_id lenovo_wmi_id_table[] = {
+ { .guid_string = WMI_LENOVO_CAMERABUTTON_EVENT_GUID },
+ { }
+};
+
+static struct wmi_driver lenovo_wmi_driver = {
+ .driver = {
+ .name = "lenovo-wmi",
+ .dev_groups = lenovo_wmi_groups,
+ },
+ .id_table = lenovo_wmi_id_table,
+ .probe = lenovo_wmi_probe,
+ .notify = lenovo_wmi_notify,
+ .remove = lenovo_wmi_remove,
+};
+
+module_wmi_driver(lenovo_wmi_driver);
+
+MODULE_DEVICE_TABLE(wmi, lenovo_wmi_id_table);
+MODULE_AUTHOR("Ai Chao <[email protected]>");
+MODULE_DESCRIPTION("Lenovo Generic WMI Driver");
+MODULE_LICENSE("GPL v2");
--
2.25.1



Subject: Re: [PATCH] platform/x86: add lenovo generic wmi driver


On 2/28/24 9:16 PM, Ai Chao wrote:
> Add lenovo generic wmi driver to support camera button.
>
> Signed-off-by: Ai Chao <[email protected]>
> ---
> drivers/platform/x86/Kconfig | 10 +++
> drivers/platform/x86/Makefile | 1 +
> drivers/platform/x86/lenovo-wmi.c | 121 ++++++++++++++++++++++++++++++
> 3 files changed, 132 insertions(+)
> create mode 100644 drivers/platform/x86/lenovo-wmi.c
>
> diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
> index bdd302274b9a..fbbb8fb843d7 100644
> --- a/drivers/platform/x86/Kconfig
> +++ b/drivers/platform/x86/Kconfig
> @@ -1001,6 +1001,16 @@ config INSPUR_PLATFORM_PROFILE
> To compile this driver as a module, choose M here: the module
> will be called inspur-platform-profile.
>
> +config LENOVO_WMI
> + tristate "Lenovo Geneirc WMI driver"
> + depends on ACPI_WMI
> + depends on INPUT
> + help
> + This driver provides support for Lenovo WMI driver.
> +
> + To compile this driver as a module, choose M here: the module
> + will be called lenovo-wmi.
> +
> source "drivers/platform/x86/x86-android-tablets/Kconfig"
>
> config FW_ATTR_CLASS
> diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
> index 1de432e8861e..d51086552192 100644
> --- a/drivers/platform/x86/Makefile
> +++ b/drivers/platform/x86/Makefile
> @@ -66,6 +66,7 @@ obj-$(CONFIG_SENSORS_HDAPS) += hdaps.o
> obj-$(CONFIG_THINKPAD_ACPI) += thinkpad_acpi.o
> obj-$(CONFIG_THINKPAD_LMI) += think-lmi.o
> obj-$(CONFIG_YOGABOOK) += lenovo-yogabook.o
> +obj-$(CONFIG_LENOVO_WMI) += lenovo-wmi.o
>
> # Intel
> obj-y += intel/
> diff --git a/drivers/platform/x86/lenovo-wmi.c b/drivers/platform/x86/lenovo-wmi.c
> new file mode 100644
> index 000000000000..e8b1c401b53e
> --- /dev/null
> +++ b/drivers/platform/x86/lenovo-wmi.c
> @@ -0,0 +1,121 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Lenovo Generic WMI Driver
> + *
> + * Copyright (C) 2018 Ai Chao <[email protected]>
2024 ?
> + */
> +
> +#include <linux/acpi.h>
> +#include <linux/device.h>
> +#include <linux/input.h>
> +#include <linux/module.h>
> +#include <linux/wmi.h>
> +
> +#define WMI_LENOVO_CAMERABUTTON_EVENT_GUID "50C76F1F-D8E4-D895-0A3D-62F4EA400013"
> +
> +static u8 camera_mode;
> +
> +struct lenovo_wmi_priv {
> + struct input_dev *idev;
> +};
> +
> +static ssize_t camerabutton_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + return sprintf(buf, "%u\n", camera_mode);
> +}
> +
> +DEVICE_ATTR_RO(camerabutton);
> +
> +static struct attribute *lenovo_wmi_attrs[] = {
> + &dev_attr_camerabutton.attr,
> + NULL,
> +};
> +
> +static const struct attribute_group lenovo_wmi_group = {
> + .attrs = lenovo_wmi_attrs,
> +};
> +
> +const struct attribute_group *lenovo_wmi_groups[] = {
> + &lenovo_wmi_group,
> + NULL,
> +};
> +
> +static void lenovo_wmi_notify(struct wmi_device *wdev, union acpi_object *obj)
> +{
> + struct lenovo_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
> +
> + if (obj->type == ACPI_TYPE_BUFFER) {
> + camera_mode = obj->buffer.pointer[0];
> + input_report_key(priv->idev, KEY_CAMERA, 1);
> + input_sync(priv->idev);
> + input_report_key(priv->idev, KEY_CAMERA, 0);
> + input_sync(priv->idev);
> + } else {
> + dev_info(&wdev->dev, "Bad response type %d\n", obj->type);
Does it needs to be dev_info? I don't think this affects the driver functionality, right?

May be dev_dbg() is better here?
> + }
> +}
> +
> +static int lenovo_wmi_input_setup(struct wmi_device *wdev)
> +{
> + struct lenovo_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
> +
> + 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;
> + priv->idev->evbit[0] = BIT_MASK(EV_KEY);
> + priv->idev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
> +
> + return input_register_device(priv->idev);
> +}
> +
> +static int lenovo_wmi_probe(struct wmi_device *wdev, const void *context)
> +{
> + struct lenovo_wmi_priv *priv;
> + int err;
> +
> + priv = devm_kzalloc(&wdev->dev, sizeof(struct lenovo_wmi_priv),
> + GFP_KERNEL);
> + if (!priv)
> + return -ENOMEM;
> +
> + dev_set_drvdata(&wdev->dev, priv);
> +
> + err = lenovo_wmi_input_setup(wdev);
> + return err;
> +}
> +
> +static void lenovo_wmi_remove(struct wmi_device *wdev)
> +{
> + struct lenovo_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
> +
> + input_unregister_device(priv->idev);

Since you use devm_input_allocate_device() for allocation, you don't need to explicitly unregister the device.
> +}
> +
> +static const struct wmi_device_id lenovo_wmi_id_table[] = {
> + { .guid_string = WMI_LENOVO_CAMERABUTTON_EVENT_GUID },
> + { }
> +};
> +
> +static struct wmi_driver lenovo_wmi_driver = {
> + .driver = {
> + .name = "lenovo-wmi",
> + .dev_groups = lenovo_wmi_groups,
> + },
> + .id_table = lenovo_wmi_id_table,
> + .probe = lenovo_wmi_probe,
> + .notify = lenovo_wmi_notify,
> + .remove = lenovo_wmi_remove,
> +};
> +
> +module_wmi_driver(lenovo_wmi_driver);
> +
> +MODULE_DEVICE_TABLE(wmi, lenovo_wmi_id_table);
> +MODULE_AUTHOR("Ai Chao <[email protected]>");
> +MODULE_DESCRIPTION("Lenovo Generic WMI Driver");
> +MODULE_LICENSE("GPL v2");

--
Sathyanarayanan Kuppuswamy
Linux Kernel Developer


2024-02-29 12:56:54

by Ilpo Järvinen

[permalink] [raw]
Subject: Re: [PATCH] platform/x86: add lenovo generic wmi driver

On Thu, 29 Feb 2024, Ai Chao wrote:

> Add lenovo generic wmi driver to support camera button.
>
> Signed-off-by: Ai Chao <[email protected]>
> ---
> drivers/platform/x86/Kconfig | 10 +++
> drivers/platform/x86/Makefile | 1 +
> drivers/platform/x86/lenovo-wmi.c | 121 ++++++++++++++++++++++++++++++
> 3 files changed, 132 insertions(+)
> create mode 100644 drivers/platform/x86/lenovo-wmi.c
>
> diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
> index bdd302274b9a..fbbb8fb843d7 100644
> --- a/drivers/platform/x86/Kconfig
> +++ b/drivers/platform/x86/Kconfig
> @@ -1001,6 +1001,16 @@ config INSPUR_PLATFORM_PROFILE
> To compile this driver as a module, choose M here: the module
> will be called inspur-platform-profile.
>
> +config LENOVO_WMI
> + tristate "Lenovo Geneirc WMI driver"
> + depends on ACPI_WMI
> + depends on INPUT
> + help
> + This driver provides support for Lenovo WMI driver.
> +
> + To compile this driver as a module, choose M here: the module
> + will be called lenovo-wmi.
> +
> source "drivers/platform/x86/x86-android-tablets/Kconfig"
>
> config FW_ATTR_CLASS
> diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
> index 1de432e8861e..d51086552192 100644
> --- a/drivers/platform/x86/Makefile
> +++ b/drivers/platform/x86/Makefile
> @@ -66,6 +66,7 @@ obj-$(CONFIG_SENSORS_HDAPS) += hdaps.o
> obj-$(CONFIG_THINKPAD_ACPI) += thinkpad_acpi.o
> obj-$(CONFIG_THINKPAD_LMI) += think-lmi.o
> obj-$(CONFIG_YOGABOOK) += lenovo-yogabook.o
> +obj-$(CONFIG_LENOVO_WMI) += lenovo-wmi.o
>
> # Intel
> obj-y += intel/
> diff --git a/drivers/platform/x86/lenovo-wmi.c b/drivers/platform/x86/lenovo-wmi.c
> new file mode 100644
> index 000000000000..e8b1c401b53e
> --- /dev/null
> +++ b/drivers/platform/x86/lenovo-wmi.c
> @@ -0,0 +1,121 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Lenovo Generic WMI Driver
> + *
> + * Copyright (C) 2018 Ai Chao <[email protected]>
> + */
> +
> +#include <linux/acpi.h>
> +#include <linux/device.h>
> +#include <linux/input.h>
> +#include <linux/module.h>
> +#include <linux/wmi.h>
> +
> +#define WMI_LENOVO_CAMERABUTTON_EVENT_GUID "50C76F1F-D8E4-D895-0A3D-62F4EA400013"
> +
> +static u8 camera_mode;
> +
> +struct lenovo_wmi_priv {
> + struct input_dev *idev;
> +};
> +
> +static ssize_t camerabutton_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + return sprintf(buf, "%u\n", camera_mode);

sysfs_emit()

> +}
> +
> +DEVICE_ATTR_RO(camerabutton);
> +
> +static struct attribute *lenovo_wmi_attrs[] = {
> + &dev_attr_camerabutton.attr,
> + NULL,
> +};
> +
> +static const struct attribute_group lenovo_wmi_group = {
> + .attrs = lenovo_wmi_attrs,
> +};
> +
> +const struct attribute_group *lenovo_wmi_groups[] = {
> + &lenovo_wmi_group,
> + NULL,
> +};
> +
> +static void lenovo_wmi_notify(struct wmi_device *wdev, union acpi_object *obj)
> +{
> + struct lenovo_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
> +
> + if (obj->type == ACPI_TYPE_BUFFER) {
> + camera_mode = obj->buffer.pointer[0];
> + input_report_key(priv->idev, KEY_CAMERA, 1);
> + input_sync(priv->idev);
> + input_report_key(priv->idev, KEY_CAMERA, 0);
> + input_sync(priv->idev);
> + } else {
> + dev_info(&wdev->dev, "Bad response type %d\n", obj->type);
> + }
> +}
> +
> +static int lenovo_wmi_input_setup(struct wmi_device *wdev)
> +{
> + struct lenovo_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
> +
> + 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;
> + priv->idev->evbit[0] = BIT_MASK(EV_KEY);
> + priv->idev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
> +
> + return input_register_device(priv->idev);
> +}
> +
> +static int lenovo_wmi_probe(struct wmi_device *wdev, const void *context)
> +{
> + struct lenovo_wmi_priv *priv;
> + int err;
> +
> + priv = devm_kzalloc(&wdev->dev, sizeof(struct lenovo_wmi_priv),
> + GFP_KERNEL);
> + if (!priv)
> + return -ENOMEM;
> +
> + dev_set_drvdata(&wdev->dev, priv);
> +
> + err = lenovo_wmi_input_setup(wdev);
> + return err;
> +}
> +
> +static void lenovo_wmi_remove(struct wmi_device *wdev)
> +{
> + struct lenovo_wmi_priv *priv = dev_get_drvdata(&wdev->dev);
> +
> + input_unregister_device(priv->idev);
> +}
> +
> +static const struct wmi_device_id lenovo_wmi_id_table[] = {
> + { .guid_string = WMI_LENOVO_CAMERABUTTON_EVENT_GUID },
> + { }
> +};
> +
> +static struct wmi_driver lenovo_wmi_driver = {
> + .driver = {
> + .name = "lenovo-wmi",
> + .dev_groups = lenovo_wmi_groups,
> + },
> + .id_table = lenovo_wmi_id_table,
> + .probe = lenovo_wmi_probe,
> + .notify = lenovo_wmi_notify,
> + .remove = lenovo_wmi_remove,
> +};
> +
> +module_wmi_driver(lenovo_wmi_driver);
> +
> +MODULE_DEVICE_TABLE(wmi, lenovo_wmi_id_table);
> +MODULE_AUTHOR("Ai Chao <[email protected]>");
> +MODULE_DESCRIPTION("Lenovo Generic WMI Driver");
> +MODULE_LICENSE("GPL v2");

"GPL" is enough for MODULE_LICENSE, more specific information is given
only within the SPDX header.

--
i.