0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024 #define pr_fmt(fmt) "ACPI: PM: " fmt
0025
0026 #include <linux/kernel.h>
0027 #include <linux/module.h>
0028 #include <linux/init.h>
0029 #include <linux/types.h>
0030 #include <linux/slab.h>
0031 #include <linux/pm_runtime.h>
0032 #include <linux/sysfs.h>
0033 #include <linux/acpi.h>
0034 #include "sleep.h"
0035 #include "internal.h"
0036
0037 #define ACPI_POWER_CLASS "power_resource"
0038 #define ACPI_POWER_DEVICE_NAME "Power Resource"
0039 #define ACPI_POWER_RESOURCE_STATE_OFF 0x00
0040 #define ACPI_POWER_RESOURCE_STATE_ON 0x01
0041 #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
0042
0043 struct acpi_power_dependent_device {
0044 struct device *dev;
0045 struct list_head node;
0046 };
0047
0048 struct acpi_power_resource {
0049 struct acpi_device device;
0050 struct list_head list_node;
0051 u32 system_level;
0052 u32 order;
0053 unsigned int ref_count;
0054 u8 state;
0055 struct mutex resource_lock;
0056 struct list_head dependents;
0057 };
0058
0059 struct acpi_power_resource_entry {
0060 struct list_head node;
0061 struct acpi_power_resource *resource;
0062 };
0063
0064 static LIST_HEAD(acpi_power_resource_list);
0065 static DEFINE_MUTEX(power_resource_list_lock);
0066
0067
0068
0069
0070
0071 static inline const char *resource_dev_name(struct acpi_power_resource *pr)
0072 {
0073 return dev_name(&pr->device.dev);
0074 }
0075
0076 static inline
0077 struct acpi_power_resource *to_power_resource(struct acpi_device *device)
0078 {
0079 return container_of(device, struct acpi_power_resource, device);
0080 }
0081
0082 static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle)
0083 {
0084 struct acpi_device *device = acpi_fetch_acpi_dev(handle);
0085
0086 if (!device)
0087 return NULL;
0088
0089 return to_power_resource(device);
0090 }
0091
0092 static int acpi_power_resources_list_add(acpi_handle handle,
0093 struct list_head *list)
0094 {
0095 struct acpi_power_resource *resource = acpi_power_get_context(handle);
0096 struct acpi_power_resource_entry *entry;
0097
0098 if (!resource || !list)
0099 return -EINVAL;
0100
0101 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
0102 if (!entry)
0103 return -ENOMEM;
0104
0105 entry->resource = resource;
0106 if (!list_empty(list)) {
0107 struct acpi_power_resource_entry *e;
0108
0109 list_for_each_entry(e, list, node)
0110 if (e->resource->order > resource->order) {
0111 list_add_tail(&entry->node, &e->node);
0112 return 0;
0113 }
0114 }
0115 list_add_tail(&entry->node, list);
0116 return 0;
0117 }
0118
0119 void acpi_power_resources_list_free(struct list_head *list)
0120 {
0121 struct acpi_power_resource_entry *entry, *e;
0122
0123 list_for_each_entry_safe(entry, e, list, node) {
0124 list_del(&entry->node);
0125 kfree(entry);
0126 }
0127 }
0128
0129 static bool acpi_power_resource_is_dup(union acpi_object *package,
0130 unsigned int start, unsigned int i)
0131 {
0132 acpi_handle rhandle, dup;
0133 unsigned int j;
0134
0135
0136 rhandle = package->package.elements[i].reference.handle;
0137 for (j = start; j < i; j++) {
0138 dup = package->package.elements[j].reference.handle;
0139 if (dup == rhandle)
0140 return true;
0141 }
0142
0143 return false;
0144 }
0145
0146 int acpi_extract_power_resources(union acpi_object *package, unsigned int start,
0147 struct list_head *list)
0148 {
0149 unsigned int i;
0150 int err = 0;
0151
0152 for (i = start; i < package->package.count; i++) {
0153 union acpi_object *element = &package->package.elements[i];
0154 struct acpi_device *rdev;
0155 acpi_handle rhandle;
0156
0157 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
0158 err = -ENODATA;
0159 break;
0160 }
0161 rhandle = element->reference.handle;
0162 if (!rhandle) {
0163 err = -ENODEV;
0164 break;
0165 }
0166
0167
0168 if (acpi_power_resource_is_dup(package, start, i))
0169 continue;
0170
0171 rdev = acpi_add_power_resource(rhandle);
0172 if (!rdev) {
0173 err = -ENODEV;
0174 break;
0175 }
0176 err = acpi_power_resources_list_add(rhandle, list);
0177 if (err)
0178 break;
0179 }
0180 if (err)
0181 acpi_power_resources_list_free(list);
0182
0183 return err;
0184 }
0185
0186 static int __get_state(acpi_handle handle, u8 *state)
0187 {
0188 acpi_status status = AE_OK;
0189 unsigned long long sta = 0;
0190 u8 cur_state;
0191
0192 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
0193 if (ACPI_FAILURE(status))
0194 return -ENODEV;
0195
0196 cur_state = sta & ACPI_POWER_RESOURCE_STATE_ON;
0197
0198 acpi_handle_debug(handle, "Power resource is %s\n",
0199 cur_state ? "on" : "off");
0200
0201 *state = cur_state;
0202 return 0;
0203 }
0204
0205 static int acpi_power_get_state(struct acpi_power_resource *resource, u8 *state)
0206 {
0207 if (resource->state == ACPI_POWER_RESOURCE_STATE_UNKNOWN) {
0208 int ret;
0209
0210 ret = __get_state(resource->device.handle, &resource->state);
0211 if (ret)
0212 return ret;
0213 }
0214
0215 *state = resource->state;
0216 return 0;
0217 }
0218
0219 static int acpi_power_get_list_state(struct list_head *list, u8 *state)
0220 {
0221 struct acpi_power_resource_entry *entry;
0222 u8 cur_state = ACPI_POWER_RESOURCE_STATE_OFF;
0223
0224 if (!list || !state)
0225 return -EINVAL;
0226
0227
0228 list_for_each_entry(entry, list, node) {
0229 struct acpi_power_resource *resource = entry->resource;
0230 int result;
0231
0232 mutex_lock(&resource->resource_lock);
0233 result = acpi_power_get_state(resource, &cur_state);
0234 mutex_unlock(&resource->resource_lock);
0235 if (result)
0236 return result;
0237
0238 if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
0239 break;
0240 }
0241
0242 pr_debug("Power resource list is %s\n", cur_state ? "on" : "off");
0243
0244 *state = cur_state;
0245 return 0;
0246 }
0247
0248 static int
0249 acpi_power_resource_add_dependent(struct acpi_power_resource *resource,
0250 struct device *dev)
0251 {
0252 struct acpi_power_dependent_device *dep;
0253 int ret = 0;
0254
0255 mutex_lock(&resource->resource_lock);
0256 list_for_each_entry(dep, &resource->dependents, node) {
0257
0258 if (dep->dev == dev)
0259 goto unlock;
0260 }
0261
0262 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
0263 if (!dep) {
0264 ret = -ENOMEM;
0265 goto unlock;
0266 }
0267
0268 dep->dev = dev;
0269 list_add_tail(&dep->node, &resource->dependents);
0270 dev_dbg(dev, "added power dependency to [%s]\n",
0271 resource_dev_name(resource));
0272
0273 unlock:
0274 mutex_unlock(&resource->resource_lock);
0275 return ret;
0276 }
0277
0278 static void
0279 acpi_power_resource_remove_dependent(struct acpi_power_resource *resource,
0280 struct device *dev)
0281 {
0282 struct acpi_power_dependent_device *dep;
0283
0284 mutex_lock(&resource->resource_lock);
0285 list_for_each_entry(dep, &resource->dependents, node) {
0286 if (dep->dev == dev) {
0287 list_del(&dep->node);
0288 kfree(dep);
0289 dev_dbg(dev, "removed power dependency to [%s]\n",
0290 resource_dev_name(resource));
0291 break;
0292 }
0293 }
0294 mutex_unlock(&resource->resource_lock);
0295 }
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312 int acpi_device_power_add_dependent(struct acpi_device *adev,
0313 struct device *dev)
0314 {
0315 struct acpi_power_resource_entry *entry;
0316 struct list_head *resources;
0317 int ret;
0318
0319 if (!adev->flags.power_manageable)
0320 return 0;
0321
0322 resources = &adev->power.states[ACPI_STATE_D0].resources;
0323 list_for_each_entry(entry, resources, node) {
0324 ret = acpi_power_resource_add_dependent(entry->resource, dev);
0325 if (ret)
0326 goto err;
0327 }
0328
0329 return 0;
0330
0331 err:
0332 list_for_each_entry(entry, resources, node)
0333 acpi_power_resource_remove_dependent(entry->resource, dev);
0334
0335 return ret;
0336 }
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347 void acpi_device_power_remove_dependent(struct acpi_device *adev,
0348 struct device *dev)
0349 {
0350 struct acpi_power_resource_entry *entry;
0351 struct list_head *resources;
0352
0353 if (!adev->flags.power_manageable)
0354 return;
0355
0356 resources = &adev->power.states[ACPI_STATE_D0].resources;
0357 list_for_each_entry_reverse(entry, resources, node)
0358 acpi_power_resource_remove_dependent(entry->resource, dev);
0359 }
0360
0361 static int __acpi_power_on(struct acpi_power_resource *resource)
0362 {
0363 acpi_handle handle = resource->device.handle;
0364 struct acpi_power_dependent_device *dep;
0365 acpi_status status = AE_OK;
0366
0367 status = acpi_evaluate_object(handle, "_ON", NULL, NULL);
0368 if (ACPI_FAILURE(status)) {
0369 resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN;
0370 return -ENODEV;
0371 }
0372
0373 resource->state = ACPI_POWER_RESOURCE_STATE_ON;
0374
0375 acpi_handle_debug(handle, "Power resource turned on\n");
0376
0377
0378
0379
0380
0381
0382 if (list_empty(&resource->dependents) ||
0383 list_is_singular(&resource->dependents))
0384 return 0;
0385
0386 list_for_each_entry(dep, &resource->dependents, node) {
0387 dev_dbg(dep->dev, "runtime resuming because [%s] turned on\n",
0388 resource_dev_name(resource));
0389 pm_request_resume(dep->dev);
0390 }
0391
0392 return 0;
0393 }
0394
0395 static int acpi_power_on_unlocked(struct acpi_power_resource *resource)
0396 {
0397 int result = 0;
0398
0399 if (resource->ref_count++) {
0400 acpi_handle_debug(resource->device.handle,
0401 "Power resource already on\n");
0402 } else {
0403 result = __acpi_power_on(resource);
0404 if (result)
0405 resource->ref_count--;
0406 }
0407 return result;
0408 }
0409
0410 static int acpi_power_on(struct acpi_power_resource *resource)
0411 {
0412 int result;
0413
0414 mutex_lock(&resource->resource_lock);
0415 result = acpi_power_on_unlocked(resource);
0416 mutex_unlock(&resource->resource_lock);
0417 return result;
0418 }
0419
0420 static int __acpi_power_off(struct acpi_power_resource *resource)
0421 {
0422 acpi_handle handle = resource->device.handle;
0423 acpi_status status;
0424
0425 status = acpi_evaluate_object(handle, "_OFF", NULL, NULL);
0426 if (ACPI_FAILURE(status)) {
0427 resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN;
0428 return -ENODEV;
0429 }
0430
0431 resource->state = ACPI_POWER_RESOURCE_STATE_OFF;
0432
0433 acpi_handle_debug(handle, "Power resource turned off\n");
0434
0435 return 0;
0436 }
0437
0438 static int acpi_power_off_unlocked(struct acpi_power_resource *resource)
0439 {
0440 int result = 0;
0441
0442 if (!resource->ref_count) {
0443 acpi_handle_debug(resource->device.handle,
0444 "Power resource already off\n");
0445 return 0;
0446 }
0447
0448 if (--resource->ref_count) {
0449 acpi_handle_debug(resource->device.handle,
0450 "Power resource still in use\n");
0451 } else {
0452 result = __acpi_power_off(resource);
0453 if (result)
0454 resource->ref_count++;
0455 }
0456 return result;
0457 }
0458
0459 static int acpi_power_off(struct acpi_power_resource *resource)
0460 {
0461 int result;
0462
0463 mutex_lock(&resource->resource_lock);
0464 result = acpi_power_off_unlocked(resource);
0465 mutex_unlock(&resource->resource_lock);
0466 return result;
0467 }
0468
0469 static int acpi_power_off_list(struct list_head *list)
0470 {
0471 struct acpi_power_resource_entry *entry;
0472 int result = 0;
0473
0474 list_for_each_entry_reverse(entry, list, node) {
0475 result = acpi_power_off(entry->resource);
0476 if (result)
0477 goto err;
0478 }
0479 return 0;
0480
0481 err:
0482 list_for_each_entry_continue(entry, list, node)
0483 acpi_power_on(entry->resource);
0484
0485 return result;
0486 }
0487
0488 static int acpi_power_on_list(struct list_head *list)
0489 {
0490 struct acpi_power_resource_entry *entry;
0491 int result = 0;
0492
0493 list_for_each_entry(entry, list, node) {
0494 result = acpi_power_on(entry->resource);
0495 if (result)
0496 goto err;
0497 }
0498 return 0;
0499
0500 err:
0501 list_for_each_entry_continue_reverse(entry, list, node)
0502 acpi_power_off(entry->resource);
0503
0504 return result;
0505 }
0506
0507 static struct attribute *attrs[] = {
0508 NULL,
0509 };
0510
0511 static const struct attribute_group attr_groups[] = {
0512 [ACPI_STATE_D0] = {
0513 .name = "power_resources_D0",
0514 .attrs = attrs,
0515 },
0516 [ACPI_STATE_D1] = {
0517 .name = "power_resources_D1",
0518 .attrs = attrs,
0519 },
0520 [ACPI_STATE_D2] = {
0521 .name = "power_resources_D2",
0522 .attrs = attrs,
0523 },
0524 [ACPI_STATE_D3_HOT] = {
0525 .name = "power_resources_D3hot",
0526 .attrs = attrs,
0527 },
0528 };
0529
0530 static const struct attribute_group wakeup_attr_group = {
0531 .name = "power_resources_wakeup",
0532 .attrs = attrs,
0533 };
0534
0535 static void acpi_power_hide_list(struct acpi_device *adev,
0536 struct list_head *resources,
0537 const struct attribute_group *attr_group)
0538 {
0539 struct acpi_power_resource_entry *entry;
0540
0541 if (list_empty(resources))
0542 return;
0543
0544 list_for_each_entry_reverse(entry, resources, node) {
0545 struct acpi_device *res_dev = &entry->resource->device;
0546
0547 sysfs_remove_link_from_group(&adev->dev.kobj,
0548 attr_group->name,
0549 dev_name(&res_dev->dev));
0550 }
0551 sysfs_remove_group(&adev->dev.kobj, attr_group);
0552 }
0553
0554 static void acpi_power_expose_list(struct acpi_device *adev,
0555 struct list_head *resources,
0556 const struct attribute_group *attr_group)
0557 {
0558 struct acpi_power_resource_entry *entry;
0559 int ret;
0560
0561 if (list_empty(resources))
0562 return;
0563
0564 ret = sysfs_create_group(&adev->dev.kobj, attr_group);
0565 if (ret)
0566 return;
0567
0568 list_for_each_entry(entry, resources, node) {
0569 struct acpi_device *res_dev = &entry->resource->device;
0570
0571 ret = sysfs_add_link_to_group(&adev->dev.kobj,
0572 attr_group->name,
0573 &res_dev->dev.kobj,
0574 dev_name(&res_dev->dev));
0575 if (ret) {
0576 acpi_power_hide_list(adev, resources, attr_group);
0577 break;
0578 }
0579 }
0580 }
0581
0582 static void acpi_power_expose_hide(struct acpi_device *adev,
0583 struct list_head *resources,
0584 const struct attribute_group *attr_group,
0585 bool expose)
0586 {
0587 if (expose)
0588 acpi_power_expose_list(adev, resources, attr_group);
0589 else
0590 acpi_power_hide_list(adev, resources, attr_group);
0591 }
0592
0593 void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
0594 {
0595 int state;
0596
0597 if (adev->wakeup.flags.valid)
0598 acpi_power_expose_hide(adev, &adev->wakeup.resources,
0599 &wakeup_attr_group, add);
0600
0601 if (!adev->power.flags.power_resources)
0602 return;
0603
0604 for (state = ACPI_STATE_D0; state <= ACPI_STATE_D3_HOT; state++)
0605 acpi_power_expose_hide(adev,
0606 &adev->power.states[state].resources,
0607 &attr_groups[state], add);
0608 }
0609
0610 int acpi_power_wakeup_list_init(struct list_head *list, int *system_level_p)
0611 {
0612 struct acpi_power_resource_entry *entry;
0613 int system_level = 5;
0614
0615 list_for_each_entry(entry, list, node) {
0616 struct acpi_power_resource *resource = entry->resource;
0617 u8 state;
0618
0619 mutex_lock(&resource->resource_lock);
0620
0621
0622
0623
0624
0625 if (!resource->ref_count &&
0626 !acpi_power_get_state(resource, &state) &&
0627 state == ACPI_POWER_RESOURCE_STATE_ON)
0628 __acpi_power_off(resource);
0629
0630 if (system_level > resource->system_level)
0631 system_level = resource->system_level;
0632
0633 mutex_unlock(&resource->resource_lock);
0634 }
0635 *system_level_p = system_level;
0636 return 0;
0637 }
0638
0639
0640
0641
0642
0643
0644
0645
0646
0647
0648
0649
0650
0651
0652
0653
0654
0655
0656
0657
0658
0659
0660 int acpi_device_sleep_wake(struct acpi_device *dev,
0661 int enable, int sleep_state, int dev_state)
0662 {
0663 union acpi_object in_arg[3];
0664 struct acpi_object_list arg_list = { 3, in_arg };
0665 acpi_status status = AE_OK;
0666
0667
0668
0669
0670
0671
0672
0673
0674
0675
0676
0677
0678 in_arg[0].type = ACPI_TYPE_INTEGER;
0679 in_arg[0].integer.value = enable;
0680 in_arg[1].type = ACPI_TYPE_INTEGER;
0681 in_arg[1].integer.value = sleep_state;
0682 in_arg[2].type = ACPI_TYPE_INTEGER;
0683 in_arg[2].integer.value = dev_state;
0684 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
0685 if (ACPI_SUCCESS(status)) {
0686 return 0;
0687 } else if (status != AE_NOT_FOUND) {
0688 acpi_handle_info(dev->handle, "_DSW execution failed\n");
0689 dev->wakeup.flags.valid = 0;
0690 return -ENODEV;
0691 }
0692
0693
0694 status = acpi_execute_simple_method(dev->handle, "_PSW", enable);
0695 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
0696 acpi_handle_info(dev->handle, "_PSW execution failed\n");
0697 dev->wakeup.flags.valid = 0;
0698 return -ENODEV;
0699 }
0700
0701 return 0;
0702 }
0703
0704
0705
0706
0707
0708
0709
0710 int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
0711 {
0712 int err = 0;
0713
0714 if (!dev || !dev->wakeup.flags.valid)
0715 return -EINVAL;
0716
0717 mutex_lock(&acpi_device_lock);
0718
0719 dev_dbg(&dev->dev, "Enabling wakeup power (count %d)\n",
0720 dev->wakeup.prepare_count);
0721
0722 if (dev->wakeup.prepare_count++)
0723 goto out;
0724
0725 err = acpi_power_on_list(&dev->wakeup.resources);
0726 if (err) {
0727 dev_err(&dev->dev, "Cannot turn on wakeup power resources\n");
0728 dev->wakeup.flags.valid = 0;
0729 goto out;
0730 }
0731
0732
0733
0734
0735
0736 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
0737 if (err) {
0738 acpi_power_off_list(&dev->wakeup.resources);
0739 dev->wakeup.prepare_count = 0;
0740 goto out;
0741 }
0742
0743 dev_dbg(&dev->dev, "Wakeup power enabled\n");
0744
0745 out:
0746 mutex_unlock(&acpi_device_lock);
0747 return err;
0748 }
0749
0750
0751
0752
0753
0754
0755
0756 int acpi_disable_wakeup_device_power(struct acpi_device *dev)
0757 {
0758 struct acpi_power_resource_entry *entry;
0759 int err = 0;
0760
0761 if (!dev || !dev->wakeup.flags.valid)
0762 return -EINVAL;
0763
0764 mutex_lock(&acpi_device_lock);
0765
0766 dev_dbg(&dev->dev, "Disabling wakeup power (count %d)\n",
0767 dev->wakeup.prepare_count);
0768
0769
0770 if (dev->wakeup.prepare_count <= 0)
0771 goto out;
0772
0773 if (--dev->wakeup.prepare_count > 0)
0774 goto out;
0775
0776 err = acpi_device_sleep_wake(dev, 0, 0, 0);
0777 if (err)
0778 goto out;
0779
0780
0781
0782
0783
0784 list_for_each_entry(entry, &dev->wakeup.resources, node) {
0785 int ret;
0786
0787 ret = acpi_power_off(entry->resource);
0788 if (ret && !err)
0789 err = ret;
0790 }
0791 if (err) {
0792 dev_err(&dev->dev, "Cannot turn off wakeup power resources\n");
0793 dev->wakeup.flags.valid = 0;
0794 goto out;
0795 }
0796
0797 dev_dbg(&dev->dev, "Wakeup power disabled\n");
0798
0799 out:
0800 mutex_unlock(&acpi_device_lock);
0801 return err;
0802 }
0803
0804 int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
0805 {
0806 u8 list_state = ACPI_POWER_RESOURCE_STATE_OFF;
0807 int result = 0;
0808 int i = 0;
0809
0810 if (!device || !state)
0811 return -EINVAL;
0812
0813
0814
0815
0816
0817 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
0818 struct list_head *list = &device->power.states[i].resources;
0819
0820 if (list_empty(list))
0821 continue;
0822
0823 result = acpi_power_get_list_state(list, &list_state);
0824 if (result)
0825 return result;
0826
0827 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
0828 *state = i;
0829 return 0;
0830 }
0831 }
0832
0833 *state = device->power.states[ACPI_STATE_D3_COLD].flags.valid ?
0834 ACPI_STATE_D3_COLD : ACPI_STATE_D3_HOT;
0835 return 0;
0836 }
0837
0838 int acpi_power_on_resources(struct acpi_device *device, int state)
0839 {
0840 if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT)
0841 return -EINVAL;
0842
0843 return acpi_power_on_list(&device->power.states[state].resources);
0844 }
0845
0846 int acpi_power_transition(struct acpi_device *device, int state)
0847 {
0848 int result = 0;
0849
0850 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
0851 return -EINVAL;
0852
0853 if (device->power.state == state || !device->flags.power_manageable)
0854 return 0;
0855
0856 if ((device->power.state < ACPI_STATE_D0)
0857 || (device->power.state > ACPI_STATE_D3_COLD))
0858 return -ENODEV;
0859
0860
0861
0862
0863
0864
0865 if (state < ACPI_STATE_D3_COLD)
0866 result = acpi_power_on_list(
0867 &device->power.states[state].resources);
0868
0869 if (!result && device->power.state < ACPI_STATE_D3_COLD)
0870 acpi_power_off_list(
0871 &device->power.states[device->power.state].resources);
0872
0873
0874 device->power.state = result ? ACPI_STATE_UNKNOWN : state;
0875
0876 return result;
0877 }
0878
0879 static void acpi_release_power_resource(struct device *dev)
0880 {
0881 struct acpi_device *device = to_acpi_device(dev);
0882 struct acpi_power_resource *resource;
0883
0884 resource = container_of(device, struct acpi_power_resource, device);
0885
0886 mutex_lock(&power_resource_list_lock);
0887 list_del(&resource->list_node);
0888 mutex_unlock(&power_resource_list_lock);
0889
0890 acpi_free_pnp_ids(&device->pnp);
0891 kfree(resource);
0892 }
0893
0894 static ssize_t resource_in_use_show(struct device *dev,
0895 struct device_attribute *attr,
0896 char *buf)
0897 {
0898 struct acpi_power_resource *resource;
0899
0900 resource = to_power_resource(to_acpi_device(dev));
0901 return sprintf(buf, "%u\n", !!resource->ref_count);
0902 }
0903 static DEVICE_ATTR_RO(resource_in_use);
0904
0905 static void acpi_power_sysfs_remove(struct acpi_device *device)
0906 {
0907 device_remove_file(&device->dev, &dev_attr_resource_in_use);
0908 }
0909
0910 static void acpi_power_add_resource_to_list(struct acpi_power_resource *resource)
0911 {
0912 mutex_lock(&power_resource_list_lock);
0913
0914 if (!list_empty(&acpi_power_resource_list)) {
0915 struct acpi_power_resource *r;
0916
0917 list_for_each_entry(r, &acpi_power_resource_list, list_node)
0918 if (r->order > resource->order) {
0919 list_add_tail(&resource->list_node, &r->list_node);
0920 goto out;
0921 }
0922 }
0923 list_add_tail(&resource->list_node, &acpi_power_resource_list);
0924
0925 out:
0926 mutex_unlock(&power_resource_list_lock);
0927 }
0928
0929 struct acpi_device *acpi_add_power_resource(acpi_handle handle)
0930 {
0931 struct acpi_device *device = acpi_fetch_acpi_dev(handle);
0932 struct acpi_power_resource *resource;
0933 union acpi_object acpi_object;
0934 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
0935 acpi_status status;
0936 u8 state_dummy;
0937 int result;
0938
0939 if (device)
0940 return device;
0941
0942 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
0943 if (!resource)
0944 return NULL;
0945
0946 device = &resource->device;
0947 acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER);
0948 mutex_init(&resource->resource_lock);
0949 INIT_LIST_HEAD(&resource->list_node);
0950 INIT_LIST_HEAD(&resource->dependents);
0951 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
0952 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
0953 device->power.state = ACPI_STATE_UNKNOWN;
0954
0955
0956 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
0957 if (ACPI_FAILURE(status))
0958 goto err;
0959
0960 resource->system_level = acpi_object.power_resource.system_level;
0961 resource->order = acpi_object.power_resource.resource_order;
0962 resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN;
0963
0964
0965 if (acpi_power_get_state(resource, &state_dummy))
0966 __acpi_power_on(resource);
0967
0968 pr_info("%s [%s]\n", acpi_device_name(device), acpi_device_bid(device));
0969
0970 device->flags.match_driver = true;
0971 result = acpi_device_add(device, acpi_release_power_resource);
0972 if (result)
0973 goto err;
0974
0975 if (!device_create_file(&device->dev, &dev_attr_resource_in_use))
0976 device->remove = acpi_power_sysfs_remove;
0977
0978 acpi_power_add_resource_to_list(resource);
0979 acpi_device_add_finalize(device);
0980 return device;
0981
0982 err:
0983 acpi_release_power_resource(&device->dev);
0984 return NULL;
0985 }
0986
0987 #ifdef CONFIG_ACPI_SLEEP
0988 void acpi_resume_power_resources(void)
0989 {
0990 struct acpi_power_resource *resource;
0991
0992 mutex_lock(&power_resource_list_lock);
0993
0994 list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
0995 int result;
0996 u8 state;
0997
0998 mutex_lock(&resource->resource_lock);
0999
1000 resource->state = ACPI_POWER_RESOURCE_STATE_UNKNOWN;
1001 result = acpi_power_get_state(resource, &state);
1002 if (result) {
1003 mutex_unlock(&resource->resource_lock);
1004 continue;
1005 }
1006
1007 if (state == ACPI_POWER_RESOURCE_STATE_OFF
1008 && resource->ref_count) {
1009 acpi_handle_debug(resource->device.handle, "Turning ON\n");
1010 __acpi_power_on(resource);
1011 }
1012
1013 mutex_unlock(&resource->resource_lock);
1014 }
1015
1016 mutex_unlock(&power_resource_list_lock);
1017 }
1018 #endif
1019
1020
1021
1022
1023 void acpi_turn_off_unused_power_resources(void)
1024 {
1025 struct acpi_power_resource *resource;
1026
1027 mutex_lock(&power_resource_list_lock);
1028
1029 list_for_each_entry_reverse(resource, &acpi_power_resource_list, list_node) {
1030 mutex_lock(&resource->resource_lock);
1031
1032 if (!resource->ref_count &&
1033 resource->state == ACPI_POWER_RESOURCE_STATE_ON) {
1034 acpi_handle_debug(resource->device.handle, "Turning OFF\n");
1035 __acpi_power_off(resource);
1036 }
1037
1038 mutex_unlock(&resource->resource_lock);
1039 }
1040
1041 mutex_unlock(&power_resource_list_lock);
1042 }