Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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> | |
5a0e3ad6 | 42 | #include <linux/slab.h> |
0090def6 | 43 | #include <linux/pm_runtime.h> |
1da177e4 LT |
44 | #include <acpi/acpi_bus.h> |
45 | #include <acpi/acpi_drivers.h> | |
9b83ccd2 | 46 | #include "sleep.h" |
0090def6 | 47 | #include "internal.h" |
9b83ccd2 | 48 | |
a192a958 LB |
49 | #define PREFIX "ACPI: " |
50 | ||
89595b8f | 51 | #define _COMPONENT ACPI_POWER_COMPONENT |
f52fd66d | 52 | ACPI_MODULE_NAME("power"); |
1da177e4 | 53 | #define ACPI_POWER_CLASS "power_resource" |
1da177e4 LT |
54 | #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 | |
f5adfaa3 | 60 | |
82c7d5ef | 61 | static inline int acpi_power_add(struct acpi_device *device) { return 0; } |
1da177e4 | 62 | |
c97adf9e | 63 | static const struct acpi_device_id power_device_ids[] = { |
1ba90e3a TR |
64 | {ACPI_POWER_HID, 0}, |
65 | {"", 0}, | |
66 | }; | |
67 | MODULE_DEVICE_TABLE(acpi, power_device_ids); | |
68 | ||
90692404 | 69 | #ifdef CONFIG_PM_SLEEP |
e579e2dd | 70 | static int acpi_power_resume(struct device *dev); |
90692404 | 71 | #endif |
e579e2dd RW |
72 | static SIMPLE_DEV_PM_OPS(acpi_power_pm, NULL, acpi_power_resume); |
73 | ||
1da177e4 | 74 | static struct acpi_driver acpi_power_driver = { |
c2b6705b | 75 | .name = "power", |
4be44fcd | 76 | .class = ACPI_POWER_CLASS, |
1ba90e3a | 77 | .ids = power_device_ids, |
82c7d5ef | 78 | .ops.add = acpi_power_add, |
e579e2dd | 79 | .drv.pm = &acpi_power_pm, |
1da177e4 LT |
80 | }; |
81 | ||
bc9b6407 RW |
82 | struct acpi_power_dependent_device { |
83 | struct list_head node; | |
84 | struct acpi_device *adev; | |
85 | struct work_struct work; | |
0090def6 LM |
86 | }; |
87 | ||
4be44fcd | 88 | struct acpi_power_resource { |
82c7d5ef | 89 | struct acpi_device device; |
bc9b6407 | 90 | struct list_head dependent; |
82c7d5ef | 91 | char *name; |
4be44fcd LB |
92 | u32 system_level; |
93 | u32 order; | |
3e384ee6 | 94 | unsigned int ref_count; |
0a613902 | 95 | struct mutex resource_lock; |
1da177e4 LT |
96 | }; |
97 | ||
4be44fcd | 98 | static struct list_head acpi_power_resource_list; |
1da177e4 | 99 | |
1da177e4 LT |
100 | /* -------------------------------------------------------------------------- |
101 | Power Resource Management | |
102 | -------------------------------------------------------------------------- */ | |
103 | ||
82c7d5ef | 104 | static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle) |
1da177e4 | 105 | { |
82c7d5ef | 106 | struct acpi_device *device; |
1da177e4 | 107 | |
82c7d5ef RW |
108 | if (acpi_bus_get_device(handle, &device)) |
109 | return NULL; | |
1da177e4 | 110 | |
82c7d5ef | 111 | return container_of(device, struct acpi_power_resource, device); |
1da177e4 LT |
112 | } |
113 | ||
a51e145f | 114 | static int acpi_power_get_state(acpi_handle handle, int *state) |
1da177e4 | 115 | { |
4be44fcd | 116 | acpi_status status = AE_OK; |
27663c58 | 117 | unsigned long long sta = 0; |
60a4ce7f LM |
118 | char node_name[5]; |
119 | struct acpi_buffer buffer = { sizeof(node_name), node_name }; | |
1da177e4 | 120 | |
1da177e4 | 121 | |
a51e145f | 122 | if (!handle || !state) |
d550d98d | 123 | return -EINVAL; |
1da177e4 | 124 | |
a51e145f | 125 | status = acpi_evaluate_integer(handle, "_STA", NULL, &sta); |
1da177e4 | 126 | if (ACPI_FAILURE(status)) |
d550d98d | 127 | return -ENODEV; |
1da177e4 | 128 | |
c35923bc AS |
129 | *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON: |
130 | ACPI_POWER_RESOURCE_STATE_OFF; | |
1da177e4 | 131 | |
60a4ce7f LM |
132 | acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer); |
133 | ||
1da177e4 | 134 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n", |
60a4ce7f | 135 | node_name, |
b1b57fbe | 136 | *state ? "on" : "off")); |
1da177e4 | 137 | |
d550d98d | 138 | return 0; |
1da177e4 LT |
139 | } |
140 | ||
4be44fcd | 141 | static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state) |
1da177e4 | 142 | { |
d0515d9f RW |
143 | int cur_state; |
144 | int i = 0; | |
1da177e4 LT |
145 | |
146 | if (!list || !state) | |
d550d98d | 147 | return -EINVAL; |
1da177e4 LT |
148 | |
149 | /* The state of the list is 'on' IFF all resources are 'on'. */ | |
150 | ||
4be44fcd | 151 | for (i = 0; i < list->count; i++) { |
d0515d9f RW |
152 | struct acpi_power_resource *resource; |
153 | acpi_handle handle = list->handles[i]; | |
154 | int result; | |
155 | ||
82c7d5ef RW |
156 | resource = acpi_power_get_context(handle); |
157 | if (!resource) | |
158 | return -ENODEV; | |
1da177e4 | 159 | |
d0515d9f RW |
160 | mutex_lock(&resource->resource_lock); |
161 | ||
162 | result = acpi_power_get_state(handle, &cur_state); | |
1da177e4 | 163 | |
d0515d9f RW |
164 | mutex_unlock(&resource->resource_lock); |
165 | ||
166 | if (result) | |
167 | return result; | |
168 | ||
169 | if (cur_state != ACPI_POWER_RESOURCE_STATE_ON) | |
1da177e4 LT |
170 | break; |
171 | } | |
172 | ||
173 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n", | |
d0515d9f | 174 | cur_state ? "on" : "off")); |
1da177e4 | 175 | |
d0515d9f RW |
176 | *state = cur_state; |
177 | ||
178 | return 0; | |
1da177e4 LT |
179 | } |
180 | ||
bc9b6407 | 181 | static void acpi_power_resume_dependent(struct work_struct *work) |
0090def6 | 182 | { |
bc9b6407 RW |
183 | struct acpi_power_dependent_device *dep; |
184 | struct acpi_device_physical_node *pn; | |
185 | struct acpi_device *adev; | |
0090def6 LM |
186 | int state; |
187 | ||
bc9b6407 RW |
188 | dep = container_of(work, struct acpi_power_dependent_device, work); |
189 | adev = dep->adev; | |
190 | if (acpi_power_get_inferred_state(adev, &state)) | |
0090def6 LM |
191 | return; |
192 | ||
bc9b6407 | 193 | if (state > ACPI_STATE_D0) |
0090def6 LM |
194 | return; |
195 | ||
bc9b6407 RW |
196 | mutex_lock(&adev->physical_node_lock); |
197 | ||
198 | list_for_each_entry(pn, &adev->physical_node_list, node) | |
199 | pm_request_resume(pn->dev); | |
200 | ||
201 | list_for_each_entry(pn, &adev->power_dependent, node) | |
202 | pm_request_resume(pn->dev); | |
203 | ||
204 | mutex_unlock(&adev->physical_node_lock); | |
0090def6 LM |
205 | } |
206 | ||
3e384ee6 | 207 | static int __acpi_power_on(struct acpi_power_resource *resource) |
1da177e4 | 208 | { |
4be44fcd | 209 | acpi_status status = AE_OK; |
1da177e4 | 210 | |
82c7d5ef | 211 | status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL); |
3e384ee6 RW |
212 | if (ACPI_FAILURE(status)) |
213 | return -ENODEV; | |
214 | ||
215 | /* Update the power resource's _device_ power state */ | |
82c7d5ef | 216 | resource->device.power.state = ACPI_STATE_D0; |
3e384ee6 RW |
217 | |
218 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n", | |
219 | resource->name)); | |
220 | ||
221 | return 0; | |
222 | } | |
223 | ||
224 | static int acpi_power_on(acpi_handle handle) | |
225 | { | |
226 | int result = 0; | |
82c7d5ef | 227 | struct acpi_power_resource *resource; |
1da177e4 | 228 | |
82c7d5ef RW |
229 | resource = acpi_power_get_context(handle); |
230 | if (!resource) | |
231 | return -ENODEV; | |
1da177e4 | 232 | |
0a613902 | 233 | mutex_lock(&resource->resource_lock); |
0a613902 | 234 | |
3e384ee6 RW |
235 | if (resource->ref_count++) { |
236 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
237 | "Power resource [%s] already on", | |
238 | resource->name)); | |
239 | } else { | |
240 | result = __acpi_power_on(resource); | |
bc9b6407 | 241 | if (result) { |
12b3b5af | 242 | resource->ref_count--; |
bc9b6407 RW |
243 | } else { |
244 | struct acpi_power_dependent_device *dep; | |
40bf66ec | 245 | |
bc9b6407 RW |
246 | list_for_each_entry(dep, &resource->dependent, node) |
247 | schedule_work(&dep->work); | |
248 | } | |
40bf66ec LM |
249 | } |
250 | ||
bc9b6407 | 251 | mutex_unlock(&resource->resource_lock); |
40bf66ec | 252 | |
12b3b5af | 253 | return result; |
1da177e4 LT |
254 | } |
255 | ||
36237fa0 | 256 | static int acpi_power_off(acpi_handle handle) |
1da177e4 | 257 | { |
bdf43bbf | 258 | int result = 0; |
4be44fcd | 259 | acpi_status status = AE_OK; |
82c7d5ef | 260 | struct acpi_power_resource *resource; |
0a613902 | 261 | |
82c7d5ef RW |
262 | resource = acpi_power_get_context(handle); |
263 | if (!resource) | |
264 | return -ENODEV; | |
1da177e4 | 265 | |
0a613902 | 266 | mutex_lock(&resource->resource_lock); |
3e384ee6 RW |
267 | |
268 | if (!resource->ref_count) { | |
269 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
270 | "Power resource [%s] already off", | |
271 | resource->name)); | |
272 | goto unlock; | |
0a613902 | 273 | } |
1da177e4 | 274 | |
3e384ee6 RW |
275 | if (--resource->ref_count) { |
276 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
277 | "Power resource [%s] still in use\n", | |
278 | resource->name)); | |
279 | goto unlock; | |
1da177e4 LT |
280 | } |
281 | ||
82c7d5ef | 282 | status = acpi_evaluate_object(resource->device.handle, "_OFF", NULL, NULL); |
3e384ee6 RW |
283 | if (ACPI_FAILURE(status)) { |
284 | result = -ENODEV; | |
285 | } else { | |
286 | /* Update the power resource's _device_ power state */ | |
82c7d5ef | 287 | resource->device.power.state = ACPI_STATE_D3; |
1da177e4 | 288 | |
3e384ee6 RW |
289 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
290 | "Power resource [%s] turned off\n", | |
291 | resource->name)); | |
292 | } | |
1da177e4 | 293 | |
3e384ee6 RW |
294 | unlock: |
295 | mutex_unlock(&resource->resource_lock); | |
1da177e4 | 296 | |
3e384ee6 | 297 | return result; |
1da177e4 LT |
298 | } |
299 | ||
d2ef555b RW |
300 | static void __acpi_power_off_list(struct acpi_handle_list *list, int num_res) |
301 | { | |
302 | int i; | |
303 | ||
304 | for (i = num_res - 1; i >= 0 ; i--) | |
36237fa0 | 305 | acpi_power_off(list->handles[i]); |
d2ef555b RW |
306 | } |
307 | ||
308 | static void acpi_power_off_list(struct acpi_handle_list *list) | |
309 | { | |
310 | __acpi_power_off_list(list, list->count); | |
311 | } | |
312 | ||
313 | static int acpi_power_on_list(struct acpi_handle_list *list) | |
314 | { | |
315 | int result = 0; | |
316 | int i; | |
317 | ||
318 | for (i = 0; i < list->count; i++) { | |
319 | result = acpi_power_on(list->handles[i]); | |
320 | if (result) { | |
321 | __acpi_power_off_list(list, i); | |
322 | break; | |
323 | } | |
324 | } | |
325 | ||
326 | return result; | |
327 | } | |
328 | ||
bc9b6407 RW |
329 | static void acpi_power_add_dependent(acpi_handle rhandle, |
330 | struct acpi_device *adev) | |
0090def6 | 331 | { |
bc9b6407 RW |
332 | struct acpi_power_dependent_device *dep; |
333 | struct acpi_power_resource *resource; | |
0090def6 | 334 | |
82c7d5ef RW |
335 | if (!rhandle || !adev) |
336 | return; | |
337 | ||
338 | resource = acpi_power_get_context(rhandle); | |
339 | if (!resource) | |
0090def6 LM |
340 | return; |
341 | ||
bc9b6407 | 342 | mutex_lock(&resource->resource_lock); |
0090def6 | 343 | |
bc9b6407 RW |
344 | list_for_each_entry(dep, &resource->dependent, node) |
345 | if (dep->adev == adev) | |
346 | goto out; | |
0090def6 | 347 | |
bc9b6407 RW |
348 | dep = kzalloc(sizeof(*dep), GFP_KERNEL); |
349 | if (!dep) | |
350 | goto out; | |
0090def6 | 351 | |
bc9b6407 RW |
352 | dep->adev = adev; |
353 | INIT_WORK(&dep->work, acpi_power_resume_dependent); | |
354 | list_add_tail(&dep->node, &resource->dependent); | |
0090def6 | 355 | |
bc9b6407 RW |
356 | out: |
357 | mutex_unlock(&resource->resource_lock); | |
0090def6 LM |
358 | } |
359 | ||
bc9b6407 RW |
360 | static void acpi_power_remove_dependent(acpi_handle rhandle, |
361 | struct acpi_device *adev) | |
0090def6 | 362 | { |
bc9b6407 RW |
363 | struct acpi_power_dependent_device *dep; |
364 | struct acpi_power_resource *resource; | |
365 | struct work_struct *work = NULL; | |
0090def6 | 366 | |
82c7d5ef RW |
367 | if (!rhandle || !adev) |
368 | return; | |
369 | ||
370 | resource = acpi_power_get_context(rhandle); | |
371 | if (!resource) | |
bc9b6407 | 372 | return; |
0090def6 | 373 | |
bc9b6407 | 374 | mutex_lock(&resource->resource_lock); |
0090def6 | 375 | |
bc9b6407 RW |
376 | list_for_each_entry(dep, &resource->dependent, node) |
377 | if (dep->adev == adev) { | |
378 | list_del(&dep->node); | |
379 | work = &dep->work; | |
380 | break; | |
381 | } | |
0090def6 | 382 | |
bc9b6407 | 383 | mutex_unlock(&resource->resource_lock); |
0090def6 | 384 | |
bc9b6407 RW |
385 | if (work) { |
386 | cancel_work_sync(work); | |
387 | kfree(dep); | |
388 | } | |
0090def6 LM |
389 | } |
390 | ||
bc9b6407 | 391 | void acpi_power_add_remove_device(struct acpi_device *adev, bool add) |
0090def6 | 392 | { |
bc9b6407 RW |
393 | if (adev->power.flags.power_resources) { |
394 | struct acpi_device_power_state *ps; | |
395 | int j; | |
0090def6 | 396 | |
bc9b6407 RW |
397 | ps = &adev->power.states[ACPI_STATE_D0]; |
398 | for (j = 0; j < ps->resources.count; j++) { | |
399 | acpi_handle rhandle = ps->resources.handles[j]; | |
0090def6 | 400 | |
bc9b6407 RW |
401 | if (add) |
402 | acpi_power_add_dependent(rhandle, adev); | |
403 | else | |
404 | acpi_power_remove_dependent(rhandle, adev); | |
0090def6 LM |
405 | } |
406 | } | |
0090def6 | 407 | } |
bc9b6407 RW |
408 | |
409 | /* -------------------------------------------------------------------------- | |
410 | Device Power Management | |
411 | -------------------------------------------------------------------------- */ | |
0090def6 | 412 | |
77e76609 RW |
413 | /** |
414 | * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in | |
415 | * ACPI 3.0) _PSW (Power State Wake) | |
416 | * @dev: Device to handle. | |
417 | * @enable: 0 - disable, 1 - enable the wake capabilities of the device. | |
418 | * @sleep_state: Target sleep state of the system. | |
419 | * @dev_state: Target power state of the device. | |
420 | * | |
421 | * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power | |
422 | * State Wake) for the device, if present. On failure reset the device's | |
423 | * wakeup.flags.valid flag. | |
424 | * | |
425 | * RETURN VALUE: | |
426 | * 0 if either _DSW or _PSW has been successfully executed | |
427 | * 0 if neither _DSW nor _PSW has been found | |
428 | * -ENODEV if the execution of either _DSW or _PSW has failed | |
429 | */ | |
430 | int acpi_device_sleep_wake(struct acpi_device *dev, | |
431 | int enable, int sleep_state, int dev_state) | |
432 | { | |
433 | union acpi_object in_arg[3]; | |
434 | struct acpi_object_list arg_list = { 3, in_arg }; | |
435 | acpi_status status = AE_OK; | |
436 | ||
437 | /* | |
438 | * Try to execute _DSW first. | |
439 | * | |
440 | * Three agruments are needed for the _DSW object: | |
441 | * Argument 0: enable/disable the wake capabilities | |
442 | * Argument 1: target system state | |
443 | * Argument 2: target device state | |
444 | * When _DSW object is called to disable the wake capabilities, maybe | |
445 | * the first argument is filled. The values of the other two agruments | |
446 | * are meaningless. | |
447 | */ | |
448 | in_arg[0].type = ACPI_TYPE_INTEGER; | |
449 | in_arg[0].integer.value = enable; | |
450 | in_arg[1].type = ACPI_TYPE_INTEGER; | |
451 | in_arg[1].integer.value = sleep_state; | |
452 | in_arg[2].type = ACPI_TYPE_INTEGER; | |
453 | in_arg[2].integer.value = dev_state; | |
454 | status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL); | |
455 | if (ACPI_SUCCESS(status)) { | |
456 | return 0; | |
457 | } else if (status != AE_NOT_FOUND) { | |
458 | printk(KERN_ERR PREFIX "_DSW execution failed\n"); | |
459 | dev->wakeup.flags.valid = 0; | |
460 | return -ENODEV; | |
461 | } | |
462 | ||
463 | /* Execute _PSW */ | |
464 | arg_list.count = 1; | |
465 | in_arg[0].integer.value = enable; | |
466 | status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL); | |
467 | if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) { | |
468 | printk(KERN_ERR PREFIX "_PSW execution failed\n"); | |
469 | dev->wakeup.flags.valid = 0; | |
470 | return -ENODEV; | |
471 | } | |
472 | ||
473 | return 0; | |
474 | } | |
475 | ||
1da177e4 LT |
476 | /* |
477 | * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229): | |
478 | * 1. Power on the power resources required for the wakeup device | |
77e76609 RW |
479 | * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power |
480 | * State Wake) for the device, if present | |
1da177e4 | 481 | */ |
77e76609 | 482 | int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state) |
1da177e4 | 483 | { |
9b83ccd2 | 484 | int i, err = 0; |
1da177e4 | 485 | |
1da177e4 | 486 | if (!dev || !dev->wakeup.flags.valid) |
77e76609 | 487 | return -EINVAL; |
1da177e4 | 488 | |
9b83ccd2 RW |
489 | mutex_lock(&acpi_device_lock); |
490 | ||
491 | if (dev->wakeup.prepare_count++) | |
492 | goto out; | |
0af4b8c4 | 493 | |
1da177e4 LT |
494 | /* Open power resource */ |
495 | for (i = 0; i < dev->wakeup.resources.count; i++) { | |
3e384ee6 | 496 | int ret = acpi_power_on(dev->wakeup.resources.handles[i]); |
1da177e4 | 497 | if (ret) { |
6468463a | 498 | printk(KERN_ERR PREFIX "Transition power state\n"); |
1da177e4 | 499 | dev->wakeup.flags.valid = 0; |
9b83ccd2 RW |
500 | err = -ENODEV; |
501 | goto err_out; | |
1da177e4 LT |
502 | } |
503 | } | |
504 | ||
77e76609 RW |
505 | /* |
506 | * Passing 3 as the third argument below means the device may be placed | |
507 | * in arbitrary power state afterwards. | |
508 | */ | |
0af4b8c4 | 509 | err = acpi_device_sleep_wake(dev, 1, sleep_state, 3); |
0af4b8c4 | 510 | |
9b83ccd2 RW |
511 | err_out: |
512 | if (err) | |
513 | dev->wakeup.prepare_count = 0; | |
514 | ||
515 | out: | |
516 | mutex_unlock(&acpi_device_lock); | |
0af4b8c4 | 517 | return err; |
1da177e4 LT |
518 | } |
519 | ||
520 | /* | |
521 | * Shutdown a wakeup device, counterpart of above method | |
77e76609 RW |
522 | * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power |
523 | * State Wake) for the device, if present | |
1da177e4 LT |
524 | * 2. Shutdown down the power resources |
525 | */ | |
4be44fcd | 526 | int acpi_disable_wakeup_device_power(struct acpi_device *dev) |
1da177e4 | 527 | { |
9b83ccd2 | 528 | int i, err = 0; |
1da177e4 LT |
529 | |
530 | if (!dev || !dev->wakeup.flags.valid) | |
77e76609 | 531 | return -EINVAL; |
1da177e4 | 532 | |
9b83ccd2 RW |
533 | mutex_lock(&acpi_device_lock); |
534 | ||
535 | if (--dev->wakeup.prepare_count > 0) | |
536 | goto out; | |
537 | ||
0af4b8c4 | 538 | /* |
9b83ccd2 RW |
539 | * Executing the code below even if prepare_count is already zero when |
540 | * the function is called may be useful, for example for initialisation. | |
0af4b8c4 | 541 | */ |
9b83ccd2 RW |
542 | if (dev->wakeup.prepare_count < 0) |
543 | dev->wakeup.prepare_count = 0; | |
0af4b8c4 | 544 | |
9b83ccd2 RW |
545 | err = acpi_device_sleep_wake(dev, 0, 0, 0); |
546 | if (err) | |
547 | goto out; | |
1da177e4 LT |
548 | |
549 | /* Close power resource */ | |
550 | for (i = 0; i < dev->wakeup.resources.count; i++) { | |
36237fa0 | 551 | int ret = acpi_power_off(dev->wakeup.resources.handles[i]); |
1da177e4 | 552 | if (ret) { |
6468463a | 553 | printk(KERN_ERR PREFIX "Transition power state\n"); |
1da177e4 | 554 | dev->wakeup.flags.valid = 0; |
9b83ccd2 RW |
555 | err = -ENODEV; |
556 | goto out; | |
1da177e4 LT |
557 | } |
558 | } | |
559 | ||
9b83ccd2 RW |
560 | out: |
561 | mutex_unlock(&acpi_device_lock); | |
562 | return err; | |
1da177e4 LT |
563 | } |
564 | ||
32a00d27 | 565 | int acpi_power_get_inferred_state(struct acpi_device *device, int *state) |
1da177e4 | 566 | { |
4be44fcd LB |
567 | int result = 0; |
568 | struct acpi_handle_list *list = NULL; | |
569 | int list_state = 0; | |
570 | int i = 0; | |
1da177e4 | 571 | |
32a00d27 | 572 | if (!device || !state) |
d550d98d | 573 | return -EINVAL; |
1da177e4 | 574 | |
1da177e4 LT |
575 | /* |
576 | * We know a device's inferred power state when all the resources | |
577 | * required for a given D-state are 'on'. | |
578 | */ | |
38c92fff | 579 | for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) { |
1da177e4 LT |
580 | list = &device->power.states[i].resources; |
581 | if (list->count < 1) | |
582 | continue; | |
583 | ||
584 | result = acpi_power_get_list_state(list, &list_state); | |
585 | if (result) | |
d550d98d | 586 | return result; |
1da177e4 LT |
587 | |
588 | if (list_state == ACPI_POWER_RESOURCE_STATE_ON) { | |
32a00d27 | 589 | *state = i; |
d550d98d | 590 | return 0; |
1da177e4 LT |
591 | } |
592 | } | |
593 | ||
32a00d27 | 594 | *state = ACPI_STATE_D3; |
d550d98d | 595 | return 0; |
1da177e4 LT |
596 | } |
597 | ||
30d3df41 RW |
598 | int acpi_power_on_resources(struct acpi_device *device, int state) |
599 | { | |
600 | if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3) | |
601 | return -EINVAL; | |
602 | ||
603 | return acpi_power_on_list(&device->power.states[state].resources); | |
604 | } | |
605 | ||
4be44fcd | 606 | int acpi_power_transition(struct acpi_device *device, int state) |
1da177e4 | 607 | { |
5c7dd710 | 608 | int result = 0; |
1da177e4 | 609 | |
3ebc81b8 | 610 | if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD)) |
d550d98d | 611 | return -EINVAL; |
1da177e4 | 612 | |
212967c6 RW |
613 | if (device->power.state == state) |
614 | return 0; | |
615 | ||
4be44fcd | 616 | if ((device->power.state < ACPI_STATE_D0) |
3ebc81b8 | 617 | || (device->power.state > ACPI_STATE_D3_COLD)) |
d550d98d | 618 | return -ENODEV; |
1da177e4 | 619 | |
1da177e4 LT |
620 | /* TBD: Resources must be ordered. */ |
621 | ||
622 | /* | |
623 | * First we reference all power resources required in the target list | |
d2ef555b RW |
624 | * (e.g. so the device doesn't lose power while transitioning). Then, |
625 | * we dereference all power resources used in the current list. | |
1da177e4 | 626 | */ |
5c7dd710 RW |
627 | if (state < ACPI_STATE_D3_COLD) |
628 | result = acpi_power_on_list( | |
629 | &device->power.states[state].resources); | |
630 | ||
631 | if (!result && device->power.state < ACPI_STATE_D3_COLD) | |
d2ef555b RW |
632 | acpi_power_off_list( |
633 | &device->power.states[device->power.state].resources); | |
1da177e4 | 634 | |
d2ef555b RW |
635 | /* We shouldn't change the state unless the above operations succeed. */ |
636 | device->power.state = result ? ACPI_STATE_UNKNOWN : state; | |
1da177e4 | 637 | |
d550d98d | 638 | return result; |
1da177e4 LT |
639 | } |
640 | ||
82c7d5ef RW |
641 | static void acpi_release_power_resource(struct device *dev) |
642 | { | |
643 | struct acpi_device *device = to_acpi_device(dev); | |
644 | struct acpi_power_resource *resource; | |
645 | ||
646 | acpi_free_ids(device); | |
647 | resource = container_of(device, struct acpi_power_resource, device); | |
648 | kfree(resource); | |
649 | } | |
1da177e4 | 650 | |
82c7d5ef | 651 | void acpi_add_power_resource(acpi_handle handle) |
1da177e4 | 652 | { |
82c7d5ef RW |
653 | struct acpi_power_resource *resource; |
654 | struct acpi_device *device = NULL; | |
4be44fcd LB |
655 | union acpi_object acpi_object; |
656 | struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object }; | |
82c7d5ef RW |
657 | acpi_status status; |
658 | int state, result = -ENODEV; | |
1da177e4 | 659 | |
82c7d5ef RW |
660 | acpi_bus_get_device(handle, &device); |
661 | if (device) | |
662 | return; | |
1da177e4 | 663 | |
82c7d5ef | 664 | resource = kzalloc(sizeof(*resource), GFP_KERNEL); |
1da177e4 | 665 | if (!resource) |
82c7d5ef | 666 | return; |
1da177e4 | 667 | |
82c7d5ef RW |
668 | device = &resource->device; |
669 | acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER, | |
670 | ACPI_STA_DEFAULT); | |
0a613902 | 671 | mutex_init(&resource->resource_lock); |
bc9b6407 | 672 | INIT_LIST_HEAD(&resource->dependent); |
82c7d5ef | 673 | resource->name = device->pnp.bus_id; |
1da177e4 LT |
674 | strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME); |
675 | strcpy(acpi_device_class(device), ACPI_POWER_CLASS); | |
1da177e4 LT |
676 | |
677 | /* Evalute the object to get the system level and resource order. */ | |
82c7d5ef RW |
678 | status = acpi_evaluate_object(handle, NULL, NULL, &buffer); |
679 | if (ACPI_FAILURE(status)) | |
680 | goto out; | |
681 | ||
1da177e4 LT |
682 | resource->system_level = acpi_object.power_resource.system_level; |
683 | resource->order = acpi_object.power_resource.resource_order; | |
684 | ||
82c7d5ef | 685 | result = acpi_power_get_state(handle, &state); |
1da177e4 | 686 | if (result) |
82c7d5ef | 687 | goto out; |
1da177e4 | 688 | |
c35923bc | 689 | switch (state) { |
1da177e4 LT |
690 | case ACPI_POWER_RESOURCE_STATE_ON: |
691 | device->power.state = ACPI_STATE_D0; | |
692 | break; | |
693 | case ACPI_POWER_RESOURCE_STATE_OFF: | |
694 | device->power.state = ACPI_STATE_D3; | |
695 | break; | |
696 | default: | |
697 | device->power.state = ACPI_STATE_UNKNOWN; | |
1da177e4 LT |
698 | } |
699 | ||
1da177e4 | 700 | printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device), |
c35923bc | 701 | acpi_device_bid(device), state ? "on" : "off"); |
1da177e4 | 702 | |
82c7d5ef RW |
703 | device->flags.match_driver = true; |
704 | result = acpi_device_register(device, acpi_release_power_resource); | |
705 | ||
706 | out: | |
1da177e4 | 707 | if (result) |
82c7d5ef | 708 | acpi_release_power_resource(&device->dev); |
4be44fcd | 709 | |
82c7d5ef | 710 | return; |
1da177e4 LT |
711 | } |
712 | ||
82c7d5ef RW |
713 | /* -------------------------------------------------------------------------- |
714 | Driver Interface | |
715 | -------------------------------------------------------------------------- */ | |
1da177e4 | 716 | |
90692404 | 717 | #ifdef CONFIG_PM_SLEEP |
e579e2dd | 718 | static int acpi_power_resume(struct device *dev) |
0a613902 | 719 | { |
c35923bc | 720 | int result = 0, state; |
e579e2dd | 721 | struct acpi_device *device; |
3e384ee6 | 722 | struct acpi_power_resource *resource; |
0a613902 | 723 | |
e579e2dd | 724 | if (!dev) |
0a613902 KK |
725 | return -EINVAL; |
726 | ||
e579e2dd | 727 | device = to_acpi_device(dev); |
db89b4f0 | 728 | resource = acpi_driver_data(device); |
3e384ee6 RW |
729 | if (!resource) |
730 | return -EINVAL; | |
731 | ||
732 | mutex_lock(&resource->resource_lock); | |
0a613902 | 733 | |
a51e145f | 734 | result = acpi_power_get_state(device->handle, &state); |
0a613902 | 735 | if (result) |
3e384ee6 | 736 | goto unlock; |
0a613902 | 737 | |
3e384ee6 RW |
738 | if (state == ACPI_POWER_RESOURCE_STATE_OFF && resource->ref_count) |
739 | result = __acpi_power_on(resource); | |
0a613902 | 740 | |
3e384ee6 | 741 | unlock: |
0a613902 | 742 | mutex_unlock(&resource->resource_lock); |
3e384ee6 RW |
743 | |
744 | return result; | |
0a613902 | 745 | } |
90692404 | 746 | #endif |
0a613902 | 747 | |
44515374 | 748 | int __init acpi_power_init(void) |
1da177e4 | 749 | { |
1da177e4 | 750 | INIT_LIST_HEAD(&acpi_power_resource_list); |
06af7eb0 | 751 | return acpi_bus_register_driver(&acpi_power_driver); |
1da177e4 | 752 | } |