GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / platform / x86 / intel-hid.c
1 /*
2  *  Intel HID event & 5 button array driver
3  *
4  *  Copyright (C) 2015 Alex Hung <alex.hung@canonical.com>
5  *  Copyright (C) 2015 Andrew Lutomirski <luto@kernel.org>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  */
18
19 #include <linux/acpi.h>
20 #include <linux/dmi.h>
21 #include <linux/input.h>
22 #include <linux/input/sparse-keymap.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/suspend.h>
27
28 MODULE_LICENSE("GPL");
29 MODULE_AUTHOR("Alex Hung");
30
31 static const struct acpi_device_id intel_hid_ids[] = {
32         {"INT33D5", 0},
33         {"", 0},
34 };
35
36 /* In theory, these are HID usages. */
37 static const struct key_entry intel_hid_keymap[] = {
38         /* 1: LSuper (Page 0x07, usage 0xE3) -- unclear what to do */
39         /* 2: Toggle SW_ROTATE_LOCK -- easy to implement if seen in wild */
40         { KE_KEY, 3, { KEY_NUMLOCK } },
41         { KE_KEY, 4, { KEY_HOME } },
42         { KE_KEY, 5, { KEY_END } },
43         { KE_KEY, 6, { KEY_PAGEUP } },
44         { KE_KEY, 7, { KEY_PAGEDOWN } },
45         { KE_KEY, 8, { KEY_RFKILL } },
46         { KE_KEY, 9, { KEY_POWER } },
47         { KE_KEY, 11, { KEY_SLEEP } },
48         /* 13 has two different meanings in the spec -- ignore it. */
49         { KE_KEY, 14, { KEY_STOPCD } },
50         { KE_KEY, 15, { KEY_PLAYPAUSE } },
51         { KE_KEY, 16, { KEY_MUTE } },
52         { KE_KEY, 17, { KEY_VOLUMEUP } },
53         { KE_KEY, 18, { KEY_VOLUMEDOWN } },
54         { KE_KEY, 19, { KEY_BRIGHTNESSUP } },
55         { KE_KEY, 20, { KEY_BRIGHTNESSDOWN } },
56         /* 27: wake -- needs special handling */
57         { KE_END },
58 };
59
60 /* 5 button array notification value. */
61 static const struct key_entry intel_array_keymap[] = {
62         { KE_KEY,    0xC2, { KEY_LEFTMETA } },                /* Press */
63         { KE_IGNORE, 0xC3, { KEY_LEFTMETA } },                /* Release */
64         { KE_KEY,    0xC4, { KEY_VOLUMEUP } },                /* Press */
65         { KE_IGNORE, 0xC5, { KEY_VOLUMEUP } },                /* Release */
66         { KE_KEY,    0xC6, { KEY_VOLUMEDOWN } },              /* Press */
67         { KE_IGNORE, 0xC7, { KEY_VOLUMEDOWN } },              /* Release */
68         { KE_KEY,    0xC8, { KEY_ROTATE_LOCK_TOGGLE } },      /* Press */
69         { KE_IGNORE, 0xC9, { KEY_ROTATE_LOCK_TOGGLE } },      /* Release */
70         { KE_KEY,    0xCE, { KEY_POWER } },                   /* Press */
71         { KE_IGNORE, 0xCF, { KEY_POWER } },                   /* Release */
72         { KE_END },
73 };
74
75 static const struct dmi_system_id button_array_table[] = {
76         {
77                 .ident = "Wacom MobileStudio Pro 13",
78                 .matches = {
79                         DMI_MATCH(DMI_SYS_VENDOR, "Wacom Co.,Ltd"),
80                         DMI_MATCH(DMI_PRODUCT_NAME, "Wacom MobileStudio Pro 13"),
81                 },
82         },
83         {
84                 .ident = "Wacom MobileStudio Pro 16",
85                 .matches = {
86                         DMI_MATCH(DMI_SYS_VENDOR, "Wacom Co.,Ltd"),
87                         DMI_MATCH(DMI_PRODUCT_NAME, "Wacom MobileStudio Pro 16"),
88                 },
89         },
90         {
91                 .ident = "HP Spectre x2 (2015)",
92                 .matches = {
93                         DMI_MATCH(DMI_SYS_VENDOR, "HP"),
94                         DMI_MATCH(DMI_PRODUCT_NAME, "HP Spectre x2 Detachable"),
95                 },
96         },
97         {
98                 .ident = "Lenovo ThinkPad X1 Tablet Gen 2",
99                 .matches = {
100                         DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
101                         DMI_MATCH(DMI_PRODUCT_FAMILY, "ThinkPad X1 Tablet Gen 2"),
102                 },
103         },
104         { }
105 };
106
107 struct intel_hid_priv {
108         struct input_dev *input_dev;
109         struct input_dev *array;
110         bool wakeup_mode;
111 };
112
113 #define HID_EVENT_FILTER_UUID   "eeec56b3-4442-408f-a792-4edd4d758054"
114
115 enum intel_hid_dsm_fn_codes {
116         INTEL_HID_DSM_FN_INVALID,
117         INTEL_HID_DSM_BTNL_FN,
118         INTEL_HID_DSM_HDMM_FN,
119         INTEL_HID_DSM_HDSM_FN,
120         INTEL_HID_DSM_HDEM_FN,
121         INTEL_HID_DSM_BTNS_FN,
122         INTEL_HID_DSM_BTNE_FN,
123         INTEL_HID_DSM_HEBC_V1_FN,
124         INTEL_HID_DSM_VGBS_FN,
125         INTEL_HID_DSM_HEBC_V2_FN,
126         INTEL_HID_DSM_FN_MAX
127 };
128
129 static const char *intel_hid_dsm_fn_to_method[INTEL_HID_DSM_FN_MAX] = {
130         NULL,
131         "BTNL",
132         "HDMM",
133         "HDSM",
134         "HDEM",
135         "BTNS",
136         "BTNE",
137         "HEBC",
138         "VGBS",
139         "HEBC"
140 };
141
142 static unsigned long long intel_hid_dsm_fn_mask;
143 static guid_t intel_dsm_guid;
144
145 static bool intel_hid_execute_method(acpi_handle handle,
146                                      enum intel_hid_dsm_fn_codes fn_index,
147                                      unsigned long long arg)
148 {
149         union acpi_object *obj, argv4, req;
150         acpi_status status;
151         char *method_name;
152
153         if (fn_index <= INTEL_HID_DSM_FN_INVALID ||
154             fn_index >= INTEL_HID_DSM_FN_MAX)
155                 return false;
156
157         method_name = (char *)intel_hid_dsm_fn_to_method[fn_index];
158
159         if (!(intel_hid_dsm_fn_mask & fn_index))
160                 goto skip_dsm_exec;
161
162         /* All methods expects a package with one integer element */
163         req.type = ACPI_TYPE_INTEGER;
164         req.integer.value = arg;
165
166         argv4.type = ACPI_TYPE_PACKAGE;
167         argv4.package.count = 1;
168         argv4.package.elements = &req;
169
170         obj = acpi_evaluate_dsm(handle, &intel_dsm_guid, 1, fn_index, &argv4);
171         if (obj) {
172                 acpi_handle_debug(handle, "Exec DSM Fn code: %d[%s] success\n",
173                                   fn_index, method_name);
174                 ACPI_FREE(obj);
175                 return true;
176         }
177
178 skip_dsm_exec:
179         status = acpi_execute_simple_method(handle, method_name, arg);
180         if (ACPI_SUCCESS(status))
181                 return true;
182
183         return false;
184 }
185
186 static bool intel_hid_evaluate_method(acpi_handle handle,
187                                       enum intel_hid_dsm_fn_codes fn_index,
188                                       unsigned long long *result)
189 {
190         union acpi_object *obj;
191         acpi_status status;
192         char *method_name;
193
194         if (fn_index <= INTEL_HID_DSM_FN_INVALID ||
195             fn_index >= INTEL_HID_DSM_FN_MAX)
196                 return false;
197
198         method_name = (char *)intel_hid_dsm_fn_to_method[fn_index];
199
200         if (!(intel_hid_dsm_fn_mask & fn_index))
201                 goto skip_dsm_eval;
202
203         obj = acpi_evaluate_dsm_typed(handle, &intel_dsm_guid,
204                                       1, fn_index,
205                                       NULL,  ACPI_TYPE_INTEGER);
206         if (obj) {
207                 *result = obj->integer.value;
208                 acpi_handle_debug(handle,
209                                   "Eval DSM Fn code: %d[%s] results: 0x%llx\n",
210                                   fn_index, method_name, *result);
211                 ACPI_FREE(obj);
212                 return true;
213         }
214
215 skip_dsm_eval:
216         status = acpi_evaluate_integer(handle, method_name, NULL, result);
217         if (ACPI_SUCCESS(status))
218                 return true;
219
220         return false;
221 }
222
223 static void intel_hid_init_dsm(acpi_handle handle)
224 {
225         union acpi_object *obj;
226
227         guid_parse(HID_EVENT_FILTER_UUID, &intel_dsm_guid);
228
229         obj = acpi_evaluate_dsm_typed(handle, &intel_dsm_guid, 1, 0, NULL,
230                                       ACPI_TYPE_BUFFER);
231         if (obj) {
232                 intel_hid_dsm_fn_mask = *obj->buffer.pointer;
233                 ACPI_FREE(obj);
234         }
235
236         acpi_handle_debug(handle, "intel_hid_dsm_fn_mask = %llx\n",
237                           intel_hid_dsm_fn_mask);
238 }
239
240 static int intel_hid_set_enable(struct device *device, bool enable)
241 {
242         acpi_handle handle = ACPI_HANDLE(device);
243
244         /* Enable|disable features - power button is always enabled */
245         if (!intel_hid_execute_method(handle, INTEL_HID_DSM_HDSM_FN,
246                                       enable)) {
247                 dev_warn(device, "failed to %sable hotkeys\n",
248                          enable ? "en" : "dis");
249                 return -EIO;
250         }
251
252         return 0;
253 }
254
255 static void intel_button_array_enable(struct device *device, bool enable)
256 {
257         struct intel_hid_priv *priv = dev_get_drvdata(device);
258         acpi_handle handle = ACPI_HANDLE(device);
259         unsigned long long button_cap;
260         acpi_status status;
261
262         if (!priv->array)
263                 return;
264
265         /* Query supported platform features */
266         status = acpi_evaluate_integer(handle, "BTNC", NULL, &button_cap);
267         if (ACPI_FAILURE(status)) {
268                 dev_warn(device, "failed to get button capability\n");
269                 return;
270         }
271
272         /* Enable|disable features - power button is always enabled */
273         if (!intel_hid_execute_method(handle, INTEL_HID_DSM_BTNE_FN,
274                                       enable ? button_cap : 1))
275                 dev_warn(device, "failed to set button capability\n");
276 }
277
278 static int intel_hid_pm_prepare(struct device *device)
279 {
280         struct intel_hid_priv *priv = dev_get_drvdata(device);
281
282         priv->wakeup_mode = true;
283         return 0;
284 }
285
286 static int intel_hid_pl_suspend_handler(struct device *device)
287 {
288         if (pm_suspend_via_firmware()) {
289                 intel_hid_set_enable(device, false);
290                 intel_button_array_enable(device, false);
291         }
292         return 0;
293 }
294
295 static int intel_hid_pl_resume_handler(struct device *device)
296 {
297         struct intel_hid_priv *priv = dev_get_drvdata(device);
298
299         priv->wakeup_mode = false;
300         if (pm_resume_via_firmware()) {
301                 intel_hid_set_enable(device, true);
302                 intel_button_array_enable(device, true);
303         }
304         return 0;
305 }
306
307 static const struct dev_pm_ops intel_hid_pl_pm_ops = {
308         .prepare = intel_hid_pm_prepare,
309         .freeze  = intel_hid_pl_suspend_handler,
310         .thaw  = intel_hid_pl_resume_handler,
311         .restore  = intel_hid_pl_resume_handler,
312         .suspend  = intel_hid_pl_suspend_handler,
313         .resume  = intel_hid_pl_resume_handler,
314 };
315
316 static int intel_hid_input_setup(struct platform_device *device)
317 {
318         struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
319         int ret;
320
321         priv->input_dev = devm_input_allocate_device(&device->dev);
322         if (!priv->input_dev)
323                 return -ENOMEM;
324
325         ret = sparse_keymap_setup(priv->input_dev, intel_hid_keymap, NULL);
326         if (ret)
327                 return ret;
328
329         priv->input_dev->name = "Intel HID events";
330         priv->input_dev->id.bustype = BUS_HOST;
331
332         return input_register_device(priv->input_dev);
333 }
334
335 static int intel_button_array_input_setup(struct platform_device *device)
336 {
337         struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
338         int ret;
339
340         /* Setup input device for 5 button array */
341         priv->array = devm_input_allocate_device(&device->dev);
342         if (!priv->array)
343                 return -ENOMEM;
344
345         ret = sparse_keymap_setup(priv->array, intel_array_keymap, NULL);
346         if (ret)
347                 return ret;
348
349         priv->array->name = "Intel HID 5 button array";
350         priv->array->id.bustype = BUS_HOST;
351
352         return input_register_device(priv->array);
353 }
354
355 static void notify_handler(acpi_handle handle, u32 event, void *context)
356 {
357         struct platform_device *device = context;
358         struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
359         unsigned long long ev_index;
360
361         if (priv->wakeup_mode) {
362                 /*
363                  * Needed for wakeup from suspend-to-idle to work on some
364                  * platforms that don't expose the 5-button array, but still
365                  * send notifies with the power button event code to this
366                  * device object on power button actions while suspended.
367                  */
368                 if (event == 0xce)
369                         goto wakeup;
370
371                 /* Wake up on 5-button array events only. */
372                 if (event == 0xc0 || !priv->array)
373                         return;
374
375                 if (!sparse_keymap_entry_from_scancode(priv->array, event)) {
376                         dev_info(&device->dev, "unknown event 0x%x\n", event);
377                         return;
378                 }
379
380 wakeup:
381                 pm_wakeup_hard_event(&device->dev);
382                 return;
383         }
384
385         /*
386          * Needed for suspend to work on some platforms that don't expose
387          * the 5-button array, but still send notifies with power button
388          * event code to this device object on power button actions.
389          *
390          * Report the power button press and release.
391          */
392         if (!priv->array) {
393                 if (event == 0xce) {
394                         input_report_key(priv->input_dev, KEY_POWER, 1);
395                         input_sync(priv->input_dev);
396                         return;
397                 }
398
399                 if (event == 0xcf) {
400                         input_report_key(priv->input_dev, KEY_POWER, 0);
401                         input_sync(priv->input_dev);
402                         return;
403                 }
404         }
405
406         /* 0xC0 is for HID events, other values are for 5 button array */
407         if (event != 0xc0) {
408                 if (!priv->array ||
409                     !sparse_keymap_report_event(priv->array, event, 1, true))
410                         dev_dbg(&device->dev, "unknown event 0x%x\n", event);
411                 return;
412         }
413
414         if (!intel_hid_evaluate_method(handle, INTEL_HID_DSM_HDEM_FN,
415                                        &ev_index)) {
416                 dev_warn(&device->dev, "failed to get event index\n");
417                 return;
418         }
419
420         if (!sparse_keymap_report_event(priv->input_dev, ev_index, 1, true))
421                 dev_dbg(&device->dev, "unknown event index 0x%llx\n",
422                          ev_index);
423 }
424
425 static bool button_array_present(struct platform_device *device)
426 {
427         acpi_handle handle = ACPI_HANDLE(&device->dev);
428         unsigned long long event_cap;
429
430         if (intel_hid_evaluate_method(handle, INTEL_HID_DSM_HEBC_V2_FN,
431                                       &event_cap)) {
432                 /* Check presence of 5 button array or v2 power button */
433                 if (event_cap & 0x60000)
434                         return true;
435         }
436
437         if (intel_hid_evaluate_method(handle, INTEL_HID_DSM_HEBC_V1_FN,
438                                       &event_cap)) {
439                 if (event_cap & 0x20000)
440                         return true;
441         }
442
443         if (dmi_check_system(button_array_table))
444                 return true;
445
446         return false;
447 }
448
449 static int intel_hid_probe(struct platform_device *device)
450 {
451         acpi_handle handle = ACPI_HANDLE(&device->dev);
452         unsigned long long mode;
453         struct intel_hid_priv *priv;
454         acpi_status status;
455         int err;
456
457         intel_hid_init_dsm(handle);
458
459         if (!intel_hid_evaluate_method(handle, INTEL_HID_DSM_HDMM_FN, &mode)) {
460                 dev_warn(&device->dev, "failed to read mode\n");
461                 return -ENODEV;
462         }
463
464         if (mode != 0) {
465                 /*
466                  * This driver only implements "simple" mode.  There appear
467                  * to be no other modes, but we should be paranoid and check
468                  * for compatibility.
469                  */
470                 dev_info(&device->dev, "platform is not in simple mode\n");
471                 return -ENODEV;
472         }
473
474         priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
475         if (!priv)
476                 return -ENOMEM;
477         dev_set_drvdata(&device->dev, priv);
478
479         err = intel_hid_input_setup(device);
480         if (err) {
481                 pr_err("Failed to setup Intel HID hotkeys\n");
482                 return err;
483         }
484
485         /* Setup 5 button array */
486         if (button_array_present(device)) {
487                 dev_info(&device->dev, "platform supports 5 button array\n");
488                 err = intel_button_array_input_setup(device);
489                 if (err)
490                         pr_err("Failed to setup Intel 5 button array hotkeys\n");
491         }
492
493         status = acpi_install_notify_handler(handle,
494                                              ACPI_DEVICE_NOTIFY,
495                                              notify_handler,
496                                              device);
497         if (ACPI_FAILURE(status))
498                 return -EBUSY;
499
500         err = intel_hid_set_enable(&device->dev, true);
501         if (err)
502                 goto err_remove_notify;
503
504         if (priv->array) {
505                 unsigned long long dummy;
506
507                 intel_button_array_enable(&device->dev, true);
508
509                 /* Call button load method to enable HID power button */
510                 if (!intel_hid_evaluate_method(handle, INTEL_HID_DSM_BTNL_FN,
511                                                &dummy)) {
512                         dev_warn(&device->dev,
513                                  "failed to enable HID power button\n");
514                 }
515         }
516
517         device_init_wakeup(&device->dev, true);
518         return 0;
519
520 err_remove_notify:
521         acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
522
523         return err;
524 }
525
526 static int intel_hid_remove(struct platform_device *device)
527 {
528         acpi_handle handle = ACPI_HANDLE(&device->dev);
529
530         device_init_wakeup(&device->dev, false);
531         acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
532         intel_hid_set_enable(&device->dev, false);
533         intel_button_array_enable(&device->dev, false);
534
535         /*
536          * Even if we failed to shut off the event stream, we can still
537          * safely detach from the device.
538          */
539         return 0;
540 }
541
542 static struct platform_driver intel_hid_pl_driver = {
543         .driver = {
544                 .name = "intel-hid",
545                 .acpi_match_table = intel_hid_ids,
546                 .pm = &intel_hid_pl_pm_ops,
547         },
548         .probe = intel_hid_probe,
549         .remove = intel_hid_remove,
550 };
551 MODULE_DEVICE_TABLE(acpi, intel_hid_ids);
552
553 /*
554  * Unfortunately, some laptops provide a _HID="INT33D5" device with
555  * _CID="PNP0C02".  This causes the pnpacpi scan driver to claim the
556  * ACPI node, so no platform device will be created.  The pnpacpi
557  * driver rejects this device in subsequent processing, so no physical
558  * node is created at all.
559  *
560  * As a workaround until the ACPI core figures out how to handle
561  * this corner case, manually ask the ACPI platform device code to
562  * claim the ACPI node.
563  */
564 static acpi_status __init
565 check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
566 {
567         const struct acpi_device_id *ids = context;
568         struct acpi_device *dev;
569
570         if (acpi_bus_get_device(handle, &dev) != 0)
571                 return AE_OK;
572
573         if (acpi_match_device_ids(dev, ids) == 0)
574                 if (!IS_ERR_OR_NULL(acpi_create_platform_device(dev, NULL)))
575                         dev_info(&dev->dev,
576                                  "intel-hid: created platform device\n");
577
578         return AE_OK;
579 }
580
581 static int __init intel_hid_init(void)
582 {
583         acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
584                             ACPI_UINT32_MAX, check_acpi_dev, NULL,
585                             (void *)intel_hid_ids, NULL);
586
587         return platform_driver_register(&intel_hid_pl_driver);
588 }
589 module_init(intel_hid_init);
590
591 static void __exit intel_hid_exit(void)
592 {
593         platform_driver_unregister(&intel_hid_pl_driver);
594 }
595 module_exit(intel_hid_exit);