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>
12 #include <acpi/acpi_drivers.h>
16 #define _COMPONENT ACPI_BUS_COMPONENT
17 ACPI_MODULE_NAME("scan");
18 #define STRUCT_TO_INT(s) (*((int*)&s))
19 extern struct acpi_device *acpi_root;
21 #define ACPI_BUS_CLASS "system_bus"
22 #define ACPI_BUS_HID "LNXSYBUS"
23 #define ACPI_BUS_DEVICE_NAME "System Bus"
25 #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
27 static LIST_HEAD(acpi_device_list);
28 static LIST_HEAD(acpi_bus_id_list);
29 DEFINE_MUTEX(acpi_device_lock);
30 LIST_HEAD(acpi_wakeup_device_list);
32 struct acpi_device_bus_id{
34 unsigned int instance_no;
35 struct list_head node;
39 * Creates hid/cid(s) string needed for modalias and uevent
40 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
41 * char *modalias: "acpi:IBM0001:ACPI0001"
43 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
48 struct acpi_hardware_id *id;
50 if (!acpi_dev->flags.hardware_id)
53 len = snprintf(modalias, size, "acpi:");
56 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
57 count = snprintf(&modalias[len], size, "%s:", id->id);
58 if (count < 0 || count >= size)
69 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
70 struct acpi_device *acpi_dev = to_acpi_device(dev);
73 /* Device has no HID and no CID or string is >1024 */
74 len = create_modalias(acpi_dev, buf, 1024);
80 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
82 static void acpi_bus_hot_remove_device(void *context)
84 struct acpi_device *device;
85 acpi_handle handle = context;
86 struct acpi_object_list arg_list;
87 union acpi_object arg;
88 acpi_status status = AE_OK;
90 if (acpi_bus_get_device(handle, &device))
96 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
97 "Hot-removing device %s...\n", dev_name(&device->dev)));
99 if (acpi_bus_trim(device, 1)) {
100 printk(KERN_ERR PREFIX
101 "Removing device failed\n");
105 /* power off device */
106 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
107 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
108 printk(KERN_WARNING PREFIX
109 "Power-off device failed\n");
111 if (device->flags.lockable) {
113 arg_list.pointer = &arg;
114 arg.type = ACPI_TYPE_INTEGER;
115 arg.integer.value = 0;
116 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
120 arg_list.pointer = &arg;
121 arg.type = ACPI_TYPE_INTEGER;
122 arg.integer.value = 1;
127 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
128 if (ACPI_FAILURE(status))
129 printk(KERN_WARNING PREFIX
130 "Eject device failed\n");
136 acpi_eject_store(struct device *d, struct device_attribute *attr,
137 const char *buf, size_t count)
141 acpi_object_type type = 0;
142 struct acpi_device *acpi_device = to_acpi_device(d);
144 if ((!count) || (buf[0] != '1')) {
148 if (acpi_device->driver == NULL) {
153 status = acpi_get_type(acpi_device->handle, &type);
154 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
159 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, acpi_device->handle);
164 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
167 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
168 struct acpi_device *acpi_dev = to_acpi_device(dev);
170 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
172 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
175 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
176 struct acpi_device *acpi_dev = to_acpi_device(dev);
177 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
180 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
184 result = sprintf(buf, "%s\n", (char*)path.pointer);
189 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
191 static int acpi_device_setup_files(struct acpi_device *dev)
198 * Devices gotten from FADT don't have a "path" attribute
201 result = device_create_file(&dev->dev, &dev_attr_path);
206 if (dev->flags.hardware_id) {
207 result = device_create_file(&dev->dev, &dev_attr_hid);
212 if (dev->flags.hardware_id) {
213 result = device_create_file(&dev->dev, &dev_attr_modalias);
219 * If device has _EJ0, 'eject' file is created that is used to trigger
220 * hot-removal function from userland.
222 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
223 if (ACPI_SUCCESS(status))
224 result = device_create_file(&dev->dev, &dev_attr_eject);
229 static void acpi_device_remove_files(struct acpi_device *dev)
235 * If device has _EJ0, 'eject' file is created that is used to trigger
236 * hot-removal function from userland.
238 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
239 if (ACPI_SUCCESS(status))
240 device_remove_file(&dev->dev, &dev_attr_eject);
242 if (dev->flags.hardware_id)
243 device_remove_file(&dev->dev, &dev_attr_modalias);
245 if (dev->flags.hardware_id)
246 device_remove_file(&dev->dev, &dev_attr_hid);
248 device_remove_file(&dev->dev, &dev_attr_path);
250 /* --------------------------------------------------------------------------
252 -------------------------------------------------------------------------- */
254 int acpi_match_device_ids(struct acpi_device *device,
255 const struct acpi_device_id *ids)
257 const struct acpi_device_id *id;
258 struct acpi_hardware_id *hwid;
261 * If the device is not present, it is unnecessary to load device
264 if (!device->status.present)
267 for (id = ids; id->id[0]; id++)
268 list_for_each_entry(hwid, &device->pnp.ids, list)
269 if (!strcmp((char *) id->id, hwid->id))
274 EXPORT_SYMBOL(acpi_match_device_ids);
276 static void acpi_free_ids(struct acpi_device *device)
278 struct acpi_hardware_id *id, *tmp;
280 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
286 static void acpi_device_release(struct device *dev)
288 struct acpi_device *acpi_dev = to_acpi_device(dev);
290 acpi_free_ids(acpi_dev);
294 static int acpi_device_suspend(struct device *dev, pm_message_t state)
296 struct acpi_device *acpi_dev = to_acpi_device(dev);
297 struct acpi_driver *acpi_drv = acpi_dev->driver;
299 if (acpi_drv && acpi_drv->ops.suspend)
300 return acpi_drv->ops.suspend(acpi_dev, state);
304 static int acpi_device_resume(struct device *dev)
306 struct acpi_device *acpi_dev = to_acpi_device(dev);
307 struct acpi_driver *acpi_drv = acpi_dev->driver;
309 if (acpi_drv && acpi_drv->ops.resume)
310 return acpi_drv->ops.resume(acpi_dev);
314 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
316 struct acpi_device *acpi_dev = to_acpi_device(dev);
317 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
319 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
322 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
324 struct acpi_device *acpi_dev = to_acpi_device(dev);
327 if (add_uevent_var(env, "MODALIAS="))
329 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
330 sizeof(env->buf) - env->buflen);
331 if (len >= (sizeof(env->buf) - env->buflen))
337 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
339 struct acpi_device *device = data;
341 device->driver->ops.notify(device, event);
344 static acpi_status acpi_device_notify_fixed(void *data)
346 struct acpi_device *device = data;
348 /* Fixed hardware devices have no handles */
349 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
353 static int acpi_device_install_notify_handler(struct acpi_device *device)
357 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
359 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
360 acpi_device_notify_fixed,
362 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
364 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
365 acpi_device_notify_fixed,
368 status = acpi_install_notify_handler(device->handle,
373 if (ACPI_FAILURE(status))
378 static void acpi_device_remove_notify_handler(struct acpi_device *device)
380 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
381 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
382 acpi_device_notify_fixed);
383 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
384 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
385 acpi_device_notify_fixed);
387 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
391 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
392 static int acpi_start_single_object(struct acpi_device *);
393 static int acpi_device_probe(struct device * dev)
395 struct acpi_device *acpi_dev = to_acpi_device(dev);
396 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
399 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
401 if (acpi_dev->bus_ops.acpi_op_start)
402 acpi_start_single_object(acpi_dev);
404 if (acpi_drv->ops.notify) {
405 ret = acpi_device_install_notify_handler(acpi_dev);
407 if (acpi_drv->ops.remove)
408 acpi_drv->ops.remove(acpi_dev,
409 acpi_dev->removal_type);
414 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
415 "Found driver [%s] for device [%s]\n",
416 acpi_drv->name, acpi_dev->pnp.bus_id));
422 static int acpi_device_remove(struct device * dev)
424 struct acpi_device *acpi_dev = to_acpi_device(dev);
425 struct acpi_driver *acpi_drv = acpi_dev->driver;
428 if (acpi_drv->ops.notify)
429 acpi_device_remove_notify_handler(acpi_dev);
430 if (acpi_drv->ops.remove)
431 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
433 acpi_dev->driver = NULL;
434 acpi_dev->driver_data = NULL;
440 struct bus_type acpi_bus_type = {
442 .suspend = acpi_device_suspend,
443 .resume = acpi_device_resume,
444 .match = acpi_bus_match,
445 .probe = acpi_device_probe,
446 .remove = acpi_device_remove,
447 .uevent = acpi_device_uevent,
450 static int acpi_device_register(struct acpi_device *device)
453 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
459 * Link this device to its parent and siblings.
461 INIT_LIST_HEAD(&device->children);
462 INIT_LIST_HEAD(&device->node);
463 INIT_LIST_HEAD(&device->wakeup_list);
465 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
467 printk(KERN_ERR PREFIX "Memory allocation error\n");
471 mutex_lock(&acpi_device_lock);
473 * Find suitable bus_id and instance number in acpi_bus_id_list
474 * If failed, create one and link it into acpi_bus_id_list
476 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
477 if (!strcmp(acpi_device_bus_id->bus_id, device->flags.hardware_id ? acpi_device_hid(device) : "device")) {
478 acpi_device_bus_id->instance_no ++;
485 acpi_device_bus_id = new_bus_id;
486 strcpy(acpi_device_bus_id->bus_id, device->flags.hardware_id ? acpi_device_hid(device) : "device");
487 acpi_device_bus_id->instance_no = 0;
488 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
490 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
493 list_add_tail(&device->node, &device->parent->children);
495 if (device->wakeup.flags.valid)
496 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
497 mutex_unlock(&acpi_device_lock);
500 device->dev.parent = &device->parent->dev;
501 device->dev.bus = &acpi_bus_type;
502 device->dev.release = &acpi_device_release;
503 result = device_register(&device->dev);
505 dev_err(&device->dev, "Error registering device\n");
509 result = acpi_device_setup_files(device);
511 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
512 dev_name(&device->dev));
514 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
517 mutex_lock(&acpi_device_lock);
519 list_del(&device->node);
520 list_del(&device->wakeup_list);
521 mutex_unlock(&acpi_device_lock);
525 static void acpi_device_unregister(struct acpi_device *device, int type)
527 mutex_lock(&acpi_device_lock);
529 list_del(&device->node);
531 list_del(&device->wakeup_list);
532 mutex_unlock(&acpi_device_lock);
534 acpi_detach_data(device->handle, acpi_bus_data_handler);
536 acpi_device_remove_files(device);
537 device_unregister(&device->dev);
540 /* --------------------------------------------------------------------------
542 -------------------------------------------------------------------------- */
544 * acpi_bus_driver_init - add a device to a driver
545 * @device: the device to add and initialize
546 * @driver: driver for the device
548 * Used to initialize a device via its device driver. Called whenever a
549 * driver is bound to a device. Invokes the driver's add() ops.
552 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
556 if (!device || !driver)
559 if (!driver->ops.add)
562 result = driver->ops.add(device);
564 device->driver = NULL;
565 device->driver_data = NULL;
569 device->driver = driver;
572 * TBD - Configuration Management: Assign resources to device based
573 * upon possible configuration and currently allocated resources.
576 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
577 "Driver successfully bound to device\n"));
581 static int acpi_start_single_object(struct acpi_device *device)
584 struct acpi_driver *driver;
587 if (!(driver = device->driver))
590 if (driver->ops.start) {
591 result = driver->ops.start(device);
592 if (result && driver->ops.remove)
593 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
600 * acpi_bus_register_driver - register a driver with the ACPI bus
601 * @driver: driver being registered
603 * Registers a driver with the ACPI bus. Searches the namespace for all
604 * devices that match the driver's criteria and binds. Returns zero for
605 * success or a negative error status for failure.
607 int acpi_bus_register_driver(struct acpi_driver *driver)
613 driver->drv.name = driver->name;
614 driver->drv.bus = &acpi_bus_type;
615 driver->drv.owner = driver->owner;
617 ret = driver_register(&driver->drv);
621 EXPORT_SYMBOL(acpi_bus_register_driver);
624 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
625 * @driver: driver to unregister
627 * Unregisters a driver with the ACPI bus. Searches the namespace for all
628 * devices that match the driver's criteria and unbinds.
630 void acpi_bus_unregister_driver(struct acpi_driver *driver)
632 driver_unregister(&driver->drv);
635 EXPORT_SYMBOL(acpi_bus_unregister_driver);
637 /* --------------------------------------------------------------------------
639 -------------------------------------------------------------------------- */
640 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
644 struct acpi_device *device;
647 * Fixed hardware devices do not appear in the namespace and do not
648 * have handles, but we fabricate acpi_devices for them, so we have
649 * to deal with them specially.
655 status = acpi_get_parent(handle, &handle);
656 if (status == AE_NULL_ENTRY)
658 if (ACPI_FAILURE(status))
661 ret = acpi_bus_get_device(handle, &device);
668 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
672 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
673 union acpi_object *obj;
675 status = acpi_get_handle(handle, "_EJD", &tmp);
676 if (ACPI_FAILURE(status))
679 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
680 if (ACPI_SUCCESS(status)) {
681 obj = buffer.pointer;
682 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
684 kfree(buffer.pointer);
688 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
690 void acpi_bus_data_handler(acpi_handle handle, void *context)
698 static int acpi_bus_get_perf_flags(struct acpi_device *device)
700 device->performance.state = ACPI_STATE_UNKNOWN;
705 acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
706 union acpi_object *package)
709 union acpi_object *element = NULL;
711 if (!device || !package || (package->package.count < 2))
712 return AE_BAD_PARAMETER;
714 element = &(package->package.elements[0]);
716 return AE_BAD_PARAMETER;
717 if (element->type == ACPI_TYPE_PACKAGE) {
718 if ((element->package.count < 2) ||
719 (element->package.elements[0].type !=
720 ACPI_TYPE_LOCAL_REFERENCE)
721 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
723 device->wakeup.gpe_device =
724 element->package.elements[0].reference.handle;
725 device->wakeup.gpe_number =
726 (u32) element->package.elements[1].integer.value;
727 } else if (element->type == ACPI_TYPE_INTEGER) {
728 device->wakeup.gpe_number = element->integer.value;
732 element = &(package->package.elements[1]);
733 if (element->type != ACPI_TYPE_INTEGER) {
736 device->wakeup.sleep_state = element->integer.value;
738 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
741 device->wakeup.resources.count = package->package.count - 2;
742 for (i = 0; i < device->wakeup.resources.count; i++) {
743 element = &(package->package.elements[i + 2]);
744 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
747 device->wakeup.resources.handles[i] = element->reference.handle;
753 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
755 acpi_status status = 0;
756 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
757 union acpi_object *package = NULL;
760 struct acpi_device_id button_device_ids[] = {
768 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
769 if (ACPI_FAILURE(status)) {
770 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
774 package = (union acpi_object *)buffer.pointer;
775 status = acpi_bus_extract_wakeup_device_power_package(device, package);
776 if (ACPI_FAILURE(status)) {
777 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
781 kfree(buffer.pointer);
783 device->wakeup.flags.valid = 1;
784 device->wakeup.prepare_count = 0;
785 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
786 * system for the ACPI device with the _PRW object.
787 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
788 * So it is necessary to call _DSW object first. Only when it is not
789 * present will the _PSW object used.
791 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
793 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
794 "error in _DSW or _PSW evaluation\n"));
796 /* Power button, Lid switch always enable wakeup */
797 if (!acpi_match_device_ids(device, button_device_ids))
798 device->wakeup.flags.run_wake = 1;
801 if (ACPI_FAILURE(status))
802 device->flags.wake_capable = 0;
806 static int acpi_bus_get_power_flags(struct acpi_device *device)
808 acpi_status status = 0;
809 acpi_handle handle = NULL;
814 * Power Management Flags
816 status = acpi_get_handle(device->handle, "_PSC", &handle);
817 if (ACPI_SUCCESS(status))
818 device->power.flags.explicit_get = 1;
819 status = acpi_get_handle(device->handle, "_IRC", &handle);
820 if (ACPI_SUCCESS(status))
821 device->power.flags.inrush_current = 1;
824 * Enumerate supported power management states
826 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
827 struct acpi_device_power_state *ps = &device->power.states[i];
828 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
830 /* Evaluate "_PRx" to se if power resources are referenced */
831 acpi_evaluate_reference(device->handle, object_name, NULL,
833 if (ps->resources.count) {
834 device->power.flags.power_resources = 1;
838 /* Evaluate "_PSx" to see if we can do explicit sets */
839 object_name[2] = 'S';
840 status = acpi_get_handle(device->handle, object_name, &handle);
841 if (ACPI_SUCCESS(status)) {
842 ps->flags.explicit_set = 1;
846 /* State is valid if we have some power control */
847 if (ps->resources.count || ps->flags.explicit_set)
850 ps->power = -1; /* Unknown - driver assigned */
851 ps->latency = -1; /* Unknown - driver assigned */
854 /* Set defaults for D0 and D3 states (always valid) */
855 device->power.states[ACPI_STATE_D0].flags.valid = 1;
856 device->power.states[ACPI_STATE_D0].power = 100;
857 device->power.states[ACPI_STATE_D3].flags.valid = 1;
858 device->power.states[ACPI_STATE_D3].power = 0;
860 /* TBD: System wake support and resource requirements. */
862 device->power.state = ACPI_STATE_UNKNOWN;
863 acpi_bus_get_power(device->handle, &(device->power.state));
868 static int acpi_bus_get_flags(struct acpi_device *device)
870 acpi_status status = AE_OK;
871 acpi_handle temp = NULL;
874 /* Presence of _STA indicates 'dynamic_status' */
875 status = acpi_get_handle(device->handle, "_STA", &temp);
876 if (ACPI_SUCCESS(status))
877 device->flags.dynamic_status = 1;
879 /* Presence of _RMV indicates 'removable' */
880 status = acpi_get_handle(device->handle, "_RMV", &temp);
881 if (ACPI_SUCCESS(status))
882 device->flags.removable = 1;
884 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
885 status = acpi_get_handle(device->handle, "_EJD", &temp);
886 if (ACPI_SUCCESS(status))
887 device->flags.ejectable = 1;
889 status = acpi_get_handle(device->handle, "_EJ0", &temp);
890 if (ACPI_SUCCESS(status))
891 device->flags.ejectable = 1;
894 /* Presence of _LCK indicates 'lockable' */
895 status = acpi_get_handle(device->handle, "_LCK", &temp);
896 if (ACPI_SUCCESS(status))
897 device->flags.lockable = 1;
899 /* Presence of _PS0|_PR0 indicates 'power manageable' */
900 status = acpi_get_handle(device->handle, "_PS0", &temp);
901 if (ACPI_FAILURE(status))
902 status = acpi_get_handle(device->handle, "_PR0", &temp);
903 if (ACPI_SUCCESS(status))
904 device->flags.power_manageable = 1;
906 /* Presence of _PRW indicates wake capable */
907 status = acpi_get_handle(device->handle, "_PRW", &temp);
908 if (ACPI_SUCCESS(status))
909 device->flags.wake_capable = 1;
911 /* TBD: Performance management */
916 static void acpi_device_get_busid(struct acpi_device *device)
918 char bus_id[5] = { '?', 0 };
919 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
925 * The device's Bus ID is simply the object name.
926 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
928 if (ACPI_IS_ROOT_DEVICE(device)) {
929 strcpy(device->pnp.bus_id, "ACPI");
933 switch (device->device_type) {
934 case ACPI_BUS_TYPE_POWER_BUTTON:
935 strcpy(device->pnp.bus_id, "PWRF");
937 case ACPI_BUS_TYPE_SLEEP_BUTTON:
938 strcpy(device->pnp.bus_id, "SLPF");
941 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
942 /* Clean up trailing underscores (if any) */
943 for (i = 3; i > 1; i--) {
944 if (bus_id[i] == '_')
949 strcpy(device->pnp.bus_id, bus_id);
955 * acpi_bay_match - see if a device is an ejectable driver bay
957 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
958 * then we can safely call it an ejectable drive bay
960 static int acpi_bay_match(struct acpi_device *device){
966 handle = device->handle;
968 status = acpi_get_handle(handle, "_EJ0", &tmp);
969 if (ACPI_FAILURE(status))
972 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
973 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
974 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
975 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
978 if (acpi_get_parent(handle, &phandle))
981 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
982 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
983 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
984 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
991 * acpi_dock_match - see if a device has a _DCK method
993 static int acpi_dock_match(struct acpi_device *device)
996 return acpi_get_handle(device->handle, "_DCK", &tmp);
999 char *acpi_device_hid(struct acpi_device *device)
1001 struct acpi_hardware_id *hid;
1003 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1006 EXPORT_SYMBOL(acpi_device_hid);
1008 static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1010 struct acpi_hardware_id *id;
1012 id = kmalloc(sizeof(*id), GFP_KERNEL);
1016 id->id = kmalloc(strlen(dev_id) + 1, GFP_KERNEL);
1022 strcpy(id->id, dev_id);
1023 list_add_tail(&id->list, &device->pnp.ids);
1026 static void acpi_device_set_id(struct acpi_device *device)
1028 struct acpi_device_info *info = NULL;
1031 struct acpica_device_id_list *cid_list = NULL;
1032 char *cid_add = NULL;
1036 switch (device->device_type) {
1037 case ACPI_BUS_TYPE_DEVICE:
1038 if (ACPI_IS_ROOT_DEVICE(device)) {
1039 hid = ACPI_SYSTEM_HID;
1041 } else if (ACPI_IS_ROOT_DEVICE(device->parent)) {
1042 /* \_SB_, the only root-level namespace device */
1044 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1045 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1049 status = acpi_get_object_info(device->handle, &info);
1050 if (ACPI_FAILURE(status)) {
1051 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1055 if (info->valid & ACPI_VALID_HID)
1056 hid = info->hardware_id.string;
1057 if (info->valid & ACPI_VALID_UID)
1058 uid = info->unique_id.string;
1059 if (info->valid & ACPI_VALID_CID)
1060 cid_list = &info->compatible_id_list;
1061 if (info->valid & ACPI_VALID_ADR) {
1062 device->pnp.bus_address = info->address;
1063 device->flags.bus_address = 1;
1066 /* If we have a video/bay/dock device, add our selfdefined
1067 HID to the CID list. Like that the video/bay/dock drivers
1068 will get autoloaded and the device might still match
1069 against another driver.
1071 if (acpi_is_video_device(device))
1072 cid_add = ACPI_VIDEO_HID;
1073 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1074 cid_add = ACPI_BAY_HID;
1075 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1076 cid_add = ACPI_DOCK_HID;
1079 case ACPI_BUS_TYPE_POWER:
1080 hid = ACPI_POWER_HID;
1082 case ACPI_BUS_TYPE_PROCESSOR:
1083 hid = ACPI_PROCESSOR_OBJECT_HID;
1085 case ACPI_BUS_TYPE_THERMAL:
1086 hid = ACPI_THERMAL_HID;
1088 case ACPI_BUS_TYPE_POWER_BUTTON:
1089 hid = ACPI_BUTTON_HID_POWERF;
1091 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1092 hid = ACPI_BUTTON_HID_SLEEPF;
1097 * We build acpi_devices for some objects that don't have _HID or _CID,
1098 * e.g., PCI bridges and slots. Drivers can't bind to these objects,
1099 * but we do use them indirectly by traversing the acpi_device tree.
1100 * This generic ID isn't useful for driver binding, but it provides
1101 * the useful property that "every acpi_device has an ID."
1103 if (!hid && !cid_list && !cid_add)
1107 acpi_add_id(device, hid);
1108 device->flags.hardware_id = 1;
1111 device->pnp.unique_id = ACPI_ALLOCATE_ZEROED(strlen (uid) + 1);
1112 if (device->pnp.unique_id) {
1113 strcpy(device->pnp.unique_id, uid);
1114 device->flags.unique_id = 1;
1117 if (!device->flags.unique_id)
1118 device->pnp.unique_id = "";
1121 for (i = 0; i < cid_list->count; i++)
1122 acpi_add_id(device, cid_list->ids[i].string);
1124 acpi_add_id(device, cid_add);
1129 static int acpi_device_set_context(struct acpi_device *device)
1136 * Attach this 'struct acpi_device' to the ACPI object. This makes
1137 * resolutions from handle->device very efficient. Fixed hardware
1138 * devices have no handles, so we skip them.
1140 if (!device->handle)
1143 status = acpi_attach_data(device->handle,
1144 acpi_bus_data_handler, device);
1145 if (ACPI_SUCCESS(status))
1148 printk(KERN_ERR PREFIX "Error attaching device data\n");
1152 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1157 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1158 device_release_driver(&dev->dev);
1164 * unbind _ADR-Based Devices when hot removal
1166 if (dev->flags.bus_address) {
1167 if ((dev->parent) && (dev->parent->ops.unbind))
1168 dev->parent->ops.unbind(dev);
1170 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1175 static int acpi_add_single_object(struct acpi_device **child,
1176 acpi_handle handle, int type,
1177 unsigned long long sta,
1178 struct acpi_bus_ops *ops)
1181 struct acpi_device *device;
1182 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1184 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1186 printk(KERN_ERR PREFIX "Memory allocation error\n");
1190 INIT_LIST_HEAD(&device->pnp.ids);
1191 device->device_type = type;
1192 device->handle = handle;
1193 device->parent = acpi_bus_get_parent(handle);
1194 device->bus_ops = *ops; /* workround for not call .start */
1195 STRUCT_TO_INT(device->status) = sta;
1197 acpi_device_get_busid(device);
1202 * Note that we only look for object handles -- cannot evaluate objects
1203 * until we know the device is present and properly initialized.
1205 result = acpi_bus_get_flags(device);
1212 * TBD: Synch with Core's enumeration/initialization process.
1216 * Hardware ID, Unique ID, & Bus Address
1217 * -------------------------------------
1219 acpi_device_set_id(device);
1225 if (device->flags.power_manageable) {
1226 result = acpi_bus_get_power_flags(device);
1232 * Wakeup device management
1233 *-----------------------
1235 if (device->flags.wake_capable) {
1236 result = acpi_bus_get_wakeup_device_flags(device);
1242 * Performance Management
1243 * ----------------------
1245 if (device->flags.performance_manageable) {
1246 result = acpi_bus_get_perf_flags(device);
1251 if ((result = acpi_device_set_context(device)))
1254 result = acpi_device_register(device);
1257 * Bind _ADR-Based Devices when hot add
1259 if (device->flags.bus_address) {
1260 if (device->parent && device->parent->ops.bind)
1261 device->parent->ops.bind(device);
1266 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1267 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1268 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1269 (char *) buffer.pointer,
1270 device->parent ? dev_name(&device->parent->dev) :
1272 kfree(buffer.pointer);
1275 acpi_device_release(&device->dev);
1280 #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1281 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1283 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1284 unsigned long long *sta)
1287 acpi_object_type acpi_type;
1289 status = acpi_get_type(handle, &acpi_type);
1290 if (ACPI_FAILURE(status))
1293 switch (acpi_type) {
1294 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1295 case ACPI_TYPE_DEVICE:
1296 *type = ACPI_BUS_TYPE_DEVICE;
1297 status = acpi_bus_get_status_handle(handle, sta);
1298 if (ACPI_FAILURE(status))
1301 case ACPI_TYPE_PROCESSOR:
1302 *type = ACPI_BUS_TYPE_PROCESSOR;
1303 status = acpi_bus_get_status_handle(handle, sta);
1304 if (ACPI_FAILURE(status))
1307 case ACPI_TYPE_THERMAL:
1308 *type = ACPI_BUS_TYPE_THERMAL;
1309 *sta = ACPI_STA_DEFAULT;
1311 case ACPI_TYPE_POWER:
1312 *type = ACPI_BUS_TYPE_POWER;
1313 *sta = ACPI_STA_DEFAULT;
1322 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1323 void *context, void **return_value)
1325 struct acpi_bus_ops *ops = context;
1327 unsigned long long sta;
1328 struct acpi_device *device;
1332 result = acpi_bus_type_and_status(handle, &type, &sta);
1336 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1337 !(sta & ACPI_STA_DEVICE_FUNCTIONING))
1338 return AE_CTRL_DEPTH;
1341 * We may already have an acpi_device from a previous enumeration. If
1342 * so, we needn't add it again, but we may still have to start it.
1345 acpi_bus_get_device(handle, &device);
1346 if (ops->acpi_op_add && !device)
1347 acpi_add_single_object(&device, handle, type, sta, ops);
1350 return AE_CTRL_DEPTH;
1352 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1353 status = acpi_start_single_object(device);
1354 if (ACPI_FAILURE(status))
1355 return AE_CTRL_DEPTH;
1359 *return_value = device;
1363 static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1364 struct acpi_device **child)
1367 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1368 void *device = NULL;
1370 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1371 printk(KERN_INFO PREFIX "Enumerating devices from [%s]\n",
1372 (char *) buffer.pointer);
1374 status = acpi_bus_check_add(handle, 0, ops, &device);
1375 if (ACPI_SUCCESS(status))
1376 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1377 acpi_bus_check_add, ops, &device);
1385 acpi_bus_add(struct acpi_device **child,
1386 struct acpi_device *parent, acpi_handle handle, int type)
1388 struct acpi_bus_ops ops;
1390 memset(&ops, 0, sizeof(ops));
1391 ops.acpi_op_add = 1;
1393 acpi_bus_scan(handle, &ops, child);
1396 EXPORT_SYMBOL(acpi_bus_add);
1398 int acpi_bus_start(struct acpi_device *device)
1400 struct acpi_bus_ops ops;
1402 memset(&ops, 0, sizeof(ops));
1403 ops.acpi_op_start = 1;
1405 acpi_bus_scan(device->handle, &ops, NULL);
1408 EXPORT_SYMBOL(acpi_bus_start);
1410 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1413 struct acpi_device *parent, *child;
1414 acpi_handle phandle, chandle;
1415 acpi_object_type type;
1420 phandle = start->handle;
1421 child = chandle = NULL;
1423 while ((level > 0) && parent && (!err)) {
1424 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1428 * If this scope is exhausted then move our way back up.
1430 if (ACPI_FAILURE(status)) {
1433 acpi_get_parent(phandle, &phandle);
1435 parent = parent->parent;
1438 err = acpi_bus_remove(child, rmdevice);
1440 err = acpi_bus_remove(child, 1);
1445 status = acpi_get_type(chandle, &type);
1446 if (ACPI_FAILURE(status)) {
1450 * If there is a device corresponding to chandle then
1451 * parse it (depth-first).
1453 if (acpi_bus_get_device(chandle, &child) == 0) {
1463 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1465 static int acpi_bus_scan_fixed(void)
1468 struct acpi_device *device = NULL;
1469 struct acpi_bus_ops ops;
1471 memset(&ops, 0, sizeof(ops));
1472 ops.acpi_op_add = 1;
1473 ops.acpi_op_start = 1;
1476 * Enumerate all fixed-feature devices.
1478 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
1479 result = acpi_add_single_object(&device, NULL,
1480 ACPI_BUS_TYPE_POWER_BUTTON,
1485 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1486 result = acpi_add_single_object(&device, NULL,
1487 ACPI_BUS_TYPE_SLEEP_BUTTON,
1495 int __init acpi_scan_init(void)
1498 struct acpi_bus_ops ops;
1500 memset(&ops, 0, sizeof(ops));
1501 ops.acpi_op_add = 1;
1502 ops.acpi_op_start = 1;
1504 result = bus_register(&acpi_bus_type);
1506 /* We don't want to quit even if we failed to add suspend/resume */
1507 printk(KERN_ERR PREFIX "Could not register bus type\n");
1511 * Enumerate devices in the ACPI namespace.
1513 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
1516 result = acpi_bus_scan_fixed();
1519 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);