2 * scan.c - support for transforming the ACPI namespace into individual objects
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/slab.h>
8 #include <linux/kernel.h>
9 #include <linux/acpi.h>
10 #include <linux/signal.h>
11 #include <linux/kthread.h>
12 #include <linux/dmi.h>
13 #include <linux/nls.h>
15 #include <acpi/acpi_drivers.h>
19 #define _COMPONENT ACPI_BUS_COMPONENT
20 ACPI_MODULE_NAME("scan");
21 #define STRUCT_TO_INT(s) (*((int*)&s))
22 extern struct acpi_device *acpi_root;
24 #define ACPI_BUS_CLASS "system_bus"
25 #define ACPI_BUS_HID "LNXSYBUS"
26 #define ACPI_BUS_DEVICE_NAME "System Bus"
28 #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
30 static const char *dummy_hid = "device";
32 static LIST_HEAD(acpi_device_list);
33 static LIST_HEAD(acpi_bus_id_list);
34 static DEFINE_MUTEX(acpi_scan_lock);
35 static LIST_HEAD(acpi_scan_handlers_list);
36 DEFINE_MUTEX(acpi_device_lock);
37 LIST_HEAD(acpi_wakeup_device_list);
39 struct acpi_device_bus_id{
41 unsigned int instance_no;
42 struct list_head node;
45 int acpi_scan_add_handler(struct acpi_scan_handler *handler)
47 if (!handler || !handler->attach)
50 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
55 * Creates hid/cid(s) string needed for modalias and uevent
56 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
57 * char *modalias: "acpi:IBM0001:ACPI0001"
59 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
64 struct acpi_hardware_id *id;
66 if (list_empty(&acpi_dev->pnp.ids))
69 len = snprintf(modalias, size, "acpi:");
72 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
73 count = snprintf(&modalias[len], size, "%s:", id->id);
74 if (count < 0 || count >= size)
85 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
86 struct acpi_device *acpi_dev = to_acpi_device(dev);
89 /* Device has no HID and no CID or string is >1024 */
90 len = create_modalias(acpi_dev, buf, 1024);
96 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
99 * acpi_bus_hot_remove_device: hot-remove a device and its children
100 * @context: struct acpi_eject_event pointer (freed in this func)
102 * Hot-remove a device and its children. This function frees up the
103 * memory space passed by arg context, so that the caller may call
104 * this function asynchronously through acpi_os_hotplug_execute().
106 void acpi_bus_hot_remove_device(void *context)
108 struct acpi_eject_event *ej_event = (struct acpi_eject_event *) context;
109 struct acpi_device *device = ej_event->device;
110 acpi_handle handle = device->handle;
112 struct acpi_object_list arg_list;
113 union acpi_object arg;
114 acpi_status status = AE_OK;
115 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
117 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
118 "Hot-removing device %s...\n", dev_name(&device->dev)));
120 acpi_bus_trim(device);
121 /* Device node has been released. */
124 /* power off device */
125 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
126 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
127 printk(KERN_WARNING PREFIX
128 "Power-off device failed\n");
130 if (ACPI_SUCCESS(acpi_get_handle(handle, "_LCK", &temp))) {
132 arg_list.pointer = &arg;
133 arg.type = ACPI_TYPE_INTEGER;
134 arg.integer.value = 0;
135 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
139 arg_list.pointer = &arg;
140 arg.type = ACPI_TYPE_INTEGER;
141 arg.integer.value = 1;
146 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
147 if (ACPI_FAILURE(status)) {
148 if (status != AE_NOT_FOUND)
149 printk(KERN_WARNING PREFIX
150 "Eject device failed\n");
158 /* Inform firmware the hot-remove operation has completed w/ error */
159 (void) acpi_evaluate_hotplug_ost(handle,
160 ej_event->event, ost_code, NULL);
164 EXPORT_SYMBOL(acpi_bus_hot_remove_device);
166 static ssize_t real_power_state_show(struct device *dev,
167 struct device_attribute *attr, char *buf)
169 struct acpi_device *adev = to_acpi_device(dev);
173 ret = acpi_device_get_power(adev, &state);
177 return sprintf(buf, "%s\n", acpi_power_state_string(state));
180 static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
182 static ssize_t power_state_show(struct device *dev,
183 struct device_attribute *attr, char *buf)
185 struct acpi_device *adev = to_acpi_device(dev);
187 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
190 static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
193 acpi_eject_store(struct device *d, struct device_attribute *attr,
194 const char *buf, size_t count)
198 acpi_object_type type = 0;
199 struct acpi_device *acpi_device = to_acpi_device(d);
200 struct acpi_eject_event *ej_event;
202 if ((!count) || (buf[0] != '1')) {
205 if (!acpi_device->driver && !acpi_device->handler) {
209 status = acpi_get_type(acpi_device->handle, &type);
210 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
215 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
221 ej_event->device = acpi_device;
222 if (acpi_device->flags.eject_pending) {
223 /* event originated from ACPI eject notification */
224 ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
225 acpi_device->flags.eject_pending = 0;
227 /* event originated from user */
228 ej_event->event = ACPI_OST_EC_OSPM_EJECT;
229 (void) acpi_evaluate_hotplug_ost(acpi_device->handle,
230 ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
233 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, (void *)ej_event);
238 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
241 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
242 struct acpi_device *acpi_dev = to_acpi_device(dev);
244 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
246 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
248 static ssize_t acpi_device_uid_show(struct device *dev,
249 struct device_attribute *attr, char *buf)
251 struct acpi_device *acpi_dev = to_acpi_device(dev);
253 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
255 static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
257 static ssize_t acpi_device_adr_show(struct device *dev,
258 struct device_attribute *attr, char *buf)
260 struct acpi_device *acpi_dev = to_acpi_device(dev);
262 return sprintf(buf, "0x%08x\n",
263 (unsigned int)(acpi_dev->pnp.bus_address));
265 static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
268 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
269 struct acpi_device *acpi_dev = to_acpi_device(dev);
270 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
273 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
277 result = sprintf(buf, "%s\n", (char*)path.pointer);
282 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
284 /* sysfs file that shows description text from the ACPI _STR method */
285 static ssize_t description_show(struct device *dev,
286 struct device_attribute *attr,
288 struct acpi_device *acpi_dev = to_acpi_device(dev);
291 if (acpi_dev->pnp.str_obj == NULL)
295 * The _STR object contains a Unicode identifier for a device.
296 * We need to convert to utf-8 so it can be displayed.
298 result = utf16s_to_utf8s(
299 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
300 acpi_dev->pnp.str_obj->buffer.length,
301 UTF16_LITTLE_ENDIAN, buf,
304 buf[result++] = '\n';
308 static DEVICE_ATTR(description, 0444, description_show, NULL);
311 acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
313 struct acpi_device *acpi_dev = to_acpi_device(dev);
315 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
317 static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
319 static int acpi_device_setup_files(struct acpi_device *dev)
321 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
324 unsigned long long sun;
328 * Devices gotten from FADT don't have a "path" attribute
331 result = device_create_file(&dev->dev, &dev_attr_path);
336 if (!list_empty(&dev->pnp.ids)) {
337 result = device_create_file(&dev->dev, &dev_attr_hid);
341 result = device_create_file(&dev->dev, &dev_attr_modalias);
347 * If device has _STR, 'description' file is created
349 status = acpi_get_handle(dev->handle, "_STR", &temp);
350 if (ACPI_SUCCESS(status)) {
351 status = acpi_evaluate_object(dev->handle, "_STR",
353 if (ACPI_FAILURE(status))
354 buffer.pointer = NULL;
355 dev->pnp.str_obj = buffer.pointer;
356 result = device_create_file(&dev->dev, &dev_attr_description);
361 if (dev->flags.bus_address)
362 result = device_create_file(&dev->dev, &dev_attr_adr);
363 if (dev->pnp.unique_id)
364 result = device_create_file(&dev->dev, &dev_attr_uid);
366 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
367 if (ACPI_SUCCESS(status)) {
368 dev->pnp.sun = (unsigned long)sun;
369 result = device_create_file(&dev->dev, &dev_attr_sun);
373 dev->pnp.sun = (unsigned long)-1;
377 * If device has _EJ0, 'eject' file is created that is used to trigger
378 * hot-removal function from userland.
380 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
381 if (ACPI_SUCCESS(status)) {
382 result = device_create_file(&dev->dev, &dev_attr_eject);
387 if (dev->flags.power_manageable) {
388 result = device_create_file(&dev->dev, &dev_attr_power_state);
392 if (dev->power.flags.power_resources)
393 result = device_create_file(&dev->dev,
394 &dev_attr_real_power_state);
401 static void acpi_device_remove_files(struct acpi_device *dev)
406 if (dev->flags.power_manageable) {
407 device_remove_file(&dev->dev, &dev_attr_power_state);
408 if (dev->power.flags.power_resources)
409 device_remove_file(&dev->dev,
410 &dev_attr_real_power_state);
414 * If device has _STR, remove 'description' file
416 status = acpi_get_handle(dev->handle, "_STR", &temp);
417 if (ACPI_SUCCESS(status)) {
418 kfree(dev->pnp.str_obj);
419 device_remove_file(&dev->dev, &dev_attr_description);
422 * If device has _EJ0, remove 'eject' file.
424 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
425 if (ACPI_SUCCESS(status))
426 device_remove_file(&dev->dev, &dev_attr_eject);
428 status = acpi_get_handle(dev->handle, "_SUN", &temp);
429 if (ACPI_SUCCESS(status))
430 device_remove_file(&dev->dev, &dev_attr_sun);
432 if (dev->pnp.unique_id)
433 device_remove_file(&dev->dev, &dev_attr_uid);
434 if (dev->flags.bus_address)
435 device_remove_file(&dev->dev, &dev_attr_adr);
436 device_remove_file(&dev->dev, &dev_attr_modalias);
437 device_remove_file(&dev->dev, &dev_attr_hid);
439 device_remove_file(&dev->dev, &dev_attr_path);
441 /* --------------------------------------------------------------------------
443 -------------------------------------------------------------------------- */
445 static const struct acpi_device_id *__acpi_match_device(
446 struct acpi_device *device, const struct acpi_device_id *ids)
448 const struct acpi_device_id *id;
449 struct acpi_hardware_id *hwid;
452 * If the device is not present, it is unnecessary to load device
455 if (!device->status.present)
458 for (id = ids; id->id[0]; id++)
459 list_for_each_entry(hwid, &device->pnp.ids, list)
460 if (!strcmp((char *) id->id, hwid->id))
467 * acpi_match_device - Match a struct device against a given list of ACPI IDs
468 * @ids: Array of struct acpi_device_id object to match against.
469 * @dev: The device structure to match.
471 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
472 * object for that handle and use that object to match against a given list of
475 * Return a pointer to the first matching ID on success or %NULL on failure.
477 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
478 const struct device *dev)
480 struct acpi_device *adev;
482 if (!ids || !ACPI_HANDLE(dev)
483 || ACPI_FAILURE(acpi_bus_get_device(ACPI_HANDLE(dev), &adev)))
486 return __acpi_match_device(adev, ids);
488 EXPORT_SYMBOL_GPL(acpi_match_device);
490 int acpi_match_device_ids(struct acpi_device *device,
491 const struct acpi_device_id *ids)
493 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
495 EXPORT_SYMBOL(acpi_match_device_ids);
497 void acpi_free_ids(struct acpi_device *device)
499 struct acpi_hardware_id *id, *tmp;
501 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
505 kfree(device->pnp.unique_id);
508 static void acpi_free_power_resources_lists(struct acpi_device *device)
512 if (device->wakeup.flags.valid)
513 acpi_power_resources_list_free(&device->wakeup.resources);
515 if (!device->flags.power_manageable)
518 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
519 struct acpi_device_power_state *ps = &device->power.states[i];
520 acpi_power_resources_list_free(&ps->resources);
524 static void acpi_device_release(struct device *dev)
526 struct acpi_device *acpi_dev = to_acpi_device(dev);
528 acpi_free_ids(acpi_dev);
529 acpi_free_power_resources_lists(acpi_dev);
533 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
535 struct acpi_device *acpi_dev = to_acpi_device(dev);
536 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
538 return acpi_dev->flags.match_driver
539 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
542 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
544 struct acpi_device *acpi_dev = to_acpi_device(dev);
547 if (list_empty(&acpi_dev->pnp.ids))
550 if (add_uevent_var(env, "MODALIAS="))
552 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
553 sizeof(env->buf) - env->buflen);
554 if (len >= (sizeof(env->buf) - env->buflen))
560 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
562 struct acpi_device *device = data;
564 device->driver->ops.notify(device, event);
567 static acpi_status acpi_device_notify_fixed(void *data)
569 struct acpi_device *device = data;
571 /* Fixed hardware devices have no handles */
572 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
576 static int acpi_device_install_notify_handler(struct acpi_device *device)
580 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
582 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
583 acpi_device_notify_fixed,
585 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
587 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
588 acpi_device_notify_fixed,
591 status = acpi_install_notify_handler(device->handle,
596 if (ACPI_FAILURE(status))
601 static void acpi_device_remove_notify_handler(struct acpi_device *device)
603 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
604 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
605 acpi_device_notify_fixed);
606 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
607 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
608 acpi_device_notify_fixed);
610 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
614 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
615 static int acpi_device_probe(struct device * dev)
617 struct acpi_device *acpi_dev = to_acpi_device(dev);
618 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
621 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
623 if (acpi_drv->ops.notify) {
624 ret = acpi_device_install_notify_handler(acpi_dev);
626 if (acpi_drv->ops.remove)
627 acpi_drv->ops.remove(acpi_dev);
628 acpi_dev->driver = NULL;
629 acpi_dev->driver_data = NULL;
634 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
635 "Found driver [%s] for device [%s]\n",
636 acpi_drv->name, acpi_dev->pnp.bus_id));
642 static int acpi_device_remove(struct device * dev)
644 struct acpi_device *acpi_dev = to_acpi_device(dev);
645 struct acpi_driver *acpi_drv = acpi_dev->driver;
648 if (acpi_drv->ops.notify)
649 acpi_device_remove_notify_handler(acpi_dev);
650 if (acpi_drv->ops.remove)
651 acpi_drv->ops.remove(acpi_dev);
653 acpi_dev->driver = NULL;
654 acpi_dev->driver_data = NULL;
660 struct bus_type acpi_bus_type = {
662 .match = acpi_bus_match,
663 .probe = acpi_device_probe,
664 .remove = acpi_device_remove,
665 .uevent = acpi_device_uevent,
668 int acpi_device_add(struct acpi_device *device,
669 void (*release)(struct device *))
672 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
675 if (device->handle) {
678 status = acpi_attach_data(device->handle, acpi_bus_data_handler,
680 if (ACPI_FAILURE(status)) {
681 acpi_handle_err(device->handle,
682 "Unable to attach device data\n");
690 * Link this device to its parent and siblings.
692 INIT_LIST_HEAD(&device->children);
693 INIT_LIST_HEAD(&device->node);
694 INIT_LIST_HEAD(&device->wakeup_list);
695 INIT_LIST_HEAD(&device->physical_node_list);
696 mutex_init(&device->physical_node_lock);
697 INIT_LIST_HEAD(&device->power_dependent);
699 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
701 pr_err(PREFIX "Memory allocation error\n");
706 mutex_lock(&acpi_device_lock);
708 * Find suitable bus_id and instance number in acpi_bus_id_list
709 * If failed, create one and link it into acpi_bus_id_list
711 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
712 if (!strcmp(acpi_device_bus_id->bus_id,
713 acpi_device_hid(device))) {
714 acpi_device_bus_id->instance_no++;
721 acpi_device_bus_id = new_bus_id;
722 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
723 acpi_device_bus_id->instance_no = 0;
724 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
726 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
729 list_add_tail(&device->node, &device->parent->children);
731 if (device->wakeup.flags.valid)
732 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
733 mutex_unlock(&acpi_device_lock);
736 device->dev.parent = &device->parent->dev;
737 device->dev.bus = &acpi_bus_type;
738 device->dev.release = release;
739 result = device_add(&device->dev);
741 dev_err(&device->dev, "Error registering device\n");
745 result = acpi_device_setup_files(device);
747 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
748 dev_name(&device->dev));
750 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
754 mutex_lock(&acpi_device_lock);
756 list_del(&device->node);
757 list_del(&device->wakeup_list);
758 mutex_unlock(&acpi_device_lock);
761 acpi_detach_data(device->handle, acpi_bus_data_handler);
765 static void acpi_device_unregister(struct acpi_device *device)
767 mutex_lock(&acpi_device_lock);
769 list_del(&device->node);
771 list_del(&device->wakeup_list);
772 mutex_unlock(&acpi_device_lock);
774 acpi_detach_data(device->handle, acpi_bus_data_handler);
776 acpi_power_add_remove_device(device, false);
777 acpi_device_remove_files(device);
779 device->remove(device);
781 device_del(&device->dev);
783 * Drop the reference counts of all power resources the device depends
784 * on and turn off the ones that have no more references.
786 acpi_power_transition(device, ACPI_STATE_D3_COLD);
787 put_device(&device->dev);
790 /* --------------------------------------------------------------------------
792 -------------------------------------------------------------------------- */
794 * acpi_bus_driver_init - add a device to a driver
795 * @device: the device to add and initialize
796 * @driver: driver for the device
798 * Used to initialize a device via its device driver. Called whenever a
799 * driver is bound to a device. Invokes the driver's add() ops.
802 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
806 if (!device || !driver)
809 if (!driver->ops.add)
812 result = driver->ops.add(device);
814 device->driver = NULL;
815 device->driver_data = NULL;
819 device->driver = driver;
822 * TBD - Configuration Management: Assign resources to device based
823 * upon possible configuration and currently allocated resources.
826 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
827 "Driver successfully bound to device\n"));
832 * acpi_bus_register_driver - register a driver with the ACPI bus
833 * @driver: driver being registered
835 * Registers a driver with the ACPI bus. Searches the namespace for all
836 * devices that match the driver's criteria and binds. Returns zero for
837 * success or a negative error status for failure.
839 int acpi_bus_register_driver(struct acpi_driver *driver)
845 driver->drv.name = driver->name;
846 driver->drv.bus = &acpi_bus_type;
847 driver->drv.owner = driver->owner;
849 ret = driver_register(&driver->drv);
853 EXPORT_SYMBOL(acpi_bus_register_driver);
856 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
857 * @driver: driver to unregister
859 * Unregisters a driver with the ACPI bus. Searches the namespace for all
860 * devices that match the driver's criteria and unbinds.
862 void acpi_bus_unregister_driver(struct acpi_driver *driver)
864 driver_unregister(&driver->drv);
867 EXPORT_SYMBOL(acpi_bus_unregister_driver);
869 /* --------------------------------------------------------------------------
871 -------------------------------------------------------------------------- */
872 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
874 struct acpi_device *device = NULL;
878 * Fixed hardware devices do not appear in the namespace and do not
879 * have handles, but we fabricate acpi_devices for them, so we have
880 * to deal with them specially.
886 status = acpi_get_parent(handle, &handle);
887 if (ACPI_FAILURE(status))
888 return status == AE_NULL_ENTRY ? NULL : acpi_root;
889 } while (acpi_bus_get_device(handle, &device));
894 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
898 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
899 union acpi_object *obj;
901 status = acpi_get_handle(handle, "_EJD", &tmp);
902 if (ACPI_FAILURE(status))
905 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
906 if (ACPI_SUCCESS(status)) {
907 obj = buffer.pointer;
908 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
910 kfree(buffer.pointer);
914 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
916 void acpi_bus_data_handler(acpi_handle handle, void *context)
924 static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
925 struct acpi_device_wakeup *wakeup)
927 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
928 union acpi_object *package = NULL;
929 union acpi_object *element = NULL;
936 INIT_LIST_HEAD(&wakeup->resources);
939 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
940 if (ACPI_FAILURE(status)) {
941 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
945 package = (union acpi_object *)buffer.pointer;
947 if (!package || package->package.count < 2)
950 element = &(package->package.elements[0]);
954 if (element->type == ACPI_TYPE_PACKAGE) {
955 if ((element->package.count < 2) ||
956 (element->package.elements[0].type !=
957 ACPI_TYPE_LOCAL_REFERENCE)
958 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
962 element->package.elements[0].reference.handle;
964 (u32) element->package.elements[1].integer.value;
965 } else if (element->type == ACPI_TYPE_INTEGER) {
966 wakeup->gpe_device = NULL;
967 wakeup->gpe_number = element->integer.value;
972 element = &(package->package.elements[1]);
973 if (element->type != ACPI_TYPE_INTEGER)
976 wakeup->sleep_state = element->integer.value;
978 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
982 if (!list_empty(&wakeup->resources)) {
985 sleep_state = acpi_power_min_system_level(&wakeup->resources);
986 if (sleep_state < wakeup->sleep_state) {
987 acpi_handle_warn(handle, "Overriding _PRW sleep state "
988 "(S%d) by S%d from power resources\n",
989 (int)wakeup->sleep_state, sleep_state);
990 wakeup->sleep_state = sleep_state;
993 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
996 kfree(buffer.pointer);
1000 static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
1002 struct acpi_device_id button_device_ids[] = {
1009 acpi_event_status event_status;
1011 device->wakeup.flags.notifier_present = 0;
1013 /* Power button, Lid switch always enable wakeup */
1014 if (!acpi_match_device_ids(device, button_device_ids)) {
1015 device->wakeup.flags.run_wake = 1;
1016 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1017 /* Do not use Lid/sleep button for S5 wakeup */
1018 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1019 device->wakeup.sleep_state = ACPI_STATE_S4;
1021 device_set_wakeup_capable(&device->dev, true);
1025 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1026 device->wakeup.gpe_number,
1028 if (status == AE_OK)
1029 device->wakeup.flags.run_wake =
1030 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1033 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1036 acpi_status status = 0;
1039 /* Presence of _PRW indicates wake capable */
1040 status = acpi_get_handle(device->handle, "_PRW", &temp);
1041 if (ACPI_FAILURE(status))
1044 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1047 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
1051 device->wakeup.flags.valid = 1;
1052 device->wakeup.prepare_count = 0;
1053 acpi_bus_set_run_wake_flags(device);
1054 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1055 * system for the ACPI device with the _PRW object.
1056 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1057 * So it is necessary to call _DSW object first. Only when it is not
1058 * present will the _PSW object used.
1060 err = acpi_device_sleep_wake(device, 0, 0, 0);
1062 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1063 "error in _DSW or _PSW evaluation\n"));
1066 static void acpi_bus_init_power_state(struct acpi_device *device, int state)
1068 struct acpi_device_power_state *ps = &device->power.states[state];
1069 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1070 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1074 INIT_LIST_HEAD(&ps->resources);
1076 /* Evaluate "_PRx" to get referenced power resources */
1077 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1078 if (ACPI_SUCCESS(status)) {
1079 union acpi_object *package = buffer.pointer;
1081 if (buffer.length && package
1082 && package->type == ACPI_TYPE_PACKAGE
1083 && package->package.count) {
1084 int err = acpi_extract_power_resources(package, 0,
1087 device->power.flags.power_resources = 1;
1089 ACPI_FREE(buffer.pointer);
1092 /* Evaluate "_PSx" to see if we can do explicit sets */
1094 status = acpi_get_handle(device->handle, pathname, &handle);
1095 if (ACPI_SUCCESS(status))
1096 ps->flags.explicit_set = 1;
1099 * State is valid if there are means to put the device into it.
1100 * D3hot is only valid if _PR3 present.
1102 if (!list_empty(&ps->resources)
1103 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1104 ps->flags.valid = 1;
1105 ps->flags.os_accessible = 1;
1108 ps->power = -1; /* Unknown - driver assigned */
1109 ps->latency = -1; /* Unknown - driver assigned */
1112 static void acpi_bus_get_power_flags(struct acpi_device *device)
1118 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1119 status = acpi_get_handle(device->handle, "_PS0", &handle);
1120 if (ACPI_FAILURE(status)) {
1121 status = acpi_get_handle(device->handle, "_PR0", &handle);
1122 if (ACPI_FAILURE(status))
1126 device->flags.power_manageable = 1;
1129 * Power Management Flags
1131 status = acpi_get_handle(device->handle, "_PSC", &handle);
1132 if (ACPI_SUCCESS(status))
1133 device->power.flags.explicit_get = 1;
1134 status = acpi_get_handle(device->handle, "_IRC", &handle);
1135 if (ACPI_SUCCESS(status))
1136 device->power.flags.inrush_current = 1;
1139 * Enumerate supported power management states
1141 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1142 acpi_bus_init_power_state(device, i);
1144 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1146 /* Set defaults for D0 and D3 states (always valid) */
1147 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1148 device->power.states[ACPI_STATE_D0].power = 100;
1149 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1150 device->power.states[ACPI_STATE_D3].power = 0;
1152 /* Set D3cold's explicit_set flag if _PS3 exists. */
1153 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1154 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1156 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1157 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1158 device->power.flags.power_resources)
1159 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1161 acpi_bus_init_power(device);
1164 static void acpi_bus_get_flags(struct acpi_device *device)
1166 acpi_status status = AE_OK;
1167 acpi_handle temp = NULL;
1169 /* Presence of _STA indicates 'dynamic_status' */
1170 status = acpi_get_handle(device->handle, "_STA", &temp);
1171 if (ACPI_SUCCESS(status))
1172 device->flags.dynamic_status = 1;
1174 /* Presence of _RMV indicates 'removable' */
1175 status = acpi_get_handle(device->handle, "_RMV", &temp);
1176 if (ACPI_SUCCESS(status))
1177 device->flags.removable = 1;
1179 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1180 status = acpi_get_handle(device->handle, "_EJD", &temp);
1181 if (ACPI_SUCCESS(status))
1182 device->flags.ejectable = 1;
1184 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1185 if (ACPI_SUCCESS(status))
1186 device->flags.ejectable = 1;
1190 static void acpi_device_get_busid(struct acpi_device *device)
1192 char bus_id[5] = { '?', 0 };
1193 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1199 * The device's Bus ID is simply the object name.
1200 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1202 if (ACPI_IS_ROOT_DEVICE(device)) {
1203 strcpy(device->pnp.bus_id, "ACPI");
1207 switch (device->device_type) {
1208 case ACPI_BUS_TYPE_POWER_BUTTON:
1209 strcpy(device->pnp.bus_id, "PWRF");
1211 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1212 strcpy(device->pnp.bus_id, "SLPF");
1215 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1216 /* Clean up trailing underscores (if any) */
1217 for (i = 3; i > 1; i--) {
1218 if (bus_id[i] == '_')
1223 strcpy(device->pnp.bus_id, bus_id);
1229 * acpi_bay_match - see if a device is an ejectable driver bay
1231 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1232 * then we can safely call it an ejectable drive bay
1234 static int acpi_bay_match(struct acpi_device *device){
1238 acpi_handle phandle;
1240 handle = device->handle;
1242 status = acpi_get_handle(handle, "_EJ0", &tmp);
1243 if (ACPI_FAILURE(status))
1246 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1247 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1248 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1249 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1252 if (acpi_get_parent(handle, &phandle))
1255 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1256 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1257 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1258 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1265 * acpi_dock_match - see if a device has a _DCK method
1267 static int acpi_dock_match(struct acpi_device *device)
1270 return acpi_get_handle(device->handle, "_DCK", &tmp);
1273 const char *acpi_device_hid(struct acpi_device *device)
1275 struct acpi_hardware_id *hid;
1277 if (list_empty(&device->pnp.ids))
1280 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1283 EXPORT_SYMBOL(acpi_device_hid);
1285 static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1287 struct acpi_hardware_id *id;
1289 id = kmalloc(sizeof(*id), GFP_KERNEL);
1293 id->id = kstrdup(dev_id, GFP_KERNEL);
1299 list_add_tail(&id->list, &device->pnp.ids);
1303 * Old IBM workstations have a DSDT bug wherein the SMBus object
1304 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1305 * prefix. Work around this.
1307 static int acpi_ibm_smbus_match(struct acpi_device *device)
1309 acpi_handle h_dummy;
1310 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1313 if (!dmi_name_in_vendors("IBM"))
1316 /* Look for SMBS object */
1317 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1321 if (strcmp("SMBS", path.pointer)) {
1326 /* Does it have the necessary (but misnamed) methods? */
1328 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1329 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1330 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1333 kfree(path.pointer);
1337 static void acpi_device_set_id(struct acpi_device *device)
1340 struct acpi_device_info *info;
1341 struct acpi_pnp_device_id_list *cid_list;
1344 switch (device->device_type) {
1345 case ACPI_BUS_TYPE_DEVICE:
1346 if (ACPI_IS_ROOT_DEVICE(device)) {
1347 acpi_add_id(device, ACPI_SYSTEM_HID);
1351 status = acpi_get_object_info(device->handle, &info);
1352 if (ACPI_FAILURE(status)) {
1353 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1357 if (info->valid & ACPI_VALID_HID)
1358 acpi_add_id(device, info->hardware_id.string);
1359 if (info->valid & ACPI_VALID_CID) {
1360 cid_list = &info->compatible_id_list;
1361 for (i = 0; i < cid_list->count; i++)
1362 acpi_add_id(device, cid_list->ids[i].string);
1364 if (info->valid & ACPI_VALID_ADR) {
1365 device->pnp.bus_address = info->address;
1366 device->flags.bus_address = 1;
1368 if (info->valid & ACPI_VALID_UID)
1369 device->pnp.unique_id = kstrdup(info->unique_id.string,
1375 * Some devices don't reliably have _HIDs & _CIDs, so add
1376 * synthetic HIDs to make sure drivers can find them.
1378 if (acpi_is_video_device(device))
1379 acpi_add_id(device, ACPI_VIDEO_HID);
1380 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1381 acpi_add_id(device, ACPI_BAY_HID);
1382 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1383 acpi_add_id(device, ACPI_DOCK_HID);
1384 else if (!acpi_ibm_smbus_match(device))
1385 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
1386 else if (list_empty(&device->pnp.ids) &&
1387 ACPI_IS_ROOT_DEVICE(device->parent)) {
1388 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1389 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1390 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1394 case ACPI_BUS_TYPE_POWER:
1395 acpi_add_id(device, ACPI_POWER_HID);
1397 case ACPI_BUS_TYPE_PROCESSOR:
1398 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
1400 case ACPI_BUS_TYPE_THERMAL:
1401 acpi_add_id(device, ACPI_THERMAL_HID);
1403 case ACPI_BUS_TYPE_POWER_BUTTON:
1404 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
1406 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1407 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
1412 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1413 int type, unsigned long long sta)
1415 INIT_LIST_HEAD(&device->pnp.ids);
1416 device->device_type = type;
1417 device->handle = handle;
1418 device->parent = acpi_bus_get_parent(handle);
1419 STRUCT_TO_INT(device->status) = sta;
1420 acpi_device_get_busid(device);
1421 acpi_device_set_id(device);
1422 acpi_bus_get_flags(device);
1423 device->flags.match_driver = false;
1424 device_initialize(&device->dev);
1425 dev_set_uevent_suppress(&device->dev, true);
1428 void acpi_device_add_finalize(struct acpi_device *device)
1430 device->flags.match_driver = true;
1431 dev_set_uevent_suppress(&device->dev, false);
1432 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1435 static int acpi_add_single_object(struct acpi_device **child,
1436 acpi_handle handle, int type,
1437 unsigned long long sta)
1440 struct acpi_device *device;
1441 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1443 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1445 printk(KERN_ERR PREFIX "Memory allocation error\n");
1449 acpi_init_device_object(device, handle, type, sta);
1450 acpi_bus_get_power_flags(device);
1451 acpi_bus_get_wakeup_device_flags(device);
1453 result = acpi_device_add(device, acpi_device_release);
1455 acpi_device_release(&device->dev);
1459 acpi_power_add_remove_device(device, true);
1460 acpi_device_add_finalize(device);
1461 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1462 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1463 dev_name(&device->dev), (char *) buffer.pointer,
1464 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1465 kfree(buffer.pointer);
1470 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1471 unsigned long long *sta)
1474 acpi_object_type acpi_type;
1476 status = acpi_get_type(handle, &acpi_type);
1477 if (ACPI_FAILURE(status))
1480 switch (acpi_type) {
1481 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1482 case ACPI_TYPE_DEVICE:
1483 *type = ACPI_BUS_TYPE_DEVICE;
1484 status = acpi_bus_get_status_handle(handle, sta);
1485 if (ACPI_FAILURE(status))
1488 case ACPI_TYPE_PROCESSOR:
1489 *type = ACPI_BUS_TYPE_PROCESSOR;
1490 status = acpi_bus_get_status_handle(handle, sta);
1491 if (ACPI_FAILURE(status))
1494 case ACPI_TYPE_THERMAL:
1495 *type = ACPI_BUS_TYPE_THERMAL;
1496 *sta = ACPI_STA_DEFAULT;
1498 case ACPI_TYPE_POWER:
1499 *type = ACPI_BUS_TYPE_POWER;
1500 *sta = ACPI_STA_DEFAULT;
1509 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1510 void *not_used, void **return_value)
1512 struct acpi_device *device = NULL;
1514 unsigned long long sta;
1518 acpi_bus_get_device(handle, &device);
1522 result = acpi_bus_type_and_status(handle, &type, &sta);
1526 if (type == ACPI_BUS_TYPE_POWER) {
1527 acpi_add_power_resource(handle);
1531 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1532 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
1533 struct acpi_device_wakeup wakeup;
1536 status = acpi_get_handle(handle, "_PRW", &temp);
1537 if (ACPI_SUCCESS(status)) {
1538 acpi_bus_extract_wakeup_device_power_package(handle,
1540 acpi_power_resources_list_free(&wakeup.resources);
1542 return AE_CTRL_DEPTH;
1545 acpi_add_single_object(&device, handle, type, sta);
1547 return AE_CTRL_DEPTH;
1551 *return_value = device;
1556 static int acpi_scan_do_attach_handler(struct acpi_device *device, char *id)
1558 struct acpi_scan_handler *handler;
1560 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node) {
1561 const struct acpi_device_id *devid;
1563 for (devid = handler->ids; devid->id[0]; devid++) {
1566 if (strcmp((char *)devid->id, id))
1569 ret = handler->attach(device, devid);
1571 device->handler = handler;
1573 } else if (ret < 0) {
1581 static int acpi_scan_attach_handler(struct acpi_device *device)
1583 struct acpi_hardware_id *hwid;
1586 list_for_each_entry(hwid, &device->pnp.ids, list) {
1587 ret = acpi_scan_do_attach_handler(device, hwid->id);
1595 static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1596 void *not_used, void **ret_not_used)
1598 struct acpi_device *device;
1599 unsigned long long sta_not_used;
1603 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1604 * namespace walks prematurely.
1606 if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
1609 if (acpi_bus_get_device(handle, &device))
1610 return AE_CTRL_DEPTH;
1612 ret = acpi_scan_attach_handler(device);
1614 return ret > 0 ? AE_OK : AE_CTRL_DEPTH;
1616 ret = device_attach(&device->dev);
1617 return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
1621 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1622 * @handle: Root of the namespace scope to scan.
1624 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1627 * If no devices were found, -ENODEV is returned, but it does not mean that
1628 * there has been a real error. There just have been no suitable ACPI objects
1629 * in the table trunk from which the kernel could create a device and add an
1630 * appropriate driver.
1632 int acpi_bus_scan(acpi_handle handle)
1634 void *device = NULL;
1637 mutex_lock(&acpi_scan_lock);
1639 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
1640 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1641 acpi_bus_check_add, NULL, NULL, &device);
1645 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
1646 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1647 acpi_bus_device_attach, NULL, NULL, NULL);
1649 mutex_unlock(&acpi_scan_lock);
1652 EXPORT_SYMBOL(acpi_bus_scan);
1654 static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
1655 void *not_used, void **ret_not_used)
1657 struct acpi_device *device = NULL;
1659 if (!acpi_bus_get_device(handle, &device)) {
1660 struct acpi_scan_handler *dev_handler = device->handler;
1662 device->removal_type = ACPI_BUS_REMOVAL_EJECT;
1664 if (dev_handler->detach)
1665 dev_handler->detach(device);
1667 device->handler = NULL;
1669 device_release_driver(&device->dev);
1675 static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
1676 void *not_used, void **ret_not_used)
1678 struct acpi_device *device = NULL;
1680 if (!acpi_bus_get_device(handle, &device))
1681 acpi_device_unregister(device);
1686 void acpi_bus_trim(struct acpi_device *start)
1688 mutex_lock(&acpi_scan_lock);
1691 * Execute acpi_bus_device_detach() as a post-order callback to detach
1692 * all ACPI drivers from the device nodes being removed.
1694 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1695 acpi_bus_device_detach, NULL, NULL);
1696 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
1698 * Execute acpi_bus_remove() as a post-order callback to remove device
1699 * nodes in the given namespace scope.
1701 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1702 acpi_bus_remove, NULL, NULL);
1703 acpi_bus_remove(start->handle, 0, NULL, NULL);
1705 mutex_unlock(&acpi_scan_lock);
1707 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1709 static int acpi_bus_scan_fixed(void)
1714 * Enumerate all fixed-feature devices.
1716 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
1717 struct acpi_device *device = NULL;
1719 result = acpi_add_single_object(&device, NULL,
1720 ACPI_BUS_TYPE_POWER_BUTTON,
1725 result = device_attach(&device->dev);
1729 device_init_wakeup(&device->dev, true);
1732 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
1733 struct acpi_device *device = NULL;
1735 result = acpi_add_single_object(&device, NULL,
1736 ACPI_BUS_TYPE_SLEEP_BUTTON,
1741 result = device_attach(&device->dev);
1744 return result < 0 ? result : 0;
1747 int __init acpi_scan_init(void)
1751 result = bus_register(&acpi_bus_type);
1753 /* We don't want to quit even if we failed to add suspend/resume */
1754 printk(KERN_ERR PREFIX "Could not register bus type\n");
1757 acpi_pci_root_init();
1758 acpi_pci_link_init();
1759 acpi_platform_init();
1763 * Enumerate devices in the ACPI namespace.
1765 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
1769 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
1773 result = acpi_bus_scan_fixed();
1775 acpi_device_unregister(acpi_root);
1779 acpi_update_all_gpes();