blob: 29803857a2efdaa9a1e762549c23b62a82ade29a [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /* Update the power resource's _device_ power state */
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100197 resource->device.power.state = ACPI_STATE_D0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200199 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400200 resource->name));
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200201
Patrick Mocheld550d982006-06-27 00:41:40 -0400202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200205static int acpi_power_on(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Bjorn Helgaasbdf43bb2009-05-21 17:28:53 -0600207 int result = 0;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100208 struct acpi_power_resource *resource;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500209
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100210 resource = acpi_power_get_context(handle);
211 if (!resource)
212 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500214 mutex_lock(&resource->resource_lock);
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200215
216 if (resource->ref_count++) {
217 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
218 "Power resource [%s] already on",
219 resource->name));
220 } else {
221 result = __acpi_power_on(resource);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100222 if (result) {
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100223 resource->ref_count--;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100224 } else {
225 struct acpi_power_dependent_device *dep;
226
227 list_for_each_entry(dep, &resource->dependent, node)
228 schedule_work(&dep->work);
229 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500232 mutex_unlock(&resource->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100234 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Rafael J. Wysocki36237fa2011-01-06 23:38:04 +0100237static int acpi_power_off(acpi_handle handle)
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200238{
239 int result = 0;
240 acpi_status status = AE_OK;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100241 struct acpi_power_resource *resource;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200242
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100243 resource = acpi_power_get_context(handle);
244 if (!resource)
245 return -ENODEV;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200246
247 mutex_lock(&resource->resource_lock);
248
249 if (!resource->ref_count) {
250 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
251 "Power resource [%s] already off",
252 resource->name));
253 goto unlock;
254 }
255
256 if (--resource->ref_count) {
257 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
258 "Power resource [%s] still in use\n",
259 resource->name));
260 goto unlock;
261 }
262
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100263 status = acpi_evaluate_object(resource->device.handle, "_OFF", NULL, NULL);
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200264 if (ACPI_FAILURE(status)) {
265 result = -ENODEV;
266 } else {
267 /* Update the power resource's _device_ power state */
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100268 resource->device.power.state = ACPI_STATE_D3;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200269
270 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
271 "Power resource [%s] turned off\n",
272 resource->name));
273 }
274
275 unlock:
276 mutex_unlock(&resource->resource_lock);
277
278 return result;
279}
280
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100281static void __acpi_power_off_list(struct acpi_handle_list *list, int num_res)
282{
283 int i;
284
285 for (i = num_res - 1; i >= 0 ; i--)
Rafael J. Wysocki36237fa2011-01-06 23:38:04 +0100286 acpi_power_off(list->handles[i]);
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100287}
288
289static void acpi_power_off_list(struct acpi_handle_list *list)
290{
291 __acpi_power_off_list(list, list->count);
292}
293
294static int acpi_power_on_list(struct acpi_handle_list *list)
295{
296 int result = 0;
297 int i;
298
299 for (i = 0; i < list->count; i++) {
300 result = acpi_power_on(list->handles[i]);
301 if (result) {
302 __acpi_power_off_list(list, i);
303 break;
304 }
305 }
306
307 return result;
308}
309
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100310static void acpi_power_add_dependent(acpi_handle rhandle,
311 struct acpi_device *adev)
Lin Ming0090def2012-03-29 14:09:39 +0800312{
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100313 struct acpi_power_dependent_device *dep;
314 struct acpi_power_resource *resource;
Lin Ming0090def2012-03-29 14:09:39 +0800315
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100316 if (!rhandle || !adev)
317 return;
318
319 resource = acpi_power_get_context(rhandle);
320 if (!resource)
Lin Ming0090def2012-03-29 14:09:39 +0800321 return;
322
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100323 mutex_lock(&resource->resource_lock);
324
325 list_for_each_entry(dep, &resource->dependent, node)
326 if (dep->adev == adev)
327 goto out;
328
329 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
330 if (!dep)
331 goto out;
332
333 dep->adev = adev;
334 INIT_WORK(&dep->work, acpi_power_resume_dependent);
335 list_add_tail(&dep->node, &resource->dependent);
336
337 out:
338 mutex_unlock(&resource->resource_lock);
339}
340
341static void acpi_power_remove_dependent(acpi_handle rhandle,
342 struct acpi_device *adev)
343{
344 struct acpi_power_dependent_device *dep;
345 struct acpi_power_resource *resource;
346 struct work_struct *work = NULL;
347
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100348 if (!rhandle || !adev)
349 return;
350
351 resource = acpi_power_get_context(rhandle);
352 if (!resource)
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100353 return;
354
355 mutex_lock(&resource->resource_lock);
356
357 list_for_each_entry(dep, &resource->dependent, node)
358 if (dep->adev == adev) {
359 list_del(&dep->node);
360 work = &dep->work;
361 break;
362 }
363
364 mutex_unlock(&resource->resource_lock);
365
366 if (work) {
367 cancel_work_sync(work);
368 kfree(dep);
369 }
370}
371
372void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
373{
374 if (adev->power.flags.power_resources) {
375 struct acpi_device_power_state *ps;
376 int j;
377
378 ps = &adev->power.states[ACPI_STATE_D0];
379 for (j = 0; j < ps->resources.count; j++) {
380 acpi_handle rhandle = ps->resources.handles[j];
381
382 if (add)
383 acpi_power_add_dependent(rhandle, adev);
Lin Ming0090def2012-03-29 14:09:39 +0800384 else
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100385 acpi_power_remove_dependent(rhandle, adev);
Lin Ming0090def2012-03-29 14:09:39 +0800386 }
387 }
Lin Ming0090def2012-03-29 14:09:39 +0800388}
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100389
390/* --------------------------------------------------------------------------
391 Device Power Management
392 -------------------------------------------------------------------------- */
Lin Ming0090def2012-03-29 14:09:39 +0800393
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200394/**
395 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
396 * ACPI 3.0) _PSW (Power State Wake)
397 * @dev: Device to handle.
398 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
399 * @sleep_state: Target sleep state of the system.
400 * @dev_state: Target power state of the device.
401 *
402 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
403 * State Wake) for the device, if present. On failure reset the device's
404 * wakeup.flags.valid flag.
405 *
406 * RETURN VALUE:
407 * 0 if either _DSW or _PSW has been successfully executed
408 * 0 if neither _DSW nor _PSW has been found
409 * -ENODEV if the execution of either _DSW or _PSW has failed
410 */
411int acpi_device_sleep_wake(struct acpi_device *dev,
412 int enable, int sleep_state, int dev_state)
413{
414 union acpi_object in_arg[3];
415 struct acpi_object_list arg_list = { 3, in_arg };
416 acpi_status status = AE_OK;
417
418 /*
419 * Try to execute _DSW first.
420 *
421 * Three agruments are needed for the _DSW object:
422 * Argument 0: enable/disable the wake capabilities
423 * Argument 1: target system state
424 * Argument 2: target device state
425 * When _DSW object is called to disable the wake capabilities, maybe
426 * the first argument is filled. The values of the other two agruments
427 * are meaningless.
428 */
429 in_arg[0].type = ACPI_TYPE_INTEGER;
430 in_arg[0].integer.value = enable;
431 in_arg[1].type = ACPI_TYPE_INTEGER;
432 in_arg[1].integer.value = sleep_state;
433 in_arg[2].type = ACPI_TYPE_INTEGER;
434 in_arg[2].integer.value = dev_state;
435 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
436 if (ACPI_SUCCESS(status)) {
437 return 0;
438 } else if (status != AE_NOT_FOUND) {
439 printk(KERN_ERR PREFIX "_DSW execution failed\n");
440 dev->wakeup.flags.valid = 0;
441 return -ENODEV;
442 }
443
444 /* Execute _PSW */
445 arg_list.count = 1;
446 in_arg[0].integer.value = enable;
447 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
448 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
449 printk(KERN_ERR PREFIX "_PSW execution failed\n");
450 dev->wakeup.flags.valid = 0;
451 return -ENODEV;
452 }
453
454 return 0;
455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/*
458 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
459 * 1. Power on the power resources required for the wakeup device
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200460 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
461 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200463int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200465 int i, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200468 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200470 mutex_lock(&acpi_device_lock);
471
472 if (dev->wakeup.prepare_count++)
473 goto out;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 /* Open power resource */
476 for (i = 0; i < dev->wakeup.resources.count; i++) {
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200477 int ret = acpi_power_on(dev->wakeup.resources.handles[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (ret) {
Len Brown64684632006-06-26 23:41:38 -0400479 printk(KERN_ERR PREFIX "Transition power state\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 dev->wakeup.flags.valid = 0;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200481 err = -ENODEV;
482 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484 }
485
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200486 /*
487 * Passing 3 as the third argument below means the device may be placed
488 * in arbitrary power state afterwards.
489 */
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200490 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200491
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200492 err_out:
493 if (err)
494 dev->wakeup.prepare_count = 0;
495
496 out:
497 mutex_unlock(&acpi_device_lock);
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200498 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
501/*
502 * Shutdown a wakeup device, counterpart of above method
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200503 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
504 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 * 2. Shutdown down the power resources
506 */
Len Brown4be44fc2005-08-05 00:44:28 -0400507int acpi_disable_wakeup_device_power(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200509 int i, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200512 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200514 mutex_lock(&acpi_device_lock);
515
516 if (--dev->wakeup.prepare_count > 0)
517 goto out;
518
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200519 /*
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200520 * Executing the code below even if prepare_count is already zero when
521 * the function is called may be useful, for example for initialisation.
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200522 */
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200523 if (dev->wakeup.prepare_count < 0)
524 dev->wakeup.prepare_count = 0;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200525
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200526 err = acpi_device_sleep_wake(dev, 0, 0, 0);
527 if (err)
528 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 /* Close power resource */
531 for (i = 0; i < dev->wakeup.resources.count; i++) {
Rafael J. Wysocki36237fa2011-01-06 23:38:04 +0100532 int ret = acpi_power_off(dev->wakeup.resources.handles[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (ret) {
Len Brown64684632006-06-26 23:41:38 -0400534 printk(KERN_ERR PREFIX "Transition power state\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 dev->wakeup.flags.valid = 0;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200536 err = -ENODEV;
537 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539 }
540
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200541 out:
542 mutex_unlock(&acpi_device_lock);
543 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100546int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Len Brown4be44fc2005-08-05 00:44:28 -0400548 int result = 0;
549 struct acpi_handle_list *list = NULL;
550 int list_state = 0;
551 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100553 if (!device || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400554 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 /*
557 * We know a device's inferred power state when all the resources
558 * required for a given D-state are 'on'.
559 */
Rafael J. Wysocki38c92ff2012-05-20 13:58:00 +0200560 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 list = &device->power.states[i].resources;
562 if (list->count < 1)
563 continue;
564
565 result = acpi_power_get_list_state(list, &list_state);
566 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400567 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100570 *state = i;
Patrick Mocheld550d982006-06-27 00:41:40 -0400571 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573 }
574
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100575 *state = ACPI_STATE_D3;
Patrick Mocheld550d982006-06-27 00:41:40 -0400576 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
Rafael J. Wysocki30d3df42010-11-25 00:06:55 +0100579int acpi_power_on_resources(struct acpi_device *device, int state)
580{
581 if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3)
582 return -EINVAL;
583
584 return acpi_power_on_list(&device->power.states[state].resources);
585}
586
Len Brown4be44fc2005-08-05 00:44:28 -0400587int acpi_power_transition(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200589 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Zhang Rui3ebc81b82012-03-29 14:09:38 +0800591 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400592 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Rafael J. Wysocki212967c2010-11-25 00:02:36 +0100594 if (device->power.state == state)
595 return 0;
596
Len Brown4be44fc2005-08-05 00:44:28 -0400597 if ((device->power.state < ACPI_STATE_D0)
Zhang Rui3ebc81b82012-03-29 14:09:38 +0800598 || (device->power.state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400599 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 /* TBD: Resources must be ordered. */
602
603 /*
604 * First we reference all power resources required in the target list
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100605 * (e.g. so the device doesn't lose power while transitioning). Then,
606 * we dereference all power resources used in the current list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 */
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200608 if (state < ACPI_STATE_D3_COLD)
609 result = acpi_power_on_list(
610 &device->power.states[state].resources);
611
612 if (!result && device->power.state < ACPI_STATE_D3_COLD)
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100613 acpi_power_off_list(
614 &device->power.states[device->power.state].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100616 /* We shouldn't change the state unless the above operations succeed. */
617 device->power.state = result ? ACPI_STATE_UNKNOWN : state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Patrick Mocheld550d982006-06-27 00:41:40 -0400619 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100622static void acpi_release_power_resource(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100624 struct acpi_device *device = to_acpi_device(dev);
625 struct acpi_power_resource *resource;
626
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100627 resource = container_of(device, struct acpi_power_resource, device);
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100628
629 mutex_lock(&power_resource_list_lock);
630 list_del(&resource->list_node);
631 mutex_unlock(&power_resource_list_lock);
632
633 acpi_free_ids(device);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100634 kfree(resource);
635}
636
637void acpi_add_power_resource(acpi_handle handle)
638{
639 struct acpi_power_resource *resource;
640 struct acpi_device *device = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400641 union acpi_object acpi_object;
642 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100643 acpi_status status;
644 int state, result = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100646 acpi_bus_get_device(handle, &device);
647 if (device)
648 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100650 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (!resource)
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100652 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100654 device = &resource->device;
655 acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER,
656 ACPI_STA_DEFAULT);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500657 mutex_init(&resource->resource_lock);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100658 INIT_LIST_HEAD(&resource->dependent);
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100659 resource->name = device->pnp.bus_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
661 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 /* Evalute the object to get the system level and resource order. */
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100664 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
665 if (ACPI_FAILURE(status))
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100666 goto err;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 resource->system_level = acpi_object.power_resource.system_level;
669 resource->order = acpi_object.power_resource.resource_order;
670
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100671 result = acpi_power_get_state(handle, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (result)
Rafael J. Wysocki781d7372013-01-17 14:11:06 +0100673 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400675 switch (state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 case ACPI_POWER_RESOURCE_STATE_ON:
677 device->power.state = ACPI_STATE_D0;
678 break;
679 case ACPI_POWER_RESOURCE_STATE_OFF:
680 device->power.state = ACPI_STATE_D3;
681 break;
682 default:
683 device->power.state = ACPI_STATE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685
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