Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * acpi_power.c - ACPI Bus Power Management ($Revision: 39 $) |
| 3 | * |
| 4 | * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> |
| 5 | * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> |
| 6 | * |
| 7 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or (at |
| 12 | * your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 22 | * |
| 23 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 24 | */ |
| 25 | |
| 26 | /* |
| 27 | * ACPI power-managed devices may be controlled in two ways: |
| 28 | * 1. via "Device Specific (D-State) Control" |
| 29 | * 2. via "Power Resource Control". |
| 30 | * This module is used to manage devices relying on Power Resource Control. |
| 31 | * |
| 32 | * An ACPI "power resource object" describes a software controllable power |
| 33 | * plane, clock plane, or other resource used by a power managed device. |
| 34 | * A device may rely on multiple power resources, and a power resource |
| 35 | * may be shared by multiple devices. |
| 36 | */ |
| 37 | |
| 38 | #include <linux/kernel.h> |
| 39 | #include <linux/module.h> |
| 40 | #include <linux/init.h> |
| 41 | #include <linux/types.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 42 | #include <linux/slab.h> |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 43 | #include <linux/pm_runtime.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <acpi/acpi_bus.h> |
| 45 | #include <acpi/acpi_drivers.h> |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 46 | #include "sleep.h" |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 47 | #include "internal.h" |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 48 | |
Len Brown | a192a95 | 2009-07-28 16:45:54 -0400 | [diff] [blame] | 49 | #define PREFIX "ACPI: " |
| 50 | |
Bjorn Helgaas | 89595b8 | 2008-11-07 16:57:45 -0700 | [diff] [blame] | 51 | #define _COMPONENT ACPI_POWER_COMPONENT |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 52 | ACPI_MODULE_NAME("power"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #define ACPI_POWER_CLASS "power_resource" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #define ACPI_POWER_DEVICE_NAME "Power Resource" |
| 55 | #define ACPI_POWER_FILE_INFO "info" |
| 56 | #define ACPI_POWER_FILE_STATUS "state" |
| 57 | #define ACPI_POWER_RESOURCE_STATE_OFF 0x00 |
| 58 | #define ACPI_POWER_RESOURCE_STATE_ON 0x01 |
| 59 | #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF |
Zhao Yakui | f5adfaa | 2008-08-11 14:57:50 +0800 | [diff] [blame] | 60 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 61 | struct acpi_power_dependent_device { |
| 62 | struct list_head node; |
| 63 | struct acpi_device *adev; |
| 64 | struct work_struct work; |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 65 | }; |
| 66 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 67 | struct acpi_power_resource { |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 68 | struct acpi_device device; |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 69 | struct list_head list_node; |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 70 | struct list_head dependent; |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 71 | char *name; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 72 | u32 system_level; |
| 73 | u32 order; |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 74 | unsigned int ref_count; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 75 | struct mutex resource_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 78 | struct acpi_power_resource_entry { |
| 79 | struct list_head node; |
| 80 | struct acpi_power_resource *resource; |
| 81 | }; |
| 82 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 83 | static LIST_HEAD(acpi_power_resource_list); |
| 84 | static DEFINE_MUTEX(power_resource_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | /* -------------------------------------------------------------------------- |
| 87 | Power Resource Management |
| 88 | -------------------------------------------------------------------------- */ |
| 89 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 90 | static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 92 | struct acpi_device *device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 94 | if (acpi_bus_get_device(handle, &device)) |
| 95 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 97 | return container_of(device, struct acpi_power_resource, device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 100 | static int acpi_power_resources_list_add(acpi_handle handle, |
| 101 | struct list_head *list) |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 102 | { |
| 103 | struct acpi_power_resource *resource = acpi_power_get_context(handle); |
| 104 | struct acpi_power_resource_entry *entry; |
| 105 | |
| 106 | if (!resource || !list) |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 107 | return -EINVAL; |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 108 | |
| 109 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 110 | if (!entry) |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 111 | return -ENOMEM; |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 112 | |
| 113 | entry->resource = resource; |
| 114 | if (!list_empty(list)) { |
| 115 | struct acpi_power_resource_entry *e; |
| 116 | |
| 117 | list_for_each_entry(e, list, node) |
| 118 | if (e->resource->order > resource->order) { |
| 119 | list_add_tail(&entry->node, &e->node); |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 120 | return 0; |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | list_add_tail(&entry->node, list); |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 124 | return 0; |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void acpi_power_resources_list_free(struct list_head *list) |
| 128 | { |
| 129 | struct acpi_power_resource_entry *entry, *e; |
| 130 | |
| 131 | list_for_each_entry_safe(entry, e, list, node) { |
| 132 | list_del(&entry->node); |
| 133 | kfree(entry); |
| 134 | } |
| 135 | } |
| 136 | |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 137 | int acpi_extract_power_resources(union acpi_object *package, unsigned int start, |
| 138 | struct list_head *list) |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 139 | { |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 140 | unsigned int i; |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 141 | int err = 0; |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 142 | |
| 143 | for (i = start; i < package->package.count; i++) { |
| 144 | union acpi_object *element = &package->package.elements[i]; |
| 145 | acpi_handle rhandle; |
| 146 | |
| 147 | if (element->type != ACPI_TYPE_LOCAL_REFERENCE) { |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 148 | err = -ENODATA; |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 149 | break; |
| 150 | } |
| 151 | rhandle = element->reference.handle; |
| 152 | if (!rhandle) { |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 153 | err = -ENODEV; |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 154 | break; |
| 155 | } |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 156 | err = acpi_add_power_resource(rhandle); |
| 157 | if (err) |
| 158 | break; |
| 159 | |
| 160 | err = acpi_power_resources_list_add(rhandle, list); |
| 161 | if (err) |
| 162 | break; |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 163 | } |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 164 | if (err) |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 165 | acpi_power_resources_list_free(list); |
| 166 | |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 167 | return err; |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Zhao Yakui | a51e145 | 2008-08-11 14:55:05 +0800 | [diff] [blame] | 170 | static int acpi_power_get_state(acpi_handle handle, int *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 172 | acpi_status status = AE_OK; |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 173 | unsigned long long sta = 0; |
Lin Ming | 60a4ce7 | 2008-12-16 17:02:22 +0800 | [diff] [blame] | 174 | char node_name[5]; |
| 175 | struct acpi_buffer buffer = { sizeof(node_name), node_name }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Zhao Yakui | a51e145 | 2008-08-11 14:55:05 +0800 | [diff] [blame] | 178 | if (!handle || !state) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 179 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
Zhao Yakui | a51e145 | 2008-08-11 14:55:05 +0800 | [diff] [blame] | 181 | status = acpi_evaluate_integer(handle, "_STA", NULL, &sta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | if (ACPI_FAILURE(status)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 183 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
Alexey Starikovskiy | c35923b | 2007-10-22 14:19:09 +0400 | [diff] [blame] | 185 | *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON: |
| 186 | ACPI_POWER_RESOURCE_STATE_OFF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Lin Ming | 60a4ce7 | 2008-12-16 17:02:22 +0800 | [diff] [blame] | 188 | acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer); |
| 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n", |
Lin Ming | 60a4ce7 | 2008-12-16 17:02:22 +0800 | [diff] [blame] | 191 | node_name, |
Zhao Yakui | b1b57fb | 2008-10-27 16:04:53 +0800 | [diff] [blame] | 192 | *state ? "on" : "off")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 194 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 197 | static int acpi_power_get_list_state(struct list_head *list, int *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 199 | struct acpi_power_resource_entry *entry; |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 200 | int cur_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | if (!list || !state) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 203 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
| 205 | /* The state of the list is 'on' IFF all resources are 'on'. */ |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 206 | list_for_each_entry(entry, list, node) { |
| 207 | struct acpi_power_resource *resource = entry->resource; |
| 208 | acpi_handle handle = resource->device.handle; |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 209 | int result; |
| 210 | |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 211 | mutex_lock(&resource->resource_lock); |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 212 | result = acpi_power_get_state(handle, &cur_state); |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 213 | mutex_unlock(&resource->resource_lock); |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 214 | if (result) |
| 215 | return result; |
| 216 | |
| 217 | if (cur_state != ACPI_POWER_RESOURCE_STATE_ON) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | break; |
| 219 | } |
| 220 | |
| 221 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n", |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 222 | cur_state ? "on" : "off")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 224 | *state = cur_state; |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 225 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 228 | static void acpi_power_resume_dependent(struct work_struct *work) |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 229 | { |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 230 | struct acpi_power_dependent_device *dep; |
| 231 | struct acpi_device_physical_node *pn; |
| 232 | struct acpi_device *adev; |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 233 | int state; |
| 234 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 235 | dep = container_of(work, struct acpi_power_dependent_device, work); |
| 236 | adev = dep->adev; |
| 237 | if (acpi_power_get_inferred_state(adev, &state)) |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 238 | return; |
| 239 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 240 | if (state > ACPI_STATE_D0) |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 241 | return; |
| 242 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 243 | mutex_lock(&adev->physical_node_lock); |
| 244 | |
| 245 | list_for_each_entry(pn, &adev->physical_node_list, node) |
| 246 | pm_request_resume(pn->dev); |
| 247 | |
| 248 | list_for_each_entry(pn, &adev->power_dependent, node) |
| 249 | pm_request_resume(pn->dev); |
| 250 | |
| 251 | mutex_unlock(&adev->physical_node_lock); |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 252 | } |
| 253 | |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 254 | static int __acpi_power_on(struct acpi_power_resource *resource) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 256 | acpi_status status = AE_OK; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 257 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 258 | status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | if (ACPI_FAILURE(status)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 260 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 262 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 263 | resource->name)); |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 264 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 265 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 268 | static int acpi_power_on(struct acpi_power_resource *resource) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | { |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 270 | int result = 0;; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 272 | mutex_lock(&resource->resource_lock); |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 273 | |
| 274 | if (resource->ref_count++) { |
| 275 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 276 | "Power resource [%s] already on", |
| 277 | resource->name)); |
| 278 | } else { |
| 279 | result = __acpi_power_on(resource); |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 280 | if (result) { |
Rafael J. Wysocki | 12b3b5a | 2010-11-25 00:03:32 +0100 | [diff] [blame] | 281 | resource->ref_count--; |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 282 | } else { |
| 283 | struct acpi_power_dependent_device *dep; |
| 284 | |
| 285 | list_for_each_entry(dep, &resource->dependent, node) |
| 286 | schedule_work(&dep->work); |
| 287 | } |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 288 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 290 | mutex_unlock(&resource->resource_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
Rafael J. Wysocki | 12b3b5a | 2010-11-25 00:03:32 +0100 | [diff] [blame] | 292 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 295 | static int acpi_power_off(struct acpi_power_resource *resource) |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 296 | { |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 297 | acpi_status status = AE_OK; |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 298 | int result = 0; |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 299 | |
| 300 | mutex_lock(&resource->resource_lock); |
| 301 | |
| 302 | if (!resource->ref_count) { |
| 303 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 304 | "Power resource [%s] already off", |
| 305 | resource->name)); |
| 306 | goto unlock; |
| 307 | } |
| 308 | |
| 309 | if (--resource->ref_count) { |
| 310 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 311 | "Power resource [%s] still in use\n", |
| 312 | resource->name)); |
| 313 | goto unlock; |
| 314 | } |
| 315 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 316 | status = acpi_evaluate_object(resource->device.handle, "_OFF", NULL, NULL); |
Rafael J. Wysocki | 722c929 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 317 | if (ACPI_FAILURE(status)) |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 318 | result = -ENODEV; |
Rafael J. Wysocki | 722c929 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 319 | else |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 320 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 321 | "Power resource [%s] turned off\n", |
| 322 | resource->name)); |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 323 | |
| 324 | unlock: |
| 325 | mutex_unlock(&resource->resource_lock); |
| 326 | |
| 327 | return result; |
| 328 | } |
| 329 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 330 | static int acpi_power_off_list(struct list_head *list) |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 331 | { |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 332 | struct acpi_power_resource_entry *entry; |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 333 | int result = 0; |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 334 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 335 | list_for_each_entry_reverse(entry, list, node) { |
| 336 | result = acpi_power_off(entry->resource); |
| 337 | if (result) |
| 338 | goto err; |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 339 | } |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 340 | return 0; |
| 341 | |
| 342 | err: |
| 343 | list_for_each_entry_continue(entry, list, node) |
| 344 | acpi_power_on(entry->resource); |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 345 | |
| 346 | return result; |
| 347 | } |
| 348 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 349 | static int acpi_power_on_list(struct list_head *list) |
| 350 | { |
| 351 | struct acpi_power_resource_entry *entry; |
| 352 | int result = 0; |
| 353 | |
| 354 | list_for_each_entry(entry, list, node) { |
| 355 | result = acpi_power_on(entry->resource); |
| 356 | if (result) |
| 357 | goto err; |
| 358 | } |
| 359 | return 0; |
| 360 | |
| 361 | err: |
| 362 | list_for_each_entry_continue_reverse(entry, list, node) |
| 363 | acpi_power_off(entry->resource); |
| 364 | |
| 365 | return result; |
| 366 | } |
| 367 | |
| 368 | static void acpi_power_add_dependent(struct acpi_power_resource *resource, |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 369 | struct acpi_device *adev) |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 370 | { |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 371 | struct acpi_power_dependent_device *dep; |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 372 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 373 | mutex_lock(&resource->resource_lock); |
| 374 | |
| 375 | list_for_each_entry(dep, &resource->dependent, node) |
| 376 | if (dep->adev == adev) |
| 377 | goto out; |
| 378 | |
| 379 | dep = kzalloc(sizeof(*dep), GFP_KERNEL); |
| 380 | if (!dep) |
| 381 | goto out; |
| 382 | |
| 383 | dep->adev = adev; |
| 384 | INIT_WORK(&dep->work, acpi_power_resume_dependent); |
| 385 | list_add_tail(&dep->node, &resource->dependent); |
| 386 | |
| 387 | out: |
| 388 | mutex_unlock(&resource->resource_lock); |
| 389 | } |
| 390 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 391 | static void acpi_power_remove_dependent(struct acpi_power_resource *resource, |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 392 | struct acpi_device *adev) |
| 393 | { |
| 394 | struct acpi_power_dependent_device *dep; |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 395 | struct work_struct *work = NULL; |
| 396 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 397 | mutex_lock(&resource->resource_lock); |
| 398 | |
| 399 | list_for_each_entry(dep, &resource->dependent, node) |
| 400 | if (dep->adev == adev) { |
| 401 | list_del(&dep->node); |
| 402 | work = &dep->work; |
| 403 | break; |
| 404 | } |
| 405 | |
| 406 | mutex_unlock(&resource->resource_lock); |
| 407 | |
| 408 | if (work) { |
| 409 | cancel_work_sync(work); |
| 410 | kfree(dep); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | void acpi_power_add_remove_device(struct acpi_device *adev, bool add) |
| 415 | { |
| 416 | if (adev->power.flags.power_resources) { |
| 417 | struct acpi_device_power_state *ps; |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 418 | struct acpi_power_resource_entry *entry; |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 419 | |
| 420 | ps = &adev->power.states[ACPI_STATE_D0]; |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 421 | list_for_each_entry(entry, &ps->resources, node) { |
| 422 | struct acpi_power_resource *resource = entry->resource; |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 423 | |
| 424 | if (add) |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 425 | acpi_power_add_dependent(resource, adev); |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 426 | else |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 427 | acpi_power_remove_dependent(resource, adev); |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 428 | } |
| 429 | } |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 430 | } |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 431 | |
Rafael J. Wysocki | 0596a52 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 432 | int acpi_power_min_system_level(struct list_head *list) |
| 433 | { |
| 434 | struct acpi_power_resource_entry *entry; |
| 435 | int system_level = 5; |
| 436 | |
| 437 | list_for_each_entry(entry, list, node) { |
| 438 | struct acpi_power_resource *resource = entry->resource; |
| 439 | |
| 440 | if (system_level > resource->system_level) |
| 441 | system_level = resource->system_level; |
| 442 | } |
| 443 | return system_level; |
| 444 | } |
| 445 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 446 | /* -------------------------------------------------------------------------- |
| 447 | Device Power Management |
| 448 | -------------------------------------------------------------------------- */ |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 449 | |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 450 | /** |
| 451 | * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in |
| 452 | * ACPI 3.0) _PSW (Power State Wake) |
| 453 | * @dev: Device to handle. |
| 454 | * @enable: 0 - disable, 1 - enable the wake capabilities of the device. |
| 455 | * @sleep_state: Target sleep state of the system. |
| 456 | * @dev_state: Target power state of the device. |
| 457 | * |
| 458 | * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power |
| 459 | * State Wake) for the device, if present. On failure reset the device's |
| 460 | * wakeup.flags.valid flag. |
| 461 | * |
| 462 | * RETURN VALUE: |
| 463 | * 0 if either _DSW or _PSW has been successfully executed |
| 464 | * 0 if neither _DSW nor _PSW has been found |
| 465 | * -ENODEV if the execution of either _DSW or _PSW has failed |
| 466 | */ |
| 467 | int acpi_device_sleep_wake(struct acpi_device *dev, |
| 468 | int enable, int sleep_state, int dev_state) |
| 469 | { |
| 470 | union acpi_object in_arg[3]; |
| 471 | struct acpi_object_list arg_list = { 3, in_arg }; |
| 472 | acpi_status status = AE_OK; |
| 473 | |
| 474 | /* |
| 475 | * Try to execute _DSW first. |
| 476 | * |
| 477 | * Three agruments are needed for the _DSW object: |
| 478 | * Argument 0: enable/disable the wake capabilities |
| 479 | * Argument 1: target system state |
| 480 | * Argument 2: target device state |
| 481 | * When _DSW object is called to disable the wake capabilities, maybe |
| 482 | * the first argument is filled. The values of the other two agruments |
| 483 | * are meaningless. |
| 484 | */ |
| 485 | in_arg[0].type = ACPI_TYPE_INTEGER; |
| 486 | in_arg[0].integer.value = enable; |
| 487 | in_arg[1].type = ACPI_TYPE_INTEGER; |
| 488 | in_arg[1].integer.value = sleep_state; |
| 489 | in_arg[2].type = ACPI_TYPE_INTEGER; |
| 490 | in_arg[2].integer.value = dev_state; |
| 491 | status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL); |
| 492 | if (ACPI_SUCCESS(status)) { |
| 493 | return 0; |
| 494 | } else if (status != AE_NOT_FOUND) { |
| 495 | printk(KERN_ERR PREFIX "_DSW execution failed\n"); |
| 496 | dev->wakeup.flags.valid = 0; |
| 497 | return -ENODEV; |
| 498 | } |
| 499 | |
| 500 | /* Execute _PSW */ |
| 501 | arg_list.count = 1; |
| 502 | in_arg[0].integer.value = enable; |
| 503 | status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL); |
| 504 | if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) { |
| 505 | printk(KERN_ERR PREFIX "_PSW execution failed\n"); |
| 506 | dev->wakeup.flags.valid = 0; |
| 507 | return -ENODEV; |
| 508 | } |
| 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | /* |
| 514 | * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229): |
| 515 | * 1. Power on the power resources required for the wakeup device |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 516 | * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power |
| 517 | * State Wake) for the device, if present |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | */ |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 519 | int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | { |
Rafael J. Wysocki | 993cbe5 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 521 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | if (!dev || !dev->wakeup.flags.valid) |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 524 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 526 | mutex_lock(&acpi_device_lock); |
| 527 | |
| 528 | if (dev->wakeup.prepare_count++) |
| 529 | goto out; |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 530 | |
Rafael J. Wysocki | 993cbe5 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 531 | err = acpi_power_on_list(&dev->wakeup.resources); |
| 532 | if (err) { |
| 533 | dev_err(&dev->dev, "Cannot turn wakeup power resources on\n"); |
| 534 | dev->wakeup.flags.valid = 0; |
| 535 | } else { |
| 536 | /* |
| 537 | * Passing 3 as the third argument below means the device may be |
| 538 | * put into arbitrary power state afterward. |
| 539 | */ |
| 540 | err = acpi_device_sleep_wake(dev, 1, sleep_state, 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | } |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 542 | if (err) |
| 543 | dev->wakeup.prepare_count = 0; |
| 544 | |
| 545 | out: |
| 546 | mutex_unlock(&acpi_device_lock); |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 547 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | /* |
| 551 | * Shutdown a wakeup device, counterpart of above method |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 552 | * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power |
| 553 | * State Wake) for the device, if present |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | * 2. Shutdown down the power resources |
| 555 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 556 | int acpi_disable_wakeup_device_power(struct acpi_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | { |
Rafael J. Wysocki | 993cbe5 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 558 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
| 560 | if (!dev || !dev->wakeup.flags.valid) |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 561 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 563 | mutex_lock(&acpi_device_lock); |
| 564 | |
| 565 | if (--dev->wakeup.prepare_count > 0) |
| 566 | goto out; |
| 567 | |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 568 | /* |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 569 | * Executing the code below even if prepare_count is already zero when |
| 570 | * the function is called may be useful, for example for initialisation. |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 571 | */ |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 572 | if (dev->wakeup.prepare_count < 0) |
| 573 | dev->wakeup.prepare_count = 0; |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 574 | |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 575 | err = acpi_device_sleep_wake(dev, 0, 0, 0); |
| 576 | if (err) |
| 577 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
Rafael J. Wysocki | 993cbe5 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 579 | err = acpi_power_off_list(&dev->wakeup.resources); |
| 580 | if (err) { |
| 581 | dev_err(&dev->dev, "Cannot turn wakeup power resources off\n"); |
| 582 | dev->wakeup.flags.valid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 585 | out: |
| 586 | mutex_unlock(&acpi_device_lock); |
| 587 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Rafael J. Wysocki | 32a00d2 | 2010-11-25 00:05:17 +0100 | [diff] [blame] | 590 | int acpi_power_get_inferred_state(struct acpi_device *device, int *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 592 | int result = 0; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 593 | int list_state = 0; |
| 594 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
Rafael J. Wysocki | 32a00d2 | 2010-11-25 00:05:17 +0100 | [diff] [blame] | 596 | if (!device || !state) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 597 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | /* |
| 600 | * We know a device's inferred power state when all the resources |
| 601 | * required for a given D-state are 'on'. |
| 602 | */ |
Rafael J. Wysocki | 38c92ff | 2012-05-20 13:58:00 +0200 | [diff] [blame] | 603 | for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) { |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 604 | struct list_head *list = &device->power.states[i].resources; |
| 605 | |
| 606 | if (list_empty(list)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | continue; |
| 608 | |
| 609 | result = acpi_power_get_list_state(list, &list_state); |
| 610 | if (result) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 611 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
| 613 | if (list_state == ACPI_POWER_RESOURCE_STATE_ON) { |
Rafael J. Wysocki | 32a00d2 | 2010-11-25 00:05:17 +0100 | [diff] [blame] | 614 | *state = i; |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 615 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
Rafael J. Wysocki | 32a00d2 | 2010-11-25 00:05:17 +0100 | [diff] [blame] | 619 | *state = ACPI_STATE_D3; |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 620 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Rafael J. Wysocki | 30d3df41 | 2010-11-25 00:06:55 +0100 | [diff] [blame] | 623 | int acpi_power_on_resources(struct acpi_device *device, int state) |
| 624 | { |
Rafael J. Wysocki | 87e753b | 2013-01-22 12:56:16 +0100 | [diff] [blame^] | 625 | if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT) |
Rafael J. Wysocki | 30d3df41 | 2010-11-25 00:06:55 +0100 | [diff] [blame] | 626 | return -EINVAL; |
| 627 | |
| 628 | return acpi_power_on_list(&device->power.states[state].resources); |
| 629 | } |
| 630 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 631 | int acpi_power_transition(struct acpi_device *device, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | { |
Rafael J. Wysocki | 5c7dd71 | 2012-05-18 00:39:35 +0200 | [diff] [blame] | 633 | int result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
Zhang Rui | 3ebc81b | 2012-03-29 14:09:38 +0800 | [diff] [blame] | 635 | if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 636 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 638 | if (device->power.state == state || !device->flags.power_manageable) |
Rafael J. Wysocki | 212967c | 2010-11-25 00:02:36 +0100 | [diff] [blame] | 639 | return 0; |
| 640 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 641 | if ((device->power.state < ACPI_STATE_D0) |
Zhang Rui | 3ebc81b | 2012-03-29 14:09:38 +0800 | [diff] [blame] | 642 | || (device->power.state > ACPI_STATE_D3_COLD)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 643 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | /* TBD: Resources must be ordered. */ |
| 646 | |
| 647 | /* |
| 648 | * First we reference all power resources required in the target list |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 649 | * (e.g. so the device doesn't lose power while transitioning). Then, |
| 650 | * we dereference all power resources used in the current list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | */ |
Rafael J. Wysocki | 5c7dd71 | 2012-05-18 00:39:35 +0200 | [diff] [blame] | 652 | if (state < ACPI_STATE_D3_COLD) |
| 653 | result = acpi_power_on_list( |
| 654 | &device->power.states[state].resources); |
| 655 | |
| 656 | if (!result && device->power.state < ACPI_STATE_D3_COLD) |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 657 | acpi_power_off_list( |
| 658 | &device->power.states[device->power.state].resources); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 660 | /* We shouldn't change the state unless the above operations succeed. */ |
| 661 | device->power.state = result ? ACPI_STATE_UNKNOWN : state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 663 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 666 | static void acpi_release_power_resource(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | { |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 668 | struct acpi_device *device = to_acpi_device(dev); |
| 669 | struct acpi_power_resource *resource; |
| 670 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 671 | resource = container_of(device, struct acpi_power_resource, device); |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 672 | |
| 673 | mutex_lock(&power_resource_list_lock); |
| 674 | list_del(&resource->list_node); |
| 675 | mutex_unlock(&power_resource_list_lock); |
| 676 | |
| 677 | acpi_free_ids(device); |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 678 | kfree(resource); |
| 679 | } |
| 680 | |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 681 | int acpi_add_power_resource(acpi_handle handle) |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 682 | { |
| 683 | struct acpi_power_resource *resource; |
| 684 | struct acpi_device *device = NULL; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 685 | union acpi_object acpi_object; |
| 686 | struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object }; |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 687 | acpi_status status; |
| 688 | int state, result = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 690 | acpi_bus_get_device(handle, &device); |
| 691 | if (device) |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 692 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 694 | resource = kzalloc(sizeof(*resource), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | if (!resource) |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 696 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 698 | device = &resource->device; |
| 699 | acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER, |
| 700 | ACPI_STA_DEFAULT); |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 701 | mutex_init(&resource->resource_lock); |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 702 | INIT_LIST_HEAD(&resource->dependent); |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 703 | resource->name = device->pnp.bus_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME); |
| 705 | strcpy(acpi_device_class(device), ACPI_POWER_CLASS); |
Rafael J. Wysocki | 722c929 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 706 | device->power.state = ACPI_STATE_UNKNOWN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | |
| 708 | /* Evalute the object to get the system level and resource order. */ |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 709 | status = acpi_evaluate_object(handle, NULL, NULL, &buffer); |
| 710 | if (ACPI_FAILURE(status)) |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 711 | goto err; |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 712 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | resource->system_level = acpi_object.power_resource.system_level; |
| 714 | resource->order = acpi_object.power_resource.resource_order; |
| 715 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 716 | result = acpi_power_get_state(handle, &state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | if (result) |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 718 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 720 | printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device), |
Alexey Starikovskiy | c35923b | 2007-10-22 14:19:09 +0400 | [diff] [blame] | 721 | acpi_device_bid(device), state ? "on" : "off"); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 722 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 723 | device->flags.match_driver = true; |
| 724 | result = acpi_device_register(device, acpi_release_power_resource); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | if (result) |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 726 | goto err; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 727 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 728 | mutex_lock(&power_resource_list_lock); |
| 729 | list_add(&resource->list_node, &acpi_power_resource_list); |
| 730 | mutex_unlock(&power_resource_list_lock); |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 731 | return 0; |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 732 | |
| 733 | err: |
| 734 | acpi_release_power_resource(&device->dev); |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 735 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 738 | #ifdef CONFIG_ACPI_SLEEP |
| 739 | void acpi_resume_power_resources(void) |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 740 | { |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 741 | struct acpi_power_resource *resource; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 742 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 743 | mutex_lock(&power_resource_list_lock); |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 744 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 745 | list_for_each_entry(resource, &acpi_power_resource_list, list_node) { |
| 746 | int result, state; |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 747 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 748 | mutex_lock(&resource->resource_lock); |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 749 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 750 | result = acpi_power_get_state(resource->device.handle, &state); |
| 751 | if (!result && state == ACPI_POWER_RESOURCE_STATE_OFF |
| 752 | && resource->ref_count) { |
| 753 | dev_info(&resource->device.dev, "Turning ON\n"); |
| 754 | __acpi_power_on(resource); |
| 755 | } |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 756 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 757 | mutex_unlock(&resource->resource_lock); |
| 758 | } |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 759 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 760 | mutex_unlock(&power_resource_list_lock); |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 761 | } |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 762 | #endif |