linux/drivers/acpi/power.c
<<
>>
Prefs
   1/*
   2 * drivers/acpi/power.c - ACPI Power Resources management.
   3 *
   4 * Copyright (C) 2001 - 2015 Intel Corp.
   5 * Author: Andy Grover <andrew.grover@intel.com>
   6 * Author: Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
   7 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
   8 *
   9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10 *
  11 *  This program is free software; you can redistribute it and/or modify
  12 *  it under the terms of the GNU General Public License as published by
  13 *  the Free Software Foundation; either version 2 of the License, or (at
  14 *  your option) any later version.
  15 *
  16 *  This program is distributed in the hope that it will be useful, but
  17 *  WITHOUT ANY WARRANTY; without even the implied warranty of
  18 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19 *  General Public License for more details.
  20 *
  21 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  22 */
  23
  24/*
  25 * ACPI power-managed devices may be controlled in two ways:
  26 * 1. via "Device Specific (D-State) Control"
  27 * 2. via "Power Resource Control".
  28 * The code below deals with ACPI Power Resources control.
  29 * 
  30 * An ACPI "power resource object" represents a software controllable power
  31 * plane, clock plane, or other resource depended on by a device.
  32 *
  33 * A device may rely on multiple power resources, and a power resource
  34 * may be shared by multiple devices.
  35 */
  36
  37#include <linux/kernel.h>
  38#include <linux/module.h>
  39#include <linux/init.h>
  40#include <linux/types.h>
  41#include <linux/slab.h>
  42#include <linux/pm_runtime.h>
  43#include <linux/sysfs.h>
  44#include <linux/acpi.h>
  45#include "sleep.h"
  46#include "internal.h"
  47
  48#define _COMPONENT                      ACPI_POWER_COMPONENT
  49ACPI_MODULE_NAME("power");
  50#define ACPI_POWER_CLASS                "power_resource"
  51#define ACPI_POWER_DEVICE_NAME          "Power Resource"
  52#define ACPI_POWER_FILE_INFO            "info"
  53#define ACPI_POWER_FILE_STATUS          "state"
  54#define ACPI_POWER_RESOURCE_STATE_OFF   0x00
  55#define ACPI_POWER_RESOURCE_STATE_ON    0x01
  56#define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
  57
  58struct acpi_power_dependent_device {
  59        struct device *dev;
  60        struct list_head node;
  61};
  62
  63struct acpi_power_resource {
  64        struct acpi_device device;
  65        struct list_head list_node;
  66        char *name;
  67        u32 system_level;
  68        u32 order;
  69        unsigned int ref_count;
  70        bool wakeup_enabled;
  71        struct mutex resource_lock;
  72        struct list_head dependents;
  73};
  74
  75struct acpi_power_resource_entry {
  76        struct list_head node;
  77        struct acpi_power_resource *resource;
  78};
  79
  80static LIST_HEAD(acpi_power_resource_list);
  81static DEFINE_MUTEX(power_resource_list_lock);
  82
  83/* --------------------------------------------------------------------------
  84                             Power Resource Management
  85   -------------------------------------------------------------------------- */
  86
  87static inline
  88struct acpi_power_resource *to_power_resource(struct acpi_device *device)
  89{
  90        return container_of(device, struct acpi_power_resource, device);
  91}
  92
  93static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle)
  94{
  95        struct acpi_device *device;
  96
  97        if (acpi_bus_get_device(handle, &device))
  98                return NULL;
  99
 100        return to_power_resource(device);
 101}
 102
 103static int acpi_power_resources_list_add(acpi_handle handle,
 104                                         struct list_head *list)
 105{
 106        struct acpi_power_resource *resource = acpi_power_get_context(handle);
 107        struct acpi_power_resource_entry *entry;
 108
 109        if (!resource || !list)
 110                return -EINVAL;
 111
 112        entry = kzalloc(sizeof(*entry), GFP_KERNEL);
 113        if (!entry)
 114                return -ENOMEM;
 115
 116        entry->resource = resource;
 117        if (!list_empty(list)) {
 118                struct acpi_power_resource_entry *e;
 119
 120                list_for_each_entry(e, list, node)
 121                        if (e->resource->order > resource->order) {
 122                                list_add_tail(&entry->node, &e->node);
 123                                return 0;
 124                        }
 125        }
 126        list_add_tail(&entry->node, list);
 127        return 0;
 128}
 129
 130void acpi_power_resources_list_free(struct list_head *list)
 131{
 132        struct acpi_power_resource_entry *entry, *e;
 133
 134        list_for_each_entry_safe(entry, e, list, node) {
 135                list_del(&entry->node);
 136                kfree(entry);
 137        }
 138}
 139
 140static bool acpi_power_resource_is_dup(union acpi_object *package,
 141                                       unsigned int start, unsigned int i)
 142{
 143        acpi_handle rhandle, dup;
 144        unsigned int j;
 145
 146        /* The caller is expected to check the package element types */
 147        rhandle = package->package.elements[i].reference.handle;
 148        for (j = start; j < i; j++) {
 149                dup = package->package.elements[j].reference.handle;
 150                if (dup == rhandle)
 151                        return true;
 152        }
 153
 154        return false;
 155}
 156
 157int acpi_extract_power_resources(union acpi_object *package, unsigned int start,
 158                                 struct list_head *list)
 159{
 160        unsigned int i;
 161        int err = 0;
 162
 163        for (i = start; i < package->package.count; i++) {
 164                union acpi_object *element = &package->package.elements[i];
 165                acpi_handle rhandle;
 166
 167                if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
 168                        err = -ENODATA;
 169                        break;
 170                }
 171                rhandle = element->reference.handle;
 172                if (!rhandle) {
 173                        err = -ENODEV;
 174                        break;
 175                }
 176
 177                /* Some ACPI tables contain duplicate power resource references */
 178                if (acpi_power_resource_is_dup(package, start, i))
 179                        continue;
 180
 181                err = acpi_add_power_resource(rhandle);
 182                if (err)
 183                        break;
 184
 185                err = acpi_power_resources_list_add(rhandle, list);
 186                if (err)
 187                        break;
 188        }
 189        if (err)
 190                acpi_power_resources_list_free(list);
 191
 192        return err;
 193}
 194
 195static int acpi_power_get_state(acpi_handle handle, int *state)
 196{
 197        acpi_status status = AE_OK;
 198        unsigned long long sta = 0;
 199        char node_name[5];
 200        struct acpi_buffer buffer = { sizeof(node_name), node_name };
 201
 202
 203        if (!handle || !state)
 204                return -EINVAL;
 205
 206        status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
 207        if (ACPI_FAILURE(status))
 208                return -ENODEV;
 209
 210        *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
 211                              ACPI_POWER_RESOURCE_STATE_OFF;
 212
 213        acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
 214
 215        ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
 216                          node_name,
 217                                *state ? "on" : "off"));
 218
 219        return 0;
 220}
 221
 222static int acpi_power_get_list_state(struct list_head *list, int *state)
 223{
 224        struct acpi_power_resource_entry *entry;
 225        int cur_state;
 226
 227        if (!list || !state)
 228                return -EINVAL;
 229
 230        /* The state of the list is 'on' IFF all resources are 'on'. */
 231        cur_state = 0;
 232        list_for_each_entry(entry, list, node) {
 233                struct acpi_power_resource *resource = entry->resource;
 234                acpi_handle handle = resource->device.handle;
 235                int result;
 236
 237                mutex_lock(&resource->resource_lock);
 238                result = acpi_power_get_state(handle, &cur_state);
 239                mutex_unlock(&resource->resource_lock);
 240                if (result)
 241                        return result;
 242
 243                if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
 244                        break;
 245        }
 246
 247        ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
 248                          cur_state ? "on" : "off"));
 249
 250        *state = cur_state;
 251        return 0;
 252}
 253
 254static int
 255acpi_power_resource_add_dependent(struct acpi_power_resource *resource,
 256                                  struct device *dev)
 257{
 258        struct acpi_power_dependent_device *dep;
 259        int ret = 0;
 260
 261        mutex_lock(&resource->resource_lock);
 262        list_for_each_entry(dep, &resource->dependents, node) {
 263                /* Only add it once */
 264                if (dep->dev == dev)
 265                        goto unlock;
 266        }
 267
 268        dep = kzalloc(sizeof(*dep), GFP_KERNEL);
 269        if (!dep) {
 270                ret = -ENOMEM;
 271                goto unlock;
 272        }
 273
 274        dep->dev = dev;
 275        list_add_tail(&dep->node, &resource->dependents);
 276        dev_dbg(dev, "added power dependency to [%s]\n", resource->name);
 277
 278unlock:
 279        mutex_unlock(&resource->resource_lock);
 280        return ret;
 281}
 282
 283static void
 284acpi_power_resource_remove_dependent(struct acpi_power_resource *resource,
 285                                     struct device *dev)
 286{
 287        struct acpi_power_dependent_device *dep;
 288
 289        mutex_lock(&resource->resource_lock);
 290        list_for_each_entry(dep, &resource->dependents, node) {
 291                if (dep->dev == dev) {
 292                        list_del(&dep->node);
 293                        kfree(dep);
 294                        dev_dbg(dev, "removed power dependency to [%s]\n",
 295                                resource->name);
 296                        break;
 297                }
 298        }
 299        mutex_unlock(&resource->resource_lock);
 300}
 301
 302/**
 303 * acpi_device_power_add_dependent - Add dependent device of this ACPI device
 304 * @adev: ACPI device pointer
 305 * @dev: Dependent device
 306 *
 307 * If @adev has non-empty _PR0 the @dev is added as dependent device to all
 308 * power resources returned by it. This means that whenever these power
 309 * resources are turned _ON the dependent devices get runtime resumed. This
 310 * is needed for devices such as PCI to allow its driver to re-initialize
 311 * it after it went to D0uninitialized.
 312 *
 313 * If @adev does not have _PR0 this does nothing.
 314 *
 315 * Returns %0 in case of success and negative errno otherwise.
 316 */
 317int acpi_device_power_add_dependent(struct acpi_device *adev,
 318                                    struct device *dev)
 319{
 320        struct acpi_power_resource_entry *entry;
 321        struct list_head *resources;
 322        int ret;
 323
 324        if (!adev->flags.power_manageable)
 325                return 0;
 326
 327        resources = &adev->power.states[ACPI_STATE_D0].resources;
 328        list_for_each_entry(entry, resources, node) {
 329                ret = acpi_power_resource_add_dependent(entry->resource, dev);
 330                if (ret)
 331                        goto err;
 332        }
 333
 334        return 0;
 335
 336err:
 337        list_for_each_entry(entry, resources, node)
 338                acpi_power_resource_remove_dependent(entry->resource, dev);
 339
 340        return ret;
 341}
 342
 343/**
 344 * acpi_device_power_remove_dependent - Remove dependent device
 345 * @adev: ACPI device pointer
 346 * @dev: Dependent device
 347 *
 348 * Does the opposite of acpi_device_power_add_dependent() and removes the
 349 * dependent device if it is found. Can be called to @adev that does not
 350 * have _PR0 as well.
 351 */
 352void acpi_device_power_remove_dependent(struct acpi_device *adev,
 353                                        struct device *dev)
 354{
 355        struct acpi_power_resource_entry *entry;
 356        struct list_head *resources;
 357
 358        if (!adev->flags.power_manageable)
 359                return;
 360
 361        resources = &adev->power.states[ACPI_STATE_D0].resources;
 362        list_for_each_entry_reverse(entry, resources, node)
 363                acpi_power_resource_remove_dependent(entry->resource, dev);
 364}
 365
 366static int __acpi_power_on(struct acpi_power_resource *resource)
 367{
 368        struct acpi_power_dependent_device *dep;
 369        acpi_status status = AE_OK;
 370
 371        status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL);
 372        if (ACPI_FAILURE(status))
 373                return -ENODEV;
 374
 375        ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n",
 376                          resource->name));
 377
 378        /*
 379         * If there are other dependents on this power resource we need to
 380         * resume them now so that their drivers can re-initialize the
 381         * hardware properly after it went back to D0.
 382         */
 383        if (list_empty(&resource->dependents) ||
 384            list_is_singular(&resource->dependents))
 385                return 0;
 386
 387        list_for_each_entry(dep, &resource->dependents, node) {
 388                dev_dbg(dep->dev, "runtime resuming because [%s] turned on\n",
 389                        resource->name);
 390                pm_request_resume(dep->dev);
 391        }
 392
 393        return 0;
 394}
 395
 396static int acpi_power_on_unlocked(struct acpi_power_resource *resource)
 397{
 398        int result = 0;
 399
 400        if (resource->ref_count++) {
 401                ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 402                                  "Power resource [%s] already on\n",
 403                                  resource->name));
 404        } else {
 405                result = __acpi_power_on(resource);
 406                if (result)
 407                        resource->ref_count--;
 408        }
 409        return result;
 410}
 411
 412static int acpi_power_on(struct acpi_power_resource *resource)
 413{
 414        int result;
 415
 416        mutex_lock(&resource->resource_lock);
 417        result = acpi_power_on_unlocked(resource);
 418        mutex_unlock(&resource->resource_lock);
 419        return result;
 420}
 421
 422static int __acpi_power_off(struct acpi_power_resource *resource)
 423{
 424        acpi_status status;
 425
 426        status = acpi_evaluate_object(resource->device.handle, "_OFF",
 427                                      NULL, NULL);
 428        if (ACPI_FAILURE(status))
 429                return -ENODEV;
 430
 431        ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned off\n",
 432                          resource->name));
 433        return 0;
 434}
 435
 436static int acpi_power_off_unlocked(struct acpi_power_resource *resource)
 437{
 438        int result = 0;
 439
 440        if (!resource->ref_count) {
 441                ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 442                                  "Power resource [%s] already off\n",
 443                                  resource->name));
 444                return 0;
 445        }
 446
 447        if (--resource->ref_count) {
 448                ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 449                                  "Power resource [%s] still in use\n",
 450                                  resource->name));
 451        } else {
 452                result = __acpi_power_off(resource);
 453                if (result)
 454                        resource->ref_count++;
 455        }
 456        return result;
 457}
 458
 459static int acpi_power_off(struct acpi_power_resource *resource)
 460{
 461        int result;
 462
 463        mutex_lock(&resource->resource_lock);
 464        result = acpi_power_off_unlocked(resource);
 465        mutex_unlock(&resource->resource_lock);
 466        return result;
 467}
 468
 469static int acpi_power_off_list(struct list_head *list)
 470{
 471        struct acpi_power_resource_entry *entry;
 472        int result = 0;
 473
 474        list_for_each_entry_reverse(entry, list, node) {
 475                result = acpi_power_off(entry->resource);
 476                if (result)
 477                        goto err;
 478        }
 479        return 0;
 480
 481 err:
 482        list_for_each_entry_continue(entry, list, node)
 483                acpi_power_on(entry->resource);
 484
 485        return result;
 486}
 487
 488static int acpi_power_on_list(struct list_head *list)
 489{
 490        struct acpi_power_resource_entry *entry;
 491        int result = 0;
 492
 493        list_for_each_entry(entry, list, node) {
 494                result = acpi_power_on(entry->resource);
 495                if (result)
 496                        goto err;
 497        }
 498        return 0;
 499
 500 err:
 501        list_for_each_entry_continue_reverse(entry, list, node)
 502                acpi_power_off(entry->resource);
 503
 504        return result;
 505}
 506
 507static struct attribute *attrs[] = {
 508        NULL,
 509};
 510
 511static const struct attribute_group attr_groups[] = {
 512        [ACPI_STATE_D0] = {
 513                .name = "power_resources_D0",
 514                .attrs = attrs,
 515        },
 516        [ACPI_STATE_D1] = {
 517                .name = "power_resources_D1",
 518                .attrs = attrs,
 519        },
 520        [ACPI_STATE_D2] = {
 521                .name = "power_resources_D2",
 522                .attrs = attrs,
 523        },
 524        [ACPI_STATE_D3_HOT] = {
 525                .name = "power_resources_D3hot",
 526                .attrs = attrs,
 527        },
 528};
 529
 530static const struct attribute_group wakeup_attr_group = {
 531        .name = "power_resources_wakeup",
 532        .attrs = attrs,
 533};
 534
 535static void acpi_power_hide_list(struct acpi_device *adev,
 536                                 struct list_head *resources,
 537                                 const struct attribute_group *attr_group)
 538{
 539        struct acpi_power_resource_entry *entry;
 540
 541        if (list_empty(resources))
 542                return;
 543
 544        list_for_each_entry_reverse(entry, resources, node) {
 545                struct acpi_device *res_dev = &entry->resource->device;
 546
 547                sysfs_remove_link_from_group(&adev->dev.kobj,
 548                                             attr_group->name,
 549                                             dev_name(&res_dev->dev));
 550        }
 551        sysfs_remove_group(&adev->dev.kobj, attr_group);
 552}
 553
 554static void acpi_power_expose_list(struct acpi_device *adev,
 555                                   struct list_head *resources,
 556                                   const struct attribute_group *attr_group)
 557{
 558        struct acpi_power_resource_entry *entry;
 559        int ret;
 560
 561        if (list_empty(resources))
 562                return;
 563
 564        ret = sysfs_create_group(&adev->dev.kobj, attr_group);
 565        if (ret)
 566                return;
 567
 568        list_for_each_entry(entry, resources, node) {
 569                struct acpi_device *res_dev = &entry->resource->device;
 570
 571                ret = sysfs_add_link_to_group(&adev->dev.kobj,
 572                                              attr_group->name,
 573                                              &res_dev->dev.kobj,
 574                                              dev_name(&res_dev->dev));
 575                if (ret) {
 576                        acpi_power_hide_list(adev, resources, attr_group);
 577                        break;
 578                }
 579        }
 580}
 581
 582static void acpi_power_expose_hide(struct acpi_device *adev,
 583                                   struct list_head *resources,
 584                                   const struct attribute_group *attr_group,
 585                                   bool expose)
 586{
 587        if (expose)
 588                acpi_power_expose_list(adev, resources, attr_group);
 589        else
 590                acpi_power_hide_list(adev, resources, attr_group);
 591}
 592
 593void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
 594{
 595        int state;
 596
 597        if (adev->wakeup.flags.valid)
 598                acpi_power_expose_hide(adev, &adev->wakeup.resources,
 599                                       &wakeup_attr_group, add);
 600
 601        if (!adev->power.flags.power_resources)
 602                return;
 603
 604        for (state = ACPI_STATE_D0; state <= ACPI_STATE_D3_HOT; state++)
 605                acpi_power_expose_hide(adev,
 606                                       &adev->power.states[state].resources,
 607                                       &attr_groups[state], add);
 608}
 609
 610int acpi_power_wakeup_list_init(struct list_head *list, int *system_level_p)
 611{
 612        struct acpi_power_resource_entry *entry;
 613        int system_level = 5;
 614
 615        list_for_each_entry(entry, list, node) {
 616                struct acpi_power_resource *resource = entry->resource;
 617                acpi_handle handle = resource->device.handle;
 618                int result;
 619                int state;
 620
 621                mutex_lock(&resource->resource_lock);
 622
 623                result = acpi_power_get_state(handle, &state);
 624                if (result) {
 625                        mutex_unlock(&resource->resource_lock);
 626                        return result;
 627                }
 628                if (state == ACPI_POWER_RESOURCE_STATE_ON) {
 629                        resource->ref_count++;
 630                        resource->wakeup_enabled = true;
 631                }
 632                if (system_level > resource->system_level)
 633                        system_level = resource->system_level;
 634
 635                mutex_unlock(&resource->resource_lock);
 636        }
 637        *system_level_p = system_level;
 638        return 0;
 639}
 640
 641/* --------------------------------------------------------------------------
 642                             Device Power Management
 643   -------------------------------------------------------------------------- */
 644
 645/**
 646 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
 647 *                          ACPI 3.0) _PSW (Power State Wake)
 648 * @dev: Device to handle.
 649 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
 650 * @sleep_state: Target sleep state of the system.
 651 * @dev_state: Target power state of the device.
 652 *
 653 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
 654 * State Wake) for the device, if present.  On failure reset the device's
 655 * wakeup.flags.valid flag.
 656 *
 657 * RETURN VALUE:
 658 * 0 if either _DSW or _PSW has been successfully executed
 659 * 0 if neither _DSW nor _PSW has been found
 660 * -ENODEV if the execution of either _DSW or _PSW has failed
 661 */
 662int acpi_device_sleep_wake(struct acpi_device *dev,
 663                           int enable, int sleep_state, int dev_state)
 664{
 665        union acpi_object in_arg[3];
 666        struct acpi_object_list arg_list = { 3, in_arg };
 667        acpi_status status = AE_OK;
 668
 669        /*
 670         * Try to execute _DSW first.
 671         *
 672         * Three arguments are needed for the _DSW object:
 673         * Argument 0: enable/disable the wake capabilities
 674         * Argument 1: target system state
 675         * Argument 2: target device state
 676         * When _DSW object is called to disable the wake capabilities, maybe
 677         * the first argument is filled. The values of the other two arguments
 678         * are meaningless.
 679         */
 680        in_arg[0].type = ACPI_TYPE_INTEGER;
 681        in_arg[0].integer.value = enable;
 682        in_arg[1].type = ACPI_TYPE_INTEGER;
 683        in_arg[1].integer.value = sleep_state;
 684        in_arg[2].type = ACPI_TYPE_INTEGER;
 685        in_arg[2].integer.value = dev_state;
 686        status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
 687        if (ACPI_SUCCESS(status)) {
 688                return 0;
 689        } else if (status != AE_NOT_FOUND) {
 690                printk(KERN_ERR PREFIX "_DSW execution failed\n");
 691                dev->wakeup.flags.valid = 0;
 692                return -ENODEV;
 693        }
 694
 695        /* Execute _PSW */
 696        status = acpi_execute_simple_method(dev->handle, "_PSW", enable);
 697        if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
 698                printk(KERN_ERR PREFIX "_PSW execution failed\n");
 699                dev->wakeup.flags.valid = 0;
 700                return -ENODEV;
 701        }
 702
 703        return 0;
 704}
 705
 706/*
 707 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
 708 * 1. Power on the power resources required for the wakeup device 
 709 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
 710 *    State Wake) for the device, if present
 711 */
 712int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
 713{
 714        struct acpi_power_resource_entry *entry;
 715        int err = 0;
 716
 717        if (!dev || !dev->wakeup.flags.valid)
 718                return -EINVAL;
 719
 720        mutex_lock(&acpi_device_lock);
 721
 722        if (dev->wakeup.prepare_count++)
 723                goto out;
 724
 725        list_for_each_entry(entry, &dev->wakeup.resources, node) {
 726                struct acpi_power_resource *resource = entry->resource;
 727
 728                mutex_lock(&resource->resource_lock);
 729
 730                if (!resource->wakeup_enabled) {
 731                        err = acpi_power_on_unlocked(resource);
 732                        if (!err)
 733                                resource->wakeup_enabled = true;
 734                }
 735
 736                mutex_unlock(&resource->resource_lock);
 737
 738                if (err) {
 739                        dev_err(&dev->dev,
 740                                "Cannot turn wakeup power resources on\n");
 741                        dev->wakeup.flags.valid = 0;
 742                        goto out;
 743                }
 744        }
 745        /*
 746         * Passing 3 as the third argument below means the device may be
 747         * put into arbitrary power state afterward.
 748         */
 749        err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
 750        if (err)
 751                dev->wakeup.prepare_count = 0;
 752
 753 out:
 754        mutex_unlock(&acpi_device_lock);
 755        return err;
 756}
 757
 758/*
 759 * Shutdown a wakeup device, counterpart of above method
 760 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
 761 *    State Wake) for the device, if present
 762 * 2. Shutdown down the power resources
 763 */
 764int acpi_disable_wakeup_device_power(struct acpi_device *dev)
 765{
 766        struct acpi_power_resource_entry *entry;
 767        int err = 0;
 768
 769        if (!dev || !dev->wakeup.flags.valid)
 770                return -EINVAL;
 771
 772        mutex_lock(&acpi_device_lock);
 773
 774        if (--dev->wakeup.prepare_count > 0)
 775                goto out;
 776
 777        /*
 778         * Executing the code below even if prepare_count is already zero when
 779         * the function is called may be useful, for example for initialisation.
 780         */
 781        if (dev->wakeup.prepare_count < 0)
 782                dev->wakeup.prepare_count = 0;
 783
 784        err = acpi_device_sleep_wake(dev, 0, 0, 0);
 785        if (err)
 786                goto out;
 787
 788        list_for_each_entry(entry, &dev->wakeup.resources, node) {
 789                struct acpi_power_resource *resource = entry->resource;
 790
 791                mutex_lock(&resource->resource_lock);
 792
 793                if (resource->wakeup_enabled) {
 794                        err = acpi_power_off_unlocked(resource);
 795                        if (!err)
 796                                resource->wakeup_enabled = false;
 797                }
 798
 799                mutex_unlock(&resource->resource_lock);
 800
 801                if (err) {
 802                        dev_err(&dev->dev,
 803                                "Cannot turn wakeup power resources off\n");
 804                        dev->wakeup.flags.valid = 0;
 805                        break;
 806                }
 807        }
 808
 809 out:
 810        mutex_unlock(&acpi_device_lock);
 811        return err;
 812}
 813
 814int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
 815{
 816        int result = 0;
 817        int list_state = 0;
 818        int i = 0;
 819
 820        if (!device || !state)
 821                return -EINVAL;
 822
 823        /*
 824         * We know a device's inferred power state when all the resources
 825         * required for a given D-state are 'on'.
 826         */
 827        for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
 828                struct list_head *list = &device->power.states[i].resources;
 829
 830                if (list_empty(list))
 831                        continue;
 832
 833                result = acpi_power_get_list_state(list, &list_state);
 834                if (result)
 835                        return result;
 836
 837                if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
 838                        *state = i;
 839                        return 0;
 840                }
 841        }
 842
 843        *state = device->power.states[ACPI_STATE_D3_COLD].flags.valid ?
 844                ACPI_STATE_D3_COLD : ACPI_STATE_D3_HOT;
 845        return 0;
 846}
 847
 848int acpi_power_on_resources(struct acpi_device *device, int state)
 849{
 850        if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT)
 851                return -EINVAL;
 852
 853        return acpi_power_on_list(&device->power.states[state].resources);
 854}
 855
 856int acpi_power_transition(struct acpi_device *device, int state)
 857{
 858        int result = 0;
 859
 860        if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
 861                return -EINVAL;
 862
 863        if (device->power.state == state || !device->flags.power_manageable)
 864                return 0;
 865
 866        if ((device->power.state < ACPI_STATE_D0)
 867            || (device->power.state > ACPI_STATE_D3_COLD))
 868                return -ENODEV;
 869
 870        /*
 871         * First we reference all power resources required in the target list
 872         * (e.g. so the device doesn't lose power while transitioning).  Then,
 873         * we dereference all power resources used in the current list.
 874         */
 875        if (state < ACPI_STATE_D3_COLD)
 876                result = acpi_power_on_list(
 877                        &device->power.states[state].resources);
 878
 879        if (!result && device->power.state < ACPI_STATE_D3_COLD)
 880                acpi_power_off_list(
 881                        &device->power.states[device->power.state].resources);
 882
 883        /* We shouldn't change the state unless the above operations succeed. */
 884        device->power.state = result ? ACPI_STATE_UNKNOWN : state;
 885
 886        return result;
 887}
 888
 889static void acpi_release_power_resource(struct device *dev)
 890{
 891        struct acpi_device *device = to_acpi_device(dev);
 892        struct acpi_power_resource *resource;
 893
 894        resource = container_of(device, struct acpi_power_resource, device);
 895
 896        mutex_lock(&power_resource_list_lock);
 897        list_del(&resource->list_node);
 898        mutex_unlock(&power_resource_list_lock);
 899
 900        acpi_free_pnp_ids(&device->pnp);
 901        kfree(resource);
 902}
 903
 904static ssize_t acpi_power_in_use_show(struct device *dev,
 905                                      struct device_attribute *attr,
 906                                      char *buf) {
 907        struct acpi_power_resource *resource;
 908
 909        resource = to_power_resource(to_acpi_device(dev));
 910        return sprintf(buf, "%u\n", !!resource->ref_count);
 911}
 912static DEVICE_ATTR(resource_in_use, 0444, acpi_power_in_use_show, NULL);
 913
 914static void acpi_power_sysfs_remove(struct acpi_device *device)
 915{
 916        device_remove_file(&device->dev, &dev_attr_resource_in_use);
 917}
 918
 919static void acpi_power_add_resource_to_list(struct acpi_power_resource *resource)
 920{
 921        mutex_lock(&power_resource_list_lock);
 922
 923        if (!list_empty(&acpi_power_resource_list)) {
 924                struct acpi_power_resource *r;
 925
 926                list_for_each_entry(r, &acpi_power_resource_list, list_node)
 927                        if (r->order > resource->order) {
 928                                list_add_tail(&resource->list_node, &r->list_node);
 929                                goto out;
 930                        }
 931        }
 932        list_add_tail(&resource->list_node, &acpi_power_resource_list);
 933
 934 out:
 935        mutex_unlock(&power_resource_list_lock);
 936}
 937
 938int acpi_add_power_resource(acpi_handle handle)
 939{
 940        struct acpi_power_resource *resource;
 941        struct acpi_device *device = NULL;
 942        union acpi_object acpi_object;
 943        struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
 944        acpi_status status;
 945        int state, result = -ENODEV;
 946
 947        acpi_bus_get_device(handle, &device);
 948        if (device)
 949                return 0;
 950
 951        resource = kzalloc(sizeof(*resource), GFP_KERNEL);
 952        if (!resource)
 953                return -ENOMEM;
 954
 955        device = &resource->device;
 956        acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER,
 957                                ACPI_STA_DEFAULT);
 958        mutex_init(&resource->resource_lock);
 959        INIT_LIST_HEAD(&resource->list_node);
 960        INIT_LIST_HEAD(&resource->dependents);
 961        resource->name = device->pnp.bus_id;
 962        strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
 963        strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
 964        device->power.state = ACPI_STATE_UNKNOWN;
 965
 966        /* Evalute the object to get the system level and resource order. */
 967        status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
 968        if (ACPI_FAILURE(status))
 969                goto err;
 970
 971        resource->system_level = acpi_object.power_resource.system_level;
 972        resource->order = acpi_object.power_resource.resource_order;
 973
 974        result = acpi_power_get_state(handle, &state);
 975        if (result)
 976                goto err;
 977
 978        printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
 979               acpi_device_bid(device), state ? "on" : "off");
 980
 981        device->flags.match_driver = true;
 982        result = acpi_device_add(device, acpi_release_power_resource);
 983        if (result)
 984                goto err;
 985
 986        if (!device_create_file(&device->dev, &dev_attr_resource_in_use))
 987                device->remove = acpi_power_sysfs_remove;
 988
 989        acpi_power_add_resource_to_list(resource);
 990        acpi_device_add_finalize(device);
 991        return 0;
 992
 993 err:
 994        acpi_release_power_resource(&device->dev);
 995        return result;
 996}
 997
 998#ifdef CONFIG_ACPI_SLEEP
 999void acpi_resume_power_resources(void)
