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 /* Returned when NOT in tablet mode on some HP Stream x360 11 models */
19 #define VGBS_TABLET_MODE_FLAG_ALT 0x10
20 /* When NOT in tablet mode, VGBS returns with the flag 0x40 */
21 #define VGBS_TABLET_MODE_FLAG 0x40
22 #define VGBS_DOCK_MODE_FLAG 0x80
24 #define VGBS_TABLET_MODE_FLAGS (VGBS_TABLET_MODE_FLAG | VGBS_TABLET_MODE_FLAG_ALT)
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("AceLan Kao");
29 static const struct acpi_device_id intel_vbtn_ids[] = {
33 MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids);
35 /* In theory, these are HID usages. */
36 static const struct key_entry intel_vbtn_keymap[] = {
37 { KE_KEY, 0xC0, { KEY_POWER } }, /* power key press */
38 { KE_IGNORE, 0xC1, { KEY_POWER } }, /* power key release */
39 { KE_KEY, 0xC2, { KEY_LEFTMETA } }, /* 'Windows' key press */
40 { KE_KEY, 0xC3, { KEY_LEFTMETA } }, /* 'Windows' key release */
41 { KE_KEY, 0xC4, { KEY_VOLUMEUP } }, /* volume-up key press */
42 { KE_IGNORE, 0xC5, { KEY_VOLUMEUP } }, /* volume-up key release */
43 { KE_KEY, 0xC6, { KEY_VOLUMEDOWN } }, /* volume-down key press */
44 { KE_IGNORE, 0xC7, { KEY_VOLUMEDOWN } }, /* volume-down key release */
45 { KE_KEY, 0xC8, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key press */
46 { KE_KEY, 0xC9, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key release */
50 static const struct key_entry intel_vbtn_switchmap[] = {
51 { KE_SW, 0xCA, { .sw = { SW_DOCK, 1 } } }, /* Docked */
52 { KE_SW, 0xCB, { .sw = { SW_DOCK, 0 } } }, /* Undocked */
53 { KE_SW, 0xCC, { .sw = { SW_TABLET_MODE, 1 } } }, /* Tablet */
54 { KE_SW, 0xCD, { .sw = { SW_TABLET_MODE, 0 } } }, /* Laptop */
59 (ARRAY_SIZE(intel_vbtn_keymap) + ARRAY_SIZE(intel_vbtn_switchmap) + 1)
61 struct intel_vbtn_priv {
62 struct input_dev *buttons_dev;
63 struct input_dev *switches_dev;
69 static void detect_tablet_mode(struct platform_device *device)
71 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
72 acpi_handle handle = ACPI_HANDLE(&device->dev);
73 unsigned long long vgbs;
77 status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
78 if (ACPI_FAILURE(status))
81 m = !(vgbs & VGBS_TABLET_MODE_FLAGS);
82 input_report_switch(priv->switches_dev, SW_TABLET_MODE, m);
83 m = (vgbs & VGBS_DOCK_MODE_FLAG) ? 1 : 0;
84 input_report_switch(priv->switches_dev, SW_DOCK, m);
88 * Note this unconditionally creates the 2 input_dev-s and sets up
89 * the sparse-keymaps. Only the registration is conditional on
90 * have_buttons / have_switches. This is done so that the notify
91 * handler can always call sparse_keymap_entry_from_scancode()
92 * on the input_dev-s do determine the event type.
94 static int intel_vbtn_input_setup(struct platform_device *device)
96 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
99 priv->buttons_dev = devm_input_allocate_device(&device->dev);
100 if (!priv->buttons_dev)
103 ret = sparse_keymap_setup(priv->buttons_dev, intel_vbtn_keymap, NULL);
107 priv->buttons_dev->dev.parent = &device->dev;
108 priv->buttons_dev->name = "Intel Virtual Buttons";
109 priv->buttons_dev->id.bustype = BUS_HOST;
111 if (priv->has_buttons) {
112 ret = input_register_device(priv->buttons_dev);
117 priv->switches_dev = devm_input_allocate_device(&device->dev);
118 if (!priv->switches_dev)
121 ret = sparse_keymap_setup(priv->switches_dev, intel_vbtn_switchmap, NULL);
125 priv->switches_dev->dev.parent = &device->dev;
126 priv->switches_dev->name = "Intel Virtual Switches";
127 priv->switches_dev->id.bustype = BUS_HOST;
129 if (priv->has_switches) {
130 detect_tablet_mode(device);
132 ret = input_register_device(priv->switches_dev);
140 static void notify_handler(acpi_handle handle, u32 event, void *context)
142 struct platform_device *device = context;
143 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
144 unsigned int val = !(event & 1); /* Even=press, Odd=release */
145 const struct key_entry *ke, *ke_rel;
146 struct input_dev *input_dev;
150 if ((ke = sparse_keymap_entry_from_scancode(priv->buttons_dev, event))) {
151 if (!priv->has_buttons) {
152 dev_warn(&device->dev, "Warning: received a button event on a device without buttons, please report this.\n");
155 input_dev = priv->buttons_dev;
156 } else if ((ke = sparse_keymap_entry_from_scancode(priv->switches_dev, event))) {
157 if (!priv->has_switches) {
158 dev_info(&device->dev, "Registering Intel Virtual Switches input-dev after receiving a switch event\n");
159 ret = input_register_device(priv->switches_dev);
163 priv->has_switches = true;
165 input_dev = priv->switches_dev;
167 dev_dbg(&device->dev, "unknown event index 0x%x\n", event);
171 if (priv->wakeup_mode) {
172 pm_wakeup_hard_event(&device->dev);
175 * Skip reporting an evdev event for button wake events,
176 * mirroring how the drivers/acpi/button.c code skips this too.
178 if (ke->type == KE_KEY)
183 * Even press events are autorelease if there is no corresponding odd
184 * release event, or if the odd event is KE_IGNORE.
186 ke_rel = sparse_keymap_entry_from_scancode(input_dev, event | 1);
187 autorelease = val && (!ke_rel || ke_rel->type == KE_IGNORE);
189 sparse_keymap_report_event(input_dev, event, val, autorelease);
193 * There are several laptops (non 2-in-1) models out there which support VGBS,
194 * but simply always return 0, which we translate to SW_TABLET_MODE=1. This in
195 * turn causes userspace (libinput) to suppress events from the builtin
196 * keyboard and touchpad, making the laptop essentially unusable.
198 * Since the problem of wrongly reporting SW_TABLET_MODE=1 in combination
199 * with libinput, leads to a non-usable system. Where as OTOH many people will
200 * not even notice when SW_TABLET_MODE is not being reported, a DMI based allow
201 * list is used here. This list mainly matches on the chassis-type of 2-in-1s.
203 * There are also some 2-in-1s which use the intel-vbtn ACPI interface to report
204 * SW_TABLET_MODE with a chassis-type of 8 ("Portable") or 10 ("Notebook"),
205 * these are matched on a per model basis, since many normal laptops with a
206 * possible broken VGBS ACPI-method also use these chassis-types.
208 static const struct dmi_system_id dmi_switches_allow_list[] = {
211 DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "31" /* Convertible */),
216 DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "32" /* Detachable */),
221 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
222 DMI_MATCH(DMI_PRODUCT_NAME, "Venue 11 Pro 7130"),
227 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
228 DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion 13 x360 PC"),
233 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
234 DMI_MATCH(DMI_PRODUCT_NAME, "Switch SA5-271"),
239 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
240 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7352"),
243 {} /* Array terminator */
246 static bool intel_vbtn_has_switches(acpi_handle handle)
248 unsigned long long vgbs;
251 if (!dmi_check_system(dmi_switches_allow_list))
254 status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
255 return ACPI_SUCCESS(status);
258 static int intel_vbtn_probe(struct platform_device *device)
260 acpi_handle handle = ACPI_HANDLE(&device->dev);
261 bool has_buttons, has_switches;
262 struct intel_vbtn_priv *priv;
266 has_buttons = acpi_has_method(handle, "VBDL");
267 has_switches = intel_vbtn_has_switches(handle);
269 if (!has_buttons && !has_switches) {
270 dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n");
274 priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
277 dev_set_drvdata(&device->dev, priv);
279 priv->has_buttons = has_buttons;
280 priv->has_switches = has_switches;
282 err = intel_vbtn_input_setup(device);
284 pr_err("Failed to setup Intel Virtual Button\n");
288 status = acpi_install_notify_handler(handle,
292 if (ACPI_FAILURE(status))
296 status = acpi_evaluate_object(handle, "VBDL", NULL, NULL);
297 if (ACPI_FAILURE(status))
298 dev_err(&device->dev, "Error VBDL failed with ACPI status %d\n", status);
301 device_init_wakeup(&device->dev, true);
303 * In order for system wakeup to work, the EC GPE has to be marked as
304 * a wakeup one, so do that here (this setting will persist, but it has
305 * no effect until the wakeup mask is set for the EC GPE).
307 acpi_ec_mark_gpe_for_wake();
311 static int intel_vbtn_remove(struct platform_device *device)
313 acpi_handle handle = ACPI_HANDLE(&device->dev);
315 device_init_wakeup(&device->dev, false);
316 acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
319 * Even if we failed to shut off the event stream, we can still
320 * safely detach from the device.
325 static int intel_vbtn_pm_prepare(struct device *dev)
327 if (device_may_wakeup(dev)) {
328 struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
330 priv->wakeup_mode = true;
335 static void intel_vbtn_pm_complete(struct device *dev)
337 struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
339 priv->wakeup_mode = false;
342 static int intel_vbtn_pm_resume(struct device *dev)
344 intel_vbtn_pm_complete(dev);
348 static const struct dev_pm_ops intel_vbtn_pm_ops = {
349 .prepare = intel_vbtn_pm_prepare,
350 .complete = intel_vbtn_pm_complete,
351 .resume = intel_vbtn_pm_resume,
352 .restore = intel_vbtn_pm_resume,
353 .thaw = intel_vbtn_pm_resume,
356 static struct platform_driver intel_vbtn_pl_driver = {
358 .name = "intel-vbtn",
359 .acpi_match_table = intel_vbtn_ids,
360 .pm = &intel_vbtn_pm_ops,
362 .probe = intel_vbtn_probe,
363 .remove = intel_vbtn_remove,
366 static acpi_status __init
367 check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
369 const struct acpi_device_id *ids = context;
370 struct acpi_device *dev;
372 if (acpi_bus_get_device(handle, &dev) != 0)
375 if (acpi_match_device_ids(dev, ids) == 0)
376 if (!IS_ERR_OR_NULL(acpi_create_platform_device(dev, NULL)))
378 "intel-vbtn: created platform device\n");
383 static int __init intel_vbtn_init(void)
385 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
386 ACPI_UINT32_MAX, check_acpi_dev, NULL,
387 (void *)intel_vbtn_ids, NULL);
389 return platform_driver_register(&intel_vbtn_pl_driver);
391 module_init(intel_vbtn_init);
393 static void __exit intel_vbtn_exit(void)
395 platform_driver_unregister(&intel_vbtn_pl_driver);
397 module_exit(intel_vbtn_exit);