Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
[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/kernel.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/input.h>
23 #include <linux/platform_device.h>
24 #include <linux/input/sparse-keymap.h>
25 #include <linux/acpi.h>
26 #include <linux/suspend.h>
27 #include <acpi/acpi_bus.h>
28 #include <linux/dmi.h>
29
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Alex Hung");
32
33 static const struct acpi_device_id intel_hid_ids[] = {
34         {"INT33D5", 0},
35         {"", 0},
36 };
37
38 /* In theory, these are HID usages. */
39 static const struct key_entry intel_hid_keymap[] = {
40         /* 1: LSuper (Page 0x07, usage 0xE3) -- unclear what to do */
41         /* 2: Toggle SW_ROTATE_LOCK -- easy to implement if seen in wild */
42         { KE_KEY, 3, { KEY_NUMLOCK } },
43         { KE_KEY, 4, { KEY_HOME } },
44         { KE_KEY, 5, { KEY_END } },
45         { KE_KEY, 6, { KEY_PAGEUP } },
46         { KE_KEY, 7, { KEY_PAGEDOWN } },
47         { KE_KEY, 8, { KEY_RFKILL } },
48         { KE_KEY, 9, { KEY_POWER } },
49         { KE_KEY, 11, { KEY_SLEEP } },
50         /* 13 has two different meanings in the spec -- ignore it. */
51         { KE_KEY, 14, { KEY_STOPCD } },
52         { KE_KEY, 15, { KEY_PLAYPAUSE } },
53         { KE_KEY, 16, { KEY_MUTE } },
54         { KE_KEY, 17, { KEY_VOLUMEUP } },
55         { KE_KEY, 18, { KEY_VOLUMEDOWN } },
56         { KE_KEY, 19, { KEY_BRIGHTNESSUP } },
57         { KE_KEY, 20, { KEY_BRIGHTNESSDOWN } },
58         /* 27: wake -- needs special handling */
59         { KE_END },
60 };
61
62 /* 5 button array notification value. */
63 static const struct key_entry intel_array_keymap[] = {
64         { KE_KEY,    0xC2, { KEY_LEFTMETA } },                /* Press */
65         { KE_IGNORE, 0xC3, { KEY_LEFTMETA } },                /* Release */
66         { KE_KEY,    0xC4, { KEY_VOLUMEUP } },                /* Press */
67         { KE_IGNORE, 0xC5, { KEY_VOLUMEUP } },                /* Release */
68         { KE_KEY,    0xC6, { KEY_VOLUMEDOWN } },              /* Press */
69         { KE_IGNORE, 0xC7, { KEY_VOLUMEDOWN } },              /* Release */
70         { KE_SW,     0xC8, { .sw = { SW_ROTATE_LOCK, 1 } } }, /* Press */
71         { KE_SW,     0xC9, { .sw = { SW_ROTATE_LOCK, 0 } } }, /* Release */
72         { KE_KEY,    0xCE, { KEY_POWER } },                   /* Press */
73         { KE_IGNORE, 0xCF, { KEY_POWER } },                   /* Release */
74         { KE_END },
75 };
76
77 static const struct dmi_system_id button_array_table[] = {
78         {
79                 .ident = "Wacom MobileStudio Pro 13",
80                 .matches = {
81                         DMI_MATCH(DMI_SYS_VENDOR, "Wacom Co.,Ltd"),
82                         DMI_MATCH(DMI_PRODUCT_NAME, "Wacom MobileStudio Pro 13"),
83                 },
84         },
85         {
86                 .ident = "Wacom MobileStudio Pro 16",
87                 .matches = {
88                         DMI_MATCH(DMI_SYS_VENDOR, "Wacom Co.,Ltd"),
89                         DMI_MATCH(DMI_PRODUCT_NAME, "Wacom MobileStudio Pro 16"),
90                 },
91         },
92         { }
93 };
94
95 struct intel_hid_priv {
96         struct input_dev *input_dev;
97         struct input_dev *array;
98         bool wakeup_mode;
99 };
100
101 static int intel_hid_set_enable(struct device *device, bool enable)
102 {
103         acpi_status status;
104
105         status = acpi_execute_simple_method(ACPI_HANDLE(device), "HDSM",
106                                             enable);
107         if (ACPI_FAILURE(status)) {
108                 dev_warn(device, "failed to %sable hotkeys\n",
109                          enable ? "en" : "dis");
110                 return -EIO;
111         }
112
113         return 0;
114 }
115
116 static void intel_button_array_enable(struct device *device, bool enable)
117 {
118         struct intel_hid_priv *priv = dev_get_drvdata(device);
119         acpi_handle handle = ACPI_HANDLE(device);
120         unsigned long long button_cap;
121         acpi_status status;
122
123         if (!priv->array)
124                 return;
125
126         /* Query supported platform features */
127         status = acpi_evaluate_integer(handle, "BTNC", NULL, &button_cap);
128         if (ACPI_FAILURE(status)) {
129                 dev_warn(device, "failed to get button capability\n");
130                 return;
131         }
132
133         /* Enable|disable features - power button is always enabled */
134         status = acpi_execute_simple_method(handle, "BTNE",
135                                             enable ? button_cap : 1);
136         if (ACPI_FAILURE(status))
137                 dev_warn(device, "failed to set button capability\n");
138 }
139
140 static int intel_hid_pm_prepare(struct device *device)
141 {
142         struct intel_hid_priv *priv = dev_get_drvdata(device);
143
144         priv->wakeup_mode = true;
145         return 0;
146 }
147
148 static int intel_hid_pl_suspend_handler(struct device *device)
149 {
150         if (pm_suspend_via_firmware()) {
151                 intel_hid_set_enable(device, false);
152                 intel_button_array_enable(device, false);
153         }
154         return 0;
155 }
156
157 static int intel_hid_pl_resume_handler(struct device *device)
158 {
159         struct intel_hid_priv *priv = dev_get_drvdata(device);
160
161         priv->wakeup_mode = false;
162         if (pm_resume_via_firmware()) {
163                 intel_hid_set_enable(device, true);
164                 intel_button_array_enable(device, true);
165         }
166         return 0;
167 }
168
169 static const struct dev_pm_ops intel_hid_pl_pm_ops = {
170         .prepare = intel_hid_pm_prepare,
171         .freeze  = intel_hid_pl_suspend_handler,
172         .thaw  = intel_hid_pl_resume_handler,
173         .restore  = intel_hid_pl_resume_handler,
174         .suspend  = intel_hid_pl_suspend_handler,
175         .resume  = intel_hid_pl_resume_handler,
176 };
177
178 static int intel_hid_input_setup(struct platform_device *device)
179 {
180         struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
181         int ret;
182
183         priv->input_dev = devm_input_allocate_device(&device->dev);
184         if (!priv->input_dev)
185                 return -ENOMEM;
186
187         ret = sparse_keymap_setup(priv->input_dev, intel_hid_keymap, NULL);
188         if (ret)
189                 return ret;
190
191         priv->input_dev->name = "Intel HID events";
192         priv->input_dev->id.bustype = BUS_HOST;
193
194         return input_register_device(priv->input_dev);
195 }
196
197 static int intel_button_array_input_setup(struct platform_device *device)
198 {
199         struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
200         int ret;
201
202         /* Setup input device for 5 button array */
203         priv->array = devm_input_allocate_device(&device->dev);
204         if (!priv->array)
205                 return -ENOMEM;
206
207         ret = sparse_keymap_setup(priv->array, intel_array_keymap, NULL);
208         if (ret)
209                 return ret;
210
211         priv->array->name = "Intel HID 5 button array";
212         priv->array->id.bustype = BUS_HOST;
213
214         return input_register_device(priv->array);
215 }
216
217 static void notify_handler(acpi_handle handle, u32 event, void *context)
218 {
219         struct platform_device *device = context;
220         struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
221         unsigned long long ev_index;
222         acpi_status status;
223
224         if (priv->wakeup_mode) {
225                 /*
226                  * Needed for wakeup from suspend-to-idle to work on some
227                  * platforms that don't expose the 5-button array, but still
228                  * send notifies with the power button event code to this
229                  * device object on power button actions while suspended.
230                  */
231                 if (event == 0xce)
232                         goto wakeup;
233
234                 /* Wake up on 5-button array events only. */
235                 if (event == 0xc0 || !priv->array)
236                         return;
237
238                 if (!sparse_keymap_entry_from_scancode(priv->array, event)) {
239                         dev_info(&device->dev, "unknown event 0x%x\n", event);
240                         return;
241                 }
242
243 wakeup:
244                 pm_wakeup_hard_event(&device->dev);
245                 return;
246         }
247
248         /*
249          * Needed for suspend to work on some platforms that don't expose
250          * the 5-button array, but still send notifies with power button
251          * event code to this device object on power button actions.
252          *
253          * Report the power button press; catch and ignore the button release.
254          */
255         if (!priv->array) {
256                 if (event == 0xce) {
257                         input_report_key(priv->input_dev, KEY_POWER, 1);
258                         input_sync(priv->input_dev);
259                         return;
260                 }
261
262                 if (event == 0xcf)
263                         return;
264         }
265
266         /* 0xC0 is for HID events, other values are for 5 button array */
267         if (event != 0xc0) {
268                 if (!priv->array ||
269                     !sparse_keymap_report_event(priv->array, event, 1, true))
270                         dev_dbg(&device->dev, "unknown event 0x%x\n", event);
271                 return;
272         }
273
274         status = acpi_evaluate_integer(handle, "HDEM", NULL, &ev_index);
275         if (ACPI_FAILURE(status)) {
276                 dev_warn(&device->dev, "failed to get event index\n");
277                 return;
278         }
279
280         if (!sparse_keymap_report_event(priv->input_dev, ev_index, 1, true))
281                 dev_dbg(&device->dev, "unknown event index 0x%llx\n",
282                          ev_index);
283 }
284
285 static bool button_array_present(struct platform_device *device)
286 {
287         acpi_handle handle = ACPI_HANDLE(&device->dev);
288         unsigned long long event_cap;
289         acpi_status status;
290         bool supported = false;
291
292         status = acpi_evaluate_integer(handle, "HEBC", NULL, &event_cap);
293         if (ACPI_SUCCESS(status) && (event_cap & 0x20000))
294                 supported = true;
295
296         if (dmi_check_system(button_array_table))
297                 supported = true;
298
299         return supported;
300 }
301
302 static int intel_hid_probe(struct platform_device *device)
303 {
304         acpi_handle handle = ACPI_HANDLE(&device->dev);
305         unsigned long long mode;
306         struct intel_hid_priv *priv;
307         acpi_status status;
308         int err;
309
310         status = acpi_evaluate_integer(handle, "HDMM", NULL, &mode);
311         if (ACPI_FAILURE(status)) {
312                 dev_warn(&device->dev, "failed to read mode\n");
313                 return -ENODEV;
314         }
315
316         if (mode != 0) {
317                 /*
318                  * This driver only implements "simple" mode.  There appear
319                  * to be no other modes, but we should be paranoid and check
320                  * for compatibility.
321                  */
322                 dev_info(&device->dev, "platform is not in simple mode\n");
323                 return -ENODEV;
324         }
325
326         priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
327         if (!priv)
328                 return -ENOMEM;
329         dev_set_drvdata(&device->dev, priv);
330
331         err = intel_hid_input_setup(device);
332         if (err) {
333                 pr_err("Failed to setup Intel HID hotkeys\n");
334                 return err;
335         }
336
337         /* Setup 5 button array */
338         if (button_array_present(device)) {
339                 dev_info(&device->dev, "platform supports 5 button array\n");
340                 err = intel_button_array_input_setup(device);
341                 if (err)
342                         pr_err("Failed to setup Intel 5 button array hotkeys\n");
343         }
344
345         status = acpi_install_notify_handler(handle,
346                                              ACPI_DEVICE_NOTIFY,
347                                              notify_handler,
348                                              device);
349         if (ACPI_FAILURE(status))
350                 return -EBUSY;
351
352         err = intel_hid_set_enable(&device->dev, true);
353         if (err)
354                 goto err_remove_notify;
355
356         if (priv->array) {
357                 intel_button_array_enable(&device->dev, true);
358
359                 /* Call button load method to enable HID power button */
360                 status = acpi_evaluate_object(handle, "BTNL", NULL, NULL);
361                 if (ACPI_FAILURE(status))
362                         dev_warn(&device->dev,
363                                  "failed to enable HID power button\n");
364         }
365
366         device_init_wakeup(&device->dev, true);
367         return 0;
368
369 err_remove_notify:
370         acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
371
372         return err;
373 }
374
375 static int intel_hid_remove(struct platform_device *device)
376 {
377         acpi_handle handle = ACPI_HANDLE(&device->dev);
378
379         acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
380         intel_hid_set_enable(&device->dev, false);
381         intel_button_array_enable(&device->dev, false);
382
383         /*
384          * Even if we failed to shut off the event stream, we can still
385          * safely detach from the device.
386          */
387         return 0;
388 }
389
390 static struct platform_driver intel_hid_pl_driver = {
391         .driver = {
392                 .name = "intel-hid",
393                 .acpi_match_table = intel_hid_ids,
394                 .pm = &intel_hid_pl_pm_ops,
395         },
396         .probe = intel_hid_probe,
397         .remove = intel_hid_remove,
398 };
399 MODULE_DEVICE_TABLE(acpi, intel_hid_ids);
400
401 /*
402  * Unfortunately, some laptops provide a _HID="INT33D5" device with
403  * _CID="PNP0C02".  This causes the pnpacpi scan driver to claim the
404  * ACPI node, so no platform device will be created.  The pnpacpi
405  * driver rejects this device in subsequent processing, so no physical
406  * node is created at all.
407  *
408  * As a workaround until the ACPI core figures out how to handle
409  * this corner case, manually ask the ACPI platform device code to
410  * claim the ACPI node.
411  */
412 static acpi_status __init
413 check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
414 {
415         const struct acpi_device_id *ids = context;
416         struct acpi_device *dev;
417
418         if (acpi_bus_get_device(handle, &dev) != 0)
419                 return AE_OK;
420
421         if (acpi_match_device_ids(dev, ids) == 0)
422                 if (acpi_create_platform_device(dev, NULL))
423                         dev_info(&dev->dev,
424                                  "intel-hid: created platform device\n");
425
426         return AE_OK;
427 }
428
429 static int __init intel_hid_init(void)
430 {
431         acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
432                             ACPI_UINT32_MAX, check_acpi_dev, NULL,
433                             (void *)intel_hid_ids, NULL);
434
435         return platform_driver_register(&intel_hid_pl_driver);
436 }
437 module_init(intel_hid_init);
438
439 static void __exit intel_hid_exit(void)
440 {
441         platform_driver_unregister(&intel_hid_pl_driver);
442 }
443 module_exit(intel_hid_exit);