button.c 14 KB

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