blob: 659386c4f0cbe6a14eca31a8a7e74a285186a5f6 [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
Len Brown4be44fc2005-08-05 00:44:28 -040061static int acpi_power_add(struct acpi_device *device);
62static int acpi_power_remove(struct acpi_device *device, int type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Márton Némethc97adf92010-01-10 17:15:36 +010064static const struct acpi_device_id power_device_ids[] = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +020065 {ACPI_POWER_HID, 0},
66 {"", 0},
67};
68MODULE_DEVICE_TABLE(acpi, power_device_ids);
69
Rafael J. Wysocki90692402012-08-09 23:00:02 +020070#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockie579e2d2012-06-27 23:26:59 +020071static int acpi_power_resume(struct device *dev);
Rafael J. Wysocki90692402012-08-09 23:00:02 +020072#endif
Rafael J. Wysockie579e2d2012-06-27 23:26:59 +020073static SIMPLE_DEV_PM_OPS(acpi_power_pm, NULL, acpi_power_resume);
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static struct acpi_driver acpi_power_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050076 .name = "power",
Len Brown4be44fc2005-08-05 00:44:28 -040077 .class = ACPI_POWER_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020078 .ids = power_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -040079 .ops = {
80 .add = acpi_power_add,
81 .remove = acpi_power_remove,
82 },
Rafael J. Wysockie579e2d2012-06-27 23:26:59 +020083 .drv.pm = &acpi_power_pm,
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
85
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +010086struct acpi_power_dependent_device {
87 struct list_head node;
88 struct acpi_device *adev;
89 struct work_struct work;
Lin Ming0090def2012-03-29 14:09:39 +080090};
91
Len Brown4be44fc2005-08-05 00:44:28 -040092struct acpi_power_resource {
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +010093 struct acpi_device *device;
94 struct list_head dependent;
Len Brown4be44fc2005-08-05 00:44:28 -040095 acpi_bus_id name;
96 u32 system_level;
97 u32 order;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +020098 unsigned int ref_count;
Konstantin Karasyov0a613902007-02-16 01:47:06 -050099 struct mutex resource_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
Len Brown4be44fc2005-08-05 00:44:28 -0400102static struct list_head acpi_power_resource_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/* --------------------------------------------------------------------------
105 Power Resource Management
106 -------------------------------------------------------------------------- */
107
108static int
Len Brown4be44fc2005-08-05 00:44:28 -0400109acpi_power_get_context(acpi_handle handle,
110 struct acpi_power_resource **resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Len Brown4be44fc2005-08-05 00:44:28 -0400112 int result = 0;
113 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 if (!resource)
Patrick Mocheld550d982006-06-27 00:41:40 -0400117 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 result = acpi_bus_get_device(handle, &device);
120 if (result) {
Len Browncece9292006-06-26 23:04:31 -0400121 printk(KERN_WARNING PREFIX "Getting context [%p]\n", handle);
Patrick Mocheld550d982006-06-27 00:41:40 -0400122 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200125 *resource = acpi_driver_data(device);
Li Zefana815ab82008-04-18 13:27:29 -0700126 if (!*resource)
Patrick Mocheld550d982006-06-27 00:41:40 -0400127 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Patrick Mocheld550d982006-06-27 00:41:40 -0400129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Zhao Yakuia51e1452008-08-11 14:55:05 +0800132static int acpi_power_get_state(acpi_handle handle, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Len Brown4be44fc2005-08-05 00:44:28 -0400134 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400135 unsigned long long sta = 0;
Lin Ming60a4ce72008-12-16 17:02:22 +0800136 char node_name[5];
137 struct acpi_buffer buffer = { sizeof(node_name), node_name };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Zhao Yakuia51e1452008-08-11 14:55:05 +0800140 if (!handle || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400141 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Zhao Yakuia51e1452008-08-11 14:55:05 +0800143 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400145 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400147 *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
148 ACPI_POWER_RESOURCE_STATE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Lin Ming60a4ce72008-12-16 17:02:22 +0800150 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
Lin Ming60a4ce72008-12-16 17:02:22 +0800153 node_name,
Zhao Yakuib1b57fb2008-10-27 16:04:53 +0800154 *state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Patrick Mocheld550d982006-06-27 00:41:40 -0400156 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Len Brown4be44fc2005-08-05 00:44:28 -0400159static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100161 int cur_state;
162 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 if (!list || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400165 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 /* The state of the list is 'on' IFF all resources are 'on'. */
168
Len Brown4be44fc2005-08-05 00:44:28 -0400169 for (i = 0; i < list->count; i++) {
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100170 struct acpi_power_resource *resource;
171 acpi_handle handle = list->handles[i];
172 int result;
173
174 result = acpi_power_get_context(handle, &resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400176 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100178 mutex_lock(&resource->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100180 result = acpi_power_get_state(handle, &cur_state);
181
182 mutex_unlock(&resource->resource_lock);
183
184 if (result)
185 return result;
186
187 if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 break;
189 }
190
191 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100192 cur_state ? "on" : "off"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Rafael J. Wysockid0515d92011-01-06 23:38:57 +0100194 *state = cur_state;
195
196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100199static void acpi_power_resume_dependent(struct work_struct *work)
Lin Ming0090def2012-03-29 14:09:39 +0800200{
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100201 struct acpi_power_dependent_device *dep;
202 struct acpi_device_physical_node *pn;
203 struct acpi_device *adev;
Lin Ming0090def2012-03-29 14:09:39 +0800204 int state;
205
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100206 dep = container_of(work, struct acpi_power_dependent_device, work);
207 adev = dep->adev;
208 if (acpi_power_get_inferred_state(adev, &state))
Lin Ming0090def2012-03-29 14:09:39 +0800209 return;
210
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100211 if (state > ACPI_STATE_D0)
Lin Ming0090def2012-03-29 14:09:39 +0800212 return;
213
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100214 mutex_lock(&adev->physical_node_lock);
215
216 list_for_each_entry(pn, &adev->physical_node_list, node)
217 pm_request_resume(pn->dev);
218
219 list_for_each_entry(pn, &adev->power_dependent, node)
220 pm_request_resume(pn->dev);
221
222 mutex_unlock(&adev->physical_node_lock);
Lin Ming0090def2012-03-29 14:09:39 +0800223}
224
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200225static int __acpi_power_on(struct acpi_power_resource *resource)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Len Brown4be44fc2005-08-05 00:44:28 -0400227 acpi_status status = AE_OK;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500228
Patrick Mochel5fbc19e2006-05-19 16:54:43 -0400229 status = acpi_evaluate_object(resource->device->handle, "_ON", NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400231 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 /* Update the power resource's _device_ power state */
Patrick Mochel41598572006-05-19 16:54:40 -0400234 resource->device->power.state = ACPI_STATE_D0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200236 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400237 resource->name));
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200238
Patrick Mocheld550d982006-06-27 00:41:40 -0400239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200242static int acpi_power_on(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Bjorn Helgaasbdf43bb2009-05-21 17:28:53 -0600244 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 struct acpi_power_resource *resource = NULL;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 result = acpi_power_get_context(handle, &resource);
248 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400249 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500251 mutex_lock(&resource->resource_lock);
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200252
253 if (resource->ref_count++) {
254 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
255 "Power resource [%s] already on",
256 resource->name));
257 } else {
258 result = __acpi_power_on(resource);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100259 if (result) {
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100260 resource->ref_count--;
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100261 } else {
262 struct acpi_power_dependent_device *dep;
263
264 list_for_each_entry(dep, &resource->dependent, node)
265 schedule_work(&dep->work);
266 }
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500269 mutex_unlock(&resource->resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Rafael J. Wysocki12b3b5a2010-11-25 00:03:32 +0100271 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
Rafael J. Wysocki36237fa2011-01-06 23:38:04 +0100274static int acpi_power_off(acpi_handle handle)
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200275{
276 int result = 0;
277 acpi_status status = AE_OK;
278 struct acpi_power_resource *resource = NULL;
279
280 result = acpi_power_get_context(handle, &resource);
281 if (result)
282 return result;
283
284 mutex_lock(&resource->resource_lock);
285
286 if (!resource->ref_count) {
287 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
288 "Power resource [%s] already off",
289 resource->name));
290 goto unlock;
291 }
292
293 if (--resource->ref_count) {
294 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
295 "Power resource [%s] still in use\n",
296 resource->name));
297 goto unlock;
298 }
299
300 status = acpi_evaluate_object(resource->device->handle, "_OFF", NULL, NULL);
301 if (ACPI_FAILURE(status)) {
302 result = -ENODEV;
303 } else {
304 /* Update the power resource's _device_ power state */
305 resource->device->power.state = ACPI_STATE_D3;
306
307 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
308 "Power resource [%s] turned off\n",
309 resource->name));
310 }
311
312 unlock:
313 mutex_unlock(&resource->resource_lock);
314
315 return result;
316}
317
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100318static void __acpi_power_off_list(struct acpi_handle_list *list, int num_res)
319{
320 int i;
321
322 for (i = num_res - 1; i >= 0 ; i--)
Rafael J. Wysocki36237fa2011-01-06 23:38:04 +0100323 acpi_power_off(list->handles[i]);
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100324}
325
326static void acpi_power_off_list(struct acpi_handle_list *list)
327{
328 __acpi_power_off_list(list, list->count);
329}
330
331static int acpi_power_on_list(struct acpi_handle_list *list)
332{
333 int result = 0;
334 int i;
335
336 for (i = 0; i < list->count; i++) {
337 result = acpi_power_on(list->handles[i]);
338 if (result) {
339 __acpi_power_off_list(list, i);
340 break;
341 }
342 }
343
344 return result;
345}
346
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100347static void acpi_power_add_dependent(acpi_handle rhandle,
348 struct acpi_device *adev)
Lin Ming0090def2012-03-29 14:09:39 +0800349{
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100350 struct acpi_power_dependent_device *dep;
351 struct acpi_power_resource *resource;
Lin Ming0090def2012-03-29 14:09:39 +0800352
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100353 if (!rhandle || !adev || acpi_power_get_context(rhandle, &resource))
Lin Ming0090def2012-03-29 14:09:39 +0800354 return;
355
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100356 mutex_lock(&resource->resource_lock);
357
358 list_for_each_entry(dep, &resource->dependent, node)
359 if (dep->adev == adev)
360 goto out;
361
362 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
363 if (!dep)
364 goto out;
365
366 dep->adev = adev;
367 INIT_WORK(&dep->work, acpi_power_resume_dependent);
368 list_add_tail(&dep->node, &resource->dependent);
369
370 out:
371 mutex_unlock(&resource->resource_lock);
372}
373
374static void acpi_power_remove_dependent(acpi_handle rhandle,
375 struct acpi_device *adev)
376{
377 struct acpi_power_dependent_device *dep;
378 struct acpi_power_resource *resource;
379 struct work_struct *work = NULL;
380
381 if (!rhandle || !adev || acpi_power_get_context(rhandle, &resource))
382 return;
383
384 mutex_lock(&resource->resource_lock);
385
386 list_for_each_entry(dep, &resource->dependent, node)
387 if (dep->adev == adev) {
388 list_del(&dep->node);
389 work = &dep->work;
390 break;
391 }
392
393 mutex_unlock(&resource->resource_lock);
394
395 if (work) {
396 cancel_work_sync(work);
397 kfree(dep);
398 }
399}
400
401void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
402{
403 if (adev->power.flags.power_resources) {
404 struct acpi_device_power_state *ps;
405 int j;
406
407 ps = &adev->power.states[ACPI_STATE_D0];
408 for (j = 0; j < ps->resources.count; j++) {
409 acpi_handle rhandle = ps->resources.handles[j];
410
411 if (add)
412 acpi_power_add_dependent(rhandle, adev);
Lin Ming0090def2012-03-29 14:09:39 +0800413 else
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100414 acpi_power_remove_dependent(rhandle, adev);
Lin Ming0090def2012-03-29 14:09:39 +0800415 }
416 }
Lin Ming0090def2012-03-29 14:09:39 +0800417}
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100418
419/* --------------------------------------------------------------------------
420 Device Power Management
421 -------------------------------------------------------------------------- */
Lin Ming0090def2012-03-29 14:09:39 +0800422
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200423/**
424 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
425 * ACPI 3.0) _PSW (Power State Wake)
426 * @dev: Device to handle.
427 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
428 * @sleep_state: Target sleep state of the system.
429 * @dev_state: Target power state of the device.
430 *
431 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
432 * State Wake) for the device, if present. On failure reset the device's
433 * wakeup.flags.valid flag.
434 *
435 * RETURN VALUE:
436 * 0 if either _DSW or _PSW has been successfully executed
437 * 0 if neither _DSW nor _PSW has been found
438 * -ENODEV if the execution of either _DSW or _PSW has failed
439 */
440int acpi_device_sleep_wake(struct acpi_device *dev,
441 int enable, int sleep_state, int dev_state)
442{
443 union acpi_object in_arg[3];
444 struct acpi_object_list arg_list = { 3, in_arg };
445 acpi_status status = AE_OK;
446
447 /*
448 * Try to execute _DSW first.
449 *
450 * Three agruments are needed for the _DSW object:
451 * Argument 0: enable/disable the wake capabilities
452 * Argument 1: target system state
453 * Argument 2: target device state
454 * When _DSW object is called to disable the wake capabilities, maybe
455 * the first argument is filled. The values of the other two agruments
456 * are meaningless.
457 */
458 in_arg[0].type = ACPI_TYPE_INTEGER;
459 in_arg[0].integer.value = enable;
460 in_arg[1].type = ACPI_TYPE_INTEGER;
461 in_arg[1].integer.value = sleep_state;
462 in_arg[2].type = ACPI_TYPE_INTEGER;
463 in_arg[2].integer.value = dev_state;
464 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
465 if (ACPI_SUCCESS(status)) {
466 return 0;
467 } else if (status != AE_NOT_FOUND) {
468 printk(KERN_ERR PREFIX "_DSW execution failed\n");
469 dev->wakeup.flags.valid = 0;
470 return -ENODEV;
471 }
472
473 /* Execute _PSW */
474 arg_list.count = 1;
475 in_arg[0].integer.value = enable;
476 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
477 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
478 printk(KERN_ERR PREFIX "_PSW execution failed\n");
479 dev->wakeup.flags.valid = 0;
480 return -ENODEV;
481 }
482
483 return 0;
484}
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486/*
487 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
488 * 1. Power on the power resources required for the wakeup device
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200489 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
490 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200492int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200494 int i, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200497 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200499 mutex_lock(&acpi_device_lock);
500
501 if (dev->wakeup.prepare_count++)
502 goto out;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /* Open power resource */
505 for (i = 0; i < dev->wakeup.resources.count; i++) {
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200506 int ret = acpi_power_on(dev->wakeup.resources.handles[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (ret) {
Len Brown64684632006-06-26 23:41:38 -0400508 printk(KERN_ERR PREFIX "Transition power state\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 dev->wakeup.flags.valid = 0;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200510 err = -ENODEV;
511 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513 }
514
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200515 /*
516 * Passing 3 as the third argument below means the device may be placed
517 * in arbitrary power state afterwards.
518 */
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200519 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200520
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200521 err_out:
522 if (err)
523 dev->wakeup.prepare_count = 0;
524
525 out:
526 mutex_unlock(&acpi_device_lock);
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200527 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
530/*
531 * Shutdown a wakeup device, counterpart of above method
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200532 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
533 * State Wake) for the device, if present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 * 2. Shutdown down the power resources
535 */
Len Brown4be44fc2005-08-05 00:44:28 -0400536int acpi_disable_wakeup_device_power(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200538 int i, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 if (!dev || !dev->wakeup.flags.valid)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200541 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200543 mutex_lock(&acpi_device_lock);
544
545 if (--dev->wakeup.prepare_count > 0)
546 goto out;
547
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200548 /*
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200549 * Executing the code below even if prepare_count is already zero when
550 * the function is called may be useful, for example for initialisation.
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200551 */
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200552 if (dev->wakeup.prepare_count < 0)
553 dev->wakeup.prepare_count = 0;
Rafael J. Wysocki0af4b8c2008-07-07 03:34:11 +0200554
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200555 err = acpi_device_sleep_wake(dev, 0, 0, 0);
556 if (err)
557 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 /* Close power resource */
560 for (i = 0; i < dev->wakeup.resources.count; i++) {
Rafael J. Wysocki36237fa2011-01-06 23:38:04 +0100561 int ret = acpi_power_off(dev->wakeup.resources.handles[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (ret) {
Len Brown64684632006-06-26 23:41:38 -0400563 printk(KERN_ERR PREFIX "Transition power state\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 dev->wakeup.flags.valid = 0;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200565 err = -ENODEV;
566 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568 }
569
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200570 out:
571 mutex_unlock(&acpi_device_lock);
572 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100575int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Len Brown4be44fc2005-08-05 00:44:28 -0400577 int result = 0;
578 struct acpi_handle_list *list = NULL;
579 int list_state = 0;
580 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100582 if (!device || !state)
Patrick Mocheld550d982006-06-27 00:41:40 -0400583 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /*
586 * We know a device's inferred power state when all the resources
587 * required for a given D-state are 'on'.
588 */
Rafael J. Wysocki38c92ff2012-05-20 13:58:00 +0200589 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 list = &device->power.states[i].resources;
591 if (list->count < 1)
592 continue;
593
594 result = acpi_power_get_list_state(list, &list_state);
595 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400596 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100599 *state = i;
Patrick Mocheld550d982006-06-27 00:41:40 -0400600 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602 }
603
Rafael J. Wysocki32a00d22010-11-25 00:05:17 +0100604 *state = ACPI_STATE_D3;
Patrick Mocheld550d982006-06-27 00:41:40 -0400605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Rafael J. Wysocki30d3df42010-11-25 00:06:55 +0100608int acpi_power_on_resources(struct acpi_device *device, int state)
609{
610 if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3)
611 return -EINVAL;
612
613 return acpi_power_on_list(&device->power.states[state].resources);
614}
615
Len Brown4be44fc2005-08-05 00:44:28 -0400616int acpi_power_transition(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200618 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800620 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400621 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Rafael J. Wysocki212967c2010-11-25 00:02:36 +0100623 if (device->power.state == state)
624 return 0;
625
Len Brown4be44fc2005-08-05 00:44:28 -0400626 if ((device->power.state < ACPI_STATE_D0)
Zhang Rui3ebc81b2012-03-29 14:09:38 +0800627 || (device->power.state > ACPI_STATE_D3_COLD))
Patrick Mocheld550d982006-06-27 00:41:40 -0400628 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /* TBD: Resources must be ordered. */
631
632 /*
633 * First we reference all power resources required in the target list
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100634 * (e.g. so the device doesn't lose power while transitioning). Then,
635 * we dereference all power resources used in the current list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 */
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200637 if (state < ACPI_STATE_D3_COLD)
638 result = acpi_power_on_list(
639 &device->power.states[state].resources);
640
641 if (!result && device->power.state < ACPI_STATE_D3_COLD)
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100642 acpi_power_off_list(
643 &device->power.states[device->power.state].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Rafael J. Wysockid2ef5552010-11-25 00:06:09 +0100645 /* We shouldn't change the state unless the above operations succeed. */
646 device->power.state = result ? ACPI_STATE_UNKNOWN : state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Patrick Mocheld550d982006-06-27 00:41:40 -0400648 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651/* --------------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 Driver Interface
653 -------------------------------------------------------------------------- */
654
Len Brown4be44fc2005-08-05 00:44:28 -0400655static int acpi_power_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400657 int result = 0, state;
Len Brown4be44fc2005-08-05 00:44:28 -0400658 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 struct acpi_power_resource *resource = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400660 union acpi_object acpi_object;
661 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400665 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Burman Yan36bcbec2006-12-19 12:56:11 -0800667 resource = kzalloc(sizeof(struct acpi_power_resource), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (!resource)
Patrick Mocheld550d982006-06-27 00:41:40 -0400669 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Patrick Mochel41598572006-05-19 16:54:40 -0400671 resource->device = device;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500672 mutex_init(&resource->resource_lock);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100673 INIT_LIST_HEAD(&resource->dependent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 strcpy(resource->name, device->pnp.bus_id);
675 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
676 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700677 device->driver_data = resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 /* Evalute the object to get the system level and resource order. */
Patrick Mochel5fbc19e2006-05-19 16:54:43 -0400680 status = acpi_evaluate_object(device->handle, NULL, NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (ACPI_FAILURE(status)) {
682 result = -ENODEV;
683 goto end;
684 }
685 resource->system_level = acpi_object.power_resource.system_level;
686 resource->order = acpi_object.power_resource.resource_order;
687
Zhao Yakuia51e1452008-08-11 14:55:05 +0800688 result = acpi_power_get_state(device->handle, &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (result)
690 goto end;
691
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400692 switch (state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 case ACPI_POWER_RESOURCE_STATE_ON:
694 device->power.state = ACPI_STATE_D0;
695 break;
696 case ACPI_POWER_RESOURCE_STATE_OFF:
697 device->power.state = ACPI_STATE_D3;
698 break;
699 default:
700 device->power.state = ACPI_STATE_UNKNOWN;
701 break;
702 }
703
Len Brown4be44fc2005-08-05 00:44:28 -0400704 printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400705 acpi_device_bid(device), state ? "on" : "off");
Len Brown4be44fc2005-08-05 00:44:28 -0400706
707 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (result)
709 kfree(resource);
Len Brown4be44fc2005-08-05 00:44:28 -0400710
Patrick Mocheld550d982006-06-27 00:41:40 -0400711 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
Len Brown4be44fc2005-08-05 00:44:28 -0400714static int acpi_power_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200716 struct acpi_power_resource *resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200718 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400719 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200721 resource = acpi_driver_data(device);
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200722 if (!resource)
723 return -EINVAL;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 kfree(resource);
726
Patrick Mocheld550d982006-06-27 00:41:40 -0400727 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200730#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockie579e2d2012-06-27 23:26:59 +0200731static int acpi_power_resume(struct device *dev)
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500732{
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400733 int result = 0, state;
Rafael J. Wysockie579e2d2012-06-27 23:26:59 +0200734 struct acpi_device *device;
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200735 struct acpi_power_resource *resource;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500736
Rafael J. Wysockie579e2d2012-06-27 23:26:59 +0200737 if (!dev)
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500738 return -EINVAL;
739
Rafael J. Wysockie579e2d2012-06-27 23:26:59 +0200740 device = to_acpi_device(dev);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700741 resource = acpi_driver_data(device);
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200742 if (!resource)
743 return -EINVAL;
744
745 mutex_lock(&resource->resource_lock);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500746
Zhao Yakuia51e1452008-08-11 14:55:05 +0800747 result = acpi_power_get_state(device->handle, &state);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500748 if (result)
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200749 goto unlock;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500750
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200751 if (state == ACPI_POWER_RESOURCE_STATE_OFF && resource->ref_count)
752 result = __acpi_power_on(resource);
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500753
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200754 unlock:
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500755 mutex_unlock(&resource->resource_lock);
Rafael J. Wysocki3e384ee2010-10-22 02:35:54 +0200756
757 return result;
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500758}
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200759#endif
Konstantin Karasyov0a613902007-02-16 01:47:06 -0500760
Bjorn Helgaas44515372009-03-24 16:49:53 -0600761int __init acpi_power_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 INIT_LIST_HEAD(&acpi_power_resource_list);
Zhang Rui06af7eb2010-07-15 10:46:38 +0800764 return acpi_bus_register_driver(&acpi_power_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}