Merge branch 'tracing-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / drivers / acpi / scan.c
1 /*
2  * scan.c - support for transforming the ACPI namespace into individual objects
3  */
4
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/kernel.h>
8 #include <linux/acpi.h>
9 #include <linux/signal.h>
10 #include <linux/kthread.h>
11
12 #include <acpi/acpi_drivers.h>
13
14 #include "internal.h"
15
16 #define _COMPONENT              ACPI_BUS_COMPONENT
17 ACPI_MODULE_NAME("scan");
18 #define STRUCT_TO_INT(s)        (*((int*)&s))
19 extern struct acpi_device *acpi_root;
20
21 #define ACPI_BUS_CLASS                  "system_bus"
22 #define ACPI_BUS_HID                    "LNXSYBUS"
23 #define ACPI_BUS_DEVICE_NAME            "System Bus"
24
25 #define ACPI_IS_ROOT_DEVICE(device)    (!(device)->parent)
26
27 static LIST_HEAD(acpi_device_list);
28 static LIST_HEAD(acpi_bus_id_list);
29 DEFINE_MUTEX(acpi_device_lock);
30 LIST_HEAD(acpi_wakeup_device_list);
31
32 struct acpi_device_bus_id{
33         char bus_id[15];
34         unsigned int instance_no;
35         struct list_head node;
36 };
37
38 /*
39  * Creates hid/cid(s) string needed for modalias and uevent
40  * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
41  * char *modalias: "acpi:IBM0001:ACPI0001"
42 */
43 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
44                            int size)
45 {
46         int len;
47         int count;
48         struct acpi_hardware_id *id;
49
50         len = snprintf(modalias, size, "acpi:");
51         size -= len;
52
53         list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
54                 count = snprintf(&modalias[len], size, "%s:", id->id);
55                 if (count < 0 || count >= size)
56                         return -EINVAL;
57                 len += count;
58                 size -= count;
59         }
60
61         modalias[len] = '\0';
62         return len;
63 }
64
65 static ssize_t
66 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
67         struct acpi_device *acpi_dev = to_acpi_device(dev);
68         int len;
69
70         /* Device has no HID and no CID or string is >1024 */
71         len = create_modalias(acpi_dev, buf, 1024);
72         if (len <= 0)
73                 return 0;
74         buf[len++] = '\n';
75         return len;
76 }
77 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
78
79 static void acpi_bus_hot_remove_device(void *context)
80 {
81         struct acpi_device *device;
82         acpi_handle handle = context;
83         struct acpi_object_list arg_list;
84         union acpi_object arg;
85         acpi_status status = AE_OK;
86
87         if (acpi_bus_get_device(handle, &device))
88                 return;
89
90         if (!device)
91                 return;
92
93         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
94                 "Hot-removing device %s...\n", dev_name(&device->dev)));
95
96         if (acpi_bus_trim(device, 1)) {
97                 printk(KERN_ERR PREFIX
98                                 "Removing device failed\n");
99                 return;
100         }
101
102         /* power off device */
103         status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
104         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
105                 printk(KERN_WARNING PREFIX
106                                 "Power-off device failed\n");
107
108         if (device->flags.lockable) {
109                 arg_list.count = 1;
110                 arg_list.pointer = &arg;
111                 arg.type = ACPI_TYPE_INTEGER;
112                 arg.integer.value = 0;
113                 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
114         }
115
116         arg_list.count = 1;
117         arg_list.pointer = &arg;
118         arg.type = ACPI_TYPE_INTEGER;
119         arg.integer.value = 1;
120
121         /*
122          * TBD: _EJD support.
123          */
124         status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
125         if (ACPI_FAILURE(status))
126                 printk(KERN_WARNING PREFIX
127                                 "Eject device failed\n");
128
129         return;
130 }
131
132 static ssize_t
133 acpi_eject_store(struct device *d, struct device_attribute *attr,
134                 const char *buf, size_t count)
135 {
136         int ret = count;
137         acpi_status status;
138         acpi_object_type type = 0;
139         struct acpi_device *acpi_device = to_acpi_device(d);
140
141         if ((!count) || (buf[0] != '1')) {
142                 return -EINVAL;
143         }
144 #ifndef FORCE_EJECT
145         if (acpi_device->driver == NULL) {
146                 ret = -ENODEV;
147                 goto err;
148         }
149 #endif
150         status = acpi_get_type(acpi_device->handle, &type);
151         if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
152                 ret = -ENODEV;
153                 goto err;
154         }
155
156         acpi_os_hotplug_execute(acpi_bus_hot_remove_device, acpi_device->handle);
157 err:
158         return ret;
159 }
160
161 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
162
163 static ssize_t
164 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
165         struct acpi_device *acpi_dev = to_acpi_device(dev);
166
167         return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
168 }
169 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
170
171 static ssize_t
172 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
173         struct acpi_device *acpi_dev = to_acpi_device(dev);
174         struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
175         int result;
176
177         result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
178         if (result)
179                 goto end;
180
181         result = sprintf(buf, "%s\n", (char*)path.pointer);
182         kfree(path.pointer);
183 end:
184         return result;
185 }
186 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
187
188 static int acpi_device_setup_files(struct acpi_device *dev)
189 {
190         acpi_status status;
191         acpi_handle temp;
192         int result = 0;
193
194         /*
195          * Devices gotten from FADT don't have a "path" attribute
196          */
197         if (dev->handle) {
198                 result = device_create_file(&dev->dev, &dev_attr_path);
199                 if (result)
200                         goto end;
201         }
202
203         result = device_create_file(&dev->dev, &dev_attr_hid);
204         if (result)
205                 goto end;
206
207         result = device_create_file(&dev->dev, &dev_attr_modalias);
208         if (result)
209                 goto end;
210
211         /*
212          * If device has _EJ0, 'eject' file is created that is used to trigger
213          * hot-removal function from userland.
214          */
215         status = acpi_get_handle(dev->handle, "_EJ0", &temp);
216         if (ACPI_SUCCESS(status))
217                 result = device_create_file(&dev->dev, &dev_attr_eject);
218 end:
219         return result;
220 }
221
222 static void acpi_device_remove_files(struct acpi_device *dev)
223 {
224         acpi_status status;
225         acpi_handle temp;
226
227         /*
228          * If device has _EJ0, 'eject' file is created that is used to trigger
229          * hot-removal function from userland.
230          */
231         status = acpi_get_handle(dev->handle, "_EJ0", &temp);
232         if (ACPI_SUCCESS(status))
233                 device_remove_file(&dev->dev, &dev_attr_eject);
234
235         device_remove_file(&dev->dev, &dev_attr_modalias);
236         device_remove_file(&dev->dev, &dev_attr_hid);
237         if (dev->handle)
238                 device_remove_file(&dev->dev, &dev_attr_path);
239 }
240 /* --------------------------------------------------------------------------
241                         ACPI Bus operations
242    -------------------------------------------------------------------------- */
243
244 int acpi_match_device_ids(struct acpi_device *device,
245                           const struct acpi_device_id *ids)
246 {
247         const struct acpi_device_id *id;
248         struct acpi_hardware_id *hwid;
249
250         /*
251          * If the device is not present, it is unnecessary to load device
252          * driver for it.
253          */
254         if (!device->status.present)
255                 return -ENODEV;
256
257         for (id = ids; id->id[0]; id++)
258                 list_for_each_entry(hwid, &device->pnp.ids, list)
259                         if (!strcmp((char *) id->id, hwid->id))
260                                 return 0;
261
262         return -ENOENT;
263 }
264 EXPORT_SYMBOL(acpi_match_device_ids);
265
266 static void acpi_free_ids(struct acpi_device *device)
267 {
268         struct acpi_hardware_id *id, *tmp;
269
270         list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
271                 kfree(id->id);
272                 kfree(id);
273         }
274 }
275
276 static void acpi_device_release(struct device *dev)
277 {
278         struct acpi_device *acpi_dev = to_acpi_device(dev);
279
280         acpi_free_ids(acpi_dev);
281         kfree(acpi_dev);
282 }
283
284 static int acpi_device_suspend(struct device *dev, pm_message_t state)
285 {
286         struct acpi_device *acpi_dev = to_acpi_device(dev);
287         struct acpi_driver *acpi_drv = acpi_dev->driver;
288
289         if (acpi_drv && acpi_drv->ops.suspend)
290                 return acpi_drv->ops.suspend(acpi_dev, state);
291         return 0;
292 }
293
294 static int acpi_device_resume(struct device *dev)
295 {
296         struct acpi_device *acpi_dev = to_acpi_device(dev);
297         struct acpi_driver *acpi_drv = acpi_dev->driver;
298
299         if (acpi_drv && acpi_drv->ops.resume)
300                 return acpi_drv->ops.resume(acpi_dev);
301         return 0;
302 }
303
304 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
305 {
306         struct acpi_device *acpi_dev = to_acpi_device(dev);
307         struct acpi_driver *acpi_drv = to_acpi_driver(drv);
308
309         return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
310 }
311
312 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
313 {
314         struct acpi_device *acpi_dev = to_acpi_device(dev);
315         int len;
316
317         if (add_uevent_var(env, "MODALIAS="))
318                 return -ENOMEM;
319         len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
320                               sizeof(env->buf) - env->buflen);
321         if (len >= (sizeof(env->buf) - env->buflen))
322                 return -ENOMEM;
323         env->buflen += len;
324         return 0;
325 }
326
327 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
328 {
329         struct acpi_device *device = data;
330
331         device->driver->ops.notify(device, event);
332 }
333
334 static acpi_status acpi_device_notify_fixed(void *data)
335 {
336         struct acpi_device *device = data;
337
338         /* Fixed hardware devices have no handles */
339         acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
340         return AE_OK;
341 }
342
343 static int acpi_device_install_notify_handler(struct acpi_device *device)
344 {
345         acpi_status status;
346
347         if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
348                 status =
349                     acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
350                                                      acpi_device_notify_fixed,
351                                                      device);
352         else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
353                 status =
354                     acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
355                                                      acpi_device_notify_fixed,
356                                                      device);
357         else
358                 status = acpi_install_notify_handler(device->handle,
359                                                      ACPI_DEVICE_NOTIFY,
360                                                      acpi_device_notify,
361                                                      device);
362
363         if (ACPI_FAILURE(status))
364                 return -EINVAL;
365         return 0;
366 }
367
368 static void acpi_device_remove_notify_handler(struct acpi_device *device)
369 {
370         if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
371                 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
372                                                 acpi_device_notify_fixed);
373         else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
374                 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
375                                                 acpi_device_notify_fixed);
376         else
377                 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
378                                            acpi_device_notify);
379 }
380
381 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
382 static int acpi_start_single_object(struct acpi_device *);
383 static int acpi_device_probe(struct device * dev)
384 {
385         struct acpi_device *acpi_dev = to_acpi_device(dev);
386         struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
387         int ret;
388
389         ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
390         if (!ret) {
391                 if (acpi_dev->bus_ops.acpi_op_start)
392                         acpi_start_single_object(acpi_dev);
393
394                 if (acpi_drv->ops.notify) {
395                         ret = acpi_device_install_notify_handler(acpi_dev);
396                         if (ret) {
397                                 if (acpi_drv->ops.remove)
398                                         acpi_drv->ops.remove(acpi_dev,
399                                                      acpi_dev->removal_type);
400                                 return ret;
401                         }
402                 }
403
404                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
405                         "Found driver [%s] for device [%s]\n",
406                         acpi_drv->name, acpi_dev->pnp.bus_id));
407                 get_device(dev);
408         }
409         return ret;
410 }
411
412 static int acpi_device_remove(struct device * dev)
413 {
414         struct acpi_device *acpi_dev = to_acpi_device(dev);
415         struct acpi_driver *acpi_drv = acpi_dev->driver;
416
417         if (acpi_drv) {
418                 if (acpi_drv->ops.notify)
419                         acpi_device_remove_notify_handler(acpi_dev);
420                 if (acpi_drv->ops.remove)
421                         acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
422         }
423         acpi_dev->driver = NULL;
424         acpi_dev->driver_data = NULL;
425
426         put_device(dev);
427         return 0;
428 }
429
430 struct bus_type acpi_bus_type = {
431         .name           = "acpi",
432         .suspend        = acpi_device_suspend,
433         .resume         = acpi_device_resume,
434         .match          = acpi_bus_match,
435         .probe          = acpi_device_probe,
436         .remove         = acpi_device_remove,
437         .uevent         = acpi_device_uevent,
438 };
439
440 static int acpi_device_register(struct acpi_device *device)
441 {
442         int result;
443         struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
444         int found = 0;
445
446         /*
447          * Linkage
448          * -------
449          * Link this device to its parent and siblings.
450          */
451         INIT_LIST_HEAD(&device->children);
452         INIT_LIST_HEAD(&device->node);
453         INIT_LIST_HEAD(&device->wakeup_list);
454
455         new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
456         if (!new_bus_id) {
457                 printk(KERN_ERR PREFIX "Memory allocation error\n");
458                 return -ENOMEM;
459         }
460
461         mutex_lock(&acpi_device_lock);
462         /*
463          * Find suitable bus_id and instance number in acpi_bus_id_list
464          * If failed, create one and link it into acpi_bus_id_list
465          */
466         list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
467                 if (!strcmp(acpi_device_bus_id->bus_id,
468                             acpi_device_hid(device))) {
469                         acpi_device_bus_id->instance_no++;
470                         found = 1;
471                         kfree(new_bus_id);
472                         break;
473                 }
474         }
475         if (!found) {
476                 acpi_device_bus_id = new_bus_id;
477                 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
478                 acpi_device_bus_id->instance_no = 0;
479                 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
480         }
481         dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
482
483         if (device->parent)
484                 list_add_tail(&device->node, &device->parent->children);
485
486         if (device->wakeup.flags.valid)
487                 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
488         mutex_unlock(&acpi_device_lock);
489
490         if (device->parent)
491                 device->dev.parent = &device->parent->dev;
492         device->dev.bus = &acpi_bus_type;
493         device->dev.release = &acpi_device_release;
494         result = device_register(&device->dev);
495         if (result) {
496                 dev_err(&device->dev, "Error registering device\n");
497                 goto end;
498         }
499
500         result = acpi_device_setup_files(device);
501         if (result)
502                 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
503                        dev_name(&device->dev));
504
505         device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
506         return 0;
507 end:
508         mutex_lock(&acpi_device_lock);
509         if (device->parent)
510                 list_del(&device->node);
511         list_del(&device->wakeup_list);
512         mutex_unlock(&acpi_device_lock);
513         return result;
514 }
515
516 static void acpi_device_unregister(struct acpi_device *device, int type)
517 {
518         mutex_lock(&acpi_device_lock);
519         if (device->parent)
520                 list_del(&device->node);
521
522         list_del(&device->wakeup_list);
523         mutex_unlock(&acpi_device_lock);
524
525         acpi_detach_data(device->handle, acpi_bus_data_handler);
526
527         acpi_device_remove_files(device);
528         device_unregister(&device->dev);
529 }
530
531 /* --------------------------------------------------------------------------
532                                  Driver Management
533    -------------------------------------------------------------------------- */
534 /**
535  * acpi_bus_driver_init - add a device to a driver
536  * @device: the device to add and initialize
537  * @driver: driver for the device
538  *
539  * Used to initialize a device via its device driver.  Called whenever a
540  * driver is bound to a device.  Invokes the driver's add() ops.
541  */
542 static int
543 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
544 {
545         int result = 0;
546
547         if (!device || !driver)
548                 return -EINVAL;
549
550         if (!driver->ops.add)
551                 return -ENOSYS;
552
553         result = driver->ops.add(device);
554         if (result) {
555                 device->driver = NULL;
556                 device->driver_data = NULL;
557                 return result;
558         }
559
560         device->driver = driver;
561
562         /*
563          * TBD - Configuration Management: Assign resources to device based
564          * upon possible configuration and currently allocated resources.
565          */
566
567         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
568                           "Driver successfully bound to device\n"));
569         return 0;
570 }
571
572 static int acpi_start_single_object(struct acpi_device *device)
573 {
574         int result = 0;
575         struct acpi_driver *driver;
576
577
578         if (!(driver = device->driver))
579                 return 0;
580
581         if (driver->ops.start) {
582                 result = driver->ops.start(device);
583                 if (result && driver->ops.remove)
584                         driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
585         }
586
587         return result;
588 }
589
590 /**
591  * acpi_bus_register_driver - register a driver with the ACPI bus
592  * @driver: driver being registered
593  *
594  * Registers a driver with the ACPI bus.  Searches the namespace for all
595  * devices that match the driver's criteria and binds.  Returns zero for
596  * success or a negative error status for failure.
597  */
598 int acpi_bus_register_driver(struct acpi_driver *driver)
599 {
600         int ret;
601
602         if (acpi_disabled)
603                 return -ENODEV;
604         driver->drv.name = driver->name;
605         driver->drv.bus = &acpi_bus_type;
606         driver->drv.owner = driver->owner;
607
608         ret = driver_register(&driver->drv);
609         return ret;
610 }
611
612 EXPORT_SYMBOL(acpi_bus_register_driver);
613
614 /**
615  * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
616  * @driver: driver to unregister
617  *
618  * Unregisters a driver with the ACPI bus.  Searches the namespace for all
619  * devices that match the driver's criteria and unbinds.
620  */
621 void acpi_bus_unregister_driver(struct acpi_driver *driver)
622 {
623         driver_unregister(&driver->drv);
624 }
625
626 EXPORT_SYMBOL(acpi_bus_unregister_driver);
627
628 /* --------------------------------------------------------------------------
629                                  Device Enumeration
630    -------------------------------------------------------------------------- */
631 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
632 {
633         acpi_status status;
634         int ret;
635         struct acpi_device *device;
636
637         /*
638          * Fixed hardware devices do not appear in the namespace and do not
639          * have handles, but we fabricate acpi_devices for them, so we have
640          * to deal with them specially.
641          */
642         if (handle == NULL)
643                 return acpi_root;
644
645         do {
646                 status = acpi_get_parent(handle, &handle);
647                 if (status == AE_NULL_ENTRY)
648                         return NULL;
649                 if (ACPI_FAILURE(status))
650                         return acpi_root;
651
652                 ret = acpi_bus_get_device(handle, &device);
653                 if (ret == 0)
654                         return device;
655         } while (1);
656 }
657
658 acpi_status
659 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
660 {
661         acpi_status status;
662         acpi_handle tmp;
663         struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
664         union acpi_object *obj;
665
666         status = acpi_get_handle(handle, "_EJD", &tmp);
667         if (ACPI_FAILURE(status))
668                 return status;
669
670         status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
671         if (ACPI_SUCCESS(status)) {
672                 obj = buffer.pointer;
673                 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
674                                          ejd);
675                 kfree(buffer.pointer);
676         }
677         return status;
678 }
679 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
680
681 void acpi_bus_data_handler(acpi_handle handle, void *context)
682 {
683
684         /* TBD */
685
686         return;
687 }
688
689 static int acpi_bus_get_perf_flags(struct acpi_device *device)
690 {
691         device->performance.state = ACPI_STATE_UNKNOWN;
692         return 0;
693 }
694
695 static acpi_status
696 acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
697                                              union acpi_object *package)
698 {
699         int i = 0;
700         union acpi_object *element = NULL;
701
702         if (!device || !package || (package->package.count < 2))
703                 return AE_BAD_PARAMETER;
704
705         element = &(package->package.elements[0]);
706         if (!element)
707                 return AE_BAD_PARAMETER;
708         if (element->type == ACPI_TYPE_PACKAGE) {
709                 if ((element->package.count < 2) ||
710                     (element->package.elements[0].type !=
711                      ACPI_TYPE_LOCAL_REFERENCE)
712                     || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
713                         return AE_BAD_DATA;
714                 device->wakeup.gpe_device =
715                     element->package.elements[0].reference.handle;
716                 device->wakeup.gpe_number =
717                     (u32) element->package.elements[1].integer.value;
718         } else if (element->type == ACPI_TYPE_INTEGER) {
719                 device->wakeup.gpe_number = element->integer.value;
720         } else
721                 return AE_BAD_DATA;
722
723         element = &(package->package.elements[1]);
724         if (element->type != ACPI_TYPE_INTEGER) {
725                 return AE_BAD_DATA;
726         }
727         device->wakeup.sleep_state = element->integer.value;
728
729         if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
730                 return AE_NO_MEMORY;
731         }
732         device->wakeup.resources.count = package->package.count - 2;
733         for (i = 0; i < device->wakeup.resources.count; i++) {
734                 element = &(package->package.elements[i + 2]);
735                 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
736                         return AE_BAD_DATA;
737
738                 device->wakeup.resources.handles[i] = element->reference.handle;
739         }
740
741         return AE_OK;
742 }
743
744 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
745 {
746         acpi_status status = 0;
747         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
748         union acpi_object *package = NULL;
749         int psw_error;
750
751         struct acpi_device_id button_device_ids[] = {
752                 {"PNP0C0D", 0},
753                 {"PNP0C0C", 0},
754                 {"PNP0C0E", 0},
755                 {"", 0},
756         };
757
758         /* _PRW */
759         status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
760         if (ACPI_FAILURE(status)) {
761                 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
762                 goto end;
763         }
764
765         package = (union acpi_object *)buffer.pointer;
766         status = acpi_bus_extract_wakeup_device_power_package(device, package);
767         if (ACPI_FAILURE(status)) {
768                 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
769                 goto end;
770         }
771
772         kfree(buffer.pointer);
773
774         device->wakeup.flags.valid = 1;
775         device->wakeup.prepare_count = 0;
776         /* Call _PSW/_DSW object to disable its ability to wake the sleeping
777          * system for the ACPI device with the _PRW object.
778          * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
779          * So it is necessary to call _DSW object first. Only when it is not
780          * present will the _PSW object used.
781          */
782         psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
783         if (psw_error)
784                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
785                                 "error in _DSW or _PSW evaluation\n"));
786
787         /* Power button, Lid switch always enable wakeup */
788         if (!acpi_match_device_ids(device, button_device_ids))
789                 device->wakeup.flags.run_wake = 1;
790
791 end:
792         if (ACPI_FAILURE(status))
793                 device->flags.wake_capable = 0;
794         return 0;
795 }
796
797 static int acpi_bus_get_power_flags(struct acpi_device *device)
798 {
799         acpi_status status = 0;
800         acpi_handle handle = NULL;
801         u32 i = 0;
802
803
804         /*
805          * Power Management Flags
806          */
807         status = acpi_get_handle(device->handle, "_PSC", &handle);
808         if (ACPI_SUCCESS(status))
809                 device->power.flags.explicit_get = 1;
810         status = acpi_get_handle(device->handle, "_IRC", &handle);
811         if (ACPI_SUCCESS(status))
812                 device->power.flags.inrush_current = 1;
813
814         /*
815          * Enumerate supported power management states
816          */
817         for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
818                 struct acpi_device_power_state *ps = &device->power.states[i];
819                 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
820
821                 /* Evaluate "_PRx" to se if power resources are referenced */
822                 acpi_evaluate_reference(device->handle, object_name, NULL,
823                                         &ps->resources);
824                 if (ps->resources.count) {
825                         device->power.flags.power_resources = 1;
826                         ps->flags.valid = 1;
827                 }
828
829                 /* Evaluate "_PSx" to see if we can do explicit sets */
830                 object_name[2] = 'S';
831                 status = acpi_get_handle(device->handle, object_name, &handle);
832                 if (ACPI_SUCCESS(status)) {
833                         ps->flags.explicit_set = 1;
834                         ps->flags.valid = 1;
835                 }
836
837                 /* State is valid if we have some power control */
838                 if (ps->resources.count || ps->flags.explicit_set)
839                         ps->flags.valid = 1;
840
841                 ps->power = -1; /* Unknown - driver assigned */
842                 ps->latency = -1;       /* Unknown - driver assigned */
843         }
844
845         /* Set defaults for D0 and D3 states (always valid) */
846         device->power.states[ACPI_STATE_D0].flags.valid = 1;
847         device->power.states[ACPI_STATE_D0].power = 100;
848         device->power.states[ACPI_STATE_D3].flags.valid = 1;
849         device->power.states[ACPI_STATE_D3].power = 0;
850
851         /* TBD: System wake support and resource requirements. */
852
853         device->power.state = ACPI_STATE_UNKNOWN;
854         acpi_bus_get_power(device->handle, &(device->power.state));
855
856         return 0;
857 }
858
859 static int acpi_bus_get_flags(struct acpi_device *device)
860 {
861         acpi_status status = AE_OK;
862         acpi_handle temp = NULL;
863
864
865         /* Presence of _STA indicates 'dynamic_status' */
866         status = acpi_get_handle(device->handle, "_STA", &temp);
867         if (ACPI_SUCCESS(status))
868                 device->flags.dynamic_status = 1;
869
870         /* Presence of _RMV indicates 'removable' */
871         status = acpi_get_handle(device->handle, "_RMV", &temp);
872         if (ACPI_SUCCESS(status))
873                 device->flags.removable = 1;
874
875         /* Presence of _EJD|_EJ0 indicates 'ejectable' */
876         status = acpi_get_handle(device->handle, "_EJD", &temp);
877         if (ACPI_SUCCESS(status))
878                 device->flags.ejectable = 1;
879         else {
880                 status = acpi_get_handle(device->handle, "_EJ0", &temp);
881                 if (ACPI_SUCCESS(status))
882                         device->flags.ejectable = 1;
883         }
884
885         /* Presence of _LCK indicates 'lockable' */
886         status = acpi_get_handle(device->handle, "_LCK", &temp);
887         if (ACPI_SUCCESS(status))
888                 device->flags.lockable = 1;
889
890         /* Presence of _PS0|_PR0 indicates 'power manageable' */
891         status = acpi_get_handle(device->handle, "_PS0", &temp);
892         if (ACPI_FAILURE(status))
893                 status = acpi_get_handle(device->handle, "_PR0", &temp);
894         if (ACPI_SUCCESS(status))
895                 device->flags.power_manageable = 1;
896
897         /* Presence of _PRW indicates wake capable */
898         status = acpi_get_handle(device->handle, "_PRW", &temp);
899         if (ACPI_SUCCESS(status))
900                 device->flags.wake_capable = 1;
901
902         /* TBD: Performance management */
903
904         return 0;
905 }
906
907 static void acpi_device_get_busid(struct acpi_device *device)
908 {
909         char bus_id[5] = { '?', 0 };
910         struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
911         int i = 0;
912
913         /*
914          * Bus ID
915          * ------
916          * The device's Bus ID is simply the object name.
917          * TBD: Shouldn't this value be unique (within the ACPI namespace)?
918          */
919         if (ACPI_IS_ROOT_DEVICE(device)) {
920                 strcpy(device->pnp.bus_id, "ACPI");
921                 return;
922         }
923
924         switch (device->device_type) {
925         case ACPI_BUS_TYPE_POWER_BUTTON:
926                 strcpy(device->pnp.bus_id, "PWRF");
927                 break;
928         case ACPI_BUS_TYPE_SLEEP_BUTTON:
929                 strcpy(device->pnp.bus_id, "SLPF");
930                 break;
931         default:
932                 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
933                 /* Clean up trailing underscores (if any) */
934                 for (i = 3; i > 1; i--) {
935                         if (bus_id[i] == '_')
936                                 bus_id[i] = '\0';
937                         else
938                                 break;
939                 }
940                 strcpy(device->pnp.bus_id, bus_id);
941                 break;
942         }
943 }
944
945 /*
946  * acpi_bay_match - see if a device is an ejectable driver bay
947  *
948  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
949  * then we can safely call it an ejectable drive bay
950  */
951 static int acpi_bay_match(struct acpi_device *device){
952         acpi_status status;
953         acpi_handle handle;
954         acpi_handle tmp;
955         acpi_handle phandle;
956
957         handle = device->handle;
958
959         status = acpi_get_handle(handle, "_EJ0", &tmp);
960         if (ACPI_FAILURE(status))
961                 return -ENODEV;
962
963         if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
964                 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
965                 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
966                 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
967                 return 0;
968
969         if (acpi_get_parent(handle, &phandle))
970                 return -ENODEV;
971
972         if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
973                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
974                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
975                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
976                 return 0;
977
978         return -ENODEV;
979 }
980
981 /*
982  * acpi_dock_match - see if a device has a _DCK method
983  */
984 static int acpi_dock_match(struct acpi_device *device)
985 {
986         acpi_handle tmp;
987         return acpi_get_handle(device->handle, "_DCK", &tmp);
988 }
989
990 char *acpi_device_hid(struct acpi_device *device)
991 {
992         struct acpi_hardware_id *hid;
993
994         hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
995         return hid->id;
996 }
997 EXPORT_SYMBOL(acpi_device_hid);
998
999 static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1000 {
1001         struct acpi_hardware_id *id;
1002
1003         id = kmalloc(sizeof(*id), GFP_KERNEL);
1004         if (!id)
1005                 return;
1006
1007         id->id = kmalloc(strlen(dev_id) + 1, GFP_KERNEL);
1008         if (!id->id) {
1009                 kfree(id);
1010                 return;
1011         }
1012
1013         strcpy(id->id, dev_id);
1014         list_add_tail(&id->list, &device->pnp.ids);
1015 }
1016
1017 static void acpi_device_set_id(struct acpi_device *device)
1018 {
1019         acpi_status status;
1020         struct acpi_device_info *info;
1021         struct acpica_device_id_list *cid_list;
1022         int i;
1023
1024         switch (device->device_type) {
1025         case ACPI_BUS_TYPE_DEVICE:
1026                 if (ACPI_IS_ROOT_DEVICE(device)) {
1027                         acpi_add_id(device, ACPI_SYSTEM_HID);
1028                         break;
1029                 } else if (ACPI_IS_ROOT_DEVICE(device->parent)) {
1030                         /* \_SB_, the only root-level namespace device */
1031                         acpi_add_id(device, ACPI_BUS_HID);
1032                         strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1033                         strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1034                         break;
1035                 }
1036
1037                 status = acpi_get_object_info(device->handle, &info);
1038                 if (ACPI_FAILURE(status)) {
1039                         printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1040                         return;
1041                 }
1042
1043                 if (info->valid & ACPI_VALID_HID)
1044                         acpi_add_id(device, info->hardware_id.string);
1045                 if (info->valid & ACPI_VALID_CID) {
1046                         cid_list = &info->compatible_id_list;
1047                         for (i = 0; i < cid_list->count; i++)
1048                                 acpi_add_id(device, cid_list->ids[i].string);
1049                 }
1050                 if (info->valid & ACPI_VALID_ADR) {
1051                         device->pnp.bus_address = info->address;
1052                         device->flags.bus_address = 1;
1053                 }
1054
1055                 /*
1056                  * Some devices don't reliably have _HIDs & _CIDs, so add
1057                  * synthetic HIDs to make sure drivers can find them.
1058                  */
1059                 if (acpi_is_video_device(device))
1060                         acpi_add_id(device, ACPI_VIDEO_HID);
1061                 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1062                         acpi_add_id(device, ACPI_BAY_HID);
1063                 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1064                         acpi_add_id(device, ACPI_DOCK_HID);
1065
1066                 break;
1067         case ACPI_BUS_TYPE_POWER:
1068                 acpi_add_id(device, ACPI_POWER_HID);
1069                 break;
1070         case ACPI_BUS_TYPE_PROCESSOR:
1071                 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
1072                 break;
1073         case ACPI_BUS_TYPE_THERMAL:
1074                 acpi_add_id(device, ACPI_THERMAL_HID);
1075                 break;
1076         case ACPI_BUS_TYPE_POWER_BUTTON:
1077                 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
1078                 break;
1079         case ACPI_BUS_TYPE_SLEEP_BUTTON:
1080                 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
1081                 break;
1082         }
1083
1084         /*
1085          * We build acpi_devices for some objects that don't have _HID or _CID,
1086          * e.g., PCI bridges and slots.  Drivers can't bind to these objects,
1087          * but we do use them indirectly by traversing the acpi_device tree.
1088          * This generic ID isn't useful for driver binding, but it provides
1089          * the useful property that "every acpi_device has an ID."
1090          */
1091         if (list_empty(&device->pnp.ids))
1092                 acpi_add_id(device, "device");
1093 }
1094
1095 static int acpi_device_set_context(struct acpi_device *device)
1096 {
1097         acpi_status status;
1098
1099         /*
1100          * Context
1101          * -------
1102          * Attach this 'struct acpi_device' to the ACPI object.  This makes
1103          * resolutions from handle->device very efficient.  Fixed hardware
1104          * devices have no handles, so we skip them.
1105          */
1106         if (!device->handle)
1107                 return 0;
1108
1109         status = acpi_attach_data(device->handle,
1110                                   acpi_bus_data_handler, device);
1111         if (ACPI_SUCCESS(status))
1112                 return 0;
1113
1114         printk(KERN_ERR PREFIX "Error attaching device data\n");
1115         return -ENODEV;
1116 }
1117
1118 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1119 {
1120         if (!dev)
1121                 return -EINVAL;
1122
1123         dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1124         device_release_driver(&dev->dev);
1125
1126         if (!rmdevice)
1127                 return 0;
1128
1129         /*
1130          * unbind _ADR-Based Devices when hot removal
1131          */
1132         if (dev->flags.bus_address) {
1133                 if ((dev->parent) && (dev->parent->ops.unbind))
1134                         dev->parent->ops.unbind(dev);
1135         }
1136         acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1137
1138         return 0;
1139 }
1140
1141 static int acpi_add_single_object(struct acpi_device **child,
1142                                   acpi_handle handle, int type,
1143                                   unsigned long long sta,
1144                                   struct acpi_bus_ops *ops)
1145 {
1146         int result;
1147         struct acpi_device *device;
1148         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1149
1150         device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1151         if (!device) {
1152                 printk(KERN_ERR PREFIX "Memory allocation error\n");
1153                 return -ENOMEM;
1154         }
1155
1156         INIT_LIST_HEAD(&device->pnp.ids);
1157         device->device_type = type;
1158         device->handle = handle;
1159         device->parent = acpi_bus_get_parent(handle);
1160         device->bus_ops = *ops; /* workround for not call .start */
1161         STRUCT_TO_INT(device->status) = sta;
1162
1163         acpi_device_get_busid(device);
1164
1165         /*
1166          * Flags
1167          * -----
1168          * Note that we only look for object handles -- cannot evaluate objects
1169          * until we know the device is present and properly initialized.
1170          */
1171         result = acpi_bus_get_flags(device);
1172         if (result)
1173                 goto end;
1174
1175         /*
1176          * Initialize Device
1177          * -----------------
1178          * TBD: Synch with Core's enumeration/initialization process.
1179          */
1180         acpi_device_set_id(device);
1181
1182         /*
1183          * Power Management
1184          * ----------------
1185          */
1186         if (device->flags.power_manageable) {
1187                 result = acpi_bus_get_power_flags(device);
1188                 if (result)
1189                         goto end;
1190         }
1191
1192         /*
1193          * Wakeup device management
1194          *-----------------------
1195          */
1196         if (device->flags.wake_capable) {
1197                 result = acpi_bus_get_wakeup_device_flags(device);
1198                 if (result)
1199                         goto end;
1200         }
1201
1202         /*
1203          * Performance Management
1204          * ----------------------
1205          */
1206         if (device->flags.performance_manageable) {
1207                 result = acpi_bus_get_perf_flags(device);
1208                 if (result)
1209                         goto end;
1210         }
1211
1212         if ((result = acpi_device_set_context(device)))
1213                 goto end;
1214
1215         result = acpi_device_register(device);
1216
1217         /*
1218          * Bind _ADR-Based Devices when hot add
1219          */
1220         if (device->flags.bus_address) {
1221                 if (device->parent && device->parent->ops.bind)
1222                         device->parent->ops.bind(device);
1223         }
1224
1225 end:
1226         if (!result) {
1227                 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1228                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1229                         "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1230                          (char *) buffer.pointer,
1231                          device->parent ? dev_name(&device->parent->dev) :
1232                                           "(null)"));
1233                 kfree(buffer.pointer);
1234                 *child = device;
1235         } else
1236                 acpi_device_release(&device->dev);
1237
1238         return result;
1239 }
1240
1241 #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1242                           ACPI_STA_DEVICE_UI      | ACPI_STA_DEVICE_FUNCTIONING)
1243
1244 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1245                                     unsigned long long *sta)
1246 {
1247         acpi_status status;
1248         acpi_object_type acpi_type;
1249
1250         status = acpi_get_type(handle, &acpi_type);
1251         if (ACPI_FAILURE(status))
1252                 return -ENODEV;
1253
1254         switch (acpi_type) {
1255         case ACPI_TYPE_ANY:             /* for ACPI_ROOT_OBJECT */
1256         case ACPI_TYPE_DEVICE:
1257                 *type = ACPI_BUS_TYPE_DEVICE;
1258                 status = acpi_bus_get_status_handle(handle, sta);
1259                 if (ACPI_FAILURE(status))
1260                         return -ENODEV;
1261                 break;
1262         case ACPI_TYPE_PROCESSOR:
1263                 *type = ACPI_BUS_TYPE_PROCESSOR;
1264                 status = acpi_bus_get_status_handle(handle, sta);
1265                 if (ACPI_FAILURE(status))
1266                         return -ENODEV;
1267                 break;
1268         case ACPI_TYPE_THERMAL:
1269                 *type = ACPI_BUS_TYPE_THERMAL;
1270                 *sta = ACPI_STA_DEFAULT;
1271                 break;
1272         case ACPI_TYPE_POWER:
1273                 *type = ACPI_BUS_TYPE_POWER;
1274                 *sta = ACPI_STA_DEFAULT;
1275                 break;
1276         default:
1277                 return -ENODEV;
1278         }
1279
1280         return 0;
1281 }
1282
1283 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1284                                       void *context, void **return_value)
1285 {
1286         struct acpi_bus_ops *ops = context;
1287         int type;
1288         unsigned long long sta;
1289         struct acpi_device *device;
1290         acpi_status status;
1291         int result;
1292
1293         result = acpi_bus_type_and_status(handle, &type, &sta);
1294         if (result)
1295                 return AE_OK;
1296
1297         if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1298             !(sta & ACPI_STA_DEVICE_FUNCTIONING))
1299                 return AE_CTRL_DEPTH;
1300
1301         /*
1302          * We may already have an acpi_device from a previous enumeration.  If
1303          * so, we needn't add it again, but we may still have to start it.
1304          */
1305         device = NULL;
1306         acpi_bus_get_device(handle, &device);
1307         if (ops->acpi_op_add && !device)
1308                 acpi_add_single_object(&device, handle, type, sta, ops);
1309
1310         if (!device)
1311                 return AE_CTRL_DEPTH;
1312
1313         if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1314                 status = acpi_start_single_object(device);
1315                 if (ACPI_FAILURE(status))
1316                         return AE_CTRL_DEPTH;
1317         }
1318
1319         if (!*return_value)
1320                 *return_value = device;
1321         return AE_OK;
1322 }
1323
1324 static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1325                          struct acpi_device **child)
1326 {
1327         acpi_status status;
1328         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1329         void *device = NULL;
1330
1331         acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1332         printk(KERN_INFO PREFIX "Enumerating devices from [%s]\n",
1333                (char *) buffer.pointer);
1334
1335         status = acpi_bus_check_add(handle, 0, ops, &device);
1336         if (ACPI_SUCCESS(status))
1337                 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1338                                     acpi_bus_check_add, ops, &device);
1339
1340         if (child)
1341                 *child = device;
1342         return 0;
1343 }
1344
1345 int
1346 acpi_bus_add(struct acpi_device **child,
1347              struct acpi_device *parent, acpi_handle handle, int type)
1348 {
1349         struct acpi_bus_ops ops;
1350
1351         memset(&ops, 0, sizeof(ops));
1352         ops.acpi_op_add = 1;
1353
1354         acpi_bus_scan(handle, &ops, child);
1355         return 0;
1356 }
1357 EXPORT_SYMBOL(acpi_bus_add);
1358
1359 int acpi_bus_start(struct acpi_device *device)
1360 {
1361         struct acpi_bus_ops ops;
1362
1363         memset(&ops, 0, sizeof(ops));
1364         ops.acpi_op_start = 1;
1365
1366         acpi_bus_scan(device->handle, &ops, NULL);
1367         return 0;
1368 }
1369 EXPORT_SYMBOL(acpi_bus_start);
1370
1371 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1372 {
1373         acpi_status status;
1374         struct acpi_device *parent, *child;
1375         acpi_handle phandle, chandle;
1376         acpi_object_type type;
1377         u32 level = 1;
1378         int err = 0;
1379
1380         parent = start;
1381         phandle = start->handle;
1382         child = chandle = NULL;
1383
1384         while ((level > 0) && parent && (!err)) {
1385                 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1386                                               chandle, &chandle);
1387
1388                 /*
1389                  * If this scope is exhausted then move our way back up.
1390                  */
1391                 if (ACPI_FAILURE(status)) {
1392                         level--;
1393                         chandle = phandle;
1394                         acpi_get_parent(phandle, &phandle);
1395                         child = parent;
1396                         parent = parent->parent;
1397
1398                         if (level == 0)
1399                                 err = acpi_bus_remove(child, rmdevice);
1400                         else
1401                                 err = acpi_bus_remove(child, 1);
1402
1403                         continue;
1404                 }
1405
1406                 status = acpi_get_type(chandle, &type);
1407                 if (ACPI_FAILURE(status)) {
1408                         continue;
1409                 }
1410                 /*
1411                  * If there is a device corresponding to chandle then
1412                  * parse it (depth-first).
1413                  */
1414                 if (acpi_bus_get_device(chandle, &child) == 0) {
1415                         level++;
1416                         phandle = chandle;
1417                         chandle = NULL;
1418                         parent = child;
1419                 }
1420                 continue;
1421         }
1422         return err;
1423 }
1424 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1425
1426 static int acpi_bus_scan_fixed(void)
1427 {
1428         int result = 0;
1429         struct acpi_device *device = NULL;
1430         struct acpi_bus_ops ops;
1431
1432         memset(&ops, 0, sizeof(ops));
1433         ops.acpi_op_add = 1;
1434         ops.acpi_op_start = 1;
1435
1436         /*
1437          * Enumerate all fixed-feature devices.
1438          */
1439         if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
1440                 result = acpi_add_single_object(&device, NULL,
1441                                                 ACPI_BUS_TYPE_POWER_BUTTON,
1442                                                 ACPI_STA_DEFAULT,
1443                                                 &ops);
1444         }
1445
1446         if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1447                 result = acpi_add_single_object(&device, NULL,
1448                                                 ACPI_BUS_TYPE_SLEEP_BUTTON,
1449                                                 ACPI_STA_DEFAULT,
1450                                                 &ops);
1451         }
1452
1453         return result;
1454 }
1455
1456 int __init acpi_scan_init(void)
1457 {
1458         int result;
1459         struct acpi_bus_ops ops;
1460
1461         memset(&ops, 0, sizeof(ops));
1462         ops.acpi_op_add = 1;
1463         ops.acpi_op_start = 1;
1464
1465         result = bus_register(&acpi_bus_type);
1466         if (result) {
1467                 /* We don't want to quit even if we failed to add suspend/resume */
1468                 printk(KERN_ERR PREFIX "Could not register bus type\n");
1469         }
1470
1471         /*
1472          * Enumerate devices in the ACPI namespace.
1473          */
1474         result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
1475
1476         if (!result)
1477                 result = acpi_bus_scan_fixed();
1478
1479         if (result)
1480                 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1481
1482         return result;
1483 }