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)
30 static char* dummy_hid = "device";
32 static LIST_HEAD(acpi_device_list);
33 static LIST_HEAD(acpi_bus_id_list);
34 DEFINE_MUTEX(acpi_device_lock);
35 LIST_HEAD(acpi_wakeup_device_list);
37 struct acpi_device_bus_id{
39 unsigned int instance_no;
40 struct list_head node;
44 * Creates hid/cid(s) string needed for modalias and uevent
45 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
46 * char *modalias: "acpi:IBM0001:ACPI0001"
48 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
53 struct acpi_hardware_id *id;
55 if (list_empty(&acpi_dev->pnp.ids))
58 len = snprintf(modalias, size, "acpi:");
61 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
62 count = snprintf(&modalias[len], size, "%s:", id->id);
63 if (count < 0 || count >= size)
74 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
75 struct acpi_device *acpi_dev = to_acpi_device(dev);
78 /* Device has no HID and no CID or string is >1024 */
79 len = create_modalias(acpi_dev, buf, 1024);
85 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
87 static void acpi_bus_hot_remove_device(void *context)
89 struct acpi_device *device;
90 acpi_handle handle = context;
91 struct acpi_object_list arg_list;
92 union acpi_object arg;
93 acpi_status status = AE_OK;
95 if (acpi_bus_get_device(handle, &device))
101 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
102 "Hot-removing device %s...\n", dev_name(&device->dev)));
104 if (acpi_bus_trim(device, 1)) {
105 printk(KERN_ERR PREFIX
106 "Removing device failed\n");
110 /* power off device */
111 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
112 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
113 printk(KERN_WARNING PREFIX
114 "Power-off device failed\n");
116 if (device->flags.lockable) {
118 arg_list.pointer = &arg;
119 arg.type = ACPI_TYPE_INTEGER;
120 arg.integer.value = 0;
121 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
125 arg_list.pointer = &arg;
126 arg.type = ACPI_TYPE_INTEGER;
127 arg.integer.value = 1;
132 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
133 if (ACPI_FAILURE(status))
134 printk(KERN_WARNING PREFIX
135 "Eject device failed\n");
141 acpi_eject_store(struct device *d, struct device_attribute *attr,
142 const char *buf, size_t count)
146 acpi_object_type type = 0;
147 struct acpi_device *acpi_device = to_acpi_device(d);
149 if ((!count) || (buf[0] != '1')) {
153 if (acpi_device->driver == NULL) {
158 status = acpi_get_type(acpi_device->handle, &type);
159 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
164 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, acpi_device->handle);
169 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
172 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
173 struct acpi_device *acpi_dev = to_acpi_device(dev);
175 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
177 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
180 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
181 struct acpi_device *acpi_dev = to_acpi_device(dev);
182 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
185 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
189 result = sprintf(buf, "%s\n", (char*)path.pointer);
194 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
196 static int acpi_device_setup_files(struct acpi_device *dev)
203 * Devices gotten from FADT don't have a "path" attribute
206 result = device_create_file(&dev->dev, &dev_attr_path);
211 if (!list_empty(&dev->pnp.ids)) {
212 result = device_create_file(&dev->dev, &dev_attr_hid);
216 result = device_create_file(&dev->dev, &dev_attr_modalias);
222 * If device has _EJ0, 'eject' file is created that is used to trigger
223 * hot-removal function from userland.
225 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
226 if (ACPI_SUCCESS(status))
227 result = device_create_file(&dev->dev, &dev_attr_eject);
232 static void acpi_device_remove_files(struct acpi_device *dev)
238 * If device has _EJ0, 'eject' file is created that is used to trigger
239 * hot-removal function from userland.
241 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
242 if (ACPI_SUCCESS(status))
243 device_remove_file(&dev->dev, &dev_attr_eject);
245 device_remove_file(&dev->dev, &dev_attr_modalias);
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 (list_empty(&acpi_dev->pnp.ids))
330 if (add_uevent_var(env, "MODALIAS="))
332 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
333 sizeof(env->buf) - env->buflen);
334 if (len >= (sizeof(env->buf) - env->buflen))
340 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
342 struct acpi_device *device = data;
344 device->driver->ops.notify(device, event);
347 static acpi_status acpi_device_notify_fixed(void *data)
349 struct acpi_device *device = data;
351 /* Fixed hardware devices have no handles */
352 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
356 static int acpi_device_install_notify_handler(struct acpi_device *device)
360 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
362 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
363 acpi_device_notify_fixed,
365 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
367 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
368 acpi_device_notify_fixed,
371 status = acpi_install_notify_handler(device->handle,
376 if (ACPI_FAILURE(status))
381 static void acpi_device_remove_notify_handler(struct acpi_device *device)
383 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
384 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
385 acpi_device_notify_fixed);
386 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
387 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
388 acpi_device_notify_fixed);
390 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
394 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
395 static int acpi_start_single_object(struct acpi_device *);
396 static int acpi_device_probe(struct device * dev)
398 struct acpi_device *acpi_dev = to_acpi_device(dev);
399 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
402 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
404 if (acpi_dev->bus_ops.acpi_op_start)
405 acpi_start_single_object(acpi_dev);
407 if (acpi_drv->ops.notify) {
408 ret = acpi_device_install_notify_handler(acpi_dev);
410 if (acpi_drv->ops.remove)
411 acpi_drv->ops.remove(acpi_dev,
412 acpi_dev->removal_type);
417 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
418 "Found driver [%s] for device [%s]\n",
419 acpi_drv->name, acpi_dev->pnp.bus_id));
425 static int acpi_device_remove(struct device * dev)
427 struct acpi_device *acpi_dev = to_acpi_device(dev);
428 struct acpi_driver *acpi_drv = acpi_dev->driver;
431 if (acpi_drv->ops.notify)
432 acpi_device_remove_notify_handler(acpi_dev);
433 if (acpi_drv->ops.remove)
434 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
436 acpi_dev->driver = NULL;
437 acpi_dev->driver_data = NULL;
443 struct bus_type acpi_bus_type = {
445 .suspend = acpi_device_suspend,
446 .resume = acpi_device_resume,
447 .match = acpi_bus_match,
448 .probe = acpi_device_probe,
449 .remove = acpi_device_remove,
450 .uevent = acpi_device_uevent,
453 static int acpi_device_register(struct acpi_device *device)
456 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
462 * Link this device to its parent and siblings.
464 INIT_LIST_HEAD(&device->children);
465 INIT_LIST_HEAD(&device->node);
466 INIT_LIST_HEAD(&device->wakeup_list);
468 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
470 printk(KERN_ERR PREFIX "Memory allocation error\n");
474 mutex_lock(&acpi_device_lock);
476 * Find suitable bus_id and instance number in acpi_bus_id_list
477 * If failed, create one and link it into acpi_bus_id_list
479 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
480 if (!strcmp(acpi_device_bus_id->bus_id,
481 acpi_device_hid(device))) {
482 acpi_device_bus_id->instance_no++;
489 acpi_device_bus_id = new_bus_id;
490 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
491 acpi_device_bus_id->instance_no = 0;
492 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
494 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
497 list_add_tail(&device->node, &device->parent->children);
499 if (device->wakeup.flags.valid)
500 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
501 mutex_unlock(&acpi_device_lock);
504 device->dev.parent = &device->parent->dev;
505 device->dev.bus = &acpi_bus_type;
506 device->dev.release = &acpi_device_release;
507 result = device_register(&device->dev);
509 dev_err(&device->dev, "Error registering device\n");
513 result = acpi_device_setup_files(device);
515 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
516 dev_name(&device->dev));
518 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
521 mutex_lock(&acpi_device_lock);
523 list_del(&device->node);
524 list_del(&device->wakeup_list);
525 mutex_unlock(&acpi_device_lock);
529 static void acpi_device_unregister(struct acpi_device *device, int type)
531 mutex_lock(&acpi_device_lock);
533 list_del(&device->node);
535 list_del(&device->wakeup_list);
536 mutex_unlock(&acpi_device_lock);
538 acpi_detach_data(device->handle, acpi_bus_data_handler);
540 acpi_device_remove_files(device);
541 device_unregister(&device->dev);
544 /* --------------------------------------------------------------------------
546 -------------------------------------------------------------------------- */
548 * acpi_bus_driver_init - add a device to a driver
549 * @device: the device to add and initialize
550 * @driver: driver for the device
552 * Used to initialize a device via its device driver. Called whenever a
553 * driver is bound to a device. Invokes the driver's add() ops.
556 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
560 if (!device || !driver)
563 if (!driver->ops.add)
566 result = driver->ops.add(device);
568 device->driver = NULL;
569 device->driver_data = NULL;
573 device->driver = driver;
576 * TBD - Configuration Management: Assign resources to device based
577 * upon possible configuration and currently allocated resources.
580 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
581 "Driver successfully bound to device\n"));
585 static int acpi_start_single_object(struct acpi_device *device)
588 struct acpi_driver *driver;
591 if (!(driver = device->driver))
594 if (driver->ops.start) {
595 result = driver->ops.start(device);
596 if (result && driver->ops.remove)
597 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
604 * acpi_bus_register_driver - register a driver with the ACPI bus
605 * @driver: driver being registered
607 * Registers a driver with the ACPI bus. Searches the namespace for all
608 * devices that match the driver's criteria and binds. Returns zero for
609 * success or a negative error status for failure.
611 int acpi_bus_register_driver(struct acpi_driver *driver)
617 driver->drv.name = driver->name;
618 driver->drv.bus = &acpi_bus_type;
619 driver->drv.owner = driver->owner;
621 ret = driver_register(&driver->drv);
625 EXPORT_SYMBOL(acpi_bus_register_driver);
628 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
629 * @driver: driver to unregister
631 * Unregisters a driver with the ACPI bus. Searches the namespace for all
632 * devices that match the driver's criteria and unbinds.
634 void acpi_bus_unregister_driver(struct acpi_driver *driver)
636 driver_unregister(&driver->drv);
639 EXPORT_SYMBOL(acpi_bus_unregister_driver);
641 /* --------------------------------------------------------------------------
643 -------------------------------------------------------------------------- */
644 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
648 struct acpi_device *device;
651 * Fixed hardware devices do not appear in the namespace and do not
652 * have handles, but we fabricate acpi_devices for them, so we have
653 * to deal with them specially.
659 status = acpi_get_parent(handle, &handle);
660 if (status == AE_NULL_ENTRY)
662 if (ACPI_FAILURE(status))
665 ret = acpi_bus_get_device(handle, &device);
672 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
676 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
677 union acpi_object *obj;
679 status = acpi_get_handle(handle, "_EJD", &tmp);
680 if (ACPI_FAILURE(status))
683 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
684 if (ACPI_SUCCESS(status)) {
685 obj = buffer.pointer;
686 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
688 kfree(buffer.pointer);
692 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
694 void acpi_bus_data_handler(acpi_handle handle, void *context)
702 static int acpi_bus_get_perf_flags(struct acpi_device *device)
704 device->performance.state = ACPI_STATE_UNKNOWN;
709 acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
710 union acpi_object *package)
713 union acpi_object *element = NULL;
715 if (!device || !package || (package->package.count < 2))
716 return AE_BAD_PARAMETER;
718 element = &(package->package.elements[0]);
720 return AE_BAD_PARAMETER;
721 if (element->type == ACPI_TYPE_PACKAGE) {
722 if ((element->package.count < 2) ||
723 (element->package.elements[0].type !=
724 ACPI_TYPE_LOCAL_REFERENCE)
725 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
727 device->wakeup.gpe_device =
728 element->package.elements[0].reference.handle;
729 device->wakeup.gpe_number =
730 (u32) element->package.elements[1].integer.value;
731 } else if (element->type == ACPI_TYPE_INTEGER) {
732 device->wakeup.gpe_number = element->integer.value;
736 element = &(package->package.elements[1]);
737 if (element->type != ACPI_TYPE_INTEGER) {
740 device->wakeup.sleep_state = element->integer.value;
742 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
745 device->wakeup.resources.count = package->package.count - 2;
746 for (i = 0; i < device->wakeup.resources.count; i++) {
747 element = &(package->package.elements[i + 2]);
748 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
751 device->wakeup.resources.handles[i] = element->reference.handle;
754 acpi_gpe_can_wake(device->wakeup.gpe_device, device->wakeup.gpe_number);
759 static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
761 struct acpi_device_id button_device_ids[] = {
768 acpi_event_status event_status;
770 device->wakeup.run_wake_count = 0;
771 device->wakeup.flags.notifier_present = 0;
773 /* Power button, Lid switch always enable wakeup */
774 if (!acpi_match_device_ids(device, button_device_ids)) {
775 device->wakeup.flags.run_wake = 1;
776 device->wakeup.flags.always_enabled = 1;
780 status = acpi_get_gpe_status(device->wakeup.gpe_device,
781 device->wakeup.gpe_number,
784 device->wakeup.flags.run_wake =
785 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
788 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
790 acpi_status status = 0;
791 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
792 union acpi_object *package = NULL;
796 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
797 if (ACPI_FAILURE(status)) {
798 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
802 package = (union acpi_object *)buffer.pointer;
803 status = acpi_bus_extract_wakeup_device_power_package(device, package);
804 if (ACPI_FAILURE(status)) {
805 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
809 kfree(buffer.pointer);
811 device->wakeup.flags.valid = 1;
812 device->wakeup.prepare_count = 0;
813 acpi_bus_set_run_wake_flags(device);
814 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
815 * system for the ACPI device with the _PRW object.
816 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
817 * So it is necessary to call _DSW object first. Only when it is not
818 * present will the _PSW object used.
820 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
822 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
823 "error in _DSW or _PSW evaluation\n"));
826 if (ACPI_FAILURE(status))
827 device->flags.wake_capable = 0;
831 static int acpi_bus_get_power_flags(struct acpi_device *device)
833 acpi_status status = 0;
834 acpi_handle handle = NULL;
839 * Power Management Flags
841 status = acpi_get_handle(device->handle, "_PSC", &handle);
842 if (ACPI_SUCCESS(status))
843 device->power.flags.explicit_get = 1;
844 status = acpi_get_handle(device->handle, "_IRC", &handle);
845 if (ACPI_SUCCESS(status))
846 device->power.flags.inrush_current = 1;
849 * Enumerate supported power management states
851 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
852 struct acpi_device_power_state *ps = &device->power.states[i];
853 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
855 /* Evaluate "_PRx" to se if power resources are referenced */
856 acpi_evaluate_reference(device->handle, object_name, NULL,
858 if (ps->resources.count) {
859 device->power.flags.power_resources = 1;
863 /* Evaluate "_PSx" to see if we can do explicit sets */
864 object_name[2] = 'S';
865 status = acpi_get_handle(device->handle, object_name, &handle);
866 if (ACPI_SUCCESS(status)) {
867 ps->flags.explicit_set = 1;
871 /* State is valid if we have some power control */
872 if (ps->resources.count || ps->flags.explicit_set)
875 ps->power = -1; /* Unknown - driver assigned */
876 ps->latency = -1; /* Unknown - driver assigned */
879 /* Set defaults for D0 and D3 states (always valid) */
880 device->power.states[ACPI_STATE_D0].flags.valid = 1;
881 device->power.states[ACPI_STATE_D0].power = 100;
882 device->power.states[ACPI_STATE_D3].flags.valid = 1;
883 device->power.states[ACPI_STATE_D3].power = 0;
885 /* TBD: System wake support and resource requirements. */
887 device->power.state = ACPI_STATE_UNKNOWN;
888 acpi_bus_get_power(device->handle, &(device->power.state));
893 static int acpi_bus_get_flags(struct acpi_device *device)
895 acpi_status status = AE_OK;
896 acpi_handle temp = NULL;
899 /* Presence of _STA indicates 'dynamic_status' */
900 status = acpi_get_handle(device->handle, "_STA", &temp);
901 if (ACPI_SUCCESS(status))
902 device->flags.dynamic_status = 1;
904 /* Presence of _RMV indicates 'removable' */
905 status = acpi_get_handle(device->handle, "_RMV", &temp);
906 if (ACPI_SUCCESS(status))
907 device->flags.removable = 1;
909 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
910 status = acpi_get_handle(device->handle, "_EJD", &temp);
911 if (ACPI_SUCCESS(status))
912 device->flags.ejectable = 1;
914 status = acpi_get_handle(device->handle, "_EJ0", &temp);
915 if (ACPI_SUCCESS(status))
916 device->flags.ejectable = 1;
919 /* Presence of _LCK indicates 'lockable' */
920 status = acpi_get_handle(device->handle, "_LCK", &temp);
921 if (ACPI_SUCCESS(status))
922 device->flags.lockable = 1;
924 /* Presence of _PS0|_PR0 indicates 'power manageable' */
925 status = acpi_get_handle(device->handle, "_PS0", &temp);
926 if (ACPI_FAILURE(status))
927 status = acpi_get_handle(device->handle, "_PR0", &temp);
928 if (ACPI_SUCCESS(status))
929 device->flags.power_manageable = 1;
931 /* Presence of _PRW indicates wake capable */
932 status = acpi_get_handle(device->handle, "_PRW", &temp);
933 if (ACPI_SUCCESS(status))
934 device->flags.wake_capable = 1;
936 /* TBD: Performance management */
941 static void acpi_device_get_busid(struct acpi_device *device)
943 char bus_id[5] = { '?', 0 };
944 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
950 * The device's Bus ID is simply the object name.
951 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
953 if (ACPI_IS_ROOT_DEVICE(device)) {
954 strcpy(device->pnp.bus_id, "ACPI");
958 switch (device->device_type) {
959 case ACPI_BUS_TYPE_POWER_BUTTON:
960 strcpy(device->pnp.bus_id, "PWRF");
962 case ACPI_BUS_TYPE_SLEEP_BUTTON:
963 strcpy(device->pnp.bus_id, "SLPF");
966 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
967 /* Clean up trailing underscores (if any) */
968 for (i = 3; i > 1; i--) {
969 if (bus_id[i] == '_')
974 strcpy(device->pnp.bus_id, bus_id);
980 * acpi_bay_match - see if a device is an ejectable driver bay
982 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
983 * then we can safely call it an ejectable drive bay
985 static int acpi_bay_match(struct acpi_device *device){
991 handle = device->handle;
993 status = acpi_get_handle(handle, "_EJ0", &tmp);
994 if (ACPI_FAILURE(status))
997 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
998 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
999 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1000 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1003 if (acpi_get_parent(handle, &phandle))
1006 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1007 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1008 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1009 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1016 * acpi_dock_match - see if a device has a _DCK method
1018 static int acpi_dock_match(struct acpi_device *device)
1021 return acpi_get_handle(device->handle, "_DCK", &tmp);
1024 char *acpi_device_hid(struct acpi_device *device)
1026 struct acpi_hardware_id *hid;
1028 if (list_empty(&device->pnp.ids))
1031 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1034 EXPORT_SYMBOL(acpi_device_hid);
1036 static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1038 struct acpi_hardware_id *id;
1040 id = kmalloc(sizeof(*id), GFP_KERNEL);
1044 id->id = kmalloc(strlen(dev_id) + 1, GFP_KERNEL);
1050 strcpy(id->id, dev_id);
1051 list_add_tail(&id->list, &device->pnp.ids);
1055 * Old IBM workstations have a DSDT bug wherein the SMBus object
1056 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1057 * prefix. Work around this.
1059 static int acpi_ibm_smbus_match(struct acpi_device *device)
1061 acpi_handle h_dummy;
1062 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1065 if (!dmi_name_in_vendors("IBM"))
1068 /* Look for SMBS object */
1069 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1073 if (strcmp("SMBS", path.pointer)) {
1078 /* Does it have the necessary (but misnamed) methods? */
1080 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1081 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1082 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1085 kfree(path.pointer);
1089 static void acpi_device_set_id(struct acpi_device *device)
1092 struct acpi_device_info *info;
1093 struct acpica_device_id_list *cid_list;
1096 switch (device->device_type) {
1097 case ACPI_BUS_TYPE_DEVICE:
1098 if (ACPI_IS_ROOT_DEVICE(device)) {
1099 acpi_add_id(device, ACPI_SYSTEM_HID);
1103 status = acpi_get_object_info(device->handle, &info);
1104 if (ACPI_FAILURE(status)) {
1105 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1109 if (info->valid & ACPI_VALID_HID)
1110 acpi_add_id(device, info->hardware_id.string);
1111 if (info->valid & ACPI_VALID_CID) {
1112 cid_list = &info->compatible_id_list;
1113 for (i = 0; i < cid_list->count; i++)
1114 acpi_add_id(device, cid_list->ids[i].string);
1116 if (info->valid & ACPI_VALID_ADR) {
1117 device->pnp.bus_address = info->address;
1118 device->flags.bus_address = 1;
1124 * Some devices don't reliably have _HIDs & _CIDs, so add
1125 * synthetic HIDs to make sure drivers can find them.
1127 if (acpi_is_video_device(device))
1128 acpi_add_id(device, ACPI_VIDEO_HID);
1129 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1130 acpi_add_id(device, ACPI_BAY_HID);
1131 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1132 acpi_add_id(device, ACPI_DOCK_HID);
1133 else if (!acpi_ibm_smbus_match(device))
1134 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
1135 else if (!acpi_device_hid(device) &&
1136 ACPI_IS_ROOT_DEVICE(device->parent)) {
1137 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1138 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1139 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1143 case ACPI_BUS_TYPE_POWER:
1144 acpi_add_id(device, ACPI_POWER_HID);
1146 case ACPI_BUS_TYPE_PROCESSOR:
1147 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
1149 case ACPI_BUS_TYPE_THERMAL:
1150 acpi_add_id(device, ACPI_THERMAL_HID);
1152 case ACPI_BUS_TYPE_POWER_BUTTON:
1153 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
1155 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1156 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
1161 static int acpi_device_set_context(struct acpi_device *device)
1168 * Attach this 'struct acpi_device' to the ACPI object. This makes
1169 * resolutions from handle->device very efficient. Fixed hardware
1170 * devices have no handles, so we skip them.
1172 if (!device->handle)
1175 status = acpi_attach_data(device->handle,
1176 acpi_bus_data_handler, device);
1177 if (ACPI_SUCCESS(status))
1180 printk(KERN_ERR PREFIX "Error attaching device data\n");
1184 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1189 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1190 device_release_driver(&dev->dev);
1196 * unbind _ADR-Based Devices when hot removal
1198 if (dev->flags.bus_address) {
1199 if ((dev->parent) && (dev->parent->ops.unbind))
1200 dev->parent->ops.unbind(dev);
1202 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1207 static int acpi_add_single_object(struct acpi_device **child,
1208 acpi_handle handle, int type,
1209 unsigned long long sta,
1210 struct acpi_bus_ops *ops)
1213 struct acpi_device *device;
1214 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1216 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1218 printk(KERN_ERR PREFIX "Memory allocation error\n");
1222 INIT_LIST_HEAD(&device->pnp.ids);
1223 device->device_type = type;
1224 device->handle = handle;
1225 device->parent = acpi_bus_get_parent(handle);
1226 device->bus_ops = *ops; /* workround for not call .start */
1227 STRUCT_TO_INT(device->status) = sta;
1229 acpi_device_get_busid(device);
1234 * Note that we only look for object handles -- cannot evaluate objects
1235 * until we know the device is present and properly initialized.
1237 result = acpi_bus_get_flags(device);
1244 * TBD: Synch with Core's enumeration/initialization process.
1246 acpi_device_set_id(device);
1252 if (device->flags.power_manageable) {
1253 result = acpi_bus_get_power_flags(device);
1259 * Wakeup device management
1260 *-----------------------
1262 if (device->flags.wake_capable) {
1263 result = acpi_bus_get_wakeup_device_flags(device);
1269 * Performance Management
1270 * ----------------------
1272 if (device->flags.performance_manageable) {
1273 result = acpi_bus_get_perf_flags(device);
1278 if ((result = acpi_device_set_context(device)))
1281 result = acpi_device_register(device);
1284 * Bind _ADR-Based Devices when hot add
1286 if (device->flags.bus_address) {
1287 if (device->parent && device->parent->ops.bind)
1288 device->parent->ops.bind(device);
1293 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1294 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1295 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1296 (char *) buffer.pointer,
1297 device->parent ? dev_name(&device->parent->dev) :
1299 kfree(buffer.pointer);
1302 acpi_device_release(&device->dev);
1307 #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1308 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1310 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1311 unsigned long long *sta)
1314 acpi_object_type acpi_type;
1316 status = acpi_get_type(handle, &acpi_type);
1317 if (ACPI_FAILURE(status))
1320 switch (acpi_type) {
1321 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1322 case ACPI_TYPE_DEVICE:
1323 *type = ACPI_BUS_TYPE_DEVICE;
1324 status = acpi_bus_get_status_handle(handle, sta);
1325 if (ACPI_FAILURE(status))
1328 case ACPI_TYPE_PROCESSOR:
1329 *type = ACPI_BUS_TYPE_PROCESSOR;
1330 status = acpi_bus_get_status_handle(handle, sta);
1331 if (ACPI_FAILURE(status))
1334 case ACPI_TYPE_THERMAL:
1335 *type = ACPI_BUS_TYPE_THERMAL;
1336 *sta = ACPI_STA_DEFAULT;
1338 case ACPI_TYPE_POWER:
1339 *type = ACPI_BUS_TYPE_POWER;
1340 *sta = ACPI_STA_DEFAULT;
1349 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1350 void *context, void **return_value)
1352 struct acpi_bus_ops *ops = context;
1354 unsigned long long sta;
1355 struct acpi_device *device;
1359 result = acpi_bus_type_and_status(handle, &type, &sta);
1363 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1364 !(sta & ACPI_STA_DEVICE_FUNCTIONING))
1365 return AE_CTRL_DEPTH;
1368 * We may already have an acpi_device from a previous enumeration. If
1369 * so, we needn't add it again, but we may still have to start it.
1372 acpi_bus_get_device(handle, &device);
1373 if (ops->acpi_op_add && !device)
1374 acpi_add_single_object(&device, handle, type, sta, ops);
1377 return AE_CTRL_DEPTH;
1379 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1380 status = acpi_start_single_object(device);
1381 if (ACPI_FAILURE(status))
1382 return AE_CTRL_DEPTH;
1386 *return_value = device;
1390 static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1391 struct acpi_device **child)
1394 void *device = NULL;
1396 status = acpi_bus_check_add(handle, 0, ops, &device);
1397 if (ACPI_SUCCESS(status))
1398 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1399 acpi_bus_check_add, NULL, ops, &device);
1411 * acpi_bus_add and acpi_bus_start
1413 * scan a given ACPI tree and (probably recently hot-plugged)
1414 * create and add or starts found devices.
1416 * If no devices were found -ENODEV is returned which does not
1417 * mean that this is a real error, there just have been no suitable
1418 * ACPI objects in the table trunk from which the kernel could create
1419 * a device and add/start an appropriate driver.
1423 acpi_bus_add(struct acpi_device **child,
1424 struct acpi_device *parent, acpi_handle handle, int type)
1426 struct acpi_bus_ops ops;
1428 memset(&ops, 0, sizeof(ops));
1429 ops.acpi_op_add = 1;
1431 return acpi_bus_scan(handle, &ops, child);
1433 EXPORT_SYMBOL(acpi_bus_add);
1435 int acpi_bus_start(struct acpi_device *device)
1437 struct acpi_bus_ops ops;
1442 memset(&ops, 0, sizeof(ops));
1443 ops.acpi_op_start = 1;
1445 return acpi_bus_scan(device->handle, &ops, NULL);
1447 EXPORT_SYMBOL(acpi_bus_start);
1449 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1452 struct acpi_device *parent, *child;
1453 acpi_handle phandle, chandle;
1454 acpi_object_type type;
1459 phandle = start->handle;
1460 child = chandle = NULL;
1462 while ((level > 0) && parent && (!err)) {
1463 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1467 * If this scope is exhausted then move our way back up.
1469 if (ACPI_FAILURE(status)) {
1472 acpi_get_parent(phandle, &phandle);
1474 parent = parent->parent;
1477 err = acpi_bus_remove(child, rmdevice);
1479 err = acpi_bus_remove(child, 1);
1484 status = acpi_get_type(chandle, &type);
1485 if (ACPI_FAILURE(status)) {
1489 * If there is a device corresponding to chandle then
1490 * parse it (depth-first).
1492 if (acpi_bus_get_device(chandle, &child) == 0) {
1502 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1504 static int acpi_bus_scan_fixed(void)
1507 struct acpi_device *device = NULL;
1508 struct acpi_bus_ops ops;
1510 memset(&ops, 0, sizeof(ops));
1511 ops.acpi_op_add = 1;
1512 ops.acpi_op_start = 1;
1515 * Enumerate all fixed-feature devices.
1517 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
1518 result = acpi_add_single_object(&device, NULL,
1519 ACPI_BUS_TYPE_POWER_BUTTON,
1524 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1525 result = acpi_add_single_object(&device, NULL,
1526 ACPI_BUS_TYPE_SLEEP_BUTTON,
1534 int __init acpi_scan_init(void)
1537 struct acpi_bus_ops ops;
1539 memset(&ops, 0, sizeof(ops));
1540 ops.acpi_op_add = 1;
1541 ops.acpi_op_start = 1;
1543 result = bus_register(&acpi_bus_type);
1545 /* We don't want to quit even if we failed to add suspend/resume */
1546 printk(KERN_ERR PREFIX "Could not register bus type\n");
1550 * Enumerate devices in the ACPI namespace.
1552 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
1555 result = acpi_bus_scan_fixed();
1558 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);