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/kernel.h>
8 #include <linux/acpi.h>
10 #include <acpi/acpi_drivers.h>
11 #include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */
13 #define _COMPONENT ACPI_BUS_COMPONENT
14 ACPI_MODULE_NAME("scan")
15 #define STRUCT_TO_INT(s) (*((int*)&s))
16 extern struct acpi_device *acpi_root;
18 #define ACPI_BUS_CLASS "system_bus"
19 #define ACPI_BUS_HID "ACPI_BUS"
20 #define ACPI_BUS_DRIVER_NAME "ACPI Bus Driver"
21 #define ACPI_BUS_DEVICE_NAME "System Bus"
23 static LIST_HEAD(acpi_device_list);
24 DEFINE_SPINLOCK(acpi_device_lock);
25 LIST_HEAD(acpi_wakeup_device_list);
28 static void acpi_device_release(struct kobject *kobj)
30 struct acpi_device *dev = container_of(kobj, struct acpi_device, kobj);
31 kfree(dev->pnp.cid_list);
35 struct acpi_device_attribute {
36 struct attribute attr;
37 ssize_t(*show) (struct acpi_device *, char *);
38 ssize_t(*store) (struct acpi_device *, const char *, size_t);
41 typedef void acpi_device_sysfs_files(struct kobject *,
42 const struct attribute *);
44 static void setup_sys_fs_device_files(struct acpi_device *dev,
45 acpi_device_sysfs_files * func);
47 #define create_sysfs_device_files(dev) \
48 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_create_file)
49 #define remove_sysfs_device_files(dev) \
50 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_remove_file)
52 #define to_acpi_device(n) container_of(n, struct acpi_device, kobj)
53 #define to_handle_attr(n) container_of(n, struct acpi_device_attribute, attr);
55 static ssize_t acpi_device_attr_show(struct kobject *kobj,
56 struct attribute *attr, char *buf)
58 struct acpi_device *device = to_acpi_device(kobj);
59 struct acpi_device_attribute *attribute = to_handle_attr(attr);
60 return attribute->show ? attribute->show(device, buf) : -EIO;
62 static ssize_t acpi_device_attr_store(struct kobject *kobj,
63 struct attribute *attr, const char *buf,
66 struct acpi_device *device = to_acpi_device(kobj);
67 struct acpi_device_attribute *attribute = to_handle_attr(attr);
68 return attribute->store ? attribute->store(device, buf, len) : -EIO;
71 static struct sysfs_ops acpi_device_sysfs_ops = {
72 .show = acpi_device_attr_show,
73 .store = acpi_device_attr_store,
76 static struct kobj_type ktype_acpi_ns = {
77 .sysfs_ops = &acpi_device_sysfs_ops,
78 .release = acpi_device_release,
81 static int namespace_uevent(struct kset *kset, struct kobject *kobj,
82 char **envp, int num_envp, char *buffer,
85 struct acpi_device *dev = to_acpi_device(kobj);
92 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
93 "PHYSDEVDRIVER=%s", dev->driver->name))
101 static struct kset_uevent_ops namespace_uevent_ops = {
102 .uevent = &namespace_uevent,
105 static struct kset acpi_namespace_kset = {
109 .subsys = &acpi_subsys,
110 .ktype = &ktype_acpi_ns,
111 .uevent_ops = &namespace_uevent_ops,
114 /* --------------------------------------------------------------------------
115 ACPI sysfs device file support
116 -------------------------------------------------------------------------- */
117 static ssize_t acpi_eject_store(struct acpi_device *device,
118 const char *buf, size_t count);
120 #define ACPI_DEVICE_ATTR(_name,_mode,_show,_store) \
121 static struct acpi_device_attribute acpi_device_attr_##_name = \
122 __ATTR(_name, _mode, _show, _store)
124 ACPI_DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
127 * setup_sys_fs_device_files - sets up the device files under device namespace
128 * @dev: acpi_device object
129 * @func: function pointer to create or destroy the device file
132 setup_sys_fs_device_files(struct acpi_device *dev,
133 acpi_device_sysfs_files * func)
136 acpi_handle temp = NULL;
139 * If device has _EJ0, 'eject' file is created that is used to trigger
140 * hot-removal function from userland.
142 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
143 if (ACPI_SUCCESS(status))
144 (*(func)) (&dev->kobj, &acpi_device_attr_eject.attr);
147 static int acpi_eject_operation(acpi_handle handle, int lockable)
149 struct acpi_object_list arg_list;
150 union acpi_object arg;
151 acpi_status status = AE_OK;
154 * TBD: evaluate _PS3?
159 arg_list.pointer = &arg;
160 arg.type = ACPI_TYPE_INTEGER;
161 arg.integer.value = 0;
162 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
166 arg_list.pointer = &arg;
167 arg.type = ACPI_TYPE_INTEGER;
168 arg.integer.value = 1;
174 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
175 if (ACPI_FAILURE(status)) {
183 acpi_eject_store(struct acpi_device *device, const char *buf, size_t count)
190 acpi_object_type type = 0;
192 if ((!count) || (buf[0] != '1')) {
196 if (device->driver == NULL) {
201 status = acpi_get_type(device->handle, &type);
202 if (ACPI_FAILURE(status) || (!device->flags.ejectable)) {
207 islockable = device->flags.lockable;
208 handle = device->handle;
210 result = acpi_bus_trim(device, 1);
213 result = acpi_eject_operation(handle, islockable);
222 /* --------------------------------------------------------------------------
224 -------------------------------------------------------------------------- */
225 static inline struct acpi_device * to_acpi_dev(struct device * dev)
227 return container_of(dev, struct acpi_device, dev);
230 static int root_suspend(struct acpi_device * acpi_dev, pm_message_t state)
232 struct acpi_device * dev, * next;
235 spin_lock(&acpi_device_lock);
236 list_for_each_entry_safe_reverse(dev, next, &acpi_device_list, g_list) {
237 if (dev->driver && dev->driver->ops.suspend) {
238 spin_unlock(&acpi_device_lock);
239 result = dev->driver->ops.suspend(dev, 0);
241 printk(KERN_ERR PREFIX "[%s - %s] Suspend failed: %d\n",
242 acpi_device_name(dev),
243 acpi_device_bid(dev), result);
245 spin_lock(&acpi_device_lock);
248 spin_unlock(&acpi_device_lock);
252 static int acpi_device_suspend(struct device * dev, pm_message_t state)
254 struct acpi_device * acpi_dev = to_acpi_dev(dev);
257 * For now, we should only register 1 generic device -
258 * the ACPI root device - and from there, we walk the
259 * tree of ACPI devices to suspend each one using the
260 * ACPI driver methods.
262 if (acpi_dev->handle == ACPI_ROOT_OBJECT)
263 root_suspend(acpi_dev, state);
267 static int root_resume(struct acpi_device * acpi_dev)
269 struct acpi_device * dev, * next;
272 spin_lock(&acpi_device_lock);
273 list_for_each_entry_safe(dev, next, &acpi_device_list, g_list) {
274 if (dev->driver && dev->driver->ops.resume) {
275 spin_unlock(&acpi_device_lock);
276 result = dev->driver->ops.resume(dev, 0);
278 printk(KERN_ERR PREFIX "[%s - %s] resume failed: %d\n",
279 acpi_device_name(dev),
280 acpi_device_bid(dev), result);
282 spin_lock(&acpi_device_lock);
285 spin_unlock(&acpi_device_lock);
289 static int acpi_device_resume(struct device * dev)
291 struct acpi_device * acpi_dev = to_acpi_dev(dev);
294 * For now, we should only register 1 generic device -
295 * the ACPI root device - and from there, we walk the
296 * tree of ACPI devices to resume each one using the
297 * ACPI driver methods.
299 if (acpi_dev->handle == ACPI_ROOT_OBJECT)
300 root_resume(acpi_dev);
305 * acpi_bus_match - match device IDs to driver's supported IDs
306 * @device: the device that we are trying to match to a driver
307 * @driver: driver whose device id table is being checked
309 * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it
310 * matches the specified driver's criteria.
313 acpi_bus_match(struct acpi_device *device, struct acpi_driver *driver)
315 if (driver && driver->ops.match)
316 return driver->ops.match(device, driver);
317 return acpi_match_ids(device, driver->ids);
320 static struct bus_type acpi_bus_type = {
322 .suspend = acpi_device_suspend,
323 .resume = acpi_device_resume,
326 static void acpi_device_register(struct acpi_device *device,
327 struct acpi_device *parent)
334 * Link this device to its parent and siblings.
336 INIT_LIST_HEAD(&device->children);
337 INIT_LIST_HEAD(&device->node);
338 INIT_LIST_HEAD(&device->g_list);
339 INIT_LIST_HEAD(&device->wakeup_list);
341 spin_lock(&acpi_device_lock);
342 if (device->parent) {
343 list_add_tail(&device->node, &device->parent->children);
344 list_add_tail(&device->g_list, &device->parent->g_list);
346 list_add_tail(&device->g_list, &acpi_device_list);
347 if (device->wakeup.flags.valid)
348 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
349 spin_unlock(&acpi_device_lock);
351 strlcpy(device->kobj.name, device->pnp.bus_id, KOBJ_NAME_LEN);
353 device->kobj.parent = &parent->kobj;
354 device->kobj.ktype = &ktype_acpi_ns;
355 device->kobj.kset = &acpi_namespace_kset;
356 err = kobject_register(&device->kobj);
358 printk(KERN_WARNING "%s: kobject_register error: %d\n",
360 create_sysfs_device_files(device);
363 static void acpi_device_unregister(struct acpi_device *device, int type)
365 spin_lock(&acpi_device_lock);
366 if (device->parent) {
367 list_del(&device->node);
368 list_del(&device->g_list);
370 list_del(&device->g_list);
372 list_del(&device->wakeup_list);
374 spin_unlock(&acpi_device_lock);
376 acpi_detach_data(device->handle, acpi_bus_data_handler);
377 remove_sysfs_device_files(device);
378 kobject_unregister(&device->kobj);
381 /* --------------------------------------------------------------------------
383 -------------------------------------------------------------------------- */
384 static LIST_HEAD(acpi_bus_drivers);
387 * acpi_bus_driver_init - add a device to a driver
388 * @device: the device to add and initialize
389 * @driver: driver for the device
391 * Used to initialize a device via its device driver. Called whenever a
392 * driver is bound to a device. Invokes the driver's add() and start() ops.
395 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
400 if (!device || !driver)
403 if (!driver->ops.add)
406 result = driver->ops.add(device);
408 device->driver = NULL;
409 acpi_driver_data(device) = NULL;
413 device->driver = driver;
416 * TBD - Configuration Management: Assign resources to device based
417 * upon possible configuration and currently allocated resources.
420 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
421 "Driver successfully bound to device\n"));
425 static int acpi_start_single_object(struct acpi_device *device)
428 struct acpi_driver *driver;
431 if (!(driver = device->driver))
434 if (driver->ops.start) {
435 result = driver->ops.start(device);
436 if (result && driver->ops.remove)
437 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
443 static void acpi_driver_attach(struct acpi_driver *drv)
445 struct list_head *node, *next;
448 spin_lock(&acpi_device_lock);
449 list_for_each_safe(node, next, &acpi_device_list) {
450 struct acpi_device *dev =
451 container_of(node, struct acpi_device, g_list);
453 if (dev->driver || !dev->status.present)
455 spin_unlock(&acpi_device_lock);
457 if (!acpi_bus_match(dev, drv)) {
458 if (!acpi_bus_driver_init(dev, drv)) {
459 acpi_start_single_object(dev);
460 atomic_inc(&drv->references);
461 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
462 "Found driver [%s] for device [%s]\n",
463 drv->name, dev->pnp.bus_id));
466 spin_lock(&acpi_device_lock);
468 spin_unlock(&acpi_device_lock);
471 static void acpi_driver_detach(struct acpi_driver *drv)
473 struct list_head *node, *next;
476 spin_lock(&acpi_device_lock);
477 list_for_each_safe(node, next, &acpi_device_list) {
478 struct acpi_device *dev =
479 container_of(node, struct acpi_device, g_list);
481 if (dev->driver == drv) {
482 spin_unlock(&acpi_device_lock);
484 drv->ops.remove(dev, ACPI_BUS_REMOVAL_NORMAL);
485 spin_lock(&acpi_device_lock);
487 dev->driver_data = NULL;
488 atomic_dec(&drv->references);
491 spin_unlock(&acpi_device_lock);
495 * acpi_bus_register_driver - register a driver with the ACPI bus
496 * @driver: driver being registered
498 * Registers a driver with the ACPI bus. Searches the namespace for all
499 * devices that match the driver's criteria and binds. Returns zero for
500 * success or a negative error status for failure.
502 int acpi_bus_register_driver(struct acpi_driver *driver)
508 spin_lock(&acpi_device_lock);
509 list_add_tail(&driver->node, &acpi_bus_drivers);
510 spin_unlock(&acpi_device_lock);
511 acpi_driver_attach(driver);
516 EXPORT_SYMBOL(acpi_bus_register_driver);
519 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
520 * @driver: driver to unregister
522 * Unregisters a driver with the ACPI bus. Searches the namespace for all
523 * devices that match the driver's criteria and unbinds.
525 void acpi_bus_unregister_driver(struct acpi_driver *driver)
527 acpi_driver_detach(driver);
529 if (!atomic_read(&driver->references)) {
530 spin_lock(&acpi_device_lock);
531 list_del_init(&driver->node);
532 spin_unlock(&acpi_device_lock);
537 EXPORT_SYMBOL(acpi_bus_unregister_driver);
540 * acpi_bus_find_driver - check if there is a driver installed for the device
541 * @device: device that we are trying to find a supporting driver for
543 * Parses the list of registered drivers looking for a driver applicable for
544 * the specified device.
546 static int acpi_bus_find_driver(struct acpi_device *device)
549 struct list_head *node, *next;
552 spin_lock(&acpi_device_lock);
553 list_for_each_safe(node, next, &acpi_bus_drivers) {
554 struct acpi_driver *driver =
555 container_of(node, struct acpi_driver, node);
557 atomic_inc(&driver->references);
558 spin_unlock(&acpi_device_lock);
559 if (!acpi_bus_match(device, driver)) {
560 result = acpi_bus_driver_init(device, driver);
564 atomic_dec(&driver->references);
565 spin_lock(&acpi_device_lock);
567 spin_unlock(&acpi_device_lock);
573 /* --------------------------------------------------------------------------
575 -------------------------------------------------------------------------- */
577 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
581 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
582 union acpi_object *obj;
584 status = acpi_get_handle(handle, "_EJD", &tmp);
585 if (ACPI_FAILURE(status))
588 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
589 if (ACPI_SUCCESS(status)) {
590 obj = buffer.pointer;
591 status = acpi_get_handle(NULL, obj->string.pointer, ejd);
592 kfree(buffer.pointer);
596 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
598 void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
606 int acpi_match_ids(struct acpi_device *device, char *ids)
608 if (device->flags.hardware_id)
609 if (strstr(ids, device->pnp.hardware_id))
612 if (device->flags.compatible_ids) {
613 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
616 /* compare multiple _CID entries against driver ids */
617 for (i = 0; i < cid_list->count; i++) {
618 if (strstr(ids, cid_list->id[i].value))
625 static int acpi_bus_get_perf_flags(struct acpi_device *device)
627 device->performance.state = ACPI_STATE_UNKNOWN;
632 acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
633 union acpi_object *package)
636 union acpi_object *element = NULL;
638 if (!device || !package || (package->package.count < 2))
639 return AE_BAD_PARAMETER;
641 element = &(package->package.elements[0]);
643 return AE_BAD_PARAMETER;
644 if (element->type == ACPI_TYPE_PACKAGE) {
645 if ((element->package.count < 2) ||
646 (element->package.elements[0].type !=
647 ACPI_TYPE_LOCAL_REFERENCE)
648 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
650 device->wakeup.gpe_device =
651 element->package.elements[0].reference.handle;
652 device->wakeup.gpe_number =
653 (u32) element->package.elements[1].integer.value;
654 } else if (element->type == ACPI_TYPE_INTEGER) {
655 device->wakeup.gpe_number = element->integer.value;
659 element = &(package->package.elements[1]);
660 if (element->type != ACPI_TYPE_INTEGER) {
663 device->wakeup.sleep_state = element->integer.value;
665 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
668 device->wakeup.resources.count = package->package.count - 2;
669 for (i = 0; i < device->wakeup.resources.count; i++) {
670 element = &(package->package.elements[i + 2]);
671 if (element->type != ACPI_TYPE_ANY) {
675 device->wakeup.resources.handles[i] = element->reference.handle;
681 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
683 acpi_status status = 0;
684 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
685 union acpi_object *package = NULL;
689 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
690 if (ACPI_FAILURE(status)) {
691 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
695 package = (union acpi_object *)buffer.pointer;
696 status = acpi_bus_extract_wakeup_device_power_package(device, package);
697 if (ACPI_FAILURE(status)) {
698 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
702 kfree(buffer.pointer);
704 device->wakeup.flags.valid = 1;
705 /* Power button, Lid switch always enable wakeup */
706 if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
707 device->wakeup.flags.run_wake = 1;
710 if (ACPI_FAILURE(status))
711 device->flags.wake_capable = 0;
715 static int acpi_bus_get_power_flags(struct acpi_device *device)
717 acpi_status status = 0;
718 acpi_handle handle = NULL;
723 * Power Management Flags
725 status = acpi_get_handle(device->handle, "_PSC", &handle);
726 if (ACPI_SUCCESS(status))
727 device->power.flags.explicit_get = 1;
728 status = acpi_get_handle(device->handle, "_IRC", &handle);
729 if (ACPI_SUCCESS(status))
730 device->power.flags.inrush_current = 1;
733 * Enumerate supported power management states
735 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
736 struct acpi_device_power_state *ps = &device->power.states[i];
737 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
739 /* Evaluate "_PRx" to se if power resources are referenced */
740 acpi_evaluate_reference(device->handle, object_name, NULL,
742 if (ps->resources.count) {
743 device->power.flags.power_resources = 1;
747 /* Evaluate "_PSx" to see if we can do explicit sets */
748 object_name[2] = 'S';
749 status = acpi_get_handle(device->handle, object_name, &handle);
750 if (ACPI_SUCCESS(status)) {
751 ps->flags.explicit_set = 1;
755 /* State is valid if we have some power control */
756 if (ps->resources.count || ps->flags.explicit_set)
759 ps->power = -1; /* Unknown - driver assigned */
760 ps->latency = -1; /* Unknown - driver assigned */
763 /* Set defaults for D0 and D3 states (always valid) */
764 device->power.states[ACPI_STATE_D0].flags.valid = 1;
765 device->power.states[ACPI_STATE_D0].power = 100;
766 device->power.states[ACPI_STATE_D3].flags.valid = 1;
767 device->power.states[ACPI_STATE_D3].power = 0;
769 /* TBD: System wake support and resource requirements. */
771 device->power.state = ACPI_STATE_UNKNOWN;
776 static int acpi_bus_get_flags(struct acpi_device *device)
778 acpi_status status = AE_OK;
779 acpi_handle temp = NULL;
782 /* Presence of _STA indicates 'dynamic_status' */
783 status = acpi_get_handle(device->handle, "_STA", &temp);
784 if (ACPI_SUCCESS(status))
785 device->flags.dynamic_status = 1;
787 /* Presence of _CID indicates 'compatible_ids' */
788 status = acpi_get_handle(device->handle, "_CID", &temp);
789 if (ACPI_SUCCESS(status))
790 device->flags.compatible_ids = 1;
792 /* Presence of _RMV indicates 'removable' */
793 status = acpi_get_handle(device->handle, "_RMV", &temp);
794 if (ACPI_SUCCESS(status))
795 device->flags.removable = 1;
797 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
798 status = acpi_get_handle(device->handle, "_EJD", &temp);
799 if (ACPI_SUCCESS(status))
800 device->flags.ejectable = 1;
802 status = acpi_get_handle(device->handle, "_EJ0", &temp);
803 if (ACPI_SUCCESS(status))
804 device->flags.ejectable = 1;
807 /* Presence of _LCK indicates 'lockable' */
808 status = acpi_get_handle(device->handle, "_LCK", &temp);
809 if (ACPI_SUCCESS(status))
810 device->flags.lockable = 1;
812 /* Presence of _PS0|_PR0 indicates 'power manageable' */
813 status = acpi_get_handle(device->handle, "_PS0", &temp);
814 if (ACPI_FAILURE(status))
815 status = acpi_get_handle(device->handle, "_PR0", &temp);
816 if (ACPI_SUCCESS(status))
817 device->flags.power_manageable = 1;
819 /* Presence of _PRW indicates wake capable */
820 status = acpi_get_handle(device->handle, "_PRW", &temp);
821 if (ACPI_SUCCESS(status))
822 device->flags.wake_capable = 1;
824 /* TBD: Peformance management */
829 static void acpi_device_get_busid(struct acpi_device *device,
830 acpi_handle handle, int type)
832 char bus_id[5] = { '?', 0 };
833 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
839 * The device's Bus ID is simply the object name.
840 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
843 case ACPI_BUS_TYPE_SYSTEM:
844 strcpy(device->pnp.bus_id, "ACPI");
846 case ACPI_BUS_TYPE_POWER_BUTTON:
847 strcpy(device->pnp.bus_id, "PWRF");
849 case ACPI_BUS_TYPE_SLEEP_BUTTON:
850 strcpy(device->pnp.bus_id, "SLPF");
853 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
854 /* Clean up trailing underscores (if any) */
855 for (i = 3; i > 1; i--) {
856 if (bus_id[i] == '_')
861 strcpy(device->pnp.bus_id, bus_id);
866 static void acpi_device_set_id(struct acpi_device *device,
867 struct acpi_device *parent, acpi_handle handle,
870 struct acpi_device_info *info;
871 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
874 struct acpi_compatible_id_list *cid_list = NULL;
878 case ACPI_BUS_TYPE_DEVICE:
879 status = acpi_get_object_info(handle, &buffer);
880 if (ACPI_FAILURE(status)) {
881 printk("%s: Error reading device info\n", __FUNCTION__);
885 info = buffer.pointer;
886 if (info->valid & ACPI_VALID_HID)
887 hid = info->hardware_id.value;
888 if (info->valid & ACPI_VALID_UID)
889 uid = info->unique_id.value;
890 if (info->valid & ACPI_VALID_CID)
891 cid_list = &info->compatibility_id;
892 if (info->valid & ACPI_VALID_ADR) {
893 device->pnp.bus_address = info->address;
894 device->flags.bus_address = 1;
897 case ACPI_BUS_TYPE_POWER:
898 hid = ACPI_POWER_HID;
900 case ACPI_BUS_TYPE_PROCESSOR:
901 hid = ACPI_PROCESSOR_HID;
903 case ACPI_BUS_TYPE_SYSTEM:
904 hid = ACPI_SYSTEM_HID;
906 case ACPI_BUS_TYPE_THERMAL:
907 hid = ACPI_THERMAL_HID;
909 case ACPI_BUS_TYPE_POWER_BUTTON:
910 hid = ACPI_BUTTON_HID_POWERF;
912 case ACPI_BUS_TYPE_SLEEP_BUTTON:
913 hid = ACPI_BUTTON_HID_SLEEPF;
920 * Fix for the system root bus device -- the only root-level device.
922 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
924 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
925 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
929 strcpy(device->pnp.hardware_id, hid);
930 device->flags.hardware_id = 1;
933 strcpy(device->pnp.unique_id, uid);
934 device->flags.unique_id = 1;
937 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
938 if (device->pnp.cid_list)
939 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
941 printk(KERN_ERR "Memory allocation error\n");
944 kfree(buffer.pointer);
947 static int acpi_device_set_context(struct acpi_device *device, int type)
949 acpi_status status = AE_OK;
954 * Attach this 'struct acpi_device' to the ACPI object. This makes
955 * resolutions from handle->device very efficient. Note that we need
956 * to be careful with fixed-feature devices as they all attach to the
959 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
960 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
961 status = acpi_attach_data(device->handle,
962 acpi_bus_data_handler, device);
964 if (ACPI_FAILURE(status)) {
965 printk("Error attaching device data\n");
972 static void acpi_device_get_debug_info(struct acpi_device *device,
973 acpi_handle handle, int type)
975 #ifdef CONFIG_ACPI_DEBUG_OUTPUT
976 char *type_string = NULL;
977 char name[80] = { '?', '\0' };
978 struct acpi_buffer buffer = { sizeof(name), name };
981 case ACPI_BUS_TYPE_DEVICE:
982 type_string = "Device";
983 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
985 case ACPI_BUS_TYPE_POWER:
986 type_string = "Power Resource";
987 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
989 case ACPI_BUS_TYPE_PROCESSOR:
990 type_string = "Processor";
991 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
993 case ACPI_BUS_TYPE_SYSTEM:
994 type_string = "System";
995 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
997 case ACPI_BUS_TYPE_THERMAL:
998 type_string = "Thermal Zone";
999 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1001 case ACPI_BUS_TYPE_POWER_BUTTON:
1002 type_string = "Power Button";
1003 sprintf(name, "PWRB");
1005 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1006 type_string = "Sleep Button";
1007 sprintf(name, "SLPB");
1011 printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
1012 #endif /*CONFIG_ACPI_DEBUG_OUTPUT */
1015 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1018 struct acpi_driver *driver;
1024 driver = dev->driver;
1026 if ((driver) && (driver->ops.remove)) {
1028 if (driver->ops.stop) {
1029 result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT);
1034 result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT);
1039 atomic_dec(&dev->driver->references);
1041 acpi_driver_data(dev) = NULL;
1047 if (dev->flags.bus_address) {
1048 if ((dev->parent) && (dev->parent->ops.unbind))
1049 dev->parent->ops.unbind(dev);
1052 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1058 acpi_add_single_object(struct acpi_device **child,
1059 struct acpi_device *parent, acpi_handle handle, int type)
1062 struct acpi_device *device = NULL;
1068 device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
1070 printk(KERN_ERR PREFIX "Memory allocation error\n");
1073 memset(device, 0, sizeof(struct acpi_device));
1075 device->handle = handle;
1076 device->parent = parent;
1078 acpi_device_get_busid(device, handle, type);
1083 * Get prior to calling acpi_bus_get_status() so we know whether
1084 * or not _STA is present. Note that we only look for object
1085 * handles -- cannot evaluate objects until we know the device is
1086 * present and properly initialized.
1088 result = acpi_bus_get_flags(device);
1095 * See if the device is present. We always assume that non-Device
1096 * and non-Processor objects (e.g. thermal zones, power resources,
1097 * etc.) are present, functioning, etc. (at least when parent object
1098 * is present). Note that _STA has a different meaning for some
1099 * objects (e.g. power resources) so we need to be careful how we use
1103 case ACPI_BUS_TYPE_PROCESSOR:
1104 case ACPI_BUS_TYPE_DEVICE:
1105 result = acpi_bus_get_status(device);
1106 if (ACPI_FAILURE(result) || !device->status.present) {
1112 STRUCT_TO_INT(device->status) = 0x0F;
1119 * TBD: Synch with Core's enumeration/initialization process.
1123 * Hardware ID, Unique ID, & Bus Address
1124 * -------------------------------------
1126 acpi_device_set_id(device, parent, handle, type);
1132 if (device->flags.power_manageable) {
1133 result = acpi_bus_get_power_flags(device);
1139 * Wakeup device management
1140 *-----------------------
1142 if (device->flags.wake_capable) {
1143 result = acpi_bus_get_wakeup_device_flags(device);
1149 * Performance Management
1150 * ----------------------
1152 if (device->flags.performance_manageable) {
1153 result = acpi_bus_get_perf_flags(device);
1158 if ((result = acpi_device_set_context(device, type)))
1161 acpi_device_get_debug_info(device, handle, type);
1163 acpi_device_register(device, parent);
1166 * Bind _ADR-Based Devices
1167 * -----------------------
1168 * If there's a a bus address (_ADR) then we utilize the parent's
1169 * 'bind' function (if exists) to bind the ACPI- and natively-
1170 * enumerated device representations.
1172 if (device->flags.bus_address) {
1173 if (device->parent && device->parent->ops.bind)
1174 device->parent->ops.bind(device);
1178 * Locate & Attach Driver
1179 * ----------------------
1180 * If there's a hardware id (_HID) or compatible ids (_CID) we check
1181 * to see if there's a driver installed for this kind of device. Note
1182 * that drivers can install before or after a device is enumerated.
1184 * TBD: Assumes LDM provides driver hot-plug capability.
1186 acpi_bus_find_driver(device);
1192 kfree(device->pnp.cid_list);
1199 static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
1201 acpi_status status = AE_OK;
1202 struct acpi_device *parent = NULL;
1203 struct acpi_device *child = NULL;
1204 acpi_handle phandle = NULL;
1205 acpi_handle chandle = NULL;
1206 acpi_object_type type = 0;
1214 phandle = start->handle;
1217 * Parse through the ACPI namespace, identify all 'devices', and
1218 * create a new 'struct acpi_device' for each.
1220 while ((level > 0) && parent) {
1222 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1226 * If this scope is exhausted then move our way back up.
1228 if (ACPI_FAILURE(status)) {
1231 acpi_get_parent(phandle, &phandle);
1233 parent = parent->parent;
1237 status = acpi_get_type(chandle, &type);
1238 if (ACPI_FAILURE(status))
1242 * If this is a scope object then parse it (depth-first).
1244 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1252 * We're only interested in objects that we consider 'devices'.
1255 case ACPI_TYPE_DEVICE:
1256 type = ACPI_BUS_TYPE_DEVICE;
1258 case ACPI_TYPE_PROCESSOR:
1259 type = ACPI_BUS_TYPE_PROCESSOR;
1261 case ACPI_TYPE_THERMAL:
1262 type = ACPI_BUS_TYPE_THERMAL;
1264 case ACPI_TYPE_POWER:
1265 type = ACPI_BUS_TYPE_POWER;
1271 if (ops->acpi_op_add)
1272 status = acpi_add_single_object(&child, parent,
1275 status = acpi_bus_get_device(chandle, &child);
1277 if (ACPI_FAILURE(status))
1280 if (ops->acpi_op_start) {
1281 status = acpi_start_single_object(child);
1282 if (ACPI_FAILURE(status))
1287 * If the device is present, enabled, and functioning then
1288 * parse its scope (depth-first). Note that we need to
1289 * represent absent devices to facilitate PnP notifications
1290 * -- but only the subtree head (not all of its children,
1291 * which will be enumerated when the parent is inserted).
1293 * TBD: Need notifications and other detection mechanisms
1294 * in place before we can fully implement this.
1296 if (child->status.present) {
1297 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1299 if (ACPI_SUCCESS(status)) {
1312 acpi_bus_add(struct acpi_device **child,
1313 struct acpi_device *parent, acpi_handle handle, int type)
1316 struct acpi_bus_ops ops;
1319 result = acpi_add_single_object(child, parent, handle, type);
1321 memset(&ops, 0, sizeof(ops));
1322 ops.acpi_op_add = 1;
1323 result = acpi_bus_scan(*child, &ops);
1328 EXPORT_SYMBOL(acpi_bus_add);
1330 int acpi_bus_start(struct acpi_device *device)
1333 struct acpi_bus_ops ops;
1339 result = acpi_start_single_object(device);
1341 memset(&ops, 0, sizeof(ops));
1342 ops.acpi_op_start = 1;
1343 result = acpi_bus_scan(device, &ops);
1348 EXPORT_SYMBOL(acpi_bus_start);
1350 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1353 struct acpi_device *parent, *child;
1354 acpi_handle phandle, chandle;
1355 acpi_object_type type;
1360 phandle = start->handle;
1361 child = chandle = NULL;
1363 while ((level > 0) && parent && (!err)) {
1364 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1368 * If this scope is exhausted then move our way back up.
1370 if (ACPI_FAILURE(status)) {
1373 acpi_get_parent(phandle, &phandle);
1375 parent = parent->parent;
1378 err = acpi_bus_remove(child, rmdevice);
1380 err = acpi_bus_remove(child, 1);
1385 status = acpi_get_type(chandle, &type);
1386 if (ACPI_FAILURE(status)) {
1390 * If there is a device corresponding to chandle then
1391 * parse it (depth-first).
1393 if (acpi_bus_get_device(chandle, &child) == 0) {
1403 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1406 static int acpi_bus_scan_fixed(struct acpi_device *root)
1409 struct acpi_device *device = NULL;
1416 * Enumerate all fixed-feature devices.
1418 if (acpi_fadt.pwr_button == 0) {
1419 result = acpi_add_single_object(&device, acpi_root,
1421 ACPI_BUS_TYPE_POWER_BUTTON);
1423 result = acpi_start_single_object(device);
1426 if (acpi_fadt.sleep_button == 0) {
1427 result = acpi_add_single_object(&device, acpi_root,
1429 ACPI_BUS_TYPE_SLEEP_BUTTON);
1431 result = acpi_start_single_object(device);
1437 static int __init acpi_scan_init(void)
1440 struct acpi_bus_ops ops;
1446 result = kset_register(&acpi_namespace_kset);
1448 printk(KERN_ERR PREFIX "kset_register error: %d\n", result);
1450 result = bus_register(&acpi_bus_type);
1452 /* We don't want to quit even if we failed to add suspend/resume */
1453 printk(KERN_ERR PREFIX "Could not register bus type\n");
1457 * Create the root device in the bus's device tree
1459 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
1460 ACPI_BUS_TYPE_SYSTEM);
1464 result = acpi_start_single_object(acpi_root);
1468 acpi_root->dev.bus = &acpi_bus_type;
1469 snprintf(acpi_root->dev.bus_id, BUS_ID_SIZE, "%s", acpi_bus_type.name);
1470 result = device_register(&acpi_root->dev);
1472 /* We don't want to quit even if we failed to add suspend/resume */
1473 printk(KERN_ERR PREFIX "Could not register device\n");
1477 * Enumerate devices in the ACPI namespace.
1479 result = acpi_bus_scan_fixed(acpi_root);
1481 memset(&ops, 0, sizeof(ops));
1482 ops.acpi_op_add = 1;
1483 ops.acpi_op_start = 1;
1484 result = acpi_bus_scan(acpi_root, &ops);
1488 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1494 subsys_initcall(acpi_scan_init);