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 <asm/pgtable.h>
19 #define _COMPONENT ACPI_BUS_COMPONENT
20 ACPI_MODULE_NAME("scan");
21 extern struct acpi_device *acpi_root;
23 #define ACPI_BUS_CLASS "system_bus"
24 #define ACPI_BUS_HID "LNXSYBUS"
25 #define ACPI_BUS_DEVICE_NAME "System Bus"
27 #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
29 #define INVALID_ACPI_HANDLE ((acpi_handle)empty_zero_page)
32 * If set, devices will be hot-removed even if they cannot be put offline
33 * gracefully (from the kernel's standpoint).
35 bool acpi_force_hot_remove;
37 static const char *dummy_hid = "device";
39 static LIST_HEAD(acpi_bus_id_list);
40 static DEFINE_MUTEX(acpi_scan_lock);
41 static LIST_HEAD(acpi_scan_handlers_list);
42 DEFINE_MUTEX(acpi_device_lock);
43 LIST_HEAD(acpi_wakeup_device_list);
45 struct acpi_device_bus_id{
47 unsigned int instance_no;
48 struct list_head node;
51 void acpi_scan_lock_acquire(void)
53 mutex_lock(&acpi_scan_lock);
55 EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
57 void acpi_scan_lock_release(void)
59 mutex_unlock(&acpi_scan_lock);
61 EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
63 int acpi_scan_add_handler(struct acpi_scan_handler *handler)
65 if (!handler || !handler->attach)
68 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
72 int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
73 const char *hotplug_profile_name)
77 error = acpi_scan_add_handler(handler);
81 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
86 * Creates hid/cid(s) string needed for modalias and uevent
87 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
88 * char *modalias: "acpi:IBM0001:ACPI0001"
89 * Return: 0: no _HID and no _CID
90 * -EINVAL: output error
91 * -ENOMEM: output is truncated
93 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
98 struct acpi_hardware_id *id;
100 if (list_empty(&acpi_dev->pnp.ids))
103 len = snprintf(modalias, size, "acpi:");
106 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
107 count = snprintf(&modalias[len], size, "%s:", id->id);
116 modalias[len] = '\0';
121 * Creates uevent modalias field for ACPI enumerated devices.
122 * Because the other buses does not support ACPI HIDs & CIDs.
123 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
124 * "acpi:IBM0001:ACPI0001"
126 int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
128 struct acpi_device *acpi_dev;
131 acpi_dev = ACPI_COMPANION(dev);
135 /* Fall back to bus specific way of modalias exporting */
136 if (list_empty(&acpi_dev->pnp.ids))
139 if (add_uevent_var(env, "MODALIAS="))
141 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
142 sizeof(env->buf) - env->buflen);
148 EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias);
151 * Creates modalias sysfs attribute for ACPI enumerated devices.
152 * Because the other buses does not support ACPI HIDs & CIDs.
153 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
154 * "acpi:IBM0001:ACPI0001"
156 int acpi_device_modalias(struct device *dev, char *buf, int size)
158 struct acpi_device *acpi_dev;
161 acpi_dev = ACPI_COMPANION(dev);
165 /* Fall back to bus specific way of modalias exporting */
166 if (list_empty(&acpi_dev->pnp.ids))
169 len = create_modalias(acpi_dev, buf, size -1);
175 EXPORT_SYMBOL_GPL(acpi_device_modalias);
178 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
179 struct acpi_device *acpi_dev = to_acpi_device(dev);
182 len = create_modalias(acpi_dev, buf, 1024);
188 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
190 bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
192 struct acpi_device_physical_node *pn;
195 mutex_lock(&adev->physical_node_lock);
197 list_for_each_entry(pn, &adev->physical_node_list, node)
198 if (device_supports_offline(pn->dev) && !pn->dev->offline) {
200 kobject_uevent(&pn->dev->kobj, KOBJ_CHANGE);
206 mutex_unlock(&adev->physical_node_lock);
210 static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
213 struct acpi_device *device = NULL;
214 struct acpi_device_physical_node *pn;
215 bool second_pass = (bool)data;
216 acpi_status status = AE_OK;
218 if (acpi_bus_get_device(handle, &device))
221 if (device->handler && !device->handler->hotplug.enabled) {
222 *ret_p = &device->dev;
226 mutex_lock(&device->physical_node_lock);
228 list_for_each_entry(pn, &device->physical_node_list, node) {
232 /* Skip devices offlined by the first pass. */
236 pn->put_online = false;
238 ret = device_offline(pn->dev);
239 if (acpi_force_hot_remove)
243 pn->put_online = !ret;
253 mutex_unlock(&device->physical_node_lock);
258 static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
261 struct acpi_device *device = NULL;
262 struct acpi_device_physical_node *pn;
264 if (acpi_bus_get_device(handle, &device))
267 mutex_lock(&device->physical_node_lock);
269 list_for_each_entry(pn, &device->physical_node_list, node)
270 if (pn->put_online) {
271 device_online(pn->dev);
272 pn->put_online = false;
275 mutex_unlock(&device->physical_node_lock);
280 static int acpi_scan_try_to_offline(struct acpi_device *device)
282 acpi_handle handle = device->handle;
283 struct device *errdev = NULL;
287 * Carry out two passes here and ignore errors in the first pass,
288 * because if the devices in question are memory blocks and
289 * CONFIG_MEMCG is set, one of the blocks may hold data structures
290 * that the other blocks depend on, but it is not known in advance which
293 * If the first pass is successful, the second one isn't needed, though.
295 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
296 NULL, acpi_bus_offline, (void *)false,
298 if (status == AE_SUPPORT) {
299 dev_warn(errdev, "Offline disabled.\n");
300 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
301 acpi_bus_online, NULL, NULL, NULL);
304 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
307 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
308 NULL, acpi_bus_offline, (void *)true,
310 if (!errdev || acpi_force_hot_remove)
311 acpi_bus_offline(handle, 0, (void *)true,
314 if (errdev && !acpi_force_hot_remove) {
315 dev_warn(errdev, "Offline failed.\n");
316 acpi_bus_online(handle, 0, NULL, NULL);
317 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
318 ACPI_UINT32_MAX, acpi_bus_online,
326 static int acpi_scan_hot_remove(struct acpi_device *device)
328 acpi_handle handle = device->handle;
329 unsigned long long sta;
332 if (device->handler && device->handler->hotplug.demand_offline
333 && !acpi_force_hot_remove) {
334 if (!acpi_scan_is_offline(device, true))
337 int error = acpi_scan_try_to_offline(device);
342 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
343 "Hot-removing device %s...\n", dev_name(&device->dev)));
345 acpi_bus_trim(device);
347 acpi_evaluate_lck(handle, 0);
351 status = acpi_evaluate_ej0(handle);
352 if (status == AE_NOT_FOUND)
354 else if (ACPI_FAILURE(status))
358 * Verify if eject was indeed successful. If not, log an error
359 * message. No need to call _OST since _EJ0 call was made OK.
361 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
362 if (ACPI_FAILURE(status)) {
363 acpi_handle_warn(handle,
364 "Status check after eject failed (0x%x)\n", status);
365 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
366 acpi_handle_warn(handle,
367 "Eject incomplete - status 0x%llx\n", sta);
373 static int acpi_scan_device_not_present(struct acpi_device *adev)
375 if (!acpi_device_enumerated(adev)) {
376 dev_warn(&adev->dev, "Still not present\n");
383 static int acpi_scan_device_check(struct acpi_device *adev)
387 acpi_bus_get_status(adev);
388 if (adev->status.present || adev->status.functional) {
390 * This function is only called for device objects for which
391 * matching scan handlers exist. The only situation in which
392 * the scan handler is not attached to this device object yet
393 * is when the device has just appeared (either it wasn't
394 * present at all before or it was removed and then added
398 dev_warn(&adev->dev, "Already enumerated\n");
401 error = acpi_bus_scan(adev->handle);
403 dev_warn(&adev->dev, "Namespace scan failure\n");
406 if (!adev->handler) {
407 dev_warn(&adev->dev, "Enumeration failure\n");
411 error = acpi_scan_device_not_present(adev);
416 static int acpi_scan_bus_check(struct acpi_device *adev)
418 struct acpi_scan_handler *handler = adev->handler;
419 struct acpi_device *child;
422 acpi_bus_get_status(adev);
423 if (!(adev->status.present || adev->status.functional)) {
424 acpi_scan_device_not_present(adev);
427 if (handler && handler->hotplug.scan_dependent)
428 return handler->hotplug.scan_dependent(adev);
430 error = acpi_bus_scan(adev->handle);
432 dev_warn(&adev->dev, "Namespace scan failure\n");
435 list_for_each_entry(child, &adev->children, node) {
436 error = acpi_scan_bus_check(child);
443 static void acpi_device_hotplug(void *data, u32 src)
445 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
446 struct acpi_device *adev = data;
449 lock_device_hotplug();
450 mutex_lock(&acpi_scan_lock);
453 * The device object's ACPI handle cannot become invalid as long as we
454 * are holding acpi_scan_lock, but it may have become invalid before
455 * that lock was acquired.
457 if (adev->handle == INVALID_ACPI_HANDLE)
461 case ACPI_NOTIFY_BUS_CHECK:
462 error = acpi_scan_bus_check(adev);
464 case ACPI_NOTIFY_DEVICE_CHECK:
465 error = acpi_scan_device_check(adev);
467 case ACPI_NOTIFY_EJECT_REQUEST:
468 case ACPI_OST_EC_OSPM_EJECT:
469 error = acpi_scan_hot_remove(adev);
476 ost_code = ACPI_OST_SC_SUCCESS;
479 acpi_evaluate_hotplug_ost(adev->handle, src, ost_code, NULL);
480 put_device(&adev->dev);
481 mutex_unlock(&acpi_scan_lock);
482 unlock_device_hotplug();
485 static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *data)
487 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
488 struct acpi_device *adev;
491 if (acpi_bus_get_device(handle, &adev))
495 case ACPI_NOTIFY_BUS_CHECK:
496 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
498 case ACPI_NOTIFY_DEVICE_CHECK:
499 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
501 case ACPI_NOTIFY_EJECT_REQUEST:
502 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
506 if (!adev->handler->hotplug.enabled) {
507 acpi_handle_err(handle, "Eject disabled\n");
508 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
511 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
512 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
515 /* non-hotplug event; possibly handled by other handler */
518 get_device(&adev->dev);
519 status = acpi_hotplug_execute(acpi_device_hotplug, adev, type);
520 if (ACPI_SUCCESS(status))
523 put_device(&adev->dev);
526 acpi_evaluate_hotplug_ost(handle, type, ost_code, NULL);
529 static ssize_t real_power_state_show(struct device *dev,
530 struct device_attribute *attr, char *buf)
532 struct acpi_device *adev = to_acpi_device(dev);
536 ret = acpi_device_get_power(adev, &state);
540 return sprintf(buf, "%s\n", acpi_power_state_string(state));
543 static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
545 static ssize_t power_state_show(struct device *dev,
546 struct device_attribute *attr, char *buf)
548 struct acpi_device *adev = to_acpi_device(dev);
550 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
553 static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
556 acpi_eject_store(struct device *d, struct device_attribute *attr,
557 const char *buf, size_t count)
559 struct acpi_device *acpi_device = to_acpi_device(d);
560 acpi_object_type not_used;
563 if (!count || buf[0] != '1')
566 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
567 && !acpi_device->driver)
570 status = acpi_get_type(acpi_device->handle, ¬_used);
571 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
574 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
575 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
576 get_device(&acpi_device->dev);
577 status = acpi_hotplug_execute(acpi_device_hotplug, acpi_device,
578 ACPI_OST_EC_OSPM_EJECT);
579 if (ACPI_SUCCESS(status))
582 put_device(&acpi_device->dev);
583 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
584 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
585 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
588 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
591 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
592 struct acpi_device *acpi_dev = to_acpi_device(dev);
594 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
596 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
598 static ssize_t acpi_device_uid_show(struct device *dev,
599 struct device_attribute *attr, char *buf)
601 struct acpi_device *acpi_dev = to_acpi_device(dev);
603 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
605 static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
607 static ssize_t acpi_device_adr_show(struct device *dev,
608 struct device_attribute *attr, char *buf)
610 struct acpi_device *acpi_dev = to_acpi_device(dev);
612 return sprintf(buf, "0x%08x\n",
613 (unsigned int)(acpi_dev->pnp.bus_address));
615 static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
618 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
619 struct acpi_device *acpi_dev = to_acpi_device(dev);
620 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
623 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
627 result = sprintf(buf, "%s\n", (char*)path.pointer);
632 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
634 /* sysfs file that shows description text from the ACPI _STR method */
635 static ssize_t description_show(struct device *dev,
636 struct device_attribute *attr,
638 struct acpi_device *acpi_dev = to_acpi_device(dev);
641 if (acpi_dev->pnp.str_obj == NULL)
645 * The _STR object contains a Unicode identifier for a device.
646 * We need to convert to utf-8 so it can be displayed.
648 result = utf16s_to_utf8s(
649 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
650 acpi_dev->pnp.str_obj->buffer.length,
651 UTF16_LITTLE_ENDIAN, buf,
654 buf[result++] = '\n';
658 static DEVICE_ATTR(description, 0444, description_show, NULL);
661 acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
663 struct acpi_device *acpi_dev = to_acpi_device(dev);
665 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
667 static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
669 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
671 struct acpi_device *acpi_dev = to_acpi_device(dev);
673 unsigned long long sta;
675 status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta);
676 if (ACPI_FAILURE(status))
679 return sprintf(buf, "%llu\n", sta);
681 static DEVICE_ATTR_RO(status);
683 static int acpi_device_setup_files(struct acpi_device *dev)
685 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
687 unsigned long long sun;
691 * Devices gotten from FADT don't have a "path" attribute
694 result = device_create_file(&dev->dev, &dev_attr_path);
699 if (!list_empty(&dev->pnp.ids)) {
700 result = device_create_file(&dev->dev, &dev_attr_hid);
704 result = device_create_file(&dev->dev, &dev_attr_modalias);
710 * If device has _STR, 'description' file is created
712 if (acpi_has_method(dev->handle, "_STR")) {
713 status = acpi_evaluate_object(dev->handle, "_STR",
715 if (ACPI_FAILURE(status))
716 buffer.pointer = NULL;
717 dev->pnp.str_obj = buffer.pointer;
718 result = device_create_file(&dev->dev, &dev_attr_description);
723 if (dev->pnp.type.bus_address)
724 result = device_create_file(&dev->dev, &dev_attr_adr);
725 if (dev->pnp.unique_id)
726 result = device_create_file(&dev->dev, &dev_attr_uid);
728 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
729 if (ACPI_SUCCESS(status)) {
730 dev->pnp.sun = (unsigned long)sun;
731 result = device_create_file(&dev->dev, &dev_attr_sun);
735 dev->pnp.sun = (unsigned long)-1;
738 if (acpi_has_method(dev->handle, "_STA")) {
739 result = device_create_file(&dev->dev, &dev_attr_status);
745 * If device has _EJ0, 'eject' file is created that is used to trigger
746 * hot-removal function from userland.
748 if (acpi_has_method(dev->handle, "_EJ0")) {
749 result = device_create_file(&dev->dev, &dev_attr_eject);
754 if (dev->flags.power_manageable) {
755 result = device_create_file(&dev->dev, &dev_attr_power_state);
759 if (dev->power.flags.power_resources)
760 result = device_create_file(&dev->dev,
761 &dev_attr_real_power_state);
768 static void acpi_device_remove_files(struct acpi_device *dev)
770 if (dev->flags.power_manageable) {
771 device_remove_file(&dev->dev, &dev_attr_power_state);
772 if (dev->power.flags.power_resources)
773 device_remove_file(&dev->dev,
774 &dev_attr_real_power_state);
778 * If device has _STR, remove 'description' file
780 if (acpi_has_method(dev->handle, "_STR")) {
781 kfree(dev->pnp.str_obj);
782 device_remove_file(&dev->dev, &dev_attr_description);
785 * If device has _EJ0, remove 'eject' file.
787 if (acpi_has_method(dev->handle, "_EJ0"))
788 device_remove_file(&dev->dev, &dev_attr_eject);
790 if (acpi_has_method(dev->handle, "_SUN"))
791 device_remove_file(&dev->dev, &dev_attr_sun);
793 if (dev->pnp.unique_id)
794 device_remove_file(&dev->dev, &dev_attr_uid);
795 if (dev->pnp.type.bus_address)
796 device_remove_file(&dev->dev, &dev_attr_adr);
797 device_remove_file(&dev->dev, &dev_attr_modalias);
798 device_remove_file(&dev->dev, &dev_attr_hid);
799 if (acpi_has_method(dev->handle, "_STA"))
800 device_remove_file(&dev->dev, &dev_attr_status);
802 device_remove_file(&dev->dev, &dev_attr_path);
804 /* --------------------------------------------------------------------------
806 -------------------------------------------------------------------------- */
808 static const struct acpi_device_id *__acpi_match_device(
809 struct acpi_device *device, const struct acpi_device_id *ids)
811 const struct acpi_device_id *id;
812 struct acpi_hardware_id *hwid;
815 * If the device is not present, it is unnecessary to load device
818 if (!device->status.present)
821 for (id = ids; id->id[0]; id++)
822 list_for_each_entry(hwid, &device->pnp.ids, list)
823 if (!strcmp((char *) id->id, hwid->id))
830 * acpi_match_device - Match a struct device against a given list of ACPI IDs
831 * @ids: Array of struct acpi_device_id object to match against.
832 * @dev: The device structure to match.
834 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
835 * object for that handle and use that object to match against a given list of
838 * Return a pointer to the first matching ID on success or %NULL on failure.
840 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
841 const struct device *dev)
843 struct acpi_device *adev;
844 acpi_handle handle = ACPI_HANDLE(dev);
846 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
849 return __acpi_match_device(adev, ids);
851 EXPORT_SYMBOL_GPL(acpi_match_device);
853 int acpi_match_device_ids(struct acpi_device *device,
854 const struct acpi_device_id *ids)
856 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
858 EXPORT_SYMBOL(acpi_match_device_ids);
860 static void acpi_free_power_resources_lists(struct acpi_device *device)
864 if (device->wakeup.flags.valid)
865 acpi_power_resources_list_free(&device->wakeup.resources);
867 if (!device->flags.power_manageable)
870 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
871 struct acpi_device_power_state *ps = &device->power.states[i];
872 acpi_power_resources_list_free(&ps->resources);
876 static void acpi_device_release(struct device *dev)
878 struct acpi_device *acpi_dev = to_acpi_device(dev);
880 acpi_free_pnp_ids(&acpi_dev->pnp);
881 acpi_free_power_resources_lists(acpi_dev);
885 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
887 struct acpi_device *acpi_dev = to_acpi_device(dev);
888 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
890 return acpi_dev->flags.match_driver
891 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
894 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
896 struct acpi_device *acpi_dev = to_acpi_device(dev);
899 if (list_empty(&acpi_dev->pnp.ids))
902 if (add_uevent_var(env, "MODALIAS="))
904 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
905 sizeof(env->buf) - env->buflen);
912 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
914 struct acpi_device *device = data;
916 device->driver->ops.notify(device, event);
919 static acpi_status acpi_device_notify_fixed(void *data)
921 struct acpi_device *device = data;
923 /* Fixed hardware devices have no handles */
924 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
928 static int acpi_device_install_notify_handler(struct acpi_device *device)
932 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
934 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
935 acpi_device_notify_fixed,
937 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
939 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
940 acpi_device_notify_fixed,
943 status = acpi_install_notify_handler(device->handle,
948 if (ACPI_FAILURE(status))
953 static void acpi_device_remove_notify_handler(struct acpi_device *device)
955 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
956 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
957 acpi_device_notify_fixed);
958 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
959 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
960 acpi_device_notify_fixed);
962 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
966 static int acpi_device_probe(struct device *dev)
968 struct acpi_device *acpi_dev = to_acpi_device(dev);
969 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
972 if (acpi_dev->handler)
975 if (!acpi_drv->ops.add)
978 ret = acpi_drv->ops.add(acpi_dev);
982 acpi_dev->driver = acpi_drv;
983 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
984 "Driver [%s] successfully bound to device [%s]\n",
985 acpi_drv->name, acpi_dev->pnp.bus_id));
987 if (acpi_drv->ops.notify) {
988 ret = acpi_device_install_notify_handler(acpi_dev);
990 if (acpi_drv->ops.remove)
991 acpi_drv->ops.remove(acpi_dev);
993 acpi_dev->driver = NULL;
994 acpi_dev->driver_data = NULL;
999 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
1000 acpi_drv->name, acpi_dev->pnp.bus_id));
1005 static int acpi_device_remove(struct device * dev)
1007 struct acpi_device *acpi_dev = to_acpi_device(dev);
1008 struct acpi_driver *acpi_drv = acpi_dev->driver;
1011 if (acpi_drv->ops.notify)
1012 acpi_device_remove_notify_handler(acpi_dev);
1013 if (acpi_drv->ops.remove)
1014 acpi_drv->ops.remove(acpi_dev);
1016 acpi_dev->driver = NULL;
1017 acpi_dev->driver_data = NULL;
1023 struct bus_type acpi_bus_type = {
1025 .match = acpi_bus_match,
1026 .probe = acpi_device_probe,
1027 .remove = acpi_device_remove,
1028 .uevent = acpi_device_uevent,
1031 static void acpi_device_del(struct acpi_device *device)
1033 mutex_lock(&acpi_device_lock);
1035 list_del(&device->node);
1037 list_del(&device->wakeup_list);
1038 mutex_unlock(&acpi_device_lock);
1040 acpi_power_add_remove_device(device, false);
1041 acpi_device_remove_files(device);
1043 device->remove(device);
1045 device_del(&device->dev);
1048 static LIST_HEAD(acpi_device_del_list);
1049 static DEFINE_MUTEX(acpi_device_del_lock);
1051 static void acpi_device_del_work_fn(struct work_struct *work_not_used)
1054 struct acpi_device *adev;
1056 mutex_lock(&acpi_device_del_lock);
1058 if (list_empty(&acpi_device_del_list)) {
1059 mutex_unlock(&acpi_device_del_lock);
1062 adev = list_first_entry(&acpi_device_del_list,
1063 struct acpi_device, del_list);
1064 list_del(&adev->del_list);
1066 mutex_unlock(&acpi_device_del_lock);
1068 acpi_device_del(adev);
1070 * Drop references to all power resources that might have been
1071 * used by the device.
1073 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
1074 put_device(&adev->dev);
1079 * acpi_scan_drop_device - Drop an ACPI device object.
1080 * @handle: Handle of an ACPI namespace node, not used.
1081 * @context: Address of the ACPI device object to drop.
1083 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
1084 * namespace node the device object pointed to by @context is attached to.
1086 * The unregistration is carried out asynchronously to avoid running
1087 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
1088 * ensure the correct ordering (the device objects must be unregistered in the
1089 * same order in which the corresponding namespace nodes are deleted).
1091 static void acpi_scan_drop_device(acpi_handle handle, void *context)
1093 static DECLARE_WORK(work, acpi_device_del_work_fn);
1094 struct acpi_device *adev = context;
1096 mutex_lock(&acpi_device_del_lock);
1099 * Use the ACPI hotplug workqueue which is ordered, so this work item
1100 * won't run after any hotplug work items submitted subsequently. That
1101 * prevents attempts to register device objects identical to those being
1102 * deleted from happening concurrently (such attempts result from
1103 * hotplug events handled via the ACPI hotplug workqueue). It also will
1104 * run after all of the work items submitted previosuly, which helps
1105 * those work items to ensure that they are not accessing stale device
1108 if (list_empty(&acpi_device_del_list))
1109 acpi_queue_hotplug_work(&work);
1111 list_add_tail(&adev->del_list, &acpi_device_del_list);
1112 /* Make acpi_ns_validate_handle() return NULL for this handle. */
1113 adev->handle = INVALID_ACPI_HANDLE;
1115 mutex_unlock(&acpi_device_del_lock);
1118 int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
1125 status = acpi_get_data(handle, acpi_scan_drop_device, (void **)device);
1126 if (ACPI_FAILURE(status) || !*device) {
1127 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
1133 EXPORT_SYMBOL(acpi_bus_get_device);
1135 int acpi_device_add(struct acpi_device *device,
1136 void (*release)(struct device *))
1139 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
1142 if (device->handle) {
1145 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
1147 if (ACPI_FAILURE(status)) {
1148 acpi_handle_err(device->handle,
1149 "Unable to attach device data\n");
1157 * Link this device to its parent and siblings.
1159 INIT_LIST_HEAD(&device->children);
1160 INIT_LIST_HEAD(&device->node);
1161 INIT_LIST_HEAD(&device->wakeup_list);
1162 INIT_LIST_HEAD(&device->physical_node_list);
1163 INIT_LIST_HEAD(&device->del_list);
1164 mutex_init(&device->physical_node_lock);
1166 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1168 pr_err(PREFIX "Memory allocation error\n");
1173 mutex_lock(&acpi_device_lock);
1175 * Find suitable bus_id and instance number in acpi_bus_id_list
1176 * If failed, create one and link it into acpi_bus_id_list
1178 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
1179 if (!strcmp(acpi_device_bus_id->bus_id,
1180 acpi_device_hid(device))) {
1181 acpi_device_bus_id->instance_no++;
1188 acpi_device_bus_id = new_bus_id;
1189 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
1190 acpi_device_bus_id->instance_no = 0;
1191 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1193 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
1196 list_add_tail(&device->node, &device->parent->children);
1198 if (device->wakeup.flags.valid)
1199 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
1200 mutex_unlock(&acpi_device_lock);
1203 device->dev.parent = &device->parent->dev;
1204 device->dev.bus = &acpi_bus_type;
1205 device->dev.release = release;
1206 result = device_add(&device->dev);
1208 dev_err(&device->dev, "Error registering device\n");
1212 result = acpi_device_setup_files(device);
1214 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1215 dev_name(&device->dev));
1220 mutex_lock(&acpi_device_lock);
1222 list_del(&device->node);
1223 list_del(&device->wakeup_list);
1224 mutex_unlock(&acpi_device_lock);
1227 acpi_detach_data(device->handle, acpi_scan_drop_device);
1231 /* --------------------------------------------------------------------------
1233 -------------------------------------------------------------------------- */
1235 * acpi_bus_register_driver - register a driver with the ACPI bus
1236 * @driver: driver being registered
1238 * Registers a driver with the ACPI bus. Searches the namespace for all
1239 * devices that match the driver's criteria and binds. Returns zero for
1240 * success or a negative error status for failure.
1242 int acpi_bus_register_driver(struct acpi_driver *driver)
1248 driver->drv.name = driver->name;
1249 driver->drv.bus = &acpi_bus_type;
1250 driver->drv.owner = driver->owner;
1252 ret = driver_register(&driver->drv);
1256 EXPORT_SYMBOL(acpi_bus_register_driver);
1259 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
1260 * @driver: driver to unregister
1262 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1263 * devices that match the driver's criteria and unbinds.
1265 void acpi_bus_unregister_driver(struct acpi_driver *driver)
1267 driver_unregister(&driver->drv);
1270 EXPORT_SYMBOL(acpi_bus_unregister_driver);
1272 /* --------------------------------------------------------------------------
1274 -------------------------------------------------------------------------- */
1275 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1277 struct acpi_device *device = NULL;
1281 * Fixed hardware devices do not appear in the namespace and do not
1282 * have handles, but we fabricate acpi_devices for them, so we have
1283 * to deal with them specially.
1289 status = acpi_get_parent(handle, &handle);
1290 if (ACPI_FAILURE(status))
1291 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1292 } while (acpi_bus_get_device(handle, &device));
1297 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1301 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1302 union acpi_object *obj;
1304 status = acpi_get_handle(handle, "_EJD", &tmp);
1305 if (ACPI_FAILURE(status))
1308 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1309 if (ACPI_SUCCESS(status)) {
1310 obj = buffer.pointer;
1311 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1313 kfree(buffer.pointer);
1317 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1319 static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1320 struct acpi_device_wakeup *wakeup)
1322 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1323 union acpi_object *package = NULL;
1324 union acpi_object *element = NULL;
1331 INIT_LIST_HEAD(&wakeup->resources);
1334 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1335 if (ACPI_FAILURE(status)) {
1336 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
1340 package = (union acpi_object *)buffer.pointer;
1342 if (!package || package->package.count < 2)
1345 element = &(package->package.elements[0]);
1349 if (element->type == ACPI_TYPE_PACKAGE) {
1350 if ((element->package.count < 2) ||
1351 (element->package.elements[0].type !=
1352 ACPI_TYPE_LOCAL_REFERENCE)
1353 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
1356 wakeup->gpe_device =
1357 element->package.elements[0].reference.handle;
1358 wakeup->gpe_number =
1359 (u32) element->package.elements[1].integer.value;
1360 } else if (element->type == ACPI_TYPE_INTEGER) {
1361 wakeup->gpe_device = NULL;
1362 wakeup->gpe_number = element->integer.value;
1367 element = &(package->package.elements[1]);
1368 if (element->type != ACPI_TYPE_INTEGER)
1371 wakeup->sleep_state = element->integer.value;
1373 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1377 if (!list_empty(&wakeup->resources)) {
1380 err = acpi_power_wakeup_list_init(&wakeup->resources,
1383 acpi_handle_warn(handle, "Retrieving current states "
1384 "of wakeup power resources failed\n");
1385 acpi_power_resources_list_free(&wakeup->resources);
1388 if (sleep_state < wakeup->sleep_state) {
1389 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1390 "(S%d) by S%d from power resources\n",
1391 (int)wakeup->sleep_state, sleep_state);
1392 wakeup->sleep_state = sleep_state;
1395 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
1398 kfree(buffer.pointer);
1402 static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
1404 struct acpi_device_id button_device_ids[] = {
1411 acpi_event_status event_status;
1413 device->wakeup.flags.notifier_present = 0;
1415 /* Power button, Lid switch always enable wakeup */
1416 if (!acpi_match_device_ids(device, button_device_ids)) {
1417 device->wakeup.flags.run_wake = 1;
1418 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1419 /* Do not use Lid/sleep button for S5 wakeup */
1420 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1421 device->wakeup.sleep_state = ACPI_STATE_S4;
1423 device_set_wakeup_capable(&device->dev, true);
1427 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1428 device->wakeup.gpe_number,
1430 if (status == AE_OK)
1431 device->wakeup.flags.run_wake =
1432 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1435 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1439 /* Presence of _PRW indicates wake capable */
1440 if (!acpi_has_method(device->handle, "_PRW"))
1443 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1446 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
1450 device->wakeup.flags.valid = 1;
1451 device->wakeup.prepare_count = 0;
1452 acpi_bus_set_run_wake_flags(device);
1453 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1454 * system for the ACPI device with the _PRW object.
1455 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1456 * So it is necessary to call _DSW object first. Only when it is not
1457 * present will the _PSW object used.
1459 err = acpi_device_sleep_wake(device, 0, 0, 0);
1461 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1462 "error in _DSW or _PSW evaluation\n"));
1465 static void acpi_bus_init_power_state(struct acpi_device *device, int state)
1467 struct acpi_device_power_state *ps = &device->power.states[state];
1468 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1469 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1472 INIT_LIST_HEAD(&ps->resources);
1474 /* Evaluate "_PRx" to get referenced power resources */
1475 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1476 if (ACPI_SUCCESS(status)) {
1477 union acpi_object *package = buffer.pointer;
1479 if (buffer.length && package
1480 && package->type == ACPI_TYPE_PACKAGE
1481 && package->package.count) {
1482 int err = acpi_extract_power_resources(package, 0,
1485 device->power.flags.power_resources = 1;
1487 ACPI_FREE(buffer.pointer);
1490 /* Evaluate "_PSx" to see if we can do explicit sets */
1492 if (acpi_has_method(device->handle, pathname))
1493 ps->flags.explicit_set = 1;
1496 * State is valid if there are means to put the device into it.
1497 * D3hot is only valid if _PR3 present.
1499 if (!list_empty(&ps->resources)
1500 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1501 ps->flags.valid = 1;
1502 ps->flags.os_accessible = 1;
1505 ps->power = -1; /* Unknown - driver assigned */
1506 ps->latency = -1; /* Unknown - driver assigned */
1509 static void acpi_bus_get_power_flags(struct acpi_device *device)
1513 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1514 if (!acpi_has_method(device->handle, "_PS0") &&
1515 !acpi_has_method(device->handle, "_PR0"))
1518 device->flags.power_manageable = 1;
1521 * Power Management Flags
1523 if (acpi_has_method(device->handle, "_PSC"))
1524 device->power.flags.explicit_get = 1;
1525 if (acpi_has_method(device->handle, "_IRC"))
1526 device->power.flags.inrush_current = 1;
1529 * Enumerate supported power management states
1531 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1532 acpi_bus_init_power_state(device, i);
1534 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1536 /* Set defaults for D0 and D3 states (always valid) */
1537 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1538 device->power.states[ACPI_STATE_D0].power = 100;
1539 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1540 device->power.states[ACPI_STATE_D3_COLD].power = 0;
1542 /* Set D3cold's explicit_set flag if _PS3 exists. */
1543 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1544 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1546 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1547 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1548 device->power.flags.power_resources)
1549 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1551 if (acpi_bus_init_power(device)) {
1552 acpi_free_power_resources_lists(device);
1553 device->flags.power_manageable = 0;
1557 static void acpi_bus_get_flags(struct acpi_device *device)
1559 /* Presence of _STA indicates 'dynamic_status' */
1560 if (acpi_has_method(device->handle, "_STA"))
1561 device->flags.dynamic_status = 1;
1563 /* Presence of _RMV indicates 'removable' */
1564 if (acpi_has_method(device->handle, "_RMV"))
1565 device->flags.removable = 1;
1567 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1568 if (acpi_has_method(device->handle, "_EJD") ||
1569 acpi_has_method(device->handle, "_EJ0"))
1570 device->flags.ejectable = 1;
1573 static void acpi_device_get_busid(struct acpi_device *device)
1575 char bus_id[5] = { '?', 0 };
1576 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1582 * The device's Bus ID is simply the object name.
1583 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1585 if (ACPI_IS_ROOT_DEVICE(device)) {
1586 strcpy(device->pnp.bus_id, "ACPI");
1590 switch (device->device_type) {
1591 case ACPI_BUS_TYPE_POWER_BUTTON:
1592 strcpy(device->pnp.bus_id, "PWRF");
1594 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1595 strcpy(device->pnp.bus_id, "SLPF");
1598 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1599 /* Clean up trailing underscores (if any) */
1600 for (i = 3; i > 1; i--) {
1601 if (bus_id[i] == '_')
1606 strcpy(device->pnp.bus_id, bus_id);
1612 * acpi_ata_match - see if an acpi object is an ATA device
1614 * If an acpi object has one of the ACPI ATA methods defined,
1615 * then we can safely call it an ATA device.
1617 bool acpi_ata_match(acpi_handle handle)
1619 return acpi_has_method(handle, "_GTF") ||
1620 acpi_has_method(handle, "_GTM") ||
1621 acpi_has_method(handle, "_STM") ||
1622 acpi_has_method(handle, "_SDD");
1626 * acpi_bay_match - see if an acpi object is an ejectable driver bay
1628 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1629 * then we can safely call it an ejectable drive bay
1631 bool acpi_bay_match(acpi_handle handle)
1633 acpi_handle phandle;
1635 if (!acpi_has_method(handle, "_EJ0"))
1637 if (acpi_ata_match(handle))
1639 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1642 return acpi_ata_match(phandle);
1646 * acpi_dock_match - see if an acpi object has a _DCK method
1648 bool acpi_dock_match(acpi_handle handle)
1650 return acpi_has_method(handle, "_DCK");
1653 const char *acpi_device_hid(struct acpi_device *device)
1655 struct acpi_hardware_id *hid;
1657 if (list_empty(&device->pnp.ids))
1660 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1663 EXPORT_SYMBOL(acpi_device_hid);
1665 static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
1667 struct acpi_hardware_id *id;
1669 id = kmalloc(sizeof(*id), GFP_KERNEL);
1673 id->id = kstrdup(dev_id, GFP_KERNEL);
1679 list_add_tail(&id->list, &pnp->ids);
1680 pnp->type.hardware_id = 1;
1684 * Old IBM workstations have a DSDT bug wherein the SMBus object
1685 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1686 * prefix. Work around this.
1688 static bool acpi_ibm_smbus_match(acpi_handle handle)
1690 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1691 struct acpi_buffer path = { sizeof(node_name), node_name };
1693 if (!dmi_name_in_vendors("IBM"))
1696 /* Look for SMBS object */
1697 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1698 strcmp("SMBS", path.pointer))
1701 /* Does it have the necessary (but misnamed) methods? */
1702 if (acpi_has_method(handle, "SBI") &&
1703 acpi_has_method(handle, "SBR") &&
1704 acpi_has_method(handle, "SBW"))
1710 static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1714 struct acpi_device_info *info;
1715 struct acpi_pnp_device_id_list *cid_list;
1718 switch (device_type) {
1719 case ACPI_BUS_TYPE_DEVICE:
1720 if (handle == ACPI_ROOT_OBJECT) {
1721 acpi_add_id(pnp, ACPI_SYSTEM_HID);
1725 status = acpi_get_object_info(handle, &info);
1726 if (ACPI_FAILURE(status)) {
1727 pr_err(PREFIX "%s: Error reading device info\n",
1732 if (info->valid & ACPI_VALID_HID)
1733 acpi_add_id(pnp, info->hardware_id.string);
1734 if (info->valid & ACPI_VALID_CID) {
1735 cid_list = &info->compatible_id_list;
1736 for (i = 0; i < cid_list->count; i++)
1737 acpi_add_id(pnp, cid_list->ids[i].string);
1739 if (info->valid & ACPI_VALID_ADR) {
1740 pnp->bus_address = info->address;
1741 pnp->type.bus_address = 1;
1743 if (info->valid & ACPI_VALID_UID)
1744 pnp->unique_id = kstrdup(info->unique_id.string,
1750 * Some devices don't reliably have _HIDs & _CIDs, so add
1751 * synthetic HIDs to make sure drivers can find them.
1753 if (acpi_is_video_device(handle))
1754 acpi_add_id(pnp, ACPI_VIDEO_HID);
1755 else if (acpi_bay_match(handle))
1756 acpi_add_id(pnp, ACPI_BAY_HID);
1757 else if (acpi_dock_match(handle))
1758 acpi_add_id(pnp, ACPI_DOCK_HID);
1759 else if (acpi_ibm_smbus_match(handle))
1760 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1761 else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1762 acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1763 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1764 strcpy(pnp->device_class, ACPI_BUS_CLASS);
1768 case ACPI_BUS_TYPE_POWER:
1769 acpi_add_id(pnp, ACPI_POWER_HID);
1771 case ACPI_BUS_TYPE_PROCESSOR:
1772 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
1774 case ACPI_BUS_TYPE_THERMAL:
1775 acpi_add_id(pnp, ACPI_THERMAL_HID);
1777 case ACPI_BUS_TYPE_POWER_BUTTON:
1778 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
1780 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1781 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
1786 void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1788 struct acpi_hardware_id *id, *tmp;
1790 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1794 kfree(pnp->unique_id);
1797 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1798 int type, unsigned long long sta)
1800 INIT_LIST_HEAD(&device->pnp.ids);
1801 device->device_type = type;
1802 device->handle = handle;
1803 device->parent = acpi_bus_get_parent(handle);
1804 acpi_set_device_status(device, sta);
1805 acpi_device_get_busid(device);
1806 acpi_set_pnp_ids(handle, &device->pnp, type);
1807 acpi_bus_get_flags(device);
1808 device->flags.match_driver = false;
1809 device->flags.initialized = true;
1810 device->flags.visited = false;
1811 device_initialize(&device->dev);
1812 dev_set_uevent_suppress(&device->dev, true);
1815 void acpi_device_add_finalize(struct acpi_device *device)
1817 dev_set_uevent_suppress(&device->dev, false);
1818 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1821 static int acpi_add_single_object(struct acpi_device **child,
1822 acpi_handle handle, int type,
1823 unsigned long long sta)
1826 struct acpi_device *device;
1827 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1829 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1831 printk(KERN_ERR PREFIX "Memory allocation error\n");
1835 acpi_init_device_object(device, handle, type, sta);
1836 acpi_bus_get_power_flags(device);
1837 acpi_bus_get_wakeup_device_flags(device);
1839 result = acpi_device_add(device, acpi_device_release);
1841 acpi_device_release(&device->dev);
1845 acpi_power_add_remove_device(device, true);
1846 acpi_device_add_finalize(device);
1847 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1848 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1849 dev_name(&device->dev), (char *) buffer.pointer,
1850 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1851 kfree(buffer.pointer);
1856 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1857 unsigned long long *sta)
1860 acpi_object_type acpi_type;
1862 status = acpi_get_type(handle, &acpi_type);
1863 if (ACPI_FAILURE(status))
1866 switch (acpi_type) {
1867 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1868 case ACPI_TYPE_DEVICE:
1869 *type = ACPI_BUS_TYPE_DEVICE;
1870 status = acpi_bus_get_status_handle(handle, sta);
1871 if (ACPI_FAILURE(status))
1874 case ACPI_TYPE_PROCESSOR:
1875 *type = ACPI_BUS_TYPE_PROCESSOR;
1876 status = acpi_bus_get_status_handle(handle, sta);
1877 if (ACPI_FAILURE(status))
1880 case ACPI_TYPE_THERMAL:
1881 *type = ACPI_BUS_TYPE_THERMAL;
1882 *sta = ACPI_STA_DEFAULT;
1884 case ACPI_TYPE_POWER:
1885 *type = ACPI_BUS_TYPE_POWER;
1886 *sta = ACPI_STA_DEFAULT;
1895 bool acpi_device_is_present(struct acpi_device *adev)
1897 if (adev->status.present || adev->status.functional)
1900 adev->flags.initialized = false;
1904 static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1906 const struct acpi_device_id **matchid)
1908 const struct acpi_device_id *devid;
1910 for (devid = handler->ids; devid->id[0]; devid++)
1911 if (!strcmp((char *)devid->id, idstr)) {
1921 static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1922 const struct acpi_device_id **matchid)
1924 struct acpi_scan_handler *handler;
1926 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1927 if (acpi_scan_handler_matching(handler, idstr, matchid))
1933 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1935 if (!!hotplug->enabled == !!val)
1938 mutex_lock(&acpi_scan_lock);
1940 hotplug->enabled = val;
1942 mutex_unlock(&acpi_scan_lock);
1945 static void acpi_scan_init_hotplug(acpi_handle handle, int type)
1947 struct acpi_device_pnp pnp = {};
1948 struct acpi_hardware_id *hwid;
1949 struct acpi_scan_handler *handler;
1951 INIT_LIST_HEAD(&pnp.ids);
1952 acpi_set_pnp_ids(handle, &pnp, type);
1954 if (!pnp.type.hardware_id)
1958 * This relies on the fact that acpi_install_notify_handler() will not
1959 * install the same notify handler routine twice for the same handle.
1961 list_for_each_entry(hwid, &pnp.ids, list) {
1962 handler = acpi_scan_match_handler(hwid->id, NULL);
1964 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1965 acpi_hotplug_notify_cb, handler);
1971 acpi_free_pnp_ids(&pnp);
1974 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1975 void *not_used, void **return_value)
1977 struct acpi_device *device = NULL;
1979 unsigned long long sta;
1982 acpi_bus_get_device(handle, &device);
1986 result = acpi_bus_type_and_status(handle, &type, &sta);
1990 if (type == ACPI_BUS_TYPE_POWER) {
1991 acpi_add_power_resource(handle);
1995 acpi_scan_init_hotplug(handle, type);
1997 acpi_add_single_object(&device, handle, type, sta);
1999 return AE_CTRL_DEPTH;
2003 *return_value = device;
2008 static int acpi_scan_attach_handler(struct acpi_device *device)
2010 struct acpi_hardware_id *hwid;
2013 list_for_each_entry(hwid, &device->pnp.ids, list) {
2014 const struct acpi_device_id *devid;
2015 struct acpi_scan_handler *handler;
2017 handler = acpi_scan_match_handler(hwid->id, &devid);
2019 ret = handler->attach(device, devid);
2021 device->handler = handler;
2023 } else if (ret < 0) {
2031 static void acpi_bus_attach(struct acpi_device *device)
2033 struct acpi_device *child;
2036 acpi_bus_get_status(device);
2037 /* Skip devices that are not present. */
2038 if (!acpi_device_is_present(device)) {
2039 device->flags.visited = false;
2042 if (device->handler)
2045 if (!device->flags.initialized) {
2046 acpi_bus_update_power(device, NULL);
2047 device->flags.initialized = true;
2049 device->flags.visited = false;
2050 ret = acpi_scan_attach_handler(device);
2054 device->flags.match_driver = true;
2056 ret = device_attach(&device->dev);
2060 device->flags.visited = true;
2063 list_for_each_entry(child, &device->children, node)
2064 acpi_bus_attach(child);
2068 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2069 * @handle: Root of the namespace scope to scan.
2071 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2074 * If no devices were found, -ENODEV is returned, but it does not mean that
2075 * there has been a real error. There just have been no suitable ACPI objects
2076 * in the table trunk from which the kernel could create a device and add an
2077 * appropriate driver.
2079 * Must be called under acpi_scan_lock.
2081 int acpi_bus_scan(acpi_handle handle)
2083 void *device = NULL;
2085 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
2086 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2087 acpi_bus_check_add, NULL, NULL, &device);
2090 acpi_bus_attach(device);
2095 EXPORT_SYMBOL(acpi_bus_scan);
2098 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2099 * @adev: Root of the ACPI namespace scope to walk.
2101 * Must be called under acpi_scan_lock.
2103 void acpi_bus_trim(struct acpi_device *adev)
2105 struct acpi_scan_handler *handler = adev->handler;
2106 struct acpi_device *child;
2108 list_for_each_entry_reverse(child, &adev->children, node)
2109 acpi_bus_trim(child);
2111 adev->flags.match_driver = false;
2113 if (handler->detach)
2114 handler->detach(adev);
2116 adev->handler = NULL;
2118 device_release_driver(&adev->dev);
2121 * Most likely, the device is going away, so put it into D3cold before
2124 acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2125 adev->flags.initialized = false;
2126 adev->flags.visited = false;
2128 EXPORT_SYMBOL_GPL(acpi_bus_trim);
2130 static int acpi_bus_scan_fixed(void)
2135 * Enumerate all fixed-feature devices.
2137 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2138 struct acpi_device *device = NULL;
2140 result = acpi_add_single_object(&device, NULL,
2141 ACPI_BUS_TYPE_POWER_BUTTON,
2146 device->flags.match_driver = true;
2147 result = device_attach(&device->dev);
2151 device_init_wakeup(&device->dev, true);
2154 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2155 struct acpi_device *device = NULL;
2157 result = acpi_add_single_object(&device, NULL,
2158 ACPI_BUS_TYPE_SLEEP_BUTTON,
2163 device->flags.match_driver = true;
2164 result = device_attach(&device->dev);
2167 return result < 0 ? result : 0;
2170 int __init acpi_scan_init(void)
2174 result = bus_register(&acpi_bus_type);
2176 /* We don't want to quit even if we failed to add suspend/resume */
2177 printk(KERN_ERR PREFIX "Could not register bus type\n");
2180 acpi_pci_root_init();
2181 acpi_pci_link_init();
2182 acpi_processor_init();
2183 acpi_platform_init();
2185 acpi_cmos_rtc_init();
2186 acpi_container_init();
2187 acpi_memory_hotplug_init();
2190 mutex_lock(&acpi_scan_lock);
2192 * Enumerate devices in the ACPI namespace.
2194 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
2198 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
2202 result = acpi_bus_scan_fixed();
2204 acpi_detach_data(acpi_root->handle, acpi_scan_drop_device);
2205 acpi_device_del(acpi_root);
2206 put_device(&acpi_root->dev);
2210 acpi_update_all_gpes();
2213 mutex_unlock(&acpi_scan_lock);