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 * create_pnp_modalias - Create hid/cid(s) string for modalias and uevent
119 * @acpi_dev: ACPI device object.
120 * @modalias: Buffer to print into.
121 * @size: Size of the buffer.
123 * Creates hid/cid(s) string needed for modalias and uevent
124 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
125 * char *modalias: "acpi:IBM0001:ACPI0001"
126 * Return: 0: no _HID and no _CID
127 * -EINVAL: output error
128 * -ENOMEM: output is truncated
130 static int create_pnp_modalias(struct acpi_device
*acpi_dev
, char *modalias
,
135 struct acpi_hardware_id
*id
;
138 * Since we skip ACPI_DT_NAMESPACE_HID from the modalias below, 0 should
139 * be returned if ACPI_DT_NAMESPACE_HID is the only ACPI/PNP ID in the
143 list_for_each_entry(id
, &acpi_dev
->pnp
.ids
, list
)
144 if (strcmp(id
->id
, ACPI_DT_NAMESPACE_HID
))
150 len
= snprintf(modalias
, size
, "acpi:");
156 list_for_each_entry(id
, &acpi_dev
->pnp
.ids
, list
) {
157 if (!strcmp(id
->id
, ACPI_DT_NAMESPACE_HID
))
160 count
= snprintf(&modalias
[len
], size
, "%s:", id
->id
);
170 modalias
[len
] = '\0';
175 * create_of_modalias - Creates DT compatible string for modalias and uevent
176 * @acpi_dev: ACPI device object.
177 * @modalias: Buffer to print into.
178 * @size: Size of the buffer.
180 * Expose DT compatible modalias as of:NnameTCcompatible. This function should
181 * only be called for devices having ACPI_DT_NAMESPACE_HID in their list of
184 static int create_of_modalias(struct acpi_device
*acpi_dev
, char *modalias
,
187 struct acpi_buffer buf
= { ACPI_ALLOCATE_BUFFER
};
188 const union acpi_object
*of_compatible
, *obj
;
193 acpi_get_name(acpi_dev
->handle
, ACPI_SINGLE_NAME
, &buf
);
194 /* DT strings are all in lower case */
195 for (c
= buf
.pointer
; *c
!= '\0'; c
++)
198 len
= snprintf(modalias
, size
, "of:N%sT", (char *)buf
.pointer
);
199 ACPI_FREE(buf
.pointer
);
204 of_compatible
= acpi_dev
->data
.of_compatible
;
205 if (of_compatible
->type
== ACPI_TYPE_PACKAGE
) {
206 nval
= of_compatible
->package
.count
;
207 obj
= of_compatible
->package
.elements
;
208 } else { /* Must be ACPI_TYPE_STRING. */
212 for (i
= 0; i
< nval
; i
++, obj
++) {
213 count
= snprintf(&modalias
[len
], size
, "C%s",
214 obj
->string
.pointer
);
224 modalias
[len
] = '\0';
229 * acpi_companion_match() - Can we match via ACPI companion device
230 * @dev: Device in question
232 * Check if the given device has an ACPI companion and if that companion has
233 * a valid list of PNP IDs, and if the device is the first (primary) physical
234 * device associated with it. Return the companion pointer if that's the case
237 * If multiple physical devices are attached to a single ACPI companion, we need
238 * to be careful. The usage scenario for this kind of relationship is that all
239 * of the physical devices in question use resources provided by the ACPI
240 * companion. A typical case is an MFD device where all the sub-devices share
241 * the parent's ACPI companion. In such cases we can only allow the primary
242 * (first) physical device to be matched with the help of the companion's PNP
245 * Additional physical devices sharing the ACPI companion can still use
246 * resources available from it but they will be matched normally using functions
247 * provided by their bus types (and analogously for their modalias).
249 static struct acpi_device
*acpi_companion_match(const struct device
*dev
)
251 struct acpi_device
*adev
;
252 struct mutex
*physical_node_lock
;
254 adev
= ACPI_COMPANION(dev
);
258 if (list_empty(&adev
->pnp
.ids
))
261 physical_node_lock
= &adev
->physical_node_lock
;
262 mutex_lock(physical_node_lock
);
263 if (list_empty(&adev
->physical_node_list
)) {
266 const struct acpi_device_physical_node
*node
;
268 node
= list_first_entry(&adev
->physical_node_list
,
269 struct acpi_device_physical_node
, node
);
270 if (node
->dev
!= dev
)
273 mutex_unlock(physical_node_lock
);
278 static int __acpi_device_uevent_modalias(struct acpi_device
*adev
,
279 struct kobj_uevent_env
*env
)
286 if (list_empty(&adev
->pnp
.ids
))
289 if (add_uevent_var(env
, "MODALIAS="))
292 len
= create_pnp_modalias(adev
, &env
->buf
[env
->buflen
- 1],
293 sizeof(env
->buf
) - env
->buflen
);
298 if (!adev
->data
.of_compatible
)
301 if (len
> 0 && add_uevent_var(env
, "MODALIAS="))
304 len
= create_of_modalias(adev
, &env
->buf
[env
->buflen
- 1],
305 sizeof(env
->buf
) - env
->buflen
);
315 * Creates uevent modalias field for ACPI enumerated devices.
316 * Because the other buses does not support ACPI HIDs & CIDs.
317 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
318 * "acpi:IBM0001:ACPI0001"
320 int acpi_device_uevent_modalias(struct device
*dev
, struct kobj_uevent_env
*env
)
322 return __acpi_device_uevent_modalias(acpi_companion_match(dev
), env
);
324 EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias
);
326 static int __acpi_device_modalias(struct acpi_device
*adev
, char *buf
, int size
)
333 if (list_empty(&adev
->pnp
.ids
))
336 len
= create_pnp_modalias(adev
, buf
, size
- 1);
339 } else if (len
> 0) {
343 if (!adev
->data
.of_compatible
)
346 count
= create_of_modalias(adev
, buf
+ len
, size
- 1);
349 } else if (count
> 0) {
358 * Creates modalias sysfs attribute for ACPI enumerated devices.
359 * Because the other buses does not support ACPI HIDs & CIDs.
360 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
361 * "acpi:IBM0001:ACPI0001"
363 int acpi_device_modalias(struct device
*dev
, char *buf
, int size
)
365 return __acpi_device_modalias(acpi_companion_match(dev
), buf
, size
);
367 EXPORT_SYMBOL_GPL(acpi_device_modalias
);
370 acpi_device_modalias_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
371 return __acpi_device_modalias(to_acpi_device(dev
), buf
, 1024);
373 static DEVICE_ATTR(modalias
, 0444, acpi_device_modalias_show
, NULL
);
375 bool acpi_scan_is_offline(struct acpi_device
*adev
, bool uevent
)
377 struct acpi_device_physical_node
*pn
;
381 * acpi_container_offline() calls this for all of the container's
382 * children under the container's physical_node_lock lock.
384 mutex_lock_nested(&adev
->physical_node_lock
, SINGLE_DEPTH_NESTING
);
386 list_for_each_entry(pn
, &adev
->physical_node_list
, node
)
387 if (device_supports_offline(pn
->dev
) && !pn
->dev
->offline
) {
389 kobject_uevent(&pn
->dev
->kobj
, KOBJ_CHANGE
);
395 mutex_unlock(&adev
->physical_node_lock
);
399 static acpi_status
acpi_bus_offline(acpi_handle handle
, u32 lvl
, void *data
,
402 struct acpi_device
*device
= NULL
;
403 struct acpi_device_physical_node
*pn
;
404 bool second_pass
= (bool)data
;
405 acpi_status status
= AE_OK
;
407 if (acpi_bus_get_device(handle
, &device
))
410 if (device
->handler
&& !device
->handler
->hotplug
.enabled
) {
411 *ret_p
= &device
->dev
;
415 mutex_lock(&device
->physical_node_lock
);
417 list_for_each_entry(pn
, &device
->physical_node_list
, node
) {
421 /* Skip devices offlined by the first pass. */
425 pn
->put_online
= false;
427 ret
= device_offline(pn
->dev
);
428 if (acpi_force_hot_remove
)
432 pn
->put_online
= !ret
;
442 mutex_unlock(&device
->physical_node_lock
);
447 static acpi_status
acpi_bus_online(acpi_handle handle
, u32 lvl
, void *data
,
450 struct acpi_device
*device
= NULL
;
451 struct acpi_device_physical_node
*pn
;
453 if (acpi_bus_get_device(handle
, &device
))
456 mutex_lock(&device
->physical_node_lock
);
458 list_for_each_entry(pn
, &device
->physical_node_list
, node
)
459 if (pn
->put_online
) {
460 device_online(pn
->dev
);
461 pn
->put_online
= false;
464 mutex_unlock(&device
->physical_node_lock
);
469 static int acpi_scan_try_to_offline(struct acpi_device
*device
)
471 acpi_handle handle
= device
->handle
;
472 struct device
*errdev
= NULL
;
476 * Carry out two passes here and ignore errors in the first pass,
477 * because if the devices in question are memory blocks and
478 * CONFIG_MEMCG is set, one of the blocks may hold data structures
479 * that the other blocks depend on, but it is not known in advance which
482 * If the first pass is successful, the second one isn't needed, though.
484 status
= acpi_walk_namespace(ACPI_TYPE_ANY
, handle
, ACPI_UINT32_MAX
,
485 NULL
, acpi_bus_offline
, (void *)false,
487 if (status
== AE_SUPPORT
) {
488 dev_warn(errdev
, "Offline disabled.\n");
489 acpi_walk_namespace(ACPI_TYPE_ANY
, handle
, ACPI_UINT32_MAX
,
490 acpi_bus_online
, NULL
, NULL
, NULL
);
493 acpi_bus_offline(handle
, 0, (void *)false, (void **)&errdev
);
496 acpi_walk_namespace(ACPI_TYPE_ANY
, handle
, ACPI_UINT32_MAX
,
497 NULL
, acpi_bus_offline
, (void *)true,
499 if (!errdev
|| acpi_force_hot_remove
)
500 acpi_bus_offline(handle
, 0, (void *)true,
503 if (errdev
&& !acpi_force_hot_remove
) {
504 dev_warn(errdev
, "Offline failed.\n");
505 acpi_bus_online(handle
, 0, NULL
, NULL
);
506 acpi_walk_namespace(ACPI_TYPE_ANY
, handle
,
507 ACPI_UINT32_MAX
, acpi_bus_online
,
515 static int acpi_scan_hot_remove(struct acpi_device
*device
)
517 acpi_handle handle
= device
->handle
;
518 unsigned long long sta
;
521 if (device
->handler
&& device
->handler
->hotplug
.demand_offline
522 && !acpi_force_hot_remove
) {
523 if (!acpi_scan_is_offline(device
, true))
526 int error
= acpi_scan_try_to_offline(device
);
531 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
532 "Hot-removing device %s...\n", dev_name(&device
->dev
)));
534 acpi_bus_trim(device
);
536 acpi_evaluate_lck(handle
, 0);
540 status
= acpi_evaluate_ej0(handle
);
541 if (status
== AE_NOT_FOUND
)
543 else if (ACPI_FAILURE(status
))
547 * Verify if eject was indeed successful. If not, log an error
548 * message. No need to call _OST since _EJ0 call was made OK.
550 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, &sta
);
551 if (ACPI_FAILURE(status
)) {
552 acpi_handle_warn(handle
,
553 "Status check after eject failed (0x%x)\n", status
);
554 } else if (sta
& ACPI_STA_DEVICE_ENABLED
) {
555 acpi_handle_warn(handle
,
556 "Eject incomplete - status 0x%llx\n", sta
);
562 static int acpi_scan_device_not_present(struct acpi_device
*adev
)
564 if (!acpi_device_enumerated(adev
)) {
565 dev_warn(&adev
->dev
, "Still not present\n");
572 static int acpi_scan_device_check(struct acpi_device
*adev
)
576 acpi_bus_get_status(adev
);
577 if (adev
->status
.present
|| adev
->status
.functional
) {
579 * This function is only called for device objects for which
580 * matching scan handlers exist. The only situation in which
581 * the scan handler is not attached to this device object yet
582 * is when the device has just appeared (either it wasn't
583 * present at all before or it was removed and then added
587 dev_warn(&adev
->dev
, "Already enumerated\n");
590 error
= acpi_bus_scan(adev
->handle
);
592 dev_warn(&adev
->dev
, "Namespace scan failure\n");
595 if (!adev
->handler
) {
596 dev_warn(&adev
->dev
, "Enumeration failure\n");
600 error
= acpi_scan_device_not_present(adev
);
605 static int acpi_scan_bus_check(struct acpi_device
*adev
)
607 struct acpi_scan_handler
*handler
= adev
->handler
;
608 struct acpi_device
*child
;
611 acpi_bus_get_status(adev
);
612 if (!(adev
->status
.present
|| adev
->status
.functional
)) {
613 acpi_scan_device_not_present(adev
);
616 if (handler
&& handler
->hotplug
.scan_dependent
)
617 return handler
->hotplug
.scan_dependent(adev
);
619 error
= acpi_bus_scan(adev
->handle
);
621 dev_warn(&adev
->dev
, "Namespace scan failure\n");
624 list_for_each_entry(child
, &adev
->children
, node
) {
625 error
= acpi_scan_bus_check(child
);
632 static int acpi_generic_hotplug_event(struct acpi_device
*adev
, u32 type
)
635 case ACPI_NOTIFY_BUS_CHECK
:
636 return acpi_scan_bus_check(adev
);
637 case ACPI_NOTIFY_DEVICE_CHECK
:
638 return acpi_scan_device_check(adev
);
639 case ACPI_NOTIFY_EJECT_REQUEST
:
640 case ACPI_OST_EC_OSPM_EJECT
:
641 if (adev
->handler
&& !adev
->handler
->hotplug
.enabled
) {
642 dev_info(&adev
->dev
, "Eject disabled\n");
645 acpi_evaluate_ost(adev
->handle
, ACPI_NOTIFY_EJECT_REQUEST
,
646 ACPI_OST_SC_EJECT_IN_PROGRESS
, NULL
);
647 return acpi_scan_hot_remove(adev
);
652 void acpi_device_hotplug(struct acpi_device
*adev
, u32 src
)
654 u32 ost_code
= ACPI_OST_SC_NON_SPECIFIC_FAILURE
;
657 lock_device_hotplug();
658 mutex_lock(&acpi_scan_lock
);
661 * The device object's ACPI handle cannot become invalid as long as we
662 * are holding acpi_scan_lock, but it might have become invalid before
663 * that lock was acquired.
665 if (adev
->handle
== INVALID_ACPI_HANDLE
)
668 if (adev
->flags
.is_dock_station
) {
669 error
= dock_notify(adev
, src
);
670 } else if (adev
->flags
.hotplug_notify
) {
671 error
= acpi_generic_hotplug_event(adev
, src
);
672 if (error
== -EPERM
) {
673 ost_code
= ACPI_OST_SC_EJECT_NOT_SUPPORTED
;
677 int (*notify
)(struct acpi_device
*, u32
);
679 acpi_lock_hp_context();
680 notify
= adev
->hp
? adev
->hp
->notify
: NULL
;
681 acpi_unlock_hp_context();
683 * There may be additional notify handlers for device objects
684 * without the .event() callback, so ignore them here.
687 error
= notify(adev
, src
);
692 ost_code
= ACPI_OST_SC_SUCCESS
;
695 acpi_evaluate_ost(adev
->handle
, src
, ost_code
, NULL
);
698 acpi_bus_put_acpi_device(adev
);
699 mutex_unlock(&acpi_scan_lock
);
700 unlock_device_hotplug();
703 static ssize_t
real_power_state_show(struct device
*dev
,
704 struct device_attribute
*attr
, char *buf
)
706 struct acpi_device
*adev
= to_acpi_device(dev
);
710 ret
= acpi_device_get_power(adev
, &state
);
714 return sprintf(buf
, "%s\n", acpi_power_state_string(state
));
717 static DEVICE_ATTR(real_power_state
, 0444, real_power_state_show
, NULL
);
719 static ssize_t
power_state_show(struct device
*dev
,
720 struct device_attribute
*attr
, char *buf
)
722 struct acpi_device
*adev
= to_acpi_device(dev
);
724 return sprintf(buf
, "%s\n", acpi_power_state_string(adev
->power
.state
));
727 static DEVICE_ATTR(power_state
, 0444, power_state_show
, NULL
);
730 acpi_eject_store(struct device
*d
, struct device_attribute
*attr
,
731 const char *buf
, size_t count
)
733 struct acpi_device
*acpi_device
= to_acpi_device(d
);
734 acpi_object_type not_used
;
737 if (!count
|| buf
[0] != '1')
740 if ((!acpi_device
->handler
|| !acpi_device
->handler
->hotplug
.enabled
)
741 && !acpi_device
->driver
)
744 status
= acpi_get_type(acpi_device
->handle
, ¬_used
);
745 if (ACPI_FAILURE(status
) || !acpi_device
->flags
.ejectable
)
748 get_device(&acpi_device
->dev
);
749 status
= acpi_hotplug_schedule(acpi_device
, ACPI_OST_EC_OSPM_EJECT
);
750 if (ACPI_SUCCESS(status
))
753 put_device(&acpi_device
->dev
);
754 acpi_evaluate_ost(acpi_device
->handle
, ACPI_OST_EC_OSPM_EJECT
,
755 ACPI_OST_SC_NON_SPECIFIC_FAILURE
, NULL
);
756 return status
== AE_NO_MEMORY
? -ENOMEM
: -EAGAIN
;
759 static DEVICE_ATTR(eject
, 0200, NULL
, acpi_eject_store
);
762 acpi_device_hid_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
763 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
765 return sprintf(buf
, "%s\n", acpi_device_hid(acpi_dev
));
767 static DEVICE_ATTR(hid
, 0444, acpi_device_hid_show
, NULL
);
769 static ssize_t
acpi_device_uid_show(struct device
*dev
,
770 struct device_attribute
*attr
, char *buf
)
772 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
774 return sprintf(buf
, "%s\n", acpi_dev
->pnp
.unique_id
);
776 static DEVICE_ATTR(uid
, 0444, acpi_device_uid_show
, NULL
);
778 static ssize_t
acpi_device_adr_show(struct device
*dev
,
779 struct device_attribute
*attr
, char *buf
)
781 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
783 return sprintf(buf
, "0x%08x\n",
784 (unsigned int)(acpi_dev
->pnp
.bus_address
));
786 static DEVICE_ATTR(adr
, 0444, acpi_device_adr_show
, NULL
);
789 acpi_device_path_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
790 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
791 struct acpi_buffer path
= {ACPI_ALLOCATE_BUFFER
, NULL
};
794 result
= acpi_get_name(acpi_dev
->handle
, ACPI_FULL_PATHNAME
, &path
);
798 result
= sprintf(buf
, "%s\n", (char*)path
.pointer
);
803 static DEVICE_ATTR(path
, 0444, acpi_device_path_show
, NULL
);
805 /* sysfs file that shows description text from the ACPI _STR method */
806 static ssize_t
description_show(struct device
*dev
,
807 struct device_attribute
*attr
,
809 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
812 if (acpi_dev
->pnp
.str_obj
== NULL
)
816 * The _STR object contains a Unicode identifier for a device.
817 * We need to convert to utf-8 so it can be displayed.
819 result
= utf16s_to_utf8s(
820 (wchar_t *)acpi_dev
->pnp
.str_obj
->buffer
.pointer
,
821 acpi_dev
->pnp
.str_obj
->buffer
.length
,
822 UTF16_LITTLE_ENDIAN
, buf
,
825 buf
[result
++] = '\n';
829 static DEVICE_ATTR(description
, 0444, description_show
, NULL
);
832 acpi_device_sun_show(struct device
*dev
, struct device_attribute
*attr
,
834 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
836 unsigned long long sun
;
838 status
= acpi_evaluate_integer(acpi_dev
->handle
, "_SUN", NULL
, &sun
);
839 if (ACPI_FAILURE(status
))
842 return sprintf(buf
, "%llu\n", sun
);
844 static DEVICE_ATTR(sun
, 0444, acpi_device_sun_show
, NULL
);
846 static ssize_t
status_show(struct device
*dev
, struct device_attribute
*attr
,
848 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
850 unsigned long long sta
;
852 status
= acpi_evaluate_integer(acpi_dev
->handle
, "_STA", NULL
, &sta
);
853 if (ACPI_FAILURE(status
))
856 return sprintf(buf
, "%llu\n", sta
);
858 static DEVICE_ATTR_RO(status
);
860 static int acpi_device_setup_files(struct acpi_device
*dev
)
862 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
867 * Devices gotten from FADT don't have a "path" attribute
870 result
= device_create_file(&dev
->dev
, &dev_attr_path
);
875 if (!list_empty(&dev
->pnp
.ids
)) {
876 result
= device_create_file(&dev
->dev
, &dev_attr_hid
);
880 result
= device_create_file(&dev
->dev
, &dev_attr_modalias
);
886 * If device has _STR, 'description' file is created
888 if (acpi_has_method(dev
->handle
, "_STR")) {
889 status
= acpi_evaluate_object(dev
->handle
, "_STR",
891 if (ACPI_FAILURE(status
))
892 buffer
.pointer
= NULL
;
893 dev
->pnp
.str_obj
= buffer
.pointer
;
894 result
= device_create_file(&dev
->dev
, &dev_attr_description
);
899 if (dev
->pnp
.type
.bus_address
)
900 result
= device_create_file(&dev
->dev
, &dev_attr_adr
);
901 if (dev
->pnp
.unique_id
)
902 result
= device_create_file(&dev
->dev
, &dev_attr_uid
);
904 if (acpi_has_method(dev
->handle
, "_SUN")) {
905 result
= device_create_file(&dev
->dev
, &dev_attr_sun
);
910 if (acpi_has_method(dev
->handle
, "_STA")) {
911 result
= device_create_file(&dev
->dev
, &dev_attr_status
);
917 * If device has _EJ0, 'eject' file is created that is used to trigger
918 * hot-removal function from userland.
920 if (acpi_has_method(dev
->handle
, "_EJ0")) {
921 result
= device_create_file(&dev
->dev
, &dev_attr_eject
);
926 if (dev
->flags
.power_manageable
) {
927 result
= device_create_file(&dev
->dev
, &dev_attr_power_state
);
931 if (dev
->power
.flags
.power_resources
)
932 result
= device_create_file(&dev
->dev
,
933 &dev_attr_real_power_state
);
940 static void acpi_device_remove_files(struct acpi_device
*dev
)
942 if (dev
->flags
.power_manageable
) {
943 device_remove_file(&dev
->dev
, &dev_attr_power_state
);
944 if (dev
->power
.flags
.power_resources
)
945 device_remove_file(&dev
->dev
,
946 &dev_attr_real_power_state
);
950 * If device has _STR, remove 'description' file
952 if (acpi_has_method(dev
->handle
, "_STR")) {
953 kfree(dev
->pnp
.str_obj
);
954 device_remove_file(&dev
->dev
, &dev_attr_description
);
957 * If device has _EJ0, remove 'eject' file.
959 if (acpi_has_method(dev
->handle
, "_EJ0"))
960 device_remove_file(&dev
->dev
, &dev_attr_eject
);
962 if (acpi_has_method(dev
->handle
, "_SUN"))
963 device_remove_file(&dev
->dev
, &dev_attr_sun
);
965 if (dev
->pnp
.unique_id
)
966 device_remove_file(&dev
->dev
, &dev_attr_uid
);
967 if (dev
->pnp
.type
.bus_address
)
968 device_remove_file(&dev
->dev
, &dev_attr_adr
);
969 device_remove_file(&dev
->dev
, &dev_attr_modalias
);
970 device_remove_file(&dev
->dev
, &dev_attr_hid
);
971 if (acpi_has_method(dev
->handle
, "_STA"))
972 device_remove_file(&dev
->dev
, &dev_attr_status
);
974 device_remove_file(&dev
->dev
, &dev_attr_path
);
976 /* --------------------------------------------------------------------------
978 -------------------------------------------------------------------------- */
981 * acpi_of_match_device - Match device object using the "compatible" property.
982 * @adev: ACPI device object to match.
983 * @of_match_table: List of device IDs to match against.
985 * If @dev has an ACPI companion which has ACPI_DT_NAMESPACE_HID in its list of
986 * identifiers and a _DSD object with the "compatible" property, use that
987 * property to match against the given list of identifiers.
989 static bool acpi_of_match_device(struct acpi_device
*adev
,
990 const struct of_device_id
*of_match_table
)
992 const union acpi_object
*of_compatible
, *obj
;
998 of_compatible
= adev
->data
.of_compatible
;
999 if (!of_match_table
|| !of_compatible
)
1002 if (of_compatible
->type
== ACPI_TYPE_PACKAGE
) {
1003 nval
= of_compatible
->package
.count
;
1004 obj
= of_compatible
->package
.elements
;
1005 } else { /* Must be ACPI_TYPE_STRING. */
1007 obj
= of_compatible
;
1009 /* Now we can look for the driver DT compatible strings */
1010 for (i
= 0; i
< nval
; i
++, obj
++) {
1011 const struct of_device_id
*id
;
1013 for (id
= of_match_table
; id
->compatible
[0]; id
++)
1014 if (!strcasecmp(obj
->string
.pointer
, id
->compatible
))
1021 static const struct acpi_device_id
*__acpi_match_device(
1022 struct acpi_device
*device
,
1023 const struct acpi_device_id
*ids
,
1024 const struct of_device_id
*of_ids
)
1026 const struct acpi_device_id
*id
;
1027 struct acpi_hardware_id
*hwid
;
1030 * If the device is not present, it is unnecessary to load device
1033 if (!device
|| !device
->status
.present
)
1036 list_for_each_entry(hwid
, &device
->pnp
.ids
, list
) {
1037 /* First, check the ACPI/PNP IDs provided by the caller. */
1038 for (id
= ids
; id
->id
[0]; id
++)
1039 if (!strcmp((char *) id
->id
, hwid
->id
))
1043 * Next, check ACPI_DT_NAMESPACE_HID and try to match the
1044 * "compatible" property if found.
1046 * The id returned by the below is not valid, but the only
1047 * caller passing non-NULL of_ids here is only interested in
1048 * whether or not the return value is NULL.
1050 if (!strcmp(ACPI_DT_NAMESPACE_HID
, hwid
->id
)
1051 && acpi_of_match_device(device
, of_ids
))
1058 * acpi_match_device - Match a struct device against a given list of ACPI IDs
1059 * @ids: Array of struct acpi_device_id object to match against.
1060 * @dev: The device structure to match.
1062 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
1063 * object for that handle and use that object to match against a given list of
1066 * Return a pointer to the first matching ID on success or %NULL on failure.
1068 const struct acpi_device_id
*acpi_match_device(const struct acpi_device_id
*ids
,
1069 const struct device
*dev
)
1071 return __acpi_match_device(acpi_companion_match(dev
), ids
, NULL
);
1073 EXPORT_SYMBOL_GPL(acpi_match_device
);
1075 int acpi_match_device_ids(struct acpi_device
*device
,
1076 const struct acpi_device_id
*ids
)
1078 return __acpi_match_device(device
, ids
, NULL
) ? 0 : -ENOENT
;
1080 EXPORT_SYMBOL(acpi_match_device_ids
);
1082 bool acpi_driver_match_device(struct device
*dev
,
1083 const struct device_driver
*drv
)
1085 if (!drv
->acpi_match_table
)
1086 return acpi_of_match_device(ACPI_COMPANION(dev
),
1087 drv
->of_match_table
);
1089 return !!__acpi_match_device(acpi_companion_match(dev
),
1090 drv
->acpi_match_table
, drv
->of_match_table
);
1092 EXPORT_SYMBOL_GPL(acpi_driver_match_device
);
1094 static void acpi_free_power_resources_lists(struct acpi_device
*device
)
1098 if (device
->wakeup
.flags
.valid
)
1099 acpi_power_resources_list_free(&device
->wakeup
.resources
);
1101 if (!device
->power
.flags
.power_resources
)
1104 for (i
= ACPI_STATE_D0
; i
<= ACPI_STATE_D3_HOT
; i
++) {
1105 struct acpi_device_power_state
*ps
= &device
->power
.states
[i
];
1106 acpi_power_resources_list_free(&ps
->resources
);
1110 static void acpi_device_release(struct device
*dev
)
1112 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
1114 acpi_free_properties(acpi_dev
);
1115 acpi_free_pnp_ids(&acpi_dev
->pnp
);
1116 acpi_free_power_resources_lists(acpi_dev
);
1120 static int acpi_bus_match(struct device
*dev
, struct device_driver
*drv
)
1122 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
1123 struct acpi_driver
*acpi_drv
= to_acpi_driver(drv
);
1125 return acpi_dev
->flags
.match_driver
1126 && !acpi_match_device_ids(acpi_dev
, acpi_drv
->ids
);
1129 static int acpi_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
1131 return __acpi_device_uevent_modalias(to_acpi_device(dev
), env
);
1134 static void acpi_device_notify(acpi_handle handle
, u32 event
, void *data
)
1136 struct acpi_device
*device
= data
;
1138 device
->driver
->ops
.notify(device
, event
);
1141 static void acpi_device_notify_fixed(void *data
)
1143 struct acpi_device
*device
= data
;
1145 /* Fixed hardware devices have no handles */
1146 acpi_device_notify(NULL
, ACPI_FIXED_HARDWARE_EVENT
, device
);
1149 static u32
acpi_device_fixed_event(void *data
)
1151 acpi_os_execute(OSL_NOTIFY_HANDLER
, acpi_device_notify_fixed
, data
);
1152 return ACPI_INTERRUPT_HANDLED
;
1155 static int acpi_device_install_notify_handler(struct acpi_device
*device
)
1159 if (device
->device_type
== ACPI_BUS_TYPE_POWER_BUTTON
)
1161 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON
,
1162 acpi_device_fixed_event
,
1164 else if (device
->device_type
== ACPI_BUS_TYPE_SLEEP_BUTTON
)
1166 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON
,
1167 acpi_device_fixed_event
,
1170 status
= acpi_install_notify_handler(device
->handle
,
1175 if (ACPI_FAILURE(status
))
1180 static void acpi_device_remove_notify_handler(struct acpi_device
*device
)
1182 if (device
->device_type
== ACPI_BUS_TYPE_POWER_BUTTON
)
1183 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON
,
1184 acpi_device_fixed_event
);
1185 else if (device
->device_type
== ACPI_BUS_TYPE_SLEEP_BUTTON
)
1186 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON
,
1187 acpi_device_fixed_event
);
1189 acpi_remove_notify_handler(device
->handle
, ACPI_DEVICE_NOTIFY
,
1190 acpi_device_notify
);
1193 static int acpi_device_probe(struct device
*dev
)
1195 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
1196 struct acpi_driver
*acpi_drv
= to_acpi_driver(dev
->driver
);
1199 if (acpi_dev
->handler
&& !acpi_is_pnp_device(acpi_dev
))
1202 if (!acpi_drv
->ops
.add
)
1205 ret
= acpi_drv
->ops
.add(acpi_dev
);
1209 acpi_dev
->driver
= acpi_drv
;
1210 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1211 "Driver [%s] successfully bound to device [%s]\n",
1212 acpi_drv
->name
, acpi_dev
->pnp
.bus_id
));
1214 if (acpi_drv
->ops
.notify
) {
1215 ret
= acpi_device_install_notify_handler(acpi_dev
);
1217 if (acpi_drv
->ops
.remove
)
1218 acpi_drv
->ops
.remove(acpi_dev
);
1220 acpi_dev
->driver
= NULL
;
1221 acpi_dev
->driver_data
= NULL
;
1226 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found driver [%s] for device [%s]\n",
1227 acpi_drv
->name
, acpi_dev
->pnp
.bus_id
));
1232 static int acpi_device_remove(struct device
* dev
)
1234 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
1235 struct acpi_driver
*acpi_drv
= acpi_dev
->driver
;
1238 if (acpi_drv
->ops
.notify
)
1239 acpi_device_remove_notify_handler(acpi_dev
);
1240 if (acpi_drv
->ops
.remove
)
1241 acpi_drv
->ops
.remove(acpi_dev
);
1243 acpi_dev
->driver
= NULL
;
1244 acpi_dev
->driver_data
= NULL
;
1250 struct bus_type acpi_bus_type
= {
1252 .match
= acpi_bus_match
,
1253 .probe
= acpi_device_probe
,
1254 .remove
= acpi_device_remove
,
1255 .uevent
= acpi_device_uevent
,
1258 static void acpi_device_del(struct acpi_device
*device
)
1260 mutex_lock(&acpi_device_lock
);
1262 list_del(&device
->node
);
1264 list_del(&device
->wakeup_list
);
1265 mutex_unlock(&acpi_device_lock
);
1267 acpi_power_add_remove_device(device
, false);
1268 acpi_device_remove_files(device
);
1270 device
->remove(device
);
1272 device_del(&device
->dev
);
1275 static LIST_HEAD(acpi_device_del_list
);
1276 static DEFINE_MUTEX(acpi_device_del_lock
);
1278 static void acpi_device_del_work_fn(struct work_struct
*work_not_used
)
1281 struct acpi_device
*adev
;
1283 mutex_lock(&acpi_device_del_lock
);
1285 if (list_empty(&acpi_device_del_list
)) {
1286 mutex_unlock(&acpi_device_del_lock
);
1289 adev
= list_first_entry(&acpi_device_del_list
,
1290 struct acpi_device
, del_list
);
1291 list_del(&adev
->del_list
);
1293 mutex_unlock(&acpi_device_del_lock
);
1295 acpi_device_del(adev
);
1297 * Drop references to all power resources that might have been
1298 * used by the device.
1300 acpi_power_transition(adev
, ACPI_STATE_D3_COLD
);
1301 put_device(&adev
->dev
);
1306 * acpi_scan_drop_device - Drop an ACPI device object.
1307 * @handle: Handle of an ACPI namespace node, not used.
1308 * @context: Address of the ACPI device object to drop.
1310 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
1311 * namespace node the device object pointed to by @context is attached to.
1313 * The unregistration is carried out asynchronously to avoid running
1314 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
1315 * ensure the correct ordering (the device objects must be unregistered in the
1316 * same order in which the corresponding namespace nodes are deleted).
1318 static void acpi_scan_drop_device(acpi_handle handle
, void *context
)
1320 static DECLARE_WORK(work
, acpi_device_del_work_fn
);
1321 struct acpi_device
*adev
= context
;
1323 mutex_lock(&acpi_device_del_lock
);
1326 * Use the ACPI hotplug workqueue which is ordered, so this work item
1327 * won't run after any hotplug work items submitted subsequently. That
1328 * prevents attempts to register device objects identical to those being
1329 * deleted from happening concurrently (such attempts result from
1330 * hotplug events handled via the ACPI hotplug workqueue). It also will
1331 * run after all of the work items submitted previosuly, which helps
1332 * those work items to ensure that they are not accessing stale device
1335 if (list_empty(&acpi_device_del_list
))
1336 acpi_queue_hotplug_work(&work
);
1338 list_add_tail(&adev
->del_list
, &acpi_device_del_list
);
1339 /* Make acpi_ns_validate_handle() return NULL for this handle. */
1340 adev
->handle
= INVALID_ACPI_HANDLE
;
1342 mutex_unlock(&acpi_device_del_lock
);
1345 static int acpi_get_device_data(acpi_handle handle
, struct acpi_device
**device
,
1346 void (*callback
)(void *))
1353 status
= acpi_get_data_full(handle
, acpi_scan_drop_device
,
1354 (void **)device
, callback
);
1355 if (ACPI_FAILURE(status
) || !*device
) {
1356 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No context for object [%p]\n",
1363 int acpi_bus_get_device(acpi_handle handle
, struct acpi_device
**device
)
1365 return acpi_get_device_data(handle
, device
, NULL
);
1367 EXPORT_SYMBOL(acpi_bus_get_device
);
1369 static void get_acpi_device(void *dev
)
1372 get_device(&((struct acpi_device
*)dev
)->dev
);
1375 struct acpi_device
*acpi_bus_get_acpi_device(acpi_handle handle
)
1377 struct acpi_device
*adev
= NULL
;
1379 acpi_get_device_data(handle
, &adev
, get_acpi_device
);
1383 void acpi_bus_put_acpi_device(struct acpi_device
*adev
)
1385 put_device(&adev
->dev
);
1388 int acpi_device_add(struct acpi_device
*device
,
1389 void (*release
)(struct device
*))
1392 struct acpi_device_bus_id
*acpi_device_bus_id
, *new_bus_id
;
1395 if (device
->handle
) {
1398 status
= acpi_attach_data(device
->handle
, acpi_scan_drop_device
,
1400 if (ACPI_FAILURE(status
)) {
1401 acpi_handle_err(device
->handle
,
1402 "Unable to attach device data\n");
1410 * Link this device to its parent and siblings.
1412 INIT_LIST_HEAD(&device
->children
);
1413 INIT_LIST_HEAD(&device
->node
);
1414 INIT_LIST_HEAD(&device
->wakeup_list
);
1415 INIT_LIST_HEAD(&device
->physical_node_list
);
1416 INIT_LIST_HEAD(&device
->del_list
);
1417 mutex_init(&device
->physical_node_lock
);
1419 new_bus_id
= kzalloc(sizeof(struct acpi_device_bus_id
), GFP_KERNEL
);
1421 pr_err(PREFIX
"Memory allocation error\n");
1426 mutex_lock(&acpi_device_lock
);
1428 * Find suitable bus_id and instance number in acpi_bus_id_list
1429 * If failed, create one and link it into acpi_bus_id_list
1431 list_for_each_entry(acpi_device_bus_id
, &acpi_bus_id_list
, node
) {
1432 if (!strcmp(acpi_device_bus_id
->bus_id
,
1433 acpi_device_hid(device
))) {
1434 acpi_device_bus_id
->instance_no
++;
1441 acpi_device_bus_id
= new_bus_id
;
1442 strcpy(acpi_device_bus_id
->bus_id
, acpi_device_hid(device
));
1443 acpi_device_bus_id
->instance_no
= 0;
1444 list_add_tail(&acpi_device_bus_id
->node
, &acpi_bus_id_list
);
1446 dev_set_name(&device
->dev
, "%s:%02x", acpi_device_bus_id
->bus_id
, acpi_device_bus_id
->instance_no
);
1449 list_add_tail(&device
->node
, &device
->parent
->children
);
1451 if (device
->wakeup
.flags
.valid
)
1452 list_add_tail(&device
->wakeup_list
, &acpi_wakeup_device_list
);
1453 mutex_unlock(&acpi_device_lock
);
1456 device
->dev
.parent
= &device
->parent
->dev
;
1457 device
->dev
.bus
= &acpi_bus_type
;
1458 device
->dev
.release
= release
;
1459 result
= device_add(&device
->dev
);
1461 dev_err(&device
->dev
, "Error registering device\n");
1465 result
= acpi_device_setup_files(device
);
1467 printk(KERN_ERR PREFIX
"Error creating sysfs interface for device %s\n",
1468 dev_name(&device
->dev
));
1473 mutex_lock(&acpi_device_lock
);
1475 list_del(&device
->node
);
1476 list_del(&device
->wakeup_list
);
1477 mutex_unlock(&acpi_device_lock
);
1480 acpi_detach_data(device
->handle
, acpi_scan_drop_device
);
1484 struct acpi_device
*acpi_get_next_child(struct device
*dev
,
1485 struct acpi_device
*child
)
1487 struct acpi_device
*adev
= ACPI_COMPANION(dev
);
1488 struct list_head
*head
, *next
;
1493 head
= &adev
->children
;
1494 if (list_empty(head
))
1498 return list_first_entry(head
, struct acpi_device
, node
);
1500 next
= child
->node
.next
;
1501 return next
== head
? NULL
: list_entry(next
, struct acpi_device
, node
);
1504 /* --------------------------------------------------------------------------
1506 -------------------------------------------------------------------------- */
1508 * acpi_bus_register_driver - register a driver with the ACPI bus
1509 * @driver: driver being registered
1511 * Registers a driver with the ACPI bus. Searches the namespace for all
1512 * devices that match the driver's criteria and binds. Returns zero for
1513 * success or a negative error status for failure.
1515 int acpi_bus_register_driver(struct acpi_driver
*driver
)
1521 driver
->drv
.name
= driver
->name
;
1522 driver
->drv
.bus
= &acpi_bus_type
;
1523 driver
->drv
.owner
= driver
->owner
;
1525 ret
= driver_register(&driver
->drv
);
1529 EXPORT_SYMBOL(acpi_bus_register_driver
);
1532 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
1533 * @driver: driver to unregister
1535 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1536 * devices that match the driver's criteria and unbinds.
1538 void acpi_bus_unregister_driver(struct acpi_driver
*driver
)
1540 driver_unregister(&driver
->drv
);
1543 EXPORT_SYMBOL(acpi_bus_unregister_driver
);
1545 /* --------------------------------------------------------------------------
1547 -------------------------------------------------------------------------- */
1548 static struct acpi_device
*acpi_bus_get_parent(acpi_handle handle
)
1550 struct acpi_device
*device
= NULL
;
1554 * Fixed hardware devices do not appear in the namespace and do not
1555 * have handles, but we fabricate acpi_devices for them, so we have
1556 * to deal with them specially.
1562 status
= acpi_get_parent(handle
, &handle
);
1563 if (ACPI_FAILURE(status
))
1564 return status
== AE_NULL_ENTRY
? NULL
: acpi_root
;
1565 } while (acpi_bus_get_device(handle
, &device
));
1570 acpi_bus_get_ejd(acpi_handle handle
, acpi_handle
*ejd
)
1574 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
1575 union acpi_object
*obj
;
1577 status
= acpi_get_handle(handle
, "_EJD", &tmp
);
1578 if (ACPI_FAILURE(status
))
1581 status
= acpi_evaluate_object(handle
, "_EJD", NULL
, &buffer
);
1582 if (ACPI_SUCCESS(status
)) {
1583 obj
= buffer
.pointer
;
1584 status
= acpi_get_handle(ACPI_ROOT_OBJECT
, obj
->string
.pointer
,
1586 kfree(buffer
.pointer
);
1590 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd
);
1592 static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle
,
1593 struct acpi_device_wakeup
*wakeup
)
1595 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1596 union acpi_object
*package
= NULL
;
1597 union acpi_object
*element
= NULL
;
1604 INIT_LIST_HEAD(&wakeup
->resources
);
1607 status
= acpi_evaluate_object(handle
, "_PRW", NULL
, &buffer
);
1608 if (ACPI_FAILURE(status
)) {
1609 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PRW"));
1613 package
= (union acpi_object
*)buffer
.pointer
;
1615 if (!package
|| package
->package
.count
< 2)
1618 element
= &(package
->package
.elements
[0]);
1622 if (element
->type
== ACPI_TYPE_PACKAGE
) {
1623 if ((element
->package
.count
< 2) ||
1624 (element
->package
.elements
[0].type
!=
1625 ACPI_TYPE_LOCAL_REFERENCE
)
1626 || (element
->package
.elements
[1].type
!= ACPI_TYPE_INTEGER
))
1629 wakeup
->gpe_device
=
1630 element
->package
.elements
[0].reference
.handle
;
1631 wakeup
->gpe_number
=
1632 (u32
) element
->package
.elements
[1].integer
.value
;
1633 } else if (element
->type
== ACPI_TYPE_INTEGER
) {
1634 wakeup
->gpe_device
= NULL
;
1635 wakeup
->gpe_number
= element
->integer
.value
;
1640 element
= &(package
->package
.elements
[1]);
1641 if (element
->type
!= ACPI_TYPE_INTEGER
)
1644 wakeup
->sleep_state
= element
->integer
.value
;
1646 err
= acpi_extract_power_resources(package
, 2, &wakeup
->resources
);
1650 if (!list_empty(&wakeup
->resources
)) {
1653 err
= acpi_power_wakeup_list_init(&wakeup
->resources
,
1656 acpi_handle_warn(handle
, "Retrieving current states "
1657 "of wakeup power resources failed\n");
1658 acpi_power_resources_list_free(&wakeup
->resources
);
1661 if (sleep_state
< wakeup
->sleep_state
) {
1662 acpi_handle_warn(handle
, "Overriding _PRW sleep state "
1663 "(S%d) by S%d from power resources\n",
1664 (int)wakeup
->sleep_state
, sleep_state
);
1665 wakeup
->sleep_state
= sleep_state
;
1670 kfree(buffer
.pointer
);
1674 static void acpi_wakeup_gpe_init(struct acpi_device
*device
)
1676 static const struct acpi_device_id button_device_ids
[] = {
1682 struct acpi_device_wakeup
*wakeup
= &device
->wakeup
;
1684 acpi_event_status event_status
;
1686 wakeup
->flags
.notifier_present
= 0;
1688 /* Power button, Lid switch always enable wakeup */
1689 if (!acpi_match_device_ids(device
, button_device_ids
)) {
1690 wakeup
->flags
.run_wake
= 1;
1691 if (!acpi_match_device_ids(device
, &button_device_ids
[1])) {
1692 /* Do not use Lid/sleep button for S5 wakeup */
1693 if (wakeup
->sleep_state
== ACPI_STATE_S5
)
1694 wakeup
->sleep_state
= ACPI_STATE_S4
;
1696 acpi_mark_gpe_for_wake(wakeup
->gpe_device
, wakeup
->gpe_number
);
1697 device_set_wakeup_capable(&device
->dev
, true);
1701 acpi_setup_gpe_for_wake(device
->handle
, wakeup
->gpe_device
,
1702 wakeup
->gpe_number
);
1703 status
= acpi_get_gpe_status(wakeup
->gpe_device
, wakeup
->gpe_number
,
1705 if (ACPI_FAILURE(status
))
1708 wakeup
->flags
.run_wake
= !!(event_status
& ACPI_EVENT_FLAG_HAS_HANDLER
);
1711 static void acpi_bus_get_wakeup_device_flags(struct acpi_device
*device
)
1715 /* Presence of _PRW indicates wake capable */
1716 if (!acpi_has_method(device
->handle
, "_PRW"))
1719 err
= acpi_bus_extract_wakeup_device_power_package(device
->handle
,
1722 dev_err(&device
->dev
, "_PRW evaluation error: %d\n", err
);
1726 device
->wakeup
.flags
.valid
= 1;
1727 device
->wakeup
.prepare_count
= 0;
1728 acpi_wakeup_gpe_init(device
);
1729 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1730 * system for the ACPI device with the _PRW object.
1731 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1732 * So it is necessary to call _DSW object first. Only when it is not
1733 * present will the _PSW object used.
1735 err
= acpi_device_sleep_wake(device
, 0, 0, 0);
1737 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1738 "error in _DSW or _PSW evaluation\n"));
1741 static void acpi_bus_init_power_state(struct acpi_device
*device
, int state
)
1743 struct acpi_device_power_state
*ps
= &device
->power
.states
[state
];
1744 char pathname
[5] = { '_', 'P', 'R', '0' + state
, '\0' };
1745 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
1748 INIT_LIST_HEAD(&ps
->resources
);
1750 /* Evaluate "_PRx" to get referenced power resources */
1751 status
= acpi_evaluate_object(device
->handle
, pathname
, NULL
, &buffer
);
1752 if (ACPI_SUCCESS(status
)) {
1753 union acpi_object
*package
= buffer
.pointer
;
1755 if (buffer
.length
&& package
1756 && package
->type
== ACPI_TYPE_PACKAGE
1757 && package
->package
.count
) {
1758 int err
= acpi_extract_power_resources(package
, 0,
1761 device
->power
.flags
.power_resources
= 1;
1763 ACPI_FREE(buffer
.pointer
);
1766 /* Evaluate "_PSx" to see if we can do explicit sets */
1768 if (acpi_has_method(device
->handle
, pathname
))
1769 ps
->flags
.explicit_set
= 1;
1771 /* State is valid if there are means to put the device into it. */
1772 if (!list_empty(&ps
->resources
) || ps
->flags
.explicit_set
)
1773 ps
->flags
.valid
= 1;
1775 ps
->power
= -1; /* Unknown - driver assigned */
1776 ps
->latency
= -1; /* Unknown - driver assigned */
1779 static void acpi_bus_get_power_flags(struct acpi_device
*device
)
1783 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1784 if (!acpi_has_method(device
->handle
, "_PS0") &&
1785 !acpi_has_method(device
->handle
, "_PR0"))
1788 device
->flags
.power_manageable
= 1;
1791 * Power Management Flags
1793 if (acpi_has_method(device
->handle
, "_PSC"))
1794 device
->power
.flags
.explicit_get
= 1;
1796 if (acpi_has_method(device
->handle
, "_IRC"))
1797 device
->power
.flags
.inrush_current
= 1;
1799 if (acpi_has_method(device
->handle
, "_DSW"))
1800 device
->power
.flags
.dsw_present
= 1;
1803 * Enumerate supported power management states
1805 for (i
= ACPI_STATE_D0
; i
<= ACPI_STATE_D3_HOT
; i
++)
1806 acpi_bus_init_power_state(device
, i
);
1808 INIT_LIST_HEAD(&device
->power
.states
[ACPI_STATE_D3_COLD
].resources
);
1809 if (!list_empty(&device
->power
.states
[ACPI_STATE_D3_HOT
].resources
))
1810 device
->power
.states
[ACPI_STATE_D3_COLD
].flags
.valid
= 1;
1812 /* Set defaults for D0 and D3hot states (always valid) */
1813 device
->power
.states
[ACPI_STATE_D0
].flags
.valid
= 1;
1814 device
->power
.states
[ACPI_STATE_D0
].power
= 100;
1815 device
->power
.states
[ACPI_STATE_D3_HOT
].flags
.valid
= 1;
1817 if (acpi_bus_init_power(device
))
1818 device
->flags
.power_manageable
= 0;
1821 static void acpi_bus_get_flags(struct acpi_device
*device
)
1823 /* Presence of _STA indicates 'dynamic_status' */
1824 if (acpi_has_method(device
->handle
, "_STA"))
1825 device
->flags
.dynamic_status
= 1;
1827 /* Presence of _RMV indicates 'removable' */
1828 if (acpi_has_method(device
->handle
, "_RMV"))
1829 device
->flags
.removable
= 1;
1831 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1832 if (acpi_has_method(device
->handle
, "_EJD") ||
1833 acpi_has_method(device
->handle
, "_EJ0"))
1834 device
->flags
.ejectable
= 1;
1837 static void acpi_device_get_busid(struct acpi_device
*device
)
1839 char bus_id
[5] = { '?', 0 };
1840 struct acpi_buffer buffer
= { sizeof(bus_id
), bus_id
};
1846 * The device's Bus ID is simply the object name.
1847 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1849 if (ACPI_IS_ROOT_DEVICE(device
)) {
1850 strcpy(device
->pnp
.bus_id
, "ACPI");
1854 switch (device
->device_type
) {
1855 case ACPI_BUS_TYPE_POWER_BUTTON
:
1856 strcpy(device
->pnp
.bus_id
, "PWRF");
1858 case ACPI_BUS_TYPE_SLEEP_BUTTON
:
1859 strcpy(device
->pnp
.bus_id
, "SLPF");
1862 acpi_get_name(device
->handle
, ACPI_SINGLE_NAME
, &buffer
);
1863 /* Clean up trailing underscores (if any) */
1864 for (i
= 3; i
> 1; i
--) {
1865 if (bus_id
[i
] == '_')
1870 strcpy(device
->pnp
.bus_id
, bus_id
);
1876 * acpi_ata_match - see if an acpi object is an ATA device
1878 * If an acpi object has one of the ACPI ATA methods defined,
1879 * then we can safely call it an ATA device.
1881 bool acpi_ata_match(acpi_handle handle
)
1883 return acpi_has_method(handle
, "_GTF") ||
1884 acpi_has_method(handle
, "_GTM") ||
1885 acpi_has_method(handle
, "_STM") ||
1886 acpi_has_method(handle
, "_SDD");
1890 * acpi_bay_match - see if an acpi object is an ejectable driver bay
1892 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1893 * then we can safely call it an ejectable drive bay
1895 bool acpi_bay_match(acpi_handle handle
)
1897 acpi_handle phandle
;
1899 if (!acpi_has_method(handle
, "_EJ0"))
1901 if (acpi_ata_match(handle
))
1903 if (ACPI_FAILURE(acpi_get_parent(handle
, &phandle
)))
1906 return acpi_ata_match(phandle
);
1909 bool acpi_device_is_battery(struct acpi_device
*adev
)
1911 struct acpi_hardware_id
*hwid
;
1913 list_for_each_entry(hwid
, &adev
->pnp
.ids
, list
)
1914 if (!strcmp("PNP0C0A", hwid
->id
))
1920 static bool is_ejectable_bay(struct acpi_device
*adev
)
1922 acpi_handle handle
= adev
->handle
;
1924 if (acpi_has_method(handle
, "_EJ0") && acpi_device_is_battery(adev
))
1927 return acpi_bay_match(handle
);
1931 * acpi_dock_match - see if an acpi object has a _DCK method
1933 bool acpi_dock_match(acpi_handle handle
)
1935 return acpi_has_method(handle
, "_DCK");
1938 const char *acpi_device_hid(struct acpi_device
*device
)
1940 struct acpi_hardware_id
*hid
;
1942 if (list_empty(&device
->pnp
.ids
))
1945 hid
= list_first_entry(&device
->pnp
.ids
, struct acpi_hardware_id
, list
);
1948 EXPORT_SYMBOL(acpi_device_hid
);
1950 static void acpi_add_id(struct acpi_device_pnp
*pnp
, const char *dev_id
)
1952 struct acpi_hardware_id
*id
;
1954 id
= kmalloc(sizeof(*id
), GFP_KERNEL
);
1958 id
->id
= kstrdup(dev_id
, GFP_KERNEL
);
1964 list_add_tail(&id
->list
, &pnp
->ids
);
1965 pnp
->type
.hardware_id
= 1;
1969 * Old IBM workstations have a DSDT bug wherein the SMBus object
1970 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1971 * prefix. Work around this.
1973 static bool acpi_ibm_smbus_match(acpi_handle handle
)
1975 char node_name
[ACPI_PATH_SEGMENT_LENGTH
];
1976 struct acpi_buffer path
= { sizeof(node_name
), node_name
};
1978 if (!dmi_name_in_vendors("IBM"))
1981 /* Look for SMBS object */
1982 if (ACPI_FAILURE(acpi_get_name(handle
, ACPI_SINGLE_NAME
, &path
)) ||
1983 strcmp("SMBS", path
.pointer
))
1986 /* Does it have the necessary (but misnamed) methods? */
1987 if (acpi_has_method(handle
, "SBI") &&
1988 acpi_has_method(handle
, "SBR") &&
1989 acpi_has_method(handle
, "SBW"))
1995 static bool acpi_object_is_system_bus(acpi_handle handle
)
1999 if (ACPI_SUCCESS(acpi_get_handle(NULL
, "\\_SB", &tmp
)) &&
2002 if (ACPI_SUCCESS(acpi_get_handle(NULL
, "\\_TZ", &tmp
)) &&
2009 static void acpi_set_pnp_ids(acpi_handle handle
, struct acpi_device_pnp
*pnp
,
2013 struct acpi_device_info
*info
;
2014 struct acpi_pnp_device_id_list
*cid_list
;
2017 switch (device_type
) {
2018 case ACPI_BUS_TYPE_DEVICE
:
2019 if (handle
== ACPI_ROOT_OBJECT
) {
2020 acpi_add_id(pnp
, ACPI_SYSTEM_HID
);
2024 status
= acpi_get_object_info(handle
, &info
);
2025 if (ACPI_FAILURE(status
)) {
2026 pr_err(PREFIX
"%s: Error reading device info\n",
2031 if (info
->valid
& ACPI_VALID_HID
) {
2032 acpi_add_id(pnp
, info
->hardware_id
.string
);
2033 pnp
->type
.platform_id
= 1;
2035 if (info
->valid
& ACPI_VALID_CID
) {
2036 cid_list
= &info
->compatible_id_list
;
2037 for (i
= 0; i
< cid_list
->count
; i
++)
2038 acpi_add_id(pnp
, cid_list
->ids
[i
].string
);
2040 if (info
->valid
& ACPI_VALID_ADR
) {
2041 pnp
->bus_address
= info
->address
;
2042 pnp
->type
.bus_address
= 1;
2044 if (info
->valid
& ACPI_VALID_UID
)
2045 pnp
->unique_id
= kstrdup(info
->unique_id
.string
,
2051 * Some devices don't reliably have _HIDs & _CIDs, so add
2052 * synthetic HIDs to make sure drivers can find them.
2054 if (acpi_is_video_device(handle
))
2055 acpi_add_id(pnp
, ACPI_VIDEO_HID
);
2056 else if (acpi_bay_match(handle
))
2057 acpi_add_id(pnp
, ACPI_BAY_HID
);
2058 else if (acpi_dock_match(handle
))
2059 acpi_add_id(pnp
, ACPI_DOCK_HID
);
2060 else if (acpi_ibm_smbus_match(handle
))
2061 acpi_add_id(pnp
, ACPI_SMBUS_IBM_HID
);
2062 else if (list_empty(&pnp
->ids
) &&
2063 acpi_object_is_system_bus(handle
)) {
2064 /* \_SB, \_TZ, LNXSYBUS */
2065 acpi_add_id(pnp
, ACPI_BUS_HID
);
2066 strcpy(pnp
->device_name
, ACPI_BUS_DEVICE_NAME
);
2067 strcpy(pnp
->device_class
, ACPI_BUS_CLASS
);
2071 case ACPI_BUS_TYPE_POWER
:
2072 acpi_add_id(pnp
, ACPI_POWER_HID
);
2074 case ACPI_BUS_TYPE_PROCESSOR
:
2075 acpi_add_id(pnp
, ACPI_PROCESSOR_OBJECT_HID
);
2077 case ACPI_BUS_TYPE_THERMAL
:
2078 acpi_add_id(pnp
, ACPI_THERMAL_HID
);
2080 case ACPI_BUS_TYPE_POWER_BUTTON
:
2081 acpi_add_id(pnp
, ACPI_BUTTON_HID_POWERF
);
2083 case ACPI_BUS_TYPE_SLEEP_BUTTON
:
2084 acpi_add_id(pnp
, ACPI_BUTTON_HID_SLEEPF
);
2089 void acpi_free_pnp_ids(struct acpi_device_pnp
*pnp
)
2091 struct acpi_hardware_id
*id
, *tmp
;
2093 list_for_each_entry_safe(id
, tmp
, &pnp
->ids
, list
) {
2097 kfree(pnp
->unique_id
);
2100 void acpi_init_device_object(struct acpi_device
*device
, acpi_handle handle
,
2101 int type
, unsigned long long sta
)
2103 INIT_LIST_HEAD(&device
->pnp
.ids
);
2104 device
->device_type
= type
;
2105 device
->handle
= handle
;
2106 device
->parent
= acpi_bus_get_parent(handle
);
2107 device
->fwnode
.type
= FWNODE_ACPI
;
2108 acpi_set_device_status(device
, sta
);
2109 acpi_device_get_busid(device
);
2110 acpi_set_pnp_ids(handle
, &device
->pnp
, type
);
2111 acpi_init_properties(device
);
2112 acpi_bus_get_flags(device
);
2113 device
->flags
.match_driver
= false;
2114 device
->flags
.initialized
= true;
2115 device
->flags
.visited
= false;
2116 device_initialize(&device
->dev
);
2117 dev_set_uevent_suppress(&device
->dev
, true);
2120 void acpi_device_add_finalize(struct acpi_device
*device
)
2122 dev_set_uevent_suppress(&device
->dev
, false);
2123 kobject_uevent(&device
->dev
.kobj
, KOBJ_ADD
);
2126 static int acpi_add_single_object(struct acpi_device
**child
,
2127 acpi_handle handle
, int type
,
2128 unsigned long long sta
)
2131 struct acpi_device
*device
;
2132 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
2134 device
= kzalloc(sizeof(struct acpi_device
), GFP_KERNEL
);
2136 printk(KERN_ERR PREFIX
"Memory allocation error\n");
2140 acpi_init_device_object(device
, handle
, type
, sta
);
2141 acpi_bus_get_power_flags(device
);
2142 acpi_bus_get_wakeup_device_flags(device
);
2144 result
= acpi_device_add(device
, acpi_device_release
);
2146 acpi_device_release(&device
->dev
);
2150 acpi_power_add_remove_device(device
, true);
2151 acpi_device_add_finalize(device
);
2152 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
);
2153 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Added %s [%s] parent %s\n",
2154 dev_name(&device
->dev
), (char *) buffer
.pointer
,
2155 device
->parent
? dev_name(&device
->parent
->dev
) : "(null)"));
2156 kfree(buffer
.pointer
);
2161 static int acpi_bus_type_and_status(acpi_handle handle
, int *type
,
2162 unsigned long long *sta
)
2165 acpi_object_type acpi_type
;
2167 status
= acpi_get_type(handle
, &acpi_type
);
2168 if (ACPI_FAILURE(status
))
2171 switch (acpi_type
) {
2172 case ACPI_TYPE_ANY
: /* for ACPI_ROOT_OBJECT */
2173 case ACPI_TYPE_DEVICE
:
2174 *type
= ACPI_BUS_TYPE_DEVICE
;
2175 status
= acpi_bus_get_status_handle(handle
, sta
);
2176 if (ACPI_FAILURE(status
))
2179 case ACPI_TYPE_PROCESSOR
:
2180 *type
= ACPI_BUS_TYPE_PROCESSOR
;
2181 status
= acpi_bus_get_status_handle(handle
, sta
);
2182 if (ACPI_FAILURE(status
))
2185 case ACPI_TYPE_THERMAL
:
2186 *type
= ACPI_BUS_TYPE_THERMAL
;
2187 *sta
= ACPI_STA_DEFAULT
;
2189 case ACPI_TYPE_POWER
:
2190 *type
= ACPI_BUS_TYPE_POWER
;
2191 *sta
= ACPI_STA_DEFAULT
;
2200 bool acpi_device_is_present(struct acpi_device
*adev
)
2202 if (adev
->status
.present
|| adev
->status
.functional
)
2205 adev
->flags
.initialized
= false;
2209 static bool acpi_scan_handler_matching(struct acpi_scan_handler
*handler
,
2211 const struct acpi_device_id
**matchid
)
2213 const struct acpi_device_id
*devid
;
2216 return handler
->match(idstr
, matchid
);
2218 for (devid
= handler
->ids
; devid
->id
[0]; devid
++)
2219 if (!strcmp((char *)devid
->id
, idstr
)) {
2229 static struct acpi_scan_handler
*acpi_scan_match_handler(char *idstr
,
2230 const struct acpi_device_id
**matchid
)
2232 struct acpi_scan_handler
*handler
;
2234 list_for_each_entry(handler
, &acpi_scan_handlers_list
, list_node
)
2235 if (acpi_scan_handler_matching(handler
, idstr
, matchid
))
2241 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile
*hotplug
, bool val
)
2243 if (!!hotplug
->enabled
== !!val
)
2246 mutex_lock(&acpi_scan_lock
);
2248 hotplug
->enabled
= val
;
2250 mutex_unlock(&acpi_scan_lock
);
2253 static void acpi_scan_init_hotplug(struct acpi_device
*adev
)
2255 struct acpi_hardware_id
*hwid
;
2257 if (acpi_dock_match(adev
->handle
) || is_ejectable_bay(adev
)) {
2258 acpi_dock_add(adev
);
2261 list_for_each_entry(hwid
, &adev
->pnp
.ids
, list
) {
2262 struct acpi_scan_handler
*handler
;
2264 handler
= acpi_scan_match_handler(hwid
->id
, NULL
);
2266 adev
->flags
.hotplug_notify
= true;
2272 static void acpi_device_dep_initialize(struct acpi_device
*adev
)
2274 struct acpi_dep_data
*dep
;
2275 struct acpi_handle_list dep_devices
;
2279 if (!acpi_has_method(adev
->handle
, "_DEP"))
2282 status
= acpi_evaluate_reference(adev
->handle
, "_DEP", NULL
,
2284 if (ACPI_FAILURE(status
)) {
2285 dev_dbg(&adev
->dev
, "Failed to evaluate _DEP.\n");
2289 for (i
= 0; i
< dep_devices
.count
; i
++) {
2290 struct acpi_device_info
*info
;
2293 status
= acpi_get_object_info(dep_devices
.handles
[i
], &info
);
2294 if (ACPI_FAILURE(status
)) {
2295 dev_dbg(&adev
->dev
, "Error reading _DEP device info\n");
2300 * Skip the dependency of Windows System Power
2301 * Management Controller
2303 skip
= info
->valid
& ACPI_VALID_HID
&&
2304 !strcmp(info
->hardware_id
.string
, "INT3396");
2311 dep
= kzalloc(sizeof(struct acpi_dep_data
), GFP_KERNEL
);
2315 dep
->master
= dep_devices
.handles
[i
];
2316 dep
->slave
= adev
->handle
;
2319 mutex_lock(&acpi_dep_list_lock
);
2320 list_add_tail(&dep
->node
, &acpi_dep_list
);
2321 mutex_unlock(&acpi_dep_list_lock
);
2325 static acpi_status
acpi_bus_check_add(acpi_handle handle
, u32 lvl_not_used
,
2326 void *not_used
, void **return_value
)
2328 struct acpi_device
*device
= NULL
;
2330 unsigned long long sta
;
2333 acpi_bus_get_device(handle
, &device
);
2337 result
= acpi_bus_type_and_status(handle
, &type
, &sta
);
2341 if (type
== ACPI_BUS_TYPE_POWER
) {
2342 acpi_add_power_resource(handle
);
2346 acpi_add_single_object(&device
, handle
, type
, sta
);
2348 return AE_CTRL_DEPTH
;
2350 acpi_scan_init_hotplug(device
);
2351 acpi_device_dep_initialize(device
);
2355 *return_value
= device
;
2360 static int acpi_check_spi_i2c_slave(struct acpi_resource
*ares
, void *data
)
2362 bool *is_spi_i2c_slave_p
= data
;
2364 if (ares
->type
!= ACPI_RESOURCE_TYPE_SERIAL_BUS
)
2368 * devices that are connected to UART still need to be enumerated to
2371 if (ares
->data
.common_serial_bus
.type
!= ACPI_RESOURCE_SERIAL_TYPE_UART
)
2372 *is_spi_i2c_slave_p
= true;
2374 /* no need to do more checking */
2378 static void acpi_default_enumeration(struct acpi_device
*device
)
2380 struct list_head resource_list
;
2381 bool is_spi_i2c_slave
= false;
2384 * Do not enemerate SPI/I2C slaves as they will be enuerated by their
2385 * respective parents.
2387 INIT_LIST_HEAD(&resource_list
);
2388 acpi_dev_get_resources(device
, &resource_list
, acpi_check_spi_i2c_slave
,
2390 acpi_dev_free_resource_list(&resource_list
);
2391 if (!is_spi_i2c_slave
)
2392 acpi_create_platform_device(device
);
2395 static const struct acpi_device_id generic_device_ids
[] = {
2396 {ACPI_DT_NAMESPACE_HID
, },
2400 static int acpi_generic_device_attach(struct acpi_device
*adev
,
2401 const struct acpi_device_id
*not_used
)
2404 * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
2405 * below can be unconditional.
2407 if (adev
->data
.of_compatible
)
2408 acpi_default_enumeration(adev
);
2413 static struct acpi_scan_handler generic_device_handler
= {
2414 .ids
= generic_device_ids
,
2415 .attach
= acpi_generic_device_attach
,
2418 static int acpi_scan_attach_handler(struct acpi_device
*device
)
2420 struct acpi_hardware_id
*hwid
;
2423 list_for_each_entry(hwid
, &device
->pnp
.ids
, list
) {
2424 const struct acpi_device_id
*devid
;
2425 struct acpi_scan_handler
*handler
;
2427 handler
= acpi_scan_match_handler(hwid
->id
, &devid
);
2429 if (!handler
->attach
) {
2430 device
->pnp
.type
.platform_id
= 0;
2433 device
->handler
= handler
;
2434 ret
= handler
->attach(device
, devid
);
2438 device
->handler
= NULL
;
2447 static void acpi_bus_attach(struct acpi_device
*device
)
2449 struct acpi_device
*child
;
2453 if (ACPI_SUCCESS(acpi_bus_get_ejd(device
->handle
, &ejd
)))
2454 register_dock_dependent_device(device
, ejd
);
2456 acpi_bus_get_status(device
);
2457 /* Skip devices that are not present. */
2458 if (!acpi_device_is_present(device
)) {
2459 device
->flags
.visited
= false;
2460 device
->flags
.power_manageable
= 0;
2463 if (device
->handler
)
2466 if (!device
->flags
.initialized
) {
2467 device
->flags
.power_manageable
=
2468 device
->power
.states
[ACPI_STATE_D0
].flags
.valid
;
2469 if (acpi_bus_init_power(device
))
2470 device
->flags
.power_manageable
= 0;
2472 device
->flags
.initialized
= true;
2474 device
->flags
.visited
= false;
2475 ret
= acpi_scan_attach_handler(device
);
2479 device
->flags
.match_driver
= true;
2481 ret
= device_attach(&device
->dev
);
2485 if (!ret
&& device
->pnp
.type
.platform_id
)
2486 acpi_default_enumeration(device
);
2488 device
->flags
.visited
= true;
2491 list_for_each_entry(child
, &device
->children
, node
)
2492 acpi_bus_attach(child
);
2494 if (device
->handler
&& device
->handler
->hotplug
.notify_online
)
2495 device
->handler
->hotplug
.notify_online(device
);
2498 void acpi_walk_dep_device_list(acpi_handle handle
)
2500 struct acpi_dep_data
*dep
, *tmp
;
2501 struct acpi_device
*adev
;
2503 mutex_lock(&acpi_dep_list_lock
);
2504 list_for_each_entry_safe(dep
, tmp
, &acpi_dep_list
, node
) {
2505 if (dep
->master
== handle
) {
2506 acpi_bus_get_device(dep
->slave
, &adev
);
2511 if (!adev
->dep_unmet
)
2512 acpi_bus_attach(adev
);
2513 list_del(&dep
->node
);
2517 mutex_unlock(&acpi_dep_list_lock
);
2519 EXPORT_SYMBOL_GPL(acpi_walk_dep_device_list
);
2522 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2523 * @handle: Root of the namespace scope to scan.
2525 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2528 * If no devices were found, -ENODEV is returned, but it does not mean that
2529 * there has been a real error. There just have been no suitable ACPI objects
2530 * in the table trunk from which the kernel could create a device and add an
2531 * appropriate driver.
2533 * Must be called under acpi_scan_lock.
2535 int acpi_bus_scan(acpi_handle handle
)
2537 void *device
= NULL
;
2539 if (ACPI_SUCCESS(acpi_bus_check_add(handle
, 0, NULL
, &device
)))
2540 acpi_walk_namespace(ACPI_TYPE_ANY
, handle
, ACPI_UINT32_MAX
,
2541 acpi_bus_check_add
, NULL
, NULL
, &device
);
2544 acpi_bus_attach(device
);
2549 EXPORT_SYMBOL(acpi_bus_scan
);
2552 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2553 * @adev: Root of the ACPI namespace scope to walk.
2555 * Must be called under acpi_scan_lock.
2557 void acpi_bus_trim(struct acpi_device
*adev
)
2559 struct acpi_scan_handler
*handler
= adev
->handler
;
2560 struct acpi_device
*child
;
2562 list_for_each_entry_reverse(child
, &adev
->children
, node
)
2563 acpi_bus_trim(child
);
2565 adev
->flags
.match_driver
= false;
2567 if (handler
->detach
)
2568 handler
->detach(adev
);
2570 adev
->handler
= NULL
;
2572 device_release_driver(&adev
->dev
);
2575 * Most likely, the device is going away, so put it into D3cold before
2578 acpi_device_set_power(adev
, ACPI_STATE_D3_COLD
);
2579 adev
->flags
.initialized
= false;
2580 adev
->flags
.visited
= false;
2582 EXPORT_SYMBOL_GPL(acpi_bus_trim
);
2584 static int acpi_bus_scan_fixed(void)
2589 * Enumerate all fixed-feature devices.
2591 if (!(acpi_gbl_FADT
.flags
& ACPI_FADT_POWER_BUTTON
)) {
2592 struct acpi_device
*device
= NULL
;
2594 result
= acpi_add_single_object(&device
, NULL
,
2595 ACPI_BUS_TYPE_POWER_BUTTON
,
2600 device
->flags
.match_driver
= true;
2601 result
= device_attach(&device
->dev
);
2605 device_init_wakeup(&device
->dev
, true);
2608 if (!(acpi_gbl_FADT
.flags
& ACPI_FADT_SLEEP_BUTTON
)) {
2609 struct acpi_device
*device
= NULL
;
2611 result
= acpi_add_single_object(&device
, NULL
,
2612 ACPI_BUS_TYPE_SLEEP_BUTTON
,
2617 device
->flags
.match_driver
= true;
2618 result
= device_attach(&device
->dev
);
2621 return result
< 0 ? result
: 0;
2624 int __init
acpi_scan_init(void)
2628 result
= bus_register(&acpi_bus_type
);
2630 /* We don't want to quit even if we failed to add suspend/resume */
2631 printk(KERN_ERR PREFIX
"Could not register bus type\n");
2634 acpi_pci_root_init();
2635 acpi_pci_link_init();
2636 acpi_processor_init();
2639 acpi_cmos_rtc_init();
2640 acpi_container_init();
2641 acpi_memory_hotplug_init();
2643 acpi_int340x_thermal_init();
2645 acpi_scan_add_handler(&generic_device_handler
);
2647 mutex_lock(&acpi_scan_lock
);
2649 * Enumerate devices in the ACPI namespace.
2651 result
= acpi_bus_scan(ACPI_ROOT_OBJECT
);
2655 result
= acpi_bus_get_device(ACPI_ROOT_OBJECT
, &acpi_root
);
2659 /* Fixed feature devices do not exist on HW-reduced platform */
2660 if (!acpi_gbl_reduced_hardware
) {
2661 result
= acpi_bus_scan_fixed();
2663 acpi_detach_data(acpi_root
->handle
,
2664 acpi_scan_drop_device
);
2665 acpi_device_del(acpi_root
);
2666 put_device(&acpi_root
->dev
);
2671 acpi_update_all_gpes();
2674 mutex_unlock(&acpi_scan_lock
);