intel-vbtn.c 4.7 KB

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