blob: 6aaceef3326c70958dcdcc1c1855817c09cbaee3 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Intel Virtual Button driver for Windows 8.1+
4 *
5 * Copyright (C) 2016 AceLan Kao <acelan.kao@canonical.com>
6 * Copyright (C) 2016 Alex Hung <alex.hung@canonical.com>
7 */
8
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>
17
Olivier Deprez0e641232021-09-23 10:07:05 +020018/* Returned when NOT in tablet mode on some HP Stream x360 11 models */
19#define VGBS_TABLET_MODE_FLAG_ALT 0x10
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020/* When NOT in tablet mode, VGBS returns with the flag 0x40 */
Olivier Deprez0e641232021-09-23 10:07:05 +020021#define VGBS_TABLET_MODE_FLAG 0x40
22#define VGBS_DOCK_MODE_FLAG 0x80
23
24#define VGBS_TABLET_MODE_FLAGS (VGBS_TABLET_MODE_FLAG | VGBS_TABLET_MODE_FLAG_ALT)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000025
26MODULE_LICENSE("GPL");
27MODULE_AUTHOR("AceLan Kao");
28
29static const struct acpi_device_id intel_vbtn_ids[] = {
30 {"INT33D6", 0},
31 {"", 0},
32};
33
34/* In theory, these are HID usages. */
35static const struct key_entry intel_vbtn_keymap[] = {
36 { KE_KEY, 0xC0, { KEY_POWER } }, /* power key press */
37 { KE_IGNORE, 0xC1, { KEY_POWER } }, /* power key release */
38 { KE_KEY, 0xC2, { KEY_LEFTMETA } }, /* 'Windows' key press */
39 { KE_KEY, 0xC3, { KEY_LEFTMETA } }, /* 'Windows' key release */
40 { KE_KEY, 0xC4, { KEY_VOLUMEUP } }, /* volume-up key press */
41 { KE_IGNORE, 0xC5, { KEY_VOLUMEUP } }, /* volume-up key release */
42 { KE_KEY, 0xC6, { KEY_VOLUMEDOWN } }, /* volume-down key press */
43 { KE_IGNORE, 0xC7, { KEY_VOLUMEDOWN } }, /* volume-down key release */
44 { KE_KEY, 0xC8, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key press */
45 { KE_KEY, 0xC9, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key release */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000046};
47
Olivier Deprez0e641232021-09-23 10:07:05 +020048static const struct key_entry intel_vbtn_switchmap[] = {
49 /*
50 * SW_DOCK should only be reported for docking stations, but DSDTs using the
51 * intel-vbtn code, always seem to use this for 2-in-1s / convertibles and set
52 * SW_DOCK=1 when in laptop-mode (in tandem with setting SW_TABLET_MODE=0).
53 * This causes userspace to think the laptop is docked to a port-replicator
54 * and to disable suspend-on-lid-close, which is undesirable.
55 * Map the dock events to KEY_IGNORE to avoid this broken SW_DOCK reporting.
56 */
57 { KE_IGNORE, 0xCA, { .sw = { SW_DOCK, 1 } } }, /* Docked */
58 { KE_IGNORE, 0xCB, { .sw = { SW_DOCK, 0 } } }, /* Undocked */
59 { KE_SW, 0xCC, { .sw = { SW_TABLET_MODE, 1 } } }, /* Tablet */
60 { KE_SW, 0xCD, { .sw = { SW_TABLET_MODE, 0 } } }, /* Laptop */
61};
62
63#define KEYMAP_LEN \
64 (ARRAY_SIZE(intel_vbtn_keymap) + ARRAY_SIZE(intel_vbtn_switchmap) + 1)
65
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000066struct intel_vbtn_priv {
Olivier Deprez0e641232021-09-23 10:07:05 +020067 struct key_entry keymap[KEYMAP_LEN];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000068 struct input_dev *input_dev;
Olivier Deprez0e641232021-09-23 10:07:05 +020069 bool has_switches;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000070 bool wakeup_mode;
71};
72
73static int intel_vbtn_input_setup(struct platform_device *device)
74{
75 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
Olivier Deprez0e641232021-09-23 10:07:05 +020076 int ret, keymap_len = 0;
77
78 if (true) {
79 memcpy(&priv->keymap[keymap_len], intel_vbtn_keymap,
80 ARRAY_SIZE(intel_vbtn_keymap) *
81 sizeof(struct key_entry));
82 keymap_len += ARRAY_SIZE(intel_vbtn_keymap);
83 }
84
85 if (priv->has_switches) {
86 memcpy(&priv->keymap[keymap_len], intel_vbtn_switchmap,
87 ARRAY_SIZE(intel_vbtn_switchmap) *
88 sizeof(struct key_entry));
89 keymap_len += ARRAY_SIZE(intel_vbtn_switchmap);
90 }
91
92 priv->keymap[keymap_len].type = KE_END;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000093
94 priv->input_dev = devm_input_allocate_device(&device->dev);
95 if (!priv->input_dev)
96 return -ENOMEM;
97
Olivier Deprez0e641232021-09-23 10:07:05 +020098 ret = sparse_keymap_setup(priv->input_dev, priv->keymap, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000099 if (ret)
100 return ret;
101
102 priv->input_dev->dev.parent = &device->dev;
103 priv->input_dev->name = "Intel Virtual Button driver";
104 priv->input_dev->id.bustype = BUS_HOST;
105
106 return input_register_device(priv->input_dev);
107}
108
109static void notify_handler(acpi_handle handle, u32 event, void *context)
110{
111 struct platform_device *device = context;
112 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
113 unsigned int val = !(event & 1); /* Even=press, Odd=release */
David Brazdil0f672f62019-12-10 10:32:29 +0000114 const struct key_entry *ke, *ke_rel;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000115 bool autorelease;
116
117 if (priv->wakeup_mode) {
David Brazdil0f672f62019-12-10 10:32:29 +0000118 ke = sparse_keymap_entry_from_scancode(priv->input_dev, event);
119 if (ke) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000120 pm_wakeup_hard_event(&device->dev);
David Brazdil0f672f62019-12-10 10:32:29 +0000121
122 /*
123 * Switch events like tablet mode will wake the device
124 * and report the new switch position to the input
125 * subsystem.
126 */
127 if (ke->type == KE_SW)
128 sparse_keymap_report_event(priv->input_dev,
129 event,
130 val,
131 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000132 return;
133 }
134 goto out_unknown;
135 }
136
137 /*
138 * Even press events are autorelease if there is no corresponding odd
139 * release event, or if the odd event is KE_IGNORE.
140 */
141 ke_rel = sparse_keymap_entry_from_scancode(priv->input_dev, event | 1);
142 autorelease = val && (!ke_rel || ke_rel->type == KE_IGNORE);
143
144 if (sparse_keymap_report_event(priv->input_dev, event, val, autorelease))
145 return;
146
147out_unknown:
148 dev_dbg(&device->dev, "unknown event index 0x%x\n", event);
149}
150
151static void detect_tablet_mode(struct platform_device *device)
152{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000153 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
154 acpi_handle handle = ACPI_HANDLE(&device->dev);
Olivier Deprez0e641232021-09-23 10:07:05 +0200155 unsigned long long vgbs;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000156 acpi_status status;
157 int m;
158
Olivier Deprez0e641232021-09-23 10:07:05 +0200159 status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000160 if (ACPI_FAILURE(status))
Olivier Deprez0e641232021-09-23 10:07:05 +0200161 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000162
Olivier Deprez0e641232021-09-23 10:07:05 +0200163 m = !(vgbs & VGBS_TABLET_MODE_FLAGS);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000164 input_report_switch(priv->input_dev, SW_TABLET_MODE, m);
Olivier Deprez0e641232021-09-23 10:07:05 +0200165 m = (vgbs & VGBS_DOCK_MODE_FLAG) ? 1 : 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000166 input_report_switch(priv->input_dev, SW_DOCK, m);
Olivier Deprez0e641232021-09-23 10:07:05 +0200167}
168
169/*
170 * There are several laptops (non 2-in-1) models out there which support VGBS,
171 * but simply always return 0, which we translate to SW_TABLET_MODE=1. This in
172 * turn causes userspace (libinput) to suppress events from the builtin
173 * keyboard and touchpad, making the laptop essentially unusable.
174 *
175 * Since the problem of wrongly reporting SW_TABLET_MODE=1 in combination
176 * with libinput, leads to a non-usable system. Where as OTOH many people will
177 * not even notice when SW_TABLET_MODE is not being reported, a DMI based allow
178 * list is used here. This list mainly matches on the chassis-type of 2-in-1s.
179 *
180 * There are also some 2-in-1s which use the intel-vbtn ACPI interface to report
181 * SW_TABLET_MODE with a chassis-type of 8 ("Portable") or 10 ("Notebook"),
182 * these are matched on a per model basis, since many normal laptops with a
183 * possible broken VGBS ACPI-method also use these chassis-types.
184 */
185static const struct dmi_system_id dmi_switches_allow_list[] = {
186 {
187 .matches = {
188 DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "31" /* Convertible */),
189 },
190 },
191 {
192 .matches = {
193 DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "32" /* Detachable */),
194 },
195 },
196 {
197 .matches = {
198 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
199 DMI_MATCH(DMI_PRODUCT_NAME, "Venue 11 Pro 7130"),
200 },
201 },
202 {
203 .matches = {
204 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
205 DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion 13 x360 PC"),
206 },
207 },
208 {
209 .matches = {
210 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
211 DMI_MATCH(DMI_PRODUCT_NAME, "Switch SA5-271"),
212 },
213 },
214 {
215 .matches = {
216 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
217 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7352"),
218 },
219 },
220 {} /* Array terminator */
221};
222
223static bool intel_vbtn_has_switches(acpi_handle handle)
224{
225 unsigned long long vgbs;
226 acpi_status status;
227
228 if (!dmi_check_system(dmi_switches_allow_list))
229 return false;
230
231 status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
232 return ACPI_SUCCESS(status);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000233}
234
235static int intel_vbtn_probe(struct platform_device *device)
236{
237 acpi_handle handle = ACPI_HANDLE(&device->dev);
238 struct intel_vbtn_priv *priv;
239 acpi_status status;
240 int err;
241
242 status = acpi_evaluate_object(handle, "VBDL", NULL, NULL);
243 if (ACPI_FAILURE(status)) {
244 dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n");
245 return -ENODEV;
246 }
247
248 priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
249 if (!priv)
250 return -ENOMEM;
251 dev_set_drvdata(&device->dev, priv);
252
Olivier Deprez0e641232021-09-23 10:07:05 +0200253 priv->has_switches = intel_vbtn_has_switches(handle);
254
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000255 err = intel_vbtn_input_setup(device);
256 if (err) {
257 pr_err("Failed to setup Intel Virtual Button\n");
258 return err;
259 }
260
Olivier Deprez0e641232021-09-23 10:07:05 +0200261 if (priv->has_switches)
262 detect_tablet_mode(device);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000263
264 status = acpi_install_notify_handler(handle,
265 ACPI_DEVICE_NOTIFY,
266 notify_handler,
267 device);
268 if (ACPI_FAILURE(status))
269 return -EBUSY;
270
271 device_init_wakeup(&device->dev, true);
David Brazdil0f672f62019-12-10 10:32:29 +0000272 /*
273 * In order for system wakeup to work, the EC GPE has to be marked as
274 * a wakeup one, so do that here (this setting will persist, but it has
275 * no effect until the wakeup mask is set for the EC GPE).
276 */
277 acpi_ec_mark_gpe_for_wake();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000278 return 0;
279}
280
281static int intel_vbtn_remove(struct platform_device *device)
282{
283 acpi_handle handle = ACPI_HANDLE(&device->dev);
284
285 device_init_wakeup(&device->dev, false);
286 acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
287
288 /*
289 * Even if we failed to shut off the event stream, we can still
290 * safely detach from the device.
291 */
292 return 0;
293}
294
295static int intel_vbtn_pm_prepare(struct device *dev)
296{
David Brazdil0f672f62019-12-10 10:32:29 +0000297 if (device_may_wakeup(dev)) {
298 struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000299
David Brazdil0f672f62019-12-10 10:32:29 +0000300 priv->wakeup_mode = true;
301 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000302 return 0;
303}
304
David Brazdil0f672f62019-12-10 10:32:29 +0000305static void intel_vbtn_pm_complete(struct device *dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000306{
307 struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
308
309 priv->wakeup_mode = false;
David Brazdil0f672f62019-12-10 10:32:29 +0000310}
311
312static int intel_vbtn_pm_resume(struct device *dev)
313{
314 intel_vbtn_pm_complete(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000315 return 0;
316}
317
318static const struct dev_pm_ops intel_vbtn_pm_ops = {
319 .prepare = intel_vbtn_pm_prepare,
David Brazdil0f672f62019-12-10 10:32:29 +0000320 .complete = intel_vbtn_pm_complete,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000321 .resume = intel_vbtn_pm_resume,
322 .restore = intel_vbtn_pm_resume,
323 .thaw = intel_vbtn_pm_resume,
324};
325
326static struct platform_driver intel_vbtn_pl_driver = {
327 .driver = {
328 .name = "intel-vbtn",
329 .acpi_match_table = intel_vbtn_ids,
330 .pm = &intel_vbtn_pm_ops,
331 },
332 .probe = intel_vbtn_probe,
333 .remove = intel_vbtn_remove,
334};
335MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids);
336
337static acpi_status __init
338check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
339{
340 const struct acpi_device_id *ids = context;
341 struct acpi_device *dev;
342
343 if (acpi_bus_get_device(handle, &dev) != 0)
344 return AE_OK;
345
346 if (acpi_match_device_ids(dev, ids) == 0)
Olivier Deprez0e641232021-09-23 10:07:05 +0200347 if (!IS_ERR_OR_NULL(acpi_create_platform_device(dev, NULL)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000348 dev_info(&dev->dev,
349 "intel-vbtn: created platform device\n");
350
351 return AE_OK;
352}
353
354static int __init intel_vbtn_init(void)
355{
356 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
357 ACPI_UINT32_MAX, check_acpi_dev, NULL,
358 (void *)intel_vbtn_ids, NULL);
359
360 return platform_driver_register(&intel_vbtn_pl_driver);
361}
362module_init(intel_vbtn_init);
363
364static void __exit intel_vbtn_exit(void)
365{
366 platform_driver_unregister(&intel_vbtn_pl_driver);
367}
368module_exit(intel_vbtn_exit);