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);
67 * Creates hid/cid(s) string needed for modalias and uevent
68 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
69 * char *modalias: "acpi:IBM0001:ACPI0001"
71 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
76 struct acpi_hardware_id *id;
78 if (list_empty(&acpi_dev->pnp.ids))
81 len = snprintf(modalias, size, "acpi:");
84 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
85 count = snprintf(&modalias[len], size, "%s:", id->id);
86 if (count < 0 || count >= size)
97 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
98 struct acpi_device *acpi_dev = to_acpi_device(dev);
101 /* Device has no HID and no CID or string is >1024 */
102 len = create_modalias(acpi_dev, buf, 1024);
108 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
110 static int acpi_scan_hot_remove(struct acpi_device *device)
112 acpi_handle handle = device->handle;
113 acpi_handle not_used;
114 struct acpi_object_list arg_list;
115 union acpi_object arg;
118 /* If there is no handle, the device node has been unregistered. */
120 dev_dbg(&device->dev, "ACPI handle missing\n");
121 put_device(&device->dev);
125 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
126 "Hot-removing device %s...\n", dev_name(&device->dev)));
128 acpi_bus_trim(device);
129 /* Device node has been unregistered. */
130 put_device(&device->dev);
133 if (ACPI_SUCCESS(acpi_get_handle(handle, "_LCK", ¬_used))) {
135 arg_list.pointer = &arg;
136 arg.type = ACPI_TYPE_INTEGER;
137 arg.integer.value = 0;
138 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
142 arg_list.pointer = &arg;
143 arg.type = ACPI_TYPE_INTEGER;
144 arg.integer.value = 1;
149 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
150 if (ACPI_FAILURE(status)) {
151 if (status == AE_NOT_FOUND) {
154 acpi_handle_warn(handle, "Eject failed\n");
161 static void acpi_bus_device_eject(void *context)
163 acpi_handle handle = context;
164 struct acpi_device *device = NULL;
165 struct acpi_scan_handler *handler;
166 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
168 mutex_lock(&acpi_scan_lock);
170 acpi_bus_get_device(handle, &device);
174 handler = device->handler;
175 if (!handler || !handler->hotplug.enabled) {
176 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
179 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
180 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
181 if (handler->hotplug.mode == AHM_CONTAINER) {
182 device->flags.eject_pending = true;
183 kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
187 get_device(&device->dev);
188 error = acpi_scan_hot_remove(device);
194 mutex_unlock(&acpi_scan_lock);
198 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST, ost_code,
203 static void acpi_scan_bus_device_check(acpi_handle handle, u32 ost_source)
205 struct acpi_device *device = NULL;
206 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
209 mutex_lock(&acpi_scan_lock);
211 acpi_bus_get_device(handle, &device);
213 dev_warn(&device->dev, "Attempt to re-insert\n");
216 acpi_evaluate_hotplug_ost(handle, ost_source,
217 ACPI_OST_SC_INSERT_IN_PROGRESS, NULL);
218 error = acpi_bus_scan(handle);
220 acpi_handle_warn(handle, "Namespace scan failure\n");
223 error = acpi_bus_get_device(handle, &device);
225 acpi_handle_warn(handle, "Missing device node object\n");
228 ost_code = ACPI_OST_SC_SUCCESS;
229 if (device->handler && device->handler->hotplug.mode == AHM_CONTAINER)
230 kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
233 acpi_evaluate_hotplug_ost(handle, ost_source, ost_code, NULL);
234 mutex_unlock(&acpi_scan_lock);
237 static void acpi_scan_bus_check(void *context)
239 acpi_scan_bus_device_check((acpi_handle)context,
240 ACPI_NOTIFY_BUS_CHECK);
243 static void acpi_scan_device_check(void *context)
245 acpi_scan_bus_device_check((acpi_handle)context,
246 ACPI_NOTIFY_DEVICE_CHECK);
249 static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *not_used)
251 acpi_osd_exec_callback callback;
255 case ACPI_NOTIFY_BUS_CHECK:
256 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
257 callback = acpi_scan_bus_check;
259 case ACPI_NOTIFY_DEVICE_CHECK:
260 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
261 callback = acpi_scan_device_check;
263 case ACPI_NOTIFY_EJECT_REQUEST:
264 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
265 callback = acpi_bus_device_eject;
268 /* non-hotplug event; possibly handled by other handler */
271 status = acpi_os_hotplug_execute(callback, handle);
272 if (ACPI_FAILURE(status))
273 acpi_evaluate_hotplug_ost(handle, type,
274 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
279 * acpi_bus_hot_remove_device: hot-remove a device and its children
280 * @context: struct acpi_eject_event pointer (freed in this func)
282 * Hot-remove a device and its children. This function frees up the
283 * memory space passed by arg context, so that the caller may call
284 * this function asynchronously through acpi_os_hotplug_execute().
286 void acpi_bus_hot_remove_device(void *context)
288 struct acpi_eject_event *ej_event = context;
289 struct acpi_device *device = ej_event->device;
290 acpi_handle handle = device->handle;
293 mutex_lock(&acpi_scan_lock);
295 error = acpi_scan_hot_remove(device);
297 acpi_evaluate_hotplug_ost(handle, ej_event->event,
298 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
301 mutex_unlock(&acpi_scan_lock);
304 EXPORT_SYMBOL(acpi_bus_hot_remove_device);
306 static ssize_t real_power_state_show(struct device *dev,
307 struct device_attribute *attr, char *buf)
309 struct acpi_device *adev = to_acpi_device(dev);
313 ret = acpi_device_get_power(adev, &state);
317 return sprintf(buf, "%s\n", acpi_power_state_string(state));
320 static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
322 static ssize_t power_state_show(struct device *dev,
323 struct device_attribute *attr, char *buf)
325 struct acpi_device *adev = to_acpi_device(dev);
327 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
330 static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
333 acpi_eject_store(struct device *d, struct device_attribute *attr,
334 const char *buf, size_t count)
336 struct acpi_device *acpi_device = to_acpi_device(d);
337 struct acpi_eject_event *ej_event;
338 acpi_object_type not_used;
343 if (!count || buf[0] != '1')
346 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
347 && !acpi_device->driver)
350 status = acpi_get_type(acpi_device->handle, ¬_used);
351 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
354 mutex_lock(&acpi_scan_lock);
356 if (acpi_device->flags.eject_pending) {
357 /* ACPI eject notification event. */
358 ost_source = ACPI_NOTIFY_EJECT_REQUEST;
359 acpi_device->flags.eject_pending = 0;
361 /* Eject initiated by user space. */
362 ost_source = ACPI_OST_EC_OSPM_EJECT;
364 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
369 acpi_evaluate_hotplug_ost(acpi_device->handle, ost_source,
370 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
371 ej_event->device = acpi_device;
372 ej_event->event = ost_source;
373 get_device(&acpi_device->dev);
374 status = acpi_os_hotplug_execute(acpi_bus_hot_remove_device, ej_event);
375 if (ACPI_FAILURE(status)) {
376 put_device(&acpi_device->dev);
378 ret = status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
384 mutex_unlock(&acpi_scan_lock);
388 acpi_evaluate_hotplug_ost(acpi_device->handle, ost_source,
389 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
393 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
396 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
397 struct acpi_device *acpi_dev = to_acpi_device(dev);
399 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
401 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
403 static ssize_t acpi_device_uid_show(struct device *dev,
404 struct device_attribute *attr, char *buf)
406 struct acpi_device *acpi_dev = to_acpi_device(dev);
408 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
410 static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
412 static ssize_t acpi_device_adr_show(struct device *dev,
413 struct device_attribute *attr, char *buf)
415 struct acpi_device *acpi_dev = to_acpi_device(dev);
417 return sprintf(buf, "0x%08x\n",
418 (unsigned int)(acpi_dev->pnp.bus_address));
420 static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
423 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
424 struct acpi_device *acpi_dev = to_acpi_device(dev);
425 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
428 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
432 result = sprintf(buf, "%s\n", (char*)path.pointer);
437 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
439 /* sysfs file that shows description text from the ACPI _STR method */
440 static ssize_t description_show(struct device *dev,
441 struct device_attribute *attr,
443 struct acpi_device *acpi_dev = to_acpi_device(dev);
446 if (acpi_dev->pnp.str_obj == NULL)
450 * The _STR object contains a Unicode identifier for a device.
451 * We need to convert to utf-8 so it can be displayed.
453 result = utf16s_to_utf8s(
454 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
455 acpi_dev->pnp.str_obj->buffer.length,
456 UTF16_LITTLE_ENDIAN, buf,
459 buf[result++] = '\n';
463 static DEVICE_ATTR(description, 0444, description_show, NULL);
466 acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
468 struct acpi_device *acpi_dev = to_acpi_device(dev);
470 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
472 static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
474 static int acpi_device_setup_files(struct acpi_device *dev)
476 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
479 unsigned long long sun;
483 * Devices gotten from FADT don't have a "path" attribute
486 result = device_create_file(&dev->dev, &dev_attr_path);
491 if (!list_empty(&dev->pnp.ids)) {
492 result = device_create_file(&dev->dev, &dev_attr_hid);
496 result = device_create_file(&dev->dev, &dev_attr_modalias);
502 * If device has _STR, 'description' file is created
504 status = acpi_get_handle(dev->handle, "_STR", &temp);
505 if (ACPI_SUCCESS(status)) {
506 status = acpi_evaluate_object(dev->handle, "_STR",
508 if (ACPI_FAILURE(status))
509 buffer.pointer = NULL;
510 dev->pnp.str_obj = buffer.pointer;
511 result = device_create_file(&dev->dev, &dev_attr_description);
516 if (dev->flags.bus_address)
517 result = device_create_file(&dev->dev, &dev_attr_adr);
518 if (dev->pnp.unique_id)
519 result = device_create_file(&dev->dev, &dev_attr_uid);
521 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
522 if (ACPI_SUCCESS(status)) {
523 dev->pnp.sun = (unsigned long)sun;
524 result = device_create_file(&dev->dev, &dev_attr_sun);
528 dev->pnp.sun = (unsigned long)-1;
532 * If device has _EJ0, 'eject' file is created that is used to trigger
533 * hot-removal function from userland.
535 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
536 if (ACPI_SUCCESS(status)) {
537 result = device_create_file(&dev->dev, &dev_attr_eject);
542 if (dev->flags.power_manageable) {
543 result = device_create_file(&dev->dev, &dev_attr_power_state);
547 if (dev->power.flags.power_resources)
548 result = device_create_file(&dev->dev,
549 &dev_attr_real_power_state);
556 static void acpi_device_remove_files(struct acpi_device *dev)
561 if (dev->flags.power_manageable) {
562 device_remove_file(&dev->dev, &dev_attr_power_state);
563 if (dev->power.flags.power_resources)
564 device_remove_file(&dev->dev,
565 &dev_attr_real_power_state);
569 * If device has _STR, remove 'description' file
571 status = acpi_get_handle(dev->handle, "_STR", &temp);
572 if (ACPI_SUCCESS(status)) {
573 kfree(dev->pnp.str_obj);
574 device_remove_file(&dev->dev, &dev_attr_description);
577 * If device has _EJ0, remove 'eject' file.
579 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
580 if (ACPI_SUCCESS(status))
581 device_remove_file(&dev->dev, &dev_attr_eject);
583 status = acpi_get_handle(dev->handle, "_SUN", &temp);
584 if (ACPI_SUCCESS(status))
585 device_remove_file(&dev->dev, &dev_attr_sun);
587 if (dev->pnp.unique_id)
588 device_remove_file(&dev->dev, &dev_attr_uid);
589 if (dev->flags.bus_address)
590 device_remove_file(&dev->dev, &dev_attr_adr);
591 device_remove_file(&dev->dev, &dev_attr_modalias);
592 device_remove_file(&dev->dev, &dev_attr_hid);
594 device_remove_file(&dev->dev, &dev_attr_path);
596 /* --------------------------------------------------------------------------
598 -------------------------------------------------------------------------- */
600 static const struct acpi_device_id *__acpi_match_device(
601 struct acpi_device *device, const struct acpi_device_id *ids)
603 const struct acpi_device_id *id;
604 struct acpi_hardware_id *hwid;
607 * If the device is not present, it is unnecessary to load device
610 if (!device->status.present)
613 for (id = ids; id->id[0]; id++)
614 list_for_each_entry(hwid, &device->pnp.ids, list)
615 if (!strcmp((char *) id->id, hwid->id))
622 * acpi_match_device - Match a struct device against a given list of ACPI IDs
623 * @ids: Array of struct acpi_device_id object to match against.
624 * @dev: The device structure to match.
626 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
627 * object for that handle and use that object to match against a given list of
630 * Return a pointer to the first matching ID on success or %NULL on failure.
632 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
633 const struct device *dev)
635 struct acpi_device *adev;
636 acpi_handle handle = ACPI_HANDLE(dev);
638 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
641 return __acpi_match_device(adev, ids);
643 EXPORT_SYMBOL_GPL(acpi_match_device);
645 int acpi_match_device_ids(struct acpi_device *device,
646 const struct acpi_device_id *ids)
648 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
650 EXPORT_SYMBOL(acpi_match_device_ids);
652 void acpi_free_ids(struct acpi_device *device)
654 struct acpi_hardware_id *id, *tmp;
656 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
660 kfree(device->pnp.unique_id);
663 static void acpi_free_power_resources_lists(struct acpi_device *device)
667 if (device->wakeup.flags.valid)
668 acpi_power_resources_list_free(&device->wakeup.resources);
670 if (!device->flags.power_manageable)
673 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
674 struct acpi_device_power_state *ps = &device->power.states[i];
675 acpi_power_resources_list_free(&ps->resources);
679 static void acpi_device_release(struct device *dev)
681 struct acpi_device *acpi_dev = to_acpi_device(dev);
683 acpi_free_ids(acpi_dev);
684 acpi_free_power_resources_lists(acpi_dev);
688 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
690 struct acpi_device *acpi_dev = to_acpi_device(dev);
691 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
693 return acpi_dev->flags.match_driver
694 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
697 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
699 struct acpi_device *acpi_dev = to_acpi_device(dev);
702 if (list_empty(&acpi_dev->pnp.ids))
705 if (add_uevent_var(env, "MODALIAS="))
707 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
708 sizeof(env->buf) - env->buflen);
709 if (len >= (sizeof(env->buf) - env->buflen))
715 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
717 struct acpi_device *device = data;
719 device->driver->ops.notify(device, event);
722 static acpi_status acpi_device_notify_fixed(void *data)
724 struct acpi_device *device = data;
726 /* Fixed hardware devices have no handles */
727 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
731 static int acpi_device_install_notify_handler(struct acpi_device *device)
735 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
737 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
738 acpi_device_notify_fixed,
740 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
742 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
743 acpi_device_notify_fixed,
746 status = acpi_install_notify_handler(device->handle,
751 if (ACPI_FAILURE(status))
756 static void acpi_device_remove_notify_handler(struct acpi_device *device)
758 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
759 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
760 acpi_device_notify_fixed);
761 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
762 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
763 acpi_device_notify_fixed);
765 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
769 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
770 static int acpi_device_probe(struct device * dev)
772 struct acpi_device *acpi_dev = to_acpi_device(dev);
773 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
776 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
778 if (acpi_drv->ops.notify) {
779 ret = acpi_device_install_notify_handler(acpi_dev);
781 if (acpi_drv->ops.remove)
782 acpi_drv->ops.remove(acpi_dev);
783 acpi_dev->driver = NULL;
784 acpi_dev->driver_data = NULL;
789 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
790 "Found driver [%s] for device [%s]\n",
791 acpi_drv->name, acpi_dev->pnp.bus_id));
797 static int acpi_device_remove(struct device * dev)
799 struct acpi_device *acpi_dev = to_acpi_device(dev);
800 struct acpi_driver *acpi_drv = acpi_dev->driver;
803 if (acpi_drv->ops.notify)
804 acpi_device_remove_notify_handler(acpi_dev);
805 if (acpi_drv->ops.remove)
806 acpi_drv->ops.remove(acpi_dev);
808 acpi_dev->driver = NULL;
809 acpi_dev->driver_data = NULL;
815 struct bus_type acpi_bus_type = {
817 .match = acpi_bus_match,
818 .probe = acpi_device_probe,
819 .remove = acpi_device_remove,
820 .uevent = acpi_device_uevent,
823 int acpi_device_add(struct acpi_device *device,
824 void (*release)(struct device *))
827 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
830 if (device->handle) {
833 status = acpi_attach_data(device->handle, acpi_bus_data_handler,
835 if (ACPI_FAILURE(status)) {
836 acpi_handle_err(device->handle,
837 "Unable to attach device data\n");
845 * Link this device to its parent and siblings.
847 INIT_LIST_HEAD(&device->children);
848 INIT_LIST_HEAD(&device->node);
849 INIT_LIST_HEAD(&device->wakeup_list);
850 INIT_LIST_HEAD(&device->physical_node_list);
851 mutex_init(&device->physical_node_lock);
852 INIT_LIST_HEAD(&device->power_dependent);
854 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
856 pr_err(PREFIX "Memory allocation error\n");
861 mutex_lock(&acpi_device_lock);
863 * Find suitable bus_id and instance number in acpi_bus_id_list
864 * If failed, create one and link it into acpi_bus_id_list
866 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
867 if (!strcmp(acpi_device_bus_id->bus_id,
868 acpi_device_hid(device))) {
869 acpi_device_bus_id->instance_no++;
876 acpi_device_bus_id = new_bus_id;
877 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
878 acpi_device_bus_id->instance_no = 0;
879 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
881 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
884 list_add_tail(&device->node, &device->parent->children);
886 if (device->wakeup.flags.valid)
887 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
888 mutex_unlock(&acpi_device_lock);
891 device->dev.parent = &device->parent->dev;
892 device->dev.bus = &acpi_bus_type;
893 device->dev.release = release;
894 result = device_add(&device->dev);
896 dev_err(&device->dev, "Error registering device\n");
900 result = acpi_device_setup_files(device);
902 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
903 dev_name(&device->dev));
905 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
909 mutex_lock(&acpi_device_lock);
911 list_del(&device->node);
912 list_del(&device->wakeup_list);
913 mutex_unlock(&acpi_device_lock);
916 acpi_detach_data(device->handle, acpi_bus_data_handler);
920 static void acpi_device_unregister(struct acpi_device *device)
922 mutex_lock(&acpi_device_lock);
924 list_del(&device->node);
926 list_del(&device->wakeup_list);
927 mutex_unlock(&acpi_device_lock);
929 acpi_detach_data(device->handle, acpi_bus_data_handler);
931 acpi_power_add_remove_device(device, false);
932 acpi_device_remove_files(device);
934 device->remove(device);
936 device_del(&device->dev);
938 * Transition the device to D3cold to drop the reference counts of all
939 * power resources the device depends on and turn off the ones that have
940 * no more references.
942 acpi_device_set_power(device, ACPI_STATE_D3_COLD);
943 device->handle = NULL;
944 put_device(&device->dev);
947 /* --------------------------------------------------------------------------
949 -------------------------------------------------------------------------- */
951 * acpi_bus_driver_init - add a device to a driver
952 * @device: the device to add and initialize
953 * @driver: driver for the device
955 * Used to initialize a device via its device driver. Called whenever a
956 * driver is bound to a device. Invokes the driver's add() ops.
959 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
963 if (!device || !driver)
966 if (!driver->ops.add)
969 result = driver->ops.add(device);
971 device->driver = NULL;
972 device->driver_data = NULL;
976 device->driver = driver;
979 * TBD - Configuration Management: Assign resources to device based
980 * upon possible configuration and currently allocated resources.
983 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
984 "Driver successfully bound to device\n"));
989 * acpi_bus_register_driver - register a driver with the ACPI bus
990 * @driver: driver being registered
992 * Registers a driver with the ACPI bus. Searches the namespace for all
993 * devices that match the driver's criteria and binds. Returns zero for
994 * success or a negative error status for failure.
996 int acpi_bus_register_driver(struct acpi_driver *driver)
1002 driver->drv.name = driver->name;
1003 driver->drv.bus = &acpi_bus_type;
1004 driver->drv.owner = driver->owner;
1006 ret = driver_register(&driver->drv);
1010 EXPORT_SYMBOL(acpi_bus_register_driver);
1013 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
1014 * @driver: driver to unregister
1016 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1017 * devices that match the driver's criteria and unbinds.
1019 void acpi_bus_unregister_driver(struct acpi_driver *driver)
1021 driver_unregister(&driver->drv);
1024 EXPORT_SYMBOL(acpi_bus_unregister_driver);
1026 /* --------------------------------------------------------------------------
1028 -------------------------------------------------------------------------- */
1029 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1031 struct acpi_device *device = NULL;
1035 * Fixed hardware devices do not appear in the namespace and do not
1036 * have handles, but we fabricate acpi_devices for them, so we have
1037 * to deal with them specially.
1043 status = acpi_get_parent(handle, &handle);
1044 if (ACPI_FAILURE(status))
1045 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1046 } while (acpi_bus_get_device(handle, &device));
1051 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1055 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1056 union acpi_object *obj;
1058 status = acpi_get_handle(handle, "_EJD", &tmp);
1059 if (ACPI_FAILURE(status))
1062 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1063 if (ACPI_SUCCESS(status)) {
1064 obj = buffer.pointer;
1065 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1067 kfree(buffer.pointer);
1071 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1073 void acpi_bus_data_handler(acpi_handle handle, void *context)
1081 static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1082 struct acpi_device_wakeup *wakeup)
1084 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1085 union acpi_object *package = NULL;
1086 union acpi_object *element = NULL;
1093 INIT_LIST_HEAD(&wakeup->resources);
1096 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1097 if (ACPI_FAILURE(status)) {
1098 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
1102 package = (union acpi_object *)buffer.pointer;
1104 if (!package || package->package.count < 2)
1107 element = &(package->package.elements[0]);
1111 if (element->type == ACPI_TYPE_PACKAGE) {
1112 if ((element->package.count < 2) ||
1113 (element->package.elements[0].type !=
1114 ACPI_TYPE_LOCAL_REFERENCE)
1115 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
1118 wakeup->gpe_device =
1119 element->package.elements[0].reference.handle;
1120 wakeup->gpe_number =
1121 (u32) element->package.elements[1].integer.value;
1122 } else if (element->type == ACPI_TYPE_INTEGER) {
1123 wakeup->gpe_device = NULL;
1124 wakeup->gpe_number = element->integer.value;
1129 element = &(package->package.elements[1]);
1130 if (element->type != ACPI_TYPE_INTEGER)
1133 wakeup->sleep_state = element->integer.value;
1135 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1139 if (!list_empty(&wakeup->resources)) {
1142 err = acpi_power_wakeup_list_init(&wakeup->resources,
1145 acpi_handle_warn(handle, "Retrieving current states "
1146 "of wakeup power resources failed\n");
1147 acpi_power_resources_list_free(&wakeup->resources);
1150 if (sleep_state < wakeup->sleep_state) {
1151 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1152 "(S%d) by S%d from power resources\n",
1153 (int)wakeup->sleep_state, sleep_state);
1154 wakeup->sleep_state = sleep_state;
1157 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
1160 kfree(buffer.pointer);
1164 static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
1166 struct acpi_device_id button_device_ids[] = {
1173 acpi_event_status event_status;
1175 device->wakeup.flags.notifier_present = 0;
1177 /* Power button, Lid switch always enable wakeup */
1178 if (!acpi_match_device_ids(device, button_device_ids)) {
1179 device->wakeup.flags.run_wake = 1;
1180 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1181 /* Do not use Lid/sleep button for S5 wakeup */
1182 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1183 device->wakeup.sleep_state = ACPI_STATE_S4;
1185 device_set_wakeup_capable(&device->dev, true);
1189 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1190 device->wakeup.gpe_number,
1192 if (status == AE_OK)
1193 device->wakeup.flags.run_wake =
1194 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1197 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1200 acpi_status status = 0;
1203 /* Presence of _PRW indicates wake capable */
1204 status = acpi_get_handle(device->handle, "_PRW", &temp);
1205 if (ACPI_FAILURE(status))
1208 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1211 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
1215 device->wakeup.flags.valid = 1;
1216 device->wakeup.prepare_count = 0;
1217 acpi_bus_set_run_wake_flags(device);
1218 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1219 * system for the ACPI device with the _PRW object.
1220 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1221 * So it is necessary to call _DSW object first. Only when it is not
1222 * present will the _PSW object used.
1224 err = acpi_device_sleep_wake(device, 0, 0, 0);
1226 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1227 "error in _DSW or _PSW evaluation\n"));
1230 static void acpi_bus_init_power_state(struct acpi_device *device, int state)
1232 struct acpi_device_power_state *ps = &device->power.states[state];
1233 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1234 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1238 INIT_LIST_HEAD(&ps->resources);
1240 /* Evaluate "_PRx" to get referenced power resources */
1241 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1242 if (ACPI_SUCCESS(status)) {
1243 union acpi_object *package = buffer.pointer;
1245 if (buffer.length && package
1246 && package->type == ACPI_TYPE_PACKAGE
1247 && package->package.count) {
1248 int err = acpi_extract_power_resources(package, 0,
1251 device->power.flags.power_resources = 1;
1253 ACPI_FREE(buffer.pointer);
1256 /* Evaluate "_PSx" to see if we can do explicit sets */
1258 status = acpi_get_handle(device->handle, pathname, &handle);
1259 if (ACPI_SUCCESS(status))
1260 ps->flags.explicit_set = 1;
1263 * State is valid if there are means to put the device into it.
1264 * D3hot is only valid if _PR3 present.
1266 if (!list_empty(&ps->resources)
1267 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1268 ps->flags.valid = 1;
1269 ps->flags.os_accessible = 1;
1272 ps->power = -1; /* Unknown - driver assigned */
1273 ps->latency = -1; /* Unknown - driver assigned */
1276 static void acpi_bus_get_power_flags(struct acpi_device *device)
1282 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1283 status = acpi_get_handle(device->handle, "_PS0", &handle);
1284 if (ACPI_FAILURE(status)) {
1285 status = acpi_get_handle(device->handle, "_PR0", &handle);
1286 if (ACPI_FAILURE(status))
1290 device->flags.power_manageable = 1;
1293 * Power Management Flags
1295 status = acpi_get_handle(device->handle, "_PSC", &handle);
1296 if (ACPI_SUCCESS(status))
1297 device->power.flags.explicit_get = 1;
1298 status = acpi_get_handle(device->handle, "_IRC", &handle);
1299 if (ACPI_SUCCESS(status))
1300 device->power.flags.inrush_current = 1;
1303 * Enumerate supported power management states
1305 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1306 acpi_bus_init_power_state(device, i);
1308 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1310 /* Set defaults for D0 and D3 states (always valid) */
1311 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1312 device->power.states[ACPI_STATE_D0].power = 100;
1313 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1314 device->power.states[ACPI_STATE_D3].power = 0;
1316 /* Set D3cold's explicit_set flag if _PS3 exists. */
1317 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1318 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1320 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1321 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1322 device->power.flags.power_resources)
1323 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1325 if (acpi_bus_init_power(device)) {
1326 acpi_free_power_resources_lists(device);
1327 device->flags.power_manageable = 0;
1331 static void acpi_bus_get_flags(struct acpi_device *device)
1333 acpi_status status = AE_OK;
1334 acpi_handle temp = NULL;
1336 /* Presence of _STA indicates 'dynamic_status' */
1337 status = acpi_get_handle(device->handle, "_STA", &temp);
1338 if (ACPI_SUCCESS(status))
1339 device->flags.dynamic_status = 1;
1341 /* Presence of _RMV indicates 'removable' */
1342 status = acpi_get_handle(device->handle, "_RMV", &temp);
1343 if (ACPI_SUCCESS(status))
1344 device->flags.removable = 1;
1346 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1347 status = acpi_get_handle(device->handle, "_EJD", &temp);
1348 if (ACPI_SUCCESS(status))
1349 device->flags.ejectable = 1;
1351 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1352 if (ACPI_SUCCESS(status))
1353 device->flags.ejectable = 1;
1357 static void acpi_device_get_busid(struct acpi_device *device)
1359 char bus_id[5] = { '?', 0 };
1360 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1366 * The device's Bus ID is simply the object name.
1367 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1369 if (ACPI_IS_ROOT_DEVICE(device)) {
1370 strcpy(device->pnp.bus_id, "ACPI");
1374 switch (device->device_type) {
1375 case ACPI_BUS_TYPE_POWER_BUTTON:
1376 strcpy(device->pnp.bus_id, "PWRF");
1378 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1379 strcpy(device->pnp.bus_id, "SLPF");
1382 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1383 /* Clean up trailing underscores (if any) */
1384 for (i = 3; i > 1; i--) {
1385 if (bus_id[i] == '_')
1390 strcpy(device->pnp.bus_id, bus_id);
1396 * acpi_bay_match - see if a device is an ejectable driver bay
1398 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1399 * then we can safely call it an ejectable drive bay
1401 static int acpi_bay_match(struct acpi_device *device){
1405 acpi_handle phandle;
1407 handle = device->handle;
1409 status = acpi_get_handle(handle, "_EJ0", &tmp);
1410 if (ACPI_FAILURE(status))
1413 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1414 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1415 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1416 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1419 if (acpi_get_parent(handle, &phandle))
1422 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1423 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1424 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1425 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1432 * acpi_dock_match - see if a device has a _DCK method
1434 static int acpi_dock_match(struct acpi_device *device)
1437 return acpi_get_handle(device->handle, "_DCK", &tmp);
1440 const char *acpi_device_hid(struct acpi_device *device)
1442 struct acpi_hardware_id *hid;
1444 if (list_empty(&device->pnp.ids))
1447 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1450 EXPORT_SYMBOL(acpi_device_hid);
1452 static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1454 struct acpi_hardware_id *id;
1456 id = kmalloc(sizeof(*id), GFP_KERNEL);
1460 id->id = kstrdup(dev_id, GFP_KERNEL);
1466 list_add_tail(&id->list, &device->pnp.ids);
1470 * Old IBM workstations have a DSDT bug wherein the SMBus object
1471 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1472 * prefix. Work around this.
1474 static int acpi_ibm_smbus_match(struct acpi_device *device)
1476 acpi_handle h_dummy;
1477 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1480 if (!dmi_name_in_vendors("IBM"))
1483 /* Look for SMBS object */
1484 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1488 if (strcmp("SMBS", path.pointer)) {
1493 /* Does it have the necessary (but misnamed) methods? */
1495 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1496 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1497 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1500 kfree(path.pointer);
1504 static void acpi_device_set_id(struct acpi_device *device)
1507 struct acpi_device_info *info;
1508 struct acpi_pnp_device_id_list *cid_list;
1511 switch (device->device_type) {
1512 case ACPI_BUS_TYPE_DEVICE:
1513 if (ACPI_IS_ROOT_DEVICE(device)) {
1514 acpi_add_id(device, ACPI_SYSTEM_HID);
1518 status = acpi_get_object_info(device->handle, &info);
1519 if (ACPI_FAILURE(status)) {
1520 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1524 if (info->valid & ACPI_VALID_HID)
1525 acpi_add_id(device, info->hardware_id.string);
1526 if (info->valid & ACPI_VALID_CID) {
1527 cid_list = &info->compatible_id_list;
1528 for (i = 0; i < cid_list->count; i++)
1529 acpi_add_id(device, cid_list->ids[i].string);
1531 if (info->valid & ACPI_VALID_ADR) {
1532 device->pnp.bus_address = info->address;
1533 device->flags.bus_address = 1;
1535 if (info->valid & ACPI_VALID_UID)
1536 device->pnp.unique_id = kstrdup(info->unique_id.string,
1542 * Some devices don't reliably have _HIDs & _CIDs, so add
1543 * synthetic HIDs to make sure drivers can find them.
1545 if (acpi_is_video_device(device))
1546 acpi_add_id(device, ACPI_VIDEO_HID);
1547 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1548 acpi_add_id(device, ACPI_BAY_HID);
1549 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1550 acpi_add_id(device, ACPI_DOCK_HID);
1551 else if (!acpi_ibm_smbus_match(device))
1552 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
1553 else if (list_empty(&device->pnp.ids) &&
1554 ACPI_IS_ROOT_DEVICE(device->parent)) {
1555 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1556 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1557 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1561 case ACPI_BUS_TYPE_POWER:
1562 acpi_add_id(device, ACPI_POWER_HID);
1564 case ACPI_BUS_TYPE_PROCESSOR:
1565 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
1567 case ACPI_BUS_TYPE_THERMAL:
1568 acpi_add_id(device, ACPI_THERMAL_HID);
1570 case ACPI_BUS_TYPE_POWER_BUTTON:
1571 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
1573 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1574 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
1579 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1580 int type, unsigned long long sta)
1582 INIT_LIST_HEAD(&device->pnp.ids);
1583 device->device_type = type;
1584 device->handle = handle;
1585 device->parent = acpi_bus_get_parent(handle);
1586 STRUCT_TO_INT(device->status) = sta;
1587 acpi_device_get_busid(device);
1588 acpi_device_set_id(device);
1589 acpi_bus_get_flags(device);
1590 device->flags.match_driver = false;
1591 device_initialize(&device->dev);
1592 dev_set_uevent_suppress(&device->dev, true);
1595 void acpi_device_add_finalize(struct acpi_device *device)
1597 device->flags.match_driver = true;
1598 dev_set_uevent_suppress(&device->dev, false);
1599 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1602 static int acpi_add_single_object(struct acpi_device **child,
1603 acpi_handle handle, int type,
1604 unsigned long long sta)
1607 struct acpi_device *device;
1608 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1610 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1612 printk(KERN_ERR PREFIX "Memory allocation error\n");
1616 acpi_init_device_object(device, handle, type, sta);
1617 acpi_bus_get_power_flags(device);
1618 acpi_bus_get_wakeup_device_flags(device);
1620 result = acpi_device_add(device, acpi_device_release);
1622 acpi_device_release(&device->dev);
1626 acpi_power_add_remove_device(device, true);
1627 acpi_device_add_finalize(device);
1628 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1629 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1630 dev_name(&device->dev), (char *) buffer.pointer,
1631 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1632 kfree(buffer.pointer);
1637 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1638 unsigned long long *sta)
1641 acpi_object_type acpi_type;
1643 status = acpi_get_type(handle, &acpi_type);
1644 if (ACPI_FAILURE(status))
1647 switch (acpi_type) {
1648 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1649 case ACPI_TYPE_DEVICE:
1650 *type = ACPI_BUS_TYPE_DEVICE;
1651 status = acpi_bus_get_status_handle(handle, sta);
1652 if (ACPI_FAILURE(status))
1655 case ACPI_TYPE_PROCESSOR:
1656 *type = ACPI_BUS_TYPE_PROCESSOR;
1657 status = acpi_bus_get_status_handle(handle, sta);
1658 if (ACPI_FAILURE(status))
1661 case ACPI_TYPE_THERMAL:
1662 *type = ACPI_BUS_TYPE_THERMAL;
1663 *sta = ACPI_STA_DEFAULT;
1665 case ACPI_TYPE_POWER:
1666 *type = ACPI_BUS_TYPE_POWER;
1667 *sta = ACPI_STA_DEFAULT;
1676 static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1677 const struct acpi_device_id **matchid)
1679 struct acpi_scan_handler *handler;
1681 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node) {
1682 const struct acpi_device_id *devid;
1684 for (devid = handler->ids; devid->id[0]; devid++)
1685 if (!strcmp((char *)devid->id, idstr)) {
1695 static void acpi_scan_init_hotplug(acpi_handle handle)
1697 struct acpi_device_info *info;
1698 struct acpi_scan_handler *handler;
1700 if (ACPI_FAILURE(acpi_get_object_info(handle, &info)))
1703 if (!(info->valid & ACPI_VALID_HID)) {
1709 * This relies on the fact that acpi_install_notify_handler() will not
1710 * install the same notify handler routine twice for the same handle.
1712 handler = acpi_scan_match_handler(info->hardware_id.string, NULL);
1714 if (handler && handler->hotplug.enabled)
1715 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1716 acpi_hotplug_notify_cb, NULL);
1719 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1720 void *not_used, void **return_value)
1722 struct acpi_device *device = NULL;
1724 unsigned long long sta;
1728 acpi_bus_get_device(handle, &device);
1732 result = acpi_bus_type_and_status(handle, &type, &sta);
1736 if (type == ACPI_BUS_TYPE_POWER) {
1737 acpi_add_power_resource(handle);
1741 acpi_scan_init_hotplug(handle);
1743 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1744 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
1745 struct acpi_device_wakeup wakeup;
1748 status = acpi_get_handle(handle, "_PRW", &temp);
1749 if (ACPI_SUCCESS(status)) {
1750 acpi_bus_extract_wakeup_device_power_package(handle,
1752 acpi_power_resources_list_free(&wakeup.resources);
1754 return AE_CTRL_DEPTH;
1757 acpi_add_single_object(&device, handle, type, sta);
1759 return AE_CTRL_DEPTH;
1763 *return_value = device;
1768 static int acpi_scan_attach_handler(struct acpi_device *device)
1770 struct acpi_hardware_id *hwid;
1773 list_for_each_entry(hwid, &device->pnp.ids, list) {
1774 const struct acpi_device_id *devid;
1775 struct acpi_scan_handler *handler;
1777 handler = acpi_scan_match_handler(hwid->id, &devid);
1779 ret = handler->attach(device, devid);
1781 device->handler = handler;
1783 } else if (ret < 0) {
1791 static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1792 void *not_used, void **ret_not_used)
1794 struct acpi_device *device;
1795 unsigned long long sta_not_used;
1799 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1800 * namespace walks prematurely.
1802 if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
1805 if (acpi_bus_get_device(handle, &device))
1806 return AE_CTRL_DEPTH;
1808 ret = acpi_scan_attach_handler(device);
1810 return ret > 0 ? AE_OK : AE_CTRL_DEPTH;
1812 ret = device_attach(&device->dev);
1813 return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
1817 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1818 * @handle: Root of the namespace scope to scan.
1820 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1823 * If no devices were found, -ENODEV is returned, but it does not mean that
1824 * there has been a real error. There just have been no suitable ACPI objects
1825 * in the table trunk from which the kernel could create a device and add an
1826 * appropriate driver.
1828 * Must be called under acpi_scan_lock.
1830 int acpi_bus_scan(acpi_handle handle)
1832 void *device = NULL;
1835 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
1836 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1837 acpi_bus_check_add, NULL, NULL, &device);
1841 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
1842 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1843 acpi_bus_device_attach, NULL, NULL, NULL);
1847 EXPORT_SYMBOL(acpi_bus_scan);
1849 static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
1850 void *not_used, void **ret_not_used)
1852 struct acpi_device *device = NULL;
1854 if (!acpi_bus_get_device(handle, &device)) {
1855 struct acpi_scan_handler *dev_handler = device->handler;
1857 device->removal_type = ACPI_BUS_REMOVAL_EJECT;
1859 if (dev_handler->detach)
1860 dev_handler->detach(device);
1862 device->handler = NULL;
1864 device_release_driver(&device->dev);
1870 static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
1871 void *not_used, void **ret_not_used)
1873 struct acpi_device *device = NULL;
1875 if (!acpi_bus_get_device(handle, &device))
1876 acpi_device_unregister(device);
1882 * acpi_bus_trim - Remove ACPI device node and all of its descendants
1883 * @start: Root of the ACPI device nodes subtree to remove.
1885 * Must be called under acpi_scan_lock.
1887 void acpi_bus_trim(struct acpi_device *start)
1890 * Execute acpi_bus_device_detach() as a post-order callback to detach
1891 * all ACPI drivers from the device nodes being removed.
1893 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1894 acpi_bus_device_detach, NULL, NULL);
1895 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
1897 * Execute acpi_bus_remove() as a post-order callback to remove device
1898 * nodes in the given namespace scope.
1900 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1901 acpi_bus_remove, NULL, NULL);
1902 acpi_bus_remove(start->handle, 0, NULL, NULL);
1904 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1906 static int acpi_bus_scan_fixed(void)
1911 * Enumerate all fixed-feature devices.
1913 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
1914 struct acpi_device *device = NULL;
1916 result = acpi_add_single_object(&device, NULL,
1917 ACPI_BUS_TYPE_POWER_BUTTON,
1922 result = device_attach(&device->dev);
1926 device_init_wakeup(&device->dev, true);
1929 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
1930 struct acpi_device *device = NULL;
1932 result = acpi_add_single_object(&device, NULL,
1933 ACPI_BUS_TYPE_SLEEP_BUTTON,
1938 result = device_attach(&device->dev);
1941 return result < 0 ? result : 0;
1944 int __init acpi_scan_init(void)
1948 result = bus_register(&acpi_bus_type);
1950 /* We don't want to quit even if we failed to add suspend/resume */
1951 printk(KERN_ERR PREFIX "Could not register bus type\n");
1954 acpi_pci_root_init();
1955 acpi_pci_link_init();
1956 acpi_platform_init();
1958 acpi_container_init();
1959 acpi_pci_slot_init();
1961 mutex_lock(&acpi_scan_lock);
1963 * Enumerate devices in the ACPI namespace.
1965 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
1969 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
1973 result = acpi_bus_scan_fixed();
1975 acpi_device_unregister(acpi_root);
1979 acpi_update_all_gpes();
1981 acpi_pci_root_hp_init();
1984 mutex_unlock(&acpi_scan_lock);