[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20251122203803.6154-4-W_Armin@gmx.de>
Date: Sat, 22 Nov 2025 21:37:57 +0100
From: Armin Wolf <W_Armin@....de>
To: hansg@...nel.org,
ilpo.jarvinen@...ux.intel.com
Cc: platform-driver-x86@...r.kernel.org,
linux-kernel@...r.kernel.org,
linux@...ssschuh.net,
Dell.Client.Kernel@...l.com,
corbet@....net,
linux-doc@...r.kernel.org
Subject: [PATCH 3/9] platform/wmi: Add helper functions for WMI string conversions
WMI strings are encoded using UTF16-LE characters, forcing WMI drivers
to manually convert them to/from standard UTF8 strings. Add a two
helper functions for those tasks.
Signed-off-by: Armin Wolf <W_Armin@....de>
---
Documentation/driver-api/wmi.rst | 3 ++
drivers/platform/wmi/Kconfig | 1 +
drivers/platform/wmi/Makefile | 2 +-
drivers/platform/wmi/string.c | 92 ++++++++++++++++++++++++++++++++
include/linux/wmi.h | 5 ++
5 files changed, 102 insertions(+), 1 deletion(-)
create mode 100644 drivers/platform/wmi/string.c
diff --git a/Documentation/driver-api/wmi.rst b/Documentation/driver-api/wmi.rst
index db835b43c937..b847bcdcbb09 100644
--- a/Documentation/driver-api/wmi.rst
+++ b/Documentation/driver-api/wmi.rst
@@ -16,5 +16,8 @@ which will be bound to compatible WMI devices by the driver core.
.. kernel-doc:: include/linux/wmi.h
:internal:
+.. kernel-doc:: drivers/platform/wmi/string.c
+ :export:
+
.. kernel-doc:: drivers/platform/wmi/core.c
:export:
diff --git a/drivers/platform/wmi/Kconfig b/drivers/platform/wmi/Kconfig
index 21fa3e440042..d62f51ff3b7f 100644
--- a/drivers/platform/wmi/Kconfig
+++ b/drivers/platform/wmi/Kconfig
@@ -6,6 +6,7 @@
menuconfig ACPI_WMI
tristate "ACPI-WMI support"
depends on ACPI && X86
+ select NLS
help
This option enables support for the ACPI-WMI driver core.
diff --git a/drivers/platform/wmi/Makefile b/drivers/platform/wmi/Makefile
index 93f37ce519ae..2feff94a5594 100644
--- a/drivers/platform/wmi/Makefile
+++ b/drivers/platform/wmi/Makefile
@@ -4,7 +4,7 @@
# ACPI WMI core
#
-wmi-y := core.o marshalling.o
+wmi-y := core.o marshalling.o string.o
obj-$(CONFIG_ACPI_WMI) += wmi.o
# Unit tests
diff --git a/drivers/platform/wmi/string.c b/drivers/platform/wmi/string.c
new file mode 100644
index 000000000000..0fc43218aa5b
--- /dev/null
+++ b/drivers/platform/wmi/string.c
@@ -0,0 +1,92 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * WMI string utility functions.
+ *
+ * Copyright (C) 2025 Armin Wolf <W_Armin@....de>
+ */
+
+#include <linux/build_bug.h>
+#include <linux/compiler_types.h>
+#include <linux/err.h>
+#include <linux/export.h>
+#include <linux/nls.h>
+#include <linux/limits.h>
+#include <linux/types.h>
+#include <linux/wmi.h>
+
+#include <asm/byteorder.h>
+
+static_assert(sizeof(__le16) == sizeof(wchar_t));
+
+/**
+ * wmi_string_to_utf8s - Convert a WMI string into a UTF8 string.
+ * @str: WMI string representation
+ * @dst: Buffer to fill with UTF8 characters
+ * @length: Length of the destination buffer
+ *
+ * Convert as WMI string into a standard UTF8 string. The conversion will stop
+ * once a NUL character is detected or when the buffer is full. Any invalid UTF16
+ * characters will be ignored. The resulting UTF8 string will always be NUL-terminated
+ * when this function returns successfully.
+ *
+ * Return: Length of the resulting UTF8 string or negative errno code on failure.
+ */
+ssize_t wmi_string_to_utf8s(const struct wmi_string *str, u8 *dst, size_t length)
+{
+ /* Contains the maximum number of UTF16 code points to read */
+ int inlen = le16_to_cpu(str->length) / 2;
+ int ret;
+
+ if (length < 1)
+ return -EINVAL;
+
+ /* We must leave room for the NUL character at the end of the destination buffer */
+ ret = utf16s_to_utf8s((__force const wchar_t *)str->chars, inlen, UTF16_LITTLE_ENDIAN, dst,
+ length - 1);
+ if (ret < 0)
+ return ret;
+
+ dst[ret] = '\0';
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(wmi_string_to_utf8s);
+
+/**
+ * wmi_string_from_utf8s - Convert a UTF8 string into a WMI string.
+ * @str: WMI string representation
+ * @max_chars: Maximum number of UTF16 code points to store inside the WMI string
+ * @src: UTF8 string to convert
+ * @src_length: Length of the source string without any trailing NUL-characters
+ *
+ * Convert a UTF8 string into a WMI string. The conversion will stop when the WMI string is
+ * full. The resulting WMI string will always be NUL-terminated and have its length field set
+ * to and appropriate value when this function returns successfully.
+ *
+ * Return: Number of UTF16 code points inside the WMI string or negative errno code on failure.
+ */
+ssize_t wmi_string_from_utf8s(struct wmi_string *str, size_t max_chars, const u8 *src,
+ size_t src_length)
+{
+ size_t str_length;
+ int ret;
+
+ if (max_chars < 1)
+ return -EINVAL;
+
+ /* We must leave room for the NUL character at the end of the WMI string */
+ ret = utf8s_to_utf16s(src, src_length, UTF16_LITTLE_ENDIAN, (__force wchar_t *)str->chars,
+ max_chars - 1);
+ if (ret < 0)
+ return ret;
+
+ str_length = (ret + 1) * sizeof(u16);
+ if (str_length > U16_MAX)
+ return -EOVERFLOW;
+
+ str->length = cpu_to_le16(str_length);
+ str->chars[ret] = '\0';
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(wmi_string_from_utf8s);
diff --git a/include/linux/wmi.h b/include/linux/wmi.h
index 4c2fc3c1f0de..fbb794951388 100644
--- a/include/linux/wmi.h
+++ b/include/linux/wmi.h
@@ -61,6 +61,11 @@ struct wmi_string {
__le16 chars[];
} __packed;
+ssize_t wmi_string_to_utf8s(const struct wmi_string *str, u8 *dst, size_t length);
+
+ssize_t wmi_string_from_utf8s(struct wmi_string *str, size_t max_chars, const u8 *src,
+ size_t src_length);
+
int wmidev_invoke_method(struct wmi_device *wdev, u8 instance, u32 method_id,
const struct wmi_buffer *in, struct wmi_buffer *out);
--
2.39.5
Powered by blists - more mailing lists