blob: 06ad05288af8d7ad8647695434518b8e4d7f02d1 [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. Wysocki781d7372013-01-17 14:11:06 +010078static LIST_HEAD(acpi_power_resource_list);
79static DEFINE_MUTEX(power_resource_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/* --------------------------------------------------------------------------
82 Power Resource Management
83 -------------------------------------------------------------------------- */
84
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010085static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010087 struct acpi_device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010089 if (acpi_bus_get_device(handle, &device))
90 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +010092 return container_of(device, struct acpi_power_resource, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Zhao Yakuia51e1452008-08-11 14:55:05 +080095static int acpi_power_get_state(acpi_handle handle, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Len Brown4be44fc2005-08-05 00:44:28 -040097 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -040098 unsigned long long sta = 0;
Lin Ming60a4ce72008-12-16 17:02:22 +080099 char node_name[5];
100 struct acpi_buffer buffer = { sizeof(node_name), node_name };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Zhao Yakuia51e1452008-08-11 14:55:05 +0800103 if (!handle || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400104 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Zhao Yakuia51e1452008-08-11 14:55:05 +0800106 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400108 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400110 *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
111 ACPI_POWER_RESOURCE_STATE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Lin Ming60a4ce72008-12-16 17:02:22 +0800113 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
Lin Ming60a4ce72008-12-16 17:02:22 +0800116 node_name,
Zhao Yakuib1b57fb2008-10-27 16:04:53 +0800117 *state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Patrick Mocheld550d982006-06-27 00:41:40 -0400119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Len Brown4be44fc2005-08-05 00:44:28 -0400122static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100124 int cur_state;
125 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 if (!list || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400128 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 /* The state of the list is 'on' IFF all resources are 'on'. */
131
Len Brown4be44fc2005-08-05 00:44:28 -0400132 for (i = 0; i < list->count; i++) {
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100133 struct acpi_power_resource *resource;
134 acpi_handle handle = list->handles[i];
135 int result;
136
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100137 resource = acpi_power_get_context(handle);
138 if (!resource)
139 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100141 mutex_lock(&resource->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100143 result = acpi_power_get_state(handle, &cur_state);
144
145 mutex_unlock(&resource->resource_lock);
146
147 if (result)
148 return result;
149
150 if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 break;
152 }
153
154 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100155 cur_state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100157 *state = cur_state;
158
159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100162static void acpi_power_resume_dependent(struct work_struct *work)
Lin Ming0090def2012-03-29 14:09:39 +0800163{
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100164 struct acpi_power_dependent_device *dep;
165 struct acpi_device_physical_node *pn;
166 struct acpi_device *adev;
Lin Ming0090def2012-03-29 14:09:39 +0800167 int state;
168
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100169 dep = container_of(work, struct acpi_power_dependent_device, work);
170 adev = dep->adev;
171 if (acpi_power_get_inferred_state(adev, &state))
Lin Ming0090def2012-03-29 14:09:39 +0800172 return;
173
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100174 if (state > ACPI_STATE_D0)
Lin Ming0090def2012-03-29 14:09:39 +0800175 return;
176
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100177 mutex_lock(&adev->physical_node_lock);
178
179 list_for_each_entry(pn, &adev->physical_node_list, node)
180 pm_request_resume(pn->dev);
181
182 list_for_each_entry(pn, &adev->power_dependent, node)
183 pm_request_resume(pn->dev);
184
185 mutex_unlock(&adev->physical_node_lock);
Lin Ming0090def2012-03-29 14:09:39 +0800186}
187
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200188static int __acpi_power_on(struct acpi_power_resource *resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Len Brown4be44fc2005-08-05 00:44:28 -0400190 acpi_status status = AE_OK;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500191
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100192 status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400194 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200196 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400197 resource->name));
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200198
Patrick Mocheld550d982006-06-27 00:41:40 -0400199 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200202static int acpi_power_on(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Bjorn Helgaasbdf43bb2009-05-21 17:28:53 -0600204 int result = 0;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100205 struct acpi_power_resource *resource;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500206
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100207 resource = acpi_power_get_context(handle);
208 if (!resource)
209 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500211 mutex_lock(&resource->resource_lock);
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200212
213 if (resource->ref_count++) {
214 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
215 "Power resource [%s] already on",
216 resource->name));
217 } else {
218 result = __acpi_power_on(resource);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100219 if (result) {
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100220 resource->ref_count--;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100221 } else {
222 struct acpi_power_dependent_device *dep;
223
224 list_for_each_entry(dep, &resource->dependent, node)
225 schedule_work(&dep->work);
226 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500229 mutex_unlock(&resource->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100231 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Rafael J. Wysocki36237fa2011-01-06 23:38:04 +0100234static int acpi_power_off(acpi_handle handle)
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200235{
236 int result = 0;
237 acpi_status status = AE_OK;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100238 struct acpi_power_resource *resource;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200239
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100240 resource = acpi_power_get_context(handle);
241 if (!resource)
242 return -ENODEV;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200243
244 mutex_lock(&resource->resource_lock);
245
246 if (!resource->ref_count) {
247 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
248 "Power resource [%s] already off",
249 resource->name));
250 goto unlock;
251 }
252
253 if (--resource->ref_count) {
254 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
255 "Power resource [%s] still in use\n",
256 resource->name));
257 goto unlock;
258 }
259
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100260 status = acpi_evaluate_object(resource->device.handle, "_OFF", NULL, NULL);
Rafael J. Wysocki722c9292013-01-17 14:11:06 +0100261 if (ACPI_FAILURE(status))
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200262 result = -ENODEV;
Rafael J. Wysocki722c9292013-01-17 14:11:06 +0100263 else
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200264 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
265 "Power resource [%s] turned off\n",
266 resource->name));
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200267
268 unlock:
269 mutex_unlock(&resource->resource_lock);
270
271 return result;
272}
273
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100274static void __acpi_power_off_list(struct acpi_handle_list *list, int num_res)
275{
276 int i;
277
278 for (i = num_res - 1; i >= 0 ; i--)
Rafael J. Wysocki36237fa2011-01-06 23:38:04 +0100279 acpi_power_off(list->handles[i]);
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100280}
281
282static void acpi_power_off_list(struct acpi_handle_list *list)
283{
284 __acpi_power_off_list(list, list->count);
285}
286
287static int acpi_power_on_list(struct acpi_handle_list *list)
288{
289 int result = 0;
290 int i;
291
292 for (i = 0; i < list->count; i++) {
293 result = acpi_power_on(list->handles[i]);
294 if (result) {
295 __acpi_power_off_list(list, i);
296 break;
297 }
298 }
299
300 return result;
301}
302
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100303static void acpi_power_add_dependent(acpi_handle rhandle,
304 struct acpi_device *adev)
Lin Ming0090def2012-03-29 14:09:39 +0800305{
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100306 struct acpi_power_dependent_device *dep;
307 struct acpi_power_resource *resource;
Lin Ming0090def2012-03-29 14:09:39 +0800308
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100309 if (!rhandle || !adev)
310 return;
311
312 resource = acpi_power_get_context(rhandle);
313 if (!resource)
Lin Ming0090def2012-03-29 14:09:39 +0800314 return;
315
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100316 mutex_lock(&resource->resource_lock);
317
318 list_for_each_entry(dep, &resource->dependent, node)
319 if (dep->adev == adev)
320 goto out;
321
322 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
323 if (!dep)
324 goto out;
325
326 dep->adev = adev;
327 INIT_WORK(&dep->work, acpi_power_resume_dependent);
328 list_add_tail(&dep->node, &resource->dependent);
329
330 out:
331 mutex_unlock(&resource->resource_lock);
332}
333
334static void acpi_power_remove_dependent(acpi_handle rhandle,
335 struct acpi_device *adev)
336{
337 struct acpi_power_dependent_device *dep;
338 struct acpi_power_resource *resource;
339 struct work_struct *work = NULL;
340
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100341 if (!rhandle || !adev)
342 return;
343
344 resource = acpi_power_get_context(rhandle);
345 if (!resource)
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100346 return;
347
348 mutex_lock(&resource->resource_lock);
349
350 list_for_each_entry(dep, &resource->dependent, node)
351 if (dep->adev == adev) {
352 list_del(&dep->node);
353 work = &dep->work;
354 break;
355 }
356
357 mutex_unlock(&resource->resource_lock);
358
359 if (work) {
360 cancel_work_sync(work);
361 kfree(dep);
362 }
363}
364
365void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
366{
367 if (adev->power.flags.power_resources) {
368 struct acpi_device_power_state *ps;
369 int j;
370
371 ps = &adev->power.states[ACPI_STATE_D0];
372 for (j = 0; j < ps->resources.count; j++) {
373 acpi_handle rhandle = ps->resources.handles[j];
374
375 if (add)
376 acpi_power_add_dependent(rhandle, adev);
Lin Ming0090def2012-03-29 14:09:39 +0800377 else
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100378 acpi_power_remove_dependent(rhandle, adev);
Lin Ming0090def2012-03-29 14:09:39 +0800379 }
380 }
Lin Ming0090def2012-03-29 14:09:39 +0800381}
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100382
383/* --------------------------------------------------------------------------
384 Device Power Management
385 -------------------------------------------------------------------------- */
Lin Ming0090def2012-03-29 14:09:39 +0800386
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200387/**
388 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
389 * ACPI 3.0) _PSW (Power State Wake)
390 * @dev: Device to handle.
391 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
392 * @sleep_state: Target sleep state of the system.
393 * @dev_state: Target power state of the device.
394 *
395 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
396 * State Wake) for the device, if present. On failure reset the device's
397 * wakeup.flags.valid flag.
398 *
399 * RETURN VALUE:
400 * 0 if either _DSW or _PSW has been successfully executed
401 * 0 if neither _DSW nor _PSW has been found
402 * -ENODEV if the execution of either _DSW or _PSW has failed
403 */
404int acpi_device_sleep_wake(struct acpi_device *dev,
405 int enable, int sleep_state, int dev_state)
406{
407 union acpi_object in_arg[3];
408 struct acpi_object_list arg_list = { 3, in_arg };
409 acpi_status status = AE_OK;
410
411 /*
412 * Try to execute _DSW first.
413 *
414 * Three agruments are needed for the _DSW object:
415 * Argument 0: enable/disable the wake capabilities
416 * Argument 1: target system state
417 * Argument 2: target device state
418 * When _DSW object is called to disable the wake capabilities, maybe
419 * the first argument is filled. The values of the other two agruments
420 * are meaningless.
421 */
422 in_arg[0].type = ACPI_TYPE_INTEGER;
423 in_arg[0].integer.value = enable;
424 in_arg[1].type = ACPI_TYPE_INTEGER;
425 in_arg[1].integer.value = sleep_state;
426 in_arg[2].type = ACPI_TYPE_INTEGER;
427 in_arg[2].integer.value = dev_state;
428 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
429 if (ACPI_SUCCESS(status)) {
430 return 0;
431 } else if (status != AE_NOT_FOUND) {
432 printk(KERN_ERR PREFIX "_DSW execution failed\n");
433 dev->wakeup.flags.valid = 0;
434 return -ENODEV;
435 }
436
437 /* Execute _PSW */
438 arg_list.count = 1;
439 in_arg[0].integer.value = enable;
440 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
441 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
442 printk(KERN_ERR PREFIX "_PSW execution failed\n");
443 dev->wakeup.flags.valid = 0;
444 return -ENODEV;
445 }
446
447 return 0;
448}
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450/*
451 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
452 * 1. Power on the power resources required for the wakeup device
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200453 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
454 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200456int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200458 int i, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200461 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200463 mutex_lock(&acpi_device_lock);
464
465 if (dev->wakeup.prepare_count++)
466 goto out;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /* Open power resource */
469 for (i = 0; i < dev->wakeup.resources.count; i++) {
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200470 int ret = acpi_power_on(dev->wakeup.resources.handles[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (ret) {
Len Brown64684632006-06-26 23:41:38 -0400472 printk(KERN_ERR PREFIX "Transition power state\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 dev->wakeup.flags.valid = 0;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200474 err = -ENODEV;
475 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477 }
478
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200479 /*
480 * Passing 3 as the third argument below means the device may be placed
481 * in arbitrary power state afterwards.
482 */
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200483 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200484
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200485 err_out:
486 if (err)
487 dev->wakeup.prepare_count = 0;
488
489 out:
490 mutex_unlock(&acpi_device_lock);
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200491 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
494/*
495 * Shutdown a wakeup device, counterpart of above method
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200496 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
497 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 * 2. Shutdown down the power resources
499 */
Len Brown4be44fc2005-08-05 00:44:28 -0400500int acpi_disable_wakeup_device_power(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200502 int i, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200505 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200507 mutex_lock(&acpi_device_lock);
508
509 if (--dev->wakeup.prepare_count > 0)
510 goto out;
511
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200512 /*
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200513 * Executing the code below even if prepare_count is already zero when
514 * the function is called may be useful, for example for initialisation.
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200515 */
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200516 if (dev->wakeup.prepare_count < 0)
517 dev->wakeup.prepare_count = 0;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200518
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200519 err = acpi_device_sleep_wake(dev, 0, 0, 0);
520 if (err)
521 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 /* Close power resource */
524 for (i = 0; i < dev->wakeup.resources.count; i++) {
Rafael J. Wysocki36237fa2011-01-06 23:38:04 +0100525 int ret = acpi_power_off(dev->wakeup.resources.handles[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (ret) {
Len Brown64684632006-06-26 23:41:38 -0400527 printk(KERN_ERR PREFIX "Transition power state\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 dev->wakeup.flags.valid = 0;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200529 err = -ENODEV;
530 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532 }
533
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200534 out:
535 mutex_unlock(&acpi_device_lock);
536 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100539int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Len Brown4be44fc2005-08-05 00:44:28 -0400541 int result = 0;
542 struct acpi_handle_list *list = NULL;
543 int list_state = 0;
544 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100546 if (!device || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400547 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /*
550 * We know a device's inferred power state when all the resources
551 * required for a given D-state are 'on'.
552 */
Rafael J. Wysocki38c92ff2012-05-20 13:58:00 +0200553 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 list = &device->power.states[i].resources;
555 if (list->count < 1)
556 continue;
557
558 result = acpi_power_get_list_state(list, &list_state);
559 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400560 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100563 *state = i;
Patrick Mocheld550d982006-06-27 00:41:40 -0400564 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566 }
567
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100568 *state = ACPI_STATE_D3;
Patrick Mocheld550d982006-06-27 00:41:40 -0400569 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
Rafael J. Wysocki30d3df412010-11-25 00:06:55 +0100572int acpi_power_on_resources(struct acpi_device *device, int state)
573{
574 if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3)
575 return -EINVAL;
576
577 return acpi_power_on_list(&device->power.states[state].resources);
578}
579
Len Brown4be44fc2005-08-05 00:44:28 -0400580int acpi_power_transition(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200582 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800584 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400585 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Rafael J. Wysocki212967c2010-11-25 00:02:36 +0100587 if (device->power.state == state)
588 return 0;
589
Len Brown4be44fc2005-08-05 00:44:28 -0400590 if ((device->power.state < ACPI_STATE_D0)
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800591 || (device->power.state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400592 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /* TBD: Resources must be ordered. */
595
596 /*
597 * First we reference all power resources required in the target list
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100598 * (e.g. so the device doesn't lose power while transitioning). Then,
599 * we dereference all power resources used in the current list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200601 if (state < ACPI_STATE_D3_COLD)
602 result = acpi_power_on_list(
603 &device->power.states[state].resources);
604
605 if (!result && device->power.state < ACPI_STATE_D3_COLD)
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100606 acpi_power_off_list(
607 &device->power.states[device->power.state].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100609 /* We shouldn't change the state unless the above operations succeed. */
610 device->power.state = result ? ACPI_STATE_UNKNOWN : state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Patrick Mocheld550d982006-06-27 00:41:40 -0400612 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100615static void acpi_release_power_resource(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100617 struct acpi_device *device = to_acpi_device(dev);
618 struct acpi_power_resource *resource;
619
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100620 resource = container_of(device, struct acpi_power_resource, device);
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100621
622 mutex_lock(&power_resource_list_lock);
623 list_del(&resource->list_node);
624 mutex_unlock(&power_resource_list_lock);
625
626 acpi_free_ids(device);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100627 kfree(resource);
628}
629
630void acpi_add_power_resource(acpi_handle handle)
631{
632 struct acpi_power_resource *resource;
633 struct acpi_device *device = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400634 union acpi_object acpi_object;
635 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100636 acpi_status status;
637 int state, result = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100639 acpi_bus_get_device(handle, &device);
640 if (device)
641 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100643 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (!resource)
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100645 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100647 device = &resource->device;
648 acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER,
649 ACPI_STA_DEFAULT);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500650 mutex_init(&resource->resource_lock);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100651 INIT_LIST_HEAD(&resource->dependent);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100652 resource->name = device->pnp.bus_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
654 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
Rafael J. Wysocki722c9292013-01-17 14:11:06 +0100655 device->power.state = ACPI_STATE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 /* Evalute the object to get the system level and resource order. */
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100658 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
659 if (ACPI_FAILURE(status))
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100660 goto err;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 resource->system_level = acpi_object.power_resource.system_level;
663 resource->order = acpi_object.power_resource.resource_order;
664
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100665 result = acpi_power_get_state(handle, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (result)
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100667 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Len Brown4be44fc2005-08-05 00:44:28 -0400669 printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400670 acpi_device_bid(device), state ? "on" : "off");
Len Brown4be44fc2005-08-05 00:44:28 -0400671
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100672 device->flags.match_driver = true;
673 result = acpi_device_register(device, acpi_release_power_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (result)
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100675 goto err;
Len Brown4be44fc2005-08-05 00:44:28 -0400676
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100677 mutex_lock(&power_resource_list_lock);
678 list_add(&resource->list_node, &acpi_power_resource_list);
679 mutex_unlock(&power_resource_list_lock);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100680 return;
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100681
682 err:
683 acpi_release_power_resource(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100686#ifdef CONFIG_ACPI_SLEEP
687void acpi_resume_power_resources(void)
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500688{
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200689 struct acpi_power_resource *resource;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500690
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100691 mutex_lock(&power_resource_list_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500692
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100693 list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
694 int result, state;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200695
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100696 mutex_lock(&resource->resource_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500697
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100698 result = acpi_power_get_state(resource->device.handle, &state);
699 if (!result && state == ACPI_POWER_RESOURCE_STATE_OFF
700 && resource->ref_count) {
701 dev_info(&resource->device.dev, "Turning ON\n");
702 __acpi_power_on(resource);
703 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500704
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100705 mutex_unlock(&resource->resource_lock);
706 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500707
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100708 mutex_unlock(&power_resource_list_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500709}
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200710#endif