button.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/types.h>
  25. #include <linux/proc_fs.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/input.h>
  28. #include <linux/slab.h>
  29. #include <linux/acpi.h>
  30. #include <acpi/button.h>
  31. #define PREFIX "ACPI: "
  32. #define ACPI_BUTTON_CLASS "button"
  33. #define ACPI_BUTTON_FILE_INFO "info"
  34. #define ACPI_BUTTON_FILE_STATE "state"
  35. #define ACPI_BUTTON_TYPE_UNKNOWN 0x00
  36. #define ACPI_BUTTON_NOTIFY_STATUS 0x80
  37. #define ACPI_BUTTON_SUBCLASS_POWER "power"
  38. #define ACPI_BUTTON_HID_POWER "PNP0C0C"
  39. #define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button"
  40. #define ACPI_BUTTON_TYPE_POWER 0x01
  41. #define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
  42. #define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
  43. #define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button"
  44. #define ACPI_BUTTON_TYPE_SLEEP 0x03
  45. #define ACPI_BUTTON_SUBCLASS_LID "lid"
  46. #define ACPI_BUTTON_HID_LID "PNP0C0D"
  47. #define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
  48. #define ACPI_BUTTON_TYPE_LID 0x05
  49. #define _COMPONENT ACPI_BUTTON_COMPONENT
  50. ACPI_MODULE_NAME("button");
  51. MODULE_AUTHOR("Paul Diefenbaugh");
  52. MODULE_DESCRIPTION("ACPI Button Driver");
  53. MODULE_LICENSE("GPL");
  54. static const struct acpi_device_id button_device_ids[] = {
  55. {ACPI_BUTTON_HID_LID, 0},
  56. {ACPI_BUTTON_HID_SLEEP, 0},
  57. {ACPI_BUTTON_HID_SLEEPF, 0},
  58. {ACPI_BUTTON_HID_POWER, 0},
  59. {ACPI_BUTTON_HID_POWERF, 0},
  60. {"", 0},
  61. };
  62. MODULE_DEVICE_TABLE(acpi, button_device_ids);
  63. static int acpi_button_add(struct acpi_device *device);
  64. static int acpi_button_remove(struct acpi_device *device);
  65. static void acpi_button_notify(struct acpi_device *device, u32 event);
  66. #ifdef CONFIG_PM_SLEEP
  67. static int acpi_button_suspend(struct device *dev);
  68. static int acpi_button_resume(struct device *dev);
  69. #else
  70. #define acpi_button_suspend NULL
  71. #define acpi_button_resume NULL
  72. #endif
  73. static SIMPLE_DEV_PM_OPS(acpi_button_pm, acpi_button_suspend, acpi_button_resume);
  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. .notify = acpi_button_notify,
  82. },
  83. .drv.pm = &acpi_button_pm,
  84. };
  85. struct acpi_button {
  86. unsigned int type;
  87. struct input_dev *input;
  88. char phys[32]; /* for input device */
  89. unsigned long pushed;
  90. bool suspended;
  91. };
  92. static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier);
  93. static struct acpi_device *lid_device;
  94. /* --------------------------------------------------------------------------
  95. FS Interface (/proc)
  96. -------------------------------------------------------------------------- */
  97. static struct proc_dir_entry *acpi_button_dir;
  98. static struct proc_dir_entry *acpi_lid_dir;
  99. static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
  100. {
  101. struct acpi_device *device = seq->private;
  102. acpi_status status;
  103. unsigned long long state;
  104. status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
  105. seq_printf(seq, "state: %s\n",
  106. ACPI_FAILURE(status) ? "unsupported" :
  107. (state ? "open" : "closed"));
  108. return 0;
  109. }
  110. static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
  111. {
  112. return single_open(file, acpi_button_state_seq_show, PDE_DATA(inode));
  113. }
  114. static const struct file_operations acpi_button_state_fops = {
  115. .owner = THIS_MODULE,
  116. .open = acpi_button_state_open_fs,
  117. .read = seq_read,
  118. .llseek = seq_lseek,
  119. .release = single_release,
  120. };
  121. static int acpi_button_add_fs(struct acpi_device *device)
  122. {
  123. struct acpi_button *button = acpi_driver_data(device);
  124. struct proc_dir_entry *entry = NULL;
  125. int ret = 0;
  126. /* procfs I/F for ACPI lid device only */
  127. if (button->type != ACPI_BUTTON_TYPE_LID)
  128. return 0;
  129. if (acpi_button_dir || acpi_lid_dir) {
  130. printk(KERN_ERR PREFIX "More than one Lid device found!\n");
  131. return -EEXIST;
  132. }
  133. /* create /proc/acpi/button */
  134. acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
  135. if (!acpi_button_dir)
  136. return -ENODEV;
  137. /* create /proc/acpi/button/lid */
  138. acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
  139. if (!acpi_lid_dir) {
  140. ret = -ENODEV;
  141. goto remove_button_dir;
  142. }
  143. /* create /proc/acpi/button/lid/LID/ */
  144. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), acpi_lid_dir);
  145. if (!acpi_device_dir(device)) {
  146. ret = -ENODEV;
  147. goto remove_lid_dir;
  148. }
  149. /* create /proc/acpi/button/lid/LID/state */
  150. entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
  151. S_IRUGO, acpi_device_dir(device),
  152. &acpi_button_state_fops, device);
  153. if (!entry) {
  154. ret = -ENODEV;
  155. goto remove_dev_dir;
  156. }
  157. done:
  158. return ret;
  159. remove_dev_dir:
  160. remove_proc_entry(acpi_device_bid(device),
  161. acpi_lid_dir);
  162. acpi_device_dir(device) = NULL;
  163. remove_lid_dir:
  164. remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
  165. remove_button_dir:
  166. remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
  167. goto done;
  168. }
  169. static int acpi_button_remove_fs(struct acpi_device *device)
  170. {
  171. struct acpi_button *button = acpi_driver_data(device);
  172. if (button->type != ACPI_BUTTON_TYPE_LID)
  173. return 0;
  174. remove_proc_entry(ACPI_BUTTON_FILE_STATE,
  175. acpi_device_dir(device));
  176. remove_proc_entry(acpi_device_bid(device),
  177. acpi_lid_dir);
  178. acpi_device_dir(device) = NULL;
  179. remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
  180. remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
  181. return 0;
  182. }
  183. /* --------------------------------------------------------------------------
  184. Driver Interface
  185. -------------------------------------------------------------------------- */
  186. int acpi_lid_notifier_register(struct notifier_block *nb)
  187. {
  188. return blocking_notifier_chain_register(&acpi_lid_notifier, nb);
  189. }
  190. EXPORT_SYMBOL(acpi_lid_notifier_register);
  191. int acpi_lid_notifier_unregister(struct notifier_block *nb)
  192. {
  193. return blocking_notifier_chain_unregister(&acpi_lid_notifier, nb);
  194. }
  195. EXPORT_SYMBOL(acpi_lid_notifier_unregister);
  196. int acpi_lid_open(void)
  197. {
  198. acpi_status status;
  199. unsigned long long state;
  200. if (!lid_device)
  201. return -ENODEV;
  202. status = acpi_evaluate_integer(lid_device->handle, "_LID", NULL,
  203. &state);
  204. if (ACPI_FAILURE(status))
  205. return -ENODEV;
  206. return !!state;
  207. }
  208. EXPORT_SYMBOL(acpi_lid_open);
  209. static int acpi_lid_send_state(struct acpi_device *device)
  210. {
  211. struct acpi_button *button = acpi_driver_data(device);
  212. unsigned long long state;
  213. acpi_status status;
  214. int ret;
  215. status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
  216. if (ACPI_FAILURE(status))
  217. return -ENODEV;
  218. /* input layer checks if event is redundant */
  219. input_report_switch(button->input, SW_LID, !state);
  220. input_sync(button->input);
  221. if (state)
  222. pm_wakeup_event(&device->dev, 0);
  223. ret = blocking_notifier_call_chain(&acpi_lid_notifier, state, device);
  224. if (ret == NOTIFY_DONE)
  225. ret = blocking_notifier_call_chain(&acpi_lid_notifier, state,
  226. device);
  227. if (ret == NOTIFY_DONE || ret == NOTIFY_OK) {
  228. /*
  229. * It is also regarded as success if the notifier_chain
  230. * returns NOTIFY_OK or NOTIFY_DONE.
  231. */
  232. ret = 0;
  233. }
  234. return ret;
  235. }
  236. static void acpi_button_notify(struct acpi_device *device, u32 event)
  237. {
  238. struct acpi_button *button = acpi_driver_data(device);
  239. struct input_dev *input;
  240. switch (event) {
  241. case ACPI_FIXED_HARDWARE_EVENT:
  242. event = ACPI_BUTTON_NOTIFY_STATUS;
  243. /* fall through */
  244. case ACPI_BUTTON_NOTIFY_STATUS:
  245. input = button->input;
  246. if (button->type == ACPI_BUTTON_TYPE_LID) {
  247. acpi_lid_send_state(device);
  248. } else {
  249. int keycode;
  250. pm_wakeup_event(&device->dev, 0);
  251. if (button->suspended)
  252. break;
  253. keycode = test_bit(KEY_SLEEP, input->keybit) ?
  254. KEY_SLEEP : KEY_POWER;
  255. input_report_key(input, keycode, 1);
  256. input_sync(input);
  257. input_report_key(input, keycode, 0);
  258. input_sync(input);
  259. acpi_bus_generate_netlink_event(
  260. device->pnp.device_class,
  261. dev_name(&device->dev),
  262. event, ++button->pushed);
  263. }
  264. break;
  265. default:
  266. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  267. "Unsupported event [0x%x]\n", event));
  268. break;
  269. }
  270. }
  271. #ifdef CONFIG_PM_SLEEP
  272. static int acpi_button_suspend(struct device *dev)
  273. {
  274. struct acpi_device *device = to_acpi_device(dev);
  275. struct acpi_button *button = acpi_driver_data(device);
  276. button->suspended = true;
  277. return 0;
  278. }
  279. static int acpi_button_resume(struct device *dev)
  280. {
  281. struct acpi_device *device = to_acpi_device(dev);
  282. struct acpi_button *button = acpi_driver_data(device);
  283. button->suspended = false;
  284. if (button->type == ACPI_BUTTON_TYPE_LID)
  285. return acpi_lid_send_state(device);
  286. return 0;
  287. }
  288. #endif
  289. static int acpi_button_add(struct acpi_device *device)
  290. {
  291. struct acpi_button *button;
  292. struct input_dev *input;
  293. const char *hid = acpi_device_hid(device);
  294. char *name, *class;
  295. int error;
  296. button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
  297. if (!button)
  298. return -ENOMEM;
  299. device->driver_data = button;
  300. button->input = input = input_allocate_device();
  301. if (!input) {
  302. error = -ENOMEM;
  303. goto err_free_button;
  304. }
  305. name = acpi_device_name(device);
  306. class = acpi_device_class(device);
  307. if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
  308. !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
  309. button->type = ACPI_BUTTON_TYPE_POWER;
  310. strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
  311. sprintf(class, "%s/%s",
  312. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
  313. } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
  314. !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
  315. button->type = ACPI_BUTTON_TYPE_SLEEP;
  316. strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
  317. sprintf(class, "%s/%s",
  318. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
  319. } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
  320. button->type = ACPI_BUTTON_TYPE_LID;
  321. strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
  322. sprintf(class, "%s/%s",
  323. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
  324. } else {
  325. printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
  326. error = -ENODEV;
  327. goto err_free_input;
  328. }
  329. error = acpi_button_add_fs(device);
  330. if (error)
  331. goto err_free_input;
  332. snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
  333. input->name = name;
  334. input->phys = button->phys;
  335. input->id.bustype = BUS_HOST;
  336. input->id.product = button->type;
  337. input->dev.parent = &device->dev;
  338. switch (button->type) {
  339. case ACPI_BUTTON_TYPE_POWER:
  340. input_set_capability(input, EV_KEY, KEY_POWER);
  341. break;
  342. case ACPI_BUTTON_TYPE_SLEEP:
  343. input_set_capability(input, EV_KEY, KEY_SLEEP);
  344. break;
  345. case ACPI_BUTTON_TYPE_LID:
  346. input_set_capability(input, EV_SW, SW_LID);
  347. break;
  348. }
  349. error = input_register_device(input);
  350. if (error)
  351. goto err_remove_fs;
  352. if (button->type == ACPI_BUTTON_TYPE_LID) {
  353. acpi_lid_send_state(device);
  354. /*
  355. * This assumes there's only one lid device, or if there are
  356. * more we only care about the last one...
  357. */
  358. lid_device = device;
  359. }
  360. printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
  361. return 0;
  362. err_remove_fs:
  363. acpi_button_remove_fs(device);
  364. err_free_input:
  365. input_free_device(input);
  366. err_free_button:
  367. kfree(button);
  368. return error;
  369. }
  370. static int acpi_button_remove(struct acpi_device *device)
  371. {
  372. struct acpi_button *button = acpi_driver_data(device);
  373. acpi_button_remove_fs(device);
  374. input_unregister_device(button->input);
  375. kfree(button);
  376. return 0;
  377. }
  378. module_acpi_driver(acpi_button_driver);