1 // 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
18 /* When NOT in tablet mode, VGBS returns with the flag 0x40 */
19 #define TABLET_MODE_FLAG 0x40
20 #define DOCK_MODE_FLAG 0x80
21
22 MODULE_LICENSE("GPL");
23 MODULE_AUTHOR("AceLan Kao");
24
25 static const struct acpi_device_id intel_vbtn_ids[] = {
26 {"INT33D6", 0},
27 {"", 0},
28 };
29 MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids);
30
31 /* In theory, these are HID usages. */
32 static const struct key_entry intel_vbtn_keymap[] = {
33 { KE_KEY, 0xC0, { KEY_POWER } }, /* power key press */
34 { KE_IGNORE, 0xC1, { KEY_POWER } }, /* power key release */
35 { KE_KEY, 0xC2, { KEY_LEFTMETA } }, /* 'Windows' key press */
36 { KE_KEY, 0xC3, { KEY_LEFTMETA } }, /* 'Windows' key release */
37 { KE_KEY, 0xC4, { KEY_VOLUMEUP } }, /* volume-up key press */
38 { KE_IGNORE, 0xC5, { KEY_VOLUMEUP } }, /* volume-up key release */
39 { KE_KEY, 0xC6, { KEY_VOLUMEDOWN } }, /* volume-down key press */
40 { KE_IGNORE, 0xC7, { KEY_VOLUMEDOWN } }, /* volume-down key release */
41 { KE_KEY, 0xC8, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key press */
42 { KE_KEY, 0xC9, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key release */
43 };
44
45 static const struct key_entry intel_vbtn_switchmap[] = {
46 { KE_SW, 0xCA, { .sw = { SW_DOCK, 1 } } }, /* Docked */
47 { KE_SW, 0xCB, { .sw = { SW_DOCK, 0 } } }, /* Undocked */
48 { KE_SW, 0xCC, { .sw = { SW_TABLET_MODE, 1 } } }, /* Tablet */
49 { KE_SW, 0xCD, { .sw = { SW_TABLET_MODE, 0 } } }, /* Laptop */
50 };
51
52 #define KEYMAP_LEN \
53 (ARRAY_SIZE(intel_vbtn_keymap) + ARRAY_SIZE(intel_vbtn_switchmap) + 1)
54
55 struct intel_vbtn_priv {
56 struct key_entry keymap[KEYMAP_LEN];
57 struct input_dev *input_dev;
58 bool has_buttons;
59 bool has_switches;
60 bool wakeup_mode;
61 };
62
detect_tablet_mode(struct platform_device * device)63 static void detect_tablet_mode(struct platform_device *device)
64 {
65 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
66 acpi_handle handle = ACPI_HANDLE(&device->dev);
67 unsigned long long vgbs;
68 acpi_status status;
69 int m;
70
71 status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
72 if (ACPI_FAILURE(status))
73 return;
74
75 m = !(vgbs & TABLET_MODE_FLAG);
76 input_report_switch(priv->input_dev, SW_TABLET_MODE, m);
77 m = (vgbs & DOCK_MODE_FLAG) ? 1 : 0;
78 input_report_switch(priv->input_dev, SW_DOCK, m);
79 }
80
intel_vbtn_input_setup(struct platform_device * device)81 static int intel_vbtn_input_setup(struct platform_device *device)
82 {
83 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
84 int ret, keymap_len = 0;
85
86 if (priv->has_buttons) {
87 memcpy(&priv->keymap[keymap_len], intel_vbtn_keymap,
88 ARRAY_SIZE(intel_vbtn_keymap) *
89 sizeof(struct key_entry));
90 keymap_len += ARRAY_SIZE(intel_vbtn_keymap);
91 }
92
93 if (priv->has_switches) {
94 memcpy(&priv->keymap[keymap_len], intel_vbtn_switchmap,
95 ARRAY_SIZE(intel_vbtn_switchmap) *
96 sizeof(struct key_entry));
97 keymap_len += ARRAY_SIZE(intel_vbtn_switchmap);
98 }
99
100 priv->keymap[keymap_len].type = KE_END;
101
102 priv->input_dev = devm_input_allocate_device(&device->dev);
103 if (!priv->input_dev)
104 return -ENOMEM;
105
106 ret = sparse_keymap_setup(priv->input_dev, priv->keymap, NULL);
107 if (ret)
108 return ret;
109
110 priv->input_dev->dev.parent = &device->dev;
111 priv->input_dev->name = "Intel Virtual Button driver";
112 priv->input_dev->id.bustype = BUS_HOST;
113
114 if (priv->has_switches)
115 detect_tablet_mode(device);
116
117 return input_register_device(priv->input_dev);
118 }
119
notify_handler(acpi_handle handle,u32 event,void * context)120 static void notify_handler(acpi_handle handle, u32 event, void *context)
121 {
122 struct platform_device *device = context;
123 struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
124 unsigned int val = !(event & 1); /* Even=press, Odd=release */
125 const struct key_entry *ke, *ke_rel;
126 bool autorelease;
127
128 if (priv->wakeup_mode) {
129 ke = sparse_keymap_entry_from_scancode(priv->input_dev, event);
130 if (ke) {
131 pm_wakeup_hard_event(&device->dev);
132
133 /*
134 * Switch events like tablet mode will wake the device
135 * and report the new switch position to the input
136 * subsystem.
137 */
138 if (ke->type == KE_SW)
139 sparse_keymap_report_event(priv->input_dev,
140 event,
141 val,
142 0);
143 return;
144 }
145 goto out_unknown;
146 }
147
148 /*
149 * Even press events are autorelease if there is no corresponding odd
150 * release event, or if the odd event is KE_IGNORE.
151 */
152 ke_rel = sparse_keymap_entry_from_scancode(priv->input_dev, event | 1);
153 autorelease = val && (!ke_rel || ke_rel->type == KE_IGNORE);
154
155 if (sparse_keymap_report_event(priv->input_dev, event, val, autorelease))
156 return;
157
158 out_unknown:
159 dev_dbg(&device->dev, "unknown event index 0x%x\n", event);
160 }
161
intel_vbtn_has_buttons(acpi_handle handle)162 static bool intel_vbtn_has_buttons(acpi_handle handle)
163 {
164 acpi_status status;
165
166 status = acpi_evaluate_object(handle, "VBDL", NULL, NULL);
167 return ACPI_SUCCESS(status);
168 }
169
170 /*
171 * There are several laptops (non 2-in-1) models out there which support VGBS,
172 * but simply always return 0, which we translate to SW_TABLET_MODE=1. This in
173 * turn causes userspace (libinput) to suppress events from the builtin
174 * keyboard and touchpad, making the laptop essentially unusable.
175 *
176 * Since the problem of wrongly reporting SW_TABLET_MODE=1 in combination
177 * with libinput, leads to a non-usable system. Where as OTOH many people will
178 * not even notice when SW_TABLET_MODE is not being reported, a DMI based allow
179 * list is used here. This list mainly matches on the chassis-type of 2-in-1s.
180 *
181 * There are also some 2-in-1s which use the intel-vbtn ACPI interface to report
182 * SW_TABLET_MODE with a chassis-type of 8 ("Portable") or 10 ("Notebook"),
183 * these are matched on a per model basis, since many normal laptops with a
184 * possible broken VGBS ACPI-method also use these chassis-types.
185 */
186 static const struct dmi_system_id dmi_switches_allow_list[] = {
187 {
188 .matches = {
189 DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "31" /* Convertible */),
190 },
191 },
192 {
193 .matches = {
194 DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "32" /* Detachable */),
195 },
196 },
197 {
198 .matches = {
199 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
200 DMI_MATCH(DMI_PRODUCT_NAME, "Venue 11 Pro 7130"),
201 },
202 },
203 {
204 .matches = {
205 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
206 DMI_MATCH(DMI_PRODUCT_NAME, "HP Stream x360 Convertible PC 11"),
207 },
208 },
209 {
210 .matches = {
211 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
212 DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion 13 x360 PC"),
213 },
214 },
215 {} /* Array terminator */
216 };
217
intel_vbtn_has_switches(acpi_handle handle)218 static bool intel_vbtn_has_switches(acpi_handle handle)
219 {
220 unsigned long long vgbs;
221 acpi_status status;
222
223 if (!dmi_check_system(dmi_switches_allow_list))
224 return false;
225
226 status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
227 return ACPI_SUCCESS(status);
228 }
229
intel_vbtn_probe(struct platform_device * device)230 static int intel_vbtn_probe(struct platform_device *device)
231 {
232 acpi_handle handle = ACPI_HANDLE(&device->dev);
233 bool has_buttons, has_switches;
234 struct intel_vbtn_priv *priv;
235 acpi_status status;
236 int err;
237
238 has_buttons = intel_vbtn_has_buttons(handle);
239 has_switches = intel_vbtn_has_switches(handle);
240
241 if (!has_buttons && !has_switches) {
242 dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n");
243 return -ENODEV;
244 }
245
246 priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
247 if (!priv)
248 return -ENOMEM;
249 dev_set_drvdata(&device->dev, priv);
250
251 priv->has_buttons = has_buttons;
252 priv->has_switches = has_switches;
253
254 err = intel_vbtn_input_setup(device);
255 if (err) {
256 pr_err("Failed to setup Intel Virtual Button\n");
257 return err;
258 }
259
260 status = acpi_install_notify_handler(handle,
261 ACPI_DEVICE_NOTIFY,
262 notify_handler,
263 device);
264 if (ACPI_FAILURE(status))
265 return -EBUSY;
266
267 device_init_wakeup(&device->dev, true);
268 /*
269 * In order for system wakeup to work, the EC GPE has to be marked as
270 * a wakeup one, so do that here (this setting will persist, but it has
271 * no effect until the wakeup mask is set for the EC GPE).
272 */
273 acpi_ec_mark_gpe_for_wake();
274 return 0;
275 }
276
intel_vbtn_remove(struct platform_device * device)277 static int intel_vbtn_remove(struct platform_device *device)
278 {
279 acpi_handle handle = ACPI_HANDLE(&device->dev);
280
281 device_init_wakeup(&device->dev, false);
282 acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
283
284 /*
285 * Even if we failed to shut off the event stream, we can still
286 * safely detach from the device.
287 */
288 return 0;
289 }
290
intel_vbtn_pm_prepare(struct device * dev)291 static int intel_vbtn_pm_prepare(struct device *dev)
292 {
293 if (device_may_wakeup(dev)) {
294 struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
295
296 priv->wakeup_mode = true;
297 }
298 return 0;
299 }
300
intel_vbtn_pm_complete(struct device * dev)301 static void intel_vbtn_pm_complete(struct device *dev)
302 {
303 struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
304
305 priv->wakeup_mode = false;
306 }
307
intel_vbtn_pm_resume(struct device * dev)308 static int intel_vbtn_pm_resume(struct device *dev)
309 {
310 intel_vbtn_pm_complete(dev);
311 return 0;
312 }
313
314 static const struct dev_pm_ops intel_vbtn_pm_ops = {
315 .prepare = intel_vbtn_pm_prepare,
316 .complete = intel_vbtn_pm_complete,
317 .resume = intel_vbtn_pm_resume,
318 .restore = intel_vbtn_pm_resume,
319 .thaw = intel_vbtn_pm_resume,
320 };
321
322 static struct platform_driver intel_vbtn_pl_driver = {
323 .driver = {
324 .name = "intel-vbtn",
325 .acpi_match_table = intel_vbtn_ids,
326 .pm = &intel_vbtn_pm_ops,
327 },
328 .probe = intel_vbtn_probe,
329 .remove = intel_vbtn_remove,
330 };
331
332 static acpi_status __init
check_acpi_dev(acpi_handle handle,u32 lvl,void * context,void ** rv)333 check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
334 {
335 const struct acpi_device_id *ids = context;
336 struct acpi_device *dev;
337
338 if (acpi_bus_get_device(handle, &dev) != 0)
339 return AE_OK;
340
341 if (acpi_match_device_ids(dev, ids) == 0)
342 if (!IS_ERR_OR_NULL(acpi_create_platform_device(dev, NULL)))
343 dev_info(&dev->dev,
344 "intel-vbtn: created platform device\n");
345
346 return AE_OK;
347 }
348
intel_vbtn_init(void)349 static int __init intel_vbtn_init(void)
350 {
351 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
352 ACPI_UINT32_MAX, check_acpi_dev, NULL,
353 (void *)intel_vbtn_ids, NULL);
354
355 return platform_driver_register(&intel_vbtn_pl_driver);
356 }
357 module_init(intel_vbtn_init);
358
intel_vbtn_exit(void)359 static void __exit intel_vbtn_exit(void)
360 {
361 platform_driver_unregister(&intel_vbtn_pl_driver);
362 }
363 module_exit(intel_vbtn_exit);
364