button.c 15 KB

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