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