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