MAINTAINERS: add maintainer for IMX LPI2C driver
[sfrench/cifs-2.6.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 };
92
93 struct intel_hid_priv {
94         struct input_dev *input_dev;
95         struct input_dev *array;
96         bool wakeup_mode;
97 };
98
99 #define HID_EVENT_FILTER_UUID   "eeec56b3-4442-408f-a792-4edd4d758054"
100
101 enum intel_hid_dsm_fn_codes {
102         INTEL_HID_DSM_FN_INVALID,
103         INTEL_HID_DSM_BTNL_FN,
104         INTEL_HID_DSM_HDMM_FN,
105         INTEL_HID_DSM_HDSM_FN,
106         INTEL_HID_DSM_HDEM_FN,
107         INTEL_HID_DSM_BTNS_FN,
108         INTEL_HID_DSM_BTNE_FN,
109         INTEL_HID_DSM_HEBC_V1_FN,
110         INTEL_HID_DSM_VGBS_FN,
111         INTEL_HID_DSM_HEBC_V2_FN,
112         INTEL_HID_DSM_FN_MAX
113 };
114
115 static const char *intel_hid_dsm_fn_to_method[INTEL_HID_DSM_FN_MAX] = {
116         NULL,
117         "BTNL",
118         "HDMM",
119         "HDSM",
120         "HDEM",
121         "BTNS",
122         "BTNE",
123         "HEBC",
124         "VGBS",
125         "HEBC"
126 };
127
128 static unsigned long long intel_hid_dsm_fn_mask;
129 static guid_t intel_dsm_guid;
130
131 static bool intel_hid_execute_method(acpi_handle handle,
132                                      enum intel_hid_dsm_fn_codes fn_index,
133                                      unsigned long long arg)
134 {
135         union acpi_object *obj, argv4, req;
136         acpi_status status;
137         char *method_name;
138
139         if (fn_index <= INTEL_HID_DSM_FN_INVALID ||
140             fn_index >= INTEL_HID_DSM_FN_MAX)
141                 return false;
142
143         method_name = (char *)intel_hid_dsm_fn_to_method[fn_index];
144
145         if (!(intel_hid_dsm_fn_mask & fn_index))
146                 goto skip_dsm_exec;
147
148         /* All methods expects a package with one integer element */
149         req.type = ACPI_TYPE_INTEGER;
150         req.integer.value = arg;
151
152         argv4.type = ACPI_TYPE_PACKAGE;
153         argv4.package.count = 1;
154         argv4.package.elements = &req;
155
156         obj = acpi_evaluate_dsm(handle, &intel_dsm_guid, 1, fn_index, &argv4);
157         if (obj) {
158                 acpi_handle_debug(handle, "Exec DSM Fn code: %d[%s] success\n",
159                                   fn_index, method_name);
160                 ACPI_FREE(obj);
161                 return true;
162         }
163
164 skip_dsm_exec:
165         status = acpi_execute_simple_method(handle, method_name, arg);
166         if (ACPI_SUCCESS(status))
167                 return true;
168
169         return false;
170 }
171
172 static bool intel_hid_evaluate_method(acpi_handle handle,
173                                       enum intel_hid_dsm_fn_codes fn_index,
174                                       unsigned long long *result)
175 {
176         union acpi_object *obj;
177         acpi_status status;
178         char *method_name;
179
180         if (fn_index <= INTEL_HID_DSM_FN_INVALID ||
181             fn_index >= INTEL_HID_DSM_FN_MAX)
182                 return false;
183
184         method_name = (char *)intel_hid_dsm_fn_to_method[fn_index];
185
186         if (!(intel_hid_dsm_fn_mask & fn_index))
187                 goto skip_dsm_eval;
188
189         obj = acpi_evaluate_dsm_typed(handle, &intel_dsm_guid,
190                                       1, fn_index,
191                                       NULL,  ACPI_TYPE_INTEGER);
192         if (obj) {
193                 *result = obj->integer.value;
194                 acpi_handle_debug(handle,
195                                   "Eval DSM Fn code: %d[%s] results: 0x%llx\n",
196                                   fn_index, method_name, *result);
197                 ACPI_FREE(obj);
198                 return true;
199         }
200
201 skip_dsm_eval:
202         status = acpi_evaluate_integer(handle, method_name, NULL, result);
203         if (ACPI_SUCCESS(status))
204                 return true;
205
206         return false;
207 }
208
209 static void intel_hid_init_dsm(acpi_handle handle)
210 {
211         union acpi_object *obj;
212
213         guid_parse(HID_EVENT_FILTER_UUID, &intel_dsm_guid);
214
215         obj = acpi_evaluate_dsm_typed(handle, &intel_dsm_guid, 1, 0, NULL,
216                                       ACPI_TYPE_BUFFER);
217         if (obj) {
218                 intel_hid_dsm_fn_mask = *obj->buffer.pointer;
219                 ACPI_FREE(obj);
220         }
221
222         acpi_handle_debug(handle, "intel_hid_dsm_fn_mask = %llx\n",
223                           intel_hid_dsm_fn_mask);
224 }
225
226 static int intel_hid_set_enable(struct device *device, bool enable)
227 {
228         acpi_handle handle = ACPI_HANDLE(device);
229
230         /* Enable|disable features - power button is always enabled */
231         if (!intel_hid_execute_method(handle, INTEL_HID_DSM_HDSM_FN,
232                                       enable)) {
233                 dev_warn(device, "failed to %sable hotkeys\n",
234                          enable ? "en" : "dis");
235                 return -EIO;
236         }
237
238         return 0;
239 }
240
241 static void intel_button_array_enable(struct device *device, bool enable)
242 {
243         struct intel_hid_priv *priv = dev_get_drvdata(device);
244         acpi_handle handle = ACPI_HANDLE(device);
245         unsigned long long button_cap;
246         acpi_status status;
247
248         if (!priv->array)
249                 return;
250
251         /* Query supported platform features */
252         status = acpi_evaluate_integer(handle, "BTNC", NULL, &button_cap);
253         if (ACPI_FAILURE(status)) {
254                 dev_warn(device, "failed to get button capability\n");
255                 return;
256         }
257
258         /* Enable|disable features - power button is always enabled */
259         if (!intel_hid_execute_method(handle, INTEL_HID_DSM_BTNE_FN,
260                                       enable ? button_cap : 1))
261                 dev_warn(device, "failed to set button capability\n");
262 }
263
264 static int intel_hid_pm_prepare(struct device *device)
265 {
266         struct intel_hid_priv *priv = dev_get_drvdata(device);
267
268         priv->wakeup_mode = true;
269         return 0;
270 }
271
272 static int intel_hid_pl_suspend_handler(struct device *device)
273 {
274         if (pm_suspend_via_firmware()) {
275                 intel_hid_set_enable(device, false);
276                 intel_button_array_enable(device, false);
277         }
278         return 0;
279 }
280
281 static int intel_hid_pl_resume_handler(struct device *device)
282 {
283         struct intel_hid_priv *priv = dev_get_drvdata(device);
284
285         priv->wakeup_mode = false;
286         if (pm_resume_via_firmware()) {
287                 intel_hid_set_enable(device, true);
288                 intel_button_array_enable(device, true);
289         }
290         return 0;
291 }
292
293 static const struct dev_pm_ops intel_hid_pl_pm_ops = {
294         .prepare = intel_hid_pm_prepare,
295         .freeze  = intel_hid_pl_suspend_handler,
296         .thaw  = intel_hid_pl_resume_handler,
297         .restore  = intel_hid_pl_resume_handler,
298         .suspend  = intel_hid_pl_suspend_handler,
299         .resume  = intel_hid_pl_resume_handler,
300 };
301
302 static int intel_hid_input_setup(struct platform_device *device)
303 {
304         struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
305         int ret;
306
307         priv->input_dev = devm_input_allocate_device(&device->dev);
308         if (!priv->input_dev)
309                 return -ENOMEM;
310
311         ret = sparse_keymap_setup(priv->input_dev, intel_hid_keymap, NULL);
312         if (ret)
313                 return ret;
314
315         priv->input_dev->name = "Intel HID events";
316         priv->input_dev->id.bustype = BUS_HOST;
317
318         return input_register_device(priv->input_dev);
319 }
320
321 static int intel_button_array_input_setup(struct platform_device *device)
322 {
323         struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
324         int ret;
325
326         /* Setup input device for 5 button array */
327         priv->array = devm_input_allocate_device(&device->dev);
328         if (!priv->array)
329                 return -ENOMEM;
330
331         ret = sparse_keymap_setup(priv->array, intel_array_keymap, NULL);
332         if (ret)
333                 return ret;
334
335         priv->array->name = "Intel HID 5 button array";
336         priv->array->id.bustype = BUS_HOST;
337
338         return input_register_device(priv->array);
339 }
340
341 static void notify_handler(acpi_handle handle, u32 event, void *context)
342 {
343         struct platform_device *device = context;
344         struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
345         unsigned long long ev_index;
346
347         if (priv->wakeup_mode) {
348                 /*
349                  * Needed for wakeup from suspend-to-idle to work on some
350                  * platforms that don't expose the 5-button array, but still
351                  * send notifies with the power button event code to this
352                  * device object on power button actions while suspended.
353                  */
354                 if (event == 0xce)
355                         goto wakeup;
356
357                 /* Wake up on 5-button array events only. */
358                 if (event == 0xc0 || !priv->array)
359                         return;
360
361                 if (!sparse_keymap_entry_from_scancode(priv->array, event)) {
362                         dev_info(&device->dev, "unknown event 0x%x\n", event);
363                         return;
364                 }
365
366 wakeup:
367                 pm_wakeup_hard_event(&device->dev);
368                 return;
369         }
370
371         /*
372          * Needed for suspend to work on some platforms that don't expose
373          * the 5-button array, but still send notifies with power button
374          * event code to this device object on power button actions.
375          *
376          * Report the power button press; catch and ignore the button release.
377          */
378         if (!priv->array) {
379                 if (event == 0xce) {
380                         input_report_key(priv->input_dev, KEY_POWER, 1);
381                         input_sync(priv->input_dev);
382                         return;
383                 }
384
385                 if (event == 0xcf)
386                         return;
387         }
388
389         /* 0xC0 is for HID events, other values are for 5 button array */
390         if (event != 0xc0) {
391                 if (!priv->array ||
392                     !sparse_keymap_report_event(priv->array, event, 1, true))
393                         dev_dbg(&device->dev, "unknown event 0x%x\n", event);
394                 return;
395         }
396
397         if (!intel_hid_evaluate_method(handle, INTEL_HID_DSM_HDEM_FN,
398                                        &ev_index)) {
399                 dev_warn(&device->dev, "failed to get event index\n");
400                 return;
401         }
402
403         if (!sparse_keymap_report_event(priv->input_dev, ev_index, 1, true))
404                 dev_dbg(&device->dev, "unknown event index 0x%llx\n",
405                          ev_index);
406 }
407
408 static bool button_array_present(struct platform_device *device)
409 {
410         acpi_handle handle = ACPI_HANDLE(&device->dev);
411         unsigned long long event_cap;
412
413         if (intel_hid_evaluate_method(handle, INTEL_HID_DSM_HEBC_V2_FN,
414                                       &event_cap)) {
415                 /* Check presence of 5 button array or v2 power button */
416                 if (event_cap & 0x60000)
417                         return true;
418         }
419
420         if (intel_hid_evaluate_method(handle, INTEL_HID_DSM_HEBC_V1_FN,
421                                       &event_cap)) {
422                 if (event_cap & 0x20000)
423                         return true;
424         }
425
426         if (dmi_check_system(button_array_table))
427                 return true;
428
429         return false;
430 }
431
432 static int intel_hid_probe(struct platform_device *device)
433 {
434         acpi_handle handle = ACPI_HANDLE(&device->dev);
435         unsigned long long mode;
436         struct intel_hid_priv *priv;
437         acpi_status status;
438         int err;
439
440         intel_hid_init_dsm(handle);
441
442         if (!intel_hid_evaluate_method(handle, INTEL_HID_DSM_HDMM_FN, &mode)) {
443                 dev_warn(&device->dev, "failed to read mode\n");
444                 return -ENODEV;
445         }
446
447         if (mode != 0) {
448                 /*
449                  * This driver only implements "simple" mode.  There appear
450                  * to be no other modes, but we should be paranoid and check
451                  * for compatibility.
452                  */
453                 dev_info(&device->dev, "platform is not in simple mode\n");
454                 return -ENODEV;
455         }
456
457         priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
458         if (!priv)
459                 return -ENOMEM;
460         dev_set_drvdata(&device->dev, priv);
461
462         err = intel_hid_input_setup(device);
463         if (err) {
464                 pr_err("Failed to setup Intel HID hotkeys\n");
465                 return err;
466         }
467
468         /* Setup 5 button array */
469         if (button_array_present(device)) {
470                 dev_info(&device->dev, "platform supports 5 button array\n");
471                 err = intel_button_array_input_setup(device);
472                 if (err)
473                         pr_err("Failed to setup Intel 5 button array hotkeys\n");
474         }
475
476         status = acpi_install_notify_handler(handle,
477                                              ACPI_DEVICE_NOTIFY,
478                                              notify_handler,
479                                              device);
480         if (ACPI_FAILURE(status))
481                 return -EBUSY;
482
483         err = intel_hid_set_enable(&device->dev, true);
484         if (err)
485                 goto err_remove_notify;
486
487         if (priv->array) {
488                 unsigned long long dummy;
489
490                 intel_button_array_enable(&device->dev, true);
491
492                 /* Call button load method to enable HID power button */
493                 if (!intel_hid_evaluate_method(handle, INTEL_HID_DSM_BTNL_FN,
494                                                &dummy)) {
495                         dev_warn(&device->dev,
496                                  "failed to enable HID power button\n");
497                 }
498         }
499
500         device_init_wakeup(&device->dev, true);
501         return 0;
502
503 err_remove_notify:
504         acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
505
506         return err;
507 }
508
509 static int intel_hid_remove(struct platform_device *device)
510 {
511         acpi_handle handle = ACPI_HANDLE(&device->dev);
512
513         device_init_wakeup(&device->dev, false);
514         acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
515         intel_hid_set_enable(&device->dev, false);
516         intel_button_array_enable(&device->dev, false);
517
518         /*
519          * Even if we failed to shut off the event stream, we can still
520          * safely detach from the device.
521          */
522         return 0;
523 }
524
525 static struct platform_driver intel_hid_pl_driver = {
526         .driver = {
527                 .name = "intel-hid",
528                 .acpi_match_table = intel_hid_ids,
529                 .pm = &intel_hid_pl_pm_ops,
530         },
531         .probe = intel_hid_probe,
532         .remove = intel_hid_remove,
533 };
534 MODULE_DEVICE_TABLE(acpi, intel_hid_ids);
535
536 /*
537  * Unfortunately, some laptops provide a _HID="INT33D5" device with
538  * _CID="PNP0C02".  This causes the pnpacpi scan driver to claim the
539  * ACPI node, so no platform device will be created.  The pnpacpi
540  * driver rejects this device in subsequent processing, so no physical
541  * node is created at all.
542  *
543  * As a workaround until the ACPI core figures out how to handle
544  * this corner case, manually ask the ACPI platform device code to
545  * claim the ACPI node.
546  */
547 static acpi_status __init
548 check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
549 {
550         const struct acpi_device_id *ids = context;
551         struct acpi_device *dev;
552
553         if (acpi_bus_get_device(handle, &dev) != 0)
554                 return AE_OK;
555
556         if (acpi_match_device_ids(dev, ids) == 0)
557                 if (acpi_create_platform_device(dev, NULL))
558                         dev_info(&dev->dev,
559                                  "intel-hid: created platform device\n");
560
561         return AE_OK;
562 }
563
564 static int __init intel_hid_init(void)
565 {
566         acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
567                             ACPI_UINT32_MAX, check_acpi_dev, NULL,
568                             (void *)intel_hid_ids, NULL);
569
570         return platform_driver_register(&intel_hid_pl_driver);
571 }
572 module_init(intel_hid_init);
573
574 static void __exit intel_hid_exit(void)
575 {
576         platform_driver_unregister(&intel_hid_pl_driver);
577 }
578 module_exit(intel_hid_exit);