drivers/platform/wmi/string.c
Source file repositories/reference/linux-study-clean/drivers/platform/wmi/string.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/platform/wmi/string.c- Extension
.c- Size
- 2832 bytes
- Lines
- 93
- Domain
- Driver Families
- Bucket
- drivers/platform
- Inferred role
- Driver Families: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/build_bug.hlinux/compiler_types.hlinux/err.hlinux/export.hlinux/nls.hlinux/limits.hlinux/types.hlinux/wmi.hasm/byteorder.h
Detected Declarations
function wmi_string_to_utf8sfunction wmi_string_from_utf8sexport wmi_string_to_utf8sexport wmi_string_from_utf8s
Annotated Snippet
// 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;
Annotation
- Immediate include surface: `linux/build_bug.h`, `linux/compiler_types.h`, `linux/err.h`, `linux/export.h`, `linux/nls.h`, `linux/limits.h`, `linux/types.h`, `linux/wmi.h`.
- Detected declarations: `function wmi_string_to_utf8s`, `function wmi_string_from_utf8s`, `export wmi_string_to_utf8s`, `export wmi_string_from_utf8s`.
- Atlas domain: Driver Families / drivers/platform.
- Implementation status: integration implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.