button.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * acpi_button.c - ACPI Button Driver ($Revision: 30 $)
  3. *
  4. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  5. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  22. *
  23. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <acpi/acpi_bus.h>
  29. #include <acpi/acpi_drivers.h>
  30. #define ACPI_BUTTON_COMPONENT 0x00080000
  31. #define ACPI_BUTTON_DRIVER_NAME "ACPI Button Driver"
  32. #define ACPI_BUTTON_CLASS "button"
  33. #define ACPI_BUTTON_NOTIFY_STATUS 0x80
  34. #define ACPI_BUTTON_SUBCLASS_POWER "power"
  35. #define ACPI_BUTTON_HID_POWER "PNP0C0C"
  36. #define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button (CM)"
  37. #define ACPI_BUTTON_DEVICE_NAME_POWERF "Power Button (FF)"
  38. #define ACPI_BUTTON_TYPE_POWER 0x01
  39. #define ACPI_BUTTON_TYPE_POWERF 0x02
  40. #define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
  41. #define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
  42. #define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button (CM)"
  43. #define ACPI_BUTTON_DEVICE_NAME_SLEEPF "Sleep Button (FF)"
  44. #define ACPI_BUTTON_TYPE_SLEEP 0x03
  45. #define ACPI_BUTTON_TYPE_SLEEPF 0x04
  46. #define ACPI_BUTTON_SUBCLASS_LID "lid"
  47. #define ACPI_BUTTON_HID_LID "PNP0C0D"
  48. #define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
  49. #define ACPI_BUTTON_TYPE_LID 0x05
  50. #define _COMPONENT ACPI_BUTTON_COMPONENT
  51. ACPI_MODULE_NAME ("acpi_button")
  52. MODULE_AUTHOR("Paul Diefenbaugh");
  53. MODULE_DESCRIPTION(ACPI_BUTTON_DRIVER_NAME);
  54. MODULE_LICENSE("GPL");
  55. static int acpi_button_add (struct acpi_device *device);
  56. static int acpi_button_remove (struct acpi_device *device, int type);
  57. static struct acpi_driver acpi_button_driver = {
  58. .name = ACPI_BUTTON_DRIVER_NAME,
  59. .class = ACPI_BUTTON_CLASS,
  60. .ids = "ACPI_FPB,ACPI_FSB,PNP0C0D,PNP0C0C,PNP0C0E",
  61. .ops = {
  62. .add = acpi_button_add,
  63. .remove = acpi_button_remove,
  64. },
  65. };
  66. struct acpi_button {
  67. acpi_handle handle;
  68. struct acpi_device *device; /* Fixed button kludge */
  69. u8 type;
  70. unsigned long pushed;
  71. };
  72. /* --------------------------------------------------------------------------
  73. Driver Interface
  74. -------------------------------------------------------------------------- */
  75. static void
  76. acpi_button_notify (
  77. acpi_handle handle,
  78. u32 event,
  79. void *data)
  80. {
  81. struct acpi_button *button = (struct acpi_button *) data;
  82. ACPI_FUNCTION_TRACE("acpi_button_notify");
  83. if (!button || !button->device)
  84. return_VOID;
  85. switch (event) {
  86. case ACPI_BUTTON_NOTIFY_STATUS:
  87. acpi_bus_generate_event(button->device, event, ++button->pushed);
  88. break;
  89. default:
  90. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  91. "Unsupported event [0x%x]\n", event));
  92. break;
  93. }
  94. return_VOID;
  95. }
  96. static acpi_status
  97. acpi_button_notify_fixed (
  98. void *data)
  99. {
  100. struct acpi_button *button = (struct acpi_button *) data;
  101. ACPI_FUNCTION_TRACE("acpi_button_notify_fixed");
  102. BUG_ON(!button);
  103. acpi_button_notify(button->handle, ACPI_BUTTON_NOTIFY_STATUS, button);
  104. return_ACPI_STATUS(AE_OK);
  105. }
  106. static int
  107. acpi_button_add (
  108. struct acpi_device *device)
  109. {
  110. int result = 0;
  111. acpi_status status = AE_OK;
  112. struct acpi_button *button = NULL;
  113. ACPI_FUNCTION_TRACE("acpi_button_add");
  114. if (!device)
  115. return_VALUE(-EINVAL);
  116. button = kmalloc(sizeof(struct acpi_button), GFP_KERNEL);
  117. if (!button)
  118. return_VALUE(-ENOMEM);
  119. memset(button, 0, sizeof(struct acpi_button));
  120. button->device = device;
  121. button->handle = device->handle;
  122. acpi_driver_data(device) = button;
  123. /*
  124. * Determine the button type (via hid), as fixed-feature buttons
  125. * need to be handled a bit differently than generic-space.
  126. */
  127. if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWER)) {
  128. button->type = ACPI_BUTTON_TYPE_POWER;
  129. strcpy(acpi_device_name(device),
  130. ACPI_BUTTON_DEVICE_NAME_POWER);
  131. sprintf(acpi_device_class(device), "%s/%s",
  132. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
  133. }
  134. else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF)) {
  135. button->type = ACPI_BUTTON_TYPE_POWERF;
  136. strcpy(acpi_device_name(device),
  137. ACPI_BUTTON_DEVICE_NAME_POWERF);
  138. sprintf(acpi_device_class(device), "%s/%s",
  139. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
  140. }
  141. else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEP)) {
  142. button->type = ACPI_BUTTON_TYPE_SLEEP;
  143. strcpy(acpi_device_name(device),
  144. ACPI_BUTTON_DEVICE_NAME_SLEEP);
  145. sprintf(acpi_device_class(device), "%s/%s",
  146. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
  147. }
  148. else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF)) {
  149. button->type = ACPI_BUTTON_TYPE_SLEEPF;
  150. strcpy(acpi_device_name(device),
  151. ACPI_BUTTON_DEVICE_NAME_SLEEPF);
  152. sprintf(acpi_device_class(device), "%s/%s",
  153. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
  154. }
  155. else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_LID)) {
  156. button->type = ACPI_BUTTON_TYPE_LID;
  157. strcpy(acpi_device_name(device),
  158. ACPI_BUTTON_DEVICE_NAME_LID);
  159. sprintf(acpi_device_class(device), "%s/%s",
  160. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
  161. }
  162. else {
  163. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unsupported hid [%s]\n",
  164. acpi_device_hid(device)));
  165. result = -ENODEV;
  166. goto end;
  167. }
  168. switch (button->type) {
  169. case ACPI_BUTTON_TYPE_POWERF:
  170. status = acpi_install_fixed_event_handler (
  171. ACPI_EVENT_POWER_BUTTON,
  172. acpi_button_notify_fixed,
  173. button);
  174. break;
  175. case ACPI_BUTTON_TYPE_SLEEPF:
  176. status = acpi_install_fixed_event_handler (
  177. ACPI_EVENT_SLEEP_BUTTON,
  178. acpi_button_notify_fixed,
  179. button);
  180. break;
  181. default:
  182. status = acpi_install_notify_handler (
  183. button->handle,
  184. ACPI_DEVICE_NOTIFY,
  185. acpi_button_notify,
  186. button);
  187. break;
  188. }
  189. if (ACPI_FAILURE(status)) {
  190. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  191. "Error installing notify handler\n"));
  192. result = -ENODEV;
  193. goto end;
  194. }
  195. if (device->wakeup.flags.valid) {
  196. /* Button's GPE is run-wake GPE */
  197. acpi_set_gpe_type(device->wakeup.gpe_device,
  198. device->wakeup.gpe_number, ACPI_GPE_TYPE_WAKE_RUN);
  199. acpi_enable_gpe(device->wakeup.gpe_device,
  200. device->wakeup.gpe_number, ACPI_NOT_ISR);
  201. device->wakeup.state.enabled = 1;
  202. }
  203. printk(KERN_INFO PREFIX "%s [%s]\n",
  204. acpi_device_name(device), acpi_device_bid(device));
  205. end:
  206. if (result) {
  207. kfree(button);
  208. }
  209. return_VALUE(result);
  210. }
  211. static int
  212. acpi_button_remove (struct acpi_device *device, int type)
  213. {
  214. acpi_status status = 0;
  215. struct acpi_button *button = NULL;
  216. ACPI_FUNCTION_TRACE("acpi_button_remove");
  217. if (!device || !acpi_driver_data(device))
  218. return_VALUE(-EINVAL);
  219. button = acpi_driver_data(device);
  220. /* Unregister for device notifications. */
  221. switch (button->type) {
  222. case ACPI_BUTTON_TYPE_POWERF:
  223. status = acpi_remove_fixed_event_handler(
  224. ACPI_EVENT_POWER_BUTTON, acpi_button_notify_fixed);
  225. break;
  226. case ACPI_BUTTON_TYPE_SLEEPF:
  227. status = acpi_remove_fixed_event_handler(
  228. ACPI_EVENT_SLEEP_BUTTON, acpi_button_notify_fixed);
  229. break;
  230. default:
  231. status = acpi_remove_notify_handler(button->handle,
  232. ACPI_DEVICE_NOTIFY, acpi_button_notify);
  233. break;
  234. }
  235. if (ACPI_FAILURE(status))
  236. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  237. "Error removing notify handler\n"));
  238. kfree(button);
  239. return_VALUE(0);
  240. }
  241. static int __init
  242. acpi_button_init (void)
  243. {
  244. int result = 0;
  245. ACPI_FUNCTION_TRACE("acpi_button_init");
  246. result = acpi_bus_register_driver(&acpi_button_driver);
  247. if (result < 0) {
  248. return_VALUE(-ENODEV);
  249. }
  250. return_VALUE(0);
  251. }
  252. static void __exit
  253. acpi_button_exit (void)
  254. {
  255. ACPI_FUNCTION_TRACE("acpi_button_exit");
  256. acpi_bus_unregister_driver(&acpi_button_driver);
  257. return_VOID;
  258. }
  259. module_init(acpi_button_init);
  260. module_exit(acpi_button_exit);