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