button.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 <linux/types.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/seq_file.h>
  31. #include <acpi/acpi_bus.h>
  32. #include <acpi/acpi_drivers.h>
  33. #define ACPI_BUTTON_COMPONENT 0x00080000
  34. #define ACPI_BUTTON_DRIVER_NAME "ACPI Button Driver"
  35. #define ACPI_BUTTON_CLASS "button"
  36. #define ACPI_BUTTON_FILE_INFO "info"
  37. #define ACPI_BUTTON_FILE_STATE "state"
  38. #define ACPI_BUTTON_TYPE_UNKNOWN 0x00
  39. #define ACPI_BUTTON_NOTIFY_STATUS 0x80
  40. #define ACPI_BUTTON_SUBCLASS_POWER "power"
  41. #define ACPI_BUTTON_HID_POWER "PNP0C0C"
  42. #define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button (CM)"
  43. #define ACPI_BUTTON_DEVICE_NAME_POWERF "Power Button (FF)"
  44. #define ACPI_BUTTON_TYPE_POWER 0x01
  45. #define ACPI_BUTTON_TYPE_POWERF 0x02
  46. #define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
  47. #define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
  48. #define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button (CM)"
  49. #define ACPI_BUTTON_DEVICE_NAME_SLEEPF "Sleep Button (FF)"
  50. #define ACPI_BUTTON_TYPE_SLEEP 0x03
  51. #define ACPI_BUTTON_TYPE_SLEEPF 0x04
  52. #define ACPI_BUTTON_SUBCLASS_LID "lid"
  53. #define ACPI_BUTTON_HID_LID "PNP0C0D"
  54. #define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
  55. #define ACPI_BUTTON_TYPE_LID 0x05
  56. #define _COMPONENT ACPI_BUTTON_COMPONENT
  57. ACPI_MODULE_NAME("acpi_button")
  58. MODULE_AUTHOR("Paul Diefenbaugh");
  59. MODULE_DESCRIPTION(ACPI_BUTTON_DRIVER_NAME);
  60. MODULE_LICENSE("GPL");
  61. static int acpi_button_add(struct acpi_device *device);
  62. static int acpi_button_remove(struct acpi_device *device, int type);
  63. static int acpi_button_info_open_fs(struct inode *inode, struct file *file);
  64. static int acpi_button_state_open_fs(struct inode *inode, struct file *file);
  65. static struct acpi_driver acpi_button_driver = {
  66. .name = ACPI_BUTTON_DRIVER_NAME,
  67. .class = ACPI_BUTTON_CLASS,
  68. .ids = "ACPI_FPB,ACPI_FSB,PNP0C0D,PNP0C0C,PNP0C0E",
  69. .ops = {
  70. .add = acpi_button_add,
  71. .remove = acpi_button_remove,
  72. },
  73. };
  74. struct acpi_button {
  75. acpi_handle handle;
  76. struct acpi_device *device; /* Fixed button kludge */
  77. u8 type;
  78. unsigned long pushed;
  79. };
  80. static struct file_operations acpi_button_info_fops = {
  81. .open = acpi_button_info_open_fs,
  82. .read = seq_read,
  83. .llseek = seq_lseek,
  84. .release = single_release,
  85. };
  86. static struct file_operations acpi_button_state_fops = {
  87. .open = acpi_button_state_open_fs,
  88. .read = seq_read,
  89. .llseek = seq_lseek,
  90. .release = single_release,
  91. };
  92. /* --------------------------------------------------------------------------
  93. FS Interface (/proc)
  94. -------------------------------------------------------------------------- */
  95. static struct proc_dir_entry *acpi_button_dir;
  96. static int acpi_button_info_seq_show(struct seq_file *seq, void *offset)
  97. {
  98. struct acpi_button *button = (struct acpi_button *)seq->private;
  99. ACPI_FUNCTION_TRACE("acpi_button_info_seq_show");
  100. if (!button || !button->device)
  101. return_VALUE(0);
  102. seq_printf(seq, "type: %s\n",
  103. acpi_device_name(button->device));
  104. return_VALUE(0);
  105. }
  106. static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
  107. {
  108. return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
  109. }
  110. static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
  111. {
  112. struct acpi_button *button = (struct acpi_button *)seq->private;
  113. acpi_status status;
  114. unsigned long state;
  115. ACPI_FUNCTION_TRACE("acpi_button_state_seq_show");
  116. if (!button || !button->device)
  117. return_VALUE(0);
  118. status = acpi_evaluate_integer(button->handle, "_LID", NULL, &state);
  119. if (ACPI_FAILURE(status)) {
  120. seq_printf(seq, "state: unsupported\n");
  121. } else {
  122. seq_printf(seq, "state: %s\n",
  123. (state ? "open" : "closed"));
  124. }
  125. return_VALUE(0);
  126. }
  127. static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
  128. {
  129. return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
  130. }
  131. static struct proc_dir_entry *acpi_power_dir;
  132. static struct proc_dir_entry *acpi_sleep_dir;
  133. static struct proc_dir_entry *acpi_lid_dir;
  134. static int acpi_button_add_fs(struct acpi_device *device)
  135. {
  136. struct proc_dir_entry *entry = NULL;
  137. struct acpi_button *button = NULL;
  138. ACPI_FUNCTION_TRACE("acpi_button_add_fs");
  139. if (!device || !acpi_driver_data(device))
  140. return_VALUE(-EINVAL);
  141. button = acpi_driver_data(device);
  142. switch (button->type) {
  143. case ACPI_BUTTON_TYPE_POWER:
  144. case ACPI_BUTTON_TYPE_POWERF:
  145. if (!acpi_power_dir)
  146. acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
  147. acpi_button_dir);
  148. entry = acpi_power_dir;
  149. break;
  150. case ACPI_BUTTON_TYPE_SLEEP:
  151. case ACPI_BUTTON_TYPE_SLEEPF:
  152. if (!acpi_sleep_dir)
  153. acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
  154. acpi_button_dir);
  155. entry = acpi_sleep_dir;
  156. break;
  157. case ACPI_BUTTON_TYPE_LID:
  158. if (!acpi_lid_dir)
  159. acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
  160. acpi_button_dir);
  161. entry = acpi_lid_dir;
  162. break;
  163. }
  164. if (!entry)
  165. return_VALUE(-ENODEV);
  166. entry->owner = THIS_MODULE;
  167. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
  168. if (!acpi_device_dir(device))
  169. return_VALUE(-ENODEV);
  170. acpi_device_dir(device)->owner = THIS_MODULE;
  171. /* 'info' [R] */
  172. entry = create_proc_entry(ACPI_BUTTON_FILE_INFO,
  173. S_IRUGO, acpi_device_dir(device));
  174. if (!entry)
  175. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  176. "Unable to create '%s' fs entry\n",
  177. ACPI_BUTTON_FILE_INFO));
  178. else {
  179. entry->proc_fops = &acpi_button_info_fops;
  180. entry->data = acpi_driver_data(device);
  181. entry->owner = THIS_MODULE;
  182. }
  183. /* show lid state [R] */
  184. if (button->type == ACPI_BUTTON_TYPE_LID) {
  185. entry = create_proc_entry(ACPI_BUTTON_FILE_STATE,
  186. S_IRUGO, acpi_device_dir(device));
  187. if (!entry)
  188. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  189. "Unable to create '%s' fs entry\n",
  190. ACPI_BUTTON_FILE_INFO));
  191. else {
  192. entry->proc_fops = &acpi_button_state_fops;
  193. entry->data = acpi_driver_data(device);
  194. entry->owner = THIS_MODULE;
  195. }
  196. }
  197. return_VALUE(0);
  198. }
  199. static int acpi_button_remove_fs(struct acpi_device *device)
  200. {
  201. struct acpi_button *button = NULL;
  202. ACPI_FUNCTION_TRACE("acpi_button_remove_fs");
  203. button = acpi_driver_data(device);
  204. if (acpi_device_dir(device)) {
  205. if (button->type == ACPI_BUTTON_TYPE_LID)
  206. remove_proc_entry(ACPI_BUTTON_FILE_STATE,
  207. acpi_device_dir(device));
  208. remove_proc_entry(ACPI_BUTTON_FILE_INFO,
  209. acpi_device_dir(device));
  210. remove_proc_entry(acpi_device_bid(device),
  211. acpi_device_dir(device)->parent);
  212. acpi_device_dir(device) = NULL;
  213. }
  214. return_VALUE(0);
  215. }
  216. /* --------------------------------------------------------------------------
  217. Driver Interface
  218. -------------------------------------------------------------------------- */
  219. static void acpi_button_notify(acpi_handle handle, u32 event, void *data)
  220. {
  221. struct acpi_button *button = (struct acpi_button *)data;
  222. ACPI_FUNCTION_TRACE("acpi_button_notify");
  223. if (!button || !button->device)
  224. return_VOID;
  225. switch (event) {
  226. case ACPI_BUTTON_NOTIFY_STATUS:
  227. acpi_bus_generate_event(button->device, event,
  228. ++button->pushed);
  229. break;
  230. default:
  231. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  232. "Unsupported event [0x%x]\n", event));
  233. break;
  234. }
  235. return_VOID;
  236. }
  237. static acpi_status acpi_button_notify_fixed(void *data)
  238. {
  239. struct acpi_button *button = (struct acpi_button *)data;
  240. ACPI_FUNCTION_TRACE("acpi_button_notify_fixed");
  241. if (!button)
  242. return_ACPI_STATUS(AE_BAD_PARAMETER);
  243. acpi_button_notify(button->handle, ACPI_BUTTON_NOTIFY_STATUS, button);
  244. return_ACPI_STATUS(AE_OK);
  245. }
  246. static int acpi_button_add(struct acpi_device *device)
  247. {
  248. int result = 0;
  249. acpi_status status = AE_OK;
  250. struct acpi_button *button = NULL;
  251. ACPI_FUNCTION_TRACE("acpi_button_add");
  252. if (!device)
  253. return_VALUE(-EINVAL);
  254. button = kmalloc(sizeof(struct acpi_button), GFP_KERNEL);
  255. if (!button)
  256. return_VALUE(-ENOMEM);
  257. memset(button, 0, sizeof(struct acpi_button));
  258. button->device = device;
  259. button->handle = device->handle;
  260. acpi_driver_data(device) = button;
  261. /*
  262. * Determine the button type (via hid), as fixed-feature buttons
  263. * need to be handled a bit differently than generic-space.
  264. */
  265. if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWER)) {
  266. button->type = ACPI_BUTTON_TYPE_POWER;
  267. strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_POWER);
  268. sprintf(acpi_device_class(device), "%s/%s",
  269. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
  270. } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF)) {
  271. button->type = ACPI_BUTTON_TYPE_POWERF;
  272. strcpy(acpi_device_name(device),
  273. ACPI_BUTTON_DEVICE_NAME_POWERF);
  274. sprintf(acpi_device_class(device), "%s/%s",
  275. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
  276. } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEP)) {
  277. button->type = ACPI_BUTTON_TYPE_SLEEP;
  278. strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_SLEEP);
  279. sprintf(acpi_device_class(device), "%s/%s",
  280. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
  281. } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF)) {
  282. button->type = ACPI_BUTTON_TYPE_SLEEPF;
  283. strcpy(acpi_device_name(device),
  284. ACPI_BUTTON_DEVICE_NAME_SLEEPF);
  285. sprintf(acpi_device_class(device), "%s/%s",
  286. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
  287. } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_LID)) {
  288. button->type = ACPI_BUTTON_TYPE_LID;
  289. strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_LID);
  290. sprintf(acpi_device_class(device), "%s/%s",
  291. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
  292. } else {
  293. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unsupported hid [%s]\n",
  294. acpi_device_hid(device)));
  295. result = -ENODEV;
  296. goto end;
  297. }
  298. result = acpi_button_add_fs(device);
  299. if (result)
  300. goto end;
  301. switch (button->type) {
  302. case ACPI_BUTTON_TYPE_POWERF:
  303. status =
  304. acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
  305. acpi_button_notify_fixed,
  306. button);
  307. break;
  308. case ACPI_BUTTON_TYPE_SLEEPF:
  309. status =
  310. acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
  311. acpi_button_notify_fixed,
  312. button);
  313. break;
  314. default:
  315. status = acpi_install_notify_handler(button->handle,
  316. ACPI_DEVICE_NOTIFY,
  317. acpi_button_notify,
  318. button);
  319. break;
  320. }
  321. if (ACPI_FAILURE(status)) {
  322. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  323. "Error installing notify handler\n"));
  324. result = -ENODEV;
  325. goto end;
  326. }
  327. if (device->wakeup.flags.valid) {
  328. /* Button's GPE is run-wake GPE */
  329. acpi_set_gpe_type(device->wakeup.gpe_device,
  330. device->wakeup.gpe_number,
  331. ACPI_GPE_TYPE_WAKE_RUN);
  332. acpi_enable_gpe(device->wakeup.gpe_device,
  333. device->wakeup.gpe_number, ACPI_NOT_ISR);
  334. device->wakeup.state.enabled = 1;
  335. }
  336. printk(KERN_INFO PREFIX "%s [%s]\n",
  337. acpi_device_name(device), acpi_device_bid(device));
  338. end:
  339. if (result) {
  340. acpi_button_remove_fs(device);
  341. kfree(button);
  342. }
  343. return_VALUE(result);
  344. }
  345. static int acpi_button_remove(struct acpi_device *device, int type)
  346. {
  347. acpi_status status = 0;
  348. struct acpi_button *button = NULL;
  349. ACPI_FUNCTION_TRACE("acpi_button_remove");
  350. if (!device || !acpi_driver_data(device))
  351. return_VALUE(-EINVAL);
  352. button = acpi_driver_data(device);
  353. /* Unregister for device notifications. */
  354. switch (button->type) {
  355. case ACPI_BUTTON_TYPE_POWERF:
  356. status =
  357. acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
  358. acpi_button_notify_fixed);
  359. break;
  360. case ACPI_BUTTON_TYPE_SLEEPF:
  361. status =
  362. acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
  363. acpi_button_notify_fixed);
  364. break;
  365. default:
  366. status = acpi_remove_notify_handler(button->handle,
  367. ACPI_DEVICE_NOTIFY,
  368. acpi_button_notify);
  369. break;
  370. }
  371. if (ACPI_FAILURE(status))
  372. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  373. "Error removing notify handler\n"));
  374. acpi_button_remove_fs(device);
  375. kfree(button);
  376. return_VALUE(0);
  377. }
  378. static int __init acpi_button_init(void)
  379. {
  380. int result = 0;
  381. ACPI_FUNCTION_TRACE("acpi_button_init");
  382. acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
  383. if (!acpi_button_dir)
  384. return_VALUE(-ENODEV);
  385. acpi_button_dir->owner = THIS_MODULE;
  386. result = acpi_bus_register_driver(&acpi_button_driver);
  387. if (result < 0) {
  388. remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
  389. return_VALUE(-ENODEV);
  390. }
  391. return_VALUE(0);
  392. }
  393. static void __exit acpi_button_exit(void)
  394. {
  395. ACPI_FUNCTION_TRACE("acpi_button_exit");
  396. acpi_bus_unregister_driver(&acpi_button_driver);
  397. if (acpi_power_dir)
  398. remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
  399. if (acpi_sleep_dir)
  400. remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
  401. if (acpi_lid_dir)
  402. remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
  403. remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
  404. return_VOID;
  405. }
  406. module_init(acpi_button_init);
  407. module_exit(acpi_button_exit);