button.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. #define pr_fmt(fmt) "ACPI : button: " fmt
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/types.h>
  26. #include <linux/proc_fs.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/input.h>
  29. #include <linux/slab.h>
  30. #include <linux/acpi.h>
  31. #include <acpi/button.h>
  32. #define PREFIX "ACPI: "
  33. #define ACPI_BUTTON_CLASS "button"
  34. #define ACPI_BUTTON_FILE_INFO "info"
  35. #define ACPI_BUTTON_FILE_STATE "state"
  36. #define ACPI_BUTTON_TYPE_UNKNOWN 0x00
  37. #define ACPI_BUTTON_NOTIFY_STATUS 0x80
  38. #define ACPI_BUTTON_SUBCLASS_POWER "power"
  39. #define ACPI_BUTTON_HID_POWER "PNP0C0C"
  40. #define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button"
  41. #define ACPI_BUTTON_TYPE_POWER 0x01
  42. #define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
  43. #define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
  44. #define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button"
  45. #define ACPI_BUTTON_TYPE_SLEEP 0x03
  46. #define ACPI_BUTTON_SUBCLASS_LID "lid"
  47. #define ACPI_BUTTON_HID_LID "PNP0C0D"
  48. #define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
  49. #define ACPI_BUTTON_TYPE_LID 0x05
  50. #define ACPI_BUTTON_LID_INIT_IGNORE 0x00
  51. #define ACPI_BUTTON_LID_INIT_OPEN 0x01
  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. int last_state;
  94. ktime_t last_time;
  95. bool suspended;
  96. };
  97. static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier);
  98. static struct acpi_device *lid_device;
  99. static u8 lid_init_state = ACPI_BUTTON_LID_INIT_OPEN;
  100. static unsigned long lid_report_interval __read_mostly = 500;
  101. module_param(lid_report_interval, ulong, 0644);
  102. MODULE_PARM_DESC(lid_report_interval, "Interval (ms) between lid key events");
  103. /* --------------------------------------------------------------------------
  104. FS Interface (/proc)
  105. -------------------------------------------------------------------------- */
  106. static struct proc_dir_entry *acpi_button_dir;
  107. static struct proc_dir_entry *acpi_lid_dir;
  108. static int acpi_lid_evaluate_state(struct acpi_device *device)
  109. {
  110. unsigned long long lid_state;
  111. acpi_status status;
  112. status = acpi_evaluate_integer(device->handle, "_LID", NULL, &lid_state);
  113. if (ACPI_FAILURE(status))
  114. return -ENODEV;
  115. return lid_state ? 1 : 0;
  116. }
  117. static int acpi_lid_notify_state(struct acpi_device *device, int state)
  118. {
  119. struct acpi_button *button = acpi_driver_data(device);
  120. int ret;
  121. ktime_t next_report;
  122. bool do_update;
  123. /*
  124. * In lid_init_state=ignore mode, if user opens/closes lid
  125. * frequently with "open" missing, and "last_time" is also updated
  126. * frequently, "close" cannot be delivered to the userspace.
  127. * So "last_time" is only updated after a timeout or an actual
  128. * switch.
  129. */
  130. if (lid_init_state != ACPI_BUTTON_LID_INIT_IGNORE ||
  131. button->last_state != !!state)
  132. do_update = true;
  133. else
  134. do_update = false;
  135. next_report = ktime_add(button->last_time,
  136. ms_to_ktime(lid_report_interval));
  137. if (button->last_state == !!state &&
  138. ktime_after(ktime_get(), next_report)) {
  139. /* Complain the buggy firmware */
  140. pr_warn_once("The lid device is not compliant to SW_LID.\n");
  141. /*
  142. * Send the unreliable complement switch event:
  143. *
  144. * On most platforms, the lid device is reliable. However
  145. * there are exceptions:
  146. * 1. Platforms returning initial lid state as "close" by
  147. * default after booting/resuming:
  148. * https://bugzilla.kernel.org/show_bug.cgi?id=89211
  149. * https://bugzilla.kernel.org/show_bug.cgi?id=106151
  150. * 2. Platforms never reporting "open" events:
  151. * https://bugzilla.kernel.org/show_bug.cgi?id=106941
  152. * On these buggy platforms, the usage model of the ACPI
  153. * lid device actually is:
  154. * 1. The initial returning value of _LID may not be
  155. * reliable.
  156. * 2. The open event may not be reliable.
  157. * 3. The close event is reliable.
  158. *
  159. * But SW_LID is typed as input switch event, the input
  160. * layer checks if the event is redundant. Hence if the
  161. * state is not switched, the userspace cannot see this
  162. * platform triggered reliable event. By inserting a
  163. * complement switch event, it then is guaranteed that the
  164. * platform triggered reliable one can always be seen by
  165. * the userspace.
  166. */
  167. if (lid_init_state == ACPI_BUTTON_LID_INIT_IGNORE) {
  168. do_update = true;
  169. /*
  170. * Do generate complement switch event for "close"
  171. * as "close" is reliable and wrong "open" won't
  172. * trigger unexpected behaviors.
  173. * Do not generate complement switch event for
  174. * "open" as "open" is not reliable and wrong
  175. * "close" will trigger unexpected behaviors.
  176. */
  177. if (!state) {
  178. input_report_switch(button->input,
  179. SW_LID, state);
  180. input_sync(button->input);
  181. }
  182. }
  183. }
  184. /* Send the platform triggered reliable event */
  185. if (do_update) {
  186. input_report_switch(button->input, SW_LID, !state);
  187. input_sync(button->input);
  188. button->last_state = !!state;
  189. button->last_time = ktime_get();
  190. }
  191. if (state)
  192. pm_wakeup_event(&device->dev, 0);
  193. ret = blocking_notifier_call_chain(&acpi_lid_notifier, state, device);
  194. if (ret == NOTIFY_DONE)
  195. ret = blocking_notifier_call_chain(&acpi_lid_notifier, state,
  196. device);
  197. if (ret == NOTIFY_DONE || ret == NOTIFY_OK) {
  198. /*
  199. * It is also regarded as success if the notifier_chain
  200. * returns NOTIFY_OK or NOTIFY_DONE.
  201. */
  202. ret = 0;
  203. }
  204. return ret;
  205. }
  206. static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
  207. {
  208. struct acpi_device *device = seq->private;
  209. int state;
  210. state = acpi_lid_evaluate_state(device);
  211. seq_printf(seq, "state: %s\n",
  212. state < 0 ? "unsupported" : (state ? "open" : "closed"));
  213. return 0;
  214. }
  215. static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
  216. {
  217. return single_open(file, acpi_button_state_seq_show, PDE_DATA(inode));
  218. }
  219. static const struct file_operations acpi_button_state_fops = {
  220. .owner = THIS_MODULE,
  221. .open = acpi_button_state_open_fs,
  222. .read = seq_read,
  223. .llseek = seq_lseek,
  224. .release = single_release,
  225. };
  226. static int acpi_button_add_fs(struct acpi_device *device)
  227. {
  228. struct acpi_button *button = acpi_driver_data(device);
  229. struct proc_dir_entry *entry = NULL;
  230. int ret = 0;
  231. /* procfs I/F for ACPI lid device only */
  232. if (button->type != ACPI_BUTTON_TYPE_LID)
  233. return 0;
  234. if (acpi_button_dir || acpi_lid_dir) {
  235. printk(KERN_ERR PREFIX "More than one Lid device found!\n");
  236. return -EEXIST;
  237. }
  238. /* create /proc/acpi/button */
  239. acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
  240. if (!acpi_button_dir)
  241. return -ENODEV;
  242. /* create /proc/acpi/button/lid */
  243. acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
  244. if (!acpi_lid_dir) {
  245. ret = -ENODEV;
  246. goto remove_button_dir;
  247. }
  248. /* create /proc/acpi/button/lid/LID/ */
  249. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), acpi_lid_dir);
  250. if (!acpi_device_dir(device)) {
  251. ret = -ENODEV;
  252. goto remove_lid_dir;
  253. }
  254. /* create /proc/acpi/button/lid/LID/state */
  255. entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
  256. S_IRUGO, acpi_device_dir(device),
  257. &acpi_button_state_fops, device);
  258. if (!entry) {
  259. ret = -ENODEV;
  260. goto remove_dev_dir;
  261. }
  262. done:
  263. return ret;
  264. remove_dev_dir:
  265. remove_proc_entry(acpi_device_bid(device),
  266. acpi_lid_dir);
  267. acpi_device_dir(device) = NULL;
  268. remove_lid_dir:
  269. remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
  270. acpi_lid_dir = NULL;
  271. remove_button_dir:
  272. remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
  273. acpi_button_dir = NULL;
  274. goto done;
  275. }
  276. static int acpi_button_remove_fs(struct acpi_device *device)
  277. {
  278. struct acpi_button *button = acpi_driver_data(device);
  279. if (button->type != ACPI_BUTTON_TYPE_LID)
  280. return 0;
  281. remove_proc_entry(ACPI_BUTTON_FILE_STATE,
  282. acpi_device_dir(device));
  283. remove_proc_entry(acpi_device_bid(device),
  284. acpi_lid_dir);
  285. acpi_device_dir(device) = NULL;
  286. remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
  287. acpi_lid_dir = NULL;
  288. remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
  289. acpi_button_dir = NULL;
  290. return 0;
  291. }
  292. /* --------------------------------------------------------------------------
  293. Driver Interface
  294. -------------------------------------------------------------------------- */
  295. int acpi_lid_notifier_register(struct notifier_block *nb)
  296. {
  297. return blocking_notifier_chain_register(&acpi_lid_notifier, nb);
  298. }
  299. EXPORT_SYMBOL(acpi_lid_notifier_register);
  300. int acpi_lid_notifier_unregister(struct notifier_block *nb)
  301. {
  302. return blocking_notifier_chain_unregister(&acpi_lid_notifier, nb);
  303. }
  304. EXPORT_SYMBOL(acpi_lid_notifier_unregister);
  305. int acpi_lid_open(void)
  306. {
  307. if (!lid_device)
  308. return -ENODEV;
  309. return acpi_lid_evaluate_state(lid_device);
  310. }
  311. EXPORT_SYMBOL(acpi_lid_open);
  312. static int acpi_lid_update_state(struct acpi_device *device)
  313. {
  314. int state;
  315. state = acpi_lid_evaluate_state(device);
  316. if (state < 0)
  317. return state;
  318. return acpi_lid_notify_state(device, state);
  319. }
  320. static void acpi_lid_initialize_state(struct acpi_device *device)
  321. {
  322. switch (lid_init_state) {
  323. case ACPI_BUTTON_LID_INIT_OPEN:
  324. (void)acpi_lid_notify_state(device, 1);
  325. break;
  326. case ACPI_BUTTON_LID_INIT_IGNORE:
  327. default:
  328. break;
  329. }
  330. }
  331. static void acpi_button_notify(struct acpi_device *device, u32 event)
  332. {
  333. struct acpi_button *button = acpi_driver_data(device);
  334. struct input_dev *input;
  335. switch (event) {
  336. case ACPI_FIXED_HARDWARE_EVENT:
  337. event = ACPI_BUTTON_NOTIFY_STATUS;
  338. /* fall through */
  339. case ACPI_BUTTON_NOTIFY_STATUS:
  340. input = button->input;
  341. if (button->type == ACPI_BUTTON_TYPE_LID) {
  342. acpi_lid_update_state(device);
  343. } else {
  344. int keycode;
  345. pm_wakeup_event(&device->dev, 0);
  346. if (button->suspended)
  347. break;
  348. keycode = test_bit(KEY_SLEEP, input->keybit) ?
  349. KEY_SLEEP : KEY_POWER;
  350. input_report_key(input, keycode, 1);
  351. input_sync(input);
  352. input_report_key(input, keycode, 0);
  353. input_sync(input);
  354. acpi_bus_generate_netlink_event(
  355. device->pnp.device_class,
  356. dev_name(&device->dev),
  357. event, ++button->pushed);
  358. }
  359. break;
  360. default:
  361. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  362. "Unsupported event [0x%x]\n", event));
  363. break;
  364. }
  365. }
  366. #ifdef CONFIG_PM_SLEEP
  367. static int acpi_button_suspend(struct device *dev)
  368. {
  369. struct acpi_device *device = to_acpi_device(dev);
  370. struct acpi_button *button = acpi_driver_data(device);
  371. button->suspended = true;
  372. return 0;
  373. }
  374. static int acpi_button_resume(struct device *dev)
  375. {
  376. struct acpi_device *device = to_acpi_device(dev);
  377. struct acpi_button *button = acpi_driver_data(device);
  378. button->suspended = false;
  379. if (button->type == ACPI_BUTTON_TYPE_LID)
  380. acpi_lid_initialize_state(device);
  381. return 0;
  382. }
  383. #endif
  384. static int acpi_button_add(struct acpi_device *device)
  385. {
  386. struct acpi_button *button;
  387. struct input_dev *input;
  388. const char *hid = acpi_device_hid(device);
  389. char *name, *class;
  390. int error;
  391. button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
  392. if (!button)
  393. return -ENOMEM;
  394. device->driver_data = button;
  395. button->input = input = input_allocate_device();
  396. if (!input) {
  397. error = -ENOMEM;
  398. goto err_free_button;
  399. }
  400. name = acpi_device_name(device);
  401. class = acpi_device_class(device);
  402. if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
  403. !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
  404. button->type = ACPI_BUTTON_TYPE_POWER;
  405. strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
  406. sprintf(class, "%s/%s",
  407. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
  408. } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
  409. !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
  410. button->type = ACPI_BUTTON_TYPE_SLEEP;
  411. strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
  412. sprintf(class, "%s/%s",
  413. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
  414. } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
  415. button->type = ACPI_BUTTON_TYPE_LID;
  416. strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
  417. sprintf(class, "%s/%s",
  418. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
  419. button->last_state = !!acpi_lid_evaluate_state(device);
  420. button->last_time = ktime_get();
  421. } else {
  422. printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
  423. error = -ENODEV;
  424. goto err_free_input;
  425. }
  426. error = acpi_button_add_fs(device);
  427. if (error)
  428. goto err_free_input;
  429. snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
  430. input->name = name;
  431. input->phys = button->phys;
  432. input->id.bustype = BUS_HOST;
  433. input->id.product = button->type;
  434. input->dev.parent = &device->dev;
  435. switch (button->type) {
  436. case ACPI_BUTTON_TYPE_POWER:
  437. input_set_capability(input, EV_KEY, KEY_POWER);
  438. break;
  439. case ACPI_BUTTON_TYPE_SLEEP:
  440. input_set_capability(input, EV_KEY, KEY_SLEEP);
  441. break;
  442. case ACPI_BUTTON_TYPE_LID:
  443. input_set_capability(input, EV_SW, SW_LID);
  444. break;
  445. }
  446. error = input_register_device(input);
  447. if (error)
  448. goto err_remove_fs;
  449. if (button->type == ACPI_BUTTON_TYPE_LID) {
  450. acpi_lid_initialize_state(device);
  451. /*
  452. * This assumes there's only one lid device, or if there are
  453. * more we only care about the last one...
  454. */
  455. lid_device = device;
  456. }
  457. printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
  458. return 0;
  459. err_remove_fs:
  460. acpi_button_remove_fs(device);
  461. err_free_input:
  462. input_free_device(input);
  463. err_free_button:
  464. kfree(button);
  465. return error;
  466. }
  467. static int acpi_button_remove(struct acpi_device *device)
  468. {
  469. struct acpi_button *button = acpi_driver_data(device);
  470. acpi_button_remove_fs(device);
  471. input_unregister_device(button->input);
  472. kfree(button);
  473. return 0;
  474. }
  475. static int param_set_lid_init_state(const char *val, struct kernel_param *kp)
  476. {
  477. int result = 0;
  478. if (!strncmp(val, "open", sizeof("open") - 1)) {
  479. lid_init_state = ACPI_BUTTON_LID_INIT_OPEN;
  480. pr_info("Notify initial lid state as open\n");
  481. } else if (!strncmp(val, "ignore", sizeof("ignore") - 1)) {
  482. lid_init_state = ACPI_BUTTON_LID_INIT_IGNORE;
  483. pr_info("Do not notify initial lid state\n");
  484. } else
  485. result = -EINVAL;
  486. return result;
  487. }
  488. static int param_get_lid_init_state(char *buffer, struct kernel_param *kp)
  489. {
  490. switch (lid_init_state) {
  491. case ACPI_BUTTON_LID_INIT_OPEN:
  492. return sprintf(buffer, "open");
  493. case ACPI_BUTTON_LID_INIT_IGNORE:
  494. return sprintf(buffer, "ignore");
  495. default:
  496. return sprintf(buffer, "invalid");
  497. }
  498. return 0;
  499. }
  500. module_param_call(lid_init_state,
  501. param_set_lid_init_state, param_get_lid_init_state,
  502. NULL, 0644);
  503. MODULE_PARM_DESC(lid_init_state, "Behavior for reporting LID initial state");
  504. module_acpi_driver(acpi_button_driver);