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>
9 #include <linux/signal.h>
10 #include <linux/kthread.h>
11 #include <linux/dmi.h>
13 #include <acpi/acpi_drivers.h>
17 #define _COMPONENT ACPI_BUS_COMPONENT
18 ACPI_MODULE_NAME("scan");
19 #define STRUCT_TO_INT(s) (*((int*)&s))
20 extern struct acpi_device *acpi_root;
22 #define ACPI_BUS_CLASS "system_bus"
23 #define ACPI_BUS_HID "LNXSYBUS"
24 #define ACPI_BUS_DEVICE_NAME "System Bus"
26 #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
28 static LIST_HEAD(acpi_device_list);
29 static LIST_HEAD(acpi_bus_id_list);
30 DEFINE_MUTEX(acpi_device_lock);
31 LIST_HEAD(acpi_wakeup_device_list);
33 struct acpi_device_bus_id{
35 unsigned int instance_no;
36 struct list_head node;
40 * Creates hid/cid(s) string needed for modalias and uevent
41 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
42 * char *modalias: "acpi:IBM0001:ACPI0001"
44 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
49 struct acpi_hardware_id *id;
51 len = snprintf(modalias, size, "acpi:");
54 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
55 count = snprintf(&modalias[len], size, "%s:", id->id);
56 if (count < 0 || count >= size)
67 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
68 struct acpi_device *acpi_dev = to_acpi_device(dev);
71 /* Device has no HID and no CID or string is >1024 */
72 len = create_modalias(acpi_dev, buf, 1024);
78 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
80 static void acpi_bus_hot_remove_device(void *context)
82 struct acpi_device *device;
83 acpi_handle handle = context;
84 struct acpi_object_list arg_list;
85 union acpi_object arg;
86 acpi_status status = AE_OK;
88 if (acpi_bus_get_device(handle, &device))
94 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
95 "Hot-removing device %s...\n", dev_name(&device->dev)));
97 if (acpi_bus_trim(device, 1)) {
98 printk(KERN_ERR PREFIX
99 "Removing device failed\n");
103 /* power off device */
104 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
105 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
106 printk(KERN_WARNING PREFIX
107 "Power-off device failed\n");
109 if (device->flags.lockable) {
111 arg_list.pointer = &arg;
112 arg.type = ACPI_TYPE_INTEGER;
113 arg.integer.value = 0;
114 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
118 arg_list.pointer = &arg;
119 arg.type = ACPI_TYPE_INTEGER;
120 arg.integer.value = 1;
125 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
126 if (ACPI_FAILURE(status))
127 printk(KERN_WARNING PREFIX
128 "Eject device failed\n");
134 acpi_eject_store(struct device *d, struct device_attribute *attr,
135 const char *buf, size_t count)
139 acpi_object_type type = 0;
140 struct acpi_device *acpi_device = to_acpi_device(d);
142 if ((!count) || (buf[0] != '1')) {
146 if (acpi_device->driver == NULL) {
151 status = acpi_get_type(acpi_device->handle, &type);
152 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
157 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, acpi_device->handle);
162 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
165 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
166 struct acpi_device *acpi_dev = to_acpi_device(dev);
168 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
170 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
172 #ifdef CONFIG_PCI_GUESTDEV
174 acpi_device_uid_show(struct device *dev, struct device_attribute *attr, char *buf) {
175 struct acpi_device *acpi_dev = to_acpi_device(dev);
177 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
179 static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
183 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
184 struct acpi_device *acpi_dev = to_acpi_device(dev);
185 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
188 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
192 result = sprintf(buf, "%s\n", (char*)path.pointer);
197 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
199 static int acpi_device_setup_files(struct acpi_device *dev)
206 * Devices gotten from FADT don't have a "path" attribute
209 result = device_create_file(&dev->dev, &dev_attr_path);
214 result = device_create_file(&dev->dev, &dev_attr_hid);
218 result = device_create_file(&dev->dev, &dev_attr_modalias);
222 #ifdef CONFIG_PCI_GUESTDEV
223 if(dev->pnp.unique_id) {
224 result = device_create_file(&dev->dev, &dev_attr_uid);
230 * If device has _EJ0, 'eject' file is created that is used to trigger
231 * hot-removal function from userland.
233 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
234 if (ACPI_SUCCESS(status))
235 result = device_create_file(&dev->dev, &dev_attr_eject);
240 static void acpi_device_remove_files(struct acpi_device *dev)
246 * If device has _EJ0, 'eject' file is created that is used to trigger
247 * hot-removal function from userland.
249 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
250 if (ACPI_SUCCESS(status))
251 device_remove_file(&dev->dev, &dev_attr_eject);
253 device_remove_file(&dev->dev, &dev_attr_modalias);
254 device_remove_file(&dev->dev, &dev_attr_hid);
256 device_remove_file(&dev->dev, &dev_attr_path);
258 /* --------------------------------------------------------------------------
260 -------------------------------------------------------------------------- */
262 int acpi_match_device_ids(struct acpi_device *device,
263 const struct acpi_device_id *ids)
265 const struct acpi_device_id *id;
266 struct acpi_hardware_id *hwid;
269 * If the device is not present, it is unnecessary to load device
272 if (!device->status.present)
275 for (id = ids; id->id[0]; id++)
276 list_for_each_entry(hwid, &device->pnp.ids, list)
277 if (!strcmp((char *) id->id, hwid->id))
282 EXPORT_SYMBOL(acpi_match_device_ids);
284 static void acpi_free_ids(struct acpi_device *device)
286 struct acpi_hardware_id *id, *tmp;
288 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
292 #ifdef CONFIG_PCI_GUESTDEV
293 kfree(device->pnp.unique_id);
297 static void acpi_device_release(struct device *dev)
299 struct acpi_device *acpi_dev = to_acpi_device(dev);
301 acpi_free_ids(acpi_dev);
305 static int acpi_device_suspend(struct device *dev, pm_message_t state)
307 struct acpi_device *acpi_dev = to_acpi_device(dev);
308 struct acpi_driver *acpi_drv = acpi_dev->driver;
310 if (acpi_drv && acpi_drv->ops.suspend)
311 return acpi_drv->ops.suspend(acpi_dev, state);
315 static int acpi_device_resume(struct device *dev)
317 struct acpi_device *acpi_dev = to_acpi_device(dev);
318 struct acpi_driver *acpi_drv = acpi_dev->driver;
320 if (acpi_drv && acpi_drv->ops.resume)
321 return acpi_drv->ops.resume(acpi_dev);
325 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
327 struct acpi_device *acpi_dev = to_acpi_device(dev);
328 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
330 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
333 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
335 struct acpi_device *acpi_dev = to_acpi_device(dev);
338 if (add_uevent_var(env, "MODALIAS="))
340 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
341 sizeof(env->buf) - env->buflen);
342 if (len >= (sizeof(env->buf) - env->buflen))
348 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
350 struct acpi_device *device = data;
352 device->driver->ops.notify(device, event);
355 static acpi_status acpi_device_notify_fixed(void *data)
357 struct acpi_device *device = data;
359 /* Fixed hardware devices have no handles */
360 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
364 static int acpi_device_install_notify_handler(struct acpi_device *device)
368 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
370 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
371 acpi_device_notify_fixed,
373 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
375 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
376 acpi_device_notify_fixed,
379 status = acpi_install_notify_handler(device->handle,
384 if (ACPI_FAILURE(status))
389 static void acpi_device_remove_notify_handler(struct acpi_device *device)
391 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
392 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
393 acpi_device_notify_fixed);
394 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
395 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
396 acpi_device_notify_fixed);
398 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
402 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
403 static int acpi_start_single_object(struct acpi_device *);
404 static int acpi_device_probe(struct device * dev)
406 struct acpi_device *acpi_dev = to_acpi_device(dev);
407 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
410 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
412 if (acpi_dev->bus_ops.acpi_op_start)
413 acpi_start_single_object(acpi_dev);
415 if (acpi_drv->ops.notify) {
416 ret = acpi_device_install_notify_handler(acpi_dev);
418 if (acpi_drv->ops.remove)
419 acpi_drv->ops.remove(acpi_dev,
420 acpi_dev->removal_type);
425 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
426 "Found driver [%s] for device [%s]\n",
427 acpi_drv->name, acpi_dev->pnp.bus_id));
433 static int acpi_device_remove(struct device * dev)
435 struct acpi_device *acpi_dev = to_acpi_device(dev);
436 struct acpi_driver *acpi_drv = acpi_dev->driver;
439 if (acpi_drv->ops.notify)
440 acpi_device_remove_notify_handler(acpi_dev);
441 if (acpi_drv->ops.remove)
442 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
444 acpi_dev->driver = NULL;
445 acpi_dev->driver_data = NULL;
451 struct bus_type acpi_bus_type = {
453 .suspend = acpi_device_suspend,
454 .resume = acpi_device_resume,
455 .match = acpi_bus_match,
456 .probe = acpi_device_probe,
457 .remove = acpi_device_remove,
458 .uevent = acpi_device_uevent,
461 static int acpi_device_register(struct acpi_device *device)
464 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
470 * Link this device to its parent and siblings.
472 INIT_LIST_HEAD(&device->children);
473 INIT_LIST_HEAD(&device->node);
474 INIT_LIST_HEAD(&device->wakeup_list);
476 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
478 printk(KERN_ERR PREFIX "Memory allocation error\n");
482 mutex_lock(&acpi_device_lock);
484 * Find suitable bus_id and instance number in acpi_bus_id_list
485 * If failed, create one and link it into acpi_bus_id_list
487 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
488 if (!strcmp(acpi_device_bus_id->bus_id,
489 acpi_device_hid(device))) {
490 acpi_device_bus_id->instance_no++;
497 acpi_device_bus_id = new_bus_id;
498 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
499 acpi_device_bus_id->instance_no = 0;
500 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
502 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
505 list_add_tail(&device->node, &device->parent->children);
507 if (device->wakeup.flags.valid)
508 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
509 mutex_unlock(&acpi_device_lock);
512 device->dev.parent = &device->parent->dev;
513 device->dev.bus = &acpi_bus_type;
514 device->dev.release = &acpi_device_release;
515 result = device_register(&device->dev);
517 dev_err(&device->dev, "Error registering device\n");
521 result = acpi_device_setup_files(device);
523 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
524 dev_name(&device->dev));
526 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
529 mutex_lock(&acpi_device_lock);
531 list_del(&device->node);
532 list_del(&device->wakeup_list);
533 mutex_unlock(&acpi_device_lock);
537 static void acpi_device_unregister(struct acpi_device *device, int type)
539 mutex_lock(&acpi_device_lock);
541 list_del(&device->node);
543 list_del(&device->wakeup_list);
544 mutex_unlock(&acpi_device_lock);
546 acpi_detach_data(device->handle, acpi_bus_data_handler);
548 acpi_device_remove_files(device);
549 device_unregister(&device->dev);
552 /* --------------------------------------------------------------------------
554 -------------------------------------------------------------------------- */
556 * acpi_bus_driver_init - add a device to a driver
557 * @device: the device to add and initialize
558 * @driver: driver for the device
560 * Used to initialize a device via its device driver. Called whenever a
561 * driver is bound to a device. Invokes the driver's add() ops.
564 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
568 if (!device || !driver)
571 if (!driver->ops.add)
574 result = driver->ops.add(device);
576 device->driver = NULL;
577 device->driver_data = NULL;
581 device->driver = driver;
584 * TBD - Configuration Management: Assign resources to device based
585 * upon possible configuration and currently allocated resources.
588 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
589 "Driver successfully bound to device\n"));
593 static int acpi_start_single_object(struct acpi_device *device)
596 struct acpi_driver *driver;
599 if (!(driver = device->driver))
602 if (driver->ops.start) {
603 result = driver->ops.start(device);
604 if (result && driver->ops.remove)
605 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
612 * acpi_bus_register_driver - register a driver with the ACPI bus
613 * @driver: driver being registered
615 * Registers a driver with the ACPI bus. Searches the namespace for all
616 * devices that match the driver's criteria and binds. Returns zero for
617 * success or a negative error status for failure.
619 int acpi_bus_register_driver(struct acpi_driver *driver)
625 driver->drv.name = driver->name;
626 driver->drv.bus = &acpi_bus_type;
627 driver->drv.owner = driver->owner;
629 ret = driver_register(&driver->drv);
633 EXPORT_SYMBOL(acpi_bus_register_driver);
636 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
637 * @driver: driver to unregister
639 * Unregisters a driver with the ACPI bus. Searches the namespace for all
640 * devices that match the driver's criteria and unbinds.
642 void acpi_bus_unregister_driver(struct acpi_driver *driver)
644 driver_unregister(&driver->drv);
647 EXPORT_SYMBOL(acpi_bus_unregister_driver);
649 /* --------------------------------------------------------------------------
651 -------------------------------------------------------------------------- */
652 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
656 struct acpi_device *device;
659 * Fixed hardware devices do not appear in the namespace and do not
660 * have handles, but we fabricate acpi_devices for them, so we have
661 * to deal with them specially.
667 status = acpi_get_parent(handle, &handle);
668 if (status == AE_NULL_ENTRY)
670 if (ACPI_FAILURE(status))
673 ret = acpi_bus_get_device(handle, &device);
680 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
684 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
685 union acpi_object *obj;
687 status = acpi_get_handle(handle, "_EJD", &tmp);
688 if (ACPI_FAILURE(status))
691 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
692 if (ACPI_SUCCESS(status)) {
693 obj = buffer.pointer;
694 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
696 kfree(buffer.pointer);
700 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
702 void acpi_bus_data_handler(acpi_handle handle, void *context)
710 static int acpi_bus_get_perf_flags(struct acpi_device *device)
712 device->performance.state = ACPI_STATE_UNKNOWN;
717 acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
718 union acpi_object *package)
721 union acpi_object *element = NULL;
723 if (!device || !package || (package->package.count < 2))
724 return AE_BAD_PARAMETER;
726 element = &(package->package.elements[0]);
728 return AE_BAD_PARAMETER;
729 if (element->type == ACPI_TYPE_PACKAGE) {
730 if ((element->package.count < 2) ||
731 (element->package.elements[0].type !=
732 ACPI_TYPE_LOCAL_REFERENCE)
733 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
735 device->wakeup.gpe_device =
736 element->package.elements[0].reference.handle;
737 device->wakeup.gpe_number =
738 (u32) element->package.elements[1].integer.value;
739 } else if (element->type == ACPI_TYPE_INTEGER) {
740 device->wakeup.gpe_number = element->integer.value;
744 element = &(package->package.elements[1]);
745 if (element->type != ACPI_TYPE_INTEGER) {
748 device->wakeup.sleep_state = element->integer.value;
750 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
753 device->wakeup.resources.count = package->package.count - 2;
754 for (i = 0; i < device->wakeup.resources.count; i++) {
755 element = &(package->package.elements[i + 2]);
756 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
759 device->wakeup.resources.handles[i] = element->reference.handle;
765 static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
767 struct acpi_device_id button_device_ids[] = {
774 acpi_event_status event_status;
776 device->wakeup.run_wake_count = 0;
777 device->wakeup.flags.notifier_present = 0;
779 /* Power button, Lid switch always enable wakeup */
780 if (!acpi_match_device_ids(device, button_device_ids)) {
781 device->wakeup.flags.run_wake = 1;
782 device->wakeup.flags.always_enabled = 1;
786 status = acpi_get_gpe_status(NULL, device->wakeup.gpe_number,
787 ACPI_NOT_ISR, &event_status);
789 device->wakeup.flags.run_wake =
790 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
793 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
795 acpi_status status = 0;
796 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
797 union acpi_object *package = NULL;
801 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
802 if (ACPI_FAILURE(status)) {
803 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
807 package = (union acpi_object *)buffer.pointer;
808 status = acpi_bus_extract_wakeup_device_power_package(device, package);
809 if (ACPI_FAILURE(status)) {
810 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
814 kfree(buffer.pointer);
816 device->wakeup.flags.valid = 1;
817 device->wakeup.prepare_count = 0;
818 acpi_bus_set_run_wake_flags(device);
819 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
820 * system for the ACPI device with the _PRW object.
821 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
822 * So it is necessary to call _DSW object first. Only when it is not
823 * present will the _PSW object used.
825 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
827 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
828 "error in _DSW or _PSW evaluation\n"));
831 if (ACPI_FAILURE(status))
832 device->flags.wake_capable = 0;
836 static int acpi_bus_get_power_flags(struct acpi_device *device)
838 acpi_status status = 0;
839 acpi_handle handle = NULL;
844 * Power Management Flags
846 status = acpi_get_handle(device->handle, "_PSC", &handle);
847 if (ACPI_SUCCESS(status))
848 device->power.flags.explicit_get = 1;
849 status = acpi_get_handle(device->handle, "_IRC", &handle);
850 if (ACPI_SUCCESS(status))
851 device->power.flags.inrush_current = 1;
854 * Enumerate supported power management states
856 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
857 struct acpi_device_power_state *ps = &device->power.states[i];
858 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
860 /* Evaluate "_PRx" to se if power resources are referenced */
861 acpi_evaluate_reference(device->handle, object_name, NULL,
863 if (ps->resources.count) {
864 device->power.flags.power_resources = 1;
868 /* Evaluate "_PSx" to see if we can do explicit sets */
869 object_name[2] = 'S';
870 status = acpi_get_handle(device->handle, object_name, &handle);
871 if (ACPI_SUCCESS(status)) {
872 ps->flags.explicit_set = 1;
876 /* State is valid if we have some power control */
877 if (ps->resources.count || ps->flags.explicit_set)
880 ps->power = -1; /* Unknown - driver assigned */
881 ps->latency = -1; /* Unknown - driver assigned */
884 /* Set defaults for D0 and D3 states (always valid) */
885 device->power.states[ACPI_STATE_D0].flags.valid = 1;
886 device->power.states[ACPI_STATE_D0].power = 100;
887 device->power.states[ACPI_STATE_D3].flags.valid = 1;
888 device->power.states[ACPI_STATE_D3].power = 0;
890 /* TBD: System wake support and resource requirements. */
892 device->power.state = ACPI_STATE_UNKNOWN;
893 acpi_bus_get_power(device->handle, &(device->power.state));
898 static int acpi_bus_get_flags(struct acpi_device *device)
900 acpi_status status = AE_OK;
901 acpi_handle temp = NULL;
904 /* Presence of _STA indicates 'dynamic_status' */
905 status = acpi_get_handle(device->handle, "_STA", &temp);
906 if (ACPI_SUCCESS(status))
907 device->flags.dynamic_status = 1;
909 /* Presence of _RMV indicates 'removable' */
910 status = acpi_get_handle(device->handle, "_RMV", &temp);
911 if (ACPI_SUCCESS(status))
912 device->flags.removable = 1;
914 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
915 status = acpi_get_handle(device->handle, "_EJD", &temp);
916 if (ACPI_SUCCESS(status))
917 device->flags.ejectable = 1;
919 status = acpi_get_handle(device->handle, "_EJ0", &temp);
920 if (ACPI_SUCCESS(status))
921 device->flags.ejectable = 1;
924 /* Presence of _LCK indicates 'lockable' */
925 status = acpi_get_handle(device->handle, "_LCK", &temp);
926 if (ACPI_SUCCESS(status))
927 device->flags.lockable = 1;
929 /* Presence of _PS0|_PR0 indicates 'power manageable' */
930 status = acpi_get_handle(device->handle, "_PS0", &temp);
931 if (ACPI_FAILURE(status))
932 status = acpi_get_handle(device->handle, "_PR0", &temp);
933 if (ACPI_SUCCESS(status))
934 device->flags.power_manageable = 1;
936 /* Presence of _PRW indicates wake capable */
937 status = acpi_get_handle(device->handle, "_PRW", &temp);
938 if (ACPI_SUCCESS(status))
939 device->flags.wake_capable = 1;
941 /* TBD: Performance management */
946 static void acpi_device_get_busid(struct acpi_device *device)
948 char bus_id[5] = { '?', 0 };
949 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
955 * The device's Bus ID is simply the object name.
956 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
958 if (ACPI_IS_ROOT_DEVICE(device)) {
959 strcpy(device->pnp.bus_id, "ACPI");
963 switch (device->device_type) {
964 case ACPI_BUS_TYPE_POWER_BUTTON:
965 strcpy(device->pnp.bus_id, "PWRF");
967 case ACPI_BUS_TYPE_SLEEP_BUTTON:
968 strcpy(device->pnp.bus_id, "SLPF");
971 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
972 /* Clean up trailing underscores (if any) */
973 for (i = 3; i > 1; i--) {
974 if (bus_id[i] == '_')
979 strcpy(device->pnp.bus_id, bus_id);
985 * acpi_bay_match - see if a device is an ejectable driver bay
987 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
988 * then we can safely call it an ejectable drive bay
990 static int acpi_bay_match(struct acpi_device *device){
996 handle = device->handle;
998 status = acpi_get_handle(handle, "_EJ0", &tmp);
999 if (ACPI_FAILURE(status))
1002 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1003 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1004 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1005 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1008 if (acpi_get_parent(handle, &phandle))
1011 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1012 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1013 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1014 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1021 * acpi_dock_match - see if a device has a _DCK method
1023 static int acpi_dock_match(struct acpi_device *device)
1026 return acpi_get_handle(device->handle, "_DCK", &tmp);
1029 char *acpi_device_hid(struct acpi_device *device)
1031 struct acpi_hardware_id *hid;
1033 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1036 EXPORT_SYMBOL(acpi_device_hid);
1038 static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1040 struct acpi_hardware_id *id;
1042 id = kmalloc(sizeof(*id), GFP_KERNEL);
1046 id->id = kmalloc(strlen(dev_id) + 1, GFP_KERNEL);
1052 strcpy(id->id, dev_id);
1053 list_add_tail(&id->list, &device->pnp.ids);
1057 * Old IBM workstations have a DSDT bug wherein the SMBus object
1058 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1059 * prefix. Work around this.
1061 static int acpi_ibm_smbus_match(struct acpi_device *device)
1063 acpi_handle h_dummy;
1064 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1067 if (!dmi_name_in_vendors("IBM"))
1070 /* Look for SMBS object */
1071 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1075 if (strcmp("SMBS", path.pointer)) {
1080 /* Does it have the necessary (but misnamed) methods? */
1082 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1083 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1084 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1087 kfree(path.pointer);
1091 static void acpi_device_set_id(struct acpi_device *device)
1094 struct acpi_device_info *info;
1095 struct acpica_device_id_list *cid_list;
1098 switch (device->device_type) {
1099 case ACPI_BUS_TYPE_DEVICE:
1100 if (ACPI_IS_ROOT_DEVICE(device)) {
1101 acpi_add_id(device, ACPI_SYSTEM_HID);
1103 } else if (ACPI_IS_ROOT_DEVICE(device->parent)) {
1104 /* \_SB_, the only root-level namespace device */
1105 acpi_add_id(device, ACPI_BUS_HID);
1106 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1107 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1111 status = acpi_get_object_info(device->handle, &info);
1112 if (ACPI_FAILURE(status)) {
1113 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1117 if (info->valid & ACPI_VALID_HID)
1118 acpi_add_id(device, info->hardware_id.string);
1119 if (info->valid & ACPI_VALID_CID) {
1120 cid_list = &info->compatible_id_list;
1121 for (i = 0; i < cid_list->count; i++)
1122 acpi_add_id(device, cid_list->ids[i].string);
1124 #ifdef CONFIG_PCI_GUESTDEV
1125 if (info->valid & ACPI_VALID_UID)
1126 device->pnp.unique_id = kstrdup(info->unique_id.string,
1129 if (info->valid & ACPI_VALID_ADR) {
1130 device->pnp.bus_address = info->address;
1131 device->flags.bus_address = 1;
1137 * Some devices don't reliably have _HIDs & _CIDs, so add
1138 * synthetic HIDs to make sure drivers can find them.
1140 if (acpi_is_video_device(device))
1141 acpi_add_id(device, ACPI_VIDEO_HID);
1142 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1143 acpi_add_id(device, ACPI_BAY_HID);
1144 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1145 acpi_add_id(device, ACPI_DOCK_HID);
1146 else if (!acpi_ibm_smbus_match(device))
1147 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
1150 case ACPI_BUS_TYPE_POWER:
1151 acpi_add_id(device, ACPI_POWER_HID);
1153 case ACPI_BUS_TYPE_PROCESSOR:
1154 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
1156 case ACPI_BUS_TYPE_THERMAL:
1157 acpi_add_id(device, ACPI_THERMAL_HID);
1159 case ACPI_BUS_TYPE_POWER_BUTTON:
1160 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
1162 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1163 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
1168 * We build acpi_devices for some objects that don't have _HID or _CID,
1169 * e.g., PCI bridges and slots. Drivers can't bind to these objects,
1170 * but we do use them indirectly by traversing the acpi_device tree.
1171 * This generic ID isn't useful for driver binding, but it provides
1172 * the useful property that "every acpi_device has an ID."
1174 if (list_empty(&device->pnp.ids))
1175 acpi_add_id(device, "device");
1178 static int acpi_device_set_context(struct acpi_device *device)
1185 * Attach this 'struct acpi_device' to the ACPI object. This makes
1186 * resolutions from handle->device very efficient. Fixed hardware
1187 * devices have no handles, so we skip them.
1189 if (!device->handle)
1192 status = acpi_attach_data(device->handle,
1193 acpi_bus_data_handler, device);
1194 if (ACPI_SUCCESS(status))
1197 printk(KERN_ERR PREFIX "Error attaching device data\n");
1201 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1206 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1207 device_release_driver(&dev->dev);
1213 * unbind _ADR-Based Devices when hot removal
1215 if (dev->flags.bus_address) {
1216 if ((dev->parent) && (dev->parent->ops.unbind))
1217 dev->parent->ops.unbind(dev);
1219 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1224 static int acpi_add_single_object(struct acpi_device **child,
1225 acpi_handle handle, int type,
1226 unsigned long long sta,
1227 struct acpi_bus_ops *ops)
1230 struct acpi_device *device;
1231 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1233 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1235 printk(KERN_ERR PREFIX "Memory allocation error\n");
1239 INIT_LIST_HEAD(&device->pnp.ids);
1240 device->device_type = type;
1241 device->handle = handle;
1242 device->parent = acpi_bus_get_parent(handle);
1243 device->bus_ops = *ops; /* workround for not call .start */
1244 STRUCT_TO_INT(device->status) = sta;
1246 acpi_device_get_busid(device);
1251 * Note that we only look for object handles -- cannot evaluate objects
1252 * until we know the device is present and properly initialized.
1254 result = acpi_bus_get_flags(device);
1261 * TBD: Synch with Core's enumeration/initialization process.
1263 acpi_device_set_id(device);
1269 if (device->flags.power_manageable) {
1270 result = acpi_bus_get_power_flags(device);
1276 * Wakeup device management
1277 *-----------------------
1279 if (device->flags.wake_capable) {
1280 result = acpi_bus_get_wakeup_device_flags(device);
1286 * Performance Management
1287 * ----------------------
1289 if (device->flags.performance_manageable) {
1290 result = acpi_bus_get_perf_flags(device);
1295 if ((result = acpi_device_set_context(device)))
1298 result = acpi_device_register(device);
1301 * Bind _ADR-Based Devices when hot add
1303 if (device->flags.bus_address) {
1304 if (device->parent && device->parent->ops.bind)
1305 device->parent->ops.bind(device);
1310 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1311 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1312 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1313 (char *) buffer.pointer,
1314 device->parent ? dev_name(&device->parent->dev) :
1316 kfree(buffer.pointer);
1319 acpi_device_release(&device->dev);
1324 #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1325 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1327 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1328 unsigned long long *sta)
1331 acpi_object_type acpi_type;
1333 status = acpi_get_type(handle, &acpi_type);
1334 if (ACPI_FAILURE(status))
1337 switch (acpi_type) {
1338 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1339 case ACPI_TYPE_DEVICE:
1340 *type = ACPI_BUS_TYPE_DEVICE;
1341 status = acpi_bus_get_status_handle(handle, sta);
1342 if (ACPI_FAILURE(status))
1345 case ACPI_TYPE_PROCESSOR:
1346 *type = ACPI_BUS_TYPE_PROCESSOR;
1347 status = acpi_bus_get_status_handle(handle, sta);
1348 if (ACPI_FAILURE(status))
1351 case ACPI_TYPE_THERMAL:
1352 *type = ACPI_BUS_TYPE_THERMAL;
1353 *sta = ACPI_STA_DEFAULT;
1355 case ACPI_TYPE_POWER:
1356 *type = ACPI_BUS_TYPE_POWER;
1357 *sta = ACPI_STA_DEFAULT;
1366 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1367 void *context, void **return_value)
1369 struct acpi_bus_ops *ops = context;
1371 unsigned long long sta;
1372 struct acpi_device *device;
1376 result = acpi_bus_type_and_status(handle, &type, &sta);
1380 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1381 !(sta & ACPI_STA_DEVICE_FUNCTIONING))
1382 return AE_CTRL_DEPTH;
1385 * We may already have an acpi_device from a previous enumeration. If
1386 * so, we needn't add it again, but we may still have to start it.
1389 acpi_bus_get_device(handle, &device);
1390 if (ops->acpi_op_add && !device)
1391 acpi_add_single_object(&device, handle, type, sta, ops);
1394 return AE_CTRL_DEPTH;
1396 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1397 status = acpi_start_single_object(device);
1398 if (ACPI_FAILURE(status))
1399 return AE_CTRL_DEPTH;
1403 *return_value = device;
1407 static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1408 struct acpi_device **child)
1411 void *device = NULL;
1413 status = acpi_bus_check_add(handle, 0, ops, &device);
1414 if (ACPI_SUCCESS(status))
1415 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1416 acpi_bus_check_add, NULL, ops, &device);
1428 * acpi_bus_add and acpi_bus_start
1430 * scan a given ACPI tree and (probably recently hot-plugged)
1431 * create and add or starts found devices.
1433 * If no devices were found -ENODEV is returned which does not
1434 * mean that this is a real error, there just have been no suitable
1435 * ACPI objects in the table trunk from which the kernel could create
1436 * a device and add/start an appropriate driver.
1440 acpi_bus_add(struct acpi_device **child,
1441 struct acpi_device *parent, acpi_handle handle, int type)
1443 struct acpi_bus_ops ops;
1445 memset(&ops, 0, sizeof(ops));
1446 ops.acpi_op_add = 1;
1448 return acpi_bus_scan(handle, &ops, child);
1450 EXPORT_SYMBOL(acpi_bus_add);
1452 int acpi_bus_start(struct acpi_device *device)
1454 struct acpi_bus_ops ops;
1459 memset(&ops, 0, sizeof(ops));
1460 ops.acpi_op_start = 1;
1462 return acpi_bus_scan(device->handle, &ops, NULL);
1464 EXPORT_SYMBOL(acpi_bus_start);
1466 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1469 struct acpi_device *parent, *child;
1470 acpi_handle phandle, chandle;
1471 acpi_object_type type;
1476 phandle = start->handle;
1477 child = chandle = NULL;
1479 while ((level > 0) && parent && (!err)) {
1480 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1484 * If this scope is exhausted then move our way back up.
1486 if (ACPI_FAILURE(status)) {
1489 acpi_get_parent(phandle, &phandle);
1491 parent = parent->parent;
1494 err = acpi_bus_remove(child, rmdevice);
1496 err = acpi_bus_remove(child, 1);
1501 status = acpi_get_type(chandle, &type);
1502 if (ACPI_FAILURE(status)) {
1506 * If there is a device corresponding to chandle then
1507 * parse it (depth-first).
1509 if (acpi_bus_get_device(chandle, &child) == 0) {
1519 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1521 static int acpi_bus_scan_fixed(void)
1524 struct acpi_device *device = NULL;
1525 struct acpi_bus_ops ops;
1527 memset(&ops, 0, sizeof(ops));
1528 ops.acpi_op_add = 1;
1529 ops.acpi_op_start = 1;
1532 * Enumerate all fixed-feature devices.
1534 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
1535 result = acpi_add_single_object(&device, NULL,
1536 ACPI_BUS_TYPE_POWER_BUTTON,
1541 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1542 result = acpi_add_single_object(&device, NULL,
1543 ACPI_BUS_TYPE_SLEEP_BUTTON,
1551 int __init acpi_scan_init(void)
1554 struct acpi_bus_ops ops;
1556 memset(&ops, 0, sizeof(ops));
1557 ops.acpi_op_add = 1;
1558 ops.acpi_op_start = 1;
1560 result = bus_register(&acpi_bus_type);
1562 /* We don't want to quit even if we failed to add suspend/resume */
1563 printk(KERN_ERR PREFIX "Could not register bus type\n");
1567 * Enumerate devices in the ACPI namespace.
1569 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
1572 result = acpi_bus_scan_fixed();
1575 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);