intel-vbtn.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Intel Virtual Button driver for Windows 8.1+
  3. *
  4. * Copyright (C) 2016 AceLan Kao <acelan.kao@canonical.com>
  5. * Copyright (C) 2016 Alex Hung <alex.hung@canonical.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/input.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/input/sparse-keymap.h>
  24. #include <linux/acpi.h>
  25. #include <linux/suspend.h>
  26. #include <acpi/acpi_bus.h>
  27. MODULE_LICENSE("GPL");
  28. MODULE_AUTHOR("AceLan Kao");
  29. static const struct acpi_device_id intel_vbtn_ids[] = {
  30. {"INT33D6", 0},
  31. {"", 0},
  32. };
  33. /* In theory, these are HID usages. */
  34. static const struct key_entry intel_vbtn_keymap[] = {
  35. { KE_KEY, 0xC0, { KEY_POWER } }, /* power key press */
  36. { KE_IGNORE, 0xC1, { KEY_POWER } }, /* power key release */
  37. { KE_KEY, 0xC4, { KEY_VOLUMEUP } }, /* volume-up key press */
  38. { KE_IGNORE, 0xC5, { KEY_VOLUMEUP } }, /* volume-up key release */
  39. { KE_KEY, 0xC6, { KEY_VOLUMEDOWN } }, /* volume-down key press */
  40. { KE_IGNORE, 0xC7, { KEY_VOLUMEDOWN } }, /* volume-down key release */
  41. { KE_END },
  42. };
  43. struct intel_vbtn_priv {
  44. struct input_dev *input_dev;
  45. bool wakeup_mode;
  46. };
  47. static int intel_vbtn_input_setup(struct platform_device *device)
  48. {
  49. struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
  50. int ret;
  51. priv->input_dev = devm_input_allocate_device(&device->dev);
  52. if (!priv->input_dev)
  53. return -ENOMEM;
  54. ret = sparse_keymap_setup(priv->input_dev, intel_vbtn_keymap, NULL);
  55. if (ret)
  56. return ret;
  57. priv->input_dev->dev.parent = &device->dev;
  58. priv->input_dev->name = "Intel Virtual Button driver";
  59. priv->input_dev->id.bustype = BUS_HOST;
  60. return input_register_device(priv->input_dev);
  61. }
  62. static void notify_handler(acpi_handle handle, u32 event, void *context)
  63. {
  64. struct platform_device *device = context;
  65. struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
  66. if (priv->wakeup_mode) {
  67. if (sparse_keymap_entry_from_scancode(priv->input_dev, event)) {
  68. pm_wakeup_hard_event(&device->dev);
  69. return;
  70. }
  71. } else if (sparse_keymap_report_event(priv->input_dev, event, 1, true)) {
  72. return;
  73. }
  74. dev_dbg(&device->dev, "unknown event index 0x%x\n", event);
  75. }
  76. static int intel_vbtn_probe(struct platform_device *device)
  77. {
  78. acpi_handle handle = ACPI_HANDLE(&device->dev);
  79. struct intel_vbtn_priv *priv;
  80. acpi_status status;
  81. int err;
  82. status = acpi_evaluate_object(handle, "VBDL", NULL, NULL);
  83. if (ACPI_FAILURE(status)) {
  84. dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n");
  85. return -ENODEV;
  86. }
  87. priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
  88. if (!priv)
  89. return -ENOMEM;
  90. dev_set_drvdata(&device->dev, priv);
  91. err = intel_vbtn_input_setup(device);
  92. if (err) {
  93. pr_err("Failed to setup Intel Virtual Button\n");
  94. return err;
  95. }
  96. status = acpi_install_notify_handler(handle,
  97. ACPI_DEVICE_NOTIFY,
  98. notify_handler,
  99. device);
  100. if (ACPI_FAILURE(status))
  101. return -EBUSY;
  102. device_init_wakeup(&device->dev, true);
  103. return 0;
  104. }
  105. static int intel_vbtn_remove(struct platform_device *device)
  106. {
  107. acpi_handle handle = ACPI_HANDLE(&device->dev);
  108. acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
  109. /*
  110. * Even if we failed to shut off the event stream, we can still
  111. * safely detach from the device.
  112. */
  113. return 0;
  114. }
  115. static int intel_vbtn_pm_prepare(struct device *dev)
  116. {
  117. struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
  118. priv->wakeup_mode = true;
  119. return 0;
  120. }
  121. static int intel_vbtn_pm_resume(struct device *dev)
  122. {
  123. struct intel_vbtn_priv *priv = dev_get_drvdata(dev);
  124. priv->wakeup_mode = false;
  125. return 0;
  126. }
  127. static const struct dev_pm_ops intel_vbtn_pm_ops = {
  128. .prepare = intel_vbtn_pm_prepare,
  129. .resume = intel_vbtn_pm_resume,
  130. .restore = intel_vbtn_pm_resume,
  131. .thaw = intel_vbtn_pm_resume,
  132. };
  133. static struct platform_driver intel_vbtn_pl_driver = {
  134. .driver = {
  135. .name = "intel-vbtn",
  136. .acpi_match_table = intel_vbtn_ids,
  137. .pm = &intel_vbtn_pm_ops,
  138. },
  139. .probe = intel_vbtn_probe,
  140. .remove = intel_vbtn_remove,
  141. };
  142. MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids);
  143. static acpi_status __init
  144. check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
  145. {
  146. const struct acpi_device_id *ids = context;
  147. struct acpi_device *dev;
  148. if (acpi_bus_get_device(handle, &dev) != 0)
  149. return AE_OK;
  150. if (acpi_match_device_ids(dev, ids) == 0)
  151. if (acpi_create_platform_device(dev, NULL))
  152. dev_info(&dev->dev,
  153. "intel-vbtn: created platform device\n");
  154. return AE_OK;
  155. }
  156. static int __init intel_vbtn_init(void)
  157. {
  158. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  159. ACPI_UINT32_MAX, check_acpi_dev, NULL,
  160. (void *)intel_vbtn_ids, NULL);
  161. return platform_driver_register(&intel_vbtn_pl_driver);
  162. }
  163. module_init(intel_vbtn_init);
  164. static void __exit intel_vbtn_exit(void)
  165. {
  166. platform_driver_unregister(&intel_vbtn_pl_driver);
  167. }
  168. module_exit(intel_vbtn_exit);