intel-vbtn.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 = input_allocate_device();
  50. if (!priv->input_dev)
  51. return -ENOMEM;
  52. ret = sparse_keymap_setup(priv->input_dev, intel_vbtn_keymap, NULL);
  53. if (ret)
  54. goto err_free_device;
  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. ret = input_register_device(priv->input_dev);
  59. if (ret)
  60. goto err_free_device;
  61. return 0;
  62. err_free_device:
  63. input_free_device(priv->input_dev);
  64. return ret;
  65. }
  66. static void intel_vbtn_input_destroy(struct platform_device *device)
  67. {
  68. struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
  69. input_unregister_device(priv->input_dev);
  70. }
  71. static void notify_handler(acpi_handle handle, u32 event, void *context)
  72. {
  73. struct platform_device *device = context;
  74. struct intel_vbtn_priv *priv = dev_get_drvdata(&device->dev);
  75. if (!sparse_keymap_report_event(priv->input_dev, event, 1, true))
  76. dev_info(&device->dev, "unknown event index 0x%x\n",
  77. event);
  78. }
  79. static int intel_vbtn_probe(struct platform_device *device)
  80. {
  81. acpi_handle handle = ACPI_HANDLE(&device->dev);
  82. struct intel_vbtn_priv *priv;
  83. acpi_status status;
  84. int err;
  85. status = acpi_evaluate_object(handle, "VBDL", NULL, NULL);
  86. if (!ACPI_SUCCESS(status)) {
  87. dev_warn(&device->dev, "failed to read Intel Virtual Button driver\n");
  88. return -ENODEV;
  89. }
  90. priv = devm_kzalloc(&device->dev, sizeof(*priv), GFP_KERNEL);
  91. if (!priv)
  92. return -ENOMEM;
  93. dev_set_drvdata(&device->dev, priv);
  94. err = intel_vbtn_input_setup(device);
  95. if (err) {
  96. pr_err("Failed to setup Intel Virtual Button\n");
  97. return err;
  98. }
  99. status = acpi_install_notify_handler(handle,
  100. ACPI_DEVICE_NOTIFY,
  101. notify_handler,
  102. device);
  103. if (ACPI_FAILURE(status)) {
  104. err = -EBUSY;
  105. goto err_remove_input;
  106. }
  107. return 0;
  108. err_remove_input:
  109. intel_vbtn_input_destroy(device);
  110. return err;
  111. }
  112. static int intel_vbtn_remove(struct platform_device *device)
  113. {
  114. acpi_handle handle = ACPI_HANDLE(&device->dev);
  115. intel_vbtn_input_destroy(device);
  116. acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, notify_handler);
  117. /*
  118. * Even if we failed to shut off the event stream, we can still
  119. * safely detach from the device.
  120. */
  121. return 0;
  122. }
  123. static struct platform_driver intel_vbtn_pl_driver = {
  124. .driver = {
  125. .name = "intel-vbtn",
  126. .acpi_match_table = intel_vbtn_ids,
  127. },
  128. .probe = intel_vbtn_probe,
  129. .remove = intel_vbtn_remove,
  130. };
  131. MODULE_DEVICE_TABLE(acpi, intel_vbtn_ids);
  132. static acpi_status __init
  133. check_acpi_dev(acpi_handle handle, u32 lvl, void *context, void **rv)
  134. {
  135. const struct acpi_device_id *ids = context;
  136. struct acpi_device *dev;
  137. if (acpi_bus_get_device(handle, &dev) != 0)
  138. return AE_OK;
  139. if (acpi_match_device_ids(dev, ids) == 0)
  140. if (acpi_create_platform_device(dev, NULL))
  141. dev_info(&dev->dev,
  142. "intel-vbtn: created platform device\n");
  143. return AE_OK;
  144. }
  145. static int __init intel_vbtn_init(void)
  146. {
  147. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  148. ACPI_UINT32_MAX, check_acpi_dev, NULL,
  149. (void *)intel_vbtn_ids, NULL);
  150. return platform_driver_register(&intel_vbtn_pl_driver);
  151. }
  152. module_init(intel_vbtn_init);
  153. static void __exit intel_vbtn_exit(void)
  154. {
  155. platform_driver_unregister(&intel_vbtn_pl_driver);
  156. }
  157. module_exit(intel_vbtn_exit);