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> |
Rafael J. Wysocki | 18a3870 | 2013-01-25 21:51:32 +0100 | [diff] [blame] | 44 | #include <linux/sysfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <acpi/acpi_bus.h> |
| 46 | #include <acpi/acpi_drivers.h> |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 47 | #include "sleep.h" |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 48 | #include "internal.h" |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 49 | |
Len Brown | a192a95 | 2009-07-28 16:45:54 -0400 | [diff] [blame] | 50 | #define PREFIX "ACPI: " |
| 51 | |
Bjorn Helgaas | 89595b8 | 2008-11-07 16:57:45 -0700 | [diff] [blame] | 52 | #define _COMPONENT ACPI_POWER_COMPONENT |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 53 | ACPI_MODULE_NAME("power"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #define ACPI_POWER_CLASS "power_resource" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #define ACPI_POWER_DEVICE_NAME "Power Resource" |
| 56 | #define ACPI_POWER_FILE_INFO "info" |
| 57 | #define ACPI_POWER_FILE_STATUS "state" |
| 58 | #define ACPI_POWER_RESOURCE_STATE_OFF 0x00 |
| 59 | #define ACPI_POWER_RESOURCE_STATE_ON 0x01 |
| 60 | #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF |
Zhao Yakui | f5adfaa | 2008-08-11 14:57:50 +0800 | [diff] [blame] | 61 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 62 | struct acpi_power_dependent_device { |
| 63 | struct list_head node; |
| 64 | struct acpi_device *adev; |
| 65 | struct work_struct work; |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 66 | }; |
| 67 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 68 | struct acpi_power_resource { |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 69 | struct acpi_device device; |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 70 | struct list_head list_node; |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 71 | struct list_head dependent; |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 72 | char *name; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 73 | u32 system_level; |
| 74 | u32 order; |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 75 | unsigned int ref_count; |
Rafael J. Wysocki | b5d667e | 2013-02-23 23:15:21 +0100 | [diff] [blame] | 76 | bool wakeup_enabled; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 77 | struct mutex resource_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 80 | struct acpi_power_resource_entry { |
| 81 | struct list_head node; |
| 82 | struct acpi_power_resource *resource; |
| 83 | }; |
| 84 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 85 | static LIST_HEAD(acpi_power_resource_list); |
| 86 | static DEFINE_MUTEX(power_resource_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | /* -------------------------------------------------------------------------- |
| 89 | Power Resource Management |
| 90 | -------------------------------------------------------------------------- */ |
| 91 | |
Rafael J. Wysocki | b1c0f99 | 2013-01-24 12:50:09 +0100 | [diff] [blame] | 92 | static inline |
| 93 | struct acpi_power_resource *to_power_resource(struct acpi_device *device) |
| 94 | { |
| 95 | return container_of(device, struct acpi_power_resource, device); |
| 96 | } |
| 97 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 98 | static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | { |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 100 | struct acpi_device *device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 102 | if (acpi_bus_get_device(handle, &device)) |
| 103 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Rafael J. Wysocki | b1c0f99 | 2013-01-24 12:50:09 +0100 | [diff] [blame] | 105 | return to_power_resource(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 108 | static int acpi_power_resources_list_add(acpi_handle handle, |
| 109 | struct list_head *list) |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 110 | { |
| 111 | struct acpi_power_resource *resource = acpi_power_get_context(handle); |
| 112 | struct acpi_power_resource_entry *entry; |
| 113 | |
| 114 | if (!resource || !list) |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 115 | return -EINVAL; |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 116 | |
| 117 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 118 | if (!entry) |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 119 | return -ENOMEM; |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 120 | |
| 121 | entry->resource = resource; |
| 122 | if (!list_empty(list)) { |
| 123 | struct acpi_power_resource_entry *e; |
| 124 | |
| 125 | list_for_each_entry(e, list, node) |
| 126 | if (e->resource->order > resource->order) { |
| 127 | list_add_tail(&entry->node, &e->node); |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 128 | return 0; |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | list_add_tail(&entry->node, list); |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 132 | return 0; |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void acpi_power_resources_list_free(struct list_head *list) |
| 136 | { |
| 137 | struct acpi_power_resource_entry *entry, *e; |
| 138 | |
| 139 | list_for_each_entry_safe(entry, e, list, node) { |
| 140 | list_del(&entry->node); |
| 141 | kfree(entry); |
| 142 | } |
| 143 | } |
| 144 | |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 145 | int acpi_extract_power_resources(union acpi_object *package, unsigned int start, |
| 146 | struct list_head *list) |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 147 | { |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 148 | unsigned int i; |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 149 | int err = 0; |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 150 | |
| 151 | for (i = start; i < package->package.count; i++) { |
| 152 | union acpi_object *element = &package->package.elements[i]; |
| 153 | acpi_handle rhandle; |
| 154 | |
| 155 | if (element->type != ACPI_TYPE_LOCAL_REFERENCE) { |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 156 | err = -ENODATA; |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 157 | break; |
| 158 | } |
| 159 | rhandle = element->reference.handle; |
| 160 | if (!rhandle) { |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 161 | err = -ENODEV; |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 162 | break; |
| 163 | } |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 164 | err = acpi_add_power_resource(rhandle); |
| 165 | if (err) |
| 166 | break; |
| 167 | |
| 168 | err = acpi_power_resources_list_add(rhandle, list); |
| 169 | if (err) |
| 170 | break; |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 171 | } |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 172 | if (err) |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 173 | acpi_power_resources_list_free(list); |
| 174 | |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 175 | return err; |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Zhao Yakui | a51e145 | 2008-08-11 14:55:05 +0800 | [diff] [blame] | 178 | static int acpi_power_get_state(acpi_handle handle, int *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 180 | acpi_status status = AE_OK; |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 181 | unsigned long long sta = 0; |
Lin Ming | 60a4ce7 | 2008-12-16 17:02:22 +0800 | [diff] [blame] | 182 | char node_name[5]; |
| 183 | struct acpi_buffer buffer = { sizeof(node_name), node_name }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
Zhao Yakui | a51e145 | 2008-08-11 14:55:05 +0800 | [diff] [blame] | 186 | if (!handle || !state) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 187 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
Zhao Yakui | a51e145 | 2008-08-11 14:55:05 +0800 | [diff] [blame] | 189 | status = acpi_evaluate_integer(handle, "_STA", NULL, &sta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | if (ACPI_FAILURE(status)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 191 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
Alexey Starikovskiy | c35923b | 2007-10-22 14:19:09 +0400 | [diff] [blame] | 193 | *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON: |
| 194 | ACPI_POWER_RESOURCE_STATE_OFF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
Lin Ming | 60a4ce7 | 2008-12-16 17:02:22 +0800 | [diff] [blame] | 196 | acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer); |
| 197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n", |
Lin Ming | 60a4ce7 | 2008-12-16 17:02:22 +0800 | [diff] [blame] | 199 | node_name, |
Zhao Yakui | b1b57fb | 2008-10-27 16:04:53 +0800 | [diff] [blame] | 200 | *state ? "on" : "off")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 202 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 205 | 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] | 206 | { |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 207 | struct acpi_power_resource_entry *entry; |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 208 | int cur_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | if (!list || !state) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 211 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | /* 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] | 214 | list_for_each_entry(entry, list, node) { |
| 215 | struct acpi_power_resource *resource = entry->resource; |
| 216 | acpi_handle handle = resource->device.handle; |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 217 | int result; |
| 218 | |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 219 | mutex_lock(&resource->resource_lock); |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 220 | result = acpi_power_get_state(handle, &cur_state); |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 221 | mutex_unlock(&resource->resource_lock); |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 222 | if (result) |
| 223 | return result; |
| 224 | |
| 225 | if (cur_state != ACPI_POWER_RESOURCE_STATE_ON) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | break; |
| 227 | } |
| 228 | |
| 229 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n", |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 230 | cur_state ? "on" : "off")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 232 | *state = cur_state; |
Rafael J. Wysocki | d0515d9 | 2011-01-06 23:38:57 +0100 | [diff] [blame] | 233 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 236 | static void acpi_power_resume_dependent(struct work_struct *work) |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 237 | { |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 238 | struct acpi_power_dependent_device *dep; |
| 239 | struct acpi_device_physical_node *pn; |
| 240 | struct acpi_device *adev; |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 241 | int state; |
| 242 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 243 | dep = container_of(work, struct acpi_power_dependent_device, work); |
| 244 | adev = dep->adev; |
| 245 | if (acpi_power_get_inferred_state(adev, &state)) |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 246 | return; |
| 247 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 248 | if (state > ACPI_STATE_D0) |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 249 | return; |
| 250 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 251 | mutex_lock(&adev->physical_node_lock); |
| 252 | |
| 253 | list_for_each_entry(pn, &adev->physical_node_list, node) |
| 254 | pm_request_resume(pn->dev); |
| 255 | |
| 256 | list_for_each_entry(pn, &adev->power_dependent, node) |
| 257 | pm_request_resume(pn->dev); |
| 258 | |
| 259 | mutex_unlock(&adev->physical_node_lock); |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 260 | } |
| 261 | |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 262 | static int __acpi_power_on(struct acpi_power_resource *resource) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 264 | acpi_status status = AE_OK; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 265 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 266 | status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | if (ACPI_FAILURE(status)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 268 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 270 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 271 | resource->name)); |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 272 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 273 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Rafael J. Wysocki | b5d667e | 2013-02-23 23:15:21 +0100 | [diff] [blame] | 276 | static int acpi_power_on_unlocked(struct acpi_power_resource *resource) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | { |
Rafael J. Wysocki | b5d667e | 2013-02-23 23:15:21 +0100 | [diff] [blame] | 278 | int result = 0; |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 279 | |
| 280 | if (resource->ref_count++) { |
| 281 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 282 | "Power resource [%s] already on", |
| 283 | resource->name)); |
| 284 | } else { |
| 285 | result = __acpi_power_on(resource); |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 286 | if (result) { |
Rafael J. Wysocki | 12b3b5a | 2010-11-25 00:03:32 +0100 | [diff] [blame] | 287 | resource->ref_count--; |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 288 | } else { |
| 289 | struct acpi_power_dependent_device *dep; |
| 290 | |
| 291 | list_for_each_entry(dep, &resource->dependent, node) |
| 292 | schedule_work(&dep->work); |
| 293 | } |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 294 | } |
Rafael J. Wysocki | b5d667e | 2013-02-23 23:15:21 +0100 | [diff] [blame] | 295 | return result; |
| 296 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
Rafael J. Wysocki | b5d667e | 2013-02-23 23:15:21 +0100 | [diff] [blame] | 298 | static int acpi_power_on(struct acpi_power_resource *resource) |
| 299 | { |
| 300 | int result; |
| 301 | |
| 302 | mutex_lock(&resource->resource_lock); |
| 303 | result = acpi_power_on_unlocked(resource); |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 304 | mutex_unlock(&resource->resource_lock); |
Rafael J. Wysocki | 12b3b5a | 2010-11-25 00:03:32 +0100 | [diff] [blame] | 305 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Rafael J. Wysocki | 660b111 | 2013-01-25 21:51:57 +0100 | [diff] [blame] | 308 | static int __acpi_power_off(struct acpi_power_resource *resource) |
| 309 | { |
| 310 | acpi_status status; |
| 311 | |
| 312 | status = acpi_evaluate_object(resource->device.handle, "_OFF", |
| 313 | NULL, NULL); |
| 314 | if (ACPI_FAILURE(status)) |
| 315 | return -ENODEV; |
| 316 | |
| 317 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned off\n", |
| 318 | resource->name)); |
| 319 | return 0; |
| 320 | } |
| 321 | |
Rafael J. Wysocki | b5d667e | 2013-02-23 23:15:21 +0100 | [diff] [blame] | 322 | static int acpi_power_off_unlocked(struct acpi_power_resource *resource) |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 323 | { |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 324 | int result = 0; |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 325 | |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 326 | if (!resource->ref_count) { |
| 327 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 328 | "Power resource [%s] already off", |
| 329 | resource->name)); |
Rafael J. Wysocki | b5d667e | 2013-02-23 23:15:21 +0100 | [diff] [blame] | 330 | return 0; |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | if (--resource->ref_count) { |
| 334 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 335 | "Power resource [%s] still in use\n", |
| 336 | resource->name)); |
Rafael J. Wysocki | 660b111 | 2013-01-25 21:51:57 +0100 | [diff] [blame] | 337 | } else { |
| 338 | result = __acpi_power_off(resource); |
| 339 | if (result) |
| 340 | resource->ref_count++; |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 341 | } |
Rafael J. Wysocki | b5d667e | 2013-02-23 23:15:21 +0100 | [diff] [blame] | 342 | return result; |
| 343 | } |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 344 | |
Rafael J. Wysocki | b5d667e | 2013-02-23 23:15:21 +0100 | [diff] [blame] | 345 | static int acpi_power_off(struct acpi_power_resource *resource) |
| 346 | { |
| 347 | int result; |
| 348 | |
| 349 | mutex_lock(&resource->resource_lock); |
| 350 | result = acpi_power_off_unlocked(resource); |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 351 | mutex_unlock(&resource->resource_lock); |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 352 | return result; |
| 353 | } |
| 354 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 355 | static int acpi_power_off_list(struct list_head *list) |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 356 | { |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 357 | struct acpi_power_resource_entry *entry; |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 358 | int result = 0; |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 359 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 360 | list_for_each_entry_reverse(entry, list, node) { |
| 361 | result = acpi_power_off(entry->resource); |
| 362 | if (result) |
| 363 | goto err; |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 364 | } |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 365 | return 0; |
| 366 | |
| 367 | err: |
| 368 | list_for_each_entry_continue(entry, list, node) |
| 369 | acpi_power_on(entry->resource); |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 370 | |
| 371 | return result; |
| 372 | } |
| 373 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 374 | static int acpi_power_on_list(struct list_head *list) |
| 375 | { |
| 376 | struct acpi_power_resource_entry *entry; |
| 377 | int result = 0; |
| 378 | |
| 379 | list_for_each_entry(entry, list, node) { |
| 380 | result = acpi_power_on(entry->resource); |
| 381 | if (result) |
| 382 | goto err; |
| 383 | } |
| 384 | return 0; |
| 385 | |
| 386 | err: |
| 387 | list_for_each_entry_continue_reverse(entry, list, node) |
| 388 | acpi_power_off(entry->resource); |
| 389 | |
| 390 | return result; |
| 391 | } |
| 392 | |
| 393 | static void acpi_power_add_dependent(struct acpi_power_resource *resource, |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 394 | struct acpi_device *adev) |
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 | struct acpi_power_dependent_device *dep; |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 397 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 398 | mutex_lock(&resource->resource_lock); |
| 399 | |
| 400 | list_for_each_entry(dep, &resource->dependent, node) |
| 401 | if (dep->adev == adev) |
| 402 | goto out; |
| 403 | |
| 404 | dep = kzalloc(sizeof(*dep), GFP_KERNEL); |
| 405 | if (!dep) |
| 406 | goto out; |
| 407 | |
| 408 | dep->adev = adev; |
| 409 | INIT_WORK(&dep->work, acpi_power_resume_dependent); |
| 410 | list_add_tail(&dep->node, &resource->dependent); |
| 411 | |
| 412 | out: |
| 413 | mutex_unlock(&resource->resource_lock); |
| 414 | } |
| 415 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 416 | static void acpi_power_remove_dependent(struct acpi_power_resource *resource, |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 417 | struct acpi_device *adev) |
| 418 | { |
| 419 | struct acpi_power_dependent_device *dep; |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 420 | struct work_struct *work = NULL; |
| 421 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 422 | mutex_lock(&resource->resource_lock); |
| 423 | |
| 424 | list_for_each_entry(dep, &resource->dependent, node) |
| 425 | if (dep->adev == adev) { |
| 426 | list_del(&dep->node); |
| 427 | work = &dep->work; |
| 428 | break; |
| 429 | } |
| 430 | |
| 431 | mutex_unlock(&resource->resource_lock); |
| 432 | |
| 433 | if (work) { |
| 434 | cancel_work_sync(work); |
| 435 | kfree(dep); |
| 436 | } |
| 437 | } |
| 438 | |
Rafael J. Wysocki | 18a3870 | 2013-01-25 21:51:32 +0100 | [diff] [blame] | 439 | static struct attribute *attrs[] = { |
| 440 | NULL, |
| 441 | }; |
| 442 | |
| 443 | static struct attribute_group attr_groups[] = { |
| 444 | [ACPI_STATE_D0] = { |
| 445 | .name = "power_resources_D0", |
| 446 | .attrs = attrs, |
| 447 | }, |
| 448 | [ACPI_STATE_D1] = { |
| 449 | .name = "power_resources_D1", |
| 450 | .attrs = attrs, |
| 451 | }, |
| 452 | [ACPI_STATE_D2] = { |
| 453 | .name = "power_resources_D2", |
| 454 | .attrs = attrs, |
| 455 | }, |
| 456 | [ACPI_STATE_D3_HOT] = { |
| 457 | .name = "power_resources_D3hot", |
| 458 | .attrs = attrs, |
| 459 | }, |
| 460 | }; |
| 461 | |
Rafael J. Wysocki | 41a2a46 | 2013-04-11 22:41:48 +0200 | [diff] [blame] | 462 | static struct attribute_group wakeup_attr_group = { |
| 463 | .name = "power_resources_wakeup", |
| 464 | .attrs = attrs, |
| 465 | }; |
| 466 | |
| 467 | static void acpi_power_hide_list(struct acpi_device *adev, |
| 468 | struct list_head *resources, |
| 469 | struct attribute_group *attr_group) |
Rafael J. Wysocki | 18a3870 | 2013-01-25 21:51:32 +0100 | [diff] [blame] | 470 | { |
Rafael J. Wysocki | 18a3870 | 2013-01-25 21:51:32 +0100 | [diff] [blame] | 471 | struct acpi_power_resource_entry *entry; |
| 472 | |
Rafael J. Wysocki | 41a2a46 | 2013-04-11 22:41:48 +0200 | [diff] [blame] | 473 | if (list_empty(resources)) |
Rafael J. Wysocki | 18a3870 | 2013-01-25 21:51:32 +0100 | [diff] [blame] | 474 | return; |
| 475 | |
Rafael J. Wysocki | 41a2a46 | 2013-04-11 22:41:48 +0200 | [diff] [blame] | 476 | list_for_each_entry_reverse(entry, resources, node) { |
Rafael J. Wysocki | 18a3870 | 2013-01-25 21:51:32 +0100 | [diff] [blame] | 477 | struct acpi_device *res_dev = &entry->resource->device; |
| 478 | |
| 479 | sysfs_remove_link_from_group(&adev->dev.kobj, |
Rafael J. Wysocki | 41a2a46 | 2013-04-11 22:41:48 +0200 | [diff] [blame] | 480 | attr_group->name, |
Rafael J. Wysocki | 18a3870 | 2013-01-25 21:51:32 +0100 | [diff] [blame] | 481 | dev_name(&res_dev->dev)); |
| 482 | } |
Rafael J. Wysocki | 41a2a46 | 2013-04-11 22:41:48 +0200 | [diff] [blame] | 483 | sysfs_remove_group(&adev->dev.kobj, attr_group); |
Rafael J. Wysocki | 18a3870 | 2013-01-25 21:51:32 +0100 | [diff] [blame] | 484 | } |
| 485 | |
Rafael J. Wysocki | 41a2a46 | 2013-04-11 22:41:48 +0200 | [diff] [blame] | 486 | static void acpi_power_expose_list(struct acpi_device *adev, |
| 487 | struct list_head *resources, |
| 488 | struct attribute_group *attr_group) |
Rafael J. Wysocki | 18a3870 | 2013-01-25 21:51:32 +0100 | [diff] [blame] | 489 | { |
Rafael J. Wysocki | 18a3870 | 2013-01-25 21:51:32 +0100 | [diff] [blame] | 490 | struct acpi_power_resource_entry *entry; |
| 491 | int ret; |
| 492 | |
Rafael J. Wysocki | 41a2a46 | 2013-04-11 22:41:48 +0200 | [diff] [blame] | 493 | if (list_empty(resources)) |
Rafael J. Wysocki | 18a3870 | 2013-01-25 21:51:32 +0100 | [diff] [blame] | 494 | return; |
| 495 | |
Rafael J. Wysocki | 41a2a46 | 2013-04-11 22:41:48 +0200 | [diff] [blame] | 496 | ret = sysfs_create_group(&adev->dev.kobj, attr_group); |
Rafael J. Wysocki | 18a3870 | 2013-01-25 21:51:32 +0100 | [diff] [blame] | 497 | if (ret) |
| 498 | return; |
| 499 | |
Rafael J. Wysocki | 41a2a46 | 2013-04-11 22:41:48 +0200 | [diff] [blame] | 500 | list_for_each_entry(entry, resources, node) { |
Rafael J. Wysocki | 18a3870 | 2013-01-25 21:51:32 +0100 | [diff] [blame] | 501 | struct acpi_device *res_dev = &entry->resource->device; |
| 502 | |
| 503 | ret = sysfs_add_link_to_group(&adev->dev.kobj, |
Rafael J. Wysocki | 41a2a46 | 2013-04-11 22:41:48 +0200 | [diff] [blame] | 504 | attr_group->name, |
Rafael J. Wysocki | 18a3870 | 2013-01-25 21:51:32 +0100 | [diff] [blame] | 505 | &res_dev->dev.kobj, |
| 506 | dev_name(&res_dev->dev)); |
| 507 | if (ret) { |
Rafael J. Wysocki | 41a2a46 | 2013-04-11 22:41:48 +0200 | [diff] [blame] | 508 | acpi_power_hide_list(adev, resources, attr_group); |
Rafael J. Wysocki | 18a3870 | 2013-01-25 21:51:32 +0100 | [diff] [blame] | 509 | break; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
Rafael J. Wysocki | 41a2a46 | 2013-04-11 22:41:48 +0200 | [diff] [blame] | 514 | static void acpi_power_expose_hide(struct acpi_device *adev, |
| 515 | struct list_head *resources, |
| 516 | struct attribute_group *attr_group, |
| 517 | bool expose) |
| 518 | { |
| 519 | if (expose) |
| 520 | acpi_power_expose_list(adev, resources, attr_group); |
| 521 | else |
| 522 | acpi_power_hide_list(adev, resources, attr_group); |
| 523 | } |
| 524 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 525 | void acpi_power_add_remove_device(struct acpi_device *adev, bool add) |
| 526 | { |
Rafael J. Wysocki | 18a3870 | 2013-01-25 21:51:32 +0100 | [diff] [blame] | 527 | struct acpi_device_power_state *ps; |
| 528 | struct acpi_power_resource_entry *entry; |
| 529 | int state; |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 530 | |
Rafael J. Wysocki | 41a2a46 | 2013-04-11 22:41:48 +0200 | [diff] [blame] | 531 | if (adev->wakeup.flags.valid) |
| 532 | acpi_power_expose_hide(adev, &adev->wakeup.resources, |
| 533 | &wakeup_attr_group, add); |
| 534 | |
Rafael J. Wysocki | 18a3870 | 2013-01-25 21:51:32 +0100 | [diff] [blame] | 535 | if (!adev->power.flags.power_resources) |
| 536 | return; |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 537 | |
Rafael J. Wysocki | 18a3870 | 2013-01-25 21:51:32 +0100 | [diff] [blame] | 538 | ps = &adev->power.states[ACPI_STATE_D0]; |
| 539 | list_for_each_entry(entry, &ps->resources, node) { |
| 540 | struct acpi_power_resource *resource = entry->resource; |
| 541 | |
| 542 | if (add) |
| 543 | acpi_power_add_dependent(resource, adev); |
| 544 | else |
| 545 | acpi_power_remove_dependent(resource, adev); |
| 546 | } |
| 547 | |
Rafael J. Wysocki | 41a2a46 | 2013-04-11 22:41:48 +0200 | [diff] [blame] | 548 | for (state = ACPI_STATE_D0; state <= ACPI_STATE_D3_HOT; state++) |
| 549 | acpi_power_expose_hide(adev, |
| 550 | &adev->power.states[state].resources, |
| 551 | &attr_groups[state], add); |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 552 | } |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 553 | |
Rafael J. Wysocki | b5d667e | 2013-02-23 23:15:21 +0100 | [diff] [blame] | 554 | int acpi_power_wakeup_list_init(struct list_head *list, int *system_level_p) |
Rafael J. Wysocki | 0596a52 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 555 | { |
| 556 | struct acpi_power_resource_entry *entry; |
| 557 | int system_level = 5; |
| 558 | |
| 559 | list_for_each_entry(entry, list, node) { |
| 560 | struct acpi_power_resource *resource = entry->resource; |
Rafael J. Wysocki | b5d667e | 2013-02-23 23:15:21 +0100 | [diff] [blame] | 561 | acpi_handle handle = resource->device.handle; |
| 562 | int result; |
| 563 | int state; |
Rafael J. Wysocki | 0596a52 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 564 | |
Rafael J. Wysocki | b5d667e | 2013-02-23 23:15:21 +0100 | [diff] [blame] | 565 | mutex_lock(&resource->resource_lock); |
| 566 | |
| 567 | result = acpi_power_get_state(handle, &state); |
| 568 | if (result) { |
| 569 | mutex_unlock(&resource->resource_lock); |
| 570 | return result; |
| 571 | } |
| 572 | if (state == ACPI_POWER_RESOURCE_STATE_ON) { |
| 573 | resource->ref_count++; |
| 574 | resource->wakeup_enabled = true; |
| 575 | } |
Rafael J. Wysocki | 0596a52 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 576 | if (system_level > resource->system_level) |
| 577 | system_level = resource->system_level; |
Rafael J. Wysocki | b5d667e | 2013-02-23 23:15:21 +0100 | [diff] [blame] | 578 | |
| 579 | mutex_unlock(&resource->resource_lock); |
Rafael J. Wysocki | 0596a52 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 580 | } |
Rafael J. Wysocki | b5d667e | 2013-02-23 23:15:21 +0100 | [diff] [blame] | 581 | *system_level_p = system_level; |
| 582 | return 0; |
Rafael J. Wysocki | 0596a52 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 583 | } |
| 584 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 585 | /* -------------------------------------------------------------------------- |
| 586 | Device Power Management |
| 587 | -------------------------------------------------------------------------- */ |
Lin Ming | 0090def | 2012-03-29 14:09:39 +0800 | [diff] [blame] | 588 | |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 589 | /** |
| 590 | * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in |
| 591 | * ACPI 3.0) _PSW (Power State Wake) |
| 592 | * @dev: Device to handle. |
| 593 | * @enable: 0 - disable, 1 - enable the wake capabilities of the device. |
| 594 | * @sleep_state: Target sleep state of the system. |
| 595 | * @dev_state: Target power state of the device. |
| 596 | * |
| 597 | * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power |
| 598 | * State Wake) for the device, if present. On failure reset the device's |
| 599 | * wakeup.flags.valid flag. |
| 600 | * |
| 601 | * RETURN VALUE: |
| 602 | * 0 if either _DSW or _PSW has been successfully executed |
| 603 | * 0 if neither _DSW nor _PSW has been found |
| 604 | * -ENODEV if the execution of either _DSW or _PSW has failed |
| 605 | */ |
| 606 | int acpi_device_sleep_wake(struct acpi_device *dev, |
| 607 | int enable, int sleep_state, int dev_state) |
| 608 | { |
| 609 | union acpi_object in_arg[3]; |
| 610 | struct acpi_object_list arg_list = { 3, in_arg }; |
| 611 | acpi_status status = AE_OK; |
| 612 | |
| 613 | /* |
| 614 | * Try to execute _DSW first. |
| 615 | * |
| 616 | * Three agruments are needed for the _DSW object: |
| 617 | * Argument 0: enable/disable the wake capabilities |
| 618 | * Argument 1: target system state |
| 619 | * Argument 2: target device state |
| 620 | * When _DSW object is called to disable the wake capabilities, maybe |
| 621 | * the first argument is filled. The values of the other two agruments |
| 622 | * are meaningless. |
| 623 | */ |
| 624 | in_arg[0].type = ACPI_TYPE_INTEGER; |
| 625 | in_arg[0].integer.value = enable; |
| 626 | in_arg[1].type = ACPI_TYPE_INTEGER; |
| 627 | in_arg[1].integer.value = sleep_state; |
| 628 | in_arg[2].type = ACPI_TYPE_INTEGER; |
| 629 | in_arg[2].integer.value = dev_state; |
| 630 | status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL); |
| 631 | if (ACPI_SUCCESS(status)) { |
| 632 | return 0; |
| 633 | } else if (status != AE_NOT_FOUND) { |
| 634 | printk(KERN_ERR PREFIX "_DSW execution failed\n"); |
| 635 | dev->wakeup.flags.valid = 0; |
| 636 | return -ENODEV; |
| 637 | } |
| 638 | |
| 639 | /* Execute _PSW */ |
| 640 | arg_list.count = 1; |
| 641 | in_arg[0].integer.value = enable; |
| 642 | status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL); |
| 643 | if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) { |
| 644 | printk(KERN_ERR PREFIX "_PSW execution failed\n"); |
| 645 | dev->wakeup.flags.valid = 0; |
| 646 | return -ENODEV; |
| 647 | } |
| 648 | |
| 649 | return 0; |
| 650 | } |
| 651 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | /* |
| 653 | * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229): |
| 654 | * 1. Power on the power resources required for the wakeup device |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 655 | * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power |
| 656 | * State Wake) for the device, if present |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | */ |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 658 | 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] | 659 | { |
Rafael J. Wysocki | b5d667e | 2013-02-23 23:15:21 +0100 | [diff] [blame] | 660 | struct acpi_power_resource_entry *entry; |
Rafael J. Wysocki | 993cbe5 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 661 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | if (!dev || !dev->wakeup.flags.valid) |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 664 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 666 | mutex_lock(&acpi_device_lock); |
| 667 | |
| 668 | if (dev->wakeup.prepare_count++) |
| 669 | goto out; |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 670 | |
Rafael J. Wysocki | b5d667e | 2013-02-23 23:15:21 +0100 | [diff] [blame] | 671 | list_for_each_entry(entry, &dev->wakeup.resources, node) { |
| 672 | struct acpi_power_resource *resource = entry->resource; |
| 673 | |
| 674 | mutex_lock(&resource->resource_lock); |
| 675 | |
| 676 | if (!resource->wakeup_enabled) { |
| 677 | err = acpi_power_on_unlocked(resource); |
| 678 | if (!err) |
| 679 | resource->wakeup_enabled = true; |
| 680 | } |
| 681 | |
| 682 | mutex_unlock(&resource->resource_lock); |
| 683 | |
| 684 | if (err) { |
| 685 | dev_err(&dev->dev, |
| 686 | "Cannot turn wakeup power resources on\n"); |
| 687 | dev->wakeup.flags.valid = 0; |
| 688 | goto out; |
| 689 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | } |
Rafael J. Wysocki | b5d667e | 2013-02-23 23:15:21 +0100 | [diff] [blame] | 691 | /* |
| 692 | * Passing 3 as the third argument below means the device may be |
| 693 | * put into arbitrary power state afterward. |
| 694 | */ |
| 695 | err = acpi_device_sleep_wake(dev, 1, sleep_state, 3); |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 696 | if (err) |
| 697 | dev->wakeup.prepare_count = 0; |
| 698 | |
| 699 | out: |
| 700 | mutex_unlock(&acpi_device_lock); |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 701 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | /* |
| 705 | * Shutdown a wakeup device, counterpart of above method |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 706 | * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power |
| 707 | * State Wake) for the device, if present |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | * 2. Shutdown down the power resources |
| 709 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 710 | int acpi_disable_wakeup_device_power(struct acpi_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | { |
Rafael J. Wysocki | b5d667e | 2013-02-23 23:15:21 +0100 | [diff] [blame] | 712 | struct acpi_power_resource_entry *entry; |
Rafael J. Wysocki | 993cbe5 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 713 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | |
| 715 | if (!dev || !dev->wakeup.flags.valid) |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 716 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 718 | mutex_lock(&acpi_device_lock); |
| 719 | |
| 720 | if (--dev->wakeup.prepare_count > 0) |
| 721 | goto out; |
| 722 | |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 723 | /* |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 724 | * Executing the code below even if prepare_count is already zero when |
| 725 | * the function is called may be useful, for example for initialisation. |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 726 | */ |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 727 | if (dev->wakeup.prepare_count < 0) |
| 728 | dev->wakeup.prepare_count = 0; |
Rafael J. Wysocki | 0af4b8c | 2008-07-07 03:34:11 +0200 | [diff] [blame] | 729 | |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 730 | err = acpi_device_sleep_wake(dev, 0, 0, 0); |
| 731 | if (err) |
| 732 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | |
Rafael J. Wysocki | b5d667e | 2013-02-23 23:15:21 +0100 | [diff] [blame] | 734 | list_for_each_entry(entry, &dev->wakeup.resources, node) { |
| 735 | struct acpi_power_resource *resource = entry->resource; |
| 736 | |
| 737 | mutex_lock(&resource->resource_lock); |
| 738 | |
| 739 | if (resource->wakeup_enabled) { |
| 740 | err = acpi_power_off_unlocked(resource); |
| 741 | if (!err) |
| 742 | resource->wakeup_enabled = false; |
| 743 | } |
| 744 | |
| 745 | mutex_unlock(&resource->resource_lock); |
| 746 | |
| 747 | if (err) { |
| 748 | dev_err(&dev->dev, |
| 749 | "Cannot turn wakeup power resources off\n"); |
| 750 | dev->wakeup.flags.valid = 0; |
| 751 | break; |
| 752 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 755 | out: |
| 756 | mutex_unlock(&acpi_device_lock); |
| 757 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Rafael J. Wysocki | 32a00d2 | 2010-11-25 00:05:17 +0100 | [diff] [blame] | 760 | int acpi_power_get_inferred_state(struct acpi_device *device, int *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 762 | int result = 0; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 763 | int list_state = 0; |
| 764 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | |
Rafael J. Wysocki | 32a00d2 | 2010-11-25 00:05:17 +0100 | [diff] [blame] | 766 | if (!device || !state) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 767 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | /* |
| 770 | * We know a device's inferred power state when all the resources |
| 771 | * required for a given D-state are 'on'. |
| 772 | */ |
Rafael J. Wysocki | 38c92ff | 2012-05-20 13:58:00 +0200 | [diff] [blame] | 773 | for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) { |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 774 | struct list_head *list = &device->power.states[i].resources; |
| 775 | |
| 776 | if (list_empty(list)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | continue; |
| 778 | |
| 779 | result = acpi_power_get_list_state(list, &list_state); |
| 780 | if (result) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 781 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | |
| 783 | if (list_state == ACPI_POWER_RESOURCE_STATE_ON) { |
Rafael J. Wysocki | 32a00d2 | 2010-11-25 00:05:17 +0100 | [diff] [blame] | 784 | *state = i; |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 785 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | } |
| 787 | } |
| 788 | |
Rafael J. Wysocki | 32a00d2 | 2010-11-25 00:05:17 +0100 | [diff] [blame] | 789 | *state = ACPI_STATE_D3; |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 790 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | } |
| 792 | |
Rafael J. Wysocki | 30d3df4 | 2010-11-25 00:06:55 +0100 | [diff] [blame] | 793 | int acpi_power_on_resources(struct acpi_device *device, int state) |
| 794 | { |
Rafael J. Wysocki | 87e753b | 2013-01-22 12:56:16 +0100 | [diff] [blame] | 795 | if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT) |
Rafael J. Wysocki | 30d3df4 | 2010-11-25 00:06:55 +0100 | [diff] [blame] | 796 | return -EINVAL; |
| 797 | |
| 798 | return acpi_power_on_list(&device->power.states[state].resources); |
| 799 | } |
| 800 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 801 | int acpi_power_transition(struct acpi_device *device, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | { |
Rafael J. Wysocki | 5c7dd71 | 2012-05-18 00:39:35 +0200 | [diff] [blame] | 803 | int result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
Zhang Rui | 3ebc81b | 2012-03-29 14:09:38 +0800 | [diff] [blame] | 805 | if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 806 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 808 | if (device->power.state == state || !device->flags.power_manageable) |
Rafael J. Wysocki | 212967c | 2010-11-25 00:02:36 +0100 | [diff] [blame] | 809 | return 0; |
| 810 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 811 | if ((device->power.state < ACPI_STATE_D0) |
Zhang Rui | 3ebc81b | 2012-03-29 14:09:38 +0800 | [diff] [blame] | 812 | || (device->power.state > ACPI_STATE_D3_COLD)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 813 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | /* TBD: Resources must be ordered. */ |
| 816 | |
| 817 | /* |
| 818 | * First we reference all power resources required in the target list |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 819 | * (e.g. so the device doesn't lose power while transitioning). Then, |
| 820 | * we dereference all power resources used in the current list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | */ |
Rafael J. Wysocki | 5c7dd71 | 2012-05-18 00:39:35 +0200 | [diff] [blame] | 822 | if (state < ACPI_STATE_D3_COLD) |
| 823 | result = acpi_power_on_list( |
| 824 | &device->power.states[state].resources); |
| 825 | |
| 826 | if (!result && device->power.state < ACPI_STATE_D3_COLD) |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 827 | acpi_power_off_list( |
| 828 | &device->power.states[device->power.state].resources); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
Rafael J. Wysocki | d2ef555 | 2010-11-25 00:06:09 +0100 | [diff] [blame] | 830 | /* We shouldn't change the state unless the above operations succeed. */ |
| 831 | device->power.state = result ? ACPI_STATE_UNKNOWN : state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 833 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 836 | static void acpi_release_power_resource(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | { |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 838 | struct acpi_device *device = to_acpi_device(dev); |
| 839 | struct acpi_power_resource *resource; |
| 840 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 841 | resource = container_of(device, struct acpi_power_resource, device); |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 842 | |
| 843 | mutex_lock(&power_resource_list_lock); |
| 844 | list_del(&resource->list_node); |
| 845 | mutex_unlock(&power_resource_list_lock); |
| 846 | |
Toshi Kani | c0af417 | 2013-03-04 21:30:42 +0000 | [diff] [blame] | 847 | acpi_free_pnp_ids(&device->pnp); |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 848 | kfree(resource); |
| 849 | } |
| 850 | |
Rafael J. Wysocki | b1c0f99 | 2013-01-24 12:50:09 +0100 | [diff] [blame] | 851 | static ssize_t acpi_power_in_use_show(struct device *dev, |
| 852 | struct device_attribute *attr, |
| 853 | char *buf) { |
| 854 | struct acpi_power_resource *resource; |
| 855 | |
| 856 | resource = to_power_resource(to_acpi_device(dev)); |
| 857 | return sprintf(buf, "%u\n", !!resource->ref_count); |
| 858 | } |
| 859 | static DEVICE_ATTR(resource_in_use, 0444, acpi_power_in_use_show, NULL); |
| 860 | |
| 861 | static void acpi_power_sysfs_remove(struct acpi_device *device) |
| 862 | { |
| 863 | device_remove_file(&device->dev, &dev_attr_resource_in_use); |
| 864 | } |
| 865 | |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 866 | int acpi_add_power_resource(acpi_handle handle) |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 867 | { |
| 868 | struct acpi_power_resource *resource; |
| 869 | struct acpi_device *device = NULL; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 870 | union acpi_object acpi_object; |
| 871 | struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object }; |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 872 | acpi_status status; |
| 873 | int state, result = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 875 | acpi_bus_get_device(handle, &device); |
| 876 | if (device) |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 877 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 879 | resource = kzalloc(sizeof(*resource), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | if (!resource) |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 881 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 883 | device = &resource->device; |
| 884 | acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER, |
| 885 | ACPI_STA_DEFAULT); |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 886 | mutex_init(&resource->resource_lock); |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 887 | INIT_LIST_HEAD(&resource->dependent); |
Rafael J. Wysocki | 6ee22e9d | 2013-06-19 00:44:45 +0200 | [diff] [blame] | 888 | INIT_LIST_HEAD(&resource->list_node); |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 889 | resource->name = device->pnp.bus_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME); |
| 891 | strcpy(acpi_device_class(device), ACPI_POWER_CLASS); |
Rafael J. Wysocki | 722c929 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 892 | device->power.state = ACPI_STATE_UNKNOWN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | |
| 894 | /* Evalute the object to get the system level and resource order. */ |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 895 | status = acpi_evaluate_object(handle, NULL, NULL, &buffer); |
| 896 | if (ACPI_FAILURE(status)) |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 897 | goto err; |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 898 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | resource->system_level = acpi_object.power_resource.system_level; |
| 900 | resource->order = acpi_object.power_resource.resource_order; |
| 901 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 902 | result = acpi_power_get_state(handle, &state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | if (result) |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 904 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 906 | printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device), |
Alexey Starikovskiy | c35923b | 2007-10-22 14:19:09 +0400 | [diff] [blame] | 907 | acpi_device_bid(device), state ? "on" : "off"); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 908 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 909 | device->flags.match_driver = true; |
Rafael J. Wysocki | cf860be | 2013-01-24 12:49:49 +0100 | [diff] [blame] | 910 | result = acpi_device_add(device, acpi_release_power_resource); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | if (result) |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 912 | goto err; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 913 | |
Rafael J. Wysocki | b1c0f99 | 2013-01-24 12:50:09 +0100 | [diff] [blame] | 914 | if (!device_create_file(&device->dev, &dev_attr_resource_in_use)) |
| 915 | device->remove = acpi_power_sysfs_remove; |
| 916 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 917 | mutex_lock(&power_resource_list_lock); |
| 918 | list_add(&resource->list_node, &acpi_power_resource_list); |
| 919 | mutex_unlock(&power_resource_list_lock); |
Rafael J. Wysocki | cf860be | 2013-01-24 12:49:49 +0100 | [diff] [blame] | 920 | acpi_device_add_finalize(device); |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 921 | return 0; |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 922 | |
| 923 | err: |
| 924 | acpi_release_power_resource(&device->dev); |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 925 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 928 | #ifdef CONFIG_ACPI_SLEEP |
| 929 | void acpi_resume_power_resources(void) |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 930 | { |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 931 | struct acpi_power_resource *resource; |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 932 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 933 | mutex_lock(&power_resource_list_lock); |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 934 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 935 | list_for_each_entry(resource, &acpi_power_resource_list, list_node) { |
| 936 | int result, state; |
Rafael J. Wysocki | 3e384ee | 2010-10-22 02:35:54 +0200 | [diff] [blame] | 937 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 938 | mutex_lock(&resource->resource_lock); |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 939 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 940 | result = acpi_power_get_state(resource->device.handle, &state); |
Rafael J. Wysocki | 660b111 | 2013-01-25 21:51:57 +0100 | [diff] [blame] | 941 | if (result) |
| 942 | continue; |
| 943 | |
| 944 | if (state == ACPI_POWER_RESOURCE_STATE_OFF |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 945 | && resource->ref_count) { |
| 946 | dev_info(&resource->device.dev, "Turning ON\n"); |
| 947 | __acpi_power_on(resource); |
Rafael J. Wysocki | 660b111 | 2013-01-25 21:51:57 +0100 | [diff] [blame] | 948 | } else if (state == ACPI_POWER_RESOURCE_STATE_ON |
| 949 | && !resource->ref_count) { |
| 950 | dev_info(&resource->device.dev, "Turning OFF\n"); |
| 951 | __acpi_power_off(resource); |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 952 | } |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 953 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 954 | mutex_unlock(&resource->resource_lock); |
| 955 | } |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 956 | |
Rafael J. Wysocki | 781d737 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 957 | mutex_unlock(&power_resource_list_lock); |
Konstantin Karasyov | 0a61390 | 2007-02-16 01:47:06 -0500 | [diff] [blame] | 958 | } |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 959 | #endif |