ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / acpi / scan.c
1 /*
2  * scan.c - support for transforming the ACPI namespace into individual objects
3  */
4
5 #include <linux/init.h>
6 #include <linux/acpi.h>
7 #include <linux/module.h>
8
9 #include <acpi/acpi_drivers.h>
10 #include <acpi/acinterp.h>      /* for acpi_ex_eisa_id_to_string() */
11
12
13 #define _COMPONENT              ACPI_BUS_COMPONENT
14 ACPI_MODULE_NAME                ("scan")
15
16 #define STRUCT_TO_INT(s)        (*((int*)&s))
17
18 extern struct acpi_device               *acpi_root;
19 struct acpi_device              *acpi_fixed_pwr_button;
20 struct acpi_device              *acpi_fixed_sleep_button;
21
22 EXPORT_SYMBOL(acpi_fixed_pwr_button);
23 EXPORT_SYMBOL(acpi_fixed_sleep_button);
24
25
26
27 #define ACPI_BUS_CLASS                  "system_bus"
28 #define ACPI_BUS_HID                    "ACPI_BUS"
29 #define ACPI_BUS_DRIVER_NAME            "ACPI Bus Driver"
30 #define ACPI_BUS_DEVICE_NAME            "System Bus"
31
32 static LIST_HEAD(acpi_device_list);
33 static spinlock_t acpi_device_lock = SPIN_LOCK_UNLOCKED;
34
35 static void acpi_device_release(struct kobject * kobj)
36 {
37         struct acpi_device * dev = container_of(kobj,struct acpi_device,kobj);
38         if (dev->pnp.cid_list)
39                 kfree(dev->pnp.cid_list);
40         kfree(dev);
41 }
42
43 static struct kobj_type ktype_acpi_ns = {
44         .release        = acpi_device_release,
45 };
46
47 static struct kset acpi_namespace_kset = {
48         .kobj           = { 
49                 .name = "namespace",
50         },
51         .subsys = &acpi_subsys,
52         .ktype  = &ktype_acpi_ns,
53 };
54
55
56 static void acpi_device_register(struct acpi_device * device, struct acpi_device * parent)
57 {
58         /*
59          * Linkage
60          * -------
61          * Link this device to its parent and siblings.
62          */
63         INIT_LIST_HEAD(&device->children);
64         INIT_LIST_HEAD(&device->node);
65         INIT_LIST_HEAD(&device->g_list);
66
67         spin_lock(&acpi_device_lock);
68         if (device->parent) {
69                 list_add_tail(&device->node, &device->parent->children);
70                 list_add_tail(&device->g_list,&device->parent->g_list);
71         } else
72                 list_add_tail(&device->g_list,&acpi_device_list);
73         spin_unlock(&acpi_device_lock);
74
75         kobject_init(&device->kobj);
76         strlcpy(device->kobj.name,device->pnp.bus_id,KOBJ_NAME_LEN);
77         if (parent)
78                 device->kobj.parent = &parent->kobj;
79         device->kobj.ktype = &ktype_acpi_ns;
80         device->kobj.kset = &acpi_namespace_kset;
81         kobject_add(&device->kobj);
82 }
83
84 static int
85 acpi_device_unregister (
86         struct acpi_device      *device, 
87         int                     type)
88 {
89         kobject_unregister(&device->kobj);
90         return 0;
91 }
92
93 void
94 acpi_bus_data_handler (
95         acpi_handle             handle,
96         u32                     function,
97         void                    *context)
98 {
99         ACPI_FUNCTION_TRACE("acpi_bus_data_handler");
100
101         /* TBD */
102
103         return_VOID;
104 }
105
106 static int
107 acpi_bus_get_power_flags (
108         struct acpi_device      *device)
109 {
110         acpi_status             status = 0;
111         acpi_handle             handle = 0;
112         u32                     i = 0;
113
114         ACPI_FUNCTION_TRACE("acpi_bus_get_power_flags");
115
116         /*
117          * Power Management Flags
118          */
119         status = acpi_get_handle(device->handle, "_PSC", &handle);
120         if (ACPI_SUCCESS(status))
121                 device->power.flags.explicit_get = 1;
122         status = acpi_get_handle(device->handle, "_IRC", &handle);
123         if (ACPI_SUCCESS(status))
124                 device->power.flags.inrush_current = 1;
125         status = acpi_get_handle(device->handle, "_PRW", &handle);
126         if (ACPI_SUCCESS(status))
127                 device->power.flags.wake_capable = 1;
128
129         /*
130          * Enumerate supported power management states
131          */
132         for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
133                 struct acpi_device_power_state *ps = &device->power.states[i];
134                 char            object_name[5] = {'_','P','R','0'+i,'\0'};
135
136                 /* Evaluate "_PRx" to se if power resources are referenced */
137                 acpi_evaluate_reference(device->handle, object_name, NULL,
138                         &ps->resources);
139                 if (ps->resources.count) {
140                         device->power.flags.power_resources = 1;
141                         ps->flags.valid = 1;
142                 }
143
144                 /* Evaluate "_PSx" to see if we can do explicit sets */
145                 object_name[2] = 'S';
146                 status = acpi_get_handle(device->handle, object_name, &handle);
147                 if (ACPI_SUCCESS(status)) {
148                         ps->flags.explicit_set = 1;
149                         ps->flags.valid = 1;
150                 }
151
152                 /* State is valid if we have some power control */
153                 if (ps->resources.count || ps->flags.explicit_set)
154                         ps->flags.valid = 1;
155
156                 ps->power = -1;         /* Unknown - driver assigned */
157                 ps->latency = -1;       /* Unknown - driver assigned */
158         }
159
160         /* Set defaults for D0 and D3 states (always valid) */
161         device->power.states[ACPI_STATE_D0].flags.valid = 1;
162         device->power.states[ACPI_STATE_D0].power = 100;
163         device->power.states[ACPI_STATE_D3].flags.valid = 1;
164         device->power.states[ACPI_STATE_D3].power = 0;
165
166         /* TBD: System wake support and resource requirements. */
167
168         device->power.state = ACPI_STATE_UNKNOWN;
169
170         return 0;
171 }
172
173
174 /* --------------------------------------------------------------------------
175                               Performance Management
176    -------------------------------------------------------------------------- */
177
178 static int
179 acpi_bus_get_perf_flags (
180         struct acpi_device      *device)
181 {
182         device->performance.state = ACPI_STATE_UNKNOWN;
183         return 0;
184 }
185
186 /* --------------------------------------------------------------------------
187                                  Driver Management
188    -------------------------------------------------------------------------- */
189
190 static LIST_HEAD(acpi_bus_drivers);
191 static DECLARE_MUTEX(acpi_bus_drivers_lock);
192
193
194 /**
195  * acpi_bus_match 
196  * --------------
197  * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it
198  * matches the specified driver's criteria.
199  */
200 static int
201 acpi_bus_match (
202         struct acpi_device      *device,
203         struct acpi_driver      *driver)
204 {
205         int error = 0;
206         struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
207
208         if (device->flags.hardware_id)
209                 if (strstr(driver->ids, device->pnp.hardware_id))
210                         goto Done;
211
212         if (device->flags.compatible_ids) {
213                 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
214                 int i;
215
216                 /* compare multiple _CID entries against driver ids */
217                 for (i = 0; i < cid_list->count; i++)
218                 {
219                         if (strstr(driver->ids, cid_list->id[i].value))
220                                 goto Done;
221                 }
222         }
223         error = -ENOENT;
224
225  Done:
226         if (buffer.pointer)
227                 acpi_os_free(buffer.pointer);
228         return error;
229 }
230
231
232 /**
233  * acpi_bus_driver_init 
234  * --------------------
235  * Used to initialize a device via its device driver.  Called whenever a 
236  * driver is bound to a device.  Invokes the driver's add() and start() ops.
237  */
238 static int
239 acpi_bus_driver_init (
240         struct acpi_device      *device, 
241         struct acpi_driver      *driver)
242 {
243         int                     result = 0;
244
245         ACPI_FUNCTION_TRACE("acpi_bus_driver_init");
246
247         if (!device || !driver)
248                 return_VALUE(-EINVAL);
249
250         if (!driver->ops.add)
251                 return_VALUE(-ENOSYS);
252
253         result = driver->ops.add(device);
254         if (result) {
255                 device->driver = NULL;
256                 acpi_driver_data(device) = NULL;
257                 return_VALUE(result);
258         }
259
260         device->driver = driver;
261
262         /*
263          * TBD - Configuration Management: Assign resources to device based
264          * upon possible configuration and currently allocated resources.
265          */
266
267         if (driver->ops.start) {
268                 result = driver->ops.start(device);
269                 if (result && driver->ops.remove)
270                         driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
271                 return_VALUE(result);
272         }
273
274         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Driver successfully bound to device\n"));
275
276         if (driver->ops.scan) {
277                 driver->ops.scan(device);
278         }
279
280         return_VALUE(0);
281 }
282
283 static int acpi_driver_attach(struct acpi_driver * drv)
284 {
285         struct list_head * node, * next;
286
287         ACPI_FUNCTION_TRACE("acpi_driver_attach");
288
289         spin_lock(&acpi_device_lock);
290         list_for_each_safe(node, next, &acpi_device_list) {
291                 struct acpi_device * dev = container_of(node, struct acpi_device, g_list);
292
293                 if (dev->driver || !dev->status.present)
294                         continue;
295                 spin_unlock(&acpi_device_lock);
296
297                 if (!acpi_bus_match(dev, drv)) {
298                         if (!acpi_bus_driver_init(dev, drv)) {
299                                 atomic_inc(&drv->references);
300                                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
301                                                   drv->name, dev->pnp.bus_id));
302                         }
303                 }
304                 spin_lock(&acpi_device_lock);
305         }
306         spin_unlock(&acpi_device_lock);
307         return_VALUE(0);
308 }
309
310 static int acpi_driver_detach(struct acpi_driver * drv)
311 {
312         struct list_head * node, * next;
313
314         ACPI_FUNCTION_TRACE("acpi_driver_detach");
315
316         spin_lock(&acpi_device_lock);
317         list_for_each_safe(node,next,&acpi_device_list) {
318                 struct acpi_device * dev = container_of(node,struct acpi_device,g_list);
319
320                 if (dev->driver == drv) {
321                         if (drv->ops.remove)
322                                 drv->ops.remove(dev,ACPI_BUS_REMOVAL_NORMAL);
323                         dev->driver = NULL;
324                         dev->driver_data = NULL;
325                         atomic_dec(&drv->references);
326                 }
327         }
328         spin_unlock(&acpi_device_lock);
329         return_VALUE(0);
330 }
331
332 /**
333  * acpi_bus_register_driver 
334  * ------------------------ 
335  * Registers a driver with the ACPI bus.  Searches the namespace for all
336  * devices that match the driver's criteria and binds.
337  */
338 int
339 acpi_bus_register_driver (
340         struct acpi_driver      *driver)
341 {
342         int error = 0;
343
344         ACPI_FUNCTION_TRACE("acpi_bus_register_driver");
345
346         if (acpi_disabled)
347                 return_VALUE(-ENODEV);
348
349         if (driver) {
350                 spin_lock(&acpi_device_lock);
351                 list_add_tail(&driver->node, &acpi_bus_drivers);
352                 spin_unlock(&acpi_device_lock);
353                 acpi_driver_attach(driver);
354         } else
355                 error = -EINVAL;
356
357         return_VALUE(error);
358 }
359
360
361 /**
362  * acpi_bus_unregister_driver 
363  * --------------------------
364  * Unregisters a driver with the ACPI bus.  Searches the namespace for all
365  * devices that match the driver's criteria and unbinds.
366  */
367 int
368 acpi_bus_unregister_driver (
369         struct acpi_driver      *driver)
370 {
371         int error = 0;
372
373         ACPI_FUNCTION_TRACE("acpi_bus_unregister_driver");
374
375         if (driver) {
376                 acpi_driver_detach(driver);
377
378                 if (!atomic_read(&driver->references)) {
379                         spin_lock(&acpi_device_lock);
380                         list_del_init(&driver->node);
381                         spin_unlock(&acpi_device_lock);
382                 } 
383         } else 
384                 error = -EINVAL;
385         return_VALUE(error);
386 }
387
388 /**
389  * acpi_bus_find_driver 
390  * --------------------
391  * Parses the list of registered drivers looking for a driver applicable for
392  * the specified device.
393  */
394 static int
395 acpi_bus_find_driver (
396         struct acpi_device      *device)
397 {
398         int                     result = 0;
399         struct list_head        * node, *next;
400
401         ACPI_FUNCTION_TRACE("acpi_bus_find_driver");
402
403         if (!device->flags.hardware_id && !device->flags.compatible_ids)
404                 goto Done;
405
406         spin_lock(&acpi_device_lock);
407         list_for_each_safe(node,next,&acpi_bus_drivers) {
408                 struct acpi_driver * driver = container_of(node,struct acpi_driver,node);
409
410                 atomic_inc(&driver->references);
411                 spin_unlock(&acpi_device_lock);
412                 if (!acpi_bus_match(device, driver)) {
413                         result = acpi_bus_driver_init(device, driver);
414                         if (!result)
415                                 goto Done;
416                 }
417                 atomic_dec(&driver->references);
418                 spin_lock(&acpi_device_lock);
419         }
420         spin_unlock(&acpi_device_lock);
421
422  Done:
423         return_VALUE(result);
424 }
425
426
427 /* --------------------------------------------------------------------------
428                                  Device Enumeration
429    -------------------------------------------------------------------------- */
430
431 static int 
432 acpi_bus_get_flags (
433         struct acpi_device      *device)
434 {
435         acpi_status             status = AE_OK;
436         acpi_handle             temp = NULL;
437
438         ACPI_FUNCTION_TRACE("acpi_bus_get_flags");
439
440         /* Presence of _STA indicates 'dynamic_status' */
441         status = acpi_get_handle(device->handle, "_STA", &temp);
442         if (ACPI_SUCCESS(status))
443                 device->flags.dynamic_status = 1;
444
445         /* Presence of _CID indicates 'compatible_ids' */
446         status = acpi_get_handle(device->handle, "_CID", &temp);
447         if (ACPI_SUCCESS(status))
448                 device->flags.compatible_ids = 1;
449
450         /* Presence of _RMV indicates 'removable' */
451         status = acpi_get_handle(device->handle, "_RMV", &temp);
452         if (ACPI_SUCCESS(status))
453                 device->flags.removable = 1;
454
455         /* Presence of _EJD|_EJ0 indicates 'ejectable' */
456         status = acpi_get_handle(device->handle, "_EJD", &temp);
457         if (ACPI_SUCCESS(status))
458                 device->flags.ejectable = 1;
459         else {
460                 status = acpi_get_handle(device->handle, "_EJ0", &temp);
461                 if (ACPI_SUCCESS(status))
462                         device->flags.ejectable = 1;
463         }
464
465         /* Presence of _LCK indicates 'lockable' */
466         status = acpi_get_handle(device->handle, "_LCK", &temp);
467         if (ACPI_SUCCESS(status))
468                 device->flags.lockable = 1;
469
470         /* Presence of _PS0|_PR0 indicates 'power manageable' */
471         status = acpi_get_handle(device->handle, "_PS0", &temp);
472         if (ACPI_FAILURE(status))
473                 status = acpi_get_handle(device->handle, "_PR0", &temp);
474         if (ACPI_SUCCESS(status))
475                 device->flags.power_manageable = 1;
476
477         /* TBD: Peformance management */
478
479         return_VALUE(0);
480 }
481
482 static void acpi_device_get_busid(struct acpi_device * device, acpi_handle handle, int type)
483 {
484         char                    bus_id[5] = {'?',0};
485         struct acpi_buffer      buffer = {sizeof(bus_id), bus_id};
486         int                     i = 0;
487
488         /*
489          * Bus ID
490          * ------
491          * The device's Bus ID is simply the object name.
492          * TBD: Shouldn't this value be unique (within the ACPI namespace)?
493          */
494         switch (type) {
495         case ACPI_BUS_TYPE_SYSTEM:
496                 strcpy(device->pnp.bus_id, "ACPI");
497                 break;
498         case ACPI_BUS_TYPE_POWER_BUTTON:
499                 strcpy(device->pnp.bus_id, "PWRF");
500                 break;
501         case ACPI_BUS_TYPE_SLEEP_BUTTON:
502                 strcpy(device->pnp.bus_id, "SLPF");
503                 break;
504         default:
505                 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
506                 /* Clean up trailing underscores (if any) */
507                 for (i = 3; i > 1; i--) {
508                         if (bus_id[i] == '_')
509                                 bus_id[i] = '\0';
510                         else
511                                 break;
512                 }
513                 strcpy(device->pnp.bus_id, bus_id);
514                 break;
515         }
516 }
517
518 static void acpi_device_set_id(struct acpi_device * device, struct acpi_device * parent,
519                                acpi_handle handle, int type)
520 {
521         struct acpi_device_info *info;
522         struct acpi_buffer      buffer = {ACPI_ALLOCATE_BUFFER, NULL};
523         char                    *hid = NULL;
524         char                    *uid = NULL;
525         struct acpi_compatible_id_list *cid_list = NULL;
526         acpi_status             status;
527
528         switch (type) {
529         case ACPI_BUS_TYPE_DEVICE:
530                 status = acpi_get_object_info(handle, &buffer);
531                 if (ACPI_FAILURE(status)) {
532                         printk("%s: Error reading device info\n",__FUNCTION__);
533                         return;
534                 }
535
536                 info = buffer.pointer;
537                 if (info->valid & ACPI_VALID_HID)
538                         hid = info->hardware_id.value;
539                 if (info->valid & ACPI_VALID_UID)
540                         uid = info->unique_id.value;
541                 if (info->valid & ACPI_VALID_CID)
542                         cid_list = &info->compatibility_id;
543                 if (info->valid & ACPI_VALID_ADR) {
544                         device->pnp.bus_address = info->address;
545                         device->flags.bus_address = 1;
546                 }
547                 break;
548         case ACPI_BUS_TYPE_POWER:
549                 hid = ACPI_POWER_HID;
550                 break;
551         case ACPI_BUS_TYPE_PROCESSOR:
552                 hid = ACPI_PROCESSOR_HID;
553                 break;
554         case ACPI_BUS_TYPE_SYSTEM:
555                 hid = ACPI_SYSTEM_HID;
556                 break;
557         case ACPI_BUS_TYPE_THERMAL:
558                 hid = ACPI_THERMAL_HID;
559                 break;
560         case ACPI_BUS_TYPE_POWER_BUTTON:
561                 hid = ACPI_BUTTON_HID_POWERF;
562                 break;
563         case ACPI_BUS_TYPE_SLEEP_BUTTON:
564                 hid = ACPI_BUTTON_HID_SLEEPF;
565                 break;
566         }
567
568         /* 
569          * \_SB
570          * ----
571          * Fix for the system root bus device -- the only root-level device.
572          */
573         if ((parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
574                 hid = ACPI_BUS_HID;
575                 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
576                 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
577         }
578
579         if (hid) {
580                 strcpy(device->pnp.hardware_id, hid);
581                 device->flags.hardware_id = 1;
582         }
583         if (uid) {
584                 strcpy(device->pnp.unique_id, uid);
585                 device->flags.unique_id = 1;
586         }
587         if (cid_list) {
588                 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
589                 if (device->pnp.cid_list)
590                         memcpy(device->pnp.cid_list, cid_list, cid_list->size);
591                 else
592                         printk(KERN_ERR "Memory allocation error\n");
593         }
594
595         acpi_os_free(buffer.pointer);
596 }
597
598 int acpi_device_set_context(struct acpi_device * device, int type)
599 {
600         acpi_status status = AE_OK;
601         int result = 0;
602         /*
603          * Context
604          * -------
605          * Attach this 'struct acpi_device' to the ACPI object.  This makes
606          * resolutions from handle->device very efficient.  Note that we need
607          * to be careful with fixed-feature devices as they all attach to the
608          * root object.
609          */
610         if (type != ACPI_BUS_TYPE_POWER_BUTTON && 
611             type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
612                 status = acpi_attach_data(device->handle,
613                         acpi_bus_data_handler, device);
614
615                 if (ACPI_FAILURE(status)) {
616                         printk("Error attaching device data\n");
617                         result = -ENODEV;
618                 }
619         }
620         return result;
621 }
622
623 void acpi_device_get_debug_info(struct acpi_device * device, acpi_handle handle, int type)
624 {
625 #ifdef CONFIG_ACPI_DEBUG_OUTPUT
626         char            *type_string = NULL;
627         char            name[80] = {'?','\0'};
628         acpi_buffer     buffer = {sizeof(name), name};
629
630         switch (type) {
631         case ACPI_BUS_TYPE_DEVICE:
632                 type_string = "Device";
633                 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
634                 break;
635         case ACPI_BUS_TYPE_POWER:
636                 type_string = "Power Resource";
637                 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
638                 break;
639         case ACPI_BUS_TYPE_PROCESSOR:
640                 type_string = "Processor";
641                 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
642                 break;
643         case ACPI_BUS_TYPE_SYSTEM:
644                 type_string = "System";
645                 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
646                 break;
647         case ACPI_BUS_TYPE_THERMAL:
648                 type_string = "Thermal Zone";
649                 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
650                 break;
651         case ACPI_BUS_TYPE_POWER_BUTTON:
652                 type_string = "Power Button";
653                 sprintf(name, "PWRB");
654                 break;
655         case ACPI_BUS_TYPE_SLEEP_BUTTON:
656                 type_string = "Sleep Button";
657                 sprintf(name, "SLPB");
658                 break;
659         }
660
661         printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
662 #endif /*CONFIG_ACPI_DEBUG_OUTPUT*/
663 }
664
665 static int 
666 acpi_bus_add (
667         struct acpi_device      **child,
668         struct acpi_device      *parent,
669         acpi_handle             handle,
670         int                     type)
671 {
672         int                     result = 0;
673         struct acpi_device      *device = NULL;
674
675         ACPI_FUNCTION_TRACE("acpi_bus_add");
676
677         if (!child)
678                 return_VALUE(-EINVAL);
679
680         device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
681         if (!device) {
682                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Memory allocation error\n"));
683                 return_VALUE(-ENOMEM);
684         }
685         memset(device, 0, sizeof(struct acpi_device));
686
687         device->handle = handle;
688         device->parent = parent;
689
690         acpi_device_get_busid(device,handle,type);
691
692         /*
693          * Flags
694          * -----
695          * Get prior to calling acpi_bus_get_status() so we know whether
696          * or not _STA is present.  Note that we only look for object
697          * handles -- cannot evaluate objects until we know the device is
698          * present and properly initialized.
699          */
700         result = acpi_bus_get_flags(device);
701         if (result)
702                 goto end;
703
704         /*
705          * Status
706          * ------
707          * See if the device is present.  We always assume that non-Device()
708          * objects (e.g. thermal zones, power resources, processors, etc.) are
709          * present, functioning, etc. (at least when parent object is present).
710          * Note that _STA has a different meaning for some objects (e.g.
711          * power resources) so we need to be careful how we use it.
712          */
713         switch (type) {
714         case ACPI_BUS_TYPE_DEVICE:
715                 result = acpi_bus_get_status(device);
716                 if (ACPI_FAILURE(result) || !device->status.present) {
717                         result = -ENOENT;
718                         goto end;
719                 }
720                 break;
721         default:
722                 STRUCT_TO_INT(device->status) = 0x0F;
723                 break;
724         }
725
726         /*
727          * Initialize Device
728          * -----------------
729          * TBD: Synch with Core's enumeration/initialization process.
730          */
731
732         /*
733          * Hardware ID, Unique ID, & Bus Address
734          * -------------------------------------
735          */
736         acpi_device_set_id(device,parent,handle,type);
737
738         /*
739          * Power Management
740          * ----------------
741          */
742         if (device->flags.power_manageable) {
743                 result = acpi_bus_get_power_flags(device);
744                 if (result)
745                         goto end;
746         }
747
748         /*
749          * Performance Management
750          * ----------------------
751          */
752         if (device->flags.performance_manageable) {
753                 result = acpi_bus_get_perf_flags(device);
754                 if (result)
755                         goto end;
756         }
757
758         if ((result = acpi_device_set_context(device,type)))
759                 goto end;
760
761         acpi_device_get_debug_info(device,handle,type);
762
763         acpi_device_register(device,parent);
764
765         /*
766          * Bind _ADR-Based Devices
767          * -----------------------
768          * If there's a a bus address (_ADR) then we utilize the parent's 
769          * 'bind' function (if exists) to bind the ACPI- and natively-
770          * enumerated device representations.
771          */
772         if (device->flags.bus_address) {
773                 if (device->parent && device->parent->ops.bind)
774                         device->parent->ops.bind(device);
775         }
776
777         /*
778          * Locate & Attach Driver
779          * ----------------------
780          * If there's a hardware id (_HID) or compatible ids (_CID) we check
781          * to see if there's a driver installed for this kind of device.  Note
782          * that drivers can install before or after a device is enumerated.
783          *
784          * TBD: Assumes LDM provides driver hot-plug capability.
785          */
786         acpi_bus_find_driver(device);
787
788 end:
789         if (!result)
790                 *child = device;
791         else {
792                 if (device->pnp.cid_list)
793                         kfree(device->pnp.cid_list);
794                 kfree(device);
795         }
796
797         return_VALUE(result);
798 }
799
800
801
802 static int acpi_bus_scan (struct acpi_device    *start)
803 {
804         acpi_status             status = AE_OK;
805         struct acpi_device      *parent = NULL;
806         struct acpi_device      *child = NULL;
807         acpi_handle             phandle = 0;
808         acpi_handle             chandle = 0;
809         acpi_object_type        type = 0;
810         u32                     level = 1;
811
812         ACPI_FUNCTION_TRACE("acpi_bus_scan");
813
814         if (!start)
815                 return_VALUE(-EINVAL);
816
817         parent = start;
818         phandle = start->handle;
819         
820         /*
821          * Parse through the ACPI namespace, identify all 'devices', and
822          * create a new 'struct acpi_device' for each.
823          */
824         while ((level > 0) && parent) {
825
826                 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
827                         chandle, &chandle);
828
829                 /*
830                  * If this scope is exhausted then move our way back up.
831                  */
832                 if (ACPI_FAILURE(status)) {
833                         level--;
834                         chandle = phandle;
835                         acpi_get_parent(phandle, &phandle);
836                         if (parent->parent)
837                                 parent = parent->parent;
838                         continue;
839                 }
840
841                 status = acpi_get_type(chandle, &type);
842                 if (ACPI_FAILURE(status))
843                         continue;
844
845                 /*
846                  * If this is a scope object then parse it (depth-first).
847                  */
848                 if (type == ACPI_TYPE_LOCAL_SCOPE) {
849                         level++;
850                         phandle = chandle;
851                         chandle = 0;
852                         continue;
853                 }
854
855                 /*
856                  * We're only interested in objects that we consider 'devices'.
857                  */
858                 switch (type) {
859                 case ACPI_TYPE_DEVICE:
860                         type = ACPI_BUS_TYPE_DEVICE;
861                         break;
862                 case ACPI_TYPE_PROCESSOR:
863                         type = ACPI_BUS_TYPE_PROCESSOR;
864                         break;
865                 case ACPI_TYPE_THERMAL:
866                         type = ACPI_BUS_TYPE_THERMAL;
867                         break;
868                 case ACPI_TYPE_POWER:
869                         type = ACPI_BUS_TYPE_POWER;
870                         break;
871                 default:
872                         continue;
873                 }
874
875                 status = acpi_bus_add(&child, parent, chandle, type);
876                 if (ACPI_FAILURE(status))
877                         continue;
878
879                 /*
880                  * If the device is present, enabled, and functioning then
881                  * parse its scope (depth-first).  Note that we need to
882                  * represent absent devices to facilitate PnP notifications
883                  * -- but only the subtree head (not all of its children,
884                  * which will be enumerated when the parent is inserted).
885                  *
886                  * TBD: Need notifications and other detection mechanisms
887                  *      in place before we can fully implement this.
888                  */
889                 if (child->status.present) {
890                         status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
891                                 0, NULL);
892                         if (ACPI_SUCCESS(status)) {
893                                 level++;
894                                 phandle = chandle;
895                                 chandle = 0;
896                                 parent = child;
897                         }
898                 }
899         }
900
901         return_VALUE(0);
902 }
903
904
905 static int
906 acpi_bus_scan_fixed (
907         struct acpi_device      *root)
908 {
909         int                     result = 0;
910
911         ACPI_FUNCTION_TRACE("acpi_bus_scan_fixed");
912
913         acpi_fixed_pwr_button = NULL;
914         acpi_fixed_sleep_button = NULL;
915
916
917         if (!root)
918                 return_VALUE(-ENODEV);
919
920         /*
921          * Enumerate all fixed-feature devices.
922          */
923         if (acpi_fadt.pwr_button == 0)
924                 result = acpi_bus_add(&acpi_fixed_pwr_button, acpi_root, 
925                         NULL, ACPI_BUS_TYPE_POWER_BUTTON);
926
927         if (acpi_fadt.sleep_button == 0)
928                 result = acpi_bus_add(&acpi_fixed_sleep_button, acpi_root, 
929                         NULL, ACPI_BUS_TYPE_SLEEP_BUTTON);
930
931         return_VALUE(result);
932 }
933
934
935 static int __init acpi_scan_init(void)
936 {
937         int result;
938
939         ACPI_FUNCTION_TRACE("acpi_scan_init");
940
941         if (acpi_disabled)
942                 return_VALUE(0);
943
944         kset_register(&acpi_namespace_kset);
945
946         /*
947          * Create the root device in the bus's device tree
948          */
949         result = acpi_bus_add(&acpi_root, NULL, ACPI_ROOT_OBJECT, 
950                 ACPI_BUS_TYPE_SYSTEM);
951         if (result)
952                 goto Done;
953
954         /*
955          * Enumerate devices in the ACPI namespace.
956          */
957         result = acpi_bus_scan_fixed(acpi_root);
958         if (!result) 
959                 result = acpi_bus_scan(acpi_root);
960
961         if (result)
962                 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
963
964  Done:
965         return_VALUE(result);
966 }
967
968 subsys_initcall(acpi_scan_init);