surfacepro3_button.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * power/home/volume button support for
  4. * Microsoft Surface Pro 3/4 tablet.
  5. *
  6. * Copyright (c) 2015 Intel Corporation.
  7. * All rights reserved.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/types.h>
  13. #include <linux/input.h>
  14. #include <linux/acpi.h>
  15. #include <acpi/button.h>
  16. #define SURFACE_PRO3_BUTTON_HID "MSHW0028"
  17. #define SURFACE_PRO4_BUTTON_HID "MSHW0040"
  18. #define SURFACE_BUTTON_OBJ_NAME "VGBI"
  19. #define SURFACE_BUTTON_DEVICE_NAME "Surface Pro 3/4 Buttons"
  20. #define MSHW0040_DSM_REVISION 0x01
  21. #define MSHW0040_DSM_GET_OMPR 0x02 // get OEM Platform Revision
  22. static const guid_t MSHW0040_DSM_UUID =
  23. GUID_INIT(0x6fd05c69, 0xcde3, 0x49f4, 0x95, 0xed, 0xab, 0x16, 0x65,
  24. 0x49, 0x80, 0x35);
  25. #define SURFACE_BUTTON_NOTIFY_TABLET_MODE 0xc8
  26. #define SURFACE_BUTTON_NOTIFY_PRESS_POWER 0xc6
  27. #define SURFACE_BUTTON_NOTIFY_RELEASE_POWER 0xc7
  28. #define SURFACE_BUTTON_NOTIFY_PRESS_HOME 0xc4
  29. #define SURFACE_BUTTON_NOTIFY_RELEASE_HOME 0xc5
  30. #define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_UP 0xc0
  31. #define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_UP 0xc1
  32. #define SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_DOWN 0xc2
  33. #define SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_DOWN 0xc3
  34. ACPI_MODULE_NAME("surface pro 3 button");
  35. MODULE_AUTHOR("Chen Yu");
  36. MODULE_DESCRIPTION("Surface Pro3 Button Driver");
  37. MODULE_LICENSE("GPL v2");
  38. /*
  39. * Power button, Home button, Volume buttons support is supposed to
  40. * be covered by drivers/input/misc/soc_button_array.c, which is implemented
  41. * according to "Windows ACPI Design Guide for SoC Platforms".
  42. * However surface pro3 seems not to obey the specs, instead it uses
  43. * device VGBI(MSHW0028) for dispatching the events.
  44. * We choose acpi_driver rather than platform_driver/i2c_driver because
  45. * although VGBI has an i2c resource connected to i2c controller, it
  46. * is not embedded in any i2c controller's scope, thus neither platform_device
  47. * will be created, nor i2c_client will be enumerated, we have to use
  48. * acpi_driver.
  49. */
  50. static const struct acpi_device_id surface_button_device_ids[] = {
  51. {SURFACE_PRO3_BUTTON_HID, 0},
  52. {SURFACE_PRO4_BUTTON_HID, 0},
  53. {"", 0},
  54. };
  55. MODULE_DEVICE_TABLE(acpi, surface_button_device_ids);
  56. struct surface_button {
  57. unsigned int type;
  58. struct input_dev *input;
  59. char phys[32]; /* for input device */
  60. unsigned long pushed;
  61. bool suspended;
  62. };
  63. static void surface_button_notify(struct acpi_device *device, u32 event)
  64. {
  65. struct surface_button *button = acpi_driver_data(device);
  66. struct input_dev *input;
  67. int key_code = KEY_RESERVED;
  68. bool pressed = false;
  69. switch (event) {
  70. /* Power button press,release handle */
  71. case SURFACE_BUTTON_NOTIFY_PRESS_POWER:
  72. pressed = true;
  73. fallthrough;
  74. case SURFACE_BUTTON_NOTIFY_RELEASE_POWER:
  75. key_code = KEY_POWER;
  76. break;
  77. /* Home button press,release handle */
  78. case SURFACE_BUTTON_NOTIFY_PRESS_HOME:
  79. pressed = true;
  80. fallthrough;
  81. case SURFACE_BUTTON_NOTIFY_RELEASE_HOME:
  82. key_code = KEY_LEFTMETA;
  83. break;
  84. /* Volume up button press,release handle */
  85. case SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_UP:
  86. pressed = true;
  87. fallthrough;
  88. case SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_UP:
  89. key_code = KEY_VOLUMEUP;
  90. break;
  91. /* Volume down button press,release handle */
  92. case SURFACE_BUTTON_NOTIFY_PRESS_VOLUME_DOWN:
  93. pressed = true;
  94. fallthrough;
  95. case SURFACE_BUTTON_NOTIFY_RELEASE_VOLUME_DOWN:
  96. key_code = KEY_VOLUMEDOWN;
  97. break;
  98. case SURFACE_BUTTON_NOTIFY_TABLET_MODE:
  99. dev_warn_once(&device->dev, "Tablet mode is not supported\n");
  100. break;
  101. default:
  102. dev_info_ratelimited(&device->dev,
  103. "Unsupported event [0x%x]\n", event);
  104. break;
  105. }
  106. input = button->input;
  107. if (key_code == KEY_RESERVED)
  108. return;
  109. if (pressed)
  110. pm_wakeup_dev_event(&device->dev, 0, button->suspended);
  111. if (button->suspended)
  112. return;
  113. input_report_key(input, key_code, pressed?1:0);
  114. input_sync(input);
  115. }
  116. #ifdef CONFIG_PM_SLEEP
  117. static int surface_button_suspend(struct device *dev)
  118. {
  119. struct acpi_device *device = to_acpi_device(dev);
  120. struct surface_button *button = acpi_driver_data(device);
  121. button->suspended = true;
  122. return 0;
  123. }
  124. static int surface_button_resume(struct device *dev)
  125. {
  126. struct acpi_device *device = to_acpi_device(dev);
  127. struct surface_button *button = acpi_driver_data(device);
  128. button->suspended = false;
  129. return 0;
  130. }
  131. #endif
  132. /*
  133. * Surface Pro 4 and Surface Book 2 / Surface Pro 2017 use the same device
  134. * ID (MSHW0040) for the power/volume buttons. Make sure this is the right
  135. * device by checking for the _DSM method and OEM Platform Revision.
  136. *
  137. * Returns true if the driver should bind to this device, i.e. the device is
  138. * either MSWH0028 (Pro 3) or MSHW0040 on a Pro 4 or Book 1.
  139. */
  140. static bool surface_button_check_MSHW0040(struct acpi_device *dev)
  141. {
  142. acpi_handle handle = dev->handle;
  143. union acpi_object *result;
  144. u64 oem_platform_rev = 0; // valid revisions are nonzero
  145. // get OEM platform revision
  146. result = acpi_evaluate_dsm_typed(handle, &MSHW0040_DSM_UUID,
  147. MSHW0040_DSM_REVISION,
  148. MSHW0040_DSM_GET_OMPR,
  149. NULL, ACPI_TYPE_INTEGER);
  150. /*
  151. * If evaluating the _DSM fails, the method is not present. This means
  152. * that we have either MSHW0028 or MSHW0040 on Pro 4 or Book 1, so we
  153. * should use this driver. We use revision 0 indicating it is
  154. * unavailable.
  155. */
  156. if (result) {
  157. oem_platform_rev = result->integer.value;
  158. ACPI_FREE(result);
  159. }
  160. dev_dbg(&dev->dev, "OEM Platform Revision %llu\n", oem_platform_rev);
  161. return oem_platform_rev == 0;
  162. }
  163. static int surface_button_add(struct acpi_device *device)
  164. {
  165. struct surface_button *button;
  166. struct input_dev *input;
  167. const char *hid = acpi_device_hid(device);
  168. char *name;
  169. int error;
  170. if (strncmp(acpi_device_bid(device), SURFACE_BUTTON_OBJ_NAME,
  171. strlen(SURFACE_BUTTON_OBJ_NAME)))
  172. return -ENODEV;
  173. if (!surface_button_check_MSHW0040(device))
  174. return -ENODEV;
  175. button = kzalloc(sizeof(struct surface_button), GFP_KERNEL);
  176. if (!button)
  177. return -ENOMEM;
  178. device->driver_data = button;
  179. button->input = input = input_allocate_device();
  180. if (!input) {
  181. error = -ENOMEM;
  182. goto err_free_button;
  183. }
  184. name = acpi_device_name(device);
  185. strcpy(name, SURFACE_BUTTON_DEVICE_NAME);
  186. snprintf(button->phys, sizeof(button->phys), "%s/buttons", hid);
  187. input->name = name;
  188. input->phys = button->phys;
  189. input->id.bustype = BUS_HOST;
  190. input->dev.parent = &device->dev;
  191. input_set_capability(input, EV_KEY, KEY_POWER);
  192. input_set_capability(input, EV_KEY, KEY_LEFTMETA);
  193. input_set_capability(input, EV_KEY, KEY_VOLUMEUP);
  194. input_set_capability(input, EV_KEY, KEY_VOLUMEDOWN);
  195. error = input_register_device(input);
  196. if (error)
  197. goto err_free_input;
  198. device_init_wakeup(&device->dev, true);
  199. dev_info(&device->dev,
  200. "%s [%s]\n", name, acpi_device_bid(device));
  201. return 0;
  202. err_free_input:
  203. input_free_device(input);
  204. err_free_button:
  205. kfree(button);
  206. return error;
  207. }
  208. static int surface_button_remove(struct acpi_device *device)
  209. {
  210. struct surface_button *button = acpi_driver_data(device);
  211. input_unregister_device(button->input);
  212. kfree(button);
  213. return 0;
  214. }
  215. static SIMPLE_DEV_PM_OPS(surface_button_pm,
  216. surface_button_suspend, surface_button_resume);
  217. static struct acpi_driver surface_button_driver = {
  218. .name = "surface_pro3_button",
  219. .class = "SurfacePro3",
  220. .ids = surface_button_device_ids,
  221. .ops = {
  222. .add = surface_button_add,
  223. .remove = surface_button_remove,
  224. .notify = surface_button_notify,
  225. },
  226. .drv.pm = &surface_button_pm,
  227. };
  228. module_acpi_driver(surface_button_driver);