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