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