surfacepro3_button.c 7.3 KB

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