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