2 * scan.c - support for transforming the ACPI namespace into individual objects
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/acpi.h>
9 #include <acpi/acpi_drivers.h>
10 #include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */
12 #define _COMPONENT ACPI_BUS_COMPONENT
13 ACPI_MODULE_NAME("scan")
14 #define STRUCT_TO_INT(s) (*((int*)&s))
15 extern struct acpi_device *acpi_root;
17 #define ACPI_BUS_CLASS "system_bus"
18 #define ACPI_BUS_HID "ACPI_BUS"
19 #define ACPI_BUS_DRIVER_NAME "ACPI Bus Driver"
20 #define ACPI_BUS_DEVICE_NAME "System Bus"
22 static LIST_HEAD(acpi_device_list);
23 DEFINE_SPINLOCK(acpi_device_lock);
24 LIST_HEAD(acpi_wakeup_device_list);
27 static void acpi_device_release(struct kobject *kobj)
29 struct acpi_device *dev = container_of(kobj, struct acpi_device, kobj);
30 kfree(dev->pnp.cid_list);
34 struct acpi_device_attribute {
35 struct attribute attr;
36 ssize_t(*show) (struct acpi_device *, char *);
37 ssize_t(*store) (struct acpi_device *, const char *, size_t);
40 typedef void acpi_device_sysfs_files(struct kobject *,
41 const struct attribute *);
43 static void setup_sys_fs_device_files(struct acpi_device *dev,
44 acpi_device_sysfs_files * func);
46 #define create_sysfs_device_files(dev) \
47 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_create_file)
48 #define remove_sysfs_device_files(dev) \
49 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_remove_file)
51 #define to_acpi_device(n) container_of(n, struct acpi_device, kobj)
52 #define to_handle_attr(n) container_of(n, struct acpi_device_attribute, attr);
54 static ssize_t acpi_device_attr_show(struct kobject *kobj,
55 struct attribute *attr, char *buf)
57 struct acpi_device *device = to_acpi_device(kobj);
58 struct acpi_device_attribute *attribute = to_handle_attr(attr);
59 return attribute->show ? attribute->show(device, buf) : -EIO;
61 static ssize_t acpi_device_attr_store(struct kobject *kobj,
62 struct attribute *attr, const char *buf,
65 struct acpi_device *device = to_acpi_device(kobj);
66 struct acpi_device_attribute *attribute = to_handle_attr(attr);
67 return attribute->store ? attribute->store(device, buf, len) : -EIO;
70 static struct sysfs_ops acpi_device_sysfs_ops = {
71 .show = acpi_device_attr_show,
72 .store = acpi_device_attr_store,
75 static struct kobj_type ktype_acpi_ns = {
76 .sysfs_ops = &acpi_device_sysfs_ops,
77 .release = acpi_device_release,
80 static int namespace_uevent(struct kset *kset, struct kobject *kobj,
81 char **envp, int num_envp, char *buffer,
84 struct acpi_device *dev = to_acpi_device(kobj);
91 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
92 "PHYSDEVDRIVER=%s", dev->driver->name))
100 static struct kset_uevent_ops namespace_uevent_ops = {
101 .uevent = &namespace_uevent,
104 static struct kset acpi_namespace_kset = {
108 .subsys = &acpi_subsys,
109 .ktype = &ktype_acpi_ns,
110 .uevent_ops = &namespace_uevent_ops,
113 static void acpi_device_register(struct acpi_device *device,
114 struct acpi_device *parent)
119 * Link this device to its parent and siblings.
121 INIT_LIST_HEAD(&device->children);
122 INIT_LIST_HEAD(&device->node);
123 INIT_LIST_HEAD(&device->g_list);
124 INIT_LIST_HEAD(&device->wakeup_list);
126 spin_lock(&acpi_device_lock);
127 if (device->parent) {
128 list_add_tail(&device->node, &device->parent->children);
129 list_add_tail(&device->g_list, &device->parent->g_list);
131 list_add_tail(&device->g_list, &acpi_device_list);
132 if (device->wakeup.flags.valid)
133 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
134 spin_unlock(&acpi_device_lock);
136 strlcpy(device->kobj.name, device->pnp.bus_id, KOBJ_NAME_LEN);
138 device->kobj.parent = &parent->kobj;
139 device->kobj.ktype = &ktype_acpi_ns;
140 device->kobj.kset = &acpi_namespace_kset;
141 kobject_register(&device->kobj);
142 create_sysfs_device_files(device);
145 static void acpi_device_unregister(struct acpi_device *device, int type)
147 spin_lock(&acpi_device_lock);
148 if (device->parent) {
149 list_del(&device->node);
150 list_del(&device->g_list);
152 list_del(&device->g_list);
154 list_del(&device->wakeup_list);
156 spin_unlock(&acpi_device_lock);
158 acpi_detach_data(device->handle, acpi_bus_data_handler);
159 remove_sysfs_device_files(device);
160 kobject_unregister(&device->kobj);
163 void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
165 ACPI_FUNCTION_TRACE("acpi_bus_data_handler");
172 static int acpi_bus_get_power_flags(struct acpi_device *device)
174 acpi_status status = 0;
175 acpi_handle handle = NULL;
178 ACPI_FUNCTION_TRACE("acpi_bus_get_power_flags");
181 * Power Management Flags
183 status = acpi_get_handle(device->handle, "_PSC", &handle);
184 if (ACPI_SUCCESS(status))
185 device->power.flags.explicit_get = 1;
186 status = acpi_get_handle(device->handle, "_IRC", &handle);
187 if (ACPI_SUCCESS(status))
188 device->power.flags.inrush_current = 1;
191 * Enumerate supported power management states
193 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
194 struct acpi_device_power_state *ps = &device->power.states[i];
195 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
197 /* Evaluate "_PRx" to se if power resources are referenced */
198 acpi_evaluate_reference(device->handle, object_name, NULL,
200 if (ps->resources.count) {
201 device->power.flags.power_resources = 1;
205 /* Evaluate "_PSx" to see if we can do explicit sets */
206 object_name[2] = 'S';
207 status = acpi_get_handle(device->handle, object_name, &handle);
208 if (ACPI_SUCCESS(status)) {
209 ps->flags.explicit_set = 1;
213 /* State is valid if we have some power control */
214 if (ps->resources.count || ps->flags.explicit_set)
217 ps->power = -1; /* Unknown - driver assigned */
218 ps->latency = -1; /* Unknown - driver assigned */
221 /* Set defaults for D0 and D3 states (always valid) */
222 device->power.states[ACPI_STATE_D0].flags.valid = 1;
223 device->power.states[ACPI_STATE_D0].power = 100;
224 device->power.states[ACPI_STATE_D3].flags.valid = 1;
225 device->power.states[ACPI_STATE_D3].power = 0;
227 /* TBD: System wake support and resource requirements. */
229 device->power.state = ACPI_STATE_UNKNOWN;
234 int acpi_match_ids(struct acpi_device *device, char *ids)
236 if (device->flags.hardware_id)
237 if (strstr(ids, device->pnp.hardware_id))
240 if (device->flags.compatible_ids) {
241 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
244 /* compare multiple _CID entries against driver ids */
245 for (i = 0; i < cid_list->count; i++) {
246 if (strstr(ids, cid_list->id[i].value))
254 acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
255 union acpi_object *package)
258 union acpi_object *element = NULL;
260 if (!device || !package || (package->package.count < 2))
261 return AE_BAD_PARAMETER;
263 element = &(package->package.elements[0]);
265 return AE_BAD_PARAMETER;
266 if (element->type == ACPI_TYPE_PACKAGE) {
267 if ((element->package.count < 2) ||
268 (element->package.elements[0].type !=
269 ACPI_TYPE_LOCAL_REFERENCE)
270 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
272 device->wakeup.gpe_device =
273 element->package.elements[0].reference.handle;
274 device->wakeup.gpe_number =
275 (u32) element->package.elements[1].integer.value;
276 } else if (element->type == ACPI_TYPE_INTEGER) {
277 device->wakeup.gpe_number = element->integer.value;
281 element = &(package->package.elements[1]);
282 if (element->type != ACPI_TYPE_INTEGER) {
285 device->wakeup.sleep_state = element->integer.value;
287 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
290 device->wakeup.resources.count = package->package.count - 2;
291 for (i = 0; i < device->wakeup.resources.count; i++) {
292 element = &(package->package.elements[i + 2]);
293 if (element->type != ACPI_TYPE_ANY) {
297 device->wakeup.resources.handles[i] = element->reference.handle;
303 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
305 acpi_status status = 0;
306 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
307 union acpi_object *package = NULL;
309 ACPI_FUNCTION_TRACE("acpi_bus_get_wakeup_flags");
312 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
313 if (ACPI_FAILURE(status)) {
314 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
318 package = (union acpi_object *)buffer.pointer;
319 status = acpi_bus_extract_wakeup_device_power_package(device, package);
320 if (ACPI_FAILURE(status)) {
321 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
325 acpi_os_free(buffer.pointer);
327 device->wakeup.flags.valid = 1;
328 /* Power button, Lid switch always enable wakeup */
329 if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
330 device->wakeup.flags.run_wake = 1;
333 if (ACPI_FAILURE(status))
334 device->flags.wake_capable = 0;
338 /* --------------------------------------------------------------------------
339 ACPI sysfs device file support
340 -------------------------------------------------------------------------- */
341 static ssize_t acpi_eject_store(struct acpi_device *device,
342 const char *buf, size_t count);
344 #define ACPI_DEVICE_ATTR(_name,_mode,_show,_store) \
345 static struct acpi_device_attribute acpi_device_attr_##_name = \
346 __ATTR(_name, _mode, _show, _store)
348 ACPI_DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
351 * setup_sys_fs_device_files - sets up the device files under device namespace
352 * @dev: acpi_device object
353 * @func: function pointer to create or destroy the device file
356 setup_sys_fs_device_files(struct acpi_device *dev,
357 acpi_device_sysfs_files * func)
360 acpi_handle temp = NULL;
363 * If device has _EJ0, 'eject' file is created that is used to trigger
364 * hot-removal function from userland.
366 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
367 if (ACPI_SUCCESS(status))
368 (*(func)) (&dev->kobj, &acpi_device_attr_eject.attr);
371 static int acpi_eject_operation(acpi_handle handle, int lockable)
373 struct acpi_object_list arg_list;
374 union acpi_object arg;
375 acpi_status status = AE_OK;
378 * TBD: evaluate _PS3?
383 arg_list.pointer = &arg;
384 arg.type = ACPI_TYPE_INTEGER;
385 arg.integer.value = 0;
386 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
390 arg_list.pointer = &arg;
391 arg.type = ACPI_TYPE_INTEGER;
392 arg.integer.value = 1;
398 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
399 if (ACPI_FAILURE(status)) {
407 acpi_eject_store(struct acpi_device *device, const char *buf, size_t count)
414 acpi_object_type type = 0;
416 if ((!count) || (buf[0] != '1')) {
420 if (device->driver == NULL) {
425 status = acpi_get_type(device->handle, &type);
426 if (ACPI_FAILURE(status) || (!device->flags.ejectable)) {
431 islockable = device->flags.lockable;
432 handle = device->handle;
434 result = acpi_bus_trim(device, 1);
437 result = acpi_eject_operation(handle, islockable);
446 /* --------------------------------------------------------------------------
447 Performance Management
448 -------------------------------------------------------------------------- */
450 static int acpi_bus_get_perf_flags(struct acpi_device *device)
452 device->performance.state = ACPI_STATE_UNKNOWN;
456 /* --------------------------------------------------------------------------
458 -------------------------------------------------------------------------- */
460 static LIST_HEAD(acpi_bus_drivers);
463 * acpi_bus_match - match device IDs to driver's supported IDs
464 * @device: the device that we are trying to match to a driver
465 * @driver: driver whose device id table is being checked
467 * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it
468 * matches the specified driver's criteria.
471 acpi_bus_match(struct acpi_device *device, struct acpi_driver *driver)
473 if (driver && driver->ops.match)
474 return driver->ops.match(device, driver);
475 return acpi_match_ids(device, driver->ids);
479 * acpi_bus_driver_init - add a device to a driver
480 * @device: the device to add and initialize
481 * @driver: driver for the device
483 * Used to initialize a device via its device driver. Called whenever a
484 * driver is bound to a device. Invokes the driver's add() and start() ops.
487 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
491 ACPI_FUNCTION_TRACE("acpi_bus_driver_init");
493 if (!device || !driver)
494 return_VALUE(-EINVAL);
496 if (!driver->ops.add)
497 return_VALUE(-ENOSYS);
499 result = driver->ops.add(device);
501 device->driver = NULL;
502 acpi_driver_data(device) = NULL;
503 return_VALUE(result);
506 device->driver = driver;
509 * TBD - Configuration Management: Assign resources to device based
510 * upon possible configuration and currently allocated resources.
513 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
514 "Driver successfully bound to device\n"));
518 static int acpi_start_single_object(struct acpi_device *device)
521 struct acpi_driver *driver;
523 ACPI_FUNCTION_TRACE("acpi_start_single_object");
525 if (!(driver = device->driver))
528 if (driver->ops.start) {
529 result = driver->ops.start(device);
530 if (result && driver->ops.remove)
531 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
534 return_VALUE(result);
537 static void acpi_driver_attach(struct acpi_driver *drv)
539 struct list_head *node, *next;
541 ACPI_FUNCTION_TRACE("acpi_driver_attach");
543 spin_lock(&acpi_device_lock);
544 list_for_each_safe(node, next, &acpi_device_list) {
545 struct acpi_device *dev =
546 container_of(node, struct acpi_device, g_list);
548 if (dev->driver || !dev->status.present)
550 spin_unlock(&acpi_device_lock);
552 if (!acpi_bus_match(dev, drv)) {
553 if (!acpi_bus_driver_init(dev, drv)) {
554 acpi_start_single_object(dev);
555 atomic_inc(&drv->references);
556 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
557 "Found driver [%s] for device [%s]\n",
558 drv->name, dev->pnp.bus_id));
561 spin_lock(&acpi_device_lock);
563 spin_unlock(&acpi_device_lock);
566 static void acpi_driver_detach(struct acpi_driver *drv)
568 struct list_head *node, *next;
570 ACPI_FUNCTION_TRACE("acpi_driver_detach");
572 spin_lock(&acpi_device_lock);
573 list_for_each_safe(node, next, &acpi_device_list) {
574 struct acpi_device *dev =
575 container_of(node, struct acpi_device, g_list);
577 if (dev->driver == drv) {
578 spin_unlock(&acpi_device_lock);
580 drv->ops.remove(dev, ACPI_BUS_REMOVAL_NORMAL);
581 spin_lock(&acpi_device_lock);
583 dev->driver_data = NULL;
584 atomic_dec(&drv->references);
587 spin_unlock(&acpi_device_lock);
591 * acpi_bus_register_driver - register a driver with the ACPI bus
592 * @driver: driver being registered
594 * Registers a driver with the ACPI bus. Searches the namespace for all
595 * devices that match the driver's criteria and binds. Returns zero for
596 * success or a negative error status for failure.
598 int acpi_bus_register_driver(struct acpi_driver *driver)
600 ACPI_FUNCTION_TRACE("acpi_bus_register_driver");
603 return_VALUE(-ENODEV);
605 spin_lock(&acpi_device_lock);
606 list_add_tail(&driver->node, &acpi_bus_drivers);
607 spin_unlock(&acpi_device_lock);
608 acpi_driver_attach(driver);
613 EXPORT_SYMBOL(acpi_bus_register_driver);
616 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
617 * @driver: driver to unregister
619 * Unregisters a driver with the ACPI bus. Searches the namespace for all
620 * devices that match the driver's criteria and unbinds.
622 void acpi_bus_unregister_driver(struct acpi_driver *driver)
624 acpi_driver_detach(driver);
626 if (!atomic_read(&driver->references)) {
627 spin_lock(&acpi_device_lock);
628 list_del_init(&driver->node);
629 spin_unlock(&acpi_device_lock);
634 EXPORT_SYMBOL(acpi_bus_unregister_driver);
637 * acpi_bus_find_driver - check if there is a driver installed for the device
638 * @device: device that we are trying to find a supporting driver for
640 * Parses the list of registered drivers looking for a driver applicable for
641 * the specified device.
643 static int acpi_bus_find_driver(struct acpi_device *device)
646 struct list_head *node, *next;
648 ACPI_FUNCTION_TRACE("acpi_bus_find_driver");
650 spin_lock(&acpi_device_lock);
651 list_for_each_safe(node, next, &acpi_bus_drivers) {
652 struct acpi_driver *driver =
653 container_of(node, struct acpi_driver, node);
655 atomic_inc(&driver->references);
656 spin_unlock(&acpi_device_lock);
657 if (!acpi_bus_match(device, driver)) {
658 result = acpi_bus_driver_init(device, driver);
662 atomic_dec(&driver->references);
663 spin_lock(&acpi_device_lock);
665 spin_unlock(&acpi_device_lock);
668 return_VALUE(result);
671 /* --------------------------------------------------------------------------
673 -------------------------------------------------------------------------- */
675 static int acpi_bus_get_flags(struct acpi_device *device)
677 acpi_status status = AE_OK;
678 acpi_handle temp = NULL;
680 ACPI_FUNCTION_TRACE("acpi_bus_get_flags");
682 /* Presence of _STA indicates 'dynamic_status' */
683 status = acpi_get_handle(device->handle, "_STA", &temp);
684 if (ACPI_SUCCESS(status))
685 device->flags.dynamic_status = 1;
687 /* Presence of _CID indicates 'compatible_ids' */
688 status = acpi_get_handle(device->handle, "_CID", &temp);
689 if (ACPI_SUCCESS(status))
690 device->flags.compatible_ids = 1;
692 /* Presence of _RMV indicates 'removable' */
693 status = acpi_get_handle(device->handle, "_RMV", &temp);
694 if (ACPI_SUCCESS(status))
695 device->flags.removable = 1;
697 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
698 status = acpi_get_handle(device->handle, "_EJD", &temp);
699 if (ACPI_SUCCESS(status))
700 device->flags.ejectable = 1;
702 status = acpi_get_handle(device->handle, "_EJ0", &temp);
703 if (ACPI_SUCCESS(status))
704 device->flags.ejectable = 1;
707 /* Presence of _LCK indicates 'lockable' */
708 status = acpi_get_handle(device->handle, "_LCK", &temp);
709 if (ACPI_SUCCESS(status))
710 device->flags.lockable = 1;
712 /* Presence of _PS0|_PR0 indicates 'power manageable' */
713 status = acpi_get_handle(device->handle, "_PS0", &temp);
714 if (ACPI_FAILURE(status))
715 status = acpi_get_handle(device->handle, "_PR0", &temp);
716 if (ACPI_SUCCESS(status))
717 device->flags.power_manageable = 1;
719 /* Presence of _PRW indicates wake capable */
720 status = acpi_get_handle(device->handle, "_PRW", &temp);
721 if (ACPI_SUCCESS(status))
722 device->flags.wake_capable = 1;
724 /* TBD: Peformance management */
729 static void acpi_device_get_busid(struct acpi_device *device,
730 acpi_handle handle, int type)
732 char bus_id[5] = { '?', 0 };
733 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
739 * The device's Bus ID is simply the object name.
740 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
743 case ACPI_BUS_TYPE_SYSTEM:
744 strcpy(device->pnp.bus_id, "ACPI");
746 case ACPI_BUS_TYPE_POWER_BUTTON:
747 strcpy(device->pnp.bus_id, "PWRF");
749 case ACPI_BUS_TYPE_SLEEP_BUTTON:
750 strcpy(device->pnp.bus_id, "SLPF");
753 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
754 /* Clean up trailing underscores (if any) */
755 for (i = 3; i > 1; i--) {
756 if (bus_id[i] == '_')
761 strcpy(device->pnp.bus_id, bus_id);
766 static void acpi_device_set_id(struct acpi_device *device,
767 struct acpi_device *parent, acpi_handle handle,
770 struct acpi_device_info *info;
771 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
774 struct acpi_compatible_id_list *cid_list = NULL;
778 case ACPI_BUS_TYPE_DEVICE:
779 status = acpi_get_object_info(handle, &buffer);
780 if (ACPI_FAILURE(status)) {
781 printk("%s: Error reading device info\n", __FUNCTION__);
785 info = buffer.pointer;
786 if (info->valid & ACPI_VALID_HID)
787 hid = info->hardware_id.value;
788 if (info->valid & ACPI_VALID_UID)
789 uid = info->unique_id.value;
790 if (info->valid & ACPI_VALID_CID)
791 cid_list = &info->compatibility_id;
792 if (info->valid & ACPI_VALID_ADR) {
793 device->pnp.bus_address = info->address;
794 device->flags.bus_address = 1;
797 case ACPI_BUS_TYPE_POWER:
798 hid = ACPI_POWER_HID;
800 case ACPI_BUS_TYPE_PROCESSOR:
801 hid = ACPI_PROCESSOR_HID;
803 case ACPI_BUS_TYPE_SYSTEM:
804 hid = ACPI_SYSTEM_HID;
806 case ACPI_BUS_TYPE_THERMAL:
807 hid = ACPI_THERMAL_HID;
809 case ACPI_BUS_TYPE_POWER_BUTTON:
810 hid = ACPI_BUTTON_HID_POWERF;
812 case ACPI_BUS_TYPE_SLEEP_BUTTON:
813 hid = ACPI_BUTTON_HID_SLEEPF;
820 * Fix for the system root bus device -- the only root-level device.
822 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
824 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
825 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
829 strcpy(device->pnp.hardware_id, hid);
830 device->flags.hardware_id = 1;
833 strcpy(device->pnp.unique_id, uid);
834 device->flags.unique_id = 1;
837 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
838 if (device->pnp.cid_list)
839 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
841 printk(KERN_ERR "Memory allocation error\n");
844 acpi_os_free(buffer.pointer);
847 static int acpi_device_set_context(struct acpi_device *device, int type)
849 acpi_status status = AE_OK;
854 * Attach this 'struct acpi_device' to the ACPI object. This makes
855 * resolutions from handle->device very efficient. Note that we need
856 * to be careful with fixed-feature devices as they all attach to the
859 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
860 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
861 status = acpi_attach_data(device->handle,
862 acpi_bus_data_handler, device);
864 if (ACPI_FAILURE(status)) {
865 printk("Error attaching device data\n");
872 static void acpi_device_get_debug_info(struct acpi_device *device,
873 acpi_handle handle, int type)
875 #ifdef CONFIG_ACPI_DEBUG_OUTPUT
876 char *type_string = NULL;
877 char name[80] = { '?', '\0' };
878 struct acpi_buffer buffer = { sizeof(name), name };
881 case ACPI_BUS_TYPE_DEVICE:
882 type_string = "Device";
883 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
885 case ACPI_BUS_TYPE_POWER:
886 type_string = "Power Resource";
887 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
889 case ACPI_BUS_TYPE_PROCESSOR:
890 type_string = "Processor";
891 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
893 case ACPI_BUS_TYPE_SYSTEM:
894 type_string = "System";
895 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
897 case ACPI_BUS_TYPE_THERMAL:
898 type_string = "Thermal Zone";
899 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
901 case ACPI_BUS_TYPE_POWER_BUTTON:
902 type_string = "Power Button";
903 sprintf(name, "PWRB");
905 case ACPI_BUS_TYPE_SLEEP_BUTTON:
906 type_string = "Sleep Button";
907 sprintf(name, "SLPB");
911 printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
912 #endif /*CONFIG_ACPI_DEBUG_OUTPUT */
915 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
918 struct acpi_driver *driver;
920 ACPI_FUNCTION_TRACE("acpi_bus_remove");
923 return_VALUE(-EINVAL);
925 driver = dev->driver;
927 if ((driver) && (driver->ops.remove)) {
929 if (driver->ops.stop) {
930 result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT);
932 return_VALUE(result);
935 result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT);
937 return_VALUE(result);
940 atomic_dec(&dev->driver->references);
942 acpi_driver_data(dev) = NULL;
948 if (dev->flags.bus_address) {
949 if ((dev->parent) && (dev->parent->ops.unbind))
950 dev->parent->ops.unbind(dev);
953 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
959 acpi_add_single_object(struct acpi_device **child,
960 struct acpi_device *parent, acpi_handle handle, int type)
963 struct acpi_device *device = NULL;
965 ACPI_FUNCTION_TRACE("acpi_add_single_object");
968 return_VALUE(-EINVAL);
970 device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
972 printk(KERN_ERR PREFIX "Memory allocation error\n");
973 return_VALUE(-ENOMEM);
975 memset(device, 0, sizeof(struct acpi_device));
977 device->handle = handle;
978 device->parent = parent;
980 acpi_device_get_busid(device, handle, type);
985 * Get prior to calling acpi_bus_get_status() so we know whether
986 * or not _STA is present. Note that we only look for object
987 * handles -- cannot evaluate objects until we know the device is
988 * present and properly initialized.
990 result = acpi_bus_get_flags(device);
997 * See if the device is present. We always assume that non-Device
998 * and non-Processor objects (e.g. thermal zones, power resources,
999 * etc.) are present, functioning, etc. (at least when parent object
1000 * is present). Note that _STA has a different meaning for some
1001 * objects (e.g. power resources) so we need to be careful how we use
1005 case ACPI_BUS_TYPE_PROCESSOR:
1006 case ACPI_BUS_TYPE_DEVICE:
1007 result = acpi_bus_get_status(device);
1008 if (ACPI_FAILURE(result) || !device->status.present) {
1014 STRUCT_TO_INT(device->status) = 0x0F;
1021 * TBD: Synch with Core's enumeration/initialization process.
1025 * Hardware ID, Unique ID, & Bus Address
1026 * -------------------------------------
1028 acpi_device_set_id(device, parent, handle, type);
1034 if (device->flags.power_manageable) {
1035 result = acpi_bus_get_power_flags(device);
1041 * Wakeup device management
1042 *-----------------------
1044 if (device->flags.wake_capable) {
1045 result = acpi_bus_get_wakeup_device_flags(device);
1051 * Performance Management
1052 * ----------------------
1054 if (device->flags.performance_manageable) {
1055 result = acpi_bus_get_perf_flags(device);
1060 if ((result = acpi_device_set_context(device, type)))
1063 acpi_device_get_debug_info(device, handle, type);
1065 acpi_device_register(device, parent);
1068 * Bind _ADR-Based Devices
1069 * -----------------------
1070 * If there's a a bus address (_ADR) then we utilize the parent's
1071 * 'bind' function (if exists) to bind the ACPI- and natively-
1072 * enumerated device representations.
1074 if (device->flags.bus_address) {
1075 if (device->parent && device->parent->ops.bind)
1076 device->parent->ops.bind(device);
1080 * Locate & Attach Driver
1081 * ----------------------
1082 * If there's a hardware id (_HID) or compatible ids (_CID) we check
1083 * to see if there's a driver installed for this kind of device. Note
1084 * that drivers can install before or after a device is enumerated.
1086 * TBD: Assumes LDM provides driver hot-plug capability.
1088 acpi_bus_find_driver(device);
1094 kfree(device->pnp.cid_list);
1098 return_VALUE(result);
1101 static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
1103 acpi_status status = AE_OK;
1104 struct acpi_device *parent = NULL;
1105 struct acpi_device *child = NULL;
1106 acpi_handle phandle = NULL;
1107 acpi_handle chandle = NULL;
1108 acpi_object_type type = 0;
1111 ACPI_FUNCTION_TRACE("acpi_bus_scan");
1114 return_VALUE(-EINVAL);
1117 phandle = start->handle;
1120 * Parse through the ACPI namespace, identify all 'devices', and
1121 * create a new 'struct acpi_device' for each.
1123 while ((level > 0) && parent) {
1125 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1129 * If this scope is exhausted then move our way back up.
1131 if (ACPI_FAILURE(status)) {
1134 acpi_get_parent(phandle, &phandle);
1136 parent = parent->parent;
1140 status = acpi_get_type(chandle, &type);
1141 if (ACPI_FAILURE(status))
1145 * If this is a scope object then parse it (depth-first).
1147 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1155 * We're only interested in objects that we consider 'devices'.
1158 case ACPI_TYPE_DEVICE:
1159 type = ACPI_BUS_TYPE_DEVICE;
1161 case ACPI_TYPE_PROCESSOR:
1162 type = ACPI_BUS_TYPE_PROCESSOR;
1164 case ACPI_TYPE_THERMAL:
1165 type = ACPI_BUS_TYPE_THERMAL;
1167 case ACPI_TYPE_POWER:
1168 type = ACPI_BUS_TYPE_POWER;
1174 if (ops->acpi_op_add)
1175 status = acpi_add_single_object(&child, parent,
1178 status = acpi_bus_get_device(chandle, &child);
1180 if (ACPI_FAILURE(status))
1183 if (ops->acpi_op_start) {
1184 status = acpi_start_single_object(child);
1185 if (ACPI_FAILURE(status))
1190 * If the device is present, enabled, and functioning then
1191 * parse its scope (depth-first). Note that we need to
1192 * represent absent devices to facilitate PnP notifications
1193 * -- but only the subtree head (not all of its children,
1194 * which will be enumerated when the parent is inserted).
1196 * TBD: Need notifications and other detection mechanisms
1197 * in place before we can fully implement this.
1199 if (child->status.present) {
1200 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1202 if (ACPI_SUCCESS(status)) {
1215 acpi_bus_add(struct acpi_device **child,
1216 struct acpi_device *parent, acpi_handle handle, int type)
1219 struct acpi_bus_ops ops;
1221 ACPI_FUNCTION_TRACE("acpi_bus_add");
1223 result = acpi_add_single_object(child, parent, handle, type);
1225 memset(&ops, 0, sizeof(ops));
1226 ops.acpi_op_add = 1;
1227 result = acpi_bus_scan(*child, &ops);
1229 return_VALUE(result);
1232 EXPORT_SYMBOL(acpi_bus_add);
1234 int acpi_bus_start(struct acpi_device *device)
1237 struct acpi_bus_ops ops;
1239 ACPI_FUNCTION_TRACE("acpi_bus_start");
1242 return_VALUE(-EINVAL);
1244 result = acpi_start_single_object(device);
1246 memset(&ops, 0, sizeof(ops));
1247 ops.acpi_op_start = 1;
1248 result = acpi_bus_scan(device, &ops);
1250 return_VALUE(result);
1253 EXPORT_SYMBOL(acpi_bus_start);
1255 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1258 struct acpi_device *parent, *child;
1259 acpi_handle phandle, chandle;
1260 acpi_object_type type;
1265 phandle = start->handle;
1266 child = chandle = NULL;
1268 while ((level > 0) && parent && (!err)) {
1269 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1273 * If this scope is exhausted then move our way back up.
1275 if (ACPI_FAILURE(status)) {
1278 acpi_get_parent(phandle, &phandle);
1280 parent = parent->parent;
1283 err = acpi_bus_remove(child, rmdevice);
1285 err = acpi_bus_remove(child, 1);
1290 status = acpi_get_type(chandle, &type);
1291 if (ACPI_FAILURE(status)) {
1295 * If there is a device corresponding to chandle then
1296 * parse it (depth-first).
1298 if (acpi_bus_get_device(chandle, &child) == 0) {
1308 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1311 static int acpi_bus_scan_fixed(struct acpi_device *root)
1314 struct acpi_device *device = NULL;
1316 ACPI_FUNCTION_TRACE("acpi_bus_scan_fixed");
1319 return_VALUE(-ENODEV);
1322 * Enumerate all fixed-feature devices.
1324 if (acpi_fadt.pwr_button == 0) {
1325 result = acpi_add_single_object(&device, acpi_root,
1327 ACPI_BUS_TYPE_POWER_BUTTON);
1329 result = acpi_start_single_object(device);
1332 if (acpi_fadt.sleep_button == 0) {
1333 result = acpi_add_single_object(&device, acpi_root,
1335 ACPI_BUS_TYPE_SLEEP_BUTTON);
1337 result = acpi_start_single_object(device);
1340 return_VALUE(result);
1344 static inline struct acpi_device * to_acpi_dev(struct device * dev)
1346 return container_of(dev, struct acpi_device, dev);
1350 static int root_suspend(struct acpi_device * acpi_dev, pm_message_t state)
1352 struct acpi_device * dev, * next;
1355 spin_lock(&acpi_device_lock);
1356 list_for_each_entry_safe_reverse(dev, next, &acpi_device_list, g_list) {
1357 if (dev->driver && dev->driver->ops.suspend) {
1358 spin_unlock(&acpi_device_lock);
1359 result = dev->driver->ops.suspend(dev, 0);
1361 printk(KERN_ERR PREFIX "[%s - %s] Suspend failed: %d\n",
1362 acpi_device_name(dev),
1363 acpi_device_bid(dev), result);
1365 spin_lock(&acpi_device_lock);
1368 spin_unlock(&acpi_device_lock);
1373 static int acpi_device_suspend(struct device * dev, pm_message_t state)
1375 struct acpi_device * acpi_dev = to_acpi_dev(dev);
1378 * For now, we should only register 1 generic device -
1379 * the ACPI root device - and from there, we walk the
1380 * tree of ACPI devices to suspend each one using the
1381 * ACPI driver methods.
1383 if (acpi_dev->handle == ACPI_ROOT_OBJECT)
1384 root_suspend(acpi_dev, state);
1390 static int root_resume(struct acpi_device * acpi_dev)
1392 struct acpi_device * dev, * next;
1395 spin_lock(&acpi_device_lock);
1396 list_for_each_entry_safe(dev, next, &acpi_device_list, g_list) {
1397 if (dev->driver && dev->driver->ops.resume) {
1398 spin_unlock(&acpi_device_lock);
1399 result = dev->driver->ops.resume(dev, 0);
1401 printk(KERN_ERR PREFIX "[%s - %s] resume failed: %d\n",
1402 acpi_device_name(dev),
1403 acpi_device_bid(dev), result);
1405 spin_lock(&acpi_device_lock);
1408 spin_unlock(&acpi_device_lock);
1413 static int acpi_device_resume(struct device * dev)
1415 struct acpi_device * acpi_dev = to_acpi_dev(dev);
1418 * For now, we should only register 1 generic device -
1419 * the ACPI root device - and from there, we walk the
1420 * tree of ACPI devices to resume each one using the
1421 * ACPI driver methods.
1423 if (acpi_dev->handle == ACPI_ROOT_OBJECT)
1424 root_resume(acpi_dev);
1429 struct bus_type acpi_bus_type = {
1431 .suspend = acpi_device_suspend,
1432 .resume = acpi_device_resume,
1437 static int __init acpi_scan_init(void)
1440 struct acpi_bus_ops ops;
1442 ACPI_FUNCTION_TRACE("acpi_scan_init");
1447 kset_register(&acpi_namespace_kset);
1449 result = bus_register(&acpi_bus_type);
1451 /* We don't want to quit even if we failed to add suspend/resume */
1452 printk(KERN_ERR PREFIX "Could not register bus type\n");
1456 * Create the root device in the bus's device tree
1458 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
1459 ACPI_BUS_TYPE_SYSTEM);
1463 result = acpi_start_single_object(acpi_root);
1467 acpi_root->dev.bus = &acpi_bus_type;
1468 snprintf(acpi_root->dev.bus_id, BUS_ID_SIZE, "%s", acpi_bus_type.name);
1469 result = device_register(&acpi_root->dev);
1471 /* We don't want to quit even if we failed to add suspend/resume */
1472 printk(KERN_ERR PREFIX "Could not register device\n");
1476 * Enumerate devices in the ACPI namespace.
1478 result = acpi_bus_scan_fixed(acpi_root);
1480 memset(&ops, 0, sizeof(ops));
1481 ops.acpi_op_add = 1;
1482 ops.acpi_op_start = 1;
1483 result = acpi_bus_scan(acpi_root, &ops);
1487 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1490 return_VALUE(result);
1493 subsys_initcall(acpi_scan_init);