blob: 34f5ef11d427bd8f9acb4690b6c64b41d9c331b0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Lin Ming0090def2012-03-29 14:09:39 +080043#include <linux/pm_runtime.h>
Rafael J. Wysocki18a38702013-01-25 21:51:32 +010044#include <linux/sysfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <acpi/acpi_bus.h>
46#include <acpi/acpi_drivers.h>
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +020047#include "sleep.h"
Lin Ming0090def2012-03-29 14:09:39 +080048#include "internal.h"
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +020049
Len Browna192a952009-07-28 16:45:54 -040050#define PREFIX "ACPI: "
51
Bjorn Helgaas89595b82008-11-07 16:57:45 -070052#define _COMPONENT ACPI_POWER_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050053ACPI_MODULE_NAME("power");
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define ACPI_POWER_CLASS "power_resource"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#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 Yakuif5adfaa2008-08-11 14:57:50 +080061
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +010062struct acpi_power_dependent_device {
63 struct list_head node;
64 struct acpi_device *adev;
65 struct work_struct work;
Lin Ming0090def2012-03-29 14:09:39 +080066};
67
Len Brown4be44fc2005-08-05 00:44:28 -040068struct acpi_power_resource {
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010069 struct acpi_device device;
Rafael J. Wysocki781d7372013-01-17 14:11:06 +010070 struct list_head list_node;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +010071 struct list_head dependent;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010072 char *name;
Len Brown4be44fc2005-08-05 00:44:28 -040073 u32 system_level;
74 u32 order;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +020075 unsigned int ref_count;
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +010076 bool wakeup_enabled;
Konstantin Karasyov0a613902007-02-16 01:47:06 -050077 struct mutex resource_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79
Rafael J. Wysocki0b224522013-01-17 14:11:06 +010080struct acpi_power_resource_entry {
81 struct list_head node;
82 struct acpi_power_resource *resource;
83};
84
Rafael J. Wysocki781d7372013-01-17 14:11:06 +010085static LIST_HEAD(acpi_power_resource_list);
86static DEFINE_MUTEX(power_resource_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/* --------------------------------------------------------------------------
89 Power Resource Management
90 -------------------------------------------------------------------------- */
91
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +010092static inline
93struct 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. Wysocki82c7d5e2013-01-17 14:11:05 +010098static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100100 struct acpi_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100102 if (acpi_bus_get_device(handle, &device))
103 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +0100105 return to_power_resource(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100108static int acpi_power_resources_list_add(acpi_handle handle,
109 struct list_head *list)
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100110{
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. Wysockie88c9c62013-01-17 14:11:07 +0100115 return -EINVAL;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100116
117 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
118 if (!entry)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100119 return -ENOMEM;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100120
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. Wysockie88c9c62013-01-17 14:11:07 +0100128 return 0;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100129 }
130 }
131 list_add_tail(&entry->node, list);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100132 return 0;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100133}
134
135void 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. Wysockie88c9c62013-01-17 14:11:07 +0100145int acpi_extract_power_resources(union acpi_object *package, unsigned int start,
146 struct list_head *list)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100147{
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100148 unsigned int i;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100149 int err = 0;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100150
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. Wysockie88c9c62013-01-17 14:11:07 +0100156 err = -ENODATA;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100157 break;
158 }
159 rhandle = element->reference.handle;
160 if (!rhandle) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100161 err = -ENODEV;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100162 break;
163 }
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100164 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. Wysockief85bdb2013-01-17 14:11:07 +0100171 }
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100172 if (err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100173 acpi_power_resources_list_free(list);
174
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100175 return err;
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +0100176}
177
Zhao Yakuia51e1452008-08-11 14:55:05 +0800178static int acpi_power_get_state(acpi_handle handle, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Len Brown4be44fc2005-08-05 00:44:28 -0400180 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400181 unsigned long long sta = 0;
Lin Ming60a4ce72008-12-16 17:02:22 +0800182 char node_name[5];
183 struct acpi_buffer buffer = { sizeof(node_name), node_name };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Zhao Yakuia51e1452008-08-11 14:55:05 +0800186 if (!handle || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400187 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Zhao Yakuia51e1452008-08-11 14:55:05 +0800189 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400191 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400193 *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
194 ACPI_POWER_RESOURCE_STATE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Lin Ming60a4ce72008-12-16 17:02:22 +0800196 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
Lin Ming60a4ce72008-12-16 17:02:22 +0800199 node_name,
Zhao Yakuib1b57fb2008-10-27 16:04:53 +0800200 *state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Patrick Mocheld550d982006-06-27 00:41:40 -0400202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100205static int acpi_power_get_list_state(struct list_head *list, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100207 struct acpi_power_resource_entry *entry;
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100208 int cur_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 if (!list || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400211 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 /* The state of the list is 'on' IFF all resources are 'on'. */
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100214 list_for_each_entry(entry, list, node) {
215 struct acpi_power_resource *resource = entry->resource;
216 acpi_handle handle = resource->device.handle;
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100217 int result;
218
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100219 mutex_lock(&resource->resource_lock);
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100220 result = acpi_power_get_state(handle, &cur_state);
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100221 mutex_unlock(&resource->resource_lock);
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100222 if (result)
223 return result;
224
225 if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 break;
227 }
228
229 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100230 cur_state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100232 *state = cur_state;
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100233 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100236static void acpi_power_resume_dependent(struct work_struct *work)
Lin Ming0090def2012-03-29 14:09:39 +0800237{
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100238 struct acpi_power_dependent_device *dep;
239 struct acpi_device_physical_node *pn;
240 struct acpi_device *adev;
Lin Ming0090def2012-03-29 14:09:39 +0800241 int state;
242
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100243 dep = container_of(work, struct acpi_power_dependent_device, work);
244 adev = dep->adev;
245 if (acpi_power_get_inferred_state(adev, &state))
Lin Ming0090def2012-03-29 14:09:39 +0800246 return;
247
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100248 if (state > ACPI_STATE_D0)
Lin Ming0090def2012-03-29 14:09:39 +0800249 return;
250
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100251 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 Ming0090def2012-03-29 14:09:39 +0800260}
261
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200262static int __acpi_power_on(struct acpi_power_resource *resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Len Brown4be44fc2005-08-05 00:44:28 -0400264 acpi_status status = AE_OK;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500265
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100266 status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400268 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200270 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400271 resource->name));
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200272
Patrick Mocheld550d982006-06-27 00:41:40 -0400273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100276static int acpi_power_on_unlocked(struct acpi_power_resource *resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100278 int result = 0;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200279
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. Wysockibc9b6402013-01-17 14:11:05 +0100286 if (result) {
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100287 resource->ref_count--;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100288 } 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 Karasyov0a613902007-02-16 01:47:06 -0500294 }
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100295 return result;
296}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100298static 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 Karasyov0a613902007-02-16 01:47:06 -0500304 mutex_unlock(&resource->resource_lock);
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100305 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100308static 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. Wysockib5d667e2013-02-23 23:15:21 +0100322static int acpi_power_off_unlocked(struct acpi_power_resource *resource)
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200323{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100324 int result = 0;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200325
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200326 if (!resource->ref_count) {
327 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
328 "Power resource [%s] already off",
329 resource->name));
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100330 return 0;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200331 }
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. Wysocki660b1112013-01-25 21:51:57 +0100337 } else {
338 result = __acpi_power_off(resource);
339 if (result)
340 resource->ref_count++;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200341 }
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100342 return result;
343}
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200344
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100345static 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. Wysocki3e384ee2010-10-22 02:35:54 +0200351 mutex_unlock(&resource->resource_lock);
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200352 return result;
353}
354
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100355static int acpi_power_off_list(struct list_head *list)
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100356{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100357 struct acpi_power_resource_entry *entry;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100358 int result = 0;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100359
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100360 list_for_each_entry_reverse(entry, list, node) {
361 result = acpi_power_off(entry->resource);
362 if (result)
363 goto err;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100364 }
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100365 return 0;
366
367 err:
368 list_for_each_entry_continue(entry, list, node)
369 acpi_power_on(entry->resource);
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100370
371 return result;
372}
373
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100374static 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
393static void acpi_power_add_dependent(struct acpi_power_resource *resource,
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100394 struct acpi_device *adev)
Lin Ming0090def2012-03-29 14:09:39 +0800395{
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100396 struct acpi_power_dependent_device *dep;
Lin Ming0090def2012-03-29 14:09:39 +0800397
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100398 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. Wysocki0b224522013-01-17 14:11:06 +0100416static void acpi_power_remove_dependent(struct acpi_power_resource *resource,
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100417 struct acpi_device *adev)
418{
419 struct acpi_power_dependent_device *dep;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100420 struct work_struct *work = NULL;
421
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100422 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. Wysocki18a38702013-01-25 21:51:32 +0100439static struct attribute *attrs[] = {
440 NULL,
441};
442
443static 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
462static void acpi_power_hide_list(struct acpi_device *adev, int state)
463{
464 struct acpi_device_power_state *ps = &adev->power.states[state];
465 struct acpi_power_resource_entry *entry;
466
467 if (list_empty(&ps->resources))
468 return;
469
470 list_for_each_entry_reverse(entry, &ps->resources, node) {
471 struct acpi_device *res_dev = &entry->resource->device;
472
473 sysfs_remove_link_from_group(&adev->dev.kobj,
474 attr_groups[state].name,
475 dev_name(&res_dev->dev));
476 }
477 sysfs_remove_group(&adev->dev.kobj, &attr_groups[state]);
478}
479
480static void acpi_power_expose_list(struct acpi_device *adev, int state)
481{
482 struct acpi_device_power_state *ps = &adev->power.states[state];
483 struct acpi_power_resource_entry *entry;
484 int ret;
485
486 if (list_empty(&ps->resources))
487 return;
488
489 ret = sysfs_create_group(&adev->dev.kobj, &attr_groups[state]);
490 if (ret)
491 return;
492
493 list_for_each_entry(entry, &ps->resources, node) {
494 struct acpi_device *res_dev = &entry->resource->device;
495
496 ret = sysfs_add_link_to_group(&adev->dev.kobj,
497 attr_groups[state].name,
498 &res_dev->dev.kobj,
499 dev_name(&res_dev->dev));
500 if (ret) {
501 acpi_power_hide_list(adev, state);
502 break;
503 }
504 }
505}
506
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100507void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
508{
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100509 struct acpi_device_power_state *ps;
510 struct acpi_power_resource_entry *entry;
511 int state;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100512
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100513 if (!adev->power.flags.power_resources)
514 return;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100515
Rafael J. Wysocki18a38702013-01-25 21:51:32 +0100516 ps = &adev->power.states[ACPI_STATE_D0];
517 list_for_each_entry(entry, &ps->resources, node) {
518 struct acpi_power_resource *resource = entry->resource;
519
520 if (add)
521 acpi_power_add_dependent(resource, adev);
522 else
523 acpi_power_remove_dependent(resource, adev);
524 }
525
526 for (state = ACPI_STATE_D0; state <= ACPI_STATE_D3_HOT; state++) {
527 if (add)
528 acpi_power_expose_list(adev, state);
529 else
530 acpi_power_hide_list(adev, state);
Lin Ming0090def2012-03-29 14:09:39 +0800531 }
Lin Ming0090def2012-03-29 14:09:39 +0800532}
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100533
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100534int acpi_power_wakeup_list_init(struct list_head *list, int *system_level_p)
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100535{
536 struct acpi_power_resource_entry *entry;
537 int system_level = 5;
538
539 list_for_each_entry(entry, list, node) {
540 struct acpi_power_resource *resource = entry->resource;
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100541 acpi_handle handle = resource->device.handle;
542 int result;
543 int state;
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100544
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100545 mutex_lock(&resource->resource_lock);
546
547 result = acpi_power_get_state(handle, &state);
548 if (result) {
549 mutex_unlock(&resource->resource_lock);
550 return result;
551 }
552 if (state == ACPI_POWER_RESOURCE_STATE_ON) {
553 resource->ref_count++;
554 resource->wakeup_enabled = true;
555 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100556 if (system_level > resource->system_level)
557 system_level = resource->system_level;
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100558
559 mutex_unlock(&resource->resource_lock);
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100560 }
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100561 *system_level_p = system_level;
562 return 0;
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100563}
564
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100565/* --------------------------------------------------------------------------
566 Device Power Management
567 -------------------------------------------------------------------------- */
Lin Ming0090def2012-03-29 14:09:39 +0800568
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200569/**
570 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
571 * ACPI 3.0) _PSW (Power State Wake)
572 * @dev: Device to handle.
573 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
574 * @sleep_state: Target sleep state of the system.
575 * @dev_state: Target power state of the device.
576 *
577 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
578 * State Wake) for the device, if present. On failure reset the device's
579 * wakeup.flags.valid flag.
580 *
581 * RETURN VALUE:
582 * 0 if either _DSW or _PSW has been successfully executed
583 * 0 if neither _DSW nor _PSW has been found
584 * -ENODEV if the execution of either _DSW or _PSW has failed
585 */
586int acpi_device_sleep_wake(struct acpi_device *dev,
587 int enable, int sleep_state, int dev_state)
588{
589 union acpi_object in_arg[3];
590 struct acpi_object_list arg_list = { 3, in_arg };
591 acpi_status status = AE_OK;
592
593 /*
594 * Try to execute _DSW first.
595 *
596 * Three agruments are needed for the _DSW object:
597 * Argument 0: enable/disable the wake capabilities
598 * Argument 1: target system state
599 * Argument 2: target device state
600 * When _DSW object is called to disable the wake capabilities, maybe
601 * the first argument is filled. The values of the other two agruments
602 * are meaningless.
603 */
604 in_arg[0].type = ACPI_TYPE_INTEGER;
605 in_arg[0].integer.value = enable;
606 in_arg[1].type = ACPI_TYPE_INTEGER;
607 in_arg[1].integer.value = sleep_state;
608 in_arg[2].type = ACPI_TYPE_INTEGER;
609 in_arg[2].integer.value = dev_state;
610 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
611 if (ACPI_SUCCESS(status)) {
612 return 0;
613 } else if (status != AE_NOT_FOUND) {
614 printk(KERN_ERR PREFIX "_DSW execution failed\n");
615 dev->wakeup.flags.valid = 0;
616 return -ENODEV;
617 }
618
619 /* Execute _PSW */
620 arg_list.count = 1;
621 in_arg[0].integer.value = enable;
622 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
623 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
624 printk(KERN_ERR PREFIX "_PSW execution failed\n");
625 dev->wakeup.flags.valid = 0;
626 return -ENODEV;
627 }
628
629 return 0;
630}
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632/*
633 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
634 * 1. Power on the power resources required for the wakeup device
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200635 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
636 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200638int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100640 struct acpi_power_resource_entry *entry;
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100641 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200644 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200646 mutex_lock(&acpi_device_lock);
647
648 if (dev->wakeup.prepare_count++)
649 goto out;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200650
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100651 list_for_each_entry(entry, &dev->wakeup.resources, node) {
652 struct acpi_power_resource *resource = entry->resource;
653
654 mutex_lock(&resource->resource_lock);
655
656 if (!resource->wakeup_enabled) {
657 err = acpi_power_on_unlocked(resource);
658 if (!err)
659 resource->wakeup_enabled = true;
660 }
661
662 mutex_unlock(&resource->resource_lock);
663
664 if (err) {
665 dev_err(&dev->dev,
666 "Cannot turn wakeup power resources on\n");
667 dev->wakeup.flags.valid = 0;
668 goto out;
669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100671 /*
672 * Passing 3 as the third argument below means the device may be
673 * put into arbitrary power state afterward.
674 */
675 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200676 if (err)
677 dev->wakeup.prepare_count = 0;
678
679 out:
680 mutex_unlock(&acpi_device_lock);
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200681 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
684/*
685 * Shutdown a wakeup device, counterpart of above method
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200686 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
687 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 * 2. Shutdown down the power resources
689 */
Len Brown4be44fc2005-08-05 00:44:28 -0400690int acpi_disable_wakeup_device_power(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100692 struct acpi_power_resource_entry *entry;
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100693 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200696 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200698 mutex_lock(&acpi_device_lock);
699
700 if (--dev->wakeup.prepare_count > 0)
701 goto out;
702
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200703 /*
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200704 * Executing the code below even if prepare_count is already zero when
705 * the function is called may be useful, for example for initialisation.
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200706 */
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200707 if (dev->wakeup.prepare_count < 0)
708 dev->wakeup.prepare_count = 0;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200709
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200710 err = acpi_device_sleep_wake(dev, 0, 0, 0);
711 if (err)
712 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +0100714 list_for_each_entry(entry, &dev->wakeup.resources, node) {
715 struct acpi_power_resource *resource = entry->resource;
716
717 mutex_lock(&resource->resource_lock);
718
719 if (resource->wakeup_enabled) {
720 err = acpi_power_off_unlocked(resource);
721 if (!err)
722 resource->wakeup_enabled = false;
723 }
724
725 mutex_unlock(&resource->resource_lock);
726
727 if (err) {
728 dev_err(&dev->dev,
729 "Cannot turn wakeup power resources off\n");
730 dev->wakeup.flags.valid = 0;
731 break;
732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200735 out:
736 mutex_unlock(&acpi_device_lock);
737 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100740int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Len Brown4be44fc2005-08-05 00:44:28 -0400742 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400743 int list_state = 0;
744 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100746 if (!device || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400747 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 /*
750 * We know a device's inferred power state when all the resources
751 * required for a given D-state are 'on'.
752 */
Rafael J. Wysocki38c92ff2012-05-20 13:58:00 +0200753 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100754 struct list_head *list = &device->power.states[i].resources;
755
756 if (list_empty(list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 continue;
758
759 result = acpi_power_get_list_state(list, &list_state);
760 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400761 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100764 *state = i;
Patrick Mocheld550d982006-06-27 00:41:40 -0400765 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767 }
768
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100769 *state = ACPI_STATE_D3;
Patrick Mocheld550d982006-06-27 00:41:40 -0400770 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
Rafael J. Wysocki30d3df42010-11-25 00:06:55 +0100773int acpi_power_on_resources(struct acpi_device *device, int state)
774{
Rafael J. Wysocki87e753b2013-01-22 12:56:16 +0100775 if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT)
Rafael J. Wysocki30d3df42010-11-25 00:06:55 +0100776 return -EINVAL;
777
778 return acpi_power_on_list(&device->power.states[state].resources);
779}
780
Len Brown4be44fc2005-08-05 00:44:28 -0400781int acpi_power_transition(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200783 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800785 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400786 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100788 if (device->power.state == state || !device->flags.power_manageable)
Rafael J. Wysocki212967c2010-11-25 00:02:36 +0100789 return 0;
790
Len Brown4be44fc2005-08-05 00:44:28 -0400791 if ((device->power.state < ACPI_STATE_D0)
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800792 || (device->power.state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400793 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /* TBD: Resources must be ordered. */
796
797 /*
798 * First we reference all power resources required in the target list
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100799 * (e.g. so the device doesn't lose power while transitioning). Then,
800 * we dereference all power resources used in the current list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 */
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200802 if (state < ACPI_STATE_D3_COLD)
803 result = acpi_power_on_list(
804 &device->power.states[state].resources);
805
806 if (!result && device->power.state < ACPI_STATE_D3_COLD)
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100807 acpi_power_off_list(
808 &device->power.states[device->power.state].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100810 /* We shouldn't change the state unless the above operations succeed. */
811 device->power.state = result ? ACPI_STATE_UNKNOWN : state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Patrick Mocheld550d982006-06-27 00:41:40 -0400813 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100816static void acpi_release_power_resource(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100818 struct acpi_device *device = to_acpi_device(dev);
819 struct acpi_power_resource *resource;
820
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100821 resource = container_of(device, struct acpi_power_resource, device);
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100822
823 mutex_lock(&power_resource_list_lock);
824 list_del(&resource->list_node);
825 mutex_unlock(&power_resource_list_lock);
826
827 acpi_free_ids(device);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100828 kfree(resource);
829}
830
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +0100831static ssize_t acpi_power_in_use_show(struct device *dev,
832 struct device_attribute *attr,
833 char *buf) {
834 struct acpi_power_resource *resource;
835
836 resource = to_power_resource(to_acpi_device(dev));
837 return sprintf(buf, "%u\n", !!resource->ref_count);
838}
839static DEVICE_ATTR(resource_in_use, 0444, acpi_power_in_use_show, NULL);
840
841static void acpi_power_sysfs_remove(struct acpi_device *device)
842{
843 device_remove_file(&device->dev, &dev_attr_resource_in_use);
844}
845
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100846int acpi_add_power_resource(acpi_handle handle)
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100847{
848 struct acpi_power_resource *resource;
849 struct acpi_device *device = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400850 union acpi_object acpi_object;
851 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100852 acpi_status status;
853 int state, result = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100855 acpi_bus_get_device(handle, &device);
856 if (device)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100857 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100859 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (!resource)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100861 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100863 device = &resource->device;
864 acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER,
865 ACPI_STA_DEFAULT);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500866 mutex_init(&resource->resource_lock);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100867 INIT_LIST_HEAD(&resource->dependent);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100868 resource->name = device->pnp.bus_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
870 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
Rafael J. Wysocki722c9292013-01-17 14:11:06 +0100871 device->power.state = ACPI_STATE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 /* Evalute the object to get the system level and resource order. */
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100874 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
875 if (ACPI_FAILURE(status))
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100876 goto err;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 resource->system_level = acpi_object.power_resource.system_level;
879 resource->order = acpi_object.power_resource.resource_order;
880
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100881 result = acpi_power_get_state(handle, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (result)
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100883 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Len Brown4be44fc2005-08-05 00:44:28 -0400885 printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400886 acpi_device_bid(device), state ? "on" : "off");
Len Brown4be44fc2005-08-05 00:44:28 -0400887
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100888 device->flags.match_driver = true;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100889 result = acpi_device_add(device, acpi_release_power_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (result)
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100891 goto err;
Len Brown4be44fc2005-08-05 00:44:28 -0400892
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +0100893 if (!device_create_file(&device->dev, &dev_attr_resource_in_use))
894 device->remove = acpi_power_sysfs_remove;
895
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100896 mutex_lock(&power_resource_list_lock);
897 list_add(&resource->list_node, &acpi_power_resource_list);
898 mutex_unlock(&power_resource_list_lock);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100899 acpi_device_add_finalize(device);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100900 return 0;
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100901
902 err:
903 acpi_release_power_resource(&device->dev);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100904 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100907#ifdef CONFIG_ACPI_SLEEP
908void acpi_resume_power_resources(void)
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500909{
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200910 struct acpi_power_resource *resource;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500911
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100912 mutex_lock(&power_resource_list_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500913
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100914 list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
915 int result, state;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200916
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100917 mutex_lock(&resource->resource_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500918
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100919 result = acpi_power_get_state(resource->device.handle, &state);
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100920 if (result)
921 continue;
922
923 if (state == ACPI_POWER_RESOURCE_STATE_OFF
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100924 && resource->ref_count) {
925 dev_info(&resource->device.dev, "Turning ON\n");
926 __acpi_power_on(resource);
Rafael J. Wysocki660b1112013-01-25 21:51:57 +0100927 } else if (state == ACPI_POWER_RESOURCE_STATE_ON
928 && !resource->ref_count) {
929 dev_info(&resource->device.dev, "Turning OFF\n");
930 __acpi_power_off(resource);
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100931 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500932
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100933 mutex_unlock(&resource->resource_lock);
934 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500935
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100936 mutex_unlock(&power_resource_list_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500937}
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200938#endif