Skip to contentSkip to conversation

platform/x86: lenovo-wmi-other: Add raw GetDataByPackage interface

PATCH Superseded Alan Pluto 4 participants 16 messages Nov 16 – Nov 24

Subsystems (5)
lore
Share & tools
lore

Alan Pluto

author
#0 lore raw

PATCH 1/1 platform/x86: lenovo-wmi-other: Add raw GetDataByPackage interface

Expose Lenovo Other Mode WMI GetDataByPackage, method 99, through a root-only character device so userspace can access the raw firmware interface. And document the interface.

The upstream driver was build-tested with W=1 and checked with Sparse. A backport using the five-argument wmidev_invoke_method() interface was tested on Ubuntu 7.0.0-42 with Lenovo hardware. The upstream 7.4 driver has not been tested on hardware.

---
 Documentation/ABI/testing/dev-tac             |  49 +++
 .../wmi/devices/lenovo-wmi-other.rst          |   8 +
 MAINTAINERS                                   |   1 +
 drivers/platform/x86/lenovo/wmi-other.c       | 325 ++++++++++++++++++
 4 files changed, 383 insertions(+)
 create mode 100644 Documentation/ABI/testing/dev-tac
Documentation/ABI/testing/dev-tac new file +49 -0
new file mode 100644
index 00000000000..a8be8e95e2d
@@ -0,0 +1,49 @@
+What:           /dev/tac[0-9]*
+Date:           November 2026
+Contact:        [email protected]
+Description:
+                Lenovo Other Mode WMI GetDataByPackage (method 99).
+                Each firmware WMI instance gets a misc character device with
+                mode 0600. Numbering follows allocation order, not necessarily
+                the firmware instance number. Registration does not probe
+                support for method 99. The parent is the bound WMI device.
+
+                Open with O_RDWR. One write submits one complete request of
+                1..100 raw bytes; shorter requests are padded with zeroes to
+                100 bytes. Empty and oversized writes fail with EINVAL without
+                calling firmware. Successful writes return the full submitted
+                length and execute two identical WMI calls for the BIOS
+                duplicate-call workaround. Request pairs through this
+                interface are serialized; calls made by other driver
+                interfaces are not covered by this serialization.
+                Evaluation failures abort the pair; the driver does not
+                automatically retry.
+
+                Each open file description owns its response. A write while
+                that response remains unread fails with EBUSY, including after
+                partial reads. A new open cannot read another open's response.
+                Descriptors shared by dup/fork share the same transaction;
+                applications must coordinate the complete write/read sequence.
+
+                Read returns little-endian u32 DataSize followed by exactly
+                DataSize data bytes. Response storage is erased when fully
+                read or closed. Further reads return EOF until a new successful
+                write. Before a successful write, or after a failed firmware
+                evaluation, reads return ENODATA. A zero-length read returns
+                zero without consuming data.
+
+                Requests are synchronous even with O_NONBLOCK. Seeking and
+                positional I/O are not supported. There is no ioctl interface.
+                A removed WMI device causes nonempty reads and valid writes
+                on existing descriptors to fail with ENODEV.
+
+                Firmware evaluation failures map to EIO. WMI marshalling
+                failures return the error reported by the WMI subsystem.
+                A truncated response or invalid DataSize maps to EPROTO.
+                Bad userspace memory maps to EFAULT. Failure does not imply
+                firmware had no side effects; never blindly resubmit commands.
+                Interrupted waits can return EINTR before firmware evaluation.
+
+                The WMI subsystem converts returned ACPI objects to a buffer.
+                This raw firmware interface may change device state. Use only
+                commands whose behavior and side effects are understood.
Documentation/wmi/devices/lenovo-wmi-other.rst +8 -0
index 011054d64ea..ec85fdeeb7a 100644
@@ -33,6 +33,10 @@ under the following path:
 
 Additionally, this driver also exports attributes to HWMON.
 
+The driver also exposes GetDataByPackage (method 99) through a raw character
+device for each WMI instance. See Documentation/ABI/testing/dev-tac for the
+request and response format and its access restrictions.
+
 LENOVO_CAPABILITY_DATA_00
 -------------------------
 
@@ -109,6 +113,10 @@ WMI interface description
 The WMI interface description can be decoded from the embedded binary MOF (bmof)
 data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility:
 
+The example below describes an older firmware revision and specifies a
+40-byte maximum input for GetDataByPackage. Newer firmware uses a 100-byte
+input, which is the size used by the raw interface described above.
+
 ::
 
   [WMI, Dynamic, Provider("WmiProv"), Locale("MS\\0x409"), Description("LENOVO_OTHER_METHOD class"), guid("{dc2a8805-3a8c-41ba-a6f7-092e0089cd3b}")]
MAINTAINERS +1 -0
index 981eb3fdbef..84b1545f525 100644
@@ -15168,6 +15168,7 @@ M:	Derek J. Clark <[email protected]>
 M:	Rong Zhang <[email protected]>
 L:	[email protected]
 S:	Maintained
+F:	Documentation/ABI/testing/dev-tac
 F:	Documentation/wmi/devices/lenovo-wmi-gamezone.rst
 F:	Documentation/wmi/devices/lenovo-wmi-other.rst
 F:	drivers/platform/x86/lenovo/*
drivers/platform/x86/lenovo/wmi-other.c +325 -0

371 lines not on this page · raw message

base-commit: 5680519168f65666d2c03cc6ed8fe2b7052aee12

Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 150 lines · Alan Pluto

Expose Lenovo Other Mode WMI GetDataByPackage, method 99, through a root-only character device so userspace can access the raw firmware interface. And document the interface.

The upstream driver was build-tested with W=1 and checked with Sparse. A backport using the five-argument wmidev_invoke_method() interface was tested on Ubuntu 7.0.0-42 with Lenovo hardware. The upstream 7.4 driver has not been tested on hardware.

---
 Documentation/ABI/testing/dev-tac             |  49 +++
 .../wmi/devices/lenovo-wmi-other.rst          |   8 +
 MAINTAINERS                                   |   1 +
 drivers/platform/x86/lenovo/wmi-other.c       | 325 ++++++++++++++++++
 4 files changed, 383 insertions(+)
 create mode 100644 Documentation/ABI/testing/dev-tac
Documentation/ABI/testing/dev-tac new file +49 -0
new file mode 100644
index 00000000000..a8be8e95e2d
@@ -0,0 +1,49 @@
+What:           /dev/tac[0-9]*
+Date:           November 2026
+Contact:        [email protected]
+Description:
+                Lenovo Other Mode WMI GetDataByPackage (method 99).
+                Each firmware WMI instance gets a misc character device with
+                mode 0600. Numbering follows allocation order, not necessarily
+                the firmware instance number. Registration does not probe
+                support for method 99. The parent is the bound WMI device.
+
+                Open with O_RDWR. One write submits one complete request of
+                1..100 raw bytes; shorter requests are padded with zeroes to
+                100 bytes. Empty and oversized writes fail with EINVAL without
+                calling firmware. Successful writes return the full submitted
+                length and execute two identical WMI calls for the BIOS
+                duplicate-call workaround. Request pairs through this
+                interface are serialized; calls made by other driver
+                interfaces are not covered by this serialization.
+                Evaluation failures abort the pair; the driver does not
+                automatically retry.
+
+                Each open file description owns its response. A write while
+                that response remains unread fails with EBUSY, including after
+                partial reads. A new open cannot read another open's response.
+                Descriptors shared by dup/fork share the same transaction;
+                applications must coordinate the complete write/read sequence.
+
+                Read returns little-endian u32 DataSize followed by exactly
+                DataSize data bytes. Response storage is erased when fully
+                read or closed. Further reads return EOF until a new successful
+                write. Before a successful write, or after a failed firmware
+                evaluation, reads return ENODATA. A zero-length read returns
+                zero without consuming data.
+
+                Requests are synchronous even with O_NONBLOCK. Seeking and
+                positional I/O are not supported. There is no ioctl interface.
+                A removed WMI device causes nonempty reads and valid writes
+                on existing descriptors to fail with ENODEV.
+
+                Firmware evaluation failures map to EIO. WMI marshalling
+                failures return the error reported by the WMI subsystem.
+                A truncated response or invalid DataSize maps to EPROTO.
+                Bad userspace memory maps to EFAULT. Failure does not imply
+                firmware had no side effects; never blindly resubmit commands.
+                Interrupted waits can return EINTR before firmware evaluation.
+
+                The WMI subsystem converts returned ACPI objects to a buffer.
+                This raw firmware interface may change device state. Use only
+                commands whose behavior and side effects are understood.
Documentation/wmi/devices/lenovo-wmi-other.rst +8 -0
index 011054d64ea..ec85fdeeb7a 100644
@@ -33,6 +33,10 @@ under the following path:
 
 Additionally, this driver also exports attributes to HWMON.
 
+The driver also exposes GetDataByPackage (method 99) through a raw character
+device for each WMI instance. See Documentation/ABI/testing/dev-tac for the
+request and response format and its access restrictions.
+
 LENOVO_CAPABILITY_DATA_00
 -------------------------
 
@@ -109,6 +113,10 @@ WMI interface description
 The WMI interface description can be decoded from the embedded binary MOF (bmof)
 data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility:
 
+The example below describes an older firmware revision and specifies a
+40-byte maximum input for GetDataByPackage. Newer firmware uses a 100-byte
+input, which is the size used by the raw interface described above.
+
 ::
 
   [WMI, Dynamic, Provider("WmiProv"), Locale("MS\\0x409"), Description("LENOVO_OTHER_METHOD class"), guid("{dc2a8805-3a8c-41ba-a6f7-092e0089cd3b}")]
MAINTAINERS +1 -0
index 981eb3fdbef..84b1545f525 100644
@@ -15168,6 +15168,7 @@ M:	Derek J. Clark <[email protected]>
 M:	Rong Zhang <[email protected]>
 L:	[email protected]
 S:	Maintained
+F:	Documentation/ABI/testing/dev-tac
 F:	Documentation/wmi/devices/lenovo-wmi-gamezone.rst
 F:	Documentation/wmi/devices/lenovo-wmi-other.rst
 F:	drivers/platform/x86/lenovo/*
drivers/platform/x86/lenovo/wmi-other.c +10 -0
index bd64e7b4466..c15b3a2a758 100644
@@ -34,16 +34,23 @@
 #include <linux/device.h>
 #include <linux/export.h>
 #include <linux/firmware_attributes.h>
+#include <linux/fs.h>
 #include <linux/gfp_types.h>
 #include <linux/hwmon.h>
 #include <linux/idr.h>
 #include <linux/kdev_t.h>
 #include <linux/kobject.h>
+#include <linux/kref.h>
 #include <linux/limits.h>
+#include <linux/miscdevice.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/platform_profile.h>
 #include <linux/power_supply.h>
+#include <linux/slab.h>
 #include <linux/types.h>
+#include <linux/uaccess.h>
+#include <linux/unaligned.h>
 #include <linux/wmi.h>
 
 #include <acpi/battery.h>
@@ -91,6 +98,9 @@ enum lwmi_feature_id_psu {
 #define LWMI_FEATURE_VALUE_GET 17
 #define LWMI_FEATURE_VALUE_SET 18
 
+#define LWMI_DATA_PACKAGE_GET 99
+#define LWMI_DATA_PACKAGE_INPUT_MAX 100
+
 #define LWMI_FAN_ID_BASE 1
 #define LWMI_FAN_NR 4
 #define LWMI_FAN_ID(x) ((x) + LWMI_FAN_ID_BASE)

drivers/platform/x86/lenovo/wmi-other.c enum lwmi_feature_id_psu { · line 129

Quoted 5 lines
drivers/platform/x86/lenovo/wmi-other.c +2 -0
@@ -115,6 +125,27 @@ enum lwmi_feature_id_psu {
 
 static DEFINE_IDA(lwmi_om_ida);
 
+static DEFINE_IDA(lwmi_tac_ida);
+static DEFINE_MUTEX(lwmi_tac_lock);

Please document what it protects.

Quoted 78 lines
drivers/platform/x86/lenovo/wmi-other.c +71 -0
+
+struct lwmi_tac {
+	struct miscdevice misc;
+	struct kref ref;
+	struct wmi_device *wdev;
+	u8 instance;
+	int id;
+};
+
+struct lwmi_tac_file {
+	struct lwmi_tac *tac;
+	/* Protects the per-open response and its position. */
+	struct mutex lock;
+	void *result;
+	size_t result_len;
+	loff_t position;
+	bool completed;
+};
+
 enum attribute_property {
 	DEFAULT_VAL,
 	MAX_VAL,
@@ -1609,6 +1640,295 @@ static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv)
 	priv->ida_id = -EIDRM;
 }
 
+/**
+ * lwmi_tac_free() - Free an unregistered endpoint after its last close
+ * @ref: Reference belonging to the endpoint.
+ */
+static void lwmi_tac_free(struct kref *ref)
+{
+	struct lwmi_tac *tac = container_of(ref, struct lwmi_tac, ref);
+
+	ida_free(&lwmi_tac_ida, tac->id);
+	kfree(tac->misc.name);
+	kfree(tac);
+}
+
+/**
+ * lwmi_tac_open() - Allocate independent response state for an open file
+ * @inode: Character device inode.
+ * @file: File initially pointing to the registered miscdevice.
+ *
+ * The misc core serializes open against deregistration, so the registration
+ * reference remains valid while the file acquires its own reference.
+ *
+ * Return: 0 on success, or -ENOMEM.
+ */
+static int lwmi_tac_open(struct inode *inode, struct file *file)
+{
+	struct lwmi_tac *tac = container_of(file->private_data, struct lwmi_tac, misc);
+	struct lwmi_tac_file *state;
+
+	state = kzalloc_obj(*state);
+	if (!state)
+		return -ENOMEM;
+	mutex_init(&state->lock);
+	state->tac = tac;
+	kref_get(&tac->ref);
+	file->private_data = state;
+	return stream_open(inode, file);
+}
+
+/**
+ * lwmi_tac_release() - Erase the response and drop the endpoint reference
+ * @inode: Character device inode.
+ * @file: File holding the endpoint state.
+ *
+ * Return: 0.
+ */
+static int lwmi_tac_release(struct inode *inode, struct file *file)
+{
+	struct lwmi_tac_file *state = file->private_data;
+
+	kfree_sensitive(state->result);
+	mutex_destroy(&state->lock);
+	kref_put(&state->tac->ref, lwmi_tac_free);

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1704

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	kfree(state);
+	return 0;
+}
+
+/**
+ * lwmi_tac_invoke() - Execute GetDataByPackage and retain the response
+ * @state: Open file whose response buffer is empty.
+ * @data: Zero-padded 100-byte input buffer.
+ *
+ * The caller holds state->lock and lwmi_tac_lock. The latter serializes call

Please lockdep assert any locks that are expected to be held.

Quoted 15 lines
drivers/platform/x86/lenovo/wmi-other.c +15 -0
+ * pairs across instances and keeps the WMI device valid during evaluation.
+ *
+ * Return: 0 on success, or a negative error code.
+ */
+static int lwmi_tac_invoke(struct lwmi_tac_file *state, u8 *data)
+{
+	struct lwmi_tac *tac = state->tac;
+	struct wmi_buffer input = { .length = LWMI_DATA_PACKAGE_INPUT_MAX, .data = data };
+	struct wmi_buffer output;
+	u32 data_size;
+	int ret;
+
+	if (!tac->wdev)
+		return -ENODEV;
+	/*

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1729

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	 * Duplicated call required to match BIOS workaround for behavior
+	 * seen when WMI accessed via scripting on other OS.
+	 */
+	for (unsigned int call = 0; call < 2; call++) {
+		ret = wmidev_invoke_method(tac->wdev, tac->instance,
+					   LWMI_DATA_PACKAGE_GET, &input, &output, 0);
+		if (ret)
+			return ret;
+		if (!call)
+			kfree_sensitive(output.data);

Why are you using _sensitive() variant extensively?

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1740

Quoted 11 lines
drivers/platform/x86/lenovo/wmi-other.c +11 -0
+	}
+	/* Validate DataSize before preserving the complete firmware response. */
+	if (output.length < sizeof(__le32)) {
+		ret = -EPROTO;
+		goto free_output;
+	}
+	data_size = get_unaligned_le32(output.data);
+	if (data_size > output.length - sizeof(__le32)) {
+		ret = -EPROTO;
+		goto free_output;
+	}

Can't the new WMI interface do size checks for you?

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1748

Quoted 8 lines
drivers/platform/x86/lenovo/wmi-other.c +8 -0
+	/* Transfer ownership without another allocation after firmware ran. */
+	state->result = output.data;
+	state->result_len = sizeof(__le32) + data_size;
+	state->position = 0;
+	state->completed = true;
+	return 0;
+free_output:
+	kfree_sensitive(output.data);

Please use __free() & no_free_ptr() instead.

I suggest assign to a newly declared (mid-function) variable after the loop as is recommended with __free() (see the long comment in cleanup.h for details).

Quoted 23 lines
drivers/platform/x86/lenovo/wmi-other.c +23 -0
+	return ret;
+}
+
+/**
+ * lwmi_tac_write() - Submit one complete TAC request
+ * @file: Open TAC endpoint with independent response state.
+ * @buffer: Userspace input bytes.
+ * @count: Input size, from 1 to 100 bytes.
+ * @ppos: Unused; TAC is a nonseekable request/response stream.
+ *
+ * Short inputs are zero-padded. An unread response must be consumed before
+ * submitting another request. A failed firmware call is never retried here.
+ *
+ * Return: @count on success, or a negative error code.
+ */
+static ssize_t lwmi_tac_write(struct file *file, const char __user *buffer,
+			      size_t count, loff_t *ppos)
+{
+	struct lwmi_tac_file *state = file->private_data;
+	u8 input[LWMI_DATA_PACKAGE_INPUT_MAX] = {};
+	int ret;
+
+	if (!count || count > sizeof(input))

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1781

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+		return -EINVAL;
+	if (copy_from_user(input, buffer, count)) {
+		ret = -EFAULT;
+		goto clear_input;
+	}
+	ret = mutex_lock_interruptible(&state->lock);
+	if (ret)
+		goto clear_input;
+	ret = mutex_lock_interruptible(&lwmi_tac_lock);
+	if (ret)

Please use ACQUIRE/ACQUIRE_ERR() for these, then you can return directly below as unlocks will be done automatically for you. It will simplify the code flow a ton here.

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+		goto unlock;
+	if (!state->tac->wdev) {
+		ret = -ENODEV;
+	} else if (state->result) {

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1795

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+		ret = -EBUSY;
+	} else {
+		state->completed = false;
+		ret = lwmi_tac_invoke(state, input);
+	}
+	mutex_unlock(&lwmi_tac_lock);
+unlock:
+	mutex_unlock(&state->lock);
+clear_input:
+	memzero_explicit(input, sizeof(input));

?

You really need to have a justification for these in the changelog if you want to go there.

Quoted 46 lines
drivers/platform/x86/lenovo/wmi-other.c +46 -0
+	if (ret)
+		return ret;
+	return count;
+}
+
+/**
+ * lwmi_tac_read() - Read only this open file's firmware response
+ * @file: Open TAC endpoint.
+ * @buffer: Userspace destination.
+ * @count: Maximum number of bytes to return.
+ * @ppos: Unused; the response position is protected by state->lock.
+ *
+ * Responses contain little-endian u32 DataSize followed by Data. Partial
+ * reads advance the response position. Consuming the result erases its
+ * storage immediately; subsequent reads return EOF until another write.
+ *
+ * Return: Bytes read, 0 at EOF or for zero @count, or a negative error code.
+ */
+static ssize_t lwmi_tac_read(struct file *file, char __user *buffer,
+			     size_t count, loff_t *ppos)
+{
+	struct lwmi_tac_file *state = file->private_data;
+	ssize_t ret;
+
+	if (!count)
+		return 0;
+	ret = mutex_lock_interruptible(&state->lock);
+	if (ret)
+		return ret;
+	ret = mutex_lock_interruptible(&lwmi_tac_lock);
+	if (ret)
+		goto unlock;
+	if (!state->tac->wdev)
+		ret = -ENODEV;
+	mutex_unlock(&lwmi_tac_lock);
+	if (ret) {
+		kfree_sensitive(state->result);
+		state->result = NULL;
+		goto unlock;
+	}
+	if (!state->completed) {
+		ret = -ENODATA;
+		goto unlock;
+	}
+	if (!state->result)
+		goto unlock;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1851

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	ret = simple_read_from_buffer(buffer, count, &state->position,
+				      state->result, state->result_len);
+	if (state->position == (loff_t)state->result_len) {
+		kfree_sensitive(state->result);
+		state->result = NULL;
+	}
+unlock:
+	mutex_unlock(&state->lock);
+	return ret;
+}

Same comments as above.

Quoted 61 lines
drivers/platform/x86/lenovo/wmi-other.c +61 -0
+
+static const struct file_operations lwmi_tac_fops = {
+	.owner = THIS_MODULE,
+	.open = lwmi_tac_open,
+	.release = lwmi_tac_release,
+	.read = lwmi_tac_read,
+	.write = lwmi_tac_write,
+};
+
+/**
+ * lwmi_tac_remove() - Unregister an endpoint and block further firmware calls
+ * @data: Endpoint registered as a device-managed cleanup action.
+ *
+ * Stop new opens before waiting for an in-flight call pair. Existing files
+ * retain the endpoint state, but their subsequent reads/writes return -ENODEV.
+ */
+static void lwmi_tac_remove(void *data)
+{
+	struct lwmi_tac *tac = data;
+
+	misc_deregister(&tac->misc);
+	mutex_lock(&lwmi_tac_lock);
+	tac->wdev = NULL;
+	mutex_unlock(&lwmi_tac_lock);
+	kref_put(&tac->ref, lwmi_tac_free);
+}
+
+/**
+ * lwmi_tac_add() - Register TAC endpoints independently of capability data
+ * @wdev: Bound Lenovo Other Mode WMI device.
+ *
+ * No capability describes method 99, so registration does not probe firmware
+ * support. Each firmware instance gets an endpoint; unsupported method calls
+ * are reported to userspace when requested.
+ *
+ * Return: 0 on success, or a negative error code. Device-managed cleanup
+ * unregisters earlier endpoints if a later registration or probe step fails.
+ */
+static int lwmi_tac_add(struct wmi_device *wdev)
+{
+	unsigned int instances = wmidev_instance_count(wdev);
+	int ret;
+
+	for (unsigned int instance = 0; instance < instances; instance++) {
+		struct lwmi_tac *tac;
+
+		tac = kzalloc_obj(*tac);
+		if (!tac)
+			return -ENOMEM;
+		ret = ida_alloc(&lwmi_tac_ida, GFP_KERNEL);
+		if (ret < 0) {
+			kfree(tac);
+			return ret;
+		}
+		tac->id = ret;
+		kref_init(&tac->ref);
+		tac->wdev = wdev;
+		tac->instance = instance;
+		tac->misc.minor = MISC_DYNAMIC_MINOR;
+		tac->misc.name = kasprintf(GFP_KERNEL, "tac%d", tac->id);
+		tac->misc.fops = &lwmi_tac_fops;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1922

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+		tac->misc.parent = &wdev->dev;
+		tac->misc.mode = 0600;
+		if (!tac->misc.name) {
+			kref_put(&tac->ref, lwmi_tac_free);
+			return -ENOMEM;
+		}
+		ret = misc_register(&tac->misc);
+		if (ret) {
+			kref_put(&tac->ref, lwmi_tac_free);
+			return ret;

Please add a rollback path to not duplicate.

Quoted 34 lines
drivers/platform/x86/lenovo/wmi-other.c +14 -0
+		}
+		/* Unregister before the WMI device and its managed state disappear. */
+		ret = devm_add_action_or_reset(&wdev->dev, lwmi_tac_remove, tac);
+		if (ret)
+			return ret;
+	}
+	return 0;
+}
+
 /* ======== Self (master: lenovo-wmi-other) ======== */
 
 /**
@@ -1693,6 +2013,7 @@ static int lwmi_other_probe(struct wmi_device *wdev, const void *context)
 {
 	struct component_match *master_match = NULL;
 	struct lwmi_om_priv *priv;
+	int ret;
 
 	priv = devm_kzalloc_obj(&wdev->dev, *priv);
 	if (!priv)
@@ -1708,6 +2029,10 @@ static int lwmi_other_probe(struct wmi_device *wdev, const void *context)
 	if (IS_ERR(master_match))
 		return PTR_ERR(master_match);
 
+	ret = lwmi_tac_add(wdev);
+	if (ret)
+		return ret;
+
 	return component_master_add_with_match(&wdev->dev, &lwmi_om_master_ops,
 					       master_match);
 }

base-commit: 5680519168f65666d2c03cc6ed8fe2b7052aee12

Signature · 1 line
 i.
author
#2 lore raw

Hi ilpo,

Quoted 531 lines · Ilpo Järvinen
Quoted 150 lines · Alan Pluto

Expose Lenovo Other Mode WMI GetDataByPackage, method 99, through a root-only character device so userspace can access the raw firmware interface. And document the interface.

The upstream driver was build-tested with W=1 and checked with Sparse. A backport using the five-argument wmidev_invoke_method() interface was tested on Ubuntu 7.0.0-42 with Lenovo hardware. The upstream 7.4 driver has not been tested on hardware.

---
 Documentation/ABI/testing/dev-tac             |  49 +++
 .../wmi/devices/lenovo-wmi-other.rst          |   8 +
 MAINTAINERS                                   |   1 +
 drivers/platform/x86/lenovo/wmi-other.c       | 325 ++++++++++++++++++
 4 files changed, 383 insertions(+)
 create mode 100644 Documentation/ABI/testing/dev-tac
Documentation/ABI/testing/dev-tac new file +49 -0
new file mode 100644
index 00000000000..a8be8e95e2d
@@ -0,0 +1,49 @@
+What:           /dev/tac[0-9]*
+Date:           November 2026
+Contact:        [email protected]
+Description:
+                Lenovo Other Mode WMI GetDataByPackage (method 99).
+                Each firmware WMI instance gets a misc character device with
+                mode 0600. Numbering follows allocation order, not necessarily
+                the firmware instance number. Registration does not probe
+                support for method 99. The parent is the bound WMI device.
+
+                Open with O_RDWR. One write submits one complete request of
+                1..100 raw bytes; shorter requests are padded with zeroes to
+                100 bytes. Empty and oversized writes fail with EINVAL without
+                calling firmware. Successful writes return the full submitted
+                length and execute two identical WMI calls for the BIOS
+                duplicate-call workaround. Request pairs through this
+                interface are serialized; calls made by other driver
+                interfaces are not covered by this serialization.
+                Evaluation failures abort the pair; the driver does not
+                automatically retry.
+
+                Each open file description owns its response. A write while
+                that response remains unread fails with EBUSY, including after
+                partial reads. A new open cannot read another open's response.
+                Descriptors shared by dup/fork share the same transaction;
+                applications must coordinate the complete write/read sequence.
+
+                Read returns little-endian u32 DataSize followed by exactly
+                DataSize data bytes. Response storage is erased when fully
+                read or closed. Further reads return EOF until a new successful
+                write. Before a successful write, or after a failed firmware
+                evaluation, reads return ENODATA. A zero-length read returns
+                zero without consuming data.
+
+                Requests are synchronous even with O_NONBLOCK. Seeking and
+                positional I/O are not supported. There is no ioctl interface.
+                A removed WMI device causes nonempty reads and valid writes
+                on existing descriptors to fail with ENODEV.
+
+                Firmware evaluation failures map to EIO. WMI marshalling
+                failures return the error reported by the WMI subsystem.
+                A truncated response or invalid DataSize maps to EPROTO.
+                Bad userspace memory maps to EFAULT. Failure does not imply
+                firmware had no side effects; never blindly resubmit commands.
+                Interrupted waits can return EINTR before firmware evaluation.
+
+                The WMI subsystem converts returned ACPI objects to a buffer.
+                This raw firmware interface may change device state. Use only
+                commands whose behavior and side effects are understood.
Documentation/wmi/devices/lenovo-wmi-other.rst +8 -0
index 011054d64ea..ec85fdeeb7a 100644
@@ -33,6 +33,10 @@ under the following path:
 
 Additionally, this driver also exports attributes to HWMON.
 
+The driver also exposes GetDataByPackage (method 99) through a raw character
+device for each WMI instance. See Documentation/ABI/testing/dev-tac for the
+request and response format and its access restrictions.
+
 LENOVO_CAPABILITY_DATA_00
 -------------------------
 
@@ -109,6 +113,10 @@ WMI interface description
 The WMI interface description can be decoded from the embedded binary MOF (bmof)
 data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility:
 
+The example below describes an older firmware revision and specifies a
+40-byte maximum input for GetDataByPackage. Newer firmware uses a 100-byte
+input, which is the size used by the raw interface described above.
+
 ::
 
   [WMI, Dynamic, Provider("WmiProv"), Locale("MS\\0x409"), Description("LENOVO_OTHER_METHOD class"), guid("{dc2a8805-3a8c-41ba-a6f7-092e0089cd3b}")]
MAINTAINERS +1 -0
index 981eb3fdbef..84b1545f525 100644
@@ -15168,6 +15168,7 @@ M:	Derek J. Clark <[email protected]>
 M:	Rong Zhang <[email protected]>
 L:	[email protected]
 S:	Maintained
+F:	Documentation/ABI/testing/dev-tac
 F:	Documentation/wmi/devices/lenovo-wmi-gamezone.rst
 F:	Documentation/wmi/devices/lenovo-wmi-other.rst
 F:	drivers/platform/x86/lenovo/*
drivers/platform/x86/lenovo/wmi-other.c +10 -0
index bd64e7b4466..c15b3a2a758 100644
@@ -34,16 +34,23 @@
 #include <linux/device.h>
 #include <linux/export.h>
 #include <linux/firmware_attributes.h>
+#include <linux/fs.h>
 #include <linux/gfp_types.h>
 #include <linux/hwmon.h>
 #include <linux/idr.h>
 #include <linux/kdev_t.h>
 #include <linux/kobject.h>
+#include <linux/kref.h>
 #include <linux/limits.h>
+#include <linux/miscdevice.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/platform_profile.h>
 #include <linux/power_supply.h>
+#include <linux/slab.h>
 #include <linux/types.h>
+#include <linux/uaccess.h>
+#include <linux/unaligned.h>
 #include <linux/wmi.h>
 
 #include <acpi/battery.h>
@@ -91,6 +98,9 @@ enum lwmi_feature_id_psu {
 #define LWMI_FEATURE_VALUE_GET 17
 #define LWMI_FEATURE_VALUE_SET 18
 
+#define LWMI_DATA_PACKAGE_GET 99
+#define LWMI_DATA_PACKAGE_INPUT_MAX 100
+
 #define LWMI_FAN_ID_BASE 1
 #define LWMI_FAN_NR 4
 #define LWMI_FAN_ID(x) ((x) + LWMI_FAN_ID_BASE)

drivers/platform/x86/lenovo/wmi-other.c enum lwmi_feature_id_psu { · line 129

Quoted 5 lines
drivers/platform/x86/lenovo/wmi-other.c +2 -0
@@ -115,6 +125,27 @@ enum lwmi_feature_id_psu {
 
 static DEFINE_IDA(lwmi_om_ida);
 
+static DEFINE_IDA(lwmi_tac_ida);
+static DEFINE_MUTEX(lwmi_tac_lock);

Please document what it protects.

Quoted 78 lines
drivers/platform/x86/lenovo/wmi-other.c +71 -0
+
+struct lwmi_tac {
+	struct miscdevice misc;
+	struct kref ref;
+	struct wmi_device *wdev;
+	u8 instance;
+	int id;
+};
+
+struct lwmi_tac_file {
+	struct lwmi_tac *tac;
+	/* Protects the per-open response and its position. */
+	struct mutex lock;
+	void *result;
+	size_t result_len;
+	loff_t position;
+	bool completed;
+};
+
 enum attribute_property {
 	DEFAULT_VAL,
 	MAX_VAL,
@@ -1609,6 +1640,295 @@ static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv)
 	priv->ida_id = -EIDRM;
 }
 
