intel-vbtn.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Intel Virtual Button driver for Windows 8.1+
  4. *
  5. * Copyright (C) 2016 AceLan Kao <acelan.kao@canonical.com>
  6. * Copyright (C) 2016 Alex Hung <alex.hung@canonical.com>
  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. /* Returned when NOT in tablet mode on some HP Stream x360 11 models */
  17. #define VGBS_TABLET_MODE_FLAG_ALT 0x10
  18. /* When NOT in tablet mode, VGBS returns with the flag 0x40 */
  19. #define VGBS_TABLET_MODE_FLAG 0x40
  20. #define VGBS_DOCK_MODE_FLAG 0x80
  21. #define VGBS_TABLET_MODE_FLAGS (VGBS_TABLET_MODE_FLAG | VGBS_TABLET_MODE_FLAG_ALT)
  22. MODULE_LICENSE("GPL");
  23. MODULE_AUTHOR("AceLan Kao");
  24. static const struct acpi_device_id intel_vbtn_ids[] = {
  25. {"INT33D6", 0},
  26. {"", 0},
  27. };
  28. MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids);
  29. /* In theory, these are HID usages. */
  30. static const struct key_entry intel_vbtn_keymap[] = {
  31. { KE_KEY, 0xC0, { KEY_POWER } }, /* power key press */
  32. { KE_IGNORE, 0xC1, { KEY_POWER } }, /* power key release */
  33. { KE_KEY, 0xC2, { KEY_LEFTMETA } }, /* 'Windows' key press */
  34. { KE_KEY, 0xC3, { KEY_LEFTMETA } }, /* 'Windows' key release */
  35. { KE_KEY, 0xC4, { KEY_VOLUMEUP } }, /* volume-up key press */
  36. { KE_IGNORE, 0xC5, { KEY_VOLUMEUP } }, /* volume-up key release */
  37. { KE_KEY, 0xC6, { KEY_VOLUMEDOWN } }, /* volume-down key press */
  38. { KE_IGNORE, 0xC7, { KEY_VOLUMEDOWN } }, /* volume-down key release */
  39. { KE_KEY, 0xC8, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key press */
  40. { KE_KEY, 0xC9, { KEY_ROTATE_LOCK_TOGGLE } }, /* rotate-lock key release */
  41. };
  42. static const struct key_entry intel_vbtn_switchmap[] = {
  43. /*
  44. * SW_DOCK should only be reported for docking stations, but DSDTs using the
  45. * intel-vbtn code, always seem to use this for 2-in-1s / convertibles and set
  46. * SW_DOCK=1 when in laptop-mode (in tandem with setting SW_TABLET_MODE=0).
  47. * This causes userspace to think the laptop is docked to a port-replicator
  48. * and to disable suspend-on-lid-close, which is undesirable.
  49. * Map the dock events to KEY_IGNORE to avoid this broken SW_DOCK reporting.
  50. */
  51. { KE_IGNORE, 0xCA, { .sw = { SW_DOCK, 1 } } }, /* Docked */
  52. { KE_IGNORE, 0xCB, { .sw = { SW_DOCK, 0 } } }, /* Undocked */
  53. { KE_SW, 0xCC, { .sw = { SW_TABLET_MODE, 1 } } }, /* Tablet */
  54. { KE_SW, 0xCD, { .sw = { SW_TABLET_MODE, 0 } } }, /* Laptop */
  55. };
  56. #define KEYMAP_LEN \
  57. (ARRAY_SIZE(intel_vbtn_keymap) + ARRAY_SIZE(intel_vbtn_switchmap) + 1)
  58. struct intel_vbtn_priv {
  59. struct key_entry keymap[KEYMAP_LEN];
  60. struct input_dev *input_dev;
  61. bool has_buttons;
  62. bool has_switches;
  63. bool wakeup_mode;
  64. };
  65. static void detect_tablet_mode(struct platform_device *device)
  66. {
  67. struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
  68. acpi_handle handle = ACPI_HANDLE(&device->dev);
  69. unsigned long long vgbs;
  70. acpi_status status;
  71. int m;
  72. status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
  73. if (ACPI_FAILURE(status))
  74. return;
  75. m = !(vgbs & VGBS_TABLET_MODE_FLAGS);
  76. input_report_switch(priv->input_dev, SW_TABLET_MODE, m);
  77. m = (vgbs & VGBS_DOCK_MODE_FLAG) ? 1 : 0;
  78. input_report_switch(priv->input_dev, SW_DOCK, m);
  79. }
  80. static int intel_vbtn_input_setup(struct platform_device *device)
  81. {
  82. struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
  83. int ret, keymap_len = 0;
  84. if (priv->has_buttons) {
  85. memcpy(&priv->keymap[keymap_len], intel_vbtn_keymap,
  86. ARRAY_SIZE(intel_vbtn_keymap) *
  87. sizeof(struct key_entry));
  88. keymap_len += ARRAY_SIZE(intel_vbtn_keymap);
  89. }
  90. if (priv->has_switches) {
  91. memcpy(&priv->keymap[keymap_len], intel_vbtn_switchmap,
  92. ARRAY_SIZE(intel_vbtn_switchmap) *
  93. sizeof(struct key_entry));
  94. keymap_len += ARRAY_SIZE(intel_vbtn_switchmap);
  95. }
  96. priv->keymap[keymap_len].type = KE_END;
  97. priv->input_dev = devm_input_allocate_device(&device->dev);
  98. if (!priv->input_dev)
  99. return -ENOMEM;
  100. ret = sparse_keymap_setup(priv->input_dev, priv->keymap, NULL);
  101. if (ret)
  102. return ret;
  103. priv->input_dev->dev.parent = &device->dev;
  104. priv->input_dev->name = "Intel Virtual Button driver";
  105. priv->input_dev->id.bustype = BUS_HOST;
  106. if (priv->has_switches)
  107. detect_tablet_mode(device);
  108. return input_register_device(priv->input_dev);
  109. }
  110. static void notify_handler(acpi_handle handle, u32 event, void *context)
  111. {
  112. struct platform_device *device = context;
  113. struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
  114. unsigned int val = !(event & 1); /* Even=press, Odd=release */
  115. const struct key_entry *ke, *ke_rel;
  116. bool autorelease;
  117. if (priv->wakeup_mode) {
  118. ke = sparse_keymap_entry_from_scancode(priv->input_dev, event);
  119. if (ke) {
  120. pm_wakeup_hard_event(&device->dev);
  121. /*
  122. * Switch events like tablet mode will wake the device
  123. * and report the new switch position to the input
  124. * subsystem.
  125. */
  126. if (ke->type == KE_SW)
  127. sparse_keymap_report_event(priv->input_dev,
  128. event,
  129. val,
  130. 0);
  131. return;
  132. }
  133. goto out_unknown;
  134. }
  135. /*
  136. * Even press events are autorelease if there is no corresponding odd
  137. * release event, or if the odd event is KE_IGNORE.
  138. */
  139. ke_rel = sparse_keymap_entry_from_scancode(priv->input_dev, event | 1);
  140. autorelease = val && (!ke_rel || ke_rel->type == KE_IGNORE);
  141. if (sparse_keymap_report_event(priv->input_dev, event, val, autorelease))
  142. return;
  143. out_unknown:
  144. dev_dbg(&device->dev, "unknown event index 0x%x\n", event);
  145. }
  146. static bool intel_vbtn_has_buttons(acpi_handle handle)
  147. {
  148. acpi_status status;
  149. status = acpi_evaluate_object(handle, "VBDL", NULL, NULL);
  150. return ACPI_SUCCESS(status);
  151. }
  152. /*
  153. * There are several laptops (non 2-in-1) models out there which support VGBS,
  154. * but simply always return 0, which we translate to SW_TABLET_MODE=1. This in
  155. * turn causes userspace (libinput) to suppress events from the builtin
  156. * keyboard and touchpad, making the laptop essentially unusable.
  157. *
  158. * Since the problem of wrongly reporting SW_TABLET_MODE=1 in combination
  159. * with libinput, leads to a non-usable system. Where as OTOH many people will
  160. * not even notice when SW_TABLET_MODE is not being reported, a DMI based allow
  161. * list is used here. This list mainly matches on the chassis-type of 2-in-1s.
  162. *
  163. * There are also some 2-in-1s which use the intel-vbtn ACPI interface to report
  164. * SW_TABLET_MODE with a chassis-type of 8 ("Portable") or 10 ("Notebook"),
  165. * these are matched on a per model basis, since many normal laptops with a
  166. * possible broken VGBS ACPI-method also use these chassis-types.
  167. */
  168. static const struct dmi_system_id dmi_switches_allow_list[] = {
  169. {
  170. .matches = {
  171. DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "31" /* Convertible */),
  172. },
  173. },
  174. {
  175. .matches = {
  176. DMI_EXACT_MATCH(DMI_CHASSIS_TYPE, "32" /* Detachable */),
  177. },
  178. },
  179. {
  180. .matches = {
  181. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  182. DMI_MATCH(DMI_PRODUCT_NAME, "Venue 11 Pro 7130"),
  183. },
  184. },
  185. {
  186. .matches = {
  187. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  188. DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion 13 x360 PC"),
  189. },
  190. },
  191. {
  192. .matches = {
  193. DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
  194. DMI_MATCH(DMI_PRODUCT_NAME, "Switch SA5-271"),
  195. },
  196. },
  197. {
  198. .matches = {
  199. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  200. DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 7352"),
  201. },
  202. },
  203. {} /* Array terminator */
  204. };
  205. static bool intel_vbtn_has_switches(acpi_handle handle)
  206. {
  207. unsigned long long vgbs;
  208. acpi_status status;
  209. if (!dmi_check_system(dmi_switches_allow_list))
  210. return false;
  211. status = acpi_evaluate_integer(handle, "VGBS", NULL, &vgbs);
  212. return ACPI_SUCCESS(status);
  213. }
  214. static int intel_vbtn_probe(struct platform_device *device)
  215. {
  216. acpi_handle handle = ACPI_HANDLE(&device->dev);
  217. bool has_buttons, has_switches;
  218. struct intel_vbtn_priv *priv;
  219. acpi_status status;
  220. int err;
  221. has_buttons = intel_vbtn_has_buttons(handle);
  222. has_switches = intel_vbtn_has_switches(handle);
  223. if (!has_buttons && !has_switches) {
  224. dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n");
  225. return -ENODEV;
  226. }
  227. priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
  228. if (!priv)
  229. return -ENOMEM;
  230. dev_set_drvdata(&device->dev, priv);
  231. priv->has_buttons = has_buttons;
  232. priv->has_switches = has_switches;
  233. err = intel_vbtn_input_setup(device);
  234. if (err) {
  235. pr_err("Failed to setup Intel Virtual Button\n");
  236. return err;
  237. }
  238. status = acpi_install_notify_handler(handle,
  239. ACPI_DEVICE_NOTIFY,
  240. notify_handler,
  241. device);
  242. if (ACPI_FAILURE(status))
  243. return -EBUSY;
  244. device_init_wakeup(&device->dev, true);
  245. /*
  246. * In order for system wakeup to work, the EC GPE has to be marked as
  247. * a wakeup one, so do that here (this setting will persist, but it has
  248. * no effect until the wakeup mask is set for the EC GPE).
  249. */
  250. acpi_ec_mark_gpe_for_wake();
  251. return 0;
  252. }
  253. static int intel_vbtn_remove(struct platform_device *device)
  254. {
  255. acpi_handle handle = ACPI_HANDLE(&device->dev);
  256. device_init_wakeup(&device->dev, false);
  257. acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
  258. /*
  259. * Even if we failed to shut off the event stream, we can still
  260. * safely detach from the device.
  261. */
  262. return 0;
  263. }
  264. static int intel_vbtn_pm_prepare(struct device *dev)
  265. {
  266. if (device_may_wakeup(dev)) {
  267. struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
  268. priv->wakeup_mode = true;
  269. }
  270. return 0;
  271. }
  272. static void intel_vbtn_pm_complete(struct device *dev)
  273. {
  274. struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
  275. priv->wakeup_mode = false;
  276. }
  277. static int intel_vbtn_pm_resume(struct device *dev)
  278. {
  279. intel_vbtn_pm_complete(dev);
  280. return 0;
  281. }
  282. static const struct dev_pm_ops intel_vbtn_pm_ops = {
  283. .prepare = intel_vbtn_pm_prepare,
  284. .complete = intel_vbtn_pm_complete,
  285. .resume = intel_vbtn_pm_resume,
  286. .restore = intel_vbtn_pm_resume,
  287. .thaw = intel_vbtn_pm_resume,
  288. };
  289. static struct platform_driver intel_vbtn_pl_driver = {
  290. .driver = {
  291. .name = "intel-vbtn",
  292. .acpi_match_table = intel_vbtn_ids,
  293. .pm = &intel_vbtn_pm_ops,
  294. },
  295. .probe = intel_vbtn_probe,
  296. .remove = intel_vbtn_remove,
  297. };
  298. static acpi_status __init
  299. check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
  300. {
  301. const struct acpi_device_id *ids = context;
  302. struct acpi_device *dev;
  303. if (acpi_bus_get_device(handle, &dev) != 0)
  304. return AE_OK;
  305. if (acpi_match_device_ids(dev, ids) == 0)
  306. if (!IS_ERR_OR_NULL(acpi_create_platform_device(dev, NULL)))
  307. dev_info(&dev->dev,
  308. "intel-vbtn: created platform device\n");
  309. return AE_OK;
  310. }
  311. static int __init intel_vbtn_init(void)
  312. {
  313. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  314. ACPI_UINT32_MAX, check_acpi_dev, NULL,
  315. (void *)intel_vbtn_ids, NULL);
  316. return platform_driver_register(&intel_vbtn_pl_driver);
  317. }
  318. module_init(intel_vbtn_init);
  319. static void __exit intel_vbtn_exit(void)
  320. {
  321. platform_driver_unregister(&intel_vbtn_pl_driver);
  322. }
  323. module_exit(intel_vbtn_exit);