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);
44 static DEFINE_MUTEX(acpi_hp_context_lock);
46 struct acpi_device_bus_id{
48 unsigned int instance_no;
49 struct list_head node;
52 void acpi_scan_lock_acquire(void)
54 mutex_lock(&acpi_scan_lock);
56 EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
58 void acpi_scan_lock_release(void)
60 mutex_unlock(&acpi_scan_lock);
62 EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
64 void acpi_lock_hp_context(void)
66 mutex_lock(&acpi_hp_context_lock);
69 void acpi_unlock_hp_context(void)
71 mutex_unlock(&acpi_hp_context_lock);
74 void acpi_initialize_hp_context(struct acpi_device *adev,
75 struct acpi_hotplug_context *hp,
76 int (*notify)(struct acpi_device *, u32),
77 void (*uevent)(struct acpi_device *, u32))
79 acpi_lock_hp_context();
82 acpi_set_hp_context(adev, hp);
83 acpi_unlock_hp_context();
85 EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
87 int acpi_scan_add_handler(struct acpi_scan_handler *handler)
92 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
96 int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
97 const char *hotplug_profile_name)
101 error = acpi_scan_add_handler(handler);
105 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
110 * Creates hid/cid(s) string needed for modalias and uevent
111 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
112 * char *modalias: "acpi:IBM0001:ACPI0001"
113 * Return: 0: no _HID and no _CID
114 * -EINVAL: output error
115 * -ENOMEM: output is truncated
117 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
122 struct acpi_hardware_id *id;
124 if (list_empty(&acpi_dev->pnp.ids))
127 len = snprintf(modalias, size, "acpi:");
130 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
131 count = snprintf(&modalias[len], size, "%s:", id->id);
140 modalias[len] = '\0';
145 * acpi_companion_match() - Can we match via ACPI companion device
146 * @dev: Device in question
148 * Check if the given device has an ACPI companion and if that companion has
149 * a valid list of PNP IDs, and if the device is the first (primary) physical
150 * device associated with it.
152 * If multiple physical devices are attached to a single ACPI companion, we need
153 * to be careful. The usage scenario for this kind of relationship is that all
154 * of the physical devices in question use resources provided by the ACPI
155 * companion. A typical case is an MFD device where all the sub-devices share
156 * the parent's ACPI companion. In such cases we can only allow the primary
157 * (first) physical device to be matched with the help of the companion's PNP
160 * Additional physical devices sharing the ACPI companion can still use
161 * resources available from it but they will be matched normally using functions
162 * provided by their bus types (and analogously for their modalias).
164 static bool acpi_companion_match(const struct device *dev)
166 struct acpi_device *adev;
169 adev = ACPI_COMPANION(dev);
173 if (list_empty(&adev->pnp.ids))
176 mutex_lock(&adev->physical_node_lock);
177 if (list_empty(&adev->physical_node_list)) {
180 const struct acpi_device_physical_node *node;
182 node = list_first_entry(&adev->physical_node_list,
183 struct acpi_device_physical_node, node);
184 ret = node->dev == dev;
186 mutex_unlock(&adev->physical_node_lock);
192 * Creates uevent modalias field for ACPI enumerated devices.
193 * Because the other buses does not support ACPI HIDs & CIDs.
194 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
195 * "acpi:IBM0001:ACPI0001"
197 int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
201 if (!acpi_companion_match(dev))
204 if (add_uevent_var(env, "MODALIAS="))
206 len = create_modalias(ACPI_COMPANION(dev), &env->buf[env->buflen - 1],
207 sizeof(env->buf) - env->buflen);
213 EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias);
216 * Creates modalias sysfs attribute for ACPI enumerated devices.
217 * Because the other buses does not support ACPI HIDs & CIDs.
218 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
219 * "acpi:IBM0001:ACPI0001"
221 int acpi_device_modalias(struct device *dev, char *buf, int size)
225 if (!acpi_companion_match(dev))
228 len = create_modalias(ACPI_COMPANION(dev), buf, size -1);
234 EXPORT_SYMBOL_GPL(acpi_device_modalias);
237 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
238 struct acpi_device *acpi_dev = to_acpi_device(dev);
241 len = create_modalias(acpi_dev, buf, 1024);
247 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
249 bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
251 struct acpi_device_physical_node *pn;
254 mutex_lock(&adev->physical_node_lock);
256 list_for_each_entry(pn, &adev->physical_node_list, node)
257 if (device_supports_offline(pn->dev) && !pn->dev->offline) {
259 kobject_uevent(&pn->dev->kobj, KOBJ_CHANGE);
265 mutex_unlock(&adev->physical_node_lock);
269 static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
272 struct acpi_device *device = NULL;
273 struct acpi_device_physical_node *pn;
274 bool second_pass = (bool)data;
275 acpi_status status = AE_OK;
277 if (acpi_bus_get_device(handle, &device))
280 if (device->handler && !device->handler->hotplug.enabled) {
281 *ret_p = &device->dev;
285 mutex_lock(&device->physical_node_lock);
287 list_for_each_entry(pn, &device->physical_node_list, node) {
291 /* Skip devices offlined by the first pass. */
295 pn->put_online = false;
297 ret = device_offline(pn->dev);
298 if (acpi_force_hot_remove)
302 pn->put_online = !ret;
312 mutex_unlock(&device->physical_node_lock);
317 static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
320 struct acpi_device *device = NULL;
321 struct acpi_device_physical_node *pn;
323 if (acpi_bus_get_device(handle, &device))
326 mutex_lock(&device->physical_node_lock);
328 list_for_each_entry(pn, &device->physical_node_list, node)
329 if (pn->put_online) {
330 device_online(pn->dev);
331 pn->put_online = false;
334 mutex_unlock(&device->physical_node_lock);
339 static int acpi_scan_try_to_offline(struct acpi_device *device)
341 acpi_handle handle = device->handle;
342 struct device *errdev = NULL;
346 * Carry out two passes here and ignore errors in the first pass,
347 * because if the devices in question are memory blocks and
348 * CONFIG_MEMCG is set, one of the blocks may hold data structures
349 * that the other blocks depend on, but it is not known in advance which
352 * If the first pass is successful, the second one isn't needed, though.
354 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
355 NULL, acpi_bus_offline, (void *)false,
357 if (status == AE_SUPPORT) {
358 dev_warn(errdev, "Offline disabled.\n");
359 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
360 acpi_bus_online, NULL, NULL, NULL);
363 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
366 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
367 NULL, acpi_bus_offline, (void *)true,
369 if (!errdev || acpi_force_hot_remove)
370 acpi_bus_offline(handle, 0, (void *)true,
373 if (errdev && !acpi_force_hot_remove) {
374 dev_warn(errdev, "Offline failed.\n");
375 acpi_bus_online(handle, 0, NULL, NULL);
376 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
377 ACPI_UINT32_MAX, acpi_bus_online,
385 static int acpi_scan_hot_remove(struct acpi_device *device)
387 acpi_handle handle = device->handle;
388 unsigned long long sta;
391 if (device->handler && device->handler->hotplug.demand_offline
392 && !acpi_force_hot_remove) {
393 if (!acpi_scan_is_offline(device, true))
396 int error = acpi_scan_try_to_offline(device);
401 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
402 "Hot-removing device %s...\n", dev_name(&device->dev)));
404 acpi_bus_trim(device);
406 acpi_evaluate_lck(handle, 0);
410 status = acpi_evaluate_ej0(handle);
411 if (status == AE_NOT_FOUND)
413 else if (ACPI_FAILURE(status))
417 * Verify if eject was indeed successful. If not, log an error
418 * message. No need to call _OST since _EJ0 call was made OK.
420 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
421 if (ACPI_FAILURE(status)) {
422 acpi_handle_warn(handle,
423 "Status check after eject failed (0x%x)\n", status);
424 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
425 acpi_handle_warn(handle,
426 "Eject incomplete - status 0x%llx\n", sta);
432 static int acpi_scan_device_not_present(struct acpi_device *adev)
434 if (!acpi_device_enumerated(adev)) {
435 dev_warn(&adev->dev, "Still not present\n");
442 static int acpi_scan_device_check(struct acpi_device *adev)
446 acpi_bus_get_status(adev);
447 if (adev->status.present || adev->status.functional) {
449 * This function is only called for device objects for which
450 * matching scan handlers exist. The only situation in which
451 * the scan handler is not attached to this device object yet
452 * is when the device has just appeared (either it wasn't
453 * present at all before or it was removed and then added
457 dev_warn(&adev->dev, "Already enumerated\n");
460 error = acpi_bus_scan(adev->handle);
462 dev_warn(&adev->dev, "Namespace scan failure\n");
465 if (!adev->handler) {
466 dev_warn(&adev->dev, "Enumeration failure\n");
470 error = acpi_scan_device_not_present(adev);
475 static int acpi_scan_bus_check(struct acpi_device *adev)
477 struct acpi_scan_handler *handler = adev->handler;
478 struct acpi_device *child;
481 acpi_bus_get_status(adev);
482 if (!(adev->status.present || adev->status.functional)) {
483 acpi_scan_device_not_present(adev);
486 if (handler && handler->hotplug.scan_dependent)
487 return handler->hotplug.scan_dependent(adev);
489 error = acpi_bus_scan(adev->handle);
491 dev_warn(&adev->dev, "Namespace scan failure\n");
494 list_for_each_entry(child, &adev->children, node) {
495 error = acpi_scan_bus_check(child);
502 static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
505 case ACPI_NOTIFY_BUS_CHECK:
506 return acpi_scan_bus_check(adev);
507 case ACPI_NOTIFY_DEVICE_CHECK:
508 return acpi_scan_device_check(adev);
509 case ACPI_NOTIFY_EJECT_REQUEST:
510 case ACPI_OST_EC_OSPM_EJECT:
511 if (adev->handler && !adev->handler->hotplug.enabled) {
512 dev_info(&adev->dev, "Eject disabled\n");
515 acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
516 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
517 return acpi_scan_hot_remove(adev);
522 void acpi_device_hotplug(struct acpi_device *adev, u32 src)
524 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
527 lock_device_hotplug();
528 mutex_lock(&acpi_scan_lock);
531 * The device object's ACPI handle cannot become invalid as long as we
532 * are holding acpi_scan_lock, but it might have become invalid before
533 * that lock was acquired.
535 if (adev->handle == INVALID_ACPI_HANDLE)
538 if (adev->flags.is_dock_station) {
539 error = dock_notify(adev, src);
540 } else if (adev->flags.hotplug_notify) {
541 error = acpi_generic_hotplug_event(adev, src);
542 if (error == -EPERM) {
543 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
547 int (*notify)(struct acpi_device *, u32);
549 acpi_lock_hp_context();
550 notify = adev->hp ? adev->hp->notify : NULL;
551 acpi_unlock_hp_context();
553 * There may be additional notify handlers for device objects
554 * without the .event() callback, so ignore them here.
557 error = notify(adev, src);
562 ost_code = ACPI_OST_SC_SUCCESS;
565 acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
568 acpi_bus_put_acpi_device(adev);
569 mutex_unlock(&acpi_scan_lock);
570 unlock_device_hotplug();
573 static ssize_t real_power_state_show(struct device *dev,
574 struct device_attribute *attr, char *buf)
576 struct acpi_device *adev = to_acpi_device(dev);
580 ret = acpi_device_get_power(adev, &state);
584 return sprintf(buf, "%s\n", acpi_power_state_string(state));
587 static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
589 static ssize_t power_state_show(struct device *dev,
590 struct device_attribute *attr, char *buf)
592 struct acpi_device *adev = to_acpi_device(dev);
594 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
597 static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
600 acpi_eject_store(struct device *d, struct device_attribute *attr,
601 const char *buf, size_t count)
603 struct acpi_device *acpi_device = to_acpi_device(d);
604 acpi_object_type not_used;
607 if (!count || buf[0] != '1')
610 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
611 && !acpi_device->driver)
614 status = acpi_get_type(acpi_device->handle, ¬_used);
615 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
618 get_device(&acpi_device->dev);
619 status = acpi_hotplug_schedule(acpi_device, ACPI_OST_EC_OSPM_EJECT);
620 if (ACPI_SUCCESS(status))
623 put_device(&acpi_device->dev);
624 acpi_evaluate_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
625 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
626 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
629 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
632 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
633 struct acpi_device *acpi_dev = to_acpi_device(dev);
635 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
637 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
639 static ssize_t acpi_device_uid_show(struct device *dev,
640 struct device_attribute *attr, char *buf)
642 struct acpi_device *acpi_dev = to_acpi_device(dev);
644 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
646 static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
648 static ssize_t acpi_device_adr_show(struct device *dev,
649 struct device_attribute *attr, char *buf)
651 struct acpi_device *acpi_dev = to_acpi_device(dev);
653 return sprintf(buf, "0x%08x\n",
654 (unsigned int)(acpi_dev->pnp.bus_address));
656 static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
659 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
660 struct acpi_device *acpi_dev = to_acpi_device(dev);
661 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
664 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
668 result = sprintf(buf, "%s\n", (char*)path.pointer);
673 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
675 /* sysfs file that shows description text from the ACPI _STR method */
676 static ssize_t description_show(struct device *dev,
677 struct device_attribute *attr,
679 struct acpi_device *acpi_dev = to_acpi_device(dev);
682 if (acpi_dev->pnp.str_obj == NULL)
686 * The _STR object contains a Unicode identifier for a device.
687 * We need to convert to utf-8 so it can be displayed.
689 result = utf16s_to_utf8s(
690 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
691 acpi_dev->pnp.str_obj->buffer.length,
692 UTF16_LITTLE_ENDIAN, buf,
695 buf[result++] = '\n';
699 static DEVICE_ATTR(description, 0444, description_show, NULL);
702 acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
704 struct acpi_device *acpi_dev = to_acpi_device(dev);
706 unsigned long long sun;
708 status = acpi_evaluate_integer(acpi_dev->handle, "_SUN", NULL, &sun);
709 if (ACPI_FAILURE(status))
712 return sprintf(buf, "%llu\n", sun);
714 static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
716 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
718 struct acpi_device *acpi_dev = to_acpi_device(dev);
720 unsigned long long sta;
722 status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta);
723 if (ACPI_FAILURE(status))
726 return sprintf(buf, "%llu\n", sta);
728 static DEVICE_ATTR_RO(status);
730 static int acpi_device_setup_files(struct acpi_device *dev)
732 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
737 * Devices gotten from FADT don't have a "path" attribute
740 result = device_create_file(&dev->dev, &dev_attr_path);
745 if (!list_empty(&dev->pnp.ids)) {
746 result = device_create_file(&dev->dev, &dev_attr_hid);
750 result = device_create_file(&dev->dev, &dev_attr_modalias);
756 * If device has _STR, 'description' file is created
758 if (acpi_has_method(dev->handle, "_STR")) {
759 status = acpi_evaluate_object(dev->handle, "_STR",
761 if (ACPI_FAILURE(status))
762 buffer.pointer = NULL;
763 dev->pnp.str_obj = buffer.pointer;
764 result = device_create_file(&dev->dev, &dev_attr_description);
769 if (dev->pnp.type.bus_address)
770 result = device_create_file(&dev->dev, &dev_attr_adr);
771 if (dev->pnp.unique_id)
772 result = device_create_file(&dev->dev, &dev_attr_uid);
774 if (acpi_has_method(dev->handle, "_SUN")) {
775 result = device_create_file(&dev->dev, &dev_attr_sun);
780 if (acpi_has_method(dev->handle, "_STA")) {
781 result = device_create_file(&dev->dev, &dev_attr_status);
787 * If device has _EJ0, 'eject' file is created that is used to trigger
788 * hot-removal function from userland.
790 if (acpi_has_method(dev->handle, "_EJ0")) {
791 result = device_create_file(&dev->dev, &dev_attr_eject);
796 if (dev->flags.power_manageable) {
797 result = device_create_file(&dev->dev, &dev_attr_power_state);
801 if (dev->power.flags.power_resources)
802 result = device_create_file(&dev->dev,
803 &dev_attr_real_power_state);
810 static void acpi_device_remove_files(struct acpi_device *dev)
812 if (dev->flags.power_manageable) {
813 device_remove_file(&dev->dev, &dev_attr_power_state);
814 if (dev->power.flags.power_resources)
815 device_remove_file(&dev->dev,
816 &dev_attr_real_power_state);
820 * If device has _STR, remove 'description' file
822 if (acpi_has_method(dev->handle, "_STR")) {
823 kfree(dev->pnp.str_obj);
824 device_remove_file(&dev->dev, &dev_attr_description);
827 * If device has _EJ0, remove 'eject' file.
829 if (acpi_has_method(dev->handle, "_EJ0"))
830 device_remove_file(&dev->dev, &dev_attr_eject);
832 if (acpi_has_method(dev->handle, "_SUN"))
833 device_remove_file(&dev->dev, &dev_attr_sun);
835 if (dev->pnp.unique_id)
836 device_remove_file(&dev->dev, &dev_attr_uid);
837 if (dev->pnp.type.bus_address)
838 device_remove_file(&dev->dev, &dev_attr_adr);
839 device_remove_file(&dev->dev, &dev_attr_modalias);
840 device_remove_file(&dev->dev, &dev_attr_hid);
841 if (acpi_has_method(dev->handle, "_STA"))
842 device_remove_file(&dev->dev, &dev_attr_status);
844 device_remove_file(&dev->dev, &dev_attr_path);
846 /* --------------------------------------------------------------------------
848 -------------------------------------------------------------------------- */
850 static const struct acpi_device_id *__acpi_match_device(
851 struct acpi_device *device, const struct acpi_device_id *ids)
853 const struct acpi_device_id *id;
854 struct acpi_hardware_id *hwid;
857 * If the device is not present, it is unnecessary to load device
860 if (!device->status.present)
863 for (id = ids; id->id[0]; id++)
864 list_for_each_entry(hwid, &device->pnp.ids, list)
865 if (!strcmp((char *) id->id, hwid->id))
872 * acpi_match_device - Match a struct device against a given list of ACPI IDs
873 * @ids: Array of struct acpi_device_id object to match against.
874 * @dev: The device structure to match.
876 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
877 * object for that handle and use that object to match against a given list of
880 * Return a pointer to the first matching ID on success or %NULL on failure.
882 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
883 const struct device *dev)
885 struct acpi_device *adev;
886 acpi_handle handle = ACPI_HANDLE(dev);
888 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
891 if (!acpi_companion_match(dev))
894 return __acpi_match_device(adev, ids);
896 EXPORT_SYMBOL_GPL(acpi_match_device);
898 int acpi_match_device_ids(struct acpi_device *device,
899 const struct acpi_device_id *ids)
901 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
903 EXPORT_SYMBOL(acpi_match_device_ids);
905 static void acpi_free_power_resources_lists(struct acpi_device *device)
909 if (device->wakeup.flags.valid)
910 acpi_power_resources_list_free(&device->wakeup.resources);
912 if (!device->flags.power_manageable)
915 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
916 struct acpi_device_power_state *ps = &device->power.states[i];
917 acpi_power_resources_list_free(&ps->resources);
921 static void acpi_device_release(struct device *dev)
923 struct acpi_device *acpi_dev = to_acpi_device(dev);
925 acpi_free_pnp_ids(&acpi_dev->pnp);
926 acpi_free_power_resources_lists(acpi_dev);
930 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
932 struct acpi_device *acpi_dev = to_acpi_device(dev);
933 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
935 return acpi_dev->flags.match_driver
936 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
939 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
941 struct acpi_device *acpi_dev = to_acpi_device(dev);
944 if (list_empty(&acpi_dev->pnp.ids))
947 if (add_uevent_var(env, "MODALIAS="))
949 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
950 sizeof(env->buf) - env->buflen);
957 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
959 struct acpi_device *device = data;
961 device->driver->ops.notify(device, event);
964 static void acpi_device_notify_fixed(void *data)
966 struct acpi_device *device = data;
968 /* Fixed hardware devices have no handles */
969 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
972 static acpi_status acpi_device_fixed_event(void *data)
974 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_device_notify_fixed, data);
978 static int acpi_device_install_notify_handler(struct acpi_device *device)
982 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
984 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
985 acpi_device_fixed_event,
987 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
989 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
990 acpi_device_fixed_event,
993 status = acpi_install_notify_handler(device->handle,
998 if (ACPI_FAILURE(status))
1003 static void acpi_device_remove_notify_handler(struct acpi_device *device)
1005 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
1006 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
1007 acpi_device_fixed_event);
1008 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
1009 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
1010 acpi_device_fixed_event);
1012 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
1013 acpi_device_notify);
1016 static int acpi_device_probe(struct device *dev)
1018 struct acpi_device *acpi_dev = to_acpi_device(dev);
1019 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
1022 if (acpi_dev->handler && !acpi_is_pnp_device(acpi_dev))
1025 if (!acpi_drv->ops.add)
1028 ret = acpi_drv->ops.add(acpi_dev);
1032 acpi_dev->driver = acpi_drv;
1033 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1034 "Driver [%s] successfully bound to device [%s]\n",
1035 acpi_drv->name, acpi_dev->pnp.bus_id));
1037 if (acpi_drv->ops.notify) {
1038 ret = acpi_device_install_notify_handler(acpi_dev);
1040 if (acpi_drv->ops.remove)
1041 acpi_drv->ops.remove(acpi_dev);
1043 acpi_dev->driver = NULL;
1044 acpi_dev->driver_data = NULL;
1049 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
1050 acpi_drv->name, acpi_dev->pnp.bus_id));
1055 static int acpi_device_remove(struct device * dev)
1057 struct acpi_device *acpi_dev = to_acpi_device(dev);
1058 struct acpi_driver *acpi_drv = acpi_dev->driver;
1061 if (acpi_drv->ops.notify)
1062 acpi_device_remove_notify_handler(acpi_dev);
1063 if (acpi_drv->ops.remove)
1064 acpi_drv->ops.remove(acpi_dev);
1066 acpi_dev->driver = NULL;
1067 acpi_dev->driver_data = NULL;
1073 struct bus_type acpi_bus_type = {
1075 .match = acpi_bus_match,
1076 .probe = acpi_device_probe,
1077 .remove = acpi_device_remove,
1078 .uevent = acpi_device_uevent,
1081 static void acpi_device_del(struct acpi_device *device)
1083 mutex_lock(&acpi_device_lock);
1085 list_del(&device->node);
1087 list_del(&device->wakeup_list);
1088 mutex_unlock(&acpi_device_lock);
1090 acpi_power_add_remove_device(device, false);
1091 acpi_device_remove_files(device);
1093 device->remove(device);
1095 device_del(&device->dev);
1098 static LIST_HEAD(acpi_device_del_list);
1099 static DEFINE_MUTEX(acpi_device_del_lock);
1101 static void acpi_device_del_work_fn(struct work_struct *work_not_used)
1104 struct acpi_device *adev;
1106 mutex_lock(&acpi_device_del_lock);
1108 if (list_empty(&acpi_device_del_list)) {
1109 mutex_unlock(&acpi_device_del_lock);
1112 adev = list_first_entry(&acpi_device_del_list,
1113 struct acpi_device, del_list);
1114 list_del(&adev->del_list);
1116 mutex_unlock(&acpi_device_del_lock);
1118 acpi_device_del(adev);
1120 * Drop references to all power resources that might have been
1121 * used by the device.
1123 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
1124 put_device(&adev->dev);
1129 * acpi_scan_drop_device - Drop an ACPI device object.
1130 * @handle: Handle of an ACPI namespace node, not used.
1131 * @context: Address of the ACPI device object to drop.
1133 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
1134 * namespace node the device object pointed to by @context is attached to.
1136 * The unregistration is carried out asynchronously to avoid running
1137 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
1138 * ensure the correct ordering (the device objects must be unregistered in the
1139 * same order in which the corresponding namespace nodes are deleted).
1141 static void acpi_scan_drop_device(acpi_handle handle, void *context)
1143 static DECLARE_WORK(work, acpi_device_del_work_fn);
1144 struct acpi_device *adev = context;
1146 mutex_lock(&acpi_device_del_lock);
1149 * Use the ACPI hotplug workqueue which is ordered, so this work item
1150 * won't run after any hotplug work items submitted subsequently. That
1151 * prevents attempts to register device objects identical to those being
1152 * deleted from happening concurrently (such attempts result from
1153 * hotplug events handled via the ACPI hotplug workqueue). It also will
1154 * run after all of the work items submitted previosuly, which helps
1155 * those work items to ensure that they are not accessing stale device
1158 if (list_empty(&acpi_device_del_list))
1159 acpi_queue_hotplug_work(&work);
1161 list_add_tail(&adev->del_list, &acpi_device_del_list);
1162 /* Make acpi_ns_validate_handle() return NULL for this handle. */
1163 adev->handle = INVALID_ACPI_HANDLE;
1165 mutex_unlock(&acpi_device_del_lock);
1168 static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device,
1169 void (*callback)(void *))
1176 status = acpi_get_data_full(handle, acpi_scan_drop_device,
1177 (void **)device, callback);
1178 if (ACPI_FAILURE(status) || !*device) {
1179 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
1186 int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
1188 return acpi_get_device_data(handle, device, NULL);
1190 EXPORT_SYMBOL(acpi_bus_get_device);
1192 static void get_acpi_device(void *dev)
1195 get_device(&((struct acpi_device *)dev)->dev);
1198 struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
1200 struct acpi_device *adev = NULL;
1202 acpi_get_device_data(handle, &adev, get_acpi_device);
1206 void acpi_bus_put_acpi_device(struct acpi_device *adev)
1208 put_device(&adev->dev);
1211 int acpi_device_add(struct acpi_device *device,
1212 void (*release)(struct device *))
1215 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
1218 if (device->handle) {
1221 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
1223 if (ACPI_FAILURE(status)) {
1224 acpi_handle_err(device->handle,
1225 "Unable to attach device data\n");
1233 * Link this device to its parent and siblings.
1235 INIT_LIST_HEAD(&device->children);
1236 INIT_LIST_HEAD(&device->node);
1237 INIT_LIST_HEAD(&device->wakeup_list);
1238 INIT_LIST_HEAD(&device->physical_node_list);
1239 INIT_LIST_HEAD(&device->del_list);
1240 mutex_init(&device->physical_node_lock);
1242 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1244 pr_err(PREFIX "Memory allocation error\n");
1249 mutex_lock(&acpi_device_lock);
1251 * Find suitable bus_id and instance number in acpi_bus_id_list
1252 * If failed, create one and link it into acpi_bus_id_list
1254 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
1255 if (!strcmp(acpi_device_bus_id->bus_id,
1256 acpi_device_hid(device))) {
1257 acpi_device_bus_id->instance_no++;
1264 acpi_device_bus_id = new_bus_id;
1265 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
1266 acpi_device_bus_id->instance_no = 0;
1267 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1269 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
1272 list_add_tail(&device->node, &device->parent->children);
1274 if (device->wakeup.flags.valid)
1275 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
1276 mutex_unlock(&acpi_device_lock);
1279 device->dev.parent = &device->parent->dev;
1280 device->dev.bus = &acpi_bus_type;
1281 device->dev.release = release;
1282 result = device_add(&device->dev);
1284 dev_err(&device->dev, "Error registering device\n");
1288 result = acpi_device_setup_files(device);
1290 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1291 dev_name(&device->dev));
1296 mutex_lock(&acpi_device_lock);
1298 list_del(&device->node);
1299 list_del(&device->wakeup_list);
1300 mutex_unlock(&acpi_device_lock);
1303 acpi_detach_data(device->handle, acpi_scan_drop_device);
1307 /* --------------------------------------------------------------------------
1309 -------------------------------------------------------------------------- */
1311 * acpi_bus_register_driver - register a driver with the ACPI bus
1312 * @driver: driver being registered
1314 * Registers a driver with the ACPI bus. Searches the namespace for all
1315 * devices that match the driver's criteria and binds. Returns zero for
1316 * success or a negative error status for failure.
1318 int acpi_bus_register_driver(struct acpi_driver *driver)
1324 driver->drv.name = driver->name;
1325 driver->drv.bus = &acpi_bus_type;
1326 driver->drv.owner = driver->owner;
1328 ret = driver_register(&driver->drv);
1332 EXPORT_SYMBOL(acpi_bus_register_driver);
1335 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
1336 * @driver: driver to unregister
1338 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1339 * devices that match the driver's criteria and unbinds.
1341 void acpi_bus_unregister_driver(struct acpi_driver *driver)
1343 driver_unregister(&driver->drv);
1346 EXPORT_SYMBOL(acpi_bus_unregister_driver);
1348 /* --------------------------------------------------------------------------
1350 -------------------------------------------------------------------------- */
1351 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1353 struct acpi_device *device = NULL;
1357 * Fixed hardware devices do not appear in the namespace and do not
1358 * have handles, but we fabricate acpi_devices for them, so we have
1359 * to deal with them specially.
1365 status = acpi_get_parent(handle, &handle);
1366 if (ACPI_FAILURE(status))
1367 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1368 } while (acpi_bus_get_device(handle, &device));
1373 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1377 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1378 union acpi_object *obj;
1380 status = acpi_get_handle(handle, "_EJD", &tmp);
1381 if (ACPI_FAILURE(status))
1384 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1385 if (ACPI_SUCCESS(status)) {
1386 obj = buffer.pointer;
1387 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1389 kfree(buffer.pointer);
1393 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1395 static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1396 struct acpi_device_wakeup *wakeup)
1398 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1399 union acpi_object *package = NULL;
1400 union acpi_object *element = NULL;
1407 INIT_LIST_HEAD(&wakeup->resources);
1410 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1411 if (ACPI_FAILURE(status)) {
1412 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
1416 package = (union acpi_object *)buffer.pointer;
1418 if (!package || package->package.count < 2)
1421 element = &(package->package.elements[0]);
1425 if (element->type == ACPI_TYPE_PACKAGE) {
1426 if ((element->package.count < 2) ||
1427 (element->package.elements[0].type !=
1428 ACPI_TYPE_LOCAL_REFERENCE)
1429 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
1432 wakeup->gpe_device =
1433 element->package.elements[0].reference.handle;
1434 wakeup->gpe_number =
1435 (u32) element->package.elements[1].integer.value;
1436 } else if (element->type == ACPI_TYPE_INTEGER) {
1437 wakeup->gpe_device = NULL;
1438 wakeup->gpe_number = element->integer.value;
1443 element = &(package->package.elements[1]);
1444 if (element->type != ACPI_TYPE_INTEGER)
1447 wakeup->sleep_state = element->integer.value;
1449 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1453 if (!list_empty(&wakeup->resources)) {
1456 err = acpi_power_wakeup_list_init(&wakeup->resources,
1459 acpi_handle_warn(handle, "Retrieving current states "
1460 "of wakeup power resources failed\n");
1461 acpi_power_resources_list_free(&wakeup->resources);
1464 if (sleep_state < wakeup->sleep_state) {
1465 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1466 "(S%d) by S%d from power resources\n",
1467 (int)wakeup->sleep_state, sleep_state);
1468 wakeup->sleep_state = sleep_state;
1473 kfree(buffer.pointer);
1477 static void acpi_wakeup_gpe_init(struct acpi_device *device)
1479 struct acpi_device_id button_device_ids[] = {
1485 struct acpi_device_wakeup *wakeup = &device->wakeup;
1487 acpi_event_status event_status;
1489 wakeup->flags.notifier_present = 0;
1491 /* Power button, Lid switch always enable wakeup */
1492 if (!acpi_match_device_ids(device, button_device_ids)) {
1493 wakeup->flags.run_wake = 1;
1494 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1495 /* Do not use Lid/sleep button for S5 wakeup */
1496 if (wakeup->sleep_state == ACPI_STATE_S5)
1497 wakeup->sleep_state = ACPI_STATE_S4;
1499 acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
1500 device_set_wakeup_capable(&device->dev, true);
1504 acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
1505 wakeup->gpe_number);
1506 status = acpi_get_gpe_status(wakeup->gpe_device, wakeup->gpe_number,
1508 if (ACPI_FAILURE(status))
1511 wakeup->flags.run_wake = !!(event_status & ACPI_EVENT_FLAG_HAS_HANDLER);
1514 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1518 /* Presence of _PRW indicates wake capable */
1519 if (!acpi_has_method(device->handle, "_PRW"))
1522 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1525 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
1529 device->wakeup.flags.valid = 1;
1530 device->wakeup.prepare_count = 0;
1531 acpi_wakeup_gpe_init(device);
1532 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1533 * system for the ACPI device with the _PRW object.
1534 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1535 * So it is necessary to call _DSW object first. Only when it is not
1536 * present will the _PSW object used.
1538 err = acpi_device_sleep_wake(device, 0, 0, 0);
1540 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1541 "error in _DSW or _PSW evaluation\n"));
1544 static void acpi_bus_init_power_state(struct acpi_device *device, int state)
1546 struct acpi_device_power_state *ps = &device->power.states[state];
1547 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1548 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1551 INIT_LIST_HEAD(&ps->resources);
1553 /* Evaluate "_PRx" to get referenced power resources */
1554 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1555 if (ACPI_SUCCESS(status)) {
1556 union acpi_object *package = buffer.pointer;
1558 if (buffer.length && package
1559 && package->type == ACPI_TYPE_PACKAGE
1560 && package->package.count) {
1561 int err = acpi_extract_power_resources(package, 0,
1564 device->power.flags.power_resources = 1;
1566 ACPI_FREE(buffer.pointer);
1569 /* Evaluate "_PSx" to see if we can do explicit sets */
1571 if (acpi_has_method(device->handle, pathname))
1572 ps->flags.explicit_set = 1;
1575 * State is valid if there are means to put the device into it.
1576 * D3hot is only valid if _PR3 present.
1578 if (!list_empty(&ps->resources)
1579 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1580 ps->flags.valid = 1;
1581 ps->flags.os_accessible = 1;
1584 ps->power = -1; /* Unknown - driver assigned */
1585 ps->latency = -1; /* Unknown - driver assigned */
1588 static void acpi_bus_get_power_flags(struct acpi_device *device)
1592 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1593 if (!acpi_has_method(device->handle, "_PS0") &&
1594 !acpi_has_method(device->handle, "_PR0"))
1597 device->flags.power_manageable = 1;
1600 * Power Management Flags
1602 if (acpi_has_method(device->handle, "_PSC"))
1603 device->power.flags.explicit_get = 1;
1605 if (acpi_has_method(device->handle, "_IRC"))
1606 device->power.flags.inrush_current = 1;
1608 if (acpi_has_method(device->handle, "_DSW"))
1609 device->power.flags.dsw_present = 1;
1612 * Enumerate supported power management states
1614 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1615 acpi_bus_init_power_state(device, i);
1617 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1619 /* Set defaults for D0 and D3 states (always valid) */
1620 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1621 device->power.states[ACPI_STATE_D0].power = 100;
1622 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1623 device->power.states[ACPI_STATE_D3_COLD].power = 0;
1625 /* Set D3cold's explicit_set flag if _PS3 exists. */
1626 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1627 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1629 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1630 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1631 device->power.flags.power_resources)
1632 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1634 if (acpi_bus_init_power(device)) {
1635 acpi_free_power_resources_lists(device);
1636 device->flags.power_manageable = 0;
1640 static void acpi_bus_get_flags(struct acpi_device *device)
1642 /* Presence of _STA indicates 'dynamic_status' */
1643 if (acpi_has_method(device->handle, "_STA"))
1644 device->flags.dynamic_status = 1;
1646 /* Presence of _RMV indicates 'removable' */
1647 if (acpi_has_method(device->handle, "_RMV"))
1648 device->flags.removable = 1;
1650 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1651 if (acpi_has_method(device->handle, "_EJD") ||
1652 acpi_has_method(device->handle, "_EJ0"))
1653 device->flags.ejectable = 1;
1656 static void acpi_device_get_busid(struct acpi_device *device)
1658 char bus_id[5] = { '?', 0 };
1659 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1665 * The device's Bus ID is simply the object name.
1666 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1668 if (ACPI_IS_ROOT_DEVICE(device)) {
1669 strcpy(device->pnp.bus_id, "ACPI");
1673 switch (device->device_type) {
1674 case ACPI_BUS_TYPE_POWER_BUTTON:
1675 strcpy(device->pnp.bus_id, "PWRF");
1677 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1678 strcpy(device->pnp.bus_id, "SLPF");
1681 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1682 /* Clean up trailing underscores (if any) */
1683 for (i = 3; i > 1; i--) {
1684 if (bus_id[i] == '_')
1689 strcpy(device->pnp.bus_id, bus_id);
1695 * acpi_ata_match - see if an acpi object is an ATA device
1697 * If an acpi object has one of the ACPI ATA methods defined,
1698 * then we can safely call it an ATA device.
1700 bool acpi_ata_match(acpi_handle handle)
1702 return acpi_has_method(handle, "_GTF") ||
1703 acpi_has_method(handle, "_GTM") ||
1704 acpi_has_method(handle, "_STM") ||
1705 acpi_has_method(handle, "_SDD");
1709 * acpi_bay_match - see if an acpi object is an ejectable driver bay
1711 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1712 * then we can safely call it an ejectable drive bay
1714 bool acpi_bay_match(acpi_handle handle)
1716 acpi_handle phandle;
1718 if (!acpi_has_method(handle, "_EJ0"))
1720 if (acpi_ata_match(handle))
1722 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1725 return acpi_ata_match(phandle);
1728 bool acpi_device_is_battery(struct acpi_device *adev)
1730 struct acpi_hardware_id *hwid;
1732 list_for_each_entry(hwid, &adev->pnp.ids, list)
1733 if (!strcmp("PNP0C0A", hwid->id))
1739 static bool is_ejectable_bay(struct acpi_device *adev)
1741 acpi_handle handle = adev->handle;
1743 if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
1746 return acpi_bay_match(handle);
1750 * acpi_dock_match - see if an acpi object has a _DCK method
1752 bool acpi_dock_match(acpi_handle handle)
1754 return acpi_has_method(handle, "_DCK");
1757 const char *acpi_device_hid(struct acpi_device *device)
1759 struct acpi_hardware_id *hid;
1761 if (list_empty(&device->pnp.ids))
1764 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1767 EXPORT_SYMBOL(acpi_device_hid);
1769 static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
1771 struct acpi_hardware_id *id;
1773 id = kmalloc(sizeof(*id), GFP_KERNEL);
1777 id->id = kstrdup(dev_id, GFP_KERNEL);
1783 list_add_tail(&id->list, &pnp->ids);
1784 pnp->type.hardware_id = 1;
1788 * Old IBM workstations have a DSDT bug wherein the SMBus object
1789 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1790 * prefix. Work around this.
1792 static bool acpi_ibm_smbus_match(acpi_handle handle)
1794 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1795 struct acpi_buffer path = { sizeof(node_name), node_name };
1797 if (!dmi_name_in_vendors("IBM"))
1800 /* Look for SMBS object */
1801 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1802 strcmp("SMBS", path.pointer))
1805 /* Does it have the necessary (but misnamed) methods? */
1806 if (acpi_has_method(handle, "SBI") &&
1807 acpi_has_method(handle, "SBR") &&
1808 acpi_has_method(handle, "SBW"))
1814 static bool acpi_object_is_system_bus(acpi_handle handle)
1818 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1821 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1828 static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1832 struct acpi_device_info *info;
1833 struct acpi_pnp_device_id_list *cid_list;
1836 switch (device_type) {
1837 case ACPI_BUS_TYPE_DEVICE:
1838 if (handle == ACPI_ROOT_OBJECT) {
1839 acpi_add_id(pnp, ACPI_SYSTEM_HID);
1843 status = acpi_get_object_info(handle, &info);
1844 if (ACPI_FAILURE(status)) {
1845 pr_err(PREFIX "%s: Error reading device info\n",
1850 if (info->valid & ACPI_VALID_HID) {
1851 acpi_add_id(pnp, info->hardware_id.string);
1852 pnp->type.platform_id = 1;
1854 if (info->valid & ACPI_VALID_CID) {
1855 cid_list = &info->compatible_id_list;
1856 for (i = 0; i < cid_list->count; i++)
1857 acpi_add_id(pnp, cid_list->ids[i].string);
1859 if (info->valid & ACPI_VALID_ADR) {
1860 pnp->bus_address = info->address;
1861 pnp->type.bus_address = 1;
1863 if (info->valid & ACPI_VALID_UID)
1864 pnp->unique_id = kstrdup(info->unique_id.string,
1870 * Some devices don't reliably have _HIDs & _CIDs, so add
1871 * synthetic HIDs to make sure drivers can find them.
1873 if (acpi_is_video_device(handle))
1874 acpi_add_id(pnp, ACPI_VIDEO_HID);
1875 else if (acpi_bay_match(handle))
1876 acpi_add_id(pnp, ACPI_BAY_HID);
1877 else if (acpi_dock_match(handle))
1878 acpi_add_id(pnp, ACPI_DOCK_HID);
1879 else if (acpi_ibm_smbus_match(handle))
1880 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1881 else if (list_empty(&pnp->ids) &&
1882 acpi_object_is_system_bus(handle)) {
1883 /* \_SB, \_TZ, LNXSYBUS */
1884 acpi_add_id(pnp, ACPI_BUS_HID);
1885 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1886 strcpy(pnp->device_class, ACPI_BUS_CLASS);
1890 case ACPI_BUS_TYPE_POWER:
1891 acpi_add_id(pnp, ACPI_POWER_HID);
1893 case ACPI_BUS_TYPE_PROCESSOR:
1894 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
1896 case ACPI_BUS_TYPE_THERMAL:
1897 acpi_add_id(pnp, ACPI_THERMAL_HID);
1899 case ACPI_BUS_TYPE_POWER_BUTTON:
1900 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
1902 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1903 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
1908 void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1910 struct acpi_hardware_id *id, *tmp;
1912 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1916 kfree(pnp->unique_id);
1919 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1920 int type, unsigned long long sta)
1922 INIT_LIST_HEAD(&device->pnp.ids);
1923 device->device_type = type;
1924 device->handle = handle;
1925 device->parent = acpi_bus_get_parent(handle);
1926 acpi_set_device_status(device, sta);
1927 acpi_device_get_busid(device);
1928 acpi_set_pnp_ids(handle, &device->pnp, type);
1929 acpi_bus_get_flags(device);
1930 device->flags.match_driver = false;
1931 device->flags.initialized = true;
1932 device->flags.visited = false;
1933 device_initialize(&device->dev);
1934 dev_set_uevent_suppress(&device->dev, true);
1937 void acpi_device_add_finalize(struct acpi_device *device)
1939 dev_set_uevent_suppress(&device->dev, false);
1940 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1943 static int acpi_add_single_object(struct acpi_device **child,
1944 acpi_handle handle, int type,
1945 unsigned long long sta)
1948 struct acpi_device *device;
1949 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1951 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1953 printk(KERN_ERR PREFIX "Memory allocation error\n");
1957 acpi_init_device_object(device, handle, type, sta);
1958 acpi_bus_get_power_flags(device);
1959 acpi_bus_get_wakeup_device_flags(device);
1961 result = acpi_device_add(device, acpi_device_release);
1963 acpi_device_release(&device->dev);
1967 acpi_power_add_remove_device(device, true);
1968 acpi_device_add_finalize(device);
1969 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1970 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1971 dev_name(&device->dev), (char *) buffer.pointer,
1972 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1973 kfree(buffer.pointer);
1978 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1979 unsigned long long *sta)
1982 acpi_object_type acpi_type;
1984 status = acpi_get_type(handle, &acpi_type);
1985 if (ACPI_FAILURE(status))
1988 switch (acpi_type) {
1989 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1990 case ACPI_TYPE_DEVICE:
1991 *type = ACPI_BUS_TYPE_DEVICE;
1992 status = acpi_bus_get_status_handle(handle, sta);
1993 if (ACPI_FAILURE(status))
1996 case ACPI_TYPE_PROCESSOR:
1997 *type = ACPI_BUS_TYPE_PROCESSOR;
1998 status = acpi_bus_get_status_handle(handle, sta);
1999 if (ACPI_FAILURE(status))
2002 case ACPI_TYPE_THERMAL:
2003 *type = ACPI_BUS_TYPE_THERMAL;
2004 *sta = ACPI_STA_DEFAULT;
2006 case ACPI_TYPE_POWER:
2007 *type = ACPI_BUS_TYPE_POWER;
2008 *sta = ACPI_STA_DEFAULT;
2017 bool acpi_device_is_present(struct acpi_device *adev)
2019 if (adev->status.present || adev->status.functional)
2022 adev->flags.initialized = false;
2026 static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
2028 const struct acpi_device_id **matchid)
2030 const struct acpi_device_id *devid;
2033 return handler->match(idstr, matchid);
2035 for (devid = handler->ids; devid->id[0]; devid++)
2036 if (!strcmp((char *)devid->id, idstr)) {
2046 static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
2047 const struct acpi_device_id **matchid)
2049 struct acpi_scan_handler *handler;
2051 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
2052 if (acpi_scan_handler_matching(handler, idstr, matchid))
2058 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
2060 if (!!hotplug->enabled == !!val)
2063 mutex_lock(&acpi_scan_lock);
2065 hotplug->enabled = val;
2067 mutex_unlock(&acpi_scan_lock);
2070 static void acpi_scan_init_hotplug(struct acpi_device *adev)
2072 struct acpi_hardware_id *hwid;
2074 if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
2075 acpi_dock_add(adev);
2078 list_for_each_entry(hwid, &adev->pnp.ids, list) {
2079 struct acpi_scan_handler *handler;
2081 handler = acpi_scan_match_handler(hwid->id, NULL);
2083 adev->flags.hotplug_notify = true;
2089 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
2090 void *not_used, void **return_value)
2092 struct acpi_device *device = NULL;
2094 unsigned long long sta;
2097 acpi_bus_get_device(handle, &device);
2101 result = acpi_bus_type_and_status(handle, &type, &sta);
2105 if (type == ACPI_BUS_TYPE_POWER) {
2106 acpi_add_power_resource(handle);
2110 acpi_add_single_object(&device, handle, type, sta);
2112 return AE_CTRL_DEPTH;
2114 acpi_scan_init_hotplug(device);
2118 *return_value = device;
2123 static int acpi_check_spi_i2c_slave(struct acpi_resource *ares, void *data)
2125 bool *is_spi_i2c_slave_p = data;
2127 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
2131 * devices that are connected to UART still need to be enumerated to
2134 if (ares->data.common_serial_bus.type != ACPI_RESOURCE_SERIAL_TYPE_UART)
2135 *is_spi_i2c_slave_p = true;
2137 /* no need to do more checking */
2141 static void acpi_default_enumeration(struct acpi_device *device)
2143 struct list_head resource_list;
2144 bool is_spi_i2c_slave = false;
2146 if (!device->pnp.type.platform_id || device->handler)
2150 * Do not enemerate SPI/I2C slaves as they will be enuerated by their
2151 * respective parents.
2153 INIT_LIST_HEAD(&resource_list);
2154 acpi_dev_get_resources(device, &resource_list, acpi_check_spi_i2c_slave,
2156 acpi_dev_free_resource_list(&resource_list);
2157 if (!is_spi_i2c_slave)
2158 acpi_create_platform_device(device);
2161 static int acpi_scan_attach_handler(struct acpi_device *device)
2163 struct acpi_hardware_id *hwid;
2166 list_for_each_entry(hwid, &device->pnp.ids, list) {
2167 const struct acpi_device_id *devid;
2168 struct acpi_scan_handler *handler;
2170 handler = acpi_scan_match_handler(hwid->id, &devid);
2172 if (!handler->attach) {
2173 device->pnp.type.platform_id = 0;
2176 device->handler = handler;
2177 ret = handler->attach(device, devid);
2181 device->handler = NULL;
2187 acpi_default_enumeration(device);
2192 static void acpi_bus_attach(struct acpi_device *device)
2194 struct acpi_device *child;
2198 if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
2199 register_dock_dependent_device(device, ejd);
2201 acpi_bus_get_status(device);
2202 /* Skip devices that are not present. */
2203 if (!acpi_device_is_present(device)) {
2204 device->flags.visited = false;
2207 if (device->handler)
2210 if (!device->flags.initialized) {
2211 acpi_bus_update_power(device, NULL);
2212 device->flags.initialized = true;
2214 device->flags.visited = false;
2215 ret = acpi_scan_attach_handler(device);
2219 device->flags.match_driver = true;
2221 ret = device_attach(&device->dev);
2225 device->flags.visited = true;
2228 list_for_each_entry(child, &device->children, node)
2229 acpi_bus_attach(child);
2231 if (device->handler && device->handler->hotplug.notify_online)
2232 device->handler->hotplug.notify_online(device);
2236 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2237 * @handle: Root of the namespace scope to scan.
2239 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2242 * If no devices were found, -ENODEV is returned, but it does not mean that
2243 * there has been a real error. There just have been no suitable ACPI objects
2244 * in the table trunk from which the kernel could create a device and add an
2245 * appropriate driver.
2247 * Must be called under acpi_scan_lock.
2249 int acpi_bus_scan(acpi_handle handle)
2251 void *device = NULL;
2253 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
2254 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2255 acpi_bus_check_add, NULL, NULL, &device);
2258 acpi_bus_attach(device);
2263 EXPORT_SYMBOL(acpi_bus_scan);
2266 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2267 * @adev: Root of the ACPI namespace scope to walk.
2269 * Must be called under acpi_scan_lock.
2271 void acpi_bus_trim(struct acpi_device *adev)
2273 struct acpi_scan_handler *handler = adev->handler;
2274 struct acpi_device *child;
2276 list_for_each_entry_reverse(child, &adev->children, node)
2277 acpi_bus_trim(child);
2279 adev->flags.match_driver = false;
2281 if (handler->detach)
2282 handler->detach(adev);
2284 adev->handler = NULL;
2286 device_release_driver(&adev->dev);
2289 * Most likely, the device is going away, so put it into D3cold before
2292 acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2293 adev->flags.initialized = false;
2294 adev->flags.visited = false;
2296 EXPORT_SYMBOL_GPL(acpi_bus_trim);
2298 static int acpi_bus_scan_fixed(void)
2303 * Enumerate all fixed-feature devices.
2305 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2306 struct acpi_device *device = NULL;
2308 result = acpi_add_single_object(&device, NULL,
2309 ACPI_BUS_TYPE_POWER_BUTTON,
2314 device->flags.match_driver = true;
2315 result = device_attach(&device->dev);
2319 device_init_wakeup(&device->dev, true);
2322 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2323 struct acpi_device *device = NULL;
2325 result = acpi_add_single_object(&device, NULL,
2326 ACPI_BUS_TYPE_SLEEP_BUTTON,
2331 device->flags.match_driver = true;
2332 result = device_attach(&device->dev);
2335 return result < 0 ? result : 0;
2338 int __init acpi_scan_init(void)
2342 result = bus_register(&acpi_bus_type);
2344 /* We don't want to quit even if we failed to add suspend/resume */
2345 printk(KERN_ERR PREFIX "Could not register bus type\n");
2348 acpi_pci_root_init();
2349 acpi_pci_link_init();
2350 acpi_processor_init();
2352 acpi_cmos_rtc_init();
2353 acpi_container_init();
2354 acpi_memory_hotplug_init();
2356 acpi_int340x_thermal_init();
2358 mutex_lock(&acpi_scan_lock);
2360 * Enumerate devices in the ACPI namespace.
2362 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
2366 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
2370 /* Fixed feature devices do not exist on HW-reduced platform */
2371 if (!acpi_gbl_reduced_hardware) {
2372 result = acpi_bus_scan_fixed();
2374 acpi_detach_data(acpi_root->handle,
2375 acpi_scan_drop_device);
2376 acpi_device_del(acpi_root);
2377 put_device(&acpi_root->dev);
2382 acpi_update_all_gpes();
2385 mutex_unlock(&acpi_scan_lock);