+/**
+ * lwmi_tac_free() - Free an unregistered endpoint after its last close
+ * @ref: Reference belonging to the endpoint.
+ */
+static void lwmi_tac_free(struct kref *ref)
+{
+	struct lwmi_tac *tac = container_of(ref, struct lwmi_tac, ref);
+
+	ida_free(&lwmi_tac_ida, tac->id);
+	kfree(tac->misc.name);
+	kfree(tac);
+}
+
+/**
+ * lwmi_tac_open() - Allocate independent response state for an open file
+ * @inode: Character device inode.
+ * @file: File initially pointing to the registered miscdevice.
+ *
+ * The misc core serializes open against deregistration, so the registration
+ * reference remains valid while the file acquires its own reference.
+ *
+ * Return: 0 on success, or -ENOMEM.
+ */
+static int lwmi_tac_open(struct inode *inode, struct file *file)
+{
+	struct lwmi_tac *tac = container_of(file->private_data, struct lwmi_tac, misc);
+	struct lwmi_tac_file *state;
+
+	state = kzalloc_obj(*state);
+	if (!state)
+		return -ENOMEM;
+	mutex_init(&state->lock);
+	state->tac = tac;
+	kref_get(&tac->ref);
+	file->private_data = state;
+	return stream_open(inode, file);
+}
+
+/**
+ * lwmi_tac_release() - Erase the response and drop the endpoint reference
+ * @inode: Character device inode.
+ * @file: File holding the endpoint state.
+ *
+ * Return: 0.
+ */
+static int lwmi_tac_release(struct inode *inode, struct file *file)
+{
+	struct lwmi_tac_file *state = file->private_data;
+
+	kfree_sensitive(state->result);
+	mutex_destroy(&state->lock);
+	kref_put(&state->tac->ref, lwmi_tac_free);

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1704

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	kfree(state);
+	return 0;
+}
+
+/**
+ * lwmi_tac_invoke() - Execute GetDataByPackage and retain the response
+ * @state: Open file whose response buffer is empty.
+ * @data: Zero-padded 100-byte input buffer.
+ *
+ * The caller holds state->lock and lwmi_tac_lock. The latter serializes call

Please lockdep assert any locks that are expected to be held.

Quoted 15 lines
drivers/platform/x86/lenovo/wmi-other.c +15 -0
+ * pairs across instances and keeps the WMI device valid during evaluation.
+ *
+ * Return: 0 on success, or a negative error code.
+ */
+static int lwmi_tac_invoke(struct lwmi_tac_file *state, u8 *data)
+{
+	struct lwmi_tac *tac = state->tac;
+	struct wmi_buffer input = { .length = LWMI_DATA_PACKAGE_INPUT_MAX, .data = data };
+	struct wmi_buffer output;
+	u32 data_size;
+	int ret;
+
+	if (!tac->wdev)
+		return -ENODEV;
+	/*

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1729

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	 * Duplicated call required to match BIOS workaround for behavior
+	 * seen when WMI accessed via scripting on other OS.
+	 */
+	for (unsigned int call = 0; call < 2; call++) {
+		ret = wmidev_invoke_method(tac->wdev, tac->instance,
+					   LWMI_DATA_PACKAGE_GET, &input, &output, 0);
+		if (ret)
+			return ret;
+		if (!call)
+			kfree_sensitive(output.data);

Why are you using _sensitive() variant extensively?

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1740

Quoted 11 lines
drivers/platform/x86/lenovo/wmi-other.c +11 -0
+	}
+	/* Validate DataSize before preserving the complete firmware response. */
+	if (output.length < sizeof(__le32)) {
+		ret = -EPROTO;
+		goto free_output;
+	}
+	data_size = get_unaligned_le32(output.data);
+	if (data_size > output.length - sizeof(__le32)) {
+		ret = -EPROTO;
+		goto free_output;
+	}

Can't the new WMI interface do size checks for you?

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1748

Quoted 8 lines
drivers/platform/x86/lenovo/wmi-other.c +8 -0
+	/* Transfer ownership without another allocation after firmware ran. */
+	state->result = output.data;
+	state->result_len = sizeof(__le32) + data_size;
+	state->position = 0;
+	state->completed = true;
+	return 0;
+free_output:
+	kfree_sensitive(output.data);

Please use __free() & no_free_ptr() instead.

I suggest assign to a newly declared (mid-function) variable after the loop as is recommended with __free() (see the long comment in cleanup.h for details).

Quoted 23 lines
drivers/platform/x86/lenovo/wmi-other.c +23 -0
+	return ret;
+}
+
+/**
+ * lwmi_tac_write() - Submit one complete TAC request
+ * @file: Open TAC endpoint with independent response state.
+ * @buffer: Userspace input bytes.
+ * @count: Input size, from 1 to 100 bytes.
+ * @ppos: Unused; TAC is a nonseekable request/response stream.
+ *
+ * Short inputs are zero-padded. An unread response must be consumed before
+ * submitting another request. A failed firmware call is never retried here.
+ *
+ * Return: @count on success, or a negative error code.
+ */
+static ssize_t lwmi_tac_write(struct file *file, const char __user *buffer,
+			      size_t count, loff_t *ppos)
+{
+	struct lwmi_tac_file *state = file->private_data;
+	u8 input[LWMI_DATA_PACKAGE_INPUT_MAX] = {};
+	int ret;
+
+	if (!count || count > sizeof(input))

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1781

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+		return -EINVAL;
+	if (copy_from_user(input, buffer, count)) {
+		ret = -EFAULT;
+		goto clear_input;
+	}
+	ret = mutex_lock_interruptible(&state->lock);
+	if (ret)
+		goto clear_input;
+	ret = mutex_lock_interruptible(&lwmi_tac_lock);
+	if (ret)

Please use ACQUIRE/ACQUIRE_ERR() for these, then you can return directly below as unlocks will be done automatically for you. It will simplify the code flow a ton here.

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+		goto unlock;
+	if (!state->tac->wdev) {
+		ret = -ENODEV;
+	} else if (state->result) {

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1795

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+		ret = -EBUSY;
+	} else {
+		state->completed = false;
+		ret = lwmi_tac_invoke(state, input);
+	}
+	mutex_unlock(&lwmi_tac_lock);
+unlock:
+	mutex_unlock(&state->lock);
+clear_input:
+	memzero_explicit(input, sizeof(input));

?

You really need to have a justification for these in the changelog if you want to go there.

Quoted 46 lines
drivers/platform/x86/lenovo/wmi-other.c +46 -0
+	if (ret)
+		return ret;
+	return count;
+}
+
+/**
+ * lwmi_tac_read() - Read only this open file's firmware response
+ * @file: Open TAC endpoint.
+ * @buffer: Userspace destination.
+ * @count: Maximum number of bytes to return.
+ * @ppos: Unused; the response position is protected by state->lock.
+ *
+ * Responses contain little-endian u32 DataSize followed by Data. Partial
+ * reads advance the response position. Consuming the result erases its
+ * storage immediately; subsequent reads return EOF until another write.
+ *
+ * Return: Bytes read, 0 at EOF or for zero @count, or a negative error code.
+ */
+static ssize_t lwmi_tac_read(struct file *file, char __user *buffer,
+			     size_t count, loff_t *ppos)
+{
+	struct lwmi_tac_file *state = file->private_data;
+	ssize_t ret;
+
+	if (!count)
+		return 0;
+	ret = mutex_lock_interruptible(&state->lock);
+	if (ret)
+		return ret;
+	ret = mutex_lock_interruptible(&lwmi_tac_lock);
+	if (ret)
+		goto unlock;
+	if (!state->tac->wdev)
+		ret = -ENODEV;
+	mutex_unlock(&lwmi_tac_lock);
+	if (ret) {
+		kfree_sensitive(state->result);
+		state->result = NULL;
+		goto unlock;
+	}
+	if (!state->completed) {
+		ret = -ENODATA;
+		goto unlock;
+	}
+	if (!state->result)
+		goto unlock;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1851

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	ret = simple_read_from_buffer(buffer, count, &state->position,
+				      state->result, state->result_len);
+	if (state->position == (loff_t)state->result_len) {
+		kfree_sensitive(state->result);
+		state->result = NULL;
+	}
+unlock:
+	mutex_unlock(&state->lock);
+	return ret;
+}

Same comments as above.

Quoted 61 lines
drivers/platform/x86/lenovo/wmi-other.c +61 -0
+
+static const struct file_operations lwmi_tac_fops = {
+	.owner = THIS_MODULE,
+	.open = lwmi_tac_open,
+	.release = lwmi_tac_release,
+	.read = lwmi_tac_read,
+	.write = lwmi_tac_write,
+};
+
+/**
+ * lwmi_tac_remove() - Unregister an endpoint and block further firmware calls
+ * @data: Endpoint registered as a device-managed cleanup action.
+ *
+ * Stop new opens before waiting for an in-flight call pair. Existing files
+ * retain the endpoint state, but their subsequent reads/writes return -ENODEV.
+ */
+static void lwmi_tac_remove(void *data)
+{
+	struct lwmi_tac *tac = data;
+
+	misc_deregister(&tac->misc);
+	mutex_lock(&lwmi_tac_lock);
+	tac->wdev = NULL;
+	mutex_unlock(&lwmi_tac_lock);
+	kref_put(&tac->ref, lwmi_tac_free);
+}
+
+/**
+ * lwmi_tac_add() - Register TAC endpoints independently of capability data
+ * @wdev: Bound Lenovo Other Mode WMI device.
+ *
+ * No capability describes method 99, so registration does not probe firmware
+ * support. Each firmware instance gets an endpoint; unsupported method calls
+ * are reported to userspace when requested.
+ *
+ * Return: 0 on success, or a negative error code. Device-managed cleanup
+ * unregisters earlier endpoints if a later registration or probe step fails.
+ */
+static int lwmi_tac_add(struct wmi_device *wdev)
+{
+	unsigned int instances = wmidev_instance_count(wdev);
+	int ret;
+
+	for (unsigned int instance = 0; instance < instances; instance++) {
+		struct lwmi_tac *tac;
+
+		tac = kzalloc_obj(*tac);
+		if (!tac)
+			return -ENOMEM;
+		ret = ida_alloc(&lwmi_tac_ida, GFP_KERNEL);
+		if (ret < 0) {
+			kfree(tac);
+			return ret;
+		}
+		tac->id = ret;
+		kref_init(&tac->ref);
+		tac->wdev = wdev;
+		tac->instance = instance;
+		tac->misc.minor = MISC_DYNAMIC_MINOR;
+		tac->misc.name = kasprintf(GFP_KERNEL, "tac%d", tac->id);
+		tac->misc.fops = &lwmi_tac_fops;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1922

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+		tac->misc.parent = &wdev->dev;
+		tac->misc.mode = 0600;
+		if (!tac->misc.name) {
+			kref_put(&tac->ref, lwmi_tac_free);
+			return -ENOMEM;
+		}
+		ret = misc_register(&tac->misc);
+		if (ret) {
+			kref_put(&tac->ref, lwmi_tac_free);
+			return ret;

Please add a rollback path to not duplicate.

Quoted 32 lines
drivers/platform/x86/lenovo/wmi-other.c +14 -0
+		}
+		/* Unregister before the WMI device and its managed state disappear. */
+		ret = devm_add_action_or_reset(&wdev->dev, lwmi_tac_remove, tac);
+		if (ret)
+			return ret;
+	}
+	return 0;
+}
+
 /* ======== Self (master: lenovo-wmi-other) ======== */
 
 /**
@@ -1693,6 +2013,7 @@ static int lwmi_other_probe(struct wmi_device *wdev, const void *context)
 {
 	struct component_match *master_match = NULL;
 	struct lwmi_om_priv *priv;
+	int ret;
 
 	priv = devm_kzalloc_obj(&wdev->dev, *priv);
 	if (!priv)
@@ -1708,6 +2029,10 @@ static int lwmi_other_probe(struct wmi_device *wdev, const void *context)
 	if (IS_ERR(master_match))
 		return PTR_ERR(master_match);
 
+	ret = lwmi_tac_add(wdev);
+	if (ret)
+		return ret;
+
 	return component_master_add_with_match(&wdev->dev, &lwmi_om_master_ops,
 					       master_match);
 }
Quoted 3 lines
Quoted 2 lines

base-commit: 5680519168f65666d2c03cc6ed8fe2b7052aee12

Thanks for the review. I sent v2 patch separately. My replies to your points are below. Please help review the v2 patch.

1. Documenting what the lock protects

The v1 global lock is gone. v2 permits one open per WMI device; the per-open mutex protects the pending response and serializes operations through shared descriptors. I added a comment describing what it protects.

2. Lockdep assertions

lwmi_package_invoke() now uses lockdep_assert_held() for the per-open mutex, which must be held by its caller.

3. Sensitive cleanup

The firmware response can contain sensitive data, so I retained kfree_sensitive() only for driver-owned buffers returned by successful WMI calls.

4. WMI minimum size and response validation

The second wmidev_invoke_method() call supplies the minimum header size. The driver still validates the payload length declared by DataSize against the returned length, which the WMI helper cannot infer.

5. __free() and ownership transfer

I declare the response pointer after the second call succeeds, use __free(kfree_sensitive) for error cleanup, and transfer ownership with no_free_ptr() after validation.

6. Scoped locks and simplified reads

The read and write paths now use ACQUIRE()/ACQUIRE_ERR(). I removed partial reads: a response is consumed only after it has been copied in full; an undersized buffer leaves it available.

7. Rollback

devm_lwmi_package_debugfs_add() registers one device-managed cleanup action. The same action removes the directory on setup failure and device removal, avoiding duplicated cleanup code.

#3 lore raw
In reply to Alan Pluto

Hi Alan,

Thanks for working on this.

Quoted 3 lines · Alan Pluto

Expose Lenovo Other Mode WMI GetDataByPackage, method 99, through a root-only character device so userspace can access the raw firmware interface. And document the interface.

What's the practical purpose of the interface? Did you use this to tune anything on your device?

Quoted 5 lines

The upstream driver was build-tested with W=1 and checked with Sparse. A backport using the five-argument wmidev_invoke_method() interface was tested on Ubuntu 7.0.0-42 with Lenovo hardware. The upstream 7.4 driver has not been tested on hardware.

Don't include this in the commit message, but rather send a cover letter or place it under `---'.

Quoted 2 lines

Is the work assisted by LLM?

Quoted 15 lines
---
 Documentation/ABI/testing/dev-tac             |  49 +++
 .../wmi/devices/lenovo-wmi-other.rst          |   8 +
 MAINTAINERS                                   |   1 +
 drivers/platform/x86/lenovo/wmi-other.c       | 325 ++++++++++++++++++
 4 files changed, 383 insertions(+)
 create mode 100644 Documentation/ABI/testing/dev-tac
Documentation/ABI/testing/dev-tac new file +0 -0
new file mode 100644
index 00000000000..a8be8e95e2d

Documentation/ABI/testing/dev-tac line 7

Quoted 7 lines
Documentation/ABI/testing/dev-tac new file +7 -0
@@ -0,0 +1,49 @@
+What:           /dev/tac[0-9]*
+Date:           November 2026
+Contact:        [email protected]
+Description:
+                Lenovo Other Mode WMI GetDataByPackage (method 99).
+                Each firmware WMI instance gets a misc character device with
+                mode 0600. 

Why would you need to add a character device instead of a debugfs file? See also lwmi_debugfs_create_dir().

IMHO the interface is quite internal and undocumented. Exposing it as a character device makes it too reachable for users, unnecessarily increases the complexity of the driver, and requires some sorts of ABI stability.

Quoted 3 lines

Numbering follows allocation order, not necessarily
+ the firmware instance number.

You must be using a wordy LLM if you are using one. Isn't this just obvious and natural for most character devices?

Quoted 2 lines

Registration does not probe
+ support for method 99. The parent is the bound WMI device.

If it can't be probed, gate it unless a module parameter is set.

Documentation/ABI/testing/dev-tac line 13

Quoted 4 lines
Documentation/ABI/testing/dev-tac new file +4 -0
+
+                Open with O_RDWR. One write submits one complete request of
+                1..100 raw bytes; shorter requests are padded with zeroes to
+                100 bytes. Empty and oversized writes fail with EINVAL without

sed -E 's/\bE[A-Z]+\b/-\0/g'

Documentation/ABI/testing/dev-tac line 18

Quoted 8 lines
Documentation/ABI/testing/dev-tac new file +3 -0
+                calling firmware. Successful writes return the full submitted
+                length and execute two identical WMI calls for the BIOS
+                duplicate-call workaround. 

Request pairs through this

Documentation/ABI/testing/dev-tac new file +2 -0
+                interface are serialized; calls made by other driver
+                interfaces are not covered by this serialization.

Why would you need to mention cross-interface implementation details in a user-facing documentation?

Documentation/ABI/testing/dev-tac

Quoted 5 lines
Documentation/ABI/testing/dev-tac new file +4 -0
+                Evaluation failures abort the pair; the driver does not
+                automatically retry.
+
+                Each open file description owns its response. 

Why would you need to support multiple writers? Couldn't you simply return -EBUSY when there is a pending writer/reader?

Documentation/ABI/testing/dev-tac

Quoted 4 lines

A write while

Documentation/ABI/testing/dev-tac new file +2 -0
+                that response remains unread fails with EBUSY, including after
+                partial reads. 

If userspace must write all data at once, why would you need to support partial reads?

Documentation/ABI/testing/dev-tac line 26

Quoted 3 lines

A new open cannot read another open's response.

Documentation/ABI/testing/dev-tac new file +2 -0
+                Descriptors shared by dup/fork share the same transaction;
+                applications must coordinate the complete write/read sequence.

Aren't these just obvious and natural for most character devices, too?

Documentation/ABI/testing/dev-tac

Quoted 11 lines
Documentation/ABI/testing/dev-tac new file +10 -0
+
+                Read returns little-endian u32 DataSize followed by exactly
+                DataSize data bytes. Response storage is erased when fully
+                read or closed. Further reads return EOF until a new successful
+                write. Before a successful write, or after a failed firmware
+                evaluation, reads return ENODATA. A zero-length read returns
+                zero without consuming data.
+
+                Requests are synchronous even with O_NONBLOCK. Seeking and
+                positional I/O are not supported. 

Aren't these just the default characteristic for most non-block character devices?

Quoted 1 line

There is no ioctl interface.

Too verbose. If you don't say it has some, it's obvious that it has none.

Documentation/ABI/testing/dev-tac line 45

Quoted 9 lines
Documentation/ABI/testing/dev-tac new file +9 -0
+                A removed WMI device causes nonempty reads and valid writes
+                on existing descriptors to fail with ENODEV.
+
+                Firmware evaluation failures map to EIO. WMI marshalling
+                failures return the error reported by the WMI subsystem.
+                A truncated response or invalid DataSize maps to EPROTO.
+                Bad userspace memory maps to EFAULT. Failure does not imply
+                firmware had no side effects; never blindly resubmit commands.
+                Interrupted waits can return EINTR before firmware evaluation.

Obvious, no?

Documentation/ABI/testing/dev-tac

Quoted 2 lines
Documentation/ABI/testing/dev-tac new file +2 -0
+
+                The WMI subsystem converts returned ACPI objects to a buffer.

Yet another implementation detail.

Quoted 136 lines
Documentation/ABI/testing/dev-tac new file +2 -0
+                This raw firmware interface may change device state. Use only
+                commands whose behavior and side effects are understood.
Documentation/wmi/devices/lenovo-wmi-other.rst +8 -0
index 011054d64ea..ec85fdeeb7a 100644
@@ -33,6 +33,10 @@ under the following path:
 
 Additionally, this driver also exports attributes to HWMON.
 
+The driver also exposes GetDataByPackage (method 99) through a raw character
+device for each WMI instance. See Documentation/ABI/testing/dev-tac for the
+request and response format and its access restrictions.
+
 LENOVO_CAPABILITY_DATA_00
 -------------------------
 
@@ -109,6 +113,10 @@ WMI interface description
 The WMI interface description can be decoded from the embedded binary MOF (bmof)
 data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility:
 
+The example below describes an older firmware revision and specifies a
+40-byte maximum input for GetDataByPackage. Newer firmware uses a 100-byte
+input, which is the size used by the raw interface described above.
+
 ::
 
   [WMI, Dynamic, Provider("WmiProv"), Locale("MS\\0x409"), Description("LENOVO_OTHER_METHOD class"), guid("{dc2a8805-3a8c-41ba-a6f7-092e0089cd3b}")]
MAINTAINERS +1 -0
index 981eb3fdbef..84b1545f525 100644
@@ -15168,6 +15168,7 @@ M:	Derek J. Clark <[email protected]>
 M:	Rong Zhang <[email protected]>
 L:	[email protected]
 S:	Maintained
+F:	Documentation/ABI/testing/dev-tac
 F:	Documentation/wmi/devices/lenovo-wmi-gamezone.rst
 F:	Documentation/wmi/devices/lenovo-wmi-other.rst
 F:	drivers/platform/x86/lenovo/*
drivers/platform/x86/lenovo/wmi-other.c +57 -0
index bd64e7b4466..c15b3a2a758 100644
@@ -34,16 +34,23 @@
 #include <linux/device.h>
 #include <linux/export.h>
 #include <linux/firmware_attributes.h>
+#include <linux/fs.h>
 #include <linux/gfp_types.h>
 #include <linux/hwmon.h>
 #include <linux/idr.h>
 #include <linux/kdev_t.h>
 #include <linux/kobject.h>
+#include <linux/kref.h>
 #include <linux/limits.h>
+#include <linux/miscdevice.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/platform_profile.h>
 #include <linux/power_supply.h>
+#include <linux/slab.h>
 #include <linux/types.h>
+#include <linux/uaccess.h>
+#include <linux/unaligned.h>
 #include <linux/wmi.h>
 
 #include <acpi/battery.h>
@@ -91,6 +98,9 @@ enum lwmi_feature_id_psu {
 #define LWMI_FEATURE_VALUE_GET 17
 #define LWMI_FEATURE_VALUE_SET 18
 
+#define LWMI_DATA_PACKAGE_GET 99
+#define LWMI_DATA_PACKAGE_INPUT_MAX 100
+
 #define LWMI_FAN_ID_BASE 1
 #define LWMI_FAN_NR 4
 #define LWMI_FAN_ID(x) ((x) + LWMI_FAN_ID_BASE)
@@ -115,6 +125,27 @@ enum lwmi_feature_id_psu {
 
 static DEFINE_IDA(lwmi_om_ida);
 
+static DEFINE_IDA(lwmi_tac_ida);
+static DEFINE_MUTEX(lwmi_tac_lock);
+
+struct lwmi_tac {
+	struct miscdevice misc;
+	struct kref ref;
+	struct wmi_device *wdev;
+	u8 instance;
+	int id;
+};
+
+struct lwmi_tac_file {
+	struct lwmi_tac *tac;
+	/* Protects the per-open response and its position. */
+	struct mutex lock;
+	void *result;
+	size_t result_len;
+	loff_t position;
+	bool completed;
+};
+
 enum attribute_property {
 	DEFAULT_VAL,
 	MAX_VAL,
@@ -1609,6 +1640,295 @@ static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv)
 	priv->ida_id = -EIDRM;
 }
 
+/**
+ * lwmi_tac_free() - Free an unregistered endpoint after its last close
+ * @ref: Reference belonging to the endpoint.
+ */
+static void lwmi_tac_free(struct kref *ref)
+{
+	struct lwmi_tac *tac = container_of(ref, struct lwmi_tac, ref);
+
+	ida_free(&lwmi_tac_ida, tac->id);
+	kfree(tac->misc.name);
+	kfree(tac);
+}
+
+/**
+ * lwmi_tac_open() - Allocate independent response state for an open file
+ * @inode: Character device inode.
+ * @file: File initially pointing to the registered miscdevice.
+ *
+ * The misc core serializes open against deregistration, so the registration
+ * reference remains valid while the file acquires its own reference.
+ *
+ * Return: 0 on success, or -ENOMEM.
+ */
+static int lwmi_tac_open(struct inode *inode, struct file *file)
+{
+	struct lwmi_tac *tac = container_of(file->private_data, struct lwmi_tac, misc);

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1678

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	struct lwmi_tac_file *state;
+
+	state = kzalloc_obj(*state);
+	if (!state)
+		return -ENOMEM;
+	mutex_init(&state->lock);
+	state->tac = tac;
+	kref_get(&tac->ref);
+	file->private_data = state;
+	return stream_open(inode, file);

Add line breaks to improve readability. Same for the following methods.

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+}
+
+/**
+ * lwmi_tac_release() - Erase the response and drop the endpoint reference

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1692

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+ * @inode: Character device inode.
+ * @file: File holding the endpoint state.
+ *
+ * Return: 0.
+ */
+static int lwmi_tac_release(struct inode *inode, struct file *file)
+{
+	struct lwmi_tac_file *state = file->private_data;
+
+	kfree_sensitive(state->result);

Why on earth is it sensitive?

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv)

Quoted 13 lines
drivers/platform/x86/lenovo/wmi-other.c +12 -0
+	mutex_destroy(&state->lock);
+	kref_put(&state->tac->ref, lwmi_tac_free);
+	kfree(state);
+	return 0;
+}
+
+/**
+ * lwmi_tac_invoke() - Execute GetDataByPackage and retain the response
+ * @state: Open file whose response buffer is empty.
+ * @data: Zero-padded 100-byte input buffer.
+ *
+ * The caller holds state->lock and lwmi_tac_lock. 
 

Add lockdep assertions instead of stating them in comments.

Quoted 8 lines

The latter serializes call
+ * pairs across instances and keeps the WMI device valid during evaluation.
+ *
+ * Return: 0 on success, or a negative error code.
+ */

drivers/platform/x86/lenovo/wmi-other.c +3 -0
+static int lwmi_tac_invoke(struct lwmi_tac_file *state, u8 *data)
+{
+	struct lwmi_tac *tac = state->tac;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1721

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	struct wmi_buffer input = { .length = LWMI_DATA_PACKAGE_INPUT_MAX, .data = data };
+	struct wmi_buffer output;
+	u32 data_size;
+	int ret;
+
+	if (!tac->wdev)
+		return -ENODEV;
+	/*
+	 * Duplicated call required to match BIOS workaround for behavior
+	 * seen when WMI accessed via scripting on other OS.

Reference? Where is the script?

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1731

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	 */
+	for (unsigned int call = 0; call < 2; call++) {
+		ret = wmidev_invoke_method(tac->wdev, tac->instance,
+					   LWMI_DATA_PACKAGE_GET, &input, &output, 0);
+		if (ret)
+			return ret;
+		if (!call)
+			kfree_sensitive(output.data);
+	}
+	/* Validate DataSize before preserving the complete firmware response. */

Don't retell what the code does, but rather explain why.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1732

Quoted 1 line
drivers/platform/x86/lenovo/wmi-other.c +1 -0
+	if (output.length < sizeof(__le32)) {

Why not use wmidev_invoke_method()'s min_size argument?

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1736

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+		ret = -EPROTO;
+		goto free_output;
+	}
+	data_size = get_unaligned_le32(output.data);

wmidev_invoke_method() returns aligned data.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1742

Quoted 6 lines
drivers/platform/x86/lenovo/wmi-other.c +6 -0
+	if (data_size > output.length - sizeof(__le32)) {
+		ret = -EPROTO;
+		goto free_output;
+	}
+	/* Transfer ownership without another allocation after firmware ran. */
+	state->result = output.data;

Use __free() and no_free_ptr().

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1743

Quoted 1 line
drivers/platform/x86/lenovo/wmi-other.c +1 -0
+	state->result_len = sizeof(__le32) + data_size;

Couldn't you declare a struct with a flex array member to represent this?

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1753

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	state->position = 0;
+	state->completed = true;
+	return 0;
+free_output:
+	kfree_sensitive(output.data);
+	return ret;
+}
+
+/**
+ * lwmi_tac_write() - Submit one complete TAC request

"complete"

Obvious.

Quoted 7 lines

+ * @file: Open TAC endpoint with independent response state.
+ * @buffer: Userspace input bytes.
+ * @count: Input size, from 1 to 100 bytes.
+ * @ppos: Unused;

TAC is a nonseekable request/response stream.

Obvious.

Quoted 15 lines

+ *
+ * Short inputs are zero-padded. An unread response must be consumed before
+ * submitting another request. A failed firmware call is never retried here.
+ *
+ * Return: @count on success, or a negative error code.
+ */

drivers/platform/x86/lenovo/wmi-other.c +9 -0
+static ssize_t lwmi_tac_write(struct file *file, const char __user *buffer,
+			      size_t count, loff_t *ppos)
+{
+	struct lwmi_tac_file *state = file->private_data;
+	u8 input[LWMI_DATA_PACKAGE_INPUT_MAX] = {};
+	int ret;
+
+	if (!count || count > sizeof(input))
+		return -EINVAL;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1782

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	if (copy_from_user(input, buffer, count)) {
+		ret = -EFAULT;
+		goto clear_input;
+	}
+	ret = mutex_lock_interruptible(&state->lock);
+	if (ret)
+		goto clear_input;
+	ret = mutex_lock_interruptible(&lwmi_tac_lock);
+	if (ret)
+		goto unlock;

Use scoped_cond_guard() or ACQUIRE().

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1795

Quoted 13 lines
drivers/platform/x86/lenovo/wmi-other.c +13 -0
+	if (!state->tac->wdev) {
+		ret = -ENODEV;
+	} else if (state->result) {
+		ret = -EBUSY;
+	} else {
+		state->completed = false;
+		ret = lwmi_tac_invoke(state, input);
+	}
+	mutex_unlock(&lwmi_tac_lock);
+unlock:
+	mutex_unlock(&state->lock);
+clear_input:
+	memzero_explicit(input, sizeof(input));

Why on earth is it sensitive, too?

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1802

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +7 -0
+	if (ret)
+		return ret;
+	return count;
+}
+
+/**
+ * lwmi_tac_read() - Read only this open file's firmware response

"only"

Obvious, no?

Quoted 38 lines

+ * @file: Open TAC endpoint.
+ * @buffer: Userspace destination.
+ * @count: Maximum number of bytes to return.
+ * @ppos: Unused; the response position is protected by state->lock.
+ *
+ * Responses contain little-endian u32 DataSize followed by Data. Partial
+ * reads advance the response position. Consuming the result erases its
+ * storage immediately; subsequent reads return EOF until another write.
+ *
+ * Return: Bytes read, 0 at EOF or for zero @count, or a negative error code.
+ */

drivers/platform/x86/lenovo/wmi-other.c +27 -0
+static ssize_t lwmi_tac_read(struct file *file, char __user *buffer,
+			     size_t count, loff_t *ppos)
+{
+	struct lwmi_tac_file *state = file->private_data;
+	ssize_t ret;
+
+	if (!count)
+		return 0;
+	ret = mutex_lock_interruptible(&state->lock);
+	if (ret)
+		return ret;
+	ret = mutex_lock_interruptible(&lwmi_tac_lock);
+	if (ret)
+		goto unlock;
+	if (!state->tac->wdev)
+		ret = -ENODEV;
+	mutex_unlock(&lwmi_tac_lock);
+	if (ret) {
+		kfree_sensitive(state->result);
+		state->result = NULL;
+		goto unlock;
+	}
+	if (!state->completed) {
+		ret = -ENODATA;
+		goto unlock;
+	}
+	if (!state->result)

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1850

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+		goto unlock;
+	ret = simple_read_from_buffer(buffer, count, &state->position,
+				      state->result, state->result_len);
+	if (state->position == (loff_t)state->result_len) {
+		kfree_sensitive(state->result);
+		state->result = NULL;
+	}
+unlock:
+	mutex_unlock(&state->lock);
+	return ret;

The entire method is in a horrible mess. Drop the support for partial reads, and use scoped_cond_guard()/ACQUIRE(), and __free() to tidy it up.

Quoted 30 lines
drivers/platform/x86/lenovo/wmi-other.c +30 -0
+}
+
+static const struct file_operations lwmi_tac_fops = {
+	.owner = THIS_MODULE,
+	.open = lwmi_tac_open,
+	.release = lwmi_tac_release,
+	.read = lwmi_tac_read,
+	.write = lwmi_tac_write,
+};
+
+/**
+ * lwmi_tac_remove() - Unregister an endpoint and block further firmware calls
+ * @data: Endpoint registered as a device-managed cleanup action.
+ *
+ * Stop new opens before waiting for an in-flight call pair. Existing files
+ * retain the endpoint state, but their subsequent reads/writes return -ENODEV.
+ */
+static void lwmi_tac_remove(void *data)
+{
+	struct lwmi_tac *tac = data;
+
+	misc_deregister(&tac->misc);
+	mutex_lock(&lwmi_tac_lock);
+	tac->wdev = NULL;
+	mutex_unlock(&lwmi_tac_lock);
+	kref_put(&tac->ref, lwmi_tac_free);
+}
+
+/**
+ * lwmi_tac_add() - Register TAC endpoints independently of capability data

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1890

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+ * @wdev: Bound Lenovo Other Mode WMI device.
+ *
+ * No capability describes method 99, so registration does not probe firmware
+ * support. Each firmware instance gets an endpoint; unsupported method calls
+ * are reported to userspace when requested.
+ *
+ * Return: 0 on success, or a negative error code. Device-managed cleanup
+ * unregisters earlier endpoints if a later registration or probe step fails.
+ */
+static int lwmi_tac_add(struct wmi_device *wdev)

devm_

