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