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