intel-hid.c 15 KB

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