1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * drivers/acpi/power.c - ACPI Power Resources management.
5 * Copyright (C) 2001 - 2015 Intel Corp.
6 * Author: Andy Grover <andrew.grover@intel.com>
7 * Author: Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
8 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
12 * ACPI power-managed devices may be controlled in two ways:
13 * 1. via "Device Specific (D-State) Control"
14 * 2. via "Power Resource Control".
15 * The code below deals with ACPI Power Resources control.
17 * An ACPI "power resource object" represents a software controllable power
18 * plane, clock plane, or other resource depended on by a device.
20 * A device may rely on multiple power resources, and a power resource
21 * may be shared by multiple devices.
24 #define pr_fmt(fmt) "ACPI: PM: " fmt
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/slab.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/sysfs.h>
33 #include <linux/acpi.h>
37 #define ACPI_POWER_CLASS "power_resource"
38 #define ACPI_POWER_DEVICE_NAME "Power Resource"
39 #define ACPI_POWER_RESOURCE_STATE_OFF 0x00
40 #define ACPI_POWER_RESOURCE_STATE_ON 0x01
41 #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
43 struct acpi_power_dependent_device
{
45 struct list_head node
;
48 struct acpi_power_resource
{
49 struct acpi_device device
;
50 struct list_head list_node
;
54 unsigned int ref_count
;
56 struct mutex resource_lock
;
57 struct list_head dependents
;
60 struct acpi_power_resource_entry
{
61 struct list_head node
;
62 struct acpi_power_resource
*resource
;
65 static LIST_HEAD(acpi_power_resource_list
);
66 static DEFINE_MUTEX(power_resource_list_lock
);
68 /* --------------------------------------------------------------------------
69 Power Resource Management
70 -------------------------------------------------------------------------- */
73 struct acpi_power_resource
*to_power_resource(struct acpi_device
*device
)
75 return container_of(device
, struct acpi_power_resource
, device
);
78 static struct acpi_power_resource
*acpi_power_get_context(acpi_handle handle
)
80 struct acpi_device
*device
;
82 if (acpi_bus_get_device(handle
, &device
))
85 return to_power_resource(device
);
88 static int acpi_power_resources_list_add(acpi_handle handle
,
89 struct list_head
*list
)
91 struct acpi_power_resource
*resource
= acpi_power_get_context(handle
);
92 struct acpi_power_resource_entry
*entry
;
94 if (!resource
|| !list
)
97 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
101 entry
->resource
= resource
;
102 if (!list_empty(list
)) {
103 struct acpi_power_resource_entry
*e
;
105 list_for_each_entry(e
, list
, node
)
106 if (e
->resource
->order
> resource
->order
) {
107 list_add_tail(&entry
->node
, &e
->node
);
111 list_add_tail(&entry
->node
, list
);
115 void acpi_power_resources_list_free(struct list_head
*list
)
117 struct acpi_power_resource_entry
*entry
, *e
;
119 list_for_each_entry_safe(entry
, e
, list
, node
) {
120 list_del(&entry
->node
);
125 static bool acpi_power_resource_is_dup(union acpi_object
*package
,
126 unsigned int start
, unsigned int i
)
128 acpi_handle rhandle
, dup
;
131 /* The caller is expected to check the package element types */
132 rhandle
= package
->package
.elements
[i
].reference
.handle
;
133 for (j
= start
; j
< i
; j
++) {
134 dup
= package
->package
.elements
[j
].reference
.handle
;
142 int acpi_extract_power_resources(union acpi_object
*package
, unsigned int start
,
143 struct list_head
*list
)
148 for (i
= start
; i
< package
->package
.count
; i
++) {
149 union acpi_object
*element
= &package
->package
.elements
[i
];
152 if (element
->type
!= ACPI_TYPE_LOCAL_REFERENCE
) {
156 rhandle
= element
->reference
.handle
;
162 /* Some ACPI tables contain duplicate power resource references */
163 if (acpi_power_resource_is_dup(package
, start
, i
))
166 err
= acpi_add_power_resource(rhandle
);
170 err
= acpi_power_resources_list_add(rhandle
, list
);
175 acpi_power_resources_list_free(list
);
180 static int acpi_power_get_state(acpi_handle handle
, int *state
)
182 acpi_status status
= AE_OK
;
183 unsigned long long sta
= 0;
185 if (!handle
|| !state
)
188 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, &sta
);
189 if (ACPI_FAILURE(status
))
192 *state
= (sta
& 0x01)?ACPI_POWER_RESOURCE_STATE_ON
:
193 ACPI_POWER_RESOURCE_STATE_OFF
;
195 acpi_handle_debug(handle
, "Power resource is %s\n",
196 *state
? "on" : "off");
201 static int acpi_power_get_list_state(struct list_head
*list
, int *state
)
203 struct acpi_power_resource_entry
*entry
;
209 /* The state of the list is 'on' IFF all resources are 'on'. */
211 list_for_each_entry(entry
, list
, node
) {
212 struct acpi_power_resource
*resource
= entry
->resource
;
213 acpi_handle handle
= resource
->device
.handle
;
216 mutex_lock(&resource
->resource_lock
);
217 result
= acpi_power_get_state(handle
, &cur_state
);
218 mutex_unlock(&resource
->resource_lock
);
222 if (cur_state
!= ACPI_POWER_RESOURCE_STATE_ON
)
226 pr_debug("Power resource list is %s\n", cur_state
? "on" : "off");
233 acpi_power_resource_add_dependent(struct acpi_power_resource
*resource
,
236 struct acpi_power_dependent_device
*dep
;
239 mutex_lock(&resource
->resource_lock
);
240 list_for_each_entry(dep
, &resource
->dependents
, node
) {
241 /* Only add it once */
246 dep
= kzalloc(sizeof(*dep
), GFP_KERNEL
);
253 list_add_tail(&dep
->node
, &resource
->dependents
);
254 dev_dbg(dev
, "added power dependency to [%s]\n", resource
->name
);
257 mutex_unlock(&resource
->resource_lock
);
262 acpi_power_resource_remove_dependent(struct acpi_power_resource
*resource
,
265 struct acpi_power_dependent_device
*dep
;
267 mutex_lock(&resource
->resource_lock
);
268 list_for_each_entry(dep
, &resource
->dependents
, node
) {
269 if (dep
->dev
== dev
) {
270 list_del(&dep
->node
);
272 dev_dbg(dev
, "removed power dependency to [%s]\n",
277 mutex_unlock(&resource
->resource_lock
);
281 * acpi_device_power_add_dependent - Add dependent device of this ACPI device
282 * @adev: ACPI device pointer
283 * @dev: Dependent device
285 * If @adev has non-empty _PR0 the @dev is added as dependent device to all
286 * power resources returned by it. This means that whenever these power
287 * resources are turned _ON the dependent devices get runtime resumed. This
288 * is needed for devices such as PCI to allow its driver to re-initialize
289 * it after it went to D0uninitialized.
291 * If @adev does not have _PR0 this does nothing.
293 * Returns %0 in case of success and negative errno otherwise.
295 int acpi_device_power_add_dependent(struct acpi_device
*adev
,
298 struct acpi_power_resource_entry
*entry
;
299 struct list_head
*resources
;
302 if (!adev
->flags
.power_manageable
)
305 resources
= &adev
->power
.states
[ACPI_STATE_D0
].resources
;
306 list_for_each_entry(entry
, resources
, node
) {
307 ret
= acpi_power_resource_add_dependent(entry
->resource
, dev
);
315 list_for_each_entry(entry
, resources
, node
)
316 acpi_power_resource_remove_dependent(entry
->resource
, dev
);
322 * acpi_device_power_remove_dependent - Remove dependent device
323 * @adev: ACPI device pointer
324 * @dev: Dependent device
326 * Does the opposite of acpi_device_power_add_dependent() and removes the
327 * dependent device if it is found. Can be called to @adev that does not
330 void acpi_device_power_remove_dependent(struct acpi_device
*adev
,
333 struct acpi_power_resource_entry
*entry
;
334 struct list_head
*resources
;
336 if (!adev
->flags
.power_manageable
)
339 resources
= &adev
->power
.states
[ACPI_STATE_D0
].resources
;
340 list_for_each_entry_reverse(entry
, resources
, node
)
341 acpi_power_resource_remove_dependent(entry
->resource
, dev
);
344 static int __acpi_power_on(struct acpi_power_resource
*resource
)
346 struct acpi_power_dependent_device
*dep
;
347 acpi_status status
= AE_OK
;
349 status
= acpi_evaluate_object(resource
->device
.handle
, "_ON", NULL
, NULL
);
350 if (ACPI_FAILURE(status
))
353 pr_debug("Power resource [%s] turned on\n", resource
->name
);
356 * If there are other dependents on this power resource we need to
357 * resume them now so that their drivers can re-initialize the
358 * hardware properly after it went back to D0.
360 if (list_empty(&resource
->dependents
) ||
361 list_is_singular(&resource
->dependents
))
364 list_for_each_entry(dep
, &resource
->dependents
, node
) {
365 dev_dbg(dep
->dev
, "runtime resuming because [%s] turned on\n",
367 pm_request_resume(dep
->dev
);
373 static int acpi_power_on_unlocked(struct acpi_power_resource
*resource
)
377 if (resource
->ref_count
++) {
378 pr_debug("Power resource [%s] already on\n", resource
->name
);
380 result
= __acpi_power_on(resource
);
382 resource
->ref_count
--;
387 static int acpi_power_on(struct acpi_power_resource
*resource
)
391 mutex_lock(&resource
->resource_lock
);
392 result
= acpi_power_on_unlocked(resource
);
393 mutex_unlock(&resource
->resource_lock
);
397 static int __acpi_power_off(struct acpi_power_resource
*resource
)
401 status
= acpi_evaluate_object(resource
->device
.handle
, "_OFF",
403 if (ACPI_FAILURE(status
))
406 pr_debug("Power resource [%s] turned off\n", resource
->name
);
411 static int acpi_power_off_unlocked(struct acpi_power_resource
*resource
)
415 if (!resource
->ref_count
) {
416 pr_debug("Power resource [%s] already off\n", resource
->name
);
420 if (--resource
->ref_count
) {
421 pr_debug("Power resource [%s] still in use\n", resource
->name
);
423 result
= __acpi_power_off(resource
);
425 resource
->ref_count
++;
430 static int acpi_power_off(struct acpi_power_resource
*resource
)
434 mutex_lock(&resource
->resource_lock
);
435 result
= acpi_power_off_unlocked(resource
);
436 mutex_unlock(&resource
->resource_lock
);
440 static int acpi_power_off_list(struct list_head
*list
)
442 struct acpi_power_resource_entry
*entry
;
445 list_for_each_entry_reverse(entry
, list
, node
) {
446 result
= acpi_power_off(entry
->resource
);
453 list_for_each_entry_continue(entry
, list
, node
)
454 acpi_power_on(entry
->resource
);
459 static int acpi_power_on_list(struct list_head
*list
)
461 struct acpi_power_resource_entry
*entry
;
464 list_for_each_entry(entry
, list
, node
) {
465 result
= acpi_power_on(entry
->resource
);
472 list_for_each_entry_continue_reverse(entry
, list
, node
)
473 acpi_power_off(entry
->resource
);
478 static struct attribute
*attrs
[] = {
482 static const struct attribute_group attr_groups
[] = {
484 .name
= "power_resources_D0",
488 .name
= "power_resources_D1",
492 .name
= "power_resources_D2",
495 [ACPI_STATE_D3_HOT
] = {
496 .name
= "power_resources_D3hot",
501 static const struct attribute_group wakeup_attr_group
= {
502 .name
= "power_resources_wakeup",
506 static void acpi_power_hide_list(struct acpi_device
*adev
,
507 struct list_head
*resources
,
508 const struct attribute_group
*attr_group
)
510 struct acpi_power_resource_entry
*entry
;
512 if (list_empty(resources
))
515 list_for_each_entry_reverse(entry
, resources
, node
) {
516 struct acpi_device
*res_dev
= &entry
->resource
->device
;
518 sysfs_remove_link_from_group(&adev
->dev
.kobj
,
520 dev_name(&res_dev
->dev
));
522 sysfs_remove_group(&adev
->dev
.kobj
, attr_group
);
525 static void acpi_power_expose_list(struct acpi_device
*adev
,
526 struct list_head
*resources
,
527 const struct attribute_group
*attr_group
)
529 struct acpi_power_resource_entry
*entry
;
532 if (list_empty(resources
))
535 ret
= sysfs_create_group(&adev
->dev
.kobj
, attr_group
);
539 list_for_each_entry(entry
, resources
, node
) {
540 struct acpi_device
*res_dev
= &entry
->resource
->device
;
542 ret
= sysfs_add_link_to_group(&adev
->dev
.kobj
,
545 dev_name(&res_dev
->dev
));
547 acpi_power_hide_list(adev
, resources
, attr_group
);
553 static void acpi_power_expose_hide(struct acpi_device
*adev
,
554 struct list_head
*resources
,
555 const struct attribute_group
*attr_group
,
559 acpi_power_expose_list(adev
, resources
, attr_group
);
561 acpi_power_hide_list(adev
, resources
, attr_group
);
564 void acpi_power_add_remove_device(struct acpi_device
*adev
, bool add
)
568 if (adev
->wakeup
.flags
.valid
)
569 acpi_power_expose_hide(adev
, &adev
->wakeup
.resources
,
570 &wakeup_attr_group
, add
);
572 if (!adev
->power
.flags
.power_resources
)
575 for (state
= ACPI_STATE_D0
; state
<= ACPI_STATE_D3_HOT
; state
++)
576 acpi_power_expose_hide(adev
,
577 &adev
->power
.states
[state
].resources
,
578 &attr_groups
[state
], add
);
581 int acpi_power_wakeup_list_init(struct list_head
*list
, int *system_level_p
)
583 struct acpi_power_resource_entry
*entry
;
584 int system_level
= 5;
586 list_for_each_entry(entry
, list
, node
) {
587 struct acpi_power_resource
*resource
= entry
->resource
;
588 acpi_handle handle
= resource
->device
.handle
;
592 mutex_lock(&resource
->resource_lock
);
594 result
= acpi_power_get_state(handle
, &state
);
596 mutex_unlock(&resource
->resource_lock
);
599 if (state
== ACPI_POWER_RESOURCE_STATE_ON
) {
600 resource
->ref_count
++;
601 resource
->wakeup_enabled
= true;
603 if (system_level
> resource
->system_level
)
604 system_level
= resource
->system_level
;
606 mutex_unlock(&resource
->resource_lock
);
608 *system_level_p
= system_level
;
612 /* --------------------------------------------------------------------------
613 Device Power Management
614 -------------------------------------------------------------------------- */
617 * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
618 * ACPI 3.0) _PSW (Power State Wake)
619 * @dev: Device to handle.
620 * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
621 * @sleep_state: Target sleep state of the system.
622 * @dev_state: Target power state of the device.
624 * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
625 * State Wake) for the device, if present. On failure reset the device's
626 * wakeup.flags.valid flag.
629 * 0 if either _DSW or _PSW has been successfully executed
630 * 0 if neither _DSW nor _PSW has been found
631 * -ENODEV if the execution of either _DSW or _PSW has failed
633 int acpi_device_sleep_wake(struct acpi_device
*dev
,
634 int enable
, int sleep_state
, int dev_state
)
636 union acpi_object in_arg
[3];
637 struct acpi_object_list arg_list
= { 3, in_arg
};
638 acpi_status status
= AE_OK
;
641 * Try to execute _DSW first.
643 * Three arguments are needed for the _DSW object:
644 * Argument 0: enable/disable the wake capabilities
645 * Argument 1: target system state
646 * Argument 2: target device state
647 * When _DSW object is called to disable the wake capabilities, maybe
648 * the first argument is filled. The values of the other two arguments
651 in_arg
[0].type
= ACPI_TYPE_INTEGER
;
652 in_arg
[0].integer
.value
= enable
;
653 in_arg
[1].type
= ACPI_TYPE_INTEGER
;
654 in_arg
[1].integer
.value
= sleep_state
;
655 in_arg
[2].type
= ACPI_TYPE_INTEGER
;
656 in_arg
[2].integer
.value
= dev_state
;
657 status
= acpi_evaluate_object(dev
->handle
, "_DSW", &arg_list
, NULL
);
658 if (ACPI_SUCCESS(status
)) {
660 } else if (status
!= AE_NOT_FOUND
) {
661 acpi_handle_info(dev
->handle
, "_DSW execution failed\n");
662 dev
->wakeup
.flags
.valid
= 0;
667 status
= acpi_execute_simple_method(dev
->handle
, "_PSW", enable
);
668 if (ACPI_FAILURE(status
) && (status
!= AE_NOT_FOUND
)) {
669 acpi_handle_info(dev
->handle
, "_PSW execution failed\n");
670 dev
->wakeup
.flags
.valid
= 0;
678 * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
679 * 1. Power on the power resources required for the wakeup device
680 * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
681 * State Wake) for the device, if present
683 int acpi_enable_wakeup_device_power(struct acpi_device
*dev
, int sleep_state
)
685 struct acpi_power_resource_entry
*entry
;
688 if (!dev
|| !dev
->wakeup
.flags
.valid
)
691 mutex_lock(&acpi_device_lock
);
693 if (dev
->wakeup
.prepare_count
++)
696 list_for_each_entry(entry
, &dev
->wakeup
.resources
, node
) {
697 struct acpi_power_resource
*resource
= entry
->resource
;
699 mutex_lock(&resource
->resource_lock
);
701 if (!resource
->wakeup_enabled
) {
702 err
= acpi_power_on_unlocked(resource
);
704 resource
->wakeup_enabled
= true;
707 mutex_unlock(&resource
->resource_lock
);
711 "Cannot turn wakeup power resources on\n");
712 dev
->wakeup
.flags
.valid
= 0;
717 * Passing 3 as the third argument below means the device may be
718 * put into arbitrary power state afterward.
720 err
= acpi_device_sleep_wake(dev
, 1, sleep_state
, 3);
722 dev
->wakeup
.prepare_count
= 0;
725 mutex_unlock(&acpi_device_lock
);
730 * Shutdown a wakeup device, counterpart of above method
731 * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
732 * State Wake) for the device, if present
733 * 2. Shutdown down the power resources
735 int acpi_disable_wakeup_device_power(struct acpi_device
*dev
)
737 struct acpi_power_resource_entry
*entry
;
740 if (!dev
|| !dev
->wakeup
.flags
.valid
)
743 mutex_lock(&acpi_device_lock
);
745 if (--dev
->wakeup
.prepare_count
> 0)
749 * Executing the code below even if prepare_count is already zero when
750 * the function is called may be useful, for example for initialisation.
752 if (dev
->wakeup
.prepare_count
< 0)
753 dev
->wakeup
.prepare_count
= 0;
755 err
= acpi_device_sleep_wake(dev
, 0, 0, 0);
759 list_for_each_entry(entry
, &dev
->wakeup
.resources
, node
) {
760 struct acpi_power_resource
*resource
= entry
->resource
;
762 mutex_lock(&resource
->resource_lock
);
764 if (resource
->wakeup_enabled
) {
765 err
= acpi_power_off_unlocked(resource
);
767 resource
->wakeup_enabled
= false;
770 mutex_unlock(&resource
->resource_lock
);
774 "Cannot turn wakeup power resources off\n");
775 dev
->wakeup
.flags
.valid
= 0;
781 mutex_unlock(&acpi_device_lock
);
785 int acpi_power_get_inferred_state(struct acpi_device
*device
, int *state
)
791 if (!device
|| !state
)
795 * We know a device's inferred power state when all the resources
796 * required for a given D-state are 'on'.
798 for (i
= ACPI_STATE_D0
; i
<= ACPI_STATE_D3_HOT
; i
++) {
799 struct list_head
*list
= &device
->power
.states
[i
].resources
;
801 if (list_empty(list
))
804 result
= acpi_power_get_list_state(list
, &list_state
);
808 if (list_state
== ACPI_POWER_RESOURCE_STATE_ON
) {
814 *state
= device
->power
.states
[ACPI_STATE_D3_COLD
].flags
.valid
?
815 ACPI_STATE_D3_COLD
: ACPI_STATE_D3_HOT
;
819 int acpi_power_on_resources(struct acpi_device
*device
, int state
)
821 if (!device
|| state
< ACPI_STATE_D0
|| state
> ACPI_STATE_D3_HOT
)
824 return acpi_power_on_list(&device
->power
.states
[state
].resources
);
827 int acpi_power_transition(struct acpi_device
*device
, int state
)
831 if (!device
|| (state
< ACPI_STATE_D0
) || (state
> ACPI_STATE_D3_COLD
))
834 if (device
->power
.state
== state
|| !device
->flags
.power_manageable
)
837 if ((device
->power
.state
< ACPI_STATE_D0
)
838 || (device
->power
.state
> ACPI_STATE_D3_COLD
))
842 * First we reference all power resources required in the target list
843 * (e.g. so the device doesn't lose power while transitioning). Then,
844 * we dereference all power resources used in the current list.
846 if (state
< ACPI_STATE_D3_COLD
)
847 result
= acpi_power_on_list(
848 &device
->power
.states
[state
].resources
);
850 if (!result
&& device
->power
.state
< ACPI_STATE_D3_COLD
)
852 &device
->power
.states
[device
->power
.state
].resources
);
854 /* We shouldn't change the state unless the above operations succeed. */
855 device
->power
.state
= result
? ACPI_STATE_UNKNOWN
: state
;
860 static void acpi_release_power_resource(struct device
*dev
)
862 struct acpi_device
*device
= to_acpi_device(dev
);
863 struct acpi_power_resource
*resource
;
865 resource
= container_of(device
, struct acpi_power_resource
, device
);
867 mutex_lock(&power_resource_list_lock
);
868 list_del(&resource
->list_node
);
869 mutex_unlock(&power_resource_list_lock
);
871 acpi_free_pnp_ids(&device
->pnp
);
875 static ssize_t
resource_in_use_show(struct device
*dev
,
876 struct device_attribute
*attr
,
879 struct acpi_power_resource
*resource
;
881 resource
= to_power_resource(to_acpi_device(dev
));
882 return sprintf(buf
, "%u\n", !!resource
->ref_count
);
884 static DEVICE_ATTR_RO(resource_in_use
);
886 static void acpi_power_sysfs_remove(struct acpi_device
*device
)
888 device_remove_file(&device
->dev
, &dev_attr_resource_in_use
);
891 static void acpi_power_add_resource_to_list(struct acpi_power_resource
*resource
)
893 mutex_lock(&power_resource_list_lock
);
895 if (!list_empty(&acpi_power_resource_list
)) {
896 struct acpi_power_resource
*r
;
898 list_for_each_entry(r
, &acpi_power_resource_list
, list_node
)
899 if (r
->order
> resource
->order
) {
900 list_add_tail(&resource
->list_node
, &r
->list_node
);
904 list_add_tail(&resource
->list_node
, &acpi_power_resource_list
);
907 mutex_unlock(&power_resource_list_lock
);
910 int acpi_add_power_resource(acpi_handle handle
)
912 struct acpi_power_resource
*resource
;
913 struct acpi_device
*device
= NULL
;
914 union acpi_object acpi_object
;
915 struct acpi_buffer buffer
= { sizeof(acpi_object
), &acpi_object
};
917 int state
, result
= -ENODEV
;
919 acpi_bus_get_device(handle
, &device
);
923 resource
= kzalloc(sizeof(*resource
), GFP_KERNEL
);
927 device
= &resource
->device
;
928 acpi_init_device_object(device
, handle
, ACPI_BUS_TYPE_POWER
);
929 mutex_init(&resource
->resource_lock
);
930 INIT_LIST_HEAD(&resource
->list_node
);
931 INIT_LIST_HEAD(&resource
->dependents
);
932 resource
->name
= device
->pnp
.bus_id
;
933 strcpy(acpi_device_name(device
), ACPI_POWER_DEVICE_NAME
);
934 strcpy(acpi_device_class(device
), ACPI_POWER_CLASS
);
935 device
->power
.state
= ACPI_STATE_UNKNOWN
;
937 /* Evaluate the object to get the system level and resource order. */
938 status
= acpi_evaluate_object(handle
, NULL
, NULL
, &buffer
);
939 if (ACPI_FAILURE(status
))
942 resource
->system_level
= acpi_object
.power_resource
.system_level
;
943 resource
->order
= acpi_object
.power_resource
.resource_order
;
945 result
= acpi_power_get_state(handle
, &state
);
949 pr_info("%s [%s] (%s)\n", acpi_device_name(device
),
950 acpi_device_bid(device
), state
? "on" : "off");
952 device
->flags
.match_driver
= true;
953 result
= acpi_device_add(device
, acpi_release_power_resource
);
957 if (!device_create_file(&device
->dev
, &dev_attr_resource_in_use
))
958 device
->remove
= acpi_power_sysfs_remove
;
960 acpi_power_add_resource_to_list(resource
);
961 acpi_device_add_finalize(device
);
965 acpi_release_power_resource(&device
->dev
);
969 #ifdef CONFIG_ACPI_SLEEP
970 void acpi_resume_power_resources(void)
972 struct acpi_power_resource
*resource
;
974 mutex_lock(&power_resource_list_lock
);
976 list_for_each_entry(resource
, &acpi_power_resource_list
, list_node
) {
979 mutex_lock(&resource
->resource_lock
);
981 result
= acpi_power_get_state(resource
->device
.handle
, &state
);
983 mutex_unlock(&resource
->resource_lock
);
987 if (state
== ACPI_POWER_RESOURCE_STATE_OFF
988 && resource
->ref_count
) {
989 dev_info(&resource
->device
.dev
, "Turning ON\n");
990 __acpi_power_on(resource
);
993 mutex_unlock(&resource
->resource_lock
);
996 mutex_unlock(&power_resource_list_lock
);
999 void acpi_turn_off_unused_power_resources(void)
1001 struct acpi_power_resource
*resource
;
1003 mutex_lock(&power_resource_list_lock
);
1005 list_for_each_entry_reverse(resource
, &acpi_power_resource_list
, list_node
) {
1006 mutex_lock(&resource
->resource_lock
);
1008 if (!resource
->ref_count
) {
1009 dev_info(&resource
->device
.dev
, "Turning OFF\n");
1010 __acpi_power_off(resource
);
1013 mutex_unlock(&resource
->resource_lock
);
1016 mutex_unlock(&power_resource_list_lock
);