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_dev(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_dev(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_dev(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_dev(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 int root_suspend(struct acpi_device * acpi_dev, pm_message_t state)
227 struct acpi_device * dev, * next;
230 spin_lock(&acpi_device_lock);
231 list_for_each_entry_safe_reverse(dev, next, &acpi_device_list, g_list) {
232 if (dev->driver && dev->driver->ops.suspend) {
233 spin_unlock(&acpi_device_lock);
234 result = dev->driver->ops.suspend(dev, 0);
236 printk(KERN_ERR PREFIX "[%s - %s] Suspend failed: %d\n",
237 acpi_device_name(dev),
238 acpi_device_bid(dev), result);
240 spin_lock(&acpi_device_lock);
243 spin_unlock(&acpi_device_lock);
247 static int acpi_device_suspend(struct device * dev, pm_message_t state)
249 struct acpi_device * acpi_dev = to_acpi_device(dev);
252 * For now, we should only register 1 generic device -
253 * the ACPI root device - and from there, we walk the
254 * tree of ACPI devices to suspend each one using the
255 * ACPI driver methods.
257 if (acpi_dev->handle == ACPI_ROOT_OBJECT)
258 root_suspend(acpi_dev, state);
262 static int root_resume(struct acpi_device * acpi_dev)
264 struct acpi_device * dev, * next;
267 spin_lock(&acpi_device_lock);
268 list_for_each_entry_safe(dev, next, &acpi_device_list, g_list) {
269 if (dev->driver && dev->driver->ops.resume) {
270 spin_unlock(&acpi_device_lock);
271 result = dev->driver->ops.resume(dev, 0);
273 printk(KERN_ERR PREFIX "[%s - %s] resume failed: %d\n",
274 acpi_device_name(dev),
275 acpi_device_bid(dev), result);
277 spin_lock(&acpi_device_lock);
280 spin_unlock(&acpi_device_lock);
284 static int acpi_device_resume(struct device * dev)
286 struct acpi_device * acpi_dev = to_acpi_device(dev);
289 * For now, we should only register 1 generic device -
290 * the ACPI root device - and from there, we walk the
291 * tree of ACPI devices to resume each one using the
292 * ACPI driver methods.
294 if (acpi_dev->handle == ACPI_ROOT_OBJECT)
295 root_resume(acpi_dev);
300 * acpi_bus_match - match device IDs to driver's supported IDs
301 * @device: the device that we are trying to match to a driver
302 * @driver: driver whose device id table is being checked
304 * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it
305 * matches the specified driver's criteria.
308 acpi_bus_match(struct acpi_device *device, struct acpi_driver *driver)
310 if (driver && driver->ops.match)
311 return driver->ops.match(device, driver);
312 return acpi_match_ids(device, driver->ids);
315 static struct bus_type acpi_bus_type = {
317 .suspend = acpi_device_suspend,
318 .resume = acpi_device_resume,
321 static void acpi_device_register(struct acpi_device *device,
322 struct acpi_device *parent)
329 * Link this device to its parent and siblings.
331 INIT_LIST_HEAD(&device->children);
332 INIT_LIST_HEAD(&device->node);
333 INIT_LIST_HEAD(&device->g_list);
334 INIT_LIST_HEAD(&device->wakeup_list);
336 spin_lock(&acpi_device_lock);
337 if (device->parent) {
338 list_add_tail(&device->node, &device->parent->children);
339 list_add_tail(&device->g_list, &device->parent->g_list);
341 list_add_tail(&device->g_list, &acpi_device_list);
342 if (device->wakeup.flags.valid)
343 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
344 spin_unlock(&acpi_device_lock);
346 strlcpy(device->kobj.name, device->pnp.bus_id, KOBJ_NAME_LEN);
348 device->kobj.parent = &parent->kobj;
349 device->kobj.ktype = &ktype_acpi_ns;
350 device->kobj.kset = &acpi_namespace_kset;
351 err = kobject_register(&device->kobj);
353 printk(KERN_WARNING "%s: kobject_register error: %d\n",
355 create_sysfs_device_files(device);
358 static void acpi_device_unregister(struct acpi_device *device, int type)
360 spin_lock(&acpi_device_lock);
361 if (device->parent) {
362 list_del(&device->node);
363 list_del(&device->g_list);
365 list_del(&device->g_list);
367 list_del(&device->wakeup_list);
369 spin_unlock(&acpi_device_lock);
371 acpi_detach_data(device->handle, acpi_bus_data_handler);
372 remove_sysfs_device_files(device);
373 kobject_unregister(&device->kobj);
376 /* --------------------------------------------------------------------------
378 -------------------------------------------------------------------------- */
379 static LIST_HEAD(acpi_bus_drivers);
382 * acpi_bus_driver_init - add a device to a driver
383 * @device: the device to add and initialize
384 * @driver: driver for the device
386 * Used to initialize a device via its device driver. Called whenever a
387 * driver is bound to a device. Invokes the driver's add() and start() ops.
390 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
395 if (!device || !driver)
398 if (!driver->ops.add)
401 result = driver->ops.add(device);
403 device->driver = NULL;
404 acpi_driver_data(device) = NULL;
408 device->driver = driver;
411 * TBD - Configuration Management: Assign resources to device based
412 * upon possible configuration and currently allocated resources.
415 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
416 "Driver successfully bound to device\n"));
420 static int acpi_start_single_object(struct acpi_device *device)
423 struct acpi_driver *driver;
426 if (!(driver = device->driver))
429 if (driver->ops.start) {
430 result = driver->ops.start(device);
431 if (result && driver->ops.remove)
432 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
438 static void acpi_driver_attach(struct acpi_driver *drv)
440 struct list_head *node, *next;
443 spin_lock(&acpi_device_lock);
444 list_for_each_safe(node, next, &acpi_device_list) {
445 struct acpi_device *dev =
446 container_of(node, struct acpi_device, g_list);
448 if (dev->driver || !dev->status.present)
450 spin_unlock(&acpi_device_lock);
452 if (!acpi_bus_match(dev, drv)) {
453 if (!acpi_bus_driver_init(dev, drv)) {
454 acpi_start_single_object(dev);
455 atomic_inc(&drv->references);
456 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
457 "Found driver [%s] for device [%s]\n",
458 drv->name, dev->pnp.bus_id));
461 spin_lock(&acpi_device_lock);
463 spin_unlock(&acpi_device_lock);
466 static void acpi_driver_detach(struct acpi_driver *drv)
468 struct list_head *node, *next;
471 spin_lock(&acpi_device_lock);
472 list_for_each_safe(node, next, &acpi_device_list) {
473 struct acpi_device *dev =
474 container_of(node, struct acpi_device, g_list);
476 if (dev->driver == drv) {
477 spin_unlock(&acpi_device_lock);
479 drv->ops.remove(dev, ACPI_BUS_REMOVAL_NORMAL);
480 spin_lock(&acpi_device_lock);
482 dev->driver_data = NULL;
483 atomic_dec(&drv->references);
486 spin_unlock(&acpi_device_lock);
490 * acpi_bus_register_driver - register a driver with the ACPI bus
491 * @driver: driver being registered
493 * Registers a driver with the ACPI bus. Searches the namespace for all
494 * devices that match the driver's criteria and binds. Returns zero for
495 * success or a negative error status for failure.
497 int acpi_bus_register_driver(struct acpi_driver *driver)
503 spin_lock(&acpi_device_lock);
504 list_add_tail(&driver->node, &acpi_bus_drivers);
505 spin_unlock(&acpi_device_lock);
506 acpi_driver_attach(driver);
511 EXPORT_SYMBOL(acpi_bus_register_driver);
514 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
515 * @driver: driver to unregister
517 * Unregisters a driver with the ACPI bus. Searches the namespace for all
518 * devices that match the driver's criteria and unbinds.
520 void acpi_bus_unregister_driver(struct acpi_driver *driver)
522 acpi_driver_detach(driver);
524 if (!atomic_read(&driver->references)) {
525 spin_lock(&acpi_device_lock);
526 list_del_init(&driver->node);
527 spin_unlock(&acpi_device_lock);
532 EXPORT_SYMBOL(acpi_bus_unregister_driver);
535 * acpi_bus_find_driver - check if there is a driver installed for the device
536 * @device: device that we are trying to find a supporting driver for
538 * Parses the list of registered drivers looking for a driver applicable for
539 * the specified device.
541 static int acpi_bus_find_driver(struct acpi_device *device)
544 struct list_head *node, *next;
547 spin_lock(&acpi_device_lock);
548 list_for_each_safe(node, next, &acpi_bus_drivers) {
549 struct acpi_driver *driver =
550 container_of(node, struct acpi_driver, node);
552 atomic_inc(&driver->references);
553 spin_unlock(&acpi_device_lock);
554 if (!acpi_bus_match(device, driver)) {
555 result = acpi_bus_driver_init(device, driver);
559 atomic_dec(&driver->references);
560 spin_lock(&acpi_device_lock);
562 spin_unlock(&acpi_device_lock);
568 /* --------------------------------------------------------------------------
570 -------------------------------------------------------------------------- */
572 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
576 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
577 union acpi_object *obj;
579 status = acpi_get_handle(handle, "_EJD", &tmp);
580 if (ACPI_FAILURE(status))
583 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
584 if (ACPI_SUCCESS(status)) {
585 obj = buffer.pointer;
586 status = acpi_get_handle(NULL, obj->string.pointer, ejd);
587 kfree(buffer.pointer);
591 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
593 void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
601 int acpi_match_ids(struct acpi_device *device, char *ids)
603 if (device->flags.hardware_id)
604 if (strstr(ids, device->pnp.hardware_id))
607 if (device->flags.compatible_ids) {
608 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
611 /* compare multiple _CID entries against driver ids */
612 for (i = 0; i < cid_list->count; i++) {
613 if (strstr(ids, cid_list->id[i].value))
620 static int acpi_bus_get_perf_flags(struct acpi_device *device)
622 device->performance.state = ACPI_STATE_UNKNOWN;
627 acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
628 union acpi_object *package)
631 union acpi_object *element = NULL;
633 if (!device || !package || (package->package.count < 2))
634 return AE_BAD_PARAMETER;
636 element = &(package->package.elements[0]);
638 return AE_BAD_PARAMETER;
639 if (element->type == ACPI_TYPE_PACKAGE) {
640 if ((element->package.count < 2) ||
641 (element->package.elements[0].type !=
642 ACPI_TYPE_LOCAL_REFERENCE)
643 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
645 device->wakeup.gpe_device =
646 element->package.elements[0].reference.handle;
647 device->wakeup.gpe_number =
648 (u32) element->package.elements[1].integer.value;
649 } else if (element->type == ACPI_TYPE_INTEGER) {
650 device->wakeup.gpe_number = element->integer.value;
654 element = &(package->package.elements[1]);
655 if (element->type != ACPI_TYPE_INTEGER) {
658 device->wakeup.sleep_state = element->integer.value;
660 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
663 device->wakeup.resources.count = package->package.count - 2;
664 for (i = 0; i < device->wakeup.resources.count; i++) {
665 element = &(package->package.elements[i + 2]);
666 if (element->type != ACPI_TYPE_ANY) {
670 device->wakeup.resources.handles[i] = element->reference.handle;
676 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
678 acpi_status status = 0;
679 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
680 union acpi_object *package = NULL;
684 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
685 if (ACPI_FAILURE(status)) {
686 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
690 package = (union acpi_object *)buffer.pointer;
691 status = acpi_bus_extract_wakeup_device_power_package(device, package);
692 if (ACPI_FAILURE(status)) {
693 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
697 kfree(buffer.pointer);
699 device->wakeup.flags.valid = 1;
700 /* Power button, Lid switch always enable wakeup */
701 if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
702 device->wakeup.flags.run_wake = 1;
705 if (ACPI_FAILURE(status))
706 device->flags.wake_capable = 0;
710 static int acpi_bus_get_power_flags(struct acpi_device *device)
712 acpi_status status = 0;
713 acpi_handle handle = NULL;
718 * Power Management Flags
720 status = acpi_get_handle(device->handle, "_PSC", &handle);
721 if (ACPI_SUCCESS(status))
722 device->power.flags.explicit_get = 1;
723 status = acpi_get_handle(device->handle, "_IRC", &handle);
724 if (ACPI_SUCCESS(status))
725 device->power.flags.inrush_current = 1;
728 * Enumerate supported power management states
730 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
731 struct acpi_device_power_state *ps = &device->power.states[i];
732 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
734 /* Evaluate "_PRx" to se if power resources are referenced */
735 acpi_evaluate_reference(device->handle, object_name, NULL,
737 if (ps->resources.count) {
738 device->power.flags.power_resources = 1;
742 /* Evaluate "_PSx" to see if we can do explicit sets */
743 object_name[2] = 'S';
744 status = acpi_get_handle(device->handle, object_name, &handle);
745 if (ACPI_SUCCESS(status)) {
746 ps->flags.explicit_set = 1;
750 /* State is valid if we have some power control */
751 if (ps->resources.count || ps->flags.explicit_set)
754 ps->power = -1; /* Unknown - driver assigned */
755 ps->latency = -1; /* Unknown - driver assigned */
758 /* Set defaults for D0 and D3 states (always valid) */
759 device->power.states[ACPI_STATE_D0].flags.valid = 1;
760 device->power.states[ACPI_STATE_D0].power = 100;
761 device->power.states[ACPI_STATE_D3].flags.valid = 1;
762 device->power.states[ACPI_STATE_D3].power = 0;
764 /* TBD: System wake support and resource requirements. */
766 device->power.state = ACPI_STATE_UNKNOWN;
771 static int acpi_bus_get_flags(struct acpi_device *device)
773 acpi_status status = AE_OK;
774 acpi_handle temp = NULL;
777 /* Presence of _STA indicates 'dynamic_status' */
778 status = acpi_get_handle(device->handle, "_STA", &temp);
779 if (ACPI_SUCCESS(status))
780 device->flags.dynamic_status = 1;
782 /* Presence of _CID indicates 'compatible_ids' */
783 status = acpi_get_handle(device->handle, "_CID", &temp);
784 if (ACPI_SUCCESS(status))
785 device->flags.compatible_ids = 1;
787 /* Presence of _RMV indicates 'removable' */
788 status = acpi_get_handle(device->handle, "_RMV", &temp);
789 if (ACPI_SUCCESS(status))
790 device->flags.removable = 1;
792 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
793 status = acpi_get_handle(device->handle, "_EJD", &temp);
794 if (ACPI_SUCCESS(status))
795 device->flags.ejectable = 1;
797 status = acpi_get_handle(device->handle, "_EJ0", &temp);
798 if (ACPI_SUCCESS(status))
799 device->flags.ejectable = 1;
802 /* Presence of _LCK indicates 'lockable' */
803 status = acpi_get_handle(device->handle, "_LCK", &temp);
804 if (ACPI_SUCCESS(status))
805 device->flags.lockable = 1;
807 /* Presence of _PS0|_PR0 indicates 'power manageable' */
808 status = acpi_get_handle(device->handle, "_PS0", &temp);
809 if (ACPI_FAILURE(status))
810 status = acpi_get_handle(device->handle, "_PR0", &temp);
811 if (ACPI_SUCCESS(status))
812 device->flags.power_manageable = 1;
814 /* Presence of _PRW indicates wake capable */
815 status = acpi_get_handle(device->handle, "_PRW", &temp);
816 if (ACPI_SUCCESS(status))
817 device->flags.wake_capable = 1;
819 /* TBD: Peformance management */
824 static void acpi_device_get_busid(struct acpi_device *device,
825 acpi_handle handle, int type)
827 char bus_id[5] = { '?', 0 };
828 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
834 * The device's Bus ID is simply the object name.
835 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
838 case ACPI_BUS_TYPE_SYSTEM:
839 strcpy(device->pnp.bus_id, "ACPI");
841 case ACPI_BUS_TYPE_POWER_BUTTON:
842 strcpy(device->pnp.bus_id, "PWRF");
844 case ACPI_BUS_TYPE_SLEEP_BUTTON:
845 strcpy(device->pnp.bus_id, "SLPF");
848 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
849 /* Clean up trailing underscores (if any) */
850 for (i = 3; i > 1; i--) {
851 if (bus_id[i] == '_')
856 strcpy(device->pnp.bus_id, bus_id);
861 static void acpi_device_set_id(struct acpi_device *device,
862 struct acpi_device *parent, acpi_handle handle,
865 struct acpi_device_info *info;
866 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
869 struct acpi_compatible_id_list *cid_list = NULL;
873 case ACPI_BUS_TYPE_DEVICE:
874 status = acpi_get_object_info(handle, &buffer);
875 if (ACPI_FAILURE(status)) {
876 printk("%s: Error reading device info\n", __FUNCTION__);
880 info = buffer.pointer;
881 if (info->valid & ACPI_VALID_HID)
882 hid = info->hardware_id.value;
883 if (info->valid & ACPI_VALID_UID)
884 uid = info->unique_id.value;
885 if (info->valid & ACPI_VALID_CID)
886 cid_list = &info->compatibility_id;
887 if (info->valid & ACPI_VALID_ADR) {
888 device->pnp.bus_address = info->address;
889 device->flags.bus_address = 1;
892 case ACPI_BUS_TYPE_POWER:
893 hid = ACPI_POWER_HID;
895 case ACPI_BUS_TYPE_PROCESSOR:
896 hid = ACPI_PROCESSOR_HID;
898 case ACPI_BUS_TYPE_SYSTEM:
899 hid = ACPI_SYSTEM_HID;
901 case ACPI_BUS_TYPE_THERMAL:
902 hid = ACPI_THERMAL_HID;
904 case ACPI_BUS_TYPE_POWER_BUTTON:
905 hid = ACPI_BUTTON_HID_POWERF;
907 case ACPI_BUS_TYPE_SLEEP_BUTTON:
908 hid = ACPI_BUTTON_HID_SLEEPF;
915 * Fix for the system root bus device -- the only root-level device.
917 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
919 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
920 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
924 strcpy(device->pnp.hardware_id, hid);
925 device->flags.hardware_id = 1;
928 strcpy(device->pnp.unique_id, uid);
929 device->flags.unique_id = 1;
932 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
933 if (device->pnp.cid_list)
934 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
936 printk(KERN_ERR "Memory allocation error\n");
939 kfree(buffer.pointer);
942 static int acpi_device_set_context(struct acpi_device *device, int type)
944 acpi_status status = AE_OK;
949 * Attach this 'struct acpi_device' to the ACPI object. This makes
950 * resolutions from handle->device very efficient. Note that we need
951 * to be careful with fixed-feature devices as they all attach to the
954 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
955 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
956 status = acpi_attach_data(device->handle,
957 acpi_bus_data_handler, device);
959 if (ACPI_FAILURE(status)) {
960 printk("Error attaching device data\n");
967 static void acpi_device_get_debug_info(struct acpi_device *device,
968 acpi_handle handle, int type)
970 #ifdef CONFIG_ACPI_DEBUG_OUTPUT
971 char *type_string = NULL;
972 char name[80] = { '?', '\0' };
973 struct acpi_buffer buffer = { sizeof(name), name };
976 case ACPI_BUS_TYPE_DEVICE:
977 type_string = "Device";
978 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
980 case ACPI_BUS_TYPE_POWER:
981 type_string = "Power Resource";
982 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
984 case ACPI_BUS_TYPE_PROCESSOR:
985 type_string = "Processor";
986 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
988 case ACPI_BUS_TYPE_SYSTEM:
989 type_string = "System";
990 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
992 case ACPI_BUS_TYPE_THERMAL:
993 type_string = "Thermal Zone";
994 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
996 case ACPI_BUS_TYPE_POWER_BUTTON:
997 type_string = "Power Button";
998 sprintf(name, "PWRB");
1000 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1001 type_string = "Sleep Button";
1002 sprintf(name, "SLPB");
1006 printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
1007 #endif /*CONFIG_ACPI_DEBUG_OUTPUT */
1010 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1013 struct acpi_driver *driver;
1019 driver = dev->driver;
1021 if ((driver) && (driver->ops.remove)) {
1023 if (driver->ops.stop) {
1024 result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT);
1029 result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT);
1034 atomic_dec(&dev->driver->references);
1036 acpi_driver_data(dev) = NULL;
1042 if (dev->flags.bus_address) {
1043 if ((dev->parent) && (dev->parent->ops.unbind))
1044 dev->parent->ops.unbind(dev);
1047 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1053 acpi_add_single_object(struct acpi_device **child,
1054 struct acpi_device *parent, acpi_handle handle, int type)
1057 struct acpi_device *device = NULL;
1063 device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
1065 printk(KERN_ERR PREFIX "Memory allocation error\n");
1068 memset(device, 0, sizeof(struct acpi_device));
1070 device->handle = handle;
1071 device->parent = parent;
1073 acpi_device_get_busid(device, handle, type);
1078 * Get prior to calling acpi_bus_get_status() so we know whether
1079 * or not _STA is present. Note that we only look for object
1080 * handles -- cannot evaluate objects until we know the device is
1081 * present and properly initialized.
1083 result = acpi_bus_get_flags(device);
1090 * See if the device is present. We always assume that non-Device
1091 * and non-Processor objects (e.g. thermal zones, power resources,
1092 * etc.) are present, functioning, etc. (at least when parent object
1093 * is present). Note that _STA has a different meaning for some
1094 * objects (e.g. power resources) so we need to be careful how we use
1098 case ACPI_BUS_TYPE_PROCESSOR:
1099 case ACPI_BUS_TYPE_DEVICE:
1100 result = acpi_bus_get_status(device);
1101 if (ACPI_FAILURE(result) || !device->status.present) {
1107 STRUCT_TO_INT(device->status) = 0x0F;
1114 * TBD: Synch with Core's enumeration/initialization process.
1118 * Hardware ID, Unique ID, & Bus Address
1119 * -------------------------------------
1121 acpi_device_set_id(device, parent, handle, type);
1127 if (device->flags.power_manageable) {
1128 result = acpi_bus_get_power_flags(device);
1134 * Wakeup device management
1135 *-----------------------
1137 if (device->flags.wake_capable) {
1138 result = acpi_bus_get_wakeup_device_flags(device);
1144 * Performance Management
1145 * ----------------------
1147 if (device->flags.performance_manageable) {
1148 result = acpi_bus_get_perf_flags(device);
1153 if ((result = acpi_device_set_context(device, type)))
1156 acpi_device_get_debug_info(device, handle, type);
1158 acpi_device_register(device, parent);
1161 * Bind _ADR-Based Devices
1162 * -----------------------
1163 * If there's a a bus address (_ADR) then we utilize the parent's
1164 * 'bind' function (if exists) to bind the ACPI- and natively-
1165 * enumerated device representations.
1167 if (device->flags.bus_address) {
1168 if (device->parent && device->parent->ops.bind)
1169 device->parent->ops.bind(device);
1173 * Locate & Attach Driver
1174 * ----------------------
1175 * If there's a hardware id (_HID) or compatible ids (_CID) we check
1176 * to see if there's a driver installed for this kind of device. Note
1177 * that drivers can install before or after a device is enumerated.
1179 * TBD: Assumes LDM provides driver hot-plug capability.
1181 acpi_bus_find_driver(device);
1187 kfree(device->pnp.cid_list);
1194 static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
1196 acpi_status status = AE_OK;
1197 struct acpi_device *parent = NULL;
1198 struct acpi_device *child = NULL;
1199 acpi_handle phandle = NULL;
1200 acpi_handle chandle = NULL;
1201 acpi_object_type type = 0;
1209 phandle = start->handle;
1212 * Parse through the ACPI namespace, identify all 'devices', and
1213 * create a new 'struct acpi_device' for each.
1215 while ((level > 0) && parent) {
1217 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1221 * If this scope is exhausted then move our way back up.
1223 if (ACPI_FAILURE(status)) {
1226 acpi_get_parent(phandle, &phandle);
1228 parent = parent->parent;
1232 status = acpi_get_type(chandle, &type);
1233 if (ACPI_FAILURE(status))
1237 * If this is a scope object then parse it (depth-first).
1239 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1247 * We're only interested in objects that we consider 'devices'.
1250 case ACPI_TYPE_DEVICE:
1251 type = ACPI_BUS_TYPE_DEVICE;
1253 case ACPI_TYPE_PROCESSOR:
1254 type = ACPI_BUS_TYPE_PROCESSOR;
1256 case ACPI_TYPE_THERMAL:
1257 type = ACPI_BUS_TYPE_THERMAL;
1259 case ACPI_TYPE_POWER:
1260 type = ACPI_BUS_TYPE_POWER;
1266 if (ops->acpi_op_add)
1267 status = acpi_add_single_object(&child, parent,
1270 status = acpi_bus_get_device(chandle, &child);
1272 if (ACPI_FAILURE(status))
1275 if (ops->acpi_op_start) {
1276 status = acpi_start_single_object(child);
1277 if (ACPI_FAILURE(status))
1282 * If the device is present, enabled, and functioning then
1283 * parse its scope (depth-first). Note that we need to
1284 * represent absent devices to facilitate PnP notifications
1285 * -- but only the subtree head (not all of its children,
1286 * which will be enumerated when the parent is inserted).
1288 * TBD: Need notifications and other detection mechanisms
1289 * in place before we can fully implement this.
1291 if (child->status.present) {
1292 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1294 if (ACPI_SUCCESS(status)) {
1307 acpi_bus_add(struct acpi_device **child,
1308 struct acpi_device *parent, acpi_handle handle, int type)
1311 struct acpi_bus_ops ops;
1314 result = acpi_add_single_object(child, parent, handle, type);
1316 memset(&ops, 0, sizeof(ops));
1317 ops.acpi_op_add = 1;
1318 result = acpi_bus_scan(*child, &ops);
1323 EXPORT_SYMBOL(acpi_bus_add);
1325 int acpi_bus_start(struct acpi_device *device)
1328 struct acpi_bus_ops ops;
1334 result = acpi_start_single_object(device);
1336 memset(&ops, 0, sizeof(ops));
1337 ops.acpi_op_start = 1;
1338 result = acpi_bus_scan(device, &ops);
1343 EXPORT_SYMBOL(acpi_bus_start);
1345 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1348 struct acpi_device *parent, *child;
1349 acpi_handle phandle, chandle;
1350 acpi_object_type type;
1355 phandle = start->handle;
1356 child = chandle = NULL;
1358 while ((level > 0) && parent && (!err)) {
1359 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1363 * If this scope is exhausted then move our way back up.
1365 if (ACPI_FAILURE(status)) {
1368 acpi_get_parent(phandle, &phandle);
1370 parent = parent->parent;
1373 err = acpi_bus_remove(child, rmdevice);
1375 err = acpi_bus_remove(child, 1);
1380 status = acpi_get_type(chandle, &type);
1381 if (ACPI_FAILURE(status)) {
1385 * If there is a device corresponding to chandle then
1386 * parse it (depth-first).
1388 if (acpi_bus_get_device(chandle, &child) == 0) {
1398 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1401 static int acpi_bus_scan_fixed(struct acpi_device *root)
1404 struct acpi_device *device = NULL;
1411 * Enumerate all fixed-feature devices.
1413 if (acpi_fadt.pwr_button == 0) {
1414 result = acpi_add_single_object(&device, acpi_root,
1416 ACPI_BUS_TYPE_POWER_BUTTON);
1418 result = acpi_start_single_object(device);
1421 if (acpi_fadt.sleep_button == 0) {
1422 result = acpi_add_single_object(&device, acpi_root,
1424 ACPI_BUS_TYPE_SLEEP_BUTTON);
1426 result = acpi_start_single_object(device);
1432 static int __init acpi_scan_init(void)
1435 struct acpi_bus_ops ops;
1441 result = kset_register(&acpi_namespace_kset);
1443 printk(KERN_ERR PREFIX "kset_register error: %d\n", result);
1445 result = bus_register(&acpi_bus_type);
1447 /* We don't want to quit even if we failed to add suspend/resume */
1448 printk(KERN_ERR PREFIX "Could not register bus type\n");
1452 * Create the root device in the bus's device tree
1454 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
1455 ACPI_BUS_TYPE_SYSTEM);
1459 result = acpi_start_single_object(acpi_root);
1463 acpi_root->dev.bus = &acpi_bus_type;
1464 snprintf(acpi_root->dev.bus_id, BUS_ID_SIZE, "%s", acpi_bus_type.name);
1465 result = device_register(&acpi_root->dev);
1467 /* We don't want to quit even if we failed to add suspend/resume */
1468 printk(KERN_ERR PREFIX "Could not register device\n");
1472 * Enumerate devices in the ACPI namespace.
1474 result = acpi_bus_scan_fixed(acpi_root);
1476 memset(&ops, 0, sizeof(ops));
1477 ops.acpi_op_add = 1;
1478 ops.acpi_op_start = 1;
1479 result = acpi_bus_scan(acpi_root, &ops);
1483 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1489 subsys_initcall(acpi_scan_init);