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