quickstart.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ACPI Direct App Launch driver
  4. *
  5. * Copyright (C) 2024 Armin Wolf <W_Armin@gmx.de>
  6. * Copyright (C) 2022 Arvid Norlander <lkml@vorapal.se>
  7. * Copyright (C) 2007-2010 Angelo Arrifano <miknix@gmail.com>
  8. *
  9. * Information gathered from disassembled dsdt and from here:
  10. * <https://archive.org/details/microsoft-acpi-dirapplaunch>
  11. */
  12. #include <linux/acpi.h>
  13. #include <linux/device.h>
  14. #include <linux/errno.h>
  15. #include <linux/init.h>
  16. #include <linux/input.h>
  17. #include <linux/input/sparse-keymap.h>
  18. #include <linux/mod_devicetable.h>
  19. #include <linux/module.h>
  20. #include <linux/mutex.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/printk.h>
  23. #include <linux/slab.h>
  24. #include <linux/sysfs.h>
  25. #include <linux/types.h>
  26. #include <linux/unaligned.h>
  27. #define DRIVER_NAME "quickstart"
  28. /*
  29. * There will be two events:
  30. * 0x02 - Button was pressed while device was off/sleeping.
  31. * 0x80 - Button was pressed while device was up.
  32. */
  33. #define QUICKSTART_EVENT_RUNTIME 0x80
  34. struct quickstart_data {
  35. struct device *dev;
  36. struct mutex input_lock; /* Protects input sequence during notify */
  37. struct input_dev *input_device;
  38. char input_name[32];
  39. char phys[32];
  40. u32 id;
  41. };
  42. /*
  43. * Knowing what these buttons do require system specific knowledge.
  44. * This could be done by matching on DMI data in a long quirk table.
  45. * However, it is easier to leave it up to user space to figure this out.
  46. *
  47. * Using for example udev hwdb the scancode 0x1 can be remapped suitably.
  48. */
  49. static const struct key_entry quickstart_keymap[] = {
  50. { KE_KEY, 0x1, { KEY_UNKNOWN } },
  51. { KE_END, 0 },
  52. };
  53. static ssize_t button_id_show(struct device *dev, struct device_attribute *attr, char *buf)
  54. {
  55. struct quickstart_data *data = dev_get_drvdata(dev);
  56. return sysfs_emit(buf, "%u\n", data->id);
  57. }
  58. static DEVICE_ATTR_RO(button_id);
  59. static struct attribute *quickstart_attrs[] = {
  60. &dev_attr_button_id.attr,
  61. NULL
  62. };
  63. ATTRIBUTE_GROUPS(quickstart);
  64. static void quickstart_notify(acpi_handle handle, u32 event, void *context)
  65. {
  66. struct quickstart_data *data = context;
  67. switch (event) {
  68. case QUICKSTART_EVENT_RUNTIME:
  69. mutex_lock(&data->input_lock);
  70. sparse_keymap_report_event(data->input_device, 0x1, 1, true);
  71. mutex_unlock(&data->input_lock);
  72. acpi_bus_generate_netlink_event(DRIVER_NAME, dev_name(data->dev), event, 0);
  73. break;
  74. default:
  75. dev_err(data->dev, FW_INFO "Unexpected ACPI notify event (%u)\n", event);
  76. break;
  77. }
  78. }
  79. /*
  80. * The GHID ACPI method is used to indicate the "role" of the button.
  81. * However, all the meanings of these values are vendor defined.
  82. *
  83. * We do however expose this value to user space.
  84. */
  85. static int quickstart_get_ghid(struct quickstart_data *data)
  86. {
  87. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  88. acpi_handle handle = ACPI_HANDLE(data->dev);
  89. union acpi_object *obj;
  90. acpi_status status;
  91. int ret = 0;
  92. /*
  93. * This returns a buffer telling the button usage ID,
  94. * and triggers pending notify events (The ones before booting).
  95. */
  96. status = acpi_evaluate_object_typed(handle, "GHID", NULL, &buffer, ACPI_TYPE_BUFFER);
  97. if (ACPI_FAILURE(status))
  98. return -EIO;
  99. obj = buffer.pointer;
  100. if (!obj)
  101. return -ENODATA;
  102. /*
  103. * Quoting the specification:
  104. * "The GHID method can return a BYTE, WORD, or DWORD.
  105. * The value must be encoded in little-endian byte
  106. * order (least significant byte first)."
  107. */
  108. switch (obj->buffer.length) {
  109. case 1:
  110. data->id = obj->buffer.pointer[0];
  111. break;
  112. case 2:
  113. data->id = get_unaligned_le16(obj->buffer.pointer);
  114. break;
  115. case 4:
  116. data->id = get_unaligned_le32(obj->buffer.pointer);
  117. break;
  118. default:
  119. dev_err(data->dev,
  120. FW_BUG "GHID method returned buffer of unexpected length %u\n",
  121. obj->buffer.length);
  122. ret = -EIO;
  123. break;
  124. }
  125. kfree(obj);
  126. return ret;
  127. }
  128. static void quickstart_notify_remove(void *context)
  129. {
  130. struct quickstart_data *data = context;
  131. acpi_handle handle;
  132. handle = ACPI_HANDLE(data->dev);
  133. acpi_remove_notify_handler(handle, ACPI_DEVICE_NOTIFY, quickstart_notify);
  134. }
  135. static int quickstart_probe(struct platform_device *pdev)
  136. {
  137. struct quickstart_data *data;
  138. acpi_handle handle;
  139. acpi_status status;
  140. int ret;
  141. handle = ACPI_HANDLE(&pdev->dev);
  142. if (!handle)
  143. return -ENODEV;
  144. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  145. if (!data)
  146. return -ENOMEM;
  147. data->dev = &pdev->dev;
  148. dev_set_drvdata(&pdev->dev, data);
  149. ret = devm_mutex_init(&pdev->dev, &data->input_lock);
  150. if (ret < 0)
  151. return ret;
  152. /*
  153. * We have to initialize the device wakeup before evaluating GHID because
  154. * doing so will notify the device if the button was used to wake the machine
  155. * from S5.
  156. */
  157. device_init_wakeup(&pdev->dev, true);
  158. ret = quickstart_get_ghid(data);
  159. if (ret < 0)
  160. return ret;
  161. data->input_device = devm_input_allocate_device(&pdev->dev);
  162. if (!data->input_device)
  163. return -ENOMEM;
  164. ret = sparse_keymap_setup(data->input_device, quickstart_keymap, NULL);
  165. if (ret < 0)
  166. return ret;
  167. snprintf(data->input_name, sizeof(data->input_name), "Quickstart Button %u", data->id);
  168. snprintf(data->phys, sizeof(data->phys), DRIVER_NAME "/input%u", data->id);
  169. data->input_device->name = data->input_name;
  170. data->input_device->phys = data->phys;
  171. data->input_device->id.bustype = BUS_HOST;
  172. ret = input_register_device(data->input_device);
  173. if (ret < 0)
  174. return ret;
  175. status = acpi_install_notify_handler(handle, ACPI_DEVICE_NOTIFY, quickstart_notify, data);
  176. if (ACPI_FAILURE(status))
  177. return -EIO;
  178. return devm_add_action_or_reset(&pdev->dev, quickstart_notify_remove, data);
  179. }
  180. static const struct acpi_device_id quickstart_device_ids[] = {
  181. { "PNP0C32" },
  182. { }
  183. };
  184. MODULE_DEVICE_TABLE(acpi, quickstart_device_ids);
  185. static struct platform_driver quickstart_platform_driver = {
  186. .driver = {
  187. .name = DRIVER_NAME,
  188. .dev_groups = quickstart_groups,
  189. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  190. .acpi_match_table = quickstart_device_ids,
  191. },
  192. .probe = quickstart_probe,
  193. };
  194. module_platform_driver(quickstart_platform_driver);
  195. MODULE_AUTHOR("Armin Wolf <W_Armin@gmx.de>");
  196. MODULE_AUTHOR("Arvid Norlander <lkml@vorpal.se>");
  197. MODULE_AUTHOR("Angelo Arrifano");
  198. MODULE_DESCRIPTION("ACPI Direct App Launch driver");
  199. MODULE_LICENSE("GPL");