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