button.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. }
  122. else{
  123. seq_printf(seq, "state: %s\n", (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 int
  132. acpi_button_add_fs (
  133. struct acpi_device *device)
  134. {
  135. struct proc_dir_entry *entry = NULL;
  136. struct acpi_button *button = NULL;
  137. ACPI_FUNCTION_TRACE("acpi_button_add_fs");
  138. if (!device || !acpi_driver_data(device))
  139. return_VALUE(-EINVAL);
  140. button = acpi_driver_data(device);
  141. switch (button->type) {
  142. case ACPI_BUTTON_TYPE_POWER:
  143. case ACPI_BUTTON_TYPE_POWERF:
  144. entry = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
  145. acpi_button_dir);
  146. break;
  147. case ACPI_BUTTON_TYPE_SLEEP:
  148. case ACPI_BUTTON_TYPE_SLEEPF:
  149. entry = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
  150. acpi_button_dir);
  151. break;
  152. case ACPI_BUTTON_TYPE_LID:
  153. entry = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
  154. acpi_button_dir);
  155. break;
  156. }
  157. if (!entry)
  158. return_VALUE(-ENODEV);
  159. entry->owner = THIS_MODULE;
  160. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
  161. if (!acpi_device_dir(device))
  162. return_VALUE(-ENODEV);
  163. acpi_device_dir(device)->owner = THIS_MODULE;
  164. /* 'info' [R] */
  165. entry = create_proc_entry(ACPI_BUTTON_FILE_INFO,
  166. S_IRUGO, acpi_device_dir(device));
  167. if (!entry)
  168. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  169. "Unable to create '%s' fs entry\n",
  170. ACPI_BUTTON_FILE_INFO));
  171. else {
  172. entry->proc_fops = &acpi_button_info_fops;
  173. entry->data = acpi_driver_data(device);
  174. entry->owner = THIS_MODULE;
  175. }
  176. /* show lid state [R] */
  177. if (button->type == ACPI_BUTTON_TYPE_LID) {
  178. entry = create_proc_entry(ACPI_BUTTON_FILE_STATE,
  179. S_IRUGO, acpi_device_dir(device));
  180. if (!entry)
  181. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  182. "Unable to create '%s' fs entry\n",
  183. ACPI_BUTTON_FILE_INFO));
  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_VALUE(0);
  191. }
  192. static int
  193. acpi_button_remove_fs (
  194. struct acpi_device *device)
  195. {
  196. struct acpi_button *button = NULL;
  197. ACPI_FUNCTION_TRACE("acpi_button_remove_fs");
  198. button = acpi_driver_data(device);
  199. if (acpi_device_dir(device)) {
  200. if (button->type == ACPI_BUTTON_TYPE_LID)
  201. remove_proc_entry(ACPI_BUTTON_FILE_STATE,
  202. acpi_device_dir(device));
  203. remove_proc_entry(ACPI_BUTTON_FILE_INFO,
  204. acpi_device_dir(device));
  205. remove_proc_entry(acpi_device_bid(device),
  206. acpi_device_dir(device)->parent);
  207. switch (button->type) {
  208. case ACPI_BUTTON_TYPE_POWER:
  209. case ACPI_BUTTON_TYPE_POWERF:
  210. remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER,
  211. acpi_button_dir);
  212. break;
  213. case ACPI_BUTTON_TYPE_SLEEP:
  214. case ACPI_BUTTON_TYPE_SLEEPF:
  215. remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP,
  216. acpi_button_dir);
  217. break;
  218. case ACPI_BUTTON_TYPE_LID:
  219. remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID,
  220. acpi_button_dir);
  221. break;
  222. }
  223. acpi_device_dir(device) = NULL;
  224. }
  225. return_VALUE(0);
  226. }
  227. /* --------------------------------------------------------------------------
  228. Driver Interface
  229. -------------------------------------------------------------------------- */
  230. static void
  231. acpi_button_notify (
  232. acpi_handle handle,
  233. u32 event,
  234. void *data)
  235. {
  236. struct acpi_button *button = (struct acpi_button *) data;
  237. ACPI_FUNCTION_TRACE("acpi_button_notify");
  238. if (!button || !button->device)
  239. return_VOID;
  240. switch (event) {
  241. case ACPI_BUTTON_NOTIFY_STATUS:
  242. acpi_bus_generate_event(button->device, event, ++button->pushed);
  243. break;
  244. default:
  245. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  246. "Unsupported event [0x%x]\n", event));
  247. break;
  248. }
  249. return_VOID;
  250. }
  251. static acpi_status
  252. acpi_button_notify_fixed (
  253. void *data)
  254. {
  255. struct acpi_button *button = (struct acpi_button *) data;
  256. ACPI_FUNCTION_TRACE("acpi_button_notify_fixed");
  257. if (!button)
  258. return_ACPI_STATUS(AE_BAD_PARAMETER);
  259. acpi_button_notify(button->handle, ACPI_BUTTON_NOTIFY_STATUS, button);
  260. return_ACPI_STATUS(AE_OK);
  261. }
  262. static int
  263. acpi_button_add (
  264. struct acpi_device *device)
  265. {
  266. int result = 0;
  267. acpi_status status = AE_OK;
  268. struct acpi_button *button = NULL;
  269. static struct acpi_device *power_button;
  270. static struct acpi_device *sleep_button;
  271. static struct acpi_device *lid_button;
  272. ACPI_FUNCTION_TRACE("acpi_button_add");
  273. if (!device)
  274. return_VALUE(-EINVAL);
  275. button = kmalloc(sizeof(struct acpi_button), GFP_KERNEL);
  276. if (!button)
  277. return_VALUE(-ENOMEM);
  278. memset(button, 0, sizeof(struct acpi_button));
  279. button->device = device;
  280. button->handle = device->handle;
  281. acpi_driver_data(device) = button;
  282. /*
  283. * Determine the button type (via hid), as fixed-feature buttons
  284. * need to be handled a bit differently than generic-space.
  285. */
  286. if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWER)) {
  287. button->type = ACPI_BUTTON_TYPE_POWER;
  288. strcpy(acpi_device_name(device),
  289. ACPI_BUTTON_DEVICE_NAME_POWER);
  290. sprintf(acpi_device_class(device), "%s/%s",
  291. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
  292. }
  293. else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF)) {
  294. button->type = ACPI_BUTTON_TYPE_POWERF;
  295. strcpy(acpi_device_name(device),
  296. ACPI_BUTTON_DEVICE_NAME_POWERF);
  297. sprintf(acpi_device_class(device), "%s/%s",
  298. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
  299. }
  300. else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEP)) {
  301. button->type = ACPI_BUTTON_TYPE_SLEEP;
  302. strcpy(acpi_device_name(device),
  303. ACPI_BUTTON_DEVICE_NAME_SLEEP);
  304. sprintf(acpi_device_class(device), "%s/%s",
  305. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
  306. }
  307. else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF)) {
  308. button->type = ACPI_BUTTON_TYPE_SLEEPF;
  309. strcpy(acpi_device_name(device),
  310. ACPI_BUTTON_DEVICE_NAME_SLEEPF);
  311. sprintf(acpi_device_class(device), "%s/%s",
  312. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
  313. }
  314. else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_LID)) {
  315. button->type = ACPI_BUTTON_TYPE_LID;
  316. strcpy(acpi_device_name(device),
  317. ACPI_BUTTON_DEVICE_NAME_LID);
  318. sprintf(acpi_device_class(device), "%s/%s",
  319. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
  320. }
  321. else {
  322. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unsupported hid [%s]\n",
  323. acpi_device_hid(device)));
  324. result = -ENODEV;
  325. goto end;
  326. }
  327. /*
  328. * Ensure only one button of each type is used.
  329. */
  330. switch (button->type) {
  331. case ACPI_BUTTON_TYPE_POWER:
  332. case ACPI_BUTTON_TYPE_POWERF:
  333. if (!power_button)
  334. power_button = device;
  335. else {
  336. kfree(button);
  337. return_VALUE(-ENODEV);
  338. }
  339. break;
  340. case ACPI_BUTTON_TYPE_SLEEP:
  341. case ACPI_BUTTON_TYPE_SLEEPF:
  342. if (!sleep_button)
  343. sleep_button = device;
  344. else {
  345. kfree(button);
  346. return_VALUE(-ENODEV);
  347. }
  348. break;
  349. case ACPI_BUTTON_TYPE_LID:
  350. if (!lid_button)
  351. lid_button = device;
  352. else {
  353. kfree(button);
  354. return_VALUE(-ENODEV);
  355. }
  356. break;
  357. }
  358. result = acpi_button_add_fs(device);
  359. if (result)
  360. goto end;
  361. switch (button->type) {
  362. case ACPI_BUTTON_TYPE_POWERF:
  363. status = acpi_install_fixed_event_handler (
  364. ACPI_EVENT_POWER_BUTTON,
  365. acpi_button_notify_fixed,
  366. button);
  367. break;
  368. case ACPI_BUTTON_TYPE_SLEEPF:
  369. status = acpi_install_fixed_event_handler (
  370. ACPI_EVENT_SLEEP_BUTTON,
  371. acpi_button_notify_fixed,
  372. button);
  373. break;
  374. default:
  375. status = acpi_install_notify_handler (
  376. button->handle,
  377. ACPI_DEVICE_NOTIFY,
  378. acpi_button_notify,
  379. button);
  380. break;
  381. }
  382. if (ACPI_FAILURE(status)) {
  383. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  384. "Error installing notify handler\n"));
  385. result = -ENODEV;
  386. goto end;
  387. }
  388. if (device->wakeup.flags.valid) {
  389. /* Button's GPE is run-wake GPE */
  390. acpi_set_gpe_type(device->wakeup.gpe_device,
  391. device->wakeup.gpe_number, ACPI_GPE_TYPE_WAKE_RUN);
  392. acpi_enable_gpe(device->wakeup.gpe_device,
  393. device->wakeup.gpe_number, ACPI_NOT_ISR);
  394. device->wakeup.state.enabled = 1;
  395. }
  396. printk(KERN_INFO PREFIX "%s [%s]\n",
  397. acpi_device_name(device), acpi_device_bid(device));
  398. end:
  399. if (result) {
  400. acpi_button_remove_fs(device);
  401. kfree(button);
  402. }
  403. return_VALUE(result);
  404. }
  405. static int
  406. acpi_button_remove (struct acpi_device *device, int type)
  407. {
  408. acpi_status status = 0;
  409. struct acpi_button *button = NULL;
  410. ACPI_FUNCTION_TRACE("acpi_button_remove");
  411. if (!device || !acpi_driver_data(device))
  412. return_VALUE(-EINVAL);
  413. button = acpi_driver_data(device);
  414. /* Unregister for device notifications. */
  415. switch (button->type) {
  416. case ACPI_BUTTON_TYPE_POWERF:
  417. status = acpi_remove_fixed_event_handler(
  418. ACPI_EVENT_POWER_BUTTON, acpi_button_notify_fixed);
  419. break;
  420. case ACPI_BUTTON_TYPE_SLEEPF:
  421. status = acpi_remove_fixed_event_handler(
  422. ACPI_EVENT_SLEEP_BUTTON, acpi_button_notify_fixed);
  423. break;
  424. default:
  425. status = acpi_remove_notify_handler(button->handle,
  426. ACPI_DEVICE_NOTIFY, acpi_button_notify);
  427. break;
  428. }
  429. if (ACPI_FAILURE(status))
  430. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  431. "Error removing notify handler\n"));
  432. acpi_button_remove_fs(device);
  433. kfree(button);
  434. return_VALUE(0);
  435. }
  436. static int __init
  437. acpi_button_init (void)
  438. {
  439. int result = 0;
  440. ACPI_FUNCTION_TRACE("acpi_button_init");
  441. acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
  442. if (!acpi_button_dir)
  443. return_VALUE(-ENODEV);
  444. acpi_button_dir->owner = THIS_MODULE;
  445. result = acpi_bus_register_driver(&acpi_button_driver);
  446. if (result < 0) {
  447. remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
  448. return_VALUE(-ENODEV);
  449. }
  450. return_VALUE(0);
  451. }
  452. static void __exit
  453. acpi_button_exit (void)
  454. {
  455. ACPI_FUNCTION_TRACE("acpi_button_exit");
  456. acpi_bus_unregister_driver(&acpi_button_driver);
  457. remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
  458. return_VOID;
  459. }
  460. module_init(acpi_button_init);
  461. module_exit(acpi_button_exit);