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_legacy(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_legacy,
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 void acpi_device_release(struct device *dev)
227 struct acpi_device *acpi_dev = to_acpi_device(dev);
229 kfree(acpi_dev->pnp.cid_list);
233 static int acpi_device_suspend(struct device *dev, pm_message_t state)
235 struct acpi_device *acpi_dev = to_acpi_device(dev);
236 struct acpi_driver *acpi_drv = acpi_dev->driver;
238 if (acpi_drv && acpi_drv->ops.suspend)
239 return acpi_drv->ops.suspend(acpi_dev, state);
243 static int acpi_device_resume(struct device *dev)
245 struct acpi_device *acpi_dev = to_acpi_device(dev);
246 struct acpi_driver *acpi_drv = acpi_dev->driver;
248 if (acpi_drv && acpi_drv->ops.resume)
249 return acpi_drv->ops.resume(acpi_dev);
253 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
255 struct acpi_device *acpi_dev = to_acpi_device(dev);
256 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
258 if (acpi_drv->ops.match)
259 return !acpi_drv->ops.match(acpi_dev, acpi_drv);
260 return !acpi_match_ids(acpi_dev, acpi_drv->ids);
263 static int acpi_device_uevent(struct device *dev, char **envp, int num_envp,
264 char *buffer, int buffer_size)
266 struct acpi_device *acpi_dev = to_acpi_device(dev);
267 int i = 0, length = 0, ret = 0;
269 if (acpi_dev->flags.hardware_id)
270 ret = add_uevent_var(envp, num_envp, &i,
271 buffer, buffer_size, &length,
272 "HWID=%s", acpi_dev->pnp.hardware_id);
275 if (acpi_dev->flags.compatible_ids) {
277 struct acpi_compatible_id_list *cid_list;
279 cid_list = acpi_dev->pnp.cid_list;
281 for (j = 0; j < cid_list->count; j++) {
282 ret = add_uevent_var(envp, num_envp, &i, buffer,
283 buffer_size, &length, "COMPTID=%s",
284 cid_list->id[j].value);
294 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
295 static int acpi_start_single_object(struct acpi_device *);
296 static int acpi_device_probe(struct device * dev)
298 struct acpi_device *acpi_dev = to_acpi_device(dev);
299 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
302 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
304 acpi_start_single_object(acpi_dev);
305 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
306 "Found driver [%s] for device [%s]\n",
307 acpi_drv->name, acpi_dev->pnp.bus_id));
313 static int acpi_device_remove(struct device * dev)
315 struct acpi_device *acpi_dev = to_acpi_device(dev);
316 struct acpi_driver *acpi_drv = acpi_dev->driver;
319 if (acpi_drv->ops.stop)
320 acpi_drv->ops.stop(acpi_dev, ACPI_BUS_REMOVAL_NORMAL);
321 if (acpi_drv->ops.remove)
322 acpi_drv->ops.remove(acpi_dev, ACPI_BUS_REMOVAL_NORMAL);
324 acpi_dev->driver = NULL;
325 acpi_driver_data(dev) = NULL;
331 static void acpi_device_shutdown(struct device *dev)
333 struct acpi_device *acpi_dev = to_acpi_device(dev);
334 struct acpi_driver *acpi_drv = acpi_dev->driver;
336 if (acpi_drv && acpi_drv->ops.shutdown)
337 acpi_drv->ops.shutdown(acpi_dev);
342 static struct bus_type acpi_bus_type = {
344 .suspend = acpi_device_suspend,
345 .resume = acpi_device_resume,
346 .shutdown = acpi_device_shutdown,
347 .match = acpi_bus_match,
348 .probe = acpi_device_probe,
349 .remove = acpi_device_remove,
350 .uevent = acpi_device_uevent,
353 static void acpi_device_register(struct acpi_device *device,
354 struct acpi_device *parent)
361 * Link this device to its parent and siblings.
363 INIT_LIST_HEAD(&device->children);
364 INIT_LIST_HEAD(&device->node);
365 INIT_LIST_HEAD(&device->g_list);
366 INIT_LIST_HEAD(&device->wakeup_list);
368 spin_lock(&acpi_device_lock);
369 if (device->parent) {
370 list_add_tail(&device->node, &device->parent->children);
371 list_add_tail(&device->g_list, &device->parent->g_list);
373 list_add_tail(&device->g_list, &acpi_device_list);
374 if (device->wakeup.flags.valid)
375 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
376 spin_unlock(&acpi_device_lock);
378 strlcpy(device->kobj.name, device->pnp.bus_id, KOBJ_NAME_LEN);
380 device->kobj.parent = &parent->kobj;
381 device->kobj.ktype = &ktype_acpi_ns;
382 device->kobj.kset = &acpi_namespace_kset;
383 err = kobject_register(&device->kobj);
385 printk(KERN_WARNING "%s: kobject_register error: %d\n",
387 create_sysfs_device_files(device);
390 device->dev.parent = &parent->dev;
391 device->dev.bus = &acpi_bus_type;
392 device_initialize(&device->dev);
393 sprintf(device->dev.bus_id, "%s", device->pnp.bus_id);
394 device->dev.release = &acpi_device_release;
395 device_add(&device->dev);
398 static void acpi_device_unregister(struct acpi_device *device, int type)
400 spin_lock(&acpi_device_lock);
401 if (device->parent) {
402 list_del(&device->node);
403 list_del(&device->g_list);
405 list_del(&device->g_list);
407 list_del(&device->wakeup_list);
409 spin_unlock(&acpi_device_lock);
411 acpi_detach_data(device->handle, acpi_bus_data_handler);
412 remove_sysfs_device_files(device);
413 kobject_unregister(&device->kobj);
415 device_unregister(&device->dev);
418 /* --------------------------------------------------------------------------
420 -------------------------------------------------------------------------- */
422 * acpi_bus_driver_init - add a device to a driver
423 * @device: the device to add and initialize
424 * @driver: driver for the device
426 * Used to initialize a device via its device driver. Called whenever a
427 * driver is bound to a device. Invokes the driver's add() ops.
430 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
435 if (!device || !driver)
438 if (!driver->ops.add)
441 result = driver->ops.add(device);
443 device->driver = NULL;
444 acpi_driver_data(device) = NULL;
448 device->driver = driver;
451 * TBD - Configuration Management: Assign resources to device based
452 * upon possible configuration and currently allocated resources.
455 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
456 "Driver successfully bound to device\n"));
460 static int acpi_start_single_object(struct acpi_device *device)
463 struct acpi_driver *driver;
466 if (!(driver = device->driver))
469 if (driver->ops.start) {
470 result = driver->ops.start(device);
471 if (result && driver->ops.remove)
472 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
479 * acpi_bus_register_driver - register a driver with the ACPI bus
480 * @driver: driver being registered
482 * Registers a driver with the ACPI bus. Searches the namespace for all
483 * devices that match the driver's criteria and binds. Returns zero for
484 * success or a negative error status for failure.
486 int acpi_bus_register_driver(struct acpi_driver *driver)
492 driver->drv.name = driver->name;
493 driver->drv.bus = &acpi_bus_type;
494 driver->drv.owner = driver->owner;
496 ret = driver_register(&driver->drv);
500 EXPORT_SYMBOL(acpi_bus_register_driver);
503 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
504 * @driver: driver to unregister
506 * Unregisters a driver with the ACPI bus. Searches the namespace for all
507 * devices that match the driver's criteria and unbinds.
509 void acpi_bus_unregister_driver(struct acpi_driver *driver)
511 driver_unregister(&driver->drv);
514 EXPORT_SYMBOL(acpi_bus_unregister_driver);
516 /* --------------------------------------------------------------------------
518 -------------------------------------------------------------------------- */
520 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
524 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
525 union acpi_object *obj;
527 status = acpi_get_handle(handle, "_EJD", &tmp);
528 if (ACPI_FAILURE(status))
531 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
532 if (ACPI_SUCCESS(status)) {
533 obj = buffer.pointer;
534 status = acpi_get_handle(NULL, obj->string.pointer, ejd);
535 kfree(buffer.pointer);
539 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
541 void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
549 int acpi_match_ids(struct acpi_device *device, char *ids)
551 if (device->flags.hardware_id)
552 if (strstr(ids, device->pnp.hardware_id))
555 if (device->flags.compatible_ids) {
556 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
559 /* compare multiple _CID entries against driver ids */
560 for (i = 0; i < cid_list->count; i++) {
561 if (strstr(ids, cid_list->id[i].value))
568 static int acpi_bus_get_perf_flags(struct acpi_device *device)
570 device->performance.state = ACPI_STATE_UNKNOWN;
575 acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
576 union acpi_object *package)
579 union acpi_object *element = NULL;
581 if (!device || !package || (package->package.count < 2))
582 return AE_BAD_PARAMETER;
584 element = &(package->package.elements[0]);
586 return AE_BAD_PARAMETER;
587 if (element->type == ACPI_TYPE_PACKAGE) {
588 if ((element->package.count < 2) ||
589 (element->package.elements[0].type !=
590 ACPI_TYPE_LOCAL_REFERENCE)
591 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
593 device->wakeup.gpe_device =
594 element->package.elements[0].reference.handle;
595 device->wakeup.gpe_number =
596 (u32) element->package.elements[1].integer.value;
597 } else if (element->type == ACPI_TYPE_INTEGER) {
598 device->wakeup.gpe_number = element->integer.value;
602 element = &(package->package.elements[1]);
603 if (element->type != ACPI_TYPE_INTEGER) {
606 device->wakeup.sleep_state = element->integer.value;
608 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
611 device->wakeup.resources.count = package->package.count - 2;
612 for (i = 0; i < device->wakeup.resources.count; i++) {
613 element = &(package->package.elements[i + 2]);
614 if (element->type != ACPI_TYPE_ANY) {
618 device->wakeup.resources.handles[i] = element->reference.handle;
624 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
626 acpi_status status = 0;
627 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
628 union acpi_object *package = NULL;
632 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
633 if (ACPI_FAILURE(status)) {
634 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
638 package = (union acpi_object *)buffer.pointer;
639 status = acpi_bus_extract_wakeup_device_power_package(device, package);
640 if (ACPI_FAILURE(status)) {
641 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
645 kfree(buffer.pointer);
647 device->wakeup.flags.valid = 1;
648 /* Power button, Lid switch always enable wakeup */
649 if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
650 device->wakeup.flags.run_wake = 1;
653 if (ACPI_FAILURE(status))
654 device->flags.wake_capable = 0;
658 static int acpi_bus_get_power_flags(struct acpi_device *device)
660 acpi_status status = 0;
661 acpi_handle handle = NULL;
666 * Power Management Flags
668 status = acpi_get_handle(device->handle, "_PSC", &handle);
669 if (ACPI_SUCCESS(status))
670 device->power.flags.explicit_get = 1;
671 status = acpi_get_handle(device->handle, "_IRC", &handle);
672 if (ACPI_SUCCESS(status))
673 device->power.flags.inrush_current = 1;
676 * Enumerate supported power management states
678 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
679 struct acpi_device_power_state *ps = &device->power.states[i];
680 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
682 /* Evaluate "_PRx" to se if power resources are referenced */
683 acpi_evaluate_reference(device->handle, object_name, NULL,
685 if (ps->resources.count) {
686 device->power.flags.power_resources = 1;
690 /* Evaluate "_PSx" to see if we can do explicit sets */
691 object_name[2] = 'S';
692 status = acpi_get_handle(device->handle, object_name, &handle);
693 if (ACPI_SUCCESS(status)) {
694 ps->flags.explicit_set = 1;
698 /* State is valid if we have some power control */
699 if (ps->resources.count || ps->flags.explicit_set)
702 ps->power = -1; /* Unknown - driver assigned */
703 ps->latency = -1; /* Unknown - driver assigned */
706 /* Set defaults for D0 and D3 states (always valid) */
707 device->power.states[ACPI_STATE_D0].flags.valid = 1;
708 device->power.states[ACPI_STATE_D0].power = 100;
709 device->power.states[ACPI_STATE_D3].flags.valid = 1;
710 device->power.states[ACPI_STATE_D3].power = 0;
712 /* TBD: System wake support and resource requirements. */
714 device->power.state = ACPI_STATE_UNKNOWN;
719 static int acpi_bus_get_flags(struct acpi_device *device)
721 acpi_status status = AE_OK;
722 acpi_handle temp = NULL;
725 /* Presence of _STA indicates 'dynamic_status' */
726 status = acpi_get_handle(device->handle, "_STA", &temp);
727 if (ACPI_SUCCESS(status))
728 device->flags.dynamic_status = 1;
730 /* Presence of _CID indicates 'compatible_ids' */
731 status = acpi_get_handle(device->handle, "_CID", &temp);
732 if (ACPI_SUCCESS(status))
733 device->flags.compatible_ids = 1;
735 /* Presence of _RMV indicates 'removable' */
736 status = acpi_get_handle(device->handle, "_RMV", &temp);
737 if (ACPI_SUCCESS(status))
738 device->flags.removable = 1;
740 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
741 status = acpi_get_handle(device->handle, "_EJD", &temp);
742 if (ACPI_SUCCESS(status))
743 device->flags.ejectable = 1;
745 status = acpi_get_handle(device->handle, "_EJ0", &temp);
746 if (ACPI_SUCCESS(status))
747 device->flags.ejectable = 1;
750 /* Presence of _LCK indicates 'lockable' */
751 status = acpi_get_handle(device->handle, "_LCK", &temp);
752 if (ACPI_SUCCESS(status))
753 device->flags.lockable = 1;
755 /* Presence of _PS0|_PR0 indicates 'power manageable' */
756 status = acpi_get_handle(device->handle, "_PS0", &temp);
757 if (ACPI_FAILURE(status))
758 status = acpi_get_handle(device->handle, "_PR0", &temp);
759 if (ACPI_SUCCESS(status))
760 device->flags.power_manageable = 1;
762 /* Presence of _PRW indicates wake capable */
763 status = acpi_get_handle(device->handle, "_PRW", &temp);
764 if (ACPI_SUCCESS(status))
765 device->flags.wake_capable = 1;
767 /* TBD: Peformance management */
772 static void acpi_device_get_busid(struct acpi_device *device,
773 acpi_handle handle, int type)
775 char bus_id[5] = { '?', 0 };
776 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
782 * The device's Bus ID is simply the object name.
783 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
786 case ACPI_BUS_TYPE_SYSTEM:
787 strcpy(device->pnp.bus_id, "ACPI");
789 case ACPI_BUS_TYPE_POWER_BUTTON:
790 strcpy(device->pnp.bus_id, "PWRF");
792 case ACPI_BUS_TYPE_SLEEP_BUTTON:
793 strcpy(device->pnp.bus_id, "SLPF");
796 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
797 /* Clean up trailing underscores (if any) */
798 for (i = 3; i > 1; i--) {
799 if (bus_id[i] == '_')
804 strcpy(device->pnp.bus_id, bus_id);
809 static void acpi_device_set_id(struct acpi_device *device,
810 struct acpi_device *parent, acpi_handle handle,
813 struct acpi_device_info *info;
814 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
817 struct acpi_compatible_id_list *cid_list = NULL;
821 case ACPI_BUS_TYPE_DEVICE:
822 status = acpi_get_object_info(handle, &buffer);
823 if (ACPI_FAILURE(status)) {
824 printk("%s: Error reading device info\n", __FUNCTION__);
828 info = buffer.pointer;
829 if (info->valid & ACPI_VALID_HID)
830 hid = info->hardware_id.value;
831 if (info->valid & ACPI_VALID_UID)
832 uid = info->unique_id.value;
833 if (info->valid & ACPI_VALID_CID)
834 cid_list = &info->compatibility_id;
835 if (info->valid & ACPI_VALID_ADR) {
836 device->pnp.bus_address = info->address;
837 device->flags.bus_address = 1;
840 case ACPI_BUS_TYPE_POWER:
841 hid = ACPI_POWER_HID;
843 case ACPI_BUS_TYPE_PROCESSOR:
844 hid = ACPI_PROCESSOR_HID;
846 case ACPI_BUS_TYPE_SYSTEM:
847 hid = ACPI_SYSTEM_HID;
849 case ACPI_BUS_TYPE_THERMAL:
850 hid = ACPI_THERMAL_HID;
852 case ACPI_BUS_TYPE_POWER_BUTTON:
853 hid = ACPI_BUTTON_HID_POWERF;
855 case ACPI_BUS_TYPE_SLEEP_BUTTON:
856 hid = ACPI_BUTTON_HID_SLEEPF;
863 * Fix for the system root bus device -- the only root-level device.
865 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
867 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
868 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
872 strcpy(device->pnp.hardware_id, hid);
873 device->flags.hardware_id = 1;
876 strcpy(device->pnp.unique_id, uid);
877 device->flags.unique_id = 1;
880 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
881 if (device->pnp.cid_list)
882 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
884 printk(KERN_ERR "Memory allocation error\n");
887 kfree(buffer.pointer);
890 static int acpi_device_set_context(struct acpi_device *device, int type)
892 acpi_status status = AE_OK;
897 * Attach this 'struct acpi_device' to the ACPI object. This makes
898 * resolutions from handle->device very efficient. Note that we need
899 * to be careful with fixed-feature devices as they all attach to the
902 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
903 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
904 status = acpi_attach_data(device->handle,
905 acpi_bus_data_handler, device);
907 if (ACPI_FAILURE(status)) {
908 printk("Error attaching device data\n");
915 static void acpi_device_get_debug_info(struct acpi_device *device,
916 acpi_handle handle, int type)
918 #ifdef CONFIG_ACPI_DEBUG_OUTPUT
919 char *type_string = NULL;
920 char name[80] = { '?', '\0' };
921 struct acpi_buffer buffer = { sizeof(name), name };
924 case ACPI_BUS_TYPE_DEVICE:
925 type_string = "Device";
926 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
928 case ACPI_BUS_TYPE_POWER:
929 type_string = "Power Resource";
930 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
932 case ACPI_BUS_TYPE_PROCESSOR:
933 type_string = "Processor";
934 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
936 case ACPI_BUS_TYPE_SYSTEM:
937 type_string = "System";
938 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
940 case ACPI_BUS_TYPE_THERMAL:
941 type_string = "Thermal Zone";
942 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
944 case ACPI_BUS_TYPE_POWER_BUTTON:
945 type_string = "Power Button";
946 sprintf(name, "PWRB");
948 case ACPI_BUS_TYPE_SLEEP_BUTTON:
949 type_string = "Sleep Button";
950 sprintf(name, "SLPB");
954 printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
955 #endif /*CONFIG_ACPI_DEBUG_OUTPUT */
958 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
963 device_release_driver(&dev->dev);
968 if (dev->flags.bus_address) {
969 if ((dev->parent) && (dev->parent->ops.unbind))
970 dev->parent->ops.unbind(dev);
973 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
979 acpi_add_single_object(struct acpi_device **child,
980 struct acpi_device *parent, acpi_handle handle, int type)
983 struct acpi_device *device = NULL;
989 device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
991 printk(KERN_ERR PREFIX "Memory allocation error\n");
994 memset(device, 0, sizeof(struct acpi_device));
996 device->handle = handle;
997 device->parent = parent;
999 acpi_device_get_busid(device, handle, type);
1004 * Get prior to calling acpi_bus_get_status() so we know whether
1005 * or not _STA is present. Note that we only look for object
1006 * handles -- cannot evaluate objects until we know the device is
1007 * present and properly initialized.
1009 result = acpi_bus_get_flags(device);
1016 * See if the device is present. We always assume that non-Device
1017 * and non-Processor objects (e.g. thermal zones, power resources,
1018 * etc.) are present, functioning, etc. (at least when parent object
1019 * is present). Note that _STA has a different meaning for some
1020 * objects (e.g. power resources) so we need to be careful how we use
1024 case ACPI_BUS_TYPE_PROCESSOR:
1025 case ACPI_BUS_TYPE_DEVICE:
1026 result = acpi_bus_get_status(device);
1027 if (ACPI_FAILURE(result) || !device->status.present) {
1033 STRUCT_TO_INT(device->status) = 0x0F;
1040 * TBD: Synch with Core's enumeration/initialization process.
1044 * Hardware ID, Unique ID, & Bus Address
1045 * -------------------------------------
1047 acpi_device_set_id(device, parent, handle, type);
1053 if (device->flags.power_manageable) {
1054 result = acpi_bus_get_power_flags(device);
1060 * Wakeup device management
1061 *-----------------------
1063 if (device->flags.wake_capable) {
1064 result = acpi_bus_get_wakeup_device_flags(device);
1070 * Performance Management
1071 * ----------------------
1073 if (device->flags.performance_manageable) {
1074 result = acpi_bus_get_perf_flags(device);
1079 if ((result = acpi_device_set_context(device, type)))
1082 acpi_device_get_debug_info(device, handle, type);
1084 acpi_device_register(device, parent);
1087 * Bind _ADR-Based Devices
1088 * -----------------------
1089 * If there's a a bus address (_ADR) then we utilize the parent's
1090 * 'bind' function (if exists) to bind the ACPI- and natively-
1091 * enumerated device representations.
1093 if (device->flags.bus_address) {
1094 if (device->parent && device->parent->ops.bind)
1095 device->parent->ops.bind(device);
1102 kfree(device->pnp.cid_list);
1109 static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
1111 acpi_status status = AE_OK;
1112 struct acpi_device *parent = NULL;
1113 struct acpi_device *child = NULL;
1114 acpi_handle phandle = NULL;
1115 acpi_handle chandle = NULL;
1116 acpi_object_type type = 0;
1124 phandle = start->handle;
1127 * Parse through the ACPI namespace, identify all 'devices', and
1128 * create a new 'struct acpi_device' for each.
1130 while ((level > 0) && parent) {
1132 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1136 * If this scope is exhausted then move our way back up.
1138 if (ACPI_FAILURE(status)) {
1141 acpi_get_parent(phandle, &phandle);
1143 parent = parent->parent;
1147 status = acpi_get_type(chandle, &type);
1148 if (ACPI_FAILURE(status))
1152 * If this is a scope object then parse it (depth-first).
1154 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1162 * We're only interested in objects that we consider 'devices'.
1165 case ACPI_TYPE_DEVICE:
1166 type = ACPI_BUS_TYPE_DEVICE;
1168 case ACPI_TYPE_PROCESSOR:
1169 type = ACPI_BUS_TYPE_PROCESSOR;
1171 case ACPI_TYPE_THERMAL:
1172 type = ACPI_BUS_TYPE_THERMAL;
1174 case ACPI_TYPE_POWER:
1175 type = ACPI_BUS_TYPE_POWER;
1181 if (ops->acpi_op_add)
1182 status = acpi_add_single_object(&child, parent,
1185 status = acpi_bus_get_device(chandle, &child);
1187 if (ACPI_FAILURE(status))
1190 if (ops->acpi_op_start) {
1191 status = acpi_start_single_object(child);
1192 if (ACPI_FAILURE(status))
1197 * If the device is present, enabled, and functioning then
1198 * parse its scope (depth-first). Note that we need to
1199 * represent absent devices to facilitate PnP notifications
1200 * -- but only the subtree head (not all of its children,
1201 * which will be enumerated when the parent is inserted).
1203 * TBD: Need notifications and other detection mechanisms
1204 * in place before we can fully implement this.
1206 if (child->status.present) {
1207 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1209 if (ACPI_SUCCESS(status)) {
1222 acpi_bus_add(struct acpi_device **child,
1223 struct acpi_device *parent, acpi_handle handle, int type)
1226 struct acpi_bus_ops ops;
1229 result = acpi_add_single_object(child, parent, handle, type);
1231 memset(&ops, 0, sizeof(ops));
1232 ops.acpi_op_add = 1;
1233 result = acpi_bus_scan(*child, &ops);
1238 EXPORT_SYMBOL(acpi_bus_add);
1240 int acpi_bus_start(struct acpi_device *device)
1243 struct acpi_bus_ops ops;
1249 result = acpi_start_single_object(device);
1251 memset(&ops, 0, sizeof(ops));
1252 ops.acpi_op_start = 1;
1253 result = acpi_bus_scan(device, &ops);
1258 EXPORT_SYMBOL(acpi_bus_start);
1260 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1263 struct acpi_device *parent, *child;
1264 acpi_handle phandle, chandle;
1265 acpi_object_type type;
1270 phandle = start->handle;
1271 child = chandle = NULL;
1273 while ((level > 0) && parent && (!err)) {
1274 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1278 * If this scope is exhausted then move our way back up.
1280 if (ACPI_FAILURE(status)) {
1283 acpi_get_parent(phandle, &phandle);
1285 parent = parent->parent;
1288 err = acpi_bus_remove(child, rmdevice);
1290 err = acpi_bus_remove(child, 1);
1295 status = acpi_get_type(chandle, &type);
1296 if (ACPI_FAILURE(status)) {
1300 * If there is a device corresponding to chandle then
1301 * parse it (depth-first).
1303 if (acpi_bus_get_device(chandle, &child) == 0) {
1313 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1316 static int acpi_bus_scan_fixed(struct acpi_device *root)
1319 struct acpi_device *device = NULL;
1326 * Enumerate all fixed-feature devices.
1328 if (acpi_fadt.pwr_button == 0) {
1329 result = acpi_add_single_object(&device, acpi_root,
1331 ACPI_BUS_TYPE_POWER_BUTTON);
1333 result = acpi_start_single_object(device);
1336 if (acpi_fadt.sleep_button == 0) {
1337 result = acpi_add_single_object(&device, acpi_root,
1339 ACPI_BUS_TYPE_SLEEP_BUTTON);
1341 result = acpi_start_single_object(device);
1347 static int __init acpi_scan_init(void)
1350 struct acpi_bus_ops ops;
1356 result = kset_register(&acpi_namespace_kset);
1358 printk(KERN_ERR PREFIX "kset_register error: %d\n", result);
1360 result = bus_register(&acpi_bus_type);
1362 /* We don't want to quit even if we failed to add suspend/resume */
1363 printk(KERN_ERR PREFIX "Could not register bus type\n");
1367 * Create the root device in the bus's device tree
1369 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
1370 ACPI_BUS_TYPE_SYSTEM);
1374 result = acpi_start_single_object(acpi_root);
1379 * Enumerate devices in the ACPI namespace.
1381 result = acpi_bus_scan_fixed(acpi_root);
1383 memset(&ops, 0, sizeof(ops));
1384 ops.acpi_op_add = 1;
1385 ops.acpi_op_start = 1;
1386 result = acpi_bus_scan(acpi_root, &ops);
1390 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1396 subsys_initcall(acpi_scan_init);