button.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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/compiler.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/types.h>
  27. #include <linux/proc_fs.h>
  28. #include <linux/seq_file.h>
  29. #include <linux/input.h>
  30. #include <linux/slab.h>
  31. #include <linux/acpi.h>
  32. #include <linux/dmi.h>
  33. #include <acpi/button.h>
  34. #define PREFIX "ACPI: "
  35. #define ACPI_BUTTON_CLASS "button"
  36. #define ACPI_BUTTON_FILE_INFO "info"
  37. #define ACPI_BUTTON_FILE_STATE "state"
  38. #define ACPI_BUTTON_TYPE_UNKNOWN 0x00
  39. #define ACPI_BUTTON_NOTIFY_STATUS 0x80
  40. #define ACPI_BUTTON_SUBCLASS_POWER "power"
  41. #define ACPI_BUTTON_HID_POWER "PNP0C0C"
  42. #define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button"
  43. #define ACPI_BUTTON_TYPE_POWER 0x01
  44. #define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
  45. #define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
  46. #define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button"
  47. #define ACPI_BUTTON_TYPE_SLEEP 0x03
  48. #define ACPI_BUTTON_SUBCLASS_LID "lid"
  49. #define ACPI_BUTTON_HID_LID "PNP0C0D"
  50. #define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
  51. #define ACPI_BUTTON_TYPE_LID 0x05
  52. #define ACPI_BUTTON_LID_INIT_IGNORE 0x00
  53. #define ACPI_BUTTON_LID_INIT_OPEN 0x01
  54. #define ACPI_BUTTON_LID_INIT_METHOD 0x02
  55. #define _COMPONENT ACPI_BUTTON_COMPONENT
  56. ACPI_MODULE_NAME("button");
  57. MODULE_AUTHOR("Paul Diefenbaugh");
  58. MODULE_DESCRIPTION("ACPI Button Driver");
  59. MODULE_LICENSE("GPL");
  60. static const struct acpi_device_id button_device_ids[] = {
  61. {ACPI_BUTTON_HID_LID, 0},
  62. {ACPI_BUTTON_HID_SLEEP, 0},
  63. {ACPI_BUTTON_HID_SLEEPF, 0},
  64. {ACPI_BUTTON_HID_POWER, 0},
  65. {ACPI_BUTTON_HID_POWERF, 0},
  66. {"", 0},
  67. };
  68. MODULE_DEVICE_TABLE(acpi, button_device_ids);
  69. /*
  70. * Some devices which don't even have a lid in anyway have a broken _LID
  71. * method (e.g. pointing to a floating gpio pin) causing spurious LID events.
  72. */
  73. static const struct dmi_system_id lid_blacklst[] = {
  74. {
  75. /* GP-electronic T701 */
  76. .matches = {
  77. DMI_MATCH(DMI_SYS_VENDOR, "Insyde"),
  78. DMI_MATCH(DMI_PRODUCT_NAME, "T701"),
  79. DMI_MATCH(DMI_BIOS_VERSION, "BYT70A.YNCHENG.WIN.007"),
  80. },
  81. },
  82. {
  83. /*
  84. * Medion Akoya E2215T, notification of the LID device only
  85. * happens on close, not on open and _LID always returns closed.
  86. */
  87. .matches = {
  88. DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
  89. DMI_MATCH(DMI_PRODUCT_NAME, "E2215T"),
  90. },
  91. .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
  92. },
  93. {
  94. /*
  95. * Medion Akoya E2228T, notification of the LID device only
  96. * happens on close, not on open and _LID always returns closed.
  97. */
  98. .matches = {
  99. DMI_MATCH(DMI_SYS_VENDOR, "MEDION"),
  100. DMI_MATCH(DMI_PRODUCT_NAME, "E2228T"),
  101. },
  102. .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
  103. },
  104. {
  105. /*
  106. * Razer Blade Stealth 13 late 2019, notification of the LID device
  107. * only happens on close, not on open and _LID always returns closed.
  108. */
  109. .matches = {
  110. DMI_MATCH(DMI_SYS_VENDOR, "Razer"),
  111. DMI_MATCH(DMI_PRODUCT_NAME, "Razer Blade Stealth 13 Late 2019"),
  112. },
  113. .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
  114. },
  115. {}
  116. };
  117. static int acpi_button_add(struct acpi_device *device);
  118. static int acpi_button_remove(struct acpi_device *device);
  119. static void acpi_button_notify(struct acpi_device *device, u32 event);
  120. #ifdef CONFIG_PM_SLEEP
  121. static int acpi_button_suspend(struct device *dev);
  122. static int acpi_button_resume(struct device *dev);
  123. #else
  124. #define acpi_button_suspend NULL
  125. #define acpi_button_resume NULL
  126. #endif
  127. static SIMPLE_DEV_PM_OPS(acpi_button_pm, acpi_button_suspend, acpi_button_resume);
  128. static struct acpi_driver acpi_button_driver = {
  129. .name = "button",
  130. .class = ACPI_BUTTON_CLASS,
  131. .ids = button_device_ids,
  132. .ops = {
  133. .add = acpi_button_add,
  134. .remove = acpi_button_remove,
  135. .notify = acpi_button_notify,
  136. },
  137. .drv.pm = &acpi_button_pm,
  138. };
  139. struct acpi_button {
  140. unsigned int type;
  141. struct input_dev *input;
  142. char phys[32]; /* for input device */
  143. unsigned long pushed;
  144. int last_state;
  145. ktime_t last_time;
  146. bool suspended;
  147. bool lid_state_initialized;
  148. };
  149. static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier);
  150. static struct acpi_device *lid_device;
  151. static u8 lid_init_state = ACPI_BUTTON_LID_INIT_METHOD;
  152. static unsigned long lid_report_interval __read_mostly = 500;
  153. module_param(lid_report_interval, ulong, 0644);
  154. MODULE_PARM_DESC(lid_report_interval, "Interval (ms) between lid key events");
  155. /* --------------------------------------------------------------------------
  156. FS Interface (/proc)
  157. -------------------------------------------------------------------------- */
  158. static struct proc_dir_entry *acpi_button_dir;
  159. static struct proc_dir_entry *acpi_lid_dir;
  160. static int acpi_lid_evaluate_state(struct acpi_device *device)
  161. {
  162. unsigned long long lid_state;
  163. acpi_status status;
  164. status = acpi_evaluate_integer(device->handle, "_LID", NULL, &lid_state);
  165. if (ACPI_FAILURE(status))
  166. return -ENODEV;
  167. return lid_state ? 1 : 0;
  168. }
  169. static int acpi_lid_notify_state(struct acpi_device *device, int state)
  170. {
  171. struct acpi_button *button = acpi_driver_data(device);
  172. int ret;
  173. ktime_t next_report;
  174. bool do_update;
  175. /*
  176. * In lid_init_state=ignore mode, if user opens/closes lid
  177. * frequently with "open" missing, and "last_time" is also updated
  178. * frequently, "close" cannot be delivered to the userspace.
  179. * So "last_time" is only updated after a timeout or an actual
  180. * switch.
  181. */
  182. if (lid_init_state != ACPI_BUTTON_LID_INIT_IGNORE ||
  183. button->last_state != !!state)
  184. do_update = true;
  185. else
  186. do_update = false;
  187. next_report = ktime_add(button->last_time,
  188. ms_to_ktime(lid_report_interval));
  189. if (button->last_state == !!state &&
  190. ktime_after(ktime_get(), next_report)) {
  191. /* Complain the buggy firmware */
  192. pr_warn_once("The lid device is not compliant to SW_LID.\n");
  193. /*
  194. * Send the unreliable complement switch event:
  195. *
  196. * On most platforms, the lid device is reliable. However
  197. * there are exceptions:
  198. * 1. Platforms returning initial lid state as "close" by
  199. * default after booting/resuming:
  200. * https://bugzilla.kernel.org/show_bug.cgi?id=89211
  201. * https://bugzilla.kernel.org/show_bug.cgi?id=106151
  202. * 2. Platforms never reporting "open" events:
  203. * https://bugzilla.kernel.org/show_bug.cgi?id=106941
  204. * On these buggy platforms, the usage model of the ACPI
  205. * lid device actually is:
  206. * 1. The initial returning value of _LID may not be
  207. * reliable.
  208. * 2. The open event may not be reliable.
  209. * 3. The close event is reliable.
  210. *
  211. * But SW_LID is typed as input switch event, the input
  212. * layer checks if the event is redundant. Hence if the
  213. * state is not switched, the userspace cannot see this
  214. * platform triggered reliable event. By inserting a
  215. * complement switch event, it then is guaranteed that the
  216. * platform triggered reliable one can always be seen by
  217. * the userspace.
  218. */
  219. if (lid_init_state == ACPI_BUTTON_LID_INIT_IGNORE) {
  220. do_update = true;
  221. /*
  222. * Do generate complement switch event for "close"
  223. * as "close" is reliable and wrong "open" won't
  224. * trigger unexpected behaviors.
  225. * Do not generate complement switch event for
  226. * "open" as "open" is not reliable and wrong
  227. * "close" will trigger unexpected behaviors.
  228. */
  229. if (!state) {
  230. input_report_switch(button->input,
  231. SW_LID, state);
  232. input_sync(button->input);
  233. }
  234. }
  235. }
  236. /* Send the platform triggered reliable event */
  237. if (do_update) {
  238. acpi_handle_debug(device->handle, "ACPI LID %s\n",
  239. state ? "open" : "closed");
  240. input_report_switch(button->input, SW_LID, !state);
  241. input_sync(button->input);
  242. button->last_state = !!state;
  243. button->last_time = ktime_get();
  244. }
  245. ret = blocking_notifier_call_chain(&acpi_lid_notifier, state, device);
  246. if (ret == NOTIFY_DONE)
  247. ret = blocking_notifier_call_chain(&acpi_lid_notifier, state,
  248. device);
  249. if (ret == NOTIFY_DONE || ret == NOTIFY_OK) {
  250. /*
  251. * It is also regarded as success if the notifier_chain
  252. * returns NOTIFY_OK or NOTIFY_DONE.
  253. */
  254. ret = 0;
  255. }
  256. return ret;
  257. }
  258. static int __maybe_unused acpi_button_state_seq_show(struct seq_file *seq,
  259. void *offset)
  260. {
  261. struct acpi_device *device = seq->private;
  262. int state;
  263. state = acpi_lid_evaluate_state(device);
  264. seq_printf(seq, "state: %s\n",
  265. state < 0 ? "unsupported" : (state ? "open" : "closed"));
  266. return 0;
  267. }
  268. static int acpi_button_add_fs(struct acpi_device *device)
  269. {
  270. struct acpi_button *button = acpi_driver_data(device);
  271. struct proc_dir_entry *entry = NULL;
  272. int ret = 0;
  273. /* procfs I/F for ACPI lid device only */
  274. if (button->type != ACPI_BUTTON_TYPE_LID)
  275. return 0;
  276. if (acpi_button_dir || acpi_lid_dir) {
  277. printk(KERN_ERR PREFIX "More than one Lid device found!\n");
  278. return -EEXIST;
  279. }
  280. /* create /proc/acpi/button */
  281. acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
  282. if (!acpi_button_dir)
  283. return -ENODEV;
  284. /* create /proc/acpi/button/lid */
  285. acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
  286. if (!acpi_lid_dir) {
  287. ret = -ENODEV;
  288. goto remove_button_dir;
  289. }
  290. /* create /proc/acpi/button/lid/LID/ */
  291. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), acpi_lid_dir);
  292. if (!acpi_device_dir(device)) {
  293. ret = -ENODEV;
  294. goto remove_lid_dir;
  295. }
  296. /* create /proc/acpi/button/lid/LID/state */
  297. entry = proc_create_single_data(ACPI_BUTTON_FILE_STATE, S_IRUGO,
  298. acpi_device_dir(device), acpi_button_state_seq_show,
  299. device);
  300. if (!entry) {
  301. ret = -ENODEV;
  302. goto remove_dev_dir;
  303. }
  304. done:
  305. return ret;
  306. remove_dev_dir:
  307. remove_proc_entry(acpi_device_bid(device),
  308. acpi_lid_dir);
  309. acpi_device_dir(device) = NULL;
  310. remove_lid_dir:
  311. remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
  312. acpi_lid_dir = NULL;
  313. remove_button_dir:
  314. remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
  315. acpi_button_dir = NULL;
  316. goto done;
  317. }
  318. static int acpi_button_remove_fs(struct acpi_device *device)
  319. {
  320. struct acpi_button *button = acpi_driver_data(device);
  321. if (button->type != ACPI_BUTTON_TYPE_LID)
  322. return 0;
  323. remove_proc_entry(ACPI_BUTTON_FILE_STATE,
  324. acpi_device_dir(device));
  325. remove_proc_entry(acpi_device_bid(device),
  326. acpi_lid_dir);
  327. acpi_device_dir(device) = NULL;
  328. remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
  329. acpi_lid_dir = NULL;
  330. remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
  331. acpi_button_dir = NULL;
  332. return 0;
  333. }
  334. /* --------------------------------------------------------------------------
  335. Driver Interface
  336. -------------------------------------------------------------------------- */
  337. int acpi_lid_notifier_register(struct notifier_block *nb)
  338. {
  339. return blocking_notifier_chain_register(&acpi_lid_notifier, nb);
  340. }
  341. EXPORT_SYMBOL(acpi_lid_notifier_register);
  342. int acpi_lid_notifier_unregister(struct notifier_block *nb)
  343. {
  344. return blocking_notifier_chain_unregister(&acpi_lid_notifier, nb);
  345. }
  346. EXPORT_SYMBOL(acpi_lid_notifier_unregister);
  347. int acpi_lid_open(void)
  348. {
  349. if (!lid_device)
  350. return -ENODEV;
  351. return acpi_lid_evaluate_state(lid_device);
  352. }
  353. EXPORT_SYMBOL(acpi_lid_open);
  354. static int acpi_lid_update_state(struct acpi_device *device,
  355. bool signal_wakeup)
  356. {
  357. int state;
  358. state = acpi_lid_evaluate_state(device);
  359. if (state < 0)
  360. return state;
  361. if (state && signal_wakeup)
  362. acpi_pm_wakeup_event(&device->dev);
  363. return acpi_lid_notify_state(device, state);
  364. }
  365. static void acpi_lid_initialize_state(struct acpi_device *device)
  366. {
  367. struct acpi_button *button = acpi_driver_data(device);
  368. switch (lid_init_state) {
  369. case ACPI_BUTTON_LID_INIT_OPEN:
  370. (void)acpi_lid_notify_state(device, 1);
  371. break;
  372. case ACPI_BUTTON_LID_INIT_METHOD:
  373. (void)acpi_lid_update_state(device, false);
  374. break;
  375. case ACPI_BUTTON_LID_INIT_IGNORE:
  376. default:
  377. break;
  378. }
  379. button->lid_state_initialized = true;
  380. }
  381. static void acpi_button_notify(struct acpi_device *device, u32 event)
  382. {
  383. struct acpi_button *button = acpi_driver_data(device);
  384. struct input_dev *input;
  385. switch (event) {
  386. case ACPI_FIXED_HARDWARE_EVENT:
  387. event = ACPI_BUTTON_NOTIFY_STATUS;
  388. /* fall through */
  389. case ACPI_BUTTON_NOTIFY_STATUS:
  390. input = button->input;
  391. if (button->type == ACPI_BUTTON_TYPE_LID) {
  392. if (button->lid_state_initialized)
  393. acpi_lid_update_state(device, true);
  394. } else {
  395. int keycode;
  396. acpi_pm_wakeup_event(&device->dev);
  397. if (button->suspended)
  398. break;
  399. keycode = test_bit(KEY_SLEEP, input->keybit) ?
  400. KEY_SLEEP : KEY_POWER;
  401. input_report_key(input, keycode, 1);
  402. input_sync(input);
  403. input_report_key(input, keycode, 0);
  404. input_sync(input);
  405. acpi_bus_generate_netlink_event(
  406. device->pnp.device_class,
  407. dev_name(&device->dev),
  408. event, ++button->pushed);
  409. }
  410. break;
  411. default:
  412. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  413. "Unsupported event [0x%x]\n", event));
  414. break;
  415. }
  416. }
  417. #ifdef CONFIG_PM_SLEEP
  418. static int acpi_button_suspend(struct device *dev)
  419. {
  420. struct acpi_device *device = to_acpi_device(dev);
  421. struct acpi_button *button = acpi_driver_data(device);
  422. button->suspended = true;
  423. return 0;
  424. }
  425. static int acpi_button_resume(struct device *dev)
  426. {
  427. struct acpi_device *device = to_acpi_device(dev);
  428. struct acpi_button *button = acpi_driver_data(device);
  429. button->suspended = false;
  430. if (button->type == ACPI_BUTTON_TYPE_LID) {
  431. button->last_state = !!acpi_lid_evaluate_state(device);
  432. button->last_time = ktime_get();
  433. acpi_lid_initialize_state(device);
  434. }
  435. return 0;
  436. }
  437. #endif
  438. static int acpi_lid_input_open(struct input_dev *input)
  439. {
  440. struct acpi_device *device = input_get_drvdata(input);
  441. struct acpi_button *button = acpi_driver_data(device);
  442. button->last_state = !!acpi_lid_evaluate_state(device);
  443. button->last_time = ktime_get();
  444. acpi_lid_initialize_state(device);
  445. return 0;
  446. }
  447. static int acpi_button_add(struct acpi_device *device)
  448. {
  449. struct acpi_button *button;
  450. struct input_dev *input;
  451. const char *hid = acpi_device_hid(device);
  452. char *name, *class;
  453. int error;
  454. if (!strcmp(hid, ACPI_BUTTON_HID_LID) && dmi_check_system(lid_blacklst))
  455. return -ENODEV;
  456. button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
  457. if (!button)
  458. return -ENOMEM;
  459. device->driver_data = button;
  460. button->input = input = input_allocate_device();
  461. if (!input) {
  462. error = -ENOMEM;
  463. goto err_free_button;
  464. }
  465. name = acpi_device_name(device);
  466. class = acpi_device_class(device);
  467. if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
  468. !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
  469. button->type = ACPI_BUTTON_TYPE_POWER;
  470. strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
  471. sprintf(class, "%s/%s",
  472. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
  473. } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
  474. !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
  475. button->type = ACPI_BUTTON_TYPE_SLEEP;
  476. strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
  477. sprintf(class, "%s/%s",
  478. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
  479. } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
  480. button->type = ACPI_BUTTON_TYPE_LID;
  481. strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
  482. sprintf(class, "%s/%s",
  483. ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
  484. input->open = acpi_lid_input_open;
  485. } else {
  486. printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
  487. error = -ENODEV;
  488. goto err_free_input;
  489. }
  490. error = acpi_button_add_fs(device);
  491. if (error)
  492. goto err_free_input;
  493. snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
  494. input->name = name;
  495. input->phys = button->phys;
  496. input->id.bustype = BUS_HOST;
  497. input->id.product = button->type;
  498. input->dev.parent = &device->dev;
  499. switch (button->type) {
  500. case ACPI_BUTTON_TYPE_POWER:
  501. input_set_capability(input, EV_KEY, KEY_POWER);
  502. break;
  503. case ACPI_BUTTON_TYPE_SLEEP:
  504. input_set_capability(input, EV_KEY, KEY_SLEEP);
  505. break;
  506. case ACPI_BUTTON_TYPE_LID:
  507. input_set_capability(input, EV_SW, SW_LID);
  508. break;
  509. }
  510. input_set_drvdata(input, device);
  511. error = input_register_device(input);
  512. if (error)
  513. goto err_remove_fs;
  514. if (button->type == ACPI_BUTTON_TYPE_LID) {
  515. /*
  516. * This assumes there's only one lid device, or if there are
  517. * more we only care about the last one...
  518. */
  519. lid_device = device;
  520. }
  521. device_init_wakeup(&device->dev, true);
  522. printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
  523. return 0;
  524. err_remove_fs:
  525. acpi_button_remove_fs(device);
  526. err_free_input:
  527. input_free_device(input);
  528. err_free_button:
  529. kfree(button);
  530. return error;
  531. }
  532. static int acpi_button_remove(struct acpi_device *device)
  533. {
  534. struct acpi_button *button = acpi_driver_data(device);
  535. acpi_button_remove_fs(device);
  536. input_unregister_device(button->input);
  537. kfree(button);
  538. return 0;
  539. }
  540. static int param_set_lid_init_state(const char *val,
  541. const struct kernel_param *kp)
  542. {
  543. int result = 0;
  544. if (!strncmp(val, "open", sizeof("open") - 1)) {
  545. lid_init_state = ACPI_BUTTON_LID_INIT_OPEN;
  546. pr_info("Notify initial lid state as open\n");
  547. } else if (!strncmp(val, "method", sizeof("method") - 1)) {
  548. lid_init_state = ACPI_BUTTON_LID_INIT_METHOD;
  549. pr_info("Notify initial lid state with _LID return value\n");
  550. } else if (!strncmp(val, "ignore", sizeof("ignore") - 1)) {
  551. lid_init_state = ACPI_BUTTON_LID_INIT_IGNORE;
  552. pr_info("Do not notify initial lid state\n");
  553. } else
  554. result = -EINVAL;
  555. return result;
  556. }
  557. static int param_get_lid_init_state(char *buffer,
  558. const struct kernel_param *kp)
  559. {
  560. switch (lid_init_state) {
  561. case ACPI_BUTTON_LID_INIT_OPEN:
  562. return sprintf(buffer, "open");
  563. case ACPI_BUTTON_LID_INIT_METHOD:
  564. return sprintf(buffer, "method");
  565. case ACPI_BUTTON_LID_INIT_IGNORE:
  566. return sprintf(buffer, "ignore");
  567. default:
  568. return sprintf(buffer, "invalid");
  569. }
  570. return 0;
  571. }
  572. module_param_call(lid_init_state,
  573. param_set_lid_init_state, param_get_lid_init_state,
  574. NULL, 0644);
  575. MODULE_PARM_DESC(lid_init_state, "Behavior for reporting LID initial state");
  576. static int acpi_button_register_driver(struct acpi_driver *driver)
  577. {
  578. /*
  579. * Modules such as nouveau.ko and i915.ko have a link time dependency
  580. * on acpi_lid_open(), and would therefore not be loadable on ACPI
  581. * capable kernels booted in non-ACPI mode if the return value of
  582. * acpi_bus_register_driver() is returned from here with ACPI disabled
  583. * when this driver is built as a module.
  584. */
  585. if (acpi_disabled)
  586. return 0;
  587. return acpi_bus_register_driver(driver);
  588. }
  589. static void acpi_button_unregister_driver(struct acpi_driver *driver)
  590. {
  591. if (!acpi_disabled)
  592. acpi_bus_unregister_driver(driver);
  593. }
  594. module_driver(acpi_button_driver, acpi_button_register_driver,
  595. acpi_button_unregister_driver);