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_dep_list);
40 static DEFINE_MUTEX(acpi_dep_list_lock);
41 static LIST_HEAD(acpi_bus_id_list);
42 static DEFINE_MUTEX(acpi_scan_lock);
43 static LIST_HEAD(acpi_scan_handlers_list);
44 DEFINE_MUTEX(acpi_device_lock);
45 LIST_HEAD(acpi_wakeup_device_list);
46 static DEFINE_MUTEX(acpi_hp_context_lock);
48 struct acpi_dep_data {
49 struct list_head node;
54 struct acpi_device_bus_id{
56 unsigned int instance_no;
57 struct list_head node;
60 void acpi_scan_lock_acquire(void)
62 mutex_lock(&acpi_scan_lock);
64 EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
66 void acpi_scan_lock_release(void)
68 mutex_unlock(&acpi_scan_lock);
70 EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
72 void acpi_lock_hp_context(void)
74 mutex_lock(&acpi_hp_context_lock);
77 void acpi_unlock_hp_context(void)
79 mutex_unlock(&acpi_hp_context_lock);
82 void acpi_initialize_hp_context(struct acpi_device *adev,
83 struct acpi_hotplug_context *hp,
84 int (*notify)(struct acpi_device *, u32),
85 void (*uevent)(struct acpi_device *, u32))
87 acpi_lock_hp_context();
90 acpi_set_hp_context(adev, hp);
91 acpi_unlock_hp_context();
93 EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
95 int acpi_scan_add_handler(struct acpi_scan_handler *handler)
100 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
104 int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
105 const char *hotplug_profile_name)
109 error = acpi_scan_add_handler(handler);
113 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
118 * Creates hid/cid(s) string needed for modalias and uevent
119 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
120 * char *modalias: "acpi:IBM0001:ACPI0001"
121 * Return: 0: no _HID and no _CID
122 * -EINVAL: output error
123 * -ENOMEM: output is truncated
125 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
130 struct acpi_hardware_id *id;
132 if (list_empty(&acpi_dev->pnp.ids))
136 * If the device has PRP0001 we expose DT compatible modalias
137 * instead in form of of:NnameTCcompatible.
139 if (acpi_dev->data.of_compatible) {
140 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
141 const union acpi_object *of_compatible, *obj;
145 acpi_get_name(acpi_dev->handle, ACPI_SINGLE_NAME, &buf);
146 /* DT strings are all in lower case */
147 for (c = buf.pointer; *c != '\0'; c++)
150 len = snprintf(modalias, size, "of:N%sT", (char *)buf.pointer);
151 ACPI_FREE(buf.pointer);
153 of_compatible = acpi_dev->data.of_compatible;
154 if (of_compatible->type == ACPI_TYPE_PACKAGE) {
155 nval = of_compatible->package.count;
156 obj = of_compatible->package.elements;
157 } else { /* Must be ACPI_TYPE_STRING. */
161 for (i = 0; i < nval; i++, obj++) {
162 count = snprintf(&modalias[len], size, "C%s",
163 obj->string.pointer);
173 len = snprintf(modalias, size, "acpi:");
176 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
177 count = snprintf(&modalias[len], size, "%s:", id->id);
187 modalias[len] = '\0';
192 * acpi_companion_match() - Can we match via ACPI companion device
193 * @dev: Device in question
195 * Check if the given device has an ACPI companion and if that companion has
196 * a valid list of PNP IDs, and if the device is the first (primary) physical
197 * device associated with it.
199 * If multiple physical devices are attached to a single ACPI companion, we need
200 * to be careful. The usage scenario for this kind of relationship is that all
201 * of the physical devices in question use resources provided by the ACPI
202 * companion. A typical case is an MFD device where all the sub-devices share
203 * the parent's ACPI companion. In such cases we can only allow the primary
204 * (first) physical device to be matched with the help of the companion's PNP
207 * Additional physical devices sharing the ACPI companion can still use
208 * resources available from it but they will be matched normally using functions
209 * provided by their bus types (and analogously for their modalias).
211 static bool acpi_companion_match(const struct device *dev)
213 struct acpi_device *adev;
216 adev = ACPI_COMPANION(dev);
220 if (list_empty(&adev->pnp.ids))
223 mutex_lock(&adev->physical_node_lock);
224 if (list_empty(&adev->physical_node_list)) {
227 const struct acpi_device_physical_node *node;
229 node = list_first_entry(&adev->physical_node_list,
230 struct acpi_device_physical_node, node);
231 ret = node->dev == dev;
233 mutex_unlock(&adev->physical_node_lock);
239 * Creates uevent modalias field for ACPI enumerated devices.
240 * Because the other buses does not support ACPI HIDs & CIDs.
241 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
242 * "acpi:IBM0001:ACPI0001"
244 int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
248 if (!acpi_companion_match(dev))
251 if (add_uevent_var(env, "MODALIAS="))
253 len = create_modalias(ACPI_COMPANION(dev), &env->buf[env->buflen - 1],
254 sizeof(env->buf) - env->buflen);
260 EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias);
263 * Creates modalias sysfs attribute for ACPI enumerated devices.
264 * Because the other buses does not support ACPI HIDs & CIDs.
265 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
266 * "acpi:IBM0001:ACPI0001"
268 int acpi_device_modalias(struct device *dev, char *buf, int size)
272 if (!acpi_companion_match(dev))
275 len = create_modalias(ACPI_COMPANION(dev), buf, size -1);
281 EXPORT_SYMBOL_GPL(acpi_device_modalias);
284 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
285 struct acpi_device *acpi_dev = to_acpi_device(dev);
288 len = create_modalias(acpi_dev, buf, 1024);
294 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
296 bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
298 struct acpi_device_physical_node *pn;
301 mutex_lock(&adev->physical_node_lock);
303 list_for_each_entry(pn, &adev->physical_node_list, node)
304 if (device_supports_offline(pn->dev) && !pn->dev->offline) {
306 kobject_uevent(&pn->dev->kobj, KOBJ_CHANGE);
312 mutex_unlock(&adev->physical_node_lock);
316 static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
319 struct acpi_device *device = NULL;
320 struct acpi_device_physical_node *pn;
321 bool second_pass = (bool)data;
322 acpi_status status = AE_OK;
324 if (acpi_bus_get_device(handle, &device))
327 if (device->handler && !device->handler->hotplug.enabled) {
328 *ret_p = &device->dev;
332 mutex_lock(&device->physical_node_lock);
334 list_for_each_entry(pn, &device->physical_node_list, node) {
338 /* Skip devices offlined by the first pass. */
342 pn->put_online = false;
344 ret = device_offline(pn->dev);
345 if (acpi_force_hot_remove)
349 pn->put_online = !ret;
359 mutex_unlock(&device->physical_node_lock);
364 static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
367 struct acpi_device *device = NULL;
368 struct acpi_device_physical_node *pn;
370 if (acpi_bus_get_device(handle, &device))
373 mutex_lock(&device->physical_node_lock);
375 list_for_each_entry(pn, &device->physical_node_list, node)
376 if (pn->put_online) {
377 device_online(pn->dev);
378 pn->put_online = false;
381 mutex_unlock(&device->physical_node_lock);
386 static int acpi_scan_try_to_offline(struct acpi_device *device)
388 acpi_handle handle = device->handle;
389 struct device *errdev = NULL;
393 * Carry out two passes here and ignore errors in the first pass,
394 * because if the devices in question are memory blocks and
395 * CONFIG_MEMCG is set, one of the blocks may hold data structures
396 * that the other blocks depend on, but it is not known in advance which
399 * If the first pass is successful, the second one isn't needed, though.
401 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
402 NULL, acpi_bus_offline, (void *)false,
404 if (status == AE_SUPPORT) {
405 dev_warn(errdev, "Offline disabled.\n");
406 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
407 acpi_bus_online, NULL, NULL, NULL);
410 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
413 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
414 NULL, acpi_bus_offline, (void *)true,
416 if (!errdev || acpi_force_hot_remove)
417 acpi_bus_offline(handle, 0, (void *)true,
420 if (errdev && !acpi_force_hot_remove) {
421 dev_warn(errdev, "Offline failed.\n");
422 acpi_bus_online(handle, 0, NULL, NULL);
423 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
424 ACPI_UINT32_MAX, acpi_bus_online,
432 static int acpi_scan_hot_remove(struct acpi_device *device)
434 acpi_handle handle = device->handle;
435 unsigned long long sta;
438 if (device->handler && device->handler->hotplug.demand_offline
439 && !acpi_force_hot_remove) {
440 if (!acpi_scan_is_offline(device, true))
443 int error = acpi_scan_try_to_offline(device);
448 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
449 "Hot-removing device %s...\n", dev_name(&device->dev)));
451 acpi_bus_trim(device);
453 acpi_evaluate_lck(handle, 0);
457 status = acpi_evaluate_ej0(handle);
458 if (status == AE_NOT_FOUND)
460 else if (ACPI_FAILURE(status))
464 * Verify if eject was indeed successful. If not, log an error
465 * message. No need to call _OST since _EJ0 call was made OK.
467 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
468 if (ACPI_FAILURE(status)) {
469 acpi_handle_warn(handle,
470 "Status check after eject failed (0x%x)\n", status);
471 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
472 acpi_handle_warn(handle,
473 "Eject incomplete - status 0x%llx\n", sta);
479 static int acpi_scan_device_not_present(struct acpi_device *adev)
481 if (!acpi_device_enumerated(adev)) {
482 dev_warn(&adev->dev, "Still not present\n");
489 static int acpi_scan_device_check(struct acpi_device *adev)
493 acpi_bus_get_status(adev);
494 if (adev->status.present || adev->status.functional) {
496 * This function is only called for device objects for which
497 * matching scan handlers exist. The only situation in which
498 * the scan handler is not attached to this device object yet
499 * is when the device has just appeared (either it wasn't
500 * present at all before or it was removed and then added
504 dev_warn(&adev->dev, "Already enumerated\n");
507 error = acpi_bus_scan(adev->handle);
509 dev_warn(&adev->dev, "Namespace scan failure\n");
512 if (!adev->handler) {
513 dev_warn(&adev->dev, "Enumeration failure\n");
517 error = acpi_scan_device_not_present(adev);
522 static int acpi_scan_bus_check(struct acpi_device *adev)
524 struct acpi_scan_handler *handler = adev->handler;
525 struct acpi_device *child;
528 acpi_bus_get_status(adev);
529 if (!(adev->status.present || adev->status.functional)) {
530 acpi_scan_device_not_present(adev);
533 if (handler && handler->hotplug.scan_dependent)
534 return handler->hotplug.scan_dependent(adev);
536 error = acpi_bus_scan(adev->handle);
538 dev_warn(&adev->dev, "Namespace scan failure\n");
541 list_for_each_entry(child, &adev->children, node) {
542 error = acpi_scan_bus_check(child);
549 static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
552 case ACPI_NOTIFY_BUS_CHECK:
553 return acpi_scan_bus_check(adev);
554 case ACPI_NOTIFY_DEVICE_CHECK:
555 return acpi_scan_device_check(adev);
556 case ACPI_NOTIFY_EJECT_REQUEST:
557 case ACPI_OST_EC_OSPM_EJECT:
558 if (adev->handler && !adev->handler->hotplug.enabled) {
559 dev_info(&adev->dev, "Eject disabled\n");
562 acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
563 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
564 return acpi_scan_hot_remove(adev);
569 void acpi_device_hotplug(struct acpi_device *adev, u32 src)
571 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
574 lock_device_hotplug();
575 mutex_lock(&acpi_scan_lock);
578 * The device object's ACPI handle cannot become invalid as long as we
579 * are holding acpi_scan_lock, but it might have become invalid before
580 * that lock was acquired.
582 if (adev->handle == INVALID_ACPI_HANDLE)
585 if (adev->flags.is_dock_station) {
586 error = dock_notify(adev, src);
587 } else if (adev->flags.hotplug_notify) {
588 error = acpi_generic_hotplug_event(adev, src);
589 if (error == -EPERM) {
590 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
594 int (*notify)(struct acpi_device *, u32);
596 acpi_lock_hp_context();
597 notify = adev->hp ? adev->hp->notify : NULL;
598 acpi_unlock_hp_context();
600 * There may be additional notify handlers for device objects
601 * without the .event() callback, so ignore them here.
604 error = notify(adev, src);
609 ost_code = ACPI_OST_SC_SUCCESS;
612 acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
615 acpi_bus_put_acpi_device(adev);
616 mutex_unlock(&acpi_scan_lock);
617 unlock_device_hotplug();
620 static ssize_t real_power_state_show(struct device *dev,
621 struct device_attribute *attr, char *buf)
623 struct acpi_device *adev = to_acpi_device(dev);
627 ret = acpi_device_get_power(adev, &state);
631 return sprintf(buf, "%s\n", acpi_power_state_string(state));
634 static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
636 static ssize_t power_state_show(struct device *dev,
637 struct device_attribute *attr, char *buf)
639 struct acpi_device *adev = to_acpi_device(dev);
641 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
644 static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
647 acpi_eject_store(struct device *d, struct device_attribute *attr,
648 const char *buf, size_t count)
650 struct acpi_device *acpi_device = to_acpi_device(d);
651 acpi_object_type not_used;
654 if (!count || buf[0] != '1')
657 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
658 && !acpi_device->driver)
661 status = acpi_get_type(acpi_device->handle, ¬_used);
662 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
665 get_device(&acpi_device->dev);
666 status = acpi_hotplug_schedule(acpi_device, ACPI_OST_EC_OSPM_EJECT);
667 if (ACPI_SUCCESS(status))
670 put_device(&acpi_device->dev);
671 acpi_evaluate_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
672 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
673 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
676 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
679 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
680 struct acpi_device *acpi_dev = to_acpi_device(dev);
682 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
684 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
686 static ssize_t acpi_device_uid_show(struct device *dev,
687 struct device_attribute *attr, char *buf)
689 struct acpi_device *acpi_dev = to_acpi_device(dev);
691 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
693 static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
695 static ssize_t acpi_device_adr_show(struct device *dev,
696 struct device_attribute *attr, char *buf)
698 struct acpi_device *acpi_dev = to_acpi_device(dev);
700 return sprintf(buf, "0x%08x\n",
701 (unsigned int)(acpi_dev->pnp.bus_address));
703 static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
706 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
707 struct acpi_device *acpi_dev = to_acpi_device(dev);
708 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
711 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
715 result = sprintf(buf, "%s\n", (char*)path.pointer);
720 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
722 /* sysfs file that shows description text from the ACPI _STR method */
723 static ssize_t description_show(struct device *dev,
724 struct device_attribute *attr,
726 struct acpi_device *acpi_dev = to_acpi_device(dev);
729 if (acpi_dev->pnp.str_obj == NULL)
733 * The _STR object contains a Unicode identifier for a device.
734 * We need to convert to utf-8 so it can be displayed.
736 result = utf16s_to_utf8s(
737 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
738 acpi_dev->pnp.str_obj->buffer.length,
739 UTF16_LITTLE_ENDIAN, buf,
742 buf[result++] = '\n';
746 static DEVICE_ATTR(description, 0444, description_show, NULL);
749 acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
751 struct acpi_device *acpi_dev = to_acpi_device(dev);
753 unsigned long long sun;
755 status = acpi_evaluate_integer(acpi_dev->handle, "_SUN", NULL, &sun);
756 if (ACPI_FAILURE(status))
759 return sprintf(buf, "%llu\n", sun);
761 static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
763 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
765 struct acpi_device *acpi_dev = to_acpi_device(dev);
767 unsigned long long sta;
769 status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta);
770 if (ACPI_FAILURE(status))
773 return sprintf(buf, "%llu\n", sta);
775 static DEVICE_ATTR_RO(status);
777 static int acpi_device_setup_files(struct acpi_device *dev)
779 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
784 * Devices gotten from FADT don't have a "path" attribute
787 result = device_create_file(&dev->dev, &dev_attr_path);
792 if (!list_empty(&dev->pnp.ids)) {
793 result = device_create_file(&dev->dev, &dev_attr_hid);
797 result = device_create_file(&dev->dev, &dev_attr_modalias);
803 * If device has _STR, 'description' file is created
805 if (acpi_has_method(dev->handle, "_STR")) {
806 status = acpi_evaluate_object(dev->handle, "_STR",
808 if (ACPI_FAILURE(status))
809 buffer.pointer = NULL;
810 dev->pnp.str_obj = buffer.pointer;
811 result = device_create_file(&dev->dev, &dev_attr_description);
816 if (dev->pnp.type.bus_address)
817 result = device_create_file(&dev->dev, &dev_attr_adr);
818 if (dev->pnp.unique_id)
819 result = device_create_file(&dev->dev, &dev_attr_uid);
821 if (acpi_has_method(dev->handle, "_SUN")) {
822 result = device_create_file(&dev->dev, &dev_attr_sun);
827 if (acpi_has_method(dev->handle, "_STA")) {
828 result = device_create_file(&dev->dev, &dev_attr_status);
834 * If device has _EJ0, 'eject' file is created that is used to trigger
835 * hot-removal function from userland.
837 if (acpi_has_method(dev->handle, "_EJ0")) {
838 result = device_create_file(&dev->dev, &dev_attr_eject);
843 if (dev->flags.power_manageable) {
844 result = device_create_file(&dev->dev, &dev_attr_power_state);
848 if (dev->power.flags.power_resources)
849 result = device_create_file(&dev->dev,
850 &dev_attr_real_power_state);
857 static void acpi_device_remove_files(struct acpi_device *dev)
859 if (dev->flags.power_manageable) {
860 device_remove_file(&dev->dev, &dev_attr_power_state);
861 if (dev->power.flags.power_resources)
862 device_remove_file(&dev->dev,
863 &dev_attr_real_power_state);
867 * If device has _STR, remove 'description' file
869 if (acpi_has_method(dev->handle, "_STR")) {
870 kfree(dev->pnp.str_obj);
871 device_remove_file(&dev->dev, &dev_attr_description);
874 * If device has _EJ0, remove 'eject' file.
876 if (acpi_has_method(dev->handle, "_EJ0"))
877 device_remove_file(&dev->dev, &dev_attr_eject);
879 if (acpi_has_method(dev->handle, "_SUN"))
880 device_remove_file(&dev->dev, &dev_attr_sun);
882 if (dev->pnp.unique_id)
883 device_remove_file(&dev->dev, &dev_attr_uid);
884 if (dev->pnp.type.bus_address)
885 device_remove_file(&dev->dev, &dev_attr_adr);
886 device_remove_file(&dev->dev, &dev_attr_modalias);
887 device_remove_file(&dev->dev, &dev_attr_hid);
888 if (acpi_has_method(dev->handle, "_STA"))
889 device_remove_file(&dev->dev, &dev_attr_status);
891 device_remove_file(&dev->dev, &dev_attr_path);
893 /* --------------------------------------------------------------------------
895 -------------------------------------------------------------------------- */
897 static const struct acpi_device_id *__acpi_match_device(
898 struct acpi_device *device, const struct acpi_device_id *ids)
900 const struct acpi_device_id *id;
901 struct acpi_hardware_id *hwid;
904 * If the device is not present, it is unnecessary to load device
907 if (!device->status.present)
910 for (id = ids; id->id[0]; id++)
911 list_for_each_entry(hwid, &device->pnp.ids, list)
912 if (!strcmp((char *) id->id, hwid->id))
919 * acpi_match_device - Match a struct device against a given list of ACPI IDs
920 * @ids: Array of struct acpi_device_id object to match against.
921 * @dev: The device structure to match.
923 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
924 * object for that handle and use that object to match against a given list of
927 * Return a pointer to the first matching ID on success or %NULL on failure.
929 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
930 const struct device *dev)
932 struct acpi_device *adev;
933 acpi_handle handle = ACPI_HANDLE(dev);
935 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
938 if (!acpi_companion_match(dev))
941 return __acpi_match_device(adev, ids);
943 EXPORT_SYMBOL_GPL(acpi_match_device);
945 int acpi_match_device_ids(struct acpi_device *device,
946 const struct acpi_device_id *ids)
948 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
950 EXPORT_SYMBOL(acpi_match_device_ids);
953 * acpi_of_match_device - Match device using the "compatible" property.
954 * @dev: Device to match.
955 * @of_match_table: List of device IDs to match against.
957 * If @dev has an ACPI companion which has the special PRP0001 device ID in its
958 * list of identifiers and a _DSD object with the "compatible" property, use
959 * that property to match against the given list of identifiers.
961 static bool acpi_of_match_device(struct device *dev,
962 const struct of_device_id *of_match_table)
964 const union acpi_object *of_compatible, *obj;
965 struct acpi_device *adev;
968 adev = ACPI_COMPANION(dev);
972 of_compatible = adev->data.of_compatible;
973 if (!of_match_table || !of_compatible)
976 if (of_compatible->type == ACPI_TYPE_PACKAGE) {
977 nval = of_compatible->package.count;
978 obj = of_compatible->package.elements;
979 } else { /* Must be ACPI_TYPE_STRING. */
983 /* Now we can look for the driver DT compatible strings */
984 for (i = 0; i < nval; i++, obj++) {
985 const struct of_device_id *id;
987 for (id = of_match_table; id->compatible[0]; id++)
988 if (!strcasecmp(obj->string.pointer, id->compatible))
995 bool acpi_driver_match_device(struct device *dev,
996 const struct device_driver *drv)
998 if (!drv->acpi_match_table)
999 return acpi_of_match_device(dev, drv->of_match_table);
1001 return !!acpi_match_device(drv->acpi_match_table, dev);
1003 EXPORT_SYMBOL_GPL(acpi_driver_match_device);
1005 static void acpi_free_power_resources_lists(struct acpi_device *device)
1009 if (device->wakeup.flags.valid)
1010 acpi_power_resources_list_free(&device->wakeup.resources);
1012 if (!device->power.flags.power_resources)
1015 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
1016 struct acpi_device_power_state *ps = &device->power.states[i];
1017 acpi_power_resources_list_free(&ps->resources);
1021 static void acpi_device_release(struct device *dev)
1023 struct acpi_device *acpi_dev = to_acpi_device(dev);
1025 acpi_free_properties(acpi_dev);
1026 acpi_free_pnp_ids(&acpi_dev->pnp);
1027 acpi_free_power_resources_lists(acpi_dev);
1031 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
1033 struct acpi_device *acpi_dev = to_acpi_device(dev);
1034 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
1036 return acpi_dev->flags.match_driver
1037 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
1040 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
1042 struct acpi_device *acpi_dev = to_acpi_device(dev);
1045 if (list_empty(&acpi_dev->pnp.ids))
1048 if (add_uevent_var(env, "MODALIAS="))
1050 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
1051 sizeof(env->buf) - env->buflen);
1058 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
1060 struct acpi_device *device = data;
1062 device->driver->ops.notify(device, event);
1065 static void acpi_device_notify_fixed(void *data)
1067 struct acpi_device *device = data;
1069 /* Fixed hardware devices have no handles */
1070 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
1073 static u32 acpi_device_fixed_event(void *data)
1075 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_device_notify_fixed, data);
1076 return ACPI_INTERRUPT_HANDLED;
1079 static int acpi_device_install_notify_handler(struct acpi_device *device)
1083 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
1085 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
1086 acpi_device_fixed_event,
1088 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
1090 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
1091 acpi_device_fixed_event,
1094 status = acpi_install_notify_handler(device->handle,
1099 if (ACPI_FAILURE(status))
1104 static void acpi_device_remove_notify_handler(struct acpi_device *device)
1106 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
1107 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
1108 acpi_device_fixed_event);
1109 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
1110 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
1111 acpi_device_fixed_event);
1113 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
1114 acpi_device_notify);
1117 static int acpi_device_probe(struct device *dev)
1119 struct acpi_device *acpi_dev = to_acpi_device(dev);
1120 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
1123 if (acpi_dev->handler && !acpi_is_pnp_device(acpi_dev))
1126 if (!acpi_drv->ops.add)
1129 ret = acpi_drv->ops.add(acpi_dev);
1133 acpi_dev->driver = acpi_drv;
1134 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1135 "Driver [%s] successfully bound to device [%s]\n",
1136 acpi_drv->name, acpi_dev->pnp.bus_id));
1138 if (acpi_drv->ops.notify) {
1139 ret = acpi_device_install_notify_handler(acpi_dev);
1141 if (acpi_drv->ops.remove)
1142 acpi_drv->ops.remove(acpi_dev);
1144 acpi_dev->driver = NULL;
1145 acpi_dev->driver_data = NULL;
1150 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
1151 acpi_drv->name, acpi_dev->pnp.bus_id));
1156 static int acpi_device_remove(struct device * dev)
1158 struct acpi_device *acpi_dev = to_acpi_device(dev);
1159 struct acpi_driver *acpi_drv = acpi_dev->driver;
1162 if (acpi_drv->ops.notify)
1163 acpi_device_remove_notify_handler(acpi_dev);
1164 if (acpi_drv->ops.remove)
1165 acpi_drv->ops.remove(acpi_dev);
1167 acpi_dev->driver = NULL;
1168 acpi_dev->driver_data = NULL;
1174 struct bus_type acpi_bus_type = {
1176 .match = acpi_bus_match,
1177 .probe = acpi_device_probe,
1178 .remove = acpi_device_remove,
1179 .uevent = acpi_device_uevent,
1182 static void acpi_device_del(struct acpi_device *device)
1184 mutex_lock(&acpi_device_lock);
1186 list_del(&device->node);
1188 list_del(&device->wakeup_list);
1189 mutex_unlock(&acpi_device_lock);
1191 acpi_power_add_remove_device(device, false);
1192 acpi_device_remove_files(device);
1194 device->remove(device);
1196 device_del(&device->dev);
1199 static LIST_HEAD(acpi_device_del_list);
1200 static DEFINE_MUTEX(acpi_device_del_lock);
1202 static void acpi_device_del_work_fn(struct work_struct *work_not_used)
1205 struct acpi_device *adev;
1207 mutex_lock(&acpi_device_del_lock);
1209 if (list_empty(&acpi_device_del_list)) {
1210 mutex_unlock(&acpi_device_del_lock);
1213 adev = list_first_entry(&acpi_device_del_list,
1214 struct acpi_device, del_list);
1215 list_del(&adev->del_list);
1217 mutex_unlock(&acpi_device_del_lock);
1219 acpi_device_del(adev);
1221 * Drop references to all power resources that might have been
1222 * used by the device.
1224 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
1225 put_device(&adev->dev);
1230 * acpi_scan_drop_device - Drop an ACPI device object.
1231 * @handle: Handle of an ACPI namespace node, not used.
1232 * @context: Address of the ACPI device object to drop.
1234 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
1235 * namespace node the device object pointed to by @context is attached to.
1237 * The unregistration is carried out asynchronously to avoid running
1238 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
1239 * ensure the correct ordering (the device objects must be unregistered in the
1240 * same order in which the corresponding namespace nodes are deleted).
1242 static void acpi_scan_drop_device(acpi_handle handle, void *context)
1244 static DECLARE_WORK(work, acpi_device_del_work_fn);
1245 struct acpi_device *adev = context;
1247 mutex_lock(&acpi_device_del_lock);
1250 * Use the ACPI hotplug workqueue which is ordered, so this work item
1251 * won't run after any hotplug work items submitted subsequently. That
1252 * prevents attempts to register device objects identical to those being
1253 * deleted from happening concurrently (such attempts result from
1254 * hotplug events handled via the ACPI hotplug workqueue). It also will
1255 * run after all of the work items submitted previosuly, which helps
1256 * those work items to ensure that they are not accessing stale device
1259 if (list_empty(&acpi_device_del_list))
1260 acpi_queue_hotplug_work(&work);
1262 list_add_tail(&adev->del_list, &acpi_device_del_list);
1263 /* Make acpi_ns_validate_handle() return NULL for this handle. */
1264 adev->handle = INVALID_ACPI_HANDLE;
1266 mutex_unlock(&acpi_device_del_lock);
1269 static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device,
1270 void (*callback)(void *))
1277 status = acpi_get_data_full(handle, acpi_scan_drop_device,
1278 (void **)device, callback);
1279 if (ACPI_FAILURE(status) || !*device) {
1280 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
1287 int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
1289 return acpi_get_device_data(handle, device, NULL);
1291 EXPORT_SYMBOL(acpi_bus_get_device);
1293 static void get_acpi_device(void *dev)
1296 get_device(&((struct acpi_device *)dev)->dev);
1299 struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
1301 struct acpi_device *adev = NULL;
1303 acpi_get_device_data(handle, &adev, get_acpi_device);
1307 void acpi_bus_put_acpi_device(struct acpi_device *adev)
1309 put_device(&adev->dev);
1312 int acpi_device_add(struct acpi_device *device,
1313 void (*release)(struct device *))
1316 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
1319 if (device->handle) {
1322 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
1324 if (ACPI_FAILURE(status)) {
1325 acpi_handle_err(device->handle,
1326 "Unable to attach device data\n");
1334 * Link this device to its parent and siblings.
1336 INIT_LIST_HEAD(&device->children);
1337 INIT_LIST_HEAD(&device->node);
1338 INIT_LIST_HEAD(&device->wakeup_list);
1339 INIT_LIST_HEAD(&device->physical_node_list);
1340 INIT_LIST_HEAD(&device->del_list);
1341 mutex_init(&device->physical_node_lock);
1343 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1345 pr_err(PREFIX "Memory allocation error\n");
1350 mutex_lock(&acpi_device_lock);
1352 * Find suitable bus_id and instance number in acpi_bus_id_list
1353 * If failed, create one and link it into acpi_bus_id_list
1355 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
1356 if (!strcmp(acpi_device_bus_id->bus_id,
1357 acpi_device_hid(device))) {
1358 acpi_device_bus_id->instance_no++;
1365 acpi_device_bus_id = new_bus_id;
1366 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
1367 acpi_device_bus_id->instance_no = 0;
1368 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1370 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
1373 list_add_tail(&device->node, &device->parent->children);
1375 if (device->wakeup.flags.valid)
1376 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
1377 mutex_unlock(&acpi_device_lock);
1380 device->dev.parent = &device->parent->dev;
1381 device->dev.bus = &acpi_bus_type;
1382 device->dev.release = release;
1383 result = device_add(&device->dev);
1385 dev_err(&device->dev, "Error registering device\n");
1389 result = acpi_device_setup_files(device);
1391 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1392 dev_name(&device->dev));
1397 mutex_lock(&acpi_device_lock);
1399 list_del(&device->node);
1400 list_del(&device->wakeup_list);
1401 mutex_unlock(&acpi_device_lock);
1404 acpi_detach_data(device->handle, acpi_scan_drop_device);
1408 struct acpi_device *acpi_get_next_child(struct device *dev,
1409 struct acpi_device *child)
1411 struct acpi_device *adev = ACPI_COMPANION(dev);
1412 struct list_head *head, *next;
1417 head = &adev->children;
1418 if (list_empty(head))
1422 return list_first_entry(head, struct acpi_device, node);
1424 next = child->node.next;
1425 return next == head ? NULL : list_entry(next, struct acpi_device, node);
1428 /* --------------------------------------------------------------------------
1430 -------------------------------------------------------------------------- */
1432 * acpi_bus_register_driver - register a driver with the ACPI bus
1433 * @driver: driver being registered
1435 * Registers a driver with the ACPI bus. Searches the namespace for all
1436 * devices that match the driver's criteria and binds. Returns zero for
1437 * success or a negative error status for failure.
1439 int acpi_bus_register_driver(struct acpi_driver *driver)
1445 driver->drv.name = driver->name;
1446 driver->drv.bus = &acpi_bus_type;
1447 driver->drv.owner = driver->owner;
1449 ret = driver_register(&driver->drv);
1453 EXPORT_SYMBOL(acpi_bus_register_driver);
1456 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
1457 * @driver: driver to unregister
1459 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1460 * devices that match the driver's criteria and unbinds.
1462 void acpi_bus_unregister_driver(struct acpi_driver *driver)
1464 driver_unregister(&driver->drv);
1467 EXPORT_SYMBOL(acpi_bus_unregister_driver);
1469 /* --------------------------------------------------------------------------
1471 -------------------------------------------------------------------------- */
1472 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1474 struct acpi_device *device = NULL;
1478 * Fixed hardware devices do not appear in the namespace and do not
1479 * have handles, but we fabricate acpi_devices for them, so we have
1480 * to deal with them specially.
1486 status = acpi_get_parent(handle, &handle);
1487 if (ACPI_FAILURE(status))
1488 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1489 } while (acpi_bus_get_device(handle, &device));
1494 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1498 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1499 union acpi_object *obj;
1501 status = acpi_get_handle(handle, "_EJD", &tmp);
1502 if (ACPI_FAILURE(status))
1505 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1506 if (ACPI_SUCCESS(status)) {
1507 obj = buffer.pointer;
1508 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1510 kfree(buffer.pointer);
1514 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1516 static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1517 struct acpi_device_wakeup *wakeup)
1519 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1520 union acpi_object *package = NULL;
1521 union acpi_object *element = NULL;
1528 INIT_LIST_HEAD(&wakeup->resources);
1531 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1532 if (ACPI_FAILURE(status)) {
1533 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
1537 package = (union acpi_object *)buffer.pointer;
1539 if (!package || package->package.count < 2)
1542 element = &(package->package.elements[0]);
1546 if (element->type == ACPI_TYPE_PACKAGE) {
1547 if ((element->package.count < 2) ||
1548 (element->package.elements[0].type !=
1549 ACPI_TYPE_LOCAL_REFERENCE)
1550 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
1553 wakeup->gpe_device =
1554 element->package.elements[0].reference.handle;
1555 wakeup->gpe_number =
1556 (u32) element->package.elements[1].integer.value;
1557 } else if (element->type == ACPI_TYPE_INTEGER) {
1558 wakeup->gpe_device = NULL;
1559 wakeup->gpe_number = element->integer.value;
1564 element = &(package->package.elements[1]);
1565 if (element->type != ACPI_TYPE_INTEGER)
1568 wakeup->sleep_state = element->integer.value;
1570 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1574 if (!list_empty(&wakeup->resources)) {
1577 err = acpi_power_wakeup_list_init(&wakeup->resources,
1580 acpi_handle_warn(handle, "Retrieving current states "
1581 "of wakeup power resources failed\n");
1582 acpi_power_resources_list_free(&wakeup->resources);
1585 if (sleep_state < wakeup->sleep_state) {
1586 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1587 "(S%d) by S%d from power resources\n",
1588 (int)wakeup->sleep_state, sleep_state);
1589 wakeup->sleep_state = sleep_state;
1594 kfree(buffer.pointer);
1598 static void acpi_wakeup_gpe_init(struct acpi_device *device)
1600 struct acpi_device_id button_device_ids[] = {
1606 struct acpi_device_wakeup *wakeup = &device->wakeup;
1608 acpi_event_status event_status;
1610 wakeup->flags.notifier_present = 0;
1612 /* Power button, Lid switch always enable wakeup */
1613 if (!acpi_match_device_ids(device, button_device_ids)) {
1614 wakeup->flags.run_wake = 1;
1615 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1616 /* Do not use Lid/sleep button for S5 wakeup */
1617 if (wakeup->sleep_state == ACPI_STATE_S5)
1618 wakeup->sleep_state = ACPI_STATE_S4;
1620 acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
1621 device_set_wakeup_capable(&device->dev, true);
1625 acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
1626 wakeup->gpe_number);
1627 status = acpi_get_gpe_status(wakeup->gpe_device, wakeup->gpe_number,
1629 if (ACPI_FAILURE(status))
1632 wakeup->flags.run_wake = !!(event_status & ACPI_EVENT_FLAG_HAS_HANDLER);
1635 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1639 /* Presence of _PRW indicates wake capable */
1640 if (!acpi_has_method(device->handle, "_PRW"))
1643 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1646 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
1650 device->wakeup.flags.valid = 1;
1651 device->wakeup.prepare_count = 0;
1652 acpi_wakeup_gpe_init(device);
1653 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1654 * system for the ACPI device with the _PRW object.
1655 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1656 * So it is necessary to call _DSW object first. Only when it is not
1657 * present will the _PSW object used.
1659 err = acpi_device_sleep_wake(device, 0, 0, 0);
1661 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1662 "error in _DSW or _PSW evaluation\n"));
1665 static void acpi_bus_init_power_state(struct acpi_device *device, int state)
1667 struct acpi_device_power_state *ps = &device->power.states[state];
1668 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1669 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1672 INIT_LIST_HEAD(&ps->resources);
1674 /* Evaluate "_PRx" to get referenced power resources */
1675 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1676 if (ACPI_SUCCESS(status)) {
1677 union acpi_object *package = buffer.pointer;
1679 if (buffer.length && package
1680 && package->type == ACPI_TYPE_PACKAGE
1681 && package->package.count) {
1682 int err = acpi_extract_power_resources(package, 0,
1685 device->power.flags.power_resources = 1;
1687 ACPI_FREE(buffer.pointer);
1690 /* Evaluate "_PSx" to see if we can do explicit sets */
1692 if (acpi_has_method(device->handle, pathname))
1693 ps->flags.explicit_set = 1;
1696 * State is valid if there are means to put the device into it.
1697 * D3hot is only valid if _PR3 present.
1699 if (!list_empty(&ps->resources)
1700 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1701 ps->flags.valid = 1;
1702 ps->flags.os_accessible = 1;
1705 ps->power = -1; /* Unknown - driver assigned */
1706 ps->latency = -1; /* Unknown - driver assigned */
1709 static void acpi_bus_get_power_flags(struct acpi_device *device)
1713 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1714 if (!acpi_has_method(device->handle, "_PS0") &&
1715 !acpi_has_method(device->handle, "_PR0"))
1718 device->flags.power_manageable = 1;
1721 * Power Management Flags
1723 if (acpi_has_method(device->handle, "_PSC"))
1724 device->power.flags.explicit_get = 1;
1726 if (acpi_has_method(device->handle, "_IRC"))
1727 device->power.flags.inrush_current = 1;
1729 if (acpi_has_method(device->handle, "_DSW"))
1730 device->power.flags.dsw_present = 1;
1733 * Enumerate supported power management states
1735 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1736 acpi_bus_init_power_state(device, i);
1738 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1740 /* Set defaults for D0 and D3 states (always valid) */
1741 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1742 device->power.states[ACPI_STATE_D0].power = 100;
1743 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1744 device->power.states[ACPI_STATE_D3_COLD].power = 0;
1746 /* Set D3cold's explicit_set flag if _PS3 exists. */
1747 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1748 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1750 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1751 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1752 device->power.flags.power_resources)
1753 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1755 if (acpi_bus_init_power(device))
1756 device->flags.power_manageable = 0;
1759 static void acpi_bus_get_flags(struct acpi_device *device)
1761 /* Presence of _STA indicates 'dynamic_status' */
1762 if (acpi_has_method(device->handle, "_STA"))
1763 device->flags.dynamic_status = 1;
1765 /* Presence of _RMV indicates 'removable' */
1766 if (acpi_has_method(device->handle, "_RMV"))
1767 device->flags.removable = 1;
1769 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1770 if (acpi_has_method(device->handle, "_EJD") ||
1771 acpi_has_method(device->handle, "_EJ0"))
1772 device->flags.ejectable = 1;
1775 static void acpi_device_get_busid(struct acpi_device *device)
1777 char bus_id[5] = { '?', 0 };
1778 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1784 * The device's Bus ID is simply the object name.
1785 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1787 if (ACPI_IS_ROOT_DEVICE(device)) {
1788 strcpy(device->pnp.bus_id, "ACPI");
1792 switch (device->device_type) {
1793 case ACPI_BUS_TYPE_POWER_BUTTON:
1794 strcpy(device->pnp.bus_id, "PWRF");
1796 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1797 strcpy(device->pnp.bus_id, "SLPF");
1800 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1801 /* Clean up trailing underscores (if any) */
1802 for (i = 3; i > 1; i--) {
1803 if (bus_id[i] == '_')
1808 strcpy(device->pnp.bus_id, bus_id);
1814 * acpi_ata_match - see if an acpi object is an ATA device
1816 * If an acpi object has one of the ACPI ATA methods defined,
1817 * then we can safely call it an ATA device.
1819 bool acpi_ata_match(acpi_handle handle)
1821 return acpi_has_method(handle, "_GTF") ||
1822 acpi_has_method(handle, "_GTM") ||
1823 acpi_has_method(handle, "_STM") ||
1824 acpi_has_method(handle, "_SDD");
1828 * acpi_bay_match - see if an acpi object is an ejectable driver bay
1830 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1831 * then we can safely call it an ejectable drive bay
1833 bool acpi_bay_match(acpi_handle handle)
1835 acpi_handle phandle;
1837 if (!acpi_has_method(handle, "_EJ0"))
1839 if (acpi_ata_match(handle))
1841 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1844 return acpi_ata_match(phandle);
1847 bool acpi_device_is_battery(struct acpi_device *adev)
1849 struct acpi_hardware_id *hwid;
1851 list_for_each_entry(hwid, &adev->pnp.ids, list)
1852 if (!strcmp("PNP0C0A", hwid->id))
1858 static bool is_ejectable_bay(struct acpi_device *adev)
1860 acpi_handle handle = adev->handle;
1862 if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
1865 return acpi_bay_match(handle);
1869 * acpi_dock_match - see if an acpi object has a _DCK method
1871 bool acpi_dock_match(acpi_handle handle)
1873 return acpi_has_method(handle, "_DCK");
1876 const char *acpi_device_hid(struct acpi_device *device)
1878 struct acpi_hardware_id *hid;
1880 if (list_empty(&device->pnp.ids))
1883 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1886 EXPORT_SYMBOL(acpi_device_hid);
1888 static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
1890 struct acpi_hardware_id *id;
1892 id = kmalloc(sizeof(*id), GFP_KERNEL);
1896 id->id = kstrdup(dev_id, GFP_KERNEL);
1902 list_add_tail(&id->list, &pnp->ids);
1903 pnp->type.hardware_id = 1;
1907 * Old IBM workstations have a DSDT bug wherein the SMBus object
1908 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1909 * prefix. Work around this.
1911 static bool acpi_ibm_smbus_match(acpi_handle handle)
1913 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1914 struct acpi_buffer path = { sizeof(node_name), node_name };
1916 if (!dmi_name_in_vendors("IBM"))
1919 /* Look for SMBS object */
1920 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1921 strcmp("SMBS", path.pointer))
1924 /* Does it have the necessary (but misnamed) methods? */
1925 if (acpi_has_method(handle, "SBI") &&
1926 acpi_has_method(handle, "SBR") &&
1927 acpi_has_method(handle, "SBW"))
1933 static bool acpi_object_is_system_bus(acpi_handle handle)
1937 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1940 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1947 static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1951 struct acpi_device_info *info;
1952 struct acpi_pnp_device_id_list *cid_list;
1955 switch (device_type) {
1956 case ACPI_BUS_TYPE_DEVICE:
1957 if (handle == ACPI_ROOT_OBJECT) {
1958 acpi_add_id(pnp, ACPI_SYSTEM_HID);
1962 status = acpi_get_object_info(handle, &info);
1963 if (ACPI_FAILURE(status)) {
1964 pr_err(PREFIX "%s: Error reading device info\n",
1969 if (info->valid & ACPI_VALID_HID) {
1970 acpi_add_id(pnp, info->hardware_id.string);
1971 pnp->type.platform_id = 1;
1973 if (info->valid & ACPI_VALID_CID) {
1974 cid_list = &info->compatible_id_list;
1975 for (i = 0; i < cid_list->count; i++)
1976 acpi_add_id(pnp, cid_list->ids[i].string);
1978 if (info->valid & ACPI_VALID_ADR) {
1979 pnp->bus_address = info->address;
1980 pnp->type.bus_address = 1;
1982 if (info->valid & ACPI_VALID_UID)
1983 pnp->unique_id = kstrdup(info->unique_id.string,
1989 * Some devices don't reliably have _HIDs & _CIDs, so add
1990 * synthetic HIDs to make sure drivers can find them.
1992 if (acpi_is_video_device(handle))
1993 acpi_add_id(pnp, ACPI_VIDEO_HID);
1994 else if (acpi_bay_match(handle))
1995 acpi_add_id(pnp, ACPI_BAY_HID);
1996 else if (acpi_dock_match(handle))
1997 acpi_add_id(pnp, ACPI_DOCK_HID);
1998 else if (acpi_ibm_smbus_match(handle))
1999 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
2000 else if (list_empty(&pnp->ids) &&
2001 acpi_object_is_system_bus(handle)) {
2002 /* \_SB, \_TZ, LNXSYBUS */
2003 acpi_add_id(pnp, ACPI_BUS_HID);
2004 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
2005 strcpy(pnp->device_class, ACPI_BUS_CLASS);
2009 case ACPI_BUS_TYPE_POWER:
2010 acpi_add_id(pnp, ACPI_POWER_HID);
2012 case ACPI_BUS_TYPE_PROCESSOR:
2013 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
2015 case ACPI_BUS_TYPE_THERMAL:
2016 acpi_add_id(pnp, ACPI_THERMAL_HID);
2018 case ACPI_BUS_TYPE_POWER_BUTTON:
2019 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
2021 case ACPI_BUS_TYPE_SLEEP_BUTTON:
2022 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
2027 void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
2029 struct acpi_hardware_id *id, *tmp;
2031 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
2035 kfree(pnp->unique_id);
2038 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
2039 int type, unsigned long long sta)
2041 INIT_LIST_HEAD(&device->pnp.ids);
2042 device->device_type = type;
2043 device->handle = handle;
2044 device->parent = acpi_bus_get_parent(handle);
2045 device->fwnode.type = FWNODE_ACPI;
2046 acpi_set_device_status(device, sta);
2047 acpi_device_get_busid(device);
2048 acpi_set_pnp_ids(handle, &device->pnp, type);
2049 acpi_init_properties(device);
2050 acpi_bus_get_flags(device);
2051 device->flags.match_driver = false;
2052 device->flags.initialized = true;
2053 device->flags.visited = false;
2054 device_initialize(&device->dev);
2055 dev_set_uevent_suppress(&device->dev, true);
2058 void acpi_device_add_finalize(struct acpi_device *device)
2060 dev_set_uevent_suppress(&device->dev, false);
2061 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
2064 static int acpi_add_single_object(struct acpi_device **child,
2065 acpi_handle handle, int type,
2066 unsigned long long sta)
2069 struct acpi_device *device;
2070 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
2072 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
2074 printk(KERN_ERR PREFIX "Memory allocation error\n");
2078 acpi_init_device_object(device, handle, type, sta);
2079 acpi_bus_get_power_flags(device);
2080 acpi_bus_get_wakeup_device_flags(device);
2082 result = acpi_device_add(device, acpi_device_release);
2084 acpi_device_release(&device->dev);
2088 acpi_power_add_remove_device(device, true);
2089 acpi_device_add_finalize(device);
2090 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
2091 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
2092 dev_name(&device->dev), (char *) buffer.pointer,
2093 device->parent ? dev_name(&device->parent->dev) : "(null)"));
2094 kfree(buffer.pointer);
2099 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
2100 unsigned long long *sta)
2103 acpi_object_type acpi_type;
2105 status = acpi_get_type(handle, &acpi_type);
2106 if (ACPI_FAILURE(status))
2109 switch (acpi_type) {
2110 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
2111 case ACPI_TYPE_DEVICE:
2112 *type = ACPI_BUS_TYPE_DEVICE;
2113 status = acpi_bus_get_status_handle(handle, sta);
2114 if (ACPI_FAILURE(status))
2117 case ACPI_TYPE_PROCESSOR:
2118 *type = ACPI_BUS_TYPE_PROCESSOR;
2119 status = acpi_bus_get_status_handle(handle, sta);
2120 if (ACPI_FAILURE(status))
2123 case ACPI_TYPE_THERMAL:
2124 *type = ACPI_BUS_TYPE_THERMAL;
2125 *sta = ACPI_STA_DEFAULT;
2127 case ACPI_TYPE_POWER:
2128 *type = ACPI_BUS_TYPE_POWER;
2129 *sta = ACPI_STA_DEFAULT;
2138 bool acpi_device_is_present(struct acpi_device *adev)
2140 if (adev->status.present || adev->status.functional)
2143 adev->flags.initialized = false;
2147 static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
2149 const struct acpi_device_id **matchid)
2151 const struct acpi_device_id *devid;
2154 return handler->match(idstr, matchid);
2156 for (devid = handler->ids; devid->id[0]; devid++)
2157 if (!strcmp((char *)devid->id, idstr)) {
2167 static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
2168 const struct acpi_device_id **matchid)
2170 struct acpi_scan_handler *handler;
2172 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
2173 if (acpi_scan_handler_matching(handler, idstr, matchid))
2179 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
2181 if (!!hotplug->enabled == !!val)
2184 mutex_lock(&acpi_scan_lock);
2186 hotplug->enabled = val;
2188 mutex_unlock(&acpi_scan_lock);
2191 static void acpi_scan_init_hotplug(struct acpi_device *adev)
2193 struct acpi_hardware_id *hwid;
2195 if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
2196 acpi_dock_add(adev);
2199 list_for_each_entry(hwid, &adev->pnp.ids, list) {
2200 struct acpi_scan_handler *handler;
2202 handler = acpi_scan_match_handler(hwid->id, NULL);
2204 adev->flags.hotplug_notify = true;
2210 static void acpi_device_dep_initialize(struct acpi_device *adev)
2212 struct acpi_dep_data *dep;
2213 struct acpi_handle_list dep_devices;
2217 if (!acpi_has_method(adev->handle, "_DEP"))
2220 status = acpi_evaluate_reference(adev->handle, "_DEP", NULL,
2222 if (ACPI_FAILURE(status)) {
2223 dev_dbg(&adev->dev, "Failed to evaluate _DEP.\n");
2227 for (i = 0; i < dep_devices.count; i++) {
2228 struct acpi_device_info *info;
2231 status = acpi_get_object_info(dep_devices.handles[i], &info);
2232 if (ACPI_FAILURE(status)) {
2233 dev_dbg(&adev->dev, "Error reading _DEP device info\n");
2238 * Skip the dependency of Windows System Power
2239 * Management Controller
2241 skip = info->valid & ACPI_VALID_HID &&
2242 !strcmp(info->hardware_id.string, "INT3396");
2249 dep = kzalloc(sizeof(struct acpi_dep_data), GFP_KERNEL);
2253 dep->master = dep_devices.handles[i];
2254 dep->slave = adev->handle;
2257 mutex_lock(&acpi_dep_list_lock);
2258 list_add_tail(&dep->node , &acpi_dep_list);
2259 mutex_unlock(&acpi_dep_list_lock);
2263 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
2264 void *not_used, void **return_value)
2266 struct acpi_device *device = NULL;
2268 unsigned long long sta;
2271 acpi_bus_get_device(handle, &device);
2275 result = acpi_bus_type_and_status(handle, &type, &sta);
2279 if (type == ACPI_BUS_TYPE_POWER) {
2280 acpi_add_power_resource(handle);
2284 acpi_add_single_object(&device, handle, type, sta);
2286 return AE_CTRL_DEPTH;
2288 acpi_scan_init_hotplug(device);
2289 acpi_device_dep_initialize(device);
2293 *return_value = device;
2298 static int acpi_check_spi_i2c_slave(struct acpi_resource *ares, void *data)
2300 bool *is_spi_i2c_slave_p = data;
2302 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
2306 * devices that are connected to UART still need to be enumerated to
2309 if (ares->data.common_serial_bus.type != ACPI_RESOURCE_SERIAL_TYPE_UART)
2310 *is_spi_i2c_slave_p = true;
2312 /* no need to do more checking */
2316 static void acpi_default_enumeration(struct acpi_device *device)
2318 struct list_head resource_list;
2319 bool is_spi_i2c_slave = false;
2321 if (!device->pnp.type.platform_id || device->handler)
2325 * Do not enemerate SPI/I2C slaves as they will be enuerated by their
2326 * respective parents.
2328 INIT_LIST_HEAD(&resource_list);
2329 acpi_dev_get_resources(device, &resource_list, acpi_check_spi_i2c_slave,
2331 acpi_dev_free_resource_list(&resource_list);
2332 if (!is_spi_i2c_slave)
2333 acpi_create_platform_device(device);
2336 static int acpi_scan_attach_handler(struct acpi_device *device)
2338 struct acpi_hardware_id *hwid;
2341 list_for_each_entry(hwid, &device->pnp.ids, list) {
2342 const struct acpi_device_id *devid;
2343 struct acpi_scan_handler *handler;
2345 handler = acpi_scan_match_handler(hwid->id, &devid);
2347 if (!handler->attach) {
2348 device->pnp.type.platform_id = 0;
2351 device->handler = handler;
2352 ret = handler->attach(device, devid);
2356 device->handler = NULL;
2362 acpi_default_enumeration(device);
2367 static void acpi_bus_attach(struct acpi_device *device)
2369 struct acpi_device *child;
2373 if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
2374 register_dock_dependent_device(device, ejd);
2376 acpi_bus_get_status(device);
2377 /* Skip devices that are not present. */
2378 if (!acpi_device_is_present(device)) {
2379 device->flags.visited = false;
2380 device->flags.power_manageable = 0;
2383 if (device->handler)
2386 if (!device->flags.initialized) {
2387 device->flags.power_manageable =
2388 device->power.states[ACPI_STATE_D0].flags.valid;
2389 if (acpi_bus_init_power(device))
2390 device->flags.power_manageable = 0;
2392 device->flags.initialized = true;
2394 device->flags.visited = false;
2395 ret = acpi_scan_attach_handler(device);
2399 device->flags.match_driver = true;
2401 ret = device_attach(&device->dev);
2405 device->flags.visited = true;
2408 list_for_each_entry(child, &device->children, node)
2409 acpi_bus_attach(child);
2411 if (device->handler && device->handler->hotplug.notify_online)
2412 device->handler->hotplug.notify_online(device);
2415 void acpi_walk_dep_device_list(acpi_handle handle)
2417 struct acpi_dep_data *dep, *tmp;
2418 struct acpi_device *adev;
2420 mutex_lock(&acpi_dep_list_lock);
2421 list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
2422 if (dep->master == handle) {
2423 acpi_bus_get_device(dep->slave, &adev);
2428 if (!adev->dep_unmet)
2429 acpi_bus_attach(adev);
2430 list_del(&dep->node);
2434 mutex_unlock(&acpi_dep_list_lock);
2436 EXPORT_SYMBOL_GPL(acpi_walk_dep_device_list);
2439 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2440 * @handle: Root of the namespace scope to scan.
2442 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2445 * If no devices were found, -ENODEV is returned, but it does not mean that
2446 * there has been a real error. There just have been no suitable ACPI objects
2447 * in the table trunk from which the kernel could create a device and add an
2448 * appropriate driver.
2450 * Must be called under acpi_scan_lock.
2452 int acpi_bus_scan(acpi_handle handle)
2454 void *device = NULL;
2456 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
2457 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2458 acpi_bus_check_add, NULL, NULL, &device);
2461 acpi_bus_attach(device);
2466 EXPORT_SYMBOL(acpi_bus_scan);
2469 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2470 * @adev: Root of the ACPI namespace scope to walk.
2472 * Must be called under acpi_scan_lock.
2474 void acpi_bus_trim(struct acpi_device *adev)
2476 struct acpi_scan_handler *handler = adev->handler;
2477 struct acpi_device *child;
2479 list_for_each_entry_reverse(child, &adev->children, node)
2480 acpi_bus_trim(child);
2482 adev->flags.match_driver = false;
2484 if (handler->detach)
2485 handler->detach(adev);
2487 adev->handler = NULL;
2489 device_release_driver(&adev->dev);
2492 * Most likely, the device is going away, so put it into D3cold before
2495 acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2496 adev->flags.initialized = false;
2497 adev->flags.visited = false;
2499 EXPORT_SYMBOL_GPL(acpi_bus_trim);
2501 static int acpi_bus_scan_fixed(void)
2506 * Enumerate all fixed-feature devices.
2508 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2509 struct acpi_device *device = NULL;
2511 result = acpi_add_single_object(&device, NULL,
2512 ACPI_BUS_TYPE_POWER_BUTTON,
2517 device->flags.match_driver = true;
2518 result = device_attach(&device->dev);
2522 device_init_wakeup(&device->dev, true);
2525 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2526 struct acpi_device *device = NULL;
2528 result = acpi_add_single_object(&device, NULL,
2529 ACPI_BUS_TYPE_SLEEP_BUTTON,
2534 device->flags.match_driver = true;
2535 result = device_attach(&device->dev);
2538 return result < 0 ? result : 0;
2541 int __init acpi_scan_init(void)
2545 result = bus_register(&acpi_bus_type);
2547 /* We don't want to quit even if we failed to add suspend/resume */
2548 printk(KERN_ERR PREFIX "Could not register bus type\n");
2551 acpi_pci_root_init();
2552 acpi_pci_link_init();
2553 acpi_processor_init();
2556 acpi_cmos_rtc_init();
2557 acpi_container_init();
2558 acpi_memory_hotplug_init();
2560 acpi_int340x_thermal_init();
2562 mutex_lock(&acpi_scan_lock);
2564 * Enumerate devices in the ACPI namespace.
2566 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
2570 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
2574 /* Fixed feature devices do not exist on HW-reduced platform */
2575 if (!acpi_gbl_reduced_hardware) {
2576 result = acpi_bus_scan_fixed();
2578 acpi_detach_data(acpi_root->handle,
2579 acpi_scan_drop_device);
2580 acpi_device_del(acpi_root);
2581 put_device(&acpi_root->dev);
2586 acpi_update_all_gpes();
2589 mutex_unlock(&acpi_scan_lock);