button.c 15 KB

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