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 if (priv->has_switches)
115 detect_tablet_mode(device);
117 return input_register_device(priv->input_dev);
120 static void notify_handler(acpi_handle handle, u32 event, void *context)
122 struct platform_device *device = context;
123 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
124 unsigned int val = !(event & 1); /* Even=press, Odd=release */
125 const struct key_entry *ke, *ke_rel;
128 if (priv->wakeup_mode) {
129 ke = sparse_keymap_entry_from_scancode(priv->input_dev, event);
131 pm_wakeup_hard_event(&device->dev);
134 * Switch events like tablet mode will wake the device
135 * and report the new switch position to the input
138 if (ke->type == KE_SW)
139 sparse_keymap_report_event(priv->input_dev,
149 * Even press events are autorelease if there is no corresponding odd
150 * release event, or if the odd event is KE_IGNORE.
152 ke_rel = sparse_keymap_entry_from_scancode(priv->input_dev, event | 1);
153 autorelease = val && (!ke_rel || ke_rel->type == KE_IGNORE);
155 if (sparse_keymap_report_event(priv->input_dev, event, val, autorelease))
159 dev_dbg(&device->dev, "unknown event index 0x%x\n", event);
162 static bool intel_vbtn_has_buttons(acpi_handle handle)
166 status = acpi_evaluate_object(handle, "VBDL", NULL, NULL);
167 return ACPI_SUCCESS(status);
171 * There are several laptops (non 2-in-1) models out there which support VGBS,
172 * but simply always return 0, which we translate to SW_TABLET_MODE=1. This in
173 * turn causes userspace (libinput) to suppress events from the builtin
174 * keyboard and touchpad, making the laptop essentially unusable.
176 * Since the problem of wrongly reporting SW_TABLET_MODE=1 in combination
177 * with libinput, leads to a non-usable system. Where as OTOH many people will
178 * not even notice when SW_TABLET_MODE is not being reported, a DMI based allow
179 * list is used here. This list mainly matches on the chassis-type of 2-in-1s.
181 * There are also some 2-in-1s which use the intel-vbtn ACPI interface to report
182 * SW_TABLET_MODE with a chassis-type of 8 ("Portable") or 10 ("Notebook"),
183 * these are matched on a per model basis, since many normal laptops with a
184 * possible broken VGBS ACPI-method also use these chassis-types.
186 static const struct dmi_system_id dmi_switches_allow_list[] = {
189 DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "31" /* Convertible */),
194 DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "32" /* Detachable */),
199 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
200 DMI_MATCH(DMI_PRODUCT_NAME, "Venue 11 Pro 7130"),
205 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
206 DMI_MATCH(DMI_PRODUCT_NAME, "HP Stream x360 Convertible PC 11"),
209 {} /* Array terminator */
212 static bool intel_vbtn_has_switches(acpi_handle handle)
214 unsigned long long vgbs;
217 if (!dmi_check_system(dmi_switches_allow_list))
220 status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
221 return ACPI_SUCCESS(status);
224 static int intel_vbtn_probe(struct platform_device *device)
226 acpi_handle handle = ACPI_HANDLE(&device->dev);
227 bool has_buttons, has_switches;
228 struct intel_vbtn_priv *priv;
232 has_buttons = intel_vbtn_has_buttons(handle);
233 has_switches = intel_vbtn_has_switches(handle);
235 if (!has_buttons && !has_switches) {
236 dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n");
240 priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
243 dev_set_drvdata(&device->dev, priv);
245 priv->has_buttons = has_buttons;
246 priv->has_switches = has_switches;
248 err = intel_vbtn_input_setup(device);
250 pr_err("Failed to setup Intel Virtual Button\n");
254 status = acpi_install_notify_handler(handle,
258 if (ACPI_FAILURE(status))
261 device_init_wakeup(&device->dev, true);
263 * In order for system wakeup to work, the EC GPE has to be marked as
264 * a wakeup one, so do that here (this setting will persist, but it has
265 * no effect until the wakeup mask is set for the EC GPE).
267 acpi_ec_mark_gpe_for_wake();
271 static int intel_vbtn_remove(struct platform_device *device)
273 acpi_handle handle = ACPI_HANDLE(&device->dev);
275 device_init_wakeup(&device->dev, false);
276 acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
279 * Even if we failed to shut off the event stream, we can still
280 * safely detach from the device.
285 static int intel_vbtn_pm_prepare(struct device *dev)
287 if (device_may_wakeup(dev)) {
288 struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
290 priv->wakeup_mode = true;
295 static void intel_vbtn_pm_complete(struct device *dev)
297 struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
299 priv->wakeup_mode = false;
302 static int intel_vbtn_pm_resume(struct device *dev)
304 intel_vbtn_pm_complete(dev);
308 static const struct dev_pm_ops intel_vbtn_pm_ops = {
309 .prepare = intel_vbtn_pm_prepare,
310 .complete = intel_vbtn_pm_complete,
311 .resume = intel_vbtn_pm_resume,
312 .restore = intel_vbtn_pm_resume,
313 .thaw = intel_vbtn_pm_resume,
316 static struct platform_driver intel_vbtn_pl_driver = {
318 .name = "intel-vbtn",
319 .acpi_match_table = intel_vbtn_ids,
320 .pm = &intel_vbtn_pm_ops,
322 .probe = intel_vbtn_probe,
323 .remove = intel_vbtn_remove,
326 static acpi_status __init
327 check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
329 const struct acpi_device_id *ids = context;
330 struct acpi_device *dev;
332 if (acpi_bus_get_device(handle, &dev) != 0)
335 if (acpi_match_device_ids(dev, ids) == 0)
336 if (!IS_ERR_OR_NULL(acpi_create_platform_device(dev, NULL)))
338 "intel-vbtn: created platform device\n");
343 static int __init intel_vbtn_init(void)
345 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
346 ACPI_UINT32_MAX, check_acpi_dev, NULL,
347 (void *)intel_vbtn_ids, NULL);
349 return platform_driver_register(&intel_vbtn_pl_driver);
351 module_init(intel_vbtn_init);
353 static void __exit intel_vbtn_exit(void)
355 platform_driver_unregister(&intel_vbtn_pl_driver);
357 module_exit(intel_vbtn_exit);