blob: 30a9062d2b4b8858d05846a1ffb9f6475e0288fd [file] [log] [blame]
Andy Shevchenko00f49ee2018-01-31 19:54:07 +02001// SPDX-License-Identifier: GPL-2.0+
AceLan Kao332e0812016-07-01 09:51:49 +08002/*
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>
AceLan Kao332e0812016-07-01 09:51:49 +08007 */
8
Andy Shevchenkobd1b27e2018-01-31 19:54:06 +02009#include <linux/acpi.h>
Mario Limonciellode9647e2018-02-22 13:58:42 -060010#include <linux/dmi.h>
Andy Shevchenkobd1b27e2018-01-31 19:54:06 +020011#include <linux/input.h>
12#include <linux/input/sparse-keymap.h>
AceLan Kao332e0812016-07-01 09:51:49 +080013#include <linux/kernel.h>
14#include <linux/module.h>
AceLan Kao332e0812016-07-01 09:51:49 +080015#include <linux/platform_device.h>
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +020016#include <linux/suspend.h>
AceLan Kao332e0812016-07-01 09:51:49 +080017
Hans de Goedea4327972020-09-12 11:35:32 +020018/* Returned when NOT in tablet mode on some HP Stream x360 11 models */
19#define VGBS_TABLET_MODE_FLAG_ALT 0x10
Marco Martin30323fb2018-01-29 21:27:43 +010020/* When NOT in tablet mode, VGBS returns with the flag 0x40 */
Hans de Goedea4327972020-09-12 11:35:32 +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)
Marco Martin30323fb2018-01-29 21:27:43 +010025
AceLan Kao332e0812016-07-01 09:51:49 +080026MODULE_LICENSE("GPL");
27MODULE_AUTHOR("AceLan Kao");
28
29static const struct acpi_device_id intel_vbtn_ids[] = {
30 {"INT33D6", 0},
31 {"", 0},
32};
Andy Shevchenko807e92d2020-01-28 12:26:21 +020033MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids);
AceLan Kao332e0812016-07-01 09:51:49 +080034
35/* In theory, these are HID usages. */
36static const struct key_entry intel_vbtn_keymap[] = {
Mario Limoncielloc8016032017-08-04 12:00:06 -050037 { KE_KEY, 0xC0, { KEY_POWER } }, /* power key press */
38 { KE_IGNORE, 0xC1, { KEY_POWER } }, /* power key release */
Stefan Brüns9678d0e2017-11-09 23:44:36 +010039 { KE_KEY, 0xC2, { KEY_LEFTMETA } }, /* 'Windows' key press */
40 { KE_KEY, 0xC3, { KEY_LEFTMETA } }, /* 'Windows' key release */
Maarten Maathuis8d9e2992017-04-24 23:35:21 +020041 { 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 */
Stefan Brüns3d5d95d2017-11-09 23:44:35 +010045 { KE_KEY, 0xC8, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key press */
46 { KE_KEY, 0xC9, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key release */
Hans de Goedef6ba5242020-05-02 20:29:48 +020047};
48
49static const struct key_entry intel_vbtn_switchmap[] = {
Matt Delco4f5e81a2018-06-24 17:29:40 -070050 { KE_SW, 0xCA, { .sw = { SW_DOCK, 1 } } }, /* Docked */
51 { KE_SW, 0xCB, { .sw = { SW_DOCK, 0 } } }, /* Undocked */
Stefan Brüns1c828492017-11-09 23:44:32 +010052 { KE_SW, 0xCC, { .sw = { SW_TABLET_MODE, 1 } } }, /* Tablet */
53 { KE_SW, 0xCD, { .sw = { SW_TABLET_MODE, 0 } } }, /* Laptop */
AceLan Kao332e0812016-07-01 09:51:49 +080054};
55
Hans de Goedef6ba5242020-05-02 20:29:48 +020056#define KEYMAP_LEN \
57 (ARRAY_SIZE(intel_vbtn_keymap) + ARRAY_SIZE(intel_vbtn_switchmap) + 1)
58
AceLan Kao332e0812016-07-01 09:51:49 +080059struct intel_vbtn_priv {
Hans de Goedef6ba5242020-05-02 20:29:48 +020060 struct key_entry keymap[KEYMAP_LEN];
AceLan Kao332e0812016-07-01 09:51:49 +080061 struct input_dev *input_dev;
Hans de Goeded307f172020-05-02 20:29:51 +020062 bool has_buttons;
Hans de Goede990fbb42020-05-02 20:29:49 +020063 bool has_switches;
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +020064 bool wakeup_mode;
AceLan Kao332e0812016-07-01 09:51:49 +080065};
66
Hans de Goedef913c3082020-05-10 14:20:46 +020067static void detect_tablet_mode(struct platform_device *device)
68{
69 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
70 acpi_handle handle = ACPI_HANDLE(&device->dev);
71 unsigned long long vgbs;
72 acpi_status status;
73 int m;
74
75 status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
76 if (ACPI_FAILURE(status))
77 return;
78
Hans de Goedea4327972020-09-12 11:35:32 +020079 m = !(vgbs & VGBS_TABLET_MODE_FLAGS);
Hans de Goedef913c3082020-05-10 14:20:46 +020080 input_report_switch(priv->input_dev, SW_TABLET_MODE, m);
Hans de Goedea4327972020-09-12 11:35:32 +020081 m = (vgbs & VGBS_DOCK_MODE_FLAG) ? 1 : 0;
Hans de Goedef913c3082020-05-10 14:20:46 +020082 input_report_switch(priv->input_dev, SW_DOCK, m);
83}
84
AceLan Kao332e0812016-07-01 09:51:49 +080085static int intel_vbtn_input_setup(struct platform_device *device)
86{
87 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
Hans de Goedef6ba5242020-05-02 20:29:48 +020088 int ret, keymap_len = 0;
89
Hans de Goeded307f172020-05-02 20:29:51 +020090 if (priv->has_buttons) {
Hans de Goedef6ba5242020-05-02 20:29:48 +020091 memcpy(&priv->keymap[keymap_len], intel_vbtn_keymap,
92 ARRAY_SIZE(intel_vbtn_keymap) *
93 sizeof(struct key_entry));
94 keymap_len += ARRAY_SIZE(intel_vbtn_keymap);
95 }
96
Hans de Goede990fbb42020-05-02 20:29:49 +020097 if (priv->has_switches) {
Hans de Goedef6ba5242020-05-02 20:29:48 +020098 memcpy(&priv->keymap[keymap_len], intel_vbtn_switchmap,
99 ARRAY_SIZE(intel_vbtn_switchmap) *
100 sizeof(struct key_entry));
101 keymap_len += ARRAY_SIZE(intel_vbtn_switchmap);
102 }
103
104 priv->keymap[keymap_len].type = KE_END;
AceLan Kao332e0812016-07-01 09:51:49 +0800105
Axel Linbb9ad482016-09-20 17:01:24 +0800106 priv->input_dev = devm_input_allocate_device(&device->dev);
AceLan Kao332e0812016-07-01 09:51:49 +0800107 if (!priv->input_dev)
108 return -ENOMEM;
109
Hans de Goedef6ba5242020-05-02 20:29:48 +0200110 ret = sparse_keymap_setup(priv->input_dev, priv->keymap, NULL);
AceLan Kao332e0812016-07-01 09:51:49 +0800111 if (ret)
Axel Linbb9ad482016-09-20 17:01:24 +0800112 return ret;
AceLan Kao332e0812016-07-01 09:51:49 +0800113
114 priv->input_dev->dev.parent = &device->dev;
115 priv->input_dev->name = "Intel Virtual Button driver";
116 priv->input_dev->id.bustype = BUS_HOST;
117
Hans de Goededd950f12020-05-10 14:20:47 +0200118 if (priv->has_switches)
119 detect_tablet_mode(device);
120
Axel Linbb9ad482016-09-20 17:01:24 +0800121 return input_register_device(priv->input_dev);
AceLan Kao332e0812016-07-01 09:51:49 +0800122}
123
124static void notify_handler(acpi_handle handle, u32 event, void *context)
125{
126 struct platform_device *device = context;
127 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
Darren Hart (VMware)1c3fdf12017-12-08 14:57:54 -0800128 unsigned int val = !(event & 1); /* Even=press, Odd=release */
Mathew Kingcb1921b2019-05-20 16:41:24 -0600129 const struct key_entry *ke, *ke_rel;
Stefan Brüns95f38fd2017-11-09 23:44:33 +0100130 bool autorelease;
AceLan Kao332e0812016-07-01 09:51:49 +0800131
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200132 if (priv->wakeup_mode) {
Mathew Kingcb1921b2019-05-20 16:41:24 -0600133 ke = sparse_keymap_entry_from_scancode(priv->input_dev, event);
134 if (ke) {
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200135 pm_wakeup_hard_event(&device->dev);
Mathew Kingcb1921b2019-05-20 16:41:24 -0600136
137 /*
138 * Switch events like tablet mode will wake the device
139 * and report the new switch position to the input
140 * subsystem.
141 */
142 if (ke->type == KE_SW)
143 sparse_keymap_report_event(priv->input_dev,
144 event,
145 val,
146 0);
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200147 return;
148 }
Darren Hart (VMware)1c3fdf12017-12-08 14:57:54 -0800149 goto out_unknown;
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200150 }
Darren Hart (VMware)1c3fdf12017-12-08 14:57:54 -0800151
152 /*
153 * Even press events are autorelease if there is no corresponding odd
154 * release event, or if the odd event is KE_IGNORE.
155 */
156 ke_rel = sparse_keymap_entry_from_scancode(priv->input_dev, event | 1);
157 autorelease = val && (!ke_rel || ke_rel->type == KE_IGNORE);
158
159 if (sparse_keymap_report_event(priv->input_dev, event, val, autorelease))
160 return;
161
162out_unknown:
Alex Hunga9c37b72017-07-20 20:56:48 -0700163 dev_dbg(&device->dev, "unknown event index 0x%x\n", event);
AceLan Kao332e0812016-07-01 09:51:49 +0800164}
165
Hans de Goeded307f172020-05-02 20:29:51 +0200166static bool intel_vbtn_has_buttons(acpi_handle handle)
167{
168 acpi_status status;
169
170 status = acpi_evaluate_object(handle, "VBDL", NULL, NULL);
171 return ACPI_SUCCESS(status);
172}
173
Hans de Goede8169bd32020-09-30 15:19:05 +0200174/*
175 * There are several laptops (non 2-in-1) models out there which support VGBS,
176 * but simply always return 0, which we translate to SW_TABLET_MODE=1. This in
177 * turn causes userspace (libinput) to suppress events from the builtin
178 * keyboard and touchpad, making the laptop essentially unusable.
179 *
180 * Since the problem of wrongly reporting SW_TABLET_MODE=1 in combination
181 * with libinput, leads to a non-usable system. Where as OTOH many people will
182 * not even notice when SW_TABLET_MODE is not being reported, a DMI based allow
183 * list is used here. This list mainly matches on the chassis-type of 2-in-1s.
184 *
185 * There are also some 2-in-1s which use the intel-vbtn ACPI interface to report
186 * SW_TABLET_MODE with a chassis-type of 8 ("Portable") or 10 ("Notebook"),
187 * these are matched on a per model basis, since many normal laptops with a
188 * possible broken VGBS ACPI-method also use these chassis-types.
189 */
190static const struct dmi_system_id dmi_switches_allow_list[] = {
191 {
192 .matches = {
193 DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "31" /* Convertible */),
194 },
195 },
196 {
197 .matches = {
198 DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "32" /* Detachable */),
199 },
200 },
201 {
202 .matches = {
203 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
204 DMI_MATCH(DMI_PRODUCT_NAME, "Venue 11 Pro 7130"),
205 },
206 },
207 {
208 .matches = {
209 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
Max Verevkin07b21192020-11-24 15:16:52 +0200210 DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion 13 x360 PC"),
211 },
212 },
Carlos Garnachofe600092020-12-01 14:57:27 +0100213 {
214 .matches = {
215 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
216 DMI_MATCH(DMI_PRODUCT_NAME, "Switch SA5-271"),
217 },
218 },
Arnold Gozumfcd38f12020-12-26 15:53:06 -0500219 {
220 .matches = {
221 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
222 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7352"),
223 },
224 },
Hans de Goede8169bd32020-09-30 15:19:05 +0200225 {} /* Array terminator */
226};
227
Hans de Goede990fbb42020-05-02 20:29:49 +0200228static bool intel_vbtn_has_switches(acpi_handle handle)
229{
Hans de Goede990fbb42020-05-02 20:29:49 +0200230 unsigned long long vgbs;
231 acpi_status status;
232
Hans de Goede8169bd32020-09-30 15:19:05 +0200233 if (!dmi_check_system(dmi_switches_allow_list))
Hans de Goede990fbb42020-05-02 20:29:49 +0200234 return false;
235
236 status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
237 return ACPI_SUCCESS(status);
238}
239
AceLan Kao332e0812016-07-01 09:51:49 +0800240static int intel_vbtn_probe(struct platform_device *device)
241{
242 acpi_handle handle = ACPI_HANDLE(&device->dev);
Hans de Goeded307f172020-05-02 20:29:51 +0200243 bool has_buttons, has_switches;
AceLan Kao332e0812016-07-01 09:51:49 +0800244 struct intel_vbtn_priv *priv;
245 acpi_status status;
246 int err;
247
Hans de Goeded307f172020-05-02 20:29:51 +0200248 has_buttons = intel_vbtn_has_buttons(handle);
249 has_switches = intel_vbtn_has_switches(handle);
250
251 if (!has_buttons && !has_switches) {
AceLan Kao332e0812016-07-01 09:51:49 +0800252 dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n");
253 return -ENODEV;
254 }
255
256 priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
257 if (!priv)
258 return -ENOMEM;
259 dev_set_drvdata(&device->dev, priv);
260
Hans de Goeded307f172020-05-02 20:29:51 +0200261 priv->has_buttons = has_buttons;
262 priv->has_switches = has_switches;
Hans de Goede990fbb42020-05-02 20:29:49 +0200263
AceLan Kao332e0812016-07-01 09:51:49 +0800264 err = intel_vbtn_input_setup(device);
265 if (err) {
266 pr_err("Failed to setup Intel Virtual Button\n");
267 return err;
268 }
269
270 status = acpi_install_notify_handler(handle,
271 ACPI_DEVICE_NOTIFY,
272 notify_handler,
273 device);
Axel Linbb9ad482016-09-20 17:01:24 +0800274 if (ACPI_FAILURE(status))
275 return -EBUSY;
AceLan Kao332e0812016-07-01 09:51:49 +0800276
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200277 device_init_wakeup(&device->dev, true);
Rafael J. Wysocki10a08fd2019-07-30 11:55:59 +0200278 /*
279 * In order for system wakeup to work, the EC GPE has to be marked as
280 * a wakeup one, so do that here (this setting will persist, but it has
281 * no effect until the wakeup mask is set for the EC GPE).
282 */
283 acpi_ec_mark_gpe_for_wake();
AceLan Kao332e0812016-07-01 09:51:49 +0800284 return 0;
AceLan Kao332e0812016-07-01 09:51:49 +0800285}
286
287static int intel_vbtn_remove(struct platform_device *device)
288{
289 acpi_handle handle = ACPI_HANDLE(&device->dev);
290
Rafael J. Wysockib758dbd2018-02-28 12:09:56 +0100291 device_init_wakeup(&device->dev, false);
AceLan Kao332e0812016-07-01 09:51:49 +0800292 acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
293
294 /*
295 * Even if we failed to shut off the event stream, we can still
296 * safely detach from the device.
297 */
298 return 0;
299}
300
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200301static int intel_vbtn_pm_prepare(struct device *dev)
302{
Rafael J. Wysocki10a08fd2019-07-30 11:55:59 +0200303 if (device_may_wakeup(dev)) {
304 struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200305
Rafael J. Wysocki10a08fd2019-07-30 11:55:59 +0200306 priv->wakeup_mode = true;
Rafael J. Wysocki10a08fd2019-07-30 11:55:59 +0200307 }
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200308 return 0;
309}
310
Rafael J. Wysocki31eb8452019-08-08 11:39:17 +0200311static void intel_vbtn_pm_complete(struct device *dev)
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200312{
Rafael J. Wysocki31eb8452019-08-08 11:39:17 +0200313 struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200314
Rafael J. Wysockib90ff352019-08-21 11:40:19 +0200315 priv->wakeup_mode = false;
Rafael J. Wysocki31eb8452019-08-08 11:39:17 +0200316}
317
318static int intel_vbtn_pm_resume(struct device *dev)
319{
320 intel_vbtn_pm_complete(dev);
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200321 return 0;
322}
323
324static const struct dev_pm_ops intel_vbtn_pm_ops = {
325 .prepare = intel_vbtn_pm_prepare,
Rafael J. Wysocki31eb8452019-08-08 11:39:17 +0200326 .complete = intel_vbtn_pm_complete,
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200327 .resume = intel_vbtn_pm_resume,
328 .restore = intel_vbtn_pm_resume,
329 .thaw = intel_vbtn_pm_resume,
330};
331
AceLan Kao332e0812016-07-01 09:51:49 +0800332static struct platform_driver intel_vbtn_pl_driver = {
333 .driver = {
334 .name = "intel-vbtn",
335 .acpi_match_table = intel_vbtn_ids,
Rafael J. Wysocki91f9e852017-06-08 02:15:52 +0200336 .pm = &intel_vbtn_pm_ops,
AceLan Kao332e0812016-07-01 09:51:49 +0800337 },
338 .probe = intel_vbtn_probe,
339 .remove = intel_vbtn_remove,
340};
AceLan Kao332e0812016-07-01 09:51:49 +0800341
342static acpi_status __init
343check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
344{
345 const struct acpi_device_id *ids = context;
346 struct acpi_device *dev;
347
348 if (acpi_bus_get_device(handle, &dev) != 0)
349 return AE_OK;
350
351 if (acpi_match_device_ids(dev, ids) == 0)
Lu Wei64dd4a52020-07-10 17:30:18 +0800352 if (!IS_ERR_OR_NULL(acpi_create_platform_device(dev, NULL)))
AceLan Kao332e0812016-07-01 09:51:49 +0800353 dev_info(&dev->dev,
354 "intel-vbtn: created platform device\n");
355
356 return AE_OK;
357}
358
359static int __init intel_vbtn_init(void)
360{
361 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
362 ACPI_UINT32_MAX, check_acpi_dev, NULL,
363 (void *)intel_vbtn_ids, NULL);
364
365 return platform_driver_register(&intel_vbtn_pl_driver);
366}
367module_init(intel_vbtn_init);
368
369static void __exit intel_vbtn_exit(void)
370{
371 platform_driver_unregister(&intel_vbtn_pl_driver);
372}
373module_exit(intel_vbtn_exit);