button.c 14 KB

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