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