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