button.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. if (!button || !button->device)
  100. return 0;
  101. seq_printf(seq, "type: %s\n",
  102. acpi_device_name(button->device));
  103. return 0;
  104. }
  105. static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
  106. {
  107. return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
  108. }
  109. static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
  110. {
  111. struct acpi_button *button = (struct acpi_button *)seq->private;
  112. acpi_status status;
  113. unsigned long state;
  114. if (!button || !button->device)
  115. return 0;
  116. status = acpi_evaluate_integer(button->device->handle, "_LID", NULL, &state);
  117. if (ACPI_FAILURE(status)) {
  118. seq_printf(seq, "state: unsupported\n");
  119. } else {
  120. seq_printf(seq, "state: %s\n",
  121. (state ? "open" : "closed"));
  122. }
  123. return 0;
  124. }
  125. static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
  126. {
  127. return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
  128. }
  129. static struct proc_dir_entry *acpi_power_dir;
  130. static struct proc_dir_entry *acpi_sleep_dir;
  131. static struct proc_dir_entry *acpi_lid_dir;
  132. static int acpi_button_add_fs(struct acpi_device *device)
  133. {
  134. struct proc_dir_entry *entry = NULL;
  135. struct acpi_button *button = NULL;
  136. if (!device || !acpi_driver_data(device))
  137. return -EINVAL;
  138. button = acpi_driver_data(device);
  139. switch (button->type) {
  140. case ACPI_BUTTON_TYPE_POWER:
  141. case ACPI_BUTTON_TYPE_POWERF:
  142. if (!acpi_power_dir)
  143. acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
  144. acpi_button_dir);
  145. entry = acpi_power_dir;
  146. break;
  147. case ACPI_BUTTON_TYPE_SLEEP:
  148. case ACPI_BUTTON_TYPE_SLEEPF:
  149. if (!acpi_sleep_dir)
  150. acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
  151. acpi_button_dir);
  152. entry = acpi_sleep_dir;
  153. break;
  154. case ACPI_BUTTON_TYPE_LID:
  155. if (!acpi_lid_dir)
  156. acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
  157. acpi_button_dir);
  158. entry = acpi_lid_dir;
  159. break;
  160. }
  161. if (!entry)
  162. return -ENODEV;
  163. entry->owner = THIS_MODULE;
  164. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
  165. if (!acpi_device_dir(device))
  166. return -ENODEV;
  167. acpi_device_dir(device)->owner = THIS_MODULE;
  168. /* 'info' [R] */
  169. entry = create_proc_entry(ACPI_BUTTON_FILE_INFO,
  170. S_IRUGO, acpi_device_dir(device));
  171. if (!entry)
  172. return -ENODEV;
  173. else {
  174. entry->proc_fops = &acpi_button_info_fops;
  175. entry->data = acpi_driver_data(device);
  176. entry->owner = THIS_MODULE;
  177. }
  178. /* show lid state [R] */
  179. if (button->type == ACPI_BUTTON_TYPE_LID) {
  180. entry = create_proc_entry(ACPI_BUTTON_FILE_STATE,
  181. S_IRUGO, acpi_device_dir(device));
  182. if (!entry)
  183. return -ENODEV;
  184. else {
  185. entry->proc_fops = &acpi_button_state_fops;
  186. entry->data = acpi_driver_data(device);
  187. entry->owner = THIS_MODULE;
  188. }
  189. }
  190. return 0;
  191. }
  192. static int acpi_button_remove_fs(struct acpi_device *device)
  193. {
  194. struct acpi_button *button = NULL;
  195. button = acpi_driver_data(device);
  196. if (acpi_device_dir(device)) {
  197. if (button->type == ACPI_BUTTON_TYPE_LID)
  198. remove_proc_entry(ACPI_BUTTON_FILE_STATE,
  199. acpi_device_dir(device));
  200. remove_proc_entry(ACPI_BUTTON_FILE_INFO,
  201. acpi_device_dir(device));
  202. remove_proc_entry(acpi_device_bid(device),
  203. acpi_device_dir(device)->parent);
  204. acpi_device_dir(device) = NULL;
  205. }
  206. return 0;
  207. }
  208. /* --------------------------------------------------------------------------
  209. Driver Interface
  210. -------------------------------------------------------------------------- */
  211. static void acpi_button_notify(acpi_handle handle, u32 event, void *data)
  212. {
  213. struct acpi_button *button = (struct acpi_button *)data;
  214. if (!button || !button->device)
  215. return;
  216. switch (event) {
  217. case ACPI_BUTTON_NOTIFY_STATUS:
  218. acpi_bus_generate_event(button->device, event,
  219. ++button->pushed);
  220. break;
  221. default:
  222. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  223. "Unsupported event [0x%x]\n", event));
  224. break;
  225. }
  226. return;
  227. }
  228. static acpi_status acpi_button_notify_fixed(void *data)
  229. {
  230. struct acpi_button *button = (struct acpi_button *)data;
  231. if (!button)
  232. return AE_BAD_PARAMETER;
  233. acpi_button_notify(button->device->handle, ACPI_BUTTON_NOTIFY_STATUS, button);
  234. return AE_OK;
  235. }
  236. static int acpi_button_add(struct acpi_device *device)
  237. {
  238. int result = 0;
  239. acpi_status status = AE_OK;
  240. struct acpi_button *button = NULL;
  241. if (!device)
  242. return -EINVAL;
  243. button = kmalloc(sizeof(struct acpi_button), GFP_KERNEL);
  244. if (!button)
  245. return -ENOMEM;
  246. memset(button, 0, sizeof(struct acpi_button));
  247. button->device = device;
  248. button->handle = device->handle;
  249. acpi_driver_data(device) = button;
  250. /*
  251. * Determine the button type (via hid), as fixed-feature buttons
  252. * need to be handled a bit differently than generic-space.
  253. */
  254. if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWER)) {
  255. button->type = ACPI_BUTTON_TYPE_POWER;
  256. strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_POWER);
  257. sprintf(acpi_device_class(device), "%s/%s",
  258. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
  259. } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF)) {
  260. button->type = ACPI_BUTTON_TYPE_POWERF;
  261. strcpy(acpi_device_name(device),
  262. ACPI_BUTTON_DEVICE_NAME_POWERF);
  263. sprintf(acpi_device_class(device), "%s/%s",
  264. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
  265. } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEP)) {
  266. button->type = ACPI_BUTTON_TYPE_SLEEP;
  267. strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_SLEEP);
  268. sprintf(acpi_device_class(device), "%s/%s",
  269. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
  270. } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF)) {
  271. button->type = ACPI_BUTTON_TYPE_SLEEPF;
  272. strcpy(acpi_device_name(device),
  273. ACPI_BUTTON_DEVICE_NAME_SLEEPF);
  274. sprintf(acpi_device_class(device), "%s/%s",
  275. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
  276. } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_LID)) {
  277. button->type = ACPI_BUTTON_TYPE_LID;
  278. strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_LID);
  279. sprintf(acpi_device_class(device), "%s/%s",
  280. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
  281. } else {
  282. printk(KERN_ERR PREFIX "Unsupported hid [%s]\n",
  283. acpi_device_hid(device));
  284. result = -ENODEV;
  285. goto end;
  286. }
  287. result = acpi_button_add_fs(device);
  288. if (result)
  289. goto end;
  290. switch (button->type) {
  291. case ACPI_BUTTON_TYPE_POWERF:
  292. status =
  293. acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
  294. acpi_button_notify_fixed,
  295. button);
  296. break;
  297. case ACPI_BUTTON_TYPE_SLEEPF:
  298. status =
  299. acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
  300. acpi_button_notify_fixed,
  301. button);
  302. break;
  303. default:
  304. status = acpi_install_notify_handler(device->handle,
  305. ACPI_DEVICE_NOTIFY,
  306. acpi_button_notify,
  307. button);
  308. break;
  309. }
  310. if (ACPI_FAILURE(status)) {
  311. result = -ENODEV;
  312. goto end;
  313. }
  314. if (device->wakeup.flags.valid) {
  315. /* Button's GPE is run-wake GPE */
  316. acpi_set_gpe_type(device->wakeup.gpe_device,
  317. device->wakeup.gpe_number,
  318. ACPI_GPE_TYPE_WAKE_RUN);
  319. acpi_enable_gpe(device->wakeup.gpe_device,
  320. device->wakeup.gpe_number, ACPI_NOT_ISR);
  321. device->wakeup.state.enabled = 1;
  322. }
  323. printk(KERN_INFO PREFIX "%s [%s]\n",
  324. acpi_device_name(device), acpi_device_bid(device));
  325. end:
  326. if (result) {
  327. acpi_button_remove_fs(device);
  328. kfree(button);
  329. }
  330. return result;
  331. }
  332. static int acpi_button_remove(struct acpi_device *device, int type)
  333. {
  334. acpi_status status = 0;
  335. struct acpi_button *button = NULL;
  336. if (!device || !acpi_driver_data(device))
  337. return -EINVAL;
  338. button = acpi_driver_data(device);
  339. /* Unregister for device notifications. */
  340. switch (button->type) {
  341. case ACPI_BUTTON_TYPE_POWERF:
  342. status =
  343. acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
  344. acpi_button_notify_fixed);
  345. break;
  346. case ACPI_BUTTON_TYPE_SLEEPF:
  347. status =
  348. acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
  349. acpi_button_notify_fixed);
  350. break;
  351. default:
  352. status = acpi_remove_notify_handler(device->handle,
  353. ACPI_DEVICE_NOTIFY,
  354. acpi_button_notify);
  355. break;
  356. }
  357. acpi_button_remove_fs(device);
  358. kfree(button);
  359. return 0;
  360. }
  361. static int __init acpi_button_init(void)
  362. {
  363. int result = 0;
  364. acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
  365. if (!acpi_button_dir)
  366. return -ENODEV;
  367. acpi_button_dir->owner = THIS_MODULE;
  368. result = acpi_bus_register_driver(&acpi_button_driver);
  369. if (result < 0) {
  370. remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
  371. return -ENODEV;
  372. }
  373. return 0;
  374. }
  375. static void __exit acpi_button_exit(void)
  376. {
  377. acpi_bus_unregister_driver(&acpi_button_driver);
  378. if (acpi_power_dir)
  379. remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
  380. if (acpi_sleep_dir)
  381. remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
  382. if (acpi_lid_dir)
  383. remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
  384. remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
  385. return;
  386. }
  387. module_init(acpi_button_init);
  388. module_exit(acpi_button_exit);