xref: /linux/drivers/acpi/power.c (revision 6c363eafc4d637ac4bd83d4a7dd06dd3cfbe7c5f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * drivers/acpi/power.c - ACPI Power Resources management.
4  *
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>
9  */
10 
11 /*
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.
16  *
17  * An ACPI "power resource object" represents a software controllable power
18  * plane, clock plane, or other resource depended on by a device.
19  *
20  * A device may rely on multiple power resources, and a power resource
21  * may be shared by multiple devices.
22  */
23 
24 #define pr_fmt(fmt) "ACPI: PM: " fmt
25 
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>
34 #include "sleep.h"
35 #include "internal.h"
36 
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
42 
43 struct acpi_power_dependent_device {
44 	struct device *dev;
45 	struct list_head node;
46 };
47 
48 struct acpi_power_resource {
49 	struct acpi_device device;
50 	struct list_head list_node;
51 	char *name;
52 	u32 system_level;
53 	u32 order;
54 	unsigned int ref_count;
55 	bool wakeup_enabled;
56 	struct mutex resource_lock;
57 	struct list_head dependents;
58 };
59 
60 struct acpi_power_resource_entry {
61 	struct list_head node;
62 	struct acpi_power_resource *resource;
63 };
64 
65 static LIST_HEAD(acpi_power_resource_list);
66 static DEFINE_MUTEX(power_resource_list_lock);
67 
68 /* --------------------------------------------------------------------------
69                              Power Resource Management
70    -------------------------------------------------------------------------- */
71 
72 static inline
73 struct acpi_power_resource *to_power_resource(struct acpi_device *device)
74 {
75 	return container_of(device, struct acpi_power_resource, device);
76 }
77 
78 static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle)
79 {
80 	struct acpi_device *device;
81 
82 	if (acpi_bus_get_device(handle, &device))
83 		return NULL;
84 
85 	return to_power_resource(device);
86 }
87 
88 static int acpi_power_resources_list_add(acpi_handle handle,
89 					 struct list_head *list)
90 {
91 	struct acpi_power_resource *resource = acpi_power_get_context(handle);
92 	struct acpi_power_resource_entry *entry;
93 
94 	if (!resource || !list)
95 		return -EINVAL;
96 
97 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
98 	if (!entry)
99 		return -ENOMEM;
100 
101 	entry->resource = resource;
102 	if (!list_empty(list)) {
103 		struct acpi_power_resource_entry *e;
104 
105 		list_for_each_entry(e, list, node)
106 			if (e->resource->order > resource->order) {
107 				list_add_tail(&entry->node, &e->node);
108 				return 0;
109 			}
110 	}
111 	list_add_tail(&entry->node, list);
112 	return 0;
113 }
114 
115 void acpi_power_resources_list_free(struct list_head *list)
116 {
117 	struct acpi_power_resource_entry *entry, *e;
118 
119 	list_for_each_entry_safe(entry, e, list, node) {
120 		list_del(&entry->node);
121 		kfree(entry);
122 	}
123 }
124 
125 static bool acpi_power_resource_is_dup(union acpi_object *package,
126 				       unsigned int start, unsigned int i)
127 {
128 	acpi_handle rhandle, dup;
129 	unsigned int j;
130 
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;
135 		if (dup == rhandle)
136 			return true;
137 	}
138 
139 	return false;
140 }
141 
142 int acpi_extract_power_resources(union acpi_object *package, unsigned int start,
143 				 struct list_head *list)
144 {
145 	unsigned int i;
146 	int err = 0;
147 
148 	for (i = start; i < package->package.count; i++) {
149 		union acpi_object *element = &package->package.elements[i];
150 		acpi_handle rhandle;
151 
152 		if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
153 			err = -ENODATA;
154 			break;
155 		}
156 		rhandle = element->reference.handle;
157 		if (!rhandle) {
158 			err = -ENODEV;
159 			break;
160 		}
161 
162 		/* Some ACPI tables contain duplicate power resource references */
163 		if (acpi_power_resource_is_dup(package, start, i))
164 			continue;
165 
166 		err = acpi_add_power_resource(rhandle);
167 		if (err)
168 			break;
169 
170 		err = acpi_power_resources_list_add(rhandle, list);
171 		if (err)
172 			break;
173 	}
174 	if (err)
175 		acpi_power_resources_list_free(list);
176 
177 	return err;
178 }
179 
180 static int acpi_power_get_state(acpi_handle handle, int *state)
181 {
182 	acpi_status status = AE_OK;
183 	unsigned long long sta = 0;
184 
185 	if (!handle || !state)
186 		return -EINVAL;
187 
188 	status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
189 	if (ACPI_FAILURE(status))
190 		return -ENODEV;
191 
192 	*state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
193 			      ACPI_POWER_RESOURCE_STATE_OFF;
194 
195 	acpi_handle_debug(handle, "Power resource is %s\n",
196 			  *state ? "on" : "off");
197 
198 	return 0;
199 }
200 
201 static int acpi_power_get_list_state(struct list_head *list, int *state)
202 {
203 	struct acpi_power_resource_entry *entry;
204 	int cur_state;
205 
206 	if (!list || !state)
207 		return -EINVAL;
208 
209 	/* The state of the list is 'on' IFF all resources are 'on'. */
210 	cur_state = 0;
211 	list_for_each_entry(entry, list, node) {
212 		struct acpi_power_resource *resource = entry->resource;
213 		acpi_handle handle = resource->device.handle;
214 		int result;
215 
216 		mutex_lock(&resource->resource_lock);
217 		result = acpi_power_get_state(handle, &cur_state);
218 		mutex_unlock(&resource->resource_lock);
219 		if (result)
220 			return result;
221 
222 		if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
223 			break;
224 	}
225 
226 	pr_debug("Power resource list is %s\n", cur_state ? "on" : "off");
227 
228 	*state = cur_state;
229 	return 0;
230 }
231 
232 static int
233 acpi_power_resource_add_dependent(struct acpi_power_resource *resource,
234 				  struct device *dev)
235 {
236 	struct acpi_power_dependent_device *dep;
237 	int ret = 0;
238 
239 	mutex_lock(&resource->resource_lock);
240 	list_for_each_entry(dep, &resource->dependents, node) {
241 		/* Only add it once */
242 		if (dep->dev == dev)
243 			goto unlock;
244 	}
245 
246 	dep = kzalloc(sizeof(*dep), GFP_KERNEL);
247 	if (!dep) {
248 		ret = -ENOMEM;
249 		goto unlock;
250 	}
251 
252 	dep->dev = dev;
253 	list_add_tail(&dep->node, &resource->dependents);
254 	dev_dbg(dev, "added power dependency to [%s]\n", resource->name);
255 
256 unlock:
257 	mutex_unlock(&resource->resource_lock);
258 	return ret;
259 }
260 
261 static void
262 acpi_power_resource_remove_dependent(struct acpi_power_resource *resource,
263 				     struct device *dev)
264 {
265 	struct acpi_power_dependent_device *dep;
266 
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);
271 			kfree(dep);
272 			dev_dbg(dev, "removed power dependency to [%s]\n",
273 				resource->name);
274 			break;
275 		}
276 	}
277 	mutex_unlock(&resource->resource_lock);
278 }
279 
280 /**
281  * acpi_device_power_add_dependent - Add dependent device of this ACPI device
282  * @adev: ACPI device pointer
283  * @dev: Dependent device
284  *
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.
290  *
291  * If @adev does not have _PR0 this does nothing.
292  *
293  * Returns %0 in case of success and negative errno otherwise.
294  */
295 int acpi_device_power_add_dependent(struct acpi_device *adev,
296 				    struct device *dev)
297 {
298 	struct acpi_power_resource_entry *entry;
299 	struct list_head *resources;
300 	int ret;
301 
302 	if (!adev->flags.power_manageable)
303 		return 0;
304 
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);
308 		if (ret)
309 			goto err;
310 	}
311 
312 	return 0;
313 
314 err:
315 	list_for_each_entry(entry, resources, node)
316 		acpi_power_resource_remove_dependent(entry->resource, dev);
317 
318 	return ret;
319 }
320 
321 /**
322  * acpi_device_power_remove_dependent - Remove dependent device
323  * @adev: ACPI device pointer
324  * @dev: Dependent device
325  *
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
328  * have _PR0 as well.
329  */
330 void acpi_device_power_remove_dependent(struct acpi_device *adev,
331 					struct device *dev)
332 {
333 	struct acpi_power_resource_entry *entry;
334 	struct list_head *resources;
335 
336 	if (!adev->flags.power_manageable)
337 		return;
338 
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);
342 }
343 
344 static int __acpi_power_on(struct acpi_power_resource *resource)
345 {
346 	struct acpi_power_dependent_device *dep;
347 	acpi_status status = AE_OK;
348 
349 	status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL);
350 	if (ACPI_FAILURE(status))
351 		return -ENODEV;
352 
353 	pr_debug("Power resource [%s] turned on\n", resource->name);
354 
355 	/*
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.
359 	 */
360 	if (list_empty(&resource->dependents) ||
361 	    list_is_singular(&resource->dependents))
362 		return 0;
363 
364 	list_for_each_entry(dep, &resource->dependents, node) {
365 		dev_dbg(dep->dev, "runtime resuming because [%s] turned on\n",
366 			resource->name);
367 		pm_request_resume(dep->dev);
368 	}
369 
370 	return 0;
371 }
372 
373 static int acpi_power_on_unlocked(struct acpi_power_resource *resource)
374 {
375 	int result = 0;
376 
377 	if (resource->ref_count++) {
378 		pr_debug("Power resource [%s] already on\n", resource->name);
379 	} else {
380 		result = __acpi_power_on(resource);
381 		if (result)
382 			resource->ref_count--;
383 	}
384 	return result;
385 }
386 
387 static int acpi_power_on(struct acpi_power_resource *resource)
388 {
389 	int result;
390 
391 	mutex_lock(&resource->resource_lock);
392 	result = acpi_power_on_unlocked(resource);
393 	mutex_unlock(&resource->resource_lock);
394 	return result;
395 }
396 
397 static int __acpi_power_off(struct acpi_power_resource *resource)
398 {
399 	acpi_status status;
400 
401 	status = acpi_evaluate_object(resource->device.handle, "_OFF",
402 				      NULL, NULL);
403 	if (ACPI_FAILURE(status))
404 		return -ENODEV;
405 
406 	pr_debug("Power resource [%s] turned off\n", resource->name);
407 
408 	return 0;
409 }
410 
411 static int acpi_power_off_unlocked(struct acpi_power_resource *resource)
412 {
413 	int result = 0;
414 
415 	if (!resource->ref_count) {
416 		pr_debug("Power resource [%s] already off\n", resource->name);
417 		return 0;
418 	}
419 
420 	if (--resource->ref_count) {
421 		pr_debug("Power resource [%s] still in use\n", resource->name);
422 	} else {
423 		result = __acpi_power_off(resource);
424 		if (result)
425 			resource->ref_count++;
426 	}
427 	return result;
428 }
429 
430 static int acpi_power_off(struct acpi_power_resource *resource)
431 {
432 	int result;
433 
434 	mutex_lock(&resource->resource_lock);
435 	result = acpi_power_off_unlocked(resource);
436 	mutex_unlock(&resource->resource_lock);
437 	return result;
438 }
439 
440 static int acpi_power_off_list(struct list_head *list)
441 {
442 	struct acpi_power_resource_entry *entry;
443 	int result = 0;
444 
445 	list_for_each_entry_reverse(entry, list, node) {
446 		result = acpi_power_off(entry->resource);
447 		if (result)
448 			goto err;
449 	}
450 	return 0;
451 
452  err:
453 	list_for_each_entry_continue(entry, list, node)
454 		acpi_power_on(entry->resource);
455 
456 	return result;
457 }
458 
459 static int acpi_power_on_list(struct list_head *list)
460 {
461 	struct acpi_power_resource_entry *entry;
462 	int result = 0;
463 
464 	list_for_each_entry(entry, list, node) {
465 		result = acpi_power_on(entry->resource);
466 		if (result)
467 			goto err;
468 	}
469 	return 0;
470 
471  err:
472 	list_for_each_entry_continue_reverse(entry, list, node)
473 		acpi_power_off(entry->resource);
474 
475 	return result;
476 }
477 
478 static struct attribute *attrs[] = {
479 	NULL,
480 };
481 
482 static const struct attribute_group attr_groups[] = {
483 	[ACPI_STATE_D0] = {
484 		.name = "power_resources_D0",
485 		.attrs = attrs,
486 	},
487 	[ACPI_STATE_D1] = {
488 		.name = "power_resources_D1",
489 		.attrs = attrs,
490 	},
491 	[ACPI_STATE_D2] = {
492 		.name = "power_resources_D2",
493 		.attrs = attrs,
494 	},
495 	[ACPI_STATE_D3_HOT] = {
496 		.name = "power_resources_D3hot",
497 		.attrs = attrs,
498 	},
499 };
500 
501 static const struct attribute_group wakeup_attr_group = {
502 	.name = "power_resources_wakeup",
503 	.attrs = attrs,
504 };
505 
506 static void acpi_power_hide_list(struct acpi_device *adev,
507 				 struct list_head *resources,
508 				 const struct attribute_group *attr_group)
509 {
510 	struct acpi_power_resource_entry *entry;
511 
512 	if (list_empty(resources))
513 		return;
514 
515 	list_for_each_entry_reverse(entry, resources, node) {
516 		struct acpi_device *res_dev = &entry->resource->device;
517 
518 		sysfs_remove_link_from_group(&adev->dev.kobj,
519 					     attr_group->name,
520 					     dev_name(&res_dev->dev));
521 	}
522 	sysfs_remove_group(&adev->dev.kobj, attr_group);
523 }
524 
525 static void acpi_power_expose_list(struct acpi_device *adev,
526 				   struct list_head *resources,
527 				   const struct attribute_group *attr_group)
528 {
529 	struct acpi_power_resource_entry *entry;
530 	int ret;
531 
532 	if (list_empty(resources))
533 		return;
534 
535 	ret = sysfs_create_group(&adev->dev.kobj, attr_group);
536 	if (ret)
537 		return;
538 
539 	list_for_each_entry(entry, resources, node) {
540 		struct acpi_device *res_dev = &entry->resource->device;
541 
542 		ret = sysfs_add_link_to_group(&adev->dev.kobj,
543 					      attr_group->name,
544 					      &res_dev->dev.kobj,
545 					      dev_name(&res_dev->dev));
546 		if (ret) {
547 			acpi_power_hide_list(adev, resources, attr_group);
548 			break;
549 		}
550 	}
551 }
552 
553 static void acpi_power_expose_hide(struct acpi_device *adev,
554 				   struct list_head *resources,
555 				   const struct attribute_group *attr_group,
556 				   bool expose)
557 {
558 	if (expose)
559 		acpi_power_expose_list(adev, resources, attr_group);
560 	else
561 		acpi_power_hide_list(adev, resources, attr_group);
562 }
563 
564 void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
565 {
566 	int state;
567 
568 	if (adev->wakeup.flags.valid)
569 		acpi_power_expose_hide(adev, &adev->wakeup.resources,
570 				       &wakeup_attr_group, add);
571 
572 	if (!adev->power.flags.power_resources)
573 		return;
574 
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);
579 }
580 
581 int acpi_power_wakeup_list_init(struct list_head *list, int *system_level_p)
582 {
583 	struct acpi_power_resource_entry *entry;
584 	int system_level = 5;
585 
586 	list_for_each_entry(entry, list, node) {
587 		struct acpi_power_resource *resource = entry->resource;
588 		acpi_handle handle = resource->device.handle;
589 		int result;
590 		int state;
591 
592 		mutex_lock(&resource->resource_lock);
593 
594 		result = acpi_power_get_state(handle, &state);
595 		if (result) {
596 			mutex_unlock(&resource->resource_lock);
597 			return result;
598 		}
599 		if (state == ACPI_POWER_RESOURCE_STATE_ON) {
600 			resource->ref_count++;
601 			resource->wakeup_enabled = true;
602 		}
603 		if (system_level > resource->system_level)
604 			system_level = resource->system_level;
605 
606 		mutex_unlock(&resource->resource_lock);
607 	}
608 	*system_level_p = system_level;
609 	return 0;
610 }
611 
612 /* --------------------------------------------------------------------------
613                              Device Power Management
614    -------------------------------------------------------------------------- */
615 
616 /**
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.
623  *
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.
627  *
628  * RETURN VALUE:
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
632  */
633 int acpi_device_sleep_wake(struct acpi_device *dev,
634 			   int enable, int sleep_state, int dev_state)
635 {
636 	union acpi_object in_arg[3];
637 	struct acpi_object_list arg_list = { 3, in_arg };
638 	acpi_status status = AE_OK;
639 
640 	/*
641 	 * Try to execute _DSW first.
642 	 *
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
649 	 * are meaningless.
650 	 */
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)) {
659 		return 0;
660 	} else if (status != AE_NOT_FOUND) {
661 		acpi_handle_info(dev->handle, "_DSW execution failed\n");
662 		dev->wakeup.flags.valid = 0;
663 		return -ENODEV;
664 	}
665 
666 	/* Execute _PSW */
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;
671 		return -ENODEV;
672 	}
673 
674 	return 0;
675 }
676 
677 /*
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
682  */
683 int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
684 {
685 	struct acpi_power_resource_entry *entry;
686 	int err = 0;
687 
688 	if (!dev || !dev->wakeup.flags.valid)
689 		return -EINVAL;
690 
691 	mutex_lock(&acpi_device_lock);
692 
693 	if (dev->wakeup.prepare_count++)
694 		goto out;
695 
696 	list_for_each_entry(entry, &dev->wakeup.resources, node) {
697 		struct acpi_power_resource *resource = entry->resource;
698 
699 		mutex_lock(&resource->resource_lock);
700 
701 		if (!resource->wakeup_enabled) {
702 			err = acpi_power_on_unlocked(resource);
703 			if (!err)
704 				resource->wakeup_enabled = true;
705 		}
706 
707 		mutex_unlock(&resource->resource_lock);
708 
709 		if (err) {
710 			dev_err(&dev->dev,
711 				"Cannot turn wakeup power resources on\n");
712 			dev->wakeup.flags.valid = 0;
713 			goto out;
714 		}
715 	}
716 	/*
717 	 * Passing 3 as the third argument below means the device may be
718 	 * put into arbitrary power state afterward.
719 	 */
720 	err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
721 	if (err)
722 		dev->wakeup.prepare_count = 0;
723 
724  out:
725 	mutex_unlock(&acpi_device_lock);
726 	return err;
727 }
728 
729 /*
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
734  */
735 int acpi_disable_wakeup_device_power(struct acpi_device *dev)
736 {
737 	struct acpi_power_resource_entry *entry;
738 	int err = 0;
739 
740 	if (!dev || !dev->wakeup.flags.valid)
741 		return -EINVAL;
742 
743 	mutex_lock(&acpi_device_lock);
744 
745 	if (--dev->wakeup.prepare_count > 0)
746 		goto out;
747 
748 	/*
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.
751 	 */
752 	if (dev->wakeup.prepare_count < 0)
753 		dev->wakeup.prepare_count = 0;
754 
755 	err = acpi_device_sleep_wake(dev, 0, 0, 0);
756 	if (err)
757 		goto out;
758 
759 	list_for_each_entry(entry, &dev->wakeup.resources, node) {
760 		struct acpi_power_resource *resource = entry->resource;
761 
762 		mutex_lock(&resource->resource_lock);
763 
764 		if (resource->wakeup_enabled) {
765 			err = acpi_power_off_unlocked(resource);
766 			if (!err)
767 				resource->wakeup_enabled = false;
768 		}
769 
770 		mutex_unlock(&resource->resource_lock);
771 
772 		if (err) {
773 			dev_err(&dev->dev,
774 				"Cannot turn wakeup power resources off\n");
775 			dev->wakeup.flags.valid = 0;
776 			break;
777 		}
778 	}
779 
780  out:
781 	mutex_unlock(&acpi_device_lock);
782 	return err;
783 }
784 
785 int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
786 {
787 	int result = 0;
788 	int list_state = 0;
789 	int i = 0;
790 
791 	if (!device || !state)
792 		return -EINVAL;
793 
794 	/*
795 	 * We know a device's inferred power state when all the resources
796 	 * required for a given D-state are 'on'.
797 	 */
798 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
799 		struct list_head *list = &device->power.states[i].resources;
800 
801 		if (list_empty(list))
802 			continue;
803 
804 		result = acpi_power_get_list_state(list, &list_state);
805 		if (result)
806 			return result;
807 
808 		if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
809 			*state = i;
810 			return 0;
811 		}
812 	}
813 
814 	*state = device->power.states[ACPI_STATE_D3_COLD].flags.valid ?
815 		ACPI_STATE_D3_COLD : ACPI_STATE_D3_HOT;
816 	return 0;
817 }
818 
819 int acpi_power_on_resources(struct acpi_device *device, int state)
820 {
821 	if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT)
822 		return -EINVAL;
823 
824 	return acpi_power_on_list(&device->power.states[state].resources);
825 }
826 
827 int acpi_power_transition(struct acpi_device *device, int state)
828 {
829 	int result = 0;
830 
831 	if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
832 		return -EINVAL;
833 
834 	if (device->power.state == state || !device->flags.power_manageable)
835 		return 0;
836 
837 	if ((device->power.state < ACPI_STATE_D0)
838 	    || (device->power.state > ACPI_STATE_D3_COLD))
839 		return -ENODEV;
840 
841 	/*
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.
845 	 */
846 	if (state < ACPI_STATE_D3_COLD)
847 		result = acpi_power_on_list(
848 			&device->power.states[state].resources);
849 
850 	if (!result && device->power.state < ACPI_STATE_D3_COLD)
851 		acpi_power_off_list(
852 			&device->power.states[device->power.state].resources);
853 
854 	/* We shouldn't change the state unless the above operations succeed. */
855 	device->power.state = result ? ACPI_STATE_UNKNOWN : state;
856 
857 	return result;
858 }
859 
860 static void acpi_release_power_resource(struct device *dev)
861 {
862 	struct acpi_device *device = to_acpi_device(dev);
863 	struct acpi_power_resource *resource;
864 
865 	resource = container_of(device, struct acpi_power_resource, device);
866 
867 	mutex_lock(&power_resource_list_lock);
868 	list_del(&resource->list_node);
869 	mutex_unlock(&power_resource_list_lock);
870 
871 	acpi_free_pnp_ids(&device->pnp);
872 	kfree(resource);
873 }
874 
875 static ssize_t resource_in_use_show(struct device *dev,
876 				    struct device_attribute *attr,
877 				    char *buf)
878 {
879 	struct acpi_power_resource *resource;
880 
881 	resource = to_power_resource(to_acpi_device(dev));
882 	return sprintf(buf, "%u\n", !!resource->ref_count);
883 }
884 static DEVICE_ATTR_RO(resource_in_use);
885 
886 static void acpi_power_sysfs_remove(struct acpi_device *device)
887 {
888 	device_remove_file(&device->dev, &dev_attr_resource_in_use);
889 }
890 
891 static void acpi_power_add_resource_to_list(struct acpi_power_resource *resource)
892 {
893 	mutex_lock(&power_resource_list_lock);
894 
895 	if (!list_empty(&acpi_power_resource_list)) {
896 		struct acpi_power_resource *r;
897 
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);
901 				goto out;
902 			}
903 	}
904 	list_add_tail(&resource->list_node, &acpi_power_resource_list);
905 
906  out:
907 	mutex_unlock(&power_resource_list_lock);
908 }
909 
910 int acpi_add_power_resource(acpi_handle handle)
911 {
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 };
916 	acpi_status status;
917 	int state, result = -ENODEV;
918 
919 	acpi_bus_get_device(handle, &device);
920 	if (device)
921 		return 0;
922 
923 	resource = kzalloc(sizeof(*resource), GFP_KERNEL);
924 	if (!resource)
925 		return -ENOMEM;
926 
927 	device = &resource->device;
928 	acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER,
929 				ACPI_STA_DEFAULT, NULL);
930 	mutex_init(&resource->resource_lock);
931 	INIT_LIST_HEAD(&resource->list_node);
932 	INIT_LIST_HEAD(&resource->dependents);
933 	resource->name = device->pnp.bus_id;
934 	strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
935 	strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
936 	device->power.state = ACPI_STATE_UNKNOWN;
937 
938 	/* Evalute the object to get the system level and resource order. */
939 	status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
940 	if (ACPI_FAILURE(status))
941 		goto err;
942 
943 	resource->system_level = acpi_object.power_resource.system_level;
944 	resource->order = acpi_object.power_resource.resource_order;
945 
946 	result = acpi_power_get_state(handle, &state);
947 	if (result)
948 		goto err;
949 
950 	pr_info("%s [%s] (%s)\n", acpi_device_name(device),
951 		acpi_device_bid(device), state ? "on" : "off");
952 
953 	device->flags.match_driver = true;
954 	result = acpi_device_add(device, acpi_release_power_resource);
955 	if (result)
956 		goto err;
957 
958 	if (!device_create_file(&device->dev, &dev_attr_resource_in_use))
959 		device->remove = acpi_power_sysfs_remove;
960 
961 	acpi_power_add_resource_to_list(resource);
962 	acpi_device_add_finalize(device);
963 	return 0;
964 
965  err:
966 	acpi_release_power_resource(&device->dev);
967 	return result;
968 }
969 
970 #ifdef CONFIG_ACPI_SLEEP
971 void acpi_resume_power_resources(void)
972 {
973 	struct acpi_power_resource *resource;
974 
975 	mutex_lock(&power_resource_list_lock);
976 
977 	list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
978 		int result, state;
979 
980 		mutex_lock(&resource->resource_lock);
981 
982 		result = acpi_power_get_state(resource->device.handle, &state);
983 		if (result) {
984 			mutex_unlock(&resource->resource_lock);
985 			continue;
986 		}
987 
988 		if (state == ACPI_POWER_RESOURCE_STATE_OFF
989 		    && resource->ref_count) {
990 			dev_info(&resource->device.dev, "Turning ON\n");
991 			__acpi_power_on(resource);
992 		}
993 
994 		mutex_unlock(&resource->resource_lock);
995 	}
996 
997 	mutex_unlock(&power_resource_list_lock);
998 }
999 
1000 void acpi_turn_off_unused_power_resources(void)
1001 {
1002 	struct acpi_power_resource *resource;
1003 
1004 	mutex_lock(&power_resource_list_lock);
1005 
1006 	list_for_each_entry_reverse(resource, &acpi_power_resource_list, list_node) {
1007 		int result, state;
1008 
1009 		mutex_lock(&resource->resource_lock);
1010 
1011 		result = acpi_power_get_state(resource->device.handle, &state);
1012 		if (result) {
1013 			mutex_unlock(&resource->resource_lock);
1014 			continue;
1015 		}
1016 
1017 		if (state == ACPI_POWER_RESOURCE_STATE_ON
1018 		    && !resource->ref_count) {
1019 			dev_info(&resource->device.dev, "Turning OFF\n");
1020 			__acpi_power_off(resource);
1021 		}
1022 
1023 		mutex_unlock(&resource->resource_lock);
1024 	}
1025 
1026 	mutex_unlock(&power_resource_list_lock);
1027 }
1028 #endif
1029