1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Intel HID event & 5 button array driver
4 *
5 * Copyright (C) 2015 Alex Hung <alex.hung@canonical.com>
6 * Copyright (C) 2015 Andrew Lutomirski <luto@kernel.org>
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 #include "../dual_accel_detect.h"
18
19 /* When NOT in tablet mode, VGBS returns with the flag 0x40 */
20 #define TABLET_MODE_FLAG BIT(6)
21
22 MODULE_LICENSE("GPL");
23 MODULE_AUTHOR("Alex Hung");
24
25 static const struct acpi_device_id intel_hid_ids[] = {
26 {"INT33D5", 0},
27 {"INTC1051", 0},
28 {"INTC1054", 0},
29 {"INTC1070", 0},
30 {"INTC1076", 0},
31 {"INTC1077", 0},
32 {"INTC1078", 0},
33 {"", 0},
34 };
35 MODULE_DEVICE_TABLE(acpi, intel_hid_ids);
36
37 /* In theory, these are HID usages. */
38 static const struct key_entry intel_hid_keymap[] = {
39 /* 1: LSuper (Page 0x07, usage 0xE3) -- unclear what to do */
40 /* 2: Toggle SW_ROTATE_LOCK -- easy to implement if seen in wild */
41 { KE_KEY, 3, { KEY_NUMLOCK } },
42 { KE_KEY, 4, { KEY_HOME } },
43 { KE_KEY, 5, { KEY_END } },
44 { KE_KEY, 6, { KEY_PAGEUP } },
45 { KE_KEY, 7, { KEY_PAGEDOWN } },
46 { KE_KEY, 8, { KEY_RFKILL } },
47 { KE_KEY, 9, { KEY_POWER } },
48 { KE_KEY, 11, { KEY_SLEEP } },
49 /* 13 has two different meanings in the spec -- ignore it. */
50 { KE_KEY, 14, { KEY_STOPCD } },
51 { KE_KEY, 15, { KEY_PLAYPAUSE } },
52 { KE_KEY, 16, { KEY_MUTE } },
53 { KE_KEY, 17, { KEY_VOLUMEUP } },
54 { KE_KEY, 18, { KEY_VOLUMEDOWN } },
55 { KE_KEY, 19, { KEY_BRIGHTNESSUP } },
56 { KE_KEY, 20, { KEY_BRIGHTNESSDOWN } },
57 /* 27: wake -- needs special handling */
58 { KE_END },
59 };
60
61 /* 5 button array notification value. */
62 static const struct key_entry intel_array_keymap[] = {
63 { KE_KEY, 0xC2, { KEY_LEFTMETA } }, /* Press */
64 { KE_IGNORE, 0xC3, { KEY_LEFTMETA } }, /* Release */
65 { KE_KEY, 0xC4, { KEY_VOLUMEUP } }, /* Press */
66 { KE_IGNORE, 0xC5, { KEY_VOLUMEUP } }, /* Release */
67 { KE_KEY, 0xC6, { KEY_VOLUMEDOWN } }, /* Press */
68 { KE_IGNORE, 0xC7, { KEY_VOLUMEDOWN } }, /* Release */
69 { KE_KEY, 0xC8, { KEY_ROTATE_LOCK_TOGGLE } }, /* Press */
70 { KE_IGNORE, 0xC9, { KEY_ROTATE_LOCK_TOGGLE } }, /* Release */
71 { KE_KEY, 0xCE, { KEY_POWER } }, /* Press */
72 { KE_IGNORE, 0xCF, { KEY_POWER } }, /* Release */
73 { KE_END },
74 };
75
76 static const struct dmi_system_id button_array_table[] = {
77 {
78 .ident = "Wacom MobileStudio Pro 13",
79 .matches = {
80 DMI_MATCH(DMI_SYS_VENDOR, "Wacom Co.,Ltd"),
81 DMI_MATCH(DMI_PRODUCT_NAME, "Wacom MobileStudio Pro 13"),
82 },
83 },
84 {
85 .ident = "Wacom MobileStudio Pro 16",
86 .matches = {
87 DMI_MATCH(DMI_SYS_VENDOR, "Wacom Co.,Ltd"),
88 DMI_MATCH(DMI_PRODUCT_NAME, "Wacom MobileStudio Pro 16"),
89 },
90 },
91 {
92 .ident = "HP Spectre x2 (2015)",
93 .matches = {
94 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
95 DMI_MATCH(DMI_PRODUCT_NAME, "HP Spectre x2 Detachable"),
96 },
97 },
98 {
99 .ident = "Lenovo ThinkPad X1 Tablet Gen 2",
100 .matches = {
101 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
102 DMI_MATCH(DMI_PRODUCT_FAMILY, "ThinkPad X1 Tablet Gen 2"),
103 },
104 },
105 {
106 .ident = "Microsoft Surface Go 3",
107 .matches = {
108 DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
109 DMI_MATCH(DMI_PRODUCT_NAME, "Surface Go 3"),
110 },
111 },
112 { }
113 };
114
115 /*
116 * Some convertible use the intel-hid ACPI interface to report SW_TABLET_MODE,
117 * these need to be compared via a DMI based authorization list because some
118 * models have unreliable VGBS return which could cause incorrect
119 * SW_TABLET_MODE report.
120 */
121 static const struct dmi_system_id dmi_vgbs_allow_list[] = {
122 {
123 .matches = {
124 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
125 DMI_MATCH(DMI_PRODUCT_NAME, "HP Spectre x360 Convertible 15-df0xxx"),
126 },
127 },
128 {
129 .matches = {
130 DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
131 DMI_MATCH(DMI_PRODUCT_NAME, "Surface Go"),
132 },
133 },
134 { }
135 };
136
137 /*
138 * Some devices, even non convertible ones, can send incorrect SW_TABLET_MODE
139 * reports. Accept such reports only from devices in this list.
140 */
141 static const struct dmi_system_id dmi_auto_add_switch[] = {
142 {
143 .matches = {
144 DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "31" /* Convertible */),
145 },
146 },
147 {
148 .matches = {
149 DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "32" /* Detachable */),
150 },
151 },
152 {} /* Array terminator */
153 };
154
155 struct intel_hid_priv {
156 struct input_dev *input_dev;
157 struct input_dev *array;
158 struct input_dev *switches;
159 bool wakeup_mode;
160 bool auto_add_switch;
161 };
162
163 #define HID_EVENT_FILTER_UUID "eeec56b3-4442-408f-a792-4edd4d758054"
164
165 enum intel_hid_dsm_fn_codes {
166 INTEL_HID_DSM_FN_INVALID,
167 INTEL_HID_DSM_BTNL_FN,
168 INTEL_HID_DSM_HDMM_FN,
169 INTEL_HID_DSM_HDSM_FN,
170 INTEL_HID_DSM_HDEM_FN,
171 INTEL_HID_DSM_BTNS_FN,
172 INTEL_HID_DSM_BTNE_FN,
173 INTEL_HID_DSM_HEBC_V1_FN,
174 INTEL_HID_DSM_VGBS_FN,
175 INTEL_HID_DSM_HEBC_V2_FN,
176 INTEL_HID_DSM_FN_MAX
177 };
178
179 static const char *intel_hid_dsm_fn_to_method[INTEL_HID_DSM_FN_MAX] = {
180 NULL,
181 "BTNL",
182 "HDMM",
183 "HDSM",
184 "HDEM",
185 "BTNS",
186 "BTNE",
187 "HEBC",
188 "VGBS",
189 "HEBC"
190 };
191
192 static unsigned long long intel_hid_dsm_fn_mask;
193 static guid_t intel_dsm_guid;
194
intel_hid_execute_method(acpi_handle handle,enum intel_hid_dsm_fn_codes fn_index,unsigned long long arg)195 static bool intel_hid_execute_method(acpi_handle handle,
196 enum intel_hid_dsm_fn_codes fn_index,
197 unsigned long long arg)
198 {
199 union acpi_object *obj, argv4, req;
200 acpi_status status;
201 char *method_name;
202
203 if (fn_index <= INTEL_HID_DSM_FN_INVALID ||
204 fn_index >= INTEL_HID_DSM_FN_MAX)
205 return false;
206
207 method_name = (char *)intel_hid_dsm_fn_to_method[fn_index];
208
209 if (!(intel_hid_dsm_fn_mask & BIT(fn_index)))
210 goto skip_dsm_exec;
211
212 /* All methods expects a package with one integer element */
213 req.type = ACPI_TYPE_INTEGER;
214 req.integer.value = arg;
215
216 argv4.type = ACPI_TYPE_PACKAGE;
217 argv4.package.count = 1;
218 argv4.package.elements = &req;
219
220 obj = acpi_evaluate_dsm(handle, &intel_dsm_guid, 1, fn_index, &argv4);
221 if (obj) {
222 acpi_handle_debug(handle, "Exec DSM Fn code: %d[%s] success\n",
223 fn_index, method_name);
224 ACPI_FREE(obj);
225 return true;
226 }
227
228 skip_dsm_exec:
229 status = acpi_execute_simple_method(handle, method_name, arg);
230 if (ACPI_SUCCESS(status))
231 return true;
232
233 return false;
234 }
235
intel_hid_evaluate_method(acpi_handle handle,enum intel_hid_dsm_fn_codes fn_index,unsigned long long * result)236 static bool intel_hid_evaluate_method(acpi_handle handle,
237 enum intel_hid_dsm_fn_codes fn_index,
238 unsigned long long *result)
239 {
240 union acpi_object *obj;
241 acpi_status status;
242 char *method_name;
243
244 if (fn_index <= INTEL_HID_DSM_FN_INVALID ||
245 fn_index >= INTEL_HID_DSM_FN_MAX)
246 return false;
247
248 method_name = (char *)intel_hid_dsm_fn_to_method[fn_index];
249
250 if (!(intel_hid_dsm_fn_mask & BIT(fn_index)))
251 goto skip_dsm_eval;
252
253 obj = acpi_evaluate_dsm_typed(handle, &intel_dsm_guid,
254 1, fn_index,
255 NULL, ACPI_TYPE_INTEGER);
256 if (obj) {
257 *result = obj->integer.value;
258 acpi_handle_debug(handle,
259 "Eval DSM Fn code: %d[%s] results: 0x%llx\n",
260 fn_index, method_name, *result);
261 ACPI_FREE(obj);
262 return true;
263 }
264
265 skip_dsm_eval:
266 status = acpi_evaluate_integer(handle, method_name, NULL, result);
267 if (ACPI_SUCCESS(status))
268 return true;
269
270 return false;
271 }
272
intel_hid_init_dsm(acpi_handle handle)273 static void intel_hid_init_dsm(acpi_handle handle)
274 {
275 union acpi_object *obj;
276
277 guid_parse(HID_EVENT_FILTER_UUID, &intel_dsm_guid);
278
279 obj = acpi_evaluate_dsm_typed(handle, &intel_dsm_guid, 1, 0, NULL,
280 ACPI_TYPE_BUFFER);
281 if (obj) {
282 switch (obj->buffer.length) {
283 default:
284 case 2:
285 intel_hid_dsm_fn_mask = *(u16 *)obj->buffer.pointer;
286 break;
287 case 1:
288 intel_hid_dsm_fn_mask = *obj->buffer.pointer;
289 break;
290 case 0:
291 acpi_handle_warn(handle, "intel_hid_dsm_fn_mask length is zero\n");
292 intel_hid_dsm_fn_mask = 0;
293 break;
294 }
295 ACPI_FREE(obj);
296 }
297
298 acpi_handle_debug(handle, "intel_hid_dsm_fn_mask = %llx\n",
299 intel_hid_dsm_fn_mask);
300 }
301
intel_hid_set_enable(struct device * device,bool enable)302 static int intel_hid_set_enable(struct device *device, bool enable)
303 {
304 acpi_handle handle = ACPI_HANDLE(device);
305
306 /* Enable|disable features - power button is always enabled */
307 if (!intel_hid_execute_method(handle, INTEL_HID_DSM_HDSM_FN,
308 enable)) {
309 dev_warn(device, "failed to %sable hotkeys\n",
310 enable ? "en" : "dis");
311 return -EIO;
312 }
313
314 return 0;
315 }
316
intel_button_array_enable(struct device * device,bool enable)317 static void intel_button_array_enable(struct device *device, bool enable)
318 {
319 struct intel_hid_priv *priv = dev_get_drvdata(device);
320 acpi_handle handle = ACPI_HANDLE(device);
321 unsigned long long button_cap;
322 acpi_status status;
323
324 if (!priv->array)
325 return;
326
327 /* Query supported platform features */
328 status = acpi_evaluate_integer(handle, "BTNC", NULL, &button_cap);
329 if (ACPI_FAILURE(status)) {
330 dev_warn(device, "failed to get button capability\n");
331 return;
332 }
333
334 /* Enable|disable features - power button is always enabled */
335 if (!intel_hid_execute_method(handle, INTEL_HID_DSM_BTNE_FN,
336 enable ? button_cap : 1))
337 dev_warn(device, "failed to set button capability\n");
338 }
339
intel_hid_pm_prepare(struct device * device)340 static int intel_hid_pm_prepare(struct device *device)
341 {
342 if (device_may_wakeup(device)) {
343 struct intel_hid_priv *priv = dev_get_drvdata(device);
344
345 priv->wakeup_mode = true;
346 }
347 return 0;
348 }
349
intel_hid_pm_complete(struct device * device)350 static void intel_hid_pm_complete(struct device *device)
351 {
352 struct intel_hid_priv *priv = dev_get_drvdata(device);
353
354 priv->wakeup_mode = false;
355 }
356
intel_hid_pl_suspend_handler(struct device * device)357 static int intel_hid_pl_suspend_handler(struct device *device)
358 {
359 intel_button_array_enable(device, false);
360
361 if (!pm_suspend_no_platform())
362 intel_hid_set_enable(device, false);
363
364 return 0;
365 }
366
intel_hid_pl_resume_handler(struct device * device)367 static int intel_hid_pl_resume_handler(struct device *device)
368 {
369 intel_hid_pm_complete(device);
370
371 if (!pm_suspend_no_platform())
372 intel_hid_set_enable(device, true);
373
374 intel_button_array_enable(device, true);
375 return 0;
376 }
377
378 static const struct dev_pm_ops intel_hid_pl_pm_ops = {
379 .prepare = intel_hid_pm_prepare,
380 .complete = intel_hid_pm_complete,
381 .freeze = intel_hid_pl_suspend_handler,
382 .thaw = intel_hid_pl_resume_handler,
383 .restore = intel_hid_pl_resume_handler,
384 .suspend = intel_hid_pl_suspend_handler,
385 .resume = intel_hid_pl_resume_handler,
386 };
387
intel_hid_input_setup(struct platform_device * device)388 static int intel_hid_input_setup(struct platform_device *device)
389 {
390 struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
391 int ret;
392
393 priv->input_dev = devm_input_allocate_device(&device->dev);
394 if (!priv->input_dev)
395 return -ENOMEM;
396
397 ret = sparse_keymap_setup(priv->input_dev, intel_hid_keymap, NULL);
398 if (ret)
399 return ret;
400
401 priv->input_dev->name = "Intel HID events";
402 priv->input_dev->id.bustype = BUS_HOST;
403
404 return input_register_device(priv->input_dev);
405 }
406
intel_button_array_input_setup(struct platform_device * device)407 static int intel_button_array_input_setup(struct platform_device *device)
408 {
409 struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
410 int ret;
411
412 /* Setup input device for 5 button array */
413 priv->array = devm_input_allocate_device(&device->dev);
414 if (!priv->array)
415 return -ENOMEM;
416
417 ret = sparse_keymap_setup(priv->array, intel_array_keymap, NULL);
418 if (ret)
419 return ret;
420
421 priv->array->name = "Intel HID 5 button array";
422 priv->array->id.bustype = BUS_HOST;
423
424 return input_register_device(priv->array);
425 }
426
intel_hid_switches_setup(struct platform_device * device)427 static int intel_hid_switches_setup(struct platform_device *device)
428 {
429 struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
430
431 /* Setup input device for switches */
432 priv->switches = devm_input_allocate_device(&device->dev);
433 if (!priv->switches)
434 return -ENOMEM;
435
436 __set_bit(EV_SW, priv->switches->evbit);
437 __set_bit(SW_TABLET_MODE, priv->switches->swbit);
438
439 priv->switches->name = "Intel HID switches";
440 priv->switches->id.bustype = BUS_HOST;
441 return input_register_device(priv->switches);
442 }
443
report_tablet_mode_state(struct platform_device * device)444 static void report_tablet_mode_state(struct platform_device *device)
445 {
446 struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
447 acpi_handle handle = ACPI_HANDLE(&device->dev);
448 unsigned long long vgbs;
449 int m;
450
451 if (!intel_hid_evaluate_method(handle, INTEL_HID_DSM_VGBS_FN, &vgbs))
452 return;
453
454 m = !(vgbs & TABLET_MODE_FLAG);
455 input_report_switch(priv->switches, SW_TABLET_MODE, m);
456 input_sync(priv->switches);
457 }
458
report_tablet_mode_event(struct input_dev * input_dev,u32 event)459 static bool report_tablet_mode_event(struct input_dev *input_dev, u32 event)
460 {
461 if (!input_dev)
462 return false;
463
464 switch (event) {
465 case 0xcc:
466 input_report_switch(input_dev, SW_TABLET_MODE, 1);
467 input_sync(input_dev);
468 return true;
469 case 0xcd:
470 input_report_switch(input_dev, SW_TABLET_MODE, 0);
471 input_sync(input_dev);
472 return true;
473 default:
474 return false;
475 }
476 }
477
notify_handler(acpi_handle handle,u32 event,void * context)478 static void notify_handler(acpi_handle handle, u32 event, void *context)
479 {
480 struct platform_device *device = context;
481 struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
482 unsigned long long ev_index;
483 int err;
484
485 /*
486 * Some convertible have unreliable VGBS return which could cause incorrect
487 * SW_TABLET_MODE report, in these cases we enable support when receiving
488 * the first event instead of during driver setup.
489 */
490 if (!priv->switches && priv->auto_add_switch && (event == 0xcc || event == 0xcd)) {
491 dev_info(&device->dev, "switch event received, enable switches supports\n");
492 err = intel_hid_switches_setup(device);
493 if (err)
494 pr_err("Failed to setup Intel HID switches\n");
495 }
496
497 if (priv->wakeup_mode) {
498 /*
499 * Needed for wakeup from suspend-to-idle to work on some
500 * platforms that don't expose the 5-button array, but still
501 * send notifies with the power button event code to this
502 * device object on power button actions while suspended.
503 */
504 if (event == 0xce)
505 goto wakeup;
506
507 /*
508 * Some devices send (duplicate) tablet-mode events when moved
509 * around even though the mode has not changed; and they do this
510 * even when suspended.
511 * Update the switch state in case it changed and then return
512 * without waking up to avoid spurious wakeups.
513 */
514 if (event == 0xcc || event == 0xcd) {
515 report_tablet_mode_event(priv->switches, event);
516 return;
517 }
518
519 /* Wake up on 5-button array events only. */
520 if (event == 0xc0 || !priv->array)
521 return;
522
523 if (!sparse_keymap_entry_from_scancode(priv->array, event)) {
524 dev_info(&device->dev, "unknown event 0x%x\n", event);
525 return;
526 }
527
528 wakeup:
529 pm_wakeup_hard_event(&device->dev);
530
531 return;
532 }
533
534 /*
535 * Needed for suspend to work on some platforms that don't expose
536 * the 5-button array, but still send notifies with power button
537 * event code to this device object on power button actions.
538 *
539 * Report the power button press and release.
540 */
541 if (!priv->array) {
542 if (event == 0xce) {
543 input_report_key(priv->input_dev, KEY_POWER, 1);
544 input_sync(priv->input_dev);
545 return;
546 }
547
548 if (event == 0xcf) {
549 input_report_key(priv->input_dev, KEY_POWER, 0);
550 input_sync(priv->input_dev);
551 return;
552 }
553 }
554
555 if (report_tablet_mode_event(priv->switches, event))
556 return;
557
558 /* 0xC0 is for HID events, other values are for 5 button array */
559 if (event != 0xc0) {
560 if (!priv->array ||
561 !sparse_keymap_report_event(priv->array, event, 1, true))
562 dev_dbg(&device->dev, "unknown event 0x%x\n", event);
563 return;
564 }
565
566 if (!intel_hid_evaluate_method(handle, INTEL_HID_DSM_HDEM_FN,
567 &ev_index)) {
568 dev_warn(&device->dev, "failed to get event index\n");
569 return;
570 }
571
572 if (!sparse_keymap_report_event(priv->input_dev, ev_index, 1, true))
573 dev_dbg(&device->dev, "unknown event index 0x%llx\n",
574 ev_index);
575 }
576
button_array_present(struct platform_device * device)577 static bool button_array_present(struct platform_device *device)
578 {
579 acpi_handle handle = ACPI_HANDLE(&device->dev);
580 unsigned long long event_cap;
581
582 if (intel_hid_evaluate_method(handle, INTEL_HID_DSM_HEBC_V2_FN,
583 &event_cap)) {
584 /* Check presence of 5 button array or v2 power button */
585 if (event_cap & 0x60000)
586 return true;
587 }
588
589 if (intel_hid_evaluate_method(handle, INTEL_HID_DSM_HEBC_V1_FN,
590 &event_cap)) {
591 if (event_cap & 0x20000)
592 return true;
593 }
594
595 if (dmi_check_system(button_array_table))
596 return true;
597
598 return false;
599 }
600
intel_hid_probe(struct platform_device * device)601 static int intel_hid_probe(struct platform_device *device)
602 {
603 acpi_handle handle = ACPI_HANDLE(&device->dev);
604 unsigned long long mode;
605 struct intel_hid_priv *priv;
606 acpi_status status;
607 int err;
608
609 intel_hid_init_dsm(handle);
610
611 if (!intel_hid_evaluate_method(handle, INTEL_HID_DSM_HDMM_FN, &mode)) {
612 dev_warn(&device->dev, "failed to read mode\n");
613 return -ENODEV;
614 }
615
616 if (mode != 0) {
617 /*
618 * This driver only implements "simple" mode. There appear
619 * to be no other modes, but we should be paranoid and check
620 * for compatibility.
621 */
622 dev_info(&device->dev, "platform is not in simple mode\n");
623 return -ENODEV;
624 }
625
626 priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
627 if (!priv)
628 return -ENOMEM;
629 dev_set_drvdata(&device->dev, priv);
630
631 /* See dual_accel_detect.h for more info on the dual_accel check. */
632 priv->auto_add_switch = dmi_check_system(dmi_auto_add_switch) && !dual_accel_detect();
633
634 err = intel_hid_input_setup(device);
635 if (err) {
636 pr_err("Failed to setup Intel HID hotkeys\n");
637 return err;
638 }
639
640 /* Setup 5 button array */
641 if (button_array_present(device)) {
642 dev_info(&device->dev, "platform supports 5 button array\n");
643 err = intel_button_array_input_setup(device);
644 if (err)
645 pr_err("Failed to setup Intel 5 button array hotkeys\n");
646 }
647
648 /* Setup switches for devices that we know VGBS return correctly */
649 if (dmi_check_system(dmi_vgbs_allow_list)) {
650 dev_info(&device->dev, "platform supports switches\n");
651 err = intel_hid_switches_setup(device);
652 if (err)
653 pr_err("Failed to setup Intel HID switches\n");
654 else
655 report_tablet_mode_state(device);
656 }
657
658 status = acpi_install_notify_handler(handle,
659 ACPI_DEVICE_NOTIFY,
660 notify_handler,
661 device);
662 if (ACPI_FAILURE(status))
663 return -EBUSY;
664
665 err = intel_hid_set_enable(&device->dev, true);
666 if (err)
667 goto err_remove_notify;
668
669 if (priv->array) {
670 unsigned long long dummy;
671
672 intel_button_array_enable(&device->dev, true);
673
674 /* Call button load method to enable HID power button */
675 if (!intel_hid_evaluate_method(handle, INTEL_HID_DSM_BTNL_FN,
676 &dummy)) {
677 dev_warn(&device->dev,
678 "failed to enable HID power button\n");
679 }
680 }
681
682 device_init_wakeup(&device->dev, true);
683 /*
684 * In order for system wakeup to work, the EC GPE has to be marked as
685 * a wakeup one, so do that here (this setting will persist, but it has
686 * no effect until the wakeup mask is set for the EC GPE).
687 */
688 acpi_ec_mark_gpe_for_wake();
689 return 0;
690
691 err_remove_notify:
692 acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
693
694 return err;
695 }
696
intel_hid_remove(struct platform_device * device)697 static int intel_hid_remove(struct platform_device *device)
698 {
699 acpi_handle handle = ACPI_HANDLE(&device->dev);
700
701 device_init_wakeup(&device->dev, false);
702 acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
703 intel_hid_set_enable(&device->dev, false);
704 intel_button_array_enable(&device->dev, false);
705
706 /*
707 * Even if we failed to shut off the event stream, we can still
708 * safely detach from the device.
709 */
710 return 0;
711 }
712
713 static struct platform_driver intel_hid_pl_driver = {
714 .driver = {
715 .name = "intel-hid",
716 .acpi_match_table = intel_hid_ids,
717 .pm = &intel_hid_pl_pm_ops,
718 },
719 .probe = intel_hid_probe,
720 .remove = intel_hid_remove,
721 };
722
723 /*
724 * Unfortunately, some laptops provide a _HID="INT33D5" device with
725 * _CID="PNP0C02". This causes the pnpacpi scan driver to claim the
726 * ACPI node, so no platform device will be created. The pnpacpi
727 * driver rejects this device in subsequent processing, so no physical
728 * node is created at all.
729 *
730 * As a workaround until the ACPI core figures out how to handle
731 * this corner case, manually ask the ACPI platform device code to
732 * claim the ACPI node.
733 */
734 static acpi_status __init
check_acpi_dev(acpi_handle handle,u32 lvl,void * context,void ** rv)735 check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
736 {
737 const struct acpi_device_id *ids = context;
738 struct acpi_device *dev = acpi_fetch_acpi_dev(handle);
739
740 if (dev && acpi_match_device_ids(dev, ids) == 0)
741 if (!IS_ERR_OR_NULL(acpi_create_platform_device(dev, NULL)))
742 dev_info(&dev->dev,
743 "intel-hid: created platform device\n");
744
745 return AE_OK;
746 }
747
intel_hid_init(void)748 static int __init intel_hid_init(void)
749 {
750 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
751 ACPI_UINT32_MAX, check_acpi_dev, NULL,
752 (void *)intel_hid_ids, NULL);
753
754 return platform_driver_register(&intel_hid_pl_driver);
755 }
756 module_init(intel_hid_init);
757
intel_hid_exit(void)758 static void __exit intel_hid_exit(void)
759 {
760 platform_driver_unregister(&intel_hid_pl_driver);
761 }
762 module_exit(intel_hid_exit);
763