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