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,
49 if (!acpi_dev->flags.hardware_id && !acpi_dev->flags.compatible_ids)
52 len = snprintf(modalias, size, "acpi:");
55 if (acpi_dev->flags.hardware_id) {
56 count = snprintf(&modalias[len], size, "%s:",
57 acpi_dev->pnp.hardware_id);
58 if (count < 0 || count >= size)
64 if (acpi_dev->flags.compatible_ids) {
65 struct acpica_device_id_list *cid_list;
68 cid_list = acpi_dev->pnp.cid_list;
69 for (i = 0; i < cid_list->count; i++) {
70 count = snprintf(&modalias[len], size, "%s:",
71 cid_list->ids[i].string);
72 if (count < 0 || count >= size) {
73 printk(KERN_ERR PREFIX "%s cid[%i] exceeds event buffer size",
74 acpi_dev->pnp.device_name, i);
87 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
88 struct acpi_device *acpi_dev = to_acpi_device(dev);
91 /* Device has no HID and no CID or string is >1024 */
92 len = create_modalias(acpi_dev, buf, 1024);
98 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
100 static void acpi_bus_hot_remove_device(void *context)
102 struct acpi_device *device;
103 acpi_handle handle = context;
104 struct acpi_object_list arg_list;
105 union acpi_object arg;
106 acpi_status status = AE_OK;
108 if (acpi_bus_get_device(handle, &device))
114 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
115 "Hot-removing device %s...\n", dev_name(&device->dev)));
117 if (acpi_bus_trim(device, 1)) {
118 printk(KERN_ERR PREFIX
119 "Removing device failed\n");
123 /* power off device */
124 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
125 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
126 printk(KERN_WARNING PREFIX
127 "Power-off device failed\n");
129 if (device->flags.lockable) {
131 arg_list.pointer = &arg;
132 arg.type = ACPI_TYPE_INTEGER;
133 arg.integer.value = 0;
134 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
138 arg_list.pointer = &arg;
139 arg.type = ACPI_TYPE_INTEGER;
140 arg.integer.value = 1;
145 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
146 if (ACPI_FAILURE(status))
147 printk(KERN_WARNING PREFIX
148 "Eject device failed\n");
154 acpi_eject_store(struct device *d, struct device_attribute *attr,
155 const char *buf, size_t count)
159 acpi_object_type type = 0;
160 struct acpi_device *acpi_device = to_acpi_device(d);
162 if ((!count) || (buf[0] != '1')) {
166 if (acpi_device->driver == NULL) {
171 status = acpi_get_type(acpi_device->handle, &type);
172 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
177 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, acpi_device->handle);
182 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
185 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
186 struct acpi_device *acpi_dev = to_acpi_device(dev);
188 return sprintf(buf, "%s\n", acpi_dev->pnp.hardware_id);
190 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
193 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
194 struct acpi_device *acpi_dev = to_acpi_device(dev);
195 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
198 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
202 result = sprintf(buf, "%s\n", (char*)path.pointer);
207 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
209 static int acpi_device_setup_files(struct acpi_device *dev)
216 * Devices gotten from FADT don't have a "path" attribute
219 result = device_create_file(&dev->dev, &dev_attr_path);
224 if (dev->flags.hardware_id) {
225 result = device_create_file(&dev->dev, &dev_attr_hid);
230 if (dev->flags.hardware_id || dev->flags.compatible_ids) {
231 result = device_create_file(&dev->dev, &dev_attr_modalias);
237 * If device has _EJ0, 'eject' file is created that is used to trigger
238 * hot-removal function from userland.
240 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
241 if (ACPI_SUCCESS(status))
242 result = device_create_file(&dev->dev, &dev_attr_eject);
247 static void acpi_device_remove_files(struct acpi_device *dev)
253 * If device has _EJ0, 'eject' file is created that is used to trigger
254 * hot-removal function from userland.
256 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
257 if (ACPI_SUCCESS(status))
258 device_remove_file(&dev->dev, &dev_attr_eject);
260 if (dev->flags.hardware_id || dev->flags.compatible_ids)
261 device_remove_file(&dev->dev, &dev_attr_modalias);
263 if (dev->flags.hardware_id)
264 device_remove_file(&dev->dev, &dev_attr_hid);
266 device_remove_file(&dev->dev, &dev_attr_path);
268 /* --------------------------------------------------------------------------
270 -------------------------------------------------------------------------- */
272 int acpi_match_device_ids(struct acpi_device *device,
273 const struct acpi_device_id *ids)
275 const struct acpi_device_id *id;
278 * If the device is not present, it is unnecessary to load device
281 if (!device->status.present)
284 if (device->flags.hardware_id) {
285 for (id = ids; id->id[0]; id++) {
286 if (!strcmp((char*)id->id, device->pnp.hardware_id))
291 if (device->flags.compatible_ids) {
292 struct acpica_device_id_list *cid_list = device->pnp.cid_list;
295 for (id = ids; id->id[0]; id++) {
296 /* compare multiple _CID entries against driver ids */
297 for (i = 0; i < cid_list->count; i++) {
298 if (!strcmp((char*)id->id,
299 cid_list->ids[i].string))
307 EXPORT_SYMBOL(acpi_match_device_ids);
309 static void acpi_device_release(struct device *dev)
311 struct acpi_device *acpi_dev = to_acpi_device(dev);
313 kfree(acpi_dev->pnp.cid_list);
314 if (acpi_dev->flags.hardware_id)
315 kfree(acpi_dev->pnp.hardware_id);
316 if (acpi_dev->flags.unique_id)
317 kfree(acpi_dev->pnp.unique_id);
321 static int acpi_device_suspend(struct device *dev, pm_message_t state)
323 struct acpi_device *acpi_dev = to_acpi_device(dev);
324 struct acpi_driver *acpi_drv = acpi_dev->driver;
326 if (acpi_drv && acpi_drv->ops.suspend)
327 return acpi_drv->ops.suspend(acpi_dev, state);
331 static int acpi_device_resume(struct device *dev)
333 struct acpi_device *acpi_dev = to_acpi_device(dev);
334 struct acpi_driver *acpi_drv = acpi_dev->driver;
336 if (acpi_drv && acpi_drv->ops.resume)
337 return acpi_drv->ops.resume(acpi_dev);
341 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
343 struct acpi_device *acpi_dev = to_acpi_device(dev);
344 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
346 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
349 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
351 struct acpi_device *acpi_dev = to_acpi_device(dev);
354 if (add_uevent_var(env, "MODALIAS="))
356 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
357 sizeof(env->buf) - env->buflen);
358 if (len >= (sizeof(env->buf) - env->buflen))
364 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
366 struct acpi_device *device = data;
368 device->driver->ops.notify(device, event);
371 static acpi_status acpi_device_notify_fixed(void *data)
373 struct acpi_device *device = data;
375 /* Fixed hardware devices have no handles */
376 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
380 static int acpi_device_install_notify_handler(struct acpi_device *device)
384 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
386 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
387 acpi_device_notify_fixed,
389 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
391 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
392 acpi_device_notify_fixed,
395 status = acpi_install_notify_handler(device->handle,
400 if (ACPI_FAILURE(status))
405 static void acpi_device_remove_notify_handler(struct acpi_device *device)
407 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
408 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
409 acpi_device_notify_fixed);
410 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
411 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
412 acpi_device_notify_fixed);
414 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
418 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
419 static int acpi_start_single_object(struct acpi_device *);
420 static int acpi_device_probe(struct device * dev)
422 struct acpi_device *acpi_dev = to_acpi_device(dev);
423 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
426 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
428 if (acpi_dev->bus_ops.acpi_op_start)
429 acpi_start_single_object(acpi_dev);
431 if (acpi_drv->ops.notify) {
432 ret = acpi_device_install_notify_handler(acpi_dev);
434 if (acpi_drv->ops.remove)
435 acpi_drv->ops.remove(acpi_dev,
436 acpi_dev->removal_type);
441 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
442 "Found driver [%s] for device [%s]\n",
443 acpi_drv->name, acpi_dev->pnp.bus_id));
449 static int acpi_device_remove(struct device * dev)
451 struct acpi_device *acpi_dev = to_acpi_device(dev);
452 struct acpi_driver *acpi_drv = acpi_dev->driver;
455 if (acpi_drv->ops.notify)
456 acpi_device_remove_notify_handler(acpi_dev);
457 if (acpi_drv->ops.remove)
458 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
460 acpi_dev->driver = NULL;
461 acpi_dev->driver_data = NULL;
467 struct bus_type acpi_bus_type = {
469 .suspend = acpi_device_suspend,
470 .resume = acpi_device_resume,
471 .match = acpi_bus_match,
472 .probe = acpi_device_probe,
473 .remove = acpi_device_remove,
474 .uevent = acpi_device_uevent,
477 static int acpi_device_register(struct acpi_device *device)
480 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
486 * Link this device to its parent and siblings.
488 INIT_LIST_HEAD(&device->children);
489 INIT_LIST_HEAD(&device->node);
490 INIT_LIST_HEAD(&device->wakeup_list);
492 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
494 printk(KERN_ERR PREFIX "Memory allocation error\n");
498 mutex_lock(&acpi_device_lock);
500 * Find suitable bus_id and instance number in acpi_bus_id_list
501 * If failed, create one and link it into acpi_bus_id_list
503 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
504 if(!strcmp(acpi_device_bus_id->bus_id, device->flags.hardware_id? device->pnp.hardware_id : "device")) {
505 acpi_device_bus_id->instance_no ++;
512 acpi_device_bus_id = new_bus_id;
513 strcpy(acpi_device_bus_id->bus_id, device->flags.hardware_id ? device->pnp.hardware_id : "device");
514 acpi_device_bus_id->instance_no = 0;
515 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
517 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
520 list_add_tail(&device->node, &device->parent->children);
522 if (device->wakeup.flags.valid)
523 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
524 mutex_unlock(&acpi_device_lock);
527 device->dev.parent = &device->parent->dev;
528 device->dev.bus = &acpi_bus_type;
529 device->dev.release = &acpi_device_release;
530 result = device_register(&device->dev);
532 dev_err(&device->dev, "Error registering device\n");
536 result = acpi_device_setup_files(device);
538 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
539 dev_name(&device->dev));
541 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
544 mutex_lock(&acpi_device_lock);
546 list_del(&device->node);
547 list_del(&device->wakeup_list);
548 mutex_unlock(&acpi_device_lock);
552 static void acpi_device_unregister(struct acpi_device *device, int type)
554 mutex_lock(&acpi_device_lock);
556 list_del(&device->node);
558 list_del(&device->wakeup_list);
559 mutex_unlock(&acpi_device_lock);
561 acpi_detach_data(device->handle, acpi_bus_data_handler);
563 acpi_device_remove_files(device);
564 device_unregister(&device->dev);
567 /* --------------------------------------------------------------------------
569 -------------------------------------------------------------------------- */
571 * acpi_bus_driver_init - add a device to a driver
572 * @device: the device to add and initialize
573 * @driver: driver for the device
575 * Used to initialize a device via its device driver. Called whenever a
576 * driver is bound to a device. Invokes the driver's add() ops.
579 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
583 if (!device || !driver)
586 if (!driver->ops.add)
589 result = driver->ops.add(device);
591 device->driver = NULL;
592 device->driver_data = NULL;
596 device->driver = driver;
599 * TBD - Configuration Management: Assign resources to device based
600 * upon possible configuration and currently allocated resources.
603 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
604 "Driver successfully bound to device\n"));
608 static int acpi_start_single_object(struct acpi_device *device)
611 struct acpi_driver *driver;
614 if (!(driver = device->driver))
617 if (driver->ops.start) {
618 result = driver->ops.start(device);
619 if (result && driver->ops.remove)
620 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
627 * acpi_bus_register_driver - register a driver with the ACPI bus
628 * @driver: driver being registered
630 * Registers a driver with the ACPI bus. Searches the namespace for all
631 * devices that match the driver's criteria and binds. Returns zero for
632 * success or a negative error status for failure.
634 int acpi_bus_register_driver(struct acpi_driver *driver)
640 driver->drv.name = driver->name;
641 driver->drv.bus = &acpi_bus_type;
642 driver->drv.owner = driver->owner;
644 ret = driver_register(&driver->drv);
648 EXPORT_SYMBOL(acpi_bus_register_driver);
651 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
652 * @driver: driver to unregister
654 * Unregisters a driver with the ACPI bus. Searches the namespace for all
655 * devices that match the driver's criteria and unbinds.
657 void acpi_bus_unregister_driver(struct acpi_driver *driver)
659 driver_unregister(&driver->drv);
662 EXPORT_SYMBOL(acpi_bus_unregister_driver);
664 /* --------------------------------------------------------------------------
666 -------------------------------------------------------------------------- */
667 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
671 struct acpi_device *device;
674 * Fixed hardware devices do not appear in the namespace and do not
675 * have handles, but we fabricate acpi_devices for them, so we have
676 * to deal with them specially.
682 status = acpi_get_parent(handle, &handle);
683 if (status == AE_NULL_ENTRY)
685 if (ACPI_FAILURE(status))
688 ret = acpi_bus_get_device(handle, &device);
695 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
699 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
700 union acpi_object *obj;
702 status = acpi_get_handle(handle, "_EJD", &tmp);
703 if (ACPI_FAILURE(status))
706 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
707 if (ACPI_SUCCESS(status)) {
708 obj = buffer.pointer;
709 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
711 kfree(buffer.pointer);
715 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
717 void acpi_bus_data_handler(acpi_handle handle, void *context)
725 static int acpi_bus_get_perf_flags(struct acpi_device *device)
727 device->performance.state = ACPI_STATE_UNKNOWN;
732 acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
733 union acpi_object *package)
736 union acpi_object *element = NULL;
738 if (!device || !package || (package->package.count < 2))
739 return AE_BAD_PARAMETER;
741 element = &(package->package.elements[0]);
743 return AE_BAD_PARAMETER;
744 if (element->type == ACPI_TYPE_PACKAGE) {
745 if ((element->package.count < 2) ||
746 (element->package.elements[0].type !=
747 ACPI_TYPE_LOCAL_REFERENCE)
748 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
750 device->wakeup.gpe_device =
751 element->package.elements[0].reference.handle;
752 device->wakeup.gpe_number =
753 (u32) element->package.elements[1].integer.value;
754 } else if (element->type == ACPI_TYPE_INTEGER) {
755 device->wakeup.gpe_number = element->integer.value;
759 element = &(package->package.elements[1]);
760 if (element->type != ACPI_TYPE_INTEGER) {
763 device->wakeup.sleep_state = element->integer.value;
765 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
768 device->wakeup.resources.count = package->package.count - 2;
769 for (i = 0; i < device->wakeup.resources.count; i++) {
770 element = &(package->package.elements[i + 2]);
771 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
774 device->wakeup.resources.handles[i] = element->reference.handle;
780 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
782 acpi_status status = 0;
783 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
784 union acpi_object *package = NULL;
787 struct acpi_device_id button_device_ids[] = {
795 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
796 if (ACPI_FAILURE(status)) {
797 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
801 package = (union acpi_object *)buffer.pointer;
802 status = acpi_bus_extract_wakeup_device_power_package(device, package);
803 if (ACPI_FAILURE(status)) {
804 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
808 kfree(buffer.pointer);
810 device->wakeup.flags.valid = 1;
811 device->wakeup.prepare_count = 0;
812 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
813 * system for the ACPI device with the _PRW object.
814 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
815 * So it is necessary to call _DSW object first. Only when it is not
816 * present will the _PSW object used.
818 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
820 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
821 "error in _DSW or _PSW evaluation\n"));
823 /* Power button, Lid switch always enable wakeup */
824 if (!acpi_match_device_ids(device, button_device_ids))
825 device->wakeup.flags.run_wake = 1;
828 if (ACPI_FAILURE(status))
829 device->flags.wake_capable = 0;
833 static int acpi_bus_get_power_flags(struct acpi_device *device)
835 acpi_status status = 0;
836 acpi_handle handle = NULL;
841 * Power Management Flags
843 status = acpi_get_handle(device->handle, "_PSC", &handle);
844 if (ACPI_SUCCESS(status))
845 device->power.flags.explicit_get = 1;
846 status = acpi_get_handle(device->handle, "_IRC", &handle);
847 if (ACPI_SUCCESS(status))
848 device->power.flags.inrush_current = 1;
851 * Enumerate supported power management states
853 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
854 struct acpi_device_power_state *ps = &device->power.states[i];
855 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
857 /* Evaluate "_PRx" to se if power resources are referenced */
858 acpi_evaluate_reference(device->handle, object_name, NULL,
860 if (ps->resources.count) {
861 device->power.flags.power_resources = 1;
865 /* Evaluate "_PSx" to see if we can do explicit sets */
866 object_name[2] = 'S';
867 status = acpi_get_handle(device->handle, object_name, &handle);
868 if (ACPI_SUCCESS(status)) {
869 ps->flags.explicit_set = 1;
873 /* State is valid if we have some power control */
874 if (ps->resources.count || ps->flags.explicit_set)
877 ps->power = -1; /* Unknown - driver assigned */
878 ps->latency = -1; /* Unknown - driver assigned */
881 /* Set defaults for D0 and D3 states (always valid) */
882 device->power.states[ACPI_STATE_D0].flags.valid = 1;
883 device->power.states[ACPI_STATE_D0].power = 100;
884 device->power.states[ACPI_STATE_D3].flags.valid = 1;
885 device->power.states[ACPI_STATE_D3].power = 0;
887 /* TBD: System wake support and resource requirements. */
889 device->power.state = ACPI_STATE_UNKNOWN;
890 acpi_bus_get_power(device->handle, &(device->power.state));
895 static int acpi_bus_get_flags(struct acpi_device *device)
897 acpi_status status = AE_OK;
898 acpi_handle temp = NULL;
901 /* Presence of _STA indicates 'dynamic_status' */
902 status = acpi_get_handle(device->handle, "_STA", &temp);
903 if (ACPI_SUCCESS(status))
904 device->flags.dynamic_status = 1;
906 /* Presence of _CID indicates 'compatible_ids' */
907 status = acpi_get_handle(device->handle, "_CID", &temp);
908 if (ACPI_SUCCESS(status))
909 device->flags.compatible_ids = 1;
911 /* Presence of _RMV indicates 'removable' */
912 status = acpi_get_handle(device->handle, "_RMV", &temp);
913 if (ACPI_SUCCESS(status))
914 device->flags.removable = 1;
916 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
917 status = acpi_get_handle(device->handle, "_EJD", &temp);
918 if (ACPI_SUCCESS(status))
919 device->flags.ejectable = 1;
921 status = acpi_get_handle(device->handle, "_EJ0", &temp);
922 if (ACPI_SUCCESS(status))
923 device->flags.ejectable = 1;
926 /* Presence of _LCK indicates 'lockable' */
927 status = acpi_get_handle(device->handle, "_LCK", &temp);
928 if (ACPI_SUCCESS(status))
929 device->flags.lockable = 1;
931 /* Presence of _PS0|_PR0 indicates 'power manageable' */
932 status = acpi_get_handle(device->handle, "_PS0", &temp);
933 if (ACPI_FAILURE(status))
934 status = acpi_get_handle(device->handle, "_PR0", &temp);
935 if (ACPI_SUCCESS(status))
936 device->flags.power_manageable = 1;
938 /* Presence of _PRW indicates wake capable */
939 status = acpi_get_handle(device->handle, "_PRW", &temp);
940 if (ACPI_SUCCESS(status))
941 device->flags.wake_capable = 1;
943 /* TBD: Performance management */
948 static void acpi_device_get_busid(struct acpi_device *device)
950 char bus_id[5] = { '?', 0 };
951 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
957 * The device's Bus ID is simply the object name.
958 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
960 if (ACPI_IS_ROOT_DEVICE(device)) {
961 strcpy(device->pnp.bus_id, "ACPI");
965 switch (device->device_type) {
966 case ACPI_BUS_TYPE_POWER_BUTTON:
967 strcpy(device->pnp.bus_id, "PWRF");
969 case ACPI_BUS_TYPE_SLEEP_BUTTON:
970 strcpy(device->pnp.bus_id, "SLPF");
973 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
974 /* Clean up trailing underscores (if any) */
975 for (i = 3; i > 1; i--) {
976 if (bus_id[i] == '_')
981 strcpy(device->pnp.bus_id, bus_id);
987 * acpi_bay_match - see if a device is an ejectable driver bay
989 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
990 * then we can safely call it an ejectable drive bay
992 static int acpi_bay_match(struct acpi_device *device){
998 handle = device->handle;
1000 status = acpi_get_handle(handle, "_EJ0", &tmp);
1001 if (ACPI_FAILURE(status))
1004 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1005 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1006 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1007 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1010 if (acpi_get_parent(handle, &phandle))
1013 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1014 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1015 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1016 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1023 * acpi_dock_match - see if a device has a _DCK method
1025 static int acpi_dock_match(struct acpi_device *device)
1028 return acpi_get_handle(device->handle, "_DCK", &tmp);
1031 static struct acpica_device_id_list*
1033 struct acpi_device_info *info,
1034 struct acpica_device_id *new_cid)
1036 struct acpica_device_id_list *cid;
1037 char *next_id_string;
1038 acpi_size cid_length;
1039 acpi_size new_cid_length;
1043 /* Allocate new CID list with room for the new CID */
1046 new_cid_length = info->compatible_id_list.list_size;
1047 else if (info->compatible_id_list.list_size)
1048 new_cid_length = info->compatible_id_list.list_size +
1049 new_cid->length + sizeof(struct acpica_device_id);
1051 new_cid_length = sizeof(struct acpica_device_id_list) + new_cid->length;
1053 cid = ACPI_ALLOCATE_ZEROED(new_cid_length);
1058 cid->list_size = new_cid_length;
1059 cid->count = info->compatible_id_list.count;
1062 next_id_string = (char *) cid->ids + (cid->count * sizeof(struct acpica_device_id));
1064 /* Copy all existing CIDs */
1066 for (i = 0; i < info->compatible_id_list.count; i++) {
1067 cid_length = info->compatible_id_list.ids[i].length;
1068 cid->ids[i].string = next_id_string;
1069 cid->ids[i].length = cid_length;
1071 ACPI_MEMCPY(next_id_string, info->compatible_id_list.ids[i].string,
1074 next_id_string += cid_length;
1077 /* Append the new CID */
1080 cid->ids[i].string = next_id_string;
1081 cid->ids[i].length = new_cid->length;
1083 ACPI_MEMCPY(next_id_string, new_cid->string, new_cid->length);
1089 static void acpi_device_set_id(struct acpi_device *device)
1091 struct acpi_device_info *info = NULL;
1094 struct acpica_device_id_list *cid_list = NULL;
1095 char *cid_add = NULL;
1098 switch (device->device_type) {
1099 case ACPI_BUS_TYPE_DEVICE:
1100 if (ACPI_IS_ROOT_DEVICE(device)) {
1101 hid = ACPI_SYSTEM_HID;
1105 status = acpi_get_object_info(device->handle, &info);
1106 if (ACPI_FAILURE(status)) {
1107 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1111 if (info->valid & ACPI_VALID_HID)
1112 hid = info->hardware_id.string;
1113 if (info->valid & ACPI_VALID_UID)
1114 uid = info->unique_id.string;
1115 if (info->valid & ACPI_VALID_CID)
1116 cid_list = &info->compatible_id_list;
1117 if (info->valid & ACPI_VALID_ADR) {
1118 device->pnp.bus_address = info->address;
1119 device->flags.bus_address = 1;
1122 /* If we have a video/bay/dock device, add our selfdefined
1123 HID to the CID list. Like that the video/bay/dock drivers
1124 will get autoloaded and the device might still match
1125 against another driver.
1127 if (acpi_is_video_device(device))
1128 cid_add = ACPI_VIDEO_HID;
1129 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1130 cid_add = ACPI_BAY_HID;
1131 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1132 cid_add = ACPI_DOCK_HID;
1135 case ACPI_BUS_TYPE_POWER:
1136 hid = ACPI_POWER_HID;
1138 case ACPI_BUS_TYPE_PROCESSOR:
1139 hid = ACPI_PROCESSOR_OBJECT_HID;
1141 case ACPI_BUS_TYPE_THERMAL:
1142 hid = ACPI_THERMAL_HID;
1144 case ACPI_BUS_TYPE_POWER_BUTTON:
1145 hid = ACPI_BUTTON_HID_POWERF;
1147 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1148 hid = ACPI_BUTTON_HID_SLEEPF;
1155 * Fix for the system root bus device -- the only root-level device.
1157 if (((acpi_handle)device->parent == ACPI_ROOT_OBJECT) &&
1158 (device->device_type == ACPI_BUS_TYPE_DEVICE)) {
1160 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1161 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1165 device->pnp.hardware_id = ACPI_ALLOCATE_ZEROED(strlen (hid) + 1);
1166 if (device->pnp.hardware_id) {
1167 strcpy(device->pnp.hardware_id, hid);
1168 device->flags.hardware_id = 1;
1171 if (!device->flags.hardware_id)
1172 device->pnp.hardware_id = "";
1175 device->pnp.unique_id = ACPI_ALLOCATE_ZEROED(strlen (uid) + 1);
1176 if (device->pnp.unique_id) {
1177 strcpy(device->pnp.unique_id, uid);
1178 device->flags.unique_id = 1;
1181 if (!device->flags.unique_id)
1182 device->pnp.unique_id = "";
1184 if (cid_list || cid_add) {
1185 struct acpica_device_id_list *list;
1188 struct acpica_device_id cid;
1189 cid.length = strlen (cid_add) + 1;
1190 cid.string = cid_add;
1192 list = acpi_add_cid(info, &cid);
1194 list = acpi_add_cid(info, NULL);
1198 device->pnp.cid_list = list;
1200 device->flags.compatible_ids = 1;
1207 static int acpi_device_set_context(struct acpi_device *device)
1214 * Attach this 'struct acpi_device' to the ACPI object. This makes
1215 * resolutions from handle->device very efficient. Fixed hardware
1216 * devices have no handles, so we skip them.
1218 if (!device->handle)
1221 status = acpi_attach_data(device->handle,
1222 acpi_bus_data_handler, device);
1223 if (ACPI_SUCCESS(status))
1226 printk(KERN_ERR PREFIX "Error attaching device data\n");
1230 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1235 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1236 device_release_driver(&dev->dev);
1242 * unbind _ADR-Based Devices when hot removal
1244 if (dev->flags.bus_address) {
1245 if ((dev->parent) && (dev->parent->ops.unbind))
1246 dev->parent->ops.unbind(dev);
1248 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1253 static int acpi_add_single_object(struct acpi_device **child,
1254 acpi_handle handle, int type,
1255 struct acpi_bus_ops *ops)
1258 struct acpi_device *device;
1259 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1261 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1263 printk(KERN_ERR PREFIX "Memory allocation error\n");
1267 device->device_type = type;
1268 device->handle = handle;
1269 device->parent = acpi_bus_get_parent(handle);
1270 device->bus_ops = *ops; /* workround for not call .start */
1272 acpi_device_get_busid(device);
1277 * Get prior to calling acpi_bus_get_status() so we know whether
1278 * or not _STA is present. Note that we only look for object
1279 * handles -- cannot evaluate objects until we know the device is
1280 * present and properly initialized.
1282 result = acpi_bus_get_flags(device);
1289 * See if the device is present. We always assume that non-Device
1290 * and non-Processor objects (e.g. thermal zones, power resources,
1291 * etc.) are present, functioning, etc. (at least when parent object
1292 * is present). Note that _STA has a different meaning for some
1293 * objects (e.g. power resources) so we need to be careful how we use
1297 case ACPI_BUS_TYPE_PROCESSOR:
1298 case ACPI_BUS_TYPE_DEVICE:
1299 result = acpi_bus_get_status(device);
1300 if (ACPI_FAILURE(result)) {
1305 * When the device is neither present nor functional, the
1306 * device should not be added to Linux ACPI device tree.
1307 * When the status of the device is not present but functinal,
1308 * it should be added to Linux ACPI tree. For example : bay
1309 * device , dock device.
1310 * In such conditions it is unncessary to check whether it is
1311 * bay device or dock device.
1313 if (!device->status.present && !device->status.functional) {
1319 STRUCT_TO_INT(device->status) =
1320 ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
1321 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
1328 * TBD: Synch with Core's enumeration/initialization process.
1332 * Hardware ID, Unique ID, & Bus Address
1333 * -------------------------------------
1335 acpi_device_set_id(device);
1341 if (device->flags.power_manageable) {
1342 result = acpi_bus_get_power_flags(device);
1348 * Wakeup device management
1349 *-----------------------
1351 if (device->flags.wake_capable) {
1352 result = acpi_bus_get_wakeup_device_flags(device);
1358 * Performance Management
1359 * ----------------------
1361 if (device->flags.performance_manageable) {
1362 result = acpi_bus_get_perf_flags(device);
1367 if ((result = acpi_device_set_context(device)))
1370 result = acpi_device_register(device);
1373 * Bind _ADR-Based Devices when hot add
1375 if (device->flags.bus_address) {
1376 if (device->parent && device->parent->ops.bind)
1377 device->parent->ops.bind(device);
1382 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1383 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1384 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1385 (char *) buffer.pointer,
1386 device->parent ? dev_name(&device->parent->dev) :
1388 kfree(buffer.pointer);
1391 acpi_device_release(&device->dev);
1396 static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops)
1398 acpi_status status = AE_OK;
1399 struct acpi_device *parent = NULL;
1400 struct acpi_device *child = NULL;
1401 acpi_handle phandle = NULL;
1402 acpi_handle chandle = NULL;
1403 acpi_object_type type = 0;
1408 * We must have an acpi_device for the starting node already, and
1409 * we scan its children.
1412 ret = acpi_bus_get_device(phandle, &parent);
1417 * Parse through the ACPI namespace, identify all 'devices', and
1418 * create a new 'struct acpi_device' for each.
1420 while ((level > 0) && parent) {
1422 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1426 * If this scope is exhausted then move our way back up.
1428 if (ACPI_FAILURE(status)) {
1431 acpi_get_parent(phandle, &phandle);
1433 parent = parent->parent;
1437 status = acpi_get_type(chandle, &type);
1438 if (ACPI_FAILURE(status))
1442 * If this is a scope object then parse it (depth-first).
1444 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1452 * We're only interested in objects that we consider 'devices'.
1455 case ACPI_TYPE_DEVICE:
1456 type = ACPI_BUS_TYPE_DEVICE;
1458 case ACPI_TYPE_PROCESSOR:
1459 type = ACPI_BUS_TYPE_PROCESSOR;
1461 case ACPI_TYPE_THERMAL:
1462 type = ACPI_BUS_TYPE_THERMAL;
1464 case ACPI_TYPE_POWER:
1465 type = ACPI_BUS_TYPE_POWER;
1471 if (ops->acpi_op_add)
1472 status = acpi_add_single_object(&child, chandle, type,
1475 status = acpi_bus_get_device(chandle, &child);
1477 if (ACPI_FAILURE(status))
1480 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1481 status = acpi_start_single_object(child);
1482 if (ACPI_FAILURE(status))
1487 * If the device is present, enabled, and functioning then
1488 * parse its scope (depth-first). Note that we need to
1489 * represent absent devices to facilitate PnP notifications
1490 * -- but only the subtree head (not all of its children,
1491 * which will be enumerated when the parent is inserted).
1493 * TBD: Need notifications and other detection mechanisms
1494 * in place before we can fully implement this.
1497 * When the device is not present but functional, it is also
1498 * necessary to scan the children of this device.
1500 if (child->status.present || (!child->status.present &&
1501 child->status.functional)) {
1502 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1504 if (ACPI_SUCCESS(status)) {
1517 acpi_bus_add(struct acpi_device **child,
1518 struct acpi_device *parent, acpi_handle handle, int type)
1521 struct acpi_bus_ops ops;
1523 memset(&ops, 0, sizeof(ops));
1524 ops.acpi_op_add = 1;
1526 result = acpi_add_single_object(child, handle, type, &ops);
1528 result = acpi_bus_scan((*child)->handle, &ops);
1532 EXPORT_SYMBOL(acpi_bus_add);
1534 int acpi_bus_start(struct acpi_device *device)
1537 struct acpi_bus_ops ops;
1539 memset(&ops, 0, sizeof(ops));
1540 ops.acpi_op_start = 1;
1542 result = acpi_start_single_object(device);
1544 result = acpi_bus_scan(device->handle, &ops);
1548 EXPORT_SYMBOL(acpi_bus_start);
1550 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1553 struct acpi_device *parent, *child;
1554 acpi_handle phandle, chandle;
1555 acpi_object_type type;
1560 phandle = start->handle;
1561 child = chandle = NULL;
1563 while ((level > 0) && parent && (!err)) {
1564 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1568 * If this scope is exhausted then move our way back up.
1570 if (ACPI_FAILURE(status)) {
1573 acpi_get_parent(phandle, &phandle);
1575 parent = parent->parent;
1578 err = acpi_bus_remove(child, rmdevice);
1580 err = acpi_bus_remove(child, 1);
1585 status = acpi_get_type(chandle, &type);
1586 if (ACPI_FAILURE(status)) {
1590 * If there is a device corresponding to chandle then
1591 * parse it (depth-first).
1593 if (acpi_bus_get_device(chandle, &child) == 0) {
1603 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1605 static int acpi_bus_scan_fixed(void)
1608 struct acpi_device *device = NULL;
1609 struct acpi_bus_ops ops;
1611 memset(&ops, 0, sizeof(ops));
1612 ops.acpi_op_add = 1;
1613 ops.acpi_op_start = 1;
1616 * Enumerate all fixed-feature devices.
1618 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
1619 result = acpi_add_single_object(&device, NULL,
1620 ACPI_BUS_TYPE_POWER_BUTTON,
1624 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1625 result = acpi_add_single_object(&device, NULL,
1626 ACPI_BUS_TYPE_SLEEP_BUTTON,
1633 int __init acpi_scan_init(void)
1636 struct acpi_bus_ops ops;
1638 memset(&ops, 0, sizeof(ops));
1639 ops.acpi_op_add = 1;
1640 ops.acpi_op_start = 1;
1642 result = bus_register(&acpi_bus_type);
1644 /* We don't want to quit even if we failed to add suspend/resume */
1645 printk(KERN_ERR PREFIX "Could not register bus type\n");
1649 * Create the root device in the bus's device tree
1651 result = acpi_add_single_object(&acpi_root, ACPI_ROOT_OBJECT,
1652 ACPI_BUS_TYPE_DEVICE, &ops);
1657 * Enumerate devices in the ACPI namespace.
1659 result = acpi_bus_scan(acpi_root->handle, &ops);
1662 result = acpi_bus_scan_fixed();
1665 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);