2 * scan.c - support for transforming the ACPI namespace into individual objects
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 static spinlock_t acpi_device_lock = SPIN_LOCK_UNLOCKED;
28 static void acpi_device_release(struct kobject * kobj)
30 struct acpi_device * dev = container_of(kobj,struct acpi_device,kobj);
31 if (dev->pnp.cid_list)
32 kfree(dev->pnp.cid_list);
36 static struct kobj_type ktype_acpi_ns = {
37 .release = acpi_device_release,
40 static struct kset acpi_namespace_kset = {
44 .subsys = &acpi_subsys,
45 .ktype = &ktype_acpi_ns,
49 static void acpi_device_register(struct acpi_device * device, struct acpi_device * parent)
54 * Link this device to its parent and siblings.
56 INIT_LIST_HEAD(&device->children);
57 INIT_LIST_HEAD(&device->node);
58 INIT_LIST_HEAD(&device->g_list);
60 spin_lock(&acpi_device_lock);
62 list_add_tail(&device->node, &device->parent->children);
63 list_add_tail(&device->g_list,&device->parent->g_list);
65 list_add_tail(&device->g_list,&acpi_device_list);
66 spin_unlock(&acpi_device_lock);
68 kobject_init(&device->kobj);
69 strlcpy(device->kobj.name,device->pnp.bus_id,KOBJ_NAME_LEN);
71 device->kobj.parent = &parent->kobj;
72 device->kobj.ktype = &ktype_acpi_ns;
73 device->kobj.kset = &acpi_namespace_kset;
74 kobject_add(&device->kobj);
78 acpi_device_unregister (
79 struct acpi_device *device,
82 kobject_unregister(&device->kobj);
87 acpi_bus_data_handler (
92 ACPI_FUNCTION_TRACE("acpi_bus_data_handler");
100 acpi_bus_get_power_flags (
101 struct acpi_device *device)
103 acpi_status status = 0;
104 acpi_handle handle = 0;
107 ACPI_FUNCTION_TRACE("acpi_bus_get_power_flags");
110 * Power Management Flags
112 status = acpi_get_handle(device->handle, "_PSC", &handle);
113 if (ACPI_SUCCESS(status))
114 device->power.flags.explicit_get = 1;
115 status = acpi_get_handle(device->handle, "_IRC", &handle);
116 if (ACPI_SUCCESS(status))
117 device->power.flags.inrush_current = 1;
118 status = acpi_get_handle(device->handle, "_PRW", &handle);
119 if (ACPI_SUCCESS(status))
120 device->power.flags.wake_capable = 1;
123 * Enumerate supported power management states
125 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
126 struct acpi_device_power_state *ps = &device->power.states[i];
127 char object_name[5] = {'_','P','R','0'+i,'\0'};
129 /* Evaluate "_PRx" to se if power resources are referenced */
130 acpi_evaluate_reference(device->handle, object_name, NULL,
132 if (ps->resources.count) {
133 device->power.flags.power_resources = 1;
137 /* Evaluate "_PSx" to see if we can do explicit sets */
138 object_name[2] = 'S';
139 status = acpi_get_handle(device->handle, object_name, &handle);
140 if (ACPI_SUCCESS(status)) {
141 ps->flags.explicit_set = 1;
145 /* State is valid if we have some power control */
146 if (ps->resources.count || ps->flags.explicit_set)
149 ps->power = -1; /* Unknown - driver assigned */
150 ps->latency = -1; /* Unknown - driver assigned */
153 /* Set defaults for D0 and D3 states (always valid) */
154 device->power.states[ACPI_STATE_D0].flags.valid = 1;
155 device->power.states[ACPI_STATE_D0].power = 100;
156 device->power.states[ACPI_STATE_D3].flags.valid = 1;
157 device->power.states[ACPI_STATE_D3].power = 0;
159 /* TBD: System wake support and resource requirements. */
161 device->power.state = ACPI_STATE_UNKNOWN;
167 /* --------------------------------------------------------------------------
168 Performance Management
169 -------------------------------------------------------------------------- */
172 acpi_bus_get_perf_flags (
173 struct acpi_device *device)
175 device->performance.state = ACPI_STATE_UNKNOWN;
179 /* --------------------------------------------------------------------------
181 -------------------------------------------------------------------------- */
183 static LIST_HEAD(acpi_bus_drivers);
184 static DECLARE_MUTEX(acpi_bus_drivers_lock);
190 * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it
191 * matches the specified driver's criteria.
195 struct acpi_device *device,
196 struct acpi_driver *driver)
199 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
201 if (device->flags.hardware_id)
202 if (strstr(driver->ids, device->pnp.hardware_id))
205 if (device->flags.compatible_ids) {
206 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
209 /* compare multiple _CID entries against driver ids */
210 for (i = 0; i < cid_list->count; i++)
212 if (strstr(driver->ids, cid_list->id[i].value))
220 acpi_os_free(buffer.pointer);
226 * acpi_bus_driver_init
227 * --------------------
228 * Used to initialize a device via its device driver. Called whenever a
229 * driver is bound to a device. Invokes the driver's add() and start() ops.
232 acpi_bus_driver_init (
233 struct acpi_device *device,
234 struct acpi_driver *driver)
238 ACPI_FUNCTION_TRACE("acpi_bus_driver_init");
240 if (!device || !driver)
241 return_VALUE(-EINVAL);
243 if (!driver->ops.add)
244 return_VALUE(-ENOSYS);
246 result = driver->ops.add(device);
248 device->driver = NULL;
249 acpi_driver_data(device) = NULL;
250 return_VALUE(result);
253 device->driver = driver;
256 * TBD - Configuration Management: Assign resources to device based
257 * upon possible configuration and currently allocated resources.
260 if (driver->ops.start) {
261 result = driver->ops.start(device);
262 if (result && driver->ops.remove)
263 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
264 return_VALUE(result);
267 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Driver successfully bound to device\n"));
269 if (driver->ops.scan) {
270 driver->ops.scan(device);
276 static int acpi_driver_attach(struct acpi_driver * drv)
278 struct list_head * node, * next;
280 ACPI_FUNCTION_TRACE("acpi_driver_attach");
282 spin_lock(&acpi_device_lock);
283 list_for_each_safe(node, next, &acpi_device_list) {
284 struct acpi_device * dev = container_of(node, struct acpi_device, g_list);
286 if (dev->driver || !dev->status.present)
288 spin_unlock(&acpi_device_lock);
290 if (!acpi_bus_match(dev, drv)) {
291 if (!acpi_bus_driver_init(dev, drv)) {
292 atomic_inc(&drv->references);
293 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
294 drv->name, dev->pnp.bus_id));
297 spin_lock(&acpi_device_lock);
299 spin_unlock(&acpi_device_lock);
303 static int acpi_driver_detach(struct acpi_driver * drv)
305 struct list_head * node, * next;
307 ACPI_FUNCTION_TRACE("acpi_driver_detach");
309 spin_lock(&acpi_device_lock);
310 list_for_each_safe(node,next,&acpi_device_list) {
311 struct acpi_device * dev = container_of(node,struct acpi_device,g_list);
313 if (dev->driver == drv) {
314 spin_unlock(&acpi_device_lock);
316 drv->ops.remove(dev,ACPI_BUS_REMOVAL_NORMAL);
317 spin_lock(&acpi_device_lock);
319 dev->driver_data = NULL;
320 atomic_dec(&drv->references);
323 spin_unlock(&acpi_device_lock);
328 * acpi_bus_register_driver
329 * ------------------------
330 * Registers a driver with the ACPI bus. Searches the namespace for all
331 * devices that match the driver's criteria and binds.
334 acpi_bus_register_driver (
335 struct acpi_driver *driver)
339 ACPI_FUNCTION_TRACE("acpi_bus_register_driver");
342 return_VALUE(-ENODEV);
345 spin_lock(&acpi_device_lock);
346 list_add_tail(&driver->node, &acpi_bus_drivers);
347 spin_unlock(&acpi_device_lock);
348 acpi_driver_attach(driver);
357 * acpi_bus_unregister_driver
358 * --------------------------
359 * Unregisters a driver with the ACPI bus. Searches the namespace for all
360 * devices that match the driver's criteria and unbinds.
363 acpi_bus_unregister_driver (
364 struct acpi_driver *driver)
368 ACPI_FUNCTION_TRACE("acpi_bus_unregister_driver");
371 acpi_driver_detach(driver);
373 if (!atomic_read(&driver->references)) {
374 spin_lock(&acpi_device_lock);
375 list_del_init(&driver->node);
376 spin_unlock(&acpi_device_lock);
384 * acpi_bus_find_driver
385 * --------------------
386 * Parses the list of registered drivers looking for a driver applicable for
387 * the specified device.
390 acpi_bus_find_driver (
391 struct acpi_device *device)
394 struct list_head * node, *next;
396 ACPI_FUNCTION_TRACE("acpi_bus_find_driver");
398 if (!device->flags.hardware_id && !device->flags.compatible_ids)
401 spin_lock(&acpi_device_lock);
402 list_for_each_safe(node,next,&acpi_bus_drivers) {
403 struct acpi_driver * driver = container_of(node,struct acpi_driver,node);
405 atomic_inc(&driver->references);
406 spin_unlock(&acpi_device_lock);
407 if (!acpi_bus_match(device, driver)) {
408 result = acpi_bus_driver_init(device, driver);
412 atomic_dec(&driver->references);
413 spin_lock(&acpi_device_lock);
415 spin_unlock(&acpi_device_lock);
418 return_VALUE(result);
422 /* --------------------------------------------------------------------------
424 -------------------------------------------------------------------------- */
428 struct acpi_device *device)
430 acpi_status status = AE_OK;
431 acpi_handle temp = NULL;
433 ACPI_FUNCTION_TRACE("acpi_bus_get_flags");
435 /* Presence of _STA indicates 'dynamic_status' */
436 status = acpi_get_handle(device->handle, "_STA", &temp);
437 if (ACPI_SUCCESS(status))
438 device->flags.dynamic_status = 1;
440 /* Presence of _CID indicates 'compatible_ids' */
441 status = acpi_get_handle(device->handle, "_CID", &temp);
442 if (ACPI_SUCCESS(status))
443 device->flags.compatible_ids = 1;
445 /* Presence of _RMV indicates 'removable' */
446 status = acpi_get_handle(device->handle, "_RMV", &temp);
447 if (ACPI_SUCCESS(status))
448 device->flags.removable = 1;
450 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
451 status = acpi_get_handle(device->handle, "_EJD", &temp);
452 if (ACPI_SUCCESS(status))
453 device->flags.ejectable = 1;
455 status = acpi_get_handle(device->handle, "_EJ0", &temp);
456 if (ACPI_SUCCESS(status))
457 device->flags.ejectable = 1;
460 /* Presence of _LCK indicates 'lockable' */
461 status = acpi_get_handle(device->handle, "_LCK", &temp);
462 if (ACPI_SUCCESS(status))
463 device->flags.lockable = 1;
465 /* Presence of _PS0|_PR0 indicates 'power manageable' */
466 status = acpi_get_handle(device->handle, "_PS0", &temp);
467 if (ACPI_FAILURE(status))
468 status = acpi_get_handle(device->handle, "_PR0", &temp);
469 if (ACPI_SUCCESS(status))
470 device->flags.power_manageable = 1;
472 /* TBD: Peformance management */
477 static void acpi_device_get_busid(struct acpi_device * device, acpi_handle handle, int type)
479 char bus_id[5] = {'?',0};
480 struct acpi_buffer buffer = {sizeof(bus_id), bus_id};
486 * The device's Bus ID is simply the object name.
487 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
490 case ACPI_BUS_TYPE_SYSTEM:
491 strcpy(device->pnp.bus_id, "ACPI");
493 case ACPI_BUS_TYPE_POWER_BUTTON:
494 strcpy(device->pnp.bus_id, "PWRF");
496 case ACPI_BUS_TYPE_SLEEP_BUTTON:
497 strcpy(device->pnp.bus_id, "SLPF");
500 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
501 /* Clean up trailing underscores (if any) */
502 for (i = 3; i > 1; i--) {
503 if (bus_id[i] == '_')
508 strcpy(device->pnp.bus_id, bus_id);
513 static void acpi_device_set_id(struct acpi_device * device, struct acpi_device * parent,
514 acpi_handle handle, int type)
516 struct acpi_device_info *info;
517 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
520 struct acpi_compatible_id_list *cid_list = NULL;
524 case ACPI_BUS_TYPE_DEVICE:
525 status = acpi_get_object_info(handle, &buffer);
526 if (ACPI_FAILURE(status)) {
527 printk("%s: Error reading device info\n",__FUNCTION__);
531 info = buffer.pointer;
532 if (info->valid & ACPI_VALID_HID)
533 hid = info->hardware_id.value;
534 if (info->valid & ACPI_VALID_UID)
535 uid = info->unique_id.value;
536 if (info->valid & ACPI_VALID_CID)
537 cid_list = &info->compatibility_id;
538 if (info->valid & ACPI_VALID_ADR) {
539 device->pnp.bus_address = info->address;
540 device->flags.bus_address = 1;
543 case ACPI_BUS_TYPE_POWER:
544 hid = ACPI_POWER_HID;
546 case ACPI_BUS_TYPE_PROCESSOR:
547 hid = ACPI_PROCESSOR_HID;
549 case ACPI_BUS_TYPE_SYSTEM:
550 hid = ACPI_SYSTEM_HID;
552 case ACPI_BUS_TYPE_THERMAL:
553 hid = ACPI_THERMAL_HID;
555 case ACPI_BUS_TYPE_POWER_BUTTON:
556 hid = ACPI_BUTTON_HID_POWERF;
558 case ACPI_BUS_TYPE_SLEEP_BUTTON:
559 hid = ACPI_BUTTON_HID_SLEEPF;
566 * Fix for the system root bus device -- the only root-level device.
568 if ((parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
570 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
571 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
575 strcpy(device->pnp.hardware_id, hid);
576 device->flags.hardware_id = 1;
579 strcpy(device->pnp.unique_id, uid);
580 device->flags.unique_id = 1;
583 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
584 if (device->pnp.cid_list)
585 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
587 printk(KERN_ERR "Memory allocation error\n");
590 acpi_os_free(buffer.pointer);
593 int acpi_device_set_context(struct acpi_device * device, int type)
595 acpi_status status = AE_OK;
600 * Attach this 'struct acpi_device' to the ACPI object. This makes
601 * resolutions from handle->device very efficient. Note that we need
602 * to be careful with fixed-feature devices as they all attach to the
605 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
606 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
607 status = acpi_attach_data(device->handle,
608 acpi_bus_data_handler, device);
610 if (ACPI_FAILURE(status)) {
611 printk("Error attaching device data\n");
618 void acpi_device_get_debug_info(struct acpi_device * device, acpi_handle handle, int type)
620 #ifdef CONFIG_ACPI_DEBUG_OUTPUT
621 char *type_string = NULL;
622 char name[80] = {'?','\0'};
623 acpi_buffer buffer = {sizeof(name), name};
626 case ACPI_BUS_TYPE_DEVICE:
627 type_string = "Device";
628 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
630 case ACPI_BUS_TYPE_POWER:
631 type_string = "Power Resource";
632 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
634 case ACPI_BUS_TYPE_PROCESSOR:
635 type_string = "Processor";
636 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
638 case ACPI_BUS_TYPE_SYSTEM:
639 type_string = "System";
640 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
642 case ACPI_BUS_TYPE_THERMAL:
643 type_string = "Thermal Zone";
644 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
646 case ACPI_BUS_TYPE_POWER_BUTTON:
647 type_string = "Power Button";
648 sprintf(name, "PWRB");
650 case ACPI_BUS_TYPE_SLEEP_BUTTON:
651 type_string = "Sleep Button";
652 sprintf(name, "SLPB");
656 printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
657 #endif /*CONFIG_ACPI_DEBUG_OUTPUT*/
662 struct acpi_device **child,
663 struct acpi_device *parent,
668 struct acpi_device *device = NULL;
670 ACPI_FUNCTION_TRACE("acpi_bus_add");
673 return_VALUE(-EINVAL);
675 device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
677 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Memory allocation error\n"));
678 return_VALUE(-ENOMEM);
680 memset(device, 0, sizeof(struct acpi_device));
682 device->handle = handle;
683 device->parent = parent;
685 acpi_device_get_busid(device,handle,type);
690 * Get prior to calling acpi_bus_get_status() so we know whether
691 * or not _STA is present. Note that we only look for object
692 * handles -- cannot evaluate objects until we know the device is
693 * present and properly initialized.
695 result = acpi_bus_get_flags(device);
702 * See if the device is present. We always assume that non-Device()
703 * objects (e.g. thermal zones, power resources, processors, etc.) are
704 * present, functioning, etc. (at least when parent object is present).
705 * Note that _STA has a different meaning for some objects (e.g.
706 * power resources) so we need to be careful how we use it.
709 case ACPI_BUS_TYPE_DEVICE:
710 result = acpi_bus_get_status(device);
711 if (ACPI_FAILURE(result) || !device->status.present) {
717 STRUCT_TO_INT(device->status) = 0x0F;
724 * TBD: Synch with Core's enumeration/initialization process.
728 * Hardware ID, Unique ID, & Bus Address
729 * -------------------------------------
731 acpi_device_set_id(device,parent,handle,type);
737 if (device->flags.power_manageable) {
738 result = acpi_bus_get_power_flags(device);
744 * Performance Management
745 * ----------------------
747 if (device->flags.performance_manageable) {
748 result = acpi_bus_get_perf_flags(device);
753 if ((result = acpi_device_set_context(device,type)))
756 acpi_device_get_debug_info(device,handle,type);
758 acpi_device_register(device,parent);
761 * Bind _ADR-Based Devices
762 * -----------------------
763 * If there's a a bus address (_ADR) then we utilize the parent's
764 * 'bind' function (if exists) to bind the ACPI- and natively-
765 * enumerated device representations.
767 if (device->flags.bus_address) {
768 if (device->parent && device->parent->ops.bind)
769 device->parent->ops.bind(device);
773 * Locate & Attach Driver
774 * ----------------------
775 * If there's a hardware id (_HID) or compatible ids (_CID) we check
776 * to see if there's a driver installed for this kind of device. Note
777 * that drivers can install before or after a device is enumerated.
779 * TBD: Assumes LDM provides driver hot-plug capability.
781 acpi_bus_find_driver(device);
787 if (device->pnp.cid_list)
788 kfree(device->pnp.cid_list);
792 return_VALUE(result);
797 static int acpi_bus_scan (struct acpi_device *start)
799 acpi_status status = AE_OK;
800 struct acpi_device *parent = NULL;
801 struct acpi_device *child = NULL;
802 acpi_handle phandle = 0;
803 acpi_handle chandle = 0;
804 acpi_object_type type = 0;
807 ACPI_FUNCTION_TRACE("acpi_bus_scan");
810 return_VALUE(-EINVAL);
813 phandle = start->handle;
816 * Parse through the ACPI namespace, identify all 'devices', and
817 * create a new 'struct acpi_device' for each.
819 while ((level > 0) && parent) {
821 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
825 * If this scope is exhausted then move our way back up.
827 if (ACPI_FAILURE(status)) {
830 acpi_get_parent(phandle, &phandle);
832 parent = parent->parent;
836 status = acpi_get_type(chandle, &type);
837 if (ACPI_FAILURE(status))
841 * If this is a scope object then parse it (depth-first).
843 if (type == ACPI_TYPE_LOCAL_SCOPE) {
851 * We're only interested in objects that we consider 'devices'.
854 case ACPI_TYPE_DEVICE:
855 type = ACPI_BUS_TYPE_DEVICE;
857 case ACPI_TYPE_PROCESSOR:
858 type = ACPI_BUS_TYPE_PROCESSOR;
860 case ACPI_TYPE_THERMAL:
861 type = ACPI_BUS_TYPE_THERMAL;
863 case ACPI_TYPE_POWER:
864 type = ACPI_BUS_TYPE_POWER;
870 status = acpi_bus_add(&child, parent, chandle, type);
871 if (ACPI_FAILURE(status))
875 * If the device is present, enabled, and functioning then
876 * parse its scope (depth-first). Note that we need to
877 * represent absent devices to facilitate PnP notifications
878 * -- but only the subtree head (not all of its children,
879 * which will be enumerated when the parent is inserted).
881 * TBD: Need notifications and other detection mechanisms
882 * in place before we can fully implement this.
884 if (child->status.present) {
885 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
887 if (ACPI_SUCCESS(status)) {
901 acpi_bus_scan_fixed (
902 struct acpi_device *root)
905 struct acpi_device *device = NULL;
907 ACPI_FUNCTION_TRACE("acpi_bus_scan_fixed");
910 return_VALUE(-ENODEV);
913 * Enumerate all fixed-feature devices.
915 if (acpi_fadt.pwr_button == 0)
916 result = acpi_bus_add(&device, acpi_root,
917 NULL, ACPI_BUS_TYPE_POWER_BUTTON);
919 if (acpi_fadt.sleep_button == 0)
920 result = acpi_bus_add(&device, acpi_root,
921 NULL, ACPI_BUS_TYPE_SLEEP_BUTTON);
923 return_VALUE(result);
927 static int __init acpi_scan_init(void)
931 ACPI_FUNCTION_TRACE("acpi_scan_init");
936 kset_register(&acpi_namespace_kset);
939 * Create the root device in the bus's device tree
941 result = acpi_bus_add(&acpi_root, NULL, ACPI_ROOT_OBJECT,
942 ACPI_BUS_TYPE_SYSTEM);
947 * Enumerate devices in the ACPI namespace.
949 result = acpi_bus_scan_fixed(acpi_root);
951 result = acpi_bus_scan(acpi_root);
954 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
957 return_VALUE(result);
960 subsys_initcall(acpi_scan_init);