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 acpi_device_suspend(struct device *dev, pm_message_t state)
227 struct acpi_device *acpi_dev = to_acpi_device(dev);
228 struct acpi_driver *acpi_drv = acpi_dev->driver;
230 if (acpi_drv && acpi_drv->ops.suspend)
231 return acpi_drv->ops.suspend(acpi_dev, state);
235 static int acpi_device_resume(struct device *dev)
237 struct acpi_device *acpi_dev = to_acpi_device(dev);
238 struct acpi_driver *acpi_drv = acpi_dev->driver;
240 if (acpi_drv && acpi_drv->ops.resume)
241 return acpi_drv->ops.resume(acpi_dev);
245 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
247 struct acpi_device *acpi_dev = to_acpi_device(dev);
248 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
250 if (acpi_drv->ops.match)
251 return !acpi_drv->ops.match(acpi_dev, acpi_drv);
252 return !acpi_match_ids(acpi_dev, acpi_drv->ids);
255 static int acpi_device_uevent(struct device *dev, char **envp, int num_envp,
256 char *buffer, int buffer_size)
258 struct acpi_device *acpi_dev = to_acpi_device(dev);
259 int i = 0, length = 0, ret = 0;
261 if (acpi_dev->flags.hardware_id)
262 ret = add_uevent_var(envp, num_envp, &i,
263 buffer, buffer_size, &length,
264 "HWID=%s", acpi_dev->pnp.hardware_id);
267 if (acpi_dev->flags.compatible_ids) {
269 struct acpi_compatible_id_list *cid_list;
271 cid_list = acpi_dev->pnp.cid_list;
273 for (j = 0; j < cid_list->count; j++) {
274 ret = add_uevent_var(envp, num_envp, &i, buffer,
275 buffer_size, &length, "COMPTID=%s",
276 cid_list->id[j].value);
286 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
287 static int acpi_start_single_object(struct acpi_device *);
288 static int acpi_device_probe(struct device * dev)
290 struct acpi_device *acpi_dev = to_acpi_device(dev);
291 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
294 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
296 acpi_start_single_object(acpi_dev);
297 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
298 "Found driver [%s] for device [%s]\n",
299 acpi_drv->name, acpi_dev->pnp.bus_id));
305 static int acpi_device_remove(struct device * dev)
307 struct acpi_device *acpi_dev = to_acpi_device(dev);
308 struct acpi_driver *acpi_drv = acpi_dev->driver;
311 if (acpi_drv->ops.stop)
312 acpi_drv->ops.stop(acpi_dev, ACPI_BUS_REMOVAL_NORMAL);
313 if (acpi_drv->ops.remove)
314 acpi_drv->ops.remove(acpi_dev, ACPI_BUS_REMOVAL_NORMAL);
316 acpi_dev->driver = NULL;
317 acpi_driver_data(dev) = NULL;
323 static void acpi_device_shutdown(struct device *dev)
325 struct acpi_device *acpi_dev = to_acpi_device(dev);
326 struct acpi_driver *acpi_drv = acpi_dev->driver;
328 if (acpi_drv && acpi_drv->ops.shutdown)
329 acpi_drv->ops.shutdown(acpi_dev);
334 static struct bus_type acpi_bus_type = {
336 .suspend = acpi_device_suspend,
337 .resume = acpi_device_resume,
338 .shutdown = acpi_device_shutdown,
339 .match = acpi_bus_match,
340 .probe = acpi_device_probe,
341 .remove = acpi_device_remove,
342 .uevent = acpi_device_uevent,
345 static void acpi_device_register(struct acpi_device *device,
346 struct acpi_device *parent)
353 * Link this device to its parent and siblings.
355 INIT_LIST_HEAD(&device->children);
356 INIT_LIST_HEAD(&device->node);
357 INIT_LIST_HEAD(&device->g_list);
358 INIT_LIST_HEAD(&device->wakeup_list);
360 spin_lock(&acpi_device_lock);
361 if (device->parent) {
362 list_add_tail(&device->node, &device->parent->children);
363 list_add_tail(&device->g_list, &device->parent->g_list);
365 list_add_tail(&device->g_list, &acpi_device_list);
366 if (device->wakeup.flags.valid)
367 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
368 spin_unlock(&acpi_device_lock);
370 strlcpy(device->kobj.name, device->pnp.bus_id, KOBJ_NAME_LEN);
372 device->kobj.parent = &parent->kobj;
373 device->kobj.ktype = &ktype_acpi_ns;
374 device->kobj.kset = &acpi_namespace_kset;
375 err = kobject_register(&device->kobj);
377 printk(KERN_WARNING "%s: kobject_register error: %d\n",
379 create_sysfs_device_files(device);
382 static void acpi_device_unregister(struct acpi_device *device, int type)
384 spin_lock(&acpi_device_lock);
385 if (device->parent) {
386 list_del(&device->node);
387 list_del(&device->g_list);
389 list_del(&device->g_list);
391 list_del(&device->wakeup_list);
393 spin_unlock(&acpi_device_lock);
395 acpi_detach_data(device->handle, acpi_bus_data_handler);
396 remove_sysfs_device_files(device);
397 kobject_unregister(&device->kobj);
400 /* --------------------------------------------------------------------------
402 -------------------------------------------------------------------------- */
403 static LIST_HEAD(acpi_bus_drivers);
406 * acpi_bus_driver_init - add a device to a driver
407 * @device: the device to add and initialize
408 * @driver: driver for the device
410 * Used to initialize a device via its device driver. Called whenever a
411 * driver is bound to a device. Invokes the driver's add() and start() ops.
414 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
419 if (!device || !driver)
422 if (!driver->ops.add)
425 result = driver->ops.add(device);
427 device->driver = NULL;
428 acpi_driver_data(device) = NULL;
432 device->driver = driver;
435 * TBD - Configuration Management: Assign resources to device based
436 * upon possible configuration and currently allocated resources.
439 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
440 "Driver successfully bound to device\n"));
444 static int acpi_start_single_object(struct acpi_device *device)
447 struct acpi_driver *driver;
450 if (!(driver = device->driver))
453 if (driver->ops.start) {
454 result = driver->ops.start(device);
455 if (result && driver->ops.remove)
456 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
462 static void acpi_driver_attach(struct acpi_driver *drv)
464 struct list_head *node, *next;
467 spin_lock(&acpi_device_lock);
468 list_for_each_safe(node, next, &acpi_device_list) {
469 struct acpi_device *dev =
470 container_of(node, struct acpi_device, g_list);
472 if (dev->driver || !dev->status.present)
474 spin_unlock(&acpi_device_lock);
476 if (!acpi_bus_match(&(dev->dev), &(drv->drv))) {
477 if (!acpi_bus_driver_init(dev, drv)) {
478 acpi_start_single_object(dev);
479 atomic_inc(&drv->references);
480 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
481 "Found driver [%s] for device [%s]\n",
482 drv->name, dev->pnp.bus_id));
485 spin_lock(&acpi_device_lock);
487 spin_unlock(&acpi_device_lock);
490 static void acpi_driver_detach(struct acpi_driver *drv)
492 struct list_head *node, *next;
495 spin_lock(&acpi_device_lock);
496 list_for_each_safe(node, next, &acpi_device_list) {
497 struct acpi_device *dev =
498 container_of(node, struct acpi_device, g_list);
500 if (dev->driver == drv) {
501 spin_unlock(&acpi_device_lock);
503 drv->ops.remove(dev, ACPI_BUS_REMOVAL_NORMAL);
504 spin_lock(&acpi_device_lock);
506 dev->driver_data = NULL;
507 atomic_dec(&drv->references);
510 spin_unlock(&acpi_device_lock);
514 * acpi_bus_register_driver - register a driver with the ACPI bus
515 * @driver: driver being registered
517 * Registers a driver with the ACPI bus. Searches the namespace for all
518 * devices that match the driver's criteria and binds. Returns zero for
519 * success or a negative error status for failure.
521 int acpi_bus_register_driver(struct acpi_driver *driver)
527 spin_lock(&acpi_device_lock);
528 list_add_tail(&driver->node, &acpi_bus_drivers);
529 spin_unlock(&acpi_device_lock);
530 acpi_driver_attach(driver);
535 EXPORT_SYMBOL(acpi_bus_register_driver);
538 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
539 * @driver: driver to unregister
541 * Unregisters a driver with the ACPI bus. Searches the namespace for all
542 * devices that match the driver's criteria and unbinds.
544 void acpi_bus_unregister_driver(struct acpi_driver *driver)
546 acpi_driver_detach(driver);
548 if (!atomic_read(&driver->references)) {
549 spin_lock(&acpi_device_lock);
550 list_del_init(&driver->node);
551 spin_unlock(&acpi_device_lock);
556 EXPORT_SYMBOL(acpi_bus_unregister_driver);
559 * acpi_bus_find_driver - check if there is a driver installed for the device
560 * @device: device that we are trying to find a supporting driver for
562 * Parses the list of registered drivers looking for a driver applicable for
563 * the specified device.
565 static int acpi_bus_find_driver(struct acpi_device *device)
568 struct list_head *node, *next;
571 spin_lock(&acpi_device_lock);
572 list_for_each_safe(node, next, &acpi_bus_drivers) {
573 struct acpi_driver *driver =
574 container_of(node, struct acpi_driver, node);
576 atomic_inc(&driver->references);
577 spin_unlock(&acpi_device_lock);
578 if (!acpi_bus_match(&(device->dev), &(driver->drv))) {
579 result = acpi_bus_driver_init(device, driver);
583 atomic_dec(&driver->references);
584 spin_lock(&acpi_device_lock);
586 spin_unlock(&acpi_device_lock);
592 /* --------------------------------------------------------------------------
594 -------------------------------------------------------------------------- */
596 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
600 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
601 union acpi_object *obj;
603 status = acpi_get_handle(handle, "_EJD", &tmp);
604 if (ACPI_FAILURE(status))
607 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
608 if (ACPI_SUCCESS(status)) {
609 obj = buffer.pointer;
610 status = acpi_get_handle(NULL, obj->string.pointer, ejd);
611 kfree(buffer.pointer);
615 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
617 void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
625 int acpi_match_ids(struct acpi_device *device, char *ids)
627 if (device->flags.hardware_id)
628 if (strstr(ids, device->pnp.hardware_id))
631 if (device->flags.compatible_ids) {
632 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
635 /* compare multiple _CID entries against driver ids */
636 for (i = 0; i < cid_list->count; i++) {
637 if (strstr(ids, cid_list->id[i].value))
644 static int acpi_bus_get_perf_flags(struct acpi_device *device)
646 device->performance.state = ACPI_STATE_UNKNOWN;
651 acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
652 union acpi_object *package)
655 union acpi_object *element = NULL;
657 if (!device || !package || (package->package.count < 2))
658 return AE_BAD_PARAMETER;
660 element = &(package->package.elements[0]);
662 return AE_BAD_PARAMETER;
663 if (element->type == ACPI_TYPE_PACKAGE) {
664 if ((element->package.count < 2) ||
665 (element->package.elements[0].type !=
666 ACPI_TYPE_LOCAL_REFERENCE)
667 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
669 device->wakeup.gpe_device =
670 element->package.elements[0].reference.handle;
671 device->wakeup.gpe_number =
672 (u32) element->package.elements[1].integer.value;
673 } else if (element->type == ACPI_TYPE_INTEGER) {
674 device->wakeup.gpe_number = element->integer.value;
678 element = &(package->package.elements[1]);
679 if (element->type != ACPI_TYPE_INTEGER) {
682 device->wakeup.sleep_state = element->integer.value;
684 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
687 device->wakeup.resources.count = package->package.count - 2;
688 for (i = 0; i < device->wakeup.resources.count; i++) {
689 element = &(package->package.elements[i + 2]);
690 if (element->type != ACPI_TYPE_ANY) {
694 device->wakeup.resources.handles[i] = element->reference.handle;
700 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
702 acpi_status status = 0;
703 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
704 union acpi_object *package = NULL;
708 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
709 if (ACPI_FAILURE(status)) {
710 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
714 package = (union acpi_object *)buffer.pointer;
715 status = acpi_bus_extract_wakeup_device_power_package(device, package);
716 if (ACPI_FAILURE(status)) {
717 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
721 kfree(buffer.pointer);
723 device->wakeup.flags.valid = 1;
724 /* Power button, Lid switch always enable wakeup */
725 if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
726 device->wakeup.flags.run_wake = 1;
729 if (ACPI_FAILURE(status))
730 device->flags.wake_capable = 0;
734 static int acpi_bus_get_power_flags(struct acpi_device *device)
736 acpi_status status = 0;
737 acpi_handle handle = NULL;
742 * Power Management Flags
744 status = acpi_get_handle(device->handle, "_PSC", &handle);
745 if (ACPI_SUCCESS(status))
746 device->power.flags.explicit_get = 1;
747 status = acpi_get_handle(device->handle, "_IRC", &handle);
748 if (ACPI_SUCCESS(status))
749 device->power.flags.inrush_current = 1;
752 * Enumerate supported power management states
754 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
755 struct acpi_device_power_state *ps = &device->power.states[i];
756 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
758 /* Evaluate "_PRx" to se if power resources are referenced */
759 acpi_evaluate_reference(device->handle, object_name, NULL,
761 if (ps->resources.count) {
762 device->power.flags.power_resources = 1;
766 /* Evaluate "_PSx" to see if we can do explicit sets */
767 object_name[2] = 'S';
768 status = acpi_get_handle(device->handle, object_name, &handle);
769 if (ACPI_SUCCESS(status)) {
770 ps->flags.explicit_set = 1;
774 /* State is valid if we have some power control */
775 if (ps->resources.count || ps->flags.explicit_set)
778 ps->power = -1; /* Unknown - driver assigned */
779 ps->latency = -1; /* Unknown - driver assigned */
782 /* Set defaults for D0 and D3 states (always valid) */
783 device->power.states[ACPI_STATE_D0].flags.valid = 1;
784 device->power.states[ACPI_STATE_D0].power = 100;
785 device->power.states[ACPI_STATE_D3].flags.valid = 1;
786 device->power.states[ACPI_STATE_D3].power = 0;
788 /* TBD: System wake support and resource requirements. */
790 device->power.state = ACPI_STATE_UNKNOWN;
795 static int acpi_bus_get_flags(struct acpi_device *device)
797 acpi_status status = AE_OK;
798 acpi_handle temp = NULL;
801 /* Presence of _STA indicates 'dynamic_status' */
802 status = acpi_get_handle(device->handle, "_STA", &temp);
803 if (ACPI_SUCCESS(status))
804 device->flags.dynamic_status = 1;
806 /* Presence of _CID indicates 'compatible_ids' */
807 status = acpi_get_handle(device->handle, "_CID", &temp);
808 if (ACPI_SUCCESS(status))
809 device->flags.compatible_ids = 1;
811 /* Presence of _RMV indicates 'removable' */
812 status = acpi_get_handle(device->handle, "_RMV", &temp);
813 if (ACPI_SUCCESS(status))
814 device->flags.removable = 1;
816 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
817 status = acpi_get_handle(device->handle, "_EJD", &temp);
818 if (ACPI_SUCCESS(status))
819 device->flags.ejectable = 1;
821 status = acpi_get_handle(device->handle, "_EJ0", &temp);
822 if (ACPI_SUCCESS(status))
823 device->flags.ejectable = 1;
826 /* Presence of _LCK indicates 'lockable' */
827 status = acpi_get_handle(device->handle, "_LCK", &temp);
828 if (ACPI_SUCCESS(status))
829 device->flags.lockable = 1;
831 /* Presence of _PS0|_PR0 indicates 'power manageable' */
832 status = acpi_get_handle(device->handle, "_PS0", &temp);
833 if (ACPI_FAILURE(status))
834 status = acpi_get_handle(device->handle, "_PR0", &temp);
835 if (ACPI_SUCCESS(status))
836 device->flags.power_manageable = 1;
838 /* Presence of _PRW indicates wake capable */
839 status = acpi_get_handle(device->handle, "_PRW", &temp);
840 if (ACPI_SUCCESS(status))
841 device->flags.wake_capable = 1;
843 /* TBD: Peformance management */
848 static void acpi_device_get_busid(struct acpi_device *device,
849 acpi_handle handle, int type)
851 char bus_id[5] = { '?', 0 };
852 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
858 * The device's Bus ID is simply the object name.
859 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
862 case ACPI_BUS_TYPE_SYSTEM:
863 strcpy(device->pnp.bus_id, "ACPI");
865 case ACPI_BUS_TYPE_POWER_BUTTON:
866 strcpy(device->pnp.bus_id, "PWRF");
868 case ACPI_BUS_TYPE_SLEEP_BUTTON:
869 strcpy(device->pnp.bus_id, "SLPF");
872 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
873 /* Clean up trailing underscores (if any) */
874 for (i = 3; i > 1; i--) {
875 if (bus_id[i] == '_')
880 strcpy(device->pnp.bus_id, bus_id);
885 static void acpi_device_set_id(struct acpi_device *device,
886 struct acpi_device *parent, acpi_handle handle,
889 struct acpi_device_info *info;
890 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
893 struct acpi_compatible_id_list *cid_list = NULL;
897 case ACPI_BUS_TYPE_DEVICE:
898 status = acpi_get_object_info(handle, &buffer);
899 if (ACPI_FAILURE(status)) {
900 printk("%s: Error reading device info\n", __FUNCTION__);
904 info = buffer.pointer;
905 if (info->valid & ACPI_VALID_HID)
906 hid = info->hardware_id.value;
907 if (info->valid & ACPI_VALID_UID)
908 uid = info->unique_id.value;
909 if (info->valid & ACPI_VALID_CID)
910 cid_list = &info->compatibility_id;
911 if (info->valid & ACPI_VALID_ADR) {
912 device->pnp.bus_address = info->address;
913 device->flags.bus_address = 1;
916 case ACPI_BUS_TYPE_POWER:
917 hid = ACPI_POWER_HID;
919 case ACPI_BUS_TYPE_PROCESSOR:
920 hid = ACPI_PROCESSOR_HID;
922 case ACPI_BUS_TYPE_SYSTEM:
923 hid = ACPI_SYSTEM_HID;
925 case ACPI_BUS_TYPE_THERMAL:
926 hid = ACPI_THERMAL_HID;
928 case ACPI_BUS_TYPE_POWER_BUTTON:
929 hid = ACPI_BUTTON_HID_POWERF;
931 case ACPI_BUS_TYPE_SLEEP_BUTTON:
932 hid = ACPI_BUTTON_HID_SLEEPF;
939 * Fix for the system root bus device -- the only root-level device.
941 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
943 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
944 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
948 strcpy(device->pnp.hardware_id, hid);
949 device->flags.hardware_id = 1;
952 strcpy(device->pnp.unique_id, uid);
953 device->flags.unique_id = 1;
956 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
957 if (device->pnp.cid_list)
958 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
960 printk(KERN_ERR "Memory allocation error\n");
963 kfree(buffer.pointer);
966 static int acpi_device_set_context(struct acpi_device *device, int type)
968 acpi_status status = AE_OK;
973 * Attach this 'struct acpi_device' to the ACPI object. This makes
974 * resolutions from handle->device very efficient. Note that we need
975 * to be careful with fixed-feature devices as they all attach to the
978 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
979 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
980 status = acpi_attach_data(device->handle,
981 acpi_bus_data_handler, device);
983 if (ACPI_FAILURE(status)) {
984 printk("Error attaching device data\n");
991 static void acpi_device_get_debug_info(struct acpi_device *device,
992 acpi_handle handle, int type)
994 #ifdef CONFIG_ACPI_DEBUG_OUTPUT
995 char *type_string = NULL;
996 char name[80] = { '?', '\0' };
997 struct acpi_buffer buffer = { sizeof(name), name };
1000 case ACPI_BUS_TYPE_DEVICE:
1001 type_string = "Device";
1002 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1004 case ACPI_BUS_TYPE_POWER:
1005 type_string = "Power Resource";
1006 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1008 case ACPI_BUS_TYPE_PROCESSOR:
1009 type_string = "Processor";
1010 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1012 case ACPI_BUS_TYPE_SYSTEM:
1013 type_string = "System";
1014 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1016 case ACPI_BUS_TYPE_THERMAL:
1017 type_string = "Thermal Zone";
1018 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1020 case ACPI_BUS_TYPE_POWER_BUTTON:
1021 type_string = "Power Button";
1022 sprintf(name, "PWRB");
1024 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1025 type_string = "Sleep Button";
1026 sprintf(name, "SLPB");
1030 printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
1031 #endif /*CONFIG_ACPI_DEBUG_OUTPUT */
1034 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1037 struct acpi_driver *driver;
1043 driver = dev->driver;
1045 if ((driver) && (driver->ops.remove)) {
1047 if (driver->ops.stop) {
1048 result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT);
1053 result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT);
1058 atomic_dec(&dev->driver->references);
1060 acpi_driver_data(dev) = NULL;
1066 if (dev->flags.bus_address) {
1067 if ((dev->parent) && (dev->parent->ops.unbind))
1068 dev->parent->ops.unbind(dev);
1071 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1077 acpi_add_single_object(struct acpi_device **child,
1078 struct acpi_device *parent, acpi_handle handle, int type)
1081 struct acpi_device *device = NULL;
1087 device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
1089 printk(KERN_ERR PREFIX "Memory allocation error\n");
1092 memset(device, 0, sizeof(struct acpi_device));
1094 device->handle = handle;
1095 device->parent = parent;
1097 acpi_device_get_busid(device, handle, type);
1102 * Get prior to calling acpi_bus_get_status() so we know whether
1103 * or not _STA is present. Note that we only look for object
1104 * handles -- cannot evaluate objects until we know the device is
1105 * present and properly initialized.
1107 result = acpi_bus_get_flags(device);
1114 * See if the device is present. We always assume that non-Device
1115 * and non-Processor objects (e.g. thermal zones, power resources,
1116 * etc.) are present, functioning, etc. (at least when parent object
1117 * is present). Note that _STA has a different meaning for some
1118 * objects (e.g. power resources) so we need to be careful how we use
1122 case ACPI_BUS_TYPE_PROCESSOR:
1123 case ACPI_BUS_TYPE_DEVICE:
1124 result = acpi_bus_get_status(device);
1125 if (ACPI_FAILURE(result) || !device->status.present) {
1131 STRUCT_TO_INT(device->status) = 0x0F;
1138 * TBD: Synch with Core's enumeration/initialization process.
1142 * Hardware ID, Unique ID, & Bus Address
1143 * -------------------------------------
1145 acpi_device_set_id(device, parent, handle, type);
1151 if (device->flags.power_manageable) {
1152 result = acpi_bus_get_power_flags(device);
1158 * Wakeup device management
1159 *-----------------------
1161 if (device->flags.wake_capable) {
1162 result = acpi_bus_get_wakeup_device_flags(device);
1168 * Performance Management
1169 * ----------------------
1171 if (device->flags.performance_manageable) {
1172 result = acpi_bus_get_perf_flags(device);
1177 if ((result = acpi_device_set_context(device, type)))
1180 acpi_device_get_debug_info(device, handle, type);
1182 acpi_device_register(device, parent);
1185 * Bind _ADR-Based Devices
1186 * -----------------------
1187 * If there's a a bus address (_ADR) then we utilize the parent's
1188 * 'bind' function (if exists) to bind the ACPI- and natively-
1189 * enumerated device representations.
1191 if (device->flags.bus_address) {
1192 if (device->parent && device->parent->ops.bind)
1193 device->parent->ops.bind(device);
1197 * Locate & Attach Driver
1198 * ----------------------
1199 * If there's a hardware id (_HID) or compatible ids (_CID) we check
1200 * to see if there's a driver installed for this kind of device. Note
1201 * that drivers can install before or after a device is enumerated.
1203 * TBD: Assumes LDM provides driver hot-plug capability.
1205 acpi_bus_find_driver(device);
1211 kfree(device->pnp.cid_list);
1218 static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
1220 acpi_status status = AE_OK;
1221 struct acpi_device *parent = NULL;
1222 struct acpi_device *child = NULL;
1223 acpi_handle phandle = NULL;
1224 acpi_handle chandle = NULL;
1225 acpi_object_type type = 0;
1233 phandle = start->handle;
1236 * Parse through the ACPI namespace, identify all 'devices', and
1237 * create a new 'struct acpi_device' for each.
1239 while ((level > 0) && parent) {
1241 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1245 * If this scope is exhausted then move our way back up.
1247 if (ACPI_FAILURE(status)) {
1250 acpi_get_parent(phandle, &phandle);
1252 parent = parent->parent;
1256 status = acpi_get_type(chandle, &type);
1257 if (ACPI_FAILURE(status))
1261 * If this is a scope object then parse it (depth-first).
1263 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1271 * We're only interested in objects that we consider 'devices'.
1274 case ACPI_TYPE_DEVICE:
1275 type = ACPI_BUS_TYPE_DEVICE;
1277 case ACPI_TYPE_PROCESSOR:
1278 type = ACPI_BUS_TYPE_PROCESSOR;
1280 case ACPI_TYPE_THERMAL:
1281 type = ACPI_BUS_TYPE_THERMAL;
1283 case ACPI_TYPE_POWER:
1284 type = ACPI_BUS_TYPE_POWER;
1290 if (ops->acpi_op_add)
1291 status = acpi_add_single_object(&child, parent,
1294 status = acpi_bus_get_device(chandle, &child);
1296 if (ACPI_FAILURE(status))
1299 if (ops->acpi_op_start) {
1300 status = acpi_start_single_object(child);
1301 if (ACPI_FAILURE(status))
1306 * If the device is present, enabled, and functioning then
1307 * parse its scope (depth-first). Note that we need to
1308 * represent absent devices to facilitate PnP notifications
1309 * -- but only the subtree head (not all of its children,
1310 * which will be enumerated when the parent is inserted).
1312 * TBD: Need notifications and other detection mechanisms
1313 * in place before we can fully implement this.
1315 if (child->status.present) {
1316 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1318 if (ACPI_SUCCESS(status)) {
1331 acpi_bus_add(struct acpi_device **child,
1332 struct acpi_device *parent, acpi_handle handle, int type)
1335 struct acpi_bus_ops ops;
1338 result = acpi_add_single_object(child, parent, handle, type);
1340 memset(&ops, 0, sizeof(ops));
1341 ops.acpi_op_add = 1;
1342 result = acpi_bus_scan(*child, &ops);
1347 EXPORT_SYMBOL(acpi_bus_add);
1349 int acpi_bus_start(struct acpi_device *device)
1352 struct acpi_bus_ops ops;
1358 result = acpi_start_single_object(device);
1360 memset(&ops, 0, sizeof(ops));
1361 ops.acpi_op_start = 1;
1362 result = acpi_bus_scan(device, &ops);
1367 EXPORT_SYMBOL(acpi_bus_start);
1369 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1372 struct acpi_device *parent, *child;
1373 acpi_handle phandle, chandle;
1374 acpi_object_type type;
1379 phandle = start->handle;
1380 child = chandle = NULL;
1382 while ((level > 0) && parent && (!err)) {
1383 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1387 * If this scope is exhausted then move our way back up.
1389 if (ACPI_FAILURE(status)) {
1392 acpi_get_parent(phandle, &phandle);
1394 parent = parent->parent;
1397 err = acpi_bus_remove(child, rmdevice);
1399 err = acpi_bus_remove(child, 1);
1404 status = acpi_get_type(chandle, &type);
1405 if (ACPI_FAILURE(status)) {
1409 * If there is a device corresponding to chandle then
1410 * parse it (depth-first).
1412 if (acpi_bus_get_device(chandle, &child) == 0) {
1422 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1425 static int acpi_bus_scan_fixed(struct acpi_device *root)
1428 struct acpi_device *device = NULL;
1435 * Enumerate all fixed-feature devices.
1437 if (acpi_fadt.pwr_button == 0) {
1438 result = acpi_add_single_object(&device, acpi_root,
1440 ACPI_BUS_TYPE_POWER_BUTTON);
1442 result = acpi_start_single_object(device);
1445 if (acpi_fadt.sleep_button == 0) {
1446 result = acpi_add_single_object(&device, acpi_root,
1448 ACPI_BUS_TYPE_SLEEP_BUTTON);
1450 result = acpi_start_single_object(device);
1456 static int __init acpi_scan_init(void)
1459 struct acpi_bus_ops ops;
1465 result = kset_register(&acpi_namespace_kset);
1467 printk(KERN_ERR PREFIX "kset_register error: %d\n", result);
1469 result = bus_register(&acpi_bus_type);
1471 /* We don't want to quit even if we failed to add suspend/resume */
1472 printk(KERN_ERR PREFIX "Could not register bus type\n");
1476 * Create the root device in the bus's device tree
1478 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
1479 ACPI_BUS_TYPE_SYSTEM);
1483 result = acpi_start_single_object(acpi_root);
1487 acpi_root->dev.bus = &acpi_bus_type;
1488 snprintf(acpi_root->dev.bus_id, BUS_ID_SIZE, "%s", acpi_bus_type.name);
1489 result = device_register(&acpi_root->dev);
1491 /* We don't want to quit even if we failed to add suspend/resume */
1492 printk(KERN_ERR PREFIX "Could not register device\n");
1496 * Enumerate devices in the ACPI namespace.
1498 result = acpi_bus_scan_fixed(acpi_root);
1500 memset(&ops, 0, sizeof(ops));
1501 ops.acpi_op_add = 1;
1502 ops.acpi_op_start = 1;
1503 result = acpi_bus_scan(acpi_root, &ops);
1507 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1513 subsys_initcall(acpi_scan_init);