Quoted 53 lines
drivers/platform/x86/lenovo/wmi-other.c +42 -0
+{
+	unsigned int instances = wmidev_instance_count(wdev);
+	int ret;
+
+	for (unsigned int instance = 0; instance < instances; instance++) {
+		struct lwmi_tac *tac;
+
+		tac = kzalloc_obj(*tac);
+		if (!tac)
+			return -ENOMEM;
+		ret = ida_alloc(&lwmi_tac_ida, GFP_KERNEL);
+		if (ret < 0) {
+			kfree(tac);
+			return ret;
+		}
+		tac->id = ret;
+		kref_init(&tac->ref);
+		tac->wdev = wdev;
+		tac->instance = instance;
+		tac->misc.minor = MISC_DYNAMIC_MINOR;
+		tac->misc.name = kasprintf(GFP_KERNEL, "tac%d", tac->id);
+		tac->misc.fops = &lwmi_tac_fops;
+		tac->misc.parent = &wdev->dev;
+		tac->misc.mode = 0600;
+		if (!tac->misc.name) {
+			kref_put(&tac->ref, lwmi_tac_free);
+			return -ENOMEM;
+		}
+		ret = misc_register(&tac->misc);
+		if (ret) {
+			kref_put(&tac->ref, lwmi_tac_free);
+			return ret;
+		}
+		/* Unregister before the WMI device and its managed state disappear. */
+		ret = devm_add_action_or_reset(&wdev->dev, lwmi_tac_remove, tac);
+		if (ret)
+			return ret;
+	}
+	return 0;
+}
+
 /* ======== Self (master: lenovo-wmi-other) ======== */
 
 /**
@@ -1693,6 +2013,7 @@ static int lwmi_other_probe(struct wmi_device *wdev, const void *context)
 {
 	struct component_match *master_match = NULL;
 	struct lwmi_om_priv *priv;
+	int ret;
 
 	priv = devm_kzalloc_obj(&wdev->dev, *priv);
 	if (!priv)

drivers/platform/x86/lenovo/wmi-other.c static int lwmi_other_probe(struct wmi_device *wdev, const void *context) · line 2034

Quoted 6 lines
drivers/platform/x86/lenovo/wmi-other.c +3 -0
@@ -1708,6 +2029,10 @@ static int lwmi_other_probe(struct wmi_device *wdev, const void *context)
 	if (IS_ERR(master_match))
 		return PTR_ERR(master_match);
 
+	ret = lwmi_tac_add(wdev);
+	if (ret)
+		return ret;

Why is this fatal?

Thanks,
Rong

Quoted 6 lines
drivers/platform/x86/lenovo/wmi-other.c +1 -0
+
 	return component_master_add_with_match(&wdev->dev, &lwmi_om_master_ops,
 					       master_match);
 }

base-commit: 5680519168f65666d2c03cc6ed8fe2b7052aee12

author
#4 lore raw

Hi Rong,

Quoted 619 lines · Rong Zhang

Hi Alan,

Thanks for working on this.

Quoted 3 lines · Alan Pluto

Expose Lenovo Other Mode WMI GetDataByPackage, method 99, through a root-only character device so userspace can access the raw firmware interface. And document the interface.

What's the practical purpose of the interface? Did you use this to tune anything on your device?

Quoted 5 lines

The upstream driver was build-tested with W=1 and checked with Sparse. A backport using the five-argument wmidev_invoke_method() interface was tested on Ubuntu 7.0.0-42 with Lenovo hardware. The upstream 7.4 driver has not been tested on hardware.

Don't include this in the commit message, but rather send a cover letter or place it under `---'.

Quoted 2 lines

Is the work assisted by LLM?

Quoted 15 lines
---
 Documentation/ABI/testing/dev-tac             |  49 +++
 .../wmi/devices/lenovo-wmi-other.rst          |   8 +
 MAINTAINERS                                   |   1 +
 drivers/platform/x86/lenovo/wmi-other.c       | 325 ++++++++++++++++++
 4 files changed, 383 insertions(+)
 create mode 100644 Documentation/ABI/testing/dev-tac
Documentation/ABI/testing/dev-tac new file +0 -0
new file mode 100644
index 00000000000..a8be8e95e2d

Documentation/ABI/testing/dev-tac line 7

Quoted 7 lines
Documentation/ABI/testing/dev-tac new file +7 -0
@@ -0,0 +1,49 @@
+What:           /dev/tac[0-9]*
+Date:           November 2026
+Contact:        [email protected]
+Description:
+                Lenovo Other Mode WMI GetDataByPackage (method 99).
+                Each firmware WMI instance gets a misc character device with
+                mode 0600. 

Why would you need to add a character device instead of a debugfs file? See also lwmi_debugfs_create_dir().

IMHO the interface is quite internal and undocumented. Exposing it as a character device makes it too reachable for users, unnecessarily increases the complexity of the driver, and requires some sorts of ABI stability.

Quoted 3 lines

Numbering follows allocation order, not necessarily
+ the firmware instance number.

You must be using a wordy LLM if you are using one. Isn't this just obvious and natural for most character devices?

Quoted 2 lines

Registration does not probe
+ support for method 99. The parent is the bound WMI device.

If it can't be probed, gate it unless a module parameter is set.

Documentation/ABI/testing/dev-tac line 13

Quoted 4 lines
Documentation/ABI/testing/dev-tac new file +4 -0
+
+                Open with O_RDWR. One write submits one complete request of
+                1..100 raw bytes; shorter requests are padded with zeroes to
+                100 bytes. Empty and oversized writes fail with EINVAL without

sed -E 's/\bE[A-Z]+\b/-\0/g'

Documentation/ABI/testing/dev-tac line 18

Quoted 8 lines
Documentation/ABI/testing/dev-tac new file +3 -0
+                calling firmware. Successful writes return the full submitted
+                length and execute two identical WMI calls for the BIOS
+                duplicate-call workaround. 

Request pairs through this

Documentation/ABI/testing/dev-tac new file +2 -0
+                interface are serialized; calls made by other driver
+                interfaces are not covered by this serialization.

Why would you need to mention cross-interface implementation details in a user-facing documentation?

Documentation/ABI/testing/dev-tac

Quoted 5 lines
Documentation/ABI/testing/dev-tac new file +4 -0
+                Evaluation failures abort the pair; the driver does not
+                automatically retry.
+
+                Each open file description owns its response. 

Why would you need to support multiple writers? Couldn't you simply return -EBUSY when there is a pending writer/reader?

Documentation/ABI/testing/dev-tac

Quoted 4 lines

A write while

Documentation/ABI/testing/dev-tac new file +2 -0
+                that response remains unread fails with EBUSY, including after
+                partial reads. 

If userspace must write all data at once, why would you need to support partial reads?

Documentation/ABI/testing/dev-tac line 26

Quoted 3 lines

A new open cannot read another open's response.

Documentation/ABI/testing/dev-tac new file +2 -0
+                Descriptors shared by dup/fork share the same transaction;
+                applications must coordinate the complete write/read sequence.

Aren't these just obvious and natural for most character devices, too?

Documentation/ABI/testing/dev-tac

Quoted 11 lines
Documentation/ABI/testing/dev-tac new file +10 -0
+
+                Read returns little-endian u32 DataSize followed by exactly
+                DataSize data bytes. Response storage is erased when fully
+                read or closed. Further reads return EOF until a new successful
+                write. Before a successful write, or after a failed firmware
+                evaluation, reads return ENODATA. A zero-length read returns
+                zero without consuming data.
+
+                Requests are synchronous even with O_NONBLOCK. Seeking and
+                positional I/O are not supported. 

Aren't these just the default characteristic for most non-block character devices?

Quoted 1 line

There is no ioctl interface.

Too verbose. If you don't say it has some, it's obvious that it has none.

Documentation/ABI/testing/dev-tac line 45

Quoted 9 lines
Documentation/ABI/testing/dev-tac new file +9 -0
+                A removed WMI device causes nonempty reads and valid writes
+                on existing descriptors to fail with ENODEV.
+
+                Firmware evaluation failures map to EIO. WMI marshalling
+                failures return the error reported by the WMI subsystem.
+                A truncated response or invalid DataSize maps to EPROTO.
+                Bad userspace memory maps to EFAULT. Failure does not imply
+                firmware had no side effects; never blindly resubmit commands.
+                Interrupted waits can return EINTR before firmware evaluation.

Obvious, no?

Documentation/ABI/testing/dev-tac

Quoted 2 lines
Documentation/ABI/testing/dev-tac new file +2 -0
+
+                The WMI subsystem converts returned ACPI objects to a buffer.

Yet another implementation detail.

Quoted 136 lines
Documentation/ABI/testing/dev-tac new file +2 -0
+                This raw firmware interface may change device state. Use only
+                commands whose behavior and side effects are understood.
Documentation/wmi/devices/lenovo-wmi-other.rst +8 -0
index 011054d64ea..ec85fdeeb7a 100644
@@ -33,6 +33,10 @@ under the following path:
 
 Additionally, this driver also exports attributes to HWMON.
 
+The driver also exposes GetDataByPackage (method 99) through a raw character
+device for each WMI instance. See Documentation/ABI/testing/dev-tac for the
+request and response format and its access restrictions.
+
 LENOVO_CAPABILITY_DATA_00
 -------------------------
 
@@ -109,6 +113,10 @@ WMI interface description
 The WMI interface description can be decoded from the embedded binary MOF (bmof)
 data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility:
 
+The example below describes an older firmware revision and specifies a
+40-byte maximum input for GetDataByPackage. Newer firmware uses a 100-byte
+input, which is the size used by the raw interface described above.
+
 ::
 
   [WMI, Dynamic, Provider("WmiProv"), Locale("MS\\0x409"), Description("LENOVO_OTHER_METHOD class"), guid("{dc2a8805-3a8c-41ba-a6f7-092e0089cd3b}")]
MAINTAINERS +1 -0
index 981eb3fdbef..84b1545f525 100644
@@ -15168,6 +15168,7 @@ M:	Derek J. Clark <[email protected]>
 M:	Rong Zhang <[email protected]>
 L:	[email protected]
 S:	Maintained
+F:	Documentation/ABI/testing/dev-tac
 F:	Documentation/wmi/devices/lenovo-wmi-gamezone.rst
 F:	Documentation/wmi/devices/lenovo-wmi-other.rst
 F:	drivers/platform/x86/lenovo/*
drivers/platform/x86/lenovo/wmi-other.c +57 -0
index bd64e7b4466..c15b3a2a758 100644
@@ -34,16 +34,23 @@
 #include <linux/device.h>
 #include <linux/export.h>
 #include <linux/firmware_attributes.h>
+#include <linux/fs.h>
 #include <linux/gfp_types.h>
 #include <linux/hwmon.h>
 #include <linux/idr.h>
 #include <linux/kdev_t.h>
 #include <linux/kobject.h>
+#include <linux/kref.h>
 #include <linux/limits.h>
+#include <linux/miscdevice.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/platform_profile.h>
 #include <linux/power_supply.h>
+#include <linux/slab.h>
 #include <linux/types.h>
+#include <linux/uaccess.h>
+#include <linux/unaligned.h>
 #include <linux/wmi.h>
 
 #include <acpi/battery.h>
@@ -91,6 +98,9 @@ enum lwmi_feature_id_psu {
 #define LWMI_FEATURE_VALUE_GET 17
 #define LWMI_FEATURE_VALUE_SET 18
 
+#define LWMI_DATA_PACKAGE_GET 99
+#define LWMI_DATA_PACKAGE_INPUT_MAX 100
+
 #define LWMI_FAN_ID_BASE 1
 #define LWMI_FAN_NR 4
 #define LWMI_FAN_ID(x) ((x) + LWMI_FAN_ID_BASE)
@@ -115,6 +125,27 @@ enum lwmi_feature_id_psu {
 
 static DEFINE_IDA(lwmi_om_ida);
 
+static DEFINE_IDA(lwmi_tac_ida);
+static DEFINE_MUTEX(lwmi_tac_lock);
+
+struct lwmi_tac {
+	struct miscdevice misc;
+	struct kref ref;
+	struct wmi_device *wdev;
+	u8 instance;
+	int id;
+};
+
+struct lwmi_tac_file {
+	struct lwmi_tac *tac;
+	/* Protects the per-open response and its position. */
+	struct mutex lock;
+	void *result;
+	size_t result_len;
+	loff_t position;
+	bool completed;
+};
+
 enum attribute_property {
 	DEFAULT_VAL,
 	MAX_VAL,
@@ -1609,6 +1640,295 @@ static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv)
 	priv->ida_id = -EIDRM;
 }
 
+/**
+ * lwmi_tac_free() - Free an unregistered endpoint after its last close
+ * @ref: Reference belonging to the endpoint.
+ */
+static void lwmi_tac_free(struct kref *ref)
+{
+	struct lwmi_tac *tac = container_of(ref, struct lwmi_tac, ref);
+
+	ida_free(&lwmi_tac_ida, tac->id);
+	kfree(tac->misc.name);
+	kfree(tac);
+}
+
+/**
+ * lwmi_tac_open() - Allocate independent response state for an open file
+ * @inode: Character device inode.
+ * @file: File initially pointing to the registered miscdevice.
+ *
+ * The misc core serializes open against deregistration, so the registration
+ * reference remains valid while the file acquires its own reference.
+ *
+ * Return: 0 on success, or -ENOMEM.
+ */
+static int lwmi_tac_open(struct inode *inode, struct file *file)
+{
+	struct lwmi_tac *tac = container_of(file->private_data, struct lwmi_tac, misc);

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1678

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	struct lwmi_tac_file *state;
+
+	state = kzalloc_obj(*state);
+	if (!state)
+		return -ENOMEM;
+	mutex_init(&state->lock);
+	state->tac = tac;
+	kref_get(&tac->ref);
+	file->private_data = state;
+	return stream_open(inode, file);

Add line breaks to improve readability. Same for the following methods.

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+}
+
+/**
+ * lwmi_tac_release() - Erase the response and drop the endpoint reference

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1692

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+ * @inode: Character device inode.
+ * @file: File holding the endpoint state.
+ *
+ * Return: 0.
+ */
+static int lwmi_tac_release(struct inode *inode, struct file *file)
+{
+	struct lwmi_tac_file *state = file->private_data;
+
+	kfree_sensitive(state->result);

Why on earth is it sensitive?

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv)

Quoted 13 lines
drivers/platform/x86/lenovo/wmi-other.c +12 -0
+	mutex_destroy(&state->lock);
+	kref_put(&state->tac->ref, lwmi_tac_free);
+	kfree(state);
+	return 0;
+}
+
+/**
+ * lwmi_tac_invoke() - Execute GetDataByPackage and retain the response
+ * @state: Open file whose response buffer is empty.
+ * @data: Zero-padded 100-byte input buffer.
+ *
+ * The caller holds state->lock and lwmi_tac_lock. 
 

Add lockdep assertions instead of stating them in comments.

Quoted 8 lines

The latter serializes call
+ * pairs across instances and keeps the WMI device valid during evaluation.
+ *
+ * Return: 0 on success, or a negative error code.
+ */

drivers/platform/x86/lenovo/wmi-other.c +3 -0
+static int lwmi_tac_invoke(struct lwmi_tac_file *state, u8 *data)
+{
+	struct lwmi_tac *tac = state->tac;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1721

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	struct wmi_buffer input = { .length = LWMI_DATA_PACKAGE_INPUT_MAX, .data = data };
+	struct wmi_buffer output;
+	u32 data_size;
+	int ret;
+
+	if (!tac->wdev)
+		return -ENODEV;
+	/*
+	 * Duplicated call required to match BIOS workaround for behavior
+	 * seen when WMI accessed via scripting on other OS.

Reference? Where is the script?

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1731

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	 */
+	for (unsigned int call = 0; call < 2; call++) {
+		ret = wmidev_invoke_method(tac->wdev, tac->instance,
+					   LWMI_DATA_PACKAGE_GET, &input, &output, 0);
+		if (ret)
+			return ret;
+		if (!call)
+			kfree_sensitive(output.data);
+	}
+	/* Validate DataSize before preserving the complete firmware response. */

Don't retell what the code does, but rather explain why.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1732

Quoted 1 line
drivers/platform/x86/lenovo/wmi-other.c +1 -0
+	if (output.length < sizeof(__le32)) {

Why not use wmidev_invoke_method()'s min_size argument?

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1736

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+		ret = -EPROTO;
+		goto free_output;
+	}
+	data_size = get_unaligned_le32(output.data);

wmidev_invoke_method() returns aligned data.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1742

Quoted 6 lines
drivers/platform/x86/lenovo/wmi-other.c +6 -0
+	if (data_size > output.length - sizeof(__le32)) {
+		ret = -EPROTO;
+		goto free_output;
+	}
+	/* Transfer ownership without another allocation after firmware ran. */
+	state->result = output.data;

Use __free() and no_free_ptr().

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1743

Quoted 1 line
drivers/platform/x86/lenovo/wmi-other.c +1 -0
+	state->result_len = sizeof(__le32) + data_size;

Couldn't you declare a struct with a flex array member to represent this?

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1753

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	state->position = 0;
+	state->completed = true;
+	return 0;
+free_output:
+	kfree_sensitive(output.data);
+	return ret;
+}
+
+/**
+ * lwmi_tac_write() - Submit one complete TAC request

"complete"

Obvious.

Quoted 7 lines

+ * @file: Open TAC endpoint with independent response state.
+ * @buffer: Userspace input bytes.
+ * @count: Input size, from 1 to 100 bytes.
+ * @ppos: Unused;

TAC is a nonseekable request/response stream.

Obvious.

Quoted 15 lines

+ *
+ * Short inputs are zero-padded. An unread response must be consumed before
+ * submitting another request. A failed firmware call is never retried here.
+ *
+ * Return: @count on success, or a negative error code.
+ */

drivers/platform/x86/lenovo/wmi-other.c +9 -0
+static ssize_t lwmi_tac_write(struct file *file, const char __user *buffer,
+			      size_t count, loff_t *ppos)
+{
+	struct lwmi_tac_file *state = file->private_data;
+	u8 input[LWMI_DATA_PACKAGE_INPUT_MAX] = {};
+	int ret;
+
+	if (!count || count > sizeof(input))
+		return -EINVAL;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1782

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	if (copy_from_user(input, buffer, count)) {
+		ret = -EFAULT;
+		goto clear_input;
+	}
+	ret = mutex_lock_interruptible(&state->lock);
+	if (ret)
+		goto clear_input;
+	ret = mutex_lock_interruptible(&lwmi_tac_lock);
+	if (ret)
+		goto unlock;

Use scoped_cond_guard() or ACQUIRE().

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1795

Quoted 13 lines
drivers/platform/x86/lenovo/wmi-other.c +13 -0
+	if (!state->tac->wdev) {
+		ret = -ENODEV;
+	} else if (state->result) {
+		ret = -EBUSY;
+	} else {
+		state->completed = false;
+		ret = lwmi_tac_invoke(state, input);
+	}
+	mutex_unlock(&lwmi_tac_lock);
+unlock:
+	mutex_unlock(&state->lock);
+clear_input:
+	memzero_explicit(input, sizeof(input));

Why on earth is it sensitive, too?

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1802

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +7 -0
+	if (ret)
+		return ret;
+	return count;
+}
+
+/**
+ * lwmi_tac_read() - Read only this open file's firmware response

"only"

Obvious, no?

Quoted 38 lines

+ * @file: Open TAC endpoint.
+ * @buffer: Userspace destination.
+ * @count: Maximum number of bytes to return.
+ * @ppos: Unused; the response position is protected by state->lock.
+ *
+ * Responses contain little-endian u32 DataSize followed by Data. Partial
+ * reads advance the response position. Consuming the result erases its
+ * storage immediately; subsequent reads return EOF until another write.
+ *
+ * Return: Bytes read, 0 at EOF or for zero @count, or a negative error code.
+ */

drivers/platform/x86/lenovo/wmi-other.c +27 -0
+static ssize_t lwmi_tac_read(struct file *file, char __user *buffer,
+			     size_t count, loff_t *ppos)
+{
+	struct lwmi_tac_file *state = file->private_data;
+	ssize_t ret;
+
+	if (!count)
+		return 0;
+	ret = mutex_lock_interruptible(&state->lock);
+	if (ret)
+		return ret;
+	ret = mutex_lock_interruptible(&lwmi_tac_lock);
+	if (ret)
+		goto unlock;
+	if (!state->tac->wdev)
+		ret = -ENODEV;
+	mutex_unlock(&lwmi_tac_lock);
+	if (ret) {
+		kfree_sensitive(state->result);
+		state->result = NULL;
+		goto unlock;
+	}
+	if (!state->completed) {
+		ret = -ENODATA;
+		goto unlock;
+	}
+	if (!state->result)

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1850

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+		goto unlock;
+	ret = simple_read_from_buffer(buffer, count, &state->position,
+				      state->result, state->result_len);
+	if (state->position == (loff_t)state->result_len) {
+		kfree_sensitive(state->result);
+		state->result = NULL;
+	}
+unlock:
+	mutex_unlock(&state->lock);
+	return ret;

The entire method is in a horrible mess. Drop the support for partial reads, and use scoped_cond_guard()/ACQUIRE(), and __free() to tidy it up.

Quoted 30 lines
drivers/platform/x86/lenovo/wmi-other.c +30 -0
+}
+
+static const struct file_operations lwmi_tac_fops = {
+	.owner = THIS_MODULE,
+	.open = lwmi_tac_open,
+	.release = lwmi_tac_release,
+	.read = lwmi_tac_read,
+	.write = lwmi_tac_write,
+};
+
+/**
+ * lwmi_tac_remove() - Unregister an endpoint and block further firmware calls
+ * @data: Endpoint registered as a device-managed cleanup action.
+ *
+ * Stop new opens before waiting for an in-flight call pair. Existing files
+ * retain the endpoint state, but their subsequent reads/writes return -ENODEV.
+ */
+static void lwmi_tac_remove(void *data)
+{
+	struct lwmi_tac *tac = data;
+
+	misc_deregister(&tac->misc);
+	mutex_lock(&lwmi_tac_lock);
+	tac->wdev = NULL;
+	mutex_unlock(&lwmi_tac_lock);
+	kref_put(&tac->ref, lwmi_tac_free);
+}
+
+/**
+ * lwmi_tac_add() - Register TAC endpoints independently of capability data

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1890

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+ * @wdev: Bound Lenovo Other Mode WMI device.
+ *
+ * No capability describes method 99, so registration does not probe firmware
+ * support. Each firmware instance gets an endpoint; unsupported method calls
+ * are reported to userspace when requested.
+ *
+ * Return: 0 on success, or a negative error code. Device-managed cleanup
+ * unregisters earlier endpoints if a later registration or probe step fails.
+ */
+static int lwmi_tac_add(struct wmi_device *wdev)

devm_

Quoted 56 lines
drivers/platform/x86/lenovo/wmi-other.c +42 -0
+{
+	unsigned int instances = wmidev_instance_count(wdev);
+	int ret;
+
+	for (unsigned int instance = 0; instance < instances; instance++) {
+		struct lwmi_tac *tac;
+
+		tac = kzalloc_obj(*tac);
+		if (!tac)
+			return -ENOMEM;
+		ret = ida_alloc(&lwmi_tac_ida, GFP_KERNEL);
+		if (ret < 0) {
+			kfree(tac);
+			return ret;
+		}
+		tac->id = ret;
+		kref_init(&tac->ref);
+		tac->wdev = wdev;
+		tac->instance = instance;
+		tac->misc.minor = MISC_DYNAMIC_MINOR;
+		tac->misc.name = kasprintf(GFP_KERNEL, "tac%d", tac->id);
+		tac->misc.fops = &lwmi_tac_fops;
+		tac->misc.parent = &wdev->dev;
+		tac->misc.mode = 0600;
+		if (!tac->misc.name) {
+			kref_put(&tac->ref, lwmi_tac_free);
+			return -ENOMEM;
+		}
+		ret = misc_register(&tac->misc);
+		if (ret) {
+			kref_put(&tac->ref, lwmi_tac_free);
+			return ret;
+		}
+		/* Unregister before the WMI device and its managed state disappear. */
+		ret = devm_add_action_or_reset(&wdev->dev, lwmi_tac_remove, tac);
+		if (ret)
+			return ret;
+	}
+	return 0;
+}
+
 /* ======== Self (master: lenovo-wmi-other) ======== */
 
 /**
@@ -1693,6 +2013,7 @@ static int lwmi_other_probe(struct wmi_device *wdev, const void *context)
 {
 	struct component_match *master_match = NULL;
 	struct lwmi_om_priv *priv;
+	int ret;
 
 	priv = devm_kzalloc_obj(&wdev->dev, *priv);
 	if (!priv)
@@ -1708,6 +2029,10 @@ static int lwmi_other_probe(struct wmi_device *wdev, const void *context)
 	if (IS_ERR(master_match))
 		return PTR_ERR(master_match);
 
Quoted 15 lines

drivers/platform/x86/lenovo/wmi-other.c static int lwmi_other_probe(struct wmi_device *wdev, const void *context) · line 2034

Quoted 3 lines
drivers/platform/x86/lenovo/wmi-other.c +3 -0
+	ret = lwmi_tac_add(wdev);
+	if (ret)
+		return ret;

Why is this fatal?

Thanks,
Rong

Quoted 6 lines
drivers/platform/x86/lenovo/wmi-other.c +1 -0
+
 	return component_master_add_with_match(&wdev->dev, &lwmi_om_master_ops,
 					       master_match);
 }

base-commit: 5680519168f65666d2c03cc6ed8fe2b7052aee12

Thanks for the detailed review. I sent v2 patch separately by referring your suggestion. Please help review it.
My replies to your points are below.

1. Practical purpose and tuning

The known use is for Lenovo utility to communicate with firmware. In v2 I use a default-off debugfs interface instead of stable userspace interface.

2. Test information in the commit message

I removed the build and test details from the v2 commit message.

3. LLM assistance

Yes, I used AI assistance for the port and review. I reviewed the changes and am responsible for the submission.

4. Character device versus debugfs, and the module parameter

I replaced the character device with a 0600 debugfs file created under lwmi_debugfs_create_dir(). There is no non-invasive probe for method 99, so creation is disabled by default and requires enable_get_data_by_package=1 when loading the module. I removed the v1 ABI document and its MAINTAINERS entry.

5. ABI documentation and verbosity

Removed the character-device ABI document change.

6. Multiple writers and partial reads

The debugfs file permits one open only per WMI device.

7. Readability and comments

I simplified the handlers and removed the redundant kernel-doc and comments that merely described normal file operations. I also removed the unsupported reference to a script on another OS.

8. Locking

With one open per WMI device, a per-open lock protects the response and serializes operations through shared descriptors. lwmi_package_invoke() uses lockdep_assert_held() for that lock.

9. Sensitive data

The firmware response can contain sensitive data, so I retained kfree_sensitive() for buffers returned successfully by WMI.

10. Duplicate calls

You can refer as think-lmi.c, same duplicated call. The reason is invoking WMI under Windows OS will result the BIOS ACPI code execute twice, so BIOS implemented the workaround, thus Linux driver will need the duplicated call to match the BIOS workaround.

11. Response size, alignment and cleanup

The second wmidev_invoke_method() call passes the response header size as min_size. The driver separately validates DataSize against the returned buffer length. The response is represented by an aligned structure with a flexible array; after the successful call I declare it with __free(kfree_sensitive) and transfer ownership with no_free_ptr().

12. Scoped locking and probe failure

Read and write use ACQUIRE()/ACQUIRE_ERR(). The device-managed debugfs cleanup action handles removal and setup rollback. A debugfs setup error is reported with dev_warn(), but does not by itself fail driver probe.

Thanks,
Alan

maintainer
#5 lore raw
Quoted 681 lines · alan

Hi Rong,

Quoted 619 lines · Rong Zhang

Hi Alan,

Thanks for working on this.

Quoted 3 lines · Alan Pluto

Expose Lenovo Other Mode WMI GetDataByPackage, method 99, through a root-only character device so userspace can access the raw firmware interface. And document the interface.

What's the practical purpose of the interface? Did you use this to tune anything on your device?

Quoted 5 lines

The upstream driver was build-tested with W=1 and checked with Sparse. A backport using the five-argument wmidev_invoke_method() interface was tested on Ubuntu 7.0.0-42 with Lenovo hardware. The upstream 7.4 driver has not been tested on hardware.

Don't include this in the commit message, but rather send a cover letter or place it under `---'.

Quoted 2 lines

Is the work assisted by LLM?

Quoted 15 lines
---
 Documentation/ABI/testing/dev-tac             |  49 +++
 .../wmi/devices/lenovo-wmi-other.rst          |   8 +
 MAINTAINERS                                   |   1 +
 drivers/platform/x86/lenovo/wmi-other.c       | 325 ++++++++++++++++++
 4 files changed, 383 insertions(+)
 create mode 100644 Documentation/ABI/testing/dev-tac
Documentation/ABI/testing/dev-tac new file +0 -0
new file mode 100644
index 00000000000..a8be8e95e2d

Documentation/ABI/testing/dev-tac line 7

Quoted 7 lines
Documentation/ABI/testing/dev-tac new file +7 -0
@@ -0,0 +1,49 @@
+What:           /dev/tac[0-9]*
+Date:           November 2026
+Contact:        [email protected]
+Description:
+                Lenovo Other Mode WMI GetDataByPackage (method 99).
+                Each firmware WMI instance gets a misc character device with
+                mode 0600. 

Why would you need to add a character device instead of a debugfs file? See also lwmi_debugfs_create_dir().

IMHO the interface is quite internal and undocumented. Exposing it as a character device makes it too reachable for users, unnecessarily increases the complexity of the driver, and requires some sorts of ABI stability.

Quoted 3 lines

Numbering follows allocation order, not necessarily
+ the firmware instance number.

You must be using a wordy LLM if you are using one. Isn't this just obvious and natural for most character devices?

Quoted 2 lines

Registration does not probe
+ support for method 99. The parent is the bound WMI device.

If it can't be probed, gate it unless a module parameter is set.

Documentation/ABI/testing/dev-tac line 13

Quoted 4 lines
Documentation/ABI/testing/dev-tac new file +4 -0
+
+                Open with O_RDWR. One write submits one complete request of
+                1..100 raw bytes; shorter requests are padded with zeroes to
+                100 bytes. Empty and oversized writes fail with EINVAL without

sed -E 's/\bE[A-Z]+\b/-\0/g'

Documentation/ABI/testing/dev-tac line 18

Quoted 8 lines
Documentation/ABI/testing/dev-tac new file +3 -0
+                calling firmware. Successful writes return the full submitted
+                length and execute two identical WMI calls for the BIOS
+                duplicate-call workaround. 

Request pairs through this

Documentation/ABI/testing/dev-tac new file +2 -0
+                interface are serialized; calls made by other driver
+                interfaces are not covered by this serialization.

Why would you need to mention cross-interface implementation details in a user-facing documentation?

Documentation/ABI/testing/dev-tac

Quoted 5 lines
Documentation/ABI/testing/dev-tac new file +4 -0
+                Evaluation failures abort the pair; the driver does not
+                automatically retry.
+
+                Each open file description owns its response. 

Why would you need to support multiple writers? Couldn't you simply return -EBUSY when there is a pending writer/reader?

Documentation/ABI/testing/dev-tac

Quoted 4 lines

A write while

Documentation/ABI/testing/dev-tac new file +2 -0
+                that response remains unread fails with EBUSY, including after
+                partial reads. 

If userspace must write all data at once, why would you need to support partial reads?

Documentation/ABI/testing/dev-tac line 26

Quoted 3 lines

A new open cannot read another open's response.

Documentation/ABI/testing/dev-tac new file +2 -0
+                Descriptors shared by dup/fork share the same transaction;
+                applications must coordinate the complete write/read sequence.

Aren't these just obvious and natural for most character devices, too?

Documentation/ABI/testing/dev-tac

Quoted 11 lines
Documentation/ABI/testing/dev-tac new file +10 -0
+
+                Read returns little-endian u32 DataSize followed by exactly
+                DataSize data bytes. Response storage is erased when fully
+                read or closed. Further reads return EOF until a new successful
+                write. Before a successful write, or after a failed firmware
+                evaluation, reads return ENODATA. A zero-length read returns
+                zero without consuming data.
+
+                Requests are synchronous even with O_NONBLOCK. Seeking and
+                positional I/O are not supported. 

Aren't these just the default characteristic for most non-block character devices?

Quoted 1 line

There is no ioctl interface.

Too verbose. If you don't say it has some, it's obvious that it has none.

Documentation/ABI/testing/dev-tac line 45

Quoted 9 lines
Documentation/ABI/testing/dev-tac new file +9 -0
+                A removed WMI device causes nonempty reads and valid writes
+                on existing descriptors to fail with ENODEV.
+
+                Firmware evaluation failures map to EIO. WMI marshalling
+                failures return the error reported by the WMI subsystem.
+                A truncated response or invalid DataSize maps to EPROTO.
+                Bad userspace memory maps to EFAULT. Failure does not imply
+                firmware had no side effects; never blindly resubmit commands.
+                Interrupted waits can return EINTR before firmware evaluation.

Obvious, no?

Documentation/ABI/testing/dev-tac

Quoted 2 lines
Documentation/ABI/testing/dev-tac new file +2 -0
+
+                The WMI subsystem converts returned ACPI objects to a buffer.

Yet another implementation detail.

Quoted 136 lines
Documentation/ABI/testing/dev-tac new file +2 -0
+                This raw firmware interface may change device state. Use only
+                commands whose behavior and side effects are understood.
Documentation/wmi/devices/lenovo-wmi-other.rst +8 -0
index 011054d64ea..ec85fdeeb7a 100644
@@ -33,6 +33,10 @@ under the following path:
 
 Additionally, this driver also exports attributes to HWMON.
 
+The driver also exposes GetDataByPackage (method 99) through a raw character
+device for each WMI instance. See Documentation/ABI/testing/dev-tac for the
+request and response format and its access restrictions.
+
 LENOVO_CAPABILITY_DATA_00
 -------------------------
 
@@ -109,6 +113,10 @@ WMI interface description
 The WMI interface description can be decoded from the embedded binary MOF (bmof)
 data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility:
 
+The example below describes an older firmware revision and specifies a
+40-byte maximum input for GetDataByPackage. Newer firmware uses a 100-byte
+input, which is the size used by the raw interface described above.
+
 ::
 
   [WMI, Dynamic, Provider("WmiProv"), Locale("MS\\0x409"), Description("LENOVO_OTHER_METHOD class"), guid("{dc2a8805-3a8c-41ba-a6f7-092e0089cd3b}")]
MAINTAINERS +1 -0
index 981eb3fdbef..84b1545f525 100644
@@ -15168,6 +15168,7 @@ M:	Derek J. Clark <[email protected]>
 M:	Rong Zhang <[email protected]>
 L:	[email protected]
 S:	Maintained
+F:	Documentation/ABI/testing/dev-tac
 F:	Documentation/wmi/devices/lenovo-wmi-gamezone.rst
 F:	Documentation/wmi/devices/lenovo-wmi-other.rst
 F:	drivers/platform/x86/lenovo/*
drivers/platform/x86/lenovo/wmi-other.c +57 -0
index bd64e7b4466..c15b3a2a758 100644
@@ -34,16 +34,23 @@
 #include <linux/device.h>
 #include <linux/export.h>
 #include <linux/firmware_attributes.h>
+#include <linux/fs.h>
 #include <linux/gfp_types.h>
 #include <linux/hwmon.h>
 #include <linux/idr.h>
 #include <linux/kdev_t.h>
 #include <linux/kobject.h>
+#include <linux/kref.h>
 #include <linux/limits.h>
+#include <linux/miscdevice.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/platform_profile.h>
 #include <linux/power_supply.h>
+#include <linux/slab.h>
 #include <linux/types.h>
+#include <linux/uaccess.h>
+#include <linux/unaligned.h>
 #include <linux/wmi.h>
 
 #include <acpi/battery.h>
@@ -91,6 +98,9 @@ enum lwmi_feature_id_psu {
 #define LWMI_FEATURE_VALUE_GET 17
 #define LWMI_FEATURE_VALUE_SET 18
 
+#define LWMI_DATA_PACKAGE_GET 99
+#define LWMI_DATA_PACKAGE_INPUT_MAX 100
+
 #define LWMI_FAN_ID_BASE 1
 #define LWMI_FAN_NR 4
 #define LWMI_FAN_ID(x) ((x) + LWMI_FAN_ID_BASE)
@@ -115,6 +125,27 @@ enum lwmi_feature_id_psu {
 
 static DEFINE_IDA(lwmi_om_ida);
 
+static DEFINE_IDA(lwmi_tac_ida);
+static DEFINE_MUTEX(lwmi_tac_lock);
+
+struct lwmi_tac {
+	struct miscdevice misc;
+	struct kref ref;
+	struct wmi_device *wdev;
+	u8 instance;
+	int id;
+};
+
+struct lwmi_tac_file {
+	struct lwmi_tac *tac;
+	/* Protects the per-open response and its position. */
+	struct mutex lock;
+	void *result;
+	size_t result_len;
+	loff_t position;
+	bool completed;
+};
+
 enum attribute_property {
 	DEFAULT_VAL,
 	MAX_VAL,
@@ -1609,6 +1640,295 @@ static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv)
 	priv->ida_id = -EIDRM;
 }
 
