hid.c 21 KB

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