blob: 22a3d00d03590532e3c3f43d161c1dd87f89749d [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <acpi/acpi_bus.h>
45#include <acpi/acpi_drivers.h>
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +020046#include "sleep.h"
Lin Ming0090def2012-03-29 14:09:39 +080047#include "internal.h"
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +020048
Len Browna192a952009-07-28 16:45:54 -040049#define PREFIX "ACPI: "
50
Bjorn Helgaas89595b82008-11-07 16:57:45 -070051#define _COMPONENT ACPI_POWER_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050052ACPI_MODULE_NAME("power");
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define ACPI_POWER_CLASS "power_resource"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define ACPI_POWER_DEVICE_NAME "Power Resource"
55#define ACPI_POWER_FILE_INFO "info"
56#define ACPI_POWER_FILE_STATUS "state"
57#define ACPI_POWER_RESOURCE_STATE_OFF 0x00
58#define ACPI_POWER_RESOURCE_STATE_ON 0x01
59#define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
Zhao Yakuif5adfaa2008-08-11 14:57:50 +080060
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +010061struct acpi_power_dependent_device {
62 struct list_head node;
63 struct acpi_device *adev;
64 struct work_struct work;
Lin Ming0090def2012-03-29 14:09:39 +080065};
66
Len Brown4be44fc2005-08-05 00:44:28 -040067struct acpi_power_resource {
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010068 struct acpi_device device;
Rafael J. Wysocki781d7372013-01-17 14:11:06 +010069 struct list_head list_node;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +010070 struct list_head dependent;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010071 char *name;
Len Brown4be44fc2005-08-05 00:44:28 -040072 u32 system_level;
73 u32 order;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +020074 unsigned int ref_count;
Konstantin Karasyov0a613902007-02-16 01:47:06 -050075 struct mutex resource_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076};
77
Rafael J. Wysocki0b224522013-01-17 14:11:06 +010078struct acpi_power_resource_entry {
79 struct list_head node;
80 struct acpi_power_resource *resource;
81};
82
Rafael J. Wysocki781d7372013-01-17 14:11:06 +010083static LIST_HEAD(acpi_power_resource_list);
84static DEFINE_MUTEX(power_resource_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/* --------------------------------------------------------------------------
87 Power Resource Management
88 -------------------------------------------------------------------------- */
89
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010090static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010092 struct acpi_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010094 if (acpi_bus_get_device(handle, &device))
95 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010097 return container_of(device, struct acpi_power_resource, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100100void acpi_power_resources_list_add(acpi_handle handle, struct list_head *list)
101{
102 struct acpi_power_resource *resource = acpi_power_get_context(handle);
103 struct acpi_power_resource_entry *entry;
104
105 if (!resource || !list)
106 return;
107
108 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
109 if (!entry)
110 return;
111
112 entry->resource = resource;
113 if (!list_empty(list)) {
114 struct acpi_power_resource_entry *e;
115
116 list_for_each_entry(e, list, node)
117 if (e->resource->order > resource->order) {
118 list_add_tail(&entry->node, &e->node);
119 return;
120 }
121 }
122 list_add_tail(&entry->node, list);
123}
124
125void acpi_power_resources_list_free(struct list_head *list)
126{
127 struct acpi_power_resource_entry *entry, *e;
128
129 list_for_each_entry_safe(entry, e, list, node) {
130 list_del(&entry->node);
131 kfree(entry);
132 }
133}
134
Zhao Yakuia51e1452008-08-11 14:55:05 +0800135static int acpi_power_get_state(acpi_handle handle, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Len Brown4be44fc2005-08-05 00:44:28 -0400137 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400138 unsigned long long sta = 0;
Lin Ming60a4ce72008-12-16 17:02:22 +0800139 char node_name[5];
140 struct acpi_buffer buffer = { sizeof(node_name), node_name };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Zhao Yakuia51e1452008-08-11 14:55:05 +0800143 if (!handle || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400144 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Zhao Yakuia51e1452008-08-11 14:55:05 +0800146 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400148 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400150 *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
151 ACPI_POWER_RESOURCE_STATE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Lin Ming60a4ce72008-12-16 17:02:22 +0800153 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
Lin Ming60a4ce72008-12-16 17:02:22 +0800156 node_name,
Zhao Yakuib1b57fb2008-10-27 16:04:53 +0800157 *state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Patrick Mocheld550d982006-06-27 00:41:40 -0400159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100162static int acpi_power_get_list_state(struct list_head *list, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100164 struct acpi_power_resource_entry *entry;
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100165 int cur_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 if (!list || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400168 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 /* The state of the list is 'on' IFF all resources are 'on'. */
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100171 list_for_each_entry(entry, list, node) {
172 struct acpi_power_resource *resource = entry->resource;
173 acpi_handle handle = resource->device.handle;
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100174 int result;
175
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100176 mutex_lock(&resource->resource_lock);
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100177 result = acpi_power_get_state(handle, &cur_state);
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100178 mutex_unlock(&resource->resource_lock);
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100179 if (result)
180 return result;
181
182 if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 break;
184 }
185
186 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100187 cur_state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100189 *state = cur_state;
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100190 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100193static void acpi_power_resume_dependent(struct work_struct *work)
Lin Ming0090def2012-03-29 14:09:39 +0800194{
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100195 struct acpi_power_dependent_device *dep;
196 struct acpi_device_physical_node *pn;
197 struct acpi_device *adev;
Lin Ming0090def2012-03-29 14:09:39 +0800198 int state;
199
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100200 dep = container_of(work, struct acpi_power_dependent_device, work);
201 adev = dep->adev;
202 if (acpi_power_get_inferred_state(adev, &state))
Lin Ming0090def2012-03-29 14:09:39 +0800203 return;
204
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100205 if (state > ACPI_STATE_D0)
Lin Ming0090def2012-03-29 14:09:39 +0800206 return;
207
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100208 mutex_lock(&adev->physical_node_lock);
209
210 list_for_each_entry(pn, &adev->physical_node_list, node)
211 pm_request_resume(pn->dev);
212
213 list_for_each_entry(pn, &adev->power_dependent, node)
214 pm_request_resume(pn->dev);
215
216 mutex_unlock(&adev->physical_node_lock);
Lin Ming0090def2012-03-29 14:09:39 +0800217}
218
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200219static int __acpi_power_on(struct acpi_power_resource *resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Len Brown4be44fc2005-08-05 00:44:28 -0400221 acpi_status status = AE_OK;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500222
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100223 status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400225 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200227 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400228 resource->name));
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200229
Patrick Mocheld550d982006-06-27 00:41:40 -0400230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100233static int acpi_power_on(struct acpi_power_resource *resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100235 int result = 0;;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500237 mutex_lock(&resource->resource_lock);
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200238
239 if (resource->ref_count++) {
240 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
241 "Power resource [%s] already on",
242 resource->name));
243 } else {
244 result = __acpi_power_on(resource);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100245 if (result) {
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100246 resource->ref_count--;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100247 } else {
248 struct acpi_power_dependent_device *dep;
249
250 list_for_each_entry(dep, &resource->dependent, node)
251 schedule_work(&dep->work);
252 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500255 mutex_unlock(&resource->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100257 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100260static int acpi_power_off(struct acpi_power_resource *resource)
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200261{
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200262 acpi_status status = AE_OK;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100263 int result = 0;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200264
265 mutex_lock(&resource->resource_lock);
266
267 if (!resource->ref_count) {
268 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
269 "Power resource [%s] already off",
270 resource->name));
271 goto unlock;
272 }
273
274 if (--resource->ref_count) {
275 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
276 "Power resource [%s] still in use\n",
277 resource->name));
278 goto unlock;
279 }
280
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100281 status = acpi_evaluate_object(resource->device.handle, "_OFF", NULL, NULL);
Rafael J. Wysocki722c9292013-01-17 14:11:06 +0100282 if (ACPI_FAILURE(status))
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200283 result = -ENODEV;
Rafael J. Wysocki722c9292013-01-17 14:11:06 +0100284 else
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200285 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
286 "Power resource [%s] turned off\n",
287 resource->name));
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200288
289 unlock:
290 mutex_unlock(&resource->resource_lock);
291
292 return result;
293}
294
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100295static int acpi_power_off_list(struct list_head *list)
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100296{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100297 struct acpi_power_resource_entry *entry;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100298 int result = 0;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100299
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100300 list_for_each_entry_reverse(entry, list, node) {
301 result = acpi_power_off(entry->resource);
302 if (result)
303 goto err;
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100304 }
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100305 return 0;
306
307 err:
308 list_for_each_entry_continue(entry, list, node)
309 acpi_power_on(entry->resource);
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100310
311 return result;
312}
313
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100314static int acpi_power_on_list(struct list_head *list)
315{
316 struct acpi_power_resource_entry *entry;
317 int result = 0;
318
319 list_for_each_entry(entry, list, node) {
320 result = acpi_power_on(entry->resource);
321 if (result)
322 goto err;
323 }
324 return 0;
325
326 err:
327 list_for_each_entry_continue_reverse(entry, list, node)
328 acpi_power_off(entry->resource);
329
330 return result;
331}
332
333static void acpi_power_add_dependent(struct acpi_power_resource *resource,
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100334 struct acpi_device *adev)
Lin Ming0090def2012-03-29 14:09:39 +0800335{
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100336 struct acpi_power_dependent_device *dep;
Lin Ming0090def2012-03-29 14:09:39 +0800337
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100338 mutex_lock(&resource->resource_lock);
339
340 list_for_each_entry(dep, &resource->dependent, node)
341 if (dep->adev == adev)
342 goto out;
343
344 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
345 if (!dep)
346 goto out;
347
348 dep->adev = adev;
349 INIT_WORK(&dep->work, acpi_power_resume_dependent);
350 list_add_tail(&dep->node, &resource->dependent);
351
352 out:
353 mutex_unlock(&resource->resource_lock);
354}
355
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100356static void acpi_power_remove_dependent(struct acpi_power_resource *resource,
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100357 struct acpi_device *adev)
358{
359 struct acpi_power_dependent_device *dep;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100360 struct work_struct *work = NULL;
361
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100362 mutex_lock(&resource->resource_lock);
363
364 list_for_each_entry(dep, &resource->dependent, node)
365 if (dep->adev == adev) {
366 list_del(&dep->node);
367 work = &dep->work;
368 break;
369 }
370
371 mutex_unlock(&resource->resource_lock);
372
373 if (work) {
374 cancel_work_sync(work);
375 kfree(dep);
376 }
377}
378
379void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
380{
381 if (adev->power.flags.power_resources) {
382 struct acpi_device_power_state *ps;
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100383 struct acpi_power_resource_entry *entry;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100384
385 ps = &adev->power.states[ACPI_STATE_D0];
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100386 list_for_each_entry(entry, &ps->resources, node) {
387 struct acpi_power_resource *resource = entry->resource;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100388
389 if (add)
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100390 acpi_power_add_dependent(resource, adev);
Lin Ming0090def2012-03-29 14:09:39 +0800391 else
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100392 acpi_power_remove_dependent(resource, adev);
Lin Ming0090def2012-03-29 14:09:39 +0800393 }
394 }
Lin Ming0090def2012-03-29 14:09:39 +0800395}
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100396
397/* --------------------------------------------------------------------------
398 Device Power Management
399 -------------------------------------------------------------------------- */
Lin Ming0090def2012-03-29 14:09:39 +0800400
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200401/**
402 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
403 * ACPI 3.0) _PSW (Power State Wake)
404 * @dev: Device to handle.
405 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
406 * @sleep_state: Target sleep state of the system.
407 * @dev_state: Target power state of the device.
408 *
409 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
410 * State Wake) for the device, if present. On failure reset the device's
411 * wakeup.flags.valid flag.
412 *
413 * RETURN VALUE:
414 * 0 if either _DSW or _PSW has been successfully executed
415 * 0 if neither _DSW nor _PSW has been found
416 * -ENODEV if the execution of either _DSW or _PSW has failed
417 */
418int acpi_device_sleep_wake(struct acpi_device *dev,
419 int enable, int sleep_state, int dev_state)
420{
421 union acpi_object in_arg[3];
422 struct acpi_object_list arg_list = { 3, in_arg };
423 acpi_status status = AE_OK;
424
425 /*
426 * Try to execute _DSW first.
427 *
428 * Three agruments are needed for the _DSW object:
429 * Argument 0: enable/disable the wake capabilities
430 * Argument 1: target system state
431 * Argument 2: target device state
432 * When _DSW object is called to disable the wake capabilities, maybe
433 * the first argument is filled. The values of the other two agruments
434 * are meaningless.
435 */
436 in_arg[0].type = ACPI_TYPE_INTEGER;
437 in_arg[0].integer.value = enable;
438 in_arg[1].type = ACPI_TYPE_INTEGER;
439 in_arg[1].integer.value = sleep_state;
440 in_arg[2].type = ACPI_TYPE_INTEGER;
441 in_arg[2].integer.value = dev_state;
442 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
443 if (ACPI_SUCCESS(status)) {
444 return 0;
445 } else if (status != AE_NOT_FOUND) {
446 printk(KERN_ERR PREFIX "_DSW execution failed\n");
447 dev->wakeup.flags.valid = 0;
448 return -ENODEV;
449 }
450
451 /* Execute _PSW */
452 arg_list.count = 1;
453 in_arg[0].integer.value = enable;
454 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
455 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
456 printk(KERN_ERR PREFIX "_PSW execution failed\n");
457 dev->wakeup.flags.valid = 0;
458 return -ENODEV;
459 }
460
461 return 0;
462}
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464/*
465 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
466 * 1. Power on the power resources required for the wakeup device
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200467 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
468 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200470int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200472 int i, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200475 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200477 mutex_lock(&acpi_device_lock);
478
479 if (dev->wakeup.prepare_count++)
480 goto out;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 /* Open power resource */
483 for (i = 0; i < dev->wakeup.resources.count; i++) {
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200484 int ret = acpi_power_on(dev->wakeup.resources.handles[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (ret) {
Len Brown64684632006-06-26 23:41:38 -0400486 printk(KERN_ERR PREFIX "Transition power state\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 dev->wakeup.flags.valid = 0;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200488 err = -ENODEV;
489 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491 }
492
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200493 /*
494 * Passing 3 as the third argument below means the device may be placed
495 * in arbitrary power state afterwards.
496 */
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200497 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200498
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200499 err_out:
500 if (err)
501 dev->wakeup.prepare_count = 0;
502
503 out:
504 mutex_unlock(&acpi_device_lock);
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200505 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
508/*
509 * Shutdown a wakeup device, counterpart of above method
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200510 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
511 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 * 2. Shutdown down the power resources
513 */
Len Brown4be44fc2005-08-05 00:44:28 -0400514int acpi_disable_wakeup_device_power(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200516 int i, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200519 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200521 mutex_lock(&acpi_device_lock);
522
523 if (--dev->wakeup.prepare_count > 0)
524 goto out;
525
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200526 /*
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200527 * Executing the code below even if prepare_count is already zero when
528 * the function is called may be useful, for example for initialisation.
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200529 */
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200530 if (dev->wakeup.prepare_count < 0)
531 dev->wakeup.prepare_count = 0;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200532
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200533 err = acpi_device_sleep_wake(dev, 0, 0, 0);
534 if (err)
535 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 /* Close power resource */
538 for (i = 0; i < dev->wakeup.resources.count; i++) {
Rafael J. Wysocki36237fa2011-01-06 23:38:04 +0100539 int ret = acpi_power_off(dev->wakeup.resources.handles[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (ret) {
Len Brown64684632006-06-26 23:41:38 -0400541 printk(KERN_ERR PREFIX "Transition power state\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 dev->wakeup.flags.valid = 0;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200543 err = -ENODEV;
544 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546 }
547
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200548 out:
549 mutex_unlock(&acpi_device_lock);
550 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100553int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Len Brown4be44fc2005-08-05 00:44:28 -0400555 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400556 int list_state = 0;
557 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100559 if (!device || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400560 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /*
563 * We know a device's inferred power state when all the resources
564 * required for a given D-state are 'on'.
565 */
Rafael J. Wysocki38c92ff2012-05-20 13:58:00 +0200566 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100567 struct list_head *list = &device->power.states[i].resources;
568
569 if (list_empty(list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 continue;
571
572 result = acpi_power_get_list_state(list, &list_state);
573 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400574 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100577 *state = i;
Patrick Mocheld550d982006-06-27 00:41:40 -0400578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580 }
581
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100582 *state = ACPI_STATE_D3;
Patrick Mocheld550d982006-06-27 00:41:40 -0400583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Rafael J. Wysocki30d3df42010-11-25 00:06:55 +0100586int acpi_power_on_resources(struct acpi_device *device, int state)
587{
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100588 if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_COLD)
Rafael J. Wysocki30d3df42010-11-25 00:06:55 +0100589 return -EINVAL;
590
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100591 if (state == ACPI_STATE_D3_COLD)
592 return 0;
593
Rafael J. Wysocki30d3df42010-11-25 00:06:55 +0100594 return acpi_power_on_list(&device->power.states[state].resources);
595}
596
Len Brown4be44fc2005-08-05 00:44:28 -0400597int acpi_power_transition(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200599 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800601 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400602 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100604 if (device->power.state == state || !device->flags.power_manageable)
Rafael J. Wysocki212967c2010-11-25 00:02:36 +0100605 return 0;
606
Len Brown4be44fc2005-08-05 00:44:28 -0400607 if ((device->power.state < ACPI_STATE_D0)
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800608 || (device->power.state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400609 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /* TBD: Resources must be ordered. */
612
613 /*
614 * First we reference all power resources required in the target list
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100615 * (e.g. so the device doesn't lose power while transitioning). Then,
616 * we dereference all power resources used in the current list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 */
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200618 if (state < ACPI_STATE_D3_COLD)
619 result = acpi_power_on_list(
620 &device->power.states[state].resources);
621
622 if (!result && device->power.state < ACPI_STATE_D3_COLD)
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100623 acpi_power_off_list(
624 &device->power.states[device->power.state].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100626 /* We shouldn't change the state unless the above operations succeed. */
627 device->power.state = result ? ACPI_STATE_UNKNOWN : state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Patrick Mocheld550d982006-06-27 00:41:40 -0400629 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100632static void acpi_release_power_resource(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100634 struct acpi_device *device = to_acpi_device(dev);
635 struct acpi_power_resource *resource;
636
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100637 resource = container_of(device, struct acpi_power_resource, device);
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100638
639 mutex_lock(&power_resource_list_lock);
640 list_del(&resource->list_node);
641 mutex_unlock(&power_resource_list_lock);
642
643 acpi_free_ids(device);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100644 kfree(resource);
645}
646
647void acpi_add_power_resource(acpi_handle handle)
648{
649 struct acpi_power_resource *resource;
650 struct acpi_device *device = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400651 union acpi_object acpi_object;
652 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100653 acpi_status status;
654 int state, result = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100656 acpi_bus_get_device(handle, &device);
657 if (device)
658 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100660 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (!resource)
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100662 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100664 device = &resource->device;
665 acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER,
666 ACPI_STA_DEFAULT);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500667 mutex_init(&resource->resource_lock);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100668 INIT_LIST_HEAD(&resource->dependent);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100669 resource->name = device->pnp.bus_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
671 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
Rafael J. Wysocki722c9292013-01-17 14:11:06 +0100672 device->power.state = ACPI_STATE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 /* Evalute the object to get the system level and resource order. */
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100675 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
676 if (ACPI_FAILURE(status))
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100677 goto err;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 resource->system_level = acpi_object.power_resource.system_level;
680 resource->order = acpi_object.power_resource.resource_order;
681
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100682 result = acpi_power_get_state(handle, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (result)
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100684 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Len Brown4be44fc2005-08-05 00:44:28 -0400686 printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400687 acpi_device_bid(device), state ? "on" : "off");
Len Brown4be44fc2005-08-05 00:44:28 -0400688
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100689 device->flags.match_driver = true;
690 result = acpi_device_register(device, acpi_release_power_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (result)
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100692 goto err;
Len Brown4be44fc2005-08-05 00:44:28 -0400693
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100694 mutex_lock(&power_resource_list_lock);
695 list_add(&resource->list_node, &acpi_power_resource_list);
696 mutex_unlock(&power_resource_list_lock);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100697 return;
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100698
699 err:
700 acpi_release_power_resource(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100703#ifdef CONFIG_ACPI_SLEEP
704void acpi_resume_power_resources(void)
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500705{
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200706 struct acpi_power_resource *resource;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500707
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100708 mutex_lock(&power_resource_list_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500709
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100710 list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
711 int result, state;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200712
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100713 mutex_lock(&resource->resource_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500714
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100715 result = acpi_power_get_state(resource->device.handle, &state);
716 if (!result && state == ACPI_POWER_RESOURCE_STATE_OFF
717 && resource->ref_count) {
718 dev_info(&resource->device.dev, "Turning ON\n");
719 __acpi_power_on(resource);
720 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500721
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100722 mutex_unlock(&resource->resource_lock);
723 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500724
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100725 mutex_unlock(&power_resource_list_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500726}
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200727#endif