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