1000{
1001        struct acpi_power_resource *resource;
1002
1003        mutex_lock(&power_resource_list_lock);
1004
1005        list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
1006                int result, state;
1007
1008                mutex_lock(&resource->resource_lock);
1009
1010                result = acpi_power_get_state(resource->device.handle, &state);
1011                if (result) {
1012                        mutex_unlock(&resource->resource_lock);
1013                        continue;
1014                }
1015
1016                if (state == ACPI_POWER_RESOURCE_STATE_OFF
1017                    && resource->ref_count) {
1018                        dev_info(&resource->device.dev, "Turning ON\n");
1019                        __acpi_power_on(resource);
1020                }
1021
1022                mutex_unlock(&resource->resource_lock);
1023        }
1024
1025        mutex_unlock(&power_resource_list_lock);
1026}
1027
1028void acpi_turn_off_unused_power_resources(void)
1029{
1030        struct acpi_power_resource *resource;
1031
1032        mutex_lock(&power_resource_list_lock);
1033
1034        list_for_each_entry_reverse(resource, &acpi_power_resource_list, list_node) {
1035                int result, state;
1036
1037                mutex_lock(&resource->resource_lock);
1038
1039                result = acpi_power_get_state(resource->device.handle, &state);
1040                if (result) {
1041                        mutex_unlock(&resource->resource_lock);
1042                        continue;
1043                }
1044
1045                if (state == ACPI_POWER_RESOURCE_STATE_ON
1046                    && !resource->ref_count) {
1047                        dev_info(&resource->device.dev, "Turning OFF\n");
1048                        __acpi_power_off(resource);
1049                }
1050
1051                mutex_unlock(&resource->resource_lock);
1052        }
1053
1054        mutex_unlock(&power_resource_list_lock);
1055}
1056#endif
1057