button.c 15 KB

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