button.c 20 KB

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