button.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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 ACPI_BUTTON_LID_INIT_IGNORE 0x00
  50. #define ACPI_BUTTON_LID_INIT_OPEN 0x01
  51. #define ACPI_BUTTON_LID_INIT_METHOD 0x02
  52. #define _COMPONENT ACPI_BUTTON_COMPONENT
  53. ACPI_MODULE_NAME("button");
  54. MODULE_AUTHOR("Paul Diefenbaugh");
  55. MODULE_DESCRIPTION("ACPI Button Driver");
  56. MODULE_LICENSE("GPL");
  57. static const struct acpi_device_id button_device_ids[] = {
  58. {ACPI_BUTTON_HID_LID, 0},
  59. {ACPI_BUTTON_HID_SLEEP, 0},
  60. {ACPI_BUTTON_HID_SLEEPF, 0},
  61. {ACPI_BUTTON_HID_POWER, 0},
  62. {ACPI_BUTTON_HID_POWERF, 0},
  63. {"", 0},
  64. };
  65. MODULE_DEVICE_TABLE(acpi, button_device_ids);
  66. static int acpi_button_add(struct acpi_device *device);
  67. static int acpi_button_remove(struct acpi_device *device);
  68. static void acpi_button_notify(struct acpi_device *device, u32 event);
  69. #ifdef CONFIG_PM_SLEEP
  70. static int acpi_button_suspend(struct device *dev);
  71. static int acpi_button_resume(struct device *dev);
  72. #else
  73. #define acpi_button_suspend NULL
  74. #define acpi_button_resume NULL
  75. #endif
  76. static SIMPLE_DEV_PM_OPS(acpi_button_pm, acpi_button_suspend, acpi_button_resume);
  77. static struct acpi_driver acpi_button_driver = {
  78. .name = "button",
  79. .class = ACPI_BUTTON_CLASS,
  80. .ids = button_device_ids,
  81. .ops = {
  82. .add = acpi_button_add,
  83. .remove = acpi_button_remove,
  84. .notify = acpi_button_notify,
  85. },
  86. .drv.pm = &acpi_button_pm,
  87. };
  88. struct acpi_button {
  89. unsigned int type;
  90. struct input_dev *input;
  91. char phys[32]; /* for input device */
  92. unsigned long pushed;
  93. bool suspended;
  94. };
  95. static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier);
  96. static struct acpi_device *lid_device;
  97. static u8 lid_init_state = ACPI_BUTTON_LID_INIT_METHOD;
  98. /* --------------------------------------------------------------------------
  99. FS Interface (/proc)
  100. -------------------------------------------------------------------------- */
  101. static struct proc_dir_entry *acpi_button_dir;
  102. static struct proc_dir_entry *acpi_lid_dir;
  103. static int acpi_lid_evaluate_state(struct acpi_device *device)
  104. {
  105. unsigned long long lid_state;
  106. acpi_status status;
  107. status = acpi_evaluate_integer(device->handle, "_LID", NULL, &lid_state);
  108. if (ACPI_FAILURE(status))
  109. return -ENODEV;
  110. return lid_state ? 1 : 0;
  111. }
  112. static int acpi_lid_notify_state(struct acpi_device *device, int state)
  113. {
  114. struct acpi_button *button = acpi_driver_data(device);
  115. int ret;
  116. /* input layer checks if event is redundant */
  117. input_report_switch(button->input, SW_LID, !state);
  118. input_sync(button->input);
  119. if (state)
  120. pm_wakeup_event(&device->dev, 0);
  121. ret = blocking_notifier_call_chain(&acpi_lid_notifier, state, device);
  122. if (ret == NOTIFY_DONE)
  123. ret = blocking_notifier_call_chain(&acpi_lid_notifier, state,
  124. device);
  125. if (ret == NOTIFY_DONE || ret == NOTIFY_OK) {
  126. /*
  127. * It is also regarded as success if the notifier_chain
  128. * returns NOTIFY_OK or NOTIFY_DONE.
  129. */
  130. ret = 0;
  131. }
  132. return ret;
  133. }
  134. static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
  135. {
  136. struct acpi_device *device = seq->private;
  137. int state;
  138. state = acpi_lid_evaluate_state(device);
  139. seq_printf(seq, "state: %s\n",
  140. state < 0 ? "unsupported" : (state ? "open" : "closed"));
  141. return 0;
  142. }
  143. static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
  144. {
  145. return single_open(file, acpi_button_state_seq_show, PDE_DATA(inode));
  146. }
  147. static const struct file_operations acpi_button_state_fops = {
  148. .owner = THIS_MODULE,
  149. .open = acpi_button_state_open_fs,
  150. .read = seq_read,
  151. .llseek = seq_lseek,
  152. .release = single_release,
  153. };
  154. static int acpi_button_add_fs(struct acpi_device *device)
  155. {
  156. struct acpi_button *button = acpi_driver_data(device);
  157. struct proc_dir_entry *entry = NULL;
  158. int ret = 0;
  159. /* procfs I/F for ACPI lid device only */
  160. if (button->type != ACPI_BUTTON_TYPE_LID)
  161. return 0;
  162. if (acpi_button_dir || acpi_lid_dir) {
  163. printk(KERN_ERR PREFIX "More than one Lid device found!\n");
  164. return -EEXIST;
  165. }
  166. /* create /proc/acpi/button */
  167. acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
  168. if (!acpi_button_dir)
  169. return -ENODEV;
  170. /* create /proc/acpi/button/lid */
  171. acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
  172. if (!acpi_lid_dir) {
  173. ret = -ENODEV;
  174. goto remove_button_dir;
  175. }
  176. /* create /proc/acpi/button/lid/LID/ */
  177. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), acpi_lid_dir);
  178. if (!acpi_device_dir(device)) {
  179. ret = -ENODEV;
  180. goto remove_lid_dir;
  181. }
  182. /* create /proc/acpi/button/lid/LID/state */
  183. entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
  184. S_IRUGO, acpi_device_dir(device),
  185. &acpi_button_state_fops, device);
  186. if (!entry) {
  187. ret = -ENODEV;
  188. goto remove_dev_dir;
  189. }
  190. done:
  191. return ret;
  192. remove_dev_dir:
  193. remove_proc_entry(acpi_device_bid(device),
  194. acpi_lid_dir);
  195. acpi_device_dir(device) = NULL;
  196. remove_lid_dir:
  197. remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
  198. acpi_lid_dir = NULL;
  199. remove_button_dir:
  200. remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
  201. acpi_button_dir = NULL;
  202. goto done;
  203. }
  204. static int acpi_button_remove_fs(struct acpi_device *device)
  205. {
  206. struct acpi_button *button = acpi_driver_data(device);
  207. if (button->type != ACPI_BUTTON_TYPE_LID)
  208. return 0;
  209. remove_proc_entry(ACPI_BUTTON_FILE_STATE,
  210. acpi_device_dir(device));
  211. remove_proc_entry(acpi_device_bid(device),
  212. acpi_lid_dir);
  213. acpi_device_dir(device) = NULL;
  214. remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
  215. acpi_lid_dir = NULL;
  216. remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
  217. acpi_button_dir = NULL;
  218. return 0;
  219. }
  220. /* --------------------------------------------------------------------------
  221. Driver Interface
  222. -------------------------------------------------------------------------- */
  223. int acpi_lid_notifier_register(struct notifier_block *nb)
  224. {
  225. return blocking_notifier_chain_register(&acpi_lid_notifier, nb);
  226. }
  227. EXPORT_SYMBOL(acpi_lid_notifier_register);
  228. int acpi_lid_notifier_unregister(struct notifier_block *nb)
  229. {
  230. return blocking_notifier_chain_unregister(&acpi_lid_notifier, nb);
  231. }
  232. EXPORT_SYMBOL(acpi_lid_notifier_unregister);
  233. int acpi_lid_open(void)
  234. {
  235. if (!lid_device)
  236. return -ENODEV;
  237. return acpi_lid_evaluate_state(lid_device);
  238. }
  239. EXPORT_SYMBOL(acpi_lid_open);
  240. static int acpi_lid_update_state(struct acpi_device *device)
  241. {
  242. int state;
  243. state = acpi_lid_evaluate_state(device);
  244. if (state < 0)
  245. return state;
  246. return acpi_lid_notify_state(device, state);
  247. }
  248. static void acpi_lid_initialize_state(struct acpi_device *device)
  249. {
  250. switch (lid_init_state) {
  251. case ACPI_BUTTON_LID_INIT_OPEN:
  252. (void)acpi_lid_notify_state(device, 1);
  253. break;
  254. case ACPI_BUTTON_LID_INIT_METHOD:
  255. (void)acpi_lid_update_state(device);
  256. break;
  257. case ACPI_BUTTON_LID_INIT_IGNORE:
  258. default:
  259. break;
  260. }
  261. }
  262. static void acpi_button_notify(struct acpi_device *device, u32 event)
  263. {
  264. struct acpi_button *button = acpi_driver_data(device);
  265. struct input_dev *input;
  266. switch (event) {
  267. case ACPI_FIXED_HARDWARE_EVENT:
  268. event = ACPI_BUTTON_NOTIFY_STATUS;
  269. /* fall through */
  270. case ACPI_BUTTON_NOTIFY_STATUS:
  271. input = button->input;
  272. if (button->type == ACPI_BUTTON_TYPE_LID) {
  273. acpi_lid_update_state(device);
  274. } else {
  275. int keycode;
  276. pm_wakeup_event(&device->dev, 0);
  277. if (button->suspended)
  278. break;
  279. keycode = test_bit(KEY_SLEEP, input->keybit) ?
  280. KEY_SLEEP : KEY_POWER;
  281. input_report_key(input, keycode, 1);
  282. input_sync(input);
  283. input_report_key(input, keycode, 0);
  284. input_sync(input);
  285. acpi_bus_generate_netlink_event(
  286. device->pnp.device_class,
  287. dev_name(&device->dev),
  288. event, ++button->pushed);
  289. }
  290. break;
  291. default:
  292. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  293. "Unsupported event [0x%x]\n", event));
  294. break;
  295. }
  296. }
  297. #ifdef CONFIG_PM_SLEEP
  298. static int acpi_button_suspend(struct device *dev)
  299. {
  300. struct acpi_device *device = to_acpi_device(dev);
  301. struct acpi_button *button = acpi_driver_data(device);
  302. button->suspended = true;
  303. return 0;
  304. }
  305. static int acpi_button_resume(struct device *dev)
  306. {
  307. struct acpi_device *device = to_acpi_device(dev);
  308. struct acpi_button *button = acpi_driver_data(device);
  309. button->suspended = false;
  310. if (button->type == ACPI_BUTTON_TYPE_LID)
  311. acpi_lid_initialize_state(device);
  312. return 0;
  313. }
  314. #endif
  315. static int acpi_button_add(struct acpi_device *device)
  316. {
  317. struct acpi_button *button;
  318. struct input_dev *input;
  319. const char *hid = acpi_device_hid(device);
  320. char *name, *class;
  321. int error;
  322. button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
  323. if (!button)
  324. return -ENOMEM;
  325. device->driver_data = button;
  326. button->input = input = input_allocate_device();
  327. if (!input) {
  328. error = -ENOMEM;
  329. goto err_free_button;
  330. }
  331. name = acpi_device_name(device);
  332. class = acpi_device_class(device);
  333. if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
  334. !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
  335. button->type = ACPI_BUTTON_TYPE_POWER;
  336. strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
  337. sprintf(class, "%s/%s",
  338. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
  339. } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
  340. !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
  341. button->type = ACPI_BUTTON_TYPE_SLEEP;
  342. strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
  343. sprintf(class, "%s/%s",
  344. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
  345. } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
  346. button->type = ACPI_BUTTON_TYPE_LID;
  347. strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
  348. sprintf(class, "%s/%s",
  349. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
  350. } else {
  351. printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
  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. snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
  359. input->name = name;
  360. input->phys = button->phys;
  361. input->id.bustype = BUS_HOST;
  362. input->id.product = button->type;
  363. input->dev.parent = &device->dev;
  364. switch (button->type) {
  365. case ACPI_BUTTON_TYPE_POWER:
  366. input_set_capability(input, EV_KEY, KEY_POWER);
  367. break;
  368. case ACPI_BUTTON_TYPE_SLEEP:
  369. input_set_capability(input, EV_KEY, KEY_SLEEP);
  370. break;
  371. case ACPI_BUTTON_TYPE_LID:
  372. input_set_capability(input, EV_SW, SW_LID);
  373. break;
  374. }
  375. error = input_register_device(input);
  376. if (error)
  377. goto err_remove_fs;
  378. if (button->type == ACPI_BUTTON_TYPE_LID) {
  379. acpi_lid_initialize_state(device);
  380. /*
  381. * This assumes there's only one lid device, or if there are
  382. * more we only care about the last one...
  383. */
  384. lid_device = device;
  385. }
  386. printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
  387. return 0;
  388. err_remove_fs:
  389. acpi_button_remove_fs(device);
  390. err_free_input:
  391. input_free_device(input);
  392. err_free_button:
  393. kfree(button);
  394. return error;
  395. }
  396. static int acpi_button_remove(struct acpi_device *device)
  397. {
  398. struct acpi_button *button = acpi_driver_data(device);
  399. acpi_button_remove_fs(device);
  400. input_unregister_device(button->input);
  401. kfree(button);
  402. return 0;
  403. }
  404. static int param_set_lid_init_state(const char *val, struct kernel_param *kp)
  405. {
  406. int result = 0;
  407. if (!strncmp(val, "open", sizeof("open") - 1)) {
  408. lid_init_state = ACPI_BUTTON_LID_INIT_OPEN;
  409. pr_info("Notify initial lid state as open\n");
  410. } else if (!strncmp(val, "method", sizeof("method") - 1)) {
  411. lid_init_state = ACPI_BUTTON_LID_INIT_METHOD;
  412. pr_info("Notify initial lid state with _LID return value\n");
  413. } else if (!strncmp(val, "ignore", sizeof("ignore") - 1)) {
  414. lid_init_state = ACPI_BUTTON_LID_INIT_IGNORE;
  415. pr_info("Do not notify initial lid state\n");
  416. } else
  417. result = -EINVAL;
  418. return result;
  419. }
  420. static int param_get_lid_init_state(char *buffer, struct kernel_param *kp)
  421. {
  422. switch (lid_init_state) {
  423. case ACPI_BUTTON_LID_INIT_OPEN:
  424. return sprintf(buffer, "open");
  425. case ACPI_BUTTON_LID_INIT_METHOD:
  426. return sprintf(buffer, "method");
  427. case ACPI_BUTTON_LID_INIT_IGNORE:
  428. return sprintf(buffer, "ignore");
  429. default:
  430. return sprintf(buffer, "invalid");
  431. }
  432. return 0;
  433. }
  434. module_param_call(lid_init_state,
  435. param_set_lid_init_state, param_get_lid_init_state,
  436. NULL, 0644);
  437. MODULE_PARM_DESC(lid_init_state, "Behavior for reporting LID initial state");
  438. module_acpi_driver(acpi_button_driver);