2 * scan.c - support for transforming the ACPI namespace into individual objects
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/kernel.h>
8 #include <linux/acpi.h>
9 #include <linux/signal.h>
10 #include <linux/kthread.h>
12 #include <acpi/acpi_drivers.h>
16 #define _COMPONENT ACPI_BUS_COMPONENT
17 ACPI_MODULE_NAME("scan");
18 #define STRUCT_TO_INT(s) (*((int*)&s))
19 extern struct acpi_device *acpi_root;
21 #define ACPI_BUS_CLASS "system_bus"
22 #define ACPI_BUS_HID "LNXSYBUS"
23 #define ACPI_BUS_DEVICE_NAME "System Bus"
25 #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
27 static LIST_HEAD(acpi_device_list);
28 static LIST_HEAD(acpi_bus_id_list);
29 DEFINE_MUTEX(acpi_device_lock);
30 LIST_HEAD(acpi_wakeup_device_list);
32 struct acpi_device_bus_id{
34 unsigned int instance_no;
35 struct list_head node;
39 * Creates hid/cid(s) string needed for modalias and uevent
40 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
41 * char *modalias: "acpi:IBM0001:ACPI0001"
43 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
48 struct acpi_hardware_id *id;
50 len = snprintf(modalias, size, "acpi:");
53 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
54 count = snprintf(&modalias[len], size, "%s:", id->id);
55 if (count < 0 || count >= size)
66 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
67 struct acpi_device *acpi_dev = to_acpi_device(dev);
70 /* Device has no HID and no CID or string is >1024 */
71 len = create_modalias(acpi_dev, buf, 1024);
77 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
79 static void acpi_bus_hot_remove_device(void *context)
81 struct acpi_device *device;
82 acpi_handle handle = context;
83 struct acpi_object_list arg_list;
84 union acpi_object arg;
85 acpi_status status = AE_OK;
87 if (acpi_bus_get_device(handle, &device))
93 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
94 "Hot-removing device %s...\n", dev_name(&device->dev)));
96 if (acpi_bus_trim(device, 1)) {
97 printk(KERN_ERR PREFIX
98 "Removing device failed\n");
102 /* power off device */
103 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
104 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
105 printk(KERN_WARNING PREFIX
106 "Power-off device failed\n");
108 if (device->flags.lockable) {
110 arg_list.pointer = &arg;
111 arg.type = ACPI_TYPE_INTEGER;
112 arg.integer.value = 0;
113 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
117 arg_list.pointer = &arg;
118 arg.type = ACPI_TYPE_INTEGER;
119 arg.integer.value = 1;
124 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
125 if (ACPI_FAILURE(status))
126 printk(KERN_WARNING PREFIX
127 "Eject device failed\n");
133 acpi_eject_store(struct device *d, struct device_attribute *attr,
134 const char *buf, size_t count)
138 acpi_object_type type = 0;
139 struct acpi_device *acpi_device = to_acpi_device(d);
141 if ((!count) || (buf[0] != '1')) {
145 if (acpi_device->driver == NULL) {
150 status = acpi_get_type(acpi_device->handle, &type);
151 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
156 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, acpi_device->handle);
161 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
164 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
165 struct acpi_device *acpi_dev = to_acpi_device(dev);
167 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
169 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
171 #ifdef CONFIG_PCI_GUESTDEV
173 acpi_device_uid_show(struct device *dev, struct device_attribute *attr, char *buf) {
174 struct acpi_device *acpi_dev = to_acpi_device(dev);
176 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
178 static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
182 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
183 struct acpi_device *acpi_dev = to_acpi_device(dev);
184 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
187 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
191 result = sprintf(buf, "%s\n", (char*)path.pointer);
196 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
198 static int acpi_device_setup_files(struct acpi_device *dev)
205 * Devices gotten from FADT don't have a "path" attribute
208 result = device_create_file(&dev->dev, &dev_attr_path);
213 result = device_create_file(&dev->dev, &dev_attr_hid);
217 result = device_create_file(&dev->dev, &dev_attr_modalias);
221 #ifdef CONFIG_PCI_GUESTDEV
222 if(dev->pnp.unique_id) {
223 result = device_create_file(&dev->dev, &dev_attr_uid);
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 result = device_create_file(&dev->dev, &dev_attr_eject);
239 static void acpi_device_remove_files(struct acpi_device *dev)
245 * If device has _EJ0, 'eject' file is created that is used to trigger
246 * hot-removal function from userland.
248 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
249 if (ACPI_SUCCESS(status))
250 device_remove_file(&dev->dev, &dev_attr_eject);
252 device_remove_file(&dev->dev, &dev_attr_modalias);
253 device_remove_file(&dev->dev, &dev_attr_hid);
255 device_remove_file(&dev->dev, &dev_attr_path);
257 /* --------------------------------------------------------------------------
259 -------------------------------------------------------------------------- */
261 int acpi_match_device_ids(struct acpi_device *device,
262 const struct acpi_device_id *ids)
264 const struct acpi_device_id *id;
265 struct acpi_hardware_id *hwid;
268 * If the device is not present, it is unnecessary to load device
271 if (!device->status.present)
274 for (id = ids; id->id[0]; id++)
275 list_for_each_entry(hwid, &device->pnp.ids, list)
276 if (!strcmp((char *) id->id, hwid->id))
281 EXPORT_SYMBOL(acpi_match_device_ids);
283 static void acpi_free_ids(struct acpi_device *device)
285 struct acpi_hardware_id *id, *tmp;
287 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
291 #ifdef CONFIG_PCI_GUESTDEV
292 kfree(device->pnp.unique_id);
296 static void acpi_device_release(struct device *dev)
298 struct acpi_device *acpi_dev = to_acpi_device(dev);
300 acpi_free_ids(acpi_dev);
304 static int acpi_device_suspend(struct device *dev, pm_message_t state)
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.suspend)
310 return acpi_drv->ops.suspend(acpi_dev, state);
314 static int acpi_device_resume(struct device *dev)
316 struct acpi_device *acpi_dev = to_acpi_device(dev);
317 struct acpi_driver *acpi_drv = acpi_dev->driver;
319 if (acpi_drv && acpi_drv->ops.resume)
320 return acpi_drv->ops.resume(acpi_dev);
324 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
326 struct acpi_device *acpi_dev = to_acpi_device(dev);
327 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
329 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
332 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
334 struct acpi_device *acpi_dev = to_acpi_device(dev);
337 if (add_uevent_var(env, "MODALIAS="))
339 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
340 sizeof(env->buf) - env->buflen);
341 if (len >= (sizeof(env->buf) - env->buflen))
347 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
349 struct acpi_device *device = data;
351 device->driver->ops.notify(device, event);
354 static acpi_status acpi_device_notify_fixed(void *data)
356 struct acpi_device *device = data;
358 /* Fixed hardware devices have no handles */
359 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
363 static int acpi_device_install_notify_handler(struct acpi_device *device)
367 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
369 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
370 acpi_device_notify_fixed,
372 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
374 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
375 acpi_device_notify_fixed,
378 status = acpi_install_notify_handler(device->handle,
383 if (ACPI_FAILURE(status))
388 static void acpi_device_remove_notify_handler(struct acpi_device *device)
390 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
391 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
392 acpi_device_notify_fixed);
393 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
394 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
395 acpi_device_notify_fixed);
397 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
401 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
402 static int acpi_start_single_object(struct acpi_device *);
403 static int acpi_device_probe(struct device * dev)
405 struct acpi_device *acpi_dev = to_acpi_device(dev);
406 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
409 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
411 if (acpi_dev->bus_ops.acpi_op_start)
412 acpi_start_single_object(acpi_dev);
414 if (acpi_drv->ops.notify) {
415 ret = acpi_device_install_notify_handler(acpi_dev);
417 if (acpi_drv->ops.remove)
418 acpi_drv->ops.remove(acpi_dev,
419 acpi_dev->removal_type);
424 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
425 "Found driver [%s] for device [%s]\n",
426 acpi_drv->name, acpi_dev->pnp.bus_id));
432 static int acpi_device_remove(struct device * dev)
434 struct acpi_device *acpi_dev = to_acpi_device(dev);
435 struct acpi_driver *acpi_drv = acpi_dev->driver;
438 if (acpi_drv->ops.notify)
439 acpi_device_remove_notify_handler(acpi_dev);
440 if (acpi_drv->ops.remove)
441 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
443 acpi_dev->driver = NULL;
444 acpi_dev->driver_data = NULL;
450 struct bus_type acpi_bus_type = {
452 .suspend = acpi_device_suspend,
453 .resume = acpi_device_resume,
454 .match = acpi_bus_match,
455 .probe = acpi_device_probe,
456 .remove = acpi_device_remove,
457 .uevent = acpi_device_uevent,
460 static int acpi_device_register(struct acpi_device *device)
463 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
469 * Link this device to its parent and siblings.
471 INIT_LIST_HEAD(&device->children);
472 INIT_LIST_HEAD(&device->node);
473 INIT_LIST_HEAD(&device->wakeup_list);
475 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
477 printk(KERN_ERR PREFIX "Memory allocation error\n");
481 mutex_lock(&acpi_device_lock);
483 * Find suitable bus_id and instance number in acpi_bus_id_list
484 * If failed, create one and link it into acpi_bus_id_list
486 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
487 if (!strcmp(acpi_device_bus_id->bus_id,
488 acpi_device_hid(device))) {
489 acpi_device_bus_id->instance_no++;
496 acpi_device_bus_id = new_bus_id;
497 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
498 acpi_device_bus_id->instance_no = 0;
499 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
501 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
504 list_add_tail(&device->node, &device->parent->children);
506 if (device->wakeup.flags.valid)
507 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
508 mutex_unlock(&acpi_device_lock);
511 device->dev.parent = &device->parent->dev;
512 device->dev.bus = &acpi_bus_type;
513 device->dev.release = &acpi_device_release;
514 result = device_register(&device->dev);
516 dev_err(&device->dev, "Error registering device\n");
520 result = acpi_device_setup_files(device);
522 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
523 dev_name(&device->dev));
525 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
528 mutex_lock(&acpi_device_lock);
530 list_del(&device->node);
531 list_del(&device->wakeup_list);
532 mutex_unlock(&acpi_device_lock);
536 static void acpi_device_unregister(struct acpi_device *device, int type)
538 mutex_lock(&acpi_device_lock);
540 list_del(&device->node);
542 list_del(&device->wakeup_list);
543 mutex_unlock(&acpi_device_lock);
545 acpi_detach_data(device->handle, acpi_bus_data_handler);
547 acpi_device_remove_files(device);
548 device_unregister(&device->dev);
551 /* --------------------------------------------------------------------------
553 -------------------------------------------------------------------------- */
555 * acpi_bus_driver_init - add a device to a driver
556 * @device: the device to add and initialize
557 * @driver: driver for the device
559 * Used to initialize a device via its device driver. Called whenever a
560 * driver is bound to a device. Invokes the driver's add() ops.
563 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
567 if (!device || !driver)
570 if (!driver->ops.add)
573 result = driver->ops.add(device);
575 device->driver = NULL;
576 device->driver_data = NULL;
580 device->driver = driver;
583 * TBD - Configuration Management: Assign resources to device based
584 * upon possible configuration and currently allocated resources.
587 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
588 "Driver successfully bound to device\n"));
592 static int acpi_start_single_object(struct acpi_device *device)
595 struct acpi_driver *driver;
598 if (!(driver = device->driver))
601 if (driver->ops.start) {
602 result = driver->ops.start(device);
603 if (result && driver->ops.remove)
604 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
611 * acpi_bus_register_driver - register a driver with the ACPI bus
612 * @driver: driver being registered
614 * Registers a driver with the ACPI bus. Searches the namespace for all
615 * devices that match the driver's criteria and binds. Returns zero for
616 * success or a negative error status for failure.
618 int acpi_bus_register_driver(struct acpi_driver *driver)
624 driver->drv.name = driver->name;
625 driver->drv.bus = &acpi_bus_type;
626 driver->drv.owner = driver->owner;
628 ret = driver_register(&driver->drv);
632 EXPORT_SYMBOL(acpi_bus_register_driver);
635 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
636 * @driver: driver to unregister
638 * Unregisters a driver with the ACPI bus. Searches the namespace for all
639 * devices that match the driver's criteria and unbinds.
641 void acpi_bus_unregister_driver(struct acpi_driver *driver)
643 driver_unregister(&driver->drv);
646 EXPORT_SYMBOL(acpi_bus_unregister_driver);
648 /* --------------------------------------------------------------------------
650 -------------------------------------------------------------------------- */
651 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
655 struct acpi_device *device;
658 * Fixed hardware devices do not appear in the namespace and do not
659 * have handles, but we fabricate acpi_devices for them, so we have
660 * to deal with them specially.
666 status = acpi_get_parent(handle, &handle);
667 if (status == AE_NULL_ENTRY)
669 if (ACPI_FAILURE(status))
672 ret = acpi_bus_get_device(handle, &device);
679 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
683 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
684 union acpi_object *obj;
686 status = acpi_get_handle(handle, "_EJD", &tmp);
687 if (ACPI_FAILURE(status))
690 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
691 if (ACPI_SUCCESS(status)) {
692 obj = buffer.pointer;
693 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
695 kfree(buffer.pointer);
699 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
701 void acpi_bus_data_handler(acpi_handle handle, void *context)
709 static int acpi_bus_get_perf_flags(struct acpi_device *device)
711 device->performance.state = ACPI_STATE_UNKNOWN;
716 acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
717 union acpi_object *package)
720 union acpi_object *element = NULL;
722 if (!device || !package || (package->package.count < 2))
723 return AE_BAD_PARAMETER;
725 element = &(package->package.elements[0]);
727 return AE_BAD_PARAMETER;
728 if (element->type == ACPI_TYPE_PACKAGE) {
729 if ((element->package.count < 2) ||
730 (element->package.elements[0].type !=
731 ACPI_TYPE_LOCAL_REFERENCE)
732 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
734 device->wakeup.gpe_device =
735 element->package.elements[0].reference.handle;
736 device->wakeup.gpe_number =
737 (u32) element->package.elements[1].integer.value;
738 } else if (element->type == ACPI_TYPE_INTEGER) {
739 device->wakeup.gpe_number = element->integer.value;
743 element = &(package->package.elements[1]);
744 if (element->type != ACPI_TYPE_INTEGER) {
747 device->wakeup.sleep_state = element->integer.value;
749 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
752 device->wakeup.resources.count = package->package.count - 2;
753 for (i = 0; i < device->wakeup.resources.count; i++) {
754 element = &(package->package.elements[i + 2]);
755 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
758 device->wakeup.resources.handles[i] = element->reference.handle;
764 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
766 acpi_status status = 0;
767 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
768 union acpi_object *package = NULL;
771 struct acpi_device_id button_device_ids[] = {
779 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
780 if (ACPI_FAILURE(status)) {
781 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
785 package = (union acpi_object *)buffer.pointer;
786 status = acpi_bus_extract_wakeup_device_power_package(device, package);
787 if (ACPI_FAILURE(status)) {
788 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
792 kfree(buffer.pointer);
794 device->wakeup.flags.valid = 1;
795 device->wakeup.prepare_count = 0;
796 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
797 * system for the ACPI device with the _PRW object.
798 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
799 * So it is necessary to call _DSW object first. Only when it is not
800 * present will the _PSW object used.
802 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
804 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
805 "error in _DSW or _PSW evaluation\n"));
807 /* Power button, Lid switch always enable wakeup */
808 if (!acpi_match_device_ids(device, button_device_ids))
809 device->wakeup.flags.run_wake = 1;
812 if (ACPI_FAILURE(status))
813 device->flags.wake_capable = 0;
817 static int acpi_bus_get_power_flags(struct acpi_device *device)
819 acpi_status status = 0;
820 acpi_handle handle = NULL;
825 * Power Management Flags
827 status = acpi_get_handle(device->handle, "_PSC", &handle);
828 if (ACPI_SUCCESS(status))
829 device->power.flags.explicit_get = 1;
830 status = acpi_get_handle(device->handle, "_IRC", &handle);
831 if (ACPI_SUCCESS(status))
832 device->power.flags.inrush_current = 1;
835 * Enumerate supported power management states
837 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
838 struct acpi_device_power_state *ps = &device->power.states[i];
839 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
841 /* Evaluate "_PRx" to se if power resources are referenced */
842 acpi_evaluate_reference(device->handle, object_name, NULL,
844 if (ps->resources.count) {
845 device->power.flags.power_resources = 1;
849 /* Evaluate "_PSx" to see if we can do explicit sets */
850 object_name[2] = 'S';
851 status = acpi_get_handle(device->handle, object_name, &handle);
852 if (ACPI_SUCCESS(status)) {
853 ps->flags.explicit_set = 1;
857 /* State is valid if we have some power control */
858 if (ps->resources.count || ps->flags.explicit_set)
861 ps->power = -1; /* Unknown - driver assigned */
862 ps->latency = -1; /* Unknown - driver assigned */
865 /* Set defaults for D0 and D3 states (always valid) */
866 device->power.states[ACPI_STATE_D0].flags.valid = 1;
867 device->power.states[ACPI_STATE_D0].power = 100;
868 device->power.states[ACPI_STATE_D3].flags.valid = 1;
869 device->power.states[ACPI_STATE_D3].power = 0;
871 /* TBD: System wake support and resource requirements. */
873 device->power.state = ACPI_STATE_UNKNOWN;
874 acpi_bus_get_power(device->handle, &(device->power.state));
879 static int acpi_bus_get_flags(struct acpi_device *device)
881 acpi_status status = AE_OK;
882 acpi_handle temp = NULL;
885 /* Presence of _STA indicates 'dynamic_status' */
886 status = acpi_get_handle(device->handle, "_STA", &temp);
887 if (ACPI_SUCCESS(status))
888 device->flags.dynamic_status = 1;
890 /* Presence of _RMV indicates 'removable' */
891 status = acpi_get_handle(device->handle, "_RMV", &temp);
892 if (ACPI_SUCCESS(status))
893 device->flags.removable = 1;
895 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
896 status = acpi_get_handle(device->handle, "_EJD", &temp);
897 if (ACPI_SUCCESS(status))
898 device->flags.ejectable = 1;
900 status = acpi_get_handle(device->handle, "_EJ0", &temp);
901 if (ACPI_SUCCESS(status))
902 device->flags.ejectable = 1;
905 /* Presence of _LCK indicates 'lockable' */
906 status = acpi_get_handle(device->handle, "_LCK", &temp);
907 if (ACPI_SUCCESS(status))
908 device->flags.lockable = 1;
910 /* Presence of _PS0|_PR0 indicates 'power manageable' */
911 status = acpi_get_handle(device->handle, "_PS0", &temp);
912 if (ACPI_FAILURE(status))
913 status = acpi_get_handle(device->handle, "_PR0", &temp);
914 if (ACPI_SUCCESS(status))
915 device->flags.power_manageable = 1;
917 /* Presence of _PRW indicates wake capable */
918 status = acpi_get_handle(device->handle, "_PRW", &temp);
919 if (ACPI_SUCCESS(status))
920 device->flags.wake_capable = 1;
922 /* TBD: Performance management */
927 static void acpi_device_get_busid(struct acpi_device *device)
929 char bus_id[5] = { '?', 0 };
930 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
936 * The device's Bus ID is simply the object name.
937 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
939 if (ACPI_IS_ROOT_DEVICE(device)) {
940 strcpy(device->pnp.bus_id, "ACPI");
944 switch (device->device_type) {
945 case ACPI_BUS_TYPE_POWER_BUTTON:
946 strcpy(device->pnp.bus_id, "PWRF");
948 case ACPI_BUS_TYPE_SLEEP_BUTTON:
949 strcpy(device->pnp.bus_id, "SLPF");
952 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
953 /* Clean up trailing underscores (if any) */
954 for (i = 3; i > 1; i--) {
955 if (bus_id[i] == '_')
960 strcpy(device->pnp.bus_id, bus_id);
966 * acpi_bay_match - see if a device is an ejectable driver bay
968 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
969 * then we can safely call it an ejectable drive bay
971 static int acpi_bay_match(struct acpi_device *device){
977 handle = device->handle;
979 status = acpi_get_handle(handle, "_EJ0", &tmp);
980 if (ACPI_FAILURE(status))
983 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
984 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
985 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
986 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
989 if (acpi_get_parent(handle, &phandle))
992 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
993 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
994 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
995 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1002 * acpi_dock_match - see if a device has a _DCK method
1004 static int acpi_dock_match(struct acpi_device *device)
1007 return acpi_get_handle(device->handle, "_DCK", &tmp);
1010 char *acpi_device_hid(struct acpi_device *device)
1012 struct acpi_hardware_id *hid;
1014 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1017 EXPORT_SYMBOL(acpi_device_hid);
1019 static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1021 struct acpi_hardware_id *id;
1023 id = kmalloc(sizeof(*id), GFP_KERNEL);
1027 id->id = kmalloc(strlen(dev_id) + 1, GFP_KERNEL);
1033 strcpy(id->id, dev_id);
1034 list_add_tail(&id->list, &device->pnp.ids);
1037 static void acpi_device_set_id(struct acpi_device *device)
1040 struct acpi_device_info *info;
1041 struct acpica_device_id_list *cid_list;
1044 switch (device->device_type) {
1045 case ACPI_BUS_TYPE_DEVICE:
1046 if (ACPI_IS_ROOT_DEVICE(device)) {
1047 acpi_add_id(device, ACPI_SYSTEM_HID);
1049 } else if (ACPI_IS_ROOT_DEVICE(device->parent)) {
1050 /* \_SB_, the only root-level namespace device */
1051 acpi_add_id(device, ACPI_BUS_HID);
1052 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1053 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1057 status = acpi_get_object_info(device->handle, &info);
1058 if (ACPI_FAILURE(status)) {
1059 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1063 if (info->valid & ACPI_VALID_HID)
1064 acpi_add_id(device, info->hardware_id.string);
1065 if (info->valid & ACPI_VALID_CID) {
1066 cid_list = &info->compatible_id_list;
1067 for (i = 0; i < cid_list->count; i++)
1068 acpi_add_id(device, cid_list->ids[i].string);
1070 #ifdef CONFIG_PCI_GUESTDEV
1071 if (info->valid & ACPI_VALID_UID)
1072 device->pnp.unique_id = kstrdup(info->unique_id.string,
1075 if (info->valid & ACPI_VALID_ADR) {
1076 device->pnp.bus_address = info->address;
1077 device->flags.bus_address = 1;
1083 * Some devices don't reliably have _HIDs & _CIDs, so add
1084 * synthetic HIDs to make sure drivers can find them.
1086 if (acpi_is_video_device(device))
1087 acpi_add_id(device, ACPI_VIDEO_HID);
1088 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1089 acpi_add_id(device, ACPI_BAY_HID);
1090 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1091 acpi_add_id(device, ACPI_DOCK_HID);
1094 case ACPI_BUS_TYPE_POWER:
1095 acpi_add_id(device, ACPI_POWER_HID);
1097 case ACPI_BUS_TYPE_PROCESSOR:
1098 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
1100 case ACPI_BUS_TYPE_THERMAL:
1101 acpi_add_id(device, ACPI_THERMAL_HID);
1103 case ACPI_BUS_TYPE_POWER_BUTTON:
1104 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
1106 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1107 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
1112 * We build acpi_devices for some objects that don't have _HID or _CID,
1113 * e.g., PCI bridges and slots. Drivers can't bind to these objects,
1114 * but we do use them indirectly by traversing the acpi_device tree.
1115 * This generic ID isn't useful for driver binding, but it provides
1116 * the useful property that "every acpi_device has an ID."
1118 if (list_empty(&device->pnp.ids))
1119 acpi_add_id(device, "device");
1122 static int acpi_device_set_context(struct acpi_device *device)
1129 * Attach this 'struct acpi_device' to the ACPI object. This makes
1130 * resolutions from handle->device very efficient. Fixed hardware
1131 * devices have no handles, so we skip them.
1133 if (!device->handle)
1136 status = acpi_attach_data(device->handle,
1137 acpi_bus_data_handler, device);
1138 if (ACPI_SUCCESS(status))
1141 printk(KERN_ERR PREFIX "Error attaching device data\n");
1145 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1150 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1151 device_release_driver(&dev->dev);
1157 * unbind _ADR-Based Devices when hot removal
1159 if (dev->flags.bus_address) {
1160 if ((dev->parent) && (dev->parent->ops.unbind))
1161 dev->parent->ops.unbind(dev);
1163 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1168 static int acpi_add_single_object(struct acpi_device **child,
1169 acpi_handle handle, int type,
1170 unsigned long long sta,
1171 struct acpi_bus_ops *ops)
1174 struct acpi_device *device;
1175 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1177 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1179 printk(KERN_ERR PREFIX "Memory allocation error\n");
1183 INIT_LIST_HEAD(&device->pnp.ids);
1184 device->device_type = type;
1185 device->handle = handle;
1186 device->parent = acpi_bus_get_parent(handle);
1187 device->bus_ops = *ops; /* workround for not call .start */
1188 STRUCT_TO_INT(device->status) = sta;
1190 acpi_device_get_busid(device);
1195 * Note that we only look for object handles -- cannot evaluate objects
1196 * until we know the device is present and properly initialized.
1198 result = acpi_bus_get_flags(device);
1205 * TBD: Synch with Core's enumeration/initialization process.
1207 acpi_device_set_id(device);
1213 if (device->flags.power_manageable) {
1214 result = acpi_bus_get_power_flags(device);
1220 * Wakeup device management
1221 *-----------------------
1223 if (device->flags.wake_capable) {
1224 result = acpi_bus_get_wakeup_device_flags(device);
1230 * Performance Management
1231 * ----------------------
1233 if (device->flags.performance_manageable) {
1234 result = acpi_bus_get_perf_flags(device);
1239 if ((result = acpi_device_set_context(device)))
1242 result = acpi_device_register(device);
1245 * Bind _ADR-Based Devices when hot add
1247 if (device->flags.bus_address) {
1248 if (device->parent && device->parent->ops.bind)
1249 device->parent->ops.bind(device);
1254 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1255 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1256 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1257 (char *) buffer.pointer,
1258 device->parent ? dev_name(&device->parent->dev) :
1260 kfree(buffer.pointer);
1263 acpi_device_release(&device->dev);
1268 #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1269 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1271 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1272 unsigned long long *sta)
1275 acpi_object_type acpi_type;
1277 status = acpi_get_type(handle, &acpi_type);
1278 if (ACPI_FAILURE(status))
1281 switch (acpi_type) {
1282 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1283 case ACPI_TYPE_DEVICE:
1284 *type = ACPI_BUS_TYPE_DEVICE;
1285 status = acpi_bus_get_status_handle(handle, sta);
1286 if (ACPI_FAILURE(status))
1289 case ACPI_TYPE_PROCESSOR:
1290 *type = ACPI_BUS_TYPE_PROCESSOR;
1291 status = acpi_bus_get_status_handle(handle, sta);
1292 if (ACPI_FAILURE(status))
1295 case ACPI_TYPE_THERMAL:
1296 *type = ACPI_BUS_TYPE_THERMAL;
1297 *sta = ACPI_STA_DEFAULT;
1299 case ACPI_TYPE_POWER:
1300 *type = ACPI_BUS_TYPE_POWER;
1301 *sta = ACPI_STA_DEFAULT;
1310 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1311 void *context, void **return_value)
1313 struct acpi_bus_ops *ops = context;
1315 unsigned long long sta;
1316 struct acpi_device *device;
1320 result = acpi_bus_type_and_status(handle, &type, &sta);
1324 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1325 !(sta & ACPI_STA_DEVICE_FUNCTIONING))
1326 return AE_CTRL_DEPTH;
1329 * We may already have an acpi_device from a previous enumeration. If
1330 * so, we needn't add it again, but we may still have to start it.
1333 acpi_bus_get_device(handle, &device);
1334 if (ops->acpi_op_add && !device)
1335 acpi_add_single_object(&device, handle, type, sta, ops);
1338 return AE_CTRL_DEPTH;
1340 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1341 status = acpi_start_single_object(device);
1342 if (ACPI_FAILURE(status))
1343 return AE_CTRL_DEPTH;
1347 *return_value = device;
1351 static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1352 struct acpi_device **child)
1355 void *device = NULL;
1357 status = acpi_bus_check_add(handle, 0, ops, &device);
1358 if (ACPI_SUCCESS(status))
1359 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1360 acpi_bus_check_add, NULL, ops, &device);
1372 * acpi_bus_add and acpi_bus_start
1374 * scan a given ACPI tree and (probably recently hot-plugged)
1375 * create and add or starts found devices.
1377 * If no devices were found -ENODEV is returned which does not
1378 * mean that this is a real error, there just have been no suitable
1379 * ACPI objects in the table trunk from which the kernel could create
1380 * a device and add/start an appropriate driver.
1384 acpi_bus_add(struct acpi_device **child,
1385 struct acpi_device *parent, acpi_handle handle, int type)
1387 struct acpi_bus_ops ops;
1389 memset(&ops, 0, sizeof(ops));
1390 ops.acpi_op_add = 1;
1392 return acpi_bus_scan(handle, &ops, child);
1394 EXPORT_SYMBOL(acpi_bus_add);
1396 int acpi_bus_start(struct acpi_device *device)
1398 struct acpi_bus_ops ops;
1400 memset(&ops, 0, sizeof(ops));
1401 ops.acpi_op_start = 1;
1403 return acpi_bus_scan(device->handle, &ops, NULL);
1405 EXPORT_SYMBOL(acpi_bus_start);
1407 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1410 struct acpi_device *parent, *child;
1411 acpi_handle phandle, chandle;
1412 acpi_object_type type;
1417 phandle = start->handle;
1418 child = chandle = NULL;
1420 while ((level > 0) && parent && (!err)) {
1421 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1425 * If this scope is exhausted then move our way back up.
1427 if (ACPI_FAILURE(status)) {
1430 acpi_get_parent(phandle, &phandle);
1432 parent = parent->parent;
1435 err = acpi_bus_remove(child, rmdevice);
1437 err = acpi_bus_remove(child, 1);
1442 status = acpi_get_type(chandle, &type);
1443 if (ACPI_FAILURE(status)) {
1447 * If there is a device corresponding to chandle then
1448 * parse it (depth-first).
1450 if (acpi_bus_get_device(chandle, &child) == 0) {
1460 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1462 static int acpi_bus_scan_fixed(void)
1465 struct acpi_device *device = NULL;
1466 struct acpi_bus_ops ops;
1468 memset(&ops, 0, sizeof(ops));
1469 ops.acpi_op_add = 1;
1470 ops.acpi_op_start = 1;
1473 * Enumerate all fixed-feature devices.
1475 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
1476 result = acpi_add_single_object(&device, NULL,
1477 ACPI_BUS_TYPE_POWER_BUTTON,
1482 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1483 result = acpi_add_single_object(&device, NULL,
1484 ACPI_BUS_TYPE_SLEEP_BUTTON,
1492 int __init acpi_scan_init(void)
1495 struct acpi_bus_ops ops;
1497 memset(&ops, 0, sizeof(ops));
1498 ops.acpi_op_add = 1;
1499 ops.acpi_op_start = 1;
1501 result = bus_register(&acpi_bus_type);
1503 /* We don't want to quit even if we failed to add suspend/resume */
1504 printk(KERN_ERR PREFIX "Could not register bus type\n");
1508 * Enumerate devices in the ACPI namespace.
1510 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
1513 result = acpi_bus_scan_fixed();
1516 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);