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