xref: /openbmc/linux/drivers/acpi/scan.c (revision be27b3dcb02335ec093b81053fc8c84b32d3106e)
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>
141da177e4SLinus Torvalds 
15d783156eSRafael J. Wysocki #include <asm/pgtable.h>
16d783156eSRafael J. Wysocki 
17e60cc7a6SBjorn Helgaas #include "internal.h"
18e60cc7a6SBjorn Helgaas 
191da177e4SLinus Torvalds #define _COMPONENT		ACPI_BUS_COMPONENT
20f52fd66dSLen Brown ACPI_MODULE_NAME("scan");
211da177e4SLinus Torvalds extern struct acpi_device *acpi_root;
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #define ACPI_BUS_CLASS			"system_bus"
2429b71a1cSThomas Renninger #define ACPI_BUS_HID			"LNXSYBUS"
251da177e4SLinus Torvalds #define ACPI_BUS_DEVICE_NAME		"System Bus"
261da177e4SLinus Torvalds 
27859ac9a4SBjorn Helgaas #define ACPI_IS_ROOT_DEVICE(device)    (!(device)->parent)
28859ac9a4SBjorn Helgaas 
29d783156eSRafael J. Wysocki #define INVALID_ACPI_HANDLE	((acpi_handle)empty_zero_page)
30d783156eSRafael J. Wysocki 
31683058e3SRafael J. Wysocki /*
32683058e3SRafael J. Wysocki  * If set, devices will be hot-removed even if they cannot be put offline
33683058e3SRafael J. Wysocki  * gracefully (from the kernel's standpoint).
34683058e3SRafael J. Wysocki  */
35683058e3SRafael J. Wysocki bool acpi_force_hot_remove;
36683058e3SRafael J. Wysocki 
37620e112cSThomas Renninger static const char *dummy_hid = "device";
382b2ae7c7SThomas Renninger 
39e49bd2ddSZhang Rui static LIST_HEAD(acpi_bus_id_list);
40c511cc19SRafael J. Wysocki static DEFINE_MUTEX(acpi_scan_lock);
41ca589f94SRafael J. Wysocki static LIST_HEAD(acpi_scan_handlers_list);
429090589dSShaohua Li DEFINE_MUTEX(acpi_device_lock);
431da177e4SLinus Torvalds LIST_HEAD(acpi_wakeup_device_list);
44e525506fSRafael J. Wysocki static DEFINE_MUTEX(acpi_hp_context_lock);
451da177e4SLinus Torvalds 
46e49bd2ddSZhang Rui struct acpi_device_bus_id{
47bb095854SZhang Rui 	char bus_id[15];
48e49bd2ddSZhang Rui 	unsigned int instance_no;
49e49bd2ddSZhang Rui 	struct list_head node;
50e49bd2ddSZhang Rui };
5129b71a1cSThomas Renninger 
523757b948SRafael J. Wysocki void acpi_scan_lock_acquire(void)
533757b948SRafael J. Wysocki {
543757b948SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
553757b948SRafael J. Wysocki }
563757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
573757b948SRafael J. Wysocki 
583757b948SRafael J. Wysocki void acpi_scan_lock_release(void)
593757b948SRafael J. Wysocki {
603757b948SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
613757b948SRafael J. Wysocki }
623757b948SRafael J. Wysocki EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
633757b948SRafael J. Wysocki 
64e525506fSRafael J. Wysocki void acpi_lock_hp_context(void)
65e525506fSRafael J. Wysocki {
66e525506fSRafael J. Wysocki 	mutex_lock(&acpi_hp_context_lock);
67e525506fSRafael J. Wysocki }
68e525506fSRafael J. Wysocki 
69e525506fSRafael J. Wysocki void acpi_unlock_hp_context(void)
70e525506fSRafael J. Wysocki {
71e525506fSRafael J. Wysocki 	mutex_unlock(&acpi_hp_context_lock);
72e525506fSRafael J. Wysocki }
73e525506fSRafael J. Wysocki 
74ca589f94SRafael J. Wysocki int acpi_scan_add_handler(struct acpi_scan_handler *handler)
75ca589f94SRafael J. Wysocki {
76ca589f94SRafael J. Wysocki 	if (!handler || !handler->attach)
77ca589f94SRafael J. Wysocki 		return -EINVAL;
78ca589f94SRafael J. Wysocki 
79ca589f94SRafael J. Wysocki 	list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
80ca589f94SRafael J. Wysocki 	return 0;
81ca589f94SRafael J. Wysocki }
82ca589f94SRafael J. Wysocki 
833f8055c3SRafael J. Wysocki int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
843f8055c3SRafael J. Wysocki 				       const char *hotplug_profile_name)
853f8055c3SRafael J. Wysocki {
863f8055c3SRafael J. Wysocki 	int error;
873f8055c3SRafael J. Wysocki 
883f8055c3SRafael J. Wysocki 	error = acpi_scan_add_handler(handler);
893f8055c3SRafael J. Wysocki 	if (error)
903f8055c3SRafael J. Wysocki 		return error;
913f8055c3SRafael J. Wysocki 
923f8055c3SRafael J. Wysocki 	acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
933f8055c3SRafael J. Wysocki 	return 0;
943f8055c3SRafael J. Wysocki }
953f8055c3SRafael J. Wysocki 
9629b71a1cSThomas Renninger /*
9729b71a1cSThomas Renninger  * Creates hid/cid(s) string needed for modalias and uevent
9829b71a1cSThomas Renninger  * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
9929b71a1cSThomas Renninger  * char *modalias: "acpi:IBM0001:ACPI0001"
1003d8e0090SZhang Rui  * Return: 0: no _HID and no _CID
1013d8e0090SZhang Rui  *         -EINVAL: output error
1023d8e0090SZhang Rui  *         -ENOMEM: output is truncated
10329b71a1cSThomas Renninger */
104b3e572d2SAdrian Bunk static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
105b3e572d2SAdrian Bunk 			   int size)
106b3e572d2SAdrian Bunk {
10729b71a1cSThomas Renninger 	int len;
1085c9fcb5dSZhang Rui 	int count;
1097f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id;
11029b71a1cSThomas Renninger 
1112b2ae7c7SThomas Renninger 	if (list_empty(&acpi_dev->pnp.ids))
1122b2ae7c7SThomas Renninger 		return 0;
1132b2ae7c7SThomas Renninger 
1145c9fcb5dSZhang Rui 	len = snprintf(modalias, size, "acpi:");
11529b71a1cSThomas Renninger 	size -= len;
11629b71a1cSThomas Renninger 
1177f47fa6cSBjorn Helgaas 	list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
1187f47fa6cSBjorn Helgaas 		count = snprintf(&modalias[len], size, "%s:", id->id);
1193d8e0090SZhang Rui 		if (count < 0)
1203d8e0090SZhang Rui 			return EINVAL;
1213d8e0090SZhang Rui 		if (count >= size)
1223d8e0090SZhang Rui 			return -ENOMEM;
1235c9fcb5dSZhang Rui 		len += count;
1245c9fcb5dSZhang Rui 		size -= count;
1255c9fcb5dSZhang Rui 	}
1265c9fcb5dSZhang Rui 
12729b71a1cSThomas Renninger 	modalias[len] = '\0';
12829b71a1cSThomas Renninger 	return len;
12929b71a1cSThomas Renninger }
13029b71a1cSThomas Renninger 
1316eb2451fSZhang Rui /*
1326eb2451fSZhang Rui  * Creates uevent modalias field for ACPI enumerated devices.
1336eb2451fSZhang Rui  * Because the other buses does not support ACPI HIDs & CIDs.
1346eb2451fSZhang Rui  * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
1356eb2451fSZhang Rui  * "acpi:IBM0001:ACPI0001"
1366eb2451fSZhang Rui  */
1376eb2451fSZhang Rui int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
1386eb2451fSZhang Rui {
1396eb2451fSZhang Rui 	struct acpi_device *acpi_dev;
1406eb2451fSZhang Rui 	int len;
1416eb2451fSZhang Rui 
1426eb2451fSZhang Rui 	acpi_dev = ACPI_COMPANION(dev);
1436eb2451fSZhang Rui 	if (!acpi_dev)
1446eb2451fSZhang Rui 		return -ENODEV;
1456eb2451fSZhang Rui 
1466eb2451fSZhang Rui 	/* Fall back to bus specific way of modalias exporting */
1476eb2451fSZhang Rui 	if (list_empty(&acpi_dev->pnp.ids))
1486eb2451fSZhang Rui 		return -ENODEV;
1496eb2451fSZhang Rui 
1506eb2451fSZhang Rui 	if (add_uevent_var(env, "MODALIAS="))
1516eb2451fSZhang Rui 		return -ENOMEM;
1526eb2451fSZhang Rui 	len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
1536eb2451fSZhang Rui 				sizeof(env->buf) - env->buflen);
1546eb2451fSZhang Rui 	if (len <= 0)
1556eb2451fSZhang Rui 		return len;
1566eb2451fSZhang Rui 	env->buflen += len;
1576eb2451fSZhang Rui 	return 0;
1586eb2451fSZhang Rui }
1596eb2451fSZhang Rui EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias);
1606eb2451fSZhang Rui 
1616eb2451fSZhang Rui /*
1626eb2451fSZhang Rui  * Creates modalias sysfs attribute for ACPI enumerated devices.
1636eb2451fSZhang Rui  * Because the other buses does not support ACPI HIDs & CIDs.
1646eb2451fSZhang Rui  * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
1656eb2451fSZhang Rui  * "acpi:IBM0001:ACPI0001"
1666eb2451fSZhang Rui  */
1676eb2451fSZhang Rui int acpi_device_modalias(struct device *dev, char *buf, int size)
1686eb2451fSZhang Rui {
1696eb2451fSZhang Rui 	struct acpi_device *acpi_dev;
1706eb2451fSZhang Rui 	int len;
1716eb2451fSZhang Rui 
1726eb2451fSZhang Rui 	acpi_dev = ACPI_COMPANION(dev);
1736eb2451fSZhang Rui 	if (!acpi_dev)
1746eb2451fSZhang Rui 		return -ENODEV;
1756eb2451fSZhang Rui 
1766eb2451fSZhang Rui 	/* Fall back to bus specific way of modalias exporting */
1776eb2451fSZhang Rui 	if (list_empty(&acpi_dev->pnp.ids))
1786eb2451fSZhang Rui 		return -ENODEV;
1796eb2451fSZhang Rui 
1806eb2451fSZhang Rui 	len = create_modalias(acpi_dev, buf, size -1);
1816eb2451fSZhang Rui 	if (len <= 0)
1826eb2451fSZhang Rui 		return len;
1836eb2451fSZhang Rui 	buf[len++] = '\n';
1846eb2451fSZhang Rui 	return len;
1856eb2451fSZhang Rui }
1866eb2451fSZhang Rui EXPORT_SYMBOL_GPL(acpi_device_modalias);
1876eb2451fSZhang Rui 
18829b71a1cSThomas Renninger static ssize_t
18929b71a1cSThomas Renninger acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
19029b71a1cSThomas Renninger 	struct acpi_device *acpi_dev = to_acpi_device(dev);
19129b71a1cSThomas Renninger 	int len;
19229b71a1cSThomas Renninger 
19329b71a1cSThomas Renninger 	len = create_modalias(acpi_dev, buf, 1024);
19429b71a1cSThomas Renninger 	if (len <= 0)
1953d8e0090SZhang Rui 		return len;
19629b71a1cSThomas Renninger 	buf[len++] = '\n';
19729b71a1cSThomas Renninger 	return len;
19829b71a1cSThomas Renninger }
19929b71a1cSThomas Renninger static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
20029b71a1cSThomas Renninger 
201caa73ea1SRafael J. Wysocki bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
202d22ddcbcSRafael J. Wysocki {
203d22ddcbcSRafael J. Wysocki 	struct acpi_device_physical_node *pn;
204d22ddcbcSRafael J. Wysocki 	bool offline = true;
205d22ddcbcSRafael J. Wysocki 
206d22ddcbcSRafael J. Wysocki 	mutex_lock(&adev->physical_node_lock);
207d22ddcbcSRafael J. Wysocki 
208d22ddcbcSRafael J. Wysocki 	list_for_each_entry(pn, &adev->physical_node_list, node)
209d22ddcbcSRafael J. Wysocki 		if (device_supports_offline(pn->dev) && !pn->dev->offline) {
210caa73ea1SRafael J. Wysocki 			if (uevent)
211d22ddcbcSRafael J. Wysocki 				kobject_uevent(&pn->dev->kobj, KOBJ_CHANGE);
212caa73ea1SRafael J. Wysocki 
213d22ddcbcSRafael J. Wysocki 			offline = false;
214d22ddcbcSRafael J. Wysocki 			break;
215d22ddcbcSRafael J. Wysocki 		}
216d22ddcbcSRafael J. Wysocki 
217d22ddcbcSRafael J. Wysocki 	mutex_unlock(&adev->physical_node_lock);
218d22ddcbcSRafael J. Wysocki 	return offline;
219d22ddcbcSRafael J. Wysocki }
220d22ddcbcSRafael J. Wysocki 
2217f28ddecSRafael J. Wysocki static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
2227f28ddecSRafael J. Wysocki 				    void **ret_p)
223683058e3SRafael J. Wysocki {
224683058e3SRafael J. Wysocki 	struct acpi_device *device = NULL;
225683058e3SRafael J. Wysocki 	struct acpi_device_physical_node *pn;
226303bfdb1SRafael J. Wysocki 	bool second_pass = (bool)data;
227683058e3SRafael J. Wysocki 	acpi_status status = AE_OK;
228683058e3SRafael J. Wysocki 
229683058e3SRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
230683058e3SRafael J. Wysocki 		return AE_OK;
231683058e3SRafael J. Wysocki 
2327f28ddecSRafael J. Wysocki 	if (device->handler && !device->handler->hotplug.enabled) {
2337f28ddecSRafael J. Wysocki 		*ret_p = &device->dev;
2347f28ddecSRafael J. Wysocki 		return AE_SUPPORT;
2357f28ddecSRafael J. Wysocki 	}
2367f28ddecSRafael J. Wysocki 
237683058e3SRafael J. Wysocki 	mutex_lock(&device->physical_node_lock);
238683058e3SRafael J. Wysocki 
239683058e3SRafael J. Wysocki 	list_for_each_entry(pn, &device->physical_node_list, node) {
240683058e3SRafael J. Wysocki 		int ret;
241683058e3SRafael J. Wysocki 
242303bfdb1SRafael J. Wysocki 		if (second_pass) {
243303bfdb1SRafael J. Wysocki 			/* Skip devices offlined by the first pass. */
244303bfdb1SRafael J. Wysocki 			if (pn->put_online)
245303bfdb1SRafael J. Wysocki 				continue;
246303bfdb1SRafael J. Wysocki 		} else {
247303bfdb1SRafael J. Wysocki 			pn->put_online = false;
248303bfdb1SRafael J. Wysocki 		}
249683058e3SRafael J. Wysocki 		ret = device_offline(pn->dev);
250683058e3SRafael J. Wysocki 		if (acpi_force_hot_remove)
251683058e3SRafael J. Wysocki 			continue;
252683058e3SRafael J. Wysocki 
253303bfdb1SRafael J. Wysocki 		if (ret >= 0) {
254303bfdb1SRafael J. Wysocki 			pn->put_online = !ret;
255303bfdb1SRafael J. Wysocki 		} else {
256303bfdb1SRafael J. Wysocki 			*ret_p = pn->dev;
257303bfdb1SRafael J. Wysocki 			if (second_pass) {
258683058e3SRafael J. Wysocki 				status = AE_ERROR;
259683058e3SRafael J. Wysocki 				break;
260683058e3SRafael J. Wysocki 			}
261303bfdb1SRafael J. Wysocki 		}
262683058e3SRafael J. Wysocki 	}
263683058e3SRafael J. Wysocki 
264683058e3SRafael J. Wysocki 	mutex_unlock(&device->physical_node_lock);
265683058e3SRafael J. Wysocki 
266683058e3SRafael J. Wysocki 	return status;
267683058e3SRafael J. Wysocki }
268683058e3SRafael J. Wysocki 
2697f28ddecSRafael J. Wysocki static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
2707f28ddecSRafael J. Wysocki 				   void **ret_p)
271683058e3SRafael J. Wysocki {
272683058e3SRafael J. Wysocki 	struct acpi_device *device = NULL;
273683058e3SRafael J. Wysocki 	struct acpi_device_physical_node *pn;
274683058e3SRafael J. Wysocki 
275683058e3SRafael J. Wysocki 	if (acpi_bus_get_device(handle, &device))
276683058e3SRafael J. Wysocki 		return AE_OK;
277683058e3SRafael J. Wysocki 
278683058e3SRafael J. Wysocki 	mutex_lock(&device->physical_node_lock);
279683058e3SRafael J. Wysocki 
280683058e3SRafael J. Wysocki 	list_for_each_entry(pn, &device->physical_node_list, node)
281683058e3SRafael J. Wysocki 		if (pn->put_online) {
282683058e3SRafael J. Wysocki 			device_online(pn->dev);
283683058e3SRafael J. Wysocki 			pn->put_online = false;
284683058e3SRafael J. Wysocki 		}
285683058e3SRafael J. Wysocki 
286683058e3SRafael J. Wysocki 	mutex_unlock(&device->physical_node_lock);
287683058e3SRafael J. Wysocki 
288683058e3SRafael J. Wysocki 	return AE_OK;
289683058e3SRafael J. Wysocki }
290683058e3SRafael J. Wysocki 
291d22ddcbcSRafael J. Wysocki static int acpi_scan_try_to_offline(struct acpi_device *device)
2921da177e4SLinus Torvalds {
2935993c467SYinghai Lu 	acpi_handle handle = device->handle;
294d22ddcbcSRafael J. Wysocki 	struct device *errdev = NULL;
295a33ec399SRafael J. Wysocki 	acpi_status status;
296f058cdf4SRafael J. Wysocki 
297303bfdb1SRafael J. Wysocki 	/*
298303bfdb1SRafael J. Wysocki 	 * Carry out two passes here and ignore errors in the first pass,
299303bfdb1SRafael J. Wysocki 	 * because if the devices in question are memory blocks and
300303bfdb1SRafael J. Wysocki 	 * CONFIG_MEMCG is set, one of the blocks may hold data structures
301303bfdb1SRafael J. Wysocki 	 * that the other blocks depend on, but it is not known in advance which
302303bfdb1SRafael J. Wysocki 	 * block holds them.
303303bfdb1SRafael J. Wysocki 	 *
304303bfdb1SRafael J. Wysocki 	 * If the first pass is successful, the second one isn't needed, though.
305303bfdb1SRafael J. Wysocki 	 */
3067f28ddecSRafael J. Wysocki 	status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
3077f28ddecSRafael J. Wysocki 				     NULL, acpi_bus_offline, (void *)false,
3087f28ddecSRafael J. Wysocki 				     (void **)&errdev);
3097f28ddecSRafael J. Wysocki 	if (status == AE_SUPPORT) {
3107f28ddecSRafael J. Wysocki 		dev_warn(errdev, "Offline disabled.\n");
311683058e3SRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
3127f28ddecSRafael J. Wysocki 				    acpi_bus_online, NULL, NULL, NULL);
3137f28ddecSRafael J. Wysocki 		return -EPERM;
3147f28ddecSRafael J. Wysocki 	}
3157f28ddecSRafael J. Wysocki 	acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
316303bfdb1SRafael J. Wysocki 	if (errdev) {
317303bfdb1SRafael J. Wysocki 		errdev = NULL;
318303bfdb1SRafael J. Wysocki 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
3197f28ddecSRafael J. Wysocki 				    NULL, acpi_bus_offline, (void *)true,
3207f28ddecSRafael J. Wysocki 				    (void **)&errdev);
321303bfdb1SRafael J. Wysocki 		if (!errdev || acpi_force_hot_remove)
3227f28ddecSRafael J. Wysocki 			acpi_bus_offline(handle, 0, (void *)true,
323303bfdb1SRafael J. Wysocki 					 (void **)&errdev);
324303bfdb1SRafael J. Wysocki 
325303bfdb1SRafael J. Wysocki 		if (errdev && !acpi_force_hot_remove) {
326303bfdb1SRafael J. Wysocki 			dev_warn(errdev, "Offline failed.\n");
3277f28ddecSRafael J. Wysocki 			acpi_bus_online(handle, 0, NULL, NULL);
328303bfdb1SRafael J. Wysocki 			acpi_walk_namespace(ACPI_TYPE_ANY, handle,
3297f28ddecSRafael J. Wysocki 					    ACPI_UINT32_MAX, acpi_bus_online,
3307f28ddecSRafael J. Wysocki 					    NULL, NULL, NULL);
331683058e3SRafael J. Wysocki 			return -EBUSY;
332683058e3SRafael J. Wysocki 		}
333303bfdb1SRafael J. Wysocki 	}
334d22ddcbcSRafael J. Wysocki 	return 0;
335d22ddcbcSRafael J. Wysocki }
336d22ddcbcSRafael J. Wysocki 
337d22ddcbcSRafael J. Wysocki static int acpi_scan_hot_remove(struct acpi_device *device)
338d22ddcbcSRafael J. Wysocki {
339d22ddcbcSRafael J. Wysocki 	acpi_handle handle = device->handle;
340d22ddcbcSRafael J. Wysocki 	unsigned long long sta;
341d22ddcbcSRafael J. Wysocki 	acpi_status status;
342d22ddcbcSRafael J. Wysocki 
343d22ddcbcSRafael J. Wysocki 	if (device->handler->hotplug.demand_offline && !acpi_force_hot_remove) {
344caa73ea1SRafael J. Wysocki 		if (!acpi_scan_is_offline(device, true))
345d22ddcbcSRafael J. Wysocki 			return -EBUSY;
346d22ddcbcSRafael J. Wysocki 	} else {
347d22ddcbcSRafael J. Wysocki 		int error = acpi_scan_try_to_offline(device);
348d22ddcbcSRafael J. Wysocki 		if (error)
349d22ddcbcSRafael J. Wysocki 			return error;
350d22ddcbcSRafael J. Wysocki 	}
351683058e3SRafael J. Wysocki 
35226d46867SZhang Rui 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
3530794469dSKay Sievers 		"Hot-removing device %s...\n", dev_name(&device->dev)));
35426d46867SZhang Rui 
355bfee26dbSRafael J. Wysocki 	acpi_bus_trim(device);
356683058e3SRafael J. Wysocki 
3577d2421f8SJiang Liu 	acpi_evaluate_lck(handle, 0);
3581da177e4SLinus Torvalds 	/*
3591da177e4SLinus Torvalds 	 * TBD: _EJD support.
3601da177e4SLinus Torvalds 	 */
3617d2421f8SJiang Liu 	status = acpi_evaluate_ej0(handle);
3627d2421f8SJiang Liu 	if (status == AE_NOT_FOUND)
363a33ec399SRafael J. Wysocki 		return -ENODEV;
3647d2421f8SJiang Liu 	else if (ACPI_FAILURE(status))
365a33ec399SRafael J. Wysocki 		return -EIO;
366f058cdf4SRafael J. Wysocki 
367882fd12eSToshi Kani 	/*
368882fd12eSToshi Kani 	 * Verify if eject was indeed successful.  If not, log an error
369882fd12eSToshi Kani 	 * message.  No need to call _OST since _EJ0 call was made OK.
370882fd12eSToshi Kani 	 */
371882fd12eSToshi Kani 	status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
372882fd12eSToshi Kani 	if (ACPI_FAILURE(status)) {
373882fd12eSToshi Kani 		acpi_handle_warn(handle,
374882fd12eSToshi Kani 			"Status check after eject failed (0x%x)\n", status);
375882fd12eSToshi Kani 	} else if (sta & ACPI_STA_DEVICE_ENABLED) {
376882fd12eSToshi Kani 		acpi_handle_warn(handle,
377882fd12eSToshi Kani 			"Eject incomplete - status 0x%llx\n", sta);
378882fd12eSToshi Kani 	}
379882fd12eSToshi Kani 
380a33ec399SRafael J. Wysocki 	return 0;
381a33ec399SRafael J. Wysocki }
382f058cdf4SRafael J. Wysocki 
383443fc820SRafael J. Wysocki static int acpi_scan_device_not_present(struct acpi_device *adev)
384443fc820SRafael J. Wysocki {
385443fc820SRafael J. Wysocki 	if (!acpi_device_enumerated(adev)) {
386443fc820SRafael J. Wysocki 		dev_warn(&adev->dev, "Still not present\n");
387443fc820SRafael J. Wysocki 		return -EALREADY;
388443fc820SRafael J. Wysocki 	}
389443fc820SRafael J. Wysocki 	acpi_bus_trim(adev);
390443fc820SRafael J. Wysocki 	return 0;
391443fc820SRafael J. Wysocki }
392443fc820SRafael J. Wysocki 
393c27b2c33SRafael J. Wysocki static int acpi_scan_device_check(struct acpi_device *adev)
394a33ec399SRafael J. Wysocki {
395f943db40SRafael J. Wysocki 	int error;
396a33ec399SRafael J. Wysocki 
397443fc820SRafael J. Wysocki 	acpi_bus_get_status(adev);
398443fc820SRafael J. Wysocki 	if (adev->status.present || adev->status.functional) {
399443fc820SRafael J. Wysocki 		/*
400443fc820SRafael J. Wysocki 		 * This function is only called for device objects for which
401443fc820SRafael J. Wysocki 		 * matching scan handlers exist.  The only situation in which
402443fc820SRafael J. Wysocki 		 * the scan handler is not attached to this device object yet
403443fc820SRafael J. Wysocki 		 * is when the device has just appeared (either it wasn't
404443fc820SRafael J. Wysocki 		 * present at all before or it was removed and then added
405443fc820SRafael J. Wysocki 		 * again).
406443fc820SRafael J. Wysocki 		 */
407443fc820SRafael J. Wysocki 		if (adev->handler) {
408443fc820SRafael J. Wysocki 			dev_warn(&adev->dev, "Already enumerated\n");
409443fc820SRafael J. Wysocki 			return -EALREADY;
410443fc820SRafael J. Wysocki 		}
411c27b2c33SRafael J. Wysocki 		error = acpi_bus_scan(adev->handle);
412a33ec399SRafael J. Wysocki 		if (error) {
413c27b2c33SRafael J. Wysocki 			dev_warn(&adev->dev, "Namespace scan failure\n");
414c27b2c33SRafael J. Wysocki 			return error;
415a33ec399SRafael J. Wysocki 		}
41646394fd0SRafael J. Wysocki 		if (!adev->handler) {
417c27b2c33SRafael J. Wysocki 			dev_warn(&adev->dev, "Enumeration failure\n");
418443fc820SRafael J. Wysocki 			error = -ENODEV;
419443fc820SRafael J. Wysocki 		}
420443fc820SRafael J. Wysocki 	} else {
421443fc820SRafael J. Wysocki 		error = acpi_scan_device_not_present(adev);
422443fc820SRafael J. Wysocki 	}
423443fc820SRafael J. Wysocki 	return error;
424443fc820SRafael J. Wysocki }
425443fc820SRafael J. Wysocki 
426443fc820SRafael J. Wysocki static int acpi_scan_bus_check(struct acpi_device *adev)
427443fc820SRafael J. Wysocki {
428443fc820SRafael J. Wysocki 	struct acpi_scan_handler *handler = adev->handler;
429443fc820SRafael J. Wysocki 	struct acpi_device *child;
430443fc820SRafael J. Wysocki 	int error;
431443fc820SRafael J. Wysocki 
432443fc820SRafael J. Wysocki 	acpi_bus_get_status(adev);
433443fc820SRafael J. Wysocki 	if (!(adev->status.present || adev->status.functional)) {
434443fc820SRafael J. Wysocki 		acpi_scan_device_not_present(adev);
435443fc820SRafael J. Wysocki 		return 0;
436443fc820SRafael J. Wysocki 	}
437443fc820SRafael J. Wysocki 	if (handler && handler->hotplug.scan_dependent)
438443fc820SRafael J. Wysocki 		return handler->hotplug.scan_dependent(adev);
439443fc820SRafael J. Wysocki 
440443fc820SRafael J. Wysocki 	error = acpi_bus_scan(adev->handle);
441443fc820SRafael J. Wysocki 	if (error) {
442443fc820SRafael J. Wysocki 		dev_warn(&adev->dev, "Namespace scan failure\n");
443443fc820SRafael J. Wysocki 		return error;
444443fc820SRafael J. Wysocki 	}
445443fc820SRafael J. Wysocki 	list_for_each_entry(child, &adev->children, node) {
446443fc820SRafael J. Wysocki 		error = acpi_scan_bus_check(child);
447443fc820SRafael J. Wysocki 		if (error)
448443fc820SRafael J. Wysocki 			return error;
449a33ec399SRafael J. Wysocki 	}
450c27b2c33SRafael J. Wysocki 	return 0;
451c27b2c33SRafael J. Wysocki }
452c27b2c33SRafael J. Wysocki 
4533c2cc7ffSRafael J. Wysocki static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
4543c2cc7ffSRafael J. Wysocki {
4553c2cc7ffSRafael J. Wysocki 	switch (type) {
4563c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_BUS_CHECK:
4573c2cc7ffSRafael J. Wysocki 		return acpi_scan_bus_check(adev);
4583c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_DEVICE_CHECK:
4593c2cc7ffSRafael J. Wysocki 		return acpi_scan_device_check(adev);
4603c2cc7ffSRafael J. Wysocki 	case ACPI_NOTIFY_EJECT_REQUEST:
4613c2cc7ffSRafael J. Wysocki 	case ACPI_OST_EC_OSPM_EJECT:
462dd2151beSRafael J. Wysocki 		if (adev->handler && !adev->handler->hotplug.enabled) {
463dd2151beSRafael J. Wysocki 			dev_info(&adev->dev, "Eject disabled\n");
464dd2151beSRafael J. Wysocki 			return -EPERM;
465dd2151beSRafael J. Wysocki 		}
466dd2151beSRafael J. Wysocki 		acpi_evaluate_hotplug_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
467dd2151beSRafael J. Wysocki 					  ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
4683c2cc7ffSRafael J. Wysocki 		return acpi_scan_hot_remove(adev);
4693c2cc7ffSRafael J. Wysocki 	}
4703c2cc7ffSRafael J. Wysocki 	return -EINVAL;
4713c2cc7ffSRafael J. Wysocki }
4723c2cc7ffSRafael J. Wysocki 
4731a699476SRafael J. Wysocki void acpi_device_hotplug(void *data, u32 src)
474c27b2c33SRafael J. Wysocki {
475c27b2c33SRafael J. Wysocki 	u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
476c27b2c33SRafael J. Wysocki 	struct acpi_device *adev = data;
4773c2cc7ffSRafael J. Wysocki 	int error = -ENODEV;
478c27b2c33SRafael J. Wysocki 
479c27b2c33SRafael J. Wysocki 	lock_device_hotplug();
480c27b2c33SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
481c27b2c33SRafael J. Wysocki 
482c27b2c33SRafael J. Wysocki 	/*
483c27b2c33SRafael J. Wysocki 	 * The device object's ACPI handle cannot become invalid as long as we
4843c2cc7ffSRafael J. Wysocki 	 * are holding acpi_scan_lock, but it might have become invalid before
485c27b2c33SRafael J. Wysocki 	 * that lock was acquired.
486c27b2c33SRafael J. Wysocki 	 */
487c27b2c33SRafael J. Wysocki 	if (adev->handle == INVALID_ACPI_HANDLE)
4883c2cc7ffSRafael J. Wysocki 		goto err_out;
489c27b2c33SRafael J. Wysocki 
4901e2380cdSRafael J. Wysocki 	if (adev->flags.is_dock_station) {
4911e2380cdSRafael J. Wysocki 		error = dock_notify(adev, src);
4921e2380cdSRafael J. Wysocki 	} else if (adev->flags.hotplug_notify) {
4933c2cc7ffSRafael J. Wysocki 		error = acpi_generic_hotplug_event(adev, src);
494dd2151beSRafael J. Wysocki 		if (error == -EPERM) {
495dd2151beSRafael J. Wysocki 			ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
496dd2151beSRafael J. Wysocki 			goto err_out;
497dd2151beSRafael J. Wysocki 		}
4983c2cc7ffSRafael J. Wysocki 	} else {
499*be27b3dcSRafael J. Wysocki 		int (*notify)(struct acpi_device *, u32);
5003c2cc7ffSRafael J. Wysocki 
5013c2cc7ffSRafael J. Wysocki 		acpi_lock_hp_context();
502*be27b3dcSRafael J. Wysocki 		notify = adev->hp ? adev->hp->notify : NULL;
5033c2cc7ffSRafael J. Wysocki 		acpi_unlock_hp_context();
5043c2cc7ffSRafael J. Wysocki 		/*
5053c2cc7ffSRafael J. Wysocki 		 * There may be additional notify handlers for device objects
5063c2cc7ffSRafael J. Wysocki 		 * without the .event() callback, so ignore them here.
5073c2cc7ffSRafael J. Wysocki 		 */
508*be27b3dcSRafael J. Wysocki 		if (notify)
509*be27b3dcSRafael J. Wysocki 			error = notify(adev, src);
5103c2cc7ffSRafael J. Wysocki 		else
5113c2cc7ffSRafael J. Wysocki 			goto out;
512c27b2c33SRafael J. Wysocki 	}
513c27b2c33SRafael J. Wysocki 	if (!error)
514a33ec399SRafael J. Wysocki 		ost_code = ACPI_OST_SC_SUCCESS;
515a33ec399SRafael J. Wysocki 
5163c2cc7ffSRafael J. Wysocki  err_out:
517c27b2c33SRafael J. Wysocki 	acpi_evaluate_hotplug_ost(adev->handle, src, ost_code, NULL);
5183c2cc7ffSRafael J. Wysocki 
5193c2cc7ffSRafael J. Wysocki  out:
52078ea4639SRafael J. Wysocki 	acpi_bus_put_acpi_device(adev);
521a33ec399SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
522e0ae8feeSRafael J. Wysocki 	unlock_device_hotplug();
523a33ec399SRafael J. Wysocki }
524a33ec399SRafael J. Wysocki 
525836aedb1SRafael J. Wysocki static ssize_t real_power_state_show(struct device *dev,
526836aedb1SRafael J. Wysocki 				     struct device_attribute *attr, char *buf)
527836aedb1SRafael J. Wysocki {
528836aedb1SRafael J. Wysocki 	struct acpi_device *adev = to_acpi_device(dev);
529836aedb1SRafael J. Wysocki 	int state;
530836aedb1SRafael J. Wysocki 	int ret;
531836aedb1SRafael J. Wysocki 
532836aedb1SRafael J. Wysocki 	ret = acpi_device_get_power(adev, &state);
533836aedb1SRafael J. Wysocki 	if (ret)
534836aedb1SRafael J. Wysocki 		return ret;
535836aedb1SRafael J. Wysocki 
536836aedb1SRafael J. Wysocki 	return sprintf(buf, "%s\n", acpi_power_state_string(state));
537836aedb1SRafael J. Wysocki }
538836aedb1SRafael J. Wysocki 
539836aedb1SRafael J. Wysocki static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
540836aedb1SRafael J. Wysocki 
541836aedb1SRafael J. Wysocki static ssize_t power_state_show(struct device *dev,
542836aedb1SRafael J. Wysocki 				struct device_attribute *attr, char *buf)
543836aedb1SRafael J. Wysocki {
544836aedb1SRafael J. Wysocki 	struct acpi_device *adev = to_acpi_device(dev);
545836aedb1SRafael J. Wysocki 
546836aedb1SRafael J. Wysocki 	return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
547836aedb1SRafael J. Wysocki }
548836aedb1SRafael J. Wysocki 
549836aedb1SRafael J. Wysocki static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
550836aedb1SRafael J. Wysocki 
5511da177e4SLinus Torvalds static ssize_t
552f883d9dbSPatrick Mochel acpi_eject_store(struct device *d, struct device_attribute *attr,
553f883d9dbSPatrick Mochel 		const char *buf, size_t count)
5541da177e4SLinus Torvalds {
555f883d9dbSPatrick Mochel 	struct acpi_device *acpi_device = to_acpi_device(d);
556a33ec399SRafael J. Wysocki 	acpi_object_type not_used;
557a33ec399SRafael J. Wysocki 	acpi_status status;
5581da177e4SLinus Torvalds 
559a33ec399SRafael J. Wysocki 	if (!count || buf[0] != '1')
5601da177e4SLinus Torvalds 		return -EINVAL;
5611da177e4SLinus Torvalds 
562a33ec399SRafael J. Wysocki 	if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
563a33ec399SRafael J. Wysocki 	    && !acpi_device->driver)
564a33ec399SRafael J. Wysocki 		return -ENODEV;
565a33ec399SRafael J. Wysocki 
566a33ec399SRafael J. Wysocki 	status = acpi_get_type(acpi_device->handle, &not_used);
567a33ec399SRafael J. Wysocki 	if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
568a33ec399SRafael J. Wysocki 		return -ENODEV;
569a33ec399SRafael J. Wysocki 
570a33ec399SRafael J. Wysocki 	get_device(&acpi_device->dev);
571c27b2c33SRafael J. Wysocki 	status = acpi_hotplug_execute(acpi_device_hotplug, acpi_device,
5727b98118aSRafael J. Wysocki 				      ACPI_OST_EC_OSPM_EJECT);
573f943db40SRafael J. Wysocki 	if (ACPI_SUCCESS(status))
574f943db40SRafael J. Wysocki 		return count;
575f943db40SRafael J. Wysocki 
5763757b948SRafael J. Wysocki 	put_device(&acpi_device->dev);
577f943db40SRafael J. Wysocki 	acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
578a33ec399SRafael J. Wysocki 				  ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
5795add99cfSRafael J. Wysocki 	return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
5801da177e4SLinus Torvalds }
5811da177e4SLinus Torvalds 
582f883d9dbSPatrick Mochel static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
583f883d9dbSPatrick Mochel 
584e49bd2ddSZhang Rui static ssize_t
585e49bd2ddSZhang Rui acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
586e49bd2ddSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
587e49bd2ddSZhang Rui 
588ea8d82fdSBjorn Helgaas 	return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
589e49bd2ddSZhang Rui }
590e49bd2ddSZhang Rui static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
591e49bd2ddSZhang Rui 
592923d4a45SLv Zheng static ssize_t acpi_device_uid_show(struct device *dev,
593923d4a45SLv Zheng 				    struct device_attribute *attr, char *buf)
594923d4a45SLv Zheng {
595923d4a45SLv Zheng 	struct acpi_device *acpi_dev = to_acpi_device(dev);
596923d4a45SLv Zheng 
597923d4a45SLv Zheng 	return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
598923d4a45SLv Zheng }
599923d4a45SLv Zheng static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
600923d4a45SLv Zheng 
601923d4a45SLv Zheng static ssize_t acpi_device_adr_show(struct device *dev,
602923d4a45SLv Zheng 				    struct device_attribute *attr, char *buf)
603923d4a45SLv Zheng {
604923d4a45SLv Zheng 	struct acpi_device *acpi_dev = to_acpi_device(dev);
605923d4a45SLv Zheng 
606923d4a45SLv Zheng 	return sprintf(buf, "0x%08x\n",
607923d4a45SLv Zheng 		       (unsigned int)(acpi_dev->pnp.bus_address));
608923d4a45SLv Zheng }
609923d4a45SLv Zheng static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
610923d4a45SLv Zheng 
611e49bd2ddSZhang Rui static ssize_t
612e49bd2ddSZhang Rui acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
613e49bd2ddSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
614e49bd2ddSZhang Rui 	struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
615e49bd2ddSZhang Rui 	int result;
616e49bd2ddSZhang Rui 
617e49bd2ddSZhang Rui 	result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
618e49bd2ddSZhang Rui 	if (result)
619e49bd2ddSZhang Rui 		goto end;
620e49bd2ddSZhang Rui 
621e49bd2ddSZhang Rui 	result = sprintf(buf, "%s\n", (char*)path.pointer);
622e49bd2ddSZhang Rui 	kfree(path.pointer);
623e49bd2ddSZhang Rui end:
624e49bd2ddSZhang Rui 	return result;
625e49bd2ddSZhang Rui }
626e49bd2ddSZhang Rui static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
627e49bd2ddSZhang Rui 
628d1efe3c3SLance Ortiz /* sysfs file that shows description text from the ACPI _STR method */
629d1efe3c3SLance Ortiz static ssize_t description_show(struct device *dev,
630d1efe3c3SLance Ortiz 				struct device_attribute *attr,
631d1efe3c3SLance Ortiz 				char *buf) {
632d1efe3c3SLance Ortiz 	struct acpi_device *acpi_dev = to_acpi_device(dev);
633d1efe3c3SLance Ortiz 	int result;
634d1efe3c3SLance Ortiz 
635d1efe3c3SLance Ortiz 	if (acpi_dev->pnp.str_obj == NULL)
636d1efe3c3SLance Ortiz 		return 0;
637d1efe3c3SLance Ortiz 
638d1efe3c3SLance Ortiz 	/*
639d1efe3c3SLance Ortiz 	 * The _STR object contains a Unicode identifier for a device.
640d1efe3c3SLance Ortiz 	 * We need to convert to utf-8 so it can be displayed.
641d1efe3c3SLance Ortiz 	 */
642d1efe3c3SLance Ortiz 	result = utf16s_to_utf8s(
643d1efe3c3SLance Ortiz 		(wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
644d1efe3c3SLance Ortiz 		acpi_dev->pnp.str_obj->buffer.length,
645d1efe3c3SLance Ortiz 		UTF16_LITTLE_ENDIAN, buf,
646d1efe3c3SLance Ortiz 		PAGE_SIZE);
647d1efe3c3SLance Ortiz 
648d1efe3c3SLance Ortiz 	buf[result++] = '\n';
649d1efe3c3SLance Ortiz 
650d1efe3c3SLance Ortiz 	return result;
651d1efe3c3SLance Ortiz }
652d1efe3c3SLance Ortiz static DEVICE_ATTR(description, 0444, description_show, NULL);
653d1efe3c3SLance Ortiz 
654bb74ac23SYasuaki Ishimatsu static ssize_t
655bb74ac23SYasuaki Ishimatsu acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
656bb74ac23SYasuaki Ishimatsu 		     char *buf) {
657bb74ac23SYasuaki Ishimatsu 	struct acpi_device *acpi_dev = to_acpi_device(dev);
658bb74ac23SYasuaki Ishimatsu 
659bb74ac23SYasuaki Ishimatsu 	return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
660bb74ac23SYasuaki Ishimatsu }
661bb74ac23SYasuaki Ishimatsu static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
662bb74ac23SYasuaki Ishimatsu 
663c713cd7fSSrinivas Pandruvada static ssize_t status_show(struct device *dev, struct device_attribute *attr,
664c713cd7fSSrinivas Pandruvada 				char *buf) {
665c713cd7fSSrinivas Pandruvada 	struct acpi_device *acpi_dev = to_acpi_device(dev);
666c713cd7fSSrinivas Pandruvada 	acpi_status status;
667c713cd7fSSrinivas Pandruvada 	unsigned long long sta;
668c713cd7fSSrinivas Pandruvada 
669c713cd7fSSrinivas Pandruvada 	status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta);
670c713cd7fSSrinivas Pandruvada 	if (ACPI_FAILURE(status))
671c713cd7fSSrinivas Pandruvada 		return -ENODEV;
672c713cd7fSSrinivas Pandruvada 
673c713cd7fSSrinivas Pandruvada 	return sprintf(buf, "%llu\n", sta);
674c713cd7fSSrinivas Pandruvada }
675c713cd7fSSrinivas Pandruvada static DEVICE_ATTR_RO(status);
676c713cd7fSSrinivas Pandruvada 
677e49bd2ddSZhang Rui static int acpi_device_setup_files(struct acpi_device *dev)
678f883d9dbSPatrick Mochel {
679d1efe3c3SLance Ortiz 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
680f883d9dbSPatrick Mochel 	acpi_status status;
681bb74ac23SYasuaki Ishimatsu 	unsigned long long sun;
682e49bd2ddSZhang Rui 	int result = 0;
683e49bd2ddSZhang Rui 
684e49bd2ddSZhang Rui 	/*
685e49bd2ddSZhang Rui 	 * Devices gotten from FADT don't have a "path" attribute
686e49bd2ddSZhang Rui 	 */
687e49bd2ddSZhang Rui 	if (dev->handle) {
688e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_path);
689e49bd2ddSZhang Rui 		if (result)
690e49bd2ddSZhang Rui 			goto end;
691e49bd2ddSZhang Rui 	}
692e49bd2ddSZhang Rui 
6932b2ae7c7SThomas Renninger 	if (!list_empty(&dev->pnp.ids)) {
694e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_hid);
695e49bd2ddSZhang Rui 		if (result)
696e49bd2ddSZhang Rui 			goto end;
697f883d9dbSPatrick Mochel 
69829b71a1cSThomas Renninger 		result = device_create_file(&dev->dev, &dev_attr_modalias);
69929b71a1cSThomas Renninger 		if (result)
70029b71a1cSThomas Renninger 			goto end;
7012b2ae7c7SThomas Renninger 	}
70229b71a1cSThomas Renninger 
703f883d9dbSPatrick Mochel 	/*
704d1efe3c3SLance Ortiz 	 * If device has _STR, 'description' file is created
705d1efe3c3SLance Ortiz 	 */
706952c63e9SJiang Liu 	if (acpi_has_method(dev->handle, "_STR")) {
707d1efe3c3SLance Ortiz 		status = acpi_evaluate_object(dev->handle, "_STR",
708d1efe3c3SLance Ortiz 					NULL, &buffer);
709d1efe3c3SLance Ortiz 		if (ACPI_FAILURE(status))
710d1efe3c3SLance Ortiz 			buffer.pointer = NULL;
711d1efe3c3SLance Ortiz 		dev->pnp.str_obj = buffer.pointer;
712d1efe3c3SLance Ortiz 		result = device_create_file(&dev->dev, &dev_attr_description);
713d1efe3c3SLance Ortiz 		if (result)
714d1efe3c3SLance Ortiz 			goto end;
715d1efe3c3SLance Ortiz 	}
716d1efe3c3SLance Ortiz 
717d4e1a692SToshi Kani 	if (dev->pnp.type.bus_address)
718923d4a45SLv Zheng 		result = device_create_file(&dev->dev, &dev_attr_adr);
719923d4a45SLv Zheng 	if (dev->pnp.unique_id)
720923d4a45SLv Zheng 		result = device_create_file(&dev->dev, &dev_attr_uid);
721923d4a45SLv Zheng 
722bb74ac23SYasuaki Ishimatsu 	status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
723bb74ac23SYasuaki Ishimatsu 	if (ACPI_SUCCESS(status)) {
724bb74ac23SYasuaki Ishimatsu 		dev->pnp.sun = (unsigned long)sun;
725bb74ac23SYasuaki Ishimatsu 		result = device_create_file(&dev->dev, &dev_attr_sun);
726bb74ac23SYasuaki Ishimatsu 		if (result)
727bb74ac23SYasuaki Ishimatsu 			goto end;
728bb74ac23SYasuaki Ishimatsu 	} else {
729bb74ac23SYasuaki Ishimatsu 		dev->pnp.sun = (unsigned long)-1;
730bb74ac23SYasuaki Ishimatsu 	}
731bb74ac23SYasuaki Ishimatsu 
732c713cd7fSSrinivas Pandruvada 	if (acpi_has_method(dev->handle, "_STA")) {
733c713cd7fSSrinivas Pandruvada 		result = device_create_file(&dev->dev, &dev_attr_status);
734c713cd7fSSrinivas Pandruvada 		if (result)
735c713cd7fSSrinivas Pandruvada 			goto end;
736c713cd7fSSrinivas Pandruvada 	}
737c713cd7fSSrinivas Pandruvada 
738d1efe3c3SLance Ortiz         /*
739f883d9dbSPatrick Mochel          * If device has _EJ0, 'eject' file is created that is used to trigger
740f883d9dbSPatrick Mochel          * hot-removal function from userland.
741f883d9dbSPatrick Mochel          */
742952c63e9SJiang Liu 	if (acpi_has_method(dev->handle, "_EJ0")) {
743e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_eject);
744836aedb1SRafael J. Wysocki 		if (result)
745836aedb1SRafael J. Wysocki 			return result;
746836aedb1SRafael J. Wysocki 	}
747836aedb1SRafael J. Wysocki 
748836aedb1SRafael J. Wysocki 	if (dev->flags.power_manageable) {
749836aedb1SRafael J. Wysocki 		result = device_create_file(&dev->dev, &dev_attr_power_state);
750836aedb1SRafael J. Wysocki 		if (result)
751836aedb1SRafael J. Wysocki 			return result;
752836aedb1SRafael J. Wysocki 
753836aedb1SRafael J. Wysocki 		if (dev->power.flags.power_resources)
754836aedb1SRafael J. Wysocki 			result = device_create_file(&dev->dev,
755836aedb1SRafael J. Wysocki 						    &dev_attr_real_power_state);
756836aedb1SRafael J. Wysocki 	}
757836aedb1SRafael J. Wysocki 
758e49bd2ddSZhang Rui end:
759e49bd2ddSZhang Rui 	return result;
760f883d9dbSPatrick Mochel }
761f883d9dbSPatrick Mochel 
762f883d9dbSPatrick Mochel static void acpi_device_remove_files(struct acpi_device *dev)
763f883d9dbSPatrick Mochel {
764836aedb1SRafael J. Wysocki 	if (dev->flags.power_manageable) {
765836aedb1SRafael J. Wysocki 		device_remove_file(&dev->dev, &dev_attr_power_state);
766836aedb1SRafael J. Wysocki 		if (dev->power.flags.power_resources)
767836aedb1SRafael J. Wysocki 			device_remove_file(&dev->dev,
768836aedb1SRafael J. Wysocki 					   &dev_attr_real_power_state);
769836aedb1SRafael J. Wysocki 	}
770836aedb1SRafael J. Wysocki 
771f883d9dbSPatrick Mochel 	/*
772d1efe3c3SLance Ortiz 	 * If device has _STR, remove 'description' file
773d1efe3c3SLance Ortiz 	 */
774952c63e9SJiang Liu 	if (acpi_has_method(dev->handle, "_STR")) {
775d1efe3c3SLance Ortiz 		kfree(dev->pnp.str_obj);
776d1efe3c3SLance Ortiz 		device_remove_file(&dev->dev, &dev_attr_description);
777d1efe3c3SLance Ortiz 	}
778d1efe3c3SLance Ortiz 	/*
779d1efe3c3SLance Ortiz 	 * If device has _EJ0, remove 'eject' file.
780f883d9dbSPatrick Mochel 	 */
781952c63e9SJiang Liu 	if (acpi_has_method(dev->handle, "_EJ0"))
782f883d9dbSPatrick Mochel 		device_remove_file(&dev->dev, &dev_attr_eject);
783e49bd2ddSZhang Rui 
784952c63e9SJiang Liu 	if (acpi_has_method(dev->handle, "_SUN"))
785bb74ac23SYasuaki Ishimatsu 		device_remove_file(&dev->dev, &dev_attr_sun);
786bb74ac23SYasuaki Ishimatsu 
787923d4a45SLv Zheng 	if (dev->pnp.unique_id)
788923d4a45SLv Zheng 		device_remove_file(&dev->dev, &dev_attr_uid);
789d4e1a692SToshi Kani 	if (dev->pnp.type.bus_address)
790923d4a45SLv Zheng 		device_remove_file(&dev->dev, &dev_attr_adr);
79129b71a1cSThomas Renninger 	device_remove_file(&dev->dev, &dev_attr_modalias);
792e49bd2ddSZhang Rui 	device_remove_file(&dev->dev, &dev_attr_hid);
793c713cd7fSSrinivas Pandruvada 	if (acpi_has_method(dev->handle, "_STA"))
794c713cd7fSSrinivas Pandruvada 		device_remove_file(&dev->dev, &dev_attr_status);
795e49bd2ddSZhang Rui 	if (dev->handle)
796e49bd2ddSZhang Rui 		device_remove_file(&dev->dev, &dev_attr_path);
797f883d9dbSPatrick Mochel }
7981da177e4SLinus Torvalds /* --------------------------------------------------------------------------
7999e89dde2SZhang Rui 			ACPI Bus operations
8001da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
80129b71a1cSThomas Renninger 
802cf761af9SMika Westerberg static const struct acpi_device_id *__acpi_match_device(
803cf761af9SMika Westerberg 	struct acpi_device *device, const struct acpi_device_id *ids)
80429b71a1cSThomas Renninger {
80529b71a1cSThomas Renninger 	const struct acpi_device_id *id;
8067f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *hwid;
80729b71a1cSThomas Renninger 
80839a0ad87SZhao Yakui 	/*
80939a0ad87SZhao Yakui 	 * If the device is not present, it is unnecessary to load device
81039a0ad87SZhao Yakui 	 * driver for it.
81139a0ad87SZhao Yakui 	 */
81239a0ad87SZhao Yakui 	if (!device->status.present)
813cf761af9SMika Westerberg 		return NULL;
81439a0ad87SZhao Yakui 
8157f47fa6cSBjorn Helgaas 	for (id = ids; id->id[0]; id++)
8167f47fa6cSBjorn Helgaas 		list_for_each_entry(hwid, &device->pnp.ids, list)
8177f47fa6cSBjorn Helgaas 			if (!strcmp((char *) id->id, hwid->id))
818cf761af9SMika Westerberg 				return id;
81929b71a1cSThomas Renninger 
820cf761af9SMika Westerberg 	return NULL;
821cf761af9SMika Westerberg }
822cf761af9SMika Westerberg 
823cf761af9SMika Westerberg /**
824cf761af9SMika Westerberg  * acpi_match_device - Match a struct device against a given list of ACPI IDs
825cf761af9SMika Westerberg  * @ids: Array of struct acpi_device_id object to match against.
826cf761af9SMika Westerberg  * @dev: The device structure to match.
827cf761af9SMika Westerberg  *
828cf761af9SMika Westerberg  * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
829cf761af9SMika Westerberg  * object for that handle and use that object to match against a given list of
830cf761af9SMika Westerberg  * device IDs.
831cf761af9SMika Westerberg  *
832cf761af9SMika Westerberg  * Return a pointer to the first matching ID on success or %NULL on failure.
833cf761af9SMika Westerberg  */
834cf761af9SMika Westerberg const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
835cf761af9SMika Westerberg 					       const struct device *dev)
836cf761af9SMika Westerberg {
837cf761af9SMika Westerberg 	struct acpi_device *adev;
8380613e1f7SRafael J. Wysocki 	acpi_handle handle = ACPI_HANDLE(dev);
839cf761af9SMika Westerberg 
8400613e1f7SRafael J. Wysocki 	if (!ids || !handle || acpi_bus_get_device(handle, &adev))
841cf761af9SMika Westerberg 		return NULL;
842cf761af9SMika Westerberg 
843cf761af9SMika Westerberg 	return __acpi_match_device(adev, ids);
844cf761af9SMika Westerberg }
845cf761af9SMika Westerberg EXPORT_SYMBOL_GPL(acpi_match_device);
846cf761af9SMika Westerberg 
847cf761af9SMika Westerberg int acpi_match_device_ids(struct acpi_device *device,
848cf761af9SMika Westerberg 			  const struct acpi_device_id *ids)
849cf761af9SMika Westerberg {
850cf761af9SMika Westerberg 	return __acpi_match_device(device, ids) ? 0 : -ENOENT;
85129b71a1cSThomas Renninger }
85229b71a1cSThomas Renninger EXPORT_SYMBOL(acpi_match_device_ids);
85329b71a1cSThomas Renninger 
8540b224527SRafael J. Wysocki static void acpi_free_power_resources_lists(struct acpi_device *device)
8550b224527SRafael J. Wysocki {
8560b224527SRafael J. Wysocki 	int i;
8570b224527SRafael J. Wysocki 
858993cbe59SRafael J. Wysocki 	if (device->wakeup.flags.valid)
859993cbe59SRafael J. Wysocki 		acpi_power_resources_list_free(&device->wakeup.resources);
860993cbe59SRafael J. Wysocki 
8610b224527SRafael J. Wysocki 	if (!device->flags.power_manageable)
8620b224527SRafael J. Wysocki 		return;
8630b224527SRafael J. Wysocki 
8640b224527SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
8650b224527SRafael J. Wysocki 		struct acpi_device_power_state *ps = &device->power.states[i];
8660b224527SRafael J. Wysocki 		acpi_power_resources_list_free(&ps->resources);
8670b224527SRafael J. Wysocki 	}
8687f47fa6cSBjorn Helgaas }
8697f47fa6cSBjorn Helgaas 
8701890a97aSPatrick Mochel static void acpi_device_release(struct device *dev)
8711890a97aSPatrick Mochel {
8721890a97aSPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
8731890a97aSPatrick Mochel 
874c0af4175SToshi Kani 	acpi_free_pnp_ids(&acpi_dev->pnp);
8750b224527SRafael J. Wysocki 	acpi_free_power_resources_lists(acpi_dev);
8761890a97aSPatrick Mochel 	kfree(acpi_dev);
8771890a97aSPatrick Mochel }
8781890a97aSPatrick Mochel 
8795d9464a4SPatrick Mochel static int acpi_bus_match(struct device *dev, struct device_driver *drv)
8801da177e4SLinus Torvalds {
8815d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
8825d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(drv);
8835d9464a4SPatrick Mochel 
884209d3b17SRafael J. Wysocki 	return acpi_dev->flags.match_driver
885805d410fSRafael J. Wysocki 		&& !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
8865d9464a4SPatrick Mochel }
8875d9464a4SPatrick Mochel 
8887eff2e7aSKay Sievers static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
8895d9464a4SPatrick Mochel {
8905d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
8917eff2e7aSKay Sievers 	int len;
8925d9464a4SPatrick Mochel 
8932b2ae7c7SThomas Renninger 	if (list_empty(&acpi_dev->pnp.ids))
8942b2ae7c7SThomas Renninger 		return 0;
8952b2ae7c7SThomas Renninger 
8967eff2e7aSKay Sievers 	if (add_uevent_var(env, "MODALIAS="))
8977eff2e7aSKay Sievers 		return -ENOMEM;
8987eff2e7aSKay Sievers 	len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
8997eff2e7aSKay Sievers 			      sizeof(env->buf) - env->buflen);
9003d8e0090SZhang Rui 	if (len <= 0)
9013d8e0090SZhang Rui 		return len;
9027eff2e7aSKay Sievers 	env->buflen += len;
9031da177e4SLinus Torvalds 	return 0;
9041da177e4SLinus Torvalds }
9051da177e4SLinus Torvalds 
90646ec8598SBjorn Helgaas static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
90746ec8598SBjorn Helgaas {
90846ec8598SBjorn Helgaas 	struct acpi_device *device = data;
90946ec8598SBjorn Helgaas 
91046ec8598SBjorn Helgaas 	device->driver->ops.notify(device, event);
91146ec8598SBjorn Helgaas }
91246ec8598SBjorn Helgaas 
91346ec8598SBjorn Helgaas static acpi_status acpi_device_notify_fixed(void *data)
91446ec8598SBjorn Helgaas {
91546ec8598SBjorn Helgaas 	struct acpi_device *device = data;
91646ec8598SBjorn Helgaas 
91753de5356SBjorn Helgaas 	/* Fixed hardware devices have no handles */
91853de5356SBjorn Helgaas 	acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
91946ec8598SBjorn Helgaas 	return AE_OK;
92046ec8598SBjorn Helgaas }
92146ec8598SBjorn Helgaas 
92246ec8598SBjorn Helgaas static int acpi_device_install_notify_handler(struct acpi_device *device)
92346ec8598SBjorn Helgaas {
92446ec8598SBjorn Helgaas 	acpi_status status;
92546ec8598SBjorn Helgaas 
926ccba2a36SBjorn Helgaas 	if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
92746ec8598SBjorn Helgaas 		status =
92846ec8598SBjorn Helgaas 		    acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
92946ec8598SBjorn Helgaas 						     acpi_device_notify_fixed,
93046ec8598SBjorn Helgaas 						     device);
931ccba2a36SBjorn Helgaas 	else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
93246ec8598SBjorn Helgaas 		status =
93346ec8598SBjorn Helgaas 		    acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
93446ec8598SBjorn Helgaas 						     acpi_device_notify_fixed,
93546ec8598SBjorn Helgaas 						     device);
93646ec8598SBjorn Helgaas 	else
93746ec8598SBjorn Helgaas 		status = acpi_install_notify_handler(device->handle,
93846ec8598SBjorn Helgaas 						     ACPI_DEVICE_NOTIFY,
93946ec8598SBjorn Helgaas 						     acpi_device_notify,
94046ec8598SBjorn Helgaas 						     device);
94146ec8598SBjorn Helgaas 
94246ec8598SBjorn Helgaas 	if (ACPI_FAILURE(status))
94346ec8598SBjorn Helgaas 		return -EINVAL;
94446ec8598SBjorn Helgaas 	return 0;
94546ec8598SBjorn Helgaas }
94646ec8598SBjorn Helgaas 
94746ec8598SBjorn Helgaas static void acpi_device_remove_notify_handler(struct acpi_device *device)
94846ec8598SBjorn Helgaas {
949ccba2a36SBjorn Helgaas 	if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
95046ec8598SBjorn Helgaas 		acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
95146ec8598SBjorn Helgaas 						acpi_device_notify_fixed);
952ccba2a36SBjorn Helgaas 	else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
95346ec8598SBjorn Helgaas 		acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
95446ec8598SBjorn Helgaas 						acpi_device_notify_fixed);
95546ec8598SBjorn Helgaas 	else
95646ec8598SBjorn Helgaas 		acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
95746ec8598SBjorn Helgaas 					   acpi_device_notify);
95846ec8598SBjorn Helgaas }
95946ec8598SBjorn Helgaas 
9605d9464a4SPatrick Mochel static int acpi_device_probe(struct device *dev)
9615d9464a4SPatrick Mochel {
9625d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
9635d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
9645d9464a4SPatrick Mochel 	int ret;
9655d9464a4SPatrick Mochel 
96624071f47SRafael J. Wysocki 	if (acpi_dev->handler)
96724071f47SRafael J. Wysocki 		return -EINVAL;
96824071f47SRafael J. Wysocki 
969d9e455f5SRafael J. Wysocki 	if (!acpi_drv->ops.add)
970d9e455f5SRafael J. Wysocki 		return -ENOSYS;
971d9e455f5SRafael J. Wysocki 
972d9e455f5SRafael J. Wysocki 	ret = acpi_drv->ops.add(acpi_dev);
973d9e455f5SRafael J. Wysocki 	if (ret)
974d9e455f5SRafael J. Wysocki 		return ret;
975d9e455f5SRafael J. Wysocki 
976d9e455f5SRafael J. Wysocki 	acpi_dev->driver = acpi_drv;
977d9e455f5SRafael J. Wysocki 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
978d9e455f5SRafael J. Wysocki 			  "Driver [%s] successfully bound to device [%s]\n",
979d9e455f5SRafael J. Wysocki 			  acpi_drv->name, acpi_dev->pnp.bus_id));
980d9e455f5SRafael J. Wysocki 
98146ec8598SBjorn Helgaas 	if (acpi_drv->ops.notify) {
98246ec8598SBjorn Helgaas 		ret = acpi_device_install_notify_handler(acpi_dev);
98346ec8598SBjorn Helgaas 		if (ret) {
98446ec8598SBjorn Helgaas 			if (acpi_drv->ops.remove)
98551fac838SRafael J. Wysocki 				acpi_drv->ops.remove(acpi_dev);
986d9e455f5SRafael J. Wysocki 
9875f27ee8eSToshi Kani 			acpi_dev->driver = NULL;
9885f27ee8eSToshi Kani 			acpi_dev->driver_data = NULL;
98946ec8598SBjorn Helgaas 			return ret;
99046ec8598SBjorn Helgaas 		}
99146ec8598SBjorn Helgaas 	}
99246ec8598SBjorn Helgaas 
993d9e455f5SRafael J. Wysocki 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
9945d9464a4SPatrick Mochel 			  acpi_drv->name, acpi_dev->pnp.bus_id));
9955d9464a4SPatrick Mochel 	get_device(dev);
996d9e455f5SRafael J. Wysocki 	return 0;
9975d9464a4SPatrick Mochel }
9985d9464a4SPatrick Mochel 
9995d9464a4SPatrick Mochel static int acpi_device_remove(struct device * dev)
10005d9464a4SPatrick Mochel {
10015d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
10025d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
10035d9464a4SPatrick Mochel 
10045d9464a4SPatrick Mochel 	if (acpi_drv) {
100546ec8598SBjorn Helgaas 		if (acpi_drv->ops.notify)
100646ec8598SBjorn Helgaas 			acpi_device_remove_notify_handler(acpi_dev);
10075d9464a4SPatrick Mochel 		if (acpi_drv->ops.remove)
100851fac838SRafael J. Wysocki 			acpi_drv->ops.remove(acpi_dev);
10095d9464a4SPatrick Mochel 	}
10105d9464a4SPatrick Mochel 	acpi_dev->driver = NULL;
1011db89b4f0SPavel Machek 	acpi_dev->driver_data = NULL;
10125d9464a4SPatrick Mochel 
10135d9464a4SPatrick Mochel 	put_device(dev);
10145d9464a4SPatrick Mochel 	return 0;
10155d9464a4SPatrick Mochel }
10165d9464a4SPatrick Mochel 
101755955aadSDavid Brownell struct bus_type acpi_bus_type = {
10189e89dde2SZhang Rui 	.name		= "acpi",
10195d9464a4SPatrick Mochel 	.match		= acpi_bus_match,
10205d9464a4SPatrick Mochel 	.probe		= acpi_device_probe,
10215d9464a4SPatrick Mochel 	.remove		= acpi_device_remove,
10225d9464a4SPatrick Mochel 	.uevent		= acpi_device_uevent,
10231da177e4SLinus Torvalds };
10241da177e4SLinus Torvalds 
1025d783156eSRafael J. Wysocki static void acpi_device_del(struct acpi_device *device)
1026caf5c03fSRafael J. Wysocki {
1027d783156eSRafael J. Wysocki 	mutex_lock(&acpi_device_lock);
1028d783156eSRafael J. Wysocki 	if (device->parent)
1029d783156eSRafael J. Wysocki 		list_del(&device->node);
1030d783156eSRafael J. Wysocki 
1031d783156eSRafael J. Wysocki 	list_del(&device->wakeup_list);
1032d783156eSRafael J. Wysocki 	mutex_unlock(&acpi_device_lock);
1033d783156eSRafael J. Wysocki 
1034d783156eSRafael J. Wysocki 	acpi_power_add_remove_device(device, false);
1035d783156eSRafael J. Wysocki 	acpi_device_remove_files(device);
1036d783156eSRafael J. Wysocki 	if (device->remove)
1037d783156eSRafael J. Wysocki 		device->remove(device);
1038d783156eSRafael J. Wysocki 
1039d783156eSRafael J. Wysocki 	device_del(&device->dev);
1040d783156eSRafael J. Wysocki }
1041d783156eSRafael J. Wysocki 
1042d783156eSRafael J. Wysocki static LIST_HEAD(acpi_device_del_list);
1043d783156eSRafael J. Wysocki static DEFINE_MUTEX(acpi_device_del_lock);
1044d783156eSRafael J. Wysocki 
1045d783156eSRafael J. Wysocki static void acpi_device_del_work_fn(struct work_struct *work_not_used)
1046d783156eSRafael J. Wysocki {
1047d783156eSRafael J. Wysocki 	for (;;) {
1048d783156eSRafael J. Wysocki 		struct acpi_device *adev;
1049d783156eSRafael J. Wysocki 
1050d783156eSRafael J. Wysocki 		mutex_lock(&acpi_device_del_lock);
1051d783156eSRafael J. Wysocki 
1052d783156eSRafael J. Wysocki 		if (list_empty(&acpi_device_del_list)) {
1053d783156eSRafael J. Wysocki 			mutex_unlock(&acpi_device_del_lock);
1054d783156eSRafael J. Wysocki 			break;
1055d783156eSRafael J. Wysocki 		}
1056d783156eSRafael J. Wysocki 		adev = list_first_entry(&acpi_device_del_list,
1057d783156eSRafael J. Wysocki 					struct acpi_device, del_list);
1058d783156eSRafael J. Wysocki 		list_del(&adev->del_list);
1059d783156eSRafael J. Wysocki 
1060d783156eSRafael J. Wysocki 		mutex_unlock(&acpi_device_del_lock);
1061d783156eSRafael J. Wysocki 
1062d783156eSRafael J. Wysocki 		acpi_device_del(adev);
1063d783156eSRafael J. Wysocki 		/*
1064d783156eSRafael J. Wysocki 		 * Drop references to all power resources that might have been
1065d783156eSRafael J. Wysocki 		 * used by the device.
1066d783156eSRafael J. Wysocki 		 */
1067d783156eSRafael J. Wysocki 		acpi_power_transition(adev, ACPI_STATE_D3_COLD);
1068d783156eSRafael J. Wysocki 		put_device(&adev->dev);
1069d783156eSRafael J. Wysocki 	}
1070d783156eSRafael J. Wysocki }
1071d783156eSRafael J. Wysocki 
1072d783156eSRafael J. Wysocki /**
1073d783156eSRafael J. Wysocki  * acpi_scan_drop_device - Drop an ACPI device object.
1074d783156eSRafael J. Wysocki  * @handle: Handle of an ACPI namespace node, not used.
1075d783156eSRafael J. Wysocki  * @context: Address of the ACPI device object to drop.
1076d783156eSRafael J. Wysocki  *
1077d783156eSRafael J. Wysocki  * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
1078d783156eSRafael J. Wysocki  * namespace node the device object pointed to by @context is attached to.
1079d783156eSRafael J. Wysocki  *
1080d783156eSRafael J. Wysocki  * The unregistration is carried out asynchronously to avoid running
1081d783156eSRafael J. Wysocki  * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
1082d783156eSRafael J. Wysocki  * ensure the correct ordering (the device objects must be unregistered in the
1083d783156eSRafael J. Wysocki  * same order in which the corresponding namespace nodes are deleted).
1084d783156eSRafael J. Wysocki  */
1085d783156eSRafael J. Wysocki static void acpi_scan_drop_device(acpi_handle handle, void *context)
1086d783156eSRafael J. Wysocki {
1087d783156eSRafael J. Wysocki 	static DECLARE_WORK(work, acpi_device_del_work_fn);
1088d783156eSRafael J. Wysocki 	struct acpi_device *adev = context;
1089d783156eSRafael J. Wysocki 
1090d783156eSRafael J. Wysocki 	mutex_lock(&acpi_device_del_lock);
1091d783156eSRafael J. Wysocki 
1092d783156eSRafael J. Wysocki 	/*
1093d783156eSRafael J. Wysocki 	 * Use the ACPI hotplug workqueue which is ordered, so this work item
1094d783156eSRafael J. Wysocki 	 * won't run after any hotplug work items submitted subsequently.  That
1095d783156eSRafael J. Wysocki 	 * prevents attempts to register device objects identical to those being
1096d783156eSRafael J. Wysocki 	 * deleted from happening concurrently (such attempts result from
1097d783156eSRafael J. Wysocki 	 * hotplug events handled via the ACPI hotplug workqueue).  It also will
1098d783156eSRafael J. Wysocki 	 * run after all of the work items submitted previosuly, which helps
1099d783156eSRafael J. Wysocki 	 * those work items to ensure that they are not accessing stale device
1100d783156eSRafael J. Wysocki 	 * objects.
1101d783156eSRafael J. Wysocki 	 */
1102d783156eSRafael J. Wysocki 	if (list_empty(&acpi_device_del_list))
1103d783156eSRafael J. Wysocki 		acpi_queue_hotplug_work(&work);
1104d783156eSRafael J. Wysocki 
1105d783156eSRafael J. Wysocki 	list_add_tail(&adev->del_list, &acpi_device_del_list);
1106d783156eSRafael J. Wysocki 	/* Make acpi_ns_validate_handle() return NULL for this handle. */
1107d783156eSRafael J. Wysocki 	adev->handle = INVALID_ACPI_HANDLE;
1108d783156eSRafael J. Wysocki 
1109d783156eSRafael J. Wysocki 	mutex_unlock(&acpi_device_del_lock);
1110caf5c03fSRafael J. Wysocki }
1111caf5c03fSRafael J. Wysocki 
111278ea4639SRafael J. Wysocki static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device,
111378ea4639SRafael J. Wysocki 				void (*callback)(void *))
1114caf5c03fSRafael J. Wysocki {
1115caf5c03fSRafael J. Wysocki 	acpi_status status;
1116caf5c03fSRafael J. Wysocki 
1117caf5c03fSRafael J. Wysocki 	if (!device)
1118caf5c03fSRafael J. Wysocki 		return -EINVAL;
1119caf5c03fSRafael J. Wysocki 
112078ea4639SRafael J. Wysocki 	status = acpi_get_data_full(handle, acpi_scan_drop_device,
112178ea4639SRafael J. Wysocki 				    (void **)device, callback);
1122caf5c03fSRafael J. Wysocki 	if (ACPI_FAILURE(status) || !*device) {
1123caf5c03fSRafael J. Wysocki 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
1124caf5c03fSRafael J. Wysocki 				  handle));
1125caf5c03fSRafael J. Wysocki 		return -ENODEV;
1126caf5c03fSRafael J. Wysocki 	}
1127caf5c03fSRafael J. Wysocki 	return 0;
1128caf5c03fSRafael J. Wysocki }
112978ea4639SRafael J. Wysocki 
113078ea4639SRafael J. Wysocki int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
113178ea4639SRafael J. Wysocki {
113278ea4639SRafael J. Wysocki 	return acpi_get_device_data(handle, device, NULL);
113378ea4639SRafael J. Wysocki }
11346585925bSRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_get_device);
1135caf5c03fSRafael J. Wysocki 
113678ea4639SRafael J. Wysocki static void get_acpi_device(void *dev)
113778ea4639SRafael J. Wysocki {
113878ea4639SRafael J. Wysocki 	if (dev)
113978ea4639SRafael J. Wysocki 		get_device(&((struct acpi_device *)dev)->dev);
114078ea4639SRafael J. Wysocki }
114178ea4639SRafael J. Wysocki 
114278ea4639SRafael J. Wysocki struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
114378ea4639SRafael J. Wysocki {
114478ea4639SRafael J. Wysocki 	struct acpi_device *adev = NULL;
114578ea4639SRafael J. Wysocki 
114678ea4639SRafael J. Wysocki 	acpi_get_device_data(handle, &adev, get_acpi_device);
114778ea4639SRafael J. Wysocki 	return adev;
114878ea4639SRafael J. Wysocki }
114978ea4639SRafael J. Wysocki 
115078ea4639SRafael J. Wysocki void acpi_bus_put_acpi_device(struct acpi_device *adev)
115178ea4639SRafael J. Wysocki {
115278ea4639SRafael J. Wysocki 	put_device(&adev->dev);
115378ea4639SRafael J. Wysocki }
115478ea4639SRafael J. Wysocki 
1155cf860be6SRafael J. Wysocki int acpi_device_add(struct acpi_device *device,
115682c7d5efSRafael J. Wysocki 		    void (*release)(struct device *))
11571da177e4SLinus Torvalds {
1158e49bd2ddSZhang Rui 	int result;
1159e49bd2ddSZhang Rui 	struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
1160e49bd2ddSZhang Rui 	int found = 0;
116166b7ed40SBjorn Helgaas 
1162d43e167dSRafael J. Wysocki 	if (device->handle) {
1163d43e167dSRafael J. Wysocki 		acpi_status status;
1164d43e167dSRafael J. Wysocki 
1165d783156eSRafael J. Wysocki 		status = acpi_attach_data(device->handle, acpi_scan_drop_device,
1166d43e167dSRafael J. Wysocki 					  device);
1167d43e167dSRafael J. Wysocki 		if (ACPI_FAILURE(status)) {
1168d43e167dSRafael J. Wysocki 			acpi_handle_err(device->handle,
1169d43e167dSRafael J. Wysocki 					"Unable to attach device data\n");
1170d43e167dSRafael J. Wysocki 			return -ENODEV;
1171d43e167dSRafael J. Wysocki 		}
1172d43e167dSRafael J. Wysocki 	}
1173d43e167dSRafael J. Wysocki 
11741da177e4SLinus Torvalds 	/*
11751da177e4SLinus Torvalds 	 * Linkage
11761da177e4SLinus Torvalds 	 * -------
11771da177e4SLinus Torvalds 	 * Link this device to its parent and siblings.
11781da177e4SLinus Torvalds 	 */
11791da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->children);
11801da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->node);
11811da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->wakeup_list);
11821033f904SLan Tianyu 	INIT_LIST_HEAD(&device->physical_node_list);
1183d783156eSRafael J. Wysocki 	INIT_LIST_HEAD(&device->del_list);
11841033f904SLan Tianyu 	mutex_init(&device->physical_node_lock);
11851da177e4SLinus Torvalds 
1186e49bd2ddSZhang Rui 	new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1187e49bd2ddSZhang Rui 	if (!new_bus_id) {
1188d43e167dSRafael J. Wysocki 		pr_err(PREFIX "Memory allocation error\n");
1189d43e167dSRafael J. Wysocki 		result = -ENOMEM;
1190d43e167dSRafael J. Wysocki 		goto err_detach;
1191e49bd2ddSZhang Rui 	}
1192e49bd2ddSZhang Rui 
11939090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
1194e49bd2ddSZhang Rui 	/*
1195e49bd2ddSZhang Rui 	 * Find suitable bus_id and instance number in acpi_bus_id_list
1196e49bd2ddSZhang Rui 	 * If failed, create one and link it into acpi_bus_id_list
1197e49bd2ddSZhang Rui 	 */
1198e49bd2ddSZhang Rui 	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
11991131b938SBjorn Helgaas 		if (!strcmp(acpi_device_bus_id->bus_id,
12001131b938SBjorn Helgaas 			    acpi_device_hid(device))) {
1201e49bd2ddSZhang Rui 			acpi_device_bus_id->instance_no++;
1202e49bd2ddSZhang Rui 			found = 1;
1203e49bd2ddSZhang Rui 			kfree(new_bus_id);
1204e49bd2ddSZhang Rui 			break;
1205e49bd2ddSZhang Rui 		}
1206e49bd2ddSZhang Rui 	}
1207e49bd2ddSZhang Rui 	if (!found) {
1208e49bd2ddSZhang Rui 		acpi_device_bus_id = new_bus_id;
12091131b938SBjorn Helgaas 		strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
1210e49bd2ddSZhang Rui 		acpi_device_bus_id->instance_no = 0;
1211e49bd2ddSZhang Rui 		list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1212e49bd2ddSZhang Rui 	}
12130794469dSKay Sievers 	dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
1214e49bd2ddSZhang Rui 
121533b57150SLen Brown 	if (device->parent)
12161da177e4SLinus Torvalds 		list_add_tail(&device->node, &device->parent->children);
121733b57150SLen Brown 
12181da177e4SLinus Torvalds 	if (device->wakeup.flags.valid)
12191da177e4SLinus Torvalds 		list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
12209090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
12211da177e4SLinus Torvalds 
12221890a97aSPatrick Mochel 	if (device->parent)
122366b7ed40SBjorn Helgaas 		device->dev.parent = &device->parent->dev;
12241890a97aSPatrick Mochel 	device->dev.bus = &acpi_bus_type;
122582c7d5efSRafael J. Wysocki 	device->dev.release = release;
1226cf860be6SRafael J. Wysocki 	result = device_add(&device->dev);
1227e49bd2ddSZhang Rui 	if (result) {
12288b12b922SAlex Chiang 		dev_err(&device->dev, "Error registering device\n");
1229d43e167dSRafael J. Wysocki 		goto err;
1230e49bd2ddSZhang Rui 	}
1231f883d9dbSPatrick Mochel 
1232e49bd2ddSZhang Rui 	result = acpi_device_setup_files(device);
1233e49bd2ddSZhang Rui 	if (result)
12340794469dSKay Sievers 		printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
12350794469dSKay Sievers 		       dev_name(&device->dev));
1236e49bd2ddSZhang Rui 
1237e49bd2ddSZhang Rui 	return 0;
1238d43e167dSRafael J. Wysocki 
1239d43e167dSRafael J. Wysocki  err:
12409090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
124133b57150SLen Brown 	if (device->parent)
1242e49bd2ddSZhang Rui 		list_del(&device->node);
1243e49bd2ddSZhang Rui 	list_del(&device->wakeup_list);
12449090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
1245d43e167dSRafael J. Wysocki 
1246d43e167dSRafael J. Wysocki  err_detach:
1247d783156eSRafael J. Wysocki 	acpi_detach_data(device->handle, acpi_scan_drop_device);
1248e49bd2ddSZhang Rui 	return result;
12491da177e4SLinus Torvalds }
12501da177e4SLinus Torvalds 
12519e89dde2SZhang Rui /* --------------------------------------------------------------------------
12529e89dde2SZhang Rui                                  Driver Management
12539e89dde2SZhang Rui    -------------------------------------------------------------------------- */
12541da177e4SLinus Torvalds /**
1255d758a8faSRandy Dunlap  * acpi_bus_register_driver - register a driver with the ACPI bus
1256d758a8faSRandy Dunlap  * @driver: driver being registered
1257d758a8faSRandy Dunlap  *
12581da177e4SLinus Torvalds  * Registers a driver with the ACPI bus.  Searches the namespace for all
12599d9f749bSBjorn Helgaas  * devices that match the driver's criteria and binds.  Returns zero for
12609d9f749bSBjorn Helgaas  * success or a negative error status for failure.
12611da177e4SLinus Torvalds  */
12624be44fcdSLen Brown int acpi_bus_register_driver(struct acpi_driver *driver)
12631da177e4SLinus Torvalds {
12641890a97aSPatrick Mochel 	int ret;
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 	if (acpi_disabled)
1267d550d98dSPatrick Mochel 		return -ENODEV;
12681890a97aSPatrick Mochel 	driver->drv.name = driver->name;
12691890a97aSPatrick Mochel 	driver->drv.bus = &acpi_bus_type;
12701890a97aSPatrick Mochel 	driver->drv.owner = driver->owner;
12711da177e4SLinus Torvalds 
12721890a97aSPatrick Mochel 	ret = driver_register(&driver->drv);
12731890a97aSPatrick Mochel 	return ret;
12741da177e4SLinus Torvalds }
12751da177e4SLinus Torvalds 
12764be44fcdSLen Brown EXPORT_SYMBOL(acpi_bus_register_driver);
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds /**
1279b27b14ceSHanjun Guo  * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
1280d758a8faSRandy Dunlap  * @driver: driver to unregister
1281d758a8faSRandy Dunlap  *
12821da177e4SLinus Torvalds  * Unregisters a driver with the ACPI bus.  Searches the namespace for all
12831da177e4SLinus Torvalds  * devices that match the driver's criteria and unbinds.
12841da177e4SLinus Torvalds  */
128506ea8e08SBjorn Helgaas void acpi_bus_unregister_driver(struct acpi_driver *driver)
12861da177e4SLinus Torvalds {
12871890a97aSPatrick Mochel 	driver_unregister(&driver->drv);
12881da177e4SLinus Torvalds }
12894be44fcdSLen Brown 
12901da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_unregister_driver);
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds /* --------------------------------------------------------------------------
12931da177e4SLinus Torvalds                                  Device Enumeration
12941da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
12955c478f49SBjorn Helgaas static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
12965c478f49SBjorn Helgaas {
1297456de893SRafael J. Wysocki 	struct acpi_device *device = NULL;
12985c478f49SBjorn Helgaas 	acpi_status status;
12995c478f49SBjorn Helgaas 
13005c478f49SBjorn Helgaas 	/*
13015c478f49SBjorn Helgaas 	 * Fixed hardware devices do not appear in the namespace and do not
13025c478f49SBjorn Helgaas 	 * have handles, but we fabricate acpi_devices for them, so we have
13035c478f49SBjorn Helgaas 	 * to deal with them specially.
13045c478f49SBjorn Helgaas 	 */
1305456de893SRafael J. Wysocki 	if (!handle)
13065c478f49SBjorn Helgaas 		return acpi_root;
13075c478f49SBjorn Helgaas 
13085c478f49SBjorn Helgaas 	do {
13095c478f49SBjorn Helgaas 		status = acpi_get_parent(handle, &handle);
13105c478f49SBjorn Helgaas 		if (ACPI_FAILURE(status))
1311456de893SRafael J. Wysocki 			return status == AE_NULL_ENTRY ? NULL : acpi_root;
1312456de893SRafael J. Wysocki 	} while (acpi_bus_get_device(handle, &device));
13135c478f49SBjorn Helgaas 	return device;
13145c478f49SBjorn Helgaas }
13155c478f49SBjorn Helgaas 
1316c8f7a62cSLen Brown acpi_status
1317c8f7a62cSLen Brown acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1318c8f7a62cSLen Brown {
1319c8f7a62cSLen Brown 	acpi_status status;
1320c8f7a62cSLen Brown 	acpi_handle tmp;
1321c8f7a62cSLen Brown 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1322c8f7a62cSLen Brown 	union acpi_object *obj;
1323c8f7a62cSLen Brown 
1324c8f7a62cSLen Brown 	status = acpi_get_handle(handle, "_EJD", &tmp);
1325c8f7a62cSLen Brown 	if (ACPI_FAILURE(status))
1326c8f7a62cSLen Brown 		return status;
1327c8f7a62cSLen Brown 
1328c8f7a62cSLen Brown 	status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1329c8f7a62cSLen Brown 	if (ACPI_SUCCESS(status)) {
1330c8f7a62cSLen Brown 		obj = buffer.pointer;
13313b5fee59SHolger Macht 		status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
13323b5fee59SHolger Macht 					 ejd);
1333c8f7a62cSLen Brown 		kfree(buffer.pointer);
1334c8f7a62cSLen Brown 	}
1335c8f7a62cSLen Brown 	return status;
1336c8f7a62cSLen Brown }
1337c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1338c8f7a62cSLen Brown 
1339e88c9c60SRafael J. Wysocki static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1340b581a7f9SRafael J. Wysocki 					struct acpi_device_wakeup *wakeup)
13411da177e4SLinus Torvalds {
1342b581a7f9SRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1343b581a7f9SRafael J. Wysocki 	union acpi_object *package = NULL;
13441da177e4SLinus Torvalds 	union acpi_object *element = NULL;
1345b581a7f9SRafael J. Wysocki 	acpi_status status;
1346e88c9c60SRafael J. Wysocki 	int err = -ENODATA;
13471da177e4SLinus Torvalds 
1348b581a7f9SRafael J. Wysocki 	if (!wakeup)
1349e88c9c60SRafael J. Wysocki 		return -EINVAL;
13501da177e4SLinus Torvalds 
1351993cbe59SRafael J. Wysocki 	INIT_LIST_HEAD(&wakeup->resources);
13521da177e4SLinus Torvalds 
1353b581a7f9SRafael J. Wysocki 	/* _PRW */
1354b581a7f9SRafael J. Wysocki 	status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1355b581a7f9SRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
1356b581a7f9SRafael J. Wysocki 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
1357e88c9c60SRafael J. Wysocki 		return err;
1358b581a7f9SRafael J. Wysocki 	}
1359b581a7f9SRafael J. Wysocki 
1360b581a7f9SRafael J. Wysocki 	package = (union acpi_object *)buffer.pointer;
1361b581a7f9SRafael J. Wysocki 
1362e88c9c60SRafael J. Wysocki 	if (!package || package->package.count < 2)
1363b581a7f9SRafael J. Wysocki 		goto out;
1364b581a7f9SRafael J. Wysocki 
13651da177e4SLinus Torvalds 	element = &(package->package.elements[0]);
1366e88c9c60SRafael J. Wysocki 	if (!element)
1367b581a7f9SRafael J. Wysocki 		goto out;
1368e88c9c60SRafael J. Wysocki 
13691da177e4SLinus Torvalds 	if (element->type == ACPI_TYPE_PACKAGE) {
13701da177e4SLinus Torvalds 		if ((element->package.count < 2) ||
13711da177e4SLinus Torvalds 		    (element->package.elements[0].type !=
13721da177e4SLinus Torvalds 		     ACPI_TYPE_LOCAL_REFERENCE)
1373e88c9c60SRafael J. Wysocki 		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
1374b581a7f9SRafael J. Wysocki 			goto out;
1375e88c9c60SRafael J. Wysocki 
1376b581a7f9SRafael J. Wysocki 		wakeup->gpe_device =
13771da177e4SLinus Torvalds 		    element->package.elements[0].reference.handle;
1378b581a7f9SRafael J. Wysocki 		wakeup->gpe_number =
13791da177e4SLinus Torvalds 		    (u32) element->package.elements[1].integer.value;
13801da177e4SLinus Torvalds 	} else if (element->type == ACPI_TYPE_INTEGER) {
1381b581a7f9SRafael J. Wysocki 		wakeup->gpe_device = NULL;
1382b581a7f9SRafael J. Wysocki 		wakeup->gpe_number = element->integer.value;
1383b581a7f9SRafael J. Wysocki 	} else {
1384b581a7f9SRafael J. Wysocki 		goto out;
1385b581a7f9SRafael J. Wysocki 	}
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 	element = &(package->package.elements[1]);
1388e88c9c60SRafael J. Wysocki 	if (element->type != ACPI_TYPE_INTEGER)
1389b581a7f9SRafael J. Wysocki 		goto out;
1390e88c9c60SRafael J. Wysocki 
1391b581a7f9SRafael J. Wysocki 	wakeup->sleep_state = element->integer.value;
13921da177e4SLinus Torvalds 
1393e88c9c60SRafael J. Wysocki 	err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1394e88c9c60SRafael J. Wysocki 	if (err)
1395b581a7f9SRafael J. Wysocki 		goto out;
13961da177e4SLinus Torvalds 
13970596a52bSRafael J. Wysocki 	if (!list_empty(&wakeup->resources)) {
13980596a52bSRafael J. Wysocki 		int sleep_state;
13999874647bSRafael J. Wysocki 
1400b5d667ebSRafael J. Wysocki 		err = acpi_power_wakeup_list_init(&wakeup->resources,
1401b5d667ebSRafael J. Wysocki 						  &sleep_state);
1402b5d667ebSRafael J. Wysocki 		if (err) {
1403b5d667ebSRafael J. Wysocki 			acpi_handle_warn(handle, "Retrieving current states "
1404b5d667ebSRafael J. Wysocki 					 "of wakeup power resources failed\n");
1405b5d667ebSRafael J. Wysocki 			acpi_power_resources_list_free(&wakeup->resources);
1406b5d667ebSRafael J. Wysocki 			goto out;
1407b5d667ebSRafael J. Wysocki 		}
14080596a52bSRafael J. Wysocki 		if (sleep_state < wakeup->sleep_state) {
14090596a52bSRafael J. Wysocki 			acpi_handle_warn(handle, "Overriding _PRW sleep state "
14100596a52bSRafael J. Wysocki 					 "(S%d) by S%d from power resources\n",
14110596a52bSRafael J. Wysocki 					 (int)wakeup->sleep_state, sleep_state);
14120596a52bSRafael J. Wysocki 			wakeup->sleep_state = sleep_state;
14130596a52bSRafael J. Wysocki 		}
14140596a52bSRafael J. Wysocki 	}
1415bba63a29SLin Ming 	acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
1416b581a7f9SRafael J. Wysocki 
1417b581a7f9SRafael J. Wysocki  out:
1418b581a7f9SRafael J. Wysocki 	kfree(buffer.pointer);
1419e88c9c60SRafael J. Wysocki 	return err;
14201da177e4SLinus Torvalds }
14211da177e4SLinus Torvalds 
1422f517709dSRafael J. Wysocki static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
14231da177e4SLinus Torvalds {
142429b71a1cSThomas Renninger 	struct acpi_device_id button_device_ids[] = {
142529b71a1cSThomas Renninger 		{"PNP0C0C", 0},
1426b7e38304SZhang Rui 		{"PNP0C0D", 0},
142729b71a1cSThomas Renninger 		{"PNP0C0E", 0},
142829b71a1cSThomas Renninger 		{"", 0},
142929b71a1cSThomas Renninger 	};
1430f517709dSRafael J. Wysocki 	acpi_status status;
1431f517709dSRafael J. Wysocki 	acpi_event_status event_status;
1432f517709dSRafael J. Wysocki 
1433b67ea761SRafael J. Wysocki 	device->wakeup.flags.notifier_present = 0;
1434f517709dSRafael J. Wysocki 
1435f517709dSRafael J. Wysocki 	/* Power button, Lid switch always enable wakeup */
1436f517709dSRafael J. Wysocki 	if (!acpi_match_device_ids(device, button_device_ids)) {
1437f517709dSRafael J. Wysocki 		device->wakeup.flags.run_wake = 1;
1438b7e38304SZhang Rui 		if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1439b7e38304SZhang Rui 			/* Do not use Lid/sleep button for S5 wakeup */
1440b7e38304SZhang Rui 			if (device->wakeup.sleep_state == ACPI_STATE_S5)
1441b7e38304SZhang Rui 				device->wakeup.sleep_state = ACPI_STATE_S4;
1442b7e38304SZhang Rui 		}
1443f2b56bc8SRafael J. Wysocki 		device_set_wakeup_capable(&device->dev, true);
1444f517709dSRafael J. Wysocki 		return;
1445f517709dSRafael J. Wysocki 	}
1446f517709dSRafael J. Wysocki 
1447e8e18c95SRafael J. Wysocki 	status = acpi_get_gpe_status(device->wakeup.gpe_device,
1448e8e18c95SRafael J. Wysocki 					device->wakeup.gpe_number,
14490f849d2cSLin Ming 						&event_status);
1450f517709dSRafael J. Wysocki 	if (status == AE_OK)
1451f517709dSRafael J. Wysocki 		device->wakeup.flags.run_wake =
1452f517709dSRafael J. Wysocki 				!!(event_status & ACPI_EVENT_FLAG_HANDLE);
1453f517709dSRafael J. Wysocki }
1454f517709dSRafael J. Wysocki 
1455d57d09a4SRafael J. Wysocki static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1456f517709dSRafael J. Wysocki {
1457e88c9c60SRafael J. Wysocki 	int err;
145829b71a1cSThomas Renninger 
1459d57d09a4SRafael J. Wysocki 	/* Presence of _PRW indicates wake capable */
1460952c63e9SJiang Liu 	if (!acpi_has_method(device->handle, "_PRW"))
1461d57d09a4SRafael J. Wysocki 		return;
1462d57d09a4SRafael J. Wysocki 
1463e88c9c60SRafael J. Wysocki 	err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1464b581a7f9SRafael J. Wysocki 							   &device->wakeup);
1465e88c9c60SRafael J. Wysocki 	if (err) {
1466e88c9c60SRafael J. Wysocki 		dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
1467d57d09a4SRafael J. Wysocki 		return;
14681da177e4SLinus Torvalds 	}
14691da177e4SLinus Torvalds 
14701da177e4SLinus Torvalds 	device->wakeup.flags.valid = 1;
14719b83ccd2SRafael J. Wysocki 	device->wakeup.prepare_count = 0;
1472f517709dSRafael J. Wysocki 	acpi_bus_set_run_wake_flags(device);
1473729b2bdbSZhao Yakui 	/* Call _PSW/_DSW object to disable its ability to wake the sleeping
1474729b2bdbSZhao Yakui 	 * system for the ACPI device with the _PRW object.
1475729b2bdbSZhao Yakui 	 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1476729b2bdbSZhao Yakui 	 * So it is necessary to call _DSW object first. Only when it is not
1477729b2bdbSZhao Yakui 	 * present will the _PSW object used.
1478729b2bdbSZhao Yakui 	 */
1479e88c9c60SRafael J. Wysocki 	err = acpi_device_sleep_wake(device, 0, 0, 0);
1480e88c9c60SRafael J. Wysocki 	if (err)
148177e76609SRafael J. Wysocki 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
148277e76609SRafael J. Wysocki 				"error in _DSW or _PSW evaluation\n"));
14831da177e4SLinus Torvalds }
14841da177e4SLinus Torvalds 
1485f33ce568SRafael J. Wysocki static void acpi_bus_init_power_state(struct acpi_device *device, int state)
14861da177e4SLinus Torvalds {
1487f33ce568SRafael J. Wysocki 	struct acpi_device_power_state *ps = &device->power.states[state];
1488ef85bdbeSRafael J. Wysocki 	char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1489ef85bdbeSRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1490f33ce568SRafael J. Wysocki 	acpi_status status;
14919e89dde2SZhang Rui 
1492f33ce568SRafael J. Wysocki 	INIT_LIST_HEAD(&ps->resources);
1493f33ce568SRafael J. Wysocki 
1494ef85bdbeSRafael J. Wysocki 	/* Evaluate "_PRx" to get referenced power resources */
1495ef85bdbeSRafael J. Wysocki 	status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1496ef85bdbeSRafael J. Wysocki 	if (ACPI_SUCCESS(status)) {
1497ef85bdbeSRafael J. Wysocki 		union acpi_object *package = buffer.pointer;
1498f33ce568SRafael J. Wysocki 
1499ef85bdbeSRafael J. Wysocki 		if (buffer.length && package
1500ef85bdbeSRafael J. Wysocki 		    && package->type == ACPI_TYPE_PACKAGE
1501ef85bdbeSRafael J. Wysocki 		    && package->package.count) {
1502e88c9c60SRafael J. Wysocki 			int err = acpi_extract_power_resources(package, 0,
1503ef85bdbeSRafael J. Wysocki 							       &ps->resources);
1504e88c9c60SRafael J. Wysocki 			if (!err)
1505f33ce568SRafael J. Wysocki 				device->power.flags.power_resources = 1;
1506f33ce568SRafael J. Wysocki 		}
1507ef85bdbeSRafael J. Wysocki 		ACPI_FREE(buffer.pointer);
1508f33ce568SRafael J. Wysocki 	}
1509f33ce568SRafael J. Wysocki 
1510f33ce568SRafael J. Wysocki 	/* Evaluate "_PSx" to see if we can do explicit sets */
1511ef85bdbeSRafael J. Wysocki 	pathname[2] = 'S';
1512952c63e9SJiang Liu 	if (acpi_has_method(device->handle, pathname))
1513f33ce568SRafael J. Wysocki 		ps->flags.explicit_set = 1;
1514f33ce568SRafael J. Wysocki 
1515f33ce568SRafael J. Wysocki 	/*
1516f33ce568SRafael J. Wysocki 	 * State is valid if there are means to put the device into it.
1517f33ce568SRafael J. Wysocki 	 * D3hot is only valid if _PR3 present.
1518f33ce568SRafael J. Wysocki 	 */
1519ef85bdbeSRafael J. Wysocki 	if (!list_empty(&ps->resources)
1520f33ce568SRafael J. Wysocki 	    || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1521f33ce568SRafael J. Wysocki 		ps->flags.valid = 1;
1522f33ce568SRafael J. Wysocki 		ps->flags.os_accessible = 1;
1523f33ce568SRafael J. Wysocki 	}
1524f33ce568SRafael J. Wysocki 
1525f33ce568SRafael J. Wysocki 	ps->power = -1;		/* Unknown - driver assigned */
1526f33ce568SRafael J. Wysocki 	ps->latency = -1;	/* Unknown - driver assigned */
1527f33ce568SRafael J. Wysocki }
1528f33ce568SRafael J. Wysocki 
1529d43e167dSRafael J. Wysocki static void acpi_bus_get_power_flags(struct acpi_device *device)
15301da177e4SLinus Torvalds {
15318bc5053bSRafael J. Wysocki 	u32 i;
15321da177e4SLinus Torvalds 
1533d43e167dSRafael J. Wysocki 	/* Presence of _PS0|_PR0 indicates 'power manageable' */
1534952c63e9SJiang Liu 	if (!acpi_has_method(device->handle, "_PS0") &&
1535952c63e9SJiang Liu 	    !acpi_has_method(device->handle, "_PR0"))
1536d43e167dSRafael J. Wysocki 		return;
1537d43e167dSRafael J. Wysocki 
1538d43e167dSRafael J. Wysocki 	device->flags.power_manageable = 1;
15391da177e4SLinus Torvalds 
15401da177e4SLinus Torvalds 	/*
15419e89dde2SZhang Rui 	 * Power Management Flags
15421da177e4SLinus Torvalds 	 */
1543952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_PSC"))
15449e89dde2SZhang Rui 		device->power.flags.explicit_get = 1;
1545952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_IRC"))
15469e89dde2SZhang Rui 		device->power.flags.inrush_current = 1;
15471da177e4SLinus Torvalds 
15481da177e4SLinus Torvalds 	/*
15499e89dde2SZhang Rui 	 * Enumerate supported power management states
15501da177e4SLinus Torvalds 	 */
1551f33ce568SRafael J. Wysocki 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1552f33ce568SRafael J. Wysocki 		acpi_bus_init_power_state(device, i);
15531da177e4SLinus Torvalds 
15540b224527SRafael J. Wysocki 	INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
15559e89dde2SZhang Rui 
15569e89dde2SZhang Rui 	/* Set defaults for D0 and D3 states (always valid) */
15579e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].flags.valid = 1;
15589e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].power = 100;
15598ad928d5SRafael J. Wysocki 	device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
15608ad928d5SRafael J. Wysocki 	device->power.states[ACPI_STATE_D3_COLD].power = 0;
15619e89dde2SZhang Rui 
15625c7dd710SRafael J. Wysocki 	/* Set D3cold's explicit_set flag if _PS3 exists. */
15635c7dd710SRafael J. Wysocki 	if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
15645c7dd710SRafael J. Wysocki 		device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
15655c7dd710SRafael J. Wysocki 
15661399dfcdSAaron Lu 	/* Presence of _PS3 or _PRx means we can put the device into D3 cold */
15671399dfcdSAaron Lu 	if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
15681399dfcdSAaron Lu 			device->power.flags.power_resources)
15691399dfcdSAaron Lu 		device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
15701399dfcdSAaron Lu 
1571b3785492SRafael J. Wysocki 	if (acpi_bus_init_power(device)) {
1572b3785492SRafael J. Wysocki 		acpi_free_power_resources_lists(device);
1573b3785492SRafael J. Wysocki 		device->flags.power_manageable = 0;
1574b3785492SRafael J. Wysocki 	}
15759e89dde2SZhang Rui }
1576c8f7a62cSLen Brown 
1577d43e167dSRafael J. Wysocki static void acpi_bus_get_flags(struct acpi_device *device)
15781da177e4SLinus Torvalds {
15791da177e4SLinus Torvalds 	/* Presence of _STA indicates 'dynamic_status' */
1580952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_STA"))
15811da177e4SLinus Torvalds 		device->flags.dynamic_status = 1;
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds 	/* Presence of _RMV indicates 'removable' */
1584952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_RMV"))
15851da177e4SLinus Torvalds 		device->flags.removable = 1;
15861da177e4SLinus Torvalds 
15871da177e4SLinus Torvalds 	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
1588952c63e9SJiang Liu 	if (acpi_has_method(device->handle, "_EJD") ||
1589952c63e9SJiang Liu 	    acpi_has_method(device->handle, "_EJ0"))
15901da177e4SLinus Torvalds 		device->flags.ejectable = 1;
15911da177e4SLinus Torvalds }
15921da177e4SLinus Torvalds 
1593c7bcb4e9SBjorn Helgaas static void acpi_device_get_busid(struct acpi_device *device)
15941da177e4SLinus Torvalds {
15951da177e4SLinus Torvalds 	char bus_id[5] = { '?', 0 };
15961da177e4SLinus Torvalds 	struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
15971da177e4SLinus Torvalds 	int i = 0;
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds 	/*
16001da177e4SLinus Torvalds 	 * Bus ID
16011da177e4SLinus Torvalds 	 * ------
16021da177e4SLinus Torvalds 	 * The device's Bus ID is simply the object name.
16031da177e4SLinus Torvalds 	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
16041da177e4SLinus Torvalds 	 */
1605859ac9a4SBjorn Helgaas 	if (ACPI_IS_ROOT_DEVICE(device)) {
16061da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "ACPI");
1607859ac9a4SBjorn Helgaas 		return;
1608859ac9a4SBjorn Helgaas 	}
1609859ac9a4SBjorn Helgaas 
1610859ac9a4SBjorn Helgaas 	switch (device->device_type) {
16111da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
16121da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "PWRF");
16131da177e4SLinus Torvalds 		break;
16141da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
16151da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "SLPF");
16161da177e4SLinus Torvalds 		break;
16171da177e4SLinus Torvalds 	default:
161866b7ed40SBjorn Helgaas 		acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
16191da177e4SLinus Torvalds 		/* Clean up trailing underscores (if any) */
16201da177e4SLinus Torvalds 		for (i = 3; i > 1; i--) {
16211da177e4SLinus Torvalds 			if (bus_id[i] == '_')
16221da177e4SLinus Torvalds 				bus_id[i] = '\0';
16231da177e4SLinus Torvalds 			else
16241da177e4SLinus Torvalds 				break;
16251da177e4SLinus Torvalds 		}
16261da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, bus_id);
16271da177e4SLinus Torvalds 		break;
16281da177e4SLinus Torvalds 	}
16291da177e4SLinus Torvalds }
16301da177e4SLinus Torvalds 
163154735266SZhang Rui /*
1632ebf4df8dSJiang Liu  * acpi_ata_match - see if an acpi object is an ATA device
1633ebf4df8dSJiang Liu  *
1634ebf4df8dSJiang Liu  * If an acpi object has one of the ACPI ATA methods defined,
1635ebf4df8dSJiang Liu  * then we can safely call it an ATA device.
1636ebf4df8dSJiang Liu  */
1637ebf4df8dSJiang Liu bool acpi_ata_match(acpi_handle handle)
1638ebf4df8dSJiang Liu {
1639ebf4df8dSJiang Liu 	return acpi_has_method(handle, "_GTF") ||
1640ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_GTM") ||
1641ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_STM") ||
1642ebf4df8dSJiang Liu 	       acpi_has_method(handle, "_SDD");
1643ebf4df8dSJiang Liu }
1644ebf4df8dSJiang Liu 
1645ebf4df8dSJiang Liu /*
1646d4e1a692SToshi Kani  * acpi_bay_match - see if an acpi object is an ejectable driver bay
164754735266SZhang Rui  *
164854735266SZhang Rui  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
164954735266SZhang Rui  * then we can safely call it an ejectable drive bay
165054735266SZhang Rui  */
1651ebf4df8dSJiang Liu bool acpi_bay_match(acpi_handle handle)
1652d4e1a692SToshi Kani {
165354735266SZhang Rui 	acpi_handle phandle;
165454735266SZhang Rui 
1655952c63e9SJiang Liu 	if (!acpi_has_method(handle, "_EJ0"))
1656ebf4df8dSJiang Liu 		return false;
1657ebf4df8dSJiang Liu 	if (acpi_ata_match(handle))
1658ebf4df8dSJiang Liu 		return true;
1659ebf4df8dSJiang Liu 	if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1660ebf4df8dSJiang Liu 		return false;
166154735266SZhang Rui 
1662ebf4df8dSJiang Liu 	return acpi_ata_match(phandle);
166354735266SZhang Rui }
166454735266SZhang Rui 
1665b43109faSRafael J. Wysocki bool acpi_device_is_battery(struct acpi_device *adev)
16661e2380cdSRafael J. Wysocki {
1667b43109faSRafael J. Wysocki 	struct acpi_hardware_id *hwid;
16681e2380cdSRafael J. Wysocki 
1669b43109faSRafael J. Wysocki 	list_for_each_entry(hwid, &adev->pnp.ids, list)
1670b43109faSRafael J. Wysocki 		if (!strcmp("PNP0C0A", hwid->id))
1671b43109faSRafael J. Wysocki 			return true;
1672b43109faSRafael J. Wysocki 
16731e2380cdSRafael J. Wysocki 	return false;
16741e2380cdSRafael J. Wysocki }
16751e2380cdSRafael J. Wysocki 
1676b43109faSRafael J. Wysocki static bool is_ejectable_bay(struct acpi_device *adev)
16771e2380cdSRafael J. Wysocki {
1678b43109faSRafael J. Wysocki 	acpi_handle handle = adev->handle;
1679b43109faSRafael J. Wysocki 
1680b43109faSRafael J. Wysocki 	if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
16811e2380cdSRafael J. Wysocki 		return true;
16821e2380cdSRafael J. Wysocki 
16831e2380cdSRafael J. Wysocki 	return acpi_bay_match(handle);
16841e2380cdSRafael J. Wysocki }
16851e2380cdSRafael J. Wysocki 
16863620f2f2SFrank Seidel /*
1687d4e1a692SToshi Kani  * acpi_dock_match - see if an acpi object has a _DCK method
16883620f2f2SFrank Seidel  */
1689ebf4df8dSJiang Liu bool acpi_dock_match(acpi_handle handle)
16903620f2f2SFrank Seidel {
1691ebf4df8dSJiang Liu 	return acpi_has_method(handle, "_DCK");
16923620f2f2SFrank Seidel }
16933620f2f2SFrank Seidel 
1694620e112cSThomas Renninger const char *acpi_device_hid(struct acpi_device *device)
169515b8dd53SBob Moore {
16967f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *hid;
169715b8dd53SBob Moore 
16982b2ae7c7SThomas Renninger 	if (list_empty(&device->pnp.ids))
16992b2ae7c7SThomas Renninger 		return dummy_hid;
17002b2ae7c7SThomas Renninger 
17017f47fa6cSBjorn Helgaas 	hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
17027f47fa6cSBjorn Helgaas 	return hid->id;
17037f47fa6cSBjorn Helgaas }
17047f47fa6cSBjorn Helgaas EXPORT_SYMBOL(acpi_device_hid);
170515b8dd53SBob Moore 
1706d4e1a692SToshi Kani static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
17077f47fa6cSBjorn Helgaas {
17087f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id;
170915b8dd53SBob Moore 
17107f47fa6cSBjorn Helgaas 	id = kmalloc(sizeof(*id), GFP_KERNEL);
17117f47fa6cSBjorn Helgaas 	if (!id)
17127f47fa6cSBjorn Helgaas 		return;
171315b8dd53SBob Moore 
1714581de59eSThomas Meyer 	id->id = kstrdup(dev_id, GFP_KERNEL);
17157f47fa6cSBjorn Helgaas 	if (!id->id) {
17167f47fa6cSBjorn Helgaas 		kfree(id);
17177f47fa6cSBjorn Helgaas 		return;
171815b8dd53SBob Moore 	}
171915b8dd53SBob Moore 
1720d4e1a692SToshi Kani 	list_add_tail(&id->list, &pnp->ids);
1721d4e1a692SToshi Kani 	pnp->type.hardware_id = 1;
172215b8dd53SBob Moore }
172315b8dd53SBob Moore 
1724222e82acSDarrick J. Wong /*
1725222e82acSDarrick J. Wong  * Old IBM workstations have a DSDT bug wherein the SMBus object
1726222e82acSDarrick J. Wong  * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1727222e82acSDarrick J. Wong  * prefix.  Work around this.
1728222e82acSDarrick J. Wong  */
1729ebf4df8dSJiang Liu static bool acpi_ibm_smbus_match(acpi_handle handle)
1730222e82acSDarrick J. Wong {
1731ebf4df8dSJiang Liu 	char node_name[ACPI_PATH_SEGMENT_LENGTH];
1732ebf4df8dSJiang Liu 	struct acpi_buffer path = { sizeof(node_name), node_name };
1733222e82acSDarrick J. Wong 
1734222e82acSDarrick J. Wong 	if (!dmi_name_in_vendors("IBM"))
1735ebf4df8dSJiang Liu 		return false;
1736222e82acSDarrick J. Wong 
1737222e82acSDarrick J. Wong 	/* Look for SMBS object */
1738ebf4df8dSJiang Liu 	if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1739ebf4df8dSJiang Liu 	    strcmp("SMBS", path.pointer))
1740ebf4df8dSJiang Liu 		return false;
1741222e82acSDarrick J. Wong 
1742222e82acSDarrick J. Wong 	/* Does it have the necessary (but misnamed) methods? */
1743952c63e9SJiang Liu 	if (acpi_has_method(handle, "SBI") &&
1744952c63e9SJiang Liu 	    acpi_has_method(handle, "SBR") &&
1745952c63e9SJiang Liu 	    acpi_has_method(handle, "SBW"))
1746ebf4df8dSJiang Liu 		return true;
1747ebf4df8dSJiang Liu 
1748ebf4df8dSJiang Liu 	return false;
1749222e82acSDarrick J. Wong }
1750222e82acSDarrick J. Wong 
1751c0af4175SToshi Kani static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1752c0af4175SToshi Kani 				int device_type)
17531da177e4SLinus Torvalds {
17541da177e4SLinus Torvalds 	acpi_status status;
175557f3674fSBjorn Helgaas 	struct acpi_device_info *info;
175678e25fefSLv Zheng 	struct acpi_pnp_device_id_list *cid_list;
17577f47fa6cSBjorn Helgaas 	int i;
17581da177e4SLinus Torvalds 
1759c0af4175SToshi Kani 	switch (device_type) {
17601da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
1761c0af4175SToshi Kani 		if (handle == ACPI_ROOT_OBJECT) {
1762c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_SYSTEM_HID);
1763859ac9a4SBjorn Helgaas 			break;
1764859ac9a4SBjorn Helgaas 		}
1765859ac9a4SBjorn Helgaas 
1766c0af4175SToshi Kani 		status = acpi_get_object_info(handle, &info);
17671da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
1768c0af4175SToshi Kani 			pr_err(PREFIX "%s: Error reading device info\n",
1769c0af4175SToshi Kani 					__func__);
17701da177e4SLinus Torvalds 			return;
17711da177e4SLinus Torvalds 		}
17721da177e4SLinus Torvalds 
17731da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_HID)
1774c0af4175SToshi Kani 			acpi_add_id(pnp, info->hardware_id.string);
177557f3674fSBjorn Helgaas 		if (info->valid & ACPI_VALID_CID) {
177615b8dd53SBob Moore 			cid_list = &info->compatible_id_list;
177757f3674fSBjorn Helgaas 			for (i = 0; i < cid_list->count; i++)
1778c0af4175SToshi Kani 				acpi_add_id(pnp, cid_list->ids[i].string);
177957f3674fSBjorn Helgaas 		}
17801da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_ADR) {
1781c0af4175SToshi Kani 			pnp->bus_address = info->address;
1782c0af4175SToshi Kani 			pnp->type.bus_address = 1;
17831da177e4SLinus Torvalds 		}
1784ccf78040SLv Zheng 		if (info->valid & ACPI_VALID_UID)
1785c0af4175SToshi Kani 			pnp->unique_id = kstrdup(info->unique_id.string,
1786ccf78040SLv Zheng 							GFP_KERNEL);
1787ae843332SZhang Rui 
1788a83893aeSBjorn Helgaas 		kfree(info);
1789a83893aeSBjorn Helgaas 
179057f3674fSBjorn Helgaas 		/*
179157f3674fSBjorn Helgaas 		 * Some devices don't reliably have _HIDs & _CIDs, so add
179257f3674fSBjorn Helgaas 		 * synthetic HIDs to make sure drivers can find them.
17933620f2f2SFrank Seidel 		 */
1794c0af4175SToshi Kani 		if (acpi_is_video_device(handle))
1795c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_VIDEO_HID);
1796ebf4df8dSJiang Liu 		else if (acpi_bay_match(handle))
1797c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_BAY_HID);
1798ebf4df8dSJiang Liu 		else if (acpi_dock_match(handle))
1799c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_DOCK_HID);
1800ebf4df8dSJiang Liu 		else if (acpi_ibm_smbus_match(handle))
1801c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1802c0af4175SToshi Kani 		else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1803c0af4175SToshi Kani 			acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1804c0af4175SToshi Kani 			strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1805c0af4175SToshi Kani 			strcpy(pnp->device_class, ACPI_BUS_CLASS);
1806b7b30de5SBjorn Helgaas 		}
180754735266SZhang Rui 
18081da177e4SLinus Torvalds 		break;
18091da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER:
1810c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_POWER_HID);
18111da177e4SLinus Torvalds 		break;
18121da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_PROCESSOR:
1813c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
18141da177e4SLinus Torvalds 		break;
18151da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_THERMAL:
1816c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_THERMAL_HID);
18171da177e4SLinus Torvalds 		break;
18181da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
1819c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
18201da177e4SLinus Torvalds 		break;
18211da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
1822c0af4175SToshi Kani 		acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
18231da177e4SLinus Torvalds 		break;
18241da177e4SLinus Torvalds 	}
18251da177e4SLinus Torvalds }
18261da177e4SLinus Torvalds 
1827c0af4175SToshi Kani void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1828c0af4175SToshi Kani {
1829c0af4175SToshi Kani 	struct acpi_hardware_id *id, *tmp;
1830c0af4175SToshi Kani 
1831c0af4175SToshi Kani 	list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1832c0af4175SToshi Kani 		kfree(id->id);
1833c0af4175SToshi Kani 		kfree(id);
1834c0af4175SToshi Kani 	}
1835c0af4175SToshi Kani 	kfree(pnp->unique_id);
1836c0af4175SToshi Kani }
1837c0af4175SToshi Kani 
183882c7d5efSRafael J. Wysocki void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1839d43e167dSRafael J. Wysocki 			     int type, unsigned long long sta)
18401da177e4SLinus Torvalds {
1841d43e167dSRafael J. Wysocki 	INIT_LIST_HEAD(&device->pnp.ids);
1842d43e167dSRafael J. Wysocki 	device->device_type = type;
1843d43e167dSRafael J. Wysocki 	device->handle = handle;
1844d43e167dSRafael J. Wysocki 	device->parent = acpi_bus_get_parent(handle);
184525db115bSRafael J. Wysocki 	acpi_set_device_status(device, sta);
1846d43e167dSRafael J. Wysocki 	acpi_device_get_busid(device);
1847c0af4175SToshi Kani 	acpi_set_pnp_ids(handle, &device->pnp, type);
1848d43e167dSRafael J. Wysocki 	acpi_bus_get_flags(device);
18492c0d4fe0SRafael J. Wysocki 	device->flags.match_driver = false;
1850202317a5SRafael J. Wysocki 	device->flags.initialized = true;
1851202317a5SRafael J. Wysocki 	device->flags.visited = false;
1852cf860be6SRafael J. Wysocki 	device_initialize(&device->dev);
1853cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, true);
1854cf860be6SRafael J. Wysocki }
1855bc3b0772SBjorn Helgaas 
1856cf860be6SRafael J. Wysocki void acpi_device_add_finalize(struct acpi_device *device)
1857cf860be6SRafael J. Wysocki {
1858cf860be6SRafael J. Wysocki 	dev_set_uevent_suppress(&device->dev, false);
1859cf860be6SRafael J. Wysocki 	kobject_uevent(&device->dev.kobj, KOBJ_ADD);
18601da177e4SLinus Torvalds }
18611da177e4SLinus Torvalds 
18625c478f49SBjorn Helgaas static int acpi_add_single_object(struct acpi_device **child,
18635c478f49SBjorn Helgaas 				  acpi_handle handle, int type,
18642c0d4fe0SRafael J. Wysocki 				  unsigned long long sta)
18651da177e4SLinus Torvalds {
186677c24888SBjorn Helgaas 	int result;
186777c24888SBjorn Helgaas 	struct acpi_device *device;
186829aaefa6SBjorn Helgaas 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
18691da177e4SLinus Torvalds 
187036bcbec7SBurman Yan 	device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
18711da177e4SLinus Torvalds 	if (!device) {
18726468463aSLen Brown 		printk(KERN_ERR PREFIX "Memory allocation error\n");
1873d550d98dSPatrick Mochel 		return -ENOMEM;
18741da177e4SLinus Torvalds 	}
18751da177e4SLinus Torvalds 
1876d43e167dSRafael J. Wysocki 	acpi_init_device_object(device, handle, type, sta);
1877d43e167dSRafael J. Wysocki 	acpi_bus_get_power_flags(device);
1878d57d09a4SRafael J. Wysocki 	acpi_bus_get_wakeup_device_flags(device);
18791da177e4SLinus Torvalds 
1880cf860be6SRafael J. Wysocki 	result = acpi_device_add(device, acpi_device_release);
1881d43e167dSRafael J. Wysocki 	if (result) {
1882718fb0deSHugh Dickins 		acpi_device_release(&device->dev);
1883d550d98dSPatrick Mochel 		return result;
18841da177e4SLinus Torvalds 	}
18851da177e4SLinus Torvalds 
1886bc9b6407SRafael J. Wysocki 	acpi_power_add_remove_device(device, true);
1887cf860be6SRafael J. Wysocki 	acpi_device_add_finalize(device);
18881da177e4SLinus Torvalds 	acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1889d43e167dSRafael J. Wysocki 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1890d43e167dSRafael J. Wysocki 		dev_name(&device->dev), (char *) buffer.pointer,
1891d43e167dSRafael J. Wysocki 		device->parent ? dev_name(&device->parent->dev) : "(null)"));
18921da177e4SLinus Torvalds 	kfree(buffer.pointer);
18931da177e4SLinus Torvalds 	*child = device;
1894d43e167dSRafael J. Wysocki 	return 0;
1895bf325f95SRafael J. Wysocki }
1896bf325f95SRafael J. Wysocki 
1897778cbc1dSBjorn Helgaas static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1898778cbc1dSBjorn Helgaas 				    unsigned long long *sta)
1899778cbc1dSBjorn Helgaas {
1900778cbc1dSBjorn Helgaas 	acpi_status status;
1901778cbc1dSBjorn Helgaas 	acpi_object_type acpi_type;
1902778cbc1dSBjorn Helgaas 
1903778cbc1dSBjorn Helgaas 	status = acpi_get_type(handle, &acpi_type);
1904778cbc1dSBjorn Helgaas 	if (ACPI_FAILURE(status))
1905778cbc1dSBjorn Helgaas 		return -ENODEV;
1906778cbc1dSBjorn Helgaas 
1907778cbc1dSBjorn Helgaas 	switch (acpi_type) {
1908778cbc1dSBjorn Helgaas 	case ACPI_TYPE_ANY:		/* for ACPI_ROOT_OBJECT */
1909778cbc1dSBjorn Helgaas 	case ACPI_TYPE_DEVICE:
1910778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_DEVICE;
1911778cbc1dSBjorn Helgaas 		status = acpi_bus_get_status_handle(handle, sta);
1912778cbc1dSBjorn Helgaas 		if (ACPI_FAILURE(status))
1913778cbc1dSBjorn Helgaas 			return -ENODEV;
1914778cbc1dSBjorn Helgaas 		break;
1915778cbc1dSBjorn Helgaas 	case ACPI_TYPE_PROCESSOR:
1916778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_PROCESSOR;
1917778cbc1dSBjorn Helgaas 		status = acpi_bus_get_status_handle(handle, sta);
1918778cbc1dSBjorn Helgaas 		if (ACPI_FAILURE(status))
1919778cbc1dSBjorn Helgaas 			return -ENODEV;
1920778cbc1dSBjorn Helgaas 		break;
1921778cbc1dSBjorn Helgaas 	case ACPI_TYPE_THERMAL:
1922778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_THERMAL;
1923778cbc1dSBjorn Helgaas 		*sta = ACPI_STA_DEFAULT;
1924778cbc1dSBjorn Helgaas 		break;
1925778cbc1dSBjorn Helgaas 	case ACPI_TYPE_POWER:
1926778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_POWER;
1927778cbc1dSBjorn Helgaas 		*sta = ACPI_STA_DEFAULT;
1928778cbc1dSBjorn Helgaas 		break;
1929778cbc1dSBjorn Helgaas 	default:
1930778cbc1dSBjorn Helgaas 		return -ENODEV;
1931778cbc1dSBjorn Helgaas 	}
1932778cbc1dSBjorn Helgaas 
1933778cbc1dSBjorn Helgaas 	return 0;
1934778cbc1dSBjorn Helgaas }
1935778cbc1dSBjorn Helgaas 
1936202317a5SRafael J. Wysocki bool acpi_device_is_present(struct acpi_device *adev)
1937202317a5SRafael J. Wysocki {
1938202317a5SRafael J. Wysocki 	if (adev->status.present || adev->status.functional)
1939202317a5SRafael J. Wysocki 		return true;
1940202317a5SRafael J. Wysocki 
1941202317a5SRafael J. Wysocki 	adev->flags.initialized = false;
1942202317a5SRafael J. Wysocki 	return false;
1943202317a5SRafael J. Wysocki }
1944202317a5SRafael J. Wysocki 
19454b59cc1fSRafael J. Wysocki static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
19464b59cc1fSRafael J. Wysocki 				       char *idstr,
1947c5698074SRafael J. Wysocki 				       const struct acpi_device_id **matchid)
1948c5698074SRafael J. Wysocki {
1949c5698074SRafael J. Wysocki 	const struct acpi_device_id *devid;
1950c5698074SRafael J. Wysocki 
1951c5698074SRafael J. Wysocki 	for (devid = handler->ids; devid->id[0]; devid++)
1952c5698074SRafael J. Wysocki 		if (!strcmp((char *)devid->id, idstr)) {
1953c5698074SRafael J. Wysocki 			if (matchid)
1954c5698074SRafael J. Wysocki 				*matchid = devid;
1955c5698074SRafael J. Wysocki 
19564b59cc1fSRafael J. Wysocki 			return true;
19574b59cc1fSRafael J. Wysocki 		}
19584b59cc1fSRafael J. Wysocki 
19594b59cc1fSRafael J. Wysocki 	return false;
19604b59cc1fSRafael J. Wysocki }
19614b59cc1fSRafael J. Wysocki 
19626b772e8fSToshi Kani static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
19636b772e8fSToshi Kani 					const struct acpi_device_id **matchid)
19646b772e8fSToshi Kani {
19656b772e8fSToshi Kani 	struct acpi_scan_handler *handler;
19666b772e8fSToshi Kani 
19676b772e8fSToshi Kani 	list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
19686b772e8fSToshi Kani 		if (acpi_scan_handler_matching(handler, idstr, matchid))
19696b772e8fSToshi Kani 			return handler;
19706b772e8fSToshi Kani 
19716b772e8fSToshi Kani 	return NULL;
19726b772e8fSToshi Kani }
19736b772e8fSToshi Kani 
19743f8055c3SRafael J. Wysocki void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
19753f8055c3SRafael J. Wysocki {
19763f8055c3SRafael J. Wysocki 	if (!!hotplug->enabled == !!val)
19773f8055c3SRafael J. Wysocki 		return;
19783f8055c3SRafael J. Wysocki 
19793f8055c3SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
19803f8055c3SRafael J. Wysocki 
19813f8055c3SRafael J. Wysocki 	hotplug->enabled = val;
19823f8055c3SRafael J. Wysocki 
19833f8055c3SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
19843f8055c3SRafael J. Wysocki }
19853f8055c3SRafael J. Wysocki 
19863c2cc7ffSRafael J. Wysocki static void acpi_scan_init_hotplug(struct acpi_device *adev)
19874b59cc1fSRafael J. Wysocki {
19886b772e8fSToshi Kani 	struct acpi_hardware_id *hwid;
19893c2cc7ffSRafael J. Wysocki 
1990b43109faSRafael J. Wysocki 	if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
19911e2380cdSRafael J. Wysocki 		acpi_dock_add(adev);
19921e2380cdSRafael J. Wysocki 		return;
19931e2380cdSRafael J. Wysocki 	}
19943c2cc7ffSRafael J. Wysocki 	list_for_each_entry(hwid, &adev->pnp.ids, list) {
19954b59cc1fSRafael J. Wysocki 		struct acpi_scan_handler *handler;
19964b59cc1fSRafael J. Wysocki 
19976b772e8fSToshi Kani 		handler = acpi_scan_match_handler(hwid->id, NULL);
19981a699476SRafael J. Wysocki 		if (handler) {
19993c2cc7ffSRafael J. Wysocki 			adev->flags.hotplug_notify = true;
20006b772e8fSToshi Kani 			break;
20016b772e8fSToshi Kani 		}
20026b772e8fSToshi Kani 	}
20031a699476SRafael J. Wysocki }
20046b772e8fSToshi Kani 
2005e3863094SRafael J. Wysocki static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
2006e3863094SRafael J. Wysocki 				      void *not_used, void **return_value)
20071da177e4SLinus Torvalds {
2008805d410fSRafael J. Wysocki 	struct acpi_device *device = NULL;
2009778cbc1dSBjorn Helgaas 	int type;
2010778cbc1dSBjorn Helgaas 	unsigned long long sta;
2011778cbc1dSBjorn Helgaas 	int result;
20121da177e4SLinus Torvalds 
20134002bf38SRafael J. Wysocki 	acpi_bus_get_device(handle, &device);
20144002bf38SRafael J. Wysocki 	if (device)
20154002bf38SRafael J. Wysocki 		goto out;
20164002bf38SRafael J. Wysocki 
2017778cbc1dSBjorn Helgaas 	result = acpi_bus_type_and_status(handle, &type, &sta);
2018778cbc1dSBjorn Helgaas 	if (result)
201951a85fafSBjorn Helgaas 		return AE_OK;
20201da177e4SLinus Torvalds 
202182c7d5efSRafael J. Wysocki 	if (type == ACPI_BUS_TYPE_POWER) {
202282c7d5efSRafael J. Wysocki 		acpi_add_power_resource(handle);
202382c7d5efSRafael J. Wysocki 		return AE_OK;
202482c7d5efSRafael J. Wysocki 	}
202582c7d5efSRafael J. Wysocki 
20262c0d4fe0SRafael J. Wysocki 	acpi_add_single_object(&device, handle, type, sta);
2027e3b87f8aSBjorn Helgaas 	if (!device)
202851a85fafSBjorn Helgaas 		return AE_CTRL_DEPTH;
20291da177e4SLinus Torvalds 
20303c2cc7ffSRafael J. Wysocki 	acpi_scan_init_hotplug(device);
20313c2cc7ffSRafael J. Wysocki 
20324002bf38SRafael J. Wysocki  out:
203351a85fafSBjorn Helgaas 	if (!*return_value)
203451a85fafSBjorn Helgaas 		*return_value = device;
2035805d410fSRafael J. Wysocki 
203651a85fafSBjorn Helgaas 	return AE_OK;
20371da177e4SLinus Torvalds }
20381da177e4SLinus Torvalds 
203987b85b3cSRafael J. Wysocki static int acpi_scan_attach_handler(struct acpi_device *device)
204087b85b3cSRafael J. Wysocki {
204187b85b3cSRafael J. Wysocki 	struct acpi_hardware_id *hwid;
204287b85b3cSRafael J. Wysocki 	int ret = 0;
204387b85b3cSRafael J. Wysocki 
204487b85b3cSRafael J. Wysocki 	list_for_each_entry(hwid, &device->pnp.ids, list) {
2045c5698074SRafael J. Wysocki 		const struct acpi_device_id *devid;
2046c5698074SRafael J. Wysocki 		struct acpi_scan_handler *handler;
204787b85b3cSRafael J. Wysocki 
2048c5698074SRafael J. Wysocki 		handler = acpi_scan_match_handler(hwid->id, &devid);
2049c5698074SRafael J. Wysocki 		if (handler) {
2050c5698074SRafael J. Wysocki 			ret = handler->attach(device, devid);
2051c5698074SRafael J. Wysocki 			if (ret > 0) {
2052c5698074SRafael J. Wysocki 				device->handler = handler;
2053c5698074SRafael J. Wysocki 				break;
2054c5698074SRafael J. Wysocki 			} else if (ret < 0) {
2055c5698074SRafael J. Wysocki 				break;
2056c5698074SRafael J. Wysocki 			}
2057c5698074SRafael J. Wysocki 		}
205887b85b3cSRafael J. Wysocki 	}
2059ca589f94SRafael J. Wysocki 	return ret;
2060ca589f94SRafael J. Wysocki }
2061ca589f94SRafael J. Wysocki 
20622c22e652SRafael J. Wysocki static void acpi_bus_attach(struct acpi_device *device)
206351a85fafSBjorn Helgaas {
20642c22e652SRafael J. Wysocki 	struct acpi_device *child;
20651e2380cdSRafael J. Wysocki 	acpi_handle ejd;
2066ca589f94SRafael J. Wysocki 	int ret;
20671da177e4SLinus Torvalds 
20681e2380cdSRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
20691e2380cdSRafael J. Wysocki 		register_dock_dependent_device(device, ejd);
20701e2380cdSRafael J. Wysocki 
20712c22e652SRafael J. Wysocki 	acpi_bus_get_status(device);
2072202317a5SRafael J. Wysocki 	/* Skip devices that are not present. */
20732c22e652SRafael J. Wysocki 	if (!acpi_device_is_present(device)) {
20742c22e652SRafael J. Wysocki 		device->flags.visited = false;
20752c22e652SRafael J. Wysocki 		return;
20762c22e652SRafael J. Wysocki 	}
20773a391a39SRafael J. Wysocki 	if (device->handler)
20782c22e652SRafael J. Wysocki 		goto ok;
20793a391a39SRafael J. Wysocki 
2080202317a5SRafael J. Wysocki 	if (!device->flags.initialized) {
2081202317a5SRafael J. Wysocki 		acpi_bus_update_power(device, NULL);
2082202317a5SRafael J. Wysocki 		device->flags.initialized = true;
2083202317a5SRafael J. Wysocki 	}
20842c22e652SRafael J. Wysocki 	device->flags.visited = false;
2085ca589f94SRafael J. Wysocki 	ret = acpi_scan_attach_handler(device);
20866931007cSRafael J. Wysocki 	if (ret < 0)
20872c22e652SRafael J. Wysocki 		return;
20886931007cSRafael J. Wysocki 
20896931007cSRafael J. Wysocki 	device->flags.match_driver = true;
20902c22e652SRafael J. Wysocki 	if (!ret) {
2091ca589f94SRafael J. Wysocki 		ret = device_attach(&device->dev);
2092202317a5SRafael J. Wysocki 		if (ret < 0)
20932c22e652SRafael J. Wysocki 			return;
20942c22e652SRafael J. Wysocki 	}
20952c22e652SRafael J. Wysocki 	device->flags.visited = true;
2096202317a5SRafael J. Wysocki 
2097202317a5SRafael J. Wysocki  ok:
20982c22e652SRafael J. Wysocki 	list_for_each_entry(child, &device->children, node)
20992c22e652SRafael J. Wysocki 		acpi_bus_attach(child);
2100805d410fSRafael J. Wysocki }
21013fb02738SRajesh Shah 
2102b8bd759aSRafael J. Wysocki /**
2103b8bd759aSRafael J. Wysocki  * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2104b8bd759aSRafael J. Wysocki  * @handle: Root of the namespace scope to scan.
2105b8bd759aSRafael J. Wysocki  *
2106b8bd759aSRafael J. Wysocki  * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2107b8bd759aSRafael J. Wysocki  * found devices.
2108b8bd759aSRafael J. Wysocki  *
2109b8bd759aSRafael J. Wysocki  * If no devices were found, -ENODEV is returned, but it does not mean that
2110b8bd759aSRafael J. Wysocki  * there has been a real error.  There just have been no suitable ACPI objects
2111b8bd759aSRafael J. Wysocki  * in the table trunk from which the kernel could create a device and add an
2112b8bd759aSRafael J. Wysocki  * appropriate driver.
21133757b948SRafael J. Wysocki  *
21143757b948SRafael J. Wysocki  * Must be called under acpi_scan_lock.
2115b8bd759aSRafael J. Wysocki  */
2116b8bd759aSRafael J. Wysocki int acpi_bus_scan(acpi_handle handle)
21173fb02738SRajesh Shah {
21181da177e4SLinus Torvalds 	void *device = NULL;
2119c511cc19SRafael J. Wysocki 
21200cd6ac52SRafael J. Wysocki 	if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
21211da177e4SLinus Torvalds 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2122e3863094SRafael J. Wysocki 				    acpi_bus_check_add, NULL, NULL, &device);
21233fb02738SRajesh Shah 
21242c22e652SRafael J. Wysocki 	if (device) {
21252c22e652SRafael J. Wysocki 		acpi_bus_attach(device);
21262c22e652SRafael J. Wysocki 		return 0;
21272c22e652SRafael J. Wysocki 	}
21282c22e652SRafael J. Wysocki 	return -ENODEV;
21293fb02738SRajesh Shah }
2130b8bd759aSRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_scan);
21311da177e4SLinus Torvalds 
21323757b948SRafael J. Wysocki /**
21332c22e652SRafael J. Wysocki  * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
21342c22e652SRafael J. Wysocki  * @adev: Root of the ACPI namespace scope to walk.
21353757b948SRafael J. Wysocki  *
21363757b948SRafael J. Wysocki  * Must be called under acpi_scan_lock.
21373757b948SRafael J. Wysocki  */
21382c22e652SRafael J. Wysocki void acpi_bus_trim(struct acpi_device *adev)
21391da177e4SLinus Torvalds {
21402c22e652SRafael J. Wysocki 	struct acpi_scan_handler *handler = adev->handler;
21412c22e652SRafael J. Wysocki 	struct acpi_device *child;
21422c22e652SRafael J. Wysocki 
21432c22e652SRafael J. Wysocki 	list_for_each_entry_reverse(child, &adev->children, node)
21442c22e652SRafael J. Wysocki 		acpi_bus_trim(child);
21452c22e652SRafael J. Wysocki 
2146a951c773SRafael J. Wysocki 	adev->flags.match_driver = false;
21472c22e652SRafael J. Wysocki 	if (handler) {
21482c22e652SRafael J. Wysocki 		if (handler->detach)
21492c22e652SRafael J. Wysocki 			handler->detach(adev);
21502c22e652SRafael J. Wysocki 
21512c22e652SRafael J. Wysocki 		adev->handler = NULL;
21522c22e652SRafael J. Wysocki 	} else {
21532c22e652SRafael J. Wysocki 		device_release_driver(&adev->dev);
21542c22e652SRafael J. Wysocki 	}
21551da177e4SLinus Torvalds 	/*
21562c22e652SRafael J. Wysocki 	 * Most likely, the device is going away, so put it into D3cold before
21572c22e652SRafael J. Wysocki 	 * that.
21581da177e4SLinus Torvalds 	 */
21592c22e652SRafael J. Wysocki 	acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
21602c22e652SRafael J. Wysocki 	adev->flags.initialized = false;
21612c22e652SRafael J. Wysocki 	adev->flags.visited = false;
21621da177e4SLinus Torvalds }
2163ceaba663SKristen Accardi EXPORT_SYMBOL_GPL(acpi_bus_trim);
2164ceaba663SKristen Accardi 
2165e8b945c9SBjorn Helgaas static int acpi_bus_scan_fixed(void)
21661da177e4SLinus Torvalds {
21671da177e4SLinus Torvalds 	int result = 0;
2168c4168bffSLi Shaohua 
21691da177e4SLinus Torvalds 	/*
21701da177e4SLinus Torvalds 	 * Enumerate all fixed-feature devices.
21711da177e4SLinus Torvalds 	 */
21722c0d4fe0SRafael J. Wysocki 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
21732c0d4fe0SRafael J. Wysocki 		struct acpi_device *device = NULL;
21742c0d4fe0SRafael J. Wysocki 
21755c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
2176c4168bffSLi Shaohua 						ACPI_BUS_TYPE_POWER_BUTTON,
21772c0d4fe0SRafael J. Wysocki 						ACPI_STA_DEFAULT);
21782c0d4fe0SRafael J. Wysocki 		if (result)
21792c0d4fe0SRafael J. Wysocki 			return result;
21802c0d4fe0SRafael J. Wysocki 
218188346167SRafael J. Wysocki 		device->flags.match_driver = true;
21822c0d4fe0SRafael J. Wysocki 		result = device_attach(&device->dev);
21832c0d4fe0SRafael J. Wysocki 		if (result < 0)
21842c0d4fe0SRafael J. Wysocki 			return result;
21852c0d4fe0SRafael J. Wysocki 
2186c10d7a13SDaniel Drake 		device_init_wakeup(&device->dev, true);
21873fb02738SRajesh Shah 	}
21881da177e4SLinus Torvalds 
21892c0d4fe0SRafael J. Wysocki 	if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
21902c0d4fe0SRafael J. Wysocki 		struct acpi_device *device = NULL;
21912c0d4fe0SRafael J. Wysocki 
21925c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
2193c4168bffSLi Shaohua 						ACPI_BUS_TYPE_SLEEP_BUTTON,
21942c0d4fe0SRafael J. Wysocki 						ACPI_STA_DEFAULT);
21952c0d4fe0SRafael J. Wysocki 		if (result)
21962c0d4fe0SRafael J. Wysocki 			return result;
21972c0d4fe0SRafael J. Wysocki 
219888346167SRafael J. Wysocki 		device->flags.match_driver = true;
21992c0d4fe0SRafael J. Wysocki 		result = device_attach(&device->dev);
22003fb02738SRajesh Shah 	}
22011da177e4SLinus Torvalds 
22022c0d4fe0SRafael J. Wysocki 	return result < 0 ? result : 0;
22031da177e4SLinus Torvalds }
22041da177e4SLinus Torvalds 
2205e747f274SBjorn Helgaas int __init acpi_scan_init(void)
22061da177e4SLinus Torvalds {
22071da177e4SLinus Torvalds 	int result;
22081da177e4SLinus Torvalds 
22095b327265SPatrick Mochel 	result = bus_register(&acpi_bus_type);
22105b327265SPatrick Mochel 	if (result) {
22115b327265SPatrick Mochel 		/* We don't want to quit even if we failed to add suspend/resume */
22125b327265SPatrick Mochel 		printk(KERN_ERR PREFIX "Could not register bus type\n");
22135b327265SPatrick Mochel 	}
22145b327265SPatrick Mochel 
221592ef2a25SRafael J. Wysocki 	acpi_pci_root_init();
22164daeaf68SRafael J. Wysocki 	acpi_pci_link_init();
2217ac212b69SRafael J. Wysocki 	acpi_processor_init();
2218141a297bSRafael J. Wysocki 	acpi_platform_init();
2219f58b082aSRafael J. Wysocki 	acpi_lpss_init();
22202fa97febSLan Tianyu 	acpi_cmos_rtc_init();
2221737f1a9fSRafael J. Wysocki 	acpi_container_init();
22220a347644SRafael J. Wysocki 	acpi_memory_hotplug_init();
222397d9a9e9SRafael J. Wysocki 
22243757b948SRafael J. Wysocki 	mutex_lock(&acpi_scan_lock);
22251da177e4SLinus Torvalds 	/*
22261da177e4SLinus Torvalds 	 * Enumerate devices in the ACPI namespace.
22271da177e4SLinus Torvalds 	 */
22280cd6ac52SRafael J. Wysocki 	result = acpi_bus_scan(ACPI_ROOT_OBJECT);
22291da177e4SLinus Torvalds 	if (result)
22303757b948SRafael J. Wysocki 		goto out;
22311da177e4SLinus Torvalds 
22320cd6ac52SRafael J. Wysocki 	result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
22331da177e4SLinus Torvalds 	if (result)
22343757b948SRafael J. Wysocki 		goto out;
2235b8bd759aSRafael J. Wysocki 
2236b8bd759aSRafael J. Wysocki 	result = acpi_bus_scan_fixed();
2237b8bd759aSRafael J. Wysocki 	if (result) {
2238202317a5SRafael J. Wysocki 		acpi_detach_data(acpi_root->handle, acpi_scan_drop_device);
2239202317a5SRafael J. Wysocki 		acpi_device_del(acpi_root);
2240202317a5SRafael J. Wysocki 		put_device(&acpi_root->dev);
22413757b948SRafael J. Wysocki 		goto out;
2242b8bd759aSRafael J. Wysocki 	}
2243b8bd759aSRafael J. Wysocki 
2244b8bd759aSRafael J. Wysocki 	acpi_update_all_gpes();
22453757b948SRafael J. Wysocki 
22463757b948SRafael J. Wysocki  out:
22473757b948SRafael J. Wysocki 	mutex_unlock(&acpi_scan_lock);
22483757b948SRafael J. Wysocki 	return result;
22491da177e4SLinus Torvalds }
2250