button.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * button.c - ACPI Button Driver
  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 <linux/slab.h>
  33. #include <acpi/acpi_bus.h>
  34. #include <acpi/acpi_drivers.h>
  35. #define PREFIX "ACPI: "
  36. #define ACPI_BUTTON_CLASS "button"
  37. #define ACPI_BUTTON_FILE_INFO "info"
  38. #define ACPI_BUTTON_FILE_STATE "state"
  39. #define ACPI_BUTTON_TYPE_UNKNOWN 0x00
  40. #define ACPI_BUTTON_NOTIFY_STATUS 0x80
  41. #define ACPI_BUTTON_SUBCLASS_POWER "power"
  42. #define ACPI_BUTTON_HID_POWER "PNP0C0C"
  43. #define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button"
  44. #define ACPI_BUTTON_TYPE_POWER 0x01
  45. #define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
  46. #define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
  47. #define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button"
  48. #define ACPI_BUTTON_TYPE_SLEEP 0x03
  49. #define ACPI_BUTTON_SUBCLASS_LID "lid"
  50. #define ACPI_BUTTON_HID_LID "PNP0C0D"
  51. #define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
  52. #define ACPI_BUTTON_TYPE_LID 0x05
  53. #define _COMPONENT ACPI_BUTTON_COMPONENT
  54. ACPI_MODULE_NAME("button");
  55. MODULE_AUTHOR("Paul Diefenbaugh");
  56. MODULE_DESCRIPTION("ACPI Button Driver");
  57. MODULE_LICENSE("GPL");
  58. static const struct acpi_device_id button_device_ids[] = {
  59. {ACPI_BUTTON_HID_LID, 0},
  60. {ACPI_BUTTON_HID_SLEEP, 0},
  61. {ACPI_BUTTON_HID_SLEEPF, 0},
  62. {ACPI_BUTTON_HID_POWER, 0},
  63. {ACPI_BUTTON_HID_POWERF, 0},
  64. {"", 0},
  65. };
  66. MODULE_DEVICE_TABLE(acpi, button_device_ids);
  67. static int acpi_button_add(struct acpi_device *device);
  68. static int acpi_button_remove(struct acpi_device *device, int type);
  69. static int acpi_button_resume(struct acpi_device *device);
  70. static void acpi_button_notify(struct acpi_device *device, u32 event);
  71. static int acpi_button_info_open_fs(struct inode *inode, struct file *file);
  72. static int acpi_button_state_open_fs(struct inode *inode, struct file *file);
  73. static struct acpi_driver acpi_button_driver = {
  74. .name = "button",
  75. .class = ACPI_BUTTON_CLASS,
  76. .ids = button_device_ids,
  77. .ops = {
  78. .add = acpi_button_add,
  79. .resume = acpi_button_resume,
  80. .remove = acpi_button_remove,
  81. .notify = acpi_button_notify,
  82. },
  83. };
  84. struct acpi_button {
  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. .owner = THIS_MODULE,
  92. .open = acpi_button_info_open_fs,
  93. .read = seq_read,
  94. .llseek = seq_lseek,
  95. .release = single_release,
  96. };
  97. static const struct file_operations acpi_button_state_fops = {
  98. .owner = THIS_MODULE,
  99. .open = acpi_button_state_open_fs,
  100. .read = seq_read,
  101. .llseek = seq_lseek,
  102. .release = single_release,
  103. };
  104. static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier);
  105. static struct acpi_device *lid_device;
  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_device *device = seq->private;
  113. seq_printf(seq, "type: %s\n",
  114. acpi_device_name(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_device *device = seq->private;
  124. acpi_status status;
  125. unsigned long long state;
  126. status = acpi_evaluate_integer(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 acpi_button *button = acpi_driver_data(device);
  142. struct proc_dir_entry *entry = NULL;
  143. switch (button->type) {
  144. case ACPI_BUTTON_TYPE_POWER:
  145. if (!acpi_power_dir)
  146. acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
  147. acpi_button_dir);
  148. entry = acpi_power_dir;
  149. break;
  150. case ACPI_BUTTON_TYPE_SLEEP:
  151. if (!acpi_sleep_dir)
  152. acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
  153. acpi_button_dir);
  154. entry = acpi_sleep_dir;
  155. break;
  156. case ACPI_BUTTON_TYPE_LID:
  157. if (!acpi_lid_dir)
  158. acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
  159. acpi_button_dir);
  160. entry = acpi_lid_dir;
  161. break;
  162. }
  163. if (!entry)
  164. return -ENODEV;
  165. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
  166. if (!acpi_device_dir(device))
  167. return -ENODEV;
  168. /* 'info' [R] */
  169. entry = proc_create_data(ACPI_BUTTON_FILE_INFO,
  170. S_IRUGO, acpi_device_dir(device),
  171. &acpi_button_info_fops, device);
  172. if (!entry)
  173. return -ENODEV;
  174. /* show lid state [R] */
  175. if (button->type == ACPI_BUTTON_TYPE_LID) {
  176. entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
  177. S_IRUGO, acpi_device_dir(device),
  178. &acpi_button_state_fops, device);
  179. if (!entry)
  180. return -ENODEV;
  181. }
  182. return 0;
  183. }
  184. static int acpi_button_remove_fs(struct acpi_device *device)
  185. {
  186. struct acpi_button *button = acpi_driver_data(device);
  187. if (acpi_device_dir(device)) {
  188. if (button->type == ACPI_BUTTON_TYPE_LID)
  189. remove_proc_entry(ACPI_BUTTON_FILE_STATE,
  190. acpi_device_dir(device));
  191. remove_proc_entry(ACPI_BUTTON_FILE_INFO,
  192. acpi_device_dir(device));
  193. remove_proc_entry(acpi_device_bid(device),
  194. acpi_device_dir(device)->parent);
  195. acpi_device_dir(device) = NULL;
  196. }
  197. return 0;
  198. }
  199. /* --------------------------------------------------------------------------
  200. Driver Interface
  201. -------------------------------------------------------------------------- */
  202. int acpi_lid_notifier_register(struct notifier_block *nb)
  203. {
  204. return blocking_notifier_chain_register(&acpi_lid_notifier, nb);
  205. }
  206. EXPORT_SYMBOL(acpi_lid_notifier_register);
  207. int acpi_lid_notifier_unregister(struct notifier_block *nb)
  208. {
  209. return blocking_notifier_chain_unregister(&acpi_lid_notifier, nb);
  210. }
  211. EXPORT_SYMBOL(acpi_lid_notifier_unregister);
  212. int acpi_lid_open(void)
  213. {
  214. acpi_status status;
  215. unsigned long long state;
  216. if (!lid_device)
  217. return -ENODEV;
  218. status = acpi_evaluate_integer(lid_device->handle, "_LID", NULL,
  219. &state);
  220. if (ACPI_FAILURE(status))
  221. return -ENODEV;
  222. return !!state;
  223. }
  224. EXPORT_SYMBOL(acpi_lid_open);
  225. static int acpi_lid_send_state(struct acpi_device *device)
  226. {
  227. struct acpi_button *button = acpi_driver_data(device);
  228. unsigned long long state;
  229. acpi_status status;
  230. int ret;
  231. status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
  232. if (ACPI_FAILURE(status))
  233. return -ENODEV;
  234. /* input layer checks if event is redundant */
  235. input_report_switch(button->input, SW_LID, !state);
  236. input_sync(button->input);
  237. ret = blocking_notifier_call_chain(&acpi_lid_notifier, state, device);
  238. if (ret == NOTIFY_DONE)
  239. ret = blocking_notifier_call_chain(&acpi_lid_notifier, state,
  240. device);
  241. if (ret == NOTIFY_DONE || ret == NOTIFY_OK) {
  242. /*
  243. * It is also regarded as success if the notifier_chain
  244. * returns NOTIFY_OK or NOTIFY_DONE.
  245. */
  246. ret = 0;
  247. }
  248. return ret;
  249. }
  250. static void acpi_button_notify(struct acpi_device *device, u32 event)
  251. {
  252. struct acpi_button *button = acpi_driver_data(device);
  253. struct input_dev *input;
  254. switch (event) {
  255. case ACPI_FIXED_HARDWARE_EVENT:
  256. event = ACPI_BUTTON_NOTIFY_STATUS;
  257. /* fall through */
  258. case ACPI_BUTTON_NOTIFY_STATUS:
  259. input = button->input;
  260. if (button->type == ACPI_BUTTON_TYPE_LID) {
  261. acpi_lid_send_state(device);
  262. } else {
  263. int keycode = test_bit(KEY_SLEEP, input->keybit) ?
  264. KEY_SLEEP : KEY_POWER;
  265. input_report_key(input, keycode, 1);
  266. input_sync(input);
  267. input_report_key(input, keycode, 0);
  268. input_sync(input);
  269. }
  270. acpi_bus_generate_proc_event(device, event, ++button->pushed);
  271. break;
  272. default:
  273. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  274. "Unsupported event [0x%x]\n", event));
  275. break;
  276. }
  277. }
  278. static int acpi_button_resume(struct acpi_device *device)
  279. {
  280. struct acpi_button *button = acpi_driver_data(device);
  281. if (button->type == ACPI_BUTTON_TYPE_LID)
  282. return acpi_lid_send_state(device);
  283. return 0;
  284. }
  285. static int acpi_button_add(struct acpi_device *device)
  286. {
  287. struct acpi_button *button;
  288. struct input_dev *input;
  289. const char *hid = acpi_device_hid(device);
  290. char *name, *class;
  291. int error;
  292. button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
  293. if (!button)
  294. return -ENOMEM;
  295. device->driver_data = button;
  296. button->input = input = input_allocate_device();
  297. if (!input) {
  298. error = -ENOMEM;
  299. goto err_free_button;
  300. }
  301. name = acpi_device_name(device);
  302. class = acpi_device_class(device);
  303. if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
  304. !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
  305. button->type = ACPI_BUTTON_TYPE_POWER;
  306. strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
  307. sprintf(class, "%s/%s",
  308. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
  309. } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
  310. !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
  311. button->type = ACPI_BUTTON_TYPE_SLEEP;
  312. strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
  313. sprintf(class, "%s/%s",
  314. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
  315. } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
  316. button->type = ACPI_BUTTON_TYPE_LID;
  317. strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
  318. sprintf(class, "%s/%s",
  319. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
  320. } else {
  321. printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
  322. error = -ENODEV;
  323. goto err_free_input;
  324. }
  325. error = acpi_button_add_fs(device);
  326. if (error)
  327. goto err_free_input;
  328. snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
  329. input->name = name;
  330. input->phys = button->phys;
  331. input->id.bustype = BUS_HOST;
  332. input->id.product = button->type;
  333. input->dev.parent = &device->dev;
  334. switch (button->type) {
  335. case ACPI_BUTTON_TYPE_POWER:
  336. input->evbit[0] = BIT_MASK(EV_KEY);
  337. set_bit(KEY_POWER, input->keybit);
  338. break;
  339. case ACPI_BUTTON_TYPE_SLEEP:
  340. input->evbit[0] = BIT_MASK(EV_KEY);
  341. set_bit(KEY_SLEEP, input->keybit);
  342. break;
  343. case ACPI_BUTTON_TYPE_LID:
  344. input->evbit[0] = BIT_MASK(EV_SW);
  345. set_bit(SW_LID, input->swbit);
  346. break;
  347. }
  348. error = input_register_device(input);
  349. if (error)
  350. goto err_remove_fs;
  351. if (button->type == ACPI_BUTTON_TYPE_LID) {
  352. acpi_lid_send_state(device);
  353. /*
  354. * This assumes there's only one lid device, or if there are
  355. * more we only care about the last one...
  356. */
  357. lid_device = device;
  358. }
  359. if (device->wakeup.flags.valid) {
  360. /* Button's GPE is run-wake GPE */
  361. acpi_enable_gpe(device->wakeup.gpe_device,
  362. device->wakeup.gpe_number);
  363. device->wakeup.run_wake_count++;
  364. device->wakeup.state.enabled = 1;
  365. }
  366. printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
  367. return 0;
  368. err_remove_fs:
  369. acpi_button_remove_fs(device);
  370. err_free_input:
  371. input_free_device(input);
  372. err_free_button:
  373. kfree(button);
  374. return error;
  375. }
  376. static int acpi_button_remove(struct acpi_device *device, int type)
  377. {
  378. struct acpi_button *button = acpi_driver_data(device);
  379. if (device->wakeup.flags.valid) {
  380. acpi_disable_gpe(device->wakeup.gpe_device,
  381. device->wakeup.gpe_number);
  382. device->wakeup.run_wake_count--;
  383. device->wakeup.state.enabled = 0;
  384. }
  385. acpi_button_remove_fs(device);
  386. input_unregister_device(button->input);
  387. kfree(button);
  388. return 0;
  389. }
  390. static int __init acpi_button_init(void)
  391. {
  392. int result;
  393. acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
  394. if (!acpi_button_dir)
  395. return -ENODEV;
  396. result = acpi_bus_register_driver(&acpi_button_driver);
  397. if (result < 0) {
  398. remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
  399. return -ENODEV;
  400. }
  401. return 0;
  402. }
  403. static void __exit acpi_button_exit(void)
  404. {
  405. acpi_bus_unregister_driver(&acpi_button_driver);
  406. if (acpi_power_dir)
  407. remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
  408. if (acpi_sleep_dir)
  409. remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
  410. if (acpi_lid_dir)
  411. remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
  412. remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
  413. }
  414. module_init(acpi_button_init);
  415. module_exit(acpi_button_exit);