diff options
Diffstat (limited to 'kernel/power/main.c')
| -rw-r--r-- | kernel/power/main.c | 160 |
1 files changed, 141 insertions, 19 deletions
diff --git a/kernel/power/main.c b/kernel/power/main.c index 1c12581f1c62..428f8a034e96 100644 --- a/kernel/power/main.c +++ b/kernel/power/main.c | |||
| @@ -269,8 +269,7 @@ static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr, | |||
| 269 | return (s - buf); | 269 | return (s - buf); |
| 270 | } | 270 | } |
| 271 | 271 | ||
| 272 | static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr, | 272 | static suspend_state_t decode_state(const char *buf, size_t n) |
| 273 | const char *buf, size_t n) | ||
| 274 | { | 273 | { |
| 275 | #ifdef CONFIG_SUSPEND | 274 | #ifdef CONFIG_SUSPEND |
| 276 | suspend_state_t state = PM_SUSPEND_STANDBY; | 275 | suspend_state_t state = PM_SUSPEND_STANDBY; |
| @@ -278,27 +277,48 @@ static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr, | |||
| 278 | #endif | 277 | #endif |
| 279 | char *p; | 278 | char *p; |
| 280 | int len; | 279 | int len; |
| 281 | int error = -EINVAL; | ||
| 282 | 280 | ||
| 283 | p = memchr(buf, '\n', n); | 281 | p = memchr(buf, '\n', n); |
| 284 | len = p ? p - buf : n; | 282 | len = p ? p - buf : n; |
| 285 | 283 | ||
| 286 | /* First, check if we are requested to hibernate */ | 284 | /* Check hibernation first. */ |
| 287 | if (len == 4 && !strncmp(buf, "disk", len)) { | 285 | if (len == 4 && !strncmp(buf, "disk", len)) |
| 288 | error = hibernate(); | 286 | return PM_SUSPEND_MAX; |
| 289 | goto Exit; | ||
| 290 | } | ||
| 291 | 287 | ||
| 292 | #ifdef CONFIG_SUSPEND | 288 | #ifdef CONFIG_SUSPEND |
| 293 | for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) { | 289 | for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) |
| 294 | if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) { | 290 | if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) |
| 295 | error = pm_suspend(state); | 291 | return state; |
| 296 | break; | ||
| 297 | } | ||
| 298 | } | ||
| 299 | #endif | 292 | #endif |
| 300 | 293 | ||
| 301 | Exit: | 294 | return PM_SUSPEND_ON; |
| 295 | } | ||
| 296 | |||
| 297 | static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr, | ||
| 298 | const char *buf, size_t n) | ||
| 299 | { | ||
| 300 | suspend_state_t state; | ||
| 301 | int error; | ||
| 302 | |||
| 303 | error = pm_autosleep_lock(); | ||
| 304 | if (error) | ||
| 305 | return error; | ||
| 306 | |||
| 307 | if (pm_autosleep_state() > PM_SUSPEND_ON) { | ||
| 308 | error = -EBUSY; | ||
| 309 | goto out; | ||
| 310 | } | ||
| 311 | |||
| 312 | state = decode_state(buf, n); | ||
| 313 | if (state < PM_SUSPEND_MAX) | ||
| 314 | error = pm_suspend(state); | ||
| 315 | else if (state == PM_SUSPEND_MAX) | ||
| 316 | error = hibernate(); | ||
| 317 | else | ||
| 318 | error = -EINVAL; | ||
| 319 | |||
| 320 | out: | ||
| 321 | pm_autosleep_unlock(); | ||
| 302 | return error ? error : n; | 322 | return error ? error : n; |
| 303 | } | 323 | } |
| 304 | 324 | ||
| @@ -339,7 +359,8 @@ static ssize_t wakeup_count_show(struct kobject *kobj, | |||
| 339 | { | 359 | { |
| 340 | unsigned int val; | 360 | unsigned int val; |
| 341 | 361 | ||
| 342 | return pm_get_wakeup_count(&val) ? sprintf(buf, "%u\n", val) : -EINTR; | 362 | return pm_get_wakeup_count(&val, true) ? |
| 363 | sprintf(buf, "%u\n", val) : -EINTR; | ||
| 343 | } | 364 | } |
| 344 | 365 | ||
| 345 | static ssize_t wakeup_count_store(struct kobject *kobj, | 366 | static ssize_t wakeup_count_store(struct kobject *kobj, |
| @@ -347,15 +368,106 @@ static ssize_t wakeup_count_store(struct kobject *kobj, | |||
| 347 | const char *buf, size_t n) | 368 | const char *buf, size_t n) |
| 348 | { | 369 | { |
| 349 | unsigned int val; | 370 | unsigned int val; |
| 371 | int error; | ||
| 372 | |||
| 373 | error = pm_autosleep_lock(); | ||
| 374 | if (error) | ||
| 375 | return error; | ||
| 376 | |||
| 377 | if (pm_autosleep_state() > PM_SUSPEND_ON) { | ||
| 378 | error = -EBUSY; | ||
| 379 | goto out; | ||
| 380 | } | ||
| 350 | 381 | ||
| 382 | error = -EINVAL; | ||
| 351 | if (sscanf(buf, "%u", &val) == 1) { | 383 | if (sscanf(buf, "%u", &val) == 1) { |
| 352 | if (pm_save_wakeup_count(val)) | 384 | if (pm_save_wakeup_count(val)) |
| 353 | return n; | 385 | error = n; |
| 354 | } | 386 | } |
| 355 | return -EINVAL; | 387 | |
| 388 | out: | ||
| 389 | pm_autosleep_unlock(); | ||
| 390 | return error; | ||
| 356 | } | 391 | } |
| 357 | 392 | ||
| 358 | power_attr(wakeup_count); | 393 | power_attr(wakeup_count); |
| 394 | |||
| 395 | #ifdef CONFIG_PM_AUTOSLEEP | ||
| 396 | static ssize_t autosleep_show(struct kobject *kobj, | ||
| 397 | struct kobj_attribute *attr, | ||
| 398 | char *buf) | ||
| 399 | { | ||
| 400 | suspend_state_t state = pm_autosleep_state(); | ||
| 401 | |||
| 402 | if (state == PM_SUSPEND_ON) | ||
| 403 | return sprintf(buf, "off\n"); | ||
| 404 | |||
| 405 | #ifdef CONFIG_SUSPEND | ||
| 406 | if (state < PM_SUSPEND_MAX) | ||
| 407 | return sprintf(buf, "%s\n", valid_state(state) ? | ||
| 408 | pm_states[state] : "error"); | ||
| 409 | #endif | ||
| 410 | #ifdef CONFIG_HIBERNATION | ||
| 411 | return sprintf(buf, "disk\n"); | ||
| 412 | #else | ||
| 413 | return sprintf(buf, "error"); | ||
| 414 | #endif | ||
| 415 | } | ||
| 416 | |||
| 417 | static ssize_t autosleep_store(struct kobject *kobj, | ||
| 418 | struct kobj_attribute *attr, | ||
| 419 | const char *buf, size_t n) | ||
| 420 | { | ||
| 421 | suspend_state_t state = decode_state(buf, n); | ||
| 422 | int error; | ||
| 423 | |||
| 424 | if (state == PM_SUSPEND_ON | ||
| 425 | && strcmp(buf, "off") && strcmp(buf, "off\n")) | ||
| 426 | return -EINVAL; | ||
| 427 | |||
| 428 | error = pm_autosleep_set_state(state); | ||
| 429 | return error ? error : n; | ||
| 430 | } | ||
| 431 | |||
| 432 | power_attr(autosleep); | ||
| 433 | #endif /* CONFIG_PM_AUTOSLEEP */ | ||
| 434 | |||
| 435 | #ifdef CONFIG_PM_WAKELOCKS | ||
| 436 | static ssize_t wake_lock_show(struct kobject *kobj, | ||
| 437 | struct kobj_attribute *attr, | ||
| 438 | char *buf) | ||
| 439 | { | ||
| 440 | return pm_show_wakelocks(buf, true); | ||
| 441 | } | ||
| 442 | |||
| 443 | static ssize_t wake_lock_store(struct kobject *kobj, | ||
| 444 | struct kobj_attribute *attr, | ||
| 445 | const char *buf, size_t n) | ||
| 446 | { | ||
| 447 | int error = pm_wake_lock(buf); | ||
| 448 | return error ? error : n; | ||
| 449 | } | ||
| 450 | |||
| 451 | power_attr(wake_lock); | ||
| 452 | |||
| 453 | static ssize_t wake_unlock_show(struct kobject *kobj, | ||
| 454 | struct kobj_attribute *attr, | ||
| 455 | char *buf) | ||
| 456 | { | ||
| 457 | return pm_show_wakelocks(buf, false); | ||
| 458 | } | ||
| 459 | |||
| 460 | static ssize_t wake_unlock_store(struct kobject *kobj, | ||
| 461 | struct kobj_attribute *attr, | ||
| 462 | const char *buf, size_t n) | ||
| 463 | { | ||
| 464 | int error = pm_wake_unlock(buf); | ||
| 465 | return error ? error : n; | ||
| 466 | } | ||
| 467 | |||
| 468 | power_attr(wake_unlock); | ||
| 469 | |||
| 470 | #endif /* CONFIG_PM_WAKELOCKS */ | ||
| 359 | #endif /* CONFIG_PM_SLEEP */ | 471 | #endif /* CONFIG_PM_SLEEP */ |
| 360 | 472 | ||
| 361 | #ifdef CONFIG_PM_TRACE | 473 | #ifdef CONFIG_PM_TRACE |
| @@ -409,6 +521,13 @@ static struct attribute * g[] = { | |||
| 409 | #ifdef CONFIG_PM_SLEEP | 521 | #ifdef CONFIG_PM_SLEEP |
| 410 | &pm_async_attr.attr, | 522 | &pm_async_attr.attr, |
| 411 | &wakeup_count_attr.attr, | 523 | &wakeup_count_attr.attr, |
| 524 | #ifdef CONFIG_PM_AUTOSLEEP | ||
| 525 | &autosleep_attr.attr, | ||
| 526 | #endif | ||
| 527 | #ifdef CONFIG_PM_WAKELOCKS | ||
| 528 | &wake_lock_attr.attr, | ||
| 529 | &wake_unlock_attr.attr, | ||
| 530 | #endif | ||
| 412 | #ifdef CONFIG_PM_DEBUG | 531 | #ifdef CONFIG_PM_DEBUG |
| 413 | &pm_test_attr.attr, | 532 | &pm_test_attr.attr, |
| 414 | #endif | 533 | #endif |
| @@ -444,7 +563,10 @@ static int __init pm_init(void) | |||
| 444 | power_kobj = kobject_create_and_add("power", NULL); | 563 | power_kobj = kobject_create_and_add("power", NULL); |
| 445 | if (!power_kobj) | 564 | if (!power_kobj) |
| 446 | return -ENOMEM; | 565 | return -ENOMEM; |
| 447 | return sysfs_create_group(power_kobj, &attr_group); | 566 | error = sysfs_create_group(power_kobj, &attr_group); |
| 567 | if (error) | ||
| 568 | return error; | ||
| 569 | return pm_autosleep_init(); | ||
| 448 | } | 570 | } |
| 449 | 571 | ||
| 450 | core_initcall(pm_init); | 572 | core_initcall(pm_init); |
