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