xref: /linux/drivers/acpi/power.c (revision b7019ac550eb3916f34d79db583e9b7ea2524afa)
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 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/types.h>
28 #include <linux/slab.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/sysfs.h>
31 #include <linux/acpi.h>
32 #include "sleep.h"
33 #include "internal.h"
34 
35 #define _COMPONENT			ACPI_POWER_COMPONENT
36 ACPI_MODULE_NAME("power");
37 #define ACPI_POWER_CLASS		"power_resource"
38 #define ACPI_POWER_DEVICE_NAME		"Power Resource"
39 #define ACPI_POWER_FILE_INFO		"info"
40 #define ACPI_POWER_FILE_STATUS		"state"
41 #define ACPI_POWER_RESOURCE_STATE_OFF	0x00
42 #define ACPI_POWER_RESOURCE_STATE_ON	0x01
43 #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
44 
45 struct acpi_power_resource {
46 	struct acpi_device device;
47 	struct list_head list_node;
48 	char *name;
49 	u32 system_level;
50 	u32 order;
51 	unsigned int ref_count;
52 	bool wakeup_enabled;
53 	struct mutex resource_lock;
54 };
55 
56 struct acpi_power_resource_entry {
57 	struct list_head node;
58 	struct acpi_power_resource *resource;
59 };
60 
61 static LIST_HEAD(acpi_power_resource_list);
62 static DEFINE_MUTEX(power_resource_list_lock);
63 
64 /* --------------------------------------------------------------------------
65                              Power Resource Management
66    -------------------------------------------------------------------------- */
67 
68 static inline
69 struct acpi_power_resource *to_power_resource(struct acpi_device *device)
70 {
71 	return container_of(device, struct acpi_power_resource, device);
72 }
73 
74 static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle)
75 {
76 	struct acpi_device *device;
77 
78 	if (acpi_bus_get_device(handle, &device))
79 		return NULL;
80 
81 	return to_power_resource(device);
82 }
83 
84 static int acpi_power_resources_list_add(acpi_handle handle,
85 					 struct list_head *list)
86 {
87 	struct acpi_power_resource *resource = acpi_power_get_context(handle);
88 	struct acpi_power_resource_entry *entry;
89 
90 	if (!resource || !list)
91 		return -EINVAL;
92 
93 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
94 	if (!entry)
95 		return -ENOMEM;
96 
97 	entry->resource = resource;
98 	if (!list_empty(list)) {
99 		struct acpi_power_resource_entry *e;
100 
101 		list_for_each_entry(e, list, node)
102 			if (e->resource->order > resource->order) {
103 				list_add_tail(&entry->node, &e->node);
104 				return 0;
105 			}
106 	}
107 	list_add_tail(&entry->node, list);
108 	return 0;
109 }
110 
111 void acpi_power_resources_list_free(struct list_head *list)
112 {
113 	struct acpi_power_resource_entry *entry, *e;
114 
115 	list_for_each_entry_safe(entry, e, list, node) {
116 		list_del(&entry->node);
117 		kfree(entry);
118 	}
119 }
120 
121 static bool acpi_power_resource_is_dup(union acpi_object *package,
122 				       unsigned int start, unsigned int i)
123 {
124 	acpi_handle rhandle, dup;
125 	unsigned int j;
126 
127 	/* The caller is expected to check the package element types */
128 	rhandle = package->package.elements[i].reference.handle;
129 	for (j = start; j < i; j++) {
130 		dup = package->package.elements[j].reference.handle;
131 		if (dup == rhandle)
132 			return true;
133 	}
134 
135 	return false;
136 }
137 
138 int acpi_extract_power_resources(union acpi_object *package, unsigned int start,
139 				 struct list_head *list)
140 {
141 	unsigned int i;
142 	int err = 0;
143 
144 	for (i = start; i < package->package.count; i++) {
145 		union acpi_object *element = &package->package.elements[i];
146 		acpi_handle rhandle;
147 
148 		if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
149 			err = -ENODATA;
150 			break;
151 		}
152 		rhandle = element->reference.handle;
153 		if (!rhandle) {
154 			err = -ENODEV;
155 			break;
156 		}
157 
158 		/* Some ACPI tables contain duplicate power resource references */
159 		if (acpi_power_resource_is_dup(package, start, i))
160 			continue;
161 
162 		err = acpi_add_power_resource(rhandle);
163 		if (err)
164 			break;
165 
166 		err = acpi_power_resources_list_add(rhandle, list);
167 		if (err)
168 			break;
169 	}
170 	if (err)
171 		acpi_power_resources_list_free(list);
172 
173 	return err;
174 }
175 
176 static int acpi_power_get_state(acpi_handle handle, int *state)
177 {
178 	acpi_status status = AE_OK;
179 	unsigned long long sta = 0;
180 	char node_name[5];
181 	struct acpi_buffer buffer = { sizeof(node_name), node_name };
182 
183 
184 	if (!handle || !state)
185 		return -EINVAL;
186 
187 	status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
188 	if (ACPI_FAILURE(status))
189 		return -ENODEV;
190 
191 	*state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
192 			      ACPI_POWER_RESOURCE_STATE_OFF;
193 
194 	acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
195 
196 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
197 			  node_name,
198 				*state ? "on" : "off"));
199 
200 	return 0;
201 }
202 
203 static int acpi_power_get_list_state(struct list_head *list, int *state)
204 {
205 	struct acpi_power_resource_entry *entry;
206 	int cur_state;
207 
208 	if (!list || !state)
209 		return -EINVAL;
210 
211 	/* The state of the list is 'on' IFF all resources are 'on'. */
212 	cur_state = 0;
213 	list_for_each_entry(entry, list, node) {
214 		struct acpi_power_resource *resource = entry->resource;
215 		acpi_handle handle = resource->device.handle;
216 		int result;
217 
218 		mutex_lock(&resource->resource_lock);
219 		result = acpi_power_get_state(handle, &cur_state);
220 		mutex_unlock(&resource->resource_lock);
221 		if (result)
222 			return result;
223 
224 		if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
225 			break;
226 	}
227 
228 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
229 			  cur_state ? "on" : "off"));
230 
231 	*state = cur_state;
232 	return 0;
233 }
234 
235 static int __acpi_power_on(struct acpi_power_resource *resource)
236 {
237 	acpi_status status = AE_OK;
238 
239 	status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL);
240 	if (ACPI_FAILURE(status))
241 		return -ENODEV;
242 
243 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n",
244 			  resource->name));
245 
246 	return 0;
247 }
248 
249 static int acpi_power_on_unlocked(struct acpi_power_resource *resource)
250 {
251 	int result = 0;
252 
253 	if (resource->ref_count++) {
254 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
255 				  "Power resource [%s] already on\n",
256 				  resource->name));
257 	} else {
258 		result = __acpi_power_on(resource);
259 		if (result)
260 			resource->ref_count--;
261 	}
262 	return result;
263 }
264 
265 static int acpi_power_on(struct acpi_power_resource *resource)
266 {
267 	int result;
268 
269 	mutex_lock(&resource->resource_lock);
270 	result = acpi_power_on_unlocked(resource);
271 	mutex_unlock(&resource->resource_lock);
272 	return result;
273 }
274 
275 static int __acpi_power_off(struct acpi_power_resource *resource)
276 {
277 	acpi_status status;
278 
279 	status = acpi_evaluate_object(resource->device.handle, "_OFF",
280 				      NULL, NULL);
281 	if (ACPI_FAILURE(status))
282 		return -ENODEV;
283 
284 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned off\n",
285 			  resource->name));
286 	return 0;
287 }
288 
289 static int acpi_power_off_unlocked(struct acpi_power_resource *resource)
290 {
291 	int result = 0;
292 
293 	if (!resource->ref_count) {
294 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
295 				  "Power resource [%s] already off\n",
296 				  resource->name));
297 		return 0;
298 	}
299 
300 	if (--resource->ref_count) {
301 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
302 				  "Power resource [%s] still in use\n",
303 				  resource->name));
304 	} else {
305 		result = __acpi_power_off(resource);
306 		if (result)
307 			resource->ref_count++;
308 	}
309 	return result;
310 }
311 
312 static int acpi_power_off(struct acpi_power_resource *resource)
313 {
314 	int result;
315 
316 	mutex_lock(&resource->resource_lock);
317 	result = acpi_power_off_unlocked(resource);
318 	mutex_unlock(&resource->resource_lock);
319 	return result;
320 }
321 
322 static int acpi_power_off_list(struct list_head *list)
323 {
324 	struct acpi_power_resource_entry *entry;
325 	int result = 0;
326 
327 	list_for_each_entry_reverse(entry, list, node) {
328 		result = acpi_power_off(entry->resource);
329 		if (result)
330 			goto err;
331 	}
332 	return 0;
333 
334  err:
335 	list_for_each_entry_continue(entry, list, node)
336 		acpi_power_on(entry->resource);
337 
338 	return result;
339 }
340 
341 static int acpi_power_on_list(struct list_head *list)
342 {
343 	struct acpi_power_resource_entry *entry;
344 	int result = 0;
345 
346 	list_for_each_entry(entry, list, node) {
347 		result = acpi_power_on(entry->resource);
348 		if (result)
349 			goto err;
350 	}
351 	return 0;
352 
353  err:
354 	list_for_each_entry_continue_reverse(entry, list, node)
355 		acpi_power_off(entry->resource);
356 
357 	return result;
358 }
359 
360 static struct attribute *attrs[] = {
361 	NULL,
362 };
363 
364 static const struct attribute_group attr_groups[] = {
365 	[ACPI_STATE_D0] = {
366 		.name = "power_resources_D0",
367 		.attrs = attrs,
368 	},
369 	[ACPI_STATE_D1] = {
370 		.name = "power_resources_D1",
371 		.attrs = attrs,
372 	},
373 	[ACPI_STATE_D2] = {
374 		.name = "power_resources_D2",
375 		.attrs = attrs,
376 	},
377 	[ACPI_STATE_D3_HOT] = {
378 		.name = "power_resources_D3hot",
379 		.attrs = attrs,
380 	},
381 };
382 
383 static const struct attribute_group wakeup_attr_group = {
384 	.name = "power_resources_wakeup",
385 	.attrs = attrs,
386 };
387 
388 static void acpi_power_hide_list(struct acpi_device *adev,
389 				 struct list_head *resources,
390 				 const struct attribute_group *attr_group)
391 {
392 	struct acpi_power_resource_entry *entry;
393 
394 	if (list_empty(resources))
395 		return;
396 
397 	list_for_each_entry_reverse(entry, resources, node) {
398 		struct acpi_device *res_dev = &entry->resource->device;
399 
400 		sysfs_remove_link_from_group(&adev->dev.kobj,
401 					     attr_group->name,
402 					     dev_name(&res_dev->dev));
403 	}
404 	sysfs_remove_group(&adev->dev.kobj, attr_group);
405 }
406 
407 static void acpi_power_expose_list(struct acpi_device *adev,
408 				   struct list_head *resources,
409 				   const struct attribute_group *attr_group)
410 {
411 	struct acpi_power_resource_entry *entry;
412 	int ret;
413 
414 	if (list_empty(resources))
415 		return;
416 
417 	ret = sysfs_create_group(&adev->dev.kobj, attr_group);
418 	if (ret)
419 		return;
420 
421 	list_for_each_entry(entry, resources, node) {
422 		struct acpi_device *res_dev = &entry->resource->device;
423 
424 		ret = sysfs_add_link_to_group(&adev->dev.kobj,
425 					      attr_group->name,
426 					      &res_dev->dev.kobj,
427 					      dev_name(&res_dev->dev));
428 		if (ret) {
429 			acpi_power_hide_list(adev, resources, attr_group);
430 			break;
431 		}
432 	}
433 }
434 
435 static void acpi_power_expose_hide(struct acpi_device *adev,
436 				   struct list_head *resources,
437 				   const struct attribute_group *attr_group,
438 				   bool expose)
439 {
440 	if (expose)
441 		acpi_power_expose_list(adev, resources, attr_group);
442 	else
443 		acpi_power_hide_list(adev, resources, attr_group);
444 }
445 
446 void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
447 {
448 	int state;
449 
450 	if (adev->wakeup.flags.valid)
451 		acpi_power_expose_hide(adev, &adev->wakeup.resources,
452 				       &wakeup_attr_group, add);
453 
454 	if (!adev->power.flags.power_resources)
455 		return;
456 
457 	for (state = ACPI_STATE_D0; state <= ACPI_STATE_D3_HOT; state++)
458 		acpi_power_expose_hide(adev,
459 				       &adev->power.states[state].resources,
460 				       &attr_groups[state], add);
461 }
462 
463 int acpi_power_wakeup_list_init(struct list_head *list, int *system_level_p)
464 {
465 	struct acpi_power_resource_entry *entry;
466 	int system_level = 5;
467 
468 	list_for_each_entry(entry, list, node) {
469 		struct acpi_power_resource *resource = entry->resource;
470 		acpi_handle handle = resource->device.handle;
471 		int result;
472 		int state;
473 
474 		mutex_lock(&resource->resource_lock);
475 
476 		result = acpi_power_get_state(handle, &state);
477 		if (result) {
478 			mutex_unlock(&resource->resource_lock);
479 			return result;
480 		}
481 		if (state == ACPI_POWER_RESOURCE_STATE_ON) {
482 			resource->ref_count++;
483 			resource->wakeup_enabled = true;
484 		}
485 		if (system_level > resource->system_level)
486 			system_level = resource->system_level;
487 
488 		mutex_unlock(&resource->resource_lock);
489 	}
490 	*system_level_p = system_level;
491 	return 0;
492 }
493 
494 /* --------------------------------------------------------------------------
495                              Device Power Management
496    -------------------------------------------------------------------------- */
497 
498 /**
499  * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
500  *                          ACPI 3.0) _PSW (Power State Wake)
501  * @dev: Device to handle.
502  * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
503  * @sleep_state: Target sleep state of the system.
504  * @dev_state: Target power state of the device.
505  *
506  * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
507  * State Wake) for the device, if present.  On failure reset the device's
508  * wakeup.flags.valid flag.
509  *
510  * RETURN VALUE:
511  * 0 if either _DSW or _PSW has been successfully executed
512  * 0 if neither _DSW nor _PSW has been found
513  * -ENODEV if the execution of either _DSW or _PSW has failed
514  */
515 int acpi_device_sleep_wake(struct acpi_device *dev,
516                            int enable, int sleep_state, int dev_state)
517 {
518 	union acpi_object in_arg[3];
519 	struct acpi_object_list arg_list = { 3, in_arg };
520 	acpi_status status = AE_OK;
521 
522 	/*
523 	 * Try to execute _DSW first.
524 	 *
525 	 * Three arguments are needed for the _DSW object:
526 	 * Argument 0: enable/disable the wake capabilities
527 	 * Argument 1: target system state
528 	 * Argument 2: target device state
529 	 * When _DSW object is called to disable the wake capabilities, maybe
530 	 * the first argument is filled. The values of the other two arguments
531 	 * are meaningless.
532 	 */
533 	in_arg[0].type = ACPI_TYPE_INTEGER;
534 	in_arg[0].integer.value = enable;
535 	in_arg[1].type = ACPI_TYPE_INTEGER;
536 	in_arg[1].integer.value = sleep_state;
537 	in_arg[2].type = ACPI_TYPE_INTEGER;
538 	in_arg[2].integer.value = dev_state;
539 	status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
540 	if (ACPI_SUCCESS(status)) {
541 		return 0;
542 	} else if (status != AE_NOT_FOUND) {
543 		printk(KERN_ERR PREFIX "_DSW execution failed\n");
544 		dev->wakeup.flags.valid = 0;
545 		return -ENODEV;
546 	}
547 
548 	/* Execute _PSW */
549 	status = acpi_execute_simple_method(dev->handle, "_PSW", enable);
550 	if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
551 		printk(KERN_ERR PREFIX "_PSW execution failed\n");
552 		dev->wakeup.flags.valid = 0;
553 		return -ENODEV;
554 	}
555 
556 	return 0;
557 }
558 
559 /*
560  * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
561  * 1. Power on the power resources required for the wakeup device
562  * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
563  *    State Wake) for the device, if present
564  */
565 int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
566 {
567 	struct acpi_power_resource_entry *entry;
568 	int err = 0;
569 
570 	if (!dev || !dev->wakeup.flags.valid)
571 		return -EINVAL;
572 
573 	mutex_lock(&acpi_device_lock);
574 
575 	if (dev->wakeup.prepare_count++)
576 		goto out;
577 
578 	list_for_each_entry(entry, &dev->wakeup.resources, node) {
579 		struct acpi_power_resource *resource = entry->resource;
580 
581 		mutex_lock(&resource->resource_lock);
582 
583 		if (!resource->wakeup_enabled) {
584 			err = acpi_power_on_unlocked(resource);
585 			if (!err)
586 				resource->wakeup_enabled = true;
587 		}
588 
589 		mutex_unlock(&resource->resource_lock);
590 
591 		if (err) {
592 			dev_err(&dev->dev,
593 				"Cannot turn wakeup power resources on\n");
594 			dev->wakeup.flags.valid = 0;
595 			goto out;
596 		}
597 	}
598 	/*
599 	 * Passing 3 as the third argument below means the device may be
600 	 * put into arbitrary power state afterward.
601 	 */
602 	err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
603 	if (err)
604 		dev->wakeup.prepare_count = 0;
605 
606  out:
607 	mutex_unlock(&acpi_device_lock);
608 	return err;
609 }
610 
611 /*
612  * Shutdown a wakeup device, counterpart of above method
613  * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
614  *    State Wake) for the device, if present
615  * 2. Shutdown down the power resources
616  */
617 int acpi_disable_wakeup_device_power(struct acpi_device *dev)
618 {
619 	struct acpi_power_resource_entry *entry;
620 	int err = 0;
621 
622 	if (!dev || !dev->wakeup.flags.valid)
623 		return -EINVAL;
624 
625 	mutex_lock(&acpi_device_lock);
626 
627 	if (--dev->wakeup.prepare_count > 0)
628 		goto out;
629 
630 	/*
631 	 * Executing the code below even if prepare_count is already zero when
632 	 * the function is called may be useful, for example for initialisation.
633 	 */
634 	if (dev->wakeup.prepare_count < 0)
635 		dev->wakeup.prepare_count = 0;
636 
637 	err = acpi_device_sleep_wake(dev, 0, 0, 0);
638 	if (err)
639 		goto out;
640 
641 	list_for_each_entry(entry, &dev->wakeup.resources, node) {
642 		struct acpi_power_resource *resource = entry->resource;
643 
644 		mutex_lock(&resource->resource_lock);
645 
646 		if (resource->wakeup_enabled) {
647 			err = acpi_power_off_unlocked(resource);
648 			if (!err)
649 				resource->wakeup_enabled = false;
650 		}
651 
652 		mutex_unlock(&resource->resource_lock);
653 
654 		if (err) {
655 			dev_err(&dev->dev,
656 				"Cannot turn wakeup power resources off\n");
657 			dev->wakeup.flags.valid = 0;
658 			break;
659 		}
660 	}
661 
662  out:
663 	mutex_unlock(&acpi_device_lock);
664 	return err;
665 }
666 
667 int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
668 {
669 	int result = 0;
670 	int list_state = 0;
671 	int i = 0;
672 
673 	if (!device || !state)
674 		return -EINVAL;
675 
676 	/*
677 	 * We know a device's inferred power state when all the resources
678 	 * required for a given D-state are 'on'.
679 	 */
680 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
681 		struct list_head *list = &device->power.states[i].resources;
682 
683 		if (list_empty(list))
684 			continue;
685 
686 		result = acpi_power_get_list_state(list, &list_state);
687 		if (result)
688 			return result;
689 
690 		if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
691 			*state = i;
692 			return 0;
693 		}
694 	}
695 
696 	*state = device->power.states[ACPI_STATE_D3_COLD].flags.valid ?
697 		ACPI_STATE_D3_COLD : ACPI_STATE_D3_HOT;
698 	return 0;
699 }
700 
701 int acpi_power_on_resources(struct acpi_device *device, int state)
702 {
703 	if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT)
704 		return -EINVAL;
705 
706 	return acpi_power_on_list(&device->power.states[state].resources);
707 }
708 
709 int acpi_power_transition(struct acpi_device *device, int state)
710 {
711 	int result = 0;
712 
713 	if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
714 		return -EINVAL;
715 
716 	if (device->power.state == state || !device->flags.power_manageable)
717 		return 0;
718 
719 	if ((device->power.state < ACPI_STATE_D0)
720 	    || (device->power.state > ACPI_STATE_D3_COLD))
721 		return -ENODEV;
722 
723 	/*
724 	 * First we reference all power resources required in the target list
725 	 * (e.g. so the device doesn't lose power while transitioning).  Then,
726 	 * we dereference all power resources used in the current list.
727 	 */
728 	if (state < ACPI_STATE_D3_COLD)
729 		result = acpi_power_on_list(
730 			&device->power.states[state].resources);
731 
732 	if (!result && device->power.state < ACPI_STATE_D3_COLD)
733 		acpi_power_off_list(
734 			&device->power.states[device->power.state].resources);
735 
736 	/* We shouldn't change the state unless the above operations succeed. */
737 	device->power.state = result ? ACPI_STATE_UNKNOWN : state;
738 
739 	return result;
740 }
741 
742 static void acpi_release_power_resource(struct device *dev)
743 {
744 	struct acpi_device *device = to_acpi_device(dev);
745 	struct acpi_power_resource *resource;
746 
747 	resource = container_of(device, struct acpi_power_resource, device);
748 
749 	mutex_lock(&power_resource_list_lock);
750 	list_del(&resource->list_node);
751 	mutex_unlock(&power_resource_list_lock);
752 
753 	acpi_free_pnp_ids(&device->pnp);
754 	kfree(resource);
755 }
756 
757 static ssize_t acpi_power_in_use_show(struct device *dev,
758 				      struct device_attribute *attr,
759 				      char *buf) {
760 	struct acpi_power_resource *resource;
761 
762 	resource = to_power_resource(to_acpi_device(dev));
763 	return sprintf(buf, "%u\n", !!resource->ref_count);
764 }
765 static DEVICE_ATTR(resource_in_use, 0444, acpi_power_in_use_show, NULL);
766 
767 static void acpi_power_sysfs_remove(struct acpi_device *device)
768 {
769 	device_remove_file(&device->dev, &dev_attr_resource_in_use);
770 }
771 
772 static void acpi_power_add_resource_to_list(struct acpi_power_resource *resource)
773 {
774 	mutex_lock(&power_resource_list_lock);
775 
776 	if (!list_empty(&acpi_power_resource_list)) {
777 		struct acpi_power_resource *r;
778 
779 		list_for_each_entry(r, &acpi_power_resource_list, list_node)
780 			if (r->order > resource->order) {
781 				list_add_tail(&resource->list_node, &r->list_node);
782 				goto out;
783 			}
784 	}
785 	list_add_tail(&resource->list_node, &acpi_power_resource_list);
786 
787  out:
788 	mutex_unlock(&power_resource_list_lock);
789 }
790 
791 int acpi_add_power_resource(acpi_handle handle)
792 {
793 	struct acpi_power_resource *resource;
794 	struct acpi_device *device = NULL;
795 	union acpi_object acpi_object;
796 	struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
797 	acpi_status status;
798 	int state, result = -ENODEV;
799 
800 	acpi_bus_get_device(handle, &device);
801 	if (device)
802 		return 0;
803 
804 	resource = kzalloc(sizeof(*resource), GFP_KERNEL);
805 	if (!resource)
806 		return -ENOMEM;
807 
808 	device = &resource->device;
809 	acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER,
810 				ACPI_STA_DEFAULT);
811 	mutex_init(&resource->resource_lock);
812 	INIT_LIST_HEAD(&resource->list_node);
813 	resource->name = device->pnp.bus_id;
814 	strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
815 	strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
816 	device->power.state = ACPI_STATE_UNKNOWN;
817 
818 	/* Evalute the object to get the system level and resource order. */
819 	status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
820 	if (ACPI_FAILURE(status))
821 		goto err;
822 
823 	resource->system_level = acpi_object.power_resource.system_level;
824 	resource->order = acpi_object.power_resource.resource_order;
825 
826 	result = acpi_power_get_state(handle, &state);
827 	if (result)
828 		goto err;
829 
830 	printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
831 	       acpi_device_bid(device), state ? "on" : "off");
832 
833 	device->flags.match_driver = true;
834 	result = acpi_device_add(device, acpi_release_power_resource);
835 	if (result)
836 		goto err;
837 
838 	if (!device_create_file(&device->dev, &dev_attr_resource_in_use))
839 		device->remove = acpi_power_sysfs_remove;
840 
841 	acpi_power_add_resource_to_list(resource);
842 	acpi_device_add_finalize(device);
843 	return 0;
844 
845  err:
846 	acpi_release_power_resource(&device->dev);
847 	return result;
848 }
849 
850 #ifdef CONFIG_ACPI_SLEEP
851 void acpi_resume_power_resources(void)
852 {
853 	struct acpi_power_resource *resource;
854 
855 	mutex_lock(&power_resource_list_lock);
856 
857 	list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
858 		int result, state;
859 
860 		mutex_lock(&resource->resource_lock);
861 
862 		result = acpi_power_get_state(resource->device.handle, &state);
863 		if (result) {
864 			mutex_unlock(&resource->resource_lock);
865 			continue;
866 		}
867 
868 		if (state == ACPI_POWER_RESOURCE_STATE_OFF
869 		    && resource->ref_count) {
870 			dev_info(&resource->device.dev, "Turning ON\n");
871 			__acpi_power_on(resource);
872 		}
873 
874 		mutex_unlock(&resource->resource_lock);
875 	}
876 
877 	mutex_unlock(&power_resource_list_lock);
878 }
879 
880 void acpi_turn_off_unused_power_resources(void)
881 {
882 	struct acpi_power_resource *resource;
883 
884 	mutex_lock(&power_resource_list_lock);
885 
886 	list_for_each_entry_reverse(resource, &acpi_power_resource_list, list_node) {
887 		int result, state;
888 
889 		mutex_lock(&resource->resource_lock);
890 
891 		result = acpi_power_get_state(resource->device.handle, &state);
892 		if (result) {
893 			mutex_unlock(&resource->resource_lock);
894 			continue;
895 		}
896 
897 		if (state == ACPI_POWER_RESOURCE_STATE_ON
898 		    && !resource->ref_count) {
899 			dev_info(&resource->device.dev, "Turning OFF\n");
900 			__acpi_power_off(resource);
901 		}
902 
903 		mutex_unlock(&resource->resource_lock);
904 	}
905 
906 	mutex_unlock(&power_resource_list_lock);
907 }
908 #endif
909