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@gmx.de>
Link: https://patch.msgid.link/20260116204116.4030-4-W_Armin@gmx.de
Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
This commit is contained in:
Armin Wolf 2026-01-16 21:41:10 +01:00 committed by Ilpo Järvinen
parent 29dfba69c3
commit b990a06f7e
No known key found for this signature in database
GPG key ID: 59AC4F6153E5CE31
5 changed files with 102 additions and 1 deletions

View file

@ -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:

View file

@ -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.

View file

@ -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

View file

@ -0,0 +1,92 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* WMI string utility functions.
*
* Copyright (C) 2025 Armin Wolf <W_Armin@gmx.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);

View file

@ -62,6 +62,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);