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/slab.h>
8 #include <linux/kernel.h>
9 #include <linux/acpi.h>
10 #include <linux/signal.h>
11 #include <linux/kthread.h>
12 #include <linux/dmi.h>
14 #include <acpi/acpi_drivers.h>
18 #define _COMPONENT ACPI_BUS_COMPONENT
19 ACPI_MODULE_NAME("scan");
20 #define STRUCT_TO_INT(s) (*((int*)&s))
21 extern struct acpi_device *acpi_root;
23 #define ACPI_BUS_CLASS "system_bus"
24 #define ACPI_BUS_HID "LNXSYBUS"
25 #define ACPI_BUS_DEVICE_NAME "System Bus"
27 #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
29 static LIST_HEAD(acpi_device_list);
30 static LIST_HEAD(acpi_bus_id_list);
31 DEFINE_MUTEX(acpi_device_lock);
32 LIST_HEAD(acpi_wakeup_device_list);
34 struct acpi_device_bus_id{
36 unsigned int instance_no;
37 struct list_head node;
41 * Creates hid/cid(s) string needed for modalias and uevent
42 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
43 * char *modalias: "acpi:IBM0001:ACPI0001"
45 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
50 struct acpi_hardware_id *id;
52 len = snprintf(modalias, size, "acpi:");
55 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
56 count = snprintf(&modalias[len], size, "%s:", id->id);
57 if (count < 0 || count >= size)
68 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
69 struct acpi_device *acpi_dev = to_acpi_device(dev);
72 /* Device has no HID and no CID or string is >1024 */
73 len = create_modalias(acpi_dev, buf, 1024);
79 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
81 static void acpi_bus_hot_remove_device(void *context)
83 struct acpi_device *device;
84 acpi_handle handle = context;
85 struct acpi_object_list arg_list;
86 union acpi_object arg;
87 acpi_status status = AE_OK;
89 if (acpi_bus_get_device(handle, &device))
95 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
96 "Hot-removing device %s...\n", dev_name(&device->dev)));
98 if (acpi_bus_trim(device, 1)) {
99 printk(KERN_ERR PREFIX
100 "Removing device failed\n");
104 /* power off device */
105 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
106 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
107 printk(KERN_WARNING PREFIX
108 "Power-off device failed\n");
110 if (device->flags.lockable) {
112 arg_list.pointer = &arg;
113 arg.type = ACPI_TYPE_INTEGER;
114 arg.integer.value = 0;
115 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
119 arg_list.pointer = &arg;
120 arg.type = ACPI_TYPE_INTEGER;
121 arg.integer.value = 1;
126 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
127 if (ACPI_FAILURE(status))
128 printk(KERN_WARNING PREFIX
129 "Eject device failed\n");
135 acpi_eject_store(struct device *d, struct device_attribute *attr,
136 const char *buf, size_t count)
140 acpi_object_type type = 0;
141 struct acpi_device *acpi_device = to_acpi_device(d);
143 if ((!count) || (buf[0] != '1')) {
147 if (acpi_device->driver == NULL) {
152 status = acpi_get_type(acpi_device->handle, &type);
153 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
158 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, acpi_device->handle);
163 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
166 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
167 struct acpi_device *acpi_dev = to_acpi_device(dev);
169 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
171 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
173 #ifdef CONFIG_PCI_GUESTDEV
175 acpi_device_uid_show(struct device *dev, struct device_attribute *attr, char *buf) {
176 struct acpi_device *acpi_dev = to_acpi_device(dev);
178 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
180 static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
184 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
185 struct acpi_device *acpi_dev = to_acpi_device(dev);
186 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
189 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
193 result = sprintf(buf, "%s\n", (char*)path.pointer);
198 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
200 static int acpi_device_setup_files(struct acpi_device *dev)
207 * Devices gotten from FADT don't have a "path" attribute
210 result = device_create_file(&dev->dev, &dev_attr_path);
215 result = device_create_file(&dev->dev, &dev_attr_hid);
219 result = device_create_file(&dev->dev, &dev_attr_modalias);
223 #ifdef CONFIG_PCI_GUESTDEV
224 if(dev->pnp.unique_id) {
225 result = device_create_file(&dev->dev, &dev_attr_uid);
231 * If device has _EJ0, 'eject' file is created that is used to trigger
232 * hot-removal function from userland.
234 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
235 if (ACPI_SUCCESS(status))
236 result = device_create_file(&dev->dev, &dev_attr_eject);
241 static void acpi_device_remove_files(struct acpi_device *dev)
247 * If device has _EJ0, 'eject' file is created that is used to trigger
248 * hot-removal function from userland.
250 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
251 if (ACPI_SUCCESS(status))
252 device_remove_file(&dev->dev, &dev_attr_eject);
254 device_remove_file(&dev->dev, &dev_attr_modalias);
255 device_remove_file(&dev->dev, &dev_attr_hid);
257 device_remove_file(&dev->dev, &dev_attr_path);
259 /* --------------------------------------------------------------------------
261 -------------------------------------------------------------------------- */
263 int acpi_match_device_ids(struct acpi_device *device,
264 const struct acpi_device_id *ids)
266 const struct acpi_device_id *id;
267 struct acpi_hardware_id *hwid;
270 * If the device is not present, it is unnecessary to load device
273 if (!device->status.present)
276 for (id = ids; id->id[0]; id++)
277 list_for_each_entry(hwid, &device->pnp.ids, list)
278 if (!strcmp((char *) id->id, hwid->id))
283 EXPORT_SYMBOL(acpi_match_device_ids);
285 static void acpi_free_ids(struct acpi_device *device)
287 struct acpi_hardware_id *id, *tmp;
289 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
293 #ifdef CONFIG_PCI_GUESTDEV
294 kfree(device->pnp.unique_id);
298 static void acpi_device_release(struct device *dev)
300 struct acpi_device *acpi_dev = to_acpi_device(dev);
302 acpi_free_ids(acpi_dev);
306 static int acpi_device_suspend(struct device *dev, pm_message_t state)
308 struct acpi_device *acpi_dev = to_acpi_device(dev);
309 struct acpi_driver *acpi_drv = acpi_dev->driver;
311 if (acpi_drv && acpi_drv->ops.suspend)
312 return acpi_drv->ops.suspend(acpi_dev, state);
316 static int acpi_device_resume(struct device *dev)
318 struct acpi_device *acpi_dev = to_acpi_device(dev);
319 struct acpi_driver *acpi_drv = acpi_dev->driver;
321 if (acpi_drv && acpi_drv->ops.resume)
322 return acpi_drv->ops.resume(acpi_dev);
326 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
328 struct acpi_device *acpi_dev = to_acpi_device(dev);
329 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
331 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
334 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
336 struct acpi_device *acpi_dev = to_acpi_device(dev);
339 if (add_uevent_var(env, "MODALIAS="))
341 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
342 sizeof(env->buf) - env->buflen);
343 if (len >= (sizeof(env->buf) - env->buflen))
349 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
351 struct acpi_device *device = data;
353 device->driver->ops.notify(device, event);
356 static acpi_status acpi_device_notify_fixed(void *data)
358 struct acpi_device *device = data;
360 /* Fixed hardware devices have no handles */
361 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
365 static int acpi_device_install_notify_handler(struct acpi_device *device)
369 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
371 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
372 acpi_device_notify_fixed,
374 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
376 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
377 acpi_device_notify_fixed,
380 status = acpi_install_notify_handler(device->handle,
385 if (ACPI_FAILURE(status))
390 static void acpi_device_remove_notify_handler(struct acpi_device *device)
392 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
393 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
394 acpi_device_notify_fixed);
395 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
396 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
397 acpi_device_notify_fixed);
399 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
403 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
404 static int acpi_start_single_object(struct acpi_device *);
405 static int acpi_device_probe(struct device * dev)
407 struct acpi_device *acpi_dev = to_acpi_device(dev);
408 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
411 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
413 if (acpi_dev->bus_ops.acpi_op_start)
414 acpi_start_single_object(acpi_dev);
416 if (acpi_drv->ops.notify) {
417 ret = acpi_device_install_notify_handler(acpi_dev);
419 if (acpi_drv->ops.remove)
420 acpi_drv->ops.remove(acpi_dev,
421 acpi_dev->removal_type);
426 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
427 "Found driver [%s] for device [%s]\n",
428 acpi_drv->name, acpi_dev->pnp.bus_id));
434 static int acpi_device_remove(struct device * dev)
436 struct acpi_device *acpi_dev = to_acpi_device(dev);
437 struct acpi_driver *acpi_drv = acpi_dev->driver;
440 if (acpi_drv->ops.notify)
441 acpi_device_remove_notify_handler(acpi_dev);
442 if (acpi_drv->ops.remove)
443 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
445 acpi_dev->driver = NULL;
446 acpi_dev->driver_data = NULL;
452 struct bus_type acpi_bus_type = {
454 .suspend = acpi_device_suspend,
455 .resume = acpi_device_resume,
456 .match = acpi_bus_match,
457 .probe = acpi_device_probe,
458 .remove = acpi_device_remove,
459 .uevent = acpi_device_uevent,
462 static int acpi_device_register(struct acpi_device *device)
465 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
471 * Link this device to its parent and siblings.
473 INIT_LIST_HEAD(&device->children);
474 INIT_LIST_HEAD(&device->node);
475 INIT_LIST_HEAD(&device->wakeup_list);
477 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
479 printk(KERN_ERR PREFIX "Memory allocation error\n");
483 mutex_lock(&acpi_device_lock);
485 * Find suitable bus_id and instance number in acpi_bus_id_list
486 * If failed, create one and link it into acpi_bus_id_list
488 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
489 if (!strcmp(acpi_device_bus_id->bus_id,
490 acpi_device_hid(device))) {
491 acpi_device_bus_id->instance_no++;
498 acpi_device_bus_id = new_bus_id;
499 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
500 acpi_device_bus_id->instance_no = 0;
501 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
503 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
506 list_add_tail(&device->node, &device->parent->children);
508 if (device->wakeup.flags.valid)
509 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
510 mutex_unlock(&acpi_device_lock);
513 device->dev.parent = &device->parent->dev;
514 device->dev.bus = &acpi_bus_type;
515 device->dev.release = &acpi_device_release;
516 result = device_register(&device->dev);
518 dev_err(&device->dev, "Error registering device\n");
522 result = acpi_device_setup_files(device);
524 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
525 dev_name(&device->dev));
527 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
530 mutex_lock(&acpi_device_lock);
532 list_del(&device->node);
533 list_del(&device->wakeup_list);
534 mutex_unlock(&acpi_device_lock);
538 static void acpi_device_unregister(struct acpi_device *device, int type)
540 mutex_lock(&acpi_device_lock);
542 list_del(&device->node);
544 list_del(&device->wakeup_list);
545 mutex_unlock(&acpi_device_lock);
547 acpi_detach_data(device->handle, acpi_bus_data_handler);
549 acpi_device_remove_files(device);
550 device_unregister(&device->dev);
553 /* --------------------------------------------------------------------------
555 -------------------------------------------------------------------------- */
557 * acpi_bus_driver_init - add a device to a driver
558 * @device: the device to add and initialize
559 * @driver: driver for the device
561 * Used to initialize a device via its device driver. Called whenever a
562 * driver is bound to a device. Invokes the driver's add() ops.
565 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
569 if (!device || !driver)
572 if (!driver->ops.add)
575 result = driver->ops.add(device);
577 device->driver = NULL;
578 device->driver_data = NULL;
582 device->driver = driver;
585 * TBD - Configuration Management: Assign resources to device based
586 * upon possible configuration and currently allocated resources.
589 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
590 "Driver successfully bound to device\n"));
594 static int acpi_start_single_object(struct acpi_device *device)
597 struct acpi_driver *driver;
600 if (!(driver = device->driver))
603 if (driver->ops.start) {
604 result = driver->ops.start(device);
605 if (result && driver->ops.remove)
606 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
613 * acpi_bus_register_driver - register a driver with the ACPI bus
614 * @driver: driver being registered
616 * Registers a driver with the ACPI bus. Searches the namespace for all
617 * devices that match the driver's criteria and binds. Returns zero for
618 * success or a negative error status for failure.
620 int acpi_bus_register_driver(struct acpi_driver *driver)
626 driver->drv.name = driver->name;
627 driver->drv.bus = &acpi_bus_type;
628 driver->drv.owner = driver->owner;
630 ret = driver_register(&driver->drv);
634 EXPORT_SYMBOL(acpi_bus_register_driver);
637 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
638 * @driver: driver to unregister
640 * Unregisters a driver with the ACPI bus. Searches the namespace for all
641 * devices that match the driver's criteria and unbinds.
643 void acpi_bus_unregister_driver(struct acpi_driver *driver)
645 driver_unregister(&driver->drv);
648 EXPORT_SYMBOL(acpi_bus_unregister_driver);
650 /* --------------------------------------------------------------------------
652 -------------------------------------------------------------------------- */
653 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
657 struct acpi_device *device;
660 * Fixed hardware devices do not appear in the namespace and do not
661 * have handles, but we fabricate acpi_devices for them, so we have
662 * to deal with them specially.
668 status = acpi_get_parent(handle, &handle);
669 if (status == AE_NULL_ENTRY)
671 if (ACPI_FAILURE(status))
674 ret = acpi_bus_get_device(handle, &device);
681 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
685 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
686 union acpi_object *obj;
688 status = acpi_get_handle(handle, "_EJD", &tmp);
689 if (ACPI_FAILURE(status))
692 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
693 if (ACPI_SUCCESS(status)) {
694 obj = buffer.pointer;
695 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
697 kfree(buffer.pointer);
701 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
703 void acpi_bus_data_handler(acpi_handle handle, void *context)
711 static int acpi_bus_get_perf_flags(struct acpi_device *device)
713 device->performance.state = ACPI_STATE_UNKNOWN;
718 acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
719 union acpi_object *package)
722 union acpi_object *element = NULL;
724 if (!device || !package || (package->package.count < 2))
725 return AE_BAD_PARAMETER;
727 element = &(package->package.elements[0]);
729 return AE_BAD_PARAMETER;
730 if (element->type == ACPI_TYPE_PACKAGE) {
731 if ((element->package.count < 2) ||
732 (element->package.elements[0].type !=
733 ACPI_TYPE_LOCAL_REFERENCE)
734 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
736 device->wakeup.gpe_device =
737 element->package.elements[0].reference.handle;
738 device->wakeup.gpe_number =
739 (u32) element->package.elements[1].integer.value;
740 } else if (element->type == ACPI_TYPE_INTEGER) {
741 device->wakeup.gpe_number = element->integer.value;
745 element = &(package->package.elements[1]);
746 if (element->type != ACPI_TYPE_INTEGER) {
749 device->wakeup.sleep_state = element->integer.value;
751 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
754 device->wakeup.resources.count = package->package.count - 2;
755 for (i = 0; i < device->wakeup.resources.count; i++) {
756 element = &(package->package.elements[i + 2]);
757 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
760 device->wakeup.resources.handles[i] = element->reference.handle;
763 acpi_gpe_can_wake(device->wakeup.gpe_device, device->wakeup.gpe_number);
768 static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
770 struct acpi_device_id button_device_ids[] = {
777 acpi_event_status event_status;
779 device->wakeup.run_wake_count = 0;
780 device->wakeup.flags.notifier_present = 0;
782 /* Power button, Lid switch always enable wakeup */
783 if (!acpi_match_device_ids(device, button_device_ids)) {
784 device->wakeup.flags.run_wake = 1;
785 device->wakeup.flags.always_enabled = 1;
789 status = acpi_get_gpe_status(device->wakeup.gpe_device,
790 device->wakeup.gpe_number,
793 device->wakeup.flags.run_wake =
794 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
797 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
799 acpi_status status = 0;
800 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
801 union acpi_object *package = NULL;
805 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
806 if (ACPI_FAILURE(status)) {
807 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
811 package = (union acpi_object *)buffer.pointer;
812 status = acpi_bus_extract_wakeup_device_power_package(device, package);
813 if (ACPI_FAILURE(status)) {
814 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
818 kfree(buffer.pointer);
820 device->wakeup.flags.valid = 1;
821 device->wakeup.prepare_count = 0;
822 acpi_bus_set_run_wake_flags(device);
823 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
824 * system for the ACPI device with the _PRW object.
825 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
826 * So it is necessary to call _DSW object first. Only when it is not
827 * present will the _PSW object used.
829 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
831 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
832 "error in _DSW or _PSW evaluation\n"));
835 if (ACPI_FAILURE(status))
836 device->flags.wake_capable = 0;
840 static int acpi_bus_get_power_flags(struct acpi_device *device)
842 acpi_status status = 0;
843 acpi_handle handle = NULL;
848 * Power Management Flags
850 status = acpi_get_handle(device->handle, "_PSC", &handle);
851 if (ACPI_SUCCESS(status))
852 device->power.flags.explicit_get = 1;
853 status = acpi_get_handle(device->handle, "_IRC", &handle);
854 if (ACPI_SUCCESS(status))
855 device->power.flags.inrush_current = 1;
858 * Enumerate supported power management states
860 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
861 struct acpi_device_power_state *ps = &device->power.states[i];
862 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
864 /* Evaluate "_PRx" to se if power resources are referenced */
865 acpi_evaluate_reference(device->handle, object_name, NULL,
867 if (ps->resources.count) {
868 device->power.flags.power_resources = 1;
872 /* Evaluate "_PSx" to see if we can do explicit sets */
873 object_name[2] = 'S';
874 status = acpi_get_handle(device->handle, object_name, &handle);
875 if (ACPI_SUCCESS(status)) {
876 ps->flags.explicit_set = 1;
880 /* State is valid if we have some power control */
881 if (ps->resources.count || ps->flags.explicit_set)
884 ps->power = -1; /* Unknown - driver assigned */
885 ps->latency = -1; /* Unknown - driver assigned */
888 /* Set defaults for D0 and D3 states (always valid) */
889 device->power.states[ACPI_STATE_D0].flags.valid = 1;
890 device->power.states[ACPI_STATE_D0].power = 100;
891 device->power.states[ACPI_STATE_D3].flags.valid = 1;
892 device->power.states[ACPI_STATE_D3].power = 0;
894 /* TBD: System wake support and resource requirements. */
896 device->power.state = ACPI_STATE_UNKNOWN;
897 acpi_bus_get_power(device->handle, &(device->power.state));
902 static int acpi_bus_get_flags(struct acpi_device *device)
904 acpi_status status = AE_OK;
905 acpi_handle temp = NULL;
908 /* Presence of _STA indicates 'dynamic_status' */
909 status = acpi_get_handle(device->handle, "_STA", &temp);
910 if (ACPI_SUCCESS(status))
911 device->flags.dynamic_status = 1;
913 /* Presence of _RMV indicates 'removable' */
914 status = acpi_get_handle(device->handle, "_RMV", &temp);
915 if (ACPI_SUCCESS(status))
916 device->flags.removable = 1;
918 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
919 status = acpi_get_handle(device->handle, "_EJD", &temp);
920 if (ACPI_SUCCESS(status))
921 device->flags.ejectable = 1;
923 status = acpi_get_handle(device->handle, "_EJ0", &temp);
924 if (ACPI_SUCCESS(status))
925 device->flags.ejectable = 1;
928 /* Presence of _LCK indicates 'lockable' */
929 status = acpi_get_handle(device->handle, "_LCK", &temp);
930 if (ACPI_SUCCESS(status))
931 device->flags.lockable = 1;
933 /* Presence of _PS0|_PR0 indicates 'power manageable' */
934 status = acpi_get_handle(device->handle, "_PS0", &temp);
935 if (ACPI_FAILURE(status))
936 status = acpi_get_handle(device->handle, "_PR0", &temp);
937 if (ACPI_SUCCESS(status))
938 device->flags.power_manageable = 1;
940 /* Presence of _PRW indicates wake capable */
941 status = acpi_get_handle(device->handle, "_PRW", &temp);
942 if (ACPI_SUCCESS(status))
943 device->flags.wake_capable = 1;
945 /* TBD: Performance management */
950 static void acpi_device_get_busid(struct acpi_device *device)
952 char bus_id[5] = { '?', 0 };
953 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
959 * The device's Bus ID is simply the object name.
960 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
962 if (ACPI_IS_ROOT_DEVICE(device)) {
963 strcpy(device->pnp.bus_id, "ACPI");
967 switch (device->device_type) {
968 case ACPI_BUS_TYPE_POWER_BUTTON:
969 strcpy(device->pnp.bus_id, "PWRF");
971 case ACPI_BUS_TYPE_SLEEP_BUTTON:
972 strcpy(device->pnp.bus_id, "SLPF");
975 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
976 /* Clean up trailing underscores (if any) */
977 for (i = 3; i > 1; i--) {
978 if (bus_id[i] == '_')
983 strcpy(device->pnp.bus_id, bus_id);
989 * acpi_bay_match - see if a device is an ejectable driver bay
991 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
992 * then we can safely call it an ejectable drive bay
994 static int acpi_bay_match(struct acpi_device *device){
1000 handle = device->handle;
1002 status = acpi_get_handle(handle, "_EJ0", &tmp);
1003 if (ACPI_FAILURE(status))
1006 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1007 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1008 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1009 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1012 if (acpi_get_parent(handle, &phandle))
1015 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1016 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1017 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1018 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1025 * acpi_dock_match - see if a device has a _DCK method
1027 static int acpi_dock_match(struct acpi_device *device)
1030 return acpi_get_handle(device->handle, "_DCK", &tmp);
1033 char *acpi_device_hid(struct acpi_device *device)
1035 struct acpi_hardware_id *hid;
1037 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1040 EXPORT_SYMBOL(acpi_device_hid);
1042 static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1044 struct acpi_hardware_id *id;
1046 id = kmalloc(sizeof(*id), GFP_KERNEL);
1050 id->id = kmalloc(strlen(dev_id) + 1, GFP_KERNEL);
1056 strcpy(id->id, dev_id);
1057 list_add_tail(&id->list, &device->pnp.ids);
1061 * Old IBM workstations have a DSDT bug wherein the SMBus object
1062 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1063 * prefix. Work around this.
1065 static int acpi_ibm_smbus_match(struct acpi_device *device)
1067 acpi_handle h_dummy;
1068 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1071 if (!dmi_name_in_vendors("IBM"))
1074 /* Look for SMBS object */
1075 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1079 if (strcmp("SMBS", path.pointer)) {
1084 /* Does it have the necessary (but misnamed) methods? */
1086 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1087 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1088 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1091 kfree(path.pointer);
1095 static void acpi_device_set_id(struct acpi_device *device)
1098 struct acpi_device_info *info;
1099 struct acpica_device_id_list *cid_list;
1102 switch (device->device_type) {
1103 case ACPI_BUS_TYPE_DEVICE:
1104 if (ACPI_IS_ROOT_DEVICE(device)) {
1105 acpi_add_id(device, ACPI_SYSTEM_HID);
1109 status = acpi_get_object_info(device->handle, &info);
1110 if (ACPI_FAILURE(status)) {
1111 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1115 if (info->valid & ACPI_VALID_HID)
1116 acpi_add_id(device, info->hardware_id.string);
1117 if (info->valid & ACPI_VALID_CID) {
1118 cid_list = &info->compatible_id_list;
1119 for (i = 0; i < cid_list->count; i++)
1120 acpi_add_id(device, cid_list->ids[i].string);
1122 #ifdef CONFIG_PCI_GUESTDEV
1123 if (info->valid & ACPI_VALID_UID)
1124 device->pnp.unique_id = kstrdup(info->unique_id.string,
1127 if (info->valid & ACPI_VALID_ADR) {
1128 device->pnp.bus_address = info->address;
1129 device->flags.bus_address = 1;
1135 * Some devices don't reliably have _HIDs & _CIDs, so add
1136 * synthetic HIDs to make sure drivers can find them.
1138 if (acpi_is_video_device(device))
1139 acpi_add_id(device, ACPI_VIDEO_HID);
1140 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1141 acpi_add_id(device, ACPI_BAY_HID);
1142 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1143 acpi_add_id(device, ACPI_DOCK_HID);
1144 else if (!acpi_ibm_smbus_match(device))
1145 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
1146 else if (!acpi_device_hid(device) &&
1147 ACPI_IS_ROOT_DEVICE(device->parent)) {
1148 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1149 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1150 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1154 case ACPI_BUS_TYPE_POWER:
1155 acpi_add_id(device, ACPI_POWER_HID);
1157 case ACPI_BUS_TYPE_PROCESSOR:
1158 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
1160 case ACPI_BUS_TYPE_THERMAL:
1161 acpi_add_id(device, ACPI_THERMAL_HID);
1163 case ACPI_BUS_TYPE_POWER_BUTTON:
1164 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
1166 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1167 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
1172 * We build acpi_devices for some objects that don't have _HID or _CID,
1173 * e.g., PCI bridges and slots. Drivers can't bind to these objects,
1174 * but we do use them indirectly by traversing the acpi_device tree.
1175 * This generic ID isn't useful for driver binding, but it provides
1176 * the useful property that "every acpi_device has an ID."
1178 if (list_empty(&device->pnp.ids))
1179 acpi_add_id(device, "device");
1182 static int acpi_device_set_context(struct acpi_device *device)
1189 * Attach this 'struct acpi_device' to the ACPI object. This makes
1190 * resolutions from handle->device very efficient. Fixed hardware
1191 * devices have no handles, so we skip them.
1193 if (!device->handle)
1196 status = acpi_attach_data(device->handle,
1197 acpi_bus_data_handler, device);
1198 if (ACPI_SUCCESS(status))
1201 printk(KERN_ERR PREFIX "Error attaching device data\n");
1205 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1210 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1211 device_release_driver(&dev->dev);
1217 * unbind _ADR-Based Devices when hot removal
1219 if (dev->flags.bus_address) {
1220 if ((dev->parent) && (dev->parent->ops.unbind))
1221 dev->parent->ops.unbind(dev);
1223 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1228 static int acpi_add_single_object(struct acpi_device **child,
1229 acpi_handle handle, int type,
1230 unsigned long long sta,
1231 struct acpi_bus_ops *ops)
1234 struct acpi_device *device;
1235 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1237 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1239 printk(KERN_ERR PREFIX "Memory allocation error\n");
1243 INIT_LIST_HEAD(&device->pnp.ids);
1244 device->device_type = type;
1245 device->handle = handle;
1246 device->parent = acpi_bus_get_parent(handle);
1247 device->bus_ops = *ops; /* workround for not call .start */
1248 STRUCT_TO_INT(device->status) = sta;
1250 acpi_device_get_busid(device);
1255 * Note that we only look for object handles -- cannot evaluate objects
1256 * until we know the device is present and properly initialized.
1258 result = acpi_bus_get_flags(device);
1265 * TBD: Synch with Core's enumeration/initialization process.
1267 acpi_device_set_id(device);
1273 if (device->flags.power_manageable) {
1274 result = acpi_bus_get_power_flags(device);
1280 * Wakeup device management
1281 *-----------------------
1283 if (device->flags.wake_capable) {
1284 result = acpi_bus_get_wakeup_device_flags(device);
1290 * Performance Management
1291 * ----------------------
1293 if (device->flags.performance_manageable) {
1294 result = acpi_bus_get_perf_flags(device);
1299 if ((result = acpi_device_set_context(device)))
1302 result = acpi_device_register(device);
1305 * Bind _ADR-Based Devices when hot add
1307 if (device->flags.bus_address) {
1308 if (device->parent && device->parent->ops.bind)
1309 device->parent->ops.bind(device);
1314 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1315 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1316 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1317 (char *) buffer.pointer,
1318 device->parent ? dev_name(&device->parent->dev) :
1320 kfree(buffer.pointer);
1323 acpi_device_release(&device->dev);
1328 #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1329 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1331 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1332 unsigned long long *sta)
1335 acpi_object_type acpi_type;
1337 status = acpi_get_type(handle, &acpi_type);
1338 if (ACPI_FAILURE(status))
1341 switch (acpi_type) {
1342 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1343 case ACPI_TYPE_DEVICE:
1344 *type = ACPI_BUS_TYPE_DEVICE;
1345 status = acpi_bus_get_status_handle(handle, sta);
1346 if (ACPI_FAILURE(status))
1349 case ACPI_TYPE_PROCESSOR:
1350 *type = ACPI_BUS_TYPE_PROCESSOR;
1351 status = acpi_bus_get_status_handle(handle, sta);
1352 if (ACPI_FAILURE(status))
1355 case ACPI_TYPE_THERMAL:
1356 *type = ACPI_BUS_TYPE_THERMAL;
1357 *sta = ACPI_STA_DEFAULT;
1359 case ACPI_TYPE_POWER:
1360 *type = ACPI_BUS_TYPE_POWER;
1361 *sta = ACPI_STA_DEFAULT;
1370 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1371 void *context, void **return_value)
1373 struct acpi_bus_ops *ops = context;
1375 unsigned long long sta;
1376 struct acpi_device *device;
1380 result = acpi_bus_type_and_status(handle, &type, &sta);
1384 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1385 !(sta & ACPI_STA_DEVICE_FUNCTIONING))
1386 return AE_CTRL_DEPTH;
1389 * We may already have an acpi_device from a previous enumeration. If
1390 * so, we needn't add it again, but we may still have to start it.
1393 acpi_bus_get_device(handle, &device);
1394 if (ops->acpi_op_add && !device)
1395 acpi_add_single_object(&device, handle, type, sta, ops);
1398 return AE_CTRL_DEPTH;
1400 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1401 status = acpi_start_single_object(device);
1402 if (ACPI_FAILURE(status))
1403 return AE_CTRL_DEPTH;
1407 *return_value = device;
1411 static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1412 struct acpi_device **child)
1415 void *device = NULL;
1417 status = acpi_bus_check_add(handle, 0, ops, &device);
1418 if (ACPI_SUCCESS(status))
1419 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1420 acpi_bus_check_add, NULL, ops, &device);
1432 * acpi_bus_add and acpi_bus_start
1434 * scan a given ACPI tree and (probably recently hot-plugged)
1435 * create and add or starts found devices.
1437 * If no devices were found -ENODEV is returned which does not
1438 * mean that this is a real error, there just have been no suitable
1439 * ACPI objects in the table trunk from which the kernel could create
1440 * a device and add/start an appropriate driver.
1444 acpi_bus_add(struct acpi_device **child,
1445 struct acpi_device *parent, acpi_handle handle, int type)
1447 struct acpi_bus_ops ops;
1449 memset(&ops, 0, sizeof(ops));
1450 ops.acpi_op_add = 1;
1452 return acpi_bus_scan(handle, &ops, child);
1454 EXPORT_SYMBOL(acpi_bus_add);
1456 int acpi_bus_start(struct acpi_device *device)
1458 struct acpi_bus_ops ops;
1463 memset(&ops, 0, sizeof(ops));
1464 ops.acpi_op_start = 1;
1466 return acpi_bus_scan(device->handle, &ops, NULL);
1468 EXPORT_SYMBOL(acpi_bus_start);
1470 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1473 struct acpi_device *parent, *child;
1474 acpi_handle phandle, chandle;
1475 acpi_object_type type;
1480 phandle = start->handle;
1481 child = chandle = NULL;
1483 while ((level > 0) && parent && (!err)) {
1484 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1488 * If this scope is exhausted then move our way back up.
1490 if (ACPI_FAILURE(status)) {
1493 acpi_get_parent(phandle, &phandle);
1495 parent = parent->parent;
1498 err = acpi_bus_remove(child, rmdevice);
1500 err = acpi_bus_remove(child, 1);
1505 status = acpi_get_type(chandle, &type);
1506 if (ACPI_FAILURE(status)) {
1510 * If there is a device corresponding to chandle then
1511 * parse it (depth-first).
1513 if (acpi_bus_get_device(chandle, &child) == 0) {
1523 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1525 static int acpi_bus_scan_fixed(void)
1528 struct acpi_device *device = NULL;
1529 struct acpi_bus_ops ops;
1531 memset(&ops, 0, sizeof(ops));
1532 ops.acpi_op_add = 1;
1533 ops.acpi_op_start = 1;
1536 * Enumerate all fixed-feature devices.
1538 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
1539 result = acpi_add_single_object(&device, NULL,
1540 ACPI_BUS_TYPE_POWER_BUTTON,
1545 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1546 result = acpi_add_single_object(&device, NULL,
1547 ACPI_BUS_TYPE_SLEEP_BUTTON,
1555 int __init acpi_scan_init(void)
1558 struct acpi_bus_ops ops;
1560 memset(&ops, 0, sizeof(ops));
1561 ops.acpi_op_add = 1;
1562 ops.acpi_op_start = 1;
1564 result = bus_register(&acpi_bus_type);
1566 /* We don't want to quit even if we failed to add suspend/resume */
1567 printk(KERN_ERR PREFIX "Could not register bus type\n");
1571 * Enumerate devices in the ACPI namespace.
1573 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
1576 result = acpi_bus_scan_fixed();
1579 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);