1 // SPDX-License-Identifier: GPL-2.0+
3 * Intel Virtual Button driver for Windows 8.1+
5 * Copyright (C) 2016 AceLan Kao <acelan.kao@canonical.com>
6 * Copyright (C) 2016 Alex Hung <alex.hung@canonical.com>
9 #include <linux/acpi.h>
10 #include <linux/input.h>
11 #include <linux/input/sparse-keymap.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/suspend.h>
17 /* When NOT in tablet mode, VGBS returns with the flag 0x40 */
18 #define TABLET_MODE_FLAG 0x40
20 MODULE_LICENSE("GPL");
21 MODULE_AUTHOR("AceLan Kao");
23 static const struct acpi_device_id intel_vbtn_ids[] = {
28 /* In theory, these are HID usages. */
29 static const struct key_entry intel_vbtn_keymap[] = {
30 { KE_KEY, 0xC0, { KEY_POWER } }, /* power key press */
31 { KE_IGNORE, 0xC1, { KEY_POWER } }, /* power key release */
32 { KE_KEY, 0xC2, { KEY_LEFTMETA } }, /* 'Windows' key press */
33 { KE_KEY, 0xC3, { KEY_LEFTMETA } }, /* 'Windows' key release */
34 { KE_KEY, 0xC4, { KEY_VOLUMEUP } }, /* volume-up key press */
35 { KE_IGNORE, 0xC5, { KEY_VOLUMEUP } }, /* volume-up key release */
36 { KE_KEY, 0xC6, { KEY_VOLUMEDOWN } }, /* volume-down key press */
37 { KE_IGNORE, 0xC7, { KEY_VOLUMEDOWN } }, /* volume-down key release */
38 { KE_KEY, 0xC8, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key press */
39 { KE_KEY, 0xC9, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key release */
40 { KE_SW, 0xCC, { .sw = { SW_TABLET_MODE, 1 } } }, /* Tablet */
41 { KE_SW, 0xCD, { .sw = { SW_TABLET_MODE, 0 } } }, /* Laptop */
45 struct intel_vbtn_priv {
46 struct input_dev *input_dev;
50 static int intel_vbtn_input_setup(struct platform_device *device)
52 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
55 priv->input_dev = devm_input_allocate_device(&device->dev);
59 ret = sparse_keymap_setup(priv->input_dev, intel_vbtn_keymap, NULL);
63 priv->input_dev->dev.parent = &device->dev;
64 priv->input_dev->name = "Intel Virtual Button driver";
65 priv->input_dev->id.bustype = BUS_HOST;
67 return input_register_device(priv->input_dev);
70 static void notify_handler(acpi_handle handle, u32 event, void *context)
72 struct platform_device *device = context;
73 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
74 unsigned int val = !(event & 1); /* Even=press, Odd=release */
75 const struct key_entry *ke_rel;
78 if (priv->wakeup_mode) {
79 if (sparse_keymap_entry_from_scancode(priv->input_dev, event)) {
80 pm_wakeup_hard_event(&device->dev);
87 * Even press events are autorelease if there is no corresponding odd
88 * release event, or if the odd event is KE_IGNORE.
90 ke_rel = sparse_keymap_entry_from_scancode(priv->input_dev, event | 1);
91 autorelease = val && (!ke_rel || ke_rel->type == KE_IGNORE);
93 if (sparse_keymap_report_event(priv->input_dev, event, val, autorelease))
97 dev_dbg(&device->dev, "unknown event index 0x%x\n", event);
100 static int intel_vbtn_probe(struct platform_device *device)
102 struct acpi_buffer vgbs_output = { ACPI_ALLOCATE_BUFFER, NULL };
103 acpi_handle handle = ACPI_HANDLE(&device->dev);
104 struct intel_vbtn_priv *priv;
108 status = acpi_evaluate_object(handle, "VBDL", NULL, NULL);
109 if (ACPI_FAILURE(status)) {
110 dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n");
114 priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
117 dev_set_drvdata(&device->dev, priv);
119 err = intel_vbtn_input_setup(device);
121 pr_err("Failed to setup Intel Virtual Button\n");
126 * VGBS being present and returning something means we have
127 * a tablet mode switch.
129 status = acpi_evaluate_object(handle, "VGBS", NULL, &vgbs_output);
130 if (ACPI_SUCCESS(status)) {
131 union acpi_object *obj = vgbs_output.pointer;
133 if (obj && obj->type == ACPI_TYPE_INTEGER) {
134 int m = !(obj->integer.value & TABLET_MODE_FLAG);
136 input_report_switch(priv->input_dev, SW_TABLET_MODE, m);
140 kfree(vgbs_output.pointer);
142 status = acpi_install_notify_handler(handle,
146 if (ACPI_FAILURE(status))
149 device_init_wakeup(&device->dev, true);
153 static int intel_vbtn_remove(struct platform_device *device)
155 acpi_handle handle = ACPI_HANDLE(&device->dev);
157 acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
160 * Even if we failed to shut off the event stream, we can still
161 * safely detach from the device.
166 static int intel_vbtn_pm_prepare(struct device *dev)
168 struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
170 priv->wakeup_mode = true;
174 static int intel_vbtn_pm_resume(struct device *dev)
176 struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
178 priv->wakeup_mode = false;
182 static const struct dev_pm_ops intel_vbtn_pm_ops = {
183 .prepare = intel_vbtn_pm_prepare,
184 .resume = intel_vbtn_pm_resume,
185 .restore = intel_vbtn_pm_resume,
186 .thaw = intel_vbtn_pm_resume,
189 static struct platform_driver intel_vbtn_pl_driver = {
191 .name = "intel-vbtn",
192 .acpi_match_table = intel_vbtn_ids,
193 .pm = &intel_vbtn_pm_ops,
195 .probe = intel_vbtn_probe,
196 .remove = intel_vbtn_remove,
198 MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids);
200 static acpi_status __init
201 check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
203 const struct acpi_device_id *ids = context;
204 struct acpi_device *dev;
206 if (acpi_bus_get_device(handle, &dev) != 0)
209 if (acpi_match_device_ids(dev, ids) == 0)
210 if (acpi_create_platform_device(dev, NULL))
212 "intel-vbtn: created platform device\n");
217 static int __init intel_vbtn_init(void)
219 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
220 ACPI_UINT32_MAX, check_acpi_dev, NULL,
221 (void *)intel_vbtn_ids, NULL);
223 return platform_driver_register(&intel_vbtn_pl_driver);
225 module_init(intel_vbtn_init);
227 static void __exit intel_vbtn_exit(void)
229 platform_driver_unregister(&intel_vbtn_pl_driver);
231 module_exit(intel_vbtn_exit);