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
[] = {
30 /* In theory, these are HID usages. */
31 static const struct key_entry intel_vbtn_keymap
[] = {
32 { KE_KEY
, 0xC0, { KEY_POWER
} }, /* power key press */
33 { KE_IGNORE
, 0xC1, { KEY_POWER
} }, /* power key release */
34 { KE_KEY
, 0xC2, { KEY_LEFTMETA
} }, /* 'Windows' key press */
35 { KE_KEY
, 0xC3, { KEY_LEFTMETA
} }, /* 'Windows' key release */
36 { KE_KEY
, 0xC4, { KEY_VOLUMEUP
} }, /* volume-up key press */
37 { KE_IGNORE
, 0xC5, { KEY_VOLUMEUP
} }, /* volume-up key release */
38 { KE_KEY
, 0xC6, { KEY_VOLUMEDOWN
} }, /* volume-down key press */
39 { KE_IGNORE
, 0xC7, { KEY_VOLUMEDOWN
} }, /* volume-down key release */
40 { KE_KEY
, 0xC8, { KEY_ROTATE_LOCK_TOGGLE
} }, /* rotate-lock key press */
41 { KE_KEY
, 0xC9, { KEY_ROTATE_LOCK_TOGGLE
} }, /* rotate-lock key release */
44 static const struct key_entry intel_vbtn_switchmap
[] = {
45 { KE_SW
, 0xCA, { .sw
= { SW_DOCK
, 1 } } }, /* Docked */
46 { KE_SW
, 0xCB, { .sw
= { SW_DOCK
, 0 } } }, /* Undocked */
47 { KE_SW
, 0xCC, { .sw
= { SW_TABLET_MODE
, 1 } } }, /* Tablet */
48 { KE_SW
, 0xCD, { .sw
= { SW_TABLET_MODE
, 0 } } }, /* Laptop */
52 (ARRAY_SIZE(intel_vbtn_keymap) + ARRAY_SIZE(intel_vbtn_switchmap) + 1)
54 struct intel_vbtn_priv
{
55 struct key_entry keymap
[KEYMAP_LEN
];
56 struct input_dev
*input_dev
;
61 static int intel_vbtn_input_setup(struct platform_device
*device
)
63 struct intel_vbtn_priv
*priv
= dev_get_drvdata(&device
->dev
);
64 int ret
, keymap_len
= 0;
67 memcpy(&priv
->keymap
[keymap_len
], intel_vbtn_keymap
,
68 ARRAY_SIZE(intel_vbtn_keymap
) *
69 sizeof(struct key_entry
));
70 keymap_len
+= ARRAY_SIZE(intel_vbtn_keymap
);
73 if (priv
->has_switches
) {
74 memcpy(&priv
->keymap
[keymap_len
], intel_vbtn_switchmap
,
75 ARRAY_SIZE(intel_vbtn_switchmap
) *
76 sizeof(struct key_entry
));
77 keymap_len
+= ARRAY_SIZE(intel_vbtn_switchmap
);
80 priv
->keymap
[keymap_len
].type
= KE_END
;
82 priv
->input_dev
= devm_input_allocate_device(&device
->dev
);
86 ret
= sparse_keymap_setup(priv
->input_dev
, priv
->keymap
, NULL
);
90 priv
->input_dev
->dev
.parent
= &device
->dev
;
91 priv
->input_dev
->name
= "Intel Virtual Button driver";
92 priv
->input_dev
->id
.bustype
= BUS_HOST
;
94 return input_register_device(priv
->input_dev
);
97 static void notify_handler(acpi_handle handle
, u32 event
, void *context
)
99 struct platform_device
*device
= context
;
100 struct intel_vbtn_priv
*priv
= dev_get_drvdata(&device
->dev
);
101 unsigned int val
= !(event
& 1); /* Even=press, Odd=release */
102 const struct key_entry
*ke
, *ke_rel
;
105 if (priv
->wakeup_mode
) {
106 ke
= sparse_keymap_entry_from_scancode(priv
->input_dev
, event
);
108 pm_wakeup_hard_event(&device
->dev
);
111 * Switch events like tablet mode will wake the device
112 * and report the new switch position to the input
115 if (ke
->type
== KE_SW
)
116 sparse_keymap_report_event(priv
->input_dev
,
126 * Even press events are autorelease if there is no corresponding odd
127 * release event, or if the odd event is KE_IGNORE.
129 ke_rel
= sparse_keymap_entry_from_scancode(priv
->input_dev
, event
| 1);
130 autorelease
= val
&& (!ke_rel
|| ke_rel
->type
== KE_IGNORE
);
132 if (sparse_keymap_report_event(priv
->input_dev
, event
, val
, autorelease
))
136 dev_dbg(&device
->dev
, "unknown event index 0x%x\n", event
);
139 static void detect_tablet_mode(struct platform_device
*device
)
141 struct intel_vbtn_priv
*priv
= dev_get_drvdata(&device
->dev
);
142 acpi_handle handle
= ACPI_HANDLE(&device
->dev
);
143 unsigned long long vgbs
;
147 status
= acpi_evaluate_integer(handle
, "VGBS", NULL
, &vgbs
);
148 if (ACPI_FAILURE(status
))
151 m
= !(vgbs
& TABLET_MODE_FLAG
);
152 input_report_switch(priv
->input_dev
, SW_TABLET_MODE
, m
);
153 m
= (vgbs
& DOCK_MODE_FLAG
) ? 1 : 0;
154 input_report_switch(priv
->input_dev
, SW_DOCK
, m
);
157 static bool intel_vbtn_has_switches(acpi_handle handle
)
159 const char *chassis_type
= dmi_get_system_info(DMI_CHASSIS_TYPE
);
160 unsigned long long vgbs
;
164 * Some normal laptops have a VGBS method despite being non-convertible
165 * and their VGBS method always returns 0, causing detect_tablet_mode()
166 * to report SW_TABLET_MODE=1 to userspace, which causes issues.
167 * These laptops have a DMI chassis_type of 9 ("Laptop"), do not report
168 * switches on any devices with a DMI chassis_type of 9.
170 if (chassis_type
&& strcmp(chassis_type
, "9") == 0)
173 status
= acpi_evaluate_integer(handle
, "VGBS", NULL
, &vgbs
);
174 return ACPI_SUCCESS(status
);
177 static int intel_vbtn_probe(struct platform_device
*device
)
179 acpi_handle handle
= ACPI_HANDLE(&device
->dev
);
180 struct intel_vbtn_priv
*priv
;
184 status
= acpi_evaluate_object(handle
, "VBDL", NULL
, NULL
);
185 if (ACPI_FAILURE(status
)) {
186 dev_warn(&device
->dev
, "failed to read Intel Virtual Button driver\n");
190 priv
= devm_kzalloc(&device
->dev
, sizeof(*priv
), GFP_KERNEL
);
193 dev_set_drvdata(&device
->dev
, priv
);
195 priv
->has_switches
= intel_vbtn_has_switches(handle
);
197 err
= intel_vbtn_input_setup(device
);
199 pr_err("Failed to setup Intel Virtual Button\n");
203 if (priv
->has_switches
)
204 detect_tablet_mode(device
);
206 status
= acpi_install_notify_handler(handle
,
210 if (ACPI_FAILURE(status
))
213 device_init_wakeup(&device
->dev
, true);
217 static int intel_vbtn_remove(struct platform_device
*device
)
219 acpi_handle handle
= ACPI_HANDLE(&device
->dev
);
221 device_init_wakeup(&device
->dev
, false);
222 acpi_remove_notify_handler(handle
, ACPI_DEVICE_NOTIFY
, notify_handler
);
225 * Even if we failed to shut off the event stream, we can still
226 * safely detach from the device.
231 static int intel_vbtn_pm_prepare(struct device
*dev
)
233 struct intel_vbtn_priv
*priv
= dev_get_drvdata(dev
);
235 priv
->wakeup_mode
= true;
239 static int intel_vbtn_pm_resume(struct device
*dev
)
241 struct intel_vbtn_priv
*priv
= dev_get_drvdata(dev
);
243 priv
->wakeup_mode
= false;
247 static const struct dev_pm_ops intel_vbtn_pm_ops
= {
248 .prepare
= intel_vbtn_pm_prepare
,
249 .resume
= intel_vbtn_pm_resume
,
250 .restore
= intel_vbtn_pm_resume
,
251 .thaw
= intel_vbtn_pm_resume
,
254 static struct platform_driver intel_vbtn_pl_driver
= {
256 .name
= "intel-vbtn",
257 .acpi_match_table
= intel_vbtn_ids
,
258 .pm
= &intel_vbtn_pm_ops
,
260 .probe
= intel_vbtn_probe
,
261 .remove
= intel_vbtn_remove
,
263 MODULE_DEVICE_TABLE(acpi
, intel_vbtn_ids
);
265 static acpi_status __init
266 check_acpi_dev(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
268 const struct acpi_device_id
*ids
= context
;
269 struct acpi_device
*dev
;
271 if (acpi_bus_get_device(handle
, &dev
) != 0)
274 if (acpi_match_device_ids(dev
, ids
) == 0)
275 if (acpi_create_platform_device(dev
, NULL
))
277 "intel-vbtn: created platform device\n");
282 static int __init
intel_vbtn_init(void)
284 acpi_walk_namespace(ACPI_TYPE_DEVICE
, ACPI_ROOT_OBJECT
,
285 ACPI_UINT32_MAX
, check_acpi_dev
, NULL
,
286 (void *)intel_vbtn_ids
, NULL
);
288 return platform_driver_register(&intel_vbtn_pl_driver
);
290 module_init(intel_vbtn_init
);
292 static void __exit
intel_vbtn_exit(void)
294 platform_driver_unregister(&intel_vbtn_pl_driver
);
296 module_exit(intel_vbtn_exit
);