GNU Linux-libre 4.19.264-gnu1
[releases.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 <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 /* Returned when NOT in tablet mode on some HP Stream x360 11 models */
19 #define VGBS_TABLET_MODE_FLAG_ALT       0x10
20 /* When NOT in tablet mode, VGBS returns with the flag 0x40 */
21 #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)
25
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("AceLan Kao");
28
29 static const struct acpi_device_id intel_vbtn_ids[] = {
30         {"INT33D6", 0},
31         {"", 0},
32 };
33
34 /* In theory, these are HID usages. */
35 static 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 */
46 };
47
48 static 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
66 struct intel_vbtn_priv {
67         struct key_entry keymap[KEYMAP_LEN];
68         struct input_dev *input_dev;
69         bool has_switches;
70         bool wakeup_mode;
71 };
72
73 static int intel_vbtn_input_setup(struct platform_device *device)
74 {
75         struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
76         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;
93
94         priv->input_dev = devm_input_allocate_device(&device->dev);
95         if (!priv->input_dev)
96                 return -ENOMEM;
97
98         ret = sparse_keymap_setup(priv->input_dev, priv->keymap, NULL);
99         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
109 static 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 */
114         const struct key_entry *ke, *ke_rel;
115         bool autorelease;
116
117         if (priv->wakeup_mode) {
118                 ke = sparse_keymap_entry_from_scancode(priv->input_dev, event);
119                 if (ke) {
120                         pm_wakeup_hard_event(&device->dev);
121
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);
132                         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
147 out_unknown:
148         dev_dbg(&device->dev, "unknown event index 0x%x\n", event);
149 }
150
151 static void detect_tablet_mode(struct platform_device *device)
152 {
153         struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
154         acpi_handle handle = ACPI_HANDLE(&device->dev);
155         unsigned long long vgbs;
156         acpi_status status;
157         int m;
158
159         status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
160         if (ACPI_FAILURE(status))
161                 return;
162
163         m = !(vgbs & VGBS_TABLET_MODE_FLAGS);
164         input_report_switch(priv->input_dev, SW_TABLET_MODE, m);
165         m = (vgbs & VGBS_DOCK_MODE_FLAG) ? 1 : 0;
166         input_report_switch(priv->input_dev, SW_DOCK, m);
167 }
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  */
185 static 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
223 static 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);
233 }
234
235 static 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
253         priv->has_switches = intel_vbtn_has_switches(handle);
254
255         err = intel_vbtn_input_setup(device);
256         if (err) {
257                 pr_err("Failed to setup Intel Virtual Button\n");
258                 return err;
259         }
260
261         if (priv->has_switches)
262                 detect_tablet_mode(device);
263
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);
272         return 0;
273 }
274
275 static int intel_vbtn_remove(struct platform_device *device)
276 {
277         acpi_handle handle = ACPI_HANDLE(&device->dev);
278
279         device_init_wakeup(&device->dev, false);
280         acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
281
282         /*
283          * Even if we failed to shut off the event stream, we can still
284          * safely detach from the device.
285          */
286         return 0;
287 }
288
289 static int intel_vbtn_pm_prepare(struct device *dev)
290 {
291         struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
292
293         priv->wakeup_mode = true;
294         return 0;
295 }
296
297 static int intel_vbtn_pm_resume(struct device *dev)
298 {
299         struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
300
301         priv->wakeup_mode = false;
302         return 0;
303 }
304
305 static const struct dev_pm_ops intel_vbtn_pm_ops = {
306         .prepare = intel_vbtn_pm_prepare,
307         .resume = intel_vbtn_pm_resume,
308         .restore = intel_vbtn_pm_resume,
309         .thaw = intel_vbtn_pm_resume,
310 };
311
312 static struct platform_driver intel_vbtn_pl_driver = {
313         .driver = {
314                 .name = "intel-vbtn",
315                 .acpi_match_table = intel_vbtn_ids,
316                 .pm = &intel_vbtn_pm_ops,
317         },
318         .probe = intel_vbtn_probe,
319         .remove = intel_vbtn_remove,
320 };
321 MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids);
322
323 static acpi_status __init
324 check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
325 {
326         const struct acpi_device_id *ids = context;
327         struct acpi_device *dev;
328
329         if (acpi_bus_get_device(handle, &dev) != 0)
330                 return AE_OK;
331
332         if (acpi_match_device_ids(dev, ids) == 0)
333                 if (!IS_ERR_OR_NULL(acpi_create_platform_device(dev, NULL)))
334                         dev_info(&dev->dev,
335                                  "intel-vbtn: created platform device\n");
336
337         return AE_OK;
338 }
339
340 static int __init intel_vbtn_init(void)
341 {
342         acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
343                             ACPI_UINT32_MAX, check_acpi_dev, NULL,
344                             (void *)intel_vbtn_ids, NULL);
345
346         return platform_driver_register(&intel_vbtn_pl_driver);
347 }
348 module_init(intel_vbtn_init);
349
350 static void __exit intel_vbtn_exit(void)
351 {
352         platform_driver_unregister(&intel_vbtn_pl_driver);
353 }
354 module_exit(intel_vbtn_exit);