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