From patchwork Fri Nov 3 18:19:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: James Seo X-Patchwork-Id: 13444875 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8A69AC4332F for ; Fri, 3 Nov 2023 18:19:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230220AbjKCST5 (ORCPT ); Fri, 3 Nov 2023 14:19:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39910 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230197AbjKCSTy (ORCPT ); Fri, 3 Nov 2023 14:19:54 -0400 Received: from so254-32.mailgun.net (so254-32.mailgun.net [198.61.254.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E862ADB for ; Fri, 3 Nov 2023 11:19:46 -0700 (PDT) DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=equiv.tech; q=dns/txt; s=mx; t=1699035585; x=1699042785; h=Content-Transfer-Encoding: Content-Type: MIME-Version: Message-Id: Date: Subject: Subject: Cc: To: To: From: From: Sender: Sender; bh=Aw3NIZOfJGjEMR/zbjqoZg8/cDgznkhoiJJtPHEZINM=; b=LZoxlEfK+ZMiQAEdQ6icHiIJqgspBOmZFb7F6voG0ic5MZQWdRv6sM0sE8D7O8qZoiZy2PeHkUOV3ap00Rt6e+tdxQV/05IEreoTLLGLg5NfNAsG7XiDBSyh0iSVv3Z+7b9nHJ6+bXNjQS1qhkXMNjdhTsfBmQ2gh8CojB4lBYQnn7ZZbdi08p4l1yu1+JEsFqoKjHQcW76T3jgk3jNy0aE70ulAQMwKOQQi9kAPAyvssRTlsSQK3OLECm6T+c7G5nWVBV4OpCsSZqI2rCu8wbvo/b3GV+zj8uiWWbAzb182Qe3SO9OF8MzoakITDfETyZS4GszJbuF4cayaD73/7g== X-Mailgun-Sending-Ip: 198.61.254.32 X-Mailgun-Sid: WyJkOWUwNSIsImxpbnV4LWh3bW9uQHZnZXIua2VybmVsLm9yZyIsIjkzZDVhYiJd Received: from mail.equiv.tech (equiv.tech [142.93.28.83]) by 0ae46d290350 with SMTP id 654539c082847315b749a83e (version=TLS1.2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256); Fri, 03 Nov 2023 18:19:44 GMT Sender: james@equiv.tech From: James Seo To: Jean Delvare , Guenter Roeck Cc: James Seo , Lukasz Stelmach , Armin Wolf , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC] hwmon: (hp-wmi-sensors) Fix failure to load on EliteDesk 800 G6 Date: Fri, 3 Nov 2023 11:19:31 -0700 Message-Id: <20231103181931.677796-1-james@equiv.tech> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org The EliteDesk 800 G6 stores a raw WMI string within the ACPI object in its BIOS corresponding to one instance of HPBIOS_PlatformEvents.Name. This is evidently a valid way of representing a WMI data item as far as the Microsoft ACPI-WMI mapper is concerned, but is preventing the driver from loading. As this seems quite rare, add a machine-limited workaround for now. Reported-by: Lukasz Stelmach Closes: https://lore.kernel.org/linux-hwmon/7850a0bd-60e7-88f8-1d6c-0bb0e3234fdc@roeck-us.net/ Signed-off-by: James Seo --- Was converting to UTF-8 a good idea? There's also not much UTF-16 validation, because at the moment it's only being done on one machine with a known input. --- drivers/hwmon/hp-wmi-sensors.c | 57 ++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) base-commit: 0f564130e5c76f1e5cf0008924f6a6cd138929d9 diff --git a/drivers/hwmon/hp-wmi-sensors.c b/drivers/hwmon/hp-wmi-sensors.c index 17ae62f88bbf..c82a9bbf16ca 100644 --- a/drivers/hwmon/hp-wmi-sensors.c +++ b/drivers/hwmon/hp-wmi-sensors.c @@ -17,13 +17,17 @@ * Available: https://github.com/linuxhw/ACPI * [4] P. Rohár, "bmfdec - Decompile binary MOF file (BMF) from WMI buffer", * 2017. [Online]. Available: https://github.com/pali/bmfdec + * [5] Microsoft Corporation, "Driver-Defined WMI Data Items", 2017. [Online]. + * Available: https://learn.microsoft.com/en-us/windows-hardware/drivers/kernel/driver-defined-wmi-data-items */ #include #include +#include #include #include #include +#include #include #include @@ -52,6 +56,10 @@ #define HP_WMI_MAX_PROPERTIES 32U #define HP_WMI_MAX_INSTANCES 32U +/* DMI board names for machines requiring workarounds. */ + +#define HP_WMI_BOARD_NAME_ELITEDESK_800_G6 "870C" + enum hp_wmi_type { HP_WMI_TYPE_OTHER = 1, HP_WMI_TYPE_TEMPERATURE = 2, @@ -412,6 +420,30 @@ static char *hp_wmi_strdup(struct device *dev, const char *src) return dst; } +/* hp_wmi_wstrdup - hp_wmi_strdup, but for a raw WMI string */ +static char *hp_wmi_wstrdup(struct device *dev, const u8 *buf) +{ + const wchar_t *src; + size_t len; + char *dst; + int i; + + /* WMI strings are length-prefixed UTF-16. See [5]. */ + src = (wchar_t *)buf; + len = min(*src++ / sizeof(*src), (size_t)HP_WMI_MAX_STR_SIZE - 1); + while (len && !src[len - 1]) + len--; + + dst = devm_kmalloc(dev, (len + 1) * sizeof(*dst), GFP_KERNEL); + if (!dst) + return NULL; + + i = utf16s_to_utf8s(src, len, UTF16_LITTLE_ENDIAN, dst, len); + dst[i] = '\0'; + + return strim(dst); +} + /* * hp_wmi_get_wobj - poll WMI for a WMI object instance * @guid: WMI object GUID @@ -442,6 +474,21 @@ static u8 hp_wmi_wobj_instance_count(const char *guid) return clamp(count, 0, (int)HP_WMI_MAX_INSTANCES); } +static bool is_raw_wmi_string(const acpi_object_type property_map[], int prop) +{ + const char *board_name; + + if (property_map != hp_wmi_platform_events_property_map || + prop != HP_WMI_PLATFORM_EVENTS_PROPERTY_NAME) + return false; + + board_name = dmi_get_system_info(DMI_BOARD_NAME); + if (!board_name) + return false; + + return !strcmp(board_name, HP_WMI_BOARD_NAME_ELITEDESK_800_G6); +} + static int check_wobj(const union acpi_object *wobj, const acpi_object_type property_map[], int last_prop) { @@ -462,8 +509,12 @@ static int check_wobj(const union acpi_object *wobj, for (prop = 0; prop <= last_prop; prop++) { type = elements[prop].type; valid_type = property_map[prop]; - if (type != valid_type) + if (type != valid_type) { + if (type == ACPI_TYPE_BUFFER && + is_raw_wmi_string(property_map, prop)) + continue; return -EINVAL; + } } return 0; @@ -480,7 +531,9 @@ static int extract_acpi_value(struct device *dev, break; case ACPI_TYPE_STRING: - *out_string = hp_wmi_strdup(dev, strim(element->string.pointer)); + *out_string = element->type == ACPI_TYPE_BUFFER ? + hp_wmi_wstrdup(dev, element->buffer.pointer) : + hp_wmi_strdup(dev, strim(element->string.pointer)); if (!*out_string) return -ENOMEM; break;