xref: /openbmc/linux/drivers/acpi/scan.c (revision dc612486c91983a113adefedac030575ea7a4c4a)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * scan.c - support for transforming the ACPI namespace into individual objects
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #include <linux/module.h>
71da177e4SLinus Torvalds #include <linux/init.h>
85a0e3ad6STejun Heo #include <linux/slab.h>
99b6d97b6SRandy Dunlap #include <linux/kernel.h>
101da177e4SLinus Torvalds #include <linux/acpi.h>
11643b8e4dSLorenzo Pieralisi #include <linux/acpi_iort.h>
1274523c90SAlok N Kataria #include <linux/signal.h>
1374523c90SAlok N Kataria #include <linux/kthread.h>
14222e82acSDarrick J. Wong #include <linux/dmi.h>
15d1efe3c3SLance Ortiz #include <linux/nls.h>
160a0f0d8bSChristoph Hellwig #include <linux/dma-map-ops.h>
17ca9ef3abSLukas Wunner #include <linux/platform_data/x86/apple.h>
18ca5999fdSMike Rapoport #include <linux/pgtable.h>
19d783156eSRafael J. Wysocki 
20e60cc7a6SBjorn Helgaas #include "internal.h"
21e60cc7a6SBjorn Helgaas 
221da177e4SLinus Torvalds extern struct acpi_device *acpi_root;
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #define ACPI_BUS_CLASS			"system_bus"
2529b71a1cSThomas Renninger #define ACPI_BUS_HID			"LNXSYBUS"
261da177e4SLinus Torvalds #define ACPI_BUS_DEVICE_NAME		"System Bus"
271da177e4SLinus Torvalds 
28859ac9a4SBjorn Helgaas #define ACPI_IS_ROOT_DEVICE(device)    (!(device)->parent)
29859ac9a4SBjorn Helgaas 
30d783156eSRafael J. Wysocki #define INVALID_ACPI_HANDLE	((acpi_handle)empty_zero_page)
31d783156eSRafael J. Wysocki 
32620e112cSThomas Renninger static const char *dummy_hid = "device";
332b2ae7c7SThomas Renninger 
3440e7fcb1SLan Tianyu static LIST_HEAD(acpi_dep_list);
3540e7fcb1SLan Tianyu static DEFINE_MUTEX(acpi_dep_list_lock);
362d12b6b3SLukas Wunner LIST_HEAD(acpi_bus_id_list);
37c511cc19SRafael J. Wysocki static DEFINE_MUTEX(acpi_scan_lock);
38ca589f94SRafael J. Wysocki static LIST_HEAD(acpi_scan_handlers_list);
399090589dSShaohua Li DEFINE_MUTEX(acpi_device_lock);
401da177e4SLinus Torvalds LIST_HEAD(acpi_wakeup_device_list);
41e525506fSRafael J. Wysocki static DEFINE_MUTEX(acpi_hp_context_lock);
421da177e4SLinus Torvalds 
43a4e081b0SShannon Zhao /*
44a4e081b0SShannon Zhao  * The UART device described by the SPCR table is the only object which needs
45a4e081b0SShannon Zhao  * special-casing. Everything else is covered by ACPI namespace paths in STAO
46a4e081b0SShannon Zhao  * table.
47a4e081b0SShannon Zhao  */
48a4e081b0SShannon Zhao static u64 spcr_uart_addr;
49a4e081b0SShannon Zhao 
503757b948SRafael J. Wysocki void acpi_scan_lock_acquire(void)
513757b948SRafael J. Wysocki {
523757b948SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
533757b948SRafael J. Wysocki }
543757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
553757b948SRafael J. Wysocki 
563757b948SRafael J. Wysocki void acpi_scan_lock_release(void)
573757b948SRafael J. Wysocki {
583757b948SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
593757b948SRafael J. Wysocki }
603757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
613757b948SRafael J. Wysocki 
62e525506fSRafael J. Wysocki void acpi_lock_hp_context(void)
63e525506fSRafael J. Wysocki {
64e525506fSRafael J. Wysocki 	mutex_lock(&acpi_hp_context_lock);
65e525506fSRafael J. Wysocki }
66e525506fSRafael J. Wysocki 
67e525506fSRafael J. Wysocki void acpi_unlock_hp_context(void)
68e525506fSRafael J. Wysocki {
69e525506fSRafael J. Wysocki 	mutex_unlock(&acpi_hp_context_lock);
70e525506fSRafael J. Wysocki }
71e525506fSRafael J. Wysocki 
725d513205SRafael J. Wysocki void acpi_initialize_hp_context(struct acpi_device *adev,
735d513205SRafael J. Wysocki 				struct acpi_hotplug_context *hp,
745d513205SRafael J. Wysocki 				int (*notify)(struct acpi_device *, u32),
755d513205SRafael J. Wysocki 				void (*uevent)(struct acpi_device *, u32))
765d513205SRafael J. Wysocki {
775d513205SRafael J. Wysocki 	acpi_lock_hp_context();
78ba574dc8SRafael J. Wysocki 	hp->notify = notify;
79ba574dc8SRafael J. Wysocki 	hp->uevent = uevent;
80ba574dc8SRafael J. Wysocki 	acpi_set_hp_context(adev, hp);
815d513205SRafael J. Wysocki 	acpi_unlock_hp_context();
825d513205SRafael J. Wysocki }
835d513205SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
845d513205SRafael J. Wysocki 
85ca589f94SRafael J. Wysocki int acpi_scan_add_handler(struct acpi_scan_handler *handler)
86ca589f94SRafael J. Wysocki {
87d34afa9dSRafael J. Wysocki 	if (!handler)
88ca589f94SRafael J. Wysocki 		return -EINVAL;
89ca589f94SRafael J. Wysocki 
90ca589f94SRafael J. Wysocki 	list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
91ca589f94SRafael J. Wysocki 	return 0;
92ca589f94SRafael J. Wysocki }
93ca589f94SRafael J. Wysocki 
943f8055c3SRafael J. Wysocki int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
953f8055c3SRafael J. Wysocki 				       const char *hotplug_profile_name)
963f8055c3SRafael J. Wysocki {
973f8055c3SRafael J. Wysocki 	int error;
983f8055c3SRafael J. Wysocki 
993f8055c3SRafael J. Wysocki 	error = acpi_scan_add_handler(handler);
1003f8055c3SRafael J. Wysocki 	if (error)
1013f8055c3SRafael J. Wysocki 		return error;
1023f8055c3SRafael J. Wysocki 
1033f8055c3SRafael J. Wysocki 	acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
1043f8055c3SRafael J. Wysocki 	return 0;
1053f8055c3SRafael J. Wysocki }
1063f8055c3SRafael J. Wysocki 
107caa73ea1SRafael J. Wysocki bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
108d22ddcbcSRafael J. Wysocki {
109d22ddcbcSRafael J. Wysocki 	struct acpi_device_physical_node *pn;
110d22ddcbcSRafael J. Wysocki 	bool offline = true;
11127664c58SLee, Chun-Yi 	char *envp[] = { "EVENT=offline", NULL };
112d22ddcbcSRafael J. Wysocki 
1134c533c80SRafael J. Wysocki 	/*
1144c533c80SRafael J. Wysocki 	 * acpi_container_offline() calls this for all of the container's
1154c533c80SRafael J. Wysocki 	 * children under the container's physical_node_lock lock.
1164c533c80SRafael J. Wysocki 	 */
1174c533c80SRafael J. Wysocki 	mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING);
118d22ddcbcSRafael J. Wysocki 
119d22ddcbcSRafael J. Wysocki 	list_for_each_entry(pn, &adev->physical_node_list, node)
120d22ddcbcSRafael J. Wysocki 		if (device_supports_offline(pn->dev) && !pn->dev->offline) {
121caa73ea1SRafael J. Wysocki 			if (uevent)
12227664c58SLee, Chun-Yi 				kobject_uevent_env(&pn->dev->kobj, KOBJ_CHANGE, envp);
123caa73ea1SRafael J. Wysocki 
124d22ddcbcSRafael J. Wysocki 			offline = false;
125d22ddcbcSRafael J. Wysocki 			break;
126d22ddcbcSRafael J. Wysocki 		}
127d22ddcbcSRafael J. Wysocki 
128d22ddcbcSRafael J. Wysocki 	mutex_unlock(&adev->physical_node_lock);
129d22ddcbcSRafael J. Wysocki 	return offline;
130d22ddcbcSRafael J. Wysocki }
131d22ddcbcSRafael J. Wysocki 
1327f28ddecSRafael J. Wysocki static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
1337f28ddecSRafael J. Wysocki 				    void **ret_p)
134683058e3SRafael J. Wysocki {
135683058e3SRafael J. Wysocki 	struct acpi_device *device = NULL;
136683058e3SRafael J. Wysocki 	struct acpi_device_physical_node *pn;
137303bfdb1SRafael J. Wysocki 	bool second_pass = (bool)data;
138683058e3SRafael J. Wysocki 	acpi_status status = AE_OK;
139683058e3SRafael J. Wysocki 
140683058e3SRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
141683058e3SRafael J. Wysocki 		return AE_OK;
142683058e3SRafael J. Wysocki 
1437f28ddecSRafael J. Wysocki 	if (device->handler && !device->handler->hotplug.enabled) {
1447f28ddecSRafael J. Wysocki 		*ret_p = &device->dev;
1457f28ddecSRafael J. Wysocki 		return AE_SUPPORT;
1467f28ddecSRafael J. Wysocki 	}
1477f28ddecSRafael J. Wysocki 
148683058e3SRafael J. Wysocki 	mutex_lock(&device->physical_node_lock);
149683058e3SRafael J. Wysocki 
150683058e3SRafael J. Wysocki 	list_for_each_entry(pn, &device->physical_node_list, node) {
151683058e3SRafael J. Wysocki 		int ret;
152683058e3SRafael J. Wysocki 
153303bfdb1SRafael J. Wysocki 		if (second_pass) {
154303bfdb1SRafael J. Wysocki 			/* Skip devices offlined by the first pass. */
155303bfdb1SRafael J. Wysocki 			if (pn->put_online)
156303bfdb1SRafael J. Wysocki 				continue;
157303bfdb1SRafael J. Wysocki 		} else {
158303bfdb1SRafael J. Wysocki 			pn->put_online = false;
159303bfdb1SRafael J. Wysocki 		}
160683058e3SRafael J. Wysocki 		ret = device_offline(pn->dev);
161303bfdb1SRafael J. Wysocki 		if (ret >= 0) {
162303bfdb1SRafael J. Wysocki 			pn->put_online = !ret;
163303bfdb1SRafael J. Wysocki 		} else {
164303bfdb1SRafael J. Wysocki 			*ret_p = pn->dev;
165303bfdb1SRafael J. Wysocki 			if (second_pass) {
166683058e3SRafael J. Wysocki 				status = AE_ERROR;
167683058e3SRafael J. Wysocki 				break;
168683058e3SRafael J. Wysocki 			}
169303bfdb1SRafael J. Wysocki 		}
170683058e3SRafael J. Wysocki 	}
171683058e3SRafael J. Wysocki 
172683058e3SRafael J. Wysocki 	mutex_unlock(&device->physical_node_lock);
173683058e3SRafael J. Wysocki 
174683058e3SRafael J. Wysocki 	return status;
175683058e3SRafael J. Wysocki }
176683058e3SRafael J. Wysocki 
1777f28ddecSRafael J. Wysocki static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
1787f28ddecSRafael J. Wysocki 				   void **ret_p)
179683058e3SRafael J. Wysocki {
180683058e3SRafael J. Wysocki 	struct acpi_device *device = NULL;
181683058e3SRafael J. Wysocki 	struct acpi_device_physical_node *pn;
182683058e3SRafael J. Wysocki 
183683058e3SRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
184683058e3SRafael J. Wysocki 		return AE_OK;
185683058e3SRafael J. Wysocki 
186683058e3SRafael J. Wysocki 	mutex_lock(&device->physical_node_lock);
187683058e3SRafael J. Wysocki 
188683058e3SRafael J. Wysocki 	list_for_each_entry(pn, &device->physical_node_list, node)
189683058e3SRafael J. Wysocki 		if (pn->put_online) {
190683058e3SRafael J. Wysocki 			device_online(pn->dev);
191683058e3SRafael J. Wysocki 			pn->put_online = false;
192683058e3SRafael J. Wysocki 		}
193683058e3SRafael J. Wysocki 
194683058e3SRafael J. Wysocki 	mutex_unlock(&device->physical_node_lock);
195683058e3SRafael J. Wysocki 
196683058e3SRafael J. Wysocki 	return AE_OK;
197683058e3SRafael J. Wysocki }
198683058e3SRafael J. Wysocki 
199d22ddcbcSRafael J. Wysocki static int acpi_scan_try_to_offline(struct acpi_device *device)
2001da177e4SLinus Torvalds {
2015993c467SYinghai Lu 	acpi_handle handle = device->handle;
202d22ddcbcSRafael J. Wysocki 	struct device *errdev = NULL;
203a33ec399SRafael J. Wysocki 	acpi_status status;
204f058cdf4SRafael J. Wysocki 
205303bfdb1SRafael J. Wysocki 	/*
206303bfdb1SRafael J. Wysocki 	 * Carry out two passes here and ignore errors in the first pass,
207303bfdb1SRafael J. Wysocki 	 * because if the devices in question are memory blocks and
208303bfdb1SRafael J. Wysocki 	 * CONFIG_MEMCG is set, one of the blocks may hold data structures
209303bfdb1SRafael J. Wysocki 	 * that the other blocks depend on, but it is not known in advance which
210303bfdb1SRafael J. Wysocki 	 * block holds them.
211303bfdb1SRafael J. Wysocki 	 *
212303bfdb1SRafael J. Wysocki 	 * If the first pass is successful, the second one isn't needed, though.
213303bfdb1SRafael J. Wysocki 	 */
2147f28ddecSRafael J. Wysocki 	status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2157f28ddecSRafael J. Wysocki 				     NULL, acpi_bus_offline, (void *)false,
2167f28ddecSRafael J. Wysocki 				     (void **)&errdev);
2177f28ddecSRafael J. Wysocki 	if (status == AE_SUPPORT) {
2187f28ddecSRafael J. Wysocki 		dev_warn(errdev, "Offline disabled.\n");
219683058e3SRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2207f28ddecSRafael J. Wysocki 				    acpi_bus_online, NULL, NULL, NULL);
2217f28ddecSRafael J. Wysocki 		return -EPERM;
2227f28ddecSRafael J. Wysocki 	}
2237f28ddecSRafael J. Wysocki 	acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
224303bfdb1SRafael J. Wysocki 	if (errdev) {
225303bfdb1SRafael J. Wysocki 		errdev = NULL;
226303bfdb1SRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2277f28ddecSRafael J. Wysocki 				    NULL, acpi_bus_offline, (void *)true,
2287f28ddecSRafael J. Wysocki 				    (void **)&errdev);
229ffc10d82SMichal Hocko 		if (!errdev)
2307f28ddecSRafael J. Wysocki 			acpi_bus_offline(handle, 0, (void *)true,
231303bfdb1SRafael J. Wysocki 					 (void **)&errdev);
232303bfdb1SRafael J. Wysocki 
233ffc10d82SMichal Hocko 		if (errdev) {
234303bfdb1SRafael J. Wysocki 			dev_warn(errdev, "Offline failed.\n");
2357f28ddecSRafael J. Wysocki 			acpi_bus_online(handle, 0, NULL, NULL);
236303bfdb1SRafael J. Wysocki 			acpi_walk_namespace(ACPI_TYPE_ANY, handle,
2377f28ddecSRafael J. Wysocki 					    ACPI_UINT32_MAX, acpi_bus_online,
2387f28ddecSRafael J. Wysocki 					    NULL, NULL, NULL);
239683058e3SRafael J. Wysocki 			return -EBUSY;
240683058e3SRafael J. Wysocki 		}
241303bfdb1SRafael J. Wysocki 	}
242d22ddcbcSRafael J. Wysocki 	return 0;
243d22ddcbcSRafael J. Wysocki }
244d22ddcbcSRafael J. Wysocki 
245d22ddcbcSRafael J. Wysocki static int acpi_scan_hot_remove(struct acpi_device *device)
246d22ddcbcSRafael J. Wysocki {
247d22ddcbcSRafael J. Wysocki 	acpi_handle handle = device->handle;
248d22ddcbcSRafael J. Wysocki 	unsigned long long sta;
249d22ddcbcSRafael J. Wysocki 	acpi_status status;
250d22ddcbcSRafael J. Wysocki 
251ffc10d82SMichal Hocko 	if (device->handler && device->handler->hotplug.demand_offline) {
252caa73ea1SRafael J. Wysocki 		if (!acpi_scan_is_offline(device, true))
253d22ddcbcSRafael J. Wysocki 			return -EBUSY;
254d22ddcbcSRafael J. Wysocki 	} else {
255d22ddcbcSRafael J. Wysocki 		int error = acpi_scan_try_to_offline(device);
256d22ddcbcSRafael J. Wysocki 		if (error)
257d22ddcbcSRafael J. Wysocki 			return error;
258d22ddcbcSRafael J. Wysocki 	}
259683058e3SRafael J. Wysocki 
260e52d9d8cSRafael J. Wysocki 	acpi_handle_debug(handle, "Ejecting\n");
26126d46867SZhang Rui 
262bfee26dbSRafael J. Wysocki 	acpi_bus_trim(device);
263683058e3SRafael J. Wysocki 
2647d2421f8SJiang Liu 	acpi_evaluate_lck(handle, 0);
2651da177e4SLinus Torvalds 	/*
2661da177e4SLinus Torvalds 	 * TBD: _EJD support.
2671da177e4SLinus Torvalds 	 */
2687d2421f8SJiang Liu 	status = acpi_evaluate_ej0(handle);
2697d2421f8SJiang Liu 	if (status == AE_NOT_FOUND)
270a33ec399SRafael J. Wysocki 		return -ENODEV;
2717d2421f8SJiang Liu 	else if (ACPI_FAILURE(status))
272a33ec399SRafael J. Wysocki 		return -EIO;
273f058cdf4SRafael J. Wysocki 
274882fd12eSToshi Kani 	/*
275882fd12eSToshi Kani 	 * Verify if eject was indeed successful.  If not, log an error
276882fd12eSToshi Kani 	 * message.  No need to call _OST since _EJ0 call was made OK.
277882fd12eSToshi Kani 	 */
278882fd12eSToshi Kani 	status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
279882fd12eSToshi Kani 	if (ACPI_FAILURE(status)) {
280882fd12eSToshi Kani 		acpi_handle_warn(handle,
281882fd12eSToshi Kani 			"Status check after eject failed (0x%x)\n", status);
282882fd12eSToshi Kani 	} else if (sta & ACPI_STA_DEVICE_ENABLED) {
283882fd12eSToshi Kani 		acpi_handle_warn(handle,
284882fd12eSToshi Kani 			"Eject incomplete - status 0x%llx\n", sta);
285882fd12eSToshi Kani 	}
286882fd12eSToshi Kani 
287a33ec399SRafael J. Wysocki 	return 0;
288a33ec399SRafael J. Wysocki }
289f058cdf4SRafael J. Wysocki 
290443fc820SRafael J. Wysocki static int acpi_scan_device_not_present(struct acpi_device *adev)
291443fc820SRafael J. Wysocki {
292443fc820SRafael J. Wysocki 	if (!acpi_device_enumerated(adev)) {
293443fc820SRafael J. Wysocki 		dev_warn(&adev->dev, "Still not present\n");
294443fc820SRafael J. Wysocki 		return -EALREADY;
295443fc820SRafael J. Wysocki 	}
296443fc820SRafael J. Wysocki 	acpi_bus_trim(adev);
297443fc820SRafael J. Wysocki 	return 0;
298443fc820SRafael J. Wysocki }
299443fc820SRafael J. Wysocki 
300c27b2c33SRafael J. Wysocki static int acpi_scan_device_check(struct acpi_device *adev)
301a33ec399SRafael J. Wysocki {
302f943db40SRafael J. Wysocki 	int error;
303a33ec399SRafael J. Wysocki 
304443fc820SRafael J. Wysocki 	acpi_bus_get_status(adev);
305443fc820SRafael J. Wysocki 	if (adev->status.present || adev->status.functional) {
306443fc820SRafael J. Wysocki 		/*
307443fc820SRafael J. Wysocki 		 * This function is only called for device objects for which
308443fc820SRafael J. Wysocki 		 * matching scan handlers exist.  The only situation in which
309443fc820SRafael J. Wysocki 		 * the scan handler is not attached to this device object yet
310443fc820SRafael J. Wysocki 		 * is when the device has just appeared (either it wasn't
311443fc820SRafael J. Wysocki 		 * present at all before or it was removed and then added
312443fc820SRafael J. Wysocki 		 * again).
313443fc820SRafael J. Wysocki 		 */
314443fc820SRafael J. Wysocki 		if (adev->handler) {
315443fc820SRafael J. Wysocki 			dev_warn(&adev->dev, "Already enumerated\n");
316443fc820SRafael J. Wysocki 			return -EALREADY;
317443fc820SRafael J. Wysocki 		}
318c27b2c33SRafael J. Wysocki 		error = acpi_bus_scan(adev->handle);
319a33ec399SRafael J. Wysocki 		if (error) {
320c27b2c33SRafael J. Wysocki 			dev_warn(&adev->dev, "Namespace scan failure\n");
321c27b2c33SRafael J. Wysocki 			return error;
322a33ec399SRafael J. Wysocki 		}
32346394fd0SRafael J. Wysocki 		if (!adev->handler) {
324c27b2c33SRafael J. Wysocki 			dev_warn(&adev->dev, "Enumeration failure\n");
325443fc820SRafael J. Wysocki 			error = -ENODEV;
326443fc820SRafael J. Wysocki 		}
327443fc820SRafael J. Wysocki 	} else {
328443fc820SRafael J. Wysocki 		error = acpi_scan_device_not_present(adev);
329443fc820SRafael J. Wysocki 	}
330443fc820SRafael J. Wysocki 	return error;
331443fc820SRafael J. Wysocki }
332443fc820SRafael J. Wysocki 
333443fc820SRafael J. Wysocki static int acpi_scan_bus_check(struct acpi_device *adev)
334443fc820SRafael J. Wysocki {
335443fc820SRafael J. Wysocki 	struct acpi_scan_handler *handler = adev->handler;
336443fc820SRafael J. Wysocki 	struct acpi_device *child;
337443fc820SRafael J. Wysocki 	int error;
338443fc820SRafael J. Wysocki 
339443fc820SRafael J. Wysocki 	acpi_bus_get_status(adev);
340443fc820SRafael J. Wysocki 	if (!(adev->status.present || adev->status.functional)) {
341443fc820SRafael J. Wysocki 		acpi_scan_device_not_present(adev);
342443fc820SRafael J. Wysocki 		return 0;
343443fc820SRafael J. Wysocki 	}
344443fc820SRafael J. Wysocki 	if (handler && handler->hotplug.scan_dependent)
345443fc820SRafael J. Wysocki 		return handler->hotplug.scan_dependent(adev);
346443fc820SRafael J. Wysocki 
347443fc820SRafael J. Wysocki 	error = acpi_bus_scan(adev->handle);
348443fc820SRafael J. Wysocki 	if (error) {
349443fc820SRafael J. Wysocki 		dev_warn(&adev->dev, "Namespace scan failure\n");
350443fc820SRafael J. Wysocki 		return error;
351443fc820SRafael J. Wysocki 	}
352443fc820SRafael J. Wysocki 	list_for_each_entry(child, &adev->children, node) {
353443fc820SRafael J. Wysocki 		error = acpi_scan_bus_check(child);
354443fc820SRafael J. Wysocki 		if (error)
355443fc820SRafael J. Wysocki 			return error;
356a33ec399SRafael J. Wysocki 	}
357c27b2c33SRafael J. Wysocki 	return 0;
358c27b2c33SRafael J. Wysocki }
359c27b2c33SRafael J. Wysocki 
3603c2cc7ffSRafael J. Wysocki static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
3613c2cc7ffSRafael J. Wysocki {
3623c2cc7ffSRafael J. Wysocki 	switch (type) {
3633c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_BUS_CHECK:
3643c2cc7ffSRafael J. Wysocki 		return acpi_scan_bus_check(adev);
3653c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_DEVICE_CHECK:
3663c2cc7ffSRafael J. Wysocki 		return acpi_scan_device_check(adev);
3673c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_EJECT_REQUEST:
3683c2cc7ffSRafael J. Wysocki 	case ACPI_OST_EC_OSPM_EJECT:
369dd2151beSRafael J. Wysocki 		if (adev->handler && !adev->handler->hotplug.enabled) {
370dd2151beSRafael J. Wysocki 			dev_info(&adev->dev, "Eject disabled\n");
371dd2151beSRafael J. Wysocki 			return -EPERM;
372dd2151beSRafael J. Wysocki 		}
373700b8422SRafael J. Wysocki 		acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
374dd2151beSRafael J. Wysocki 				  ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
3753c2cc7ffSRafael J. Wysocki 		return acpi_scan_hot_remove(adev);
3763c2cc7ffSRafael J. Wysocki 	}
3773c2cc7ffSRafael J. Wysocki 	return -EINVAL;
3783c2cc7ffSRafael J. Wysocki }
3793c2cc7ffSRafael J. Wysocki 
3801e3bcb59SRafael J. Wysocki void acpi_device_hotplug(struct acpi_device *adev, u32 src)
381c27b2c33SRafael J. Wysocki {
382c27b2c33SRafael J. Wysocki 	u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
3833c2cc7ffSRafael J. Wysocki 	int error = -ENODEV;
384c27b2c33SRafael J. Wysocki 
385c27b2c33SRafael J. Wysocki 	lock_device_hotplug();
386c27b2c33SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
387c27b2c33SRafael J. Wysocki 
388c27b2c33SRafael J. Wysocki 	/*
389c27b2c33SRafael J. Wysocki 	 * The device object's ACPI handle cannot become invalid as long as we
3903c2cc7ffSRafael J. Wysocki 	 * are holding acpi_scan_lock, but it might have become invalid before
391c27b2c33SRafael J. Wysocki 	 * that lock was acquired.
392c27b2c33SRafael J. Wysocki 	 */
393c27b2c33SRafael J. Wysocki 	if (adev->handle == INVALID_ACPI_HANDLE)
3943c2cc7ffSRafael J. Wysocki 		goto err_out;
395c27b2c33SRafael J. Wysocki 
3961e2380cdSRafael J. Wysocki 	if (adev->flags.is_dock_station) {
3971e2380cdSRafael J. Wysocki 		error = dock_notify(adev, src);
3981e2380cdSRafael J. Wysocki 	} else if (adev->flags.hotplug_notify) {
3993c2cc7ffSRafael J. Wysocki 		error = acpi_generic_hotplug_event(adev, src);
4003c2cc7ffSRafael J. Wysocki 	} else {
401be27b3dcSRafael J. Wysocki 		int (*notify)(struct acpi_device *, u32);
4023c2cc7ffSRafael J. Wysocki 
4033c2cc7ffSRafael J. Wysocki 		acpi_lock_hp_context();
404be27b3dcSRafael J. Wysocki 		notify = adev->hp ? adev->hp->notify : NULL;
4053c2cc7ffSRafael J. Wysocki 		acpi_unlock_hp_context();
4063c2cc7ffSRafael J. Wysocki 		/*
4073c2cc7ffSRafael J. Wysocki 		 * There may be additional notify handlers for device objects
4083c2cc7ffSRafael J. Wysocki 		 * without the .event() callback, so ignore them here.
4093c2cc7ffSRafael J. Wysocki 		 */
410be27b3dcSRafael J. Wysocki 		if (notify)
411be27b3dcSRafael J. Wysocki 			error = notify(adev, src);
4123c2cc7ffSRafael J. Wysocki 		else
4133c2cc7ffSRafael J. Wysocki 			goto out;
414c27b2c33SRafael J. Wysocki 	}
415d429e5c1SLee, Chun-Yi 	switch (error) {
416d429e5c1SLee, Chun-Yi 	case 0:
417a33ec399SRafael J. Wysocki 		ost_code = ACPI_OST_SC_SUCCESS;
418d429e5c1SLee, Chun-Yi 		break;
419d429e5c1SLee, Chun-Yi 	case -EPERM:
420d429e5c1SLee, Chun-Yi 		ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
421d429e5c1SLee, Chun-Yi 		break;
422d429e5c1SLee, Chun-Yi 	case -EBUSY:
423d429e5c1SLee, Chun-Yi 		ost_code = ACPI_OST_SC_DEVICE_BUSY;
424d429e5c1SLee, Chun-Yi 		break;
425d429e5c1SLee, Chun-Yi 	default:
426d429e5c1SLee, Chun-Yi 		ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
427d429e5c1SLee, Chun-Yi 		break;
428d429e5c1SLee, Chun-Yi 	}
429a33ec399SRafael J. Wysocki 
4303c2cc7ffSRafael J. Wysocki  err_out:
431700b8422SRafael J. Wysocki 	acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
4323c2cc7ffSRafael J. Wysocki 
4333c2cc7ffSRafael J. Wysocki  out:
43478ea4639SRafael J. Wysocki 	acpi_bus_put_acpi_device(adev);
435a33ec399SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
436e0ae8feeSRafael J. Wysocki 	unlock_device_hotplug();
437a33ec399SRafael J. Wysocki }
438a33ec399SRafael J. Wysocki 
4390b224527SRafael J. Wysocki static void acpi_free_power_resources_lists(struct acpi_device *device)
4400b224527SRafael J. Wysocki {
4410b224527SRafael J. Wysocki 	int i;
4420b224527SRafael J. Wysocki 
443993cbe59SRafael J. Wysocki 	if (device->wakeup.flags.valid)
444993cbe59SRafael J. Wysocki 		acpi_power_resources_list_free(&device->wakeup.resources);
445993cbe59SRafael J. Wysocki 
4461b1f3e16SRafael J. Wysocki 	if (!device->power.flags.power_resources)
4470b224527SRafael J. Wysocki 		return;
4480b224527SRafael J. Wysocki 
4490b224527SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
4500b224527SRafael J. Wysocki 		struct acpi_device_power_state *ps = &device->power.states[i];
4510b224527SRafael J. Wysocki 		acpi_power_resources_list_free(&ps->resources);
4520b224527SRafael J. Wysocki 	}
4537f47fa6cSBjorn Helgaas }
4547f47fa6cSBjorn Helgaas 
4551890a97aSPatrick Mochel static void acpi_device_release(struct device *dev)
4561890a97aSPatrick Mochel {
4571890a97aSPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
4581890a97aSPatrick Mochel 
459ffdcd955SMika Westerberg 	acpi_free_properties(acpi_dev);
460c0af4175SToshi Kani 	acpi_free_pnp_ids(&acpi_dev->pnp);
4610b224527SRafael J. Wysocki 	acpi_free_power_resources_lists(acpi_dev);
4621890a97aSPatrick Mochel 	kfree(acpi_dev);
4631890a97aSPatrick Mochel }
4641890a97aSPatrick Mochel 
465d783156eSRafael J. Wysocki static void acpi_device_del(struct acpi_device *device)
466caf5c03fSRafael J. Wysocki {
467ca9dc8d4SLukas Wunner 	struct acpi_device_bus_id *acpi_device_bus_id;
468ca9dc8d4SLukas Wunner 
469d783156eSRafael J. Wysocki 	mutex_lock(&acpi_device_lock);
470d783156eSRafael J. Wysocki 	if (device->parent)
471d783156eSRafael J. Wysocki 		list_del(&device->node);
472d783156eSRafael J. Wysocki 
473ca9dc8d4SLukas Wunner 	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node)
474ca9dc8d4SLukas Wunner 		if (!strcmp(acpi_device_bus_id->bus_id,
475ca9dc8d4SLukas Wunner 			    acpi_device_hid(device))) {
476eb50aaf9SAndy Shevchenko 			ida_simple_remove(&acpi_device_bus_id->instance_ida, device->pnp.instance_no);
477eb50aaf9SAndy Shevchenko 			if (ida_is_empty(&acpi_device_bus_id->instance_ida)) {
478ca9dc8d4SLukas Wunner 				list_del(&acpi_device_bus_id->node);
479a58015d6SDexuan Cui 				kfree_const(acpi_device_bus_id->bus_id);
480ca9dc8d4SLukas Wunner 				kfree(acpi_device_bus_id);
481ca9dc8d4SLukas Wunner 			}
482ca9dc8d4SLukas Wunner 			break;
483ca9dc8d4SLukas Wunner 		}
484ca9dc8d4SLukas Wunner 
485d783156eSRafael J. Wysocki 	list_del(&device->wakeup_list);
486d783156eSRafael J. Wysocki 	mutex_unlock(&acpi_device_lock);
487d783156eSRafael J. Wysocki 
488d783156eSRafael J. Wysocki 	acpi_power_add_remove_device(device, false);
489d783156eSRafael J. Wysocki 	acpi_device_remove_files(device);
490d783156eSRafael J. Wysocki 	if (device->remove)
491d783156eSRafael J. Wysocki 		device->remove(device);
492d783156eSRafael J. Wysocki 
493d783156eSRafael J. Wysocki 	device_del(&device->dev);
494d783156eSRafael J. Wysocki }
495d783156eSRafael J. Wysocki 
49668bdb677SOctavian Purdila static BLOCKING_NOTIFIER_HEAD(acpi_reconfig_chain);
49768bdb677SOctavian Purdila 
498d783156eSRafael J. Wysocki static LIST_HEAD(acpi_device_del_list);
499d783156eSRafael J. Wysocki static DEFINE_MUTEX(acpi_device_del_lock);
500d783156eSRafael J. Wysocki 
501d783156eSRafael J. Wysocki static void acpi_device_del_work_fn(struct work_struct *work_not_used)
502d783156eSRafael J. Wysocki {
503d783156eSRafael J. Wysocki 	for (;;) {
504d783156eSRafael J. Wysocki 		struct acpi_device *adev;
505d783156eSRafael J. Wysocki 
506d783156eSRafael J. Wysocki 		mutex_lock(&acpi_device_del_lock);
507d783156eSRafael J. Wysocki 
508d783156eSRafael J. Wysocki 		if (list_empty(&acpi_device_del_list)) {
509d783156eSRafael J. Wysocki 			mutex_unlock(&acpi_device_del_lock);
510d783156eSRafael J. Wysocki 			break;
511d783156eSRafael J. Wysocki 		}
512d783156eSRafael J. Wysocki 		adev = list_first_entry(&acpi_device_del_list,
513d783156eSRafael J. Wysocki 					struct acpi_device, del_list);
514d783156eSRafael J. Wysocki 		list_del(&adev->del_list);
515d783156eSRafael J. Wysocki 
516d783156eSRafael J. Wysocki 		mutex_unlock(&acpi_device_del_lock);
517d783156eSRafael J. Wysocki 
51868bdb677SOctavian Purdila 		blocking_notifier_call_chain(&acpi_reconfig_chain,
51968bdb677SOctavian Purdila 					     ACPI_RECONFIG_DEVICE_REMOVE, adev);
52068bdb677SOctavian Purdila 
521d783156eSRafael J. Wysocki 		acpi_device_del(adev);
522d783156eSRafael J. Wysocki 		/*
523d783156eSRafael J. Wysocki 		 * Drop references to all power resources that might have been
524d783156eSRafael J. Wysocki 		 * used by the device.
525d783156eSRafael J. Wysocki 		 */
526d783156eSRafael J. Wysocki 		acpi_power_transition(adev, ACPI_STATE_D3_COLD);
5274cbaba4eSAndy Shevchenko 		acpi_dev_put(adev);
528d783156eSRafael J. Wysocki 	}
529d783156eSRafael J. Wysocki }
530d783156eSRafael J. Wysocki 
531d783156eSRafael J. Wysocki /**
532d783156eSRafael J. Wysocki  * acpi_scan_drop_device - Drop an ACPI device object.
533d783156eSRafael J. Wysocki  * @handle: Handle of an ACPI namespace node, not used.
534d783156eSRafael J. Wysocki  * @context: Address of the ACPI device object to drop.
535d783156eSRafael J. Wysocki  *
536d783156eSRafael J. Wysocki  * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
537d783156eSRafael J. Wysocki  * namespace node the device object pointed to by @context is attached to.
538d783156eSRafael J. Wysocki  *
539d783156eSRafael J. Wysocki  * The unregistration is carried out asynchronously to avoid running
540d783156eSRafael J. Wysocki  * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
541d783156eSRafael J. Wysocki  * ensure the correct ordering (the device objects must be unregistered in the
542d783156eSRafael J. Wysocki  * same order in which the corresponding namespace nodes are deleted).
543d783156eSRafael J. Wysocki  */
544d783156eSRafael J. Wysocki static void acpi_scan_drop_device(acpi_handle handle, void *context)
545d783156eSRafael J. Wysocki {
546d783156eSRafael J. Wysocki 	static DECLARE_WORK(work, acpi_device_del_work_fn);
547d783156eSRafael J. Wysocki 	struct acpi_device *adev = context;
548d783156eSRafael J. Wysocki 
549d783156eSRafael J. Wysocki 	mutex_lock(&acpi_device_del_lock);
550d783156eSRafael J. Wysocki 
551d783156eSRafael J. Wysocki 	/*
552d783156eSRafael J. Wysocki 	 * Use the ACPI hotplug workqueue which is ordered, so this work item
553d783156eSRafael J. Wysocki 	 * won't run after any hotplug work items submitted subsequently.  That
554d783156eSRafael J. Wysocki 	 * prevents attempts to register device objects identical to those being
555d783156eSRafael J. Wysocki 	 * deleted from happening concurrently (such attempts result from
556d783156eSRafael J. Wysocki 	 * hotplug events handled via the ACPI hotplug workqueue).  It also will
557935ab850STom Saeger 	 * run after all of the work items submitted previously, which helps
558d783156eSRafael J. Wysocki 	 * those work items to ensure that they are not accessing stale device
559d783156eSRafael J. Wysocki 	 * objects.
560d783156eSRafael J. Wysocki 	 */
561d783156eSRafael J. Wysocki 	if (list_empty(&acpi_device_del_list))
562d783156eSRafael J. Wysocki 		acpi_queue_hotplug_work(&work);
563d783156eSRafael J. Wysocki 
564d783156eSRafael J. Wysocki 	list_add_tail(&adev->del_list, &acpi_device_del_list);
565d783156eSRafael J. Wysocki 	/* Make acpi_ns_validate_handle() return NULL for this handle. */
566d783156eSRafael J. Wysocki 	adev->handle = INVALID_ACPI_HANDLE;
567d783156eSRafael J. Wysocki 
568d783156eSRafael J. Wysocki 	mutex_unlock(&acpi_device_del_lock);
569caf5c03fSRafael J. Wysocki }
570caf5c03fSRafael J. Wysocki 
57183e2c8fcSRafael J. Wysocki static struct acpi_device *handle_to_device(acpi_handle handle,
57278ea4639SRafael J. Wysocki 					    void (*callback)(void *))
573caf5c03fSRafael J. Wysocki {
57483e2c8fcSRafael J. Wysocki 	struct acpi_device *adev = NULL;
575caf5c03fSRafael J. Wysocki 	acpi_status status;
576caf5c03fSRafael J. Wysocki 
57778ea4639SRafael J. Wysocki 	status = acpi_get_data_full(handle, acpi_scan_drop_device,
57883e2c8fcSRafael J. Wysocki 				    (void **)&adev, callback);
57983e2c8fcSRafael J. Wysocki 	if (ACPI_FAILURE(status) || !adev) {
58083e2c8fcSRafael J. Wysocki 		acpi_handle_debug(handle, "No context!\n");
58183e2c8fcSRafael J. Wysocki 		return NULL;
582caf5c03fSRafael J. Wysocki 	}
58383e2c8fcSRafael J. Wysocki 	return adev;
584caf5c03fSRafael J. Wysocki }
58578ea4639SRafael J. Wysocki 
58678ea4639SRafael J. Wysocki int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
58778ea4639SRafael J. Wysocki {
58883e2c8fcSRafael J. Wysocki 	if (!device)
58983e2c8fcSRafael J. Wysocki 		return -EINVAL;
59083e2c8fcSRafael J. Wysocki 
59183e2c8fcSRafael J. Wysocki 	*device = handle_to_device(handle, NULL);
59283e2c8fcSRafael J. Wysocki 	if (!*device)
59383e2c8fcSRafael J. Wysocki 		return -ENODEV;
59483e2c8fcSRafael J. Wysocki 
59583e2c8fcSRafael J. Wysocki 	return 0;
59678ea4639SRafael J. Wysocki }
5976585925bSRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_get_device);
598caf5c03fSRafael J. Wysocki 
59978ea4639SRafael J. Wysocki static void get_acpi_device(void *dev)
60078ea4639SRafael J. Wysocki {
6014cbaba4eSAndy Shevchenko 	acpi_dev_get(dev);
60278ea4639SRafael J. Wysocki }
60378ea4639SRafael J. Wysocki 
60478ea4639SRafael J. Wysocki struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
60578ea4639SRafael J. Wysocki {
60683e2c8fcSRafael J. Wysocki 	return handle_to_device(handle, get_acpi_device);
60778ea4639SRafael J. Wysocki }
60878ea4639SRafael J. Wysocki 
609c1013ff7SRafael J. Wysocki static struct acpi_device_bus_id *acpi_device_bus_id_match(const char *dev_id)
610c1013ff7SRafael J. Wysocki {
611c1013ff7SRafael J. Wysocki 	struct acpi_device_bus_id *acpi_device_bus_id;
612c1013ff7SRafael J. Wysocki 
613c1013ff7SRafael J. Wysocki 	/* Find suitable bus_id and instance number in acpi_bus_id_list. */
614c1013ff7SRafael J. Wysocki 	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
615c1013ff7SRafael J. Wysocki 		if (!strcmp(acpi_device_bus_id->bus_id, dev_id))
616c1013ff7SRafael J. Wysocki 			return acpi_device_bus_id;
617c1013ff7SRafael J. Wysocki 	}
618c1013ff7SRafael J. Wysocki 	return NULL;
619c1013ff7SRafael J. Wysocki }
620c1013ff7SRafael J. Wysocki 
621eb50aaf9SAndy Shevchenko static int acpi_device_set_name(struct acpi_device *device,
622eb50aaf9SAndy Shevchenko 				struct acpi_device_bus_id *acpi_device_bus_id)
623eb50aaf9SAndy Shevchenko {
624eb50aaf9SAndy Shevchenko 	struct ida *instance_ida = &acpi_device_bus_id->instance_ida;
625eb50aaf9SAndy Shevchenko 	int result;
626eb50aaf9SAndy Shevchenko 
627eb50aaf9SAndy Shevchenko 	result = ida_simple_get(instance_ida, 0, ACPI_MAX_DEVICE_INSTANCES, GFP_KERNEL);
628eb50aaf9SAndy Shevchenko 	if (result < 0)
629eb50aaf9SAndy Shevchenko 		return result;
630eb50aaf9SAndy Shevchenko 
631eb50aaf9SAndy Shevchenko 	device->pnp.instance_no = result;
632eb50aaf9SAndy Shevchenko 	dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, result);
633eb50aaf9SAndy Shevchenko 	return 0;
634eb50aaf9SAndy Shevchenko }
635eb50aaf9SAndy Shevchenko 
636cf860be6SRafael J. Wysocki int acpi_device_add(struct acpi_device *device,
63782c7d5efSRafael J. Wysocki 		    void (*release)(struct device *))
6381da177e4SLinus Torvalds {
639c1013ff7SRafael J. Wysocki 	struct acpi_device_bus_id *acpi_device_bus_id;
640e49bd2ddSZhang Rui 	int result;
64166b7ed40SBjorn Helgaas 
642d43e167dSRafael J. Wysocki 	if (device->handle) {
643d43e167dSRafael J. Wysocki 		acpi_status status;
644d43e167dSRafael J. Wysocki 
645d783156eSRafael J. Wysocki 		status = acpi_attach_data(device->handle, acpi_scan_drop_device,
646d43e167dSRafael J. Wysocki 					  device);
647d43e167dSRafael J. Wysocki 		if (ACPI_FAILURE(status)) {
648d43e167dSRafael J. Wysocki 			acpi_handle_err(device->handle,
649d43e167dSRafael J. Wysocki 					"Unable to attach device data\n");
650d43e167dSRafael J. Wysocki 			return -ENODEV;
651d43e167dSRafael J. Wysocki 		}
652d43e167dSRafael J. Wysocki 	}
653d43e167dSRafael J. Wysocki 
6541da177e4SLinus Torvalds 	/*
6551da177e4SLinus Torvalds 	 * Linkage
6561da177e4SLinus Torvalds 	 * -------
6571da177e4SLinus Torvalds 	 * Link this device to its parent and siblings.
6581da177e4SLinus Torvalds 	 */
6591da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->children);
6601da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->node);
6611da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->wakeup_list);
6621033f904SLan Tianyu 	INIT_LIST_HEAD(&device->physical_node_list);
663d783156eSRafael J. Wysocki 	INIT_LIST_HEAD(&device->del_list);
6641033f904SLan Tianyu 	mutex_init(&device->physical_node_lock);
6651da177e4SLinus Torvalds 
6669090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
667c1013ff7SRafael J. Wysocki 
668c1013ff7SRafael J. Wysocki 	acpi_device_bus_id = acpi_device_bus_id_match(acpi_device_hid(device));
669c1013ff7SRafael J. Wysocki 	if (acpi_device_bus_id) {
670eb50aaf9SAndy Shevchenko 		result = acpi_device_set_name(device, acpi_device_bus_id);
671eb50aaf9SAndy Shevchenko 		if (result)
672eb50aaf9SAndy Shevchenko 			goto err_unlock;
673c1013ff7SRafael J. Wysocki 	} else {
674c1013ff7SRafael J. Wysocki 		acpi_device_bus_id = kzalloc(sizeof(*acpi_device_bus_id),
675c1013ff7SRafael J. Wysocki 					     GFP_KERNEL);
676c1013ff7SRafael J. Wysocki 		if (!acpi_device_bus_id) {
677c1013ff7SRafael J. Wysocki 			result = -ENOMEM;
678c1013ff7SRafael J. Wysocki 			goto err_unlock;
679e49bd2ddSZhang Rui 		}
680a58015d6SDexuan Cui 		acpi_device_bus_id->bus_id =
681a58015d6SDexuan Cui 			kstrdup_const(acpi_device_hid(device), GFP_KERNEL);
682a58015d6SDexuan Cui 		if (!acpi_device_bus_id->bus_id) {
683c1013ff7SRafael J. Wysocki 			kfree(acpi_device_bus_id);
684a58015d6SDexuan Cui 			result = -ENOMEM;
685c1013ff7SRafael J. Wysocki 			goto err_unlock;
686a58015d6SDexuan Cui 		}
687a58015d6SDexuan Cui 
688eb50aaf9SAndy Shevchenko 		ida_init(&acpi_device_bus_id->instance_ida);
689eb50aaf9SAndy Shevchenko 
690eb50aaf9SAndy Shevchenko 		result = acpi_device_set_name(device, acpi_device_bus_id);
691eb50aaf9SAndy Shevchenko 		if (result) {
6920c8bd174SChristophe JAILLET 			kfree_const(acpi_device_bus_id->bus_id);
693eb50aaf9SAndy Shevchenko 			kfree(acpi_device_bus_id);
694eb50aaf9SAndy Shevchenko 			goto err_unlock;
695eb50aaf9SAndy Shevchenko 		}
696eb50aaf9SAndy Shevchenko 
697e49bd2ddSZhang Rui 		list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
698e49bd2ddSZhang Rui 	}
699e49bd2ddSZhang Rui 
70033b57150SLen Brown 	if (device->parent)
7011da177e4SLinus Torvalds 		list_add_tail(&device->node, &device->parent->children);
70233b57150SLen Brown 
7031da177e4SLinus Torvalds 	if (device->wakeup.flags.valid)
7041da177e4SLinus Torvalds 		list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
7055e73c518SRafael J. Wysocki 
7069090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
7071da177e4SLinus Torvalds 
7081890a97aSPatrick Mochel 	if (device->parent)
70966b7ed40SBjorn Helgaas 		device->dev.parent = &device->parent->dev;
7105e73c518SRafael J. Wysocki 
7111890a97aSPatrick Mochel 	device->dev.bus = &acpi_bus_type;
71282c7d5efSRafael J. Wysocki 	device->dev.release = release;
713cf860be6SRafael J. Wysocki 	result = device_add(&device->dev);
714e49bd2ddSZhang Rui 	if (result) {
7158b12b922SAlex Chiang 		dev_err(&device->dev, "Error registering device\n");
716d43e167dSRafael J. Wysocki 		goto err;
717e49bd2ddSZhang Rui 	}
718f883d9dbSPatrick Mochel 
719e49bd2ddSZhang Rui 	result = acpi_device_setup_files(device);
720e49bd2ddSZhang Rui 	if (result)
7210794469dSKay Sievers 		printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
7220794469dSKay Sievers 		       dev_name(&device->dev));
723e49bd2ddSZhang Rui 
724e49bd2ddSZhang Rui 	return 0;
725d43e167dSRafael J. Wysocki 
726d43e167dSRafael J. Wysocki err:
7279090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
7285e73c518SRafael J. Wysocki 
72933b57150SLen Brown 	if (device->parent)
730e49bd2ddSZhang Rui 		list_del(&device->node);
7315e73c518SRafael J. Wysocki 
732e49bd2ddSZhang Rui 	list_del(&device->wakeup_list);
733a58015d6SDexuan Cui 
734c1013ff7SRafael J. Wysocki err_unlock:
7359090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
736d43e167dSRafael J. Wysocki 
737d783156eSRafael J. Wysocki 	acpi_detach_data(device->handle, acpi_scan_drop_device);
7385e73c518SRafael J. Wysocki 
739e49bd2ddSZhang Rui 	return result;
7401da177e4SLinus Torvalds }
7411da177e4SLinus Torvalds 
7429e89dde2SZhang Rui /* --------------------------------------------------------------------------
7431da177e4SLinus Torvalds                                  Device Enumeration
7441da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
7452ef33ee7SRafael J. Wysocki static bool acpi_info_matches_ids(struct acpi_device_info *info,
7462ef33ee7SRafael J. Wysocki 				  const char * const ids[])
747637b9f1aSHans de Goede {
7482ef33ee7SRafael J. Wysocki 	struct acpi_pnp_device_id_list *cid_list = NULL;
74902050558SAndy Shevchenko 	int i, index;
750637b9f1aSHans de Goede 
751637b9f1aSHans de Goede 	if (!(info->valid & ACPI_VALID_HID))
752637b9f1aSHans de Goede 		return false;
753637b9f1aSHans de Goede 
75402050558SAndy Shevchenko 	index = match_string(ids, -1, info->hardware_id.string);
75502050558SAndy Shevchenko 	if (index >= 0)
75602050558SAndy Shevchenko 		return true;
75702050558SAndy Shevchenko 
7582ef33ee7SRafael J. Wysocki 	if (info->valid & ACPI_VALID_CID)
7592ef33ee7SRafael J. Wysocki 		cid_list = &info->compatible_id_list;
7602ef33ee7SRafael J. Wysocki 
7612ef33ee7SRafael J. Wysocki 	if (!cid_list)
76202050558SAndy Shevchenko 		return false;
7632ef33ee7SRafael J. Wysocki 
76402050558SAndy Shevchenko 	for (i = 0; i < cid_list->count; i++) {
76502050558SAndy Shevchenko 		index = match_string(ids, -1, cid_list->ids[i].string);
76602050558SAndy Shevchenko 		if (index >= 0)
7672ef33ee7SRafael J. Wysocki 			return true;
7682ef33ee7SRafael J. Wysocki 	}
769637b9f1aSHans de Goede 
770637b9f1aSHans de Goede 	return false;
771637b9f1aSHans de Goede }
772637b9f1aSHans de Goede 
773637b9f1aSHans de Goede /* List of HIDs for which we ignore matching ACPI devices, when checking _DEP lists. */
7742ef33ee7SRafael J. Wysocki static const char * const acpi_ignore_dep_ids[] = {
7752ef33ee7SRafael J. Wysocki 	"PNP0D80", /* Windows-compatible System Power Management Controller */
7769272e97aSHans de Goede 	"INT33BD", /* Intel Baytrail Mailbox Device */
777637b9f1aSHans de Goede 	NULL
778637b9f1aSHans de Goede };
779637b9f1aSHans de Goede 
7805c478f49SBjorn Helgaas static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
7815c478f49SBjorn Helgaas {
782456de893SRafael J. Wysocki 	struct acpi_device *device = NULL;
7835c478f49SBjorn Helgaas 	acpi_status status;
7845c478f49SBjorn Helgaas 
7855c478f49SBjorn Helgaas 	/*
7865c478f49SBjorn Helgaas 	 * Fixed hardware devices do not appear in the namespace and do not
7875c478f49SBjorn Helgaas 	 * have handles, but we fabricate acpi_devices for them, so we have
7885c478f49SBjorn Helgaas 	 * to deal with them specially.
7895c478f49SBjorn Helgaas 	 */
790456de893SRafael J. Wysocki 	if (!handle)
7915c478f49SBjorn Helgaas 		return acpi_root;
7925c478f49SBjorn Helgaas 
7935c478f49SBjorn Helgaas 	do {
7945c478f49SBjorn Helgaas 		status = acpi_get_parent(handle, &handle);
7955c478f49SBjorn Helgaas 		if (ACPI_FAILURE(status))
796456de893SRafael J. Wysocki 			return status == AE_NULL_ENTRY ? NULL : acpi_root;
797456de893SRafael J. Wysocki 	} while (acpi_bus_get_device(handle, &device));
7985c478f49SBjorn Helgaas 	return device;
7995c478f49SBjorn Helgaas }
8005c478f49SBjorn Helgaas 
801c8f7a62cSLen Brown acpi_status
802c8f7a62cSLen Brown acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
803c8f7a62cSLen Brown {
804c8f7a62cSLen Brown 	acpi_status status;
805c8f7a62cSLen Brown 	acpi_handle tmp;
806c8f7a62cSLen Brown 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
807c8f7a62cSLen Brown 	union acpi_object *obj;
808c8f7a62cSLen Brown 
809c8f7a62cSLen Brown 	status = acpi_get_handle(handle, "_EJD", &tmp);
810c8f7a62cSLen Brown 	if (ACPI_FAILURE(status))
811c8f7a62cSLen Brown 		return status;
812c8f7a62cSLen Brown 
813c8f7a62cSLen Brown 	status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
814c8f7a62cSLen Brown 	if (ACPI_SUCCESS(status)) {
815c8f7a62cSLen Brown 		obj = buffer.pointer;
8163b5fee59SHolger Macht 		status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
8173b5fee59SHolger Macht 					 ejd);
818c8f7a62cSLen Brown 		kfree(buffer.pointer);
819c8f7a62cSLen Brown 	}
820c8f7a62cSLen Brown 	return status;
821c8f7a62cSLen Brown }
822c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
823c8f7a62cSLen Brown 
82440381a3cSBjorn Helgaas static int acpi_bus_extract_wakeup_device_power_package(struct acpi_device *dev)
8251da177e4SLinus Torvalds {
82640381a3cSBjorn Helgaas 	acpi_handle handle = dev->handle;
82740381a3cSBjorn Helgaas 	struct acpi_device_wakeup *wakeup = &dev->wakeup;
828b581a7f9SRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
829b581a7f9SRafael J. Wysocki 	union acpi_object *package = NULL;
8301da177e4SLinus Torvalds 	union acpi_object *element = NULL;
831b581a7f9SRafael J. Wysocki 	acpi_status status;
832e88c9c60SRafael J. Wysocki 	int err = -ENODATA;
8331da177e4SLinus Torvalds 
834993cbe59SRafael J. Wysocki 	INIT_LIST_HEAD(&wakeup->resources);
8351da177e4SLinus Torvalds 
836b581a7f9SRafael J. Wysocki 	/* _PRW */
837b581a7f9SRafael J. Wysocki 	status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
838b581a7f9SRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
839e52d9d8cSRafael J. Wysocki 		acpi_handle_info(handle, "_PRW evaluation failed: %s\n",
840e52d9d8cSRafael J. Wysocki 				 acpi_format_exception(status));
841e88c9c60SRafael J. Wysocki 		return err;
842b581a7f9SRafael J. Wysocki 	}
843b581a7f9SRafael J. Wysocki 
844b581a7f9SRafael J. Wysocki 	package = (union acpi_object *)buffer.pointer;
845b581a7f9SRafael J. Wysocki 
846e88c9c60SRafael J. Wysocki 	if (!package || package->package.count < 2)
847b581a7f9SRafael J. Wysocki 		goto out;
848b581a7f9SRafael J. Wysocki 
8491da177e4SLinus Torvalds 	element = &(package->package.elements[0]);
850e88c9c60SRafael J. Wysocki 	if (!element)
851b581a7f9SRafael J. Wysocki 		goto out;
852e88c9c60SRafael J. Wysocki 
8531da177e4SLinus Torvalds 	if (element->type == ACPI_TYPE_PACKAGE) {
8541da177e4SLinus Torvalds 		if ((element->package.count < 2) ||
8551da177e4SLinus Torvalds 		    (element->package.elements[0].type !=
8561da177e4SLinus Torvalds 		     ACPI_TYPE_LOCAL_REFERENCE)
857e88c9c60SRafael J. Wysocki 		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
858b581a7f9SRafael J. Wysocki 			goto out;
859e88c9c60SRafael J. Wysocki 
860b581a7f9SRafael J. Wysocki 		wakeup->gpe_device =
8611da177e4SLinus Torvalds 		    element->package.elements[0].reference.handle;
862b581a7f9SRafael J. Wysocki 		wakeup->gpe_number =
8631da177e4SLinus Torvalds 		    (u32) element->package.elements[1].integer.value;
8641da177e4SLinus Torvalds 	} else if (element->type == ACPI_TYPE_INTEGER) {
865b581a7f9SRafael J. Wysocki 		wakeup->gpe_device = NULL;
866b581a7f9SRafael J. Wysocki 		wakeup->gpe_number = element->integer.value;
867b581a7f9SRafael J. Wysocki 	} else {
868b581a7f9SRafael J. Wysocki 		goto out;
869b581a7f9SRafael J. Wysocki 	}
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	element = &(package->package.elements[1]);
872e88c9c60SRafael J. Wysocki 	if (element->type != ACPI_TYPE_INTEGER)
873b581a7f9SRafael J. Wysocki 		goto out;
874e88c9c60SRafael J. Wysocki 
875b581a7f9SRafael J. Wysocki 	wakeup->sleep_state = element->integer.value;
8761da177e4SLinus Torvalds 
877e88c9c60SRafael J. Wysocki 	err = acpi_extract_power_resources(package, 2, &wakeup->resources);
878e88c9c60SRafael J. Wysocki 	if (err)
879b581a7f9SRafael J. Wysocki 		goto out;
8801da177e4SLinus Torvalds 
8810596a52bSRafael J. Wysocki 	if (!list_empty(&wakeup->resources)) {
8820596a52bSRafael J. Wysocki 		int sleep_state;
8839874647bSRafael J. Wysocki 
884b5d667ebSRafael J. Wysocki 		err = acpi_power_wakeup_list_init(&wakeup->resources,
885b5d667ebSRafael J. Wysocki 						  &sleep_state);
886b5d667ebSRafael J. Wysocki 		if (err) {
887b5d667ebSRafael J. Wysocki 			acpi_handle_warn(handle, "Retrieving current states "
888b5d667ebSRafael J. Wysocki 					 "of wakeup power resources failed\n");
889b5d667ebSRafael J. Wysocki 			acpi_power_resources_list_free(&wakeup->resources);
890b5d667ebSRafael J. Wysocki 			goto out;
891b5d667ebSRafael J. Wysocki 		}
8920596a52bSRafael J. Wysocki 		if (sleep_state < wakeup->sleep_state) {
8930596a52bSRafael J. Wysocki 			acpi_handle_warn(handle, "Overriding _PRW sleep state "
8940596a52bSRafael J. Wysocki 					 "(S%d) by S%d from power resources\n",
8950596a52bSRafael J. Wysocki 					 (int)wakeup->sleep_state, sleep_state);
8960596a52bSRafael J. Wysocki 			wakeup->sleep_state = sleep_state;
8970596a52bSRafael J. Wysocki 		}
8980596a52bSRafael J. Wysocki 	}
899b581a7f9SRafael J. Wysocki 
900b581a7f9SRafael J. Wysocki  out:
901b581a7f9SRafael J. Wysocki 	kfree(buffer.pointer);
902e88c9c60SRafael J. Wysocki 	return err;
9031da177e4SLinus Torvalds }
9041da177e4SLinus Torvalds 
905a1a66393SRafael J. Wysocki static bool acpi_wakeup_gpe_init(struct acpi_device *device)
9061da177e4SLinus Torvalds {
9070519ade7SMathias Krause 	static const struct acpi_device_id button_device_ids[] = {
9085ceb5f05SBjorn Helgaas 		{"PNP0C0C", 0},		/* Power button */
9095ceb5f05SBjorn Helgaas 		{"PNP0C0D", 0},		/* Lid */
9105ceb5f05SBjorn Helgaas 		{"PNP0C0E", 0},		/* Sleep button */
91129b71a1cSThomas Renninger 		{"", 0},
91229b71a1cSThomas Renninger 	};
913bd9b2f9aSRafael J. Wysocki 	struct acpi_device_wakeup *wakeup = &device->wakeup;
914f517709dSRafael J. Wysocki 	acpi_status status;
915f517709dSRafael J. Wysocki 
916bd9b2f9aSRafael J. Wysocki 	wakeup->flags.notifier_present = 0;
917f517709dSRafael J. Wysocki 
918f517709dSRafael J. Wysocki 	/* Power button, Lid switch always enable wakeup */
919f517709dSRafael J. Wysocki 	if (!acpi_match_device_ids(device, button_device_ids)) {
920b7e38304SZhang Rui 		if (!acpi_match_device_ids(device, &button_device_ids[1])) {
921b7e38304SZhang Rui 			/* Do not use Lid/sleep button for S5 wakeup */
922bd9b2f9aSRafael J. Wysocki 			if (wakeup->sleep_state == ACPI_STATE_S5)
923bd9b2f9aSRafael J. Wysocki 				wakeup->sleep_state = ACPI_STATE_S4;
924b7e38304SZhang Rui 		}
925bd9b2f9aSRafael J. Wysocki 		acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
926f2b56bc8SRafael J. Wysocki 		device_set_wakeup_capable(&device->dev, true);
927a1a66393SRafael J. Wysocki 		return true;
928f517709dSRafael J. Wysocki 	}
929f517709dSRafael J. Wysocki 
930a1a66393SRafael J. Wysocki 	status = acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
931bd9b2f9aSRafael J. Wysocki 					 wakeup->gpe_number);
932a1a66393SRafael J. Wysocki 	return ACPI_SUCCESS(status);
933f517709dSRafael J. Wysocki }
934f517709dSRafael J. Wysocki 
935d57d09a4SRafael J. Wysocki static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
936f517709dSRafael J. Wysocki {
937e88c9c60SRafael J. Wysocki 	int err;
93829b71a1cSThomas Renninger 
939d57d09a4SRafael J. Wysocki 	/* Presence of _PRW indicates wake capable */
940952c63e9SJiang Liu 	if (!acpi_has_method(device->handle, "_PRW"))
941d57d09a4SRafael J. Wysocki 		return;
942d57d09a4SRafael J. Wysocki 
94340381a3cSBjorn Helgaas 	err = acpi_bus_extract_wakeup_device_power_package(device);
944e88c9c60SRafael J. Wysocki 	if (err) {
945e52d9d8cSRafael J. Wysocki 		dev_err(&device->dev, "Unable to extract wakeup power resources");
946d57d09a4SRafael J. Wysocki 		return;
9471da177e4SLinus Torvalds 	}
9481da177e4SLinus Torvalds 
949a1a66393SRafael J. Wysocki 	device->wakeup.flags.valid = acpi_wakeup_gpe_init(device);
9509b83ccd2SRafael J. Wysocki 	device->wakeup.prepare_count = 0;
951a1a66393SRafael J. Wysocki 	/*
952a1a66393SRafael J. Wysocki 	 * Call _PSW/_DSW object to disable its ability to wake the sleeping
953729b2bdbSZhao Yakui 	 * system for the ACPI device with the _PRW object.
954603fadf3SBjorn Helgaas 	 * The _PSW object is deprecated in ACPI 3.0 and is replaced by _DSW.
955729b2bdbSZhao Yakui 	 * So it is necessary to call _DSW object first. Only when it is not
956729b2bdbSZhao Yakui 	 * present will the _PSW object used.
957729b2bdbSZhao Yakui 	 */
958e88c9c60SRafael J. Wysocki 	err = acpi_device_sleep_wake(device, 0, 0, 0);
959e88c9c60SRafael J. Wysocki 	if (err)
96005de0686STian Tao 		pr_debug("error in _DSW or _PSW evaluation\n");
9611da177e4SLinus Torvalds }
9621da177e4SLinus Torvalds 
963f33ce568SRafael J. Wysocki static void acpi_bus_init_power_state(struct acpi_device *device, int state)
9641da177e4SLinus Torvalds {
965f33ce568SRafael J. Wysocki 	struct acpi_device_power_state *ps = &device->power.states[state];
966ef85bdbeSRafael J. Wysocki 	char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
967ef85bdbeSRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
968f33ce568SRafael J. Wysocki 	acpi_status status;
9699e89dde2SZhang Rui 
970f33ce568SRafael J. Wysocki 	INIT_LIST_HEAD(&ps->resources);
971f33ce568SRafael J. Wysocki 
972ef85bdbeSRafael J. Wysocki 	/* Evaluate "_PRx" to get referenced power resources */
973ef85bdbeSRafael J. Wysocki 	status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
974ef85bdbeSRafael J. Wysocki 	if (ACPI_SUCCESS(status)) {
975ef85bdbeSRafael J. Wysocki 		union acpi_object *package = buffer.pointer;
976f33ce568SRafael J. Wysocki 
977ef85bdbeSRafael J. Wysocki 		if (buffer.length && package
978ef85bdbeSRafael J. Wysocki 		    && package->type == ACPI_TYPE_PACKAGE
979956ad9d9SRafael J. Wysocki 		    && package->package.count)
980956ad9d9SRafael J. Wysocki 			acpi_extract_power_resources(package, 0, &ps->resources);
981956ad9d9SRafael J. Wysocki 
982ef85bdbeSRafael J. Wysocki 		ACPI_FREE(buffer.pointer);
983f33ce568SRafael J. Wysocki 	}
984f33ce568SRafael J. Wysocki 
985f33ce568SRafael J. Wysocki 	/* Evaluate "_PSx" to see if we can do explicit sets */
986ef85bdbeSRafael J. Wysocki 	pathname[2] = 'S';
987952c63e9SJiang Liu 	if (acpi_has_method(device->handle, pathname))
988f33ce568SRafael J. Wysocki 		ps->flags.explicit_set = 1;
989f33ce568SRafael J. Wysocki 
99020dacb71SRafael J. Wysocki 	/* State is valid if there are means to put the device into it. */
99120dacb71SRafael J. Wysocki 	if (!list_empty(&ps->resources) || ps->flags.explicit_set)
992f33ce568SRafael J. Wysocki 		ps->flags.valid = 1;
993f33ce568SRafael J. Wysocki 
994f33ce568SRafael J. Wysocki 	ps->power = -1;		/* Unknown - driver assigned */
995f33ce568SRafael J. Wysocki 	ps->latency = -1;	/* Unknown - driver assigned */
996f33ce568SRafael J. Wysocki }
997f33ce568SRafael J. Wysocki 
998d43e167dSRafael J. Wysocki static void acpi_bus_get_power_flags(struct acpi_device *device)
9991da177e4SLinus Torvalds {
10008bc5053bSRafael J. Wysocki 	u32 i;
10011da177e4SLinus Torvalds 
1002d43e167dSRafael J. Wysocki 	/* Presence of _PS0|_PR0 indicates 'power manageable' */
1003952c63e9SJiang Liu 	if (!acpi_has_method(device->handle, "_PS0") &&
1004952c63e9SJiang Liu 	    !acpi_has_method(device->handle, "_PR0"))
1005d43e167dSRafael J. Wysocki 		return;
1006d43e167dSRafael J. Wysocki 
1007d43e167dSRafael J. Wysocki 	device->flags.power_manageable = 1;
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 	/*
10109e89dde2SZhang Rui 	 * Power Management Flags
10111da177e4SLinus Torvalds 	 */
1012952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_PSC"))
10139e89dde2SZhang Rui 		device->power.flags.explicit_get = 1;
1014f25c0ae2SRafael J. Wysocki 
1015952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_IRC"))
10169e89dde2SZhang Rui 		device->power.flags.inrush_current = 1;
10171da177e4SLinus Torvalds 
1018f25c0ae2SRafael J. Wysocki 	if (acpi_has_method(device->handle, "_DSW"))
1019f25c0ae2SRafael J. Wysocki 		device->power.flags.dsw_present = 1;
1020f25c0ae2SRafael J. Wysocki 
10211da177e4SLinus Torvalds 	/*
10229e89dde2SZhang Rui 	 * Enumerate supported power management states
10231da177e4SLinus Torvalds 	 */
1024f33ce568SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1025f33ce568SRafael J. Wysocki 		acpi_bus_init_power_state(device, i);
10261da177e4SLinus Torvalds 
10270b224527SRafael J. Wysocki 	INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
10289e89dde2SZhang Rui 
1029956ad9d9SRafael J. Wysocki 	/* Set the defaults for D0 and D3hot (always supported). */
10309e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].flags.valid = 1;
10319e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].power = 100;
103220dacb71SRafael J. Wysocki 	device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1;
10331399dfcdSAaron Lu 
1034956ad9d9SRafael J. Wysocki 	/*
1035956ad9d9SRafael J. Wysocki 	 * Use power resources only if the D0 list of them is populated, because
1036956ad9d9SRafael J. Wysocki 	 * some platforms may provide _PR3 only to indicate D3cold support and
1037956ad9d9SRafael J. Wysocki 	 * in those cases the power resources list returned by it may be bogus.
1038956ad9d9SRafael J. Wysocki 	 */
1039956ad9d9SRafael J. Wysocki 	if (!list_empty(&device->power.states[ACPI_STATE_D0].resources)) {
1040956ad9d9SRafael J. Wysocki 		device->power.flags.power_resources = 1;
1041956ad9d9SRafael J. Wysocki 		/*
1042956ad9d9SRafael J. Wysocki 		 * D3cold is supported if the D3hot list of power resources is
1043956ad9d9SRafael J. Wysocki 		 * not empty.
1044956ad9d9SRafael J. Wysocki 		 */
1045956ad9d9SRafael J. Wysocki 		if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources))
1046956ad9d9SRafael J. Wysocki 			device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1047956ad9d9SRafael J. Wysocki 	}
1048956ad9d9SRafael J. Wysocki 
10491b1f3e16SRafael J. Wysocki 	if (acpi_bus_init_power(device))
1050b3785492SRafael J. Wysocki 		device->flags.power_manageable = 0;
1051b3785492SRafael J. Wysocki }
1052c8f7a62cSLen Brown 
1053d43e167dSRafael J. Wysocki static void acpi_bus_get_flags(struct acpi_device *device)
10541da177e4SLinus Torvalds {
10551da177e4SLinus Torvalds 	/* Presence of _STA indicates 'dynamic_status' */
1056952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_STA"))
10571da177e4SLinus Torvalds 		device->flags.dynamic_status = 1;
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 	/* Presence of _RMV indicates 'removable' */
1060952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_RMV"))
10611da177e4SLinus Torvalds 		device->flags.removable = 1;
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
1064952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_EJD") ||
1065952c63e9SJiang Liu 	    acpi_has_method(device->handle, "_EJ0"))
10661da177e4SLinus Torvalds 		device->flags.ejectable = 1;
10671da177e4SLinus Torvalds }
10681da177e4SLinus Torvalds 
1069c7bcb4e9SBjorn Helgaas static void acpi_device_get_busid(struct acpi_device *device)
10701da177e4SLinus Torvalds {
10711da177e4SLinus Torvalds 	char bus_id[5] = { '?', 0 };
10721da177e4SLinus Torvalds 	struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
10731da177e4SLinus Torvalds 	int i = 0;
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 	/*
10761da177e4SLinus Torvalds 	 * Bus ID
10771da177e4SLinus Torvalds 	 * ------
10781da177e4SLinus Torvalds 	 * The device's Bus ID is simply the object name.
10791da177e4SLinus Torvalds 	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
10801da177e4SLinus Torvalds 	 */
1081859ac9a4SBjorn Helgaas 	if (ACPI_IS_ROOT_DEVICE(device)) {
10821da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "ACPI");
1083859ac9a4SBjorn Helgaas 		return;
1084859ac9a4SBjorn Helgaas 	}
1085859ac9a4SBjorn Helgaas 
1086859ac9a4SBjorn Helgaas 	switch (device->device_type) {
10871da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
10881da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "PWRF");
10891da177e4SLinus Torvalds 		break;
10901da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
10911da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "SLPF");
10921da177e4SLinus Torvalds 		break;
1093a64a62ceSLv Zheng 	case ACPI_BUS_TYPE_ECDT_EC:
1094a64a62ceSLv Zheng 		strcpy(device->pnp.bus_id, "ECDT");
1095a64a62ceSLv Zheng 		break;
10961da177e4SLinus Torvalds 	default:
109766b7ed40SBjorn Helgaas 		acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
10981da177e4SLinus Torvalds 		/* Clean up trailing underscores (if any) */
10991da177e4SLinus Torvalds 		for (i = 3; i > 1; i--) {
11001da177e4SLinus Torvalds 			if (bus_id[i] == '_')
11011da177e4SLinus Torvalds 				bus_id[i] = '\0';
11021da177e4SLinus Torvalds 			else
11031da177e4SLinus Torvalds 				break;
11041da177e4SLinus Torvalds 		}
11051da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, bus_id);
11061da177e4SLinus Torvalds 		break;
11071da177e4SLinus Torvalds 	}
11081da177e4SLinus Torvalds }
11091da177e4SLinus Torvalds 
111054735266SZhang Rui /*
1111ebf4df8dSJiang Liu  * acpi_ata_match - see if an acpi object is an ATA device
1112ebf4df8dSJiang Liu  *
1113ebf4df8dSJiang Liu  * If an acpi object has one of the ACPI ATA methods defined,
1114ebf4df8dSJiang Liu  * then we can safely call it an ATA device.
1115ebf4df8dSJiang Liu  */
1116ebf4df8dSJiang Liu bool acpi_ata_match(acpi_handle handle)
1117ebf4df8dSJiang Liu {
1118ebf4df8dSJiang Liu 	return acpi_has_method(handle, "_GTF") ||
1119ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_GTM") ||
1120ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_STM") ||
1121ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_SDD");
1122ebf4df8dSJiang Liu }
1123ebf4df8dSJiang Liu 
1124ebf4df8dSJiang Liu /*
1125d4e1a692SToshi Kani  * acpi_bay_match - see if an acpi object is an ejectable driver bay
112654735266SZhang Rui  *
112754735266SZhang Rui  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
112854735266SZhang Rui  * then we can safely call it an ejectable drive bay
112954735266SZhang Rui  */
1130ebf4df8dSJiang Liu bool acpi_bay_match(acpi_handle handle)
1131d4e1a692SToshi Kani {
113254735266SZhang Rui 	acpi_handle phandle;
113354735266SZhang Rui 
1134952c63e9SJiang Liu 	if (!acpi_has_method(handle, "_EJ0"))
1135ebf4df8dSJiang Liu 		return false;
1136ebf4df8dSJiang Liu 	if (acpi_ata_match(handle))
1137ebf4df8dSJiang Liu 		return true;
1138ebf4df8dSJiang Liu 	if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1139ebf4df8dSJiang Liu 		return false;
114054735266SZhang Rui 
1141ebf4df8dSJiang Liu 	return acpi_ata_match(phandle);
114254735266SZhang Rui }
114354735266SZhang Rui 
1144b43109faSRafael J. Wysocki bool acpi_device_is_battery(struct acpi_device *adev)
11451e2380cdSRafael J. Wysocki {
1146b43109faSRafael J. Wysocki 	struct acpi_hardware_id *hwid;
11471e2380cdSRafael J. Wysocki 
1148b43109faSRafael J. Wysocki 	list_for_each_entry(hwid, &adev->pnp.ids, list)
1149b43109faSRafael J. Wysocki 		if (!strcmp("PNP0C0A", hwid->id))
1150b43109faSRafael J. Wysocki 			return true;
1151b43109faSRafael J. Wysocki 
11521e2380cdSRafael J. Wysocki 	return false;
11531e2380cdSRafael J. Wysocki }
11541e2380cdSRafael J. Wysocki 
1155b43109faSRafael J. Wysocki static bool is_ejectable_bay(struct acpi_device *adev)
11561e2380cdSRafael J. Wysocki {
1157b43109faSRafael J. Wysocki 	acpi_handle handle = adev->handle;
1158b43109faSRafael J. Wysocki 
1159b43109faSRafael J. Wysocki 	if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
11601e2380cdSRafael J. Wysocki 		return true;
11611e2380cdSRafael J. Wysocki 
11621e2380cdSRafael J. Wysocki 	return acpi_bay_match(handle);
11631e2380cdSRafael J. Wysocki }
11641e2380cdSRafael J. Wysocki 
11653620f2f2SFrank Seidel /*
1166d4e1a692SToshi Kani  * acpi_dock_match - see if an acpi object has a _DCK method
11673620f2f2SFrank Seidel  */
1168ebf4df8dSJiang Liu bool acpi_dock_match(acpi_handle handle)
11693620f2f2SFrank Seidel {
1170ebf4df8dSJiang Liu 	return acpi_has_method(handle, "_DCK");
11713620f2f2SFrank Seidel }
11723620f2f2SFrank Seidel 
1173adc8bb8eSHans de Goede static acpi_status
1174adc8bb8eSHans de Goede acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context,
1175adc8bb8eSHans de Goede 			  void **return_value)
1176adc8bb8eSHans de Goede {
1177adc8bb8eSHans de Goede 	long *cap = context;
1178adc8bb8eSHans de Goede 
1179adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_BCM") &&
1180adc8bb8eSHans de Goede 	    acpi_has_method(handle, "_BCL")) {
1181e52d9d8cSRafael J. Wysocki 		acpi_handle_debug(handle, "Found generic backlight support\n");
1182adc8bb8eSHans de Goede 		*cap |= ACPI_VIDEO_BACKLIGHT;
1183adc8bb8eSHans de Goede 		/* We have backlight support, no need to scan further */
1184adc8bb8eSHans de Goede 		return AE_CTRL_TERMINATE;
1185adc8bb8eSHans de Goede 	}
1186adc8bb8eSHans de Goede 	return 0;
1187adc8bb8eSHans de Goede }
1188adc8bb8eSHans de Goede 
1189adc8bb8eSHans de Goede /* Returns true if the ACPI object is a video device which can be
1190adc8bb8eSHans de Goede  * handled by video.ko.
1191adc8bb8eSHans de Goede  * The device will get a Linux specific CID added in scan.c to
1192adc8bb8eSHans de Goede  * identify the device as an ACPI graphics device
1193adc8bb8eSHans de Goede  * Be aware that the graphics device may not be physically present
1194adc8bb8eSHans de Goede  * Use acpi_video_get_capabilities() to detect general ACPI video
1195adc8bb8eSHans de Goede  * capabilities of present cards
1196adc8bb8eSHans de Goede  */
1197adc8bb8eSHans de Goede long acpi_is_video_device(acpi_handle handle)
1198adc8bb8eSHans de Goede {
1199adc8bb8eSHans de Goede 	long video_caps = 0;
1200adc8bb8eSHans de Goede 
1201adc8bb8eSHans de Goede 	/* Is this device able to support video switching ? */
1202adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS"))
1203adc8bb8eSHans de Goede 		video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING;
1204adc8bb8eSHans de Goede 
1205adc8bb8eSHans de Goede 	/* Is this device able to retrieve a video ROM ? */
1206adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_ROM"))
1207adc8bb8eSHans de Goede 		video_caps |= ACPI_VIDEO_ROM_AVAILABLE;
1208adc8bb8eSHans de Goede 
1209adc8bb8eSHans de Goede 	/* Is this device able to configure which video head to be POSTed ? */
1210adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_VPO") &&
1211adc8bb8eSHans de Goede 	    acpi_has_method(handle, "_GPD") &&
1212adc8bb8eSHans de Goede 	    acpi_has_method(handle, "_SPD"))
1213adc8bb8eSHans de Goede 		video_caps |= ACPI_VIDEO_DEVICE_POSTING;
1214adc8bb8eSHans de Goede 
1215adc8bb8eSHans de Goede 	/* Only check for backlight functionality if one of the above hit. */
1216adc8bb8eSHans de Goede 	if (video_caps)
1217adc8bb8eSHans de Goede 		acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1218adc8bb8eSHans de Goede 				    ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL,
1219adc8bb8eSHans de Goede 				    &video_caps, NULL);
1220adc8bb8eSHans de Goede 
1221adc8bb8eSHans de Goede 	return video_caps;
1222adc8bb8eSHans de Goede }
1223adc8bb8eSHans de Goede EXPORT_SYMBOL(acpi_is_video_device);
1224adc8bb8eSHans de Goede 
1225620e112cSThomas Renninger const char *acpi_device_hid(struct acpi_device *device)
122615b8dd53SBob Moore {
12277f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *hid;
122815b8dd53SBob Moore 
12292b2ae7c7SThomas Renninger 	if (list_empty(&device->pnp.ids))
12302b2ae7c7SThomas Renninger 		return dummy_hid;
12312b2ae7c7SThomas Renninger 
12327f47fa6cSBjorn Helgaas 	hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
12337f47fa6cSBjorn Helgaas 	return hid->id;
12347f47fa6cSBjorn Helgaas }
12357f47fa6cSBjorn Helgaas EXPORT_SYMBOL(acpi_device_hid);
123615b8dd53SBob Moore 
1237d4e1a692SToshi Kani static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
12387f47fa6cSBjorn Helgaas {
12397f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id;
124015b8dd53SBob Moore 
12417f47fa6cSBjorn Helgaas 	id = kmalloc(sizeof(*id), GFP_KERNEL);
12427f47fa6cSBjorn Helgaas 	if (!id)
12437f47fa6cSBjorn Helgaas 		return;
124415b8dd53SBob Moore 
12456a0d12efSRasmus Villemoes 	id->id = kstrdup_const(dev_id, GFP_KERNEL);
12467f47fa6cSBjorn Helgaas 	if (!id->id) {
12477f47fa6cSBjorn Helgaas 		kfree(id);
12487f47fa6cSBjorn Helgaas 		return;
124915b8dd53SBob Moore 	}
125015b8dd53SBob Moore 
1251d4e1a692SToshi Kani 	list_add_tail(&id->list, &pnp->ids);
1252d4e1a692SToshi Kani 	pnp->type.hardware_id = 1;
125315b8dd53SBob Moore }
125415b8dd53SBob Moore 
1255222e82acSDarrick J. Wong /*
1256222e82acSDarrick J. Wong  * Old IBM workstations have a DSDT bug wherein the SMBus object
1257222e82acSDarrick J. Wong  * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1258222e82acSDarrick J. Wong  * prefix.  Work around this.
1259222e82acSDarrick J. Wong  */
1260ebf4df8dSJiang Liu static bool acpi_ibm_smbus_match(acpi_handle handle)
1261222e82acSDarrick J. Wong {
1262ebf4df8dSJiang Liu 	char node_name[ACPI_PATH_SEGMENT_LENGTH];
1263ebf4df8dSJiang Liu 	struct acpi_buffer path = { sizeof(node_name), node_name };
1264222e82acSDarrick J. Wong 
1265222e82acSDarrick J. Wong 	if (!dmi_name_in_vendors("IBM"))
1266ebf4df8dSJiang Liu 		return false;
1267222e82acSDarrick J. Wong 
1268222e82acSDarrick J. Wong 	/* Look for SMBS object */
1269ebf4df8dSJiang Liu 	if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1270ebf4df8dSJiang Liu 	    strcmp("SMBS", path.pointer))
1271ebf4df8dSJiang Liu 		return false;
1272222e82acSDarrick J. Wong 
1273222e82acSDarrick J. Wong 	/* Does it have the necessary (but misnamed) methods? */
1274952c63e9SJiang Liu 	if (acpi_has_method(handle, "SBI") &&
1275952c63e9SJiang Liu 	    acpi_has_method(handle, "SBR") &&
1276952c63e9SJiang Liu 	    acpi_has_method(handle, "SBW"))
1277ebf4df8dSJiang Liu 		return true;
1278ebf4df8dSJiang Liu 
1279ebf4df8dSJiang Liu 	return false;
1280222e82acSDarrick J. Wong }
1281222e82acSDarrick J. Wong 
1282ae3caa80SJiang Liu static bool acpi_object_is_system_bus(acpi_handle handle)
1283ae3caa80SJiang Liu {
1284ae3caa80SJiang Liu 	acpi_handle tmp;
1285ae3caa80SJiang Liu 
1286ae3caa80SJiang Liu 	if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1287ae3caa80SJiang Liu 	    tmp == handle)
1288ae3caa80SJiang Liu 		return true;
1289ae3caa80SJiang Liu 	if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1290ae3caa80SJiang Liu 	    tmp == handle)
1291ae3caa80SJiang Liu 		return true;
1292ae3caa80SJiang Liu 
1293ae3caa80SJiang Liu 	return false;
1294ae3caa80SJiang Liu }
1295ae3caa80SJiang Liu 
1296c0af4175SToshi Kani static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1297c830dbcfSRafael J. Wysocki 			     int device_type)
12981da177e4SLinus Torvalds {
1299c830dbcfSRafael J. Wysocki 	struct acpi_device_info *info = NULL;
130078e25fefSLv Zheng 	struct acpi_pnp_device_id_list *cid_list;
13017f47fa6cSBjorn Helgaas 	int i;
13021da177e4SLinus Torvalds 
1303c0af4175SToshi Kani 	switch (device_type) {
13041da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
1305c0af4175SToshi Kani 		if (handle == ACPI_ROOT_OBJECT) {
1306c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_SYSTEM_HID);
1307859ac9a4SBjorn Helgaas 			break;
1308859ac9a4SBjorn Helgaas 		}
1309859ac9a4SBjorn Helgaas 
1310c830dbcfSRafael J. Wysocki 		acpi_get_object_info(handle, &info);
13116091b263SHans de Goede 		if (!info) {
1312c0af4175SToshi Kani 			pr_err(PREFIX "%s: Error reading device info\n",
1313c0af4175SToshi Kani 					__func__);
13141da177e4SLinus Torvalds 			return;
13151da177e4SLinus Torvalds 		}
13161da177e4SLinus Torvalds 
1317549e6845SRafael J. Wysocki 		if (info->valid & ACPI_VALID_HID) {
1318c0af4175SToshi Kani 			acpi_add_id(pnp, info->hardware_id.string);
1319549e6845SRafael J. Wysocki 			pnp->type.platform_id = 1;
1320549e6845SRafael J. Wysocki 		}
132157f3674fSBjorn Helgaas 		if (info->valid & ACPI_VALID_CID) {
132215b8dd53SBob Moore 			cid_list = &info->compatible_id_list;
132357f3674fSBjorn Helgaas 			for (i = 0; i < cid_list->count; i++)
1324c0af4175SToshi Kani 				acpi_add_id(pnp, cid_list->ids[i].string);
132557f3674fSBjorn Helgaas 		}
13261da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_ADR) {
1327c0af4175SToshi Kani 			pnp->bus_address = info->address;
1328c0af4175SToshi Kani 			pnp->type.bus_address = 1;
13291da177e4SLinus Torvalds 		}
1330ccf78040SLv Zheng 		if (info->valid & ACPI_VALID_UID)
1331c0af4175SToshi Kani 			pnp->unique_id = kstrdup(info->unique_id.string,
1332ccf78040SLv Zheng 							GFP_KERNEL);
133326095a01SSuthikulpanit, Suravee 		if (info->valid & ACPI_VALID_CLS)
133426095a01SSuthikulpanit, Suravee 			acpi_add_id(pnp, info->class_code.string);
1335ae843332SZhang Rui 
1336c830dbcfSRafael J. Wysocki 		kfree(info);
1337c830dbcfSRafael J. Wysocki 
133857f3674fSBjorn Helgaas 		/*
133957f3674fSBjorn Helgaas 		 * Some devices don't reliably have _HIDs & _CIDs, so add
134057f3674fSBjorn Helgaas 		 * synthetic HIDs to make sure drivers can find them.
13413620f2f2SFrank Seidel 		 */
1342c0af4175SToshi Kani 		if (acpi_is_video_device(handle))
1343c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_VIDEO_HID);
1344ebf4df8dSJiang Liu 		else if (acpi_bay_match(handle))
1345c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_BAY_HID);
1346ebf4df8dSJiang Liu 		else if (acpi_dock_match(handle))
1347c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_DOCK_HID);
1348ebf4df8dSJiang Liu 		else if (acpi_ibm_smbus_match(handle))
1349c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1350ae3caa80SJiang Liu 		else if (list_empty(&pnp->ids) &&
1351ae3caa80SJiang Liu 			 acpi_object_is_system_bus(handle)) {
1352ae3caa80SJiang Liu 			/* \_SB, \_TZ, LNXSYBUS */
1353ae3caa80SJiang Liu 			acpi_add_id(pnp, ACPI_BUS_HID);
1354c0af4175SToshi Kani 			strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1355c0af4175SToshi Kani 			strcpy(pnp->device_class, ACPI_BUS_CLASS);
1356b7b30de5SBjorn Helgaas 		}
135754735266SZhang Rui 
13581da177e4SLinus Torvalds 		break;
13591da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER:
1360c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_POWER_HID);
13611da177e4SLinus Torvalds 		break;
13621da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_PROCESSOR:
1363c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
13641da177e4SLinus Torvalds 		break;
13651da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_THERMAL:
1366c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_THERMAL_HID);
13671da177e4SLinus Torvalds 		break;
13681da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
1369c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
13701da177e4SLinus Torvalds 		break;
13711da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
1372c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
13731da177e4SLinus Torvalds 		break;
1374a64a62ceSLv Zheng 	case ACPI_BUS_TYPE_ECDT_EC:
1375a64a62ceSLv Zheng 		acpi_add_id(pnp, ACPI_ECDT_HID);
1376a64a62ceSLv Zheng 		break;
13771da177e4SLinus Torvalds 	}
13781da177e4SLinus Torvalds }
13791da177e4SLinus Torvalds 
1380c0af4175SToshi Kani void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1381c0af4175SToshi Kani {
1382c0af4175SToshi Kani 	struct acpi_hardware_id *id, *tmp;
1383c0af4175SToshi Kani 
1384c0af4175SToshi Kani 	list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
13856a0d12efSRasmus Villemoes 		kfree_const(id->id);
1386c0af4175SToshi Kani 		kfree(id);
1387c0af4175SToshi Kani 	}
1388c0af4175SToshi Kani 	kfree(pnp->unique_id);
1389c0af4175SToshi Kani }
1390c0af4175SToshi Kani 
1391b84f196dSSuthikulpanit, Suravee /**
1392b84f196dSSuthikulpanit, Suravee  * acpi_dma_supported - Check DMA support for the specified device.
1393b84f196dSSuthikulpanit, Suravee  * @adev: The pointer to acpi device
1394b84f196dSSuthikulpanit, Suravee  *
1395b84f196dSSuthikulpanit, Suravee  * Return false if DMA is not supported. Otherwise, return true
1396b84f196dSSuthikulpanit, Suravee  */
1397b84f196dSSuthikulpanit, Suravee bool acpi_dma_supported(struct acpi_device *adev)
1398b84f196dSSuthikulpanit, Suravee {
1399b84f196dSSuthikulpanit, Suravee 	if (!adev)
1400b84f196dSSuthikulpanit, Suravee 		return false;
1401b84f196dSSuthikulpanit, Suravee 
1402b84f196dSSuthikulpanit, Suravee 	if (adev->flags.cca_seen)
1403b84f196dSSuthikulpanit, Suravee 		return true;
1404b84f196dSSuthikulpanit, Suravee 
1405b84f196dSSuthikulpanit, Suravee 	/*
1406b84f196dSSuthikulpanit, Suravee 	* Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent
1407b84f196dSSuthikulpanit, Suravee 	* DMA on "Intel platforms".  Presumably that includes all x86 and
1408b84f196dSSuthikulpanit, Suravee 	* ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y.
1409b84f196dSSuthikulpanit, Suravee 	*/
1410b84f196dSSuthikulpanit, Suravee 	if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1411b84f196dSSuthikulpanit, Suravee 		return true;
1412b84f196dSSuthikulpanit, Suravee 
1413b84f196dSSuthikulpanit, Suravee 	return false;
1414b84f196dSSuthikulpanit, Suravee }
1415b84f196dSSuthikulpanit, Suravee 
1416b84f196dSSuthikulpanit, Suravee /**
1417b84f196dSSuthikulpanit, Suravee  * acpi_get_dma_attr - Check the supported DMA attr for the specified device.
1418b84f196dSSuthikulpanit, Suravee  * @adev: The pointer to acpi device
1419b84f196dSSuthikulpanit, Suravee  *
1420b84f196dSSuthikulpanit, Suravee  * Return enum dev_dma_attr.
1421b84f196dSSuthikulpanit, Suravee  */
1422b84f196dSSuthikulpanit, Suravee enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
1423b84f196dSSuthikulpanit, Suravee {
1424b84f196dSSuthikulpanit, Suravee 	if (!acpi_dma_supported(adev))
1425b84f196dSSuthikulpanit, Suravee 		return DEV_DMA_NOT_SUPPORTED;
1426b84f196dSSuthikulpanit, Suravee 
1427b84f196dSSuthikulpanit, Suravee 	if (adev->flags.coherent_dma)
1428b84f196dSSuthikulpanit, Suravee 		return DEV_DMA_COHERENT;
1429b84f196dSSuthikulpanit, Suravee 	else
1430b84f196dSSuthikulpanit, Suravee 		return DEV_DMA_NON_COHERENT;
1431b84f196dSSuthikulpanit, Suravee }
1432b84f196dSSuthikulpanit, Suravee 
1433d760a1baSLorenzo Pieralisi /**
1434c04ac679SLorenzo Pieralisi  * acpi_dma_get_range() - Get device DMA parameters.
1435c04ac679SLorenzo Pieralisi  *
1436c04ac679SLorenzo Pieralisi  * @dev: device to configure
1437c04ac679SLorenzo Pieralisi  * @dma_addr: pointer device DMA address result
1438c04ac679SLorenzo Pieralisi  * @offset: pointer to the DMA offset result
1439c04ac679SLorenzo Pieralisi  * @size: pointer to DMA range size result
1440c04ac679SLorenzo Pieralisi  *
1441c04ac679SLorenzo Pieralisi  * Evaluate DMA regions and return respectively DMA region start, offset
1442c04ac679SLorenzo Pieralisi  * and size in dma_addr, offset and size on parsing success; it does not
1443c04ac679SLorenzo Pieralisi  * update the passed in values on failure.
1444c04ac679SLorenzo Pieralisi  *
1445c04ac679SLorenzo Pieralisi  * Return 0 on success, < 0 on failure.
1446c04ac679SLorenzo Pieralisi  */
1447c04ac679SLorenzo Pieralisi int acpi_dma_get_range(struct device *dev, u64 *dma_addr, u64 *offset,
1448c04ac679SLorenzo Pieralisi 		       u64 *size)
1449c04ac679SLorenzo Pieralisi {
1450c04ac679SLorenzo Pieralisi 	struct acpi_device *adev;
1451c04ac679SLorenzo Pieralisi 	LIST_HEAD(list);
1452c04ac679SLorenzo Pieralisi 	struct resource_entry *rentry;
1453c04ac679SLorenzo Pieralisi 	int ret;
1454c04ac679SLorenzo Pieralisi 	struct device *dma_dev = dev;
1455c04ac679SLorenzo Pieralisi 	u64 len, dma_start = U64_MAX, dma_end = 0, dma_offset = 0;
1456c04ac679SLorenzo Pieralisi 
1457c04ac679SLorenzo Pieralisi 	/*
1458c04ac679SLorenzo Pieralisi 	 * Walk the device tree chasing an ACPI companion with a _DMA
1459c04ac679SLorenzo Pieralisi 	 * object while we go. Stop if we find a device with an ACPI
1460c04ac679SLorenzo Pieralisi 	 * companion containing a _DMA method.
1461c04ac679SLorenzo Pieralisi 	 */
1462c04ac679SLorenzo Pieralisi 	do {
1463c04ac679SLorenzo Pieralisi 		adev = ACPI_COMPANION(dma_dev);
1464c04ac679SLorenzo Pieralisi 		if (adev && acpi_has_method(adev->handle, METHOD_NAME__DMA))
1465c04ac679SLorenzo Pieralisi 			break;
1466c04ac679SLorenzo Pieralisi 
1467c04ac679SLorenzo Pieralisi 		dma_dev = dma_dev->parent;
1468c04ac679SLorenzo Pieralisi 	} while (dma_dev);
1469c04ac679SLorenzo Pieralisi 
1470c04ac679SLorenzo Pieralisi 	if (!dma_dev)
1471c04ac679SLorenzo Pieralisi 		return -ENODEV;
1472c04ac679SLorenzo Pieralisi 
1473c04ac679SLorenzo Pieralisi 	if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) {
1474c04ac679SLorenzo Pieralisi 		acpi_handle_warn(adev->handle, "_DMA is valid only if _CRS is present\n");
1475c04ac679SLorenzo Pieralisi 		return -EINVAL;
1476c04ac679SLorenzo Pieralisi 	}
1477c04ac679SLorenzo Pieralisi 
1478c04ac679SLorenzo Pieralisi 	ret = acpi_dev_get_dma_resources(adev, &list);
1479c04ac679SLorenzo Pieralisi 	if (ret > 0) {
1480c04ac679SLorenzo Pieralisi 		list_for_each_entry(rentry, &list, node) {
1481c04ac679SLorenzo Pieralisi 			if (dma_offset && rentry->offset != dma_offset) {
1482c04ac679SLorenzo Pieralisi 				ret = -EINVAL;
1483c04ac679SLorenzo Pieralisi 				dev_warn(dma_dev, "Can't handle multiple windows with different offsets\n");
1484c04ac679SLorenzo Pieralisi 				goto out;
1485c04ac679SLorenzo Pieralisi 			}
1486c04ac679SLorenzo Pieralisi 			dma_offset = rentry->offset;
1487c04ac679SLorenzo Pieralisi 
1488c04ac679SLorenzo Pieralisi 			/* Take lower and upper limits */
1489c04ac679SLorenzo Pieralisi 			if (rentry->res->start < dma_start)
1490c04ac679SLorenzo Pieralisi 				dma_start = rentry->res->start;
1491c04ac679SLorenzo Pieralisi 			if (rentry->res->end > dma_end)
1492c04ac679SLorenzo Pieralisi 				dma_end = rentry->res->end;
1493c04ac679SLorenzo Pieralisi 		}
1494c04ac679SLorenzo Pieralisi 
1495c04ac679SLorenzo Pieralisi 		if (dma_start >= dma_end) {
1496c04ac679SLorenzo Pieralisi 			ret = -EINVAL;
1497c04ac679SLorenzo Pieralisi 			dev_dbg(dma_dev, "Invalid DMA regions configuration\n");
1498c04ac679SLorenzo Pieralisi 			goto out;
1499c04ac679SLorenzo Pieralisi 		}
1500c04ac679SLorenzo Pieralisi 
1501c04ac679SLorenzo Pieralisi 		*dma_addr = dma_start - dma_offset;
1502c04ac679SLorenzo Pieralisi 		len = dma_end - dma_start;
1503c04ac679SLorenzo Pieralisi 		*size = max(len, len + 1);
1504c04ac679SLorenzo Pieralisi 		*offset = dma_offset;
1505c04ac679SLorenzo Pieralisi 	}
1506c04ac679SLorenzo Pieralisi  out:
1507c04ac679SLorenzo Pieralisi 	acpi_dev_free_resource_list(&list);
1508c04ac679SLorenzo Pieralisi 
1509c04ac679SLorenzo Pieralisi 	return ret >= 0 ? 0 : ret;
1510c04ac679SLorenzo Pieralisi }
1511c04ac679SLorenzo Pieralisi 
1512c04ac679SLorenzo Pieralisi /**
1513c1e97359SJohn Garry  * acpi_dma_configure_id - Set-up DMA configuration for the device.
1514d760a1baSLorenzo Pieralisi  * @dev: The pointer to the device
1515d760a1baSLorenzo Pieralisi  * @attr: device dma attributes
1516b8e069a2SLorenzo Pieralisi  * @input_id: input device id const value pointer
1517d760a1baSLorenzo Pieralisi  */
1518b8e069a2SLorenzo Pieralisi int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,
1519b8e069a2SLorenzo Pieralisi 			  const u32 *input_id)
1520d760a1baSLorenzo Pieralisi {
1521643b8e4dSLorenzo Pieralisi 	const struct iommu_ops *iommu;
15227ad42639SLorenzo Pieralisi 	u64 dma_addr = 0, size = 0;
1523643b8e4dSLorenzo Pieralisi 
1524e5361ca2SRobin Murphy 	if (attr == DEV_DMA_NOT_SUPPORTED) {
1525e5361ca2SRobin Murphy 		set_dma_ops(dev, &dma_dummy_ops);
1526e5361ca2SRobin Murphy 		return 0;
1527e5361ca2SRobin Murphy 	}
1528e5361ca2SRobin Murphy 
15297ad42639SLorenzo Pieralisi 	iort_dma_setup(dev, &dma_addr, &size);
1530d760a1baSLorenzo Pieralisi 
1531b8e069a2SLorenzo Pieralisi 	iommu = iort_iommu_configure_id(dev, input_id);
153245586c70SMasahiro Yamada 	if (PTR_ERR(iommu) == -EPROBE_DEFER)
1533058f8c3fSSricharan R 		return -EPROBE_DEFER;
1534643b8e4dSLorenzo Pieralisi 
15357ad42639SLorenzo Pieralisi 	arch_setup_dma_ops(dev, dma_addr, size,
15367ad42639SLorenzo Pieralisi 				iommu, attr == DEV_DMA_COHERENT);
15375a1bb638SSricharan R 
15385a1bb638SSricharan R 	return 0;
1539d760a1baSLorenzo Pieralisi }
1540b8e069a2SLorenzo Pieralisi EXPORT_SYMBOL_GPL(acpi_dma_configure_id);
1541d760a1baSLorenzo Pieralisi 
1542d0562674SSuthikulpanit, Suravee static void acpi_init_coherency(struct acpi_device *adev)
1543d0562674SSuthikulpanit, Suravee {
1544d0562674SSuthikulpanit, Suravee 	unsigned long long cca = 0;
1545d0562674SSuthikulpanit, Suravee 	acpi_status status;
1546d0562674SSuthikulpanit, Suravee 	struct acpi_device *parent = adev->parent;
1547d0562674SSuthikulpanit, Suravee 
1548d0562674SSuthikulpanit, Suravee 	if (parent && parent->flags.cca_seen) {
1549d0562674SSuthikulpanit, Suravee 		/*
1550d0562674SSuthikulpanit, Suravee 		 * From ACPI spec, OSPM will ignore _CCA if an ancestor
1551d0562674SSuthikulpanit, Suravee 		 * already saw one.
1552d0562674SSuthikulpanit, Suravee 		 */
1553d0562674SSuthikulpanit, Suravee 		adev->flags.cca_seen = 1;
1554d0562674SSuthikulpanit, Suravee 		cca = parent->flags.coherent_dma;
1555d0562674SSuthikulpanit, Suravee 	} else {
1556d0562674SSuthikulpanit, Suravee 		status = acpi_evaluate_integer(adev->handle, "_CCA",
1557d0562674SSuthikulpanit, Suravee 					       NULL, &cca);
1558d0562674SSuthikulpanit, Suravee 		if (ACPI_SUCCESS(status))
1559d0562674SSuthikulpanit, Suravee 			adev->flags.cca_seen = 1;
1560d0562674SSuthikulpanit, Suravee 		else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1561d0562674SSuthikulpanit, Suravee 			/*
1562d0562674SSuthikulpanit, Suravee 			 * If architecture does not specify that _CCA is
1563d0562674SSuthikulpanit, Suravee 			 * required for DMA-able devices (e.g. x86),
1564d0562674SSuthikulpanit, Suravee 			 * we default to _CCA=1.
1565d0562674SSuthikulpanit, Suravee 			 */
1566d0562674SSuthikulpanit, Suravee 			cca = 1;
1567d0562674SSuthikulpanit, Suravee 		else
1568d0562674SSuthikulpanit, Suravee 			acpi_handle_debug(adev->handle,
1569d0562674SSuthikulpanit, Suravee 					  "ACPI device is missing _CCA.\n");
1570d0562674SSuthikulpanit, Suravee 	}
1571d0562674SSuthikulpanit, Suravee 
1572d0562674SSuthikulpanit, Suravee 	adev->flags.coherent_dma = cca;
1573d0562674SSuthikulpanit, Suravee }
1574d0562674SSuthikulpanit, Suravee 
1575e361d1f8SFrédéric Danis static int acpi_check_serial_bus_slave(struct acpi_resource *ares, void *data)
1576e4330d8bSJarkko Nikula {
1577e361d1f8SFrédéric Danis 	bool *is_serial_bus_slave_p = data;
1578e4330d8bSJarkko Nikula 
1579e4330d8bSJarkko Nikula 	if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
1580e4330d8bSJarkko Nikula 		return 1;
1581e4330d8bSJarkko Nikula 
1582e361d1f8SFrédéric Danis 	*is_serial_bus_slave_p = true;
1583e4330d8bSJarkko Nikula 
1584e4330d8bSJarkko Nikula 	 /* no need to do more checking */
1585e4330d8bSJarkko Nikula 	return -1;
1586e4330d8bSJarkko Nikula }
1587e4330d8bSJarkko Nikula 
1588dfda4492SJohn Garry static bool acpi_is_indirect_io_slave(struct acpi_device *device)
1589dfda4492SJohn Garry {
1590dfda4492SJohn Garry 	struct acpi_device *parent = device->parent;
1591a6f07295SJohn Garry 	static const struct acpi_device_id indirect_io_hosts[] = {
1592dfda4492SJohn Garry 		{"HISI0191", 0},
1593dfda4492SJohn Garry 		{}
1594dfda4492SJohn Garry 	};
1595dfda4492SJohn Garry 
1596dfda4492SJohn Garry 	return parent && !acpi_match_device_ids(parent, indirect_io_hosts);
1597dfda4492SJohn Garry }
1598dfda4492SJohn Garry 
1599d87fb091SJohn Garry static bool acpi_device_enumeration_by_parent(struct acpi_device *device)
1600e4330d8bSJarkko Nikula {
1601e4330d8bSJarkko Nikula 	struct list_head resource_list;
1602e361d1f8SFrédéric Danis 	bool is_serial_bus_slave = false;
1603aba94139SHans de Goede 	/*
1604aba94139SHans de Goede 	 * These devices have multiple I2cSerialBus resources and an i2c-client
1605aba94139SHans de Goede 	 * must be instantiated for each, each with its own i2c_device_id.
1606aba94139SHans de Goede 	 * Normally we only instantiate an i2c-client for the first resource,
1607aba94139SHans de Goede 	 * using the ACPI HID as id. These special cases are handled by the
1608aba94139SHans de Goede 	 * drivers/platform/x86/i2c-multi-instantiate.c driver, which knows
1609aba94139SHans de Goede 	 * which i2c_device_id to use for each resource.
1610aba94139SHans de Goede 	 */
1611aba94139SHans de Goede 	static const struct acpi_device_id i2c_multi_instantiate_ids[] = {
1612aba94139SHans de Goede 		{"BSG1160", },
161396f984d3SHans de Goede 		{"BSG2150", },
1614589edb56SHans de Goede 		{"INT33FE", },
1615a3dd034aSAndy Shevchenko 		{"INT3515", },
1616aba94139SHans de Goede 		{}
1617aba94139SHans de Goede 	};
1618e4330d8bSJarkko Nikula 
1619dfda4492SJohn Garry 	if (acpi_is_indirect_io_slave(device))
1620dfda4492SJohn Garry 		return true;
1621dfda4492SJohn Garry 
1622ca9ef3abSLukas Wunner 	/* Macs use device properties in lieu of _CRS resources */
1623ca9ef3abSLukas Wunner 	if (x86_apple_machine &&
1624ca9ef3abSLukas Wunner 	    (fwnode_property_present(&device->fwnode, "spiSclkPeriod") ||
1625e361d1f8SFrédéric Danis 	     fwnode_property_present(&device->fwnode, "i2cAddress") ||
1626e361d1f8SFrédéric Danis 	     fwnode_property_present(&device->fwnode, "baud")))
1627ca9ef3abSLukas Wunner 		return true;
1628ca9ef3abSLukas Wunner 
1629aba94139SHans de Goede 	/* Instantiate a pdev for the i2c-multi-instantiate drv to bind to */
1630aba94139SHans de Goede 	if (!acpi_match_device_ids(device, i2c_multi_instantiate_ids))
1631aba94139SHans de Goede 		return false;
1632aba94139SHans de Goede 
1633e4330d8bSJarkko Nikula 	INIT_LIST_HEAD(&resource_list);
1634e361d1f8SFrédéric Danis 	acpi_dev_get_resources(device, &resource_list,
1635e361d1f8SFrédéric Danis 			       acpi_check_serial_bus_slave,
1636e361d1f8SFrédéric Danis 			       &is_serial_bus_slave);
1637e4330d8bSJarkko Nikula 	acpi_dev_free_resource_list(&resource_list);
1638e4330d8bSJarkko Nikula 
1639e361d1f8SFrédéric Danis 	return is_serial_bus_slave;
1640e4330d8bSJarkko Nikula }
1641e4330d8bSJarkko Nikula 
164282c7d5efSRafael J. Wysocki void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1643c830dbcfSRafael J. Wysocki 			     int type)
16441da177e4SLinus Torvalds {
1645d43e167dSRafael J. Wysocki 	INIT_LIST_HEAD(&device->pnp.ids);
1646d43e167dSRafael J. Wysocki 	device->device_type = type;
1647d43e167dSRafael J. Wysocki 	device->handle = handle;
1648d43e167dSRafael J. Wysocki 	device->parent = acpi_bus_get_parent(handle);
164901bb86b3SSaravana Kannan 	fwnode_init(&device->fwnode, &acpi_device_fwnode_ops);
1650f5d9ab1dSRafael J. Wysocki 	acpi_set_device_status(device, ACPI_STA_DEFAULT);
1651d43e167dSRafael J. Wysocki 	acpi_device_get_busid(device);
1652c830dbcfSRafael J. Wysocki 	acpi_set_pnp_ids(handle, &device->pnp, type);
1653ffdcd955SMika Westerberg 	acpi_init_properties(device);
1654d43e167dSRafael J. Wysocki 	acpi_bus_get_flags(device);
16552c0d4fe0SRafael J. Wysocki 	device->flags.match_driver = false;
1656202317a5SRafael J. Wysocki 	device->flags.initialized = true;
1657d87fb091SJohn Garry 	device->flags.enumeration_by_parent =
1658d87fb091SJohn Garry 		acpi_device_enumeration_by_parent(device);
165910c7e20bSOctavian Purdila 	acpi_device_clear_enumerated(device);
1660cf860be6SRafael J. Wysocki 	device_initialize(&device->dev);
1661cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, true);
1662d0562674SSuthikulpanit, Suravee 	acpi_init_coherency(device);
16636d279758SRafael J. Wysocki }
16646d279758SRafael J. Wysocki 
16656d279758SRafael J. Wysocki static void acpi_scan_dep_init(struct acpi_device *adev)
16666d279758SRafael J. Wysocki {
16676d279758SRafael J. Wysocki 	struct acpi_dep_data *dep;
16686d279758SRafael J. Wysocki 
16696d279758SRafael J. Wysocki 	mutex_lock(&acpi_dep_list_lock);
16706d279758SRafael J. Wysocki 
16716d279758SRafael J. Wysocki 	list_for_each_entry(dep, &acpi_dep_list, node) {
16726d279758SRafael J. Wysocki 		if (dep->consumer == adev->handle)
16736d279758SRafael J. Wysocki 			adev->dep_unmet++;
16746d279758SRafael J. Wysocki 	}
16756d279758SRafael J. Wysocki 
16766d279758SRafael J. Wysocki 	mutex_unlock(&acpi_dep_list_lock);
1677cf860be6SRafael J. Wysocki }
1678bc3b0772SBjorn Helgaas 
1679cf860be6SRafael J. Wysocki void acpi_device_add_finalize(struct acpi_device *device)
1680cf860be6SRafael J. Wysocki {
1681cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, false);
1682cf860be6SRafael J. Wysocki 	kobject_uevent(&device->dev.kobj, KOBJ_ADD);
16831da177e4SLinus Torvalds }
16841da177e4SLinus Torvalds 
1685f5d9ab1dSRafael J. Wysocki static void acpi_scan_init_status(struct acpi_device *adev)
16861da177e4SLinus Torvalds {
1687f5d9ab1dSRafael J. Wysocki 	if (acpi_bus_get_status(adev))
1688f5d9ab1dSRafael J. Wysocki 		acpi_set_device_status(adev, 0);
1689f5d9ab1dSRafael J. Wysocki }
1690f5d9ab1dSRafael J. Wysocki 
16911da177e4SLinus Torvalds static int acpi_add_single_object(struct acpi_device **child,
16926d279758SRafael J. Wysocki 				  acpi_handle handle, int type, bool dep_init)
16931da177e4SLinus Torvalds {
1694e52d9d8cSRafael J. Wysocki 	struct acpi_device *device;
1695e52d9d8cSRafael J. Wysocki 	int result;
16966091b263SHans de Goede 
169736bcbec7SBurman Yan 	device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1698c830dbcfSRafael J. Wysocki 	if (!device)
1699d550d98dSPatrick Mochel 		return -ENOMEM;
17001da177e4SLinus Torvalds 
1701c830dbcfSRafael J. Wysocki 	acpi_init_device_object(device, handle, type);
170263347db0SHans de Goede 	/*
1703f5d9ab1dSRafael J. Wysocki 	 * Getting the status is delayed till here so that we can call
1704f5d9ab1dSRafael J. Wysocki 	 * acpi_bus_get_status() and use its quirk handling.  Note that
1705f5d9ab1dSRafael J. Wysocki 	 * this must be done before the get power-/wakeup_dev-flags calls.
170663347db0SHans de Goede 	 */
17076d279758SRafael J. Wysocki 	if (type == ACPI_BUS_TYPE_DEVICE || type == ACPI_BUS_TYPE_PROCESSOR) {
17086d279758SRafael J. Wysocki 		if (dep_init)
17096d279758SRafael J. Wysocki 			acpi_scan_dep_init(device);
17106d279758SRafael J. Wysocki 
1711f5d9ab1dSRafael J. Wysocki 		acpi_scan_init_status(device);
17126d279758SRafael J. Wysocki 	}
171363347db0SHans de Goede 
1714d43e167dSRafael J. Wysocki 	acpi_bus_get_power_flags(device);
1715d57d09a4SRafael J. Wysocki 	acpi_bus_get_wakeup_device_flags(device);
17161da177e4SLinus Torvalds 
1717cf860be6SRafael J. Wysocki 	result = acpi_device_add(device, acpi_device_release);
1718d43e167dSRafael J. Wysocki 	if (result) {
1719718fb0deSHugh Dickins 		acpi_device_release(&device->dev);
1720d550d98dSPatrick Mochel 		return result;
17211da177e4SLinus Torvalds 	}
17221da177e4SLinus Torvalds 
1723bc9b6407SRafael J. Wysocki 	acpi_power_add_remove_device(device, true);
1724cf860be6SRafael J. Wysocki 	acpi_device_add_finalize(device);
1725e52d9d8cSRafael J. Wysocki 
1726e52d9d8cSRafael J. Wysocki 	acpi_handle_debug(handle, "Added as %s, parent %s\n",
1727e52d9d8cSRafael J. Wysocki 			  dev_name(&device->dev), device->parent ?
1728e52d9d8cSRafael J. Wysocki 				dev_name(&device->parent->dev) : "(null)");
1729e52d9d8cSRafael J. Wysocki 
17301da177e4SLinus Torvalds 	*child = device;
1731d43e167dSRafael J. Wysocki 	return 0;
1732bf325f95SRafael J. Wysocki }
1733bf325f95SRafael J. Wysocki 
1734a4e081b0SShannon Zhao static acpi_status acpi_get_resource_memory(struct acpi_resource *ares,
1735a4e081b0SShannon Zhao 					    void *context)
1736a4e081b0SShannon Zhao {
1737a4e081b0SShannon Zhao 	struct resource *res = context;
1738a4e081b0SShannon Zhao 
1739a4e081b0SShannon Zhao 	if (acpi_dev_resource_memory(ares, res))
1740a4e081b0SShannon Zhao 		return AE_CTRL_TERMINATE;
1741a4e081b0SShannon Zhao 
1742a4e081b0SShannon Zhao 	return AE_OK;
1743a4e081b0SShannon Zhao }
1744a4e081b0SShannon Zhao 
1745a4e081b0SShannon Zhao static bool acpi_device_should_be_hidden(acpi_handle handle)
1746a4e081b0SShannon Zhao {
1747a4e081b0SShannon Zhao 	acpi_status status;
1748a4e081b0SShannon Zhao 	struct resource res;
1749a4e081b0SShannon Zhao 
1750a4e081b0SShannon Zhao 	/* Check if it should ignore the UART device */
1751a4e081b0SShannon Zhao 	if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS)))
1752a4e081b0SShannon Zhao 		return false;
1753a4e081b0SShannon Zhao 
1754a4e081b0SShannon Zhao 	/*
1755a4e081b0SShannon Zhao 	 * The UART device described in SPCR table is assumed to have only one
1756a4e081b0SShannon Zhao 	 * memory resource present. So we only look for the first one here.
1757a4e081b0SShannon Zhao 	 */
1758a4e081b0SShannon Zhao 	status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1759a4e081b0SShannon Zhao 				     acpi_get_resource_memory, &res);
1760a4e081b0SShannon Zhao 	if (ACPI_FAILURE(status) || res.start != spcr_uart_addr)
1761a4e081b0SShannon Zhao 		return false;
1762a4e081b0SShannon Zhao 
1763a4e081b0SShannon Zhao 	acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n",
1764a4e081b0SShannon Zhao 			 &res.start);
1765a4e081b0SShannon Zhao 
1766a4e081b0SShannon Zhao 	return true;
1767a4e081b0SShannon Zhao }
1768a4e081b0SShannon Zhao 
1769cde1f95fSSakari Ailus bool acpi_device_is_present(const struct acpi_device *adev)
1770202317a5SRafael J. Wysocki {
1771cde1f95fSSakari Ailus 	return adev->status.present || adev->status.functional;
1772202317a5SRafael J. Wysocki }
1773202317a5SRafael J. Wysocki 
17744b59cc1fSRafael J. Wysocki static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1775844142c3SRasmus Villemoes 				       const char *idstr,
1776c5698074SRafael J. Wysocki 				       const struct acpi_device_id **matchid)
1777c5698074SRafael J. Wysocki {
1778c5698074SRafael J. Wysocki 	const struct acpi_device_id *devid;
1779c5698074SRafael J. Wysocki 
1780aca0a4ebSRafael J. Wysocki 	if (handler->match)
1781aca0a4ebSRafael J. Wysocki 		return handler->match(idstr, matchid);
1782aca0a4ebSRafael J. Wysocki 
1783c5698074SRafael J. Wysocki 	for (devid = handler->ids; devid->id[0]; devid++)
1784c5698074SRafael J. Wysocki 		if (!strcmp((char *)devid->id, idstr)) {
1785c5698074SRafael J. Wysocki 			if (matchid)
1786c5698074SRafael J. Wysocki 				*matchid = devid;
1787c5698074SRafael J. Wysocki 
17884b59cc1fSRafael J. Wysocki 			return true;
17894b59cc1fSRafael J. Wysocki 		}
17904b59cc1fSRafael J. Wysocki 
17914b59cc1fSRafael J. Wysocki 	return false;
17924b59cc1fSRafael J. Wysocki }
17934b59cc1fSRafael J. Wysocki 
1794844142c3SRasmus Villemoes static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
17956b772e8fSToshi Kani 					const struct acpi_device_id **matchid)
17966b772e8fSToshi Kani {
17976b772e8fSToshi Kani 	struct acpi_scan_handler *handler;
17986b772e8fSToshi Kani 
17996b772e8fSToshi Kani 	list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
18006b772e8fSToshi Kani 		if (acpi_scan_handler_matching(handler, idstr, matchid))
18016b772e8fSToshi Kani 			return handler;
18026b772e8fSToshi Kani 
18036b772e8fSToshi Kani 	return NULL;
18046b772e8fSToshi Kani }
18056b772e8fSToshi Kani 
18063f8055c3SRafael J. Wysocki void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
18073f8055c3SRafael J. Wysocki {
18083f8055c3SRafael J. Wysocki 	if (!!hotplug->enabled == !!val)
18093f8055c3SRafael J. Wysocki 		return;
18103f8055c3SRafael J. Wysocki 
18113f8055c3SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
18123f8055c3SRafael J. Wysocki 
18133f8055c3SRafael J. Wysocki 	hotplug->enabled = val;
18143f8055c3SRafael J. Wysocki 
18153f8055c3SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
18163f8055c3SRafael J. Wysocki }
18173f8055c3SRafael J. Wysocki 
18183c2cc7ffSRafael J. Wysocki static void acpi_scan_init_hotplug(struct acpi_device *adev)
18194b59cc1fSRafael J. Wysocki {
18206b772e8fSToshi Kani 	struct acpi_hardware_id *hwid;
18213c2cc7ffSRafael J. Wysocki 
1822b43109faSRafael J. Wysocki 	if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
18231e2380cdSRafael J. Wysocki 		acpi_dock_add(adev);
18241e2380cdSRafael J. Wysocki 		return;
18251e2380cdSRafael J. Wysocki 	}
18263c2cc7ffSRafael J. Wysocki 	list_for_each_entry(hwid, &adev->pnp.ids, list) {
18274b59cc1fSRafael J. Wysocki 		struct acpi_scan_handler *handler;
18284b59cc1fSRafael J. Wysocki 
18296b772e8fSToshi Kani 		handler = acpi_scan_match_handler(hwid->id, NULL);
18301a699476SRafael J. Wysocki 		if (handler) {
18313c2cc7ffSRafael J. Wysocki 			adev->flags.hotplug_notify = true;
18326b772e8fSToshi Kani 			break;
18336b772e8fSToshi Kani 		}
18346b772e8fSToshi Kani 	}
18351a699476SRafael J. Wysocki }
18366b772e8fSToshi Kani 
183702056a4fSRafael J. Wysocki static u32 acpi_scan_check_dep(acpi_handle handle, bool check_dep)
183840e7fcb1SLan Tianyu {
183940e7fcb1SLan Tianyu 	struct acpi_handle_list dep_devices;
184040e7fcb1SLan Tianyu 	acpi_status status;
18416fc25088SRafael J. Wysocki 	u32 count;
184240e7fcb1SLan Tianyu 	int i;
184340e7fcb1SLan Tianyu 
184471da201fSRafael J. Wysocki 	/*
184571da201fSRafael J. Wysocki 	 * Check for _HID here to avoid deferring the enumeration of:
184671da201fSRafael J. Wysocki 	 * 1. PCI devices.
184771da201fSRafael J. Wysocki 	 * 2. ACPI nodes describing USB ports.
184871da201fSRafael J. Wysocki 	 * Still, checking for _HID catches more then just these cases ...
184971da201fSRafael J. Wysocki 	 */
185002056a4fSRafael J. Wysocki 	if (!check_dep || !acpi_has_method(handle, "_DEP") ||
185102056a4fSRafael J. Wysocki 	    !acpi_has_method(handle, "_HID"))
18526fc25088SRafael J. Wysocki 		return 0;
185363347db0SHans de Goede 
18546fc25088SRafael J. Wysocki 	status = acpi_evaluate_reference(handle, "_DEP", NULL, &dep_devices);
185540e7fcb1SLan Tianyu 	if (ACPI_FAILURE(status)) {
18566fc25088SRafael J. Wysocki 		acpi_handle_debug(handle, "Failed to evaluate _DEP.\n");
18576fc25088SRafael J. Wysocki 		return 0;
185840e7fcb1SLan Tianyu 	}
185940e7fcb1SLan Tianyu 
18606fc25088SRafael J. Wysocki 	for (count = 0, i = 0; i < dep_devices.count; i++) {
186140e7fcb1SLan Tianyu 		struct acpi_device_info *info;
18626fc25088SRafael J. Wysocki 		struct acpi_dep_data *dep;
18636fc25088SRafael J. Wysocki 		bool skip;
186440e7fcb1SLan Tianyu 
186540e7fcb1SLan Tianyu 		status = acpi_get_object_info(dep_devices.handles[i], &info);
186640e7fcb1SLan Tianyu 		if (ACPI_FAILURE(status)) {
18676fc25088SRafael J. Wysocki 			acpi_handle_debug(handle, "Error reading _DEP device info\n");
186840e7fcb1SLan Tianyu 			continue;
186940e7fcb1SLan Tianyu 		}
187040e7fcb1SLan Tianyu 
18712ef33ee7SRafael J. Wysocki 		skip = acpi_info_matches_ids(info, acpi_ignore_dep_ids);
187240e7fcb1SLan Tianyu 		kfree(info);
187340e7fcb1SLan Tianyu 
187440e7fcb1SLan Tianyu 		if (skip)
187540e7fcb1SLan Tianyu 			continue;
187640e7fcb1SLan Tianyu 
18776fc25088SRafael J. Wysocki 		dep = kzalloc(sizeof(*dep), GFP_KERNEL);
187840e7fcb1SLan Tianyu 		if (!dep)
18796fc25088SRafael J. Wysocki 			continue;
18806fc25088SRafael J. Wysocki 
18816fc25088SRafael J. Wysocki 		count++;
188240e7fcb1SLan Tianyu 
188391438aebSRafael J. Wysocki 		dep->supplier = dep_devices.handles[i];
18846fc25088SRafael J. Wysocki 		dep->consumer = handle;
188540e7fcb1SLan Tianyu 
188640e7fcb1SLan Tianyu 		mutex_lock(&acpi_dep_list_lock);
188740e7fcb1SLan Tianyu 		list_add_tail(&dep->node , &acpi_dep_list);
188840e7fcb1SLan Tianyu 		mutex_unlock(&acpi_dep_list_lock);
188940e7fcb1SLan Tianyu 	}
18906fc25088SRafael J. Wysocki 
18916fc25088SRafael J. Wysocki 	return count;
189240e7fcb1SLan Tianyu }
189340e7fcb1SLan Tianyu 
18940de7fb7cSRafael J. Wysocki static bool acpi_bus_scan_second_pass;
18950de7fb7cSRafael J. Wysocki 
189671da201fSRafael J. Wysocki static acpi_status acpi_bus_check_add(acpi_handle handle, bool check_dep,
189771da201fSRafael J. Wysocki 				      struct acpi_device **adev_p)
18981da177e4SLinus Torvalds {
1899805d410fSRafael J. Wysocki 	struct acpi_device *device = NULL;
1900e6c1067dSRafael J. Wysocki 	acpi_object_type acpi_type;
19016fc25088SRafael J. Wysocki 	int type;
19021da177e4SLinus Torvalds 
19034002bf38SRafael J. Wysocki 	acpi_bus_get_device(handle, &device);
19044002bf38SRafael J. Wysocki 	if (device)
19054002bf38SRafael J. Wysocki 		goto out;
19064002bf38SRafael J. Wysocki 
1907e6c1067dSRafael J. Wysocki 	if (ACPI_FAILURE(acpi_get_type(handle, &acpi_type)))
190851a85fafSBjorn Helgaas 		return AE_OK;
19091da177e4SLinus Torvalds 
1910e6c1067dSRafael J. Wysocki 	switch (acpi_type) {
1911e6c1067dSRafael J. Wysocki 	case ACPI_TYPE_DEVICE:
1912e6c1067dSRafael J. Wysocki 		if (acpi_device_should_be_hidden(handle))
191382c7d5efSRafael J. Wysocki 			return AE_OK;
191482c7d5efSRafael J. Wysocki 
191502056a4fSRafael J. Wysocki 		/* Bail out if there are dependencies. */
191602056a4fSRafael J. Wysocki 		if (acpi_scan_check_dep(handle, check_dep) > 0) {
19170de7fb7cSRafael J. Wysocki 			acpi_bus_scan_second_pass = true;
191871da201fSRafael J. Wysocki 			return AE_CTRL_DEPTH;
191971da201fSRafael J. Wysocki 		}
192002056a4fSRafael J. Wysocki 
1921e6c1067dSRafael J. Wysocki 		fallthrough;
1922e6c1067dSRafael J. Wysocki 	case ACPI_TYPE_ANY:	/* for ACPI_ROOT_OBJECT */
1923e6c1067dSRafael J. Wysocki 		type = ACPI_BUS_TYPE_DEVICE;
1924e6c1067dSRafael J. Wysocki 		break;
1925e6c1067dSRafael J. Wysocki 
1926e6c1067dSRafael J. Wysocki 	case ACPI_TYPE_PROCESSOR:
1927e6c1067dSRafael J. Wysocki 		type = ACPI_BUS_TYPE_PROCESSOR;
1928e6c1067dSRafael J. Wysocki 		break;
1929e6c1067dSRafael J. Wysocki 
1930e6c1067dSRafael J. Wysocki 	case ACPI_TYPE_THERMAL:
1931e6c1067dSRafael J. Wysocki 		type = ACPI_BUS_TYPE_THERMAL;
1932e6c1067dSRafael J. Wysocki 		break;
1933e6c1067dSRafael J. Wysocki 
1934e6c1067dSRafael J. Wysocki 	case ACPI_TYPE_POWER:
193502056a4fSRafael J. Wysocki 		acpi_add_power_resource(handle);
193602056a4fSRafael J. Wysocki 		fallthrough;
1937e6c1067dSRafael J. Wysocki 	default:
1938e6c1067dSRafael J. Wysocki 		return AE_OK;
19390de7fb7cSRafael J. Wysocki 	}
19406fc25088SRafael J. Wysocki 
19413e759425SHans de Goede 	/*
19423e759425SHans de Goede 	 * If check_dep is true at this point, the device has no dependencies,
19433e759425SHans de Goede 	 * or the creation of the device object would have been postponed above.
19443e759425SHans de Goede 	 */
19456d279758SRafael J. Wysocki 	acpi_add_single_object(&device, handle, type, !check_dep);
19466d279758SRafael J. Wysocki 	if (!device)
19476d279758SRafael J. Wysocki 		return AE_CTRL_DEPTH;
19486d279758SRafael J. Wysocki 
19496d279758SRafael J. Wysocki 	acpi_scan_init_hotplug(device);
19503c2cc7ffSRafael J. Wysocki 
19514002bf38SRafael J. Wysocki out:
195271da201fSRafael J. Wysocki 	if (!*adev_p)
195371da201fSRafael J. Wysocki 		*adev_p = device;
1954805d410fSRafael J. Wysocki 
195551a85fafSBjorn Helgaas 	return AE_OK;
19561da177e4SLinus Torvalds }
19571da177e4SLinus Torvalds 
195871da201fSRafael J. Wysocki static acpi_status acpi_bus_check_add_1(acpi_handle handle, u32 lvl_not_used,
195971da201fSRafael J. Wysocki 					void *not_used, void **ret_p)
196071da201fSRafael J. Wysocki {
196171da201fSRafael J. Wysocki 	return acpi_bus_check_add(handle, true, (struct acpi_device **)ret_p);
196271da201fSRafael J. Wysocki }
196371da201fSRafael J. Wysocki 
196471da201fSRafael J. Wysocki static acpi_status acpi_bus_check_add_2(acpi_handle handle, u32 lvl_not_used,
196571da201fSRafael J. Wysocki 					void *not_used, void **ret_p)
196671da201fSRafael J. Wysocki {
196771da201fSRafael J. Wysocki 	return acpi_bus_check_add(handle, false, (struct acpi_device **)ret_p);
196871da201fSRafael J. Wysocki }
196971da201fSRafael J. Wysocki 
197048459340SZhang Rui static void acpi_default_enumeration(struct acpi_device *device)
197148459340SZhang Rui {
197248459340SZhang Rui 	/*
1973d87fb091SJohn Garry 	 * Do not enumerate devices with enumeration_by_parent flag set as
1974d87fb091SJohn Garry 	 * they will be enumerated by their respective parents.
197548459340SZhang Rui 	 */
1976d87fb091SJohn Garry 	if (!device->flags.enumeration_by_parent) {
19771571875bSHeikki Krogerus 		acpi_create_platform_device(device, NULL);
197810c7e20bSOctavian Purdila 		acpi_device_set_enumerated(device);
197968bdb677SOctavian Purdila 	} else {
198068bdb677SOctavian Purdila 		blocking_notifier_call_chain(&acpi_reconfig_chain,
198168bdb677SOctavian Purdila 					     ACPI_RECONFIG_DEVICE_ADD, device);
198210c7e20bSOctavian Purdila 	}
198348459340SZhang Rui }
198448459340SZhang Rui 
19857d284352SRafael J. Wysocki static const struct acpi_device_id generic_device_ids[] = {
1986ee892094SRafael J. Wysocki 	{ACPI_DT_NAMESPACE_HID, },
19877d284352SRafael J. Wysocki 	{"", },
19887d284352SRafael J. Wysocki };
19897d284352SRafael J. Wysocki 
19907d284352SRafael J. Wysocki static int acpi_generic_device_attach(struct acpi_device *adev,
19917d284352SRafael J. Wysocki 				      const struct acpi_device_id *not_used)
19927d284352SRafael J. Wysocki {
19937d284352SRafael J. Wysocki 	/*
1994ee892094SRafael J. Wysocki 	 * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
1995ee892094SRafael J. Wysocki 	 * below can be unconditional.
19967d284352SRafael J. Wysocki 	 */
19977d284352SRafael J. Wysocki 	if (adev->data.of_compatible)
19987d284352SRafael J. Wysocki 		acpi_default_enumeration(adev);
19997d284352SRafael J. Wysocki 
20007d284352SRafael J. Wysocki 	return 1;
20017d284352SRafael J. Wysocki }
20027d284352SRafael J. Wysocki 
20037d284352SRafael J. Wysocki static struct acpi_scan_handler generic_device_handler = {
20047d284352SRafael J. Wysocki 	.ids = generic_device_ids,
20057d284352SRafael J. Wysocki 	.attach = acpi_generic_device_attach,
20067d284352SRafael J. Wysocki };
20077d284352SRafael J. Wysocki 
200887b85b3cSRafael J. Wysocki static int acpi_scan_attach_handler(struct acpi_device *device)
200987b85b3cSRafael J. Wysocki {
201087b85b3cSRafael J. Wysocki 	struct acpi_hardware_id *hwid;
201187b85b3cSRafael J. Wysocki 	int ret = 0;
201287b85b3cSRafael J. Wysocki 
201387b85b3cSRafael J. Wysocki 	list_for_each_entry(hwid, &device->pnp.ids, list) {
2014c5698074SRafael J. Wysocki 		const struct acpi_device_id *devid;
2015c5698074SRafael J. Wysocki 		struct acpi_scan_handler *handler;
201687b85b3cSRafael J. Wysocki 
2017c5698074SRafael J. Wysocki 		handler = acpi_scan_match_handler(hwid->id, &devid);
2018c5698074SRafael J. Wysocki 		if (handler) {
2019d34afa9dSRafael J. Wysocki 			if (!handler->attach) {
2020d34afa9dSRafael J. Wysocki 				device->pnp.type.platform_id = 0;
2021d34afa9dSRafael J. Wysocki 				continue;
2022d34afa9dSRafael J. Wysocki 			}
2023c5698074SRafael J. Wysocki 			device->handler = handler;
20249cb32acfSRafael J. Wysocki 			ret = handler->attach(device, devid);
20259cb32acfSRafael J. Wysocki 			if (ret > 0)
2026c5698074SRafael J. Wysocki 				break;
20279cb32acfSRafael J. Wysocki 
20289cb32acfSRafael J. Wysocki 			device->handler = NULL;
20299cb32acfSRafael J. Wysocki 			if (ret < 0)
2030c5698074SRafael J. Wysocki 				break;
2031c5698074SRafael J. Wysocki 		}
2032c5698074SRafael J. Wysocki 	}
203348459340SZhang Rui 
2034ca589f94SRafael J. Wysocki 	return ret;
2035ca589f94SRafael J. Wysocki }
2036ca589f94SRafael J. Wysocki 
203771da201fSRafael J. Wysocki static void acpi_bus_attach(struct acpi_device *device, bool first_pass)
203851a85fafSBjorn Helgaas {
20392c22e652SRafael J. Wysocki 	struct acpi_device *child;
204071da201fSRafael J. Wysocki 	bool skip = !first_pass && device->flags.visited;
20411e2380cdSRafael J. Wysocki 	acpi_handle ejd;
2042ca589f94SRafael J. Wysocki 	int ret;
20431da177e4SLinus Torvalds 
204471da201fSRafael J. Wysocki 	if (skip)
204571da201fSRafael J. Wysocki 		goto ok;
204671da201fSRafael J. Wysocki 
20471e2380cdSRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
20481e2380cdSRafael J. Wysocki 		register_dock_dependent_device(device, ejd);
20491e2380cdSRafael J. Wysocki 
20502c22e652SRafael J. Wysocki 	acpi_bus_get_status(device);
2051202317a5SRafael J. Wysocki 	/* Skip devices that are not present. */
20522c22e652SRafael J. Wysocki 	if (!acpi_device_is_present(device)) {
2053cde1f95fSSakari Ailus 		device->flags.initialized = false;
205410c7e20bSOctavian Purdila 		acpi_device_clear_enumerated(device);
20551b1f3e16SRafael J. Wysocki 		device->flags.power_manageable = 0;
20562c22e652SRafael J. Wysocki 		return;
20572c22e652SRafael J. Wysocki 	}
20583a391a39SRafael J. Wysocki 	if (device->handler)
20592c22e652SRafael J. Wysocki 		goto ok;
20603a391a39SRafael J. Wysocki 
2061202317a5SRafael J. Wysocki 	if (!device->flags.initialized) {
20621b1f3e16SRafael J. Wysocki 		device->flags.power_manageable =
20631b1f3e16SRafael J. Wysocki 			device->power.states[ACPI_STATE_D0].flags.valid;
20641b1f3e16SRafael J. Wysocki 		if (acpi_bus_init_power(device))
20651b1f3e16SRafael J. Wysocki 			device->flags.power_manageable = 0;
20661b1f3e16SRafael J. Wysocki 
2067202317a5SRafael J. Wysocki 		device->flags.initialized = true;
2068c381fc3aSRafael J. Wysocki 	} else if (device->flags.visited) {
2069c381fc3aSRafael J. Wysocki 		goto ok;
2070202317a5SRafael J. Wysocki 	}
207110c7e20bSOctavian Purdila 
2072ca589f94SRafael J. Wysocki 	ret = acpi_scan_attach_handler(device);
20736931007cSRafael J. Wysocki 	if (ret < 0)
20742c22e652SRafael J. Wysocki 		return;
20756931007cSRafael J. Wysocki 
20766931007cSRafael J. Wysocki 	device->flags.match_driver = true;
2077d87fb091SJohn Garry 	if (ret > 0 && !device->flags.enumeration_by_parent) {
2078f406270bSRafael J. Wysocki 		acpi_device_set_enumerated(device);
2079f406270bSRafael J. Wysocki 		goto ok;
2080f406270bSRafael J. Wysocki 	}
2081f406270bSRafael J. Wysocki 
2082ca589f94SRafael J. Wysocki 	ret = device_attach(&device->dev);
2083202317a5SRafael J. Wysocki 	if (ret < 0)
20842c22e652SRafael J. Wysocki 		return;
20857d284352SRafael J. Wysocki 
2086d87fb091SJohn Garry 	if (device->pnp.type.platform_id || device->flags.enumeration_by_parent)
2087e4330d8bSJarkko Nikula 		acpi_default_enumeration(device);
2088d87fb091SJohn Garry 	else
2089d87fb091SJohn Garry 		acpi_device_set_enumerated(device);
2090202317a5SRafael J. Wysocki 
2091202317a5SRafael J. Wysocki  ok:
20922c22e652SRafael J. Wysocki 	list_for_each_entry(child, &device->children, node)
209371da201fSRafael J. Wysocki 		acpi_bus_attach(child, first_pass);
20948ab17fc9SRafael J. Wysocki 
209571da201fSRafael J. Wysocki 	if (!skip && device->handler && device->handler->hotplug.notify_online)
20968ab17fc9SRafael J. Wysocki 		device->handler->hotplug.notify_online(device);
2097805d410fSRafael J. Wysocki }
20983fb02738SRajesh Shah 
2099b83e2b30SDaniel Scally static int acpi_dev_get_first_consumer_dev_cb(struct acpi_dep_data *dep, void *data)
2100b83e2b30SDaniel Scally {
2101b83e2b30SDaniel Scally 	struct acpi_device *adev;
2102b83e2b30SDaniel Scally 
2103b83e2b30SDaniel Scally 	adev = acpi_bus_get_acpi_device(dep->consumer);
2104ad4d451eSRafael J. Wysocki 	if (adev) {
2105b83e2b30SDaniel Scally 		*(struct acpi_device **)data = adev;
2106b83e2b30SDaniel Scally 		return 1;
2107b83e2b30SDaniel Scally 	}
2108ad4d451eSRafael J. Wysocki 	/* Continue parsing if the device object is not present. */
2109ad4d451eSRafael J. Wysocki 	return 0;
2110ad4d451eSRafael J. Wysocki }
2111b83e2b30SDaniel Scally 
2112*dc612486SRafael J. Wysocki struct acpi_scan_clear_dep_work {
2113*dc612486SRafael J. Wysocki 	struct work_struct work;
2114*dc612486SRafael J. Wysocki 	struct acpi_device *adev;
2115*dc612486SRafael J. Wysocki };
2116*dc612486SRafael J. Wysocki 
2117*dc612486SRafael J. Wysocki static void acpi_scan_clear_dep_fn(struct work_struct *work)
2118*dc612486SRafael J. Wysocki {
2119*dc612486SRafael J. Wysocki 	struct acpi_scan_clear_dep_work *cdw;
2120*dc612486SRafael J. Wysocki 
2121*dc612486SRafael J. Wysocki 	cdw = container_of(work, struct acpi_scan_clear_dep_work, work);
2122*dc612486SRafael J. Wysocki 
2123*dc612486SRafael J. Wysocki 	acpi_scan_lock_acquire();
2124*dc612486SRafael J. Wysocki 	acpi_bus_attach(cdw->adev, true);
2125*dc612486SRafael J. Wysocki 	acpi_scan_lock_release();
2126*dc612486SRafael J. Wysocki 
2127*dc612486SRafael J. Wysocki 	acpi_dev_put(cdw->adev);
2128*dc612486SRafael J. Wysocki 	kfree(cdw);
2129*dc612486SRafael J. Wysocki }
2130*dc612486SRafael J. Wysocki 
2131*dc612486SRafael J. Wysocki static bool acpi_scan_clear_dep_queue(struct acpi_device *adev)
2132*dc612486SRafael J. Wysocki {
2133*dc612486SRafael J. Wysocki 	struct acpi_scan_clear_dep_work *cdw;
2134*dc612486SRafael J. Wysocki 
2135*dc612486SRafael J. Wysocki 	if (adev->dep_unmet)
2136*dc612486SRafael J. Wysocki 		return false;
2137*dc612486SRafael J. Wysocki 
2138*dc612486SRafael J. Wysocki 	cdw = kmalloc(sizeof(*cdw), GFP_KERNEL);
2139*dc612486SRafael J. Wysocki 	if (!cdw)
2140*dc612486SRafael J. Wysocki 		return false;
2141*dc612486SRafael J. Wysocki 
2142*dc612486SRafael J. Wysocki 	cdw->adev = adev;
2143*dc612486SRafael J. Wysocki 	INIT_WORK(&cdw->work, acpi_scan_clear_dep_fn);
2144*dc612486SRafael J. Wysocki 	/*
2145*dc612486SRafael J. Wysocki 	 * Since the work function may block on the lock until the entire
2146*dc612486SRafael J. Wysocki 	 * initial enumeration of devices is complete, put it into the unbound
2147*dc612486SRafael J. Wysocki 	 * workqueue.
2148*dc612486SRafael J. Wysocki 	 */
2149*dc612486SRafael J. Wysocki 	queue_work(system_unbound_wq, &cdw->work);
2150*dc612486SRafael J. Wysocki 
2151*dc612486SRafael J. Wysocki 	return true;
2152*dc612486SRafael J. Wysocki }
2153*dc612486SRafael J. Wysocki 
2154a9e10e58SDaniel Scally static int acpi_scan_clear_dep(struct acpi_dep_data *dep, void *data)
215540e7fcb1SLan Tianyu {
2156*dc612486SRafael J. Wysocki 	struct acpi_device *adev = acpi_bus_get_acpi_device(dep->consumer);
215740e7fcb1SLan Tianyu 
21580f347aa0SHans de Goede 	if (adev) {
215940e7fcb1SLan Tianyu 		adev->dep_unmet--;
2160*dc612486SRafael J. Wysocki 		if (!acpi_scan_clear_dep_queue(adev))
2161*dc612486SRafael J. Wysocki 			acpi_dev_put(adev);
21620f347aa0SHans de Goede 	}
216371da201fSRafael J. Wysocki 
216440e7fcb1SLan Tianyu 	list_del(&dep->node);
216540e7fcb1SLan Tianyu 	kfree(dep);
2166a9e10e58SDaniel Scally 
2167a9e10e58SDaniel Scally 	return 0;
2168a9e10e58SDaniel Scally }
2169a9e10e58SDaniel Scally 
2170a9e10e58SDaniel Scally /**
2171a9e10e58SDaniel Scally  * acpi_walk_dep_device_list - Apply a callback to every entry in acpi_dep_list
2172a9e10e58SDaniel Scally  * @handle:	The ACPI handle of the supplier device
2173a9e10e58SDaniel Scally  * @callback:	Pointer to the callback function to apply
2174a9e10e58SDaniel Scally  * @data:	Pointer to some data to pass to the callback
2175a9e10e58SDaniel Scally  *
2176a9e10e58SDaniel Scally  * The return value of the callback determines this function's behaviour. If 0
2177a9e10e58SDaniel Scally  * is returned we continue to iterate over acpi_dep_list. If a positive value
2178a9e10e58SDaniel Scally  * is returned then the loop is broken but this function returns 0. If a
2179a9e10e58SDaniel Scally  * negative value is returned by the callback then the loop is broken and that
2180a9e10e58SDaniel Scally  * value is returned as the final error.
2181a9e10e58SDaniel Scally  */
2182aff0dbd0SRafael J. Wysocki static int acpi_walk_dep_device_list(acpi_handle handle,
2183a9e10e58SDaniel Scally 				int (*callback)(struct acpi_dep_data *, void *),
2184a9e10e58SDaniel Scally 				void *data)
2185a9e10e58SDaniel Scally {
2186a9e10e58SDaniel Scally 	struct acpi_dep_data *dep, *tmp;
218723db673dSColin Ian King 	int ret = 0;
2188a9e10e58SDaniel Scally 
2189a9e10e58SDaniel Scally 	mutex_lock(&acpi_dep_list_lock);
2190a9e10e58SDaniel Scally 	list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
2191a9e10e58SDaniel Scally 		if (dep->supplier == handle) {
2192a9e10e58SDaniel Scally 			ret = callback(dep, data);
2193a9e10e58SDaniel Scally 			if (ret)
2194a9e10e58SDaniel Scally 				break;
219540e7fcb1SLan Tianyu 		}
219640e7fcb1SLan Tianyu 	}
219740e7fcb1SLan Tianyu 	mutex_unlock(&acpi_dep_list_lock);
2198a9e10e58SDaniel Scally 
2199a9e10e58SDaniel Scally 	return ret > 0 ? 0 : ret;
220040e7fcb1SLan Tianyu }
220140e7fcb1SLan Tianyu 
2202b8bd759aSRafael J. Wysocki /**
2203a9e10e58SDaniel Scally  * acpi_dev_clear_dependencies - Inform consumers that the device is now active
2204a9e10e58SDaniel Scally  * @supplier: Pointer to the supplier &struct acpi_device
2205a9e10e58SDaniel Scally  *
2206a9e10e58SDaniel Scally  * Clear dependencies on the given device.
2207a9e10e58SDaniel Scally  */
2208a9e10e58SDaniel Scally void acpi_dev_clear_dependencies(struct acpi_device *supplier)
2209a9e10e58SDaniel Scally {
2210a9e10e58SDaniel Scally 	acpi_walk_dep_device_list(supplier->handle, acpi_scan_clear_dep, NULL);
2211a9e10e58SDaniel Scally }
2212a9e10e58SDaniel Scally EXPORT_SYMBOL_GPL(acpi_dev_clear_dependencies);
2213a9e10e58SDaniel Scally 
2214a9e10e58SDaniel Scally /**
2215b83e2b30SDaniel Scally  * acpi_dev_get_first_consumer_dev - Return ACPI device dependent on @supplier
2216b83e2b30SDaniel Scally  * @supplier: Pointer to the dependee device
2217b83e2b30SDaniel Scally  *
2218b83e2b30SDaniel Scally  * Returns the first &struct acpi_device which declares itself dependent on
2219b83e2b30SDaniel Scally  * @supplier via the _DEP buffer, parsed from the acpi_dep_list.
2220b83e2b30SDaniel Scally  *
2221b83e2b30SDaniel Scally  * The caller is responsible for putting the reference to adev when it is no
2222b83e2b30SDaniel Scally  * longer needed.
2223b83e2b30SDaniel Scally  */
2224b83e2b30SDaniel Scally struct acpi_device *acpi_dev_get_first_consumer_dev(struct acpi_device *supplier)
2225b83e2b30SDaniel Scally {
2226b83e2b30SDaniel Scally 	struct acpi_device *adev = NULL;
2227b83e2b30SDaniel Scally 
2228b83e2b30SDaniel Scally 	acpi_walk_dep_device_list(supplier->handle,
2229b83e2b30SDaniel Scally 				  acpi_dev_get_first_consumer_dev_cb, &adev);
2230b83e2b30SDaniel Scally 
2231b83e2b30SDaniel Scally 	return adev;
2232b83e2b30SDaniel Scally }
2233b83e2b30SDaniel Scally EXPORT_SYMBOL_GPL(acpi_dev_get_first_consumer_dev);
2234b83e2b30SDaniel Scally 
2235b83e2b30SDaniel Scally /**
2236b8bd759aSRafael J. Wysocki  * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2237b8bd759aSRafael J. Wysocki  * @handle: Root of the namespace scope to scan.
2238b8bd759aSRafael J. Wysocki  *
2239b8bd759aSRafael J. Wysocki  * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2240b8bd759aSRafael J. Wysocki  * found devices.
2241b8bd759aSRafael J. Wysocki  *
2242b8bd759aSRafael J. Wysocki  * If no devices were found, -ENODEV is returned, but it does not mean that
2243b8bd759aSRafael J. Wysocki  * there has been a real error.  There just have been no suitable ACPI objects
2244b8bd759aSRafael J. Wysocki  * in the table trunk from which the kernel could create a device and add an
2245b8bd759aSRafael J. Wysocki  * appropriate driver.
22463757b948SRafael J. Wysocki  *
22473757b948SRafael J. Wysocki  * Must be called under acpi_scan_lock.
2248b8bd759aSRafael J. Wysocki  */
2249b8bd759aSRafael J. Wysocki int acpi_bus_scan(acpi_handle handle)
22503fb02738SRajesh Shah {
225171da201fSRafael J. Wysocki 	struct acpi_device *device = NULL;
2252c511cc19SRafael J. Wysocki 
22530de7fb7cSRafael J. Wysocki 	acpi_bus_scan_second_pass = false;
22540de7fb7cSRafael J. Wysocki 
225571da201fSRafael J. Wysocki 	/* Pass 1: Avoid enumerating devices with missing dependencies. */
225671da201fSRafael J. Wysocki 
225771da201fSRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_check_add(handle, true, &device)))
22581da177e4SLinus Torvalds 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
225971da201fSRafael J. Wysocki 				    acpi_bus_check_add_1, NULL, NULL,
226071da201fSRafael J. Wysocki 				    (void **)&device);
22613fb02738SRajesh Shah 
226271da201fSRafael J. Wysocki 	if (!device)
22632c22e652SRafael J. Wysocki 		return -ENODEV;
226471da201fSRafael J. Wysocki 
226571da201fSRafael J. Wysocki 	acpi_bus_attach(device, true);
226671da201fSRafael J. Wysocki 
22670de7fb7cSRafael J. Wysocki 	if (!acpi_bus_scan_second_pass)
22680de7fb7cSRafael J. Wysocki 		return 0;
22690de7fb7cSRafael J. Wysocki 
227071da201fSRafael J. Wysocki 	/* Pass 2: Enumerate all of the remaining devices. */
227171da201fSRafael J. Wysocki 
227271da201fSRafael J. Wysocki 	device = NULL;
227371da201fSRafael J. Wysocki 
227471da201fSRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_check_add(handle, false, &device)))
227571da201fSRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
227671da201fSRafael J. Wysocki 				    acpi_bus_check_add_2, NULL, NULL,
227771da201fSRafael J. Wysocki 				    (void **)&device);
227871da201fSRafael J. Wysocki 
227971da201fSRafael J. Wysocki 	acpi_bus_attach(device, false);
228071da201fSRafael J. Wysocki 
228171da201fSRafael J. Wysocki 	return 0;
22823fb02738SRajesh Shah }
2283b8bd759aSRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_scan);
22841da177e4SLinus Torvalds 
22853757b948SRafael J. Wysocki /**
22862c22e652SRafael J. Wysocki  * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
22872c22e652SRafael J. Wysocki  * @adev: Root of the ACPI namespace scope to walk.
22883757b948SRafael J. Wysocki  *
22893757b948SRafael J. Wysocki  * Must be called under acpi_scan_lock.
22903757b948SRafael J. Wysocki  */
22912c22e652SRafael J. Wysocki void acpi_bus_trim(struct acpi_device *adev)
22921da177e4SLinus Torvalds {
22932c22e652SRafael J. Wysocki 	struct acpi_scan_handler *handler = adev->handler;
22942c22e652SRafael J. Wysocki 	struct acpi_device *child;
22952c22e652SRafael J. Wysocki 
22962c22e652SRafael J. Wysocki 	list_for_each_entry_reverse(child, &adev->children, node)
22972c22e652SRafael J. Wysocki 		acpi_bus_trim(child);
22982c22e652SRafael J. Wysocki 
2299a951c773SRafael J. Wysocki 	adev->flags.match_driver = false;
23002c22e652SRafael J. Wysocki 	if (handler) {
23012c22e652SRafael J. Wysocki 		if (handler->detach)
23022c22e652SRafael J. Wysocki 			handler->detach(adev);
23032c22e652SRafael J. Wysocki 
23042c22e652SRafael J. Wysocki 		adev->handler = NULL;
23052c22e652SRafael J. Wysocki 	} else {
23062c22e652SRafael J. Wysocki 		device_release_driver(&adev->dev);
23072c22e652SRafael J. Wysocki 	}
23081da177e4SLinus Torvalds 	/*
23092c22e652SRafael J. Wysocki 	 * Most likely, the device is going away, so put it into D3cold before
23102c22e652SRafael J. Wysocki 	 * that.
23111da177e4SLinus Torvalds 	 */
23122c22e652SRafael J. Wysocki 	acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
23132c22e652SRafael J. Wysocki 	adev->flags.initialized = false;
231410c7e20bSOctavian Purdila 	acpi_device_clear_enumerated(adev);
23151da177e4SLinus Torvalds }
2316ceaba663SKristen Accardi EXPORT_SYMBOL_GPL(acpi_bus_trim);
2317ceaba663SKristen Accardi 
2318a64a62ceSLv Zheng int acpi_bus_register_early_device(int type)
2319a64a62ceSLv Zheng {
2320a64a62ceSLv Zheng 	struct acpi_device *device = NULL;
2321a64a62ceSLv Zheng 	int result;
2322a64a62ceSLv Zheng 
23236d279758SRafael J. Wysocki 	result = acpi_add_single_object(&device, NULL, type, false);
2324a64a62ceSLv Zheng 	if (result)
2325a64a62ceSLv Zheng 		return result;
2326a64a62ceSLv Zheng 
2327a64a62ceSLv Zheng 	device->flags.match_driver = true;
2328a64a62ceSLv Zheng 	return device_attach(&device->dev);
2329a64a62ceSLv Zheng }
2330a64a62ceSLv Zheng EXPORT_SYMBOL_GPL(acpi_bus_register_early_device);
2331a64a62ceSLv Zheng 
2332e8b945c9SBjorn Helgaas static int acpi_bus_scan_fixed(void)
23331da177e4SLinus Torvalds {
23341da177e4SLinus Torvalds 	int result = 0;
2335c4168bffSLi Shaohua 
23361da177e4SLinus Torvalds 	/*
23371da177e4SLinus Torvalds 	 * Enumerate all fixed-feature devices.
23381da177e4SLinus Torvalds 	 */
23392c0d4fe0SRafael J. Wysocki 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
23402c0d4fe0SRafael J. Wysocki 		struct acpi_device *device = NULL;
23412c0d4fe0SRafael J. Wysocki 
23425c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
23436d279758SRafael J. Wysocki 						ACPI_BUS_TYPE_POWER_BUTTON, false);
23442c0d4fe0SRafael J. Wysocki 		if (result)
23452c0d4fe0SRafael J. Wysocki 			return result;
23462c0d4fe0SRafael J. Wysocki 
234788346167SRafael J. Wysocki 		device->flags.match_driver = true;
23482c0d4fe0SRafael J. Wysocki 		result = device_attach(&device->dev);
23492c0d4fe0SRafael J. Wysocki 		if (result < 0)
23502c0d4fe0SRafael J. Wysocki 			return result;
23512c0d4fe0SRafael J. Wysocki 
2352c10d7a13SDaniel Drake 		device_init_wakeup(&device->dev, true);
23533fb02738SRajesh Shah 	}
23541da177e4SLinus Torvalds 
23552c0d4fe0SRafael J. Wysocki 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
23562c0d4fe0SRafael J. Wysocki 		struct acpi_device *device = NULL;
23572c0d4fe0SRafael J. Wysocki 
23585c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
23596d279758SRafael J. Wysocki 						ACPI_BUS_TYPE_SLEEP_BUTTON, false);
23602c0d4fe0SRafael J. Wysocki 		if (result)
23612c0d4fe0SRafael J. Wysocki 			return result;
23622c0d4fe0SRafael J. Wysocki 
236388346167SRafael J. Wysocki 		device->flags.match_driver = true;
23642c0d4fe0SRafael J. Wysocki 		result = device_attach(&device->dev);
23653fb02738SRajesh Shah 	}
23661da177e4SLinus Torvalds 
23672c0d4fe0SRafael J. Wysocki 	return result < 0 ? result : 0;
23681da177e4SLinus Torvalds }
23691da177e4SLinus Torvalds 
2370a4e081b0SShannon Zhao static void __init acpi_get_spcr_uart_addr(void)
2371a4e081b0SShannon Zhao {
2372a4e081b0SShannon Zhao 	acpi_status status;
2373a4e081b0SShannon Zhao 	struct acpi_table_spcr *spcr_ptr;
2374a4e081b0SShannon Zhao 
2375a4e081b0SShannon Zhao 	status = acpi_get_table(ACPI_SIG_SPCR, 0,
2376a4e081b0SShannon Zhao 				(struct acpi_table_header **)&spcr_ptr);
2377ead7ba13SHanjun Guo 	if (ACPI_FAILURE(status)) {
2378ead7ba13SHanjun Guo 		pr_warn(PREFIX "STAO table present, but SPCR is missing\n");
2379ead7ba13SHanjun Guo 		return;
2380ead7ba13SHanjun Guo 	}
2381ead7ba13SHanjun Guo 
2382a4e081b0SShannon Zhao 	spcr_uart_addr = spcr_ptr->serial_port.address;
2383ead7ba13SHanjun Guo 	acpi_put_table((struct acpi_table_header *)spcr_ptr);
2384a4e081b0SShannon Zhao }
2385a4e081b0SShannon Zhao 
238668bdb677SOctavian Purdila static bool acpi_scan_initialized;
238768bdb677SOctavian Purdila 
2388e747f274SBjorn Helgaas int __init acpi_scan_init(void)
23891da177e4SLinus Torvalds {
23901da177e4SLinus Torvalds 	int result;
2391a4e081b0SShannon Zhao 	acpi_status status;
2392a4e081b0SShannon Zhao 	struct acpi_table_stao *stao_ptr;
23931da177e4SLinus Torvalds 
239492ef2a25SRafael J. Wysocki 	acpi_pci_root_init();
23954daeaf68SRafael J. Wysocki 	acpi_pci_link_init();
2396ac212b69SRafael J. Wysocki 	acpi_processor_init();
2397cb0701acSAndy Shevchenko 	acpi_platform_init();
2398f58b082aSRafael J. Wysocki 	acpi_lpss_init();
239992082a88SKen Xue 	acpi_apd_init();
24002fa97febSLan Tianyu 	acpi_cmos_rtc_init();
2401737f1a9fSRafael J. Wysocki 	acpi_container_init();
24020a347644SRafael J. Wysocki 	acpi_memory_hotplug_init();
2403cc6a0e31SMika Westerberg 	acpi_watchdog_init();
2404eec15edbSZhang Rui 	acpi_pnp_init();
24053230bbfcSZhang Rui 	acpi_int340x_thermal_init();
24066ce2e188SGraeme Gregory 	acpi_amba_init();
2407eeb2d80dSSrinivas Pandruvada 	acpi_init_lpit();
240897d9a9e9SRafael J. Wysocki 
24097d284352SRafael J. Wysocki 	acpi_scan_add_handler(&generic_device_handler);
24107d284352SRafael J. Wysocki 
2411a4e081b0SShannon Zhao 	/*
2412a4e081b0SShannon Zhao 	 * If there is STAO table, check whether it needs to ignore the UART
2413a4e081b0SShannon Zhao 	 * device in SPCR table.
2414a4e081b0SShannon Zhao 	 */
2415a4e081b0SShannon Zhao 	status = acpi_get_table(ACPI_SIG_STAO, 0,
2416a4e081b0SShannon Zhao 				(struct acpi_table_header **)&stao_ptr);
2417a4e081b0SShannon Zhao 	if (ACPI_SUCCESS(status)) {
2418a4e081b0SShannon Zhao 		if (stao_ptr->header.length > sizeof(struct acpi_table_stao))
2419ead7ba13SHanjun Guo 			pr_info(PREFIX "STAO Name List not yet supported.\n");
2420a4e081b0SShannon Zhao 
2421a4e081b0SShannon Zhao 		if (stao_ptr->ignore_uart)
2422a4e081b0SShannon Zhao 			acpi_get_spcr_uart_addr();
2423ead7ba13SHanjun Guo 
2424ead7ba13SHanjun Guo 		acpi_put_table((struct acpi_table_header *)stao_ptr);
2425a4e081b0SShannon Zhao 	}
2426a4e081b0SShannon Zhao 
2427eb7f43c4SRafael J. Wysocki 	acpi_gpe_apply_masked_gpes();
2428eb7f43c4SRafael J. Wysocki 	acpi_update_all_gpes();
2429eb7f43c4SRafael J. Wysocki 
24307291edcaSDavid Hildenbrand 	/*
24317291edcaSDavid Hildenbrand 	 * Although we call __add_memory() that is documented to require the
24327291edcaSDavid Hildenbrand 	 * device_hotplug_lock, it is not necessary here because this is an
24337291edcaSDavid Hildenbrand 	 * early code when userspace or any other code path cannot trigger
24347291edcaSDavid Hildenbrand 	 * hotplug/hotunplug operations.
24357291edcaSDavid Hildenbrand 	 */
24363757b948SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
24371da177e4SLinus Torvalds 	/*
24381da177e4SLinus Torvalds 	 * Enumerate devices in the ACPI namespace.
24391da177e4SLinus Torvalds 	 */
24400cd6ac52SRafael J. Wysocki 	result = acpi_bus_scan(ACPI_ROOT_OBJECT);
24411da177e4SLinus Torvalds 	if (result)
24423757b948SRafael J. Wysocki 		goto out;
24431da177e4SLinus Torvalds 
24440cd6ac52SRafael J. Wysocki 	result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
24451da177e4SLinus Torvalds 	if (result)
24463757b948SRafael J. Wysocki 		goto out;
2447b8bd759aSRafael J. Wysocki 
24482650ef42SAaron Lu 	/* Fixed feature devices do not exist on HW-reduced platform */
24492650ef42SAaron Lu 	if (!acpi_gbl_reduced_hardware) {
2450b8bd759aSRafael J. Wysocki 		result = acpi_bus_scan_fixed();
2451b8bd759aSRafael J. Wysocki 		if (result) {
24522650ef42SAaron Lu 			acpi_detach_data(acpi_root->handle,
24532650ef42SAaron Lu 					 acpi_scan_drop_device);
2454202317a5SRafael J. Wysocki 			acpi_device_del(acpi_root);
24554cbaba4eSAndy Shevchenko 			acpi_bus_put_acpi_device(acpi_root);
24563757b948SRafael J. Wysocki 			goto out;
2457b8bd759aSRafael J. Wysocki 		}
24582650ef42SAaron Lu 	}
2459b8bd759aSRafael J. Wysocki 
246029038ae2SRafael J. Wysocki 	acpi_turn_off_unused_power_resources();
246129038ae2SRafael J. Wysocki 
246268bdb677SOctavian Purdila 	acpi_scan_initialized = true;
246368bdb677SOctavian Purdila 
24643757b948SRafael J. Wysocki  out:
24653757b948SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
24663757b948SRafael J. Wysocki 	return result;
24671da177e4SLinus Torvalds }
2468e647b532SMarc Zyngier 
2469e647b532SMarc Zyngier static struct acpi_probe_entry *ape;
2470e647b532SMarc Zyngier static int acpi_probe_count;
24715331d9caSLorenzo Pieralisi static DEFINE_MUTEX(acpi_probe_mutex);
2472e647b532SMarc Zyngier 
247360574d1eSKeith Busch static int __init acpi_match_madt(union acpi_subtable_headers *header,
2474e647b532SMarc Zyngier 				  const unsigned long end)
2475e647b532SMarc Zyngier {
247660574d1eSKeith Busch 	if (!ape->subtable_valid || ape->subtable_valid(&header->common, ape))
2477e647b532SMarc Zyngier 		if (!ape->probe_subtbl(header, end))
2478e647b532SMarc Zyngier 			acpi_probe_count++;
2479e647b532SMarc Zyngier 
2480e647b532SMarc Zyngier 	return 0;
2481e647b532SMarc Zyngier }
2482e647b532SMarc Zyngier 
2483e647b532SMarc Zyngier int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
2484e647b532SMarc Zyngier {
2485e647b532SMarc Zyngier 	int count = 0;
2486e647b532SMarc Zyngier 
2487e647b532SMarc Zyngier 	if (acpi_disabled)
2488e647b532SMarc Zyngier 		return 0;
2489e647b532SMarc Zyngier 
24905331d9caSLorenzo Pieralisi 	mutex_lock(&acpi_probe_mutex);
2491e647b532SMarc Zyngier 	for (ape = ap_head; nr; ape++, nr--) {
24925599fb69SBob Moore 		if (ACPI_COMPARE_NAMESEG(ACPI_SIG_MADT, ape->id)) {
2493e647b532SMarc Zyngier 			acpi_probe_count = 0;
2494e647b532SMarc Zyngier 			acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
2495e647b532SMarc Zyngier 			count += acpi_probe_count;
2496e647b532SMarc Zyngier 		} else {
2497e647b532SMarc Zyngier 			int res;
2498e647b532SMarc Zyngier 			res = acpi_table_parse(ape->id, ape->probe_table);
2499e647b532SMarc Zyngier 			if (!res)
2500e647b532SMarc Zyngier 				count++;
2501e647b532SMarc Zyngier 		}
2502e647b532SMarc Zyngier 	}
25035331d9caSLorenzo Pieralisi 	mutex_unlock(&acpi_probe_mutex);
2504e647b532SMarc Zyngier 
2505e647b532SMarc Zyngier 	return count;
2506e647b532SMarc Zyngier }
250768bdb677SOctavian Purdila 
250868bdb677SOctavian Purdila struct acpi_table_events_work {
250968bdb677SOctavian Purdila 	struct work_struct work;
251068bdb677SOctavian Purdila 	void *table;
251168bdb677SOctavian Purdila 	u32 event;
251268bdb677SOctavian Purdila };
251368bdb677SOctavian Purdila 
251468bdb677SOctavian Purdila static void acpi_table_events_fn(struct work_struct *work)
251568bdb677SOctavian Purdila {
251668bdb677SOctavian Purdila 	struct acpi_table_events_work *tew;
251768bdb677SOctavian Purdila 
251868bdb677SOctavian Purdila 	tew = container_of(work, struct acpi_table_events_work, work);
251968bdb677SOctavian Purdila 
252068bdb677SOctavian Purdila 	if (tew->event == ACPI_TABLE_EVENT_LOAD) {
252168bdb677SOctavian Purdila 		acpi_scan_lock_acquire();
252268bdb677SOctavian Purdila 		acpi_bus_scan(ACPI_ROOT_OBJECT);
252368bdb677SOctavian Purdila 		acpi_scan_lock_release();
252468bdb677SOctavian Purdila 	}
252568bdb677SOctavian Purdila 
252668bdb677SOctavian Purdila 	kfree(tew);
252768bdb677SOctavian Purdila }
252868bdb677SOctavian Purdila 
252968bdb677SOctavian Purdila void acpi_scan_table_handler(u32 event, void *table, void *context)
253068bdb677SOctavian Purdila {
253168bdb677SOctavian Purdila 	struct acpi_table_events_work *tew;
253268bdb677SOctavian Purdila 
253368bdb677SOctavian Purdila 	if (!acpi_scan_initialized)
253468bdb677SOctavian Purdila 		return;
253568bdb677SOctavian Purdila 
253668bdb677SOctavian Purdila 	if (event != ACPI_TABLE_EVENT_LOAD)
253768bdb677SOctavian Purdila 		return;
253868bdb677SOctavian Purdila 
253968bdb677SOctavian Purdila 	tew = kmalloc(sizeof(*tew), GFP_KERNEL);
254068bdb677SOctavian Purdila 	if (!tew)
254168bdb677SOctavian Purdila 		return;
254268bdb677SOctavian Purdila 
254368bdb677SOctavian Purdila 	INIT_WORK(&tew->work, acpi_table_events_fn);
254468bdb677SOctavian Purdila 	tew->table = table;
254568bdb677SOctavian Purdila 	tew->event = event;
254668bdb677SOctavian Purdila 
254768bdb677SOctavian Purdila 	schedule_work(&tew->work);
254868bdb677SOctavian Purdila }
254968bdb677SOctavian Purdila 
255068bdb677SOctavian Purdila int acpi_reconfig_notifier_register(struct notifier_block *nb)
255168bdb677SOctavian Purdila {
255268bdb677SOctavian Purdila 	return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
255368bdb677SOctavian Purdila }
255468bdb677SOctavian Purdila EXPORT_SYMBOL(acpi_reconfig_notifier_register);
255568bdb677SOctavian Purdila 
255668bdb677SOctavian Purdila int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
255768bdb677SOctavian Purdila {
255868bdb677SOctavian Purdila 	return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
255968bdb677SOctavian Purdila }
256068bdb677SOctavian Purdila EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);
2561