+/**
+ * lwmi_tac_free() - Free an unregistered endpoint after its last close
+ * @ref: Reference belonging to the endpoint.
+ */
+static void lwmi_tac_free(struct kref *ref)
+{
+	struct lwmi_tac *tac = container_of(ref, struct lwmi_tac, ref);
+
+	ida_free(&lwmi_tac_ida, tac->id);
+	kfree(tac->misc.name);
+	kfree(tac);
+}
+
+/**
+ * lwmi_tac_open() - Allocate independent response state for an open file
+ * @inode: Character device inode.
+ * @file: File initially pointing to the registered miscdevice.
+ *
+ * The misc core serializes open against deregistration, so the registration
+ * reference remains valid while the file acquires its own reference.
+ *
+ * Return: 0 on success, or -ENOMEM.
+ */
+static int lwmi_tac_open(struct inode *inode, struct file *file)
+{
+	struct lwmi_tac *tac = container_of(file->private_data, struct lwmi_tac, misc);

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1678

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	struct lwmi_tac_file *state;
+
+	state = kzalloc_obj(*state);
+	if (!state)
+		return -ENOMEM;
+	mutex_init(&state->lock);
+	state->tac = tac;
+	kref_get(&tac->ref);
+	file->private_data = state;
+	return stream_open(inode, file);

Add line breaks to improve readability. Same for the following methods.

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+}
+
+/**
+ * lwmi_tac_release() - Erase the response and drop the endpoint reference

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1692

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+ * @inode: Character device inode.
+ * @file: File holding the endpoint state.
+ *
+ * Return: 0.
+ */
+static int lwmi_tac_release(struct inode *inode, struct file *file)
+{
+	struct lwmi_tac_file *state = file->private_data;
+
+	kfree_sensitive(state->result);

Why on earth is it sensitive?

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv)

Quoted 13 lines
drivers/platform/x86/lenovo/wmi-other.c +12 -0
+	mutex_destroy(&state->lock);
+	kref_put(&state->tac->ref, lwmi_tac_free);
+	kfree(state);
+	return 0;
+}
+
+/**
+ * lwmi_tac_invoke() - Execute GetDataByPackage and retain the response
+ * @state: Open file whose response buffer is empty.
+ * @data: Zero-padded 100-byte input buffer.
+ *
+ * The caller holds state->lock and lwmi_tac_lock. 
 

Add lockdep assertions instead of stating them in comments.

Quoted 8 lines

The latter serializes call
+ * pairs across instances and keeps the WMI device valid during evaluation.
+ *
+ * Return: 0 on success, or a negative error code.
+ */

drivers/platform/x86/lenovo/wmi-other.c +3 -0
+static int lwmi_tac_invoke(struct lwmi_tac_file *state, u8 *data)
+{
+	struct lwmi_tac *tac = state->tac;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1721

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	struct wmi_buffer input = { .length = LWMI_DATA_PACKAGE_INPUT_MAX, .data = data };
+	struct wmi_buffer output;
+	u32 data_size;
+	int ret;
+
+	if (!tac->wdev)
+		return -ENODEV;
+	/*
+	 * Duplicated call required to match BIOS workaround for behavior
+	 * seen when WMI accessed via scripting on other OS.

Reference? Where is the script?

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1731

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	 */
+	for (unsigned int call = 0; call < 2; call++) {
+		ret = wmidev_invoke_method(tac->wdev, tac->instance,
+					   LWMI_DATA_PACKAGE_GET, &input, &output, 0);
+		if (ret)
+			return ret;
+		if (!call)
+			kfree_sensitive(output.data);
+	}
+	/* Validate DataSize before preserving the complete firmware response. */

Don't retell what the code does, but rather explain why.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1732

Quoted 1 line
drivers/platform/x86/lenovo/wmi-other.c +1 -0
+	if (output.length < sizeof(__le32)) {

Why not use wmidev_invoke_method()'s min_size argument?

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1736

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+		ret = -EPROTO;
+		goto free_output;
+	}
+	data_size = get_unaligned_le32(output.data);

wmidev_invoke_method() returns aligned data.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1742

Quoted 6 lines
drivers/platform/x86/lenovo/wmi-other.c +6 -0
+	if (data_size > output.length - sizeof(__le32)) {
+		ret = -EPROTO;
+		goto free_output;
+	}
+	/* Transfer ownership without another allocation after firmware ran. */
+	state->result = output.data;

Use __free() and no_free_ptr().

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1743

Quoted 1 line
drivers/platform/x86/lenovo/wmi-other.c +1 -0
+	state->result_len = sizeof(__le32) + data_size;

Couldn't you declare a struct with a flex array member to represent this?

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1753

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	state->position = 0;
+	state->completed = true;
+	return 0;
+free_output:
+	kfree_sensitive(output.data);
+	return ret;
+}
+
+/**
+ * lwmi_tac_write() - Submit one complete TAC request

"complete"

Obvious.

Quoted 7 lines

+ * @file: Open TAC endpoint with independent response state.
+ * @buffer: Userspace input bytes.
+ * @count: Input size, from 1 to 100 bytes.
+ * @ppos: Unused;

TAC is a nonseekable request/response stream.

Obvious.

Quoted 15 lines

+ *
+ * Short inputs are zero-padded. An unread response must be consumed before
+ * submitting another request. A failed firmware call is never retried here.
+ *
+ * Return: @count on success, or a negative error code.
+ */

drivers/platform/x86/lenovo/wmi-other.c +9 -0
+static ssize_t lwmi_tac_write(struct file *file, const char __user *buffer,
+			      size_t count, loff_t *ppos)
+{
+	struct lwmi_tac_file *state = file->private_data;
+	u8 input[LWMI_DATA_PACKAGE_INPUT_MAX] = {};
+	int ret;
+
+	if (!count || count > sizeof(input))
+		return -EINVAL;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1782

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	if (copy_from_user(input, buffer, count)) {
+		ret = -EFAULT;
+		goto clear_input;
+	}
+	ret = mutex_lock_interruptible(&state->lock);
+	if (ret)
+		goto clear_input;
+	ret = mutex_lock_interruptible(&lwmi_tac_lock);
+	if (ret)
+		goto unlock;

Use scoped_cond_guard() or ACQUIRE().

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1795

Quoted 13 lines
drivers/platform/x86/lenovo/wmi-other.c +13 -0
+	if (!state->tac->wdev) {
+		ret = -ENODEV;
+	} else if (state->result) {
+		ret = -EBUSY;
+	} else {
+		state->completed = false;
+		ret = lwmi_tac_invoke(state, input);
+	}
+	mutex_unlock(&lwmi_tac_lock);
+unlock:
+	mutex_unlock(&state->lock);
+clear_input:
+	memzero_explicit(input, sizeof(input));

Why on earth is it sensitive, too?

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1802

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +7 -0
+	if (ret)
+		return ret;
+	return count;
+}
+
+/**
+ * lwmi_tac_read() - Read only this open file's firmware response

"only"

Obvious, no?

Quoted 38 lines

+ * @file: Open TAC endpoint.
+ * @buffer: Userspace destination.
+ * @count: Maximum number of bytes to return.
+ * @ppos: Unused; the response position is protected by state->lock.
+ *
+ * Responses contain little-endian u32 DataSize followed by Data. Partial
+ * reads advance the response position. Consuming the result erases its
+ * storage immediately; subsequent reads return EOF until another write.
+ *
+ * Return: Bytes read, 0 at EOF or for zero @count, or a negative error code.
+ */

drivers/platform/x86/lenovo/wmi-other.c +27 -0
+static ssize_t lwmi_tac_read(struct file *file, char __user *buffer,
+			     size_t count, loff_t *ppos)
+{
+	struct lwmi_tac_file *state = file->private_data;
+	ssize_t ret;
+
+	if (!count)
+		return 0;
+	ret = mutex_lock_interruptible(&state->lock);
+	if (ret)
+		return ret;
+	ret = mutex_lock_interruptible(&lwmi_tac_lock);
+	if (ret)
+		goto unlock;
+	if (!state->tac->wdev)
+		ret = -ENODEV;
+	mutex_unlock(&lwmi_tac_lock);
+	if (ret) {
+		kfree_sensitive(state->result);
+		state->result = NULL;
+		goto unlock;
+	}
+	if (!state->completed) {
+		ret = -ENODATA;
+		goto unlock;
+	}
+	if (!state->result)

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1850

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+		goto unlock;
+	ret = simple_read_from_buffer(buffer, count, &state->position,
+				      state->result, state->result_len);
+	if (state->position == (loff_t)state->result_len) {
+		kfree_sensitive(state->result);
+		state->result = NULL;
+	}
+unlock:
+	mutex_unlock(&state->lock);
+	return ret;

The entire method is in a horrible mess. Drop the support for partial reads, and use scoped_cond_guard()/ACQUIRE(), and __free() to tidy it up.

Quoted 30 lines
drivers/platform/x86/lenovo/wmi-other.c +30 -0
+}
+
+static const struct file_operations lwmi_tac_fops = {
+	.owner = THIS_MODULE,
+	.open = lwmi_tac_open,
+	.release = lwmi_tac_release,
+	.read = lwmi_tac_read,
+	.write = lwmi_tac_write,
+};
+
+/**
+ * lwmi_tac_remove() - Unregister an endpoint and block further firmware calls
+ * @data: Endpoint registered as a device-managed cleanup action.
+ *
+ * Stop new opens before waiting for an in-flight call pair. Existing files
+ * retain the endpoint state, but their subsequent reads/writes return -ENODEV.
+ */
+static void lwmi_tac_remove(void *data)
+{
+	struct lwmi_tac *tac = data;
+
+	misc_deregister(&tac->misc);
+	mutex_lock(&lwmi_tac_lock);
+	tac->wdev = NULL;
+	mutex_unlock(&lwmi_tac_lock);
+	kref_put(&tac->ref, lwmi_tac_free);
+}
+
+/**
+ * lwmi_tac_add() - Register TAC endpoints independently of capability data

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1890

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+ * @wdev: Bound Lenovo Other Mode WMI device.
+ *
+ * No capability describes method 99, so registration does not probe firmware
+ * support. Each firmware instance gets an endpoint; unsupported method calls
+ * are reported to userspace when requested.
+ *
+ * Return: 0 on success, or a negative error code. Device-managed cleanup
+ * unregisters earlier endpoints if a later registration or probe step fails.
+ */
+static int lwmi_tac_add(struct wmi_device *wdev)

devm_

Quoted 56 lines
drivers/platform/x86/lenovo/wmi-other.c +42 -0
+{
+	unsigned int instances = wmidev_instance_count(wdev);
+	int ret;
+
+	for (unsigned int instance = 0; instance < instances; instance++) {
+		struct lwmi_tac *tac;
+
+		tac = kzalloc_obj(*tac);
+		if (!tac)
+			return -ENOMEM;
+		ret = ida_alloc(&lwmi_tac_ida, GFP_KERNEL);
+		if (ret < 0) {
+			kfree(tac);
+			return ret;
+		}
+		tac->id = ret;
+		kref_init(&tac->ref);
+		tac->wdev = wdev;
+		tac->instance = instance;
+		tac->misc.minor = MISC_DYNAMIC_MINOR;
+		tac->misc.name = kasprintf(GFP_KERNEL, "tac%d", tac->id);
+		tac->misc.fops = &lwmi_tac_fops;
+		tac->misc.parent = &wdev->dev;
+		tac->misc.mode = 0600;
+		if (!tac->misc.name) {
+			kref_put(&tac->ref, lwmi_tac_free);
+			return -ENOMEM;
+		}
+		ret = misc_register(&tac->misc);
+		if (ret) {
+			kref_put(&tac->ref, lwmi_tac_free);
+			return ret;
+		}
+		/* Unregister before the WMI device and its managed state disappear. */
+		ret = devm_add_action_or_reset(&wdev->dev, lwmi_tac_remove, tac);
+		if (ret)
+			return ret;
+	}
+	return 0;
+}
+
 /* ======== Self (master: lenovo-wmi-other) ======== */
 
 /**
@@ -1693,6 +2013,7 @@ static int lwmi_other_probe(struct wmi_device *wdev, const void *context)
 {
 	struct component_match *master_match = NULL;
 	struct lwmi_om_priv *priv;
+	int ret;
 
 	priv = devm_kzalloc_obj(&wdev->dev, *priv);
 	if (!priv)
@@ -1708,6 +2029,10 @@ static int lwmi_other_probe(struct wmi_device *wdev, const void *context)
 	if (IS_ERR(master_match))
 		return PTR_ERR(master_match);
 
Quoted 15 lines

drivers/platform/x86/lenovo/wmi-other.c static int lwmi_other_probe(struct wmi_device *wdev, const void *context) · line 2034

Quoted 3 lines
drivers/platform/x86/lenovo/wmi-other.c +3 -0
+	ret = lwmi_tac_add(wdev);
+	if (ret)
+		return ret;

Why is this fatal?

Thanks,
Rong

Quoted 6 lines
drivers/platform/x86/lenovo/wmi-other.c +1 -0
+
 	return component_master_add_with_match(&wdev->dev, &lwmi_om_master_ops,
 					       master_match);
 }

base-commit: 5680519168f65666d2c03cc6ed8fe2b7052aee12

Thanks for the detailed review. I sent v2 patch separately by referring your suggestion. Please help review it.
My replies to your points are below.

1. Practical purpose and tuning

The known use is for Lenovo utility to communicate with firmware. In v2 I use a default-off debugfs interface instead of stable userspace interface.

2. Test information in the commit message

I removed the build and test details from the v2 commit message.

3. LLM assistance

Yes, I used AI assistance for the port and review. I reviewed the changes and am responsible for the submission.

4. Character device versus debugfs, and the module parameter

I replaced the character device with a 0600 debugfs file created under lwmi_debugfs_create_dir(). There is no non-invasive probe for method 99, so creation is disabled by default and requires enable_get_data_by_package=1 when loading the module. I removed the v1 ABI document and its MAINTAINERS entry.

5. ABI documentation and verbosity

Removed the character-device ABI document change.

6. Multiple writers and partial reads

The debugfs file permits one open only per WMI device.

7. Readability and comments

I simplified the handlers and removed the redundant kernel-doc and comments that merely described normal file operations. I also removed the unsupported reference to a script on another OS.

8. Locking

Quoted 8 lines

With one open per WMI device, a per-open lock protects the response and serializes operations through shared descriptors. lwmi_package_invoke() uses lockdep_assert_held() for that lock.

9. Sensitive data

The firmware response can contain sensitive data, so I retained kfree_sensitive() for buffers returned successfully by WMI.

Vague and broad statements like that are unacceptable for me.

Either provide clear and unambiguous justification or do not use _sensitive. I'll not accept doing it using "just in case" reasoning. You might want to split code path for the sensitive case for clarity.

Quoted 15 lines

10. Duplicate calls

You can refer as think-lmi.c, same duplicated call. The reason is invoking WMI under Windows OS will result the BIOS ACPI code execute twice, so BIOS implemented the workaround, thus Linux driver will need the duplicated call to match the BIOS workaround.

11. Response size, alignment and cleanup

The second wmidev_invoke_method() call passes the response header size as min_size. The driver separately validates DataSize against the returned buffer length. The response is represented by an aligned structure with a flexible array; after the successful call I declare it with __free(kfree_sensitive) and transfer ownership with no_free_ptr().

Quoted 5 lines

12. Scoped locking and probe failure

Read and write use ACQUIRE()/ACQUIRE_ERR(). The device-managed debugfs cleanup action handles removal and setup rollback. A debugfs setup error is reported with dev_warn(), but does not by itself fail driver probe.

In future, please reply properly to the review comments (inline), not adding an LLM-generated reply to tail. Tail (or head) replies makes it hard to connect the reviewer comment to your reply and you don't want to upset the acting maintainer with that. ;-)

Signature · 1 line
 i.
maintainer
#6 lore raw
In reply to Alan Pluto

Hi Alan,

Quoted 556 lines · Alan Pluto

Expose Lenovo Other Mode WMI GetDataByPackage, method 99, through a root-only character device so userspace can access the raw firmware interface. And document the interface.

The upstream driver was build-tested with W=1 and checked with Sparse. A backport using the five-argument wmidev_invoke_method() interface was tested on Ubuntu 7.0.0-42 with Lenovo hardware. The upstream 7.4 driver has not been tested on hardware.

---
 Documentation/ABI/testing/dev-tac             |  49 +++
 .../wmi/devices/lenovo-wmi-other.rst          |   8 +
 MAINTAINERS                                   |   1 +
 drivers/platform/x86/lenovo/wmi-other.c       | 325 ++++++++++++++++++
 4 files changed, 383 insertions(+)
 create mode 100644 Documentation/ABI/testing/dev-tac
Documentation/ABI/testing/dev-tac new file +6 -0
new file mode 100644
index 00000000000..a8be8e95e2d
@@ -0,0 +1,49 @@
+What:           /dev/tac[0-9]*
+Date:           November 2026
+Contact:        [email protected]
+Description:
+                Lenovo Other Mode WMI GetDataByPackage (method 99).
+                Each firmware WMI instance gets a misc character 

device with
+ mode 0600. Numbering follows allocation order, not
necessarily
+ the firmware instance number. Registration does not
probe
+ support for method 99. The parent is the bound WMI
device.

Documentation/ABI/testing/dev-tac new file +2 -0
+
+                Open with O_RDWR. One write submits one complete 

request of
+ 1..100 raw bytes; shorter requests are padded with
zeroes to
+ 100 bytes. Empty and oversized writes fail with EINVAL
without
+ calling firmware. Successful writes return the full
submitted

Documentation/ABI/testing/dev-tac new file +8 -0
+                length and execute two identical WMI calls for the BIOS
+                duplicate-call workaround. Request pairs through this
+                interface are serialized; calls made by other driver
+                interfaces are not covered by this serialization.
+                Evaluation failures abort the pair; the driver does not
+                automatically retry.
+
+                Each open file description owns its response. A write 

while
+ that response remains unread fails with EBUSY,
including after
+ partial reads. A new open cannot read another open's
response.
+ Descriptors shared by dup/fork share the same
transaction;
+ applications must coordinate the complete write/read
sequence.

Documentation/ABI/testing/dev-tac new file +2 -0
+
+                Read returns little-endian u32 DataSize followed by 

exactly
+ DataSize data bytes. Response storage is erased when
fully
+ read or closed. Further reads return EOF until a new
successful
+ write. Before a successful write, or after a failed
firmware
+ evaluation, reads return ENODATA. A zero-length read
returns

Documentation/ABI/testing/dev-tac new file +3 -0
+                zero without consuming data.
+
+                Requests are synchronous even with O_NONBLOCK. Seeking 

and
+ positional I/O are not supported. There is no ioctl
interface.
+ A removed WMI device causes nonempty reads and valid
writes

Documentation/ABI/testing/dev-tac new file +3 -0
+                on existing descriptors to fail with ENODEV.
+
+                Firmware evaluation failures map to EIO. WMI 

marshalling
+ failures return the error reported by the WMI
subsystem.
+ A truncated response or invalid DataSize maps to
EPROTO.
+ Bad userspace memory maps to EFAULT. Failure does not
imply
+ firmware had no side effects; never blindly resubmit
commands.
+ Interrupted waits can return EINTR before firmware
evaluation.

Documentation/ABI/testing/dev-tac new file +2 -0
+
+                The WMI subsystem converts returned ACPI objects to a 

buffer.
+ This raw firmware interface may change device state.
Use only
+ commands whose behavior and side effects are
understood.

Documentation/wmi/devices/lenovo-wmi-other.rst +8 -0
index 011054d64ea..ec85fdeeb7a 100644
@@ -33,6 +33,10 @@ under the following path:
 
 Additionally, this driver also exports attributes to HWMON.
 
+The driver also exposes GetDataByPackage (method 99) through a raw character
+device for each WMI instance. See Documentation/ABI/testing/dev-tac for the
+request and response format and its access restrictions.
+
 LENOVO_CAPABILITY_DATA_00
 -------------------------
 
@@ -109,6 +113,10 @@ WMI interface description
 The WMI interface description can be decoded from the embedded binary 
 MOF (bmof)
 data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility:
 
+The example below describes an older firmware revision and specifies a
+40-byte maximum input for GetDataByPackage. Newer firmware uses a 100-byte
+input, which is the size used by the raw interface described above.
+
 ::
 
   [WMI, Dynamic, Provider("WmiProv"), Locale("MS\\0x409"), 

Description("LENOVO_OTHER_METHOD class"),
guid("{dc2a8805-3a8c-41ba-a6f7-092e0089cd3b}")]

MAINTAINERS +1 -0
index 981eb3fdbef..84b1545f525 100644
@@ -15168,6 +15168,7 @@ M:	Derek J. Clark <[email protected]>
 M:	Rong Zhang <[email protected]>
 L:	[email protected]
 S:	Maintained
+F:	Documentation/ABI/testing/dev-tac
 F:	Documentation/wmi/devices/lenovo-wmi-gamezone.rst
 F:	Documentation/wmi/devices/lenovo-wmi-other.rst
 F:	drivers/platform/x86/lenovo/*
drivers/platform/x86/lenovo/wmi-other.c +46 -0
index bd64e7b4466..c15b3a2a758 100644
@@ -34,16 +34,23 @@
 #include <linux/device.h>
 #include <linux/export.h>
 #include <linux/firmware_attributes.h>
+#include <linux/fs.h>
 #include <linux/gfp_types.h>
 #include <linux/hwmon.h>
 #include <linux/idr.h>
 #include <linux/kdev_t.h>
 #include <linux/kobject.h>
+#include <linux/kref.h>
 #include <linux/limits.h>
+#include <linux/miscdevice.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/platform_profile.h>
 #include <linux/power_supply.h>
+#include <linux/slab.h>
 #include <linux/types.h>
+#include <linux/uaccess.h>
+#include <linux/unaligned.h>
 #include <linux/wmi.h>
 
 #include <acpi/battery.h>
@@ -91,6 +98,9 @@ enum lwmi_feature_id_psu {
 #define LWMI_FEATURE_VALUE_GET 17
 #define LWMI_FEATURE_VALUE_SET 18
 
+#define LWMI_DATA_PACKAGE_GET 99
+#define LWMI_DATA_PACKAGE_INPUT_MAX 100
+
 #define LWMI_FAN_ID_BASE 1
 #define LWMI_FAN_NR 4
 #define LWMI_FAN_ID(x) ((x) + LWMI_FAN_ID_BASE)
@@ -115,6 +125,27 @@ enum lwmi_feature_id_psu {
 
 static DEFINE_IDA(lwmi_om_ida);
 
+static DEFINE_IDA(lwmi_tac_ida);
+static DEFINE_MUTEX(lwmi_tac_lock);
+
+struct lwmi_tac {
+	struct miscdevice misc;
+	struct kref ref;
+	struct wmi_device *wdev;
+	u8 instance;
+	int id;
+};
+
+struct lwmi_tac_file {
+	struct lwmi_tac *tac;
+	/* Protects the per-open response and its position. */
+	struct mutex lock;
+	void *result;
+	size_t result_len;
+	loff_t position;
+	bool completed;
+};
+
 enum attribute_property {
 	DEFAULT_VAL,
 	MAX_VAL,
@@ -1609,6 +1640,295 @@ static void lwmi_om_fw_attr_remove(struct 
 lwmi_om_priv *priv)
 	priv->ida_id = -EIDRM;
 }
 
+/**
+ * lwmi_tac_free() - Free an unregistered endpoint after its last close
+ * @ref: Reference belonging to the endpoint.
+ */
+static void lwmi_tac_free(struct kref *ref)
+{
+	struct lwmi_tac *tac = container_of(ref, struct lwmi_tac, ref);
+
+	ida_free(&lwmi_tac_ida, tac->id);
+	kfree(tac->misc.name);
+	kfree(tac);
+}
+
+/**
+ * lwmi_tac_open() - Allocate independent response state for an open 

file
+ * @inode: Character device inode.
+ * @file: File initially pointing to the registered miscdevice.
+ *
+ * The misc core serializes open against deregistration, so the registration
+ * reference remains valid while the file acquires its own reference.
+ *
+ * Return: 0 on success, or -ENOMEM.
+ */

drivers/platform/x86/lenovo/wmi-other.c +3 -0
+static int lwmi_tac_open(struct inode *inode, struct file *file)
+{
+	struct lwmi_tac *tac = container_of(file->private_data, struct 

lwmi_tac, misc);

drivers/platform/x86/lenovo/wmi-other.c +14 -0
+	struct lwmi_tac_file *state;
+
+	state = kzalloc_obj(*state);
+	if (!state)
+		return -ENOMEM;
+	mutex_init(&state->lock);
+	state->tac = tac;
+	kref_get(&tac->ref);
+	file->private_data = state;
+	return stream_open(inode, file);
+}
+
+/**
+ * lwmi_tac_release() - Erase the response and drop the endpoint 

reference
+ * @inode: Character device inode.
+ * @file: File holding the endpoint state.
+ *
+ * Return: 0.
+ */

drivers/platform/x86/lenovo/wmi-other.c +17 -0
+static int lwmi_tac_release(struct inode *inode, struct file *file)
+{
+	struct lwmi_tac_file *state = file->private_data;
+
+	kfree_sensitive(state->result);
+	mutex_destroy(&state->lock);
+	kref_put(&state->tac->ref, lwmi_tac_free);
+	kfree(state);
+	return 0;
+}
+
+/**
+ * lwmi_tac_invoke() - Execute GetDataByPackage and retain the response
+ * @state: Open file whose response buffer is empty.
+ * @data: Zero-padded 100-byte input buffer.
+ *
+ * The caller holds state->lock and lwmi_tac_lock. The latter 

serializes call
+ * pairs across instances and keeps the WMI device valid during evaluation.
+ *
+ * Return: 0 on success, or a negative error code.
+ */

drivers/platform/x86/lenovo/wmi-other.c +4 -0
+static int lwmi_tac_invoke(struct lwmi_tac_file *state, u8 *data)
+{
+	struct lwmi_tac *tac = state->tac;
+	struct wmi_buffer input = { .length = LWMI_DATA_PACKAGE_INPUT_MAX, 

.data = data };

drivers/platform/x86/lenovo/wmi-other.c +19 -0
+	struct wmi_buffer output;
+	u32 data_size;
+	int ret;
+
+	if (!tac->wdev)
+		return -ENODEV;
+	/*
+	 * Duplicated call required to match BIOS workaround for behavior
+	 * seen when WMI accessed via scripting on other OS.
+	 */
+	for (unsigned int call = 0; call < 2; call++) {
+		ret = wmidev_invoke_method(tac->wdev, tac->instance,
+					   LWMI_DATA_PACKAGE_GET, &input, &output, 0);
+		if (ret)
+			return ret;
+		if (!call)
+			kfree_sensitive(output.data);
+	}
+	/* Validate DataSize before preserving the complete firmware 

response. */

drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	if (output.length < sizeof(__le32)) {
+		ret = -EPROTO;
+		goto free_output;
+	}
+	data_size = get_unaligned_le32(output.data);
+	if (data_size > output.length - sizeof(__le32)) {
+		ret = -EPROTO;
+		goto free_output;
+	}
+	/* Transfer ownership without another allocation after firmware ran. 

*/

drivers/platform/x86/lenovo/wmi-other.c +18 -0
+	state->result = output.data;
+	state->result_len = sizeof(__le32) + data_size;
+	state->position = 0;
+	state->completed = true;
+	return 0;
+free_output:
+	kfree_sensitive(output.data);
+	return ret;
+}
+
+/**
+ * lwmi_tac_write() - Submit one complete TAC request
+ * @file: Open TAC endpoint with independent response state.
+ * @buffer: Userspace input bytes.
+ * @count: Input size, from 1 to 100 bytes.
+ * @ppos: Unused; TAC is a nonseekable request/response stream.
+ *
+ * Short inputs are zero-padded. An unread response must be consumed 

before
+ * submitting another request. A failed firmware call is never retried here.
+ *
+ * Return: @count on success, or a negative error code.
+ */
+static ssize_t lwmi_tac_write(struct file *file, const char __user *buffer,

drivers/platform/x86/lenovo/wmi-other.c +44 -0
+			      size_t count, loff_t *ppos)
+{
+	struct lwmi_tac_file *state = file->private_data;
+	u8 input[LWMI_DATA_PACKAGE_INPUT_MAX] = {};
+	int ret;
+
+	if (!count || count > sizeof(input))
+		return -EINVAL;
+	if (copy_from_user(input, buffer, count)) {
+		ret = -EFAULT;
+		goto clear_input;
+	}
+	ret = mutex_lock_interruptible(&state->lock);
+	if (ret)
+		goto clear_input;
+	ret = mutex_lock_interruptible(&lwmi_tac_lock);
+	if (ret)
+		goto unlock;
+	if (!state->tac->wdev) {
+		ret = -ENODEV;
+	} else if (state->result) {
+		ret = -EBUSY;
+	} else {
+		state->completed = false;
+		ret = lwmi_tac_invoke(state, input);
+	}
+	mutex_unlock(&lwmi_tac_lock);
+unlock:
+	mutex_unlock(&state->lock);
+clear_input:
+	memzero_explicit(input, sizeof(input));
+	if (ret)
+		return ret;
+	return count;
+}
+
+/**
+ * lwmi_tac_read() - Read only this open file's firmware response
+ * @file: Open TAC endpoint.
+ * @buffer: Userspace destination.
+ * @count: Maximum number of bytes to return.
+ * @ppos: Unused; the response position is protected by state->lock.
+ *
+ * Responses contain little-endian u32 DataSize followed by Data. 

Partial
+ * reads advance the response position. Consuming the result erases its
+ * storage immediately; subsequent reads return EOF until another write.
+ *
+ * Return: Bytes read, 0 at EOF or for zero @count, or a negative error code.
+ */

drivers/platform/x86/lenovo/wmi-other.c +49 -0
+static ssize_t lwmi_tac_read(struct file *file, char __user *buffer,
+			     size_t count, loff_t *ppos)
+{
+	struct lwmi_tac_file *state = file->private_data;
+	ssize_t ret;
+
+	if (!count)
+		return 0;
+	ret = mutex_lock_interruptible(&state->lock);
+	if (ret)
+		return ret;
+	ret = mutex_lock_interruptible(&lwmi_tac_lock);
+	if (ret)
+		goto unlock;
+	if (!state->tac->wdev)
+		ret = -ENODEV;
+	mutex_unlock(&lwmi_tac_lock);
+	if (ret) {
+		kfree_sensitive(state->result);
+		state->result = NULL;
+		goto unlock;
+	}
+	if (!state->completed) {
+		ret = -ENODATA;
+		goto unlock;
+	}
+	if (!state->result)
+		goto unlock;
+	ret = simple_read_from_buffer(buffer, count, &state->position,
+				      state->result, state->result_len);
+	if (state->position == (loff_t)state->result_len) {
+		kfree_sensitive(state->result);
+		state->result = NULL;
+	}
+unlock:
+	mutex_unlock(&state->lock);
+	return ret;
+}
+
+static const struct file_operations lwmi_tac_fops = {
+	.owner = THIS_MODULE,
+	.open = lwmi_tac_open,
+	.release = lwmi_tac_release,
+	.read = lwmi_tac_read,
+	.write = lwmi_tac_write,
+};
+
+/**
+ * lwmi_tac_remove() - Unregister an endpoint and block further 

firmware calls
+ * @data: Endpoint registered as a device-managed cleanup action.
+ *
+ * Stop new opens before waiting for an in-flight call pair. Existing files
+ * retain the endpoint state, but their subsequent reads/writes return

drivers/platform/x86/lenovo/wmi-other.c +14 -1
-ENODEV.
+ */
+static void lwmi_tac_remove(void *data)
+{
+	struct lwmi_tac *tac = data;
+
+	misc_deregister(&tac->misc);
+	mutex_lock(&lwmi_tac_lock);
+	tac->wdev = NULL;
+	mutex_unlock(&lwmi_tac_lock);
+	kref_put(&tac->ref, lwmi_tac_free);
+}
+
+/**
+ * lwmi_tac_add() - Register TAC endpoints independently of capability 

data
+ * @wdev: Bound Lenovo Other Mode WMI device.
+ *
+ * No capability describes method 99, so registration does not probe firmware
+ * support. Each firmware instance gets an endpoint; unsupported method calls
+ * are reported to userspace when requested.
+ *
+ * Return: 0 on success, or a negative error code. Device-managed cleanup
+ * unregisters earlier endpoints if a later registration or probe step fails.
+ */

drivers/platform/x86/lenovo/wmi-other.c +35 -0
+static int lwmi_tac_add(struct wmi_device *wdev)
+{
+	unsigned int instances = wmidev_instance_count(wdev);
+	int ret;
+
+	for (unsigned int instance = 0; instance < instances; instance++) {
+		struct lwmi_tac *tac;
+
+		tac = kzalloc_obj(*tac);
+		if (!tac)
+			return -ENOMEM;
+		ret = ida_alloc(&lwmi_tac_ida, GFP_KERNEL);
+		if (ret < 0) {
+			kfree(tac);
+			return ret;
+		}
+		tac->id = ret;
+		kref_init(&tac->ref);
+		tac->wdev = wdev;
+		tac->instance = instance;
+		tac->misc.minor = MISC_DYNAMIC_MINOR;
+		tac->misc.name = kasprintf(GFP_KERNEL, "tac%d", tac->id);
+		tac->misc.fops = &lwmi_tac_fops;
+		tac->misc.parent = &wdev->dev;
+		tac->misc.mode = 0600;
+		if (!tac->misc.name) {
+			kref_put(&tac->ref, lwmi_tac_free);
+			return -ENOMEM;
+		}
+		ret = misc_register(&tac->misc);
+		if (ret) {
+			kref_put(&tac->ref, lwmi_tac_free);
+			return ret;
+		}
+		/* Unregister before the WMI device and its managed state disappear. 

*/

drivers/platform/x86/lenovo/wmi-other.c +8 -0
+		ret = devm_add_action_or_reset(&wdev->dev, lwmi_tac_remove, tac);
+		if (ret)
+			return ret;
+	}
+	return 0;
+}
+
 /* ======== Self (master: lenovo-wmi-other) ======== */
 
 /**
@@ -1693,6 +2013,7 @@ static int lwmi_other_probe(struct wmi_device 
 *wdev, const void *context)
 {
 	struct component_match *master_match = NULL;
 	struct lwmi_om_priv *priv;
+	int ret;
 
 	priv = devm_kzalloc_obj(&wdev->dev, *priv);
drivers/platform/x86/lenovo/wmi-other.c +4 -0
 	if (!priv)
@@ -1708,6 +2029,10 @@ static int lwmi_other_probe(struct wmi_device 
 *wdev, const void *context)
 	if (IS_ERR(master_match))
 		return PTR_ERR(master_match);
 
+	ret = lwmi_tac_add(wdev);
+	if (ret)
+		return ret;
+
 	return component_master_add_with_match(&wdev->dev, &lwmi_om_master_ops,
 					       master_match);

}

Quoted 3 lines

base-commit: 5680519168f65666d2c03cc6ed8fe2b7052aee12

Signature · 1 line
2.53.0

Are you working within Lenovo? I looked up what this method does and details in the spec I have are very limited, but I was wondering if you're working on one of the new products?

If so, please ping me on my work address. You should be able to look me (Mark Pearson) up - or reach out to the Linux preload team. I think it would be good to discuss what is being done here.

If you're not from within Lenovo please let me know how/why you're using this interface. I would rather go and discuss with the FW team to confirm what is being exposed first, before we enable it blindly.

Thanks
Mark

author
#7 lore raw

Hi Mark,

Quoted 565 lines · Mark Pearson

Hi Alan,

Quoted 556 lines · Alan Pluto

Expose Lenovo Other Mode WMI GetDataByPackage, method 99, through a root-only character device so userspace can access the raw firmware interface. And document the interface.

The upstream driver was build-tested with W=1 and checked with Sparse. A backport using the five-argument wmidev_invoke_method() interface was tested on Ubuntu 7.0.0-42 with Lenovo hardware. The upstream 7.4 driver has not been tested on hardware.

---
 Documentation/ABI/testing/dev-tac             |  49 +++
 .../wmi/devices/lenovo-wmi-other.rst          |   8 +
 MAINTAINERS                                   |   1 +
 drivers/platform/x86/lenovo/wmi-other.c       | 325 ++++++++++++++++++
 4 files changed, 383 insertions(+)
 create mode 100644 Documentation/ABI/testing/dev-tac
Documentation/ABI/testing/dev-tac new file +6 -0
new file mode 100644
index 00000000000..a8be8e95e2d
@@ -0,0 +1,49 @@
+What:           /dev/tac[0-9]*
+Date:           November 2026
+Contact:        [email protected]
+Description:
+                Lenovo Other Mode WMI GetDataByPackage (method 99).
+                Each firmware WMI instance gets a misc character 

device with
+ mode 0600. Numbering follows allocation order, not
necessarily
+ the firmware instance number. Registration does not
probe
+ support for method 99. The parent is the bound WMI
device.

Documentation/ABI/testing/dev-tac new file +2 -0
+
+                Open with O_RDWR. One write submits one complete 

request of
+ 1..100 raw bytes; shorter requests are padded with
zeroes to
+ 100 bytes. Empty and oversized writes fail with EINVAL
without
+ calling firmware. Successful writes return the full
submitted

Documentation/ABI/testing/dev-tac new file +8 -0
+                length and execute two identical WMI calls for the BIOS
+                duplicate-call workaround. Request pairs through this
+                interface are serialized; calls made by other driver
+                interfaces are not covered by this serialization.
+                Evaluation failures abort the pair; the driver does not
+                automatically retry.
+
+                Each open file description owns its response. A write 

while
+ that response remains unread fails with EBUSY,
including after
+ partial reads. A new open cannot read another open's
response.
+ Descriptors shared by dup/fork share the same
transaction;
+ applications must coordinate the complete write/read
sequence.

Documentation/ABI/testing/dev-tac new file +2 -0
+
+                Read returns little-endian u32 DataSize followed by 

exactly
+ DataSize data bytes. Response storage is erased when
fully
+ read or closed. Further reads return EOF until a new
successful
+ write. Before a successful write, or after a failed
firmware
+ evaluation, reads return ENODATA. A zero-length read
returns

Documentation/ABI/testing/dev-tac new file +3 -0
+                zero without consuming data.
+
+                Requests are synchronous even with O_NONBLOCK. Seeking 

and
+ positional I/O are not supported. There is no ioctl
interface.
+ A removed WMI device causes nonempty reads and valid
writes

Documentation/ABI/testing/dev-tac new file +3 -0
+                on existing descriptors to fail with ENODEV.
+
+                Firmware evaluation failures map to EIO. WMI 

marshalling
+ failures return the error reported by the WMI
subsystem.
+ A truncated response or invalid DataSize maps to
EPROTO.
+ Bad userspace memory maps to EFAULT. Failure does not
imply
+ firmware had no side effects; never blindly resubmit
commands.
+ Interrupted waits can return EINTR before firmware
evaluation.

Documentation/ABI/testing/dev-tac new file +2 -0
+
+                The WMI subsystem converts returned ACPI objects to a 

buffer.
+ This raw firmware interface may change device state.
Use only
+ commands whose behavior and side effects are
understood.

Documentation/wmi/devices/lenovo-wmi-other.rst +8 -0
index 011054d64ea..ec85fdeeb7a 100644
@@ -33,6 +33,10 @@ under the following path:
 
 Additionally, this driver also exports attributes to HWMON.
 
+The driver also exposes GetDataByPackage (method 99) through a raw character
+device for each WMI instance. See Documentation/ABI/testing/dev-tac for the
+request and response format and its access restrictions.
+
 LENOVO_CAPABILITY_DATA_00
 -------------------------
 
@@ -109,6 +113,10 @@ WMI interface description
 The WMI interface description can be decoded from the embedded binary 
 MOF (bmof)
 data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility:
 
+The example below describes an older firmware revision and specifies a
+40-byte maximum input for GetDataByPackage. Newer firmware uses a 100-byte
+input, which is the size used by the raw interface described above.
+
 ::
 
   [WMI, Dynamic, Provider("WmiProv"), Locale("MS\\0x409"), 

Description("LENOVO_OTHER_METHOD class"),
guid("{dc2a8805-3a8c-41ba-a6f7-092e0089cd3b}")]

MAINTAINERS +1 -0
index 981eb3fdbef..84b1545f525 100644
@@ -15168,6 +15168,7 @@ M:	Derek J. Clark <[email protected]>
 M:	Rong Zhang <[email protected]>
 L:	[email protected]
 S:	Maintained
+F:	Documentation/ABI/testing/dev-tac
 F:	Documentation/wmi/devices/lenovo-wmi-gamezone.rst
 F:	Documentation/wmi/devices/lenovo-wmi-other.rst
 F:	drivers/platform/x86/lenovo/*
drivers/platform/x86/lenovo/wmi-other.c +46 -0
index bd64e7b4466..c15b3a2a758 100644
@@ -34,16 +34,23 @@
 #include <linux/device.h>
 #include <linux/export.h>
 #include <linux/firmware_attributes.h>
+#include <linux/fs.h>
 #include <linux/gfp_types.h>
 #include <linux/hwmon.h>
 #include <linux/idr.h>
 #include <linux/kdev_t.h>
 #include <linux/kobject.h>
+#include <linux/kref.h>
 #include <linux/limits.h>
+#include <linux/miscdevice.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/platform_profile.h>
 #include <linux/power_supply.h>
+#include <linux/slab.h>
 #include <linux/types.h>
+#include <linux/uaccess.h>
+#include <linux/unaligned.h>
 #include <linux/wmi.h>
 
 #include <acpi/battery.h>
@@ -91,6 +98,9 @@ enum lwmi_feature_id_psu {
 #define LWMI_FEATURE_VALUE_GET 17
 #define LWMI_FEATURE_VALUE_SET 18
 
+#define LWMI_DATA_PACKAGE_GET 99
+#define LWMI_DATA_PACKAGE_INPUT_MAX 100
+
 #define LWMI_FAN_ID_BASE 1
 #define LWMI_FAN_NR 4
 #define LWMI_FAN_ID(x) ((x) + LWMI_FAN_ID_BASE)
@@ -115,6 +125,27 @@ enum lwmi_feature_id_psu {
 
 static DEFINE_IDA(lwmi_om_ida);
 
+static DEFINE_IDA(lwmi_tac_ida);
+static DEFINE_MUTEX(lwmi_tac_lock);
+
+struct lwmi_tac {
+	struct miscdevice misc;
+	struct kref ref;
+	struct wmi_device *wdev;
+	u8 instance;
+	int id;
+};
+
+struct lwmi_tac_file {
+	struct lwmi_tac *tac;
+	/* Protects the per-open response and its position. */
+	struct mutex lock;
+	void *result;
+	size_t result_len;
+	loff_t position;
+	bool completed;
+};
+
 enum attribute_property {
 	DEFAULT_VAL,
 	MAX_VAL,
@@ -1609,6 +1640,295 @@ static void lwmi_om_fw_attr_remove(struct 
 lwmi_om_priv *priv)
 	priv->ida_id = -EIDRM;
 }
 
+/**
+ * lwmi_tac_free() - Free an unregistered endpoint after its last close
+ * @ref: Reference belonging to the endpoint.
+ */
+static void lwmi_tac_free(struct kref *ref)
+{
+	struct lwmi_tac *tac = container_of(ref, struct lwmi_tac, ref);
+
+	ida_free(&lwmi_tac_ida, tac->id);
+	kfree(tac->misc.name);
+	kfree(tac);
+}
+
+/**
+ * lwmi_tac_open() - Allocate independent response state for an open 

file
+ * @inode: Character device inode.
+ * @file: File initially pointing to the registered miscdevice.
+ *
+ * The misc core serializes open against deregistration, so the registration
+ * reference remains valid while the file acquires its own reference.
+ *
+ * Return: 0 on success, or -ENOMEM.
+ */

drivers/platform/x86/lenovo/wmi-other.c +3 -0
+static int lwmi_tac_open(struct inode *inode, struct file *file)
+{
+	struct lwmi_tac *tac = container_of(file->private_data, struct 

lwmi_tac, misc);

drivers/platform/x86/lenovo/wmi-other.c +14 -0
+	struct lwmi_tac_file *state;
+
+	state = kzalloc_obj(*state);
+	if (!state)
+		return -ENOMEM;
+	mutex_init(&state->lock);
+	state->tac = tac;
+	kref_get(&tac->ref);
+	file->private_data = state;
+	return stream_open(inode, file);
+}
+
+/**
+ * lwmi_tac_release() - Erase the response and drop the endpoint 

reference
+ * @inode: Character device inode.
+ * @file: File holding the endpoint state.
+ *
+ * Return: 0.
+ */

drivers/platform/x86/lenovo/wmi-other.c +17 -0
+static int lwmi_tac_release(struct inode *inode, struct file *file)
+{
+	struct lwmi_tac_file *state = file->private_data;
+
+	kfree_sensitive(state->result);
+	mutex_destroy(&state->lock);
+	kref_put(&state->tac->ref, lwmi_tac_free);
+	kfree(state);
+	return 0;
+}
+
+/**
+ * lwmi_tac_invoke() - Execute GetDataByPackage and retain the response
+ * @state: Open file whose response buffer is empty.
+ * @data: Zero-padded 100-byte input buffer.
+ *
+ * The caller holds state->lock and lwmi_tac_lock. The latter 

serializes call
+ * pairs across instances and keeps the WMI device valid during evaluation.
+ *
+ * Return: 0 on success, or a negative error code.
+ */

drivers/platform/x86/lenovo/wmi-other.c +4 -0
+static int lwmi_tac_invoke(struct lwmi_tac_file *state, u8 *data)
+{
+	struct lwmi_tac *tac = state->tac;
+	struct wmi_buffer input = { .length = LWMI_DATA_PACKAGE_INPUT_MAX, 

.data = data };

drivers/platform/x86/lenovo/wmi-other.c +19 -0
+	struct wmi_buffer output;
+	u32 data_size;
+	int ret;
+
+	if (!tac->wdev)
+		return -ENODEV;
+	/*
+	 * Duplicated call required to match BIOS workaround for behavior
+	 * seen when WMI accessed via scripting on other OS.
+	 */
+	for (unsigned int call = 0; call < 2; call++) {
+		ret = wmidev_invoke_method(tac->wdev, tac->instance,
+					   LWMI_DATA_PACKAGE_GET, &input, &output, 0);
+		if (ret)
+			return ret;
+		if (!call)
+			kfree_sensitive(output.data);
+	}
+	/* Validate DataSize before preserving the complete firmware 

response. */

drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	if (output.length < sizeof(__le32)) {
+		ret = -EPROTO;
+		goto free_output;
+	}
+	data_size = get_unaligned_le32(output.data);
+	if (data_size > output.length - sizeof(__le32)) {
+		ret = -EPROTO;
+		goto free_output;
+	}
+	/* Transfer ownership without another allocation after firmware ran. 

*/

drivers/platform/x86/lenovo/wmi-other.c +18 -0
+	state->result = output.data;
+	state->result_len = sizeof(__le32) + data_size;
+	state->position = 0;
+	state->completed = true;
+	return 0;
+free_output:
+	kfree_sensitive(output.data);
+	return ret;
+}
+
+/**
+ * lwmi_tac_write() - Submit one complete TAC request
+ * @file: Open TAC endpoint with independent response state.
+ * @buffer: Userspace input bytes.
+ * @count: Input size, from 1 to 100 bytes.
+ * @ppos: Unused; TAC is a nonseekable request/response stream.
+ *
+ * Short inputs are zero-padded. An unread response must be consumed 

before
+ * submitting another request. A failed firmware call is never retried here.
+ *
+ * Return: @count on success, or a negative error code.
+ */
+static ssize_t lwmi_tac_write(struct file *file, const char __user *buffer,

drivers/platform/x86/lenovo/wmi-other.c +44 -0
+			      size_t count, loff_t *ppos)
+{
+	struct lwmi_tac_file *state = file->private_data;
+	u8 input[LWMI_DATA_PACKAGE_INPUT_MAX] = {};
+	int ret;
+
+	if (!count || count > sizeof(input))
+		return -EINVAL;
+	if (copy_from_user(input, buffer, count)) {
+		ret = -EFAULT;
+		goto clear_input;
+	}
+	ret = mutex_lock_interruptible(&state->lock);
+	if (ret)
+		goto clear_input;
+	ret = mutex_lock_interruptible(&lwmi_tac_lock);
+	if (ret)
+		goto unlock;
+	if (!state->tac->wdev) {
+		ret = -ENODEV;
+	} else if (state->result) {
+		ret = -EBUSY;
+	} else {
+		state->completed = false;
+		ret = lwmi_tac_invoke(state, input);
+	}
+	mutex_unlock(&lwmi_tac_lock);
+unlock:
+	mutex_unlock(&state->lock);
+clear_input:
+	memzero_explicit(input, sizeof(input));
+	if (ret)
+		return ret;
+	return count;
+}
+
+/**
+ * lwmi_tac_read() - Read only this open file's firmware response
+ * @file: Open TAC endpoint.
+ * @buffer: Userspace destination.
+ * @count: Maximum number of bytes to return.
+ * @ppos: Unused; the response position is protected by state->lock.
+ *
+ * Responses contain little-endian u32 DataSize followed by Data. 

Partial
+ * reads advance the response position. Consuming the result erases its
+ * storage immediately; subsequent reads return EOF until another write.
+ *
+ * Return: Bytes read, 0 at EOF or for zero @count, or a negative error code.
+ */

drivers/platform/x86/lenovo/wmi-other.c +49 -0
+static ssize_t lwmi_tac_read(struct file *file, char __user *buffer,
+			     size_t count, loff_t *ppos)
+{
+	struct lwmi_tac_file *state = file->private_data;
+	ssize_t ret;
+
+	if (!count)
+		return 0;
+	ret = mutex_lock_interruptible(&state->lock);
+	if (ret)
+		return ret;
+	ret = mutex_lock_interruptible(&lwmi_tac_lock);
+	if (ret)
+		goto unlock;
+	if (!state->tac->wdev)
+		ret = -ENODEV;
+	mutex_unlock(&lwmi_tac_lock);
+	if (ret) {
+		kfree_sensitive(state->result);
+		state->result = NULL;
+		goto unlock;
+	}
+	if (!state->completed) {
+		ret = -ENODATA;
+		goto unlock;
+	}
+	if (!state->result)
+		goto unlock;
+	ret = simple_read_from_buffer(buffer, count, &state->position,
+				      state->result, state->result_len);
+	if (state->position == (loff_t)state->result_len) {
+		kfree_sensitive(state->result);
+		state->result = NULL;
+	}
+unlock:
+	mutex_unlock(&state->lock);
+	return ret;
+}
+
+static const struct file_operations lwmi_tac_fops = {
+	.owner = THIS_MODULE,
+	.open = lwmi_tac_open,
+	.release = lwmi_tac_release,
+	.read = lwmi_tac_read,
+	.write = lwmi_tac_write,
+};
+
+/**
+ * lwmi_tac_remove() - Unregister an endpoint and block further 

firmware calls
+ * @data: Endpoint registered as a device-managed cleanup action.
+ *
+ * Stop new opens before waiting for an in-flight call pair. Existing files
+ * retain the endpoint state, but their subsequent reads/writes return

drivers/platform/x86/lenovo/wmi-other.c +14 -1
-ENODEV.
+ */
+static void lwmi_tac_remove(void *data)
+{
+	struct lwmi_tac *tac = data;
+
+	misc_deregister(&tac->misc);
+	mutex_lock(&lwmi_tac_lock);
+	tac->wdev = NULL;
+	mutex_unlock(&lwmi_tac_lock);
+	kref_put(&tac->ref, lwmi_tac_free);
+}
+
+/**
+ * lwmi_tac_add() - Register TAC endpoints independently of capability 

data
+ * @wdev: Bound Lenovo Other Mode WMI device.
+ *
+ * No capability describes method 99, so registration does not probe firmware
+ * support. Each firmware instance gets an endpoint; unsupported method calls
+ * are reported to userspace when requested.
+ *
+ * Return: 0 on success, or a negative error code. Device-managed cleanup
+ * unregisters earlier endpoints if a later registration or probe step fails.
+ */

drivers/platform/x86/lenovo/wmi-other.c +35 -0
+static int lwmi_tac_add(struct wmi_device *wdev)
+{
+	unsigned int instances = wmidev_instance_count(wdev);
+	int ret;
+
+	for (unsigned int instance = 0; instance < instances; instance++) {
+		struct lwmi_tac *tac;
+
+		tac = kzalloc_obj(*tac);
+		if (!tac)
+			return -ENOMEM;
+		ret = ida_alloc(&lwmi_tac_ida, GFP_KERNEL);
+		if (ret < 0) {
+			kfree(tac);
+			return ret;
+		}
+		tac->id = ret;
+		kref_init(&tac->ref);
+		tac->wdev = wdev;
+		tac->instance = instance;
+		tac->misc.minor = MISC_DYNAMIC_MINOR;
+		tac->misc.name = kasprintf(GFP_KERNEL, "tac%d", tac->id);
+		tac->misc.fops = &lwmi_tac_fops;
+		tac->misc.parent = &wdev->dev;
+		tac->misc.mode = 0600;
+		if (!tac->misc.name) {
+			kref_put(&tac->ref, lwmi_tac_free);
+			return -ENOMEM;
+		}
+		ret = misc_register(&tac->misc);
+		if (ret) {
+			kref_put(&tac->ref, lwmi_tac_free);
+			return ret;
+		}
+		/* Unregister before the WMI device and its managed state disappear. 

*/

drivers/platform/x86/lenovo/wmi-other.c +8 -0
+		ret = devm_add_action_or_reset(&wdev->dev, lwmi_tac_remove, tac);
+		if (ret)
+			return ret;
+	}
+	return 0;
+}
+
 /* ======== Self (master: lenovo-wmi-other) ======== */
 
 /**
@@ -1693,6 +2013,7 @@ static int lwmi_other_probe(struct wmi_device 
 *wdev, const void *context)
 {
 	struct component_match *master_match = NULL;
 	struct lwmi_om_priv *priv;
+	int ret;
 
 	priv = devm_kzalloc_obj(&wdev->dev, *priv);
drivers/platform/x86/lenovo/wmi-other.c +4 -0
 	if (!priv)
@@ -1708,6 +2029,10 @@ static int lwmi_other_probe(struct wmi_device 
 *wdev, const void *context)
 	if (IS_ERR(master_match))
 		return PTR_ERR(master_match);
 
+	ret = lwmi_tac_add(wdev);
+	if (ret)
+		return ret;
+
 	return component_master_add_with_match(&wdev->dev, &lwmi_om_master_ops,
 					       master_match);

}

Quoted 3 lines

base-commit: 5680519168f65666d2c03cc6ed8fe2b7052aee12

Signature · 1 line
2.53.0

Are you working within Lenovo? I looked up what this method does and details in the spec I have are very limited, but I was wondering if you're working on one of the new products?

Quoted 6 lines

If so, please ping me on my work address. You should be able to look me (Mark Pearson) up - or reach out to the Linux preload team. I think it would be good to discuss what is being done here.

If you're not from within Lenovo please let me know how/why you're using this interface. I would rather go and discuss with the FW team to confirm what is being exposed first, before we enable it blindly.

Thanks
Mark

Thanks for the reply.
I'll reach out to your work address to discuss this further. Once the internal discussion is concluded, I'll come back to this thread and continue the upstream work.

author
#8 lore raw

PATCH v2 1/1 platform/x86: lenovo-wmi-other: Add raw GetDataByPackage interface

In reply to Alan Pluto

Lenovo's userspace utility uses the Other Mode WMI GetDataByPackage method (99) to check device readiness. Expose the method through debugfs so the firmware request and response can be inspected on Linux without adding a stable userspace ABI.

The interface is disabled by default and can be enabled by passing enable_get_data_by_package=1 when loading the module.

---
Changes in v2:
- Replace the character device with a debugfs file, disabled by default.
- Allow one open per WMI device and require a single read for the response.
- Use scoped locks, lockdep checks, automatic buffer cleanup, and the WMI helper's minimum response size check.

 Documentation/wmi/devices/lenovo-wmi-other.rst |  12 +-
 drivers/platform/x86/lenovo/wmi-other.c        | 208 +++++++++++++++++++++++++
 2 files changed, 219 insertions(+), 1 deletion(-)
Documentation/wmi/devices/lenovo-wmi-other.rst +11 -1
index 011054d64eac5b601333373c4d405f9c5117ffc2..42e1525a8852bd7c8ddc04cc4047b1f09c5b770f 100644
@@ -33,6 +33,15 @@ under the following path:
 
 Additionally, this driver also exports attributes to HWMON.
 
+GetDataByPackage diagnostics
+~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+The driver does not create the diagnostic file by default. To enable it,
+pass ``enable_get_data_by_package=1`` when loading ``lenovo-wmi-other``.
+The file is at ``lenovo_wmi/<WMI device>/get_data_by_package``.
+Not all devices support the method. Use only commands whose behavior and
+side effects are understood.
+
 LENOVO_CAPABILITY_DATA_00
 -------------------------
 
@@ -107,7 +116,8 @@ WMI interface description
 =========================
 
 The WMI interface description can be decoded from the embedded binary MOF (bmof)
-data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility:
+data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility. This older
+example lists ``Max(40)`` for GetDataByPackage; newer firmware uses 100 bytes.
 
 ::
 
drivers/platform/x86/lenovo/wmi-other.c +208 -0

272 lines not on this page · raw message

#9 lore raw

Hi Alan,

Quoted 1 line · Alan Pluto

Lenovo's userspace utility uses the Other Mode WMI GetDataByPackage

What's the purpose of the utility? Could you provide some context? You can ignore this question if it's confidential, but in general providing necessary context can help maintainers and reviewers learn about the intent.

Quoted 24 lines

method (99) to check device readiness. Expose the method through debugfs so the firmware request and response can be inspected on Linux without adding a stable userspace ABI.

The interface is disabled by default and can be enabled by passing enable_get_data_by_package=1 when loading the module.

---
Changes in v2:
- Replace the character device with a debugfs file, disabled by default.
- Allow one open per WMI device and require a single read for the response.
- Use scoped locks, lockdep checks, automatic buffer cleanup, and the WMI helper's minimum response size check.

 Documentation/wmi/devices/lenovo-wmi-other.rst |  12 +-
 drivers/platform/x86/lenovo/wmi-other.c        | 208 +++++++++++++++++++++++++
 2 files changed, 219 insertions(+), 1 deletion(-)
Documentation/wmi/devices/lenovo-wmi-other.rst +0 -0
index 011054d64eac5b601333373c4d405f9c5117ffc2..42e1525a8852bd7c8ddc04cc4047b1f09c5b770f 100644

Documentation/wmi/devices/lenovo-wmi-other.rst under the following path: · line 37

Quoted 5 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +2 -0
@@ -33,6 +33,15 @@ under the following path:
 
 Additionally, this driver also exports attributes to HWMON.
 
+GetDataByPackage diagnostics
+~~~~~~~~~~~~~~~~~~~~~~~~~~~

Missing a ~

Quoted 11 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +7 -0
+
+The driver does not create the diagnostic file by default. To enable it,
+pass ``enable_get_data_by_package=1`` when loading ``lenovo-wmi-other``.
+The file is at ``lenovo_wmi/<WMI device>/get_data_by_package``.
+Not all devices support the method. Use only commands whose behavior and
+side effects are understood.
+
 LENOVO_CAPABILITY_DATA_00
 -------------------------
 

Documentation/wmi/devices/lenovo-wmi-other.rst WMI interface description · line 120

Quoted 6 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +2 -1
@@ -107,7 +116,8 @@ WMI interface description
 =========================
 
 The WMI interface description can be decoded from the embedded binary MOF (bmof)
-data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility:
+data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility. This older
+example lists ``Max(40)`` for GetDataByPackage; newer firmware uses 100 bytes.

??? Could you move the sentence to the right place?

Quoted 56 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +0 -0
 ::
 
drivers/platform/x86/lenovo/wmi-other.c +10 -0
index bd64e7b44669db684145298ae088dd2c7f539d29..827b4215c612fbfc052bbadff0672e68e65517f0 100644
@@ -27,13 +27,16 @@
  */
 
 #include <linux/acpi.h>
+#include <linux/atomic.h>
 #include <linux/bitfield.h>
 #include <linux/cleanup.h>
 #include <linux/component.h>
 #include <linux/container_of.h>
+#include <linux/debugfs.h>
 #include <linux/device.h>
 #include <linux/export.h>
 #include <linux/firmware_attributes.h>
+#include <linux/fs.h>
 #include <linux/gfp_types.h>
 #include <linux/hwmon.h>
 #include <linux/idr.h>
@@ -41,9 +44,12 @@
 #include <linux/kobject.h>
 #include <linux/limits.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/platform_profile.h>
 #include <linux/power_supply.h>
+#include <linux/slab.h>
 #include <linux/types.h>
+#include <linux/uaccess.h>
 #include <linux/wmi.h>
 
 #include <acpi/battery.h>
@@ -90,6 +96,8 @@ enum lwmi_feature_id_psu {
 
 #define LWMI_FEATURE_VALUE_GET 17
 #define LWMI_FEATURE_VALUE_SET 18
+#define LWMI_DATA_PACKAGE_GET 99
+#define LWMI_DATA_PACKAGE_INPUT_MAX 100
 
 #define LWMI_FAN_ID_BASE 1
 #define LWMI_FAN_NR 4
@@ -141,6 +149,8 @@ struct lwmi_om_priv {
 	struct device *fw_attr_dev;
 	struct kset *fw_attr_kset;
 	struct wmi_device *wdev;
+	struct dentry *debugfs_dir;
+	atomic_t package_opened;
 	int ida_id;
 
 	struct lwmi_fan_info fan_info[LWMI_FAN_NR];

drivers/platform/x86/lenovo/wmi-other.c struct lwmi_om_priv { · line 172

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
@@ -156,6 +166,23 @@ struct lwmi_om_priv {
 	bool bh_registered;
 };
 
+struct lwmi_package_response {
+	__le32 data_size;
+	u8 data[];
+};

Even though there is no gap between data_size and data, please add a __packed annotation to clearly indicate your intent.

Quoted 17 lines
drivers/platform/x86/lenovo/wmi-other.c +13 -0
+
+struct lwmi_package_file {
+	struct lwmi_om_priv *priv;
+	/* Protects the pending response on shared descriptors. */
+	struct mutex lock;
+	struct lwmi_package_response *response;
+	size_t response_len;
+};
+
+static bool enable_get_data_by_package;
+module_param(enable_get_data_by_package, bool, 0400);
+MODULE_PARM_DESC(enable_get_data_by_package, "Enable the GetDataByPackage debugfs interface");
+
 /*
  * Visibility of fan channels:
  *

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1644

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
@@ -1611,6 +1638,174 @@ static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv)
 
 /* ======== Self (master: lenovo-wmi-other) ======== */
 
+static int lwmi_package_open(struct inode *inode, struct file *file)
+{
+	struct lwmi_om_priv *priv = inode->i_private;
+	struct lwmi_package_file *state __free(kfree) = NULL;

The declarations of __free() pointers should be placed at where it's allocated, i.e., ...

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1649

Quoted 5 lines
drivers/platform/x86/lenovo/wmi-other.c +5 -0
+
+	if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) != (FMODE_READ | FMODE_WRITE))
+		return -EINVAL;
+
+	state = kzalloc_obj(*state);

...here.

Meanwhile, opening the file while priv->package_opened == 1 allocates memory in vain. Could you move it below the package_opened check? In this manner you don't need the __free() either.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1653

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+	if (!state)
+		return -ENOMEM;
+
+	if (atomic_cmpxchg(&priv->package_opened, 0, 1))

atomic_cmpxchg_acquire()

Quoted 5 lines
drivers/platform/x86/lenovo/wmi-other.c +5 -0
+		return -EBUSY;
+
+	mutex_init(&state->lock);
+	state->priv = priv;
+	file->private_data = no_free_ptr(state);

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1668

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+
+	return stream_open(inode, file);
+}
+
+static int lwmi_package_release(struct inode *inode, struct file *file)
+{
+	struct lwmi_package_file *state = file->private_data;
+	struct dentry *dentry = file->f_path.dentry;
+
+	kfree_sensitive(state->response);

You didn't explain what data it is in your replies. "Can contain sensitive data" is not an acceptable explanation.

Please grep the occurrence of kfree_sensitive() in the kernel tree, and you'll find that they are mostly credentials. Does the data passed by the interface fall into the same category?

TBH, anything goes through WMI is *never* secret. If a manufacturer ever decided to use it to deliver "sensitive" data, it either misunderstood the word or chose the wrong interface.

An ACPI-WMI method goes through the ACPICA, then the WMI core, and finally here. The former two's temporary buffers are either on stack or freed by the vanilla kfree(). If leaking "sensitive" response were really a concern, it must had been leaked before it reached your code.

Please don't use kfree_sensitive() unless you really understand the data flow path and have a strong reason to use it. If you insist on it, please answer both questions.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1671

Quoted 3 lines
drivers/platform/x86/lenovo/wmi-other.c +3 -0
+	mutex_destroy(&state->lock);
+
+	/* Unlike read and write, release can run after debugfs removal. */

The comment is not helpful, as it's just repeating well-known knowledge. Please explain what it protects:

The release() callback may be called after state->priv has been freed. Only when the debugfs file hasn't been removed is the pointer valid.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1673

Quoted 2 lines
drivers/platform/x86/lenovo/wmi-other.c +2 -0
+	if (!debugfs_file_get(dentry)) {
+		atomic_set(&state->priv->package_opened, 0);

atomic_set_release()

Quoted 24 lines
drivers/platform/x86/lenovo/wmi-other.c +24 -0
+		debugfs_file_put(dentry);
+	}
+
+	kfree(state);
+	return 0;
+}
+
+static int lwmi_package_invoke(struct lwmi_package_file *state, u8 *data)
+{
+	struct lwmi_om_priv *priv = state->priv;
+	struct wmi_buffer input = { .length = LWMI_DATA_PACKAGE_INPUT_MAX, .data = data };
+	struct wmi_buffer output;
+	u32 data_size;
+	int ret;
+
+	lockdep_assert_held(&state->lock);
+
+	/* The tested firmware needs two calls before its response is usable. */
+	ret = wmidev_invoke_method(priv->wdev, 0, LWMI_DATA_PACKAGE_GET,
+				   &input, &output, 0);
+	if (ret)
+		return ret;
+
+	kfree_sensitive(output.data);

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1707

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+
+	ret = wmidev_invoke_method(priv->wdev, 0, LWMI_DATA_PACKAGE_GET,
+				   &input, &output, sizeof(struct lwmi_package_response));
+	if (ret)
+		return ret;
+
+	struct lwmi_package_response *response __free(kfree_sensitive) = output.data;
+
+	data_size = le32_to_cpu(response->data_size);
+	if (data_size > output.length - sizeof(*response))

Why not use struct_size() here? Considering that...

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1710

Quoted 3 lines
drivers/platform/x86/lenovo/wmi-other.c +3 -0
+		return -EPROTO;
+
+	state->response_len = struct_size(response, data, data_size);

...you've used it here, you may introduce a on-stack variable to temporarily store the calculated size and use it here and there.

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +7 -0
+	state->response = no_free_ptr(response);
+	return 0;
+}
+
+static ssize_t lwmi_package_write(struct file *file, const char __user *buffer,
+				  size_t count, loff_t *ppos)
+{

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1727

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	struct lwmi_package_file *state = file->private_data;
+	u8 input[LWMI_DATA_PACKAGE_INPUT_MAX] = {};
+	int ret;
+
+	if (!count || count > sizeof(input))
+		return -EINVAL;
+
+	ACQUIRE(mutex_try, state_guard)(&state->lock);
+	if (ACQUIRE_ERR(mutex_try, &state_guard) || state->response)
+		return -EBUSY;

I don't think it's a good idea to share the same errno between the two cases.

While ACQUIRE_ERR(mutex_try, &state_guard) should return -EBUSY, a pending state->response should return something like -EALREADY or -ENOTEMPTY.

Quoted 16 lines
drivers/platform/x86/lenovo/wmi-other.c +16 -0
+
+	if (copy_from_user(input, buffer, count))
+		return -EFAULT;
+
+	ret = lwmi_package_invoke(state, input);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+static ssize_t lwmi_package_read(struct file *file, char __user *buffer,
+				 size_t count, loff_t *ppos)
+{
+	struct lwmi_package_file *state = file->private_data;
+	size_t len;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1753

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+
+	if (!count)
+		return 0;
+
+	ACQUIRE(mutex_try, state_guard)(&state->lock);
+	if (ACQUIRE_ERR(mutex_try, &state_guard))
+		return -EBUSY;
+
+	if (!state->response)
+		return 0;

Reading the file without writing to it in prior is a mistake, isn't it? Thus:

		return -ENODATA;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1757

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+
+	len = state->response_len;
+	if (len > count)
+		return -ENOSPC;

Returning -ENOSPC is confusing as there is nothing to allocate during read. How about -ENOBUFS?

Quoted 26 lines
drivers/platform/x86/lenovo/wmi-other.c +26 -0
+
+	if (copy_to_user(buffer, state->response, len))
+		return -EFAULT;
+
+	kfree_sensitive(state->response);
+	state->response = NULL;
+	return len;
+}
+
+static const struct file_operations lwmi_package_fops = {
+	.owner = THIS_MODULE,
+	.open = lwmi_package_open,
+	.release = lwmi_package_release,
+	.read = lwmi_package_read,
+	.write = lwmi_package_write,
+};
+
+static void lwmi_package_debugfs_remove(void *data)
+{
+	debugfs_remove_recursive(data);
+}
+
+static int devm_lwmi_package_debugfs_add(struct lwmi_om_priv *priv)
+{
+	struct device *dev = &priv->wdev->dev;
+	struct dentry *dir, *entry;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1793

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	int ret;
+
+	if (!wmidev_instance_count(priv->wdev))
+		return 0;
+
+	dir = lwmi_debugfs_create_dir(priv->wdev);
+	if (IS_ERR(dir))
+		return PTR_ERR(dir);
+
+	atomic_set(&priv->package_opened, 0);

Why would you need this? The struct is kzalloc()-allocated.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1805

Quoted 12 lines
drivers/platform/x86/lenovo/wmi-other.c +12 -0
+
+	ret = devm_add_action_or_reset(dev, lwmi_package_debugfs_remove, dir);
+	if (ret)
+		return ret;
+
+	entry = debugfs_create_file("get_data_by_package", 0600, dir, priv, &lwmi_package_fops);
+	if (IS_ERR(entry)) {
+		devm_release_action(dev, lwmi_package_debugfs_remove, dir);
+		return PTR_ERR(entry);
+	}
+
+	priv->debugfs_dir = dir;

Why would you need this? The dir is devm-managed.

Quoted 15 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+	return 0;
+}
+
 /**
  * lwmi_om_master_bind() - Bind all components of the other mode driver
  * @dev: The lenovo-wmi-other driver basic device.
@@ -1693,6 +1888,7 @@ static int lwmi_other_probe(struct wmi_device *wdev, const void *context)
 {
 	struct component_match *master_match = NULL;
 	struct lwmi_om_priv *priv;
+	int ret;
 
 	priv = devm_kzalloc_obj(&wdev->dev, *priv);
 	if (!priv)

drivers/platform/x86/lenovo/wmi-other.c static int lwmi_other_probe(struct wmi_device *wdev, const void *context) · line 1910

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
@@ -1708,12 +1904,24 @@ static int lwmi_other_probe(struct wmi_device *wdev, const void *context)
 	if (IS_ERR(master_match))
 		return PTR_ERR(master_match);
 
+	if (enable_get_data_by_package) {
+		ret = devm_lwmi_package_debugfs_add(priv);
+		if (ret)
+			dev_warn(&wdev->dev, "failed to create GetDataByPackage debugfs files: %d\n",

The file is named get_data_by_package.

drivers/platform/x86/lenovo/wmi-other.c static int lwmi_other_probe(struct wmi_device *wdev, const void *context) · line 1923

Quoted 13 lines
drivers/platform/x86/lenovo/wmi-other.c +7 -0
+				 ret);
+	}
+
 	return component_master_add_with_match(&wdev->dev, &lwmi_om_master_ops,
 					       master_match);
 }
 
 static void lwmi_other_remove(struct wmi_device *wdev)
 {
+	struct lwmi_om_priv *priv = dev_get_drvdata(&wdev->dev);
+
+	if (priv->debugfs_dir)
+		devm_release_action(&wdev->dev, lwmi_package_debugfs_remove, priv->debugfs_dir);

Why would you need this? The interface does not depend on the components.

Thanks,
Rong

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +1 -0
+
 	component_master_del(&wdev->dev, &lwmi_om_master_ops);
 }
 
maintainer
#10 lore raw
Quoted 292 lines · Rong Zhang

Hi Alan,

Quoted 1 line · Alan Pluto

Lenovo's userspace utility uses the Other Mode WMI GetDataByPackage

What's the purpose of the utility? Could you provide some context? You can ignore this question if it's confidential, but in general providing necessary context can help maintainers and reviewers learn about the intent.

Quoted 24 lines

method (99) to check device readiness. Expose the method through debugfs so the firmware request and response can be inspected on Linux without adding a stable userspace ABI.

The interface is disabled by default and can be enabled by passing enable_get_data_by_package=1 when loading the module.

---
Changes in v2:
- Replace the character device with a debugfs file, disabled by default.
- Allow one open per WMI device and require a single read for the response.
- Use scoped locks, lockdep checks, automatic buffer cleanup, and the WMI helper's minimum response size check.

 Documentation/wmi/devices/lenovo-wmi-other.rst |  12 +-
 drivers/platform/x86/lenovo/wmi-other.c        | 208 +++++++++++++++++++++++++
 2 files changed, 219 insertions(+), 1 deletion(-)
Documentation/wmi/devices/lenovo-wmi-other.rst +0 -0
index 011054d64eac5b601333373c4d405f9c5117ffc2..42e1525a8852bd7c8ddc04cc4047b1f09c5b770f 100644

Documentation/wmi/devices/lenovo-wmi-other.rst under the following path: · line 37

Quoted 5 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +2 -0
@@ -33,6 +33,15 @@ under the following path:
 
 Additionally, this driver also exports attributes to HWMON.
 
+GetDataByPackage diagnostics
+~~~~~~~~~~~~~~~~~~~~~~~~~~~

Missing a ~

Quoted 11 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +7 -0
+
+The driver does not create the diagnostic file by default. To enable it,
+pass ``enable_get_data_by_package=1`` when loading ``lenovo-wmi-other``.
+The file is at ``lenovo_wmi/<WMI device>/get_data_by_package``.
+Not all devices support the method. Use only commands whose behavior and
+side effects are understood.
+
 LENOVO_CAPABILITY_DATA_00
 -------------------------
 

Documentation/wmi/devices/lenovo-wmi-other.rst WMI interface description · line 120

Quoted 6 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +2 -1
@@ -107,7 +116,8 @@ WMI interface description
 =========================
 
 The WMI interface description can be decoded from the embedded binary MOF (bmof)
-data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility:
+data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility. This older
+example lists ``Max(40)`` for GetDataByPackage; newer firmware uses 100 bytes.

??? Could you move the sentence to the right place?

Quoted 56 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +0 -0
 ::
 
drivers/platform/x86/lenovo/wmi-other.c +10 -0
index bd64e7b44669db684145298ae088dd2c7f539d29..827b4215c612fbfc052bbadff0672e68e65517f0 100644
@@ -27,13 +27,16 @@
  */
 
 #include <linux/acpi.h>
+#include <linux/atomic.h>
 #include <linux/bitfield.h>
 #include <linux/cleanup.h>
 #include <linux/component.h>
 #include <linux/container_of.h>
+#include <linux/debugfs.h>
 #include <linux/device.h>
 #include <linux/export.h>
 #include <linux/firmware_attributes.h>
+#include <linux/fs.h>
 #include <linux/gfp_types.h>
 #include <linux/hwmon.h>
 #include <linux/idr.h>
@@ -41,9 +44,12 @@
 #include <linux/kobject.h>
 #include <linux/limits.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/platform_profile.h>
 #include <linux/power_supply.h>
+#include <linux/slab.h>
 #include <linux/types.h>
+#include <linux/uaccess.h>
 #include <linux/wmi.h>
 
 #include <acpi/battery.h>
@@ -90,6 +96,8 @@ enum lwmi_feature_id_psu {
 
 #define LWMI_FEATURE_VALUE_GET 17
 #define LWMI_FEATURE_VALUE_SET 18
+#define LWMI_DATA_PACKAGE_GET 99
+#define LWMI_DATA_PACKAGE_INPUT_MAX 100
 
 #define LWMI_FAN_ID_BASE 1
 #define LWMI_FAN_NR 4
@@ -141,6 +149,8 @@ struct lwmi_om_priv {
 	struct device *fw_attr_dev;
 	struct kset *fw_attr_kset;
 	struct wmi_device *wdev;
+	struct dentry *debugfs_dir;
+	atomic_t package_opened;
 	int ida_id;
 
 	struct lwmi_fan_info fan_info[LWMI_FAN_NR];

drivers/platform/x86/lenovo/wmi-other.c struct lwmi_om_priv { · line 172

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
@@ -156,6 +166,23 @@ struct lwmi_om_priv {
 	bool bh_registered;
 };
 
+struct lwmi_package_response {
+	__le32 data_size;
+	u8 data[];
+};

Even though there is no gap between data_size and data, please add a __packed annotation to clearly indicate your intent.

Quoted 17 lines
drivers/platform/x86/lenovo/wmi-other.c +13 -0
+
+struct lwmi_package_file {
+	struct lwmi_om_priv *priv;
+	/* Protects the pending response on shared descriptors. */
+	struct mutex lock;
+	struct lwmi_package_response *response;
+	size_t response_len;
+};
+
+static bool enable_get_data_by_package;
+module_param(enable_get_data_by_package, bool, 0400);
+MODULE_PARM_DESC(enable_get_data_by_package, "Enable the GetDataByPackage debugfs interface");
+
 /*
  * Visibility of fan channels:
  *

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1644

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
@@ -1611,6 +1638,174 @@ static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv)
 
 /* ======== Self (master: lenovo-wmi-other) ======== */
 
+static int lwmi_package_open(struct inode *inode, struct file *file)
+{
+	struct lwmi_om_priv *priv = inode->i_private;
+	struct lwmi_package_file *state __free(kfree) = NULL;

The declarations of __free() pointers should be placed at where it's allocated, i.e., ...

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1649

Quoted 5 lines
drivers/platform/x86/lenovo/wmi-other.c +5 -0
+
+	if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) != (FMODE_READ | FMODE_WRITE))
+		return -EINVAL;
+
+	state = kzalloc_obj(*state);

...here.

Meanwhile, opening the file while priv->package_opened == 1 allocates memory in vain. Could you move it below the package_opened check? In this manner you don't need the __free() either.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1653

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+	if (!state)
+		return -ENOMEM;
+
+	if (atomic_cmpxchg(&priv->package_opened, 0, 1))

atomic_cmpxchg_acquire()

Quoted 5 lines
drivers/platform/x86/lenovo/wmi-other.c +5 -0
+		return -EBUSY;
+
+	mutex_init(&state->lock);
+	state->priv = priv;
+	file->private_data = no_free_ptr(state);

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1668

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+
+	return stream_open(inode, file);
+}
+
+static int lwmi_package_release(struct inode *inode, struct file *file)
+{
+	struct lwmi_package_file *state = file->private_data;
+	struct dentry *dentry = file->f_path.dentry;
+
+	kfree_sensitive(state->response);

You didn't explain what data it is in your replies. "Can contain sensitive data" is not an acceptable explanation.

Please grep the occurrence of kfree_sensitive() in the kernel tree, and you'll find that they are mostly credentials. Does the data passed by the interface fall into the same category?

TBH, anything goes through WMI is *never* secret. If a manufacturer ever decided to use it to deliver "sensitive" data, it either misunderstood the word or chose the wrong interface.

An ACPI-WMI method goes through the ACPICA, then the WMI core, and finally here. The former two's temporary buffers are either on stack or freed by the vanilla kfree(). If leaking "sensitive" response were really a concern, it must had been leaked before it reached your code.

Please don't use kfree_sensitive() unless you really understand the data flow path and have a strong reason to use it. If you insist on it, please answer both questions.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1671

Quoted 3 lines
drivers/platform/x86/lenovo/wmi-other.c +3 -0
+	mutex_destroy(&state->lock);
+
+	/* Unlike read and write, release can run after debugfs removal. */

The comment is not helpful, as it's just repeating well-known knowledge. Please explain what it protects:

The release() callback may be called after state->priv has been freed. Only when the debugfs file hasn't been removed is the pointer valid.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1673

Quoted 2 lines
drivers/platform/x86/lenovo/wmi-other.c +2 -0
+	if (!debugfs_file_get(dentry)) {
+		atomic_set(&state->priv->package_opened, 0);

atomic_set_release()

Quoted 24 lines
drivers/platform/x86/lenovo/wmi-other.c +24 -0
+		debugfs_file_put(dentry);
+	}
+
+	kfree(state);
+	return 0;
+}
+
+static int lwmi_package_invoke(struct lwmi_package_file *state, u8 *data)
+{
+	struct lwmi_om_priv *priv = state->priv;
+	struct wmi_buffer input = { .length = LWMI_DATA_PACKAGE_INPUT_MAX, .data = data };
+	struct wmi_buffer output;
+	u32 data_size;
+	int ret;
+
+	lockdep_assert_held(&state->lock);
+
+	/* The tested firmware needs two calls before its response is usable. */
+	ret = wmidev_invoke_method(priv->wdev, 0, LWMI_DATA_PACKAGE_GET,
+				   &input, &output, 0);
+	if (ret)
+		return ret;
+
+	kfree_sensitive(output.data);

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1707

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+
+	ret = wmidev_invoke_method(priv->wdev, 0, LWMI_DATA_PACKAGE_GET,
+				   &input, &output, sizeof(struct lwmi_package_response));
+	if (ret)
+		return ret;
+
+	struct lwmi_package_response *response __free(kfree_sensitive) = output.data;
+
+	data_size = le32_to_cpu(response->data_size);
+	if (data_size > output.length - sizeof(*response))

Why not use struct_size() here? Considering that...

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1710

Quoted 3 lines
drivers/platform/x86/lenovo/wmi-other.c +3 -0
+		return -EPROTO;
+
+	state->response_len = struct_size(response, data, data_size);

...you've used it here, you may introduce a on-stack variable to temporarily store the calculated size and use it here and there.

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +7 -0
+	state->response = no_free_ptr(response);
+	return 0;
+}
+
+static ssize_t lwmi_package_write(struct file *file, const char __user *buffer,
+				  size_t count, loff_t *ppos)
+{

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1727

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	struct lwmi_package_file *state = file->private_data;
+	u8 input[LWMI_DATA_PACKAGE_INPUT_MAX] = {};
+	int ret;
+
+	if (!count || count > sizeof(input))
+		return -EINVAL;
+
+	ACQUIRE(mutex_try, state_guard)(&state->lock);
+	if (ACQUIRE_ERR(mutex_try, &state_guard) || state->response)
+		return -EBUSY;

I don't think it's a good idea to share the same errno between the two cases.

Quoted 3 lines

While ACQUIRE_ERR(mutex_try, &state_guard) should return -EBUSY, a pending state->response should return something like -EALREADY or -ENOTEMPTY.

Generally, one should be mostly be returning the error from ACQUIRE_ERR() for that case.

	ret = ACQUIRE_ERR(..);
	if (ret)
		return ret;
Signature · 1 line
 i.
author
#11 lore raw
Quoted 298 lines · Ilpo Järvinen
Quoted 292 lines · Rong Zhang

Hi Alan,

Quoted 1 line · Alan Pluto

Lenovo's userspace utility uses the Other Mode WMI GetDataByPackage

What's the purpose of the utility? Could you provide some context? You can ignore this question if it's confidential, but in general providing necessary context can help maintainers and reviewers learn about the intent.

Quoted 24 lines

method (99) to check device readiness. Expose the method through debugfs so the firmware request and response can be inspected on Linux without adding a stable userspace ABI.

The interface is disabled by default and can be enabled by passing enable_get_data_by_package=1 when loading the module.

---
Changes in v2:
- Replace the character device with a debugfs file, disabled by default.
- Allow one open per WMI device and require a single read for the response.
- Use scoped locks, lockdep checks, automatic buffer cleanup, and the WMI helper's minimum response size check.

 Documentation/wmi/devices/lenovo-wmi-other.rst |  12 +-
 drivers/platform/x86/lenovo/wmi-other.c        | 208 +++++++++++++++++++++++++
 2 files changed, 219 insertions(+), 1 deletion(-)
Documentation/wmi/devices/lenovo-wmi-other.rst +0 -0
index 011054d64eac5b601333373c4d405f9c5117ffc2..42e1525a8852bd7c8ddc04cc4047b1f09c5b770f 100644

Documentation/wmi/devices/lenovo-wmi-other.rst under the following path: · line 37

Quoted 5 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +2 -0
@@ -33,6 +33,15 @@ under the following path:
 
 Additionally, this driver also exports attributes to HWMON.
 
+GetDataByPackage diagnostics
+~~~~~~~~~~~~~~~~~~~~~~~~~~~

Missing a ~

Quoted 11 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +7 -0
+
+The driver does not create the diagnostic file by default. To enable it,
+pass ``enable_get_data_by_package=1`` when loading ``lenovo-wmi-other``.
+The file is at ``lenovo_wmi/<WMI device>/get_data_by_package``.
+Not all devices support the method. Use only commands whose behavior and
+side effects are understood.
+
 LENOVO_CAPABILITY_DATA_00
 -------------------------
 

Documentation/wmi/devices/lenovo-wmi-other.rst WMI interface description · line 120

Quoted 6 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +2 -1
@@ -107,7 +116,8 @@ WMI interface description
 =========================
 
 The WMI interface description can be decoded from the embedded binary MOF (bmof)
-data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility:
+data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility. This older
+example lists ``Max(40)`` for GetDataByPackage; newer firmware uses 100 bytes.

??? Could you move the sentence to the right place?

Quoted 56 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +0 -0
 ::
 
drivers/platform/x86/lenovo/wmi-other.c +10 -0
index bd64e7b44669db684145298ae088dd2c7f539d29..827b4215c612fbfc052bbadff0672e68e65517f0 100644
@@ -27,13 +27,16 @@
  */
 
 #include <linux/acpi.h>
+#include <linux/atomic.h>
 #include <linux/bitfield.h>
 #include <linux/cleanup.h>
 #include <linux/component.h>
 #include <linux/container_of.h>
+#include <linux/debugfs.h>
 #include <linux/device.h>
 #include <linux/export.h>
 #include <linux/firmware_attributes.h>
+#include <linux/fs.h>
 #include <linux/gfp_types.h>
 #include <linux/hwmon.h>
 #include <linux/idr.h>
@@ -41,9 +44,12 @@
 #include <linux/kobject.h>
 #include <linux/limits.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/platform_profile.h>
 #include <linux/power_supply.h>
+#include <linux/slab.h>
 #include <linux/types.h>
+#include <linux/uaccess.h>
 #include <linux/wmi.h>
 
 #include <acpi/battery.h>
@@ -90,6 +96,8 @@ enum lwmi_feature_id_psu {
 
 #define LWMI_FEATURE_VALUE_GET 17
 #define LWMI_FEATURE_VALUE_SET 18
+#define LWMI_DATA_PACKAGE_GET 99
+#define LWMI_DATA_PACKAGE_INPUT_MAX 100
 
 #define LWMI_FAN_ID_BASE 1
 #define LWMI_FAN_NR 4
@@ -141,6 +149,8 @@ struct lwmi_om_priv {
 	struct device *fw_attr_dev;
 	struct kset *fw_attr_kset;
 	struct wmi_device *wdev;
+	struct dentry *debugfs_dir;
+	atomic_t package_opened;
 	int ida_id;
 
 	struct lwmi_fan_info fan_info[LWMI_FAN_NR];

drivers/platform/x86/lenovo/wmi-other.c struct lwmi_om_priv { · line 172

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
@@ -156,6 +166,23 @@ struct lwmi_om_priv {
 	bool bh_registered;
 };
 
+struct lwmi_package_response {
+	__le32 data_size;
+	u8 data[];
+};

Even though there is no gap between data_size and data, please add a __packed annotation to clearly indicate your intent.

Quoted 17 lines
drivers/platform/x86/lenovo/wmi-other.c +13 -0
+
+struct lwmi_package_file {
+	struct lwmi_om_priv *priv;
+	/* Protects the pending response on shared descriptors. */
+	struct mutex lock;
+	struct lwmi_package_response *response;
+	size_t response_len;
+};
+
+static bool enable_get_data_by_package;
+module_param(enable_get_data_by_package, bool, 0400);
+MODULE_PARM_DESC(enable_get_data_by_package, "Enable the GetDataByPackage debugfs interface");
+
 /*
  * Visibility of fan channels:
  *

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1644

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
@@ -1611,6 +1638,174 @@ static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv)
 
 /* ======== Self (master: lenovo-wmi-other) ======== */
 
+static int lwmi_package_open(struct inode *inode, struct file *file)
+{
+	struct lwmi_om_priv *priv = inode->i_private;
+	struct lwmi_package_file *state __free(kfree) = NULL;

The declarations of __free() pointers should be placed at where it's allocated, i.e., ...

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1649

Quoted 5 lines
drivers/platform/x86/lenovo/wmi-other.c +5 -0
+
+	if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) != (FMODE_READ | FMODE_WRITE))
+		return -EINVAL;
+
+	state = kzalloc_obj(*state);

...here.

Meanwhile, opening the file while priv->package_opened == 1 allocates memory in vain. Could you move it below the package_opened check? In this manner you don't need the __free() either.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1653

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+	if (!state)
+		return -ENOMEM;
+
+	if (atomic_cmpxchg(&priv->package_opened, 0, 1))

atomic_cmpxchg_acquire()

Quoted 5 lines
drivers/platform/x86/lenovo/wmi-other.c +5 -0
+		return -EBUSY;
+
+	mutex_init(&state->lock);
+	state->priv = priv;
+	file->private_data = no_free_ptr(state);

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1668

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+
+	return stream_open(inode, file);
+}
+
+static int lwmi_package_release(struct inode *inode, struct file *file)
+{
+	struct lwmi_package_file *state = file->private_data;
+	struct dentry *dentry = file->f_path.dentry;
+
+	kfree_sensitive(state->response);

You didn't explain what data it is in your replies. "Can contain sensitive data" is not an acceptable explanation.

Please grep the occurrence of kfree_sensitive() in the kernel tree, and you'll find that they are mostly credentials. Does the data passed by the interface fall into the same category?

TBH, anything goes through WMI is *never* secret. If a manufacturer ever decided to use it to deliver "sensitive" data, it either misunderstood the word or chose the wrong interface.

An ACPI-WMI method goes through the ACPICA, then the WMI core, and finally here. The former two's temporary buffers are either on stack or freed by the vanilla kfree(). If leaking "sensitive" response were really a concern, it must had been leaked before it reached your code.

Please don't use kfree_sensitive() unless you really understand the data flow path and have a strong reason to use it. If you insist on it, please answer both questions.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1671

Quoted 3 lines
drivers/platform/x86/lenovo/wmi-other.c +3 -0
+	mutex_destroy(&state->lock);
+
+	/* Unlike read and write, release can run after debugfs removal. */

The comment is not helpful, as it's just repeating well-known knowledge. Please explain what it protects:

The release() callback may be called after state->priv has been freed. Only when the debugfs file hasn't been removed is the pointer valid.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1673

Quoted 2 lines
drivers/platform/x86/lenovo/wmi-other.c +2 -0
+	if (!debugfs_file_get(dentry)) {
+		atomic_set(&state->priv->package_opened, 0);

atomic_set_release()

Quoted 24 lines
drivers/platform/x86/lenovo/wmi-other.c +24 -0
+		debugfs_file_put(dentry);
+	}
+
+	kfree(state);
+	return 0;
+}
+
+static int lwmi_package_invoke(struct lwmi_package_file *state, u8 *data)
+{
+	struct lwmi_om_priv *priv = state->priv;
+	struct wmi_buffer input = { .length = LWMI_DATA_PACKAGE_INPUT_MAX, .data = data };
+	struct wmi_buffer output;
+	u32 data_size;
+	int ret;
+
+	lockdep_assert_held(&state->lock);
+
+	/* The tested firmware needs two calls before its response is usable. */
+	ret = wmidev_invoke_method(priv->wdev, 0, LWMI_DATA_PACKAGE_GET,
+				   &input, &output, 0);
+	if (ret)
+		return ret;
+
+	kfree_sensitive(output.data);

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1707

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+
+	ret = wmidev_invoke_method(priv->wdev, 0, LWMI_DATA_PACKAGE_GET,
+				   &input, &output, sizeof(struct lwmi_package_response));
+	if (ret)
+		return ret;
+
+	struct lwmi_package_response *response __free(kfree_sensitive) = output.data;
+
+	data_size = le32_to_cpu(response->data_size);
+	if (data_size > output.length - sizeof(*response))

Why not use struct_size() here? Considering that...

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1710

Quoted 3 lines
drivers/platform/x86/lenovo/wmi-other.c +3 -0
+		return -EPROTO;
+
+	state->response_len = struct_size(response, data, data_size);

...you've used it here, you may introduce a on-stack variable to temporarily store the calculated size and use it here and there.

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +7 -0
+	state->response = no_free_ptr(response);
+	return 0;
+}
+
+static ssize_t lwmi_package_write(struct file *file, const char __user *buffer,
+				  size_t count, loff_t *ppos)
+{

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1727

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	struct lwmi_package_file *state = file->private_data;
+	u8 input[LWMI_DATA_PACKAGE_INPUT_MAX] = {};
+	int ret;
+
+	if (!count || count > sizeof(input))
+		return -EINVAL;
+
+	ACQUIRE(mutex_try, state_guard)(&state->lock);
+	if (ACQUIRE_ERR(mutex_try, &state_guard) || state->response)
+		return -EBUSY;

I don't think it's a good idea to share the same errno between the two cases.

Quoted 3 lines

While ACQUIRE_ERR(mutex_try, &state_guard) should return -EBUSY, a pending state->response should return something like -EALREADY or -ENOTEMPTY.

Quoted 7 lines

Generally, one should be mostly be returning the error from ACQUIRE_ERR() for that case.

	ret = ACQUIRE_ERR(..);
	if (ret)
		return ret;

I'll split the two cases: return the error from ACQUIRE_ERR() directly, and return a separate -EALREADY for the pending state->response case. Thanks!

#12 lore raw
In reply to Rong Zhang

Hi Alan,

Quoted 241 lines · Rong Zhang

Hi Alan,

Quoted 1 line · Alan Pluto

Lenovo's userspace utility uses the Other Mode WMI GetDataByPackage

What's the purpose of the utility? Could you provide some context? You can ignore this question if it's confidential, but in general providing necessary context can help maintainers and reviewers learn about the intent.

Quoted 24 lines

method (99) to check device readiness. Expose the method through debugfs so the firmware request and response can be inspected on Linux without adding a stable userspace ABI.

The interface is disabled by default and can be enabled by passing enable_get_data_by_package=1 when loading the module.

---
Changes in v2:
- Replace the character device with a debugfs file, disabled by default.
- Allow one open per WMI device and require a single read for the response.
- Use scoped locks, lockdep checks, automatic buffer cleanup, and the WMI helper's minimum response size check.

 Documentation/wmi/devices/lenovo-wmi-other.rst |  12 +-
 drivers/platform/x86/lenovo/wmi-other.c        | 208 +++++++++++++++++++++++++
 2 files changed, 219 insertions(+), 1 deletion(-)
Documentation/wmi/devices/lenovo-wmi-other.rst +0 -0
index 011054d64eac5b601333373c4d405f9c5117ffc2..42e1525a8852bd7c8ddc04cc4047b1f09c5b770f 100644

Documentation/wmi/devices/lenovo-wmi-other.rst under the following path: · line 37

Quoted 5 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +2 -0
@@ -33,6 +33,15 @@ under the following path:
 
 Additionally, this driver also exports attributes to HWMON.
 
+GetDataByPackage diagnostics
+~~~~~~~~~~~~~~~~~~~~~~~~~~~

Missing a ~

Quoted 11 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +7 -0
+
+The driver does not create the diagnostic file by default. To enable it,
+pass ``enable_get_data_by_package=1`` when loading ``lenovo-wmi-other``.
+The file is at ``lenovo_wmi/<WMI device>/get_data_by_package``.
+Not all devices support the method. Use only commands whose behavior and
+side effects are understood.
+
 LENOVO_CAPABILITY_DATA_00
 -------------------------
 

Documentation/wmi/devices/lenovo-wmi-other.rst WMI interface description · line 120

Quoted 6 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +2 -1
@@ -107,7 +116,8 @@ WMI interface description
 =========================
 
 The WMI interface description can be decoded from the embedded binary MOF (bmof)
-data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility:
+data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility. This older
+example lists ``Max(40)`` for GetDataByPackage; newer firmware uses 100 bytes.

??? Could you move the sentence to the right place?

Quoted 56 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +0 -0
 ::
 
drivers/platform/x86/lenovo/wmi-other.c +10 -0
index bd64e7b44669db684145298ae088dd2c7f539d29..827b4215c612fbfc052bbadff0672e68e65517f0 100644
@@ -27,13 +27,16 @@
  */
 
 #include <linux/acpi.h>
+#include <linux/atomic.h>
 #include <linux/bitfield.h>
 #include <linux/cleanup.h>
 #include <linux/component.h>
 #include <linux/container_of.h>
+#include <linux/debugfs.h>
 #include <linux/device.h>
 #include <linux/export.h>
 #include <linux/firmware_attributes.h>
+#include <linux/fs.h>
 #include <linux/gfp_types.h>
 #include <linux/hwmon.h>
 #include <linux/idr.h>
@@ -41,9 +44,12 @@
 #include <linux/kobject.h>
 #include <linux/limits.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/platform_profile.h>
 #include <linux/power_supply.h>
+#include <linux/slab.h>
 #include <linux/types.h>
+#include <linux/uaccess.h>
 #include <linux/wmi.h>
 
 #include <acpi/battery.h>
@@ -90,6 +96,8 @@ enum lwmi_feature_id_psu {
 
 #define LWMI_FEATURE_VALUE_GET 17
 #define LWMI_FEATURE_VALUE_SET 18
+#define LWMI_DATA_PACKAGE_GET 99
+#define LWMI_DATA_PACKAGE_INPUT_MAX 100
 
 #define LWMI_FAN_ID_BASE 1
 #define LWMI_FAN_NR 4
@@ -141,6 +149,8 @@ struct lwmi_om_priv {
 	struct device *fw_attr_dev;
 	struct kset *fw_attr_kset;
 	struct wmi_device *wdev;
+	struct dentry *debugfs_dir;
+	atomic_t package_opened;
 	int ida_id;
 
 	struct lwmi_fan_info fan_info[LWMI_FAN_NR];

drivers/platform/x86/lenovo/wmi-other.c struct lwmi_om_priv { · line 172

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
@@ -156,6 +166,23 @@ struct lwmi_om_priv {
 	bool bh_registered;
 };
 
+struct lwmi_package_response {
+	__le32 data_size;
+	u8 data[];
+};

Even though there is no gap between data_size and data, please add a __packed annotation to clearly indicate your intent.

Quoted 17 lines
drivers/platform/x86/lenovo/wmi-other.c +13 -0
+
+struct lwmi_package_file {
+	struct lwmi_om_priv *priv;
+	/* Protects the pending response on shared descriptors. */
+	struct mutex lock;
+	struct lwmi_package_response *response;
+	size_t response_len;
+};
+
+static bool enable_get_data_by_package;
+module_param(enable_get_data_by_package, bool, 0400);
+MODULE_PARM_DESC(enable_get_data_by_package, "Enable the GetDataByPackage debugfs interface");
+
 /*
  * Visibility of fan channels:
  *

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1644

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
@@ -1611,6 +1638,174 @@ static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv)
 
 /* ======== Self (master: lenovo-wmi-other) ======== */
 
+static int lwmi_package_open(struct inode *inode, struct file *file)
+{
+	struct lwmi_om_priv *priv = inode->i_private;
+	struct lwmi_package_file *state __free(kfree) = NULL;

The declarations of __free() pointers should be placed at where it's allocated, i.e., ...

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1649

Quoted 5 lines
drivers/platform/x86/lenovo/wmi-other.c +5 -0
+
+	if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) != (FMODE_READ | FMODE_WRITE))
+		return -EINVAL;
+
+	state = kzalloc_obj(*state);

...here.

Meanwhile, opening the file while priv->package_opened == 1 allocates memory in vain. Could you move it below the package_opened check? In this manner you don't need the __free() either.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1653

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+	if (!state)
+		return -ENOMEM;
+
+	if (atomic_cmpxchg(&priv->package_opened, 0, 1))

atomic_cmpxchg_acquire()

Quoted 5 lines
drivers/platform/x86/lenovo/wmi-other.c +5 -0
+		return -EBUSY;
+
+	mutex_init(&state->lock);
+	state->priv = priv;
+	file->private_data = no_free_ptr(state);

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1668

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+
+	return stream_open(inode, file);
+}
+
+static int lwmi_package_release(struct inode *inode, struct file *file)
+{
+	struct lwmi_package_file *state = file->private_data;
+	struct dentry *dentry = file->f_path.dentry;
+
+	kfree_sensitive(state->response);

You didn't explain what data it is in your replies. "Can contain sensitive data" is not an acceptable explanation.

Please grep the occurrence of kfree_sensitive() in the kernel tree, and you'll find that they are mostly credentials. Does the data passed by the interface fall into the same category?

TBH, anything goes through WMI is *never* secret. If a manufacturer ever decided to use it to deliver "sensitive" data, it either misunderstood the word or chose the wrong interface.

An ACPI-WMI method goes through the ACPICA, then the WMI core, and finally here. The former two's temporary buffers are either on stack or freed by the vanilla kfree(). If leaking "sensitive" response were really a concern, it must had been leaked before it reached your code.

Please don't use kfree_sensitive() unless you really understand the data flow path and have a strong reason to use it. If you insist on it, please answer both questions.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1671

Quoted 3 lines
drivers/platform/x86/lenovo/wmi-other.c +3 -0
+	mutex_destroy(&state->lock);
+
+	/* Unlike read and write, release can run after debugfs removal. */

The comment is not helpful, as it's just repeating well-known knowledge. Please explain what it protects:

The release() callback may be called after state->priv has been freed. Only when the debugfs file hasn't been removed is the pointer valid.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1673

Quoted 2 lines
drivers/platform/x86/lenovo/wmi-other.c +2 -0
+	if (!debugfs_file_get(dentry)) {
+		atomic_set(&state->priv->package_opened, 0);

atomic_set_release()

Quoted 14 lines
drivers/platform/x86/lenovo/wmi-other.c +14 -0
+		debugfs_file_put(dentry);
+	}
+
+	kfree(state);
+	return 0;
+}
+
+static int lwmi_package_invoke(struct lwmi_package_file *state, u8 *data)
+{
+	struct lwmi_om_priv *priv = state->priv;
+	struct wmi_buffer input = { .length = LWMI_DATA_PACKAGE_INPUT_MAX, .data = data };
+	struct wmi_buffer output;
+	u32 data_size;
+	int ret;
Quoted 8 lines

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1695

Quoted 8 lines
drivers/platform/x86/lenovo/wmi-other.c +8 -0
+
+	lockdep_assert_held(&state->lock);
+
+	/* The tested firmware needs two calls before its response is usable. */
+	ret = wmidev_invoke_method(priv->wdev, 0, LWMI_DATA_PACKAGE_GET,
+				   &input, &output, 0);
+	if (ret)
+		return ret;

If you don't care about the first call's response, use wmidev_invoke_procedure().

Thanks,
Rong

Quoted 189 lines

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1707

Quoted 12 lines
drivers/platform/x86/lenovo/wmi-other.c +12 -0
+
+	kfree_sensitive(output.data);
+
+	ret = wmidev_invoke_method(priv->wdev, 0, LWMI_DATA_PACKAGE_GET,
+				   &input, &output, sizeof(struct lwmi_package_response));
+	if (ret)
+		return ret;
+
+	struct lwmi_package_response *response __free(kfree_sensitive) = output.data;
+
+	data_size = le32_to_cpu(response->data_size);
+	if (data_size > output.length - sizeof(*response))

Why not use struct_size() here? Considering that...

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1710

Quoted 3 lines
drivers/platform/x86/lenovo/wmi-other.c +3 -0
+		return -EPROTO;
+
+	state->response_len = struct_size(response, data, data_size);

...you've used it here, you may introduce a on-stack variable to temporarily store the calculated size and use it here and there.

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +7 -0
+	state->response = no_free_ptr(response);
+	return 0;
+}
+
+static ssize_t lwmi_package_write(struct file *file, const char __user *buffer,
+				  size_t count, loff_t *ppos)
+{

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1727

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	struct lwmi_package_file *state = file->private_data;
+	u8 input[LWMI_DATA_PACKAGE_INPUT_MAX] = {};
+	int ret;
+
+	if (!count || count > sizeof(input))
+		return -EINVAL;
+
+	ACQUIRE(mutex_try, state_guard)(&state->lock);
+	if (ACQUIRE_ERR(mutex_try, &state_guard) || state->response)
+		return -EBUSY;

I don't think it's a good idea to share the same errno between the two cases.

While ACQUIRE_ERR(mutex_try, &state_guard) should return -EBUSY, a pending state->response should return something like -EALREADY or -ENOTEMPTY.

Quoted 16 lines
drivers/platform/x86/lenovo/wmi-other.c +16 -0
+
+	if (copy_from_user(input, buffer, count))
+		return -EFAULT;
+
+	ret = lwmi_package_invoke(state, input);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+static ssize_t lwmi_package_read(struct file *file, char __user *buffer,
+				 size_t count, loff_t *ppos)
+{
+	struct lwmi_package_file *state = file->private_data;
+	size_t len;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1753

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+
+	if (!count)
+		return 0;
+
+	ACQUIRE(mutex_try, state_guard)(&state->lock);
+	if (ACQUIRE_ERR(mutex_try, &state_guard))
+		return -EBUSY;
+
+	if (!state->response)
+		return 0;

Reading the file without writing to it in prior is a mistake, isn't it? Thus:

		return -ENODATA;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1757

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+
+	len = state->response_len;
+	if (len > count)
+		return -ENOSPC;

Returning -ENOSPC is confusing as there is nothing to allocate during read. How about -ENOBUFS?

Quoted 26 lines
drivers/platform/x86/lenovo/wmi-other.c +26 -0
+
+	if (copy_to_user(buffer, state->response, len))
+		return -EFAULT;
+
+	kfree_sensitive(state->response);
+	state->response = NULL;
+	return len;
+}
+
+static const struct file_operations lwmi_package_fops = {
+	.owner = THIS_MODULE,
+	.open = lwmi_package_open,
+	.release = lwmi_package_release,
+	.read = lwmi_package_read,
+	.write = lwmi_package_write,
+};
+
+static void lwmi_package_debugfs_remove(void *data)
+{
+	debugfs_remove_recursive(data);
+}
+
+static int devm_lwmi_package_debugfs_add(struct lwmi_om_priv *priv)
+{
+	struct device *dev = &priv->wdev->dev;
+	struct dentry *dir, *entry;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1793

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	int ret;
+
+	if (!wmidev_instance_count(priv->wdev))
+		return 0;
+
+	dir = lwmi_debugfs_create_dir(priv->wdev);
+	if (IS_ERR(dir))
+		return PTR_ERR(dir);
+
+	atomic_set(&priv->package_opened, 0);

Why would you need this? The struct is kzalloc()-allocated.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1805

Quoted 12 lines
drivers/platform/x86/lenovo/wmi-other.c +12 -0
+
+	ret = devm_add_action_or_reset(dev, lwmi_package_debugfs_remove, dir);
+	if (ret)
+		return ret;
+
+	entry = debugfs_create_file("get_data_by_package", 0600, dir, priv, &lwmi_package_fops);
+	if (IS_ERR(entry)) {
+		devm_release_action(dev, lwmi_package_debugfs_remove, dir);
+		return PTR_ERR(entry);
+	}
+
+	priv->debugfs_dir = dir;

Why would you need this? The dir is devm-managed.

Quoted 15 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+	return 0;
+}
+
 /**
  * lwmi_om_master_bind() - Bind all components of the other mode driver
  * @dev: The lenovo-wmi-other driver basic device.
@@ -1693,6 +1888,7 @@ static int lwmi_other_probe(struct wmi_device *wdev, const void *context)
 {
 	struct component_match *master_match = NULL;
 	struct lwmi_om_priv *priv;
+	int ret;
 
 	priv = devm_kzalloc_obj(&wdev->dev, *priv);
 	if (!priv)

drivers/platform/x86/lenovo/wmi-other.c static int lwmi_other_probe(struct wmi_device *wdev, const void *context) · line 1910

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
@@ -1708,12 +1904,24 @@ static int lwmi_other_probe(struct wmi_device *wdev, const void *context)
 	if (IS_ERR(master_match))
 		return PTR_ERR(master_match);
 
+	if (enable_get_data_by_package) {
+		ret = devm_lwmi_package_debugfs_add(priv);
+		if (ret)
+			dev_warn(&wdev->dev, "failed to create GetDataByPackage debugfs files: %d\n",

The file is named get_data_by_package.

drivers/platform/x86/lenovo/wmi-other.c static int lwmi_other_probe(struct wmi_device *wdev, const void *context) · line 1923

Quoted 13 lines
drivers/platform/x86/lenovo/wmi-other.c +7 -0
+				 ret);
+	}
+
 	return component_master_add_with_match(&wdev->dev, &lwmi_om_master_ops,
 					       master_match);
 }
 
 static void lwmi_other_remove(struct wmi_device *wdev)
 {
+	struct lwmi_om_priv *priv = dev_get_drvdata(&wdev->dev);
+
+	if (priv->debugfs_dir)
+		devm_release_action(&wdev->dev, lwmi_package_debugfs_remove, priv->debugfs_dir);

Why would you need this? The interface does not depend on the components.

Thanks,
Rong

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +1 -0
+
 	component_master_del(&wdev->dev, &lwmi_om_master_ops);
 }
 
#13 lore raw

Hi Alan,

Quoted 368 lines · Rong Zhang

Hi Alan,

Quoted 241 lines · Rong Zhang

Hi Alan,

Quoted 1 line · Alan Pluto

Lenovo's userspace utility uses the Other Mode WMI GetDataByPackage

What's the purpose of the utility? Could you provide some context? You can ignore this question if it's confidential, but in general providing necessary context can help maintainers and reviewers learn about the intent.

Quoted 24 lines

method (99) to check device readiness. Expose the method through debugfs so the firmware request and response can be inspected on Linux without adding a stable userspace ABI.

The interface is disabled by default and can be enabled by passing enable_get_data_by_package=1 when loading the module.

---
Changes in v2:
- Replace the character device with a debugfs file, disabled by default.
- Allow one open per WMI device and require a single read for the response.
- Use scoped locks, lockdep checks, automatic buffer cleanup, and the WMI helper's minimum response size check.

 Documentation/wmi/devices/lenovo-wmi-other.rst |  12 +-
 drivers/platform/x86/lenovo/wmi-other.c        | 208 +++++++++++++++++++++++++
 2 files changed, 219 insertions(+), 1 deletion(-)
Documentation/wmi/devices/lenovo-wmi-other.rst +0 -0
index 011054d64eac5b601333373c4d405f9c5117ffc2..42e1525a8852bd7c8ddc04cc4047b1f09c5b770f 100644

Documentation/wmi/devices/lenovo-wmi-other.rst under the following path: · line 37

Quoted 5 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +2 -0
@@ -33,6 +33,15 @@ under the following path:
 
 Additionally, this driver also exports attributes to HWMON.
 
+GetDataByPackage diagnostics
+~~~~~~~~~~~~~~~~~~~~~~~~~~~

Missing a ~

Quoted 11 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +7 -0
+
+The driver does not create the diagnostic file by default. To enable it,
+pass ``enable_get_data_by_package=1`` when loading ``lenovo-wmi-other``.
+The file is at ``lenovo_wmi/<WMI device>/get_data_by_package``.
+Not all devices support the method. Use only commands whose behavior and
+side effects are understood.
+
 LENOVO_CAPABILITY_DATA_00
 -------------------------
 

Documentation/wmi/devices/lenovo-wmi-other.rst WMI interface description · line 120

Quoted 6 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +2 -1
@@ -107,7 +116,8 @@ WMI interface description
 =========================
 
 The WMI interface description can be decoded from the embedded binary MOF (bmof)
-data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility:
+data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility. This older
+example lists ``Max(40)`` for GetDataByPackage; newer firmware uses 100 bytes.

??? Could you move the sentence to the right place?

Quoted 56 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +0 -0
 ::
 
drivers/platform/x86/lenovo/wmi-other.c +10 -0
index bd64e7b44669db684145298ae088dd2c7f539d29..827b4215c612fbfc052bbadff0672e68e65517f0 100644
@@ -27,13 +27,16 @@
  */
 
 #include <linux/acpi.h>
+#include <linux/atomic.h>
 #include <linux/bitfield.h>
 #include <linux/cleanup.h>
 #include <linux/component.h>
 #include <linux/container_of.h>
+#include <linux/debugfs.h>
 #include <linux/device.h>
 #include <linux/export.h>
 #include <linux/firmware_attributes.h>
+#include <linux/fs.h>
 #include <linux/gfp_types.h>
 #include <linux/hwmon.h>
 #include <linux/idr.h>
@@ -41,9 +44,12 @@
 #include <linux/kobject.h>
 #include <linux/limits.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/platform_profile.h>
 #include <linux/power_supply.h>
+#include <linux/slab.h>
 #include <linux/types.h>
+#include <linux/uaccess.h>
 #include <linux/wmi.h>
 
 #include <acpi/battery.h>
@@ -90,6 +96,8 @@ enum lwmi_feature_id_psu {
 
 #define LWMI_FEATURE_VALUE_GET 17
 #define LWMI_FEATURE_VALUE_SET 18
+#define LWMI_DATA_PACKAGE_GET 99
+#define LWMI_DATA_PACKAGE_INPUT_MAX 100
 
 #define LWMI_FAN_ID_BASE 1
 #define LWMI_FAN_NR 4
@@ -141,6 +149,8 @@ struct lwmi_om_priv {
 	struct device *fw_attr_dev;
 	struct kset *fw_attr_kset;
 	struct wmi_device *wdev;
+	struct dentry *debugfs_dir;
+	atomic_t package_opened;
 	int ida_id;
 
 	struct lwmi_fan_info fan_info[LWMI_FAN_NR];

drivers/platform/x86/lenovo/wmi-other.c struct lwmi_om_priv { · line 172

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
@@ -156,6 +166,23 @@ struct lwmi_om_priv {
 	bool bh_registered;
 };
 
+struct lwmi_package_response {
+	__le32 data_size;
+	u8 data[];
+};

Even though there is no gap between data_size and data, please add a __packed annotation to clearly indicate your intent.

Quoted 17 lines
drivers/platform/x86/lenovo/wmi-other.c +13 -0
+
+struct lwmi_package_file {
+	struct lwmi_om_priv *priv;
+	/* Protects the pending response on shared descriptors. */
+	struct mutex lock;
+	struct lwmi_package_response *response;
+	size_t response_len;
+};
+
+static bool enable_get_data_by_package;
+module_param(enable_get_data_by_package, bool, 0400);
+MODULE_PARM_DESC(enable_get_data_by_package, "Enable the GetDataByPackage debugfs interface");
+
 /*
  * Visibility of fan channels:
  *

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1644

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
@@ -1611,6 +1638,174 @@ static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv)
 
 /* ======== Self (master: lenovo-wmi-other) ======== */
 
+static int lwmi_package_open(struct inode *inode, struct file *file)
+{
+	struct lwmi_om_priv *priv = inode->i_private;
+	struct lwmi_package_file *state __free(kfree) = NULL;

The declarations of __free() pointers should be placed at where it's allocated, i.e., ...

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1649

Quoted 5 lines
drivers/platform/x86/lenovo/wmi-other.c +5 -0
+
+	if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) != (FMODE_READ | FMODE_WRITE))
+		return -EINVAL;
+
+	state = kzalloc_obj(*state);

...here.

Meanwhile, opening the file while priv->package_opened == 1 allocates memory in vain. Could you move it below the package_opened check? In this manner you don't need the __free() either.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1653

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+	if (!state)
+		return -ENOMEM;
+
+	if (atomic_cmpxchg(&priv->package_opened, 0, 1))

atomic_cmpxchg_acquire()

Quoted 5 lines
drivers/platform/x86/lenovo/wmi-other.c +5 -0
+		return -EBUSY;
+
+	mutex_init(&state->lock);
+	state->priv = priv;
+	file->private_data = no_free_ptr(state);

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1668

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+
+	return stream_open(inode, file);
+}
+
+static int lwmi_package_release(struct inode *inode, struct file *file)
+{
+	struct lwmi_package_file *state = file->private_data;
+	struct dentry *dentry = file->f_path.dentry;
+
+	kfree_sensitive(state->response);

You didn't explain what data it is in your replies. "Can contain sensitive data" is not an acceptable explanation.

Please grep the occurrence of kfree_sensitive() in the kernel tree, and you'll find that they are mostly credentials. Does the data passed by the interface fall into the same category?

TBH, anything goes through WMI is *never* secret. If a manufacturer ever decided to use it to deliver "sensitive" data, it either misunderstood the word or chose the wrong interface.

An ACPI-WMI method goes through the ACPICA, then the WMI core, and finally here. The former two's temporary buffers are either on stack or freed by the vanilla kfree(). If leaking "sensitive" response were really a concern, it must had been leaked before it reached your code.

Please don't use kfree_sensitive() unless you really understand the data flow path and have a strong reason to use it. If you insist on it, please answer both questions.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1671

Quoted 3 lines
drivers/platform/x86/lenovo/wmi-other.c +3 -0
+	mutex_destroy(&state->lock);
+
+	/* Unlike read and write, release can run after debugfs removal. */

The comment is not helpful, as it's just repeating well-known knowledge. Please explain what it protects:

The release() callback may be called after state->priv has been freed. Only when the debugfs file hasn't been removed is the pointer valid.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1673

Quoted 2 lines
drivers/platform/x86/lenovo/wmi-other.c +2 -0
+	if (!debugfs_file_get(dentry)) {
+		atomic_set(&state->priv->package_opened, 0);

atomic_set_release()

Quoted 14 lines
drivers/platform/x86/lenovo/wmi-other.c +14 -0
+		debugfs_file_put(dentry);
+	}
+
+	kfree(state);
+	return 0;
+}
+
+static int lwmi_package_invoke(struct lwmi_package_file *state, u8 *data)
+{
+	struct lwmi_om_priv *priv = state->priv;
+	struct wmi_buffer input = { .length = LWMI_DATA_PACKAGE_INPUT_MAX, .data = data };
+	struct wmi_buffer output;
+	u32 data_size;
+	int ret;
Quoted 8 lines

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1695

Quoted 8 lines
drivers/platform/x86/lenovo/wmi-other.c +8 -0
+
+	lockdep_assert_held(&state->lock);
+
+	/* The tested firmware needs two calls before its response is usable. */
+	ret = wmidev_invoke_method(priv->wdev, 0, LWMI_DATA_PACKAGE_GET,
+				   &input, &output, 0);
+	if (ret)
+		return ret;

If you don't care about the first call's response, use wmidev_invoke_procedure().

Thanks,
Rong

Quoted 109 lines

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1707

Quoted 12 lines
drivers/platform/x86/lenovo/wmi-other.c +12 -0
+
+	kfree_sensitive(output.data);
+
+	ret = wmidev_invoke_method(priv->wdev, 0, LWMI_DATA_PACKAGE_GET,
+				   &input, &output, sizeof(struct lwmi_package_response));
+	if (ret)
+		return ret;
+
+	struct lwmi_package_response *response __free(kfree_sensitive) = output.data;
+
+	data_size = le32_to_cpu(response->data_size);
+	if (data_size > output.length - sizeof(*response))

Why not use struct_size() here? Considering that...

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1710

Quoted 3 lines
drivers/platform/x86/lenovo/wmi-other.c +3 -0
+		return -EPROTO;
+
+	state->response_len = struct_size(response, data, data_size);

...you've used it here, you may introduce a on-stack variable to temporarily store the calculated size and use it here and there.

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +7 -0
+	state->response = no_free_ptr(response);
+	return 0;
+}
+
+static ssize_t lwmi_package_write(struct file *file, const char __user *buffer,
+				  size_t count, loff_t *ppos)
+{

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1727

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	struct lwmi_package_file *state = file->private_data;
+	u8 input[LWMI_DATA_PACKAGE_INPUT_MAX] = {};
+	int ret;
+
+	if (!count || count > sizeof(input))
+		return -EINVAL;
+
+	ACQUIRE(mutex_try, state_guard)(&state->lock);
+	if (ACQUIRE_ERR(mutex_try, &state_guard) || state->response)
+		return -EBUSY;

I don't think it's a good idea to share the same errno between the two cases.

While ACQUIRE_ERR(mutex_try, &state_guard) should return -EBUSY, a pending state->response should return something like -EALREADY or -ENOTEMPTY.

Quoted 16 lines
drivers/platform/x86/lenovo/wmi-other.c +16 -0
+
+	if (copy_from_user(input, buffer, count))
+		return -EFAULT;
+
+	ret = lwmi_package_invoke(state, input);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+static ssize_t lwmi_package_read(struct file *file, char __user *buffer,
+				 size_t count, loff_t *ppos)
+{
+	struct lwmi_package_file *state = file->private_data;
+	size_t len;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1753

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+
+	if (!count)
+		return 0;
+
+	ACQUIRE(mutex_try, state_guard)(&state->lock);
+	if (ACQUIRE_ERR(mutex_try, &state_guard))
+		return -EBUSY;
+
+	if (!state->response)
+		return 0;

Reading the file without writing to it in prior is a mistake, isn't it? Thus:

		return -ENODATA;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1757

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+
+	len = state->response_len;
+	if (len > count)
+		return -ENOSPC;

Returning -ENOSPC is confusing as there is nothing to allocate during read. How about -ENOBUFS?

Quoted 22 lines
drivers/platform/x86/lenovo/wmi-other.c +22 -0
+
+	if (copy_to_user(buffer, state->response, len))
+		return -EFAULT;
+
+	kfree_sensitive(state->response);
+	state->response = NULL;
+	return len;
+}
+
+static const struct file_operations lwmi_package_fops = {
+	.owner = THIS_MODULE,
+	.open = lwmi_package_open,
+	.release = lwmi_package_release,
+	.read = lwmi_package_read,
+	.write = lwmi_package_write,
+};
+
+static void lwmi_package_debugfs_remove(void *data)
+{
+	debugfs_remove_recursive(data);
+}
+
Quoted 8 lines
Quoted 8 lines

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1787

Quoted 8 lines
drivers/platform/x86/lenovo/wmi-other.c +8 -0
+static int devm_lwmi_package_debugfs_add(struct lwmi_om_priv *priv)
+{
+	struct device *dev = &priv->wdev->dev;
+	struct dentry *dir, *entry;
+	int ret;
+
+	if (!wmidev_instance_count(priv->wdev))
+		return 0;

Is the bail-out reachable? See commit cbf54f37600e ("platform/x86: wmi:
Skip blocks with zero instances").

Quoted 4 lines
Quoted 4 lines

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1791

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+
+	dir = lwmi_debugfs_create_dir(priv->wdev);
+	if (IS_ERR(dir))
+		return PTR_ERR(dir);

IIRC, usually any error returned by debugfs methods can (and should) be blindly (yeah!) ignored by passing the returned pointer to the next debugfs method regardless.

See the kdoc of debugfs_create_dir() and debugfs_create_file().

Quoted 9 lines
Quoted 9 lines

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1793

Quoted 2 lines
drivers/platform/x86/lenovo/wmi-other.c +2 -0
+
+	atomic_set(&priv->package_opened, 0);

Why would you need this? The struct is kzalloc()-allocated.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1797

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+
+	ret = devm_add_action_or_reset(dev, lwmi_package_debugfs_remove, dir);
+	if (ret)
+		return ret;

Technically, by postponing devm_add_action_or_reset() after debugfs_create_file(), we can blindly ignore the former's error, too. Several drivers have already been doing so.

That being said, if doing so seems weird, it's also acceptable to store a pointer in priv and manage it manually instead of using devres.

Quoted 5 lines
Quoted 5 lines

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1802

Quoted 5 lines
drivers/platform/x86/lenovo/wmi-other.c +5 -0
+
+	entry = debugfs_create_file("get_data_by_package", 0600, dir, priv, &lwmi_package_fops);
+	if (IS_ERR(entry)) {
+		devm_release_action(dev, lwmi_package_debugfs_remove, dir);
+		return PTR_ERR(entry);

This error should also be ignored.

Quoted 18 lines
Quoted 18 lines

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1805

Quoted 3 lines
drivers/platform/x86/lenovo/wmi-other.c +3 -0
+	}
+
+	priv->debugfs_dir = dir;

Why would you need this? The dir is devm-managed.

Quoted 12 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+	return 0;
+}
+
 /**
  * lwmi_om_master_bind() - Bind all components of the other mode driver
  * @dev: The lenovo-wmi-other driver basic device.
@@ -1693,6 +1888,7 @@ static int lwmi_other_probe(struct wmi_device *wdev, const void *context)
 {
 	struct component_match *master_match = NULL;
 	struct lwmi_om_priv *priv;
+	int ret;
 
Quoted 7 lines
Quoted 7 lines

drivers/platform/x86/lenovo/wmi-other.c static int lwmi_other_probe(struct wmi_device *wdev, const void *context) · line 1907

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +1 -0
 	priv = devm_kzalloc_obj(&wdev->dev, *priv);
 	if (!priv)
@@ -1708,12 +1904,24 @@ static int lwmi_other_probe(struct wmi_device *wdev, const void *context)
 	if (IS_ERR(master_match))
 		return PTR_ERR(master_match);
 
+	if (enable_get_data_by_package) {

It'd be better to move the check inside the helper function.

Quoted 7 lines
Quoted 7 lines

drivers/platform/x86/lenovo/wmi-other.c static int lwmi_other_probe(struct wmi_device *wdev, const void *context) · line 1910

Quoted 3 lines
drivers/platform/x86/lenovo/wmi-other.c +3 -0
+		ret = devm_lwmi_package_debugfs_add(priv);
+		if (ret)
+			dev_warn(&wdev->dev, "failed to create GetDataByPackage debugfs files: %d\n",

The file is named get_data_by_package.

drivers/platform/x86/lenovo/wmi-other.c static int lwmi_other_probe(struct wmi_device *wdev, const void *context) · line 1911

Quoted 1 line
drivers/platform/x86/lenovo/wmi-other.c +1 -0
+				 ret);

With error paths removed, the dev_warn() is no longer needed.

Thanks,
Rong

Quoted 22 lines
Quoted 22 lines

drivers/platform/x86/lenovo/wmi-other.c static int lwmi_other_probe(struct wmi_device *wdev, const void *context) · line 1923

Quoted 12 lines
drivers/platform/x86/lenovo/wmi-other.c +6 -0
+	}
+
 	return component_master_add_with_match(&wdev->dev, &lwmi_om_master_ops,
 					       master_match);
 }
 
 static void lwmi_other_remove(struct wmi_device *wdev)
 {
+	struct lwmi_om_priv *priv = dev_get_drvdata(&wdev->dev);
+
+	if (priv->debugfs_dir)
+		devm_release_action(&wdev->dev, lwmi_package_debugfs_remove, priv->debugfs_dir);

Why would you need this? The interface does not depend on the components.

Thanks,
Rong

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +1 -0
+
 	component_master_del(&wdev->dev, &lwmi_om_master_ops);
 }
 
author
#14 lore raw

Hi Rong,

Quoted 453 lines · Rong Zhang

Hi Alan,

Quoted 368 lines · Rong Zhang

Hi Alan,

Quoted 241 lines · Rong Zhang

Hi Alan,

Quoted 1 line · Alan Pluto

Lenovo's userspace utility uses the Other Mode WMI GetDataByPackage

What's the purpose of the utility? Could you provide some context? You can ignore this question if it's confidential, but in general providing necessary context can help maintainers and reviewers learn about the intent.

Quoted 24 lines

method (99) to check device readiness. Expose the method through debugfs so the firmware request and response can be inspected on Linux without adding a stable userspace ABI.

The interface is disabled by default and can be enabled by passing enable_get_data_by_package=1 when loading the module.

---
Changes in v2:
- Replace the character device with a debugfs file, disabled by default.
- Allow one open per WMI device and require a single read for the response.
- Use scoped locks, lockdep checks, automatic buffer cleanup, and the WMI helper's minimum response size check.

 Documentation/wmi/devices/lenovo-wmi-other.rst |  12 +-
 drivers/platform/x86/lenovo/wmi-other.c        | 208 +++++++++++++++++++++++++
 2 files changed, 219 insertions(+), 1 deletion(-)
Documentation/wmi/devices/lenovo-wmi-other.rst +0 -0
index 011054d64eac5b601333373c4d405f9c5117ffc2..42e1525a8852bd7c8ddc04cc4047b1f09c5b770f 100644

Documentation/wmi/devices/lenovo-wmi-other.rst under the following path: · line 37

Quoted 5 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +2 -0
@@ -33,6 +33,15 @@ under the following path:
 
 Additionally, this driver also exports attributes to HWMON.
 
+GetDataByPackage diagnostics
+~~~~~~~~~~~~~~~~~~~~~~~~~~~

Missing a ~

Quoted 11 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +7 -0
+
+The driver does not create the diagnostic file by default. To enable it,
+pass ``enable_get_data_by_package=1`` when loading ``lenovo-wmi-other``.
+The file is at ``lenovo_wmi/<WMI device>/get_data_by_package``.
+Not all devices support the method. Use only commands whose behavior and
+side effects are understood.
+
 LENOVO_CAPABILITY_DATA_00
 -------------------------
 

Documentation/wmi/devices/lenovo-wmi-other.rst WMI interface description · line 120

Quoted 6 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +2 -1
@@ -107,7 +116,8 @@ WMI interface description
 =========================
 
 The WMI interface description can be decoded from the embedded binary MOF (bmof)
-data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility:
+data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility. This older
+example lists ``Max(40)`` for GetDataByPackage; newer firmware uses 100 bytes.

??? Could you move the sentence to the right place?

Quoted 56 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +0 -0
 ::
 
drivers/platform/x86/lenovo/wmi-other.c +10 -0
index bd64e7b44669db684145298ae088dd2c7f539d29..827b4215c612fbfc052bbadff0672e68e65517f0 100644
@@ -27,13 +27,16 @@
  */
 
 #include <linux/acpi.h>
+#include <linux/atomic.h>
 #include <linux/bitfield.h>
 #include <linux/cleanup.h>
 #include <linux/component.h>
 #include <linux/container_of.h>
+#include <linux/debugfs.h>
 #include <linux/device.h>
 #include <linux/export.h>
 #include <linux/firmware_attributes.h>
+#include <linux/fs.h>
 #include <linux/gfp_types.h>
 #include <linux/hwmon.h>
 #include <linux/idr.h>
@@ -41,9 +44,12 @@
 #include <linux/kobject.h>
 #include <linux/limits.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/platform_profile.h>
 #include <linux/power_supply.h>
+#include <linux/slab.h>
 #include <linux/types.h>
+#include <linux/uaccess.h>
 #include <linux/wmi.h>
 
 #include <acpi/battery.h>
@@ -90,6 +96,8 @@ enum lwmi_feature_id_psu {
 
 #define LWMI_FEATURE_VALUE_GET 17
 #define LWMI_FEATURE_VALUE_SET 18
+#define LWMI_DATA_PACKAGE_GET 99
+#define LWMI_DATA_PACKAGE_INPUT_MAX 100
 
 #define LWMI_FAN_ID_BASE 1
 #define LWMI_FAN_NR 4
@@ -141,6 +149,8 @@ struct lwmi_om_priv {
 	struct device *fw_attr_dev;
 	struct kset *fw_attr_kset;
 	struct wmi_device *wdev;
+	struct dentry *debugfs_dir;
+	atomic_t package_opened;
 	int ida_id;
 
 	struct lwmi_fan_info fan_info[LWMI_FAN_NR];

drivers/platform/x86/lenovo/wmi-other.c struct lwmi_om_priv { · line 172

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
@@ -156,6 +166,23 @@ struct lwmi_om_priv {
 	bool bh_registered;
 };
 
+struct lwmi_package_response {
+	__le32 data_size;
+	u8 data[];
+};

Even though there is no gap between data_size and data, please add a __packed annotation to clearly indicate your intent.

Quoted 17 lines
drivers/platform/x86/lenovo/wmi-other.c +13 -0
+
+struct lwmi_package_file {
+	struct lwmi_om_priv *priv;
+	/* Protects the pending response on shared descriptors. */
+	struct mutex lock;
+	struct lwmi_package_response *response;
+	size_t response_len;
+};
+
+static bool enable_get_data_by_package;
+module_param(enable_get_data_by_package, bool, 0400);
+MODULE_PARM_DESC(enable_get_data_by_package, "Enable the GetDataByPackage debugfs interface");
+
 /*
  * Visibility of fan channels:
  *

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1644

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
@@ -1611,6 +1638,174 @@ static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv)
 
 /* ======== Self (master: lenovo-wmi-other) ======== */
 
+static int lwmi_package_open(struct inode *inode, struct file *file)
+{
+	struct lwmi_om_priv *priv = inode->i_private;
+	struct lwmi_package_file *state __free(kfree) = NULL;

The declarations of __free() pointers should be placed at where it's allocated, i.e., ...

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1649

Quoted 5 lines
drivers/platform/x86/lenovo/wmi-other.c +5 -0
+
+	if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) != (FMODE_READ | FMODE_WRITE))
+		return -EINVAL;
+
+	state = kzalloc_obj(*state);

...here.

Meanwhile, opening the file while priv->package_opened == 1 allocates memory in vain. Could you move it below the package_opened check? In this manner you don't need the __free() either.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1653

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+	if (!state)
+		return -ENOMEM;
+
+	if (atomic_cmpxchg(&priv->package_opened, 0, 1))

atomic_cmpxchg_acquire()

Quoted 5 lines
drivers/platform/x86/lenovo/wmi-other.c +5 -0
+		return -EBUSY;
+
+	mutex_init(&state->lock);
+	state->priv = priv;
+	file->private_data = no_free_ptr(state);

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1668

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+
+	return stream_open(inode, file);
+}
+
+static int lwmi_package_release(struct inode *inode, struct file *file)
+{
+	struct lwmi_package_file *state = file->private_data;
+	struct dentry *dentry = file->f_path.dentry;
+
+	kfree_sensitive(state->response);

You didn't explain what data it is in your replies. "Can contain sensitive data" is not an acceptable explanation.

Please grep the occurrence of kfree_sensitive() in the kernel tree, and you'll find that they are mostly credentials. Does the data passed by the interface fall into the same category?

TBH, anything goes through WMI is *never* secret. If a manufacturer ever decided to use it to deliver "sensitive" data, it either misunderstood the word or chose the wrong interface.

An ACPI-WMI method goes through the ACPICA, then the WMI core, and finally here. The former two's temporary buffers are either on stack or freed by the vanilla kfree(). If leaking "sensitive" response were really a concern, it must had been leaked before it reached your code.

Please don't use kfree_sensitive() unless you really understand the data flow path and have a strong reason to use it. If you insist on it, please answer both questions.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1671

Quoted 3 lines
drivers/platform/x86/lenovo/wmi-other.c +3 -0
+	mutex_destroy(&state->lock);
+
+	/* Unlike read and write, release can run after debugfs removal. */

The comment is not helpful, as it's just repeating well-known knowledge. Please explain what it protects:

The release() callback may be called after state->priv has been freed. Only when the debugfs file hasn't been removed is the pointer valid.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1673

Quoted 2 lines
drivers/platform/x86/lenovo/wmi-other.c +2 -0
+	if (!debugfs_file_get(dentry)) {
+		atomic_set(&state->priv->package_opened, 0);

atomic_set_release()

Quoted 14 lines
drivers/platform/x86/lenovo/wmi-other.c +14 -0
+		debugfs_file_put(dentry);
+	}
+
+	kfree(state);
+	return 0;
+}
+
+static int lwmi_package_invoke(struct lwmi_package_file *state, u8 *data)
+{
+	struct lwmi_om_priv *priv = state->priv;
+	struct wmi_buffer input = { .length = LWMI_DATA_PACKAGE_INPUT_MAX, .data = data };
+	struct wmi_buffer output;
+	u32 data_size;
+	int ret;
Quoted 8 lines

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1695

Quoted 8 lines
drivers/platform/x86/lenovo/wmi-other.c +8 -0
+
+	lockdep_assert_held(&state->lock);
+
+	/* The tested firmware needs two calls before its response is usable. */
+	ret = wmidev_invoke_method(priv->wdev, 0, LWMI_DATA_PACKAGE_GET,
+				   &input, &output, 0);
+	if (ret)
+		return ret;

If you don't care about the first call's response, use wmidev_invoke_procedure().

Thanks,
Rong

Quoted 109 lines

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1707

Quoted 12 lines
drivers/platform/x86/lenovo/wmi-other.c +12 -0
+
+	kfree_sensitive(output.data);
+
+	ret = wmidev_invoke_method(priv->wdev, 0, LWMI_DATA_PACKAGE_GET,
+				   &input, &output, sizeof(struct lwmi_package_response));
+	if (ret)
+		return ret;
+
+	struct lwmi_package_response *response __free(kfree_sensitive) = output.data;
+
+	data_size = le32_to_cpu(response->data_size);
+	if (data_size > output.length - sizeof(*response))

Why not use struct_size() here? Considering that...

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1710

Quoted 3 lines
drivers/platform/x86/lenovo/wmi-other.c +3 -0
+		return -EPROTO;
+
+	state->response_len = struct_size(response, data, data_size);

...you've used it here, you may introduce a on-stack variable to temporarily store the calculated size and use it here and there.

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +7 -0
+	state->response = no_free_ptr(response);
+	return 0;
+}
+
+static ssize_t lwmi_package_write(struct file *file, const char __user *buffer,
+				  size_t count, loff_t *ppos)
+{

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1727

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	struct lwmi_package_file *state = file->private_data;
+	u8 input[LWMI_DATA_PACKAGE_INPUT_MAX] = {};
+	int ret;
+
+	if (!count || count > sizeof(input))
+		return -EINVAL;
+
+	ACQUIRE(mutex_try, state_guard)(&state->lock);
+	if (ACQUIRE_ERR(mutex_try, &state_guard) || state->response)
+		return -EBUSY;

I don't think it's a good idea to share the same errno between the two cases.

While ACQUIRE_ERR(mutex_try, &state_guard) should return -EBUSY, a pending state->response should return something like -EALREADY or -ENOTEMPTY.

Quoted 16 lines
drivers/platform/x86/lenovo/wmi-other.c +16 -0
+
+	if (copy_from_user(input, buffer, count))
+		return -EFAULT;
+
+	ret = lwmi_package_invoke(state, input);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+static ssize_t lwmi_package_read(struct file *file, char __user *buffer,
+				 size_t count, loff_t *ppos)
+{
+	struct lwmi_package_file *state = file->private_data;
+	size_t len;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1753

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+
+	if (!count)
+		return 0;
+
+	ACQUIRE(mutex_try, state_guard)(&state->lock);
+	if (ACQUIRE_ERR(mutex_try, &state_guard))
+		return -EBUSY;
+
+	if (!state->response)
+		return 0;

Reading the file without writing to it in prior is a mistake, isn't it? Thus:

		return -ENODATA;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1757

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+
+	len = state->response_len;
+	if (len > count)
+		return -ENOSPC;

Returning -ENOSPC is confusing as there is nothing to allocate during read. How about -ENOBUFS?

Quoted 22 lines
drivers/platform/x86/lenovo/wmi-other.c +22 -0
+
+	if (copy_to_user(buffer, state->response, len))
+		return -EFAULT;
+
+	kfree_sensitive(state->response);
+	state->response = NULL;
+	return len;
+}
+
+static const struct file_operations lwmi_package_fops = {
+	.owner = THIS_MODULE,
+	.open = lwmi_package_open,
+	.release = lwmi_package_release,
+	.read = lwmi_package_read,
+	.write = lwmi_package_write,
+};
+
+static void lwmi_package_debugfs_remove(void *data)
+{
+	debugfs_remove_recursive(data);
+}
+
Quoted 8 lines
Quoted 8 lines

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1787

Quoted 8 lines
drivers/platform/x86/lenovo/wmi-other.c +8 -0
+static int devm_lwmi_package_debugfs_add(struct lwmi_om_priv *priv)
+{
+	struct device *dev = &priv->wdev->dev;
+	struct dentry *dir, *entry;
+	int ret;
+
+	if (!wmidev_instance_count(priv->wdev))
+		return 0;

Is the bail-out reachable? See commit cbf54f37600e ("platform/x86: wmi:
Skip blocks with zero instances").

Quoted 4 lines
Quoted 4 lines

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1791

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+
+	dir = lwmi_debugfs_create_dir(priv->wdev);
+	if (IS_ERR(dir))
+		return PTR_ERR(dir);

IIRC, usually any error returned by debugfs methods can (and should) be blindly (yeah!) ignored by passing the returned pointer to the next debugfs method regardless.

See the kdoc of debugfs_create_dir() and debugfs_create_file().

Quoted 9 lines
Quoted 9 lines

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1793

Quoted 2 lines
drivers/platform/x86/lenovo/wmi-other.c +2 -0
+
+	atomic_set(&priv->package_opened, 0);

Why would you need this? The struct is kzalloc()-allocated.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1797

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+
+	ret = devm_add_action_or_reset(dev, lwmi_package_debugfs_remove, dir);
+	if (ret)
+		return ret;

Technically, by postponing devm_add_action_or_reset() after debugfs_create_file(), we can blindly ignore the former's error, too. Several drivers have already been doing so.

That being said, if doing so seems weird, it's also acceptable to store a pointer in priv and manage it manually instead of using devres.

Quoted 5 lines
Quoted 5 lines

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1802

Quoted 5 lines
drivers/platform/x86/lenovo/wmi-other.c +5 -0
+
+	entry = debugfs_create_file("get_data_by_package", 0600, dir, priv, &lwmi_package_fops);
+	if (IS_ERR(entry)) {
+		devm_release_action(dev, lwmi_package_debugfs_remove, dir);
+		return PTR_ERR(entry);

This error should also be ignored.

Quoted 18 lines
Quoted 18 lines

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1805

Quoted 3 lines
drivers/platform/x86/lenovo/wmi-other.c +3 -0
+	}
+
+	priv->debugfs_dir = dir;

Why would you need this? The dir is devm-managed.

Quoted 12 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+	return 0;
+}
+
 /**
  * lwmi_om_master_bind() - Bind all components of the other mode driver
  * @dev: The lenovo-wmi-other driver basic device.
@@ -1693,6 +1888,7 @@ static int lwmi_other_probe(struct wmi_device *wdev, const void *context)
 {
 	struct component_match *master_match = NULL;
 	struct lwmi_om_priv *priv;
+	int ret;
 
Quoted 7 lines
Quoted 7 lines

drivers/platform/x86/lenovo/wmi-other.c static int lwmi_other_probe(struct wmi_device *wdev, const void *context) · line 1907

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +1 -0
 	priv = devm_kzalloc_obj(&wdev->dev, *priv);
 	if (!priv)
@@ -1708,12 +1904,24 @@ static int lwmi_other_probe(struct wmi_device *wdev, const void *context)
 	if (IS_ERR(master_match))
 		return PTR_ERR(master_match);
 
+	if (enable_get_data_by_package) {

It'd be better to move the check inside the helper function.

Quoted 6 lines
Quoted 6 lines

drivers/platform/x86/lenovo/wmi-other.c static int lwmi_other_probe(struct wmi_device *wdev, const void *context) · line 1910

Quoted 3 lines
drivers/platform/x86/lenovo/wmi-other.c +3 -0
+		ret = devm_lwmi_package_debugfs_add(priv);
+		if (ret)
+			dev_warn(&wdev->dev, "failed to create GetDataByPackage debugfs files: %d\n",

The file is named get_data_by_package.

Quoted 29 lines
Quoted 1 line
Quoted 1 line

drivers/platform/x86/lenovo/wmi-other.c static int lwmi_other_probe(struct wmi_device *wdev, const void *context) · line 1911

Quoted 1 line
drivers/platform/x86/lenovo/wmi-other.c +1 -0
+				 ret);

With error paths removed, the dev_warn() is no longer needed.

Thanks,
Rong

Quoted 22 lines
Quoted 22 lines

drivers/platform/x86/lenovo/wmi-other.c static int lwmi_other_probe(struct wmi_device *wdev, const void *context) · line 1923

Quoted 12 lines
drivers/platform/x86/lenovo/wmi-other.c +6 -0
+	}
+
 	return component_master_add_with_match(&wdev->dev, &lwmi_om_master_ops,
 					       master_match);
 }
 
 static void lwmi_other_remove(struct wmi_device *wdev)
 {
+	struct lwmi_om_priv *priv = dev_get_drvdata(&wdev->dev);
+
+	if (priv->debugfs_dir)
+		devm_release_action(&wdev->dev, lwmi_package_debugfs_remove, priv->debugfs_dir);

Why would you need this? The interface does not depend on the components.

Thanks,
Rong

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +1 -0
+
 	component_master_del(&wdev->dev, &lwmi_om_master_ops);
 }
 

Thanks a lot for the detailed suggestions. I need some more time to go through them carefully and respond.

There is one important issue I'd like to discuss first: the userspace interface.

V1 used a character device, and V2 switched to debugfs. However, we have since realized that debugfs is not suitable for our use case. We have a production userspace application that needs read/write access to this WMI interface, while access to debugfs is restricted when Secure Boot is enabled.

So we need to reconsider the interface. Would exposing it through sysfs be acceptable, for example as a binary attribute under /sys/bus/wmi/devices/DC2A8805-.../, similar to drivers/platform/x86/dell/dcdbas.c?

What do you think?

author
#15 lore raw
In reply to Rong Zhang

Hi Rong,

Quoted 247 lines · Rong Zhang

Hi Alan,

Quoted 244 lines · Rong Zhang

Hi Alan,

Quoted 1 line · Alan Pluto

Lenovo's userspace utility uses the Other Mode WMI GetDataByPackage

What's the purpose of the utility? Could you provide some context? You can ignore this question if it's confidential, but in general providing necessary context can help maintainers and reviewers learn about the intent.

Quoted 24 lines

method (99) to check device readiness. Expose the method through debugfs so the firmware request and response can be inspected on Linux without adding a stable userspace ABI.

The interface is disabled by default and can be enabled by passing enable_get_data_by_package=1 when loading the module.

---
Changes in v2:
- Replace the character device with a debugfs file, disabled by default.
- Allow one open per WMI device and require a single read for the response.
- Use scoped locks, lockdep checks, automatic buffer cleanup, and the WMI helper's minimum response size check.

 Documentation/wmi/devices/lenovo-wmi-other.rst |  12 +-
 drivers/platform/x86/lenovo/wmi-other.c        | 208 +++++++++++++++++++++++++
 2 files changed, 219 insertions(+), 1 deletion(-)
Documentation/wmi/devices/lenovo-wmi-other.rst +0 -0
index 011054d64eac5b601333373c4d405f9c5117ffc2..42e1525a8852bd7c8ddc04cc4047b1f09c5b770f 100644

Documentation/wmi/devices/lenovo-wmi-other.rst under the following path: · line 37

Quoted 5 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +2 -0
@@ -33,6 +33,15 @@ under the following path:
 
 Additionally, this driver also exports attributes to HWMON.
 
+GetDataByPackage diagnostics
+~~~~~~~~~~~~~~~~~~~~~~~~~~~

Missing a ~

Quoted 11 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +7 -0
+
+The driver does not create the diagnostic file by default. To enable it,
+pass ``enable_get_data_by_package=1`` when loading ``lenovo-wmi-other``.
+The file is at ``lenovo_wmi/<WMI device>/get_data_by_package``.
+Not all devices support the method. Use only commands whose behavior and
+side effects are understood.
+
 LENOVO_CAPABILITY_DATA_00
 -------------------------
 

Documentation/wmi/devices/lenovo-wmi-other.rst WMI interface description · line 120

Quoted 6 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +2 -1
@@ -107,7 +116,8 @@ WMI interface description
 =========================
 
 The WMI interface description can be decoded from the embedded binary MOF (bmof)
-data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility:
+data using the `bmfdec <https://github.com/pali/bmfdec>`_ utility. This older
+example lists ``Max(40)`` for GetDataByPackage; newer firmware uses 100 bytes.

??? Could you move the sentence to the right place?

Quoted 56 lines
Documentation/wmi/devices/lenovo-wmi-other.rst +0 -0
 ::
 
drivers/platform/x86/lenovo/wmi-other.c +10 -0
index bd64e7b44669db684145298ae088dd2c7f539d29..827b4215c612fbfc052bbadff0672e68e65517f0 100644
@@ -27,13 +27,16 @@
  */
 
 #include <linux/acpi.h>
+#include <linux/atomic.h>
 #include <linux/bitfield.h>
 #include <linux/cleanup.h>
 #include <linux/component.h>
 #include <linux/container_of.h>
+#include <linux/debugfs.h>
 #include <linux/device.h>
 #include <linux/export.h>
 #include <linux/firmware_attributes.h>
+#include <linux/fs.h>
 #include <linux/gfp_types.h>
 #include <linux/hwmon.h>
 #include <linux/idr.h>
@@ -41,9 +44,12 @@
 #include <linux/kobject.h>
 #include <linux/limits.h>
 #include <linux/module.h>
+#include <linux/mutex.h>
 #include <linux/platform_profile.h>
 #include <linux/power_supply.h>
+#include <linux/slab.h>
 #include <linux/types.h>
+#include <linux/uaccess.h>
 #include <linux/wmi.h>
 
 #include <acpi/battery.h>
@@ -90,6 +96,8 @@ enum lwmi_feature_id_psu {
 
 #define LWMI_FEATURE_VALUE_GET 17
 #define LWMI_FEATURE_VALUE_SET 18
+#define LWMI_DATA_PACKAGE_GET 99
+#define LWMI_DATA_PACKAGE_INPUT_MAX 100
 
 #define LWMI_FAN_ID_BASE 1
 #define LWMI_FAN_NR 4
@@ -141,6 +149,8 @@ struct lwmi_om_priv {
 	struct device *fw_attr_dev;
 	struct kset *fw_attr_kset;
 	struct wmi_device *wdev;
+	struct dentry *debugfs_dir;
+	atomic_t package_opened;
 	int ida_id;
 
 	struct lwmi_fan_info fan_info[LWMI_FAN_NR];

drivers/platform/x86/lenovo/wmi-other.c struct lwmi_om_priv { · line 172

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
@@ -156,6 +166,23 @@ struct lwmi_om_priv {
 	bool bh_registered;
 };
 
+struct lwmi_package_response {
+	__le32 data_size;
+	u8 data[];
+};

Even though there is no gap between data_size and data, please add a __packed annotation to clearly indicate your intent.

Quoted 17 lines
drivers/platform/x86/lenovo/wmi-other.c +13 -0
+
+struct lwmi_package_file {
+	struct lwmi_om_priv *priv;
+	/* Protects the pending response on shared descriptors. */
+	struct mutex lock;
+	struct lwmi_package_response *response;
+	size_t response_len;
+};
+
+static bool enable_get_data_by_package;
+module_param(enable_get_data_by_package, bool, 0400);
+MODULE_PARM_DESC(enable_get_data_by_package, "Enable the GetDataByPackage debugfs interface");
+
 /*
  * Visibility of fan channels:
  *

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1644

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
@@ -1611,6 +1638,174 @@ static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv)
 
 /* ======== Self (master: lenovo-wmi-other) ======== */
 
+static int lwmi_package_open(struct inode *inode, struct file *file)
+{
+	struct lwmi_om_priv *priv = inode->i_private;
+	struct lwmi_package_file *state __free(kfree) = NULL;

The declarations of __free() pointers should be placed at where it's allocated, i.e., ...

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1649

Quoted 5 lines
drivers/platform/x86/lenovo/wmi-other.c +5 -0
+
+	if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) != (FMODE_READ | FMODE_WRITE))
+		return -EINVAL;
+
+	state = kzalloc_obj(*state);

...here.

Meanwhile, opening the file while priv->package_opened == 1 allocates memory in vain. Could you move it below the package_opened check? In this manner you don't need the __free() either.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1653

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+	if (!state)
+		return -ENOMEM;
+
+	if (atomic_cmpxchg(&priv->package_opened, 0, 1))

atomic_cmpxchg_acquire()

Quoted 5 lines
drivers/platform/x86/lenovo/wmi-other.c +5 -0
+		return -EBUSY;
+
+	mutex_init(&state->lock);
+	state->priv = priv;
+	file->private_data = no_free_ptr(state);

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1668

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+
+	return stream_open(inode, file);
+}
+
+static int lwmi_package_release(struct inode *inode, struct file *file)
+{
+	struct lwmi_package_file *state = file->private_data;
+	struct dentry *dentry = file->f_path.dentry;
+
+	kfree_sensitive(state->response);

You didn't explain what data it is in your replies. "Can contain sensitive data" is not an acceptable explanation.

Please grep the occurrence of kfree_sensitive() in the kernel tree, and you'll find that they are mostly credentials. Does the data passed by the interface fall into the same category?

TBH, anything goes through WMI is *never* secret. If a manufacturer ever decided to use it to deliver "sensitive" data, it either misunderstood the word or chose the wrong interface.

An ACPI-WMI method goes through the ACPICA, then the WMI core, and finally here. The former two's temporary buffers are either on stack or freed by the vanilla kfree(). If leaking "sensitive" response were really a concern, it must had been leaked before it reached your code.

Please don't use kfree_sensitive() unless you really understand the data flow path and have a strong reason to use it. If you insist on it, please answer both questions.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1671

Quoted 3 lines
drivers/platform/x86/lenovo/wmi-other.c +3 -0
+	mutex_destroy(&state->lock);
+
+	/* Unlike read and write, release can run after debugfs removal. */

The comment is not helpful, as it's just repeating well-known knowledge. Please explain what it protects:

The release() callback may be called after state->priv has been freed. Only when the debugfs file hasn't been removed is the pointer valid.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1673

Quoted 2 lines
drivers/platform/x86/lenovo/wmi-other.c +2 -0
+	if (!debugfs_file_get(dentry)) {
+		atomic_set(&state->priv->package_opened, 0);

atomic_set_release()

Quoted 17 lines
drivers/platform/x86/lenovo/wmi-other.c +17 -0
+		debugfs_file_put(dentry);
+	}
+
+	kfree(state);
+	return 0;
+}
+
+static int lwmi_package_invoke(struct lwmi_package_file *state, u8 *data)
+{
+	struct lwmi_om_priv *priv = state->priv;
+	struct wmi_buffer input = { .length = LWMI_DATA_PACKAGE_INPUT_MAX, .data = data };
+	struct wmi_buffer output;
+	u32 data_size;
+	int ret;
+
+	lockdep_assert_held(&state->lock);
+
Quoted 8 lines
Quoted 5 lines

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1695

Quoted 5 lines
drivers/platform/x86/lenovo/wmi-other.c +5 -0
+	/* The tested firmware needs two calls before its response is usable. */
+	ret = wmidev_invoke_method(priv->wdev, 0, LWMI_DATA_PACKAGE_GET,
+				   &input, &output, 0);
+	if (ret)
+		return ret;

If you don't care about the first call's response, use wmidev_invoke_procedure().

I'll use wmidev_invoke_procedure() for the first call.

Thanks,
Alan

Quoted 193 lines

Thanks,
Rong

Quoted 189 lines

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1707

Quoted 12 lines
drivers/platform/x86/lenovo/wmi-other.c +12 -0
+
+	kfree_sensitive(output.data);
+
+	ret = wmidev_invoke_method(priv->wdev, 0, LWMI_DATA_PACKAGE_GET,
+				   &input, &output, sizeof(struct lwmi_package_response));
+	if (ret)
+		return ret;
+
+	struct lwmi_package_response *response __free(kfree_sensitive) = output.data;
+
+	data_size = le32_to_cpu(response->data_size);
+	if (data_size > output.length - sizeof(*response))

Why not use struct_size() here? Considering that...

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1710

Quoted 3 lines
drivers/platform/x86/lenovo/wmi-other.c +3 -0
+		return -EPROTO;
+
+	state->response_len = struct_size(response, data, data_size);

...you've used it here, you may introduce a on-stack variable to temporarily store the calculated size and use it here and there.

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +7 -0
+	state->response = no_free_ptr(response);
+	return 0;
+}
+
+static ssize_t lwmi_package_write(struct file *file, const char __user *buffer,
+				  size_t count, loff_t *ppos)
+{

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1727

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	struct lwmi_package_file *state = file->private_data;
+	u8 input[LWMI_DATA_PACKAGE_INPUT_MAX] = {};
+	int ret;
+
+	if (!count || count > sizeof(input))
+		return -EINVAL;
+
+	ACQUIRE(mutex_try, state_guard)(&state->lock);
+	if (ACQUIRE_ERR(mutex_try, &state_guard) || state->response)
+		return -EBUSY;

I don't think it's a good idea to share the same errno between the two cases.

While ACQUIRE_ERR(mutex_try, &state_guard) should return -EBUSY, a pending state->response should return something like -EALREADY or -ENOTEMPTY.

Quoted 16 lines
drivers/platform/x86/lenovo/wmi-other.c +16 -0
+
+	if (copy_from_user(input, buffer, count))
+		return -EFAULT;
+
+	ret = lwmi_package_invoke(state, input);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+static ssize_t lwmi_package_read(struct file *file, char __user *buffer,
+				 size_t count, loff_t *ppos)
+{
+	struct lwmi_package_file *state = file->private_data;
+	size_t len;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1753

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+
+	if (!count)
+		return 0;
+
+	ACQUIRE(mutex_try, state_guard)(&state->lock);
+	if (ACQUIRE_ERR(mutex_try, &state_guard))
+		return -EBUSY;
+
+	if (!state->response)
+		return 0;

Reading the file without writing to it in prior is a mistake, isn't it? Thus:

		return -ENODATA;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1757

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+
+	len = state->response_len;
+	if (len > count)
+		return -ENOSPC;

Returning -ENOSPC is confusing as there is nothing to allocate during read. How about -ENOBUFS?

Quoted 26 lines
drivers/platform/x86/lenovo/wmi-other.c +26 -0
+
+	if (copy_to_user(buffer, state->response, len))
+		return -EFAULT;
+
+	kfree_sensitive(state->response);
+	state->response = NULL;
+	return len;
+}
+
+static const struct file_operations lwmi_package_fops = {
+	.owner = THIS_MODULE,
+	.open = lwmi_package_open,
+	.release = lwmi_package_release,
+	.read = lwmi_package_read,
+	.write = lwmi_package_write,
+};
+
+static void lwmi_package_debugfs_remove(void *data)
+{
+	debugfs_remove_recursive(data);
+}
+
+static int devm_lwmi_package_debugfs_add(struct lwmi_om_priv *priv)
+{
+	struct device *dev = &priv->wdev->dev;
+	struct dentry *dir, *entry;

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1793

Quoted 10 lines
drivers/platform/x86/lenovo/wmi-other.c +10 -0
+	int ret;
+
+	if (!wmidev_instance_count(priv->wdev))
+		return 0;
+
+	dir = lwmi_debugfs_create_dir(priv->wdev);
+	if (IS_ERR(dir))
+		return PTR_ERR(dir);
+
+	atomic_set(&priv->package_opened, 0);

Why would you need this? The struct is kzalloc()-allocated.

drivers/platform/x86/lenovo/wmi-other.c static void lwmi_om_fw_attr_remove(struct lwmi_om_priv *priv) · line 1805

Quoted 12 lines
drivers/platform/x86/lenovo/wmi-other.c +12 -0
+
+	ret = devm_add_action_or_reset(dev, lwmi_package_debugfs_remove, dir);
+	if (ret)
+		return ret;
+
+	entry = debugfs_create_file("get_data_by_package", 0600, dir, priv, &lwmi_package_fops);
+	if (IS_ERR(entry)) {
+		devm_release_action(dev, lwmi_package_debugfs_remove, dir);
+		return PTR_ERR(entry);
+	}
+
+	priv->debugfs_dir = dir;

Why would you need this? The dir is devm-managed.

Quoted 15 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
+	return 0;
+}
+
 /**
  * lwmi_om_master_bind() - Bind all components of the other mode driver
  * @dev: The lenovo-wmi-other driver basic device.
@@ -1693,6 +1888,7 @@ static int lwmi_other_probe(struct wmi_device *wdev, const void *context)
 {
 	struct component_match *master_match = NULL;
 	struct lwmi_om_priv *priv;
+	int ret;
 
 	priv = devm_kzalloc_obj(&wdev->dev, *priv);
 	if (!priv)

drivers/platform/x86/lenovo/wmi-other.c static int lwmi_other_probe(struct wmi_device *wdev, const void *context) · line 1910

Quoted 7 lines
drivers/platform/x86/lenovo/wmi-other.c +4 -0
@@ -1708,12 +1904,24 @@ static int lwmi_other_probe(struct wmi_device *wdev, const void *context)
 	if (IS_ERR(master_match))
 		return PTR_ERR(master_match);
 
+	if (enable_get_data_by_package) {
+		ret = devm_lwmi_package_debugfs_add(priv);
+		if (ret)
+			dev_warn(&wdev->dev, "failed to create GetDataByPackage debugfs files: %d\n",

The file is named get_data_by_package.

drivers/platform/x86/lenovo/wmi-other.c static int lwmi_other_probe(struct wmi_device *wdev, const void *context) · line 1923

Quoted 13 lines
drivers/platform/x86/lenovo/wmi-other.c +7 -0
+				 ret);
+	}
+
 	return component_master_add_with_match(&wdev->dev, &lwmi_om_master_ops,
 					       master_match);
 }
 
 static void lwmi_other_remove(struct wmi_device *wdev)
 {
+	struct lwmi_om_priv *priv = dev_get_drvdata(&wdev->dev);
+
+	if (priv->debugfs_dir)
+		devm_release_action(&wdev->dev, lwmi_package_debugfs_remove, priv->debugfs_dir);

Why would you need this? The interface does not depend on the components.

Thanks,
Rong

Quoted 4 lines
drivers/platform/x86/lenovo/wmi-other.c +1 -0
+
 	component_master_del(&wdev->dev, &lwmi_om_master_ops);
 }