Merge tag 'platform-drivers-x86-v4.17-1' of git://git.infradead.org/linux-platform...
[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 static int intel_hid_set_enable(struct device *device, bool enable)
100 {
101         acpi_status status;
102
103         status = acpi_execute_simple_method(ACPI_HANDLE(device), "HDSM",
104                                             enable);
105         if (ACPI_FAILURE(status)) {
106                 dev_warn(device, "failed to %sable hotkeys\n",
107                          enable ? "en" : "dis");
108                 return -EIO;
109         }
110
111         return 0;
112 }
113
114 static void intel_button_array_enable(struct device *device, bool enable)
115 {
116         struct intel_hid_priv *priv = dev_get_drvdata(device);
117         acpi_handle handle = ACPI_HANDLE(device);
118         unsigned long long button_cap;
119         acpi_status status;
120
121         if (!priv->array)
122                 return;
123
124         /* Query supported platform features */
125         status = acpi_evaluate_integer(handle, "BTNC", NULL, &button_cap);
126         if (ACPI_FAILURE(status)) {
127                 dev_warn(device, "failed to get button capability\n");
128                 return;
129         }
130
131         /* Enable|disable features - power button is always enabled */
132         status = acpi_execute_simple_method(handle, "BTNE",
133                                             enable ? button_cap : 1);
134         if (ACPI_FAILURE(status))
135                 dev_warn(device, "failed to set button capability\n");
136 }
137
138 static int intel_hid_pm_prepare(struct device *device)
139 {
140         struct intel_hid_priv *priv = dev_get_drvdata(device);
141
142         priv->wakeup_mode = true;
143         return 0;
144 }
145
146 static int intel_hid_pl_suspend_handler(struct device *device)
147 {
148         if (pm_suspend_via_firmware()) {
149                 intel_hid_set_enable(device, false);
150                 intel_button_array_enable(device, false);
151         }
152         return 0;
153 }
154
155 static int intel_hid_pl_resume_handler(struct device *device)
156 {
157         struct intel_hid_priv *priv = dev_get_drvdata(device);
158
159         priv->wakeup_mode = false;
160         if (pm_resume_via_firmware()) {
161                 intel_hid_set_enable(device, true);
162                 intel_button_array_enable(device, true);
163         }
164         return 0;
165 }
166
167 static const struct dev_pm_ops intel_hid_pl_pm_ops = {
168         .prepare = intel_hid_pm_prepare,
169         .freeze  = intel_hid_pl_suspend_handler,
170         .thaw  = intel_hid_pl_resume_handler,
171         .restore  = intel_hid_pl_resume_handler,
172         .suspend  = intel_hid_pl_suspend_handler,
173         .resume  = intel_hid_pl_resume_handler,
174 };
175
176 static int intel_hid_input_setup(struct platform_device *device)
177 {
178         struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
179         int ret;
180
181         priv->input_dev = devm_input_allocate_device(&device->dev);
182         if (!priv->input_dev)
183                 return -ENOMEM;
184
185         ret = sparse_keymap_setup(priv->input_dev, intel_hid_keymap, NULL);
186         if (ret)
187                 return ret;
188
189         priv->input_dev->name = "Intel HID events";
190         priv->input_dev->id.bustype = BUS_HOST;
191
192         return input_register_device(priv->input_dev);
193 }
194
195 static int intel_button_array_input_setup(struct platform_device *device)
196 {
197         struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
198         int ret;
199
200         /* Setup input device for 5 button array */
201         priv->array = devm_input_allocate_device(&device->dev);
202         if (!priv->array)
203                 return -ENOMEM;
204
205         ret = sparse_keymap_setup(priv->array, intel_array_keymap, NULL);
206         if (ret)
207                 return ret;
208
209         priv->array->name = "Intel HID 5 button array";
210         priv->array->id.bustype = BUS_HOST;
211
212         return input_register_device(priv->array);
213 }
214
215 static void notify_handler(acpi_handle handle, u32 event, void *context)
216 {
217         struct platform_device *device = context;
218         struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
219         unsigned long long ev_index;
220         acpi_status status;
221
222         if (priv->wakeup_mode) {
223                 /*
224                  * Needed for wakeup from suspend-to-idle to work on some
225                  * platforms that don't expose the 5-button array, but still
226                  * send notifies with the power button event code to this
227                  * device object on power button actions while suspended.
228                  */
229                 if (event == 0xce)
230                         goto wakeup;
231
232                 /* Wake up on 5-button array events only. */
233                 if (event == 0xc0 || !priv->array)
234                         return;
235
236                 if (!sparse_keymap_entry_from_scancode(priv->array, event)) {
237                         dev_info(&device->dev, "unknown event 0x%x\n", event);
238                         return;
239                 }
240
241 wakeup:
242                 pm_wakeup_hard_event(&device->dev);
243                 return;
244         }
245
246         /*
247          * Needed for suspend to work on some platforms that don't expose
248          * the 5-button array, but still send notifies with power button
249          * event code to this device object on power button actions.
250          *
251          * Report the power button press; catch and ignore the button release.
252          */
253         if (!priv->array) {
254                 if (event == 0xce) {
255                         input_report_key(priv->input_dev, KEY_POWER, 1);
256                         input_sync(priv->input_dev);
257                         return;
258                 }
259
260                 if (event == 0xcf)
261                         return;
262         }
263
264         /* 0xC0 is for HID events, other values are for 5 button array */
265         if (event != 0xc0) {
266                 if (!priv->array ||
267                     !sparse_keymap_report_event(priv->array, event, 1, true))
268                         dev_dbg(&device->dev, "unknown event 0x%x\n", event);
269                 return;
270         }
271
272         status = acpi_evaluate_integer(handle, "HDEM", NULL, &ev_index);
273         if (ACPI_FAILURE(status)) {
274                 dev_warn(&device->dev, "failed to get event index\n");
275                 return;
276         }
277
278         if (!sparse_keymap_report_event(priv->input_dev, ev_index, 1, true))
279                 dev_dbg(&device->dev, "unknown event index 0x%llx\n",
280                          ev_index);
281 }
282
283 static bool button_array_present(struct platform_device *device)
284 {
285         acpi_handle handle = ACPI_HANDLE(&device->dev);
286         unsigned long long event_cap;
287         acpi_status status;
288         bool supported = false;
289
290         status = acpi_evaluate_integer(handle, "HEBC", NULL, &event_cap);
291         if (ACPI_SUCCESS(status) && (event_cap & 0x20000))
292                 supported = true;
293
294         if (dmi_check_system(button_array_table))
295                 supported = true;
296
297         return supported;
298 }
299
300 static int intel_hid_probe(struct platform_device *device)
301 {
302         acpi_handle handle = ACPI_HANDLE(&device->dev);
303         unsigned long long mode;
304         struct intel_hid_priv *priv;
305         acpi_status status;
306         int err;
307
308         status = acpi_evaluate_integer(handle, "HDMM", NULL, &mode);
309         if (ACPI_FAILURE(status)) {
310                 dev_warn(&device->dev, "failed to read mode\n");
311                 return -ENODEV;
312         }
313
314         if (mode != 0) {
315                 /*
316                  * This driver only implements "simple" mode.  There appear
317                  * to be no other modes, but we should be paranoid and check
318                  * for compatibility.
319                  */
320                 dev_info(&device->dev, "platform is not in simple mode\n");
321                 return -ENODEV;
322         }
323
324         priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
325         if (!priv)
326                 return -ENOMEM;
327         dev_set_drvdata(&device->dev, priv);
328
329         err = intel_hid_input_setup(device);
330         if (err) {
331                 pr_err("Failed to setup Intel HID hotkeys\n");
332                 return err;
333         }
334
335         /* Setup 5 button array */
336         if (button_array_present(device)) {
337                 dev_info(&device->dev, "platform supports 5 button array\n");
338                 err = intel_button_array_input_setup(device);
339                 if (err)
340                         pr_err("Failed to setup Intel 5 button array hotkeys\n");
341         }
342
343         status = acpi_install_notify_handler(handle,
344                                              ACPI_DEVICE_NOTIFY,
345                                              notify_handler,
346                                              device);
347         if (ACPI_FAILURE(status))
348                 return -EBUSY;
349
350         err = intel_hid_set_enable(&device->dev, true);
351         if (err)
352                 goto err_remove_notify;
353
354         if (priv->array) {
355                 intel_button_array_enable(&device->dev, true);
356
357                 /* Call button load method to enable HID power button */
358                 status = acpi_evaluate_object(handle, "BTNL", NULL, NULL);
359                 if (ACPI_FAILURE(status))
360                         dev_warn(&device->dev,
361                                  "failed to enable HID power button\n");
362         }
363
364         device_init_wakeup(&device->dev, true);
365         return 0;
366
367 err_remove_notify:
368         acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
369
370         return err;
371 }
372
373 static int intel_hid_remove(struct platform_device *device)
374 {
375         acpi_handle handle = ACPI_HANDLE(&device->dev);
376
377         device_init_wakeup(&device->dev, false);
378         acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
379         intel_hid_set_enable(&device->dev, false);
380         intel_button_array_enable(&device->dev, false);
381
382         /*
383          * Even if we failed to shut off the event stream, we can still
384          * safely detach from the device.
385          */
386         return 0;
387 }
388
389 static struct platform_driver intel_hid_pl_driver = {
390         .driver = {
391                 .name = "intel-hid",
392                 .acpi_match_table = intel_hid_ids,
393                 .pm = &intel_hid_pl_pm_ops,
394         },
395         .probe = intel_hid_probe,
396         .remove = intel_hid_remove,
397 };
398 MODULE_DEVICE_TABLE(acpi, intel_hid_ids);
399
400 /*
401  * Unfortunately, some laptops provide a _HID="INT33D5" device with
402  * _CID="PNP0C02".  This causes the pnpacpi scan driver to claim the
403  * ACPI node, so no platform device will be created.  The pnpacpi
404  * driver rejects this device in subsequent processing, so no physical
405  * node is created at all.
406  *
407  * As a workaround until the ACPI core figures out how to handle
408  * this corner case, manually ask the ACPI platform device code to
409  * claim the ACPI node.
410  */
411 static acpi_status __init
412 check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
413 {
414         const struct acpi_device_id *ids = context;
415         struct acpi_device *dev;
416
417         if (acpi_bus_get_device(handle, &dev) != 0)
418                 return AE_OK;
419
420         if (acpi_match_device_ids(dev, ids) == 0)
421                 if (acpi_create_platform_device(dev, NULL))
422                         dev_info(&dev->dev,
423                                  "intel-hid: created platform device\n");
424
425         return AE_OK;
426 }
427
428 static int __init intel_hid_init(void)
429 {
430         acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
431                             ACPI_UINT32_MAX, check_acpi_dev, NULL,
432                             (void *)intel_hid_ids, NULL);
433
434         return platform_driver_register(&intel_hid_pl_driver);
435 }
436 module_init(intel_hid_init);
437
438 static void __exit intel_hid_exit(void)
439 {
440         platform_driver_unregister(&intel_hid_pl_driver);
441 }
442 module_exit(intel_hid_exit);