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