MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / acpi / scan.c
blob4e46eeddf19c4bec9312f736069c50bba65f5ff9
1 /*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
5 #include <linux/init.h>
6 #include <linux/acpi.h>
8 #include <acpi/acpi_drivers.h>
9 #include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */
12 #define _COMPONENT ACPI_BUS_COMPONENT
13 ACPI_MODULE_NAME ("scan")
15 #define STRUCT_TO_INT(s) (*((int*)&s))
17 extern struct acpi_device *acpi_root;
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"
25 static LIST_HEAD(acpi_device_list);
26 spinlock_t acpi_device_lock = SPIN_LOCK_UNLOCKED;
27 LIST_HEAD(acpi_wakeup_device_list);
29 static void acpi_device_release(struct kobject * kobj)
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);
37 static struct kobj_type ktype_acpi_ns = {
38 .release = acpi_device_release,
41 static struct kset acpi_namespace_kset = {
42 .kobj = {
43 .name = "namespace",
45 .subsys = &acpi_subsys,
46 .ktype = &ktype_acpi_ns,
50 static void acpi_device_register(struct acpi_device * device, struct acpi_device * parent)
53 * Linkage
54 * -------
55 * Link this device to its parent and siblings.
57 INIT_LIST_HEAD(&device->children);
58 INIT_LIST_HEAD(&device->node);
59 INIT_LIST_HEAD(&device->g_list);
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);
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);
78 static int
79 acpi_device_unregister (
80 struct acpi_device *device,
81 int type)
83 kobject_unregister(&device->kobj);
84 return 0;
87 void
88 acpi_bus_data_handler (
89 acpi_handle handle,
90 u32 function,
91 void *context)
93 ACPI_FUNCTION_TRACE("acpi_bus_data_handler");
95 /* TBD */
97 return_VOID;
100 static int
101 acpi_bus_get_power_flags (
102 struct acpi_device *device)
104 acpi_status status = 0;
105 acpi_handle handle = NULL;
106 u32 i = 0;
108 ACPI_FUNCTION_TRACE("acpi_bus_get_power_flags");
111 * Power Management Flags
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;
121 * Enumerate supported power management states
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'};
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;
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;
143 /* State is valid if we have some power control */
144 if (ps->resources.count || ps->flags.explicit_set)
145 ps->flags.valid = 1;
147 ps->power = -1; /* Unknown - driver assigned */
148 ps->latency = -1; /* Unknown - driver assigned */
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;
157 /* TBD: System wake support and resource requirements. */
159 device->power.state = ACPI_STATE_UNKNOWN;
161 return 0;
164 static int
165 acpi_match_ids (
166 struct acpi_device *device,
167 char *ids)
169 int error = 0;
170 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
172 if (device->flags.hardware_id)
173 if (strstr(ids, device->pnp.hardware_id))
174 goto Done;
176 if (device->flags.compatible_ids) {
177 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
178 int i;
180 /* compare multiple _CID entries against driver ids */
181 for (i = 0; i < cid_list->count; i++)
183 if (strstr(ids, cid_list->id[i].value))
184 goto Done;
187 error = -ENOENT;
189 Done:
190 if (buffer.pointer)
191 acpi_os_free(buffer.pointer);
192 return error;
195 static acpi_status
196 acpi_bus_extract_wakeup_device_power_package (
197 struct acpi_device *device,
198 union acpi_object *package)
200 int i = 0;
201 union acpi_object *element = NULL;
203 if (!device || !package || (package->package.count < 2))
204 return AE_BAD_PARAMETER;
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;
221 element = &(package->package.elements[1]);
222 if (element->type != ACPI_TYPE_INTEGER) {
223 return AE_BAD_DATA;
225 device->wakeup.sleep_state = element->integer.value;
227 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
228 return AE_NO_MEMORY;
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;
237 device->wakeup.resources.handles[i] = element->reference.handle;
240 return AE_OK;
243 static int
244 acpi_bus_get_wakeup_device_flags (
245 struct acpi_device *device)
247 acpi_status status = 0;
248 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
249 union acpi_object *package = NULL;
251 ACPI_FUNCTION_TRACE("acpi_bus_get_wakeup_flags");
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;
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;
267 acpi_os_free(buffer.pointer);
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;
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);
280 end:
281 if (ACPI_FAILURE(status))
282 device->flags.wake_capable = 0;
283 return 0;
286 /* --------------------------------------------------------------------------
287 Performance Management
288 -------------------------------------------------------------------------- */
290 static int
291 acpi_bus_get_perf_flags (
292 struct acpi_device *device)
294 device->performance.state = ACPI_STATE_UNKNOWN;
295 return 0;
298 /* --------------------------------------------------------------------------
299 Driver Management
300 -------------------------------------------------------------------------- */
302 static LIST_HEAD(acpi_bus_drivers);
303 static DECLARE_MUTEX(acpi_bus_drivers_lock);
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.
312 static int
313 acpi_bus_match (
314 struct acpi_device *device,
315 struct acpi_driver *driver)
317 return acpi_match_ids(device, driver->ids);
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.
327 static int
328 acpi_bus_driver_init (
329 struct acpi_device *device,
330 struct acpi_driver *driver)
332 int result = 0;
334 ACPI_FUNCTION_TRACE("acpi_bus_driver_init");
336 if (!device || !driver)
337 return_VALUE(-EINVAL);
339 if (!driver->ops.add)
340 return_VALUE(-ENOSYS);
342 result = driver->ops.add(device);
343 if (result) {
344 device->driver = NULL;
345 acpi_driver_data(device) = NULL;
346 return_VALUE(result);
349 device->driver = driver;
352 * TBD - Configuration Management: Assign resources to device based
353 * upon possible configuration and currently allocated resources.
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);
363 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Driver successfully bound to device\n"));
365 if (driver->ops.scan) {
366 driver->ops.scan(device);
369 return_VALUE(0);
372 static int acpi_driver_attach(struct acpi_driver * drv)
374 struct list_head * node, * next;
375 int count = 0;
377 ACPI_FUNCTION_TRACE("acpi_driver_attach");
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);
383 if (dev->driver || !dev->status.present)
384 continue;
385 spin_unlock(&acpi_device_lock);
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));
395 spin_lock(&acpi_device_lock);
397 spin_unlock(&acpi_device_lock);
398 return_VALUE(count);
401 static int acpi_driver_detach(struct acpi_driver * drv)
403 struct list_head * node, * next;
405 ACPI_FUNCTION_TRACE("acpi_driver_detach");
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);
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);
421 spin_unlock(&acpi_device_lock);
422 return_VALUE(0);
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.
434 acpi_bus_register_driver (
435 struct acpi_driver *driver)
437 int count;
439 ACPI_FUNCTION_TRACE("acpi_bus_register_driver");
441 if (acpi_disabled)
442 return_VALUE(-ENODEV);
444 if (!driver)
445 return_VALUE(-EINVAL);
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);
452 return_VALUE(count);
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.
463 acpi_bus_unregister_driver (
464 struct acpi_driver *driver)
466 int error = 0;
468 ACPI_FUNCTION_TRACE("acpi_bus_unregister_driver");
470 if (driver) {
471 acpi_driver_detach(driver);
473 if (!atomic_read(&driver->references)) {
474 spin_lock(&acpi_device_lock);
475 list_del_init(&driver->node);
476 spin_unlock(&acpi_device_lock);
478 } else
479 error = -EINVAL;
480 return_VALUE(error);
484 * acpi_bus_find_driver
485 * --------------------
486 * Parses the list of registered drivers looking for a driver applicable for
487 * the specified device.
489 static int
490 acpi_bus_find_driver (
491 struct acpi_device *device)
493 int result = 0;
494 struct list_head * node, *next;
496 ACPI_FUNCTION_TRACE("acpi_bus_find_driver");
498 if (!device->flags.hardware_id && !device->flags.compatible_ids)
499 goto Done;
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);
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;
512 atomic_dec(&driver->references);
513 spin_lock(&acpi_device_lock);
515 spin_unlock(&acpi_device_lock);
517 Done:
518 return_VALUE(result);
522 /* --------------------------------------------------------------------------
523 Device Enumeration
524 -------------------------------------------------------------------------- */
526 static int
527 acpi_bus_get_flags (
528 struct acpi_device *device)
530 acpi_status status = AE_OK;
531 acpi_handle temp = NULL;
533 ACPI_FUNCTION_TRACE("acpi_bus_get_flags");
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;
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;
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;
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;
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;
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;
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;
577 /* TBD: Peformance management */
579 return_VALUE(0);
582 static void acpi_device_get_busid(struct acpi_device * device, acpi_handle handle, int type)
584 char bus_id[5] = {'?',0};
585 struct acpi_buffer buffer = {sizeof(bus_id), bus_id};
586 int i = 0;
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)?
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;
613 strcpy(device->pnp.bus_id, bus_id);
614 break;
618 static void acpi_device_set_id(struct acpi_device * device, struct acpi_device * parent,
619 acpi_handle handle, int type)
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;
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;
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;
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;
669 * \_SB
670 * ----
671 * Fix for the system root bus device -- the only root-level device.
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);
679 if (hid) {
680 strcpy(device->pnp.hardware_id, hid);
681 device->flags.hardware_id = 1;
683 if (uid) {
684 strcpy(device->pnp.unique_id, uid);
685 device->flags.unique_id = 1;
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");
695 acpi_os_free(buffer.pointer);
698 int acpi_device_set_context(struct acpi_device * device, int type)
700 acpi_status status = AE_OK;
701 int result = 0;
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.
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);
715 if (ACPI_FAILURE(status)) {
716 printk("Error attaching device data\n");
717 result = -ENODEV;
720 return result;
723 void acpi_device_get_debug_info(struct acpi_device * device, acpi_handle handle, int type)
725 #ifdef CONFIG_ACPI_DEBUG_OUTPUT
726 char *type_string = NULL;
727 char name[80] = {'?','\0'};
728 acpi_buffer buffer = {sizeof(name), name};
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;
761 printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
762 #endif /*CONFIG_ACPI_DEBUG_OUTPUT*/
765 static int
766 acpi_bus_add (
767 struct acpi_device **child,
768 struct acpi_device *parent,
769 acpi_handle handle,
770 int type)
772 int result = 0;
773 struct acpi_device *device = NULL;
775 ACPI_FUNCTION_TRACE("acpi_bus_add");
777 if (!child)
778 return_VALUE(-EINVAL);
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);
785 memset(device, 0, sizeof(struct acpi_device));
787 device->handle = handle;
788 device->parent = parent;
790 acpi_device_get_busid(device,handle,type);
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.
800 result = acpi_bus_get_flags(device);
801 if (result)
802 goto end;
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.
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;
820 break;
821 default:
822 STRUCT_TO_INT(device->status) = 0x0F;
823 break;
827 * Initialize Device
828 * -----------------
829 * TBD: Synch with Core's enumeration/initialization process.
833 * Hardware ID, Unique ID, & Bus Address
834 * -------------------------------------
836 acpi_device_set_id(device,parent,handle,type);
839 * Power Management
840 * ----------------
842 if (device->flags.power_manageable) {
843 result = acpi_bus_get_power_flags(device);
844 if (result)
845 goto end;
849 * Wakeup device management
850 *-----------------------
852 if (device->flags.wake_capable) {
853 result = acpi_bus_get_wakeup_device_flags(device);
854 if (result)
855 goto end;
859 * Performance Management
860 * ----------------------
862 if (device->flags.performance_manageable) {
863 result = acpi_bus_get_perf_flags(device);
864 if (result)
865 goto end;
868 if ((result = acpi_device_set_context(device,type)))
869 goto end;
871 acpi_device_get_debug_info(device,handle,type);
873 acpi_device_register(device,parent);
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.
882 if (device->flags.bus_address) {
883 if (device->parent && device->parent->ops.bind)
884 device->parent->ops.bind(device);
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.
894 * TBD: Assumes LDM provides driver hot-plug capability.
896 acpi_bus_find_driver(device);
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);
907 return_VALUE(result);
912 static int acpi_bus_scan (struct acpi_device *start)
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;
922 ACPI_FUNCTION_TRACE("acpi_bus_scan");
924 if (!start)
925 return_VALUE(-EINVAL);
927 parent = start;
928 phandle = start->handle;
931 * Parse through the ACPI namespace, identify all 'devices', and
932 * create a new 'struct acpi_device' for each.
934 while ((level > 0) && parent) {
936 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
937 chandle, &chandle);
940 * If this scope is exhausted then move our way back up.
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;
951 status = acpi_get_type(chandle, &type);
952 if (ACPI_FAILURE(status))
953 continue;
956 * If this is a scope object then parse it (depth-first).
958 if (type == ACPI_TYPE_LOCAL_SCOPE) {
959 level++;
960 phandle = chandle;
961 chandle = NULL;
962 continue;
966 * We're only interested in objects that we consider 'devices'.
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;
985 status = acpi_bus_add(&child, parent, chandle, type);
986 if (ACPI_FAILURE(status))
987 continue;
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).
996 * TBD: Need notifications and other detection mechanisms
997 * in place before we can fully implement this.
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;
1011 return_VALUE(0);
1015 static int
1016 acpi_bus_scan_fixed (
1017 struct acpi_device *root)
1019 int result = 0;
1020 struct acpi_device *device = NULL;
1022 ACPI_FUNCTION_TRACE("acpi_bus_scan_fixed");
1024 if (!root)
1025 return_VALUE(-ENODEV);
1028 * Enumerate all fixed-feature devices.
1030 if (acpi_fadt.pwr_button == 0)
1031 result = acpi_bus_add(&device, acpi_root,
1032 NULL, ACPI_BUS_TYPE_POWER_BUTTON);
1034 if (acpi_fadt.sleep_button == 0)
1035 result = acpi_bus_add(&device, acpi_root,
1036 NULL, ACPI_BUS_TYPE_SLEEP_BUTTON);
1038 return_VALUE(result);
1042 static int __init acpi_scan_init(void)
1044 int result;
1046 ACPI_FUNCTION_TRACE("acpi_scan_init");
1048 if (acpi_disabled)
1049 return_VALUE(0);
1051 kset_register(&acpi_namespace_kset);
1054 * Create the root device in the bus's device tree
1056 result = acpi_bus_add(&acpi_root, NULL, ACPI_ROOT_OBJECT,
1057 ACPI_BUS_TYPE_SYSTEM);
1058 if (result)
1059 goto Done;
1062 * Enumerate devices in the ACPI namespace.
1064 result = acpi_bus_scan_fixed(acpi_root);
1065 if (!result)
1066 result = acpi_bus_scan(acpi_root);
1068 if (result)
1069 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1071 Done:
1072 return_VALUE(result);
1075 subsys_initcall(acpi_scan_init);