button.c 14 KB

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