button.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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 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 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. input_sync(button->input);
  226. return 0;
  227. }
  228. static void acpi_button_notify(acpi_handle handle, u32 event, void *data)
  229. {
  230. struct acpi_button *button = data;
  231. struct input_dev *input;
  232. if (!button || !button->device)
  233. return;
  234. switch (event) {
  235. case ACPI_BUTTON_NOTIFY_STATUS:
  236. input = button->input;
  237. if (button->type == ACPI_BUTTON_TYPE_LID) {
  238. acpi_lid_send_state(button);
  239. } else {
  240. int keycode = test_bit(KEY_SLEEP, input->keybit) ?
  241. KEY_SLEEP : KEY_POWER;
  242. input_report_key(input, keycode, 1);
  243. input_sync(input);
  244. input_report_key(input, keycode, 0);
  245. input_sync(input);
  246. }
  247. acpi_bus_generate_proc_event(button->device, event,
  248. ++button->pushed);
  249. break;
  250. default:
  251. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  252. "Unsupported event [0x%x]\n", event));
  253. break;
  254. }
  255. return;
  256. }
  257. static acpi_status acpi_button_notify_fixed(void *data)
  258. {
  259. struct acpi_button *button = data;
  260. if (!button)
  261. return AE_BAD_PARAMETER;
  262. acpi_button_notify(button->device->handle, ACPI_BUTTON_NOTIFY_STATUS, button);
  263. return AE_OK;
  264. }
  265. static int acpi_button_install_notify_handlers(struct acpi_button *button)
  266. {
  267. acpi_status status;
  268. switch (button->type) {
  269. case ACPI_BUTTON_TYPE_POWERF:
  270. status =
  271. acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
  272. acpi_button_notify_fixed,
  273. button);
  274. break;
  275. case ACPI_BUTTON_TYPE_SLEEPF:
  276. status =
  277. acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
  278. acpi_button_notify_fixed,
  279. button);
  280. break;
  281. default:
  282. status = acpi_install_notify_handler(button->device->handle,
  283. ACPI_DEVICE_NOTIFY,
  284. acpi_button_notify,
  285. button);
  286. break;
  287. }
  288. return ACPI_FAILURE(status) ? -ENODEV : 0;
  289. }
  290. static int acpi_button_resume(struct acpi_device *device)
  291. {
  292. struct acpi_button *button;
  293. if (!device)
  294. return -EINVAL;
  295. button = acpi_driver_data(device);
  296. if (button && button->type == ACPI_BUTTON_TYPE_LID)
  297. return acpi_lid_send_state(button);
  298. return 0;
  299. }
  300. static void acpi_button_remove_notify_handlers(struct acpi_button *button)
  301. {
  302. switch (button->type) {
  303. case ACPI_BUTTON_TYPE_POWERF:
  304. acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
  305. acpi_button_notify_fixed);
  306. break;
  307. case ACPI_BUTTON_TYPE_SLEEPF:
  308. acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
  309. acpi_button_notify_fixed);
  310. break;
  311. default:
  312. acpi_remove_notify_handler(button->device->handle,
  313. ACPI_DEVICE_NOTIFY,
  314. acpi_button_notify);
  315. break;
  316. }
  317. }
  318. static int acpi_button_add(struct acpi_device *device)
  319. {
  320. int error;
  321. struct acpi_button *button;
  322. struct input_dev *input;
  323. if (!device)
  324. return -EINVAL;
  325. button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
  326. if (!button)
  327. return -ENOMEM;
  328. button->device = device;
  329. device->driver_data = button;
  330. button->input = input = input_allocate_device();
  331. if (!input) {
  332. error = -ENOMEM;
  333. goto err_free_button;
  334. }
  335. /*
  336. * Determine the button type (via hid), as fixed-feature buttons
  337. * need to be handled a bit differently than generic-space.
  338. */
  339. if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWER)) {
  340. button->type = ACPI_BUTTON_TYPE_POWER;
  341. strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_POWER);
  342. sprintf(acpi_device_class(device), "%s/%s",
  343. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
  344. } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF)) {
  345. button->type = ACPI_BUTTON_TYPE_POWERF;
  346. strcpy(acpi_device_name(device),
  347. ACPI_BUTTON_DEVICE_NAME_POWERF);
  348. sprintf(acpi_device_class(device), "%s/%s",
  349. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
  350. } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEP)) {
  351. button->type = ACPI_BUTTON_TYPE_SLEEP;
  352. strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_SLEEP);
  353. sprintf(acpi_device_class(device), "%s/%s",
  354. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
  355. } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF)) {
  356. button->type = ACPI_BUTTON_TYPE_SLEEPF;
  357. strcpy(acpi_device_name(device),
  358. ACPI_BUTTON_DEVICE_NAME_SLEEPF);
  359. sprintf(acpi_device_class(device), "%s/%s",
  360. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
  361. } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_LID)) {
  362. button->type = ACPI_BUTTON_TYPE_LID;
  363. strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_LID);
  364. sprintf(acpi_device_class(device), "%s/%s",
  365. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
  366. } else {
  367. printk(KERN_ERR PREFIX "Unsupported hid [%s]\n",
  368. acpi_device_hid(device));
  369. error = -ENODEV;
  370. goto err_free_input;
  371. }
  372. error = acpi_button_add_fs(device);
  373. if (error)
  374. goto err_free_input;
  375. error = acpi_button_install_notify_handlers(button);
  376. if (error)
  377. goto err_remove_fs;
  378. snprintf(button->phys, sizeof(button->phys),
  379. "%s/button/input0", acpi_device_hid(device));
  380. input->name = acpi_device_name(device);
  381. input->phys = button->phys;
  382. input->id.bustype = BUS_HOST;
  383. input->id.product = button->type;
  384. input->dev.parent = &device->dev;
  385. switch (button->type) {
  386. case ACPI_BUTTON_TYPE_POWER:
  387. case ACPI_BUTTON_TYPE_POWERF:
  388. input->evbit[0] = BIT_MASK(EV_KEY);
  389. set_bit(KEY_POWER, input->keybit);
  390. break;
  391. case ACPI_BUTTON_TYPE_SLEEP:
  392. case ACPI_BUTTON_TYPE_SLEEPF:
  393. input->evbit[0] = BIT_MASK(EV_KEY);
  394. set_bit(KEY_SLEEP, input->keybit);
  395. break;
  396. case ACPI_BUTTON_TYPE_LID:
  397. input->evbit[0] = BIT_MASK(EV_SW);
  398. set_bit(SW_LID, input->swbit);
  399. break;
  400. }
  401. error = input_register_device(input);
  402. if (error)
  403. goto err_remove_handlers;
  404. if (button->type == ACPI_BUTTON_TYPE_LID)
  405. acpi_lid_send_state(button);
  406. if (device->wakeup.flags.valid) {
  407. /* Button's GPE is run-wake GPE */
  408. acpi_set_gpe_type(device->wakeup.gpe_device,
  409. device->wakeup.gpe_number,
  410. ACPI_GPE_TYPE_WAKE_RUN);
  411. acpi_enable_gpe(device->wakeup.gpe_device,
  412. device->wakeup.gpe_number, ACPI_NOT_ISR);
  413. device->wakeup.state.enabled = 1;
  414. }
  415. printk(KERN_INFO PREFIX "%s [%s]\n",
  416. acpi_device_name(device), acpi_device_bid(device));
  417. return 0;
  418. err_remove_handlers:
  419. acpi_button_remove_notify_handlers(button);
  420. err_remove_fs:
  421. acpi_button_remove_fs(device);
  422. err_free_input:
  423. input_free_device(input);
  424. err_free_button:
  425. kfree(button);
  426. return error;
  427. }
  428. static int acpi_button_remove(struct acpi_device *device, int type)
  429. {
  430. struct acpi_button *button;
  431. if (!device || !acpi_driver_data(device))
  432. return -EINVAL;
  433. button = acpi_driver_data(device);
  434. acpi_button_remove_notify_handlers(button);
  435. acpi_button_remove_fs(device);
  436. input_unregister_device(button->input);
  437. kfree(button);
  438. return 0;
  439. }
  440. static int __init acpi_button_init(void)
  441. {
  442. int result;
  443. acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
  444. if (!acpi_button_dir)
  445. return -ENODEV;
  446. acpi_button_dir->owner = THIS_MODULE;
  447. result = acpi_bus_register_driver(&acpi_button_driver);
  448. if (result < 0) {
  449. remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
  450. return -ENODEV;
  451. }
  452. return 0;
  453. }
  454. static void __exit acpi_button_exit(void)
  455. {
  456. acpi_bus_unregister_driver(&acpi_button_driver);
  457. if (acpi_power_dir)
  458. remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
  459. if (acpi_sleep_dir)
  460. remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
  461. if (acpi_lid_dir)
  462. remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
  463. remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
  464. }
  465. module_init(acpi_button_init);
  466. module_exit(acpi_button_exit);