aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/acpi/scan.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/acpi/scan.c')
-rw-r--r--drivers/acpi/scan.c976
1 files changed, 489 insertions, 487 deletions
diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c
index c88be6c37c30..daee7497efd3 100644
--- a/drivers/acpi/scan.c
+++ b/drivers/acpi/scan.c
@@ -29,29 +29,10 @@ extern struct acpi_device *acpi_root;
29 29
30static const char *dummy_hid = "device"; 30static const char *dummy_hid = "device";
31 31
32/*
33 * The following ACPI IDs are known to be suitable for representing as
34 * platform devices.
35 */
36static const struct acpi_device_id acpi_platform_device_ids[] = {
37
38 { "PNP0D40" },
39
40 /* Haswell LPSS devices */
41 { "INT33C0", 0 },
42 { "INT33C1", 0 },
43 { "INT33C2", 0 },
44 { "INT33C3", 0 },
45 { "INT33C4", 0 },
46 { "INT33C5", 0 },
47 { "INT33C6", 0 },
48 { "INT33C7", 0 },
49
50 { }
51};
52
53static LIST_HEAD(acpi_device_list); 32static LIST_HEAD(acpi_device_list);
54static LIST_HEAD(acpi_bus_id_list); 33static LIST_HEAD(acpi_bus_id_list);
34static DEFINE_MUTEX(acpi_scan_lock);
35static LIST_HEAD(acpi_scan_handlers_list);
55DEFINE_MUTEX(acpi_device_lock); 36DEFINE_MUTEX(acpi_device_lock);
56LIST_HEAD(acpi_wakeup_device_list); 37LIST_HEAD(acpi_wakeup_device_list);
57 38
@@ -61,6 +42,27 @@ struct acpi_device_bus_id{
61 struct list_head node; 42 struct list_head node;
62}; 43};
63 44
45void acpi_scan_lock_acquire(void)
46{
47 mutex_lock(&acpi_scan_lock);
48}
49EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
50
51void acpi_scan_lock_release(void)
52{
53 mutex_unlock(&acpi_scan_lock);
54}
55EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
56
57int acpi_scan_add_handler(struct acpi_scan_handler *handler)
58{
59 if (!handler || !handler->attach)
60 return -EINVAL;
61
62 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
63 return 0;
64}
65
64/* 66/*
65 * Creates hid/cid(s) string needed for modalias and uevent 67 * Creates hid/cid(s) string needed for modalias and uevent
66 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get: 68 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
@@ -115,39 +117,32 @@ static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
115 */ 117 */
116void acpi_bus_hot_remove_device(void *context) 118void acpi_bus_hot_remove_device(void *context)
117{ 119{
118 struct acpi_eject_event *ej_event = (struct acpi_eject_event *) context; 120 struct acpi_eject_event *ej_event = context;
119 struct acpi_device *device; 121 struct acpi_device *device = ej_event->device;
120 acpi_handle handle = ej_event->handle; 122 acpi_handle handle = device->handle;
121 acpi_handle temp; 123 acpi_handle temp;
122 struct acpi_object_list arg_list; 124 struct acpi_object_list arg_list;
123 union acpi_object arg; 125 union acpi_object arg;
124 acpi_status status = AE_OK; 126 acpi_status status = AE_OK;
125 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */ 127 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
126 128
127 if (acpi_bus_get_device(handle, &device)) 129 mutex_lock(&acpi_scan_lock);
128 goto err_out;
129 130
130 if (!device) 131 /* If there is no handle, the device node has been unregistered. */
131 goto err_out; 132 if (!device->handle) {
133 dev_dbg(&device->dev, "ACPI handle missing\n");
134 put_device(&device->dev);
135 goto out;
136 }
132 137
133 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 138 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
134 "Hot-removing device %s...\n", dev_name(&device->dev))); 139 "Hot-removing device %s...\n", dev_name(&device->dev)));
135 140
136 if (acpi_bus_trim(device, 1)) { 141 acpi_bus_trim(device);
137 printk(KERN_ERR PREFIX 142 /* Device node has been unregistered. */
138 "Removing device failed\n"); 143 put_device(&device->dev);
139 goto err_out;
140 }
141
142 /* device has been freed */
143 device = NULL; 144 device = NULL;
144 145
145 /* power off device */
146 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
147 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
148 printk(KERN_WARNING PREFIX
149 "Power-off device failed\n");
150
151 if (ACPI_SUCCESS(acpi_get_handle(handle, "_LCK", &temp))) { 146 if (ACPI_SUCCESS(acpi_get_handle(handle, "_LCK", &temp))) {
152 arg_list.count = 1; 147 arg_list.count = 1;
153 arg_list.pointer = &arg; 148 arg_list.pointer = &arg;
@@ -167,23 +162,46 @@ void acpi_bus_hot_remove_device(void *context)
167 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL); 162 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
168 if (ACPI_FAILURE(status)) { 163 if (ACPI_FAILURE(status)) {
169 if (status != AE_NOT_FOUND) 164 if (status != AE_NOT_FOUND)
170 printk(KERN_WARNING PREFIX 165 acpi_handle_warn(handle, "Eject failed\n");
171 "Eject device failed\n");
172 goto err_out;
173 }
174 166
175 kfree(context); 167 /* Tell the firmware the hot-remove operation has failed. */
176 return; 168 acpi_evaluate_hotplug_ost(handle, ej_event->event,
169 ost_code, NULL);
170 }
177 171
178err_out: 172 out:
179 /* Inform firmware the hot-remove operation has completed w/ error */ 173 mutex_unlock(&acpi_scan_lock);
180 (void) acpi_evaluate_hotplug_ost(handle,
181 ej_event->event, ost_code, NULL);
182 kfree(context); 174 kfree(context);
183 return; 175 return;
184} 176}
185EXPORT_SYMBOL(acpi_bus_hot_remove_device); 177EXPORT_SYMBOL(acpi_bus_hot_remove_device);
186 178
179static ssize_t real_power_state_show(struct device *dev,
180 struct device_attribute *attr, char *buf)
181{
182 struct acpi_device *adev = to_acpi_device(dev);
183 int state;
184 int ret;
185
186 ret = acpi_device_get_power(adev, &state);
187 if (ret)
188 return ret;
189
190 return sprintf(buf, "%s\n", acpi_power_state_string(state));
191}
192
193static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
194
195static ssize_t power_state_show(struct device *dev,
196 struct device_attribute *attr, char *buf)
197{
198 struct acpi_device *adev = to_acpi_device(dev);
199
200 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
201}
202
203static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
204
187static ssize_t 205static ssize_t
188acpi_eject_store(struct device *d, struct device_attribute *attr, 206acpi_eject_store(struct device *d, struct device_attribute *attr,
189 const char *buf, size_t count) 207 const char *buf, size_t count)
@@ -197,12 +215,10 @@ acpi_eject_store(struct device *d, struct device_attribute *attr,
197 if ((!count) || (buf[0] != '1')) { 215 if ((!count) || (buf[0] != '1')) {
198 return -EINVAL; 216 return -EINVAL;
199 } 217 }
200#ifndef FORCE_EJECT 218 if (!acpi_device->driver && !acpi_device->handler) {
201 if (acpi_device->driver == NULL) {
202 ret = -ENODEV; 219 ret = -ENODEV;
203 goto err; 220 goto err;
204 } 221 }
205#endif
206 status = acpi_get_type(acpi_device->handle, &type); 222 status = acpi_get_type(acpi_device->handle, &type);
207 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) { 223 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
208 ret = -ENODEV; 224 ret = -ENODEV;
@@ -215,7 +231,8 @@ acpi_eject_store(struct device *d, struct device_attribute *attr,
215 goto err; 231 goto err;
216 } 232 }
217 233
218 ej_event->handle = acpi_device->handle; 234 get_device(&acpi_device->dev);
235 ej_event->device = acpi_device;
219 if (acpi_device->flags.eject_pending) { 236 if (acpi_device->flags.eject_pending) {
220 /* event originated from ACPI eject notification */ 237 /* event originated from ACPI eject notification */
221 ej_event->event = ACPI_NOTIFY_EJECT_REQUEST; 238 ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
@@ -223,11 +240,15 @@ acpi_eject_store(struct device *d, struct device_attribute *attr,
223 } else { 240 } else {
224 /* event originated from user */ 241 /* event originated from user */
225 ej_event->event = ACPI_OST_EC_OSPM_EJECT; 242 ej_event->event = ACPI_OST_EC_OSPM_EJECT;
226 (void) acpi_evaluate_hotplug_ost(ej_event->handle, 243 (void) acpi_evaluate_hotplug_ost(acpi_device->handle,
227 ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL); 244 ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
228 } 245 }
229 246
230 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, (void *)ej_event); 247 status = acpi_os_hotplug_execute(acpi_bus_hot_remove_device, ej_event);
248 if (ACPI_FAILURE(status)) {
249 put_device(&acpi_device->dev);
250 kfree(ej_event);
251 }
231err: 252err:
232 return ret; 253 return ret;
233} 254}
@@ -375,8 +396,22 @@ static int acpi_device_setup_files(struct acpi_device *dev)
375 * hot-removal function from userland. 396 * hot-removal function from userland.
376 */ 397 */
377 status = acpi_get_handle(dev->handle, "_EJ0", &temp); 398 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
378 if (ACPI_SUCCESS(status)) 399 if (ACPI_SUCCESS(status)) {
379 result = device_create_file(&dev->dev, &dev_attr_eject); 400 result = device_create_file(&dev->dev, &dev_attr_eject);
401 if (result)
402 return result;
403 }
404
405 if (dev->flags.power_manageable) {
406 result = device_create_file(&dev->dev, &dev_attr_power_state);
407 if (result)
408 return result;
409
410 if (dev->power.flags.power_resources)
411 result = device_create_file(&dev->dev,
412 &dev_attr_real_power_state);
413 }
414
380end: 415end:
381 return result; 416 return result;
382} 417}
@@ -386,6 +421,13 @@ static void acpi_device_remove_files(struct acpi_device *dev)
386 acpi_status status; 421 acpi_status status;
387 acpi_handle temp; 422 acpi_handle temp;
388 423
424 if (dev->flags.power_manageable) {
425 device_remove_file(&dev->dev, &dev_attr_power_state);
426 if (dev->power.flags.power_resources)
427 device_remove_file(&dev->dev,
428 &dev_attr_real_power_state);
429 }
430
389 /* 431 /*
390 * If device has _STR, remove 'description' file 432 * If device has _STR, remove 'description' file
391 */ 433 */
@@ -454,9 +496,9 @@ const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
454 const struct device *dev) 496 const struct device *dev)
455{ 497{
456 struct acpi_device *adev; 498 struct acpi_device *adev;
499 acpi_handle handle = ACPI_HANDLE(dev);
457 500
458 if (!ids || !ACPI_HANDLE(dev) 501 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
459 || ACPI_FAILURE(acpi_bus_get_device(ACPI_HANDLE(dev), &adev)))
460 return NULL; 502 return NULL;
461 503
462 return __acpi_match_device(adev, ids); 504 return __acpi_match_device(adev, ids);
@@ -470,7 +512,7 @@ int acpi_match_device_ids(struct acpi_device *device,
470} 512}
471EXPORT_SYMBOL(acpi_match_device_ids); 513EXPORT_SYMBOL(acpi_match_device_ids);
472 514
473static void acpi_free_ids(struct acpi_device *device) 515void acpi_free_ids(struct acpi_device *device)
474{ 516{
475 struct acpi_hardware_id *id, *tmp; 517 struct acpi_hardware_id *id, *tmp;
476 518
@@ -478,6 +520,23 @@ static void acpi_free_ids(struct acpi_device *device)
478 kfree(id->id); 520 kfree(id->id);
479 kfree(id); 521 kfree(id);
480 } 522 }
523 kfree(device->pnp.unique_id);
524}
525
526static void acpi_free_power_resources_lists(struct acpi_device *device)
527{
528 int i;
529
530 if (device->wakeup.flags.valid)
531 acpi_power_resources_list_free(&device->wakeup.resources);
532
533 if (!device->flags.power_manageable)
534 return;
535
536 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
537 struct acpi_device_power_state *ps = &device->power.states[i];
538 acpi_power_resources_list_free(&ps->resources);
539 }
481} 540}
482 541
483static void acpi_device_release(struct device *dev) 542static void acpi_device_release(struct device *dev)
@@ -485,7 +544,7 @@ static void acpi_device_release(struct device *dev)
485 struct acpi_device *acpi_dev = to_acpi_device(dev); 544 struct acpi_device *acpi_dev = to_acpi_device(dev);
486 545
487 acpi_free_ids(acpi_dev); 546 acpi_free_ids(acpi_dev);
488 kfree(acpi_dev->pnp.unique_id); 547 acpi_free_power_resources_lists(acpi_dev);
489 kfree(acpi_dev); 548 kfree(acpi_dev);
490} 549}
491 550
@@ -494,7 +553,8 @@ static int acpi_bus_match(struct device *dev, struct device_driver *drv)
494 struct acpi_device *acpi_dev = to_acpi_device(dev); 553 struct acpi_device *acpi_dev = to_acpi_device(dev);
495 struct acpi_driver *acpi_drv = to_acpi_driver(drv); 554 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
496 555
497 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids); 556 return acpi_dev->flags.match_driver
557 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
498} 558}
499 559
500static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env) 560static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
@@ -570,7 +630,6 @@ static void acpi_device_remove_notify_handler(struct acpi_device *device)
570} 630}
571 631
572static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *); 632static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
573static int acpi_start_single_object(struct acpi_device *);
574static int acpi_device_probe(struct device * dev) 633static int acpi_device_probe(struct device * dev)
575{ 634{
576 struct acpi_device *acpi_dev = to_acpi_device(dev); 635 struct acpi_device *acpi_dev = to_acpi_device(dev);
@@ -579,15 +638,13 @@ static int acpi_device_probe(struct device * dev)
579 638
580 ret = acpi_bus_driver_init(acpi_dev, acpi_drv); 639 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
581 if (!ret) { 640 if (!ret) {
582 if (acpi_dev->bus_ops.acpi_op_start)
583 acpi_start_single_object(acpi_dev);
584
585 if (acpi_drv->ops.notify) { 641 if (acpi_drv->ops.notify) {
586 ret = acpi_device_install_notify_handler(acpi_dev); 642 ret = acpi_device_install_notify_handler(acpi_dev);
587 if (ret) { 643 if (ret) {
588 if (acpi_drv->ops.remove) 644 if (acpi_drv->ops.remove)
589 acpi_drv->ops.remove(acpi_dev, 645 acpi_drv->ops.remove(acpi_dev);
590 acpi_dev->removal_type); 646 acpi_dev->driver = NULL;
647 acpi_dev->driver_data = NULL;
591 return ret; 648 return ret;
592 } 649 }
593 } 650 }
@@ -609,7 +666,7 @@ static int acpi_device_remove(struct device * dev)
609 if (acpi_drv->ops.notify) 666 if (acpi_drv->ops.notify)
610 acpi_device_remove_notify_handler(acpi_dev); 667 acpi_device_remove_notify_handler(acpi_dev);
611 if (acpi_drv->ops.remove) 668 if (acpi_drv->ops.remove)
612 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type); 669 acpi_drv->ops.remove(acpi_dev);
613 } 670 }
614 acpi_dev->driver = NULL; 671 acpi_dev->driver = NULL;
615 acpi_dev->driver_data = NULL; 672 acpi_dev->driver_data = NULL;
@@ -626,12 +683,25 @@ struct bus_type acpi_bus_type = {
626 .uevent = acpi_device_uevent, 683 .uevent = acpi_device_uevent,
627}; 684};
628 685
629static int acpi_device_register(struct acpi_device *device) 686int acpi_device_add(struct acpi_device *device,
687 void (*release)(struct device *))
630{ 688{
631 int result; 689 int result;
632 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id; 690 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
633 int found = 0; 691 int found = 0;
634 692
693 if (device->handle) {
694 acpi_status status;
695
696 status = acpi_attach_data(device->handle, acpi_bus_data_handler,
697 device);
698 if (ACPI_FAILURE(status)) {
699 acpi_handle_err(device->handle,
700 "Unable to attach device data\n");
701 return -ENODEV;
702 }
703 }
704
635 /* 705 /*
636 * Linkage 706 * Linkage
637 * ------- 707 * -------
@@ -642,11 +712,13 @@ static int acpi_device_register(struct acpi_device *device)
642 INIT_LIST_HEAD(&device->wakeup_list); 712 INIT_LIST_HEAD(&device->wakeup_list);
643 INIT_LIST_HEAD(&device->physical_node_list); 713 INIT_LIST_HEAD(&device->physical_node_list);
644 mutex_init(&device->physical_node_lock); 714 mutex_init(&device->physical_node_lock);
715 INIT_LIST_HEAD(&device->power_dependent);
645 716
646 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL); 717 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
647 if (!new_bus_id) { 718 if (!new_bus_id) {
648 printk(KERN_ERR PREFIX "Memory allocation error\n"); 719 pr_err(PREFIX "Memory allocation error\n");
649 return -ENOMEM; 720 result = -ENOMEM;
721 goto err_detach;
650 } 722 }
651 723
652 mutex_lock(&acpi_device_lock); 724 mutex_lock(&acpi_device_lock);
@@ -681,11 +753,11 @@ static int acpi_device_register(struct acpi_device *device)
681 if (device->parent) 753 if (device->parent)
682 device->dev.parent = &device->parent->dev; 754 device->dev.parent = &device->parent->dev;
683 device->dev.bus = &acpi_bus_type; 755 device->dev.bus = &acpi_bus_type;
684 device->dev.release = &acpi_device_release; 756 device->dev.release = release;
685 result = device_register(&device->dev); 757 result = device_add(&device->dev);
686 if (result) { 758 if (result) {
687 dev_err(&device->dev, "Error registering device\n"); 759 dev_err(&device->dev, "Error registering device\n");
688 goto end; 760 goto err;
689 } 761 }
690 762
691 result = acpi_device_setup_files(device); 763 result = acpi_device_setup_files(device);
@@ -695,16 +767,20 @@ static int acpi_device_register(struct acpi_device *device)
695 767
696 device->removal_type = ACPI_BUS_REMOVAL_NORMAL; 768 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
697 return 0; 769 return 0;
698end: 770
771 err:
699 mutex_lock(&acpi_device_lock); 772 mutex_lock(&acpi_device_lock);
700 if (device->parent) 773 if (device->parent)
701 list_del(&device->node); 774 list_del(&device->node);
702 list_del(&device->wakeup_list); 775 list_del(&device->wakeup_list);
703 mutex_unlock(&acpi_device_lock); 776 mutex_unlock(&acpi_device_lock);
777
778 err_detach:
779 acpi_detach_data(device->handle, acpi_bus_data_handler);
704 return result; 780 return result;
705} 781}
706 782
707static void acpi_device_unregister(struct acpi_device *device, int type) 783static void acpi_device_unregister(struct acpi_device *device)
708{ 784{
709 mutex_lock(&acpi_device_lock); 785 mutex_lock(&acpi_device_lock);
710 if (device->parent) 786 if (device->parent)
@@ -715,8 +791,20 @@ static void acpi_device_unregister(struct acpi_device *device, int type)
715 791
716 acpi_detach_data(device->handle, acpi_bus_data_handler); 792 acpi_detach_data(device->handle, acpi_bus_data_handler);
717 793
794 acpi_power_add_remove_device(device, false);
718 acpi_device_remove_files(device); 795 acpi_device_remove_files(device);
719 device_unregister(&device->dev); 796 if (device->remove)
797 device->remove(device);
798
799 device_del(&device->dev);
800 /*
801 * Transition the device to D3cold to drop the reference counts of all
802 * power resources the device depends on and turn off the ones that have
803 * no more references.
804 */
805 acpi_device_set_power(device, ACPI_STATE_D3_COLD);
806 device->handle = NULL;
807 put_device(&device->dev);
720} 808}
721 809
722/* -------------------------------------------------------------------------- 810/* --------------------------------------------------------------------------
@@ -760,24 +848,6 @@ acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
760 return 0; 848 return 0;
761} 849}
762 850
763static int acpi_start_single_object(struct acpi_device *device)
764{
765 int result = 0;
766 struct acpi_driver *driver;
767
768
769 if (!(driver = device->driver))
770 return 0;
771
772 if (driver->ops.start) {
773 result = driver->ops.start(device);
774 if (result && driver->ops.remove)
775 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
776 }
777
778 return result;
779}
780
781/** 851/**
782 * acpi_bus_register_driver - register a driver with the ACPI bus 852 * acpi_bus_register_driver - register a driver with the ACPI bus
783 * @driver: driver being registered 853 * @driver: driver being registered
@@ -821,29 +891,23 @@ EXPORT_SYMBOL(acpi_bus_unregister_driver);
821 -------------------------------------------------------------------------- */ 891 -------------------------------------------------------------------------- */
822static struct acpi_device *acpi_bus_get_parent(acpi_handle handle) 892static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
823{ 893{
894 struct acpi_device *device = NULL;
824 acpi_status status; 895 acpi_status status;
825 int ret;
826 struct acpi_device *device;
827 896
828 /* 897 /*
829 * Fixed hardware devices do not appear in the namespace and do not 898 * Fixed hardware devices do not appear in the namespace and do not
830 * have handles, but we fabricate acpi_devices for them, so we have 899 * have handles, but we fabricate acpi_devices for them, so we have
831 * to deal with them specially. 900 * to deal with them specially.
832 */ 901 */
833 if (handle == NULL) 902 if (!handle)
834 return acpi_root; 903 return acpi_root;
835 904
836 do { 905 do {
837 status = acpi_get_parent(handle, &handle); 906 status = acpi_get_parent(handle, &handle);
838 if (status == AE_NULL_ENTRY)
839 return NULL;
840 if (ACPI_FAILURE(status)) 907 if (ACPI_FAILURE(status))
841 return acpi_root; 908 return status == AE_NULL_ENTRY ? NULL : acpi_root;
842 909 } while (acpi_bus_get_device(handle, &device));
843 ret = acpi_bus_get_device(handle, &device); 910 return device;
844 if (ret == 0)
845 return device;
846 } while (1);
847} 911}
848 912
849acpi_status 913acpi_status
@@ -877,52 +941,43 @@ void acpi_bus_data_handler(acpi_handle handle, void *context)
877 return; 941 return;
878} 942}
879 943
880static int acpi_bus_get_perf_flags(struct acpi_device *device) 944static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
881{ 945 struct acpi_device_wakeup *wakeup)
882 device->performance.state = ACPI_STATE_UNKNOWN;
883 return 0;
884}
885
886static acpi_status
887acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
888 struct acpi_device_wakeup *wakeup)
889{ 946{
890 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 947 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
891 union acpi_object *package = NULL; 948 union acpi_object *package = NULL;
892 union acpi_object *element = NULL; 949 union acpi_object *element = NULL;
893 acpi_status status; 950 acpi_status status;
894 int i = 0; 951 int err = -ENODATA;
895 952
896 if (!wakeup) 953 if (!wakeup)
897 return AE_BAD_PARAMETER; 954 return -EINVAL;
955
956 INIT_LIST_HEAD(&wakeup->resources);
898 957
899 /* _PRW */ 958 /* _PRW */
900 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer); 959 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
901 if (ACPI_FAILURE(status)) { 960 if (ACPI_FAILURE(status)) {
902 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW")); 961 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
903 return status; 962 return err;
904 } 963 }
905 964
906 package = (union acpi_object *)buffer.pointer; 965 package = (union acpi_object *)buffer.pointer;
907 966
908 if (!package || (package->package.count < 2)) { 967 if (!package || package->package.count < 2)
909 status = AE_BAD_DATA;
910 goto out; 968 goto out;
911 }
912 969
913 element = &(package->package.elements[0]); 970 element = &(package->package.elements[0]);
914 if (!element) { 971 if (!element)
915 status = AE_BAD_DATA;
916 goto out; 972 goto out;
917 } 973
918 if (element->type == ACPI_TYPE_PACKAGE) { 974 if (element->type == ACPI_TYPE_PACKAGE) {
919 if ((element->package.count < 2) || 975 if ((element->package.count < 2) ||
920 (element->package.elements[0].type != 976 (element->package.elements[0].type !=
921 ACPI_TYPE_LOCAL_REFERENCE) 977 ACPI_TYPE_LOCAL_REFERENCE)
922 || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) { 978 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
923 status = AE_BAD_DATA;
924 goto out; 979 goto out;
925 } 980
926 wakeup->gpe_device = 981 wakeup->gpe_device =
927 element->package.elements[0].reference.handle; 982 element->package.elements[0].reference.handle;
928 wakeup->gpe_number = 983 wakeup->gpe_number =
@@ -931,38 +986,35 @@ acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
931 wakeup->gpe_device = NULL; 986 wakeup->gpe_device = NULL;
932 wakeup->gpe_number = element->integer.value; 987 wakeup->gpe_number = element->integer.value;
933 } else { 988 } else {
934 status = AE_BAD_DATA;
935 goto out; 989 goto out;
936 } 990 }
937 991
938 element = &(package->package.elements[1]); 992 element = &(package->package.elements[1]);
939 if (element->type != ACPI_TYPE_INTEGER) { 993 if (element->type != ACPI_TYPE_INTEGER)
940 status = AE_BAD_DATA;
941 goto out; 994 goto out;
942 } 995
943 wakeup->sleep_state = element->integer.value; 996 wakeup->sleep_state = element->integer.value;
944 997
945 if ((package->package.count - 2) > ACPI_MAX_HANDLES) { 998 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
946 status = AE_NO_MEMORY; 999 if (err)
947 goto out; 1000 goto out;
948 }
949 wakeup->resources.count = package->package.count - 2;
950 for (i = 0; i < wakeup->resources.count; i++) {
951 element = &(package->package.elements[i + 2]);
952 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
953 status = AE_BAD_DATA;
954 goto out;
955 }
956 1001
957 wakeup->resources.handles[i] = element->reference.handle; 1002 if (!list_empty(&wakeup->resources)) {
958 } 1003 int sleep_state;
959 1004
1005 sleep_state = acpi_power_min_system_level(&wakeup->resources);
1006 if (sleep_state < wakeup->sleep_state) {
1007 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1008 "(S%d) by S%d from power resources\n",
1009 (int)wakeup->sleep_state, sleep_state);
1010 wakeup->sleep_state = sleep_state;
1011 }
1012 }
960 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number); 1013 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
961 1014
962 out: 1015 out:
963 kfree(buffer.pointer); 1016 kfree(buffer.pointer);
964 1017 return err;
965 return status;
966} 1018}
967 1019
968static void acpi_bus_set_run_wake_flags(struct acpi_device *device) 1020static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
@@ -1002,17 +1054,17 @@ static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1002{ 1054{
1003 acpi_handle temp; 1055 acpi_handle temp;
1004 acpi_status status = 0; 1056 acpi_status status = 0;
1005 int psw_error; 1057 int err;
1006 1058
1007 /* Presence of _PRW indicates wake capable */ 1059 /* Presence of _PRW indicates wake capable */
1008 status = acpi_get_handle(device->handle, "_PRW", &temp); 1060 status = acpi_get_handle(device->handle, "_PRW", &temp);
1009 if (ACPI_FAILURE(status)) 1061 if (ACPI_FAILURE(status))
1010 return; 1062 return;
1011 1063
1012 status = acpi_bus_extract_wakeup_device_power_package(device->handle, 1064 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1013 &device->wakeup); 1065 &device->wakeup);
1014 if (ACPI_FAILURE(status)) { 1066 if (err) {
1015 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package")); 1067 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
1016 return; 1068 return;
1017 } 1069 }
1018 1070
@@ -1025,20 +1077,73 @@ static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1025 * So it is necessary to call _DSW object first. Only when it is not 1077 * So it is necessary to call _DSW object first. Only when it is not
1026 * present will the _PSW object used. 1078 * present will the _PSW object used.
1027 */ 1079 */
1028 psw_error = acpi_device_sleep_wake(device, 0, 0, 0); 1080 err = acpi_device_sleep_wake(device, 0, 0, 0);
1029 if (psw_error) 1081 if (err)
1030 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 1082 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1031 "error in _DSW or _PSW evaluation\n")); 1083 "error in _DSW or _PSW evaluation\n"));
1032} 1084}
1033 1085
1034static void acpi_bus_add_power_resource(acpi_handle handle); 1086static void acpi_bus_init_power_state(struct acpi_device *device, int state)
1087{
1088 struct acpi_device_power_state *ps = &device->power.states[state];
1089 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1090 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1091 acpi_handle handle;
1092 acpi_status status;
1093
1094 INIT_LIST_HEAD(&ps->resources);
1095
1096 /* Evaluate "_PRx" to get referenced power resources */
1097 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1098 if (ACPI_SUCCESS(status)) {
1099 union acpi_object *package = buffer.pointer;
1100
1101 if (buffer.length && package
1102 && package->type == ACPI_TYPE_PACKAGE
1103 && package->package.count) {
1104 int err = acpi_extract_power_resources(package, 0,
1105 &ps->resources);
1106 if (!err)
1107 device->power.flags.power_resources = 1;
1108 }
1109 ACPI_FREE(buffer.pointer);
1110 }
1111
1112 /* Evaluate "_PSx" to see if we can do explicit sets */
1113 pathname[2] = 'S';
1114 status = acpi_get_handle(device->handle, pathname, &handle);
1115 if (ACPI_SUCCESS(status))
1116 ps->flags.explicit_set = 1;
1117
1118 /*
1119 * State is valid if there are means to put the device into it.
1120 * D3hot is only valid if _PR3 present.
1121 */
1122 if (!list_empty(&ps->resources)
1123 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1124 ps->flags.valid = 1;
1125 ps->flags.os_accessible = 1;
1126 }
1127
1128 ps->power = -1; /* Unknown - driver assigned */
1129 ps->latency = -1; /* Unknown - driver assigned */
1130}
1035 1131
1036static int acpi_bus_get_power_flags(struct acpi_device *device) 1132static void acpi_bus_get_power_flags(struct acpi_device *device)
1037{ 1133{
1038 acpi_status status = 0; 1134 acpi_status status;
1039 acpi_handle handle = NULL; 1135 acpi_handle handle;
1040 u32 i = 0; 1136 u32 i;
1137
1138 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1139 status = acpi_get_handle(device->handle, "_PS0", &handle);
1140 if (ACPI_FAILURE(status)) {
1141 status = acpi_get_handle(device->handle, "_PR0", &handle);
1142 if (ACPI_FAILURE(status))
1143 return;
1144 }
1041 1145
1146 device->flags.power_manageable = 1;
1042 1147
1043 /* 1148 /*
1044 * Power Management Flags 1149 * Power Management Flags
@@ -1053,40 +1158,10 @@ static int acpi_bus_get_power_flags(struct acpi_device *device)
1053 /* 1158 /*
1054 * Enumerate supported power management states 1159 * Enumerate supported power management states
1055 */ 1160 */
1056 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) { 1161 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1057 struct acpi_device_power_state *ps = &device->power.states[i]; 1162 acpi_bus_init_power_state(device, i);
1058 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
1059
1060 /* Evaluate "_PRx" to se if power resources are referenced */
1061 acpi_evaluate_reference(device->handle, object_name, NULL,
1062 &ps->resources);
1063 if (ps->resources.count) {
1064 int j;
1065
1066 device->power.flags.power_resources = 1;
1067 for (j = 0; j < ps->resources.count; j++)
1068 acpi_bus_add_power_resource(ps->resources.handles[j]);
1069 }
1070 1163
1071 /* Evaluate "_PSx" to see if we can do explicit sets */ 1164 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1072 object_name[2] = 'S';
1073 status = acpi_get_handle(device->handle, object_name, &handle);
1074 if (ACPI_SUCCESS(status))
1075 ps->flags.explicit_set = 1;
1076
1077 /*
1078 * State is valid if there are means to put the device into it.
1079 * D3hot is only valid if _PR3 present.
1080 */
1081 if (ps->resources.count ||
1082 (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT)) {
1083 ps->flags.valid = 1;
1084 ps->flags.os_accessible = 1;
1085 }
1086
1087 ps->power = -1; /* Unknown - driver assigned */
1088 ps->latency = -1; /* Unknown - driver assigned */
1089 }
1090 1165
1091 /* Set defaults for D0 and D3 states (always valid) */ 1166 /* Set defaults for D0 and D3 states (always valid) */
1092 device->power.states[ACPI_STATE_D0].flags.valid = 1; 1167 device->power.states[ACPI_STATE_D0].flags.valid = 1;
@@ -1103,17 +1178,17 @@ static int acpi_bus_get_power_flags(struct acpi_device *device)
1103 device->power.flags.power_resources) 1178 device->power.flags.power_resources)
1104 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1; 1179 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1105 1180
1106 acpi_bus_init_power(device); 1181 if (acpi_bus_init_power(device)) {
1107 1182 acpi_free_power_resources_lists(device);
1108 return 0; 1183 device->flags.power_manageable = 0;
1184 }
1109} 1185}
1110 1186
1111static int acpi_bus_get_flags(struct acpi_device *device) 1187static void acpi_bus_get_flags(struct acpi_device *device)
1112{ 1188{
1113 acpi_status status = AE_OK; 1189 acpi_status status = AE_OK;
1114 acpi_handle temp = NULL; 1190 acpi_handle temp = NULL;
1115 1191
1116
1117 /* Presence of _STA indicates 'dynamic_status' */ 1192 /* Presence of _STA indicates 'dynamic_status' */
1118 status = acpi_get_handle(device->handle, "_STA", &temp); 1193 status = acpi_get_handle(device->handle, "_STA", &temp);
1119 if (ACPI_SUCCESS(status)) 1194 if (ACPI_SUCCESS(status))
@@ -1133,21 +1208,6 @@ static int acpi_bus_get_flags(struct acpi_device *device)
1133 if (ACPI_SUCCESS(status)) 1208 if (ACPI_SUCCESS(status))
1134 device->flags.ejectable = 1; 1209 device->flags.ejectable = 1;
1135 } 1210 }
1136
1137 /* Power resources cannot be power manageable. */
1138 if (device->device_type == ACPI_BUS_TYPE_POWER)
1139 return 0;
1140
1141 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1142 status = acpi_get_handle(device->handle, "_PS0", &temp);
1143 if (ACPI_FAILURE(status))
1144 status = acpi_get_handle(device->handle, "_PR0", &temp);
1145 if (ACPI_SUCCESS(status))
1146 device->flags.power_manageable = 1;
1147
1148 /* TBD: Performance management */
1149
1150 return 0;
1151} 1211}
1152 1212
1153static void acpi_device_get_busid(struct acpi_device *device) 1213static void acpi_device_get_busid(struct acpi_device *device)
@@ -1372,56 +1432,32 @@ static void acpi_device_set_id(struct acpi_device *device)
1372 } 1432 }
1373} 1433}
1374 1434
1375static int acpi_device_set_context(struct acpi_device *device) 1435void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1436 int type, unsigned long long sta)
1376{ 1437{
1377 acpi_status status; 1438 INIT_LIST_HEAD(&device->pnp.ids);
1378 1439 device->device_type = type;
1379 /* 1440 device->handle = handle;
1380 * Context 1441 device->parent = acpi_bus_get_parent(handle);
1381 * ------- 1442 STRUCT_TO_INT(device->status) = sta;
1382 * Attach this 'struct acpi_device' to the ACPI object. This makes 1443 acpi_device_get_busid(device);
1383 * resolutions from handle->device very efficient. Fixed hardware 1444 acpi_device_set_id(device);
1384 * devices have no handles, so we skip them. 1445 acpi_bus_get_flags(device);
1385 */ 1446 device->flags.match_driver = false;
1386 if (!device->handle) 1447 device_initialize(&device->dev);
1387 return 0; 1448 dev_set_uevent_suppress(&device->dev, true);
1388
1389 status = acpi_attach_data(device->handle,
1390 acpi_bus_data_handler, device);
1391 if (ACPI_SUCCESS(status))
1392 return 0;
1393
1394 printk(KERN_ERR PREFIX "Error attaching device data\n");
1395 return -ENODEV;
1396} 1449}
1397 1450
1398static int acpi_bus_remove(struct acpi_device *dev, int rmdevice) 1451void acpi_device_add_finalize(struct acpi_device *device)
1399{ 1452{
1400 if (!dev) 1453 device->flags.match_driver = true;
1401 return -EINVAL; 1454 dev_set_uevent_suppress(&device->dev, false);
1402 1455 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1403 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1404 device_release_driver(&dev->dev);
1405
1406 if (!rmdevice)
1407 return 0;
1408
1409 /*
1410 * unbind _ADR-Based Devices when hot removal
1411 */
1412 if (dev->flags.bus_address) {
1413 if ((dev->parent) && (dev->parent->ops.unbind))
1414 dev->parent->ops.unbind(dev);
1415 }
1416 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1417
1418 return 0;
1419} 1456}
1420 1457
1421static int acpi_add_single_object(struct acpi_device **child, 1458static int acpi_add_single_object(struct acpi_device **child,
1422 acpi_handle handle, int type, 1459 acpi_handle handle, int type,
1423 unsigned long long sta, 1460 unsigned long long sta)
1424 struct acpi_bus_ops *ops)
1425{ 1461{
1426 int result; 1462 int result;
1427 struct acpi_device *device; 1463 struct acpi_device *device;
@@ -1433,102 +1469,25 @@ static int acpi_add_single_object(struct acpi_device **child,
1433 return -ENOMEM; 1469 return -ENOMEM;
1434 } 1470 }
1435 1471
1436 INIT_LIST_HEAD(&device->pnp.ids); 1472 acpi_init_device_object(device, handle, type, sta);
1437 device->device_type = type; 1473 acpi_bus_get_power_flags(device);
1438 device->handle = handle;
1439 device->parent = acpi_bus_get_parent(handle);
1440 device->bus_ops = *ops; /* workround for not call .start */
1441 STRUCT_TO_INT(device->status) = sta;
1442
1443 acpi_device_get_busid(device);
1444
1445 /*
1446 * Flags
1447 * -----
1448 * Note that we only look for object handles -- cannot evaluate objects
1449 * until we know the device is present and properly initialized.
1450 */
1451 result = acpi_bus_get_flags(device);
1452 if (result)
1453 goto end;
1454
1455 /*
1456 * Initialize Device
1457 * -----------------
1458 * TBD: Synch with Core's enumeration/initialization process.
1459 */
1460 acpi_device_set_id(device);
1461
1462 /*
1463 * Power Management
1464 * ----------------
1465 */
1466 if (device->flags.power_manageable) {
1467 result = acpi_bus_get_power_flags(device);
1468 if (result)
1469 goto end;
1470 }
1471
1472 /*
1473 * Wakeup device management
1474 *-----------------------
1475 */
1476 acpi_bus_get_wakeup_device_flags(device); 1474 acpi_bus_get_wakeup_device_flags(device);
1477 1475
1478 /* 1476 result = acpi_device_add(device, acpi_device_release);
1479 * Performance Management 1477 if (result) {
1480 * ----------------------
1481 */
1482 if (device->flags.performance_manageable) {
1483 result = acpi_bus_get_perf_flags(device);
1484 if (result)
1485 goto end;
1486 }
1487
1488 if ((result = acpi_device_set_context(device)))
1489 goto end;
1490
1491 result = acpi_device_register(device);
1492
1493 /*
1494 * Bind _ADR-Based Devices when hot add
1495 */
1496 if (device->flags.bus_address) {
1497 if (device->parent && device->parent->ops.bind)
1498 device->parent->ops.bind(device);
1499 }
1500
1501end:
1502 if (!result) {
1503 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1504 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1505 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1506 (char *) buffer.pointer,
1507 device->parent ? dev_name(&device->parent->dev) :
1508 "(null)"));
1509 kfree(buffer.pointer);
1510 *child = device;
1511 } else
1512 acpi_device_release(&device->dev); 1478 acpi_device_release(&device->dev);
1479 return result;
1480 }
1513 1481
1514 return result; 1482 acpi_power_add_remove_device(device, true);
1515} 1483 acpi_device_add_finalize(device);
1516 1484 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1517#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \ 1485 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1518 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING) 1486 dev_name(&device->dev), (char *) buffer.pointer,
1519 1487 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1520static void acpi_bus_add_power_resource(acpi_handle handle) 1488 kfree(buffer.pointer);
1521{ 1489 *child = device;
1522 struct acpi_bus_ops ops = { 1490 return 0;
1523 .acpi_op_add = 1,
1524 .acpi_op_start = 1,
1525 };
1526 struct acpi_device *device = NULL;
1527
1528 acpi_bus_get_device(handle, &device);
1529 if (!device)
1530 acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER,
1531 ACPI_STA_DEFAULT, &ops);
1532} 1491}
1533 1492
1534static int acpi_bus_type_and_status(acpi_handle handle, int *type, 1493static int acpi_bus_type_and_status(acpi_handle handle, int *type,
@@ -1570,218 +1529,248 @@ static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1570 return 0; 1529 return 0;
1571} 1530}
1572 1531
1573static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl, 1532static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1574 void *context, void **return_value) 1533 void *not_used, void **return_value)
1575{ 1534{
1576 struct acpi_bus_ops *ops = context; 1535 struct acpi_device *device = NULL;
1577 int type; 1536 int type;
1578 unsigned long long sta; 1537 unsigned long long sta;
1579 struct acpi_device *device;
1580 acpi_status status; 1538 acpi_status status;
1581 int result; 1539 int result;
1582 1540
1541 acpi_bus_get_device(handle, &device);
1542 if (device)
1543 goto out;
1544
1583 result = acpi_bus_type_and_status(handle, &type, &sta); 1545 result = acpi_bus_type_and_status(handle, &type, &sta);
1584 if (result) 1546 if (result)
1585 return AE_OK; 1547 return AE_OK;
1586 1548
1549 if (type == ACPI_BUS_TYPE_POWER) {
1550 acpi_add_power_resource(handle);
1551 return AE_OK;
1552 }
1553
1587 if (!(sta & ACPI_STA_DEVICE_PRESENT) && 1554 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1588 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) { 1555 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
1589 struct acpi_device_wakeup wakeup; 1556 struct acpi_device_wakeup wakeup;
1590 acpi_handle temp; 1557 acpi_handle temp;
1591 1558
1592 status = acpi_get_handle(handle, "_PRW", &temp); 1559 status = acpi_get_handle(handle, "_PRW", &temp);
1593 if (ACPI_SUCCESS(status)) 1560 if (ACPI_SUCCESS(status)) {
1594 acpi_bus_extract_wakeup_device_power_package(handle, 1561 acpi_bus_extract_wakeup_device_power_package(handle,
1595 &wakeup); 1562 &wakeup);
1563 acpi_power_resources_list_free(&wakeup.resources);
1564 }
1596 return AE_CTRL_DEPTH; 1565 return AE_CTRL_DEPTH;
1597 } 1566 }
1598 1567
1599 /* 1568 acpi_add_single_object(&device, handle, type, sta);
1600 * We may already have an acpi_device from a previous enumeration. If
1601 * so, we needn't add it again, but we may still have to start it.
1602 */
1603 device = NULL;
1604 acpi_bus_get_device(handle, &device);
1605 if (ops->acpi_op_add && !device) {
1606 acpi_add_single_object(&device, handle, type, sta, ops);
1607 /* Is the device a known good platform device? */
1608 if (device
1609 && !acpi_match_device_ids(device, acpi_platform_device_ids))
1610 acpi_create_platform_device(device);
1611 }
1612
1613 if (!device) 1569 if (!device)
1614 return AE_CTRL_DEPTH; 1570 return AE_CTRL_DEPTH;
1615 1571
1616 if (ops->acpi_op_start && !(ops->acpi_op_add)) { 1572 out:
1617 status = acpi_start_single_object(device);
1618 if (ACPI_FAILURE(status))
1619 return AE_CTRL_DEPTH;
1620 }
1621
1622 if (!*return_value) 1573 if (!*return_value)
1623 *return_value = device; 1574 *return_value = device;
1575
1624 return AE_OK; 1576 return AE_OK;
1625} 1577}
1626 1578
1627static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops, 1579static int acpi_scan_do_attach_handler(struct acpi_device *device, char *id)
1628 struct acpi_device **child)
1629{ 1580{
1630 acpi_status status; 1581 struct acpi_scan_handler *handler;
1631 void *device = NULL;
1632 1582
1633 status = acpi_bus_check_add(handle, 0, ops, &device); 1583 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node) {
1634 if (ACPI_SUCCESS(status)) 1584 const struct acpi_device_id *devid;
1635 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1636 acpi_bus_check_add, NULL, ops, &device);
1637 1585
1638 if (child) 1586 for (devid = handler->ids; devid->id[0]; devid++) {
1639 *child = device; 1587 int ret;
1640 1588
1641 if (device) 1589 if (strcmp((char *)devid->id, id))
1642 return 0; 1590 continue;
1643 else
1644 return -ENODEV;
1645}
1646 1591
1647/* 1592 ret = handler->attach(device, devid);
1648 * acpi_bus_add and acpi_bus_start 1593 if (ret > 0) {
1649 * 1594 device->handler = handler;
1650 * scan a given ACPI tree and (probably recently hot-plugged) 1595 return ret;
1651 * create and add or starts found devices. 1596 } else if (ret < 0) {
1652 * 1597 return ret;
1653 * If no devices were found -ENODEV is returned which does not 1598 }
1654 * mean that this is a real error, there just have been no suitable 1599 }
1655 * ACPI objects in the table trunk from which the kernel could create 1600 }
1656 * a device and add/start an appropriate driver. 1601 return 0;
1657 */ 1602}
1658 1603
1659int 1604static int acpi_scan_attach_handler(struct acpi_device *device)
1660acpi_bus_add(struct acpi_device **child,
1661 struct acpi_device *parent, acpi_handle handle, int type)
1662{ 1605{
1663 struct acpi_bus_ops ops; 1606 struct acpi_hardware_id *hwid;
1607 int ret = 0;
1664 1608
1665 memset(&ops, 0, sizeof(ops)); 1609 list_for_each_entry(hwid, &device->pnp.ids, list) {
1666 ops.acpi_op_add = 1; 1610 ret = acpi_scan_do_attach_handler(device, hwid->id);
1611 if (ret)
1612 break;
1667 1613
1668 return acpi_bus_scan(handle, &ops, child); 1614 }
1615 return ret;
1669} 1616}
1670EXPORT_SYMBOL(acpi_bus_add);
1671 1617
1672int acpi_bus_start(struct acpi_device *device) 1618static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1619 void *not_used, void **ret_not_used)
1673{ 1620{
1674 struct acpi_bus_ops ops; 1621 struct acpi_device *device;
1675 int result; 1622 unsigned long long sta_not_used;
1676 1623 int ret;
1677 if (!device)
1678 return -EINVAL;
1679 1624
1680 memset(&ops, 0, sizeof(ops)); 1625 /*
1681 ops.acpi_op_start = 1; 1626 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1627 * namespace walks prematurely.
1628 */
1629 if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
1630 return AE_OK;
1682 1631
1683 result = acpi_bus_scan(device->handle, &ops, NULL); 1632 if (acpi_bus_get_device(handle, &device))
1633 return AE_CTRL_DEPTH;
1684 1634
1685 acpi_update_all_gpes(); 1635 ret = acpi_scan_attach_handler(device);
1636 if (ret)
1637 return ret > 0 ? AE_OK : AE_CTRL_DEPTH;
1686 1638
1687 return result; 1639 ret = device_attach(&device->dev);
1640 return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
1688} 1641}
1689EXPORT_SYMBOL(acpi_bus_start);
1690 1642
1691int acpi_bus_trim(struct acpi_device *start, int rmdevice) 1643/**
1644 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1645 * @handle: Root of the namespace scope to scan.
1646 *
1647 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1648 * found devices.
1649 *
1650 * If no devices were found, -ENODEV is returned, but it does not mean that
1651 * there has been a real error. There just have been no suitable ACPI objects
1652 * in the table trunk from which the kernel could create a device and add an
1653 * appropriate driver.
1654 *
1655 * Must be called under acpi_scan_lock.
1656 */
1657int acpi_bus_scan(acpi_handle handle)
1692{ 1658{
1693 acpi_status status; 1659 void *device = NULL;
1694 struct acpi_device *parent, *child; 1660 int error = 0;
1695 acpi_handle phandle, chandle;
1696 acpi_object_type type;
1697 u32 level = 1;
1698 int err = 0;
1699 1661
1700 parent = start; 1662 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
1701 phandle = start->handle; 1663 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1702 child = chandle = NULL; 1664 acpi_bus_check_add, NULL, NULL, &device);
1703 1665
1704 while ((level > 0) && parent && (!err)) { 1666 if (!device)
1705 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle, 1667 error = -ENODEV;
1706 chandle, &chandle); 1668 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
1669 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1670 acpi_bus_device_attach, NULL, NULL, NULL);
1707 1671
1708 /* 1672 return error;
1709 * If this scope is exhausted then move our way back up. 1673}
1710 */ 1674EXPORT_SYMBOL(acpi_bus_scan);
1711 if (ACPI_FAILURE(status)) {
1712 level--;
1713 chandle = phandle;
1714 acpi_get_parent(phandle, &phandle);
1715 child = parent;
1716 parent = parent->parent;
1717
1718 if (level == 0)
1719 err = acpi_bus_remove(child, rmdevice);
1720 else
1721 err = acpi_bus_remove(child, 1);
1722 1675
1723 continue; 1676static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
1724 } 1677 void *not_used, void **ret_not_used)
1678{
1679 struct acpi_device *device = NULL;
1725 1680
1726 status = acpi_get_type(chandle, &type); 1681 if (!acpi_bus_get_device(handle, &device)) {
1727 if (ACPI_FAILURE(status)) { 1682 struct acpi_scan_handler *dev_handler = device->handler;
1728 continue; 1683
1729 } 1684 device->removal_type = ACPI_BUS_REMOVAL_EJECT;
1730 /* 1685 if (dev_handler) {
1731 * If there is a device corresponding to chandle then 1686 if (dev_handler->detach)
1732 * parse it (depth-first). 1687 dev_handler->detach(device);
1733 */ 1688
1734 if (acpi_bus_get_device(chandle, &child) == 0) { 1689 device->handler = NULL;
1735 level++; 1690 } else {
1736 phandle = chandle; 1691 device_release_driver(&device->dev);
1737 chandle = NULL;
1738 parent = child;
1739 } 1692 }
1740 continue;
1741 } 1693 }
1742 return err; 1694 return AE_OK;
1695}
1696
1697static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
1698 void *not_used, void **ret_not_used)
1699{
1700 struct acpi_device *device = NULL;
1701
1702 if (!acpi_bus_get_device(handle, &device))
1703 acpi_device_unregister(device);
1704
1705 return AE_OK;
1706}
1707
1708/**
1709 * acpi_bus_trim - Remove ACPI device node and all of its descendants
1710 * @start: Root of the ACPI device nodes subtree to remove.
1711 *
1712 * Must be called under acpi_scan_lock.
1713 */
1714void acpi_bus_trim(struct acpi_device *start)
1715{
1716 /*
1717 * Execute acpi_bus_device_detach() as a post-order callback to detach
1718 * all ACPI drivers from the device nodes being removed.
1719 */
1720 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1721 acpi_bus_device_detach, NULL, NULL);
1722 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
1723 /*
1724 * Execute acpi_bus_remove() as a post-order callback to remove device
1725 * nodes in the given namespace scope.
1726 */
1727 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1728 acpi_bus_remove, NULL, NULL);
1729 acpi_bus_remove(start->handle, 0, NULL, NULL);
1743} 1730}
1744EXPORT_SYMBOL_GPL(acpi_bus_trim); 1731EXPORT_SYMBOL_GPL(acpi_bus_trim);
1745 1732
1746static int acpi_bus_scan_fixed(void) 1733static int acpi_bus_scan_fixed(void)
1747{ 1734{
1748 int result = 0; 1735 int result = 0;
1749 struct acpi_device *device = NULL;
1750 struct acpi_bus_ops ops;
1751
1752 memset(&ops, 0, sizeof(ops));
1753 ops.acpi_op_add = 1;
1754 ops.acpi_op_start = 1;
1755 1736
1756 /* 1737 /*
1757 * Enumerate all fixed-feature devices. 1738 * Enumerate all fixed-feature devices.
1758 */ 1739 */
1759 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) { 1740 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
1741 struct acpi_device *device = NULL;
1742
1760 result = acpi_add_single_object(&device, NULL, 1743 result = acpi_add_single_object(&device, NULL,
1761 ACPI_BUS_TYPE_POWER_BUTTON, 1744 ACPI_BUS_TYPE_POWER_BUTTON,
1762 ACPI_STA_DEFAULT, 1745 ACPI_STA_DEFAULT);
1763 &ops); 1746 if (result)
1747 return result;
1748
1749 result = device_attach(&device->dev);
1750 if (result < 0)
1751 return result;
1752
1764 device_init_wakeup(&device->dev, true); 1753 device_init_wakeup(&device->dev, true);
1765 } 1754 }
1766 1755
1767 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) { 1756 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
1757 struct acpi_device *device = NULL;
1758
1768 result = acpi_add_single_object(&device, NULL, 1759 result = acpi_add_single_object(&device, NULL,
1769 ACPI_BUS_TYPE_SLEEP_BUTTON, 1760 ACPI_BUS_TYPE_SLEEP_BUTTON,
1770 ACPI_STA_DEFAULT, 1761 ACPI_STA_DEFAULT);
1771 &ops); 1762 if (result)
1763 return result;
1764
1765 result = device_attach(&device->dev);
1772 } 1766 }
1773 1767
1774 return result; 1768 return result < 0 ? result : 0;
1775} 1769}
1776 1770
1777int __init acpi_scan_init(void) 1771int __init acpi_scan_init(void)
1778{ 1772{
1779 int result; 1773 int result;
1780 struct acpi_bus_ops ops;
1781
1782 memset(&ops, 0, sizeof(ops));
1783 ops.acpi_op_add = 1;
1784 ops.acpi_op_start = 1;
1785 1774
1786 result = bus_register(&acpi_bus_type); 1775 result = bus_register(&acpi_bus_type);
1787 if (result) { 1776 if (result) {
@@ -1789,20 +1778,33 @@ int __init acpi_scan_init(void)
1789 printk(KERN_ERR PREFIX "Could not register bus type\n"); 1778 printk(KERN_ERR PREFIX "Could not register bus type\n");
1790 } 1779 }
1791 1780
1792 acpi_power_init(); 1781 acpi_pci_root_init();
1782 acpi_pci_link_init();
1783 acpi_platform_init();
1784 acpi_csrt_init();
1785 acpi_container_init();
1793 1786
1787 mutex_lock(&acpi_scan_lock);
1794 /* 1788 /*
1795 * Enumerate devices in the ACPI namespace. 1789 * Enumerate devices in the ACPI namespace.
1796 */ 1790 */
1797 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root); 1791 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
1798 1792 if (result)
1799 if (!result) 1793 goto out;
1800 result = acpi_bus_scan_fixed();
1801 1794
1795 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
1802 if (result) 1796 if (result)
1803 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL); 1797 goto out;
1804 else 1798
1805 acpi_update_all_gpes(); 1799 result = acpi_bus_scan_fixed();
1800 if (result) {
1801 acpi_device_unregister(acpi_root);
1802 goto out;
1803 }
1806 1804
1805 acpi_update_all_gpes();
1806
1807 out:
1808 mutex_unlock(&acpi_scan_lock);
1807 return result; 1809 return result;
1808} 1810}