intel-vbtn.c 10 KB

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