From bbc29f39fc2e2e62a5d7b1b4eabfd9311a0807c0 Mon Sep 17 00:00:00 2001 From: Christoffer Sandberg Date: Fri, 4 Dec 2020 12:23:39 +0100 Subject: [PATCH] clevo_interfaces: Add decoupled clevo_wmi interface --- src/clevo_wmi.c | 165 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 src/clevo_wmi.c diff --git a/src/clevo_wmi.c b/src/clevo_wmi.c new file mode 100644 index 0000000..e86a3d3 --- /dev/null +++ b/src/clevo_wmi.c @@ -0,0 +1,165 @@ +/* +* clevo_wmi.h +* +* Copyright (C) 2020 TUXEDO Computers GmbH +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt +#include +#include +#include +#include +#include "clevo_interfaces.h" + +#define CLEVO_WMI_EVENT_GUID "ABBC0F6B-8EA1-11D1-00A0-C90629100000" +#define CLEVO_WMI_EMAIL_GUID "ABBC0F6C-8EA1-11D1-00A0-C90629100000" +#define CLEVO_WMI_METHOD_GUID "ABBC0F6D-8EA1-11D1-00A0-C90629100000" + +static int clevo_wmi_evaluate(u32 wmi_method_id, u32 wmi_arg, u32 *result) +{ + struct acpi_buffer acpi_buffer_in = { (acpi_size)sizeof(wmi_arg), + &wmi_arg }; + struct acpi_buffer acpi_buffer_out = { ACPI_ALLOCATE_BUFFER, NULL }; + union acpi_object *acpi_result; + acpi_status status_acpi; + u32 return_status = 0; + + status_acpi = + wmi_evaluate_method(CLEVO_WMI_METHOD_GUID, 0x00, wmi_method_id, + &acpi_buffer_in, &acpi_buffer_out); + + if (unlikely(ACPI_FAILURE(status_acpi))) { + pr_err("failed to evaluate wmi method\n"); + return -EIO; + } + + acpi_result = (union acpi_object *)acpi_buffer_out.pointer; + if (!acpi_result) { + pr_err("failed to evaluate WMI method\n"); + return_status = -1; + } else { + if (acpi_result->type == ACPI_TYPE_INTEGER) { + if (!IS_ERR_OR_NULL(result)) { + *result = (u32)acpi_result->integer.value; + pr_debug( + "evaluate wmi cmd: %0#4x arg: %0#10x\n", + wmi_method_id, wmi_arg); + } + } else { + pr_err("unknown output from wmi method\n"); + return_status = -EIO; + } + } + + kfree(acpi_result); + + return return_status; +} + +u32 clevo_wmi_interface_method_call(u8 cmd, u32 arg, u32 *result_value) +{ + return clevo_wmi_evaluate(cmd, arg, result_value); +} + +struct clevo_interface_t clevo_wmi_interface = { + .string_id = "clevo_wmi", + .method_call = clevo_wmi_interface_method_call, +}; + +#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 3, 0) +static int clevo_wmi_probe(struct wmi_device *wdev) +#else +static int clevo_wmi_probe(struct wmi_device *wdev, const void *dummy_context) +#endif +{ + u32 status, ret; + + pr_debug("clevo_wmi driver probe\n"); + + if (!wmi_has_guid(CLEVO_WMI_EVENT_GUID)) { + pr_debug("probe: Clevo event guid missing\n"); + return -ENODEV; + } + + if (!wmi_has_guid(CLEVO_WMI_METHOD_GUID)) { + pr_debug("probe: Clevo method guid missing\n"); + return -ENODEV; + } + + // Since the WMI GUIDs aren't unique let's (at least) + // check the return of some "known existing general" method + status = clevo_wmi_evaluate(0x52, 0, &ret); + if (status < 0) { + pr_debug("probe: Clevo GUIDs present but method call failed\n"); + return -ENODEV; + } + if (ret == 0xffffffff) { + pr_debug( + "probe: Clevo GUIDs present but method returned unexpected value\n"); + return -ENODEV; + } + + // Initiate clevo keyboard, if not already loaded by other interface driver + clevo_keyboard_init(); + + // Add this interface + clevo_keyboard_add_interface(&clevo_wmi_interface); + + pr_info("clevo_wmi: interface initialized\n"); + + return 0; +} + +static int clevo_wmi_remove(struct wmi_device *wdev) +{ + pr_debug("clevo_wmi driver remove\n"); + return 0; +} + +static void clevo_wmi_notify(struct wmi_device *wdev, union acpi_object *dummy) +{ + u32 event_value; + clevo_wmi_evaluate(0x01, 0, &event_value); + pr_debug("clevo_wmi notify\n"); + if (!IS_ERR_OR_NULL(clevo_wmi_interface.event_callb)) { + // Execute registered callback + clevo_wmi_interface.event_callb(event_value); + } +} + +static const struct wmi_device_id clevo_wmi_device_ids[] = { + // Listing one should be enough, for a driver that "takes care of all anyways" + // also prevents probe (and handling) per "device" + { .guid_string = CLEVO_WMI_EVENT_GUID }, + { "" } +}; + +static struct wmi_driver clevo_wmi_driver = { + .driver = { .name = "clevo_wmi", .owner = THIS_MODULE }, + .id_table = clevo_wmi_device_ids, + .probe = clevo_wmi_probe, + .remove = clevo_wmi_remove, + .notify = clevo_wmi_notify, +}; + +module_wmi_driver(clevo_wmi_driver); + +MODULE_AUTHOR("TUXEDO Computers GmbH "); +MODULE_DESCRIPTION("Driver for Clevo WMI interface"); +MODULE_VERSION("0.0.1"); +MODULE_LICENSE("GPL"); + +MODULE_DEVICE_TABLE(wmi, clevo_wmi_device_ids);