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/dmi.h>
11 #include <linux/input.h>
12 #include <linux/input/sparse-keymap.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/suspend.h>
18 /* When NOT in tablet mode, VGBS returns with the flag 0x40 */
19 #define TABLET_MODE_FLAG 0x40
20 #define DOCK_MODE_FLAG 0x80
22 MODULE_LICENSE("GPL");
23 MODULE_AUTHOR("AceLan Kao");
25 static const struct acpi_device_id intel_vbtn_ids[] = {
29 MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids);
31 /* In theory, these are HID usages. */
32 static const struct key_entry intel_vbtn_keymap[] = {
33 { KE_KEY, 0xC0, { KEY_POWER } }, /* power key press */
34 { KE_IGNORE, 0xC1, { KEY_POWER } }, /* power key release */
35 { KE_KEY, 0xC2, { KEY_LEFTMETA } }, /* 'Windows' key press */
36 { KE_KEY, 0xC3, { KEY_LEFTMETA } }, /* 'Windows' key release */
37 { KE_KEY, 0xC4, { KEY_VOLUMEUP } }, /* volume-up key press */
38 { KE_IGNORE, 0xC5, { KEY_VOLUMEUP } }, /* volume-up key release */
39 { KE_KEY, 0xC6, { KEY_VOLUMEDOWN } }, /* volume-down key press */
40 { KE_IGNORE, 0xC7, { KEY_VOLUMEDOWN } }, /* volume-down key release */
41 { KE_KEY, 0xC8, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key press */
42 { KE_KEY, 0xC9, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key release */
45 static const struct key_entry intel_vbtn_switchmap[] = {
46 { KE_SW, 0xCA, { .sw = { SW_DOCK, 1 } } }, /* Docked */
47 { KE_SW, 0xCB, { .sw = { SW_DOCK, 0 } } }, /* Undocked */
48 { KE_SW, 0xCC, { .sw = { SW_TABLET_MODE, 1 } } }, /* Tablet */
49 { KE_SW, 0xCD, { .sw = { SW_TABLET_MODE, 0 } } }, /* Laptop */
53 (ARRAY_SIZE(intel_vbtn_keymap) + ARRAY_SIZE(intel_vbtn_switchmap) + 1)
55 struct intel_vbtn_priv {
56 struct key_entry keymap[KEYMAP_LEN];
57 struct input_dev *input_dev;
63 static void detect_tablet_mode(struct platform_device *device)
65 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
66 acpi_handle handle = ACPI_HANDLE(&device->dev);
67 unsigned long long vgbs;
71 status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
72 if (ACPI_FAILURE(status))
75 m = !(vgbs & TABLET_MODE_FLAG);
76 input_report_switch(priv->input_dev, SW_TABLET_MODE, m);
77 m = (vgbs & DOCK_MODE_FLAG) ? 1 : 0;
78 input_report_switch(priv->input_dev, SW_DOCK, m);
81 static int intel_vbtn_input_setup(struct platform_device *device)
83 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
84 int ret, keymap_len = 0;
86 if (priv->has_buttons) {
87 memcpy(&priv->keymap[keymap_len], intel_vbtn_keymap,
88 ARRAY_SIZE(intel_vbtn_keymap) *
89 sizeof(struct key_entry));
90 keymap_len += ARRAY_SIZE(intel_vbtn_keymap);
93 if (priv->has_switches) {
94 memcpy(&priv->keymap[keymap_len], intel_vbtn_switchmap,
95 ARRAY_SIZE(intel_vbtn_switchmap) *
96 sizeof(struct key_entry));
97 keymap_len += ARRAY_SIZE(intel_vbtn_switchmap);
100 priv->keymap[keymap_len].type = KE_END;
102 priv->input_dev = devm_input_allocate_device(&device->dev);
103 if (!priv->input_dev)
106 ret = sparse_keymap_setup(priv->input_dev, priv->keymap, NULL);
110 priv->input_dev->dev.parent = &device->dev;
111 priv->input_dev->name = "Intel Virtual Button driver";
112 priv->input_dev->id.bustype = BUS_HOST;
114 return input_register_device(priv->input_dev);
117 static void notify_handler(acpi_handle handle, u32 event, void *context)
119 struct platform_device *device = context;
120 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
121 unsigned int val = !(event & 1); /* Even=press, Odd=release */
122 const struct key_entry *ke, *ke_rel;
125 if (priv->wakeup_mode) {
126 ke = sparse_keymap_entry_from_scancode(priv->input_dev, event);
128 pm_wakeup_hard_event(&device->dev);
131 * Switch events like tablet mode will wake the device
132 * and report the new switch position to the input
135 if (ke->type == KE_SW)
136 sparse_keymap_report_event(priv->input_dev,
146 * Even press events are autorelease if there is no corresponding odd
147 * release event, or if the odd event is KE_IGNORE.
149 ke_rel = sparse_keymap_entry_from_scancode(priv->input_dev, event | 1);
150 autorelease = val && (!ke_rel || ke_rel->type == KE_IGNORE);
152 if (sparse_keymap_report_event(priv->input_dev, event, val, autorelease))
156 dev_dbg(&device->dev, "unknown event index 0x%x\n", event);
159 static bool intel_vbtn_has_buttons(acpi_handle handle)
163 status = acpi_evaluate_object(handle, "VBDL", NULL, NULL);
164 return ACPI_SUCCESS(status);
167 static bool intel_vbtn_has_switches(acpi_handle handle)
169 const char *chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
170 unsigned long chassis_type_int;
171 unsigned long long vgbs;
174 if (kstrtoul(chassis_type, 10, &chassis_type_int))
177 switch (chassis_type_int) {
178 case 8: /* Portable */
179 case 31: /* Convertible */
180 case 32: /* Detachable */
186 status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
187 return ACPI_SUCCESS(status);
190 static int intel_vbtn_probe(struct platform_device *device)
192 acpi_handle handle = ACPI_HANDLE(&device->dev);
193 bool has_buttons, has_switches;
194 struct intel_vbtn_priv *priv;
198 has_buttons = intel_vbtn_has_buttons(handle);
199 has_switches = intel_vbtn_has_switches(handle);
201 if (!has_buttons && !has_switches) {
202 dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n");
206 priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
209 dev_set_drvdata(&device->dev, priv);
211 priv->has_buttons = has_buttons;
212 priv->has_switches = has_switches;
214 err = intel_vbtn_input_setup(device);
216 pr_err("Failed to setup Intel Virtual Button\n");
220 if (priv->has_switches)
221 detect_tablet_mode(device);
223 status = acpi_install_notify_handler(handle,
227 if (ACPI_FAILURE(status))
230 device_init_wakeup(&device->dev, true);
232 * In order for system wakeup to work, the EC GPE has to be marked as
233 * a wakeup one, so do that here (this setting will persist, but it has
234 * no effect until the wakeup mask is set for the EC GPE).
236 acpi_ec_mark_gpe_for_wake();
240 static int intel_vbtn_remove(struct platform_device *device)
242 acpi_handle handle = ACPI_HANDLE(&device->dev);
244 device_init_wakeup(&device->dev, false);
245 acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
248 * Even if we failed to shut off the event stream, we can still
249 * safely detach from the device.
254 static int intel_vbtn_pm_prepare(struct device *dev)
256 if (device_may_wakeup(dev)) {
257 struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
259 priv->wakeup_mode = true;
264 static void intel_vbtn_pm_complete(struct device *dev)
266 struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
268 priv->wakeup_mode = false;
271 static int intel_vbtn_pm_resume(struct device *dev)
273 intel_vbtn_pm_complete(dev);
277 static const struct dev_pm_ops intel_vbtn_pm_ops = {
278 .prepare = intel_vbtn_pm_prepare,
279 .complete = intel_vbtn_pm_complete,
280 .resume = intel_vbtn_pm_resume,
281 .restore = intel_vbtn_pm_resume,
282 .thaw = intel_vbtn_pm_resume,
285 static struct platform_driver intel_vbtn_pl_driver = {
287 .name = "intel-vbtn",
288 .acpi_match_table = intel_vbtn_ids,
289 .pm = &intel_vbtn_pm_ops,
291 .probe = intel_vbtn_probe,
292 .remove = intel_vbtn_remove,
295 static acpi_status __init
296 check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
298 const struct acpi_device_id *ids = context;
299 struct acpi_device *dev;
301 if (acpi_bus_get_device(handle, &dev) != 0)
304 if (acpi_match_device_ids(dev, ids) == 0)
305 if (acpi_create_platform_device(dev, NULL))
307 "intel-vbtn: created platform device\n");
312 static int __init intel_vbtn_init(void)
314 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
315 ACPI_UINT32_MAX, check_acpi_dev, NULL,
316 (void *)intel_vbtn_ids, NULL);
318 return platform_driver_register(&intel_vbtn_pl_driver);
320 module_init(intel_vbtn_init);
322 static void __exit intel_vbtn_exit(void)
324 platform_driver_unregister(&intel_vbtn_pl_driver);
326 module_exit(intel_vbtn_exit);