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);
173 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
174 struct acpi_device *acpi_dev = to_acpi_device(dev);
175 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
178 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
182 result = sprintf(buf, "%s\n", (char*)path.pointer);
187 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
189 static int acpi_device_setup_files(struct acpi_device *dev)
196 * Devices gotten from FADT don't have a "path" attribute
199 result = device_create_file(&dev->dev, &dev_attr_path);
204 result = device_create_file(&dev->dev, &dev_attr_hid);
208 result = device_create_file(&dev->dev, &dev_attr_modalias);
213 * If device has _EJ0, 'eject' file is created that is used to trigger
214 * hot-removal function from userland.
216 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
217 if (ACPI_SUCCESS(status))
218 result = device_create_file(&dev->dev, &dev_attr_eject);
223 static void acpi_device_remove_files(struct acpi_device *dev)
229 * If device has _EJ0, 'eject' file is created that is used to trigger
230 * hot-removal function from userland.
232 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
233 if (ACPI_SUCCESS(status))
234 device_remove_file(&dev->dev, &dev_attr_eject);
236 device_remove_file(&dev->dev, &dev_attr_modalias);
237 device_remove_file(&dev->dev, &dev_attr_hid);
239 device_remove_file(&dev->dev, &dev_attr_path);
241 /* --------------------------------------------------------------------------
243 -------------------------------------------------------------------------- */
245 int acpi_match_device_ids(struct acpi_device *device,
246 const struct acpi_device_id *ids)
248 const struct acpi_device_id *id;
249 struct acpi_hardware_id *hwid;
252 * If the device is not present, it is unnecessary to load device
255 if (!device->status.present)
258 for (id = ids; id->id[0]; id++)
259 list_for_each_entry(hwid, &device->pnp.ids, list)
260 if (!strcmp((char *) id->id, hwid->id))
265 EXPORT_SYMBOL(acpi_match_device_ids);
267 static void acpi_free_ids(struct acpi_device *device)
269 struct acpi_hardware_id *id, *tmp;
271 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
277 static void acpi_device_release(struct device *dev)
279 struct acpi_device *acpi_dev = to_acpi_device(dev);
281 acpi_free_ids(acpi_dev);
285 static int acpi_device_suspend(struct device *dev, pm_message_t state)
287 struct acpi_device *acpi_dev = to_acpi_device(dev);
288 struct acpi_driver *acpi_drv = acpi_dev->driver;
290 if (acpi_drv && acpi_drv->ops.suspend)
291 return acpi_drv->ops.suspend(acpi_dev, state);
295 static int acpi_device_resume(struct device *dev)
297 struct acpi_device *acpi_dev = to_acpi_device(dev);
298 struct acpi_driver *acpi_drv = acpi_dev->driver;
300 if (acpi_drv && acpi_drv->ops.resume)
301 return acpi_drv->ops.resume(acpi_dev);
305 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
307 struct acpi_device *acpi_dev = to_acpi_device(dev);
308 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
310 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
313 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
315 struct acpi_device *acpi_dev = to_acpi_device(dev);
318 if (add_uevent_var(env, "MODALIAS="))
320 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
321 sizeof(env->buf) - env->buflen);
322 if (len >= (sizeof(env->buf) - env->buflen))
328 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
330 struct acpi_device *device = data;
332 device->driver->ops.notify(device, event);
335 static acpi_status acpi_device_notify_fixed(void *data)
337 struct acpi_device *device = data;
339 /* Fixed hardware devices have no handles */
340 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
344 static int acpi_device_install_notify_handler(struct acpi_device *device)
348 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
350 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
351 acpi_device_notify_fixed,
353 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
355 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
356 acpi_device_notify_fixed,
359 status = acpi_install_notify_handler(device->handle,
364 if (ACPI_FAILURE(status))
369 static void acpi_device_remove_notify_handler(struct acpi_device *device)
371 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
372 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
373 acpi_device_notify_fixed);
374 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
375 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
376 acpi_device_notify_fixed);
378 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
382 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
383 static int acpi_start_single_object(struct acpi_device *);
384 static int acpi_device_probe(struct device * dev)
386 struct acpi_device *acpi_dev = to_acpi_device(dev);
387 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
390 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
392 if (acpi_dev->bus_ops.acpi_op_start)
393 acpi_start_single_object(acpi_dev);
395 if (acpi_drv->ops.notify) {
396 ret = acpi_device_install_notify_handler(acpi_dev);
398 if (acpi_drv->ops.remove)
399 acpi_drv->ops.remove(acpi_dev,
400 acpi_dev->removal_type);
405 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
406 "Found driver [%s] for device [%s]\n",
407 acpi_drv->name, acpi_dev->pnp.bus_id));
413 static int acpi_device_remove(struct device * dev)
415 struct acpi_device *acpi_dev = to_acpi_device(dev);
416 struct acpi_driver *acpi_drv = acpi_dev->driver;
419 if (acpi_drv->ops.notify)
420 acpi_device_remove_notify_handler(acpi_dev);
421 if (acpi_drv->ops.remove)
422 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
424 acpi_dev->driver = NULL;
425 acpi_dev->driver_data = NULL;
431 struct bus_type acpi_bus_type = {
433 .suspend = acpi_device_suspend,
434 .resume = acpi_device_resume,
435 .match = acpi_bus_match,
436 .probe = acpi_device_probe,
437 .remove = acpi_device_remove,
438 .uevent = acpi_device_uevent,
441 static int acpi_device_register(struct acpi_device *device)
444 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
450 * Link this device to its parent and siblings.
452 INIT_LIST_HEAD(&device->children);
453 INIT_LIST_HEAD(&device->node);
454 INIT_LIST_HEAD(&device->wakeup_list);
456 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
458 printk(KERN_ERR PREFIX "Memory allocation error\n");
462 mutex_lock(&acpi_device_lock);
464 * Find suitable bus_id and instance number in acpi_bus_id_list
465 * If failed, create one and link it into acpi_bus_id_list
467 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
468 if (!strcmp(acpi_device_bus_id->bus_id,
469 acpi_device_hid(device))) {
470 acpi_device_bus_id->instance_no++;
477 acpi_device_bus_id = new_bus_id;
478 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
479 acpi_device_bus_id->instance_no = 0;
480 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
482 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
485 list_add_tail(&device->node, &device->parent->children);
487 if (device->wakeup.flags.valid)
488 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
489 mutex_unlock(&acpi_device_lock);
492 device->dev.parent = &device->parent->dev;
493 device->dev.bus = &acpi_bus_type;
494 device->dev.release = &acpi_device_release;
495 result = device_register(&device->dev);
497 dev_err(&device->dev, "Error registering device\n");
501 result = acpi_device_setup_files(device);
503 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
504 dev_name(&device->dev));
506 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
509 mutex_lock(&acpi_device_lock);
511 list_del(&device->node);
512 list_del(&device->wakeup_list);
513 mutex_unlock(&acpi_device_lock);
517 static void acpi_device_unregister(struct acpi_device *device, int type)
519 mutex_lock(&acpi_device_lock);
521 list_del(&device->node);
523 list_del(&device->wakeup_list);
524 mutex_unlock(&acpi_device_lock);
526 acpi_detach_data(device->handle, acpi_bus_data_handler);
528 acpi_device_remove_files(device);
529 device_unregister(&device->dev);
532 /* --------------------------------------------------------------------------
534 -------------------------------------------------------------------------- */
536 * acpi_bus_driver_init - add a device to a driver
537 * @device: the device to add and initialize
538 * @driver: driver for the device
540 * Used to initialize a device via its device driver. Called whenever a
541 * driver is bound to a device. Invokes the driver's add() ops.
544 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
548 if (!device || !driver)
551 if (!driver->ops.add)
554 result = driver->ops.add(device);
556 device->driver = NULL;
557 device->driver_data = NULL;
561 device->driver = driver;
564 * TBD - Configuration Management: Assign resources to device based
565 * upon possible configuration and currently allocated resources.
568 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
569 "Driver successfully bound to device\n"));
573 static int acpi_start_single_object(struct acpi_device *device)
576 struct acpi_driver *driver;
579 if (!(driver = device->driver))
582 if (driver->ops.start) {
583 result = driver->ops.start(device);
584 if (result && driver->ops.remove)
585 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
592 * acpi_bus_register_driver - register a driver with the ACPI bus
593 * @driver: driver being registered
595 * Registers a driver with the ACPI bus. Searches the namespace for all
596 * devices that match the driver's criteria and binds. Returns zero for
597 * success or a negative error status for failure.
599 int acpi_bus_register_driver(struct acpi_driver *driver)
605 driver->drv.name = driver->name;
606 driver->drv.bus = &acpi_bus_type;
607 driver->drv.owner = driver->owner;
609 ret = driver_register(&driver->drv);
613 EXPORT_SYMBOL(acpi_bus_register_driver);
616 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
617 * @driver: driver to unregister
619 * Unregisters a driver with the ACPI bus. Searches the namespace for all
620 * devices that match the driver's criteria and unbinds.
622 void acpi_bus_unregister_driver(struct acpi_driver *driver)
624 driver_unregister(&driver->drv);
627 EXPORT_SYMBOL(acpi_bus_unregister_driver);
629 /* --------------------------------------------------------------------------
631 -------------------------------------------------------------------------- */
632 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
636 struct acpi_device *device;
639 * Fixed hardware devices do not appear in the namespace and do not
640 * have handles, but we fabricate acpi_devices for them, so we have
641 * to deal with them specially.
647 status = acpi_get_parent(handle, &handle);
648 if (status == AE_NULL_ENTRY)
650 if (ACPI_FAILURE(status))
653 ret = acpi_bus_get_device(handle, &device);
660 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
664 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
665 union acpi_object *obj;
667 status = acpi_get_handle(handle, "_EJD", &tmp);
668 if (ACPI_FAILURE(status))
671 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
672 if (ACPI_SUCCESS(status)) {
673 obj = buffer.pointer;
674 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
676 kfree(buffer.pointer);
680 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
682 void acpi_bus_data_handler(acpi_handle handle, void *context)
690 static int acpi_bus_get_perf_flags(struct acpi_device *device)
692 device->performance.state = ACPI_STATE_UNKNOWN;
697 acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
698 union acpi_object *package)
701 union acpi_object *element = NULL;
703 if (!device || !package || (package->package.count < 2))
704 return AE_BAD_PARAMETER;
706 element = &(package->package.elements[0]);
708 return AE_BAD_PARAMETER;
709 if (element->type == ACPI_TYPE_PACKAGE) {
710 if ((element->package.count < 2) ||
711 (element->package.elements[0].type !=
712 ACPI_TYPE_LOCAL_REFERENCE)
713 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
715 device->wakeup.gpe_device =
716 element->package.elements[0].reference.handle;
717 device->wakeup.gpe_number =
718 (u32) element->package.elements[1].integer.value;
719 } else if (element->type == ACPI_TYPE_INTEGER) {
720 device->wakeup.gpe_number = element->integer.value;
724 element = &(package->package.elements[1]);
725 if (element->type != ACPI_TYPE_INTEGER) {
728 device->wakeup.sleep_state = element->integer.value;
730 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
733 device->wakeup.resources.count = package->package.count - 2;
734 for (i = 0; i < device->wakeup.resources.count; i++) {
735 element = &(package->package.elements[i + 2]);
736 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
739 device->wakeup.resources.handles[i] = element->reference.handle;
745 static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
747 struct acpi_device_id button_device_ids[] = {
754 acpi_event_status event_status;
756 device->wakeup.run_wake_count = 0;
757 device->wakeup.flags.notifier_present = 0;
759 /* Power button, Lid switch always enable wakeup */
760 if (!acpi_match_device_ids(device, button_device_ids)) {
761 device->wakeup.flags.run_wake = 1;
762 device->wakeup.flags.always_enabled = 1;
766 status = acpi_get_gpe_status(NULL, device->wakeup.gpe_number,
767 ACPI_NOT_ISR, &event_status);
769 device->wakeup.flags.run_wake =
770 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
773 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
775 acpi_status status = 0;
776 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
777 union acpi_object *package = NULL;
781 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
782 if (ACPI_FAILURE(status)) {
783 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
787 package = (union acpi_object *)buffer.pointer;
788 status = acpi_bus_extract_wakeup_device_power_package(device, package);
789 if (ACPI_FAILURE(status)) {
790 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
794 kfree(buffer.pointer);
796 device->wakeup.flags.valid = 1;
797 device->wakeup.prepare_count = 0;
798 acpi_bus_set_run_wake_flags(device);
799 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
800 * system for the ACPI device with the _PRW object.
801 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
802 * So it is necessary to call _DSW object first. Only when it is not
803 * present will the _PSW object used.
805 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
807 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
808 "error in _DSW or _PSW evaluation\n"));
811 if (ACPI_FAILURE(status))
812 device->flags.wake_capable = 0;
816 static int acpi_bus_get_power_flags(struct acpi_device *device)
818 acpi_status status = 0;
819 acpi_handle handle = NULL;
824 * Power Management Flags
826 status = acpi_get_handle(device->handle, "_PSC", &handle);
827 if (ACPI_SUCCESS(status))
828 device->power.flags.explicit_get = 1;
829 status = acpi_get_handle(device->handle, "_IRC", &handle);
830 if (ACPI_SUCCESS(status))
831 device->power.flags.inrush_current = 1;
834 * Enumerate supported power management states
836 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
837 struct acpi_device_power_state *ps = &device->power.states[i];
838 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
840 /* Evaluate "_PRx" to se if power resources are referenced */
841 acpi_evaluate_reference(device->handle, object_name, NULL,
843 if (ps->resources.count) {
844 device->power.flags.power_resources = 1;
848 /* Evaluate "_PSx" to see if we can do explicit sets */
849 object_name[2] = 'S';
850 status = acpi_get_handle(device->handle, object_name, &handle);
851 if (ACPI_SUCCESS(status)) {
852 ps->flags.explicit_set = 1;
856 /* State is valid if we have some power control */
857 if (ps->resources.count || ps->flags.explicit_set)
860 ps->power = -1; /* Unknown - driver assigned */
861 ps->latency = -1; /* Unknown - driver assigned */
864 /* Set defaults for D0 and D3 states (always valid) */
865 device->power.states[ACPI_STATE_D0].flags.valid = 1;
866 device->power.states[ACPI_STATE_D0].power = 100;
867 device->power.states[ACPI_STATE_D3].flags.valid = 1;
868 device->power.states[ACPI_STATE_D3].power = 0;
870 /* TBD: System wake support and resource requirements. */
872 device->power.state = ACPI_STATE_UNKNOWN;
873 acpi_bus_get_power(device->handle, &(device->power.state));
878 static int acpi_bus_get_flags(struct acpi_device *device)
880 acpi_status status = AE_OK;
881 acpi_handle temp = NULL;
884 /* Presence of _STA indicates 'dynamic_status' */
885 status = acpi_get_handle(device->handle, "_STA", &temp);
886 if (ACPI_SUCCESS(status))
887 device->flags.dynamic_status = 1;
889 /* Presence of _RMV indicates 'removable' */
890 status = acpi_get_handle(device->handle, "_RMV", &temp);
891 if (ACPI_SUCCESS(status))
892 device->flags.removable = 1;
894 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
895 status = acpi_get_handle(device->handle, "_EJD", &temp);
896 if (ACPI_SUCCESS(status))
897 device->flags.ejectable = 1;
899 status = acpi_get_handle(device->handle, "_EJ0", &temp);
900 if (ACPI_SUCCESS(status))
901 device->flags.ejectable = 1;
904 /* Presence of _LCK indicates 'lockable' */
905 status = acpi_get_handle(device->handle, "_LCK", &temp);
906 if (ACPI_SUCCESS(status))
907 device->flags.lockable = 1;
909 /* Presence of _PS0|_PR0 indicates 'power manageable' */
910 status = acpi_get_handle(device->handle, "_PS0", &temp);
911 if (ACPI_FAILURE(status))
912 status = acpi_get_handle(device->handle, "_PR0", &temp);
913 if (ACPI_SUCCESS(status))
914 device->flags.power_manageable = 1;
916 /* Presence of _PRW indicates wake capable */
917 status = acpi_get_handle(device->handle, "_PRW", &temp);
918 if (ACPI_SUCCESS(status))
919 device->flags.wake_capable = 1;
921 /* TBD: Performance management */
926 static void acpi_device_get_busid(struct acpi_device *device)
928 char bus_id[5] = { '?', 0 };
929 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
935 * The device's Bus ID is simply the object name.
936 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
938 if (ACPI_IS_ROOT_DEVICE(device)) {
939 strcpy(device->pnp.bus_id, "ACPI");
943 switch (device->device_type) {
944 case ACPI_BUS_TYPE_POWER_BUTTON:
945 strcpy(device->pnp.bus_id, "PWRF");
947 case ACPI_BUS_TYPE_SLEEP_BUTTON:
948 strcpy(device->pnp.bus_id, "SLPF");
951 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
952 /* Clean up trailing underscores (if any) */
953 for (i = 3; i > 1; i--) {
954 if (bus_id[i] == '_')
959 strcpy(device->pnp.bus_id, bus_id);
965 * acpi_bay_match - see if a device is an ejectable driver bay
967 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
968 * then we can safely call it an ejectable drive bay
970 static int acpi_bay_match(struct acpi_device *device){
976 handle = device->handle;
978 status = acpi_get_handle(handle, "_EJ0", &tmp);
979 if (ACPI_FAILURE(status))
982 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
983 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
984 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
985 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
988 if (acpi_get_parent(handle, &phandle))
991 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
992 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
993 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
994 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1001 * acpi_dock_match - see if a device has a _DCK method
1003 static int acpi_dock_match(struct acpi_device *device)
1006 return acpi_get_handle(device->handle, "_DCK", &tmp);
1009 char *acpi_device_hid(struct acpi_device *device)
1011 struct acpi_hardware_id *hid;
1013 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1016 EXPORT_SYMBOL(acpi_device_hid);
1018 static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1020 struct acpi_hardware_id *id;
1022 id = kmalloc(sizeof(*id), GFP_KERNEL);
1026 id->id = kmalloc(strlen(dev_id) + 1, GFP_KERNEL);
1032 strcpy(id->id, dev_id);
1033 list_add_tail(&id->list, &device->pnp.ids);
1037 * Old IBM workstations have a DSDT bug wherein the SMBus object
1038 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1039 * prefix. Work around this.
1041 static int acpi_ibm_smbus_match(struct acpi_device *device)
1043 acpi_handle h_dummy;
1044 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1047 if (!dmi_name_in_vendors("IBM"))
1050 /* Look for SMBS object */
1051 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1055 if (strcmp("SMBS", path.pointer)) {
1060 /* Does it have the necessary (but misnamed) methods? */
1062 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1063 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1064 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1067 kfree(path.pointer);
1071 static void acpi_device_set_id(struct acpi_device *device)
1074 struct acpi_device_info *info;
1075 struct acpica_device_id_list *cid_list;
1078 switch (device->device_type) {
1079 case ACPI_BUS_TYPE_DEVICE:
1080 if (ACPI_IS_ROOT_DEVICE(device)) {
1081 acpi_add_id(device, ACPI_SYSTEM_HID);
1083 } else if (ACPI_IS_ROOT_DEVICE(device->parent)) {
1084 /* \_SB_, the only root-level namespace device */
1085 acpi_add_id(device, ACPI_BUS_HID);
1086 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1087 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1091 status = acpi_get_object_info(device->handle, &info);
1092 if (ACPI_FAILURE(status)) {
1093 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1097 if (info->valid & ACPI_VALID_HID)
1098 acpi_add_id(device, info->hardware_id.string);
1099 if (info->valid & ACPI_VALID_CID) {
1100 cid_list = &info->compatible_id_list;
1101 for (i = 0; i < cid_list->count; i++)
1102 acpi_add_id(device, cid_list->ids[i].string);
1104 if (info->valid & ACPI_VALID_ADR) {
1105 device->pnp.bus_address = info->address;
1106 device->flags.bus_address = 1;
1112 * Some devices don't reliably have _HIDs & _CIDs, so add
1113 * synthetic HIDs to make sure drivers can find them.
1115 if (acpi_is_video_device(device))
1116 acpi_add_id(device, ACPI_VIDEO_HID);
1117 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1118 acpi_add_id(device, ACPI_BAY_HID);
1119 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1120 acpi_add_id(device, ACPI_DOCK_HID);
1121 else if (!acpi_ibm_smbus_match(device))
1122 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
1125 case ACPI_BUS_TYPE_POWER:
1126 acpi_add_id(device, ACPI_POWER_HID);
1128 case ACPI_BUS_TYPE_PROCESSOR:
1129 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
1131 case ACPI_BUS_TYPE_THERMAL:
1132 acpi_add_id(device, ACPI_THERMAL_HID);
1134 case ACPI_BUS_TYPE_POWER_BUTTON:
1135 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
1137 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1138 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
1143 * We build acpi_devices for some objects that don't have _HID or _CID,
1144 * e.g., PCI bridges and slots. Drivers can't bind to these objects,
1145 * but we do use them indirectly by traversing the acpi_device tree.
1146 * This generic ID isn't useful for driver binding, but it provides
1147 * the useful property that "every acpi_device has an ID."
1149 if (list_empty(&device->pnp.ids))
1150 acpi_add_id(device, "device");
1153 static int acpi_device_set_context(struct acpi_device *device)
1160 * Attach this 'struct acpi_device' to the ACPI object. This makes
1161 * resolutions from handle->device very efficient. Fixed hardware
1162 * devices have no handles, so we skip them.
1164 if (!device->handle)
1167 status = acpi_attach_data(device->handle,
1168 acpi_bus_data_handler, device);
1169 if (ACPI_SUCCESS(status))
1172 printk(KERN_ERR PREFIX "Error attaching device data\n");
1176 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1181 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1182 device_release_driver(&dev->dev);
1188 * unbind _ADR-Based Devices when hot removal
1190 if (dev->flags.bus_address) {
1191 if ((dev->parent) && (dev->parent->ops.unbind))
1192 dev->parent->ops.unbind(dev);
1194 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1199 static int acpi_add_single_object(struct acpi_device **child,
1200 acpi_handle handle, int type,
1201 unsigned long long sta,
1202 struct acpi_bus_ops *ops)
1205 struct acpi_device *device;
1206 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1208 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1210 printk(KERN_ERR PREFIX "Memory allocation error\n");
1214 INIT_LIST_HEAD(&device->pnp.ids);
1215 device->device_type = type;
1216 device->handle = handle;
1217 device->parent = acpi_bus_get_parent(handle);
1218 device->bus_ops = *ops; /* workround for not call .start */
1219 STRUCT_TO_INT(device->status) = sta;
1221 acpi_device_get_busid(device);
1226 * Note that we only look for object handles -- cannot evaluate objects
1227 * until we know the device is present and properly initialized.
1229 result = acpi_bus_get_flags(device);
1236 * TBD: Synch with Core's enumeration/initialization process.
1238 acpi_device_set_id(device);
1244 if (device->flags.power_manageable) {
1245 result = acpi_bus_get_power_flags(device);
1251 * Wakeup device management
1252 *-----------------------
1254 if (device->flags.wake_capable) {
1255 result = acpi_bus_get_wakeup_device_flags(device);
1261 * Performance Management
1262 * ----------------------
1264 if (device->flags.performance_manageable) {
1265 result = acpi_bus_get_perf_flags(device);
1270 if ((result = acpi_device_set_context(device)))
1273 result = acpi_device_register(device);
1276 * Bind _ADR-Based Devices when hot add
1278 if (device->flags.bus_address) {
1279 if (device->parent && device->parent->ops.bind)
1280 device->parent->ops.bind(device);
1285 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1286 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1287 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1288 (char *) buffer.pointer,
1289 device->parent ? dev_name(&device->parent->dev) :
1291 kfree(buffer.pointer);
1294 acpi_device_release(&device->dev);
1299 #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1300 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1302 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1303 unsigned long long *sta)
1306 acpi_object_type acpi_type;
1308 status = acpi_get_type(handle, &acpi_type);
1309 if (ACPI_FAILURE(status))
1312 switch (acpi_type) {
1313 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1314 case ACPI_TYPE_DEVICE:
1315 *type = ACPI_BUS_TYPE_DEVICE;
1316 status = acpi_bus_get_status_handle(handle, sta);
1317 if (ACPI_FAILURE(status))
1320 case ACPI_TYPE_PROCESSOR:
1321 *type = ACPI_BUS_TYPE_PROCESSOR;
1322 status = acpi_bus_get_status_handle(handle, sta);
1323 if (ACPI_FAILURE(status))
1326 case ACPI_TYPE_THERMAL:
1327 *type = ACPI_BUS_TYPE_THERMAL;
1328 *sta = ACPI_STA_DEFAULT;
1330 case ACPI_TYPE_POWER:
1331 *type = ACPI_BUS_TYPE_POWER;
1332 *sta = ACPI_STA_DEFAULT;
1341 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1342 void *context, void **return_value)
1344 struct acpi_bus_ops *ops = context;
1346 unsigned long long sta;
1347 struct acpi_device *device;
1351 result = acpi_bus_type_and_status(handle, &type, &sta);
1355 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1356 !(sta & ACPI_STA_DEVICE_FUNCTIONING))
1357 return AE_CTRL_DEPTH;
1360 * We may already have an acpi_device from a previous enumeration. If
1361 * so, we needn't add it again, but we may still have to start it.
1364 acpi_bus_get_device(handle, &device);
1365 if (ops->acpi_op_add && !device)
1366 acpi_add_single_object(&device, handle, type, sta, ops);
1369 return AE_CTRL_DEPTH;
1371 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1372 status = acpi_start_single_object(device);
1373 if (ACPI_FAILURE(status))
1374 return AE_CTRL_DEPTH;
1378 *return_value = device;
1382 static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1383 struct acpi_device **child)
1386 void *device = NULL;
1388 status = acpi_bus_check_add(handle, 0, ops, &device);
1389 if (ACPI_SUCCESS(status))
1390 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1391 acpi_bus_check_add, NULL, ops, &device);
1403 * acpi_bus_add and acpi_bus_start
1405 * scan a given ACPI tree and (probably recently hot-plugged)
1406 * create and add or starts found devices.
1408 * If no devices were found -ENODEV is returned which does not
1409 * mean that this is a real error, there just have been no suitable
1410 * ACPI objects in the table trunk from which the kernel could create
1411 * a device and add/start an appropriate driver.
1415 acpi_bus_add(struct acpi_device **child,
1416 struct acpi_device *parent, acpi_handle handle, int type)
1418 struct acpi_bus_ops ops;
1420 memset(&ops, 0, sizeof(ops));
1421 ops.acpi_op_add = 1;
1423 return acpi_bus_scan(handle, &ops, child);
1425 EXPORT_SYMBOL(acpi_bus_add);
1427 int acpi_bus_start(struct acpi_device *device)
1429 struct acpi_bus_ops ops;
1434 memset(&ops, 0, sizeof(ops));
1435 ops.acpi_op_start = 1;
1437 return acpi_bus_scan(device->handle, &ops, NULL);
1439 EXPORT_SYMBOL(acpi_bus_start);
1441 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1444 struct acpi_device *parent, *child;
1445 acpi_handle phandle, chandle;
1446 acpi_object_type type;
1451 phandle = start->handle;
1452 child = chandle = NULL;
1454 while ((level > 0) && parent && (!err)) {
1455 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1459 * If this scope is exhausted then move our way back up.
1461 if (ACPI_FAILURE(status)) {
1464 acpi_get_parent(phandle, &phandle);
1466 parent = parent->parent;
1469 err = acpi_bus_remove(child, rmdevice);
1471 err = acpi_bus_remove(child, 1);
1476 status = acpi_get_type(chandle, &type);
1477 if (ACPI_FAILURE(status)) {
1481 * If there is a device corresponding to chandle then
1482 * parse it (depth-first).
1484 if (acpi_bus_get_device(chandle, &child) == 0) {
1494 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1496 static int acpi_bus_scan_fixed(void)
1499 struct acpi_device *device = NULL;
1500 struct acpi_bus_ops ops;
1502 memset(&ops, 0, sizeof(ops));
1503 ops.acpi_op_add = 1;
1504 ops.acpi_op_start = 1;
1507 * Enumerate all fixed-feature devices.
1509 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
1510 result = acpi_add_single_object(&device, NULL,
1511 ACPI_BUS_TYPE_POWER_BUTTON,
1516 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1517 result = acpi_add_single_object(&device, NULL,
1518 ACPI_BUS_TYPE_SLEEP_BUTTON,
1526 int __init acpi_scan_init(void)
1529 struct acpi_bus_ops ops;
1531 memset(&ops, 0, sizeof(ops));
1532 ops.acpi_op_add = 1;
1533 ops.acpi_op_start = 1;
1535 result = bus_register(&acpi_bus_type);
1537 /* We don't want to quit even if we failed to add suspend/resume */
1538 printk(KERN_ERR PREFIX "Could not register bus type\n");
1542 * Enumerate devices in the ACPI namespace.
1544 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
1547 result = acpi_bus_scan_fixed();
1550 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);