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