Commit | Line | Data |
---|---|---|
00f49ee7 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
332e0812 AK |
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> | |
332e0812 AK |
7 | */ |
8 | ||
bd1b27e2 | 9 | #include <linux/acpi.h> |
de9647ef | 10 | #include <linux/dmi.h> |
bd1b27e2 AS |
11 | #include <linux/input.h> |
12 | #include <linux/input/sparse-keymap.h> | |
332e0812 AK |
13 | #include <linux/kernel.h> |
14 | #include <linux/module.h> | |
332e0812 | 15 | #include <linux/platform_device.h> |
91f9e850 | 16 | #include <linux/suspend.h> |
3afeacfd | 17 | #include "../dual_accel_detect.h" |
332e0812 | 18 | |
a4327979 HG |
19 | /* Returned when NOT in tablet mode on some HP Stream x360 11 models */ |
20 | #define VGBS_TABLET_MODE_FLAG_ALT 0x10 | |
30323fb6 | 21 | /* When NOT in tablet mode, VGBS returns with the flag 0x40 */ |
a4327979 HG |
22 | #define VGBS_TABLET_MODE_FLAG 0x40 |
23 | #define VGBS_DOCK_MODE_FLAG 0x80 | |
24 | ||
25 | #define VGBS_TABLET_MODE_FLAGS (VGBS_TABLET_MODE_FLAG | VGBS_TABLET_MODE_FLAG_ALT) | |
30323fb6 | 26 | |
332e0812 AK |
27 | MODULE_LICENSE("GPL"); |
28 | MODULE_AUTHOR("AceLan Kao"); | |
29 | ||
30 | static const struct acpi_device_id intel_vbtn_ids[] = { | |
31 | {"INT33D6", 0}, | |
32 | {"", 0}, | |
33 | }; | |
807e92d1 | 34 | MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids); |
332e0812 AK |
35 | |
36 | /* In theory, these are HID usages. */ | |
37 | static const struct key_entry intel_vbtn_keymap[] = { | |
c801603e ML |
38 | { KE_KEY, 0xC0, { KEY_POWER } }, /* power key press */ |
39 | { KE_IGNORE, 0xC1, { KEY_POWER } }, /* power key release */ | |
9678d0ef SB |
40 | { KE_KEY, 0xC2, { KEY_LEFTMETA } }, /* 'Windows' key press */ |
41 | { KE_KEY, 0xC3, { KEY_LEFTMETA } }, /* 'Windows' key release */ | |
8d9e2997 MM |
42 | { KE_KEY, 0xC4, { KEY_VOLUMEUP } }, /* volume-up key press */ |
43 | { KE_IGNORE, 0xC5, { KEY_VOLUMEUP } }, /* volume-up key release */ | |
44 | { KE_KEY, 0xC6, { KEY_VOLUMEDOWN } }, /* volume-down key press */ | |
45 | { KE_IGNORE, 0xC7, { KEY_VOLUMEDOWN } }, /* volume-down key release */ | |
3d5d95d3 SB |
46 | { KE_KEY, 0xC8, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key press */ |
47 | { KE_KEY, 0xC9, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key release */ | |
034b8c2e | 48 | { KE_END } |
f6ba5249 HG |
49 | }; |
50 | ||
51 | static const struct key_entry intel_vbtn_switchmap[] = { | |
2728f39d HG |
52 | /* |
53 | * SW_DOCK should only be reported for docking stations, but DSDTs using the | |
54 | * intel-vbtn code, always seem to use this for 2-in-1s / convertibles and set | |
55 | * SW_DOCK=1 when in laptop-mode (in tandem with setting SW_TABLET_MODE=0). | |
56 | * This causes userspace to think the laptop is docked to a port-replicator | |
57 | * and to disable suspend-on-lid-close, which is undesirable. | |
58 | * Map the dock events to KEY_IGNORE to avoid this broken SW_DOCK reporting. | |
59 | */ | |
60 | { KE_IGNORE, 0xCA, { .sw = { SW_DOCK, 1 } } }, /* Docked */ | |
61 | { KE_IGNORE, 0xCB, { .sw = { SW_DOCK, 0 } } }, /* Undocked */ | |
1c828496 SB |
62 | { KE_SW, 0xCC, { .sw = { SW_TABLET_MODE, 1 } } }, /* Tablet */ |
63 | { KE_SW, 0xCD, { .sw = { SW_TABLET_MODE, 0 } } }, /* Laptop */ | |
034b8c2e | 64 | { KE_END } |
332e0812 AK |
65 | }; |
66 | ||
67 | struct intel_vbtn_priv { | |
034b8c2e HG |
68 | struct input_dev *buttons_dev; |
69 | struct input_dev *switches_dev; | |
153cca9c | 70 | bool dual_accel; |
d307f172 | 71 | bool has_buttons; |
990fbb48 | 72 | bool has_switches; |
91f9e850 | 73 | bool wakeup_mode; |
332e0812 AK |
74 | }; |
75 | ||
14c200b7 | 76 | static void detect_tablet_mode(struct device *dev) |
f913c308 | 77 | { |
14c200b7 HG |
78 | struct intel_vbtn_priv *priv = dev_get_drvdata(dev); |
79 | acpi_handle handle = ACPI_HANDLE(dev); | |
f913c308 HG |
80 | unsigned long long vgbs; |
81 | acpi_status status; | |
82 | int m; | |
83 | ||
84 | status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs); | |
85 | if (ACPI_FAILURE(status)) | |
86 | return; | |
87 | ||
a4327979 | 88 | m = !(vgbs & VGBS_TABLET_MODE_FLAGS); |
034b8c2e | 89 | input_report_switch(priv->switches_dev, SW_TABLET_MODE, m); |
a4327979 | 90 | m = (vgbs & VGBS_DOCK_MODE_FLAG) ? 1 : 0; |
034b8c2e | 91 | input_report_switch(priv->switches_dev, SW_DOCK, m); |
14c200b7 HG |
92 | |
93 | input_sync(priv->switches_dev); | |
f913c308 HG |
94 | } |
95 | ||
034b8c2e HG |
96 | /* |
97 | * Note this unconditionally creates the 2 input_dev-s and sets up | |
98 | * the sparse-keymaps. Only the registration is conditional on | |
99 | * have_buttons / have_switches. This is done so that the notify | |
100 | * handler can always call sparse_keymap_entry_from_scancode() | |
101 | * on the input_dev-s do determine the event type. | |
102 | */ | |
332e0812 AK |
103 | static int intel_vbtn_input_setup(struct platform_device *device) |
104 | { | |
105 | struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev); | |
034b8c2e | 106 | int ret; |
f6ba5249 | 107 | |
034b8c2e HG |
108 | priv->buttons_dev = devm_input_allocate_device(&device->dev); |
109 | if (!priv->buttons_dev) | |
110 | return -ENOMEM; | |
f6ba5249 | 111 | |
034b8c2e HG |
112 | ret = sparse_keymap_setup(priv->buttons_dev, intel_vbtn_keymap, NULL); |
113 | if (ret) | |
114 | return ret; | |
f6ba5249 | 115 | |
034b8c2e HG |
116 | priv->buttons_dev->dev.parent = &device->dev; |
117 | priv->buttons_dev->name = "Intel Virtual Buttons"; | |
118 | priv->buttons_dev->id.bustype = BUS_HOST; | |
119 | ||
120 | if (priv->has_buttons) { | |
121 | ret = input_register_device(priv->buttons_dev); | |
122 | if (ret) | |
123 | return ret; | |
124 | } | |
332e0812 | 125 | |
034b8c2e HG |
126 | priv->switches_dev = devm_input_allocate_device(&device->dev); |
127 | if (!priv->switches_dev) | |
332e0812 AK |
128 | return -ENOMEM; |
129 | ||
034b8c2e | 130 | ret = sparse_keymap_setup(priv->switches_dev, intel_vbtn_switchmap, NULL); |
332e0812 | 131 | if (ret) |
bb9ad484 | 132 | return ret; |
332e0812 | 133 | |
034b8c2e HG |
134 | priv->switches_dev->dev.parent = &device->dev; |
135 | priv->switches_dev->name = "Intel Virtual Switches"; | |
136 | priv->switches_dev->id.bustype = BUS_HOST; | |
332e0812 | 137 | |
034b8c2e | 138 | if (priv->has_switches) { |
14c200b7 | 139 | detect_tablet_mode(&device->dev); |
dd950f16 | 140 | |
034b8c2e HG |
141 | ret = input_register_device(priv->switches_dev); |
142 | if (ret) | |
143 | return ret; | |
144 | } | |
145 | ||
146 | return 0; | |
332e0812 AK |
147 | } |
148 | ||
149 | static void notify_handler(acpi_handle handle, u32 event, void *context) | |
150 | { | |
151 | struct platform_device *device = context; | |
152 | struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev); | |
1c3fdf12 | 153 | unsigned int val = !(event & 1); /* Even=press, Odd=release */ |
cb1921b1 | 154 | const struct key_entry *ke, *ke_rel; |
034b8c2e | 155 | struct input_dev *input_dev; |
95f38fd4 | 156 | bool autorelease; |
3a2f53cd | 157 | int ret; |
332e0812 | 158 | |
034b8c2e HG |
159 | if ((ke = sparse_keymap_entry_from_scancode(priv->buttons_dev, event))) { |
160 | if (!priv->has_buttons) { | |
161 | dev_warn(&device->dev, "Warning: received a button event on a device without buttons, please report this.\n"); | |
162 | return; | |
163 | } | |
164 | input_dev = priv->buttons_dev; | |
165 | } else if ((ke = sparse_keymap_entry_from_scancode(priv->switches_dev, event))) { | |
166 | if (!priv->has_switches) { | |
153cca9c HG |
167 | /* See dual_accel_detect.h for more info */ |
168 | if (priv->dual_accel) | |
169 | return; | |
170 | ||
3a2f53cd HG |
171 | dev_info(&device->dev, "Registering Intel Virtual Switches input-dev after receiving a switch event\n"); |
172 | ret = input_register_device(priv->switches_dev); | |
173 | if (ret) | |
174 | return; | |
175 | ||
176 | priv->has_switches = true; | |
034b8c2e HG |
177 | } |
178 | input_dev = priv->switches_dev; | |
179 | } else { | |
180 | dev_dbg(&device->dev, "unknown event index 0x%x\n", event); | |
181 | return; | |
182 | } | |
5862b4df | 183 | |
034b8c2e | 184 | if (priv->wakeup_mode) { |
5862b4df HG |
185 | pm_wakeup_hard_event(&device->dev); |
186 | ||
187 | /* | |
188 | * Skip reporting an evdev event for button wake events, | |
189 | * mirroring how the drivers/acpi/button.c code skips this too. | |
190 | */ | |
191 | if (ke->type == KE_KEY) | |
91f9e850 | 192 | return; |
1c3fdf12 | 193 | } |
95f38fd4 | 194 | |
1c3fdf12 DHV |
195 | /* |
196 | * Even press events are autorelease if there is no corresponding odd | |
197 | * release event, or if the odd event is KE_IGNORE. | |
198 | */ | |
034b8c2e | 199 | ke_rel = sparse_keymap_entry_from_scancode(input_dev, event | 1); |
1c3fdf12 | 200 | autorelease = val && (!ke_rel || ke_rel->type == KE_IGNORE); |
95f38fd4 | 201 | |
034b8c2e | 202 | sparse_keymap_report_event(input_dev, event, val, autorelease); |
332e0812 AK |
203 | } |
204 | ||
8169bd3e HG |
205 | /* |
206 | * There are several laptops (non 2-in-1) models out there which support VGBS, | |
207 | * but simply always return 0, which we translate to SW_TABLET_MODE=1. This in | |
208 | * turn causes userspace (libinput) to suppress events from the builtin | |
209 | * keyboard and touchpad, making the laptop essentially unusable. | |
210 | * | |
211 | * Since the problem of wrongly reporting SW_TABLET_MODE=1 in combination | |
212 | * with libinput, leads to a non-usable system. Where as OTOH many people will | |
213 | * not even notice when SW_TABLET_MODE is not being reported, a DMI based allow | |
214 | * list is used here. This list mainly matches on the chassis-type of 2-in-1s. | |
215 | * | |
216 | * There are also some 2-in-1s which use the intel-vbtn ACPI interface to report | |
217 | * SW_TABLET_MODE with a chassis-type of 8 ("Portable") or 10 ("Notebook"), | |
218 | * these are matched on a per model basis, since many normal laptops with a | |
219 | * possible broken VGBS ACPI-method also use these chassis-types. | |
220 | */ | |
221 | static const struct dmi_system_id dmi_switches_allow_list[] = { | |
222 | { | |
223 | .matches = { | |
224 | DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "31" /* Convertible */), | |
225 | }, | |
226 | }, | |
227 | { | |
228 | .matches = { | |
229 | DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "32" /* Detachable */), | |
230 | }, | |
231 | }, | |
232 | { | |
233 | .matches = { | |
234 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
235 | DMI_MATCH(DMI_PRODUCT_NAME, "Venue 11 Pro 7130"), | |
236 | }, | |
237 | }, | |
07b21199 MV |
238 | { |
239 | .matches = { | |
240 | DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"), | |
241 | DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion 13 x360 PC"), | |
242 | }, | |
243 | }, | |
fe600099 CG |
244 | { |
245 | .matches = { | |
246 | DMI_MATCH(DMI_SYS_VENDOR, "Acer"), | |
247 | DMI_MATCH(DMI_PRODUCT_NAME, "Switch SA5-271"), | |
248 | }, | |
249 | }, | |
fcd38f17 AG |
250 | { |
251 | .matches = { | |
252 | DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."), | |
253 | DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7352"), | |
254 | }, | |
255 | }, | |
8169bd3e HG |
256 | {} /* Array terminator */ |
257 | }; | |
258 | ||
153cca9c | 259 | static bool intel_vbtn_has_switches(acpi_handle handle, bool dual_accel) |
990fbb48 | 260 | { |
990fbb48 HG |
261 | unsigned long long vgbs; |
262 | acpi_status status; | |
263 | ||
153cca9c HG |
264 | /* See dual_accel_detect.h for more info */ |
265 | if (dual_accel) | |
266 | return false; | |
267 | ||
8169bd3e | 268 | if (!dmi_check_system(dmi_switches_allow_list)) |
1fac39fd | 269 | return false; |
1fac39fd | 270 | |
990fbb48 HG |
271 | status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs); |
272 | return ACPI_SUCCESS(status); | |
273 | } | |
274 | ||
de9647ef ML |
275 | static int intel_vbtn_probe(struct platform_device *device) |
276 | { | |
332e0812 | 277 | acpi_handle handle = ACPI_HANDLE(&device->dev); |
153cca9c | 278 | bool dual_accel, has_buttons, has_switches; |
332e0812 AK |
279 | struct intel_vbtn_priv *priv; |
280 | acpi_status status; | |
281 | int err; | |
282 | ||
153cca9c | 283 | dual_accel = dual_accel_detect(); |
26173179 | 284 | has_buttons = acpi_has_method(handle, "VBDL"); |
153cca9c | 285 | has_switches = intel_vbtn_has_switches(handle, dual_accel); |
d307f172 HG |
286 | |
287 | if (!has_buttons && !has_switches) { | |
332e0812 AK |
288 | dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n"); |
289 | return -ENODEV; | |
290 | } | |
291 | ||
292 | priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL); | |
293 | if (!priv) | |
294 | return -ENOMEM; | |
295 | dev_set_drvdata(&device->dev, priv); | |
296 | ||
153cca9c | 297 | priv->dual_accel = dual_accel; |
d307f172 HG |
298 | priv->has_buttons = has_buttons; |
299 | priv->has_switches = has_switches; | |
990fbb48 | 300 | |
332e0812 AK |
301 | err = intel_vbtn_input_setup(device); |
302 | if (err) { | |
303 | pr_err("Failed to setup Intel Virtual Button\n"); | |
304 | return err; | |
305 | } | |
306 | ||
307 | status = acpi_install_notify_handler(handle, | |
308 | ACPI_DEVICE_NOTIFY, | |
309 | notify_handler, | |
310 | device); | |
bb9ad484 AL |
311 | if (ACPI_FAILURE(status)) |
312 | return -EBUSY; | |
332e0812 | 313 | |
26173179 HG |
314 | if (has_buttons) { |
315 | status = acpi_evaluate_object(handle, "VBDL", NULL, NULL); | |
316 | if (ACPI_FAILURE(status)) | |
317 | dev_err(&device->dev, "Error VBDL failed with ACPI status %d\n", status); | |
318 | } | |
319 | ||
91f9e850 | 320 | device_init_wakeup(&device->dev, true); |
10a08fd6 RW |
321 | /* |
322 | * In order for system wakeup to work, the EC GPE has to be marked as | |
323 | * a wakeup one, so do that here (this setting will persist, but it has | |
324 | * no effect until the wakeup mask is set for the EC GPE). | |
325 | */ | |
326 | acpi_ec_mark_gpe_for_wake(); | |
332e0812 | 327 | return 0; |
332e0812 AK |
328 | } |
329 | ||
4222272a | 330 | static void intel_vbtn_remove(struct platform_device *device) |
332e0812 AK |
331 | { |
332 | acpi_handle handle = ACPI_HANDLE(&device->dev); | |
333 | ||
b758dbd5 | 334 | device_init_wakeup(&device->dev, false); |
332e0812 | 335 | acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler); |
332e0812 AK |
336 | } |
337 | ||
91f9e850 RW |
338 | static int intel_vbtn_pm_prepare(struct device *dev) |
339 | { | |
10a08fd6 RW |
340 | if (device_may_wakeup(dev)) { |
341 | struct intel_vbtn_priv *priv = dev_get_drvdata(dev); | |
91f9e850 | 342 | |
10a08fd6 | 343 | priv->wakeup_mode = true; |
10a08fd6 | 344 | } |
91f9e850 RW |
345 | return 0; |
346 | } | |
347 | ||
31eb8457 | 348 | static void intel_vbtn_pm_complete(struct device *dev) |
91f9e850 | 349 | { |
31eb8457 | 350 | struct intel_vbtn_priv *priv = dev_get_drvdata(dev); |
91f9e850 | 351 | |
b90ff355 | 352 | priv->wakeup_mode = false; |
31eb8457 RW |
353 | } |
354 | ||
355 | static int intel_vbtn_pm_resume(struct device *dev) | |
356 | { | |
14c200b7 HG |
357 | struct intel_vbtn_priv *priv = dev_get_drvdata(dev); |
358 | ||
31eb8457 | 359 | intel_vbtn_pm_complete(dev); |
14c200b7 HG |
360 | |
361 | if (priv->has_switches) | |
362 | detect_tablet_mode(dev); | |
363 | ||
91f9e850 RW |
364 | return 0; |
365 | } | |
366 | ||
367 | static const struct dev_pm_ops intel_vbtn_pm_ops = { | |
368 | .prepare = intel_vbtn_pm_prepare, | |
31eb8457 | 369 | .complete = intel_vbtn_pm_complete, |
91f9e850 RW |
370 | .resume = intel_vbtn_pm_resume, |
371 | .restore = intel_vbtn_pm_resume, | |
372 | .thaw = intel_vbtn_pm_resume, | |
373 | }; | |
374 | ||
332e0812 AK |
375 | static struct platform_driver intel_vbtn_pl_driver = { |
376 | .driver = { | |
377 | .name = "intel-vbtn", | |
378 | .acpi_match_table = intel_vbtn_ids, | |
91f9e850 | 379 | .pm = &intel_vbtn_pm_ops, |
332e0812 AK |
380 | }, |
381 | .probe = intel_vbtn_probe, | |
4222272a | 382 | .remove_new = intel_vbtn_remove, |
332e0812 | 383 | }; |
332e0812 AK |
384 | |
385 | static acpi_status __init | |
386 | check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv) | |
387 | { | |
388 | const struct acpi_device_id *ids = context; | |
f7e62c58 | 389 | struct acpi_device *dev = acpi_fetch_acpi_dev(handle); |
332e0812 | 390 | |
f7e62c58 | 391 | if (dev && acpi_match_device_ids(dev, ids) == 0) |
64dd4a5a | 392 | if (!IS_ERR_OR_NULL(acpi_create_platform_device(dev, NULL))) |
332e0812 AK |
393 | dev_info(&dev->dev, |
394 | "intel-vbtn: created platform device\n"); | |
395 | ||
396 | return AE_OK; | |
397 | } | |
398 | ||
399 | static int __init intel_vbtn_init(void) | |
400 | { | |
401 | acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, | |
402 | ACPI_UINT32_MAX, check_acpi_dev, NULL, | |
403 | (void *)intel_vbtn_ids, NULL); | |
404 | ||
405 | return platform_driver_register(&intel_vbtn_pl_driver); | |
406 | } | |
407 | module_init(intel_vbtn_init); | |
408 | ||
409 | static void __exit intel_vbtn_exit(void) | |
410 | { | |
411 | platform_driver_unregister(&intel_vbtn_pl_driver); | |
412 | } | |
413 | module_exit(intel_vbtn_exit); |