intel-vbtn.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 <acpi/acpi_bus.h>
  26. MODULE_LICENSE("GPL");
  27. MODULE_AUTHOR("AceLan Kao");
  28. static const struct acpi_device_id intel_vbtn_ids[] = {
  29. {"INT33D6", 0},
  30. {"", 0},
  31. };
  32. /* In theory, these are HID usages. */
  33. static const struct key_entry intel_vbtn_keymap[] = {
  34. { KE_IGNORE, 0xC0, { KEY_POWER } }, /* power key press */
  35. { KE_KEY, 0xC1, { KEY_POWER } }, /* power key release */
  36. { KE_KEY, 0xC4, { KEY_VOLUMEUP } }, /* volume-up key press */
  37. { KE_IGNORE, 0xC5, { KEY_VOLUMEUP } }, /* volume-up key release */
  38. { KE_KEY, 0xC6, { KEY_VOLUMEDOWN } }, /* volume-down key press */
  39. { KE_IGNORE, 0xC7, { KEY_VOLUMEDOWN } }, /* volume-down key release */
  40. { KE_END },
  41. };
  42. struct intel_vbtn_priv {
  43. struct input_dev *input_dev;
  44. };
  45. static int intel_vbtn_input_setup(struct platform_device *device)
  46. {
  47. struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
  48. int ret;
  49. priv->input_dev = devm_input_allocate_device(&device->dev);
  50. if (!priv->input_dev)
  51. return -ENOMEM;
  52. ret = sparse_keymap_setup(priv->input_dev, intel_vbtn_keymap, NULL);
  53. if (ret)
  54. return ret;
  55. priv->input_dev->dev.parent = &device->dev;
  56. priv->input_dev->name = "Intel Virtual Button driver";
  57. priv->input_dev->id.bustype = BUS_HOST;
  58. return input_register_device(priv->input_dev);
  59. }
  60. static void notify_handler(acpi_handle handle, u32 event, void *context)
  61. {
  62. struct platform_device *device = context;
  63. struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
  64. if (!sparse_keymap_report_event(priv->input_dev, event, 1, true))
  65. dev_info(&device->dev, "unknown event index 0x%x\n",
  66. event);
  67. }
  68. static int intel_vbtn_probe(struct platform_device *device)
  69. {
  70. acpi_handle handle = ACPI_HANDLE(&device->dev);
  71. struct intel_vbtn_priv *priv;
  72. acpi_status status;
  73. int err;
  74. status = acpi_evaluate_object(handle, "VBDL", NULL, NULL);
  75. if (ACPI_FAILURE(status)) {
  76. dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n");
  77. return -ENODEV;
  78. }
  79. priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
  80. if (!priv)
  81. return -ENOMEM;
  82. dev_set_drvdata(&device->dev, priv);
  83. err = intel_vbtn_input_setup(device);
  84. if (err) {
  85. pr_err("Failed to setup Intel Virtual Button\n");
  86. return err;
  87. }
  88. status = acpi_install_notify_handler(handle,
  89. ACPI_DEVICE_NOTIFY,
  90. notify_handler,
  91. device);
  92. if (ACPI_FAILURE(status))
  93. return -EBUSY;
  94. return 0;
  95. }
  96. static int intel_vbtn_remove(struct platform_device *device)
  97. {
  98. acpi_handle handle = ACPI_HANDLE(&device->dev);
  99. acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
  100. /*
  101. * Even if we failed to shut off the event stream, we can still
  102. * safely detach from the device.
  103. */
  104. return 0;
  105. }
  106. static struct platform_driver intel_vbtn_pl_driver = {
  107. .driver = {
  108. .name = "intel-vbtn",
  109. .acpi_match_table = intel_vbtn_ids,
  110. },
  111. .probe = intel_vbtn_probe,
  112. .remove = intel_vbtn_remove,
  113. };
  114. MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids);
  115. static acpi_status __init
  116. check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
  117. {
  118. const struct acpi_device_id *ids = context;
  119. struct acpi_device *dev;
  120. if (acpi_bus_get_device(handle, &dev) != 0)
  121. return AE_OK;
  122. if (acpi_match_device_ids(dev, ids) == 0)
  123. if (acpi_create_platform_device(dev, NULL))
  124. dev_info(&dev->dev,
  125. "intel-vbtn: created platform device\n");
  126. return AE_OK;
  127. }
  128. static int __init intel_vbtn_init(void)
  129. {
  130. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  131. ACPI_UINT32_MAX, check_acpi_dev, NULL,
  132. (void *)intel_vbtn_ids, NULL);
  133. return platform_driver_register(&intel_vbtn_pl_driver);
  134. }
  135. module_init(intel_vbtn_init);
  136. static void __exit intel_vbtn_exit(void)
  137. {
  138. platform_driver_unregister(&intel_vbtn_pl_driver);
  139. }
  140. module_exit(intel_vbtn_exit);