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;
766 static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
768 struct acpi_device_id button_device_ids[] = {
775 acpi_event_status event_status;
777 device->wakeup.run_wake_count = 0;
778 device->wakeup.flags.notifier_present = 0;
780 /* Power button, Lid switch always enable wakeup */
781 if (!acpi_match_device_ids(device, button_device_ids)) {
782 device->wakeup.flags.run_wake = 1;
783 device->wakeup.flags.always_enabled = 1;
787 status = acpi_get_gpe_status(NULL, device->wakeup.gpe_number,
788 ACPI_NOT_ISR, &event_status);
790 device->wakeup.flags.run_wake =
791 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
794 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
796 acpi_status status = 0;
797 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
798 union acpi_object *package = NULL;
802 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
803 if (ACPI_FAILURE(status)) {
804 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
808 package = (union acpi_object *)buffer.pointer;
809 status = acpi_bus_extract_wakeup_device_power_package(device, package);
810 if (ACPI_FAILURE(status)) {
811 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
815 kfree(buffer.pointer);
817 device->wakeup.flags.valid = 1;
818 device->wakeup.prepare_count = 0;
819 acpi_bus_set_run_wake_flags(device);
820 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
821 * system for the ACPI device with the _PRW object.
822 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
823 * So it is necessary to call _DSW object first. Only when it is not
824 * present will the _PSW object used.
826 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
828 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
829 "error in _DSW or _PSW evaluation\n"));
832 if (ACPI_FAILURE(status))
833 device->flags.wake_capable = 0;
837 static int acpi_bus_get_power_flags(struct acpi_device *device)
839 acpi_status status = 0;
840 acpi_handle handle = NULL;
845 * Power Management Flags
847 status = acpi_get_handle(device->handle, "_PSC", &handle);
848 if (ACPI_SUCCESS(status))
849 device->power.flags.explicit_get = 1;
850 status = acpi_get_handle(device->handle, "_IRC", &handle);
851 if (ACPI_SUCCESS(status))
852 device->power.flags.inrush_current = 1;
855 * Enumerate supported power management states
857 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
858 struct acpi_device_power_state *ps = &device->power.states[i];
859 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
861 /* Evaluate "_PRx" to se if power resources are referenced */
862 acpi_evaluate_reference(device->handle, object_name, NULL,
864 if (ps->resources.count) {
865 device->power.flags.power_resources = 1;
869 /* Evaluate "_PSx" to see if we can do explicit sets */
870 object_name[2] = 'S';
871 status = acpi_get_handle(device->handle, object_name, &handle);
872 if (ACPI_SUCCESS(status)) {
873 ps->flags.explicit_set = 1;
877 /* State is valid if we have some power control */
878 if (ps->resources.count || ps->flags.explicit_set)
881 ps->power = -1; /* Unknown - driver assigned */
882 ps->latency = -1; /* Unknown - driver assigned */
885 /* Set defaults for D0 and D3 states (always valid) */
886 device->power.states[ACPI_STATE_D0].flags.valid = 1;
887 device->power.states[ACPI_STATE_D0].power = 100;
888 device->power.states[ACPI_STATE_D3].flags.valid = 1;
889 device->power.states[ACPI_STATE_D3].power = 0;
891 /* TBD: System wake support and resource requirements. */
893 device->power.state = ACPI_STATE_UNKNOWN;
894 acpi_bus_get_power(device->handle, &(device->power.state));
899 static int acpi_bus_get_flags(struct acpi_device *device)
901 acpi_status status = AE_OK;
902 acpi_handle temp = NULL;
905 /* Presence of _STA indicates 'dynamic_status' */
906 status = acpi_get_handle(device->handle, "_STA", &temp);
907 if (ACPI_SUCCESS(status))
908 device->flags.dynamic_status = 1;
910 /* Presence of _RMV indicates 'removable' */
911 status = acpi_get_handle(device->handle, "_RMV", &temp);
912 if (ACPI_SUCCESS(status))
913 device->flags.removable = 1;
915 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
916 status = acpi_get_handle(device->handle, "_EJD", &temp);
917 if (ACPI_SUCCESS(status))
918 device->flags.ejectable = 1;
920 status = acpi_get_handle(device->handle, "_EJ0", &temp);
921 if (ACPI_SUCCESS(status))
922 device->flags.ejectable = 1;
925 /* Presence of _LCK indicates 'lockable' */
926 status = acpi_get_handle(device->handle, "_LCK", &temp);
927 if (ACPI_SUCCESS(status))
928 device->flags.lockable = 1;
930 /* Presence of _PS0|_PR0 indicates 'power manageable' */
931 status = acpi_get_handle(device->handle, "_PS0", &temp);
932 if (ACPI_FAILURE(status))
933 status = acpi_get_handle(device->handle, "_PR0", &temp);
934 if (ACPI_SUCCESS(status))
935 device->flags.power_manageable = 1;
937 /* Presence of _PRW indicates wake capable */
938 status = acpi_get_handle(device->handle, "_PRW", &temp);
939 if (ACPI_SUCCESS(status))
940 device->flags.wake_capable = 1;
942 /* TBD: Performance management */
947 static void acpi_device_get_busid(struct acpi_device *device)
949 char bus_id[5] = { '?', 0 };
950 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
956 * The device's Bus ID is simply the object name.
957 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
959 if (ACPI_IS_ROOT_DEVICE(device)) {
960 strcpy(device->pnp.bus_id, "ACPI");
964 switch (device->device_type) {
965 case ACPI_BUS_TYPE_POWER_BUTTON:
966 strcpy(device->pnp.bus_id, "PWRF");
968 case ACPI_BUS_TYPE_SLEEP_BUTTON:
969 strcpy(device->pnp.bus_id, "SLPF");
972 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
973 /* Clean up trailing underscores (if any) */
974 for (i = 3; i > 1; i--) {
975 if (bus_id[i] == '_')
980 strcpy(device->pnp.bus_id, bus_id);
986 * acpi_bay_match - see if a device is an ejectable driver bay
988 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
989 * then we can safely call it an ejectable drive bay
991 static int acpi_bay_match(struct acpi_device *device){
997 handle = device->handle;
999 status = acpi_get_handle(handle, "_EJ0", &tmp);
1000 if (ACPI_FAILURE(status))
1003 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1004 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1005 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1006 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1009 if (acpi_get_parent(handle, &phandle))
1012 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1013 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1014 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1015 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1022 * acpi_dock_match - see if a device has a _DCK method
1024 static int acpi_dock_match(struct acpi_device *device)
1027 return acpi_get_handle(device->handle, "_DCK", &tmp);
1030 char *acpi_device_hid(struct acpi_device *device)
1032 struct acpi_hardware_id *hid;
1034 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1037 EXPORT_SYMBOL(acpi_device_hid);
1039 static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1041 struct acpi_hardware_id *id;
1043 id = kmalloc(sizeof(*id), GFP_KERNEL);
1047 id->id = kmalloc(strlen(dev_id) + 1, GFP_KERNEL);
1053 strcpy(id->id, dev_id);
1054 list_add_tail(&id->list, &device->pnp.ids);
1058 * Old IBM workstations have a DSDT bug wherein the SMBus object
1059 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1060 * prefix. Work around this.
1062 static int acpi_ibm_smbus_match(struct acpi_device *device)
1064 acpi_handle h_dummy;
1065 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1068 if (!dmi_name_in_vendors("IBM"))
1071 /* Look for SMBS object */
1072 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1076 if (strcmp("SMBS", path.pointer)) {
1081 /* Does it have the necessary (but misnamed) methods? */
1083 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1084 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1085 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1088 kfree(path.pointer);
1092 static void acpi_device_set_id(struct acpi_device *device)
1095 struct acpi_device_info *info;
1096 struct acpica_device_id_list *cid_list;
1099 switch (device->device_type) {
1100 case ACPI_BUS_TYPE_DEVICE:
1101 if (ACPI_IS_ROOT_DEVICE(device)) {
1102 acpi_add_id(device, ACPI_SYSTEM_HID);
1106 status = acpi_get_object_info(device->handle, &info);
1107 if (ACPI_FAILURE(status)) {
1108 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1112 if (info->valid & ACPI_VALID_HID)
1113 acpi_add_id(device, info->hardware_id.string);
1114 if (info->valid & ACPI_VALID_CID) {
1115 cid_list = &info->compatible_id_list;
1116 for (i = 0; i < cid_list->count; i++)
1117 acpi_add_id(device, cid_list->ids[i].string);
1119 #ifdef CONFIG_PCI_GUESTDEV
1120 if (info->valid & ACPI_VALID_UID)
1121 device->pnp.unique_id = kstrdup(info->unique_id.string,
1124 if (info->valid & ACPI_VALID_ADR) {
1125 device->pnp.bus_address = info->address;
1126 device->flags.bus_address = 1;
1132 * Some devices don't reliably have _HIDs & _CIDs, so add
1133 * synthetic HIDs to make sure drivers can find them.
1135 if (acpi_is_video_device(device))
1136 acpi_add_id(device, ACPI_VIDEO_HID);
1137 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1138 acpi_add_id(device, ACPI_BAY_HID);
1139 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1140 acpi_add_id(device, ACPI_DOCK_HID);
1141 else if (!acpi_ibm_smbus_match(device))
1142 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
1143 else if (!acpi_device_hid(device) &&
1144 ACPI_IS_ROOT_DEVICE(device->parent)) {
1145 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1146 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1147 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1151 case ACPI_BUS_TYPE_POWER:
1152 acpi_add_id(device, ACPI_POWER_HID);
1154 case ACPI_BUS_TYPE_PROCESSOR:
1155 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
1157 case ACPI_BUS_TYPE_THERMAL:
1158 acpi_add_id(device, ACPI_THERMAL_HID);
1160 case ACPI_BUS_TYPE_POWER_BUTTON:
1161 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
1163 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1164 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
1169 * We build acpi_devices for some objects that don't have _HID or _CID,
1170 * e.g., PCI bridges and slots. Drivers can't bind to these objects,
1171 * but we do use them indirectly by traversing the acpi_device tree.
1172 * This generic ID isn't useful for driver binding, but it provides
1173 * the useful property that "every acpi_device has an ID."
1175 if (list_empty(&device->pnp.ids))
1176 acpi_add_id(device, "device");
1179 static int acpi_device_set_context(struct acpi_device *device)
1186 * Attach this 'struct acpi_device' to the ACPI object. This makes
1187 * resolutions from handle->device very efficient. Fixed hardware
1188 * devices have no handles, so we skip them.
1190 if (!device->handle)
1193 status = acpi_attach_data(device->handle,
1194 acpi_bus_data_handler, device);
1195 if (ACPI_SUCCESS(status))
1198 printk(KERN_ERR PREFIX "Error attaching device data\n");
1202 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1207 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1208 device_release_driver(&dev->dev);
1214 * unbind _ADR-Based Devices when hot removal
1216 if (dev->flags.bus_address) {
1217 if ((dev->parent) && (dev->parent->ops.unbind))
1218 dev->parent->ops.unbind(dev);
1220 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1225 static int acpi_add_single_object(struct acpi_device **child,
1226 acpi_handle handle, int type,
1227 unsigned long long sta,
1228 struct acpi_bus_ops *ops)
1231 struct acpi_device *device;
1232 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1234 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1236 printk(KERN_ERR PREFIX "Memory allocation error\n");
1240 INIT_LIST_HEAD(&device->pnp.ids);
1241 device->device_type = type;
1242 device->handle = handle;
1243 device->parent = acpi_bus_get_parent(handle);
1244 device->bus_ops = *ops; /* workround for not call .start */
1245 STRUCT_TO_INT(device->status) = sta;
1247 acpi_device_get_busid(device);
1252 * Note that we only look for object handles -- cannot evaluate objects
1253 * until we know the device is present and properly initialized.
1255 result = acpi_bus_get_flags(device);
1262 * TBD: Synch with Core's enumeration/initialization process.
1264 acpi_device_set_id(device);
1270 if (device->flags.power_manageable) {
1271 result = acpi_bus_get_power_flags(device);
1277 * Wakeup device management
1278 *-----------------------
1280 if (device->flags.wake_capable) {
1281 result = acpi_bus_get_wakeup_device_flags(device);
1287 * Performance Management
1288 * ----------------------
1290 if (device->flags.performance_manageable) {
1291 result = acpi_bus_get_perf_flags(device);
1296 if ((result = acpi_device_set_context(device)))
1299 result = acpi_device_register(device);
1302 * Bind _ADR-Based Devices when hot add
1304 if (device->flags.bus_address) {
1305 if (device->parent && device->parent->ops.bind)
1306 device->parent->ops.bind(device);
1311 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1312 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1313 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1314 (char *) buffer.pointer,
1315 device->parent ? dev_name(&device->parent->dev) :
1317 kfree(buffer.pointer);
1320 acpi_device_release(&device->dev);
1325 #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1326 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1328 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1329 unsigned long long *sta)
1332 acpi_object_type acpi_type;
1334 status = acpi_get_type(handle, &acpi_type);
1335 if (ACPI_FAILURE(status))
1338 switch (acpi_type) {
1339 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1340 case ACPI_TYPE_DEVICE:
1341 *type = ACPI_BUS_TYPE_DEVICE;
1342 status = acpi_bus_get_status_handle(handle, sta);
1343 if (ACPI_FAILURE(status))
1346 case ACPI_TYPE_PROCESSOR:
1347 *type = ACPI_BUS_TYPE_PROCESSOR;
1348 status = acpi_bus_get_status_handle(handle, sta);
1349 if (ACPI_FAILURE(status))
1352 case ACPI_TYPE_THERMAL:
1353 *type = ACPI_BUS_TYPE_THERMAL;
1354 *sta = ACPI_STA_DEFAULT;
1356 case ACPI_TYPE_POWER:
1357 *type = ACPI_BUS_TYPE_POWER;
1358 *sta = ACPI_STA_DEFAULT;
1367 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1368 void *context, void **return_value)
1370 struct acpi_bus_ops *ops = context;
1372 unsigned long long sta;
1373 struct acpi_device *device;
1377 result = acpi_bus_type_and_status(handle, &type, &sta);
1381 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1382 !(sta & ACPI_STA_DEVICE_FUNCTIONING))
1383 return AE_CTRL_DEPTH;
1386 * We may already have an acpi_device from a previous enumeration. If
1387 * so, we needn't add it again, but we may still have to start it.
1390 acpi_bus_get_device(handle, &device);
1391 if (ops->acpi_op_add && !device)
1392 acpi_add_single_object(&device, handle, type, sta, ops);
1395 return AE_CTRL_DEPTH;
1397 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1398 status = acpi_start_single_object(device);
1399 if (ACPI_FAILURE(status))
1400 return AE_CTRL_DEPTH;
1404 *return_value = device;
1408 static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1409 struct acpi_device **child)
1412 void *device = NULL;
1414 status = acpi_bus_check_add(handle, 0, ops, &device);
1415 if (ACPI_SUCCESS(status))
1416 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1417 acpi_bus_check_add, NULL, ops, &device);
1429 * acpi_bus_add and acpi_bus_start
1431 * scan a given ACPI tree and (probably recently hot-plugged)
1432 * create and add or starts found devices.
1434 * If no devices were found -ENODEV is returned which does not
1435 * mean that this is a real error, there just have been no suitable
1436 * ACPI objects in the table trunk from which the kernel could create
1437 * a device and add/start an appropriate driver.
1441 acpi_bus_add(struct acpi_device **child,
1442 struct acpi_device *parent, acpi_handle handle, int type)
1444 struct acpi_bus_ops ops;
1446 memset(&ops, 0, sizeof(ops));
1447 ops.acpi_op_add = 1;
1449 return acpi_bus_scan(handle, &ops, child);
1451 EXPORT_SYMBOL(acpi_bus_add);
1453 int acpi_bus_start(struct acpi_device *device)
1455 struct acpi_bus_ops ops;
1460 memset(&ops, 0, sizeof(ops));
1461 ops.acpi_op_start = 1;
1463 return acpi_bus_scan(device->handle, &ops, NULL);
1465 EXPORT_SYMBOL(acpi_bus_start);
1467 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1470 struct acpi_device *parent, *child;
1471 acpi_handle phandle, chandle;
1472 acpi_object_type type;
1477 phandle = start->handle;
1478 child = chandle = NULL;
1480 while ((level > 0) && parent && (!err)) {
1481 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1485 * If this scope is exhausted then move our way back up.
1487 if (ACPI_FAILURE(status)) {
1490 acpi_get_parent(phandle, &phandle);
1492 parent = parent->parent;
1495 err = acpi_bus_remove(child, rmdevice);
1497 err = acpi_bus_remove(child, 1);
1502 status = acpi_get_type(chandle, &type);
1503 if (ACPI_FAILURE(status)) {
1507 * If there is a device corresponding to chandle then
1508 * parse it (depth-first).
1510 if (acpi_bus_get_device(chandle, &child) == 0) {
1520 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1522 static int acpi_bus_scan_fixed(void)
1525 struct acpi_device *device = NULL;
1526 struct acpi_bus_ops ops;
1528 memset(&ops, 0, sizeof(ops));
1529 ops.acpi_op_add = 1;
1530 ops.acpi_op_start = 1;
1533 * Enumerate all fixed-feature devices.
1535 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
1536 result = acpi_add_single_object(&device, NULL,
1537 ACPI_BUS_TYPE_POWER_BUTTON,
1542 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1543 result = acpi_add_single_object(&device, NULL,
1544 ACPI_BUS_TYPE_SLEEP_BUTTON,
1552 int __init acpi_scan_init(void)
1555 struct acpi_bus_ops ops;
1557 memset(&ops, 0, sizeof(ops));
1558 ops.acpi_op_add = 1;
1559 ops.acpi_op_start = 1;
1561 result = bus_register(&acpi_bus_type);
1563 /* We don't want to quit even if we failed to add suspend/resume */
1564 printk(KERN_ERR PREFIX "Could not register bus type\n");
1568 * Enumerate devices in the ACPI namespace.
1570 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
1573 result = acpi_bus_scan_fixed();
1576 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);