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