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