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