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 void acpi_scan_lock_acquire(void)
47 mutex_lock(&acpi_scan_lock);
49 EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
51 void acpi_scan_lock_release(void)
53 mutex_unlock(&acpi_scan_lock);
55 EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
57 int acpi_scan_add_handler(struct acpi_scan_handler *handler)
59 if (!handler || !handler->attach)
62 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
66 int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
67 const char *hotplug_profile_name)
71 error = acpi_scan_add_handler(handler);
75 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
80 * Creates hid/cid(s) string needed for modalias and uevent
81 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
82 * char *modalias: "acpi:IBM0001:ACPI0001"
84 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
89 struct acpi_hardware_id *id;
91 if (list_empty(&acpi_dev->pnp.ids))
94 len = snprintf(modalias, size, "acpi:");
97 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
98 count = snprintf(&modalias[len], size, "%s:", id->id);
99 if (count < 0 || count >= size)
105 modalias[len] = '\0';
110 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
111 struct acpi_device *acpi_dev = to_acpi_device(dev);
114 /* Device has no HID and no CID or string is >1024 */
115 len = create_modalias(acpi_dev, buf, 1024);
121 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
123 static int acpi_scan_hot_remove(struct acpi_device *device)
125 acpi_handle handle = device->handle;
126 acpi_handle not_used;
127 struct acpi_object_list arg_list;
128 union acpi_object arg;
131 /* If there is no handle, the device node has been unregistered. */
133 dev_dbg(&device->dev, "ACPI handle missing\n");
134 put_device(&device->dev);
138 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
139 "Hot-removing device %s...\n", dev_name(&device->dev)));
141 acpi_bus_trim(device);
142 /* Device node has been unregistered. */
143 put_device(&device->dev);
146 if (ACPI_SUCCESS(acpi_get_handle(handle, "_LCK", ¬_used))) {
148 arg_list.pointer = &arg;
149 arg.type = ACPI_TYPE_INTEGER;
150 arg.integer.value = 0;
151 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
155 arg_list.pointer = &arg;
156 arg.type = ACPI_TYPE_INTEGER;
157 arg.integer.value = 1;
162 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
163 if (ACPI_FAILURE(status)) {
164 if (status == AE_NOT_FOUND) {
167 acpi_handle_warn(handle, "Eject failed\n");
174 static void acpi_bus_device_eject(void *context)
176 acpi_handle handle = context;
177 struct acpi_device *device = NULL;
178 struct acpi_scan_handler *handler;
179 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
181 mutex_lock(&acpi_scan_lock);
183 acpi_bus_get_device(handle, &device);
187 handler = device->handler;
188 if (!handler || !handler->hotplug.enabled) {
189 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
192 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
193 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
194 if (handler->hotplug.mode == AHM_CONTAINER) {
195 device->flags.eject_pending = true;
196 kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
200 get_device(&device->dev);
201 error = acpi_scan_hot_remove(device);
207 mutex_unlock(&acpi_scan_lock);
211 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST, ost_code,
216 static void acpi_scan_bus_device_check(acpi_handle handle, u32 ost_source)
218 struct acpi_device *device = NULL;
219 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
222 mutex_lock(&acpi_scan_lock);
224 acpi_bus_get_device(handle, &device);
226 dev_warn(&device->dev, "Attempt to re-insert\n");
229 acpi_evaluate_hotplug_ost(handle, ost_source,
230 ACPI_OST_SC_INSERT_IN_PROGRESS, NULL);
231 error = acpi_bus_scan(handle);
233 acpi_handle_warn(handle, "Namespace scan failure\n");
236 error = acpi_bus_get_device(handle, &device);
238 acpi_handle_warn(handle, "Missing device node object\n");
241 ost_code = ACPI_OST_SC_SUCCESS;
242 if (device->handler && device->handler->hotplug.mode == AHM_CONTAINER)
243 kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
246 acpi_evaluate_hotplug_ost(handle, ost_source, ost_code, NULL);
247 mutex_unlock(&acpi_scan_lock);
250 static void acpi_scan_bus_check(void *context)
252 acpi_scan_bus_device_check((acpi_handle)context,
253 ACPI_NOTIFY_BUS_CHECK);
256 static void acpi_scan_device_check(void *context)
258 acpi_scan_bus_device_check((acpi_handle)context,
259 ACPI_NOTIFY_DEVICE_CHECK);
262 static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *not_used)
264 acpi_osd_exec_callback callback;
268 case ACPI_NOTIFY_BUS_CHECK:
269 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
270 callback = acpi_scan_bus_check;
272 case ACPI_NOTIFY_DEVICE_CHECK:
273 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
274 callback = acpi_scan_device_check;
276 case ACPI_NOTIFY_EJECT_REQUEST:
277 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
278 callback = acpi_bus_device_eject;
281 /* non-hotplug event; possibly handled by other handler */
284 status = acpi_os_hotplug_execute(callback, handle);
285 if (ACPI_FAILURE(status))
286 acpi_evaluate_hotplug_ost(handle, type,
287 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
292 * acpi_bus_hot_remove_device: hot-remove a device and its children
293 * @context: struct acpi_eject_event pointer (freed in this func)
295 * Hot-remove a device and its children. This function frees up the
296 * memory space passed by arg context, so that the caller may call
297 * this function asynchronously through acpi_os_hotplug_execute().
299 void acpi_bus_hot_remove_device(void *context)
301 struct acpi_eject_event *ej_event = context;
302 struct acpi_device *device = ej_event->device;
303 acpi_handle handle = device->handle;
306 mutex_lock(&acpi_scan_lock);
308 error = acpi_scan_hot_remove(device);
310 acpi_evaluate_hotplug_ost(handle, ej_event->event,
311 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
314 mutex_unlock(&acpi_scan_lock);
317 EXPORT_SYMBOL(acpi_bus_hot_remove_device);
319 static ssize_t real_power_state_show(struct device *dev,
320 struct device_attribute *attr, char *buf)
322 struct acpi_device *adev = to_acpi_device(dev);
326 ret = acpi_device_get_power(adev, &state);
330 return sprintf(buf, "%s\n", acpi_power_state_string(state));
333 static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
335 static ssize_t power_state_show(struct device *dev,
336 struct device_attribute *attr, char *buf)
338 struct acpi_device *adev = to_acpi_device(dev);
340 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
343 static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
346 acpi_eject_store(struct device *d, struct device_attribute *attr,
347 const char *buf, size_t count)
349 struct acpi_device *acpi_device = to_acpi_device(d);
350 struct acpi_eject_event *ej_event;
351 acpi_object_type not_used;
356 if (!count || buf[0] != '1')
359 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
360 && !acpi_device->driver)
363 status = acpi_get_type(acpi_device->handle, ¬_used);
364 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
367 mutex_lock(&acpi_scan_lock);
369 if (acpi_device->flags.eject_pending) {
370 /* ACPI eject notification event. */
371 ost_source = ACPI_NOTIFY_EJECT_REQUEST;
372 acpi_device->flags.eject_pending = 0;
374 /* Eject initiated by user space. */
375 ost_source = ACPI_OST_EC_OSPM_EJECT;
377 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
382 acpi_evaluate_hotplug_ost(acpi_device->handle, ost_source,
383 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
384 ej_event->device = acpi_device;
385 ej_event->event = ost_source;
386 get_device(&acpi_device->dev);
387 status = acpi_os_hotplug_execute(acpi_bus_hot_remove_device, ej_event);
388 if (ACPI_FAILURE(status)) {
389 put_device(&acpi_device->dev);
391 ret = status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
397 mutex_unlock(&acpi_scan_lock);
401 acpi_evaluate_hotplug_ost(acpi_device->handle, ost_source,
402 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
406 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
409 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
410 struct acpi_device *acpi_dev = to_acpi_device(dev);
412 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
414 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
416 static ssize_t acpi_device_uid_show(struct device *dev,
417 struct device_attribute *attr, char *buf)
419 struct acpi_device *acpi_dev = to_acpi_device(dev);
421 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
423 static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
425 static ssize_t acpi_device_adr_show(struct device *dev,
426 struct device_attribute *attr, char *buf)
428 struct acpi_device *acpi_dev = to_acpi_device(dev);
430 return sprintf(buf, "0x%08x\n",
431 (unsigned int)(acpi_dev->pnp.bus_address));
433 static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
436 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
437 struct acpi_device *acpi_dev = to_acpi_device(dev);
438 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
441 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
445 result = sprintf(buf, "%s\n", (char*)path.pointer);
450 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
452 /* sysfs file that shows description text from the ACPI _STR method */
453 static ssize_t description_show(struct device *dev,
454 struct device_attribute *attr,
456 struct acpi_device *acpi_dev = to_acpi_device(dev);
459 if (acpi_dev->pnp.str_obj == NULL)
463 * The _STR object contains a Unicode identifier for a device.
464 * We need to convert to utf-8 so it can be displayed.
466 result = utf16s_to_utf8s(
467 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
468 acpi_dev->pnp.str_obj->buffer.length,
469 UTF16_LITTLE_ENDIAN, buf,
472 buf[result++] = '\n';
476 static DEVICE_ATTR(description, 0444, description_show, NULL);
479 acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
481 struct acpi_device *acpi_dev = to_acpi_device(dev);
483 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
485 static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
487 static int acpi_device_setup_files(struct acpi_device *dev)
489 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
492 unsigned long long sun;
496 * Devices gotten from FADT don't have a "path" attribute
499 result = device_create_file(&dev->dev, &dev_attr_path);
504 if (!list_empty(&dev->pnp.ids)) {
505 result = device_create_file(&dev->dev, &dev_attr_hid);
509 result = device_create_file(&dev->dev, &dev_attr_modalias);
515 * If device has _STR, 'description' file is created
517 status = acpi_get_handle(dev->handle, "_STR", &temp);
518 if (ACPI_SUCCESS(status)) {
519 status = acpi_evaluate_object(dev->handle, "_STR",
521 if (ACPI_FAILURE(status))
522 buffer.pointer = NULL;
523 dev->pnp.str_obj = buffer.pointer;
524 result = device_create_file(&dev->dev, &dev_attr_description);
529 if (dev->flags.bus_address)
530 result = device_create_file(&dev->dev, &dev_attr_adr);
531 if (dev->pnp.unique_id)
532 result = device_create_file(&dev->dev, &dev_attr_uid);
534 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
535 if (ACPI_SUCCESS(status)) {
536 dev->pnp.sun = (unsigned long)sun;
537 result = device_create_file(&dev->dev, &dev_attr_sun);
541 dev->pnp.sun = (unsigned long)-1;
545 * If device has _EJ0, 'eject' file is created that is used to trigger
546 * hot-removal function from userland.
548 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
549 if (ACPI_SUCCESS(status)) {
550 result = device_create_file(&dev->dev, &dev_attr_eject);
555 if (dev->flags.power_manageable) {
556 result = device_create_file(&dev->dev, &dev_attr_power_state);
560 if (dev->power.flags.power_resources)
561 result = device_create_file(&dev->dev,
562 &dev_attr_real_power_state);
569 static void acpi_device_remove_files(struct acpi_device *dev)
574 if (dev->flags.power_manageable) {
575 device_remove_file(&dev->dev, &dev_attr_power_state);
576 if (dev->power.flags.power_resources)
577 device_remove_file(&dev->dev,
578 &dev_attr_real_power_state);
582 * If device has _STR, remove 'description' file
584 status = acpi_get_handle(dev->handle, "_STR", &temp);
585 if (ACPI_SUCCESS(status)) {
586 kfree(dev->pnp.str_obj);
587 device_remove_file(&dev->dev, &dev_attr_description);
590 * If device has _EJ0, remove 'eject' file.
592 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
593 if (ACPI_SUCCESS(status))
594 device_remove_file(&dev->dev, &dev_attr_eject);
596 status = acpi_get_handle(dev->handle, "_SUN", &temp);
597 if (ACPI_SUCCESS(status))
598 device_remove_file(&dev->dev, &dev_attr_sun);
600 if (dev->pnp.unique_id)
601 device_remove_file(&dev->dev, &dev_attr_uid);
602 if (dev->flags.bus_address)
603 device_remove_file(&dev->dev, &dev_attr_adr);
604 device_remove_file(&dev->dev, &dev_attr_modalias);
605 device_remove_file(&dev->dev, &dev_attr_hid);
607 device_remove_file(&dev->dev, &dev_attr_path);
609 /* --------------------------------------------------------------------------
611 -------------------------------------------------------------------------- */
613 static const struct acpi_device_id *__acpi_match_device(
614 struct acpi_device *device, const struct acpi_device_id *ids)
616 const struct acpi_device_id *id;
617 struct acpi_hardware_id *hwid;
620 * If the device is not present, it is unnecessary to load device
623 if (!device->status.present)
626 for (id = ids; id->id[0]; id++)
627 list_for_each_entry(hwid, &device->pnp.ids, list)
628 if (!strcmp((char *) id->id, hwid->id))
635 * acpi_match_device - Match a struct device against a given list of ACPI IDs
636 * @ids: Array of struct acpi_device_id object to match against.
637 * @dev: The device structure to match.
639 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
640 * object for that handle and use that object to match against a given list of
643 * Return a pointer to the first matching ID on success or %NULL on failure.
645 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
646 const struct device *dev)
648 struct acpi_device *adev;
649 acpi_handle handle = ACPI_HANDLE(dev);
651 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
654 return __acpi_match_device(adev, ids);
656 EXPORT_SYMBOL_GPL(acpi_match_device);
658 int acpi_match_device_ids(struct acpi_device *device,
659 const struct acpi_device_id *ids)
661 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
663 EXPORT_SYMBOL(acpi_match_device_ids);
665 void acpi_free_ids(struct acpi_device *device)
667 struct acpi_hardware_id *id, *tmp;
669 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
673 kfree(device->pnp.unique_id);
676 static void acpi_free_power_resources_lists(struct acpi_device *device)
680 if (device->wakeup.flags.valid)
681 acpi_power_resources_list_free(&device->wakeup.resources);
683 if (!device->flags.power_manageable)
686 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
687 struct acpi_device_power_state *ps = &device->power.states[i];
688 acpi_power_resources_list_free(&ps->resources);
692 static void acpi_device_release(struct device *dev)
694 struct acpi_device *acpi_dev = to_acpi_device(dev);
696 acpi_free_ids(acpi_dev);
697 acpi_free_power_resources_lists(acpi_dev);
701 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
703 struct acpi_device *acpi_dev = to_acpi_device(dev);
704 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
706 return acpi_dev->flags.match_driver
707 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
710 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
712 struct acpi_device *acpi_dev = to_acpi_device(dev);
715 if (list_empty(&acpi_dev->pnp.ids))
718 if (add_uevent_var(env, "MODALIAS="))
720 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
721 sizeof(env->buf) - env->buflen);
722 if (len >= (sizeof(env->buf) - env->buflen))
728 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
730 struct acpi_device *device = data;
732 device->driver->ops.notify(device, event);
735 static acpi_status acpi_device_notify_fixed(void *data)
737 struct acpi_device *device = data;
739 /* Fixed hardware devices have no handles */
740 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
744 static int acpi_device_install_notify_handler(struct acpi_device *device)
748 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
750 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
751 acpi_device_notify_fixed,
753 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
755 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
756 acpi_device_notify_fixed,
759 status = acpi_install_notify_handler(device->handle,
764 if (ACPI_FAILURE(status))
769 static void acpi_device_remove_notify_handler(struct acpi_device *device)
771 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
772 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
773 acpi_device_notify_fixed);
774 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
775 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
776 acpi_device_notify_fixed);
778 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
782 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
783 static int acpi_device_probe(struct device * dev)
785 struct acpi_device *acpi_dev = to_acpi_device(dev);
786 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
789 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
791 if (acpi_drv->ops.notify) {
792 ret = acpi_device_install_notify_handler(acpi_dev);
794 if (acpi_drv->ops.remove)
795 acpi_drv->ops.remove(acpi_dev);
796 acpi_dev->driver = NULL;
797 acpi_dev->driver_data = NULL;
802 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
803 "Found driver [%s] for device [%s]\n",
804 acpi_drv->name, acpi_dev->pnp.bus_id));
810 static int acpi_device_remove(struct device * dev)
812 struct acpi_device *acpi_dev = to_acpi_device(dev);
813 struct acpi_driver *acpi_drv = acpi_dev->driver;
816 if (acpi_drv->ops.notify)
817 acpi_device_remove_notify_handler(acpi_dev);
818 if (acpi_drv->ops.remove)
819 acpi_drv->ops.remove(acpi_dev);
821 acpi_dev->driver = NULL;
822 acpi_dev->driver_data = NULL;
828 struct bus_type acpi_bus_type = {
830 .match = acpi_bus_match,
831 .probe = acpi_device_probe,
832 .remove = acpi_device_remove,
833 .uevent = acpi_device_uevent,
836 int acpi_device_add(struct acpi_device *device,
837 void (*release)(struct device *))
840 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
843 if (device->handle) {
846 status = acpi_attach_data(device->handle, acpi_bus_data_handler,
848 if (ACPI_FAILURE(status)) {
849 acpi_handle_err(device->handle,
850 "Unable to attach device data\n");
858 * Link this device to its parent and siblings.
860 INIT_LIST_HEAD(&device->children);
861 INIT_LIST_HEAD(&device->node);
862 INIT_LIST_HEAD(&device->wakeup_list);
863 INIT_LIST_HEAD(&device->physical_node_list);
864 mutex_init(&device->physical_node_lock);
865 INIT_LIST_HEAD(&device->power_dependent);
867 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
869 pr_err(PREFIX "Memory allocation error\n");
874 mutex_lock(&acpi_device_lock);
876 * Find suitable bus_id and instance number in acpi_bus_id_list
877 * If failed, create one and link it into acpi_bus_id_list
879 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
880 if (!strcmp(acpi_device_bus_id->bus_id,
881 acpi_device_hid(device))) {
882 acpi_device_bus_id->instance_no++;
889 acpi_device_bus_id = new_bus_id;
890 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
891 acpi_device_bus_id->instance_no = 0;
892 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
894 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
897 list_add_tail(&device->node, &device->parent->children);
899 if (device->wakeup.flags.valid)
900 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
901 mutex_unlock(&acpi_device_lock);
904 device->dev.parent = &device->parent->dev;
905 device->dev.bus = &acpi_bus_type;
906 device->dev.release = release;
907 result = device_add(&device->dev);
909 dev_err(&device->dev, "Error registering device\n");
913 result = acpi_device_setup_files(device);
915 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
916 dev_name(&device->dev));
918 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
922 mutex_lock(&acpi_device_lock);
924 list_del(&device->node);
925 list_del(&device->wakeup_list);
926 mutex_unlock(&acpi_device_lock);
929 acpi_detach_data(device->handle, acpi_bus_data_handler);
933 static void acpi_device_unregister(struct acpi_device *device)
935 mutex_lock(&acpi_device_lock);
937 list_del(&device->node);
939 list_del(&device->wakeup_list);
940 mutex_unlock(&acpi_device_lock);
942 acpi_detach_data(device->handle, acpi_bus_data_handler);
944 acpi_power_add_remove_device(device, false);
945 acpi_device_remove_files(device);
947 device->remove(device);
949 device_del(&device->dev);
951 * Transition the device to D3cold to drop the reference counts of all
952 * power resources the device depends on and turn off the ones that have
953 * no more references.
955 acpi_device_set_power(device, ACPI_STATE_D3_COLD);
956 device->handle = NULL;
957 put_device(&device->dev);
960 /* --------------------------------------------------------------------------
962 -------------------------------------------------------------------------- */
964 * acpi_bus_driver_init - add a device to a driver
965 * @device: the device to add and initialize
966 * @driver: driver for the device
968 * Used to initialize a device via its device driver. Called whenever a
969 * driver is bound to a device. Invokes the driver's add() ops.
972 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
976 if (!device || !driver)
979 if (!driver->ops.add)
982 result = driver->ops.add(device);
984 device->driver = NULL;
985 device->driver_data = NULL;
989 device->driver = driver;
992 * TBD - Configuration Management: Assign resources to device based
993 * upon possible configuration and currently allocated resources.
996 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
997 "Driver successfully bound to device\n"));
1002 * acpi_bus_register_driver - register a driver with the ACPI bus
1003 * @driver: driver being registered
1005 * Registers a driver with the ACPI bus. Searches the namespace for all
1006 * devices that match the driver's criteria and binds. Returns zero for
1007 * success or a negative error status for failure.
1009 int acpi_bus_register_driver(struct acpi_driver *driver)
1015 driver->drv.name = driver->name;
1016 driver->drv.bus = &acpi_bus_type;
1017 driver->drv.owner = driver->owner;
1019 ret = driver_register(&driver->drv);
1023 EXPORT_SYMBOL(acpi_bus_register_driver);
1026 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
1027 * @driver: driver to unregister
1029 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1030 * devices that match the driver's criteria and unbinds.
1032 void acpi_bus_unregister_driver(struct acpi_driver *driver)
1034 driver_unregister(&driver->drv);
1037 EXPORT_SYMBOL(acpi_bus_unregister_driver);
1039 /* --------------------------------------------------------------------------
1041 -------------------------------------------------------------------------- */
1042 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1044 struct acpi_device *device = NULL;
1048 * Fixed hardware devices do not appear in the namespace and do not
1049 * have handles, but we fabricate acpi_devices for them, so we have
1050 * to deal with them specially.
1056 status = acpi_get_parent(handle, &handle);
1057 if (ACPI_FAILURE(status))
1058 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1059 } while (acpi_bus_get_device(handle, &device));
1064 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1068 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1069 union acpi_object *obj;
1071 status = acpi_get_handle(handle, "_EJD", &tmp);
1072 if (ACPI_FAILURE(status))
1075 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1076 if (ACPI_SUCCESS(status)) {
1077 obj = buffer.pointer;
1078 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1080 kfree(buffer.pointer);
1084 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1086 void acpi_bus_data_handler(acpi_handle handle, void *context)
1094 static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1095 struct acpi_device_wakeup *wakeup)
1097 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1098 union acpi_object *package = NULL;
1099 union acpi_object *element = NULL;
1106 INIT_LIST_HEAD(&wakeup->resources);
1109 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1110 if (ACPI_FAILURE(status)) {
1111 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
1115 package = (union acpi_object *)buffer.pointer;
1117 if (!package || package->package.count < 2)
1120 element = &(package->package.elements[0]);
1124 if (element->type == ACPI_TYPE_PACKAGE) {
1125 if ((element->package.count < 2) ||
1126 (element->package.elements[0].type !=
1127 ACPI_TYPE_LOCAL_REFERENCE)
1128 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
1131 wakeup->gpe_device =
1132 element->package.elements[0].reference.handle;
1133 wakeup->gpe_number =
1134 (u32) element->package.elements[1].integer.value;
1135 } else if (element->type == ACPI_TYPE_INTEGER) {
1136 wakeup->gpe_device = NULL;
1137 wakeup->gpe_number = element->integer.value;
1142 element = &(package->package.elements[1]);
1143 if (element->type != ACPI_TYPE_INTEGER)
1146 wakeup->sleep_state = element->integer.value;
1148 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1152 if (!list_empty(&wakeup->resources)) {
1155 err = acpi_power_wakeup_list_init(&wakeup->resources,
1158 acpi_handle_warn(handle, "Retrieving current states "
1159 "of wakeup power resources failed\n");
1160 acpi_power_resources_list_free(&wakeup->resources);
1163 if (sleep_state < wakeup->sleep_state) {
1164 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1165 "(S%d) by S%d from power resources\n",
1166 (int)wakeup->sleep_state, sleep_state);
1167 wakeup->sleep_state = sleep_state;
1170 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
1173 kfree(buffer.pointer);
1177 static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
1179 struct acpi_device_id button_device_ids[] = {
1186 acpi_event_status event_status;
1188 device->wakeup.flags.notifier_present = 0;
1190 /* Power button, Lid switch always enable wakeup */
1191 if (!acpi_match_device_ids(device, button_device_ids)) {
1192 device->wakeup.flags.run_wake = 1;
1193 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1194 /* Do not use Lid/sleep button for S5 wakeup */
1195 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1196 device->wakeup.sleep_state = ACPI_STATE_S4;
1198 device_set_wakeup_capable(&device->dev, true);
1202 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1203 device->wakeup.gpe_number,
1205 if (status == AE_OK)
1206 device->wakeup.flags.run_wake =
1207 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1210 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1213 acpi_status status = 0;
1216 /* Presence of _PRW indicates wake capable */
1217 status = acpi_get_handle(device->handle, "_PRW", &temp);
1218 if (ACPI_FAILURE(status))
1221 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1224 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
1228 device->wakeup.flags.valid = 1;
1229 device->wakeup.prepare_count = 0;
1230 acpi_bus_set_run_wake_flags(device);
1231 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1232 * system for the ACPI device with the _PRW object.
1233 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1234 * So it is necessary to call _DSW object first. Only when it is not
1235 * present will the _PSW object used.
1237 err = acpi_device_sleep_wake(device, 0, 0, 0);
1239 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1240 "error in _DSW or _PSW evaluation\n"));
1243 static void acpi_bus_init_power_state(struct acpi_device *device, int state)
1245 struct acpi_device_power_state *ps = &device->power.states[state];
1246 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1247 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1251 INIT_LIST_HEAD(&ps->resources);
1253 /* Evaluate "_PRx" to get referenced power resources */
1254 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1255 if (ACPI_SUCCESS(status)) {
1256 union acpi_object *package = buffer.pointer;
1258 if (buffer.length && package
1259 && package->type == ACPI_TYPE_PACKAGE
1260 && package->package.count) {
1261 int err = acpi_extract_power_resources(package, 0,
1264 device->power.flags.power_resources = 1;
1266 ACPI_FREE(buffer.pointer);
1269 /* Evaluate "_PSx" to see if we can do explicit sets */
1271 status = acpi_get_handle(device->handle, pathname, &handle);
1272 if (ACPI_SUCCESS(status))
1273 ps->flags.explicit_set = 1;
1276 * State is valid if there are means to put the device into it.
1277 * D3hot is only valid if _PR3 present.
1279 if (!list_empty(&ps->resources)
1280 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1281 ps->flags.valid = 1;
1282 ps->flags.os_accessible = 1;
1285 ps->power = -1; /* Unknown - driver assigned */
1286 ps->latency = -1; /* Unknown - driver assigned */
1289 static void acpi_bus_get_power_flags(struct acpi_device *device)
1295 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1296 status = acpi_get_handle(device->handle, "_PS0", &handle);
1297 if (ACPI_FAILURE(status)) {
1298 status = acpi_get_handle(device->handle, "_PR0", &handle);
1299 if (ACPI_FAILURE(status))
1303 device->flags.power_manageable = 1;
1306 * Power Management Flags
1308 status = acpi_get_handle(device->handle, "_PSC", &handle);
1309 if (ACPI_SUCCESS(status))
1310 device->power.flags.explicit_get = 1;
1311 status = acpi_get_handle(device->handle, "_IRC", &handle);
1312 if (ACPI_SUCCESS(status))
1313 device->power.flags.inrush_current = 1;
1316 * Enumerate supported power management states
1318 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1319 acpi_bus_init_power_state(device, i);
1321 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1323 /* Set defaults for D0 and D3 states (always valid) */
1324 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1325 device->power.states[ACPI_STATE_D0].power = 100;
1326 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1327 device->power.states[ACPI_STATE_D3].power = 0;
1329 /* Set D3cold's explicit_set flag if _PS3 exists. */
1330 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1331 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1333 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1334 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1335 device->power.flags.power_resources)
1336 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1338 if (acpi_bus_init_power(device)) {
1339 acpi_free_power_resources_lists(device);
1340 device->flags.power_manageable = 0;
1344 static void acpi_bus_get_flags(struct acpi_device *device)
1346 acpi_status status = AE_OK;
1347 acpi_handle temp = NULL;
1349 /* Presence of _STA indicates 'dynamic_status' */
1350 status = acpi_get_handle(device->handle, "_STA", &temp);
1351 if (ACPI_SUCCESS(status))
1352 device->flags.dynamic_status = 1;
1354 /* Presence of _RMV indicates 'removable' */
1355 status = acpi_get_handle(device->handle, "_RMV", &temp);
1356 if (ACPI_SUCCESS(status))
1357 device->flags.removable = 1;
1359 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1360 status = acpi_get_handle(device->handle, "_EJD", &temp);
1361 if (ACPI_SUCCESS(status))
1362 device->flags.ejectable = 1;
1364 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1365 if (ACPI_SUCCESS(status))
1366 device->flags.ejectable = 1;
1370 static void acpi_device_get_busid(struct acpi_device *device)
1372 char bus_id[5] = { '?', 0 };
1373 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1379 * The device's Bus ID is simply the object name.
1380 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1382 if (ACPI_IS_ROOT_DEVICE(device)) {
1383 strcpy(device->pnp.bus_id, "ACPI");
1387 switch (device->device_type) {
1388 case ACPI_BUS_TYPE_POWER_BUTTON:
1389 strcpy(device->pnp.bus_id, "PWRF");
1391 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1392 strcpy(device->pnp.bus_id, "SLPF");
1395 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1396 /* Clean up trailing underscores (if any) */
1397 for (i = 3; i > 1; i--) {
1398 if (bus_id[i] == '_')
1403 strcpy(device->pnp.bus_id, bus_id);
1409 * acpi_bay_match - see if a device is an ejectable driver bay
1411 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1412 * then we can safely call it an ejectable drive bay
1414 static int acpi_bay_match(struct acpi_device *device){
1418 acpi_handle phandle;
1420 handle = device->handle;
1422 status = acpi_get_handle(handle, "_EJ0", &tmp);
1423 if (ACPI_FAILURE(status))
1426 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1427 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1428 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1429 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1432 if (acpi_get_parent(handle, &phandle))
1435 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1436 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1437 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1438 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1445 * acpi_dock_match - see if a device has a _DCK method
1447 static int acpi_dock_match(struct acpi_device *device)
1450 return acpi_get_handle(device->handle, "_DCK", &tmp);
1453 const char *acpi_device_hid(struct acpi_device *device)
1455 struct acpi_hardware_id *hid;
1457 if (list_empty(&device->pnp.ids))
1460 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1463 EXPORT_SYMBOL(acpi_device_hid);
1465 static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1467 struct acpi_hardware_id *id;
1469 id = kmalloc(sizeof(*id), GFP_KERNEL);
1473 id->id = kstrdup(dev_id, GFP_KERNEL);
1479 list_add_tail(&id->list, &device->pnp.ids);
1483 * Old IBM workstations have a DSDT bug wherein the SMBus object
1484 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1485 * prefix. Work around this.
1487 static int acpi_ibm_smbus_match(struct acpi_device *device)
1489 acpi_handle h_dummy;
1490 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1493 if (!dmi_name_in_vendors("IBM"))
1496 /* Look for SMBS object */
1497 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1501 if (strcmp("SMBS", path.pointer)) {
1506 /* Does it have the necessary (but misnamed) methods? */
1508 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1509 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1510 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1513 kfree(path.pointer);
1517 static void acpi_device_set_id(struct acpi_device *device)
1520 struct acpi_device_info *info;
1521 struct acpi_pnp_device_id_list *cid_list;
1524 switch (device->device_type) {
1525 case ACPI_BUS_TYPE_DEVICE:
1526 if (ACPI_IS_ROOT_DEVICE(device)) {
1527 acpi_add_id(device, ACPI_SYSTEM_HID);
1531 status = acpi_get_object_info(device->handle, &info);
1532 if (ACPI_FAILURE(status)) {
1533 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1537 if (info->valid & ACPI_VALID_HID)
1538 acpi_add_id(device, info->hardware_id.string);
1539 if (info->valid & ACPI_VALID_CID) {
1540 cid_list = &info->compatible_id_list;
1541 for (i = 0; i < cid_list->count; i++)
1542 acpi_add_id(device, cid_list->ids[i].string);
1544 if (info->valid & ACPI_VALID_ADR) {
1545 device->pnp.bus_address = info->address;
1546 device->flags.bus_address = 1;
1548 if (info->valid & ACPI_VALID_UID)
1549 device->pnp.unique_id = kstrdup(info->unique_id.string,
1555 * Some devices don't reliably have _HIDs & _CIDs, so add
1556 * synthetic HIDs to make sure drivers can find them.
1558 if (acpi_is_video_device(device))
1559 acpi_add_id(device, ACPI_VIDEO_HID);
1560 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1561 acpi_add_id(device, ACPI_BAY_HID);
1562 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1563 acpi_add_id(device, ACPI_DOCK_HID);
1564 else if (!acpi_ibm_smbus_match(device))
1565 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
1566 else if (list_empty(&device->pnp.ids) &&
1567 ACPI_IS_ROOT_DEVICE(device->parent)) {
1568 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1569 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1570 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1574 case ACPI_BUS_TYPE_POWER:
1575 acpi_add_id(device, ACPI_POWER_HID);
1577 case ACPI_BUS_TYPE_PROCESSOR:
1578 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
1580 case ACPI_BUS_TYPE_THERMAL:
1581 acpi_add_id(device, ACPI_THERMAL_HID);
1583 case ACPI_BUS_TYPE_POWER_BUTTON:
1584 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
1586 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1587 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
1592 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1593 int type, unsigned long long sta)
1595 INIT_LIST_HEAD(&device->pnp.ids);
1596 device->device_type = type;
1597 device->handle = handle;
1598 device->parent = acpi_bus_get_parent(handle);
1599 STRUCT_TO_INT(device->status) = sta;
1600 acpi_device_get_busid(device);
1601 acpi_device_set_id(device);
1602 acpi_bus_get_flags(device);
1603 device->flags.match_driver = false;
1604 device_initialize(&device->dev);
1605 dev_set_uevent_suppress(&device->dev, true);
1608 void acpi_device_add_finalize(struct acpi_device *device)
1610 device->flags.match_driver = true;
1611 dev_set_uevent_suppress(&device->dev, false);
1612 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1615 static int acpi_add_single_object(struct acpi_device **child,
1616 acpi_handle handle, int type,
1617 unsigned long long sta)
1620 struct acpi_device *device;
1621 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1623 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1625 printk(KERN_ERR PREFIX "Memory allocation error\n");
1629 acpi_init_device_object(device, handle, type, sta);
1630 acpi_bus_get_power_flags(device);
1631 acpi_bus_get_wakeup_device_flags(device);
1633 result = acpi_device_add(device, acpi_device_release);
1635 acpi_device_release(&device->dev);
1639 acpi_power_add_remove_device(device, true);
1640 acpi_device_add_finalize(device);
1641 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1642 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1643 dev_name(&device->dev), (char *) buffer.pointer,
1644 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1645 kfree(buffer.pointer);
1650 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1651 unsigned long long *sta)
1654 acpi_object_type acpi_type;
1656 status = acpi_get_type(handle, &acpi_type);
1657 if (ACPI_FAILURE(status))
1660 switch (acpi_type) {
1661 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1662 case ACPI_TYPE_DEVICE:
1663 *type = ACPI_BUS_TYPE_DEVICE;
1664 status = acpi_bus_get_status_handle(handle, sta);
1665 if (ACPI_FAILURE(status))
1668 case ACPI_TYPE_PROCESSOR:
1669 *type = ACPI_BUS_TYPE_PROCESSOR;
1670 status = acpi_bus_get_status_handle(handle, sta);
1671 if (ACPI_FAILURE(status))
1674 case ACPI_TYPE_THERMAL:
1675 *type = ACPI_BUS_TYPE_THERMAL;
1676 *sta = ACPI_STA_DEFAULT;
1678 case ACPI_TYPE_POWER:
1679 *type = ACPI_BUS_TYPE_POWER;
1680 *sta = ACPI_STA_DEFAULT;
1689 static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1691 const struct acpi_device_id **matchid)
1693 const struct acpi_device_id *devid;
1695 for (devid = handler->ids; devid->id[0]; devid++)
1696 if (!strcmp((char *)devid->id, idstr)) {
1706 static acpi_status acpi_scan_hotplug_modify(acpi_handle handle,
1707 u32 lvl_not_used, void *data,
1708 void **ret_not_used)
1710 struct acpi_scan_handler *handler = data;
1711 struct acpi_device_info *info;
1714 if (ACPI_FAILURE(acpi_get_object_info(handle, &info)))
1717 if (info->valid & ACPI_VALID_HID) {
1718 char *idstr = info->hardware_id.string;
1719 match = acpi_scan_handler_matching(handler, idstr, NULL);
1725 if (handler->hotplug.enabled)
1726 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1727 acpi_hotplug_notify_cb, NULL);
1729 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1730 acpi_hotplug_notify_cb);
1735 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1737 struct acpi_scan_handler *handler;
1739 if (!!hotplug->enabled == !!val)
1742 mutex_lock(&acpi_scan_lock);
1744 hotplug->enabled = val;
1745 handler = container_of(hotplug, struct acpi_scan_handler, hotplug);
1746 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
1747 acpi_scan_hotplug_modify, NULL, handler, NULL);
1749 mutex_unlock(&acpi_scan_lock);
1752 static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1753 const struct acpi_device_id **matchid)
1755 struct acpi_scan_handler *handler;
1757 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1758 if (acpi_scan_handler_matching(handler, idstr, matchid))
1764 static void acpi_scan_init_hotplug(acpi_handle handle)
1766 struct acpi_device_info *info;
1767 struct acpi_scan_handler *handler;
1769 if (ACPI_FAILURE(acpi_get_object_info(handle, &info)))
1772 if (!(info->valid & ACPI_VALID_HID)) {
1778 * This relies on the fact that acpi_install_notify_handler() will not
1779 * install the same notify handler routine twice for the same handle.
1781 handler = acpi_scan_match_handler(info->hardware_id.string, NULL);
1783 if (handler && handler->hotplug.enabled)
1784 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1785 acpi_hotplug_notify_cb, NULL);
1788 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1789 void *not_used, void **return_value)
1791 struct acpi_device *device = NULL;
1793 unsigned long long sta;
1797 acpi_bus_get_device(handle, &device);
1801 result = acpi_bus_type_and_status(handle, &type, &sta);
1805 if (type == ACPI_BUS_TYPE_POWER) {
1806 acpi_add_power_resource(handle);
1810 acpi_scan_init_hotplug(handle);
1812 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1813 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
1814 struct acpi_device_wakeup wakeup;
1817 status = acpi_get_handle(handle, "_PRW", &temp);
1818 if (ACPI_SUCCESS(status)) {
1819 acpi_bus_extract_wakeup_device_power_package(handle,
1821 acpi_power_resources_list_free(&wakeup.resources);
1823 return AE_CTRL_DEPTH;
1826 acpi_add_single_object(&device, handle, type, sta);
1828 return AE_CTRL_DEPTH;
1832 *return_value = device;
1837 static int acpi_scan_attach_handler(struct acpi_device *device)
1839 struct acpi_hardware_id *hwid;
1842 list_for_each_entry(hwid, &device->pnp.ids, list) {
1843 const struct acpi_device_id *devid;
1844 struct acpi_scan_handler *handler;
1846 handler = acpi_scan_match_handler(hwid->id, &devid);
1848 ret = handler->attach(device, devid);
1850 device->handler = handler;
1852 } else if (ret < 0) {
1860 static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1861 void *not_used, void **ret_not_used)
1863 struct acpi_device *device;
1864 unsigned long long sta_not_used;
1868 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1869 * namespace walks prematurely.
1871 if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
1874 if (acpi_bus_get_device(handle, &device))
1875 return AE_CTRL_DEPTH;
1877 ret = acpi_scan_attach_handler(device);
1879 return ret > 0 ? AE_OK : AE_CTRL_DEPTH;
1881 ret = device_attach(&device->dev);
1882 return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
1886 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1887 * @handle: Root of the namespace scope to scan.
1889 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1892 * If no devices were found, -ENODEV is returned, but it does not mean that
1893 * there has been a real error. There just have been no suitable ACPI objects
1894 * in the table trunk from which the kernel could create a device and add an
1895 * appropriate driver.
1897 * Must be called under acpi_scan_lock.
1899 int acpi_bus_scan(acpi_handle handle)
1901 void *device = NULL;
1904 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
1905 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1906 acpi_bus_check_add, NULL, NULL, &device);
1910 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
1911 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1912 acpi_bus_device_attach, NULL, NULL, NULL);
1916 EXPORT_SYMBOL(acpi_bus_scan);
1918 static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
1919 void *not_used, void **ret_not_used)
1921 struct acpi_device *device = NULL;
1923 if (!acpi_bus_get_device(handle, &device)) {
1924 struct acpi_scan_handler *dev_handler = device->handler;
1926 device->removal_type = ACPI_BUS_REMOVAL_EJECT;
1928 if (dev_handler->detach)
1929 dev_handler->detach(device);
1931 device->handler = NULL;
1933 device_release_driver(&device->dev);
1939 static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
1940 void *not_used, void **ret_not_used)
1942 struct acpi_device *device = NULL;
1944 if (!acpi_bus_get_device(handle, &device))
1945 acpi_device_unregister(device);
1951 * acpi_bus_trim - Remove ACPI device node and all of its descendants
1952 * @start: Root of the ACPI device nodes subtree to remove.
1954 * Must be called under acpi_scan_lock.
1956 void acpi_bus_trim(struct acpi_device *start)
1959 * Execute acpi_bus_device_detach() as a post-order callback to detach
1960 * all ACPI drivers from the device nodes being removed.
1962 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1963 acpi_bus_device_detach, NULL, NULL);
1964 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
1966 * Execute acpi_bus_remove() as a post-order callback to remove device
1967 * nodes in the given namespace scope.
1969 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1970 acpi_bus_remove, NULL, NULL);
1971 acpi_bus_remove(start->handle, 0, NULL, NULL);
1973 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1975 static int acpi_bus_scan_fixed(void)
1980 * Enumerate all fixed-feature devices.
1982 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
1983 struct acpi_device *device = NULL;
1985 result = acpi_add_single_object(&device, NULL,
1986 ACPI_BUS_TYPE_POWER_BUTTON,
1991 result = device_attach(&device->dev);
1995 device_init_wakeup(&device->dev, true);
1998 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
1999 struct acpi_device *device = NULL;
2001 result = acpi_add_single_object(&device, NULL,
2002 ACPI_BUS_TYPE_SLEEP_BUTTON,
2007 result = device_attach(&device->dev);
2010 return result < 0 ? result : 0;
2013 int __init acpi_scan_init(void)
2017 result = bus_register(&acpi_bus_type);
2019 /* We don't want to quit even if we failed to add suspend/resume */
2020 printk(KERN_ERR PREFIX "Could not register bus type\n");
2023 acpi_pci_root_init();
2024 acpi_pci_link_init();
2025 acpi_platform_init();
2027 acpi_container_init();
2028 acpi_pci_slot_init();
2029 acpi_memory_hotplug_init();
2031 mutex_lock(&acpi_scan_lock);
2033 * Enumerate devices in the ACPI namespace.
2035 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
2039 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
2043 result = acpi_bus_scan_fixed();
2045 acpi_device_unregister(acpi_root);
2049 acpi_update_all_gpes();
2051 acpi_pci_root_hp_init();
2054 mutex_unlock(&acpi_scan_lock);