From patchwork Fri Sep 20 16:02:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ayman Bagabas X-Patchwork-Id: 11154629 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8154A16B1 for ; Fri, 20 Sep 2019 16:03:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4BA90206B6 for ; Fri, 20 Sep 2019 16:03:31 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jcINIcFw" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729336AbfITQDb (ORCPT ); Fri, 20 Sep 2019 12:03:31 -0400 Received: from mail-yw1-f66.google.com ([209.85.161.66]:38770 "EHLO mail-yw1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729225AbfITQDa (ORCPT ); Fri, 20 Sep 2019 12:03:30 -0400 Received: by mail-yw1-f66.google.com with SMTP id s6so2659076ywe.5; Fri, 20 Sep 2019 09:03:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=UTE7QtTKG0Qt8VGCOJuFvDtSDxGzSKqUw1u9Hg2orpU=; b=jcINIcFwJb22TEwLQnI5a8gzNrKM6iOfG/VZTjLm6e8CdQNqFRdP+rwdJl1gZHIQX4 PKzkwBRVe6Mm4VVLVioZPslfvrw07pcr0SRPiEXs8opXnFtfwRowNmm74vR6b0UCaA8O OqrgzZJRNIo9fukMHHpBIyfzIeAfCyPCwV0v4ArVTA6gYfRq2i5M+S2vBmkc7QxaZ7ji BW0e7KzPdPEnv57RT0o5vFi5MXE86pXf/9+h5c+0rwHeXO39lIQjhFA8fZV93ql1A82v DYQ9WeffQZAsosVgX50Z/D8N1kanb3xk+tCLD6b1nMI+YzoDi/xJsgCd9f/r1G9/lJDl tOtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UTE7QtTKG0Qt8VGCOJuFvDtSDxGzSKqUw1u9Hg2orpU=; b=o6gLsHujGTBpMbP8ZYDh0iiCHtX6LOJiXUsI+6ibV3UpS6cMsIMGsCnvEh/7AIUgSz DTlC+RMRf+ntbg2waSoFdYWubj4LQhGNaZnB8CLPfNyPJnf5I4HzujMQxSA1N8J12s7Y MKIhuIBh/vuqj/OKbqIHuENavWvOx06/eNkl7OK1Jt7ZDnqXkfHpHTiihLXODcIO0Jf6 zSGGS21FLm2w9m9F2xsgAqbCkX+RA8d3j0pBCmKTzxSgV297lA9PwhkdTYUBsh6KTY87 4I5UoPJdLpKQEjGgpdshGa3dtGTOyj6E/it2fTKgPp4rHpVoICATnQey6KZ1DC+1LufI SJcw== X-Gm-Message-State: APjAAAU5hwCVOfW8E1fzvHCsnAuujFVehmvDp1C4zQKT3Rl+bEFO1iVU Neny8XxTxH6XHzJmO4I+BQ== X-Google-Smtp-Source: APXvYqzPDQ9+JUuiBtsu0/flyNZiRHlJnv/bxav5gcj6Gjz1zOjhEfLhynmauPOejtoSKDMJk1ZKNg== X-Received: by 2002:a81:2849:: with SMTP id o70mr12703966ywo.389.1568995409441; Fri, 20 Sep 2019 09:03:29 -0700 (PDT) Received: from localhost.localdomain ([12.156.111.130]) by smtp.gmail.com with ESMTPSA id 207sm518086ywu.106.2019.09.20.09.03.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Sep 2019 09:03:28 -0700 (PDT) From: Ayman Bagabas To: Darren Hart , Andy Shevchenko , Sinan Kaya , "Rafael J. Wysocki" , Mauro Carvalho Chehab , Takashi Iwai , Ayman Bagabas , Stuart Hayes , Matan Ziv-Av , "Enrico Weigelt, metux IT consult" , Hans de Goede , Peng Hao , Krzysztof Kozlowski , Mattias Jacobsson <2pi@mok.nu>, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 1/6] platform/x86: huawei-wmi: Move to platform driver Date: Fri, 20 Sep 2019 12:02:34 -0400 Message-Id: <20190920160250.12510-2-ayman.bagabas@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190920160250.12510-1-ayman.bagabas@gmail.com> References: <20190920160250.12510-1-ayman.bagabas@gmail.com> MIME-Version: 1.0 Sender: platform-driver-x86-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: platform-driver-x86@vger.kernel.org Move from WMI driver to platform driver. This move is necessary since the driver is no longer a hotkeys driver only. Platform driver makes it easier for users to access sysfs attributes under (i.e. /sys/devices/platform/huawei-wmi) compared to wmi driver. Use WMI device UID, AMW0 has a UID of HWMI. WMI0 is the device name and doesn't have a UID so keep it as it is. Signed-off-by: Ayman Bagabas --- drivers/platform/x86/Kconfig | 7 +- drivers/platform/x86/huawei-wmi.c | 226 ++++++++++++++++++++---------- 2 files changed, 156 insertions(+), 77 deletions(-) diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig index 1b67bb578f9f..61bf180d25c7 100644 --- a/drivers/platform/x86/Kconfig +++ b/drivers/platform/x86/Kconfig @@ -1304,7 +1304,7 @@ config INTEL_ATOMISP2_PM will be called intel_atomisp2_pm. config HUAWEI_WMI - tristate "Huawei WMI hotkeys driver" + tristate "Huawei WMI laptop extras driver" depends on ACPI_WMI depends on INPUT select INPUT_SPARSEKMAP @@ -1313,9 +1313,8 @@ config HUAWEI_WMI select LEDS_TRIGGER_AUDIO select NEW_LEDS help - This driver provides support for Huawei WMI hotkeys. - It enables the missing keys and adds support to the micmute - LED found on some of these laptops. + This driver provides support for Huawei WMI hotkeys, battery charge + control, fn-lock, mic-mute LED, and other extra features. To compile this driver as a module, choose M here: the module will be called huawei-wmi. diff --git a/drivers/platform/x86/huawei-wmi.c b/drivers/platform/x86/huawei-wmi.c index 195a7f3638cb..9496ea3c78b5 100644 --- a/drivers/platform/x86/huawei-wmi.c +++ b/drivers/platform/x86/huawei-wmi.c @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 /* - * Huawei WMI hotkeys + * Huawei WMI laptop extras driver * * Copyright (C) 2018 Ayman Bagabas */ @@ -10,23 +10,28 @@ #include #include #include +#include #include /* * Huawei WMI GUIDs */ -#define WMI0_EVENT_GUID "59142400-C6A3-40fa-BADB-8A2652834100" -#define AMW0_EVENT_GUID "ABBC0F5C-8EA1-11D1-A000-C90629100000" +#define HWMI_EVENT_GUID "ABBC0F5C-8EA1-11D1-A000-C90629100000" +/* Legacy GUIDs */ #define WMI0_EXPENSIVE_GUID "39142400-C6A3-40fa-BADB-8A2652834100" +#define WMI0_EVENT_GUID "59142400-C6A3-40fa-BADB-8A2652834100" -struct huawei_wmi_priv { - struct input_dev *idev; +struct huawei_wmi { + struct input_dev *idev[2]; struct led_classdev cdev; + struct platform_device *pdev; acpi_handle handle; char *acpi_method; }; +struct huawei_wmi *huawei_wmi; + static const struct key_entry huawei_wmi_keymap[] = { { KE_KEY, 0x281, { KEY_BRIGHTNESSDOWN } }, { KE_KEY, 0x282, { KEY_BRIGHTNESSUP } }, @@ -37,7 +42,7 @@ static const struct key_entry huawei_wmi_keymap[] = { { KE_KEY, 0x289, { KEY_WLAN } }, // Huawei |M| key { KE_KEY, 0x28a, { KEY_CONFIG } }, - // Keyboard backlight + // Keyboard backlit { KE_IGNORE, 0x293, { KEY_KBDILLUMTOGGLE } }, { KE_IGNORE, 0x294, { KEY_KBDILLUMUP } }, { KE_IGNORE, 0x295, { KEY_KBDILLUMUP } }, @@ -47,7 +52,7 @@ static const struct key_entry huawei_wmi_keymap[] = { static int huawei_wmi_micmute_led_set(struct led_classdev *led_cdev, enum led_brightness brightness) { - struct huawei_wmi_priv *priv = dev_get_drvdata(led_cdev->dev->parent); + struct huawei_wmi *huawei = dev_get_drvdata(led_cdev->dev->parent); acpi_status status; union acpi_object args[3]; struct acpi_object_list arg_list = { @@ -58,52 +63,53 @@ static int huawei_wmi_micmute_led_set(struct led_classdev *led_cdev, args[0].type = args[1].type = args[2].type = ACPI_TYPE_INTEGER; args[1].integer.value = 0x04; - if (strcmp(priv->acpi_method, "SPIN") == 0) { + if (strcmp(huawei->acpi_method, "SPIN") == 0) { args[0].integer.value = 0; args[2].integer.value = brightness ? 1 : 0; - } else if (strcmp(priv->acpi_method, "WPIN") == 0) { + } else if (strcmp(huawei->acpi_method, "WPIN") == 0) { args[0].integer.value = 1; args[2].integer.value = brightness ? 0 : 1; } else { return -EINVAL; } - status = acpi_evaluate_object(priv->handle, priv->acpi_method, &arg_list, NULL); + status = acpi_evaluate_object(huawei->handle, huawei->acpi_method, &arg_list, NULL); if (ACPI_FAILURE(status)) return -ENXIO; return 0; } -static int huawei_wmi_leds_setup(struct wmi_device *wdev) +static void huawei_wmi_leds_setup(struct device *dev) { - struct huawei_wmi_priv *priv = dev_get_drvdata(&wdev->dev); + struct huawei_wmi *huawei = dev_get_drvdata(dev); - priv->handle = ec_get_handle(); - if (!priv->handle) - return 0; + huawei->handle = ec_get_handle(); + if (!huawei->handle) + return; - if (acpi_has_method(priv->handle, "SPIN")) - priv->acpi_method = "SPIN"; - else if (acpi_has_method(priv->handle, "WPIN")) - priv->acpi_method = "WPIN"; + if (acpi_has_method(huawei->handle, "SPIN")) + huawei->acpi_method = "SPIN"; + else if (acpi_has_method(huawei->handle, "WPIN")) + huawei->acpi_method = "WPIN"; else - return 0; + return; - priv->cdev.name = "platform::micmute"; - priv->cdev.max_brightness = 1; - priv->cdev.brightness_set_blocking = huawei_wmi_micmute_led_set; - priv->cdev.default_trigger = "audio-micmute"; - priv->cdev.brightness = ledtrig_audio_get(LED_AUDIO_MICMUTE); - priv->cdev.dev = &wdev->dev; - priv->cdev.flags = LED_CORE_SUSPENDRESUME; + huawei->cdev.name = "platform::micmute"; + huawei->cdev.max_brightness = 1; + huawei->cdev.brightness_set_blocking = &huawei_wmi_micmute_led_set; + huawei->cdev.default_trigger = "audio-micmute"; + huawei->cdev.brightness = ledtrig_audio_get(LED_AUDIO_MICMUTE); + huawei->cdev.dev = dev; + huawei->cdev.flags = LED_CORE_SUSPENDRESUME; - return devm_led_classdev_register(&wdev->dev, &priv->cdev); + devm_led_classdev_register(dev, &huawei->cdev); } -static void huawei_wmi_process_key(struct wmi_device *wdev, int code) +/* Input */ + +static void huawei_wmi_process_key(struct input_dev *idev, int code) { - struct huawei_wmi_priv *priv = dev_get_drvdata(&wdev->dev); const struct key_entry *key; /* @@ -127,81 +133,155 @@ static void huawei_wmi_process_key(struct wmi_device *wdev, int code) kfree(response.pointer); } - key = sparse_keymap_entry_from_scancode(priv->idev, code); + key = sparse_keymap_entry_from_scancode(idev, code); if (!key) { - dev_info(&wdev->dev, "Unknown key pressed, code: 0x%04x\n", code); + dev_info(&idev->dev, "Unknown key pressed, code: 0x%04x\n", code); return; } - sparse_keymap_report_entry(priv->idev, key, 1, true); + sparse_keymap_report_entry(idev, key, 1, true); } -static void huawei_wmi_notify(struct wmi_device *wdev, - union acpi_object *obj) +static void huawei_wmi_input_notify(u32 value, void *context) { - if (obj->type == ACPI_TYPE_INTEGER) - huawei_wmi_process_key(wdev, obj->integer.value); + struct input_dev *idev = (struct input_dev *)context; + struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL }; + union acpi_object *obj; + acpi_status status; + + status = wmi_get_event_data(value, &response); + if (ACPI_FAILURE(status)) { + dev_err(&idev->dev, "Unable to get event data\n"); + return; + } + + obj = (union acpi_object *)response.pointer; + if (obj && obj->type == ACPI_TYPE_INTEGER) + huawei_wmi_process_key(idev, obj->integer.value); else - dev_info(&wdev->dev, "Bad response type %d\n", obj->type); + dev_err(&idev->dev, "Bad response type\n"); + + kfree(response.pointer); } -static int huawei_wmi_input_setup(struct wmi_device *wdev) +static int huawei_wmi_input_setup(struct device *dev, + const char *guid, + struct input_dev **idev) { - struct huawei_wmi_priv *priv = dev_get_drvdata(&wdev->dev); - int err; - - priv->idev = devm_input_allocate_device(&wdev->dev); - if (!priv->idev) + *idev = devm_input_allocate_device(dev); + if (!*idev) return -ENOMEM; - priv->idev->name = "Huawei WMI hotkeys"; - priv->idev->phys = "wmi/input0"; - priv->idev->id.bustype = BUS_HOST; - priv->idev->dev.parent = &wdev->dev; + (*idev)->name = "Huawei WMI hotkeys"; + (*idev)->phys = "wmi/input0"; + (*idev)->id.bustype = BUS_HOST; + (*idev)->dev.parent = dev; - err = sparse_keymap_setup(priv->idev, huawei_wmi_keymap, NULL); - if (err) - return err; + return sparse_keymap_setup(*idev, huawei_wmi_keymap, NULL) || + input_register_device(*idev) || + wmi_install_notify_handler(guid, huawei_wmi_input_notify, + *idev); +} - return input_register_device(priv->idev); +static void huawei_wmi_input_exit(struct device *dev, const char *guid) +{ + wmi_remove_notify_handler(guid); } -static int huawei_wmi_probe(struct wmi_device *wdev, const void *context) +/* Huawei driver */ + +static const struct wmi_device_id huawei_wmi_events_id_table[] = { + { .guid_string = WMI0_EVENT_GUID }, + { } +}; + +static int huawei_wmi_probe(struct platform_device *pdev) { - struct huawei_wmi_priv *priv; + const struct wmi_device_id *guid = huawei_wmi_events_id_table; int err; - priv = devm_kzalloc(&wdev->dev, sizeof(struct huawei_wmi_priv), GFP_KERNEL); - if (!priv) - return -ENOMEM; + platform_set_drvdata(pdev, huawei_wmi); + huawei_wmi->pdev = pdev; - dev_set_drvdata(&wdev->dev, priv); + while (*guid->guid_string) { + struct input_dev *idev = *huawei_wmi->idev; - err = huawei_wmi_input_setup(wdev); - if (err) - return err; + if (wmi_has_guid(guid->guid_string)) { + err = huawei_wmi_input_setup(&pdev->dev, guid->guid_string, &idev); + if (err) { + dev_err(&pdev->dev, "Failed to setup input on %s\n", guid->guid_string); + return err; + } + } - return huawei_wmi_leds_setup(wdev); + idev++; + guid++; + } + + huawei_wmi_leds_setup(&pdev->dev); + return 0; } -static const struct wmi_device_id huawei_wmi_id_table[] = { - { .guid_string = WMI0_EVENT_GUID }, - { .guid_string = AMW0_EVENT_GUID }, - { } -}; +static int huawei_wmi_remove(struct platform_device *pdev) +{ + const struct wmi_device_id *guid = huawei_wmi_events_id_table; + + while (*guid->guid_string) { + if (wmi_has_guid(guid->guid_string)) + huawei_wmi_input_exit(&pdev->dev, guid->guid_string); + + guid++; + } -static struct wmi_driver huawei_wmi_driver = { + return 0; +} + +static struct platform_driver huawei_wmi_driver = { .driver = { .name = "huawei-wmi", }, - .id_table = huawei_wmi_id_table, .probe = huawei_wmi_probe, - .notify = huawei_wmi_notify, + .remove = huawei_wmi_remove, }; -module_wmi_driver(huawei_wmi_driver); +static __init int huawei_wmi_init(void) +{ + struct platform_device *pdev; + int err; + + huawei_wmi = kzalloc(sizeof(struct huawei_wmi), GFP_KERNEL); + if (!huawei_wmi) + return -ENOMEM; + + err = platform_driver_register(&huawei_wmi_driver); + if (err) + goto pdrv_err; + + pdev = platform_device_register_simple("huawei-wmi", -1, NULL, 0); + if (IS_ERR(pdev)) { + err = PTR_ERR(pdev); + goto pdev_err; + } + + return 0; + +pdev_err: + platform_driver_unregister(&huawei_wmi_driver); +pdrv_err: + kfree(huawei_wmi); + return err; +} + +static __exit void huawei_wmi_exit(void) +{ + platform_device_unregister(huawei_wmi->pdev); + platform_driver_unregister(&huawei_wmi_driver); +} + +module_init(huawei_wmi_init); +module_exit(huawei_wmi_exit); -MODULE_DEVICE_TABLE(wmi, huawei_wmi_id_table); +MODULE_DEVICE_TABLE(wmi, huawei_wmi_events_id_table); MODULE_AUTHOR("Ayman Bagabas "); -MODULE_DESCRIPTION("Huawei WMI hotkeys"); +MODULE_DESCRIPTION("Huawei WMI laptop extras driver"); MODULE_LICENSE("GPL v2"); From patchwork Fri Sep 20 16:02:35 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ayman Bagabas X-Patchwork-Id: 11154631 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 508F91747 for ; Fri, 20 Sep 2019 16:03:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E8255206B6 for ; Fri, 20 Sep 2019 16:03:42 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="tkgjJbAL" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729366AbfITQDm (ORCPT ); Fri, 20 Sep 2019 12:03:42 -0400 Received: from mail-yw1-f67.google.com ([209.85.161.67]:44563 "EHLO mail-yw1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729225AbfITQDm (ORCPT ); Fri, 20 Sep 2019 12:03:42 -0400 Received: by mail-yw1-f67.google.com with SMTP id u187so2646015ywa.11; Fri, 20 Sep 2019 09:03:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=mDRWgzAX5BzQRDnjOLDKf4M+7Qn9M6zsk32ycuv6xH4=; b=tkgjJbAL/w8dExIaZzY63NUGa99MfsPUhokfbZ0x962LcmI/zhaiQpB3+PNEpAJPSh 4oIuQhcJ112OxvCeCdgSswvVGeQ8c9G/ZoCnQLw+5IWzxxajWGHd34Nq4uQRcMAz6VCL v4vo7Ngr1aMFM9msk8oeiDyf9gjtiOZGsOqnR8MAwBJcn5McIn6hyh/VhqnzMYG7PLGT m90luQWBcghkDuL+wLbspVx137iFRjl9eYjopXgT6vqjXnXVmwF5uHwivDlPM42ZB6vu hm9htIs4T78ZFy8QbChbwUU3N4Ok8PREXpdj9i50oeN4jbewah1feWoE/PQcgb1tChGn qlbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mDRWgzAX5BzQRDnjOLDKf4M+7Qn9M6zsk32ycuv6xH4=; b=mg213GxHtdQPCzdMsiClq9NgPGfvIT5qt2uKvF49+GOsicLbC+nX6nKwCu+9YJ467A JvQ1t3qd4ZEOJ3vOCzcV3GsZKifiazdtX1zvjgHS8w17k/bHWw/ckmVtMQTBjXW7g2BM j3zWPB/QASRlFwiu7c9KGEUVe1zfQpDG3B5XrNMkBldaIUwbLKG+K3/Z802dpNqFkMXp IjfGm4i32w6owyvKjaljshARwupT1np3LcC2xdugWD7BmnWDXBGsOlisFYSSyjSMZJ2L eyxR5HBymNytA7cKD6XFwd8I5b+mk/YIodzrtf174BnqvrpzAVEwwGCJuj3Hd3cS7pDf A9zg== X-Gm-Message-State: APjAAAW4jylZ3/kVC+58KOlu4V8Zj3IzeuPv9bp/9wPXuRjogWjxRLbK VOxmhuOXKd3CUXxaXKvuyQ== X-Google-Smtp-Source: APXvYqyUb/RjNgsaIlaget/WadHgtRrtngo/Be9QajxJZxEZlEFAQY3S/jXIwQ0s+LhNP4PjXSKo1w== X-Received: by 2002:a81:4702:: with SMTP id u2mr13665436ywa.53.1568995420868; Fri, 20 Sep 2019 09:03:40 -0700 (PDT) Received: from localhost.localdomain ([12.156.111.130]) by smtp.gmail.com with ESMTPSA id 207sm518086ywu.106.2019.09.20.09.03.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Sep 2019 09:03:39 -0700 (PDT) From: Ayman Bagabas To: Darren Hart , Andy Shevchenko , Sinan Kaya , "Rafael J. Wysocki" , Mauro Carvalho Chehab , Takashi Iwai , Ayman Bagabas , Stuart Hayes , Matan Ziv-Av , Hans de Goede , "Enrico Weigelt, metux IT consult" , Peng Hao , Krzysztof Kozlowski , Mattias Jacobsson <2pi@mok.nu>, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 2/6] platform/x86: huawei-wmi: Add quirks and module parameters Date: Fri, 20 Sep 2019 12:02:35 -0400 Message-Id: <20190920160250.12510-3-ayman.bagabas@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190920160250.12510-1-ayman.bagabas@gmail.com> References: <20190920160250.12510-1-ayman.bagabas@gmail.com> MIME-Version: 1.0 Sender: platform-driver-x86-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: platform-driver-x86@vger.kernel.org Introduce quirks and module parameters. 3 quirks are added: 1. Fixes reporting brightness keys twice since it's already handled by acpi-video. 2. Some models need a short delay when setting battery thresholds to prevent a race condition when two processes read/write. (will be used later) 3. Matebook X (2017) handles micmute led through the "legacy" interface which is not currently implemented. Use ACPI EC method to control this led. (will be used later) 2 module parameters are added to enable this short delay and/or report brightness keys through this driver. Signed-off-by: Ayman Bagabas --- drivers/platform/x86/huawei-wmi.c | 73 +++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/drivers/platform/x86/huawei-wmi.c b/drivers/platform/x86/huawei-wmi.c index 9496ea3c78b5..0e919bf56ad1 100644 --- a/drivers/platform/x86/huawei-wmi.c +++ b/drivers/platform/x86/huawei-wmi.c @@ -6,6 +6,7 @@ */ #include +#include #include #include #include @@ -22,6 +23,14 @@ #define WMI0_EXPENSIVE_GUID "39142400-C6A3-40fa-BADB-8A2652834100" #define WMI0_EVENT_GUID "59142400-C6A3-40fa-BADB-8A2652834100" +struct quirk_entry { + bool battery_reset; + bool ec_micmute; + bool report_brightness; +}; + +static struct quirk_entry *quirks; + struct huawei_wmi { struct input_dev *idev[2]; struct led_classdev cdev; @@ -49,6 +58,58 @@ static const struct key_entry huawei_wmi_keymap[] = { { KE_END, 0 } }; +static int battery_reset = -1; +static int report_brightness = -1; + +module_param(battery_reset, bint, 0444); +MODULE_PARM_DESC(battery_reset, + "Reset battery charge values to (0-0) before disabling it using (0-100)"); +module_param(report_brightness, bint, 0444); +MODULE_PARM_DESC(report_brightness, + "Report brightness keys."); + +/* Quirks */ + +static int __init dmi_matched(const struct dmi_system_id *dmi) +{ + quirks = dmi->driver_data; + return 1; +} + +static struct quirk_entry quirk_unknown = { +}; + +static struct quirk_entry quirk_battery_reset = { + .battery_reset = true, +}; + +static struct quirk_entry quirk_matebook_x = { + .ec_micmute = true, + .report_brightness = true, +}; + +static const struct dmi_system_id huawei_quirks[] = { + { + .callback = dmi_matched, + .ident = "Huawei MACH-WX9", + .matches = { + DMI_MATCH(DMI_SYS_VENDOR, "HUAWEI"), + DMI_MATCH(DMI_PRODUCT_NAME, "MACH-WX9"), + }, + .driver_data = &quirk_battery_reset + }, + { + .callback = dmi_matched, + .ident = "Huawei MateBook X", + .matches = { + DMI_MATCH(DMI_SYS_VENDOR, "HUAWEI"), + DMI_MATCH(DMI_PRODUCT_NAME, "HUAWEI MateBook X") + }, + .driver_data = &quirk_matebook_x + }, + { } +}; + static int huawei_wmi_micmute_led_set(struct led_classdev *led_cdev, enum led_brightness brightness) { @@ -139,6 +200,11 @@ static void huawei_wmi_process_key(struct input_dev *idev, int code) return; } + if (quirks && !quirks->report_brightness && + (key->sw.code == KEY_BRIGHTNESSDOWN || + key->sw.code == KEY_BRIGHTNESSUP)) + return; + sparse_keymap_report_entry(idev, key, 1, true); } @@ -253,6 +319,13 @@ static __init int huawei_wmi_init(void) if (!huawei_wmi) return -ENOMEM; + quirks = &quirk_unknown; + dmi_check_system(huawei_quirks); + if (battery_reset != -1) + quirks->battery_reset = battery_reset; + if (report_brightness != -1) + quirks->report_brightness = report_brightness; + err = platform_driver_register(&huawei_wmi_driver); if (err) goto pdrv_err; From patchwork Fri Sep 20 16:02:36 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ayman Bagabas X-Patchwork-Id: 11154633 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 42D461747 for ; Fri, 20 Sep 2019 16:03:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0D8BD206B6 for ; Fri, 20 Sep 2019 16:03:54 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XcWlsXvC" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387713AbfITQDy (ORCPT ); Fri, 20 Sep 2019 12:03:54 -0400 Received: from mail-yw1-f67.google.com ([209.85.161.67]:43737 "EHLO mail-yw1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387662AbfITQDy (ORCPT ); Fri, 20 Sep 2019 12:03:54 -0400 Received: by mail-yw1-f67.google.com with SMTP id q7so2653070ywe.10; Fri, 20 Sep 2019 09:03:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=SyZ6ZNBYrJa+OSPLPWtsq1fBWNzH7pY34uln4ti1vqg=; b=XcWlsXvCeP22cUPD7hWaHcyOsuYV78QxtnZktNCuyu5OYoktmzeMBvso3pCh2iY5Yq 2CAJqdmoEyJ+V/PP8UoR4uhSJdXkkBR9om4+IexiHzFgODmpwZ2ifW2oIa7mvKLgrwP3 +nflAO37cCadQNIHvQkBNeSzxqhz95t/YScrwVczaaEBH7I9mjnLj+UlEzrSDCwhbpui kajYeu19kI+ioGTh1lyT1wB6hUhp40cUGRn1/eupAON3VD2MMHeDvbAO4ImfxphyGYx+ fsrfUhwTCoHqtDw6Wohs9eSEHAo1sDeCm2c/WBlRiRn3+cwCntyNEZ/Hy2J0gpd7nyCq zZjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=SyZ6ZNBYrJa+OSPLPWtsq1fBWNzH7pY34uln4ti1vqg=; b=a81nVqeTLSmM0ImpZ2ftwJIiNDBhnY30L1QTdfc9z6ADqLA2MS08tSY3i0FFyvxewH KI18cKtlh4h1mGgoIQge6gOXS1PD/7P6FLXSwQbVCHc/plUk9qlijVyFgfQQsmHZOhIL HIvQOW7EHA0aiqRT+eZdZKgx4Um366v5VmutWd6KfxZijz4XJlI30lOguKpKw6zf2eXy JJVmBMHl4T6bmXguCS7WnayhapGIv3dH4MJYEqwie0TVPhy4wIGCGqYtzj46oVQ8A+AR oalTaA6gLH9l5x3hYq5g0ZYLSNIkFC/ElV6dD0zKGUdjeazc5sfOfnik5Ng74RZtvtZF 6bAA== X-Gm-Message-State: APjAAAXfG6HWUseg2wLdI6/Gu4M1m1FLsG1m5TRMqWGycww7fW+ll0zK mkq5knVKcRdYY241ngPgUA== X-Google-Smtp-Source: APXvYqzpl8JUl4sZeSzxllS/jPqvsd5kReAsnk/8I4b2T0sYv4DkJm2Gxfr9sBILb1UeMy4uuGwDsA== X-Received: by 2002:a0d:dd01:: with SMTP id g1mr12575281ywe.309.1568995432025; Fri, 20 Sep 2019 09:03:52 -0700 (PDT) Received: from localhost.localdomain ([12.156.111.130]) by smtp.gmail.com with ESMTPSA id 207sm518086ywu.106.2019.09.20.09.03.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Sep 2019 09:03:51 -0700 (PDT) From: Ayman Bagabas To: Darren Hart , Andy Shevchenko , Sinan Kaya , "Rafael J. Wysocki" , Mauro Carvalho Chehab , Takashi Iwai , Ayman Bagabas , Stuart Hayes , Matan Ziv-Av , Hans de Goede , "Enrico Weigelt, metux IT consult" , Peng Hao , Krzysztof Kozlowski , Mattias Jacobsson <2pi@mok.nu>, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 3/6] platform/x86: huawei-wmi: Implement huawei wmi management Date: Fri, 20 Sep 2019 12:02:36 -0400 Message-Id: <20190920160250.12510-4-ayman.bagabas@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190920160250.12510-1-ayman.bagabas@gmail.com> References: <20190920160250.12510-1-ayman.bagabas@gmail.com> MIME-Version: 1.0 Sender: platform-driver-x86-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: platform-driver-x86@vger.kernel.org Huawei Matebook laptops come with a WMI management interface that can control various aspects of the device. This interface is also found on the old Matebook X released in 2017. Use that to control the mic mute LED. Signed-off-by: Ayman Bagabas --- drivers/platform/x86/huawei-wmi.c | 217 +++++++++++++++++++++++++----- 1 file changed, 180 insertions(+), 37 deletions(-) diff --git a/drivers/platform/x86/huawei-wmi.c b/drivers/platform/x86/huawei-wmi.c index 0e919bf56ad1..41904b1cc284 100644 --- a/drivers/platform/x86/huawei-wmi.c +++ b/drivers/platform/x86/huawei-wmi.c @@ -11,18 +11,35 @@ #include #include #include +#include #include #include /* * Huawei WMI GUIDs */ +#define HWMI_METHOD_GUID "ABBC0F5B-8EA1-11D1-A000-C90629100000" #define HWMI_EVENT_GUID "ABBC0F5C-8EA1-11D1-A000-C90629100000" /* Legacy GUIDs */ #define WMI0_EXPENSIVE_GUID "39142400-C6A3-40fa-BADB-8A2652834100" #define WMI0_EVENT_GUID "59142400-C6A3-40fa-BADB-8A2652834100" +/* HWMI commands */ + +enum { + BATTERY_THRESH_GET = 0x00001103, /* \GBTT */ + BATTERY_THRESH_SET = 0x00001003, /* \SBTT */ + FN_LOCK_GET = 0x00000604, /* \GFRS */ + FN_LOCK_SET = 0x00000704, /* \SFRS */ + MICMUTE_LED_SET = 0x00000b04, /* \SMLS */ +}; + +union hwmi_arg { + u64 cmd; + u8 args[8]; +}; + struct quirk_entry { bool battery_reset; bool ec_micmute; @@ -35,8 +52,8 @@ struct huawei_wmi { struct input_dev *idev[2]; struct led_classdev cdev; struct platform_device *pdev; - acpi_handle handle; - char *acpi_method; + + struct mutex wmi_lock; }; struct huawei_wmi *huawei_wmi; @@ -110,52 +127,168 @@ static const struct dmi_system_id huawei_quirks[] = { { } }; +/* Utils */ + +static int huawei_wmi_call(struct acpi_buffer *in, struct acpi_buffer *out) +{ + acpi_status status; + + mutex_lock(&huawei_wmi->wmi_lock); + status = wmi_evaluate_method(HWMI_METHOD_GUID, 0, 1, in, out); + mutex_unlock(&huawei_wmi->wmi_lock); + if (ACPI_FAILURE(status)) { + dev_err(&huawei_wmi->pdev->dev, "Failed to evaluate wmi method\n"); + return -ENODEV; + } + + return 0; +} + +/* HWMI takes a 64 bit input and returns either a package with 2 buffers, one of + * 4 bytes and the other of 256 bytes, or one buffer of size 0x104 (260) bytes. + * The first 4 bytes are ignored, we ignore the first 4 bytes buffer if we got a + * package, or skip the first 4 if a buffer of 0x104 is used. The first byte of + * the remaining 0x100 sized buffer has the return status of every call. In case + * the return status is non-zero, we return -ENODEV but still copy the returned + * buffer to the given buffer parameter (buf). + */ +static int huawei_wmi_cmd(u64 arg, u8 *buf, size_t buflen) +{ + struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL }; + struct acpi_buffer in; + union acpi_object *obj; + size_t len; + int err, i; + + in.length = sizeof(arg); + in.pointer = &arg; + + /* Some models require calling HWMI twice to execute a command. We evaluate + * HWMI and if we get a non-zero return status we evaluate it again. + */ + for (i = 0; i < 2; i++) { + err = huawei_wmi_call(&in, &out); + if (err) + goto fail_cmd; + + obj = out.pointer; + if (!obj) { + err = -EIO; + goto fail_cmd; + } + + switch (obj->type) { + /* Models that implement both "legacy" and HWMI tend to return a 0x104 + * sized buffer instead of a package of 0x4 and 0x100 buffers. + */ + case ACPI_TYPE_BUFFER: + if (obj->buffer.length == 0x104) { + // Skip the first 4 bytes. + obj->buffer.pointer += 4; + len = 0x100; + } else { + dev_err(&huawei_wmi->pdev->dev, "Bad buffer length, got %d\n", obj->buffer.length); + err = -EIO; + goto fail_cmd; + } + + break; + /* HWMI returns a package with 2 buffer elements, one of 4 bytes and the + * other is 256 bytes. + */ + case ACPI_TYPE_PACKAGE: + if (obj->package.count != 2) { + dev_err(&huawei_wmi->pdev->dev, "Bad package count, got %d\n", obj->package.count); + err = -EIO; + goto fail_cmd; + } + + obj = &obj->package.elements[1]; + if (obj->type != ACPI_TYPE_BUFFER) { + dev_err(&huawei_wmi->pdev->dev, "Bad package element type, got %d\n", obj->type); + err = -EIO; + goto fail_cmd; + } + len = obj->buffer.length; + + break; + /* Shouldn't get here! */ + default: + dev_err(&huawei_wmi->pdev->dev, "Unexpected obj type, got: %d\n", obj->type); + err = -EIO; + goto fail_cmd; + } + + if (!*obj->buffer.pointer) + break; + } + + err = (*obj->buffer.pointer) ? -ENODEV : 0; + + if (buf) { + len = min(buflen, len); + memcpy(buf, obj->buffer.pointer, len); + } + +fail_cmd: + kfree(out.pointer); + return err; +} + +/* LEDs */ + static int huawei_wmi_micmute_led_set(struct led_classdev *led_cdev, enum led_brightness brightness) { - struct huawei_wmi *huawei = dev_get_drvdata(led_cdev->dev->parent); - acpi_status status; - union acpi_object args[3]; - struct acpi_object_list arg_list = { - .pointer = args, - .count = ARRAY_SIZE(args), - }; - - args[0].type = args[1].type = args[2].type = ACPI_TYPE_INTEGER; - args[1].integer.value = 0x04; - - if (strcmp(huawei->acpi_method, "SPIN") == 0) { - args[0].integer.value = 0; - args[2].integer.value = brightness ? 1 : 0; - } else if (strcmp(huawei->acpi_method, "WPIN") == 0) { - args[0].integer.value = 1; - args[2].integer.value = brightness ? 0 : 1; + /* This is a workaround until the "legacy" interface is implemented. */ + if (quirks && quirks->ec_micmute) { + char *acpi_method; + acpi_handle handle; + acpi_status status; + union acpi_object args[3]; + struct acpi_object_list arg_list = { + .pointer = args, + .count = ARRAY_SIZE(args), + }; + + handle = ec_get_handle(); + if (!handle) + return -ENODEV; + + args[0].type = args[1].type = args[2].type = ACPI_TYPE_INTEGER; + args[1].integer.value = 0x04; + + if (acpi_has_method(handle, "SPIN")) { + acpi_method = "SPIN"; + args[0].integer.value = 0; + args[2].integer.value = brightness ? 1 : 0; + } else if (acpi_has_method(handle, "WPIN")) { + acpi_method = "WPIN"; + args[0].integer.value = 1; + args[2].integer.value = brightness ? 0 : 1; + } else { + return -ENODEV; + } + + status = acpi_evaluate_object(handle, acpi_method, &arg_list, NULL); + if (ACPI_FAILURE(status)) + return -ENODEV; + + return 0; } else { - return -EINVAL; - } + union hwmi_arg arg; - status = acpi_evaluate_object(huawei->handle, huawei->acpi_method, &arg_list, NULL); - if (ACPI_FAILURE(status)) - return -ENXIO; + arg.cmd = MICMUTE_LED_SET; + arg.args[2] = brightness; - return 0; + return huawei_wmi_cmd(arg.cmd, NULL, 0); + } } static void huawei_wmi_leds_setup(struct device *dev) { struct huawei_wmi *huawei = dev_get_drvdata(dev); - huawei->handle = ec_get_handle(); - if (!huawei->handle) - return; - - if (acpi_has_method(huawei->handle, "SPIN")) - huawei->acpi_method = "SPIN"; - else if (acpi_has_method(huawei->handle, "WPIN")) - huawei->acpi_method = "WPIN"; - else - return; - huawei->cdev.name = "platform::micmute"; huawei->cdev.max_brightness = 1; huawei->cdev.brightness_set_blocking = &huawei_wmi_micmute_led_set; @@ -258,6 +391,7 @@ static void huawei_wmi_input_exit(struct device *dev, const char *guid) static const struct wmi_device_id huawei_wmi_events_id_table[] = { { .guid_string = WMI0_EVENT_GUID }, + { .guid_string = HWMI_EVENT_GUID }, { } }; @@ -284,7 +418,12 @@ static int huawei_wmi_probe(struct platform_device *pdev) guid++; } - huawei_wmi_leds_setup(&pdev->dev); + if (wmi_has_guid(HWMI_METHOD_GUID)) { + mutex_init(&huawei_wmi->wmi_lock); + + huawei_wmi_leds_setup(&pdev->dev); + } + return 0; } @@ -299,6 +438,9 @@ static int huawei_wmi_remove(struct platform_device *pdev) guid++; } + if (wmi_has_guid(HWMI_METHOD_GUID)) { + } + return 0; } @@ -354,6 +496,7 @@ static __exit void huawei_wmi_exit(void) module_init(huawei_wmi_init); module_exit(huawei_wmi_exit); +MODULE_ALIAS("wmi:"HWMI_METHOD_GUID); MODULE_DEVICE_TABLE(wmi, huawei_wmi_events_id_table); MODULE_AUTHOR("Ayman Bagabas "); MODULE_DESCRIPTION("Huawei WMI laptop extras driver"); From patchwork Fri Sep 20 16:02:37 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ayman Bagabas X-Patchwork-Id: 11154635 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 11C1216B1 for ; Fri, 20 Sep 2019 16:04:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D054E206B6 for ; Fri, 20 Sep 2019 16:04:05 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Q+yF2qJu" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387676AbfITQEF (ORCPT ); Fri, 20 Sep 2019 12:04:05 -0400 Received: from mail-yw1-f66.google.com ([209.85.161.66]:46285 "EHLO mail-yw1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387662AbfITQEF (ORCPT ); Fri, 20 Sep 2019 12:04:05 -0400 Received: by mail-yw1-f66.google.com with SMTP id 201so2643022ywn.13; Fri, 20 Sep 2019 09:04:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=4z8I/CXv1oEWkgFQw4hQrn5Fcq+os8zrDkpZFa+UqdA=; b=Q+yF2qJusMeBNSWw+Ly61QaiRxD3HniciFNWR9iRt7nqEJqGBliclCU6ZrPcSmXs/v 3V8b/wW4H4LQeORg5RLEz/Gani24BvZusr5j+L51O0EMhyyC5CKm0n6FCQXUwR6GwEM3 pWqB1xY7Lo3hATZk2F7LFihs8sgPipGistYOcNihgdWo6L5t++lTjULxc5Jh51IephKF yEmdALB+eewNA0T/TlR+sbVv/Q7DtBO6Y/vWeqnL93zQJLwVwMnhfjZlugj3EP0o3+6g l2JuhShlFL6AZwZpXd+0MHyuovR5ox4Pw4DT/2EOqabD++kFgfkQ6xBDVJnOAy7JhuuD zYkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4z8I/CXv1oEWkgFQw4hQrn5Fcq+os8zrDkpZFa+UqdA=; b=BrM8SUmKwap9z8aCBndzU2mnuG54zE72fQj6kTk+nwsVuvsDJpxFTLISRXYa0tZXQK 7aS+LGlZlUnIM1t/r3Uo9MD3zajV/+jeak+6wr/oSzKXQjyYdgFUHineD9PtkKWxRPy1 H9Pdi+vPFy83uFDss0C9xE278BV346NkAfN7kmzlnX7boWn11m7EnQW/EsFAjK0WCqwL y6YzLAe/32Yk2iDGdXiL1S4ym5ufF7BJ+BdDWwLH1OdWvCqG1JRBtJCU+zyLxGBUan1Q 2Qq9/pwdA87fb3Kh6XuWq+kgV6QK9oD2SYJe+g6/VhuzV7/qs0M4p8vGaSbin0j/ptJR qOqg== X-Gm-Message-State: APjAAAXyRJCRzTVtQ50qY4r9J7Ljw1kJtaHj9oCZZJ/UXMtseqI53cPS XS8AASOaQQFu/fPX3gaYgQ== X-Google-Smtp-Source: APXvYqwYC8UODFqvRVZpZK+sJLRAQ5xdLfARGCQ+RnZuqYOWXdqk97v5qDjguvexXO5N6Bslio9JAw== X-Received: by 2002:a81:6043:: with SMTP id u64mr13850894ywb.104.1568995443860; Fri, 20 Sep 2019 09:04:03 -0700 (PDT) Received: from localhost.localdomain ([12.156.111.130]) by smtp.gmail.com with ESMTPSA id 207sm518086ywu.106.2019.09.20.09.04.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Sep 2019 09:04:02 -0700 (PDT) From: Ayman Bagabas To: Darren Hart , Andy Shevchenko , Mauro Carvalho Chehab , "Rafael J. Wysocki" , Sinan Kaya , Takashi Iwai , Ayman Bagabas , Stuart Hayes , Matan Ziv-Av , "Enrico Weigelt, metux IT consult" , Hans de Goede , Peng Hao , Krzysztof Kozlowski , Mattias Jacobsson <2pi@mok.nu>, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 4/6] platform/x86: huawei-wmi: Add battery charging thresholds Date: Fri, 20 Sep 2019 12:02:37 -0400 Message-Id: <20190920160250.12510-5-ayman.bagabas@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190920160250.12510-1-ayman.bagabas@gmail.com> References: <20190920160250.12510-1-ayman.bagabas@gmail.com> MIME-Version: 1.0 Sender: platform-driver-x86-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: platform-driver-x86@vger.kernel.org Control battery charge thresholds through the battery API and driver's attributes. Setting battery charging thresholds can introduce a race condition with MACH-WX9 where two or more threads are trying to read/write values from/to EC memory. Signed-off-by: Ayman Bagabas Reported-by: kbuild test robot --- drivers/platform/x86/Kconfig | 1 + drivers/platform/x86/huawei-wmi.c | 212 ++++++++++++++++++++++++++++++ 2 files changed, 213 insertions(+) diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig index 61bf180d25c7..0659589e46bb 100644 --- a/drivers/platform/x86/Kconfig +++ b/drivers/platform/x86/Kconfig @@ -1305,6 +1305,7 @@ config INTEL_ATOMISP2_PM config HUAWEI_WMI tristate "Huawei WMI laptop extras driver" + depends on ACPI_BATTERY depends on ACPI_WMI depends on INPUT select INPUT_SPARSEKMAP diff --git a/drivers/platform/x86/huawei-wmi.c b/drivers/platform/x86/huawei-wmi.c index 41904b1cc284..e577786e6306 100644 --- a/drivers/platform/x86/huawei-wmi.c +++ b/drivers/platform/x86/huawei-wmi.c @@ -6,6 +6,7 @@ */ #include +#include #include #include #include @@ -13,7 +14,10 @@ #include #include #include +#include +#include #include +#include /* * Huawei WMI GUIDs @@ -49,10 +53,13 @@ struct quirk_entry { static struct quirk_entry *quirks; struct huawei_wmi { + bool battery_available; + struct input_dev *idev[2]; struct led_classdev cdev; struct platform_device *pdev; + struct mutex battery_lock; struct mutex wmi_lock; }; @@ -300,6 +307,208 @@ static void huawei_wmi_leds_setup(struct device *dev) devm_led_classdev_register(dev, &huawei->cdev); } +/* Battery protection */ + +static int huawei_wmi_battery_get(int *start, int *end) +{ + u8 ret[0x100]; + int err, i; + + err = huawei_wmi_cmd(BATTERY_THRESH_GET, ret, 0x100); + if (err) + return err; + + /* Find the last two non-zero values. Return status is ignored. */ + i = 0x100; + do { + if (start) + *start = ret[i-1]; + if (end) + *end = ret[i]; + } while (i > 2 && !ret[i--]); + + return 0; +} + +static int huawei_wmi_battery_set(int start, int end) +{ + union hwmi_arg arg; + int err; + + if (start < 0 || end > 100) + return -EINVAL; + + arg.cmd = BATTERY_THRESH_SET; + arg.args[2] = start; + arg.args[3] = end; + + /* This is an edge case were some models turn battery protection + * off without changing their thresholds values. We clear the + * values before turning off protection. Sometimes we need a sleep delay to + * make sure these values make their way to EC memory. + */ + if (quirks && quirks->battery_reset && start == 0 && end == 100) { + err = huawei_wmi_battery_set(0, 0); + if (err) + return err; + + msleep(1000); + } + + err = huawei_wmi_cmd(arg.cmd, NULL, 0); + + return err; +} + +static ssize_t charge_control_start_threshold_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + int err, start; + + err = huawei_wmi_battery_get(&start, NULL); + if (err) + return err; + + return sprintf(buf, "%d\n", start); +} + +static ssize_t charge_control_end_threshold_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + int err, end; + + err = huawei_wmi_battery_get(NULL, &end); + if (err) + return err; + + return sprintf(buf, "%d\n", end); +} + +static ssize_t charge_control_thresholds_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + int err, start, end; + + err = huawei_wmi_battery_get(&start, &end); + if (err) + return err; + + return sprintf(buf, "%d %d\n", start, end); +} + +static ssize_t charge_control_start_threshold_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t size) +{ + int err, start, end; + + err = huawei_wmi_battery_get(NULL, &end); + if (err) + return err; + + if (sscanf(buf, "%d", &start) != 1) + return -EINVAL; + + err = huawei_wmi_battery_set(start, end); + if (err) + return err; + + return size; +} + +static ssize_t charge_control_end_threshold_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t size) +{ + int err, start, end; + + err = huawei_wmi_battery_get(&start, NULL); + if (err) + return err; + + if (sscanf(buf, "%d", &end) != 1) + return -EINVAL; + + err = huawei_wmi_battery_set(start, end); + if (err) + return err; + + return size; +} + +static ssize_t charge_control_thresholds_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t size) +{ + int err, start, end; + + if (sscanf(buf, "%d %d", &start, &end) != 2) + return -EINVAL; + + err = huawei_wmi_battery_set(start, end); + if (err) + return err; + + return size; +} + +static DEVICE_ATTR_RW(charge_control_start_threshold); +static DEVICE_ATTR_RW(charge_control_end_threshold); +static DEVICE_ATTR_RW(charge_control_thresholds); + +static int huawei_wmi_battery_add(struct power_supply *battery) +{ + /* Huawei laptops come with one battery only */ + if (strcmp(battery->desc->name, "BAT") != 1) + return -ENODEV; + + device_create_file(&battery->dev, &dev_attr_charge_control_start_threshold); + device_create_file(&battery->dev, &dev_attr_charge_control_end_threshold); + + return 0; +} + +static int huawei_wmi_battery_remove(struct power_supply *battery) +{ + device_remove_file(&battery->dev, &dev_attr_charge_control_start_threshold); + device_remove_file(&battery->dev, &dev_attr_charge_control_end_threshold); + + return 0; +} + +static struct acpi_battery_hook huawei_wmi_battery_hook = { + .add_battery = huawei_wmi_battery_add, + .remove_battery = huawei_wmi_battery_remove, + .name = "Huawei Battery Extension" +}; + +static void huawei_wmi_battery_setup(struct device *dev) +{ + struct huawei_wmi *huawei = dev_get_drvdata(dev); + + huawei->battery_available = true; + if (huawei_wmi_battery_get(NULL, NULL)) { + huawei->battery_available = false; + return; + } + + battery_hook_register(&huawei_wmi_battery_hook); + device_create_file(dev, &dev_attr_charge_control_thresholds); +} + +static void huawei_wmi_battery_exit(struct device *dev) +{ + struct huawei_wmi *huawei = dev_get_drvdata(dev); + + if (huawei->battery_available) { + battery_hook_unregister(&huawei_wmi_battery_hook); + device_remove_file(dev, &dev_attr_charge_control_thresholds); + } +} + /* Input */ static void huawei_wmi_process_key(struct input_dev *idev, int code) @@ -420,8 +629,10 @@ static int huawei_wmi_probe(struct platform_device *pdev) if (wmi_has_guid(HWMI_METHOD_GUID)) { mutex_init(&huawei_wmi->wmi_lock); + mutex_init(&huawei_wmi->battery_lock); huawei_wmi_leds_setup(&pdev->dev); + huawei_wmi_battery_setup(&pdev->dev); } return 0; @@ -439,6 +650,7 @@ static int huawei_wmi_remove(struct platform_device *pdev) } if (wmi_has_guid(HWMI_METHOD_GUID)) { + huawei_wmi_battery_exit(&pdev->dev); } return 0; From patchwork Fri Sep 20 16:02:38 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ayman Bagabas X-Patchwork-Id: 11154637 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4DFA717E6 for ; Fri, 20 Sep 2019 16:04:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 203A6206B6 for ; Fri, 20 Sep 2019 16:04:17 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EL/wudyG" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388531AbfITQEQ (ORCPT ); Fri, 20 Sep 2019 12:04:16 -0400 Received: from mail-yb1-f196.google.com ([209.85.219.196]:33475 "EHLO mail-yb1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387662AbfITQEQ (ORCPT ); Fri, 20 Sep 2019 12:04:16 -0400 Received: by mail-yb1-f196.google.com with SMTP id z7so2835293ybg.0; Fri, 20 Sep 2019 09:04:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=hFTvBwgfUH9STxKzxO5lst19eGkZ0SUa5KmMQ8gPWJc=; b=EL/wudyG1wF378lU33lu1PQIltktXtNyL93l03GjpKP87yRy37glIbC48zsn9jJGfL jdhy5UtGH1lElgPlav6z1ihzO4Id41bvhtVyK5Lq4xZSriWCqZZ8rn4b9rqqbZuRG1rV VDoaRfKvUdVWrYmNW5Yw8pbd4EKW8bi4jv07ebpgL7sOwoPiYIF/JywQuJNsGv2nOvt3 MznZ+c9SDjQiQ/8iJOcnLHXW77iXuR5vz9GKENA9QwKhaasTNxarQ52HRV+dPk5NemgN ZEzRf9nWtlZB0Q39c+8xcu2qnsIIjbmcRWktu0p12q8nyT5lFE99nv71AnMKf9Pv6o4D A47Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=hFTvBwgfUH9STxKzxO5lst19eGkZ0SUa5KmMQ8gPWJc=; b=F8JucA+RMsdC6QiPHcbCLwCNNqEXaQ/3vTebD6JxXFLm365xULsuM2Feukqrl5jivP NXItNkOwYop9K1fs0P2nZCtFcosVn6VFFonzBXHi02emVBkriFEgs45zdSTjdT5nMPoy YY9pVWcGvrYI4+Bs0XnYn/EOz0x+uTcxywVVtGqe+/dJ04dTwUbSksAQUocm8GI+75GC 75b9oTt0g2yTlYqS6AszmZpjEyD5aAg/9Goj4GzdkHNQtLYSGeQvpsQyCP2g+q0wQ+6k CAZd0VcAvtcqY/Ml5s5EvLwcbVAyGdwbxiQKu6dNYaEoG7bfVprEbAXcxlHFw99gV1xl /+Pg== X-Gm-Message-State: APjAAAVgf2xDcDVSxcvS605l8uWaHwqOXphi16bUExcz3nnWL1Wy6iyH HQK/gZ+Umy96/Cu6hijDdg== X-Google-Smtp-Source: APXvYqxYbKo2+znHlegpWlJsOORlZCZyYD3/Jc+vZranlNmoAeCVjkYNgiX4STkLdkOP6o+CRXxfvw== X-Received: by 2002:a25:7e81:: with SMTP id z123mr10839615ybc.230.1568995455381; Fri, 20 Sep 2019 09:04:15 -0700 (PDT) Received: from localhost.localdomain ([12.156.111.130]) by smtp.gmail.com with ESMTPSA id 207sm518086ywu.106.2019.09.20.09.04.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Sep 2019 09:04:14 -0700 (PDT) From: Ayman Bagabas To: Darren Hart , Andy Shevchenko , "Rafael J. Wysocki" , Mauro Carvalho Chehab , Sinan Kaya , Takashi Iwai , Ayman Bagabas , Stuart Hayes , Matan Ziv-Av , Hans de Goede , "Enrico Weigelt, metux IT consult" , Peng Hao , Krzysztof Kozlowski , Mattias Jacobsson <2pi@mok.nu>, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 5/6] platform/x86: huawei-wmi: Add fn-lock support Date: Fri, 20 Sep 2019 12:02:38 -0400 Message-Id: <20190920160250.12510-6-ayman.bagabas@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190920160250.12510-1-ayman.bagabas@gmail.com> References: <20190920160250.12510-1-ayman.bagabas@gmail.com> MIME-Version: 1.0 Sender: platform-driver-x86-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: platform-driver-x86@vger.kernel.org Huawei Matebook laptops uses Fn key and toggle to access F1-F12 keys. Along with that, there is this feature called fn-lock that inverts the behavior of this Fn key and the F1-F12 row. Signed-off-by: Ayman Bagabas --- drivers/platform/x86/huawei-wmi.c | 85 +++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/drivers/platform/x86/huawei-wmi.c b/drivers/platform/x86/huawei-wmi.c index e577786e6306..56697d49377d 100644 --- a/drivers/platform/x86/huawei-wmi.c +++ b/drivers/platform/x86/huawei-wmi.c @@ -54,6 +54,7 @@ static struct quirk_entry *quirks; struct huawei_wmi { bool battery_available; + bool fn_lock_available; struct input_dev *idev[2]; struct led_classdev cdev; @@ -509,6 +510,88 @@ static void huawei_wmi_battery_exit(struct device *dev) } } +/* Fn lock */ + +static int huawei_wmi_fn_lock_get(int *on) +{ + u8 ret[0x100] = { 0 }; + int err, i; + + err = huawei_wmi_cmd(FN_LOCK_GET, ret, 0x100); + if (err) + return err; + + /* Find the first non-zero value. Return status is ignored. */ + i = 1; + do { + if (on) + *on = ret[i] - 1; // -1 undefined, 0 off, 1 on. + } while (i < 0x100 && !ret[i++]); + + return 0; +} + +static int huawei_wmi_fn_lock_set(int on) +{ + union hwmi_arg arg; + + arg.cmd = FN_LOCK_SET; + arg.args[2] = on + 1; // 0 undefined, 1 off, 2 on. + + return huawei_wmi_cmd(arg.cmd, NULL, 0); +} + +static ssize_t fn_lock_state_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + int err, on; + + err = huawei_wmi_fn_lock_get(&on); + if (err) + return err; + + return sprintf(buf, "%d\n", on); +} + +static ssize_t fn_lock_state_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t size) +{ + int on, err; + + if (kstrtoint(buf, 10, &on) || + on < 0 || on > 1) + return -EINVAL; + + err = huawei_wmi_fn_lock_set(on); + if (err) + return err; + + return size; +} + +static DEVICE_ATTR_RW(fn_lock_state); + +static void huawei_wmi_fn_lock_setup(struct device *dev) +{ + struct huawei_wmi *huawei = dev_get_drvdata(dev); + + huawei->fn_lock_available = true; + if (huawei_wmi_fn_lock_get(NULL)) { + huawei->fn_lock_available = false; + return; + } + + device_create_file(dev, &dev_attr_fn_lock_state); +} + +static void huawei_wmi_fn_lock_exit(struct device *dev) +{ + if (huawei_wmi->fn_lock_available) + device_remove_file(dev, &dev_attr_fn_lock_state); +} + /* Input */ static void huawei_wmi_process_key(struct input_dev *idev, int code) @@ -632,6 +715,7 @@ static int huawei_wmi_probe(struct platform_device *pdev) mutex_init(&huawei_wmi->battery_lock); huawei_wmi_leds_setup(&pdev->dev); + huawei_wmi_fn_lock_setup(&pdev->dev); huawei_wmi_battery_setup(&pdev->dev); } @@ -651,6 +735,7 @@ static int huawei_wmi_remove(struct platform_device *pdev) if (wmi_has_guid(HWMI_METHOD_GUID)) { huawei_wmi_battery_exit(&pdev->dev); + huawei_wmi_fn_lock_exit(&pdev->dev); } return 0; From patchwork Fri Sep 20 16:02:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ayman Bagabas X-Patchwork-Id: 11154639 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7205416B1 for ; Fri, 20 Sep 2019 16:04:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 437B5206B6 for ; Fri, 20 Sep 2019 16:04:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="qmCFDsvO" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388810AbfITQE1 (ORCPT ); Fri, 20 Sep 2019 12:04:27 -0400 Received: from mail-yb1-f194.google.com ([209.85.219.194]:35716 "EHLO mail-yb1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387662AbfITQE1 (ORCPT ); Fri, 20 Sep 2019 12:04:27 -0400 Received: by mail-yb1-f194.google.com with SMTP id b128so2828313ybh.2; Fri, 20 Sep 2019 09:04:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=medjA5JOhuw+KXtE21Z9wSEAHcoK7CM97EXuoE1ZTMk=; b=qmCFDsvOe/YgB+NGjcoewLJKldrjcEn2rXJ75aqtzkNrNfTkfVKuTLzdAQxunKHyXG 1Lx/1ohULnzmjrtoVF/AmLNCECLJSXFTVfahFr7+AQSvid3JcsXgDVCNHnpilZIJvR79 EEqalIiXSJif2RCgkpr7jn5gejTkqgIzZSIjbCujmI6ZjN2zTCJkUy2j/QZWI20/Rn2P JlCsHSytzlePCQc+tk/leX4MS/qbh/zKjHT62kbxBlz6dc+0m56XGpsWDBuAjRRMmGI7 2qjKDJszIY+HrSfoC5UcO/jLss7Z/0JwH0GnrqJH4VF1bOV70KbrbG+C5JqNM7LF5uDR jbPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=medjA5JOhuw+KXtE21Z9wSEAHcoK7CM97EXuoE1ZTMk=; b=DoiR7sY+py6T6EayXjf+tES9+y8XFszjmGIWcsVS4sm65QZsufqgl2C0ZSq61z8oRC VS/uLIMz/in9UjzgeX1Khcc3AMVVh9ONs/MT9VEs786XvEi8FA4dG//LPcln+SlykME5 xUZqklv9ldgIcXiuMjtM09WLnB2FN0aKml6icZPDLNBKLEdMbjs/CHftqpNpDYEdmZTv JSBL7bIyhvDa47H7krDkx4rCp50rEH+5gcmpw3V7BPZJFf3Mrex1wFL4rvLG3S1/9L6E 0tpYhxJlKM1J5+ng8lfoCYCXQeqNSnUDzLmxVq2SSRgNnIEjs+2h4ecwSYPup7/XnekI A2qw== X-Gm-Message-State: APjAAAXtdHZdo99a2JSeJiCn932BHe3DZw+NqM9UIKnaqSduzDtVMHoV NdQivcYRXE9xGPoiVB6rbQ== X-Google-Smtp-Source: APXvYqxlS66AQHJOgZy8g1lRD1J9PxCvfi3Ck8LLWIhdVAWblCpttWFegVV0sYSwTJJEAH2mE9HPeg== X-Received: by 2002:a25:42cf:: with SMTP id p198mr1761945yba.147.1568995466140; Fri, 20 Sep 2019 09:04:26 -0700 (PDT) Received: from localhost.localdomain ([12.156.111.130]) by smtp.gmail.com with ESMTPSA id 207sm518086ywu.106.2019.09.20.09.04.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Sep 2019 09:04:25 -0700 (PDT) From: Ayman Bagabas To: Darren Hart , Andy Shevchenko , Mauro Carvalho Chehab , Sinan Kaya , "Rafael J. Wysocki" , Takashi Iwai , Ayman Bagabas , Stuart Hayes , Matan Ziv-Av , "Enrico Weigelt, metux IT consult" , Hans de Goede , Peng Hao , Krzysztof Kozlowski , Mattias Jacobsson <2pi@mok.nu>, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 6/6] platform/x86: huawei-wmi: Add debugfs support Date: Fri, 20 Sep 2019 12:02:39 -0400 Message-Id: <20190920160250.12510-7-ayman.bagabas@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190920160250.12510-1-ayman.bagabas@gmail.com> References: <20190920160250.12510-1-ayman.bagabas@gmail.com> MIME-Version: 1.0 Sender: platform-driver-x86-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: platform-driver-x86@vger.kernel.org Add a debugfs interface that can be used to call the WMI management interface function if available. Signed-off-by: Ayman Bagabas --- drivers/platform/x86/huawei-wmi.c | 97 +++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/drivers/platform/x86/huawei-wmi.c b/drivers/platform/x86/huawei-wmi.c index 56697d49377d..5efb8d1bbda7 100644 --- a/drivers/platform/x86/huawei-wmi.c +++ b/drivers/platform/x86/huawei-wmi.c @@ -6,6 +6,7 @@ */ #include +#include #include #include #include @@ -52,10 +53,16 @@ struct quirk_entry { static struct quirk_entry *quirks; +struct huawei_wmi_debug { + struct dentry *root; + u64 arg; +}; + struct huawei_wmi { bool battery_available; bool fn_lock_available; + struct huawei_wmi_debug debug; struct input_dev *idev[2]; struct led_classdev cdev; struct platform_device *pdev; @@ -592,6 +599,94 @@ static void huawei_wmi_fn_lock_exit(struct device *dev) device_remove_file(dev, &dev_attr_fn_lock_state); } +/* debugfs */ + +static void huawei_wmi_debugfs_call_dump(struct seq_file *m, void *data, + union acpi_object *obj) +{ + struct huawei_wmi *huawei = m->private; + int i; + + switch (obj->type) { + case ACPI_TYPE_INTEGER: + seq_printf(m, "0x%llx", obj->integer.value); + break; + case ACPI_TYPE_STRING: + seq_printf(m, "\"%*s\"", obj->string.length, obj->string.pointer); + break; + case ACPI_TYPE_BUFFER: + seq_puts(m, "{"); + for (i = 0; i < obj->buffer.length; i++) { + seq_printf(m, "0x%02x", obj->buffer.pointer[i]); + if (i < obj->buffer.length - 1) + seq_puts(m, ","); + } + seq_puts(m, "}"); + break; + case ACPI_TYPE_PACKAGE: + seq_puts(m, "["); + for (i = 0; i < obj->package.count; i++) { + huawei_wmi_debugfs_call_dump(m, huawei, &obj->package.elements[i]); + if (i < obj->package.count - 1) + seq_puts(m, ","); + } + seq_puts(m, "]"); + break; + default: + dev_err(&huawei->pdev->dev, "Unexpected obj type, got %d\n", obj->type); + return; + } +} + +static int huawei_wmi_debugfs_call_show(struct seq_file *m, void *data) +{ + struct huawei_wmi *huawei = m->private; + struct acpi_buffer out = { ACPI_ALLOCATE_BUFFER, NULL }; + struct acpi_buffer in; + union acpi_object *obj; + int err; + + in.length = sizeof(u64); + in.pointer = &huawei->debug.arg; + + err = huawei_wmi_call(&in, &out); + if (err) + return err; + + obj = out.pointer; + if (!obj) { + err = -EIO; + goto fail_debugfs_call; + } + + huawei_wmi_debugfs_call_dump(m, huawei, obj); + +fail_debugfs_call: + kfree(out.pointer); + return err; +} + +DEFINE_SHOW_ATTRIBUTE(huawei_wmi_debugfs_call); + +static void huawei_wmi_debugfs_setup(struct device *dev) +{ + struct huawei_wmi *huawei = dev_get_drvdata(dev); + + huawei->debug.root = debugfs_create_dir("huawei-wmi", NULL); + + debugfs_create_x64("arg", 0644, huawei->debug.root, + &huawei->debug.arg); + debugfs_create_file("call", 0400, + huawei->debug.root, huawei, &huawei_wmi_debugfs_call_fops); +} + +static void huawei_wmi_debugfs_exit(struct device *dev) +{ + struct huawei_wmi *huawei = dev_get_drvdata(dev); + + debugfs_remove_recursive(huawei->debug.root); +} + /* Input */ static void huawei_wmi_process_key(struct input_dev *idev, int code) @@ -717,6 +812,7 @@ static int huawei_wmi_probe(struct platform_device *pdev) huawei_wmi_leds_setup(&pdev->dev); huawei_wmi_fn_lock_setup(&pdev->dev); huawei_wmi_battery_setup(&pdev->dev); + huawei_wmi_debugfs_setup(&pdev->dev); } return 0; @@ -734,6 +830,7 @@ static int huawei_wmi_remove(struct platform_device *pdev) } if (wmi_has_guid(HWMI_METHOD_GUID)) { + huawei_wmi_debugfs_exit(&pdev->dev); huawei_wmi_battery_exit(&pdev->dev); huawei_wmi_fn_lock_exit(&pdev->dev); }