xref: /openbmc/linux/drivers/acpi/scan.c (revision e399037e06beec6d190a302ff5e4fee4aae77c50)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * scan.c - support for transforming the ACPI namespace into individual objects
31da177e4SLinus Torvalds  */
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds #include <linux/module.h>
61da177e4SLinus Torvalds #include <linux/init.h>
75a0e3ad6STejun Heo #include <linux/slab.h>
89b6d97b6SRandy Dunlap #include <linux/kernel.h>
91da177e4SLinus Torvalds #include <linux/acpi.h>
1074523c90SAlok N Kataria #include <linux/signal.h>
1174523c90SAlok N Kataria #include <linux/kthread.h>
12222e82acSDarrick J. Wong #include <linux/dmi.h>
13d1efe3c3SLance Ortiz #include <linux/nls.h>
14d0562674SSuthikulpanit, Suravee #include <linux/dma-mapping.h>
151da177e4SLinus Torvalds 
16d783156eSRafael J. Wysocki #include <asm/pgtable.h>
17d783156eSRafael J. Wysocki 
18e60cc7a6SBjorn Helgaas #include "internal.h"
19e60cc7a6SBjorn Helgaas 
201da177e4SLinus Torvalds #define _COMPONENT		ACPI_BUS_COMPONENT
21f52fd66dSLen Brown ACPI_MODULE_NAME("scan");
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 
32683058e3SRafael J. Wysocki /*
33683058e3SRafael J. Wysocki  * If set, devices will be hot-removed even if they cannot be put offline
34683058e3SRafael J. Wysocki  * gracefully (from the kernel's standpoint).
35683058e3SRafael J. Wysocki  */
36683058e3SRafael J. Wysocki bool acpi_force_hot_remove;
37683058e3SRafael J. Wysocki 
38620e112cSThomas Renninger static const char *dummy_hid = "device";
392b2ae7c7SThomas Renninger 
4040e7fcb1SLan Tianyu static LIST_HEAD(acpi_dep_list);
4140e7fcb1SLan Tianyu static DEFINE_MUTEX(acpi_dep_list_lock);
42e49bd2ddSZhang Rui static LIST_HEAD(acpi_bus_id_list);
43c511cc19SRafael J. Wysocki static DEFINE_MUTEX(acpi_scan_lock);
44ca589f94SRafael J. Wysocki static LIST_HEAD(acpi_scan_handlers_list);
459090589dSShaohua Li DEFINE_MUTEX(acpi_device_lock);
461da177e4SLinus Torvalds LIST_HEAD(acpi_wakeup_device_list);
47e525506fSRafael J. Wysocki static DEFINE_MUTEX(acpi_hp_context_lock);
481da177e4SLinus Torvalds 
4940e7fcb1SLan Tianyu struct acpi_dep_data {
5040e7fcb1SLan Tianyu 	struct list_head node;
5140e7fcb1SLan Tianyu 	acpi_handle master;
5240e7fcb1SLan Tianyu 	acpi_handle slave;
5340e7fcb1SLan Tianyu };
5440e7fcb1SLan Tianyu 
55e49bd2ddSZhang Rui struct acpi_device_bus_id{
56bb095854SZhang Rui 	char bus_id[15];
57e49bd2ddSZhang Rui 	unsigned int instance_no;
58e49bd2ddSZhang Rui 	struct list_head node;
59e49bd2ddSZhang Rui };
6029b71a1cSThomas Renninger 
613757b948SRafael J. Wysocki void acpi_scan_lock_acquire(void)
623757b948SRafael J. Wysocki {
633757b948SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
643757b948SRafael J. Wysocki }
653757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
663757b948SRafael J. Wysocki 
673757b948SRafael J. Wysocki void acpi_scan_lock_release(void)
683757b948SRafael J. Wysocki {
693757b948SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
703757b948SRafael J. Wysocki }
713757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
723757b948SRafael J. Wysocki 
73e525506fSRafael J. Wysocki void acpi_lock_hp_context(void)
74e525506fSRafael J. Wysocki {
75e525506fSRafael J. Wysocki 	mutex_lock(&acpi_hp_context_lock);
76e525506fSRafael J. Wysocki }
77e525506fSRafael J. Wysocki 
78e525506fSRafael J. Wysocki void acpi_unlock_hp_context(void)
79e525506fSRafael J. Wysocki {
80e525506fSRafael J. Wysocki 	mutex_unlock(&acpi_hp_context_lock);
81e525506fSRafael J. Wysocki }
82e525506fSRafael J. Wysocki 
835d513205SRafael J. Wysocki void acpi_initialize_hp_context(struct acpi_device *adev,
845d513205SRafael J. Wysocki 				struct acpi_hotplug_context *hp,
855d513205SRafael J. Wysocki 				int (*notify)(struct acpi_device *, u32),
865d513205SRafael J. Wysocki 				void (*uevent)(struct acpi_device *, u32))
875d513205SRafael J. Wysocki {
885d513205SRafael J. Wysocki 	acpi_lock_hp_context();
89ba574dc8SRafael J. Wysocki 	hp->notify = notify;
90ba574dc8SRafael J. Wysocki 	hp->uevent = uevent;
91ba574dc8SRafael J. Wysocki 	acpi_set_hp_context(adev, hp);
925d513205SRafael J. Wysocki 	acpi_unlock_hp_context();
935d513205SRafael J. Wysocki }
945d513205SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
955d513205SRafael J. Wysocki 
96ca589f94SRafael J. Wysocki int acpi_scan_add_handler(struct acpi_scan_handler *handler)
97ca589f94SRafael J. Wysocki {
98d34afa9dSRafael J. Wysocki 	if (!handler)
99ca589f94SRafael J. Wysocki 		return -EINVAL;
100ca589f94SRafael J. Wysocki 
101ca589f94SRafael J. Wysocki 	list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
102ca589f94SRafael J. Wysocki 	return 0;
103ca589f94SRafael J. Wysocki }
104ca589f94SRafael J. Wysocki 
1053f8055c3SRafael J. Wysocki int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
1063f8055c3SRafael J. Wysocki 				       const char *hotplug_profile_name)
1073f8055c3SRafael J. Wysocki {
1083f8055c3SRafael J. Wysocki 	int error;
1093f8055c3SRafael J. Wysocki 
1103f8055c3SRafael J. Wysocki 	error = acpi_scan_add_handler(handler);
1113f8055c3SRafael J. Wysocki 	if (error)
1123f8055c3SRafael J. Wysocki 		return error;
1133f8055c3SRafael J. Wysocki 
1143f8055c3SRafael J. Wysocki 	acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
1153f8055c3SRafael J. Wysocki 	return 0;
1163f8055c3SRafael J. Wysocki }
1173f8055c3SRafael J. Wysocki 
118caa73ea1SRafael J. Wysocki bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
119d22ddcbcSRafael J. Wysocki {
120d22ddcbcSRafael J. Wysocki 	struct acpi_device_physical_node *pn;
121d22ddcbcSRafael J. Wysocki 	bool offline = true;
122d22ddcbcSRafael J. Wysocki 
1234c533c80SRafael J. Wysocki 	/*
1244c533c80SRafael J. Wysocki 	 * acpi_container_offline() calls this for all of the container's
1254c533c80SRafael J. Wysocki 	 * children under the container's physical_node_lock lock.
1264c533c80SRafael J. Wysocki 	 */
1274c533c80SRafael J. Wysocki 	mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING);
128d22ddcbcSRafael J. Wysocki 
129d22ddcbcSRafael J. Wysocki 	list_for_each_entry(pn, &adev->physical_node_list, node)
130d22ddcbcSRafael J. Wysocki 		if (device_supports_offline(pn->dev) && !pn->dev->offline) {
131caa73ea1SRafael J. Wysocki 			if (uevent)
132d22ddcbcSRafael J. Wysocki 				kobject_uevent(&pn->dev->kobj, KOBJ_CHANGE);
133caa73ea1SRafael J. Wysocki 
134d22ddcbcSRafael J. Wysocki 			offline = false;
135d22ddcbcSRafael J. Wysocki 			break;
136d22ddcbcSRafael J. Wysocki 		}
137d22ddcbcSRafael J. Wysocki 
138d22ddcbcSRafael J. Wysocki 	mutex_unlock(&adev->physical_node_lock);
139d22ddcbcSRafael J. Wysocki 	return offline;
140d22ddcbcSRafael J. Wysocki }
141d22ddcbcSRafael J. Wysocki 
1427f28ddecSRafael J. Wysocki static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
1437f28ddecSRafael J. Wysocki 				    void **ret_p)
144683058e3SRafael J. Wysocki {
145683058e3SRafael J. Wysocki 	struct acpi_device *device = NULL;
146683058e3SRafael J. Wysocki 	struct acpi_device_physical_node *pn;
147303bfdb1SRafael J. Wysocki 	bool second_pass = (bool)data;
148683058e3SRafael J. Wysocki 	acpi_status status = AE_OK;
149683058e3SRafael J. Wysocki 
150683058e3SRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
151683058e3SRafael J. Wysocki 		return AE_OK;
152683058e3SRafael J. Wysocki 
1537f28ddecSRafael J. Wysocki 	if (device->handler && !device->handler->hotplug.enabled) {
1547f28ddecSRafael J. Wysocki 		*ret_p = &device->dev;
1557f28ddecSRafael J. Wysocki 		return AE_SUPPORT;
1567f28ddecSRafael J. Wysocki 	}
1577f28ddecSRafael J. Wysocki 
158683058e3SRafael J. Wysocki 	mutex_lock(&device->physical_node_lock);
159683058e3SRafael J. Wysocki 
160683058e3SRafael J. Wysocki 	list_for_each_entry(pn, &device->physical_node_list, node) {
161683058e3SRafael J. Wysocki 		int ret;
162683058e3SRafael J. Wysocki 
163303bfdb1SRafael J. Wysocki 		if (second_pass) {
164303bfdb1SRafael J. Wysocki 			/* Skip devices offlined by the first pass. */
165303bfdb1SRafael J. Wysocki 			if (pn->put_online)
166303bfdb1SRafael J. Wysocki 				continue;
167303bfdb1SRafael J. Wysocki 		} else {
168303bfdb1SRafael J. Wysocki 			pn->put_online = false;
169303bfdb1SRafael J. Wysocki 		}
170683058e3SRafael J. Wysocki 		ret = device_offline(pn->dev);
171683058e3SRafael J. Wysocki 		if (acpi_force_hot_remove)
172683058e3SRafael J. Wysocki 			continue;
173683058e3SRafael J. Wysocki 
174303bfdb1SRafael J. Wysocki 		if (ret >= 0) {
175303bfdb1SRafael J. Wysocki 			pn->put_online = !ret;
176303bfdb1SRafael J. Wysocki 		} else {
177303bfdb1SRafael J. Wysocki 			*ret_p = pn->dev;
178303bfdb1SRafael J. Wysocki 			if (second_pass) {
179683058e3SRafael J. Wysocki 				status = AE_ERROR;
180683058e3SRafael J. Wysocki 				break;
181683058e3SRafael J. Wysocki 			}
182303bfdb1SRafael J. Wysocki 		}
183683058e3SRafael J. Wysocki 	}
184683058e3SRafael J. Wysocki 
185683058e3SRafael J. Wysocki 	mutex_unlock(&device->physical_node_lock);
186683058e3SRafael J. Wysocki 
187683058e3SRafael J. Wysocki 	return status;
188683058e3SRafael J. Wysocki }
189683058e3SRafael J. Wysocki 
1907f28ddecSRafael J. Wysocki static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
1917f28ddecSRafael J. Wysocki 				   void **ret_p)
192683058e3SRafael J. Wysocki {
193683058e3SRafael J. Wysocki 	struct acpi_device *device = NULL;
194683058e3SRafael J. Wysocki 	struct acpi_device_physical_node *pn;
195683058e3SRafael J. Wysocki 
196683058e3SRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
197683058e3SRafael J. Wysocki 		return AE_OK;
198683058e3SRafael J. Wysocki 
199683058e3SRafael J. Wysocki 	mutex_lock(&device->physical_node_lock);
200683058e3SRafael J. Wysocki 
201683058e3SRafael J. Wysocki 	list_for_each_entry(pn, &device->physical_node_list, node)
202683058e3SRafael J. Wysocki 		if (pn->put_online) {
203683058e3SRafael J. Wysocki 			device_online(pn->dev);
204683058e3SRafael J. Wysocki 			pn->put_online = false;
205683058e3SRafael J. Wysocki 		}
206683058e3SRafael J. Wysocki 
207683058e3SRafael J. Wysocki 	mutex_unlock(&device->physical_node_lock);
208683058e3SRafael J. Wysocki 
209683058e3SRafael J. Wysocki 	return AE_OK;
210683058e3SRafael J. Wysocki }
211683058e3SRafael J. Wysocki 
212d22ddcbcSRafael J. Wysocki static int acpi_scan_try_to_offline(struct acpi_device *device)
2131da177e4SLinus Torvalds {
2145993c467SYinghai Lu 	acpi_handle handle = device->handle;
215d22ddcbcSRafael J. Wysocki 	struct device *errdev = NULL;
216a33ec399SRafael J. Wysocki 	acpi_status status;
217f058cdf4SRafael J. Wysocki 
218303bfdb1SRafael J. Wysocki 	/*
219303bfdb1SRafael J. Wysocki 	 * Carry out two passes here and ignore errors in the first pass,
220303bfdb1SRafael J. Wysocki 	 * because if the devices in question are memory blocks and
221303bfdb1SRafael J. Wysocki 	 * CONFIG_MEMCG is set, one of the blocks may hold data structures
222303bfdb1SRafael J. Wysocki 	 * that the other blocks depend on, but it is not known in advance which
223303bfdb1SRafael J. Wysocki 	 * block holds them.
224303bfdb1SRafael J. Wysocki 	 *
225303bfdb1SRafael J. Wysocki 	 * If the first pass is successful, the second one isn't needed, though.
226303bfdb1SRafael J. Wysocki 	 */
2277f28ddecSRafael J. Wysocki 	status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2287f28ddecSRafael J. Wysocki 				     NULL, acpi_bus_offline, (void *)false,
2297f28ddecSRafael J. Wysocki 				     (void **)&errdev);
2307f28ddecSRafael J. Wysocki 	if (status == AE_SUPPORT) {
2317f28ddecSRafael J. Wysocki 		dev_warn(errdev, "Offline disabled.\n");
232683058e3SRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2337f28ddecSRafael J. Wysocki 				    acpi_bus_online, NULL, NULL, NULL);
2347f28ddecSRafael J. Wysocki 		return -EPERM;
2357f28ddecSRafael J. Wysocki 	}
2367f28ddecSRafael J. Wysocki 	acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
237303bfdb1SRafael J. Wysocki 	if (errdev) {
238303bfdb1SRafael J. Wysocki 		errdev = NULL;
239303bfdb1SRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2407f28ddecSRafael J. Wysocki 				    NULL, acpi_bus_offline, (void *)true,
2417f28ddecSRafael J. Wysocki 				    (void **)&errdev);
242303bfdb1SRafael J. Wysocki 		if (!errdev || acpi_force_hot_remove)
2437f28ddecSRafael J. Wysocki 			acpi_bus_offline(handle, 0, (void *)true,
244303bfdb1SRafael J. Wysocki 					 (void **)&errdev);
245303bfdb1SRafael J. Wysocki 
246303bfdb1SRafael J. Wysocki 		if (errdev && !acpi_force_hot_remove) {
247303bfdb1SRafael J. Wysocki 			dev_warn(errdev, "Offline failed.\n");
2487f28ddecSRafael J. Wysocki 			acpi_bus_online(handle, 0, NULL, NULL);
249303bfdb1SRafael J. Wysocki 			acpi_walk_namespace(ACPI_TYPE_ANY, handle,
2507f28ddecSRafael J. Wysocki 					    ACPI_UINT32_MAX, acpi_bus_online,
2517f28ddecSRafael J. Wysocki 					    NULL, NULL, NULL);
252683058e3SRafael J. Wysocki 			return -EBUSY;
253683058e3SRafael J. Wysocki 		}
254303bfdb1SRafael J. Wysocki 	}
255d22ddcbcSRafael J. Wysocki 	return 0;
256d22ddcbcSRafael J. Wysocki }
257d22ddcbcSRafael J. Wysocki 
258d22ddcbcSRafael J. Wysocki static int acpi_scan_hot_remove(struct acpi_device *device)
259d22ddcbcSRafael J. Wysocki {
260d22ddcbcSRafael J. Wysocki 	acpi_handle handle = device->handle;
261d22ddcbcSRafael J. Wysocki 	unsigned long long sta;
262d22ddcbcSRafael J. Wysocki 	acpi_status status;
263d22ddcbcSRafael J. Wysocki 
264dee15926STang Chen 	if (device->handler && device->handler->hotplug.demand_offline
265dee15926STang Chen 	    && !acpi_force_hot_remove) {
266caa73ea1SRafael J. Wysocki 		if (!acpi_scan_is_offline(device, true))
267d22ddcbcSRafael J. Wysocki 			return -EBUSY;
268d22ddcbcSRafael J. Wysocki 	} else {
269d22ddcbcSRafael J. Wysocki 		int error = acpi_scan_try_to_offline(device);
270d22ddcbcSRafael J. Wysocki 		if (error)
271d22ddcbcSRafael J. Wysocki 			return error;
272d22ddcbcSRafael J. Wysocki 	}
273683058e3SRafael J. Wysocki 
27426d46867SZhang Rui 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
2750794469dSKay Sievers 		"Hot-removing device %s...\n", dev_name(&device->dev)));
27626d46867SZhang Rui 
277bfee26dbSRafael J. Wysocki 	acpi_bus_trim(device);
278683058e3SRafael J. Wysocki 
2797d2421f8SJiang Liu 	acpi_evaluate_lck(handle, 0);
2801da177e4SLinus Torvalds 	/*
2811da177e4SLinus Torvalds 	 * TBD: _EJD support.
2821da177e4SLinus Torvalds 	 */
2837d2421f8SJiang Liu 	status = acpi_evaluate_ej0(handle);
2847d2421f8SJiang Liu 	if (status == AE_NOT_FOUND)
285a33ec399SRafael J. Wysocki 		return -ENODEV;
2867d2421f8SJiang Liu 	else if (ACPI_FAILURE(status))
287a33ec399SRafael J. Wysocki 		return -EIO;
288f058cdf4SRafael J. Wysocki 
289882fd12eSToshi Kani 	/*
290882fd12eSToshi Kani 	 * Verify if eject was indeed successful.  If not, log an error
291882fd12eSToshi Kani 	 * message.  No need to call _OST since _EJ0 call was made OK.
292882fd12eSToshi Kani 	 */
293882fd12eSToshi Kani 	status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
294882fd12eSToshi Kani 	if (ACPI_FAILURE(status)) {
295882fd12eSToshi Kani 		acpi_handle_warn(handle,
296882fd12eSToshi Kani 			"Status check after eject failed (0x%x)\n", status);
297882fd12eSToshi Kani 	} else if (sta & ACPI_STA_DEVICE_ENABLED) {
298882fd12eSToshi Kani 		acpi_handle_warn(handle,
299882fd12eSToshi Kani 			"Eject incomplete - status 0x%llx\n", sta);
300882fd12eSToshi Kani 	}
301882fd12eSToshi Kani 
302a33ec399SRafael J. Wysocki 	return 0;
303a33ec399SRafael J. Wysocki }
304f058cdf4SRafael J. Wysocki 
305443fc820SRafael J. Wysocki static int acpi_scan_device_not_present(struct acpi_device *adev)
306443fc820SRafael J. Wysocki {
307443fc820SRafael J. Wysocki 	if (!acpi_device_enumerated(adev)) {
308443fc820SRafael J. Wysocki 		dev_warn(&adev->dev, "Still not present\n");
309443fc820SRafael J. Wysocki 		return -EALREADY;
310443fc820SRafael J. Wysocki 	}
311443fc820SRafael J. Wysocki 	acpi_bus_trim(adev);
312443fc820SRafael J. Wysocki 	return 0;
313443fc820SRafael J. Wysocki }
314443fc820SRafael J. Wysocki 
315c27b2c33SRafael J. Wysocki static int acpi_scan_device_check(struct acpi_device *adev)
316a33ec399SRafael J. Wysocki {
317f943db40SRafael J. Wysocki 	int error;
318a33ec399SRafael J. Wysocki 
319443fc820SRafael J. Wysocki 	acpi_bus_get_status(adev);
320443fc820SRafael J. Wysocki 	if (adev->status.present || adev->status.functional) {
321443fc820SRafael J. Wysocki 		/*
322443fc820SRafael J. Wysocki 		 * This function is only called for device objects for which
323443fc820SRafael J. Wysocki 		 * matching scan handlers exist.  The only situation in which
324443fc820SRafael J. Wysocki 		 * the scan handler is not attached to this device object yet
325443fc820SRafael J. Wysocki 		 * is when the device has just appeared (either it wasn't
326443fc820SRafael J. Wysocki 		 * present at all before or it was removed and then added
327443fc820SRafael J. Wysocki 		 * again).
328443fc820SRafael J. Wysocki 		 */
329443fc820SRafael J. Wysocki 		if (adev->handler) {
330443fc820SRafael J. Wysocki 			dev_warn(&adev->dev, "Already enumerated\n");
331443fc820SRafael J. Wysocki 			return -EALREADY;
332443fc820SRafael J. Wysocki 		}
333c27b2c33SRafael J. Wysocki 		error = acpi_bus_scan(adev->handle);
334a33ec399SRafael J. Wysocki 		if (error) {
335c27b2c33SRafael J. Wysocki 			dev_warn(&adev->dev, "Namespace scan failure\n");
336c27b2c33SRafael J. Wysocki 			return error;
337a33ec399SRafael J. Wysocki 		}
33846394fd0SRafael J. Wysocki 		if (!adev->handler) {
339c27b2c33SRafael J. Wysocki 			dev_warn(&adev->dev, "Enumeration failure\n");
340443fc820SRafael J. Wysocki 			error = -ENODEV;
341443fc820SRafael J. Wysocki 		}
342443fc820SRafael J. Wysocki 	} else {
343443fc820SRafael J. Wysocki 		error = acpi_scan_device_not_present(adev);
344443fc820SRafael J. Wysocki 	}
345443fc820SRafael J. Wysocki 	return error;
346443fc820SRafael J. Wysocki }
347443fc820SRafael J. Wysocki 
348443fc820SRafael J. Wysocki static int acpi_scan_bus_check(struct acpi_device *adev)
349443fc820SRafael J. Wysocki {
350443fc820SRafael J. Wysocki 	struct acpi_scan_handler *handler = adev->handler;
351443fc820SRafael J. Wysocki 	struct acpi_device *child;
352443fc820SRafael J. Wysocki 	int error;
353443fc820SRafael J. Wysocki 
354443fc820SRafael J. Wysocki 	acpi_bus_get_status(adev);
355443fc820SRafael J. Wysocki 	if (!(adev->status.present || adev->status.functional)) {
356443fc820SRafael J. Wysocki 		acpi_scan_device_not_present(adev);
357443fc820SRafael J. Wysocki 		return 0;
358443fc820SRafael J. Wysocki 	}
359443fc820SRafael J. Wysocki 	if (handler && handler->hotplug.scan_dependent)
360443fc820SRafael J. Wysocki 		return handler->hotplug.scan_dependent(adev);
361443fc820SRafael J. Wysocki 
362443fc820SRafael J. Wysocki 	error = acpi_bus_scan(adev->handle);
363443fc820SRafael J. Wysocki 	if (error) {
364443fc820SRafael J. Wysocki 		dev_warn(&adev->dev, "Namespace scan failure\n");
365443fc820SRafael J. Wysocki 		return error;
366443fc820SRafael J. Wysocki 	}
367443fc820SRafael J. Wysocki 	list_for_each_entry(child, &adev->children, node) {
368443fc820SRafael J. Wysocki 		error = acpi_scan_bus_check(child);
369443fc820SRafael J. Wysocki 		if (error)
370443fc820SRafael J. Wysocki 			return error;
371a33ec399SRafael J. Wysocki 	}
372c27b2c33SRafael J. Wysocki 	return 0;
373c27b2c33SRafael J. Wysocki }
374c27b2c33SRafael J. Wysocki 
3753c2cc7ffSRafael J. Wysocki static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
3763c2cc7ffSRafael J. Wysocki {
3773c2cc7ffSRafael J. Wysocki 	switch (type) {
3783c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_BUS_CHECK:
3793c2cc7ffSRafael J. Wysocki 		return acpi_scan_bus_check(adev);
3803c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_DEVICE_CHECK:
3813c2cc7ffSRafael J. Wysocki 		return acpi_scan_device_check(adev);
3823c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_EJECT_REQUEST:
3833c2cc7ffSRafael J. Wysocki 	case ACPI_OST_EC_OSPM_EJECT:
384dd2151beSRafael J. Wysocki 		if (adev->handler && !adev->handler->hotplug.enabled) {
385dd2151beSRafael J. Wysocki 			dev_info(&adev->dev, "Eject disabled\n");
386dd2151beSRafael J. Wysocki 			return -EPERM;
387dd2151beSRafael J. Wysocki 		}
388700b8422SRafael J. Wysocki 		acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
389dd2151beSRafael J. Wysocki 				  ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
3903c2cc7ffSRafael J. Wysocki 		return acpi_scan_hot_remove(adev);
3913c2cc7ffSRafael J. Wysocki 	}
3923c2cc7ffSRafael J. Wysocki 	return -EINVAL;
3933c2cc7ffSRafael J. Wysocki }
3943c2cc7ffSRafael J. Wysocki 
3951e3bcb59SRafael J. Wysocki void acpi_device_hotplug(struct acpi_device *adev, u32 src)
396c27b2c33SRafael J. Wysocki {
397c27b2c33SRafael J. Wysocki 	u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
3983c2cc7ffSRafael J. Wysocki 	int error = -ENODEV;
399c27b2c33SRafael J. Wysocki 
400c27b2c33SRafael J. Wysocki 	lock_device_hotplug();
401c27b2c33SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
402c27b2c33SRafael J. Wysocki 
403c27b2c33SRafael J. Wysocki 	/*
404c27b2c33SRafael J. Wysocki 	 * The device object's ACPI handle cannot become invalid as long as we
4053c2cc7ffSRafael J. Wysocki 	 * are holding acpi_scan_lock, but it might have become invalid before
406c27b2c33SRafael J. Wysocki 	 * that lock was acquired.
407c27b2c33SRafael J. Wysocki 	 */
408c27b2c33SRafael J. Wysocki 	if (adev->handle == INVALID_ACPI_HANDLE)
4093c2cc7ffSRafael J. Wysocki 		goto err_out;
410c27b2c33SRafael J. Wysocki 
4111e2380cdSRafael J. Wysocki 	if (adev->flags.is_dock_station) {
4121e2380cdSRafael J. Wysocki 		error = dock_notify(adev, src);
4131e2380cdSRafael J. Wysocki 	} else if (adev->flags.hotplug_notify) {
4143c2cc7ffSRafael J. Wysocki 		error = acpi_generic_hotplug_event(adev, src);
415dd2151beSRafael J. Wysocki 		if (error == -EPERM) {
416dd2151beSRafael J. Wysocki 			ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
417dd2151beSRafael J. Wysocki 			goto err_out;
418dd2151beSRafael J. Wysocki 		}
4193c2cc7ffSRafael J. Wysocki 	} else {
420be27b3dcSRafael J. Wysocki 		int (*notify)(struct acpi_device *, u32);
4213c2cc7ffSRafael J. Wysocki 
4223c2cc7ffSRafael J. Wysocki 		acpi_lock_hp_context();
423be27b3dcSRafael J. Wysocki 		notify = adev->hp ? adev->hp->notify : NULL;
4243c2cc7ffSRafael J. Wysocki 		acpi_unlock_hp_context();
4253c2cc7ffSRafael J. Wysocki 		/*
4263c2cc7ffSRafael J. Wysocki 		 * There may be additional notify handlers for device objects
4273c2cc7ffSRafael J. Wysocki 		 * without the .event() callback, so ignore them here.
4283c2cc7ffSRafael J. Wysocki 		 */
429be27b3dcSRafael J. Wysocki 		if (notify)
430be27b3dcSRafael J. Wysocki 			error = notify(adev, src);
4313c2cc7ffSRafael J. Wysocki 		else
4323c2cc7ffSRafael J. Wysocki 			goto out;
433c27b2c33SRafael J. Wysocki 	}
434c27b2c33SRafael J. Wysocki 	if (!error)
435a33ec399SRafael J. Wysocki 		ost_code = ACPI_OST_SC_SUCCESS;
436a33ec399SRafael J. Wysocki 
4373c2cc7ffSRafael J. Wysocki  err_out:
438700b8422SRafael J. Wysocki 	acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
4393c2cc7ffSRafael J. Wysocki 
4403c2cc7ffSRafael J. Wysocki  out:
44178ea4639SRafael J. Wysocki 	acpi_bus_put_acpi_device(adev);
442a33ec399SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
443e0ae8feeSRafael J. Wysocki 	unlock_device_hotplug();
444a33ec399SRafael J. Wysocki }
445a33ec399SRafael J. Wysocki 
4460b224527SRafael J. Wysocki static void acpi_free_power_resources_lists(struct acpi_device *device)
4470b224527SRafael J. Wysocki {
4480b224527SRafael J. Wysocki 	int i;
4490b224527SRafael J. Wysocki 
450993cbe59SRafael J. Wysocki 	if (device->wakeup.flags.valid)
451993cbe59SRafael J. Wysocki 		acpi_power_resources_list_free(&device->wakeup.resources);
452993cbe59SRafael J. Wysocki 
4531b1f3e16SRafael J. Wysocki 	if (!device->power.flags.power_resources)
4540b224527SRafael J. Wysocki 		return;
4550b224527SRafael J. Wysocki 
4560b224527SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
4570b224527SRafael J. Wysocki 		struct acpi_device_power_state *ps = &device->power.states[i];
4580b224527SRafael J. Wysocki 		acpi_power_resources_list_free(&ps->resources);
4590b224527SRafael J. Wysocki 	}
4607f47fa6cSBjorn Helgaas }
4617f47fa6cSBjorn Helgaas 
4621890a97aSPatrick Mochel static void acpi_device_release(struct device *dev)
4631890a97aSPatrick Mochel {
4641890a97aSPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
4651890a97aSPatrick Mochel 
466ffdcd955SMika Westerberg 	acpi_free_properties(acpi_dev);
467c0af4175SToshi Kani 	acpi_free_pnp_ids(&acpi_dev->pnp);
4680b224527SRafael J. Wysocki 	acpi_free_power_resources_lists(acpi_dev);
4691890a97aSPatrick Mochel 	kfree(acpi_dev);
4701890a97aSPatrick Mochel }
4711890a97aSPatrick Mochel 
472d783156eSRafael J. Wysocki static void acpi_device_del(struct acpi_device *device)
473caf5c03fSRafael J. Wysocki {
474d783156eSRafael J. Wysocki 	mutex_lock(&acpi_device_lock);
475d783156eSRafael J. Wysocki 	if (device->parent)
476d783156eSRafael J. Wysocki 		list_del(&device->node);
477d783156eSRafael J. Wysocki 
478d783156eSRafael J. Wysocki 	list_del(&device->wakeup_list);
479d783156eSRafael J. Wysocki 	mutex_unlock(&acpi_device_lock);
480d783156eSRafael J. Wysocki 
481d783156eSRafael J. Wysocki 	acpi_power_add_remove_device(device, false);
482d783156eSRafael J. Wysocki 	acpi_device_remove_files(device);
483d783156eSRafael J. Wysocki 	if (device->remove)
484d783156eSRafael J. Wysocki 		device->remove(device);
485d783156eSRafael J. Wysocki 
486d783156eSRafael J. Wysocki 	device_del(&device->dev);
487d783156eSRafael J. Wysocki }
488d783156eSRafael J. Wysocki 
489d783156eSRafael J. Wysocki static LIST_HEAD(acpi_device_del_list);
490d783156eSRafael J. Wysocki static DEFINE_MUTEX(acpi_device_del_lock);
491d783156eSRafael J. Wysocki 
492d783156eSRafael J. Wysocki static void acpi_device_del_work_fn(struct work_struct *work_not_used)
493d783156eSRafael J. Wysocki {
494d783156eSRafael J. Wysocki 	for (;;) {
495d783156eSRafael J. Wysocki 		struct acpi_device *adev;
496d783156eSRafael J. Wysocki 
497d783156eSRafael J. Wysocki 		mutex_lock(&acpi_device_del_lock);
498d783156eSRafael J. Wysocki 
499d783156eSRafael J. Wysocki 		if (list_empty(&acpi_device_del_list)) {
500d783156eSRafael J. Wysocki 			mutex_unlock(&acpi_device_del_lock);
501d783156eSRafael J. Wysocki 			break;
502d783156eSRafael J. Wysocki 		}
503d783156eSRafael J. Wysocki 		adev = list_first_entry(&acpi_device_del_list,
504d783156eSRafael J. Wysocki 					struct acpi_device, del_list);
505d783156eSRafael J. Wysocki 		list_del(&adev->del_list);
506d783156eSRafael J. Wysocki 
507d783156eSRafael J. Wysocki 		mutex_unlock(&acpi_device_del_lock);
508d783156eSRafael J. Wysocki 
509d783156eSRafael J. Wysocki 		acpi_device_del(adev);
510d783156eSRafael J. Wysocki 		/*
511d783156eSRafael J. Wysocki 		 * Drop references to all power resources that might have been
512d783156eSRafael J. Wysocki 		 * used by the device.
513d783156eSRafael J. Wysocki 		 */
514d783156eSRafael J. Wysocki 		acpi_power_transition(adev, ACPI_STATE_D3_COLD);
515d783156eSRafael J. Wysocki 		put_device(&adev->dev);
516d783156eSRafael J. Wysocki 	}
517d783156eSRafael J. Wysocki }
518d783156eSRafael J. Wysocki 
519d783156eSRafael J. Wysocki /**
520d783156eSRafael J. Wysocki  * acpi_scan_drop_device - Drop an ACPI device object.
521d783156eSRafael J. Wysocki  * @handle: Handle of an ACPI namespace node, not used.
522d783156eSRafael J. Wysocki  * @context: Address of the ACPI device object to drop.
523d783156eSRafael J. Wysocki  *
524d783156eSRafael J. Wysocki  * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
525d783156eSRafael J. Wysocki  * namespace node the device object pointed to by @context is attached to.
526d783156eSRafael J. Wysocki  *
527d783156eSRafael J. Wysocki  * The unregistration is carried out asynchronously to avoid running
528d783156eSRafael J. Wysocki  * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
529d783156eSRafael J. Wysocki  * ensure the correct ordering (the device objects must be unregistered in the
530d783156eSRafael J. Wysocki  * same order in which the corresponding namespace nodes are deleted).
531d783156eSRafael J. Wysocki  */
532d783156eSRafael J. Wysocki static void acpi_scan_drop_device(acpi_handle handle, void *context)
533d783156eSRafael J. Wysocki {
534d783156eSRafael J. Wysocki 	static DECLARE_WORK(work, acpi_device_del_work_fn);
535d783156eSRafael J. Wysocki 	struct acpi_device *adev = context;
536d783156eSRafael J. Wysocki 
537d783156eSRafael J. Wysocki 	mutex_lock(&acpi_device_del_lock);
538d783156eSRafael J. Wysocki 
539d783156eSRafael J. Wysocki 	/*
540d783156eSRafael J. Wysocki 	 * Use the ACPI hotplug workqueue which is ordered, so this work item
541d783156eSRafael J. Wysocki 	 * won't run after any hotplug work items submitted subsequently.  That
542d783156eSRafael J. Wysocki 	 * prevents attempts to register device objects identical to those being
543d783156eSRafael J. Wysocki 	 * deleted from happening concurrently (such attempts result from
544d783156eSRafael J. Wysocki 	 * hotplug events handled via the ACPI hotplug workqueue).  It also will
545d783156eSRafael J. Wysocki 	 * run after all of the work items submitted previosuly, which helps
546d783156eSRafael J. Wysocki 	 * those work items to ensure that they are not accessing stale device
547d783156eSRafael J. Wysocki 	 * objects.
548d783156eSRafael J. Wysocki 	 */
549d783156eSRafael J. Wysocki 	if (list_empty(&acpi_device_del_list))
550d783156eSRafael J. Wysocki 		acpi_queue_hotplug_work(&work);
551d783156eSRafael J. Wysocki 
552d783156eSRafael J. Wysocki 	list_add_tail(&adev->del_list, &acpi_device_del_list);
553d783156eSRafael J. Wysocki 	/* Make acpi_ns_validate_handle() return NULL for this handle. */
554d783156eSRafael J. Wysocki 	adev->handle = INVALID_ACPI_HANDLE;
555d783156eSRafael J. Wysocki 
556d783156eSRafael J. Wysocki 	mutex_unlock(&acpi_device_del_lock);
557caf5c03fSRafael J. Wysocki }
558caf5c03fSRafael J. Wysocki 
55978ea4639SRafael J. Wysocki static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device,
56078ea4639SRafael J. Wysocki 				void (*callback)(void *))
561caf5c03fSRafael J. Wysocki {
562caf5c03fSRafael J. Wysocki 	acpi_status status;
563caf5c03fSRafael J. Wysocki 
564caf5c03fSRafael J. Wysocki 	if (!device)
565caf5c03fSRafael J. Wysocki 		return -EINVAL;
566caf5c03fSRafael J. Wysocki 
56778ea4639SRafael J. Wysocki 	status = acpi_get_data_full(handle, acpi_scan_drop_device,
56878ea4639SRafael J. Wysocki 				    (void **)device, callback);
569caf5c03fSRafael J. Wysocki 	if (ACPI_FAILURE(status) || !*device) {
570caf5c03fSRafael J. Wysocki 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
571caf5c03fSRafael J. Wysocki 				  handle));
572caf5c03fSRafael J. Wysocki 		return -ENODEV;
573caf5c03fSRafael J. Wysocki 	}
574caf5c03fSRafael J. Wysocki 	return 0;
575caf5c03fSRafael J. Wysocki }
57678ea4639SRafael J. Wysocki 
57778ea4639SRafael J. Wysocki int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
57878ea4639SRafael J. Wysocki {
57978ea4639SRafael J. Wysocki 	return acpi_get_device_data(handle, device, NULL);
58078ea4639SRafael J. Wysocki }
5816585925bSRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_get_device);
582caf5c03fSRafael J. Wysocki 
58378ea4639SRafael J. Wysocki static void get_acpi_device(void *dev)
58478ea4639SRafael J. Wysocki {
58578ea4639SRafael J. Wysocki 	if (dev)
58678ea4639SRafael J. Wysocki 		get_device(&((struct acpi_device *)dev)->dev);
58778ea4639SRafael J. Wysocki }
58878ea4639SRafael J. Wysocki 
58978ea4639SRafael J. Wysocki struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
59078ea4639SRafael J. Wysocki {
59178ea4639SRafael J. Wysocki 	struct acpi_device *adev = NULL;
59278ea4639SRafael J. Wysocki 
59378ea4639SRafael J. Wysocki 	acpi_get_device_data(handle, &adev, get_acpi_device);
59478ea4639SRafael J. Wysocki 	return adev;
59578ea4639SRafael J. Wysocki }
59678ea4639SRafael J. Wysocki 
59778ea4639SRafael J. Wysocki void acpi_bus_put_acpi_device(struct acpi_device *adev)
59878ea4639SRafael J. Wysocki {
59978ea4639SRafael J. Wysocki 	put_device(&adev->dev);
60078ea4639SRafael J. Wysocki }
60178ea4639SRafael J. Wysocki 
602cf860be6SRafael J. Wysocki int acpi_device_add(struct acpi_device *device,
60382c7d5efSRafael J. Wysocki 		    void (*release)(struct device *))
6041da177e4SLinus Torvalds {
605e49bd2ddSZhang Rui 	int result;
606e49bd2ddSZhang Rui 	struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
607e49bd2ddSZhang Rui 	int found = 0;
60866b7ed40SBjorn Helgaas 
609d43e167dSRafael J. Wysocki 	if (device->handle) {
610d43e167dSRafael J. Wysocki 		acpi_status status;
611d43e167dSRafael J. Wysocki 
612d783156eSRafael J. Wysocki 		status = acpi_attach_data(device->handle, acpi_scan_drop_device,
613d43e167dSRafael J. Wysocki 					  device);
614d43e167dSRafael J. Wysocki 		if (ACPI_FAILURE(status)) {
615d43e167dSRafael J. Wysocki 			acpi_handle_err(device->handle,
616d43e167dSRafael J. Wysocki 					"Unable to attach device data\n");
617d43e167dSRafael J. Wysocki 			return -ENODEV;
618d43e167dSRafael J. Wysocki 		}
619d43e167dSRafael J. Wysocki 	}
620d43e167dSRafael J. Wysocki 
6211da177e4SLinus Torvalds 	/*
6221da177e4SLinus Torvalds 	 * Linkage
6231da177e4SLinus Torvalds 	 * -------
6241da177e4SLinus Torvalds 	 * Link this device to its parent and siblings.
6251da177e4SLinus Torvalds 	 */
6261da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->children);
6271da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->node);
6281da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->wakeup_list);
6291033f904SLan Tianyu 	INIT_LIST_HEAD(&device->physical_node_list);
630d783156eSRafael J. Wysocki 	INIT_LIST_HEAD(&device->del_list);
6311033f904SLan Tianyu 	mutex_init(&device->physical_node_lock);
6321da177e4SLinus Torvalds 
633e49bd2ddSZhang Rui 	new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
634e49bd2ddSZhang Rui 	if (!new_bus_id) {
635d43e167dSRafael J. Wysocki 		pr_err(PREFIX "Memory allocation error\n");
636d43e167dSRafael J. Wysocki 		result = -ENOMEM;
637d43e167dSRafael J. Wysocki 		goto err_detach;
638e49bd2ddSZhang Rui 	}
639e49bd2ddSZhang Rui 
6409090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
641e49bd2ddSZhang Rui 	/*
642e49bd2ddSZhang Rui 	 * Find suitable bus_id and instance number in acpi_bus_id_list
643e49bd2ddSZhang Rui 	 * If failed, create one and link it into acpi_bus_id_list
644e49bd2ddSZhang Rui 	 */
645e49bd2ddSZhang Rui 	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
6461131b938SBjorn Helgaas 		if (!strcmp(acpi_device_bus_id->bus_id,
6471131b938SBjorn Helgaas 			    acpi_device_hid(device))) {
648e49bd2ddSZhang Rui 			acpi_device_bus_id->instance_no++;
649e49bd2ddSZhang Rui 			found = 1;
650e49bd2ddSZhang Rui 			kfree(new_bus_id);
651e49bd2ddSZhang Rui 			break;
652e49bd2ddSZhang Rui 		}
653e49bd2ddSZhang Rui 	}
654e49bd2ddSZhang Rui 	if (!found) {
655e49bd2ddSZhang Rui 		acpi_device_bus_id = new_bus_id;
6561131b938SBjorn Helgaas 		strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
657e49bd2ddSZhang Rui 		acpi_device_bus_id->instance_no = 0;
658e49bd2ddSZhang Rui 		list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
659e49bd2ddSZhang Rui 	}
6600794469dSKay Sievers 	dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
661e49bd2ddSZhang Rui 
66233b57150SLen Brown 	if (device->parent)
6631da177e4SLinus Torvalds 		list_add_tail(&device->node, &device->parent->children);
66433b57150SLen Brown 
6651da177e4SLinus Torvalds 	if (device->wakeup.flags.valid)
6661da177e4SLinus Torvalds 		list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
6679090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
6681da177e4SLinus Torvalds 
6691890a97aSPatrick Mochel 	if (device->parent)
67066b7ed40SBjorn Helgaas 		device->dev.parent = &device->parent->dev;
6711890a97aSPatrick Mochel 	device->dev.bus = &acpi_bus_type;
67282c7d5efSRafael J. Wysocki 	device->dev.release = release;
673cf860be6SRafael J. Wysocki 	result = device_add(&device->dev);
674e49bd2ddSZhang Rui 	if (result) {
6758b12b922SAlex Chiang 		dev_err(&device->dev, "Error registering device\n");
676d43e167dSRafael J. Wysocki 		goto err;
677e49bd2ddSZhang Rui 	}
678f883d9dbSPatrick Mochel 
679e49bd2ddSZhang Rui 	result = acpi_device_setup_files(device);
680e49bd2ddSZhang Rui 	if (result)
6810794469dSKay Sievers 		printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
6820794469dSKay Sievers 		       dev_name(&device->dev));
683e49bd2ddSZhang Rui 
684e49bd2ddSZhang Rui 	return 0;
685d43e167dSRafael J. Wysocki 
686d43e167dSRafael J. Wysocki  err:
6879090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
68833b57150SLen Brown 	if (device->parent)
689e49bd2ddSZhang Rui 		list_del(&device->node);
690e49bd2ddSZhang Rui 	list_del(&device->wakeup_list);
6919090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
692d43e167dSRafael J. Wysocki 
693d43e167dSRafael J. Wysocki  err_detach:
694d783156eSRafael J. Wysocki 	acpi_detach_data(device->handle, acpi_scan_drop_device);
695e49bd2ddSZhang Rui 	return result;
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds 
6989e89dde2SZhang Rui /* --------------------------------------------------------------------------
6991da177e4SLinus Torvalds                                  Device Enumeration
7001da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
7015c478f49SBjorn Helgaas static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
7025c478f49SBjorn Helgaas {
703456de893SRafael J. Wysocki 	struct acpi_device *device = NULL;
7045c478f49SBjorn Helgaas 	acpi_status status;
7055c478f49SBjorn Helgaas 
7065c478f49SBjorn Helgaas 	/*
7075c478f49SBjorn Helgaas 	 * Fixed hardware devices do not appear in the namespace and do not
7085c478f49SBjorn Helgaas 	 * have handles, but we fabricate acpi_devices for them, so we have
7095c478f49SBjorn Helgaas 	 * to deal with them specially.
7105c478f49SBjorn Helgaas 	 */
711456de893SRafael J. Wysocki 	if (!handle)
7125c478f49SBjorn Helgaas 		return acpi_root;
7135c478f49SBjorn Helgaas 
7145c478f49SBjorn Helgaas 	do {
7155c478f49SBjorn Helgaas 		status = acpi_get_parent(handle, &handle);
7165c478f49SBjorn Helgaas 		if (ACPI_FAILURE(status))
717456de893SRafael J. Wysocki 			return status == AE_NULL_ENTRY ? NULL : acpi_root;
718456de893SRafael J. Wysocki 	} while (acpi_bus_get_device(handle, &device));
7195c478f49SBjorn Helgaas 	return device;
7205c478f49SBjorn Helgaas }
7215c478f49SBjorn Helgaas 
722c8f7a62cSLen Brown acpi_status
723c8f7a62cSLen Brown acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
724c8f7a62cSLen Brown {
725c8f7a62cSLen Brown 	acpi_status status;
726c8f7a62cSLen Brown 	acpi_handle tmp;
727c8f7a62cSLen Brown 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
728c8f7a62cSLen Brown 	union acpi_object *obj;
729c8f7a62cSLen Brown 
730c8f7a62cSLen Brown 	status = acpi_get_handle(handle, "_EJD", &tmp);
731c8f7a62cSLen Brown 	if (ACPI_FAILURE(status))
732c8f7a62cSLen Brown 		return status;
733c8f7a62cSLen Brown 
734c8f7a62cSLen Brown 	status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
735c8f7a62cSLen Brown 	if (ACPI_SUCCESS(status)) {
736c8f7a62cSLen Brown 		obj = buffer.pointer;
7373b5fee59SHolger Macht 		status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
7383b5fee59SHolger Macht 					 ejd);
739c8f7a62cSLen Brown 		kfree(buffer.pointer);
740c8f7a62cSLen Brown 	}
741c8f7a62cSLen Brown 	return status;
742c8f7a62cSLen Brown }
743c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
744c8f7a62cSLen Brown 
745e88c9c60SRafael J. Wysocki static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
746b581a7f9SRafael J. Wysocki 					struct acpi_device_wakeup *wakeup)
7471da177e4SLinus Torvalds {
748b581a7f9SRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
749b581a7f9SRafael J. Wysocki 	union acpi_object *package = NULL;
7501da177e4SLinus Torvalds 	union acpi_object *element = NULL;
751b581a7f9SRafael J. Wysocki 	acpi_status status;
752e88c9c60SRafael J. Wysocki 	int err = -ENODATA;
7531da177e4SLinus Torvalds 
754b581a7f9SRafael J. Wysocki 	if (!wakeup)
755e88c9c60SRafael J. Wysocki 		return -EINVAL;
7561da177e4SLinus Torvalds 
757993cbe59SRafael J. Wysocki 	INIT_LIST_HEAD(&wakeup->resources);
7581da177e4SLinus Torvalds 
759b581a7f9SRafael J. Wysocki 	/* _PRW */
760b581a7f9SRafael J. Wysocki 	status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
761b581a7f9SRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
762b581a7f9SRafael J. Wysocki 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
763e88c9c60SRafael J. Wysocki 		return err;
764b581a7f9SRafael J. Wysocki 	}
765b581a7f9SRafael J. Wysocki 
766b581a7f9SRafael J. Wysocki 	package = (union acpi_object *)buffer.pointer;
767b581a7f9SRafael J. Wysocki 
768e88c9c60SRafael J. Wysocki 	if (!package || package->package.count < 2)
769b581a7f9SRafael J. Wysocki 		goto out;
770b581a7f9SRafael J. Wysocki 
7711da177e4SLinus Torvalds 	element = &(package->package.elements[0]);
772e88c9c60SRafael J. Wysocki 	if (!element)
773b581a7f9SRafael J. Wysocki 		goto out;
774e88c9c60SRafael J. Wysocki 
7751da177e4SLinus Torvalds 	if (element->type == ACPI_TYPE_PACKAGE) {
7761da177e4SLinus Torvalds 		if ((element->package.count < 2) ||
7771da177e4SLinus Torvalds 		    (element->package.elements[0].type !=
7781da177e4SLinus Torvalds 		     ACPI_TYPE_LOCAL_REFERENCE)
779e88c9c60SRafael J. Wysocki 		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
780b581a7f9SRafael J. Wysocki 			goto out;
781e88c9c60SRafael J. Wysocki 
782b581a7f9SRafael J. Wysocki 		wakeup->gpe_device =
7831da177e4SLinus Torvalds 		    element->package.elements[0].reference.handle;
784b581a7f9SRafael J. Wysocki 		wakeup->gpe_number =
7851da177e4SLinus Torvalds 		    (u32) element->package.elements[1].integer.value;
7861da177e4SLinus Torvalds 	} else if (element->type == ACPI_TYPE_INTEGER) {
787b581a7f9SRafael J. Wysocki 		wakeup->gpe_device = NULL;
788b581a7f9SRafael J. Wysocki 		wakeup->gpe_number = element->integer.value;
789b581a7f9SRafael J. Wysocki 	} else {
790b581a7f9SRafael J. Wysocki 		goto out;
791b581a7f9SRafael J. Wysocki 	}
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 	element = &(package->package.elements[1]);
794e88c9c60SRafael J. Wysocki 	if (element->type != ACPI_TYPE_INTEGER)
795b581a7f9SRafael J. Wysocki 		goto out;
796e88c9c60SRafael J. Wysocki 
797b581a7f9SRafael J. Wysocki 	wakeup->sleep_state = element->integer.value;
7981da177e4SLinus Torvalds 
799e88c9c60SRafael J. Wysocki 	err = acpi_extract_power_resources(package, 2, &wakeup->resources);
800e88c9c60SRafael J. Wysocki 	if (err)
801b581a7f9SRafael J. Wysocki 		goto out;
8021da177e4SLinus Torvalds 
8030596a52bSRafael J. Wysocki 	if (!list_empty(&wakeup->resources)) {
8040596a52bSRafael J. Wysocki 		int sleep_state;
8059874647bSRafael J. Wysocki 
806b5d667ebSRafael J. Wysocki 		err = acpi_power_wakeup_list_init(&wakeup->resources,
807b5d667ebSRafael J. Wysocki 						  &sleep_state);
808b5d667ebSRafael J. Wysocki 		if (err) {
809b5d667ebSRafael J. Wysocki 			acpi_handle_warn(handle, "Retrieving current states "
810b5d667ebSRafael J. Wysocki 					 "of wakeup power resources failed\n");
811b5d667ebSRafael J. Wysocki 			acpi_power_resources_list_free(&wakeup->resources);
812b5d667ebSRafael J. Wysocki 			goto out;
813b5d667ebSRafael J. Wysocki 		}
8140596a52bSRafael J. Wysocki 		if (sleep_state < wakeup->sleep_state) {
8150596a52bSRafael J. Wysocki 			acpi_handle_warn(handle, "Overriding _PRW sleep state "
8160596a52bSRafael J. Wysocki 					 "(S%d) by S%d from power resources\n",
8170596a52bSRafael J. Wysocki 					 (int)wakeup->sleep_state, sleep_state);
8180596a52bSRafael J. Wysocki 			wakeup->sleep_state = sleep_state;
8190596a52bSRafael J. Wysocki 		}
8200596a52bSRafael J. Wysocki 	}
821b581a7f9SRafael J. Wysocki 
822b581a7f9SRafael J. Wysocki  out:
823b581a7f9SRafael J. Wysocki 	kfree(buffer.pointer);
824e88c9c60SRafael J. Wysocki 	return err;
8251da177e4SLinus Torvalds }
8261da177e4SLinus Torvalds 
827bd9b2f9aSRafael J. Wysocki static void acpi_wakeup_gpe_init(struct acpi_device *device)
8281da177e4SLinus Torvalds {
8290519ade7SMathias Krause 	static const struct acpi_device_id button_device_ids[] = {
83029b71a1cSThomas Renninger 		{"PNP0C0C", 0},
831b7e38304SZhang Rui 		{"PNP0C0D", 0},
83229b71a1cSThomas Renninger 		{"PNP0C0E", 0},
83329b71a1cSThomas Renninger 		{"", 0},
83429b71a1cSThomas Renninger 	};
835bd9b2f9aSRafael J. Wysocki 	struct acpi_device_wakeup *wakeup = &device->wakeup;
836f517709dSRafael J. Wysocki 	acpi_status status;
837f517709dSRafael J. Wysocki 	acpi_event_status event_status;
838f517709dSRafael J. Wysocki 
839bd9b2f9aSRafael J. Wysocki 	wakeup->flags.notifier_present = 0;
840f517709dSRafael J. Wysocki 
841f517709dSRafael J. Wysocki 	/* Power button, Lid switch always enable wakeup */
842f517709dSRafael J. Wysocki 	if (!acpi_match_device_ids(device, button_device_ids)) {
843bd9b2f9aSRafael J. Wysocki 		wakeup->flags.run_wake = 1;
844b7e38304SZhang Rui 		if (!acpi_match_device_ids(device, &button_device_ids[1])) {
845b7e38304SZhang Rui 			/* Do not use Lid/sleep button for S5 wakeup */
846bd9b2f9aSRafael J. Wysocki 			if (wakeup->sleep_state == ACPI_STATE_S5)
847bd9b2f9aSRafael J. Wysocki 				wakeup->sleep_state = ACPI_STATE_S4;
848b7e38304SZhang Rui 		}
849bd9b2f9aSRafael J. Wysocki 		acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
850f2b56bc8SRafael J. Wysocki 		device_set_wakeup_capable(&device->dev, true);
851f517709dSRafael J. Wysocki 		return;
852f517709dSRafael J. Wysocki 	}
853f517709dSRafael J. Wysocki 
854bd9b2f9aSRafael J. Wysocki 	acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
855bd9b2f9aSRafael J. Wysocki 				wakeup->gpe_number);
856bd9b2f9aSRafael J. Wysocki 	status = acpi_get_gpe_status(wakeup->gpe_device, wakeup->gpe_number,
8570f849d2cSLin Ming 				     &event_status);
858bd9b2f9aSRafael J. Wysocki 	if (ACPI_FAILURE(status))
859bd9b2f9aSRafael J. Wysocki 		return;
860bd9b2f9aSRafael J. Wysocki 
8612f857234SLv Zheng 	wakeup->flags.run_wake = !!(event_status & ACPI_EVENT_FLAG_HAS_HANDLER);
862f517709dSRafael J. Wysocki }
863f517709dSRafael J. Wysocki 
864d57d09a4SRafael J. Wysocki static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
865f517709dSRafael J. Wysocki {
866e88c9c60SRafael J. Wysocki 	int err;
86729b71a1cSThomas Renninger 
868d57d09a4SRafael J. Wysocki 	/* Presence of _PRW indicates wake capable */
869952c63e9SJiang Liu 	if (!acpi_has_method(device->handle, "_PRW"))
870d57d09a4SRafael J. Wysocki 		return;
871d57d09a4SRafael J. Wysocki 
872e88c9c60SRafael J. Wysocki 	err = acpi_bus_extract_wakeup_device_power_package(device->handle,
873b581a7f9SRafael J. Wysocki 							   &device->wakeup);
874e88c9c60SRafael J. Wysocki 	if (err) {
875e88c9c60SRafael J. Wysocki 		dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
876d57d09a4SRafael J. Wysocki 		return;
8771da177e4SLinus Torvalds 	}
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	device->wakeup.flags.valid = 1;
8809b83ccd2SRafael J. Wysocki 	device->wakeup.prepare_count = 0;
881bd9b2f9aSRafael J. Wysocki 	acpi_wakeup_gpe_init(device);
882729b2bdbSZhao Yakui 	/* Call _PSW/_DSW object to disable its ability to wake the sleeping
883729b2bdbSZhao Yakui 	 * system for the ACPI device with the _PRW object.
884729b2bdbSZhao Yakui 	 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
885729b2bdbSZhao Yakui 	 * So it is necessary to call _DSW object first. Only when it is not
886729b2bdbSZhao Yakui 	 * present will the _PSW object used.
887729b2bdbSZhao Yakui 	 */
888e88c9c60SRafael J. Wysocki 	err = acpi_device_sleep_wake(device, 0, 0, 0);
889e88c9c60SRafael J. Wysocki 	if (err)
89077e76609SRafael J. Wysocki 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
89177e76609SRafael J. Wysocki 				"error in _DSW or _PSW evaluation\n"));
8921da177e4SLinus Torvalds }
8931da177e4SLinus Torvalds 
894f33ce568SRafael J. Wysocki static void acpi_bus_init_power_state(struct acpi_device *device, int state)
8951da177e4SLinus Torvalds {
896f33ce568SRafael J. Wysocki 	struct acpi_device_power_state *ps = &device->power.states[state];
897ef85bdbeSRafael J. Wysocki 	char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
898ef85bdbeSRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
899f33ce568SRafael J. Wysocki 	acpi_status status;
9009e89dde2SZhang Rui 
901f33ce568SRafael J. Wysocki 	INIT_LIST_HEAD(&ps->resources);
902f33ce568SRafael J. Wysocki 
903ef85bdbeSRafael J. Wysocki 	/* Evaluate "_PRx" to get referenced power resources */
904ef85bdbeSRafael J. Wysocki 	status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
905ef85bdbeSRafael J. Wysocki 	if (ACPI_SUCCESS(status)) {
906ef85bdbeSRafael J. Wysocki 		union acpi_object *package = buffer.pointer;
907f33ce568SRafael J. Wysocki 
908ef85bdbeSRafael J. Wysocki 		if (buffer.length && package
909ef85bdbeSRafael J. Wysocki 		    && package->type == ACPI_TYPE_PACKAGE
910ef85bdbeSRafael J. Wysocki 		    && package->package.count) {
911e88c9c60SRafael J. Wysocki 			int err = acpi_extract_power_resources(package, 0,
912ef85bdbeSRafael J. Wysocki 							       &ps->resources);
913e88c9c60SRafael J. Wysocki 			if (!err)
914f33ce568SRafael J. Wysocki 				device->power.flags.power_resources = 1;
915f33ce568SRafael J. Wysocki 		}
916ef85bdbeSRafael J. Wysocki 		ACPI_FREE(buffer.pointer);
917f33ce568SRafael J. Wysocki 	}
918f33ce568SRafael J. Wysocki 
919f33ce568SRafael J. Wysocki 	/* Evaluate "_PSx" to see if we can do explicit sets */
920ef85bdbeSRafael J. Wysocki 	pathname[2] = 'S';
921952c63e9SJiang Liu 	if (acpi_has_method(device->handle, pathname))
922f33ce568SRafael J. Wysocki 		ps->flags.explicit_set = 1;
923f33ce568SRafael J. Wysocki 
92420dacb71SRafael J. Wysocki 	/* State is valid if there are means to put the device into it. */
92520dacb71SRafael J. Wysocki 	if (!list_empty(&ps->resources) || ps->flags.explicit_set)
926f33ce568SRafael J. Wysocki 		ps->flags.valid = 1;
927f33ce568SRafael J. Wysocki 
928f33ce568SRafael J. Wysocki 	ps->power = -1;		/* Unknown - driver assigned */
929f33ce568SRafael J. Wysocki 	ps->latency = -1;	/* Unknown - driver assigned */
930f33ce568SRafael J. Wysocki }
931f33ce568SRafael J. Wysocki 
932d43e167dSRafael J. Wysocki static void acpi_bus_get_power_flags(struct acpi_device *device)
9331da177e4SLinus Torvalds {
9348bc5053bSRafael J. Wysocki 	u32 i;
9351da177e4SLinus Torvalds 
936d43e167dSRafael J. Wysocki 	/* Presence of _PS0|_PR0 indicates 'power manageable' */
937952c63e9SJiang Liu 	if (!acpi_has_method(device->handle, "_PS0") &&
938952c63e9SJiang Liu 	    !acpi_has_method(device->handle, "_PR0"))
939d43e167dSRafael J. Wysocki 		return;
940d43e167dSRafael J. Wysocki 
941d43e167dSRafael J. Wysocki 	device->flags.power_manageable = 1;
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	/*
9449e89dde2SZhang Rui 	 * Power Management Flags
9451da177e4SLinus Torvalds 	 */
946952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_PSC"))
9479e89dde2SZhang Rui 		device->power.flags.explicit_get = 1;
948f25c0ae2SRafael J. Wysocki 
949952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_IRC"))
9509e89dde2SZhang Rui 		device->power.flags.inrush_current = 1;
9511da177e4SLinus Torvalds 
952f25c0ae2SRafael J. Wysocki 	if (acpi_has_method(device->handle, "_DSW"))
953f25c0ae2SRafael J. Wysocki 		device->power.flags.dsw_present = 1;
954f25c0ae2SRafael J. Wysocki 
9551da177e4SLinus Torvalds 	/*
9569e89dde2SZhang Rui 	 * Enumerate supported power management states
9571da177e4SLinus Torvalds 	 */
958f33ce568SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
959f33ce568SRafael J. Wysocki 		acpi_bus_init_power_state(device, i);
9601da177e4SLinus Torvalds 
9610b224527SRafael J. Wysocki 	INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
96220dacb71SRafael J. Wysocki 	if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources))
96320dacb71SRafael J. Wysocki 		device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
9649e89dde2SZhang Rui 
96520dacb71SRafael J. Wysocki 	/* Set defaults for D0 and D3hot states (always valid) */
9669e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].flags.valid = 1;
9679e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].power = 100;
96820dacb71SRafael J. Wysocki 	device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1;
9691399dfcdSAaron Lu 
9701b1f3e16SRafael J. Wysocki 	if (acpi_bus_init_power(device))
971b3785492SRafael J. Wysocki 		device->flags.power_manageable = 0;
972b3785492SRafael J. Wysocki }
973c8f7a62cSLen Brown 
974d43e167dSRafael J. Wysocki static void acpi_bus_get_flags(struct acpi_device *device)
9751da177e4SLinus Torvalds {
9761da177e4SLinus Torvalds 	/* Presence of _STA indicates 'dynamic_status' */
977952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_STA"))
9781da177e4SLinus Torvalds 		device->flags.dynamic_status = 1;
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	/* Presence of _RMV indicates 'removable' */
981952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_RMV"))
9821da177e4SLinus Torvalds 		device->flags.removable = 1;
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
985952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_EJD") ||
986952c63e9SJiang Liu 	    acpi_has_method(device->handle, "_EJ0"))
9871da177e4SLinus Torvalds 		device->flags.ejectable = 1;
9881da177e4SLinus Torvalds }
9891da177e4SLinus Torvalds 
990c7bcb4e9SBjorn Helgaas static void acpi_device_get_busid(struct acpi_device *device)
9911da177e4SLinus Torvalds {
9921da177e4SLinus Torvalds 	char bus_id[5] = { '?', 0 };
9931da177e4SLinus Torvalds 	struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
9941da177e4SLinus Torvalds 	int i = 0;
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	/*
9971da177e4SLinus Torvalds 	 * Bus ID
9981da177e4SLinus Torvalds 	 * ------
9991da177e4SLinus Torvalds 	 * The device's Bus ID is simply the object name.
10001da177e4SLinus Torvalds 	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
10011da177e4SLinus Torvalds 	 */
1002859ac9a4SBjorn Helgaas 	if (ACPI_IS_ROOT_DEVICE(device)) {
10031da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "ACPI");
1004859ac9a4SBjorn Helgaas 		return;
1005859ac9a4SBjorn Helgaas 	}
1006859ac9a4SBjorn Helgaas 
1007859ac9a4SBjorn Helgaas 	switch (device->device_type) {
10081da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
10091da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "PWRF");
10101da177e4SLinus Torvalds 		break;
10111da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
10121da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "SLPF");
10131da177e4SLinus Torvalds 		break;
10141da177e4SLinus Torvalds 	default:
101566b7ed40SBjorn Helgaas 		acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
10161da177e4SLinus Torvalds 		/* Clean up trailing underscores (if any) */
10171da177e4SLinus Torvalds 		for (i = 3; i > 1; i--) {
10181da177e4SLinus Torvalds 			if (bus_id[i] == '_')
10191da177e4SLinus Torvalds 				bus_id[i] = '\0';
10201da177e4SLinus Torvalds 			else
10211da177e4SLinus Torvalds 				break;
10221da177e4SLinus Torvalds 		}
10231da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, bus_id);
10241da177e4SLinus Torvalds 		break;
10251da177e4SLinus Torvalds 	}
10261da177e4SLinus Torvalds }
10271da177e4SLinus Torvalds 
102854735266SZhang Rui /*
1029ebf4df8dSJiang Liu  * acpi_ata_match - see if an acpi object is an ATA device
1030ebf4df8dSJiang Liu  *
1031ebf4df8dSJiang Liu  * If an acpi object has one of the ACPI ATA methods defined,
1032ebf4df8dSJiang Liu  * then we can safely call it an ATA device.
1033ebf4df8dSJiang Liu  */
1034ebf4df8dSJiang Liu bool acpi_ata_match(acpi_handle handle)
1035ebf4df8dSJiang Liu {
1036ebf4df8dSJiang Liu 	return acpi_has_method(handle, "_GTF") ||
1037ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_GTM") ||
1038ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_STM") ||
1039ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_SDD");
1040ebf4df8dSJiang Liu }
1041ebf4df8dSJiang Liu 
1042ebf4df8dSJiang Liu /*
1043d4e1a692SToshi Kani  * acpi_bay_match - see if an acpi object is an ejectable driver bay
104454735266SZhang Rui  *
104554735266SZhang Rui  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
104654735266SZhang Rui  * then we can safely call it an ejectable drive bay
104754735266SZhang Rui  */
1048ebf4df8dSJiang Liu bool acpi_bay_match(acpi_handle handle)
1049d4e1a692SToshi Kani {
105054735266SZhang Rui 	acpi_handle phandle;
105154735266SZhang Rui 
1052952c63e9SJiang Liu 	if (!acpi_has_method(handle, "_EJ0"))
1053ebf4df8dSJiang Liu 		return false;
1054ebf4df8dSJiang Liu 	if (acpi_ata_match(handle))
1055ebf4df8dSJiang Liu 		return true;
1056ebf4df8dSJiang Liu 	if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1057ebf4df8dSJiang Liu 		return false;
105854735266SZhang Rui 
1059ebf4df8dSJiang Liu 	return acpi_ata_match(phandle);
106054735266SZhang Rui }
106154735266SZhang Rui 
1062b43109faSRafael J. Wysocki bool acpi_device_is_battery(struct acpi_device *adev)
10631e2380cdSRafael J. Wysocki {
1064b43109faSRafael J. Wysocki 	struct acpi_hardware_id *hwid;
10651e2380cdSRafael J. Wysocki 
1066b43109faSRafael J. Wysocki 	list_for_each_entry(hwid, &adev->pnp.ids, list)
1067b43109faSRafael J. Wysocki 		if (!strcmp("PNP0C0A", hwid->id))
1068b43109faSRafael J. Wysocki 			return true;
1069b43109faSRafael J. Wysocki 
10701e2380cdSRafael J. Wysocki 	return false;
10711e2380cdSRafael J. Wysocki }
10721e2380cdSRafael J. Wysocki 
1073b43109faSRafael J. Wysocki static bool is_ejectable_bay(struct acpi_device *adev)
10741e2380cdSRafael J. Wysocki {
1075b43109faSRafael J. Wysocki 	acpi_handle handle = adev->handle;
1076b43109faSRafael J. Wysocki 
1077b43109faSRafael J. Wysocki 	if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
10781e2380cdSRafael J. Wysocki 		return true;
10791e2380cdSRafael J. Wysocki 
10801e2380cdSRafael J. Wysocki 	return acpi_bay_match(handle);
10811e2380cdSRafael J. Wysocki }
10821e2380cdSRafael J. Wysocki 
10833620f2f2SFrank Seidel /*
1084d4e1a692SToshi Kani  * acpi_dock_match - see if an acpi object has a _DCK method
10853620f2f2SFrank Seidel  */
1086ebf4df8dSJiang Liu bool acpi_dock_match(acpi_handle handle)
10873620f2f2SFrank Seidel {
1088ebf4df8dSJiang Liu 	return acpi_has_method(handle, "_DCK");
10893620f2f2SFrank Seidel }
10903620f2f2SFrank Seidel 
1091adc8bb8eSHans de Goede static acpi_status
1092adc8bb8eSHans de Goede acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context,
1093adc8bb8eSHans de Goede 			  void **return_value)
1094adc8bb8eSHans de Goede {
1095adc8bb8eSHans de Goede 	long *cap = context;
1096adc8bb8eSHans de Goede 
1097adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_BCM") &&
1098adc8bb8eSHans de Goede 	    acpi_has_method(handle, "_BCL")) {
1099adc8bb8eSHans de Goede 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found generic backlight "
1100adc8bb8eSHans de Goede 				  "support\n"));
1101adc8bb8eSHans de Goede 		*cap |= ACPI_VIDEO_BACKLIGHT;
1102adc8bb8eSHans de Goede 		if (!acpi_has_method(handle, "_BQC"))
1103adc8bb8eSHans de Goede 			printk(KERN_WARNING FW_BUG PREFIX "No _BQC method, "
1104adc8bb8eSHans de Goede 				"cannot determine initial brightness\n");
1105adc8bb8eSHans de Goede 		/* We have backlight support, no need to scan further */
1106adc8bb8eSHans de Goede 		return AE_CTRL_TERMINATE;
1107adc8bb8eSHans de Goede 	}
1108adc8bb8eSHans de Goede 	return 0;
1109adc8bb8eSHans de Goede }
1110adc8bb8eSHans de Goede 
1111adc8bb8eSHans de Goede /* Returns true if the ACPI object is a video device which can be
1112adc8bb8eSHans de Goede  * handled by video.ko.
1113adc8bb8eSHans de Goede  * The device will get a Linux specific CID added in scan.c to
1114adc8bb8eSHans de Goede  * identify the device as an ACPI graphics device
1115adc8bb8eSHans de Goede  * Be aware that the graphics device may not be physically present
1116adc8bb8eSHans de Goede  * Use acpi_video_get_capabilities() to detect general ACPI video
1117adc8bb8eSHans de Goede  * capabilities of present cards
1118adc8bb8eSHans de Goede  */
1119adc8bb8eSHans de Goede long acpi_is_video_device(acpi_handle handle)
1120adc8bb8eSHans de Goede {
1121adc8bb8eSHans de Goede 	long video_caps = 0;
1122adc8bb8eSHans de Goede 
1123adc8bb8eSHans de Goede 	/* Is this device able to support video switching ? */
1124adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS"))
1125adc8bb8eSHans de Goede 		video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING;
1126adc8bb8eSHans de Goede 
1127adc8bb8eSHans de Goede 	/* Is this device able to retrieve a video ROM ? */
1128adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_ROM"))
1129adc8bb8eSHans de Goede 		video_caps |= ACPI_VIDEO_ROM_AVAILABLE;
1130adc8bb8eSHans de Goede 
1131adc8bb8eSHans de Goede 	/* Is this device able to configure which video head to be POSTed ? */
1132adc8bb8eSHans de Goede 	if (acpi_has_method(handle, "_VPO") &&
1133adc8bb8eSHans de Goede 	    acpi_has_method(handle, "_GPD") &&
1134adc8bb8eSHans de Goede 	    acpi_has_method(handle, "_SPD"))
1135adc8bb8eSHans de Goede 		video_caps |= ACPI_VIDEO_DEVICE_POSTING;
1136adc8bb8eSHans de Goede 
1137adc8bb8eSHans de Goede 	/* Only check for backlight functionality if one of the above hit. */
1138adc8bb8eSHans de Goede 	if (video_caps)
1139adc8bb8eSHans de Goede 		acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1140adc8bb8eSHans de Goede 				    ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL,
1141adc8bb8eSHans de Goede 				    &video_caps, NULL);
1142adc8bb8eSHans de Goede 
1143adc8bb8eSHans de Goede 	return video_caps;
1144adc8bb8eSHans de Goede }
1145adc8bb8eSHans de Goede EXPORT_SYMBOL(acpi_is_video_device);
1146adc8bb8eSHans de Goede 
1147620e112cSThomas Renninger const char *acpi_device_hid(struct acpi_device *device)
114815b8dd53SBob Moore {
11497f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *hid;
115015b8dd53SBob Moore 
11512b2ae7c7SThomas Renninger 	if (list_empty(&device->pnp.ids))
11522b2ae7c7SThomas Renninger 		return dummy_hid;
11532b2ae7c7SThomas Renninger 
11547f47fa6cSBjorn Helgaas 	hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
11557f47fa6cSBjorn Helgaas 	return hid->id;
11567f47fa6cSBjorn Helgaas }
11577f47fa6cSBjorn Helgaas EXPORT_SYMBOL(acpi_device_hid);
115815b8dd53SBob Moore 
1159d4e1a692SToshi Kani static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
11607f47fa6cSBjorn Helgaas {
11617f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id;
116215b8dd53SBob Moore 
11637f47fa6cSBjorn Helgaas 	id = kmalloc(sizeof(*id), GFP_KERNEL);
11647f47fa6cSBjorn Helgaas 	if (!id)
11657f47fa6cSBjorn Helgaas 		return;
116615b8dd53SBob Moore 
11676a0d12efSRasmus Villemoes 	id->id = kstrdup_const(dev_id, GFP_KERNEL);
11687f47fa6cSBjorn Helgaas 	if (!id->id) {
11697f47fa6cSBjorn Helgaas 		kfree(id);
11707f47fa6cSBjorn Helgaas 		return;
117115b8dd53SBob Moore 	}
117215b8dd53SBob Moore 
1173d4e1a692SToshi Kani 	list_add_tail(&id->list, &pnp->ids);
1174d4e1a692SToshi Kani 	pnp->type.hardware_id = 1;
117515b8dd53SBob Moore }
117615b8dd53SBob Moore 
1177222e82acSDarrick J. Wong /*
1178222e82acSDarrick J. Wong  * Old IBM workstations have a DSDT bug wherein the SMBus object
1179222e82acSDarrick J. Wong  * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1180222e82acSDarrick J. Wong  * prefix.  Work around this.
1181222e82acSDarrick J. Wong  */
1182ebf4df8dSJiang Liu static bool acpi_ibm_smbus_match(acpi_handle handle)
1183222e82acSDarrick J. Wong {
1184ebf4df8dSJiang Liu 	char node_name[ACPI_PATH_SEGMENT_LENGTH];
1185ebf4df8dSJiang Liu 	struct acpi_buffer path = { sizeof(node_name), node_name };
1186222e82acSDarrick J. Wong 
1187222e82acSDarrick J. Wong 	if (!dmi_name_in_vendors("IBM"))
1188ebf4df8dSJiang Liu 		return false;
1189222e82acSDarrick J. Wong 
1190222e82acSDarrick J. Wong 	/* Look for SMBS object */
1191ebf4df8dSJiang Liu 	if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1192ebf4df8dSJiang Liu 	    strcmp("SMBS", path.pointer))
1193ebf4df8dSJiang Liu 		return false;
1194222e82acSDarrick J. Wong 
1195222e82acSDarrick J. Wong 	/* Does it have the necessary (but misnamed) methods? */
1196952c63e9SJiang Liu 	if (acpi_has_method(handle, "SBI") &&
1197952c63e9SJiang Liu 	    acpi_has_method(handle, "SBR") &&
1198952c63e9SJiang Liu 	    acpi_has_method(handle, "SBW"))
1199ebf4df8dSJiang Liu 		return true;
1200ebf4df8dSJiang Liu 
1201ebf4df8dSJiang Liu 	return false;
1202222e82acSDarrick J. Wong }
1203222e82acSDarrick J. Wong 
1204ae3caa80SJiang Liu static bool acpi_object_is_system_bus(acpi_handle handle)
1205ae3caa80SJiang Liu {
1206ae3caa80SJiang Liu 	acpi_handle tmp;
1207ae3caa80SJiang Liu 
1208ae3caa80SJiang Liu 	if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1209ae3caa80SJiang Liu 	    tmp == handle)
1210ae3caa80SJiang Liu 		return true;
1211ae3caa80SJiang Liu 	if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1212ae3caa80SJiang Liu 	    tmp == handle)
1213ae3caa80SJiang Liu 		return true;
1214ae3caa80SJiang Liu 
1215ae3caa80SJiang Liu 	return false;
1216ae3caa80SJiang Liu }
1217ae3caa80SJiang Liu 
1218c0af4175SToshi Kani static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1219c0af4175SToshi Kani 				int device_type)
12201da177e4SLinus Torvalds {
12211da177e4SLinus Torvalds 	acpi_status status;
122257f3674fSBjorn Helgaas 	struct acpi_device_info *info;
122378e25fefSLv Zheng 	struct acpi_pnp_device_id_list *cid_list;
12247f47fa6cSBjorn Helgaas 	int i;
12251da177e4SLinus Torvalds 
1226c0af4175SToshi Kani 	switch (device_type) {
12271da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
1228c0af4175SToshi Kani 		if (handle == ACPI_ROOT_OBJECT) {
1229c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_SYSTEM_HID);
1230859ac9a4SBjorn Helgaas 			break;
1231859ac9a4SBjorn Helgaas 		}
1232859ac9a4SBjorn Helgaas 
1233c0af4175SToshi Kani 		status = acpi_get_object_info(handle, &info);
12341da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
1235c0af4175SToshi Kani 			pr_err(PREFIX "%s: Error reading device info\n",
1236c0af4175SToshi Kani 					__func__);
12371da177e4SLinus Torvalds 			return;
12381da177e4SLinus Torvalds 		}
12391da177e4SLinus Torvalds 
1240549e6845SRafael J. Wysocki 		if (info->valid & ACPI_VALID_HID) {
1241c0af4175SToshi Kani 			acpi_add_id(pnp, info->hardware_id.string);
1242549e6845SRafael J. Wysocki 			pnp->type.platform_id = 1;
1243549e6845SRafael J. Wysocki 		}
124457f3674fSBjorn Helgaas 		if (info->valid & ACPI_VALID_CID) {
124515b8dd53SBob Moore 			cid_list = &info->compatible_id_list;
124657f3674fSBjorn Helgaas 			for (i = 0; i < cid_list->count; i++)
1247c0af4175SToshi Kani 				acpi_add_id(pnp, cid_list->ids[i].string);
124857f3674fSBjorn Helgaas 		}
12491da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_ADR) {
1250c0af4175SToshi Kani 			pnp->bus_address = info->address;
1251c0af4175SToshi Kani 			pnp->type.bus_address = 1;
12521da177e4SLinus Torvalds 		}
1253ccf78040SLv Zheng 		if (info->valid & ACPI_VALID_UID)
1254c0af4175SToshi Kani 			pnp->unique_id = kstrdup(info->unique_id.string,
1255ccf78040SLv Zheng 							GFP_KERNEL);
125626095a01SSuthikulpanit, Suravee 		if (info->valid & ACPI_VALID_CLS)
125726095a01SSuthikulpanit, Suravee 			acpi_add_id(pnp, info->class_code.string);
1258ae843332SZhang Rui 
1259a83893aeSBjorn Helgaas 		kfree(info);
1260a83893aeSBjorn Helgaas 
126157f3674fSBjorn Helgaas 		/*
126257f3674fSBjorn Helgaas 		 * Some devices don't reliably have _HIDs & _CIDs, so add
126357f3674fSBjorn Helgaas 		 * synthetic HIDs to make sure drivers can find them.
12643620f2f2SFrank Seidel 		 */
1265c0af4175SToshi Kani 		if (acpi_is_video_device(handle))
1266c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_VIDEO_HID);
1267ebf4df8dSJiang Liu 		else if (acpi_bay_match(handle))
1268c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_BAY_HID);
1269ebf4df8dSJiang Liu 		else if (acpi_dock_match(handle))
1270c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_DOCK_HID);
1271ebf4df8dSJiang Liu 		else if (acpi_ibm_smbus_match(handle))
1272c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1273ae3caa80SJiang Liu 		else if (list_empty(&pnp->ids) &&
1274ae3caa80SJiang Liu 			 acpi_object_is_system_bus(handle)) {
1275ae3caa80SJiang Liu 			/* \_SB, \_TZ, LNXSYBUS */
1276ae3caa80SJiang Liu 			acpi_add_id(pnp, ACPI_BUS_HID);
1277c0af4175SToshi Kani 			strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1278c0af4175SToshi Kani 			strcpy(pnp->device_class, ACPI_BUS_CLASS);
1279b7b30de5SBjorn Helgaas 		}
128054735266SZhang Rui 
12811da177e4SLinus Torvalds 		break;
12821da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER:
1283c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_POWER_HID);
12841da177e4SLinus Torvalds 		break;
12851da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_PROCESSOR:
1286c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
12871da177e4SLinus Torvalds 		break;
12881da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_THERMAL:
1289c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_THERMAL_HID);
12901da177e4SLinus Torvalds 		break;
12911da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
1292c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
12931da177e4SLinus Torvalds 		break;
12941da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
1295c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
12961da177e4SLinus Torvalds 		break;
12971da177e4SLinus Torvalds 	}
12981da177e4SLinus Torvalds }
12991da177e4SLinus Torvalds 
1300c0af4175SToshi Kani void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1301c0af4175SToshi Kani {
1302c0af4175SToshi Kani 	struct acpi_hardware_id *id, *tmp;
1303c0af4175SToshi Kani 
1304c0af4175SToshi Kani 	list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
13056a0d12efSRasmus Villemoes 		kfree_const(id->id);
1306c0af4175SToshi Kani 		kfree(id);
1307c0af4175SToshi Kani 	}
1308c0af4175SToshi Kani 	kfree(pnp->unique_id);
1309c0af4175SToshi Kani }
1310c0af4175SToshi Kani 
1311b84f196dSSuthikulpanit, Suravee /**
1312b84f196dSSuthikulpanit, Suravee  * acpi_dma_supported - Check DMA support for the specified device.
1313b84f196dSSuthikulpanit, Suravee  * @adev: The pointer to acpi device
1314b84f196dSSuthikulpanit, Suravee  *
1315b84f196dSSuthikulpanit, Suravee  * Return false if DMA is not supported. Otherwise, return true
1316b84f196dSSuthikulpanit, Suravee  */
1317b84f196dSSuthikulpanit, Suravee bool acpi_dma_supported(struct acpi_device *adev)
1318b84f196dSSuthikulpanit, Suravee {
1319b84f196dSSuthikulpanit, Suravee 	if (!adev)
1320b84f196dSSuthikulpanit, Suravee 		return false;
1321b84f196dSSuthikulpanit, Suravee 
1322b84f196dSSuthikulpanit, Suravee 	if (adev->flags.cca_seen)
1323b84f196dSSuthikulpanit, Suravee 		return true;
1324b84f196dSSuthikulpanit, Suravee 
1325b84f196dSSuthikulpanit, Suravee 	/*
1326b84f196dSSuthikulpanit, Suravee 	* Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent
1327b84f196dSSuthikulpanit, Suravee 	* DMA on "Intel platforms".  Presumably that includes all x86 and
1328b84f196dSSuthikulpanit, Suravee 	* ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y.
1329b84f196dSSuthikulpanit, Suravee 	*/
1330b84f196dSSuthikulpanit, Suravee 	if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1331b84f196dSSuthikulpanit, Suravee 		return true;
1332b84f196dSSuthikulpanit, Suravee 
1333b84f196dSSuthikulpanit, Suravee 	return false;
1334b84f196dSSuthikulpanit, Suravee }
1335b84f196dSSuthikulpanit, Suravee 
1336b84f196dSSuthikulpanit, Suravee /**
1337b84f196dSSuthikulpanit, Suravee  * acpi_get_dma_attr - Check the supported DMA attr for the specified device.
1338b84f196dSSuthikulpanit, Suravee  * @adev: The pointer to acpi device
1339b84f196dSSuthikulpanit, Suravee  *
1340b84f196dSSuthikulpanit, Suravee  * Return enum dev_dma_attr.
1341b84f196dSSuthikulpanit, Suravee  */
1342b84f196dSSuthikulpanit, Suravee enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
1343b84f196dSSuthikulpanit, Suravee {
1344b84f196dSSuthikulpanit, Suravee 	if (!acpi_dma_supported(adev))
1345b84f196dSSuthikulpanit, Suravee 		return DEV_DMA_NOT_SUPPORTED;
1346b84f196dSSuthikulpanit, Suravee 
1347b84f196dSSuthikulpanit, Suravee 	if (adev->flags.coherent_dma)
1348b84f196dSSuthikulpanit, Suravee 		return DEV_DMA_COHERENT;
1349b84f196dSSuthikulpanit, Suravee 	else
1350b84f196dSSuthikulpanit, Suravee 		return DEV_DMA_NON_COHERENT;
1351b84f196dSSuthikulpanit, Suravee }
1352b84f196dSSuthikulpanit, Suravee 
1353d0562674SSuthikulpanit, Suravee static void acpi_init_coherency(struct acpi_device *adev)
1354d0562674SSuthikulpanit, Suravee {
1355d0562674SSuthikulpanit, Suravee 	unsigned long long cca = 0;
1356d0562674SSuthikulpanit, Suravee 	acpi_status status;
1357d0562674SSuthikulpanit, Suravee 	struct acpi_device *parent = adev->parent;
1358d0562674SSuthikulpanit, Suravee 
1359d0562674SSuthikulpanit, Suravee 	if (parent && parent->flags.cca_seen) {
1360d0562674SSuthikulpanit, Suravee 		/*
1361d0562674SSuthikulpanit, Suravee 		 * From ACPI spec, OSPM will ignore _CCA if an ancestor
1362d0562674SSuthikulpanit, Suravee 		 * already saw one.
1363d0562674SSuthikulpanit, Suravee 		 */
1364d0562674SSuthikulpanit, Suravee 		adev->flags.cca_seen = 1;
1365d0562674SSuthikulpanit, Suravee 		cca = parent->flags.coherent_dma;
1366d0562674SSuthikulpanit, Suravee 	} else {
1367d0562674SSuthikulpanit, Suravee 		status = acpi_evaluate_integer(adev->handle, "_CCA",
1368d0562674SSuthikulpanit, Suravee 					       NULL, &cca);
1369d0562674SSuthikulpanit, Suravee 		if (ACPI_SUCCESS(status))
1370d0562674SSuthikulpanit, Suravee 			adev->flags.cca_seen = 1;
1371d0562674SSuthikulpanit, Suravee 		else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1372d0562674SSuthikulpanit, Suravee 			/*
1373d0562674SSuthikulpanit, Suravee 			 * If architecture does not specify that _CCA is
1374d0562674SSuthikulpanit, Suravee 			 * required for DMA-able devices (e.g. x86),
1375d0562674SSuthikulpanit, Suravee 			 * we default to _CCA=1.
1376d0562674SSuthikulpanit, Suravee 			 */
1377d0562674SSuthikulpanit, Suravee 			cca = 1;
1378d0562674SSuthikulpanit, Suravee 		else
1379d0562674SSuthikulpanit, Suravee 			acpi_handle_debug(adev->handle,
1380d0562674SSuthikulpanit, Suravee 					  "ACPI device is missing _CCA.\n");
1381d0562674SSuthikulpanit, Suravee 	}
1382d0562674SSuthikulpanit, Suravee 
1383d0562674SSuthikulpanit, Suravee 	adev->flags.coherent_dma = cca;
1384d0562674SSuthikulpanit, Suravee }
1385d0562674SSuthikulpanit, Suravee 
138682c7d5efSRafael J. Wysocki void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1387d43e167dSRafael J. Wysocki 			     int type, unsigned long long sta)
13881da177e4SLinus Torvalds {
1389d43e167dSRafael J. Wysocki 	INIT_LIST_HEAD(&device->pnp.ids);
1390d43e167dSRafael J. Wysocki 	device->device_type = type;
1391d43e167dSRafael J. Wysocki 	device->handle = handle;
1392d43e167dSRafael J. Wysocki 	device->parent = acpi_bus_get_parent(handle);
13938a0662d9SRafael J. Wysocki 	device->fwnode.type = FWNODE_ACPI;
139425db115bSRafael J. Wysocki 	acpi_set_device_status(device, sta);
1395d43e167dSRafael J. Wysocki 	acpi_device_get_busid(device);
1396c0af4175SToshi Kani 	acpi_set_pnp_ids(handle, &device->pnp, type);
1397ffdcd955SMika Westerberg 	acpi_init_properties(device);
1398d43e167dSRafael J. Wysocki 	acpi_bus_get_flags(device);
13992c0d4fe0SRafael J. Wysocki 	device->flags.match_driver = false;
1400202317a5SRafael J. Wysocki 	device->flags.initialized = true;
1401202317a5SRafael J. Wysocki 	device->flags.visited = false;
1402cf860be6SRafael J. Wysocki 	device_initialize(&device->dev);
1403cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, true);
1404d0562674SSuthikulpanit, Suravee 	acpi_init_coherency(device);
1405cf860be6SRafael J. Wysocki }
1406bc3b0772SBjorn Helgaas 
1407cf860be6SRafael J. Wysocki void acpi_device_add_finalize(struct acpi_device *device)
1408cf860be6SRafael J. Wysocki {
1409cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, false);
1410cf860be6SRafael J. Wysocki 	kobject_uevent(&device->dev.kobj, KOBJ_ADD);
14111da177e4SLinus Torvalds }
14121da177e4SLinus Torvalds 
14135c478f49SBjorn Helgaas static int acpi_add_single_object(struct acpi_device **child,
14145c478f49SBjorn Helgaas 				  acpi_handle handle, int type,
14152c0d4fe0SRafael J. Wysocki 				  unsigned long long sta)
14161da177e4SLinus Torvalds {
141777c24888SBjorn Helgaas 	int result;
141877c24888SBjorn Helgaas 	struct acpi_device *device;
141929aaefa6SBjorn Helgaas 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
14201da177e4SLinus Torvalds 
142136bcbec7SBurman Yan 	device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
14221da177e4SLinus Torvalds 	if (!device) {
14236468463aSLen Brown 		printk(KERN_ERR PREFIX "Memory allocation error\n");
1424d550d98dSPatrick Mochel 		return -ENOMEM;
14251da177e4SLinus Torvalds 	}
14261da177e4SLinus Torvalds 
1427d43e167dSRafael J. Wysocki 	acpi_init_device_object(device, handle, type, sta);
1428d43e167dSRafael J. Wysocki 	acpi_bus_get_power_flags(device);
1429d57d09a4SRafael J. Wysocki 	acpi_bus_get_wakeup_device_flags(device);
14301da177e4SLinus Torvalds 
1431cf860be6SRafael J. Wysocki 	result = acpi_device_add(device, acpi_device_release);
1432d43e167dSRafael J. Wysocki 	if (result) {
1433718fb0deSHugh Dickins 		acpi_device_release(&device->dev);
1434d550d98dSPatrick Mochel 		return result;
14351da177e4SLinus Torvalds 	}
14361da177e4SLinus Torvalds 
1437bc9b6407SRafael J. Wysocki 	acpi_power_add_remove_device(device, true);
1438cf860be6SRafael J. Wysocki 	acpi_device_add_finalize(device);
14391da177e4SLinus Torvalds 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1440d43e167dSRafael J. Wysocki 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1441d43e167dSRafael J. Wysocki 		dev_name(&device->dev), (char *) buffer.pointer,
1442d43e167dSRafael J. Wysocki 		device->parent ? dev_name(&device->parent->dev) : "(null)"));
14431da177e4SLinus Torvalds 	kfree(buffer.pointer);
14441da177e4SLinus Torvalds 	*child = device;
1445d43e167dSRafael J. Wysocki 	return 0;
1446bf325f95SRafael J. Wysocki }
1447bf325f95SRafael J. Wysocki 
1448778cbc1dSBjorn Helgaas static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1449778cbc1dSBjorn Helgaas 				    unsigned long long *sta)
1450778cbc1dSBjorn Helgaas {
1451778cbc1dSBjorn Helgaas 	acpi_status status;
1452778cbc1dSBjorn Helgaas 	acpi_object_type acpi_type;
1453778cbc1dSBjorn Helgaas 
1454778cbc1dSBjorn Helgaas 	status = acpi_get_type(handle, &acpi_type);
1455778cbc1dSBjorn Helgaas 	if (ACPI_FAILURE(status))
1456778cbc1dSBjorn Helgaas 		return -ENODEV;
1457778cbc1dSBjorn Helgaas 
1458778cbc1dSBjorn Helgaas 	switch (acpi_type) {
1459778cbc1dSBjorn Helgaas 	case ACPI_TYPE_ANY:		/* for ACPI_ROOT_OBJECT */
1460778cbc1dSBjorn Helgaas 	case ACPI_TYPE_DEVICE:
1461778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_DEVICE;
1462778cbc1dSBjorn Helgaas 		status = acpi_bus_get_status_handle(handle, sta);
1463778cbc1dSBjorn Helgaas 		if (ACPI_FAILURE(status))
1464*e399037eSAaron Lu 			*sta = 0;
1465778cbc1dSBjorn Helgaas 		break;
1466778cbc1dSBjorn Helgaas 	case ACPI_TYPE_PROCESSOR:
1467778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_PROCESSOR;
1468778cbc1dSBjorn Helgaas 		status = acpi_bus_get_status_handle(handle, sta);
1469778cbc1dSBjorn Helgaas 		if (ACPI_FAILURE(status))
1470778cbc1dSBjorn Helgaas 			return -ENODEV;
1471778cbc1dSBjorn Helgaas 		break;
1472778cbc1dSBjorn Helgaas 	case ACPI_TYPE_THERMAL:
1473778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_THERMAL;
1474778cbc1dSBjorn Helgaas 		*sta = ACPI_STA_DEFAULT;
1475778cbc1dSBjorn Helgaas 		break;
1476778cbc1dSBjorn Helgaas 	case ACPI_TYPE_POWER:
1477778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_POWER;
1478778cbc1dSBjorn Helgaas 		*sta = ACPI_STA_DEFAULT;
1479778cbc1dSBjorn Helgaas 		break;
1480778cbc1dSBjorn Helgaas 	default:
1481778cbc1dSBjorn Helgaas 		return -ENODEV;
1482778cbc1dSBjorn Helgaas 	}
1483778cbc1dSBjorn Helgaas 
1484778cbc1dSBjorn Helgaas 	return 0;
1485778cbc1dSBjorn Helgaas }
1486778cbc1dSBjorn Helgaas 
1487202317a5SRafael J. Wysocki bool acpi_device_is_present(struct acpi_device *adev)
1488202317a5SRafael J. Wysocki {
1489202317a5SRafael J. Wysocki 	if (adev->status.present || adev->status.functional)
1490202317a5SRafael J. Wysocki 		return true;
1491202317a5SRafael J. Wysocki 
1492202317a5SRafael J. Wysocki 	adev->flags.initialized = false;
1493202317a5SRafael J. Wysocki 	return false;
1494202317a5SRafael J. Wysocki }
1495202317a5SRafael J. Wysocki 
14964b59cc1fSRafael J. Wysocki static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1497844142c3SRasmus Villemoes 				       const char *idstr,
1498c5698074SRafael J. Wysocki 				       const struct acpi_device_id **matchid)
1499c5698074SRafael J. Wysocki {
1500c5698074SRafael J. Wysocki 	const struct acpi_device_id *devid;
1501c5698074SRafael J. Wysocki 
1502aca0a4ebSRafael J. Wysocki 	if (handler->match)
1503aca0a4ebSRafael J. Wysocki 		return handler->match(idstr, matchid);
1504aca0a4ebSRafael J. Wysocki 
1505c5698074SRafael J. Wysocki 	for (devid = handler->ids; devid->id[0]; devid++)
1506c5698074SRafael J. Wysocki 		if (!strcmp((char *)devid->id, idstr)) {
1507c5698074SRafael J. Wysocki 			if (matchid)
1508c5698074SRafael J. Wysocki 				*matchid = devid;
1509c5698074SRafael J. Wysocki 
15104b59cc1fSRafael J. Wysocki 			return true;
15114b59cc1fSRafael J. Wysocki 		}
15124b59cc1fSRafael J. Wysocki 
15134b59cc1fSRafael J. Wysocki 	return false;
15144b59cc1fSRafael J. Wysocki }
15154b59cc1fSRafael J. Wysocki 
1516844142c3SRasmus Villemoes static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
15176b772e8fSToshi Kani 					const struct acpi_device_id **matchid)
15186b772e8fSToshi Kani {
15196b772e8fSToshi Kani 	struct acpi_scan_handler *handler;
15206b772e8fSToshi Kani 
15216b772e8fSToshi Kani 	list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
15226b772e8fSToshi Kani 		if (acpi_scan_handler_matching(handler, idstr, matchid))
15236b772e8fSToshi Kani 			return handler;
15246b772e8fSToshi Kani 
15256b772e8fSToshi Kani 	return NULL;
15266b772e8fSToshi Kani }
15276b772e8fSToshi Kani 
15283f8055c3SRafael J. Wysocki void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
15293f8055c3SRafael J. Wysocki {
15303f8055c3SRafael J. Wysocki 	if (!!hotplug->enabled == !!val)
15313f8055c3SRafael J. Wysocki 		return;
15323f8055c3SRafael J. Wysocki 
15333f8055c3SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
15343f8055c3SRafael J. Wysocki 
15353f8055c3SRafael J. Wysocki 	hotplug->enabled = val;
15363f8055c3SRafael J. Wysocki 
15373f8055c3SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
15383f8055c3SRafael J. Wysocki }
15393f8055c3SRafael J. Wysocki 
15403c2cc7ffSRafael J. Wysocki static void acpi_scan_init_hotplug(struct acpi_device *adev)
15414b59cc1fSRafael J. Wysocki {
15426b772e8fSToshi Kani 	struct acpi_hardware_id *hwid;
15433c2cc7ffSRafael J. Wysocki 
1544b43109faSRafael J. Wysocki 	if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
15451e2380cdSRafael J. Wysocki 		acpi_dock_add(adev);
15461e2380cdSRafael J. Wysocki 		return;
15471e2380cdSRafael J. Wysocki 	}
15483c2cc7ffSRafael J. Wysocki 	list_for_each_entry(hwid, &adev->pnp.ids, list) {
15494b59cc1fSRafael J. Wysocki 		struct acpi_scan_handler *handler;
15504b59cc1fSRafael J. Wysocki 
15516b772e8fSToshi Kani 		handler = acpi_scan_match_handler(hwid->id, NULL);
15521a699476SRafael J. Wysocki 		if (handler) {
15533c2cc7ffSRafael J. Wysocki 			adev->flags.hotplug_notify = true;
15546b772e8fSToshi Kani 			break;
15556b772e8fSToshi Kani 		}
15566b772e8fSToshi Kani 	}
15571a699476SRafael J. Wysocki }
15586b772e8fSToshi Kani 
155940e7fcb1SLan Tianyu static void acpi_device_dep_initialize(struct acpi_device *adev)
156040e7fcb1SLan Tianyu {
156140e7fcb1SLan Tianyu 	struct acpi_dep_data *dep;
156240e7fcb1SLan Tianyu 	struct acpi_handle_list dep_devices;
156340e7fcb1SLan Tianyu 	acpi_status status;
156440e7fcb1SLan Tianyu 	int i;
156540e7fcb1SLan Tianyu 
156640e7fcb1SLan Tianyu 	if (!acpi_has_method(adev->handle, "_DEP"))
156740e7fcb1SLan Tianyu 		return;
156840e7fcb1SLan Tianyu 
156940e7fcb1SLan Tianyu 	status = acpi_evaluate_reference(adev->handle, "_DEP", NULL,
157040e7fcb1SLan Tianyu 					&dep_devices);
157140e7fcb1SLan Tianyu 	if (ACPI_FAILURE(status)) {
157280167a24SRafael J. Wysocki 		dev_dbg(&adev->dev, "Failed to evaluate _DEP.\n");
157340e7fcb1SLan Tianyu 		return;
157440e7fcb1SLan Tianyu 	}
157540e7fcb1SLan Tianyu 
157640e7fcb1SLan Tianyu 	for (i = 0; i < dep_devices.count; i++) {
157740e7fcb1SLan Tianyu 		struct acpi_device_info *info;
157840e7fcb1SLan Tianyu 		int skip;
157940e7fcb1SLan Tianyu 
158040e7fcb1SLan Tianyu 		status = acpi_get_object_info(dep_devices.handles[i], &info);
158140e7fcb1SLan Tianyu 		if (ACPI_FAILURE(status)) {
158280167a24SRafael J. Wysocki 			dev_dbg(&adev->dev, "Error reading _DEP device info\n");
158340e7fcb1SLan Tianyu 			continue;
158440e7fcb1SLan Tianyu 		}
158540e7fcb1SLan Tianyu 
158640e7fcb1SLan Tianyu 		/*
158740e7fcb1SLan Tianyu 		 * Skip the dependency of Windows System Power
158840e7fcb1SLan Tianyu 		 * Management Controller
158940e7fcb1SLan Tianyu 		 */
159040e7fcb1SLan Tianyu 		skip = info->valid & ACPI_VALID_HID &&
159140e7fcb1SLan Tianyu 			!strcmp(info->hardware_id.string, "INT3396");
159240e7fcb1SLan Tianyu 
159340e7fcb1SLan Tianyu 		kfree(info);
159440e7fcb1SLan Tianyu 
159540e7fcb1SLan Tianyu 		if (skip)
159640e7fcb1SLan Tianyu 			continue;
159740e7fcb1SLan Tianyu 
159840e7fcb1SLan Tianyu 		dep = kzalloc(sizeof(struct acpi_dep_data), GFP_KERNEL);
159940e7fcb1SLan Tianyu 		if (!dep)
160040e7fcb1SLan Tianyu 			return;
160140e7fcb1SLan Tianyu 
160240e7fcb1SLan Tianyu 		dep->master = dep_devices.handles[i];
160340e7fcb1SLan Tianyu 		dep->slave  = adev->handle;
160440e7fcb1SLan Tianyu 		adev->dep_unmet++;
160540e7fcb1SLan Tianyu 
160640e7fcb1SLan Tianyu 		mutex_lock(&acpi_dep_list_lock);
160740e7fcb1SLan Tianyu 		list_add_tail(&dep->node , &acpi_dep_list);
160840e7fcb1SLan Tianyu 		mutex_unlock(&acpi_dep_list_lock);
160940e7fcb1SLan Tianyu 	}
161040e7fcb1SLan Tianyu }
161140e7fcb1SLan Tianyu 
1612e3863094SRafael J. Wysocki static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1613e3863094SRafael J. Wysocki 				      void *not_used, void **return_value)
16141da177e4SLinus Torvalds {
1615805d410fSRafael J. Wysocki 	struct acpi_device *device = NULL;
1616778cbc1dSBjorn Helgaas 	int type;
1617778cbc1dSBjorn Helgaas 	unsigned long long sta;
1618778cbc1dSBjorn Helgaas 	int result;
16191da177e4SLinus Torvalds 
16204002bf38SRafael J. Wysocki 	acpi_bus_get_device(handle, &device);
16214002bf38SRafael J. Wysocki 	if (device)
16224002bf38SRafael J. Wysocki 		goto out;
16234002bf38SRafael J. Wysocki 
1624778cbc1dSBjorn Helgaas 	result = acpi_bus_type_and_status(handle, &type, &sta);
1625778cbc1dSBjorn Helgaas 	if (result)
162651a85fafSBjorn Helgaas 		return AE_OK;
16271da177e4SLinus Torvalds 
162882c7d5efSRafael J. Wysocki 	if (type == ACPI_BUS_TYPE_POWER) {
162982c7d5efSRafael J. Wysocki 		acpi_add_power_resource(handle);
163082c7d5efSRafael J. Wysocki 		return AE_OK;
163182c7d5efSRafael J. Wysocki 	}
163282c7d5efSRafael J. Wysocki 
16332c0d4fe0SRafael J. Wysocki 	acpi_add_single_object(&device, handle, type, sta);
1634e3b87f8aSBjorn Helgaas 	if (!device)
163551a85fafSBjorn Helgaas 		return AE_CTRL_DEPTH;
16361da177e4SLinus Torvalds 
16373c2cc7ffSRafael J. Wysocki 	acpi_scan_init_hotplug(device);
163840e7fcb1SLan Tianyu 	acpi_device_dep_initialize(device);
16393c2cc7ffSRafael J. Wysocki 
16404002bf38SRafael J. Wysocki  out:
164151a85fafSBjorn Helgaas 	if (!*return_value)
164251a85fafSBjorn Helgaas 		*return_value = device;
1643805d410fSRafael J. Wysocki 
164451a85fafSBjorn Helgaas 	return AE_OK;
16451da177e4SLinus Torvalds }
16461da177e4SLinus Torvalds 
164748459340SZhang Rui static int acpi_check_spi_i2c_slave(struct acpi_resource *ares, void *data)
164848459340SZhang Rui {
164948459340SZhang Rui 	bool *is_spi_i2c_slave_p = data;
165048459340SZhang Rui 
165148459340SZhang Rui 	if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
165248459340SZhang Rui 		return 1;
165348459340SZhang Rui 
165448459340SZhang Rui 	/*
165548459340SZhang Rui 	 * devices that are connected to UART still need to be enumerated to
165648459340SZhang Rui 	 * platform bus
165748459340SZhang Rui 	 */
165848459340SZhang Rui 	if (ares->data.common_serial_bus.type != ACPI_RESOURCE_SERIAL_TYPE_UART)
165948459340SZhang Rui 		*is_spi_i2c_slave_p = true;
166048459340SZhang Rui 
166148459340SZhang Rui 	 /* no need to do more checking */
166248459340SZhang Rui 	return -1;
166348459340SZhang Rui }
166448459340SZhang Rui 
166548459340SZhang Rui static void acpi_default_enumeration(struct acpi_device *device)
166648459340SZhang Rui {
166748459340SZhang Rui 	struct list_head resource_list;
166848459340SZhang Rui 	bool is_spi_i2c_slave = false;
166948459340SZhang Rui 
167048459340SZhang Rui 	/*
167148459340SZhang Rui 	 * Do not enemerate SPI/I2C slaves as they will be enuerated by their
167248459340SZhang Rui 	 * respective parents.
167348459340SZhang Rui 	 */
167448459340SZhang Rui 	INIT_LIST_HEAD(&resource_list);
167548459340SZhang Rui 	acpi_dev_get_resources(device, &resource_list, acpi_check_spi_i2c_slave,
167648459340SZhang Rui 			       &is_spi_i2c_slave);
167748459340SZhang Rui 	acpi_dev_free_resource_list(&resource_list);
167848459340SZhang Rui 	if (!is_spi_i2c_slave)
167948459340SZhang Rui 		acpi_create_platform_device(device);
168048459340SZhang Rui }
168148459340SZhang Rui 
16827d284352SRafael J. Wysocki static const struct acpi_device_id generic_device_ids[] = {
1683ee892094SRafael J. Wysocki 	{ACPI_DT_NAMESPACE_HID, },
16847d284352SRafael J. Wysocki 	{"", },
16857d284352SRafael J. Wysocki };
16867d284352SRafael J. Wysocki 
16877d284352SRafael J. Wysocki static int acpi_generic_device_attach(struct acpi_device *adev,
16887d284352SRafael J. Wysocki 				      const struct acpi_device_id *not_used)
16897d284352SRafael J. Wysocki {
16907d284352SRafael J. Wysocki 	/*
1691ee892094SRafael J. Wysocki 	 * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
1692ee892094SRafael J. Wysocki 	 * below can be unconditional.
16937d284352SRafael J. Wysocki 	 */
16947d284352SRafael J. Wysocki 	if (adev->data.of_compatible)
16957d284352SRafael J. Wysocki 		acpi_default_enumeration(adev);
16967d284352SRafael J. Wysocki 
16977d284352SRafael J. Wysocki 	return 1;
16987d284352SRafael J. Wysocki }
16997d284352SRafael J. Wysocki 
17007d284352SRafael J. Wysocki static struct acpi_scan_handler generic_device_handler = {
17017d284352SRafael J. Wysocki 	.ids = generic_device_ids,
17027d284352SRafael J. Wysocki 	.attach = acpi_generic_device_attach,
17037d284352SRafael J. Wysocki };
17047d284352SRafael J. Wysocki 
170587b85b3cSRafael J. Wysocki static int acpi_scan_attach_handler(struct acpi_device *device)
170687b85b3cSRafael J. Wysocki {
170787b85b3cSRafael J. Wysocki 	struct acpi_hardware_id *hwid;
170887b85b3cSRafael J. Wysocki 	int ret = 0;
170987b85b3cSRafael J. Wysocki 
171087b85b3cSRafael J. Wysocki 	list_for_each_entry(hwid, &device->pnp.ids, list) {
1711c5698074SRafael J. Wysocki 		const struct acpi_device_id *devid;
1712c5698074SRafael J. Wysocki 		struct acpi_scan_handler *handler;
171387b85b3cSRafael J. Wysocki 
1714c5698074SRafael J. Wysocki 		handler = acpi_scan_match_handler(hwid->id, &devid);
1715c5698074SRafael J. Wysocki 		if (handler) {
1716d34afa9dSRafael J. Wysocki 			if (!handler->attach) {
1717d34afa9dSRafael J. Wysocki 				device->pnp.type.platform_id = 0;
1718d34afa9dSRafael J. Wysocki 				continue;
1719d34afa9dSRafael J. Wysocki 			}
1720c5698074SRafael J. Wysocki 			device->handler = handler;
17219cb32acfSRafael J. Wysocki 			ret = handler->attach(device, devid);
17229cb32acfSRafael J. Wysocki 			if (ret > 0)
1723c5698074SRafael J. Wysocki 				break;
17249cb32acfSRafael J. Wysocki 
17259cb32acfSRafael J. Wysocki 			device->handler = NULL;
17269cb32acfSRafael J. Wysocki 			if (ret < 0)
1727c5698074SRafael J. Wysocki 				break;
1728c5698074SRafael J. Wysocki 		}
1729c5698074SRafael J. Wysocki 	}
173048459340SZhang Rui 
1731ca589f94SRafael J. Wysocki 	return ret;
1732ca589f94SRafael J. Wysocki }
1733ca589f94SRafael J. Wysocki 
17342c22e652SRafael J. Wysocki static void acpi_bus_attach(struct acpi_device *device)
173551a85fafSBjorn Helgaas {
17362c22e652SRafael J. Wysocki 	struct acpi_device *child;
17371e2380cdSRafael J. Wysocki 	acpi_handle ejd;
1738ca589f94SRafael J. Wysocki 	int ret;
17391da177e4SLinus Torvalds 
17401e2380cdSRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
17411e2380cdSRafael J. Wysocki 		register_dock_dependent_device(device, ejd);
17421e2380cdSRafael J. Wysocki 
17432c22e652SRafael J. Wysocki 	acpi_bus_get_status(device);
1744202317a5SRafael J. Wysocki 	/* Skip devices that are not present. */
17452c22e652SRafael J. Wysocki 	if (!acpi_device_is_present(device)) {
17462c22e652SRafael J. Wysocki 		device->flags.visited = false;
17471b1f3e16SRafael J. Wysocki 		device->flags.power_manageable = 0;
17482c22e652SRafael J. Wysocki 		return;
17492c22e652SRafael J. Wysocki 	}
17503a391a39SRafael J. Wysocki 	if (device->handler)
17512c22e652SRafael J. Wysocki 		goto ok;
17523a391a39SRafael J. Wysocki 
1753202317a5SRafael J. Wysocki 	if (!device->flags.initialized) {
17541b1f3e16SRafael J. Wysocki 		device->flags.power_manageable =
17551b1f3e16SRafael J. Wysocki 			device->power.states[ACPI_STATE_D0].flags.valid;
17561b1f3e16SRafael J. Wysocki 		if (acpi_bus_init_power(device))
17571b1f3e16SRafael J. Wysocki 			device->flags.power_manageable = 0;
17581b1f3e16SRafael J. Wysocki 
1759202317a5SRafael J. Wysocki 		device->flags.initialized = true;
1760202317a5SRafael J. Wysocki 	}
17612c22e652SRafael J. Wysocki 	device->flags.visited = false;
1762ca589f94SRafael J. Wysocki 	ret = acpi_scan_attach_handler(device);
17636931007cSRafael J. Wysocki 	if (ret < 0)
17642c22e652SRafael J. Wysocki 		return;
17656931007cSRafael J. Wysocki 
17666931007cSRafael J. Wysocki 	device->flags.match_driver = true;
17672c22e652SRafael J. Wysocki 	if (!ret) {
1768ca589f94SRafael J. Wysocki 		ret = device_attach(&device->dev);
1769202317a5SRafael J. Wysocki 		if (ret < 0)
17702c22e652SRafael J. Wysocki 			return;
17717d284352SRafael J. Wysocki 
17727d284352SRafael J. Wysocki 		if (!ret && device->pnp.type.platform_id)
17737d284352SRafael J. Wysocki 			acpi_default_enumeration(device);
17742c22e652SRafael J. Wysocki 	}
17752c22e652SRafael J. Wysocki 	device->flags.visited = true;
1776202317a5SRafael J. Wysocki 
1777202317a5SRafael J. Wysocki  ok:
17782c22e652SRafael J. Wysocki 	list_for_each_entry(child, &device->children, node)
17792c22e652SRafael J. Wysocki 		acpi_bus_attach(child);
17808ab17fc9SRafael J. Wysocki 
17818ab17fc9SRafael J. Wysocki 	if (device->handler && device->handler->hotplug.notify_online)
17828ab17fc9SRafael J. Wysocki 		device->handler->hotplug.notify_online(device);
1783805d410fSRafael J. Wysocki }
17843fb02738SRajesh Shah 
178540e7fcb1SLan Tianyu void acpi_walk_dep_device_list(acpi_handle handle)
178640e7fcb1SLan Tianyu {
178740e7fcb1SLan Tianyu 	struct acpi_dep_data *dep, *tmp;
178840e7fcb1SLan Tianyu 	struct acpi_device *adev;
178940e7fcb1SLan Tianyu 
179040e7fcb1SLan Tianyu 	mutex_lock(&acpi_dep_list_lock);
179140e7fcb1SLan Tianyu 	list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
179240e7fcb1SLan Tianyu 		if (dep->master == handle) {
179340e7fcb1SLan Tianyu 			acpi_bus_get_device(dep->slave, &adev);
179440e7fcb1SLan Tianyu 			if (!adev)
179540e7fcb1SLan Tianyu 				continue;
179640e7fcb1SLan Tianyu 
179740e7fcb1SLan Tianyu 			adev->dep_unmet--;
179840e7fcb1SLan Tianyu 			if (!adev->dep_unmet)
179940e7fcb1SLan Tianyu 				acpi_bus_attach(adev);
180040e7fcb1SLan Tianyu 			list_del(&dep->node);
180140e7fcb1SLan Tianyu 			kfree(dep);
180240e7fcb1SLan Tianyu 		}
180340e7fcb1SLan Tianyu 	}
180440e7fcb1SLan Tianyu 	mutex_unlock(&acpi_dep_list_lock);
180540e7fcb1SLan Tianyu }
180640e7fcb1SLan Tianyu EXPORT_SYMBOL_GPL(acpi_walk_dep_device_list);
180740e7fcb1SLan Tianyu 
1808b8bd759aSRafael J. Wysocki /**
1809b8bd759aSRafael J. Wysocki  * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1810b8bd759aSRafael J. Wysocki  * @handle: Root of the namespace scope to scan.
1811b8bd759aSRafael J. Wysocki  *
1812b8bd759aSRafael J. Wysocki  * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1813b8bd759aSRafael J. Wysocki  * found devices.
1814b8bd759aSRafael J. Wysocki  *
1815b8bd759aSRafael J. Wysocki  * If no devices were found, -ENODEV is returned, but it does not mean that
1816b8bd759aSRafael J. Wysocki  * there has been a real error.  There just have been no suitable ACPI objects
1817b8bd759aSRafael J. Wysocki  * in the table trunk from which the kernel could create a device and add an
1818b8bd759aSRafael J. Wysocki  * appropriate driver.
18193757b948SRafael J. Wysocki  *
18203757b948SRafael J. Wysocki  * Must be called under acpi_scan_lock.
1821b8bd759aSRafael J. Wysocki  */
1822b8bd759aSRafael J. Wysocki int acpi_bus_scan(acpi_handle handle)
18233fb02738SRajesh Shah {
18241da177e4SLinus Torvalds 	void *device = NULL;
1825c511cc19SRafael J. Wysocki 
18260cd6ac52SRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
18271da177e4SLinus Torvalds 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1828e3863094SRafael J. Wysocki 				    acpi_bus_check_add, NULL, NULL, &device);
18293fb02738SRajesh Shah 
18302c22e652SRafael J. Wysocki 	if (device) {
18312c22e652SRafael J. Wysocki 		acpi_bus_attach(device);
18322c22e652SRafael J. Wysocki 		return 0;
18332c22e652SRafael J. Wysocki 	}
18342c22e652SRafael J. Wysocki 	return -ENODEV;
18353fb02738SRajesh Shah }
1836b8bd759aSRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_scan);
18371da177e4SLinus Torvalds 
18383757b948SRafael J. Wysocki /**
18392c22e652SRafael J. Wysocki  * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
18402c22e652SRafael J. Wysocki  * @adev: Root of the ACPI namespace scope to walk.
18413757b948SRafael J. Wysocki  *
18423757b948SRafael J. Wysocki  * Must be called under acpi_scan_lock.
18433757b948SRafael J. Wysocki  */
18442c22e652SRafael J. Wysocki void acpi_bus_trim(struct acpi_device *adev)
18451da177e4SLinus Torvalds {
18462c22e652SRafael J. Wysocki 	struct acpi_scan_handler *handler = adev->handler;
18472c22e652SRafael J. Wysocki 	struct acpi_device *child;
18482c22e652SRafael J. Wysocki 
18492c22e652SRafael J. Wysocki 	list_for_each_entry_reverse(child, &adev->children, node)
18502c22e652SRafael J. Wysocki 		acpi_bus_trim(child);
18512c22e652SRafael J. Wysocki 
1852a951c773SRafael J. Wysocki 	adev->flags.match_driver = false;
18532c22e652SRafael J. Wysocki 	if (handler) {
18542c22e652SRafael J. Wysocki 		if (handler->detach)
18552c22e652SRafael J. Wysocki 			handler->detach(adev);
18562c22e652SRafael J. Wysocki 
18572c22e652SRafael J. Wysocki 		adev->handler = NULL;
18582c22e652SRafael J. Wysocki 	} else {
18592c22e652SRafael J. Wysocki 		device_release_driver(&adev->dev);
18602c22e652SRafael J. Wysocki 	}
18611da177e4SLinus Torvalds 	/*
18622c22e652SRafael J. Wysocki 	 * Most likely, the device is going away, so put it into D3cold before
18632c22e652SRafael J. Wysocki 	 * that.
18641da177e4SLinus Torvalds 	 */
18652c22e652SRafael J. Wysocki 	acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
18662c22e652SRafael J. Wysocki 	adev->flags.initialized = false;
18672c22e652SRafael J. Wysocki 	adev->flags.visited = false;
18681da177e4SLinus Torvalds }
1869ceaba663SKristen Accardi EXPORT_SYMBOL_GPL(acpi_bus_trim);
1870ceaba663SKristen Accardi 
1871e8b945c9SBjorn Helgaas static int acpi_bus_scan_fixed(void)
18721da177e4SLinus Torvalds {
18731da177e4SLinus Torvalds 	int result = 0;
1874c4168bffSLi Shaohua 
18751da177e4SLinus Torvalds 	/*
18761da177e4SLinus Torvalds 	 * Enumerate all fixed-feature devices.
18771da177e4SLinus Torvalds 	 */
18782c0d4fe0SRafael J. Wysocki 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
18792c0d4fe0SRafael J. Wysocki 		struct acpi_device *device = NULL;
18802c0d4fe0SRafael J. Wysocki 
18815c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
1882c4168bffSLi Shaohua 						ACPI_BUS_TYPE_POWER_BUTTON,
18832c0d4fe0SRafael J. Wysocki 						ACPI_STA_DEFAULT);
18842c0d4fe0SRafael J. Wysocki 		if (result)
18852c0d4fe0SRafael J. Wysocki 			return result;
18862c0d4fe0SRafael J. Wysocki 
188788346167SRafael J. Wysocki 		device->flags.match_driver = true;
18882c0d4fe0SRafael J. Wysocki 		result = device_attach(&device->dev);
18892c0d4fe0SRafael J. Wysocki 		if (result < 0)
18902c0d4fe0SRafael J. Wysocki 			return result;
18912c0d4fe0SRafael J. Wysocki 
1892c10d7a13SDaniel Drake 		device_init_wakeup(&device->dev, true);
18933fb02738SRajesh Shah 	}
18941da177e4SLinus Torvalds 
18952c0d4fe0SRafael J. Wysocki 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
18962c0d4fe0SRafael J. Wysocki 		struct acpi_device *device = NULL;
18972c0d4fe0SRafael J. Wysocki 
18985c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
1899c4168bffSLi Shaohua 						ACPI_BUS_TYPE_SLEEP_BUTTON,
19002c0d4fe0SRafael J. Wysocki 						ACPI_STA_DEFAULT);
19012c0d4fe0SRafael J. Wysocki 		if (result)
19022c0d4fe0SRafael J. Wysocki 			return result;
19032c0d4fe0SRafael J. Wysocki 
190488346167SRafael J. Wysocki 		device->flags.match_driver = true;
19052c0d4fe0SRafael J. Wysocki 		result = device_attach(&device->dev);
19063fb02738SRajesh Shah 	}
19071da177e4SLinus Torvalds 
19082c0d4fe0SRafael J. Wysocki 	return result < 0 ? result : 0;
19091da177e4SLinus Torvalds }
19101da177e4SLinus Torvalds 
1911e747f274SBjorn Helgaas int __init acpi_scan_init(void)
19121da177e4SLinus Torvalds {
19131da177e4SLinus Torvalds 	int result;
19141da177e4SLinus Torvalds 
191592ef2a25SRafael J. Wysocki 	acpi_pci_root_init();
19164daeaf68SRafael J. Wysocki 	acpi_pci_link_init();
1917ac212b69SRafael J. Wysocki 	acpi_processor_init();
1918f58b082aSRafael J. Wysocki 	acpi_lpss_init();
191992082a88SKen Xue 	acpi_apd_init();
19202fa97febSLan Tianyu 	acpi_cmos_rtc_init();
1921737f1a9fSRafael J. Wysocki 	acpi_container_init();
19220a347644SRafael J. Wysocki 	acpi_memory_hotplug_init();
1923eec15edbSZhang Rui 	acpi_pnp_init();
19243230bbfcSZhang Rui 	acpi_int340x_thermal_init();
192597d9a9e9SRafael J. Wysocki 
19267d284352SRafael J. Wysocki 	acpi_scan_add_handler(&generic_device_handler);
19277d284352SRafael J. Wysocki 
19283757b948SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
19291da177e4SLinus Torvalds 	/*
19301da177e4SLinus Torvalds 	 * Enumerate devices in the ACPI namespace.
19311da177e4SLinus Torvalds 	 */
19320cd6ac52SRafael J. Wysocki 	result = acpi_bus_scan(ACPI_ROOT_OBJECT);
19331da177e4SLinus Torvalds 	if (result)
19343757b948SRafael J. Wysocki 		goto out;
19351da177e4SLinus Torvalds 
19360cd6ac52SRafael J. Wysocki 	result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
19371da177e4SLinus Torvalds 	if (result)
19383757b948SRafael J. Wysocki 		goto out;
1939b8bd759aSRafael J. Wysocki 
19402650ef42SAaron Lu 	/* Fixed feature devices do not exist on HW-reduced platform */
19412650ef42SAaron Lu 	if (!acpi_gbl_reduced_hardware) {
1942b8bd759aSRafael J. Wysocki 		result = acpi_bus_scan_fixed();
1943b8bd759aSRafael J. Wysocki 		if (result) {
19442650ef42SAaron Lu 			acpi_detach_data(acpi_root->handle,
19452650ef42SAaron Lu 					 acpi_scan_drop_device);
1946202317a5SRafael J. Wysocki 			acpi_device_del(acpi_root);
1947202317a5SRafael J. Wysocki 			put_device(&acpi_root->dev);
19483757b948SRafael J. Wysocki 			goto out;
1949b8bd759aSRafael J. Wysocki 		}
19502650ef42SAaron Lu 	}
1951b8bd759aSRafael J. Wysocki 
1952b8bd759aSRafael J. Wysocki 	acpi_update_all_gpes();
19533757b948SRafael J. Wysocki 
19543757b948SRafael J. Wysocki  out:
19553757b948SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
19563757b948SRafael J. Wysocki 	return result;
19571da177e4SLinus Torvalds }
1958e647b532SMarc Zyngier 
1959e647b532SMarc Zyngier static struct acpi_probe_entry *ape;
1960e647b532SMarc Zyngier static int acpi_probe_count;
1961e647b532SMarc Zyngier static DEFINE_SPINLOCK(acpi_probe_lock);
1962e647b532SMarc Zyngier 
1963e647b532SMarc Zyngier static int __init acpi_match_madt(struct acpi_subtable_header *header,
1964e647b532SMarc Zyngier 				  const unsigned long end)
1965e647b532SMarc Zyngier {
1966e647b532SMarc Zyngier 	if (!ape->subtable_valid || ape->subtable_valid(header, ape))
1967e647b532SMarc Zyngier 		if (!ape->probe_subtbl(header, end))
1968e647b532SMarc Zyngier 			acpi_probe_count++;
1969e647b532SMarc Zyngier 
1970e647b532SMarc Zyngier 	return 0;
1971e647b532SMarc Zyngier }
1972e647b532SMarc Zyngier 
1973e647b532SMarc Zyngier int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
1974e647b532SMarc Zyngier {
1975e647b532SMarc Zyngier 	int count = 0;
1976e647b532SMarc Zyngier 
1977e647b532SMarc Zyngier 	if (acpi_disabled)
1978e647b532SMarc Zyngier 		return 0;
1979e647b532SMarc Zyngier 
1980e647b532SMarc Zyngier 	spin_lock(&acpi_probe_lock);
1981e647b532SMarc Zyngier 	for (ape = ap_head; nr; ape++, nr--) {
1982e647b532SMarc Zyngier 		if (ACPI_COMPARE_NAME(ACPI_SIG_MADT, ape->id)) {
1983e647b532SMarc Zyngier 			acpi_probe_count = 0;
1984e647b532SMarc Zyngier 			acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
1985e647b532SMarc Zyngier 			count += acpi_probe_count;
1986e647b532SMarc Zyngier 		} else {
1987e647b532SMarc Zyngier 			int res;
1988e647b532SMarc Zyngier 			res = acpi_table_parse(ape->id, ape->probe_table);
1989e647b532SMarc Zyngier 			if (!res)
1990e647b532SMarc Zyngier 				count++;
1991e647b532SMarc Zyngier 		}
1992e647b532SMarc Zyngier 	}
1993e647b532SMarc Zyngier 	spin_unlock(&acpi_probe_lock);
1994e647b532SMarc Zyngier 
1995e647b532SMarc Zyngier 	return count;
1996e647b532SMarc Zyngier }
1997