button.c 16 KB

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