1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Linux hotkey driver for Uniwill notebooks.
4 *
5 * Special thanks go to Pőcze Barnabás, Christoffer Sandberg and Werner Sembach
6 * for supporting the development of this driver either through prior work or
7 * by answering questions regarding the underlying WMI interface.
8 *
9 * Copyright (C) 2025 Armin Wolf <W_Armin@gmx.de>
10 */
11
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14 #include <linux/acpi.h>
15 #include <linux/device.h>
16 #include <linux/init.h>
17 #include <linux/notifier.h>
18 #include <linux/printk.h>
19 #include <linux/types.h>
20 #include <linux/wmi.h>
21
22 #include "uniwill-wmi.h"
23
24 #define DRIVER_NAME "uniwill-wmi"
25 #define UNIWILL_EVENT_GUID "ABBC0F72-8EA1-11D1-00A0-C90629100000"
26
27 static BLOCKING_NOTIFIER_HEAD(uniwill_wmi_chain_head);
28
devm_uniwill_wmi_unregister_notifier(void * data)29 static void devm_uniwill_wmi_unregister_notifier(void *data)
30 {
31 struct notifier_block *nb = data;
32
33 blocking_notifier_chain_unregister(&uniwill_wmi_chain_head, nb);
34 }
35
devm_uniwill_wmi_register_notifier(struct device * dev,struct notifier_block * nb)36 int devm_uniwill_wmi_register_notifier(struct device *dev, struct notifier_block *nb)
37 {
38 int ret;
39
40 ret = blocking_notifier_chain_register(&uniwill_wmi_chain_head, nb);
41 if (ret < 0)
42 return ret;
43
44 return devm_add_action_or_reset(dev, devm_uniwill_wmi_unregister_notifier, nb);
45 }
46
uniwill_wmi_notify(struct wmi_device * wdev,union acpi_object * obj)47 static void uniwill_wmi_notify(struct wmi_device *wdev, union acpi_object *obj)
48 {
49 u32 value;
50 int ret;
51
52 if (obj->type != ACPI_TYPE_INTEGER)
53 return;
54
55 value = obj->integer.value;
56
57 dev_dbg(&wdev->dev, "Received WMI event %u\n", value);
58
59 ret = blocking_notifier_call_chain(&uniwill_wmi_chain_head, value, NULL);
60 if (notifier_to_errno(ret) < 0)
61 dev_err(&wdev->dev, "Failed to handle event %u\n", value);
62 }
63
64 /*
65 * We cannot fully trust this GUID since Uniwill just copied the WMI GUID
66 * from the Windows driver example, and others probably did the same.
67 *
68 * Because of this we cannot use this WMI GUID for autoloading. Instead the
69 * associated driver will be registered manually after matching a DMI table.
70 */
71 static const struct wmi_device_id uniwill_wmi_id_table[] = {
72 { UNIWILL_EVENT_GUID, NULL },
73 { }
74 };
75
76 static struct wmi_driver uniwill_wmi_driver = {
77 .driver = {
78 .name = DRIVER_NAME,
79 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
80 },
81 .id_table = uniwill_wmi_id_table,
82 .min_event_size = sizeof(u32),
83 .notify = uniwill_wmi_notify,
84 .no_singleton = true,
85 };
86
uniwill_wmi_register_driver(void)87 int __init uniwill_wmi_register_driver(void)
88 {
89 return wmi_driver_register(&uniwill_wmi_driver);
90 }
91
uniwill_wmi_unregister_driver(void)92 void __exit uniwill_wmi_unregister_driver(void)
93 {
94 wmi_driver_unregister(&uniwill_wmi_driver);
95 }
96