intel-hid.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. #include <linux/acpi.h>
  9. #include <linux/dmi.h>
  10. #include <linux/input.h>
  11. #include <linux/input/sparse-keymap.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/suspend.h>
  16. MODULE_LICENSE("GPL");
  17. MODULE_AUTHOR("Alex Hung");
  18. static const struct acpi_device_id intel_hid_ids[] = {
  19. {"INT33D5", 0},
  20. {"INTC1051", 0},
  21. {"", 0},
  22. };
  23. MODULE_DEVICE_TABLE(acpi, intel_hid_ids);
  24. /* In theory, these are HID usages. */
  25. static const struct key_entry intel_hid_keymap[] = {
  26. /* 1: LSuper (Page 0x07, usage 0xE3) -- unclear what to do */
  27. /* 2: Toggle SW_ROTATE_LOCK -- easy to implement if seen in wild */
  28. { KE_KEY, 3, { KEY_NUMLOCK } },
  29. { KE_KEY, 4, { KEY_HOME } },
  30. { KE_KEY, 5, { KEY_END } },
  31. { KE_KEY, 6, { KEY_PAGEUP } },
  32. { KE_KEY, 7, { KEY_PAGEDOWN } },
  33. { KE_KEY, 8, { KEY_RFKILL } },
  34. { KE_KEY, 9, { KEY_POWER } },
  35. { KE_KEY, 11, { KEY_SLEEP } },
  36. /* 13 has two different meanings in the spec -- ignore it. */
  37. { KE_KEY, 14, { KEY_STOPCD } },
  38. { KE_KEY, 15, { KEY_PLAYPAUSE } },
  39. { KE_KEY, 16, { KEY_MUTE } },
  40. { KE_KEY, 17, { KEY_VOLUMEUP } },
  41. { KE_KEY, 18, { KEY_VOLUMEDOWN } },
  42. { KE_KEY, 19, { KEY_BRIGHTNESSUP } },
  43. { KE_KEY, 20, { KEY_BRIGHTNESSDOWN } },
  44. /* 27: wake -- needs special handling */
  45. { KE_END },
  46. };
  47. /* 5 button array notification value. */
  48. static const struct key_entry intel_array_keymap[] = {
  49. { KE_KEY, 0xC2, { KEY_LEFTMETA } }, /* Press */
  50. { KE_IGNORE, 0xC3, { KEY_LEFTMETA } }, /* Release */
  51. { KE_KEY, 0xC4, { KEY_VOLUMEUP } }, /* Press */
  52. { KE_IGNORE, 0xC5, { KEY_VOLUMEUP } }, /* Release */
  53. { KE_KEY, 0xC6, { KEY_VOLUMEDOWN } }, /* Press */
  54. { KE_IGNORE, 0xC7, { KEY_VOLUMEDOWN } }, /* Release */
  55. { KE_KEY, 0xC8, { KEY_ROTATE_LOCK_TOGGLE } }, /* Press */
  56. { KE_IGNORE, 0xC9, { KEY_ROTATE_LOCK_TOGGLE } }, /* Release */
  57. { KE_KEY, 0xCE, { KEY_POWER } }, /* Press */
  58. { KE_IGNORE, 0xCF, { KEY_POWER } }, /* Release */
  59. { KE_END },
  60. };
  61. static const struct dmi_system_id button_array_table[] = {
  62. {
  63. .ident = "Wacom MobileStudio Pro 13",
  64. .matches = {
  65. DMI_MATCH(DMI_SYS_VENDOR, "Wacom Co.,Ltd"),
  66. DMI_MATCH(DMI_PRODUCT_NAME, "Wacom MobileStudio Pro 13"),
  67. },
  68. },
  69. {
  70. .ident = "Wacom MobileStudio Pro 16",
  71. .matches = {
  72. DMI_MATCH(DMI_SYS_VENDOR, "Wacom Co.,Ltd"),
  73. DMI_MATCH(DMI_PRODUCT_NAME, "Wacom MobileStudio Pro 16"),
  74. },
  75. },
  76. {
  77. .ident = "HP Spectre x2 (2015)",
  78. .matches = {
  79. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  80. DMI_MATCH(DMI_PRODUCT_NAME, "HP Spectre x2 Detachable"),
  81. },
  82. },
  83. {
  84. .ident = "Lenovo ThinkPad X1 Tablet Gen 2",
  85. .matches = {
  86. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  87. DMI_MATCH(DMI_PRODUCT_FAMILY, "ThinkPad X1 Tablet Gen 2"),
  88. },
  89. },
  90. { }
  91. };
  92. struct intel_hid_priv {
  93. struct input_dev *input_dev;
  94. struct input_dev *array;
  95. bool wakeup_mode;
  96. };
  97. #define HID_EVENT_FILTER_UUID "eeec56b3-4442-408f-a792-4edd4d758054"
  98. enum intel_hid_dsm_fn_codes {
  99. INTEL_HID_DSM_FN_INVALID,
  100. INTEL_HID_DSM_BTNL_FN,
  101. INTEL_HID_DSM_HDMM_FN,
  102. INTEL_HID_DSM_HDSM_FN,
  103. INTEL_HID_DSM_HDEM_FN,
  104. INTEL_HID_DSM_BTNS_FN,
  105. INTEL_HID_DSM_BTNE_FN,
  106. INTEL_HID_DSM_HEBC_V1_FN,
  107. INTEL_HID_DSM_VGBS_FN,
  108. INTEL_HID_DSM_HEBC_V2_FN,
  109. INTEL_HID_DSM_FN_MAX
  110. };
  111. static const char *intel_hid_dsm_fn_to_method[INTEL_HID_DSM_FN_MAX] = {
  112. NULL,
  113. "BTNL",
  114. "HDMM",
  115. "HDSM",
  116. "HDEM",
  117. "BTNS",
  118. "BTNE",
  119. "HEBC",
  120. "VGBS",
  121. "HEBC"
  122. };
  123. static unsigned long long intel_hid_dsm_fn_mask;
  124. static guid_t intel_dsm_guid;
  125. static bool intel_hid_execute_method(acpi_handle handle,
  126. enum intel_hid_dsm_fn_codes fn_index,
  127. unsigned long long arg)
  128. {
  129. union acpi_object *obj, argv4, req;
  130. acpi_status status;
  131. char *method_name;
  132. if (fn_index <= INTEL_HID_DSM_FN_INVALID ||
  133. fn_index >= INTEL_HID_DSM_FN_MAX)
  134. return false;
  135. method_name = (char *)intel_hid_dsm_fn_to_method[fn_index];
  136. if (!(intel_hid_dsm_fn_mask & fn_index))
  137. goto skip_dsm_exec;
  138. /* All methods expects a package with one integer element */
  139. req.type = ACPI_TYPE_INTEGER;
  140. req.integer.value = arg;
  141. argv4.type = ACPI_TYPE_PACKAGE;
  142. argv4.package.count = 1;
  143. argv4.package.elements = &req;
  144. obj = acpi_evaluate_dsm(handle, &intel_dsm_guid, 1, fn_index, &argv4);
  145. if (obj) {
  146. acpi_handle_debug(handle, "Exec DSM Fn code: %d[%s] success\n",
  147. fn_index, method_name);
  148. ACPI_FREE(obj);
  149. return true;
  150. }
  151. skip_dsm_exec:
  152. status = acpi_execute_simple_method(handle, method_name, arg);
  153. if (ACPI_SUCCESS(status))
  154. return true;
  155. return false;
  156. }
  157. static bool intel_hid_evaluate_method(acpi_handle handle,
  158. enum intel_hid_dsm_fn_codes fn_index,
  159. unsigned long long *result)
  160. {
  161. union acpi_object *obj;
  162. acpi_status status;
  163. char *method_name;
  164. if (fn_index <= INTEL_HID_DSM_FN_INVALID ||
  165. fn_index >= INTEL_HID_DSM_FN_MAX)
  166. return false;
  167. method_name = (char *)intel_hid_dsm_fn_to_method[fn_index];
  168. if (!(intel_hid_dsm_fn_mask & fn_index))
  169. goto skip_dsm_eval;
  170. obj = acpi_evaluate_dsm_typed(handle, &intel_dsm_guid,
  171. 1, fn_index,
  172. NULL, ACPI_TYPE_INTEGER);
  173. if (obj) {
  174. *result = obj->integer.value;
  175. acpi_handle_debug(handle,
  176. "Eval DSM Fn code: %d[%s] results: 0x%llx\n",
  177. fn_index, method_name, *result);
  178. ACPI_FREE(obj);
  179. return true;
  180. }
  181. skip_dsm_eval:
  182. status = acpi_evaluate_integer(handle, method_name, NULL, result);
  183. if (ACPI_SUCCESS(status))
  184. return true;
  185. return false;
  186. }
  187. static void intel_hid_init_dsm(acpi_handle handle)
  188. {
  189. union acpi_object *obj;
  190. guid_parse(HID_EVENT_FILTER_UUID, &intel_dsm_guid);
  191. obj = acpi_evaluate_dsm_typed(handle, &intel_dsm_guid, 1, 0, NULL,
  192. ACPI_TYPE_BUFFER);
  193. if (obj) {
  194. intel_hid_dsm_fn_mask = *obj->buffer.pointer;
  195. ACPI_FREE(obj);
  196. }
  197. acpi_handle_debug(handle, "intel_hid_dsm_fn_mask = %llx\n",
  198. intel_hid_dsm_fn_mask);
  199. }
  200. static int intel_hid_set_enable(struct device *device, bool enable)
  201. {
  202. acpi_handle handle = ACPI_HANDLE(device);
  203. /* Enable|disable features - power button is always enabled */
  204. if (!intel_hid_execute_method(handle, INTEL_HID_DSM_HDSM_FN,
  205. enable)) {
  206. dev_warn(device, "failed to %sable hotkeys\n",
  207. enable ? "en" : "dis");
  208. return -EIO;
  209. }
  210. return 0;
  211. }
  212. static void intel_button_array_enable(struct device *device, bool enable)
  213. {
  214. struct intel_hid_priv *priv = dev_get_drvdata(device);
  215. acpi_handle handle = ACPI_HANDLE(device);
  216. unsigned long long button_cap;
  217. acpi_status status;
  218. if (!priv->array)
  219. return;
  220. /* Query supported platform features */
  221. status = acpi_evaluate_integer(handle, "BTNC", NULL, &button_cap);
  222. if (ACPI_FAILURE(status)) {
  223. dev_warn(device, "failed to get button capability\n");
  224. return;
  225. }
  226. /* Enable|disable features - power button is always enabled */
  227. if (!intel_hid_execute_method(handle, INTEL_HID_DSM_BTNE_FN,
  228. enable ? button_cap : 1))
  229. dev_warn(device, "failed to set button capability\n");
  230. }
  231. static int intel_hid_pm_prepare(struct device *device)
  232. {
  233. if (device_may_wakeup(device)) {
  234. struct intel_hid_priv *priv = dev_get_drvdata(device);
  235. priv->wakeup_mode = true;
  236. }
  237. return 0;
  238. }
  239. static void intel_hid_pm_complete(struct device *device)
  240. {
  241. struct intel_hid_priv *priv = dev_get_drvdata(device);
  242. priv->wakeup_mode = false;
  243. }
  244. static int intel_hid_pl_suspend_handler(struct device *device)
  245. {
  246. intel_button_array_enable(device, false);
  247. if (!pm_suspend_no_platform())
  248. intel_hid_set_enable(device, false);
  249. return 0;
  250. }
  251. static int intel_hid_pl_resume_handler(struct device *device)
  252. {
  253. intel_hid_pm_complete(device);
  254. if (!pm_suspend_no_platform())
  255. intel_hid_set_enable(device, true);
  256. intel_button_array_enable(device, true);
  257. return 0;
  258. }
  259. static const struct dev_pm_ops intel_hid_pl_pm_ops = {
  260. .prepare = intel_hid_pm_prepare,
  261. .complete = intel_hid_pm_complete,
  262. .freeze = intel_hid_pl_suspend_handler,
  263. .thaw = intel_hid_pl_resume_handler,
  264. .restore = intel_hid_pl_resume_handler,
  265. .suspend = intel_hid_pl_suspend_handler,
  266. .resume = intel_hid_pl_resume_handler,
  267. };
  268. static int intel_hid_input_setup(struct platform_device *device)
  269. {
  270. struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
  271. int ret;
  272. priv->input_dev = devm_input_allocate_device(&device->dev);
  273. if (!priv->input_dev)
  274. return -ENOMEM;
  275. ret = sparse_keymap_setup(priv->input_dev, intel_hid_keymap, NULL);
  276. if (ret)
  277. return ret;
  278. priv->input_dev->name = "Intel HID events";
  279. priv->input_dev->id.bustype = BUS_HOST;
  280. return input_register_device(priv->input_dev);
  281. }
  282. static int intel_button_array_input_setup(struct platform_device *device)
  283. {
  284. struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
  285. int ret;
  286. /* Setup input device for 5 button array */
  287. priv->array = devm_input_allocate_device(&device->dev);
  288. if (!priv->array)
  289. return -ENOMEM;
  290. ret = sparse_keymap_setup(priv->array, intel_array_keymap, NULL);
  291. if (ret)
  292. return ret;
  293. priv->array->name = "Intel HID 5 button array";
  294. priv->array->id.bustype = BUS_HOST;
  295. return input_register_device(priv->array);
  296. }
  297. static void notify_handler(acpi_handle handle, u32 event, void *context)
  298. {
  299. struct platform_device *device = context;
  300. struct intel_hid_priv *priv = dev_get_drvdata(&device->dev);
  301. unsigned long long ev_index;
  302. if (priv->wakeup_mode) {
  303. /*
  304. * Needed for wakeup from suspend-to-idle to work on some
  305. * platforms that don't expose the 5-button array, but still
  306. * send notifies with the power button event code to this
  307. * device object on power button actions while suspended.
  308. */
  309. if (event == 0xce)
  310. goto wakeup;
  311. /* Wake up on 5-button array events only. */
  312. if (event == 0xc0 || !priv->array)
  313. return;
  314. if (!sparse_keymap_entry_from_scancode(priv->array, event)) {
  315. dev_info(&device->dev, "unknown event 0x%x\n", event);
  316. return;
  317. }
  318. wakeup:
  319. pm_wakeup_hard_event(&device->dev);
  320. return;
  321. }
  322. /*
  323. * Needed for suspend to work on some platforms that don't expose
  324. * the 5-button array, but still send notifies with power button
  325. * event code to this device object on power button actions.
  326. *
  327. * Report the power button press and release.
  328. */
  329. if (!priv->array) {
  330. if (event == 0xce) {
  331. input_report_key(priv->input_dev, KEY_POWER, 1);
  332. input_sync(priv->input_dev);
  333. return;
  334. }
  335. if (event == 0xcf) {
  336. input_report_key(priv->input_dev, KEY_POWER, 0);
  337. input_sync(priv->input_dev);
  338. return;
  339. }
  340. }
  341. /* 0xC0 is for HID events, other values are for 5 button array */
  342. if (event != 0xc0) {
  343. if (!priv->array ||
  344. !sparse_keymap_report_event(priv->array, event, 1, true))
  345. dev_dbg(&device->dev, "unknown event 0x%x\n", event);
  346. return;
  347. }
  348. if (!intel_hid_evaluate_method(handle, INTEL_HID_DSM_HDEM_FN,
  349. &ev_index)) {
  350. dev_warn(&device->dev, "failed to get event index\n");
  351. return;
  352. }
  353. if (!sparse_keymap_report_event(priv->input_dev, ev_index, 1, true))
  354. dev_dbg(&device->dev, "unknown event index 0x%llx\n",
  355. ev_index);
  356. }
  357. static bool button_array_present(struct platform_device *device)
  358. {
  359. acpi_handle handle = ACPI_HANDLE(&device->dev);
  360. unsigned long long event_cap;
  361. if (intel_hid_evaluate_method(handle, INTEL_HID_DSM_HEBC_V2_FN,
  362. &event_cap)) {
  363. /* Check presence of 5 button array or v2 power button */
  364. if (event_cap & 0x60000)
  365. return true;
  366. }
  367. if (intel_hid_evaluate_method(handle, INTEL_HID_DSM_HEBC_V1_FN,
  368. &event_cap)) {
  369. if (event_cap & 0x20000)
  370. return true;
  371. }
  372. if (dmi_check_system(button_array_table))
  373. return true;
  374. return false;
  375. }
  376. static int intel_hid_probe(struct platform_device *device)
  377. {
  378. acpi_handle handle = ACPI_HANDLE(&device->dev);
  379. unsigned long long mode;
  380. struct intel_hid_priv *priv;
  381. acpi_status status;
  382. int err;
  383. intel_hid_init_dsm(handle);
  384. if (!intel_hid_evaluate_method(handle, INTEL_HID_DSM_HDMM_FN, &mode)) {
  385. dev_warn(&device->dev, "failed to read mode\n");
  386. return -ENODEV;
  387. }
  388. if (mode != 0) {
  389. /*
  390. * This driver only implements "simple" mode. There appear
  391. * to be no other modes, but we should be paranoid and check
  392. * for compatibility.
  393. */
  394. dev_info(&device->dev, "platform is not in simple mode\n");
  395. return -ENODEV;
  396. }
  397. priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
  398. if (!priv)
  399. return -ENOMEM;
  400. dev_set_drvdata(&device->dev, priv);
  401. err = intel_hid_input_setup(device);
  402. if (err) {
  403. pr_err("Failed to setup Intel HID hotkeys\n");
  404. return err;
  405. }
  406. /* Setup 5 button array */
  407. if (button_array_present(device)) {
  408. dev_info(&device->dev, "platform supports 5 button array\n");
  409. err = intel_button_array_input_setup(device);
  410. if (err)
  411. pr_err("Failed to setup Intel 5 button array hotkeys\n");
  412. }
  413. status = acpi_install_notify_handler(handle,
  414. ACPI_DEVICE_NOTIFY,
  415. notify_handler,
  416. device);
  417. if (ACPI_FAILURE(status))
  418. return -EBUSY;
  419. err = intel_hid_set_enable(&device->dev, true);
  420. if (err)
  421. goto err_remove_notify;
  422. if (priv->array) {
  423. unsigned long long dummy;
  424. intel_button_array_enable(&device->dev, true);
  425. /* Call button load method to enable HID power button */
  426. if (!intel_hid_evaluate_method(handle, INTEL_HID_DSM_BTNL_FN,
  427. &dummy)) {
  428. dev_warn(&device->dev,
  429. "failed to enable HID power button\n");
  430. }
  431. }
  432. device_init_wakeup(&device->dev, true);
  433. /*
  434. * In order for system wakeup to work, the EC GPE has to be marked as
  435. * a wakeup one, so do that here (this setting will persist, but it has
  436. * no effect until the wakeup mask is set for the EC GPE).
  437. */
  438. acpi_ec_mark_gpe_for_wake();
  439. return 0;
  440. err_remove_notify:
  441. acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
  442. return err;
  443. }
  444. static int intel_hid_remove(struct platform_device *device)
  445. {
  446. acpi_handle handle = ACPI_HANDLE(&device->dev);
  447. device_init_wakeup(&device->dev, false);
  448. acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
  449. intel_hid_set_enable(&device->dev, false);
  450. intel_button_array_enable(&device->dev, false);
  451. /*
  452. * Even if we failed to shut off the event stream, we can still
  453. * safely detach from the device.
  454. */
  455. return 0;
  456. }
  457. static struct platform_driver intel_hid_pl_driver = {
  458. .driver = {
  459. .name = "intel-hid",
  460. .acpi_match_table = intel_hid_ids,
  461. .pm = &intel_hid_pl_pm_ops,
  462. },
  463. .probe = intel_hid_probe,
  464. .remove = intel_hid_remove,
  465. };
  466. /*
  467. * Unfortunately, some laptops provide a _HID="INT33D5" device with
  468. * _CID="PNP0C02". This causes the pnpacpi scan driver to claim the
  469. * ACPI node, so no platform device will be created. The pnpacpi
  470. * driver rejects this device in subsequent processing, so no physical
  471. * node is created at all.
  472. *
  473. * As a workaround until the ACPI core figures out how to handle
  474. * this corner case, manually ask the ACPI platform device code to
  475. * claim the ACPI node.
  476. */
  477. static acpi_status __init
  478. check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
  479. {
  480. const struct acpi_device_id *ids = context;
  481. struct acpi_device *dev;
  482. if (acpi_bus_get_device(handle, &dev) != 0)
  483. return AE_OK;
  484. if (acpi_match_device_ids(dev, ids) == 0)
  485. if (!IS_ERR_OR_NULL(acpi_create_platform_device(dev, NULL)))
  486. dev_info(&dev->dev,
  487. "intel-hid: created platform device\n");
  488. return AE_OK;
  489. }
  490. static int __init intel_hid_init(void)
  491. {
  492. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  493. ACPI_UINT32_MAX, check_acpi_dev, NULL,
  494. (void *)intel_hid_ids, NULL);
  495. return platform_driver_register(&intel_hid_pl_driver);
  496. }
  497. module_init(intel_hid_init);
  498. static void __exit intel_hid_exit(void)
  499. {
  500. platform_driver_unregister(&intel_hid_pl_driver);
  501. }
  502. module_exit(intel_hid_exit);