xref: /openbmc/linux/drivers/acpi/scan.c (revision 37239978778806ecba54da60676abb46870acebb)
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>
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #include <acpi/acpi_drivers.h>
151da177e4SLinus Torvalds 
16e60cc7a6SBjorn Helgaas #include "internal.h"
17e60cc7a6SBjorn Helgaas 
181da177e4SLinus Torvalds #define _COMPONENT		ACPI_BUS_COMPONENT
19f52fd66dSLen Brown ACPI_MODULE_NAME("scan");
201da177e4SLinus Torvalds #define STRUCT_TO_INT(s)	(*((int*)&s))
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 
29620e112cSThomas Renninger static const char *dummy_hid = "device";
302b2ae7c7SThomas Renninger 
311da177e4SLinus Torvalds static LIST_HEAD(acpi_device_list);
32e49bd2ddSZhang Rui static LIST_HEAD(acpi_bus_id_list);
339090589dSShaohua Li DEFINE_MUTEX(acpi_device_lock);
341da177e4SLinus Torvalds LIST_HEAD(acpi_wakeup_device_list);
351da177e4SLinus Torvalds 
36e49bd2ddSZhang Rui struct acpi_device_bus_id{
37bb095854SZhang Rui 	char bus_id[15];
38e49bd2ddSZhang Rui 	unsigned int instance_no;
39e49bd2ddSZhang Rui 	struct list_head node;
40e49bd2ddSZhang Rui };
4129b71a1cSThomas Renninger 
4229b71a1cSThomas Renninger /*
4329b71a1cSThomas Renninger  * Creates hid/cid(s) string needed for modalias and uevent
4429b71a1cSThomas Renninger  * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
4529b71a1cSThomas Renninger  * char *modalias: "acpi:IBM0001:ACPI0001"
4629b71a1cSThomas Renninger */
47b3e572d2SAdrian Bunk static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
48b3e572d2SAdrian Bunk 			   int size)
49b3e572d2SAdrian Bunk {
5029b71a1cSThomas Renninger 	int len;
515c9fcb5dSZhang Rui 	int count;
527f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id;
5329b71a1cSThomas Renninger 
542b2ae7c7SThomas Renninger 	if (list_empty(&acpi_dev->pnp.ids))
552b2ae7c7SThomas Renninger 		return 0;
562b2ae7c7SThomas Renninger 
575c9fcb5dSZhang Rui 	len = snprintf(modalias, size, "acpi:");
5829b71a1cSThomas Renninger 	size -= len;
5929b71a1cSThomas Renninger 
607f47fa6cSBjorn Helgaas 	list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
617f47fa6cSBjorn Helgaas 		count = snprintf(&modalias[len], size, "%s:", id->id);
625c9fcb5dSZhang Rui 		if (count < 0 || count >= size)
635c9fcb5dSZhang Rui 			return -EINVAL;
645c9fcb5dSZhang Rui 		len += count;
655c9fcb5dSZhang Rui 		size -= count;
665c9fcb5dSZhang Rui 	}
675c9fcb5dSZhang Rui 
6829b71a1cSThomas Renninger 	modalias[len] = '\0';
6929b71a1cSThomas Renninger 	return len;
7029b71a1cSThomas Renninger }
7129b71a1cSThomas Renninger 
7229b71a1cSThomas Renninger static ssize_t
7329b71a1cSThomas Renninger acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
7429b71a1cSThomas Renninger 	struct acpi_device *acpi_dev = to_acpi_device(dev);
7529b71a1cSThomas Renninger 	int len;
7629b71a1cSThomas Renninger 
7729b71a1cSThomas Renninger 	/* Device has no HID and no CID or string is >1024 */
7829b71a1cSThomas Renninger 	len = create_modalias(acpi_dev, buf, 1024);
7929b71a1cSThomas Renninger 	if (len <= 0)
8029b71a1cSThomas Renninger 		return 0;
8129b71a1cSThomas Renninger 	buf[len++] = '\n';
8229b71a1cSThomas Renninger 	return len;
8329b71a1cSThomas Renninger }
8429b71a1cSThomas Renninger static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
8529b71a1cSThomas Renninger 
86c8d72a5eSZhang Rui static void acpi_bus_hot_remove_device(void *context)
871da177e4SLinus Torvalds {
8826d46867SZhang Rui 	struct acpi_device *device;
8926d46867SZhang Rui 	acpi_handle handle = context;
901da177e4SLinus Torvalds 	struct acpi_object_list arg_list;
911da177e4SLinus Torvalds 	union acpi_object arg;
921da177e4SLinus Torvalds 	acpi_status status = AE_OK;
931da177e4SLinus Torvalds 
9426d46867SZhang Rui 	if (acpi_bus_get_device(handle, &device))
95c8d72a5eSZhang Rui 		return;
961da177e4SLinus Torvalds 
9726d46867SZhang Rui 	if (!device)
98c8d72a5eSZhang Rui 		return;
9926d46867SZhang Rui 
10026d46867SZhang Rui 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1010794469dSKay Sievers 		"Hot-removing device %s...\n", dev_name(&device->dev)));
10226d46867SZhang Rui 
10326d46867SZhang Rui 	if (acpi_bus_trim(device, 1)) {
10455ac9a01SLin Ming 		printk(KERN_ERR PREFIX
10555ac9a01SLin Ming 				"Removing device failed\n");
106c8d72a5eSZhang Rui 		return;
10726d46867SZhang Rui 	}
10826d46867SZhang Rui 
10926d46867SZhang Rui 	/* power off device */
11026d46867SZhang Rui 	status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
11126d46867SZhang Rui 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
11255ac9a01SLin Ming 		printk(KERN_WARNING PREFIX
11355ac9a01SLin Ming 				"Power-off device failed\n");
11426d46867SZhang Rui 
11526d46867SZhang Rui 	if (device->flags.lockable) {
1161da177e4SLinus Torvalds 		arg_list.count = 1;
1171da177e4SLinus Torvalds 		arg_list.pointer = &arg;
1181da177e4SLinus Torvalds 		arg.type = ACPI_TYPE_INTEGER;
1191da177e4SLinus Torvalds 		arg.integer.value = 0;
1201da177e4SLinus Torvalds 		acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
1211da177e4SLinus Torvalds 	}
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	arg_list.count = 1;
1241da177e4SLinus Torvalds 	arg_list.pointer = &arg;
1251da177e4SLinus Torvalds 	arg.type = ACPI_TYPE_INTEGER;
1261da177e4SLinus Torvalds 	arg.integer.value = 1;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 	/*
1291da177e4SLinus Torvalds 	 * TBD: _EJD support.
1301da177e4SLinus Torvalds 	 */
1311da177e4SLinus Torvalds 	status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
13226d46867SZhang Rui 	if (ACPI_FAILURE(status))
133c8d72a5eSZhang Rui 		printk(KERN_WARNING PREFIX
134c8d72a5eSZhang Rui 				"Eject device failed\n");
1351da177e4SLinus Torvalds 
136c8d72a5eSZhang Rui 	return;
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds static ssize_t
140f883d9dbSPatrick Mochel acpi_eject_store(struct device *d, struct device_attribute *attr,
141f883d9dbSPatrick Mochel 		const char *buf, size_t count)
1421da177e4SLinus Torvalds {
1431da177e4SLinus Torvalds 	int ret = count;
1441da177e4SLinus Torvalds 	acpi_status status;
1451da177e4SLinus Torvalds 	acpi_object_type type = 0;
146f883d9dbSPatrick Mochel 	struct acpi_device *acpi_device = to_acpi_device(d);
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	if ((!count) || (buf[0] != '1')) {
1491da177e4SLinus Torvalds 		return -EINVAL;
1501da177e4SLinus Torvalds 	}
1511da177e4SLinus Torvalds #ifndef FORCE_EJECT
152f883d9dbSPatrick Mochel 	if (acpi_device->driver == NULL) {
1531da177e4SLinus Torvalds 		ret = -ENODEV;
1541da177e4SLinus Torvalds 		goto err;
1551da177e4SLinus Torvalds 	}
1561da177e4SLinus Torvalds #endif
157f883d9dbSPatrick Mochel 	status = acpi_get_type(acpi_device->handle, &type);
158f883d9dbSPatrick Mochel 	if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
1591da177e4SLinus Torvalds 		ret = -ENODEV;
1601da177e4SLinus Torvalds 		goto err;
1611da177e4SLinus Torvalds 	}
1621da177e4SLinus Torvalds 
163c8d72a5eSZhang Rui 	acpi_os_hotplug_execute(acpi_bus_hot_remove_device, acpi_device->handle);
1641da177e4SLinus Torvalds err:
1651da177e4SLinus Torvalds 	return ret;
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds 
168f883d9dbSPatrick Mochel static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
169f883d9dbSPatrick Mochel 
170e49bd2ddSZhang Rui static ssize_t
171e49bd2ddSZhang Rui acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
172e49bd2ddSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
173e49bd2ddSZhang Rui 
174ea8d82fdSBjorn Helgaas 	return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
175e49bd2ddSZhang Rui }
176e49bd2ddSZhang Rui static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
177e49bd2ddSZhang Rui 
178e49bd2ddSZhang Rui static ssize_t
179e49bd2ddSZhang Rui acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
180e49bd2ddSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
181e49bd2ddSZhang Rui 	struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
182e49bd2ddSZhang Rui 	int result;
183e49bd2ddSZhang Rui 
184e49bd2ddSZhang Rui 	result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
185e49bd2ddSZhang Rui 	if (result)
186e49bd2ddSZhang Rui 		goto end;
187e49bd2ddSZhang Rui 
188e49bd2ddSZhang Rui 	result = sprintf(buf, "%s\n", (char*)path.pointer);
189e49bd2ddSZhang Rui 	kfree(path.pointer);
190e49bd2ddSZhang Rui end:
191e49bd2ddSZhang Rui 	return result;
192e49bd2ddSZhang Rui }
193e49bd2ddSZhang Rui static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
194e49bd2ddSZhang Rui 
195e49bd2ddSZhang Rui static int acpi_device_setup_files(struct acpi_device *dev)
196f883d9dbSPatrick Mochel {
197f883d9dbSPatrick Mochel 	acpi_status status;
198f883d9dbSPatrick Mochel 	acpi_handle temp;
199e49bd2ddSZhang Rui 	int result = 0;
200e49bd2ddSZhang Rui 
201e49bd2ddSZhang Rui 	/*
202e49bd2ddSZhang Rui 	 * Devices gotten from FADT don't have a "path" attribute
203e49bd2ddSZhang Rui 	 */
204e49bd2ddSZhang Rui 	if (dev->handle) {
205e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_path);
206e49bd2ddSZhang Rui 		if (result)
207e49bd2ddSZhang Rui 			goto end;
208e49bd2ddSZhang Rui 	}
209e49bd2ddSZhang Rui 
2102b2ae7c7SThomas Renninger 	if (!list_empty(&dev->pnp.ids)) {
211e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_hid);
212e49bd2ddSZhang Rui 		if (result)
213e49bd2ddSZhang Rui 			goto end;
214f883d9dbSPatrick Mochel 
21529b71a1cSThomas Renninger 		result = device_create_file(&dev->dev, &dev_attr_modalias);
21629b71a1cSThomas Renninger 		if (result)
21729b71a1cSThomas Renninger 			goto end;
2182b2ae7c7SThomas Renninger 	}
21929b71a1cSThomas Renninger 
220f883d9dbSPatrick Mochel         /*
221f883d9dbSPatrick Mochel          * If device has _EJ0, 'eject' file is created that is used to trigger
222f883d9dbSPatrick Mochel          * hot-removal function from userland.
223f883d9dbSPatrick Mochel          */
224f883d9dbSPatrick Mochel 	status = acpi_get_handle(dev->handle, "_EJ0", &temp);
225f883d9dbSPatrick Mochel 	if (ACPI_SUCCESS(status))
226e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_eject);
227e49bd2ddSZhang Rui end:
228e49bd2ddSZhang Rui 	return result;
229f883d9dbSPatrick Mochel }
230f883d9dbSPatrick Mochel 
231f883d9dbSPatrick Mochel static void acpi_device_remove_files(struct acpi_device *dev)
232f883d9dbSPatrick Mochel {
233f883d9dbSPatrick Mochel 	acpi_status status;
234f883d9dbSPatrick Mochel 	acpi_handle temp;
235f883d9dbSPatrick Mochel 
236f883d9dbSPatrick Mochel 	/*
237f883d9dbSPatrick Mochel 	 * If device has _EJ0, 'eject' file is created that is used to trigger
238f883d9dbSPatrick Mochel 	 * hot-removal function from userland.
239f883d9dbSPatrick Mochel 	 */
240f883d9dbSPatrick Mochel 	status = acpi_get_handle(dev->handle, "_EJ0", &temp);
241f883d9dbSPatrick Mochel 	if (ACPI_SUCCESS(status))
242f883d9dbSPatrick Mochel 		device_remove_file(&dev->dev, &dev_attr_eject);
243e49bd2ddSZhang Rui 
24429b71a1cSThomas Renninger 	device_remove_file(&dev->dev, &dev_attr_modalias);
245e49bd2ddSZhang Rui 	device_remove_file(&dev->dev, &dev_attr_hid);
246e49bd2ddSZhang Rui 	if (dev->handle)
247e49bd2ddSZhang Rui 		device_remove_file(&dev->dev, &dev_attr_path);
248f883d9dbSPatrick Mochel }
2491da177e4SLinus Torvalds /* --------------------------------------------------------------------------
2509e89dde2SZhang Rui 			ACPI Bus operations
2511da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
25229b71a1cSThomas Renninger 
25329b71a1cSThomas Renninger int acpi_match_device_ids(struct acpi_device *device,
25429b71a1cSThomas Renninger 			  const struct acpi_device_id *ids)
25529b71a1cSThomas Renninger {
25629b71a1cSThomas Renninger 	const struct acpi_device_id *id;
2577f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *hwid;
25829b71a1cSThomas Renninger 
25939a0ad87SZhao Yakui 	/*
26039a0ad87SZhao Yakui 	 * If the device is not present, it is unnecessary to load device
26139a0ad87SZhao Yakui 	 * driver for it.
26239a0ad87SZhao Yakui 	 */
26339a0ad87SZhao Yakui 	if (!device->status.present)
26439a0ad87SZhao Yakui 		return -ENODEV;
26539a0ad87SZhao Yakui 
2667f47fa6cSBjorn Helgaas 	for (id = ids; id->id[0]; id++)
2677f47fa6cSBjorn Helgaas 		list_for_each_entry(hwid, &device->pnp.ids, list)
2687f47fa6cSBjorn Helgaas 			if (!strcmp((char *) id->id, hwid->id))
26929b71a1cSThomas Renninger 				return 0;
27029b71a1cSThomas Renninger 
27129b71a1cSThomas Renninger 	return -ENOENT;
27229b71a1cSThomas Renninger }
27329b71a1cSThomas Renninger EXPORT_SYMBOL(acpi_match_device_ids);
27429b71a1cSThomas Renninger 
2757f47fa6cSBjorn Helgaas static void acpi_free_ids(struct acpi_device *device)
2767f47fa6cSBjorn Helgaas {
2777f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id, *tmp;
2787f47fa6cSBjorn Helgaas 
2797f47fa6cSBjorn Helgaas 	list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
2807f47fa6cSBjorn Helgaas 		kfree(id->id);
2817f47fa6cSBjorn Helgaas 		kfree(id);
2827f47fa6cSBjorn Helgaas 	}
2837f47fa6cSBjorn Helgaas }
2847f47fa6cSBjorn Helgaas 
2851890a97aSPatrick Mochel static void acpi_device_release(struct device *dev)
2861890a97aSPatrick Mochel {
2871890a97aSPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
2881890a97aSPatrick Mochel 
2897f47fa6cSBjorn Helgaas 	acpi_free_ids(acpi_dev);
2901890a97aSPatrick Mochel 	kfree(acpi_dev);
2911890a97aSPatrick Mochel }
2921890a97aSPatrick Mochel 
2939e89dde2SZhang Rui static int acpi_device_suspend(struct device *dev, pm_message_t state)
2949e89dde2SZhang Rui {
2951d268b0aSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
2965d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
2971da177e4SLinus Torvalds 
2985d9464a4SPatrick Mochel 	if (acpi_drv && acpi_drv->ops.suspend)
2995d9464a4SPatrick Mochel 		return acpi_drv->ops.suspend(acpi_dev, state);
3001da177e4SLinus Torvalds 	return 0;
3019e89dde2SZhang Rui }
3021da177e4SLinus Torvalds 
3039e89dde2SZhang Rui static int acpi_device_resume(struct device *dev)
3049e89dde2SZhang Rui {
3051d268b0aSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3065d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
3079e89dde2SZhang Rui 
3085d9464a4SPatrick Mochel 	if (acpi_drv && acpi_drv->ops.resume)
3095d9464a4SPatrick Mochel 		return acpi_drv->ops.resume(acpi_dev);
3109e89dde2SZhang Rui 	return 0;
3119e89dde2SZhang Rui }
3121da177e4SLinus Torvalds 
3135d9464a4SPatrick Mochel static int acpi_bus_match(struct device *dev, struct device_driver *drv)
3141da177e4SLinus Torvalds {
3155d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3165d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(drv);
3175d9464a4SPatrick Mochel 
31829b71a1cSThomas Renninger 	return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
3195d9464a4SPatrick Mochel }
3205d9464a4SPatrick Mochel 
3217eff2e7aSKay Sievers static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
3225d9464a4SPatrick Mochel {
3235d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3247eff2e7aSKay Sievers 	int len;
3255d9464a4SPatrick Mochel 
3262b2ae7c7SThomas Renninger 	if (list_empty(&acpi_dev->pnp.ids))
3272b2ae7c7SThomas Renninger 		return 0;
3282b2ae7c7SThomas Renninger 
3297eff2e7aSKay Sievers 	if (add_uevent_var(env, "MODALIAS="))
3307eff2e7aSKay Sievers 		return -ENOMEM;
3317eff2e7aSKay Sievers 	len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
3327eff2e7aSKay Sievers 			      sizeof(env->buf) - env->buflen);
3337eff2e7aSKay Sievers 	if (len >= (sizeof(env->buf) - env->buflen))
3347eff2e7aSKay Sievers 		return -ENOMEM;
3357eff2e7aSKay Sievers 	env->buflen += len;
3361da177e4SLinus Torvalds 	return 0;
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds 
33946ec8598SBjorn Helgaas static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
34046ec8598SBjorn Helgaas {
34146ec8598SBjorn Helgaas 	struct acpi_device *device = data;
34246ec8598SBjorn Helgaas 
34346ec8598SBjorn Helgaas 	device->driver->ops.notify(device, event);
34446ec8598SBjorn Helgaas }
34546ec8598SBjorn Helgaas 
34646ec8598SBjorn Helgaas static acpi_status acpi_device_notify_fixed(void *data)
34746ec8598SBjorn Helgaas {
34846ec8598SBjorn Helgaas 	struct acpi_device *device = data;
34946ec8598SBjorn Helgaas 
35053de5356SBjorn Helgaas 	/* Fixed hardware devices have no handles */
35153de5356SBjorn Helgaas 	acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
35246ec8598SBjorn Helgaas 	return AE_OK;
35346ec8598SBjorn Helgaas }
35446ec8598SBjorn Helgaas 
35546ec8598SBjorn Helgaas static int acpi_device_install_notify_handler(struct acpi_device *device)
35646ec8598SBjorn Helgaas {
35746ec8598SBjorn Helgaas 	acpi_status status;
35846ec8598SBjorn Helgaas 
359ccba2a36SBjorn Helgaas 	if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
36046ec8598SBjorn Helgaas 		status =
36146ec8598SBjorn Helgaas 		    acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
36246ec8598SBjorn Helgaas 						     acpi_device_notify_fixed,
36346ec8598SBjorn Helgaas 						     device);
364ccba2a36SBjorn Helgaas 	else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
36546ec8598SBjorn Helgaas 		status =
36646ec8598SBjorn Helgaas 		    acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
36746ec8598SBjorn Helgaas 						     acpi_device_notify_fixed,
36846ec8598SBjorn Helgaas 						     device);
36946ec8598SBjorn Helgaas 	else
37046ec8598SBjorn Helgaas 		status = acpi_install_notify_handler(device->handle,
37146ec8598SBjorn Helgaas 						     ACPI_DEVICE_NOTIFY,
37246ec8598SBjorn Helgaas 						     acpi_device_notify,
37346ec8598SBjorn Helgaas 						     device);
37446ec8598SBjorn Helgaas 
37546ec8598SBjorn Helgaas 	if (ACPI_FAILURE(status))
37646ec8598SBjorn Helgaas 		return -EINVAL;
37746ec8598SBjorn Helgaas 	return 0;
37846ec8598SBjorn Helgaas }
37946ec8598SBjorn Helgaas 
38046ec8598SBjorn Helgaas static void acpi_device_remove_notify_handler(struct acpi_device *device)
38146ec8598SBjorn Helgaas {
382ccba2a36SBjorn Helgaas 	if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
38346ec8598SBjorn Helgaas 		acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
38446ec8598SBjorn Helgaas 						acpi_device_notify_fixed);
385ccba2a36SBjorn Helgaas 	else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
38646ec8598SBjorn Helgaas 		acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
38746ec8598SBjorn Helgaas 						acpi_device_notify_fixed);
38846ec8598SBjorn Helgaas 	else
38946ec8598SBjorn Helgaas 		acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
39046ec8598SBjorn Helgaas 					   acpi_device_notify);
39146ec8598SBjorn Helgaas }
39246ec8598SBjorn Helgaas 
3935d9464a4SPatrick Mochel static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
3945d9464a4SPatrick Mochel static int acpi_start_single_object(struct acpi_device *);
3955d9464a4SPatrick Mochel static int acpi_device_probe(struct device * dev)
3965d9464a4SPatrick Mochel {
3975d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3985d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
3995d9464a4SPatrick Mochel 	int ret;
4005d9464a4SPatrick Mochel 
4015d9464a4SPatrick Mochel 	ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
4025d9464a4SPatrick Mochel 	if (!ret) {
403c4168bffSLi Shaohua 		if (acpi_dev->bus_ops.acpi_op_start)
4045d9464a4SPatrick Mochel 			acpi_start_single_object(acpi_dev);
40546ec8598SBjorn Helgaas 
40646ec8598SBjorn Helgaas 		if (acpi_drv->ops.notify) {
40746ec8598SBjorn Helgaas 			ret = acpi_device_install_notify_handler(acpi_dev);
40846ec8598SBjorn Helgaas 			if (ret) {
40946ec8598SBjorn Helgaas 				if (acpi_drv->ops.remove)
41046ec8598SBjorn Helgaas 					acpi_drv->ops.remove(acpi_dev,
41146ec8598SBjorn Helgaas 						     acpi_dev->removal_type);
41246ec8598SBjorn Helgaas 				return ret;
41346ec8598SBjorn Helgaas 			}
41446ec8598SBjorn Helgaas 		}
41546ec8598SBjorn Helgaas 
4165d9464a4SPatrick Mochel 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4175d9464a4SPatrick Mochel 			"Found driver [%s] for device [%s]\n",
4185d9464a4SPatrick Mochel 			acpi_drv->name, acpi_dev->pnp.bus_id));
4195d9464a4SPatrick Mochel 		get_device(dev);
4205d9464a4SPatrick Mochel 	}
4215d9464a4SPatrick Mochel 	return ret;
4225d9464a4SPatrick Mochel }
4235d9464a4SPatrick Mochel 
4245d9464a4SPatrick Mochel static int acpi_device_remove(struct device * dev)
4255d9464a4SPatrick Mochel {
4265d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
4275d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
4285d9464a4SPatrick Mochel 
4295d9464a4SPatrick Mochel 	if (acpi_drv) {
43046ec8598SBjorn Helgaas 		if (acpi_drv->ops.notify)
43146ec8598SBjorn Helgaas 			acpi_device_remove_notify_handler(acpi_dev);
4325d9464a4SPatrick Mochel 		if (acpi_drv->ops.remove)
43396333578SLi Shaohua 			acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
4345d9464a4SPatrick Mochel 	}
4355d9464a4SPatrick Mochel 	acpi_dev->driver = NULL;
436db89b4f0SPavel Machek 	acpi_dev->driver_data = NULL;
4375d9464a4SPatrick Mochel 
4385d9464a4SPatrick Mochel 	put_device(dev);
4395d9464a4SPatrick Mochel 	return 0;
4405d9464a4SPatrick Mochel }
4415d9464a4SPatrick Mochel 
44255955aadSDavid Brownell struct bus_type acpi_bus_type = {
4439e89dde2SZhang Rui 	.name		= "acpi",
4449e89dde2SZhang Rui 	.suspend	= acpi_device_suspend,
4459e89dde2SZhang Rui 	.resume		= acpi_device_resume,
4465d9464a4SPatrick Mochel 	.match		= acpi_bus_match,
4475d9464a4SPatrick Mochel 	.probe		= acpi_device_probe,
4485d9464a4SPatrick Mochel 	.remove		= acpi_device_remove,
4495d9464a4SPatrick Mochel 	.uevent		= acpi_device_uevent,
4501da177e4SLinus Torvalds };
4511da177e4SLinus Torvalds 
45266b7ed40SBjorn Helgaas static int acpi_device_register(struct acpi_device *device)
4531da177e4SLinus Torvalds {
454e49bd2ddSZhang Rui 	int result;
455e49bd2ddSZhang Rui 	struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
456e49bd2ddSZhang Rui 	int found = 0;
45766b7ed40SBjorn Helgaas 
4581da177e4SLinus Torvalds 	/*
4591da177e4SLinus Torvalds 	 * Linkage
4601da177e4SLinus Torvalds 	 * -------
4611da177e4SLinus Torvalds 	 * Link this device to its parent and siblings.
4621da177e4SLinus Torvalds 	 */
4631da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->children);
4641da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->node);
4651da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->wakeup_list);
4661da177e4SLinus Torvalds 
467e49bd2ddSZhang Rui 	new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
468e49bd2ddSZhang Rui 	if (!new_bus_id) {
469e49bd2ddSZhang Rui 		printk(KERN_ERR PREFIX "Memory allocation error\n");
470e49bd2ddSZhang Rui 		return -ENOMEM;
471e49bd2ddSZhang Rui 	}
472e49bd2ddSZhang Rui 
4739090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
474e49bd2ddSZhang Rui 	/*
475e49bd2ddSZhang Rui 	 * Find suitable bus_id and instance number in acpi_bus_id_list
476e49bd2ddSZhang Rui 	 * If failed, create one and link it into acpi_bus_id_list
477e49bd2ddSZhang Rui 	 */
478e49bd2ddSZhang Rui 	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
4791131b938SBjorn Helgaas 		if (!strcmp(acpi_device_bus_id->bus_id,
4801131b938SBjorn Helgaas 			    acpi_device_hid(device))) {
481e49bd2ddSZhang Rui 			acpi_device_bus_id->instance_no++;
482e49bd2ddSZhang Rui 			found = 1;
483e49bd2ddSZhang Rui 			kfree(new_bus_id);
484e49bd2ddSZhang Rui 			break;
485e49bd2ddSZhang Rui 		}
486e49bd2ddSZhang Rui 	}
487e49bd2ddSZhang Rui 	if (!found) {
488e49bd2ddSZhang Rui 		acpi_device_bus_id = new_bus_id;
4891131b938SBjorn Helgaas 		strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
490e49bd2ddSZhang Rui 		acpi_device_bus_id->instance_no = 0;
491e49bd2ddSZhang Rui 		list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
492e49bd2ddSZhang Rui 	}
4930794469dSKay Sievers 	dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
494e49bd2ddSZhang Rui 
49533b57150SLen Brown 	if (device->parent)
4961da177e4SLinus Torvalds 		list_add_tail(&device->node, &device->parent->children);
49733b57150SLen Brown 
4981da177e4SLinus Torvalds 	if (device->wakeup.flags.valid)
4991da177e4SLinus Torvalds 		list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
5009090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
5011da177e4SLinus Torvalds 
5021890a97aSPatrick Mochel 	if (device->parent)
50366b7ed40SBjorn Helgaas 		device->dev.parent = &device->parent->dev;
5041890a97aSPatrick Mochel 	device->dev.bus = &acpi_bus_type;
5051890a97aSPatrick Mochel 	device->dev.release = &acpi_device_release;
5068b12b922SAlex Chiang 	result = device_register(&device->dev);
507e49bd2ddSZhang Rui 	if (result) {
5088b12b922SAlex Chiang 		dev_err(&device->dev, "Error registering device\n");
509e49bd2ddSZhang Rui 		goto end;
510e49bd2ddSZhang Rui 	}
511f883d9dbSPatrick Mochel 
512e49bd2ddSZhang Rui 	result = acpi_device_setup_files(device);
513e49bd2ddSZhang Rui 	if (result)
5140794469dSKay Sievers 		printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
5150794469dSKay Sievers 		       dev_name(&device->dev));
516e49bd2ddSZhang Rui 
51796333578SLi Shaohua 	device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
518e49bd2ddSZhang Rui 	return 0;
519e49bd2ddSZhang Rui end:
5209090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
52133b57150SLen Brown 	if (device->parent)
522e49bd2ddSZhang Rui 		list_del(&device->node);
523e49bd2ddSZhang Rui 	list_del(&device->wakeup_list);
5249090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
525e49bd2ddSZhang Rui 	return result;
5261da177e4SLinus Torvalds }
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds static void acpi_device_unregister(struct acpi_device *device, int type)
5291da177e4SLinus Torvalds {
5309090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
53133b57150SLen Brown 	if (device->parent)
5321da177e4SLinus Torvalds 		list_del(&device->node);
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	list_del(&device->wakeup_list);
5359090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 	acpi_detach_data(device->handle, acpi_bus_data_handler);
5381890a97aSPatrick Mochel 
539f883d9dbSPatrick Mochel 	acpi_device_remove_files(device);
5401890a97aSPatrick Mochel 	device_unregister(&device->dev);
5411da177e4SLinus Torvalds }
5421da177e4SLinus Torvalds 
5439e89dde2SZhang Rui /* --------------------------------------------------------------------------
5449e89dde2SZhang Rui                                  Driver Management
5459e89dde2SZhang Rui    -------------------------------------------------------------------------- */
5461da177e4SLinus Torvalds /**
547d758a8faSRandy Dunlap  * acpi_bus_driver_init - add a device to a driver
548d758a8faSRandy Dunlap  * @device: the device to add and initialize
549d758a8faSRandy Dunlap  * @driver: driver for the device
550d758a8faSRandy Dunlap  *
5511da177e4SLinus Torvalds  * Used to initialize a device via its device driver.  Called whenever a
5521890a97aSPatrick Mochel  * driver is bound to a device.  Invokes the driver's add() ops.
5531da177e4SLinus Torvalds  */
5541da177e4SLinus Torvalds static int
5554be44fcdSLen Brown acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
5561da177e4SLinus Torvalds {
5571da177e4SLinus Torvalds 	int result = 0;
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	if (!device || !driver)
560d550d98dSPatrick Mochel 		return -EINVAL;
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	if (!driver->ops.add)
563d550d98dSPatrick Mochel 		return -ENOSYS;
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	result = driver->ops.add(device);
5661da177e4SLinus Torvalds 	if (result) {
5671da177e4SLinus Torvalds 		device->driver = NULL;
568db89b4f0SPavel Machek 		device->driver_data = NULL;
569d550d98dSPatrick Mochel 		return result;
5701da177e4SLinus Torvalds 	}
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	device->driver = driver;
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	/*
5751da177e4SLinus Torvalds 	 * TBD - Configuration Management: Assign resources to device based
5761da177e4SLinus Torvalds 	 * upon possible configuration and currently allocated resources.
5771da177e4SLinus Torvalds 	 */
5781da177e4SLinus Torvalds 
5794be44fcdSLen Brown 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
5804be44fcdSLen Brown 			  "Driver successfully bound to device\n"));
581d550d98dSPatrick Mochel 	return 0;
5823fb02738SRajesh Shah }
5833fb02738SRajesh Shah 
5848713cbefSAdrian Bunk static int acpi_start_single_object(struct acpi_device *device)
5853fb02738SRajesh Shah {
5863fb02738SRajesh Shah 	int result = 0;
5873fb02738SRajesh Shah 	struct acpi_driver *driver;
5883fb02738SRajesh Shah 
5893fb02738SRajesh Shah 
5903fb02738SRajesh Shah 	if (!(driver = device->driver))
591d550d98dSPatrick Mochel 		return 0;
5923fb02738SRajesh Shah 
5931da177e4SLinus Torvalds 	if (driver->ops.start) {
5941da177e4SLinus Torvalds 		result = driver->ops.start(device);
5951da177e4SLinus Torvalds 		if (result && driver->ops.remove)
5961da177e4SLinus Torvalds 			driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
5973fb02738SRajesh Shah 	}
5983fb02738SRajesh Shah 
599d550d98dSPatrick Mochel 	return result;
6001da177e4SLinus Torvalds }
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds /**
603d758a8faSRandy Dunlap  * acpi_bus_register_driver - register a driver with the ACPI bus
604d758a8faSRandy Dunlap  * @driver: driver being registered
605d758a8faSRandy Dunlap  *
6061da177e4SLinus Torvalds  * Registers a driver with the ACPI bus.  Searches the namespace for all
6079d9f749bSBjorn Helgaas  * devices that match the driver's criteria and binds.  Returns zero for
6089d9f749bSBjorn Helgaas  * success or a negative error status for failure.
6091da177e4SLinus Torvalds  */
6104be44fcdSLen Brown int acpi_bus_register_driver(struct acpi_driver *driver)
6111da177e4SLinus Torvalds {
6121890a97aSPatrick Mochel 	int ret;
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	if (acpi_disabled)
615d550d98dSPatrick Mochel 		return -ENODEV;
6161890a97aSPatrick Mochel 	driver->drv.name = driver->name;
6171890a97aSPatrick Mochel 	driver->drv.bus = &acpi_bus_type;
6181890a97aSPatrick Mochel 	driver->drv.owner = driver->owner;
6191da177e4SLinus Torvalds 
6201890a97aSPatrick Mochel 	ret = driver_register(&driver->drv);
6211890a97aSPatrick Mochel 	return ret;
6221da177e4SLinus Torvalds }
6231da177e4SLinus Torvalds 
6244be44fcdSLen Brown EXPORT_SYMBOL(acpi_bus_register_driver);
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds /**
627d758a8faSRandy Dunlap  * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
628d758a8faSRandy Dunlap  * @driver: driver to unregister
629d758a8faSRandy Dunlap  *
6301da177e4SLinus Torvalds  * Unregisters a driver with the ACPI bus.  Searches the namespace for all
6311da177e4SLinus Torvalds  * devices that match the driver's criteria and unbinds.
6321da177e4SLinus Torvalds  */
63306ea8e08SBjorn Helgaas void acpi_bus_unregister_driver(struct acpi_driver *driver)
6341da177e4SLinus Torvalds {
6351890a97aSPatrick Mochel 	driver_unregister(&driver->drv);
6361da177e4SLinus Torvalds }
6374be44fcdSLen Brown 
6381da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_unregister_driver);
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds /* --------------------------------------------------------------------------
6411da177e4SLinus Torvalds                                  Device Enumeration
6421da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
6435c478f49SBjorn Helgaas static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
6445c478f49SBjorn Helgaas {
6455c478f49SBjorn Helgaas 	acpi_status status;
6465c478f49SBjorn Helgaas 	int ret;
6475c478f49SBjorn Helgaas 	struct acpi_device *device;
6485c478f49SBjorn Helgaas 
6495c478f49SBjorn Helgaas 	/*
6505c478f49SBjorn Helgaas 	 * Fixed hardware devices do not appear in the namespace and do not
6515c478f49SBjorn Helgaas 	 * have handles, but we fabricate acpi_devices for them, so we have
6525c478f49SBjorn Helgaas 	 * to deal with them specially.
6535c478f49SBjorn Helgaas 	 */
6545c478f49SBjorn Helgaas 	if (handle == NULL)
6555c478f49SBjorn Helgaas 		return acpi_root;
6565c478f49SBjorn Helgaas 
6575c478f49SBjorn Helgaas 	do {
6585c478f49SBjorn Helgaas 		status = acpi_get_parent(handle, &handle);
6595c478f49SBjorn Helgaas 		if (status == AE_NULL_ENTRY)
6605c478f49SBjorn Helgaas 			return NULL;
6615c478f49SBjorn Helgaas 		if (ACPI_FAILURE(status))
6625c478f49SBjorn Helgaas 			return acpi_root;
6635c478f49SBjorn Helgaas 
6645c478f49SBjorn Helgaas 		ret = acpi_bus_get_device(handle, &device);
6655c478f49SBjorn Helgaas 		if (ret == 0)
6665c478f49SBjorn Helgaas 			return device;
6675c478f49SBjorn Helgaas 	} while (1);
6685c478f49SBjorn Helgaas }
6695c478f49SBjorn Helgaas 
670c8f7a62cSLen Brown acpi_status
671c8f7a62cSLen Brown acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
672c8f7a62cSLen Brown {
673c8f7a62cSLen Brown 	acpi_status status;
674c8f7a62cSLen Brown 	acpi_handle tmp;
675c8f7a62cSLen Brown 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
676c8f7a62cSLen Brown 	union acpi_object *obj;
677c8f7a62cSLen Brown 
678c8f7a62cSLen Brown 	status = acpi_get_handle(handle, "_EJD", &tmp);
679c8f7a62cSLen Brown 	if (ACPI_FAILURE(status))
680c8f7a62cSLen Brown 		return status;
681c8f7a62cSLen Brown 
682c8f7a62cSLen Brown 	status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
683c8f7a62cSLen Brown 	if (ACPI_SUCCESS(status)) {
684c8f7a62cSLen Brown 		obj = buffer.pointer;
6853b5fee59SHolger Macht 		status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
6863b5fee59SHolger Macht 					 ejd);
687c8f7a62cSLen Brown 		kfree(buffer.pointer);
688c8f7a62cSLen Brown 	}
689c8f7a62cSLen Brown 	return status;
690c8f7a62cSLen Brown }
691c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
692c8f7a62cSLen Brown 
6938e4319c4SBob Moore void acpi_bus_data_handler(acpi_handle handle, void *context)
6941da177e4SLinus Torvalds {
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 	/* TBD */
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	return;
6991da177e4SLinus Torvalds }
7001da177e4SLinus Torvalds 
7019e89dde2SZhang Rui static int acpi_bus_get_perf_flags(struct acpi_device *device)
7029e89dde2SZhang Rui {
7039e89dde2SZhang Rui 	device->performance.state = ACPI_STATE_UNKNOWN;
7049e89dde2SZhang Rui 	return 0;
7059e89dde2SZhang Rui }
7069e89dde2SZhang Rui 
7071da177e4SLinus Torvalds static acpi_status
708b581a7f9SRafael J. Wysocki acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
709b581a7f9SRafael J. Wysocki 					     struct acpi_device_wakeup *wakeup)
7101da177e4SLinus Torvalds {
711b581a7f9SRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
712b581a7f9SRafael J. Wysocki 	union acpi_object *package = NULL;
7131da177e4SLinus Torvalds 	union acpi_object *element = NULL;
714b581a7f9SRafael J. Wysocki 	acpi_status status;
715b581a7f9SRafael J. Wysocki 	int i = 0;
7161da177e4SLinus Torvalds 
717b581a7f9SRafael J. Wysocki 	if (!wakeup)
7181da177e4SLinus Torvalds 		return AE_BAD_PARAMETER;
7191da177e4SLinus Torvalds 
720b581a7f9SRafael J. Wysocki 	/* _PRW */
721b581a7f9SRafael J. Wysocki 	status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
722b581a7f9SRafael J. Wysocki 	if (ACPI_FAILURE(status)) {
723b581a7f9SRafael J. Wysocki 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
724b581a7f9SRafael J. Wysocki 		return status;
725b581a7f9SRafael J. Wysocki 	}
726b581a7f9SRafael J. Wysocki 
727b581a7f9SRafael J. Wysocki 	package = (union acpi_object *)buffer.pointer;
728b581a7f9SRafael J. Wysocki 
729b581a7f9SRafael J. Wysocki 	if (!package || (package->package.count < 2)) {
730b581a7f9SRafael J. Wysocki 		status = AE_BAD_DATA;
731b581a7f9SRafael J. Wysocki 		goto out;
732b581a7f9SRafael J. Wysocki 	}
733b581a7f9SRafael J. Wysocki 
7341da177e4SLinus Torvalds 	element = &(package->package.elements[0]);
735b581a7f9SRafael J. Wysocki 	if (!element) {
736b581a7f9SRafael J. Wysocki 		status = AE_BAD_DATA;
737b581a7f9SRafael J. Wysocki 		goto out;
738b581a7f9SRafael J. Wysocki 	}
7391da177e4SLinus Torvalds 	if (element->type == ACPI_TYPE_PACKAGE) {
7401da177e4SLinus Torvalds 		if ((element->package.count < 2) ||
7411da177e4SLinus Torvalds 		    (element->package.elements[0].type !=
7421da177e4SLinus Torvalds 		     ACPI_TYPE_LOCAL_REFERENCE)
743b581a7f9SRafael J. Wysocki 		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) {
744b581a7f9SRafael J. Wysocki 			status = AE_BAD_DATA;
745b581a7f9SRafael J. Wysocki 			goto out;
746b581a7f9SRafael J. Wysocki 		}
747b581a7f9SRafael J. Wysocki 		wakeup->gpe_device =
7481da177e4SLinus Torvalds 		    element->package.elements[0].reference.handle;
749b581a7f9SRafael J. Wysocki 		wakeup->gpe_number =
7501da177e4SLinus Torvalds 		    (u32) element->package.elements[1].integer.value;
7511da177e4SLinus Torvalds 	} else if (element->type == ACPI_TYPE_INTEGER) {
752b581a7f9SRafael J. Wysocki 		wakeup->gpe_device = NULL;
753b581a7f9SRafael J. Wysocki 		wakeup->gpe_number = element->integer.value;
754b581a7f9SRafael J. Wysocki 	} else {
755b581a7f9SRafael J. Wysocki 		status = AE_BAD_DATA;
756b581a7f9SRafael J. Wysocki 		goto out;
757b581a7f9SRafael J. Wysocki 	}
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	element = &(package->package.elements[1]);
7601da177e4SLinus Torvalds 	if (element->type != ACPI_TYPE_INTEGER) {
761b581a7f9SRafael J. Wysocki 		status = AE_BAD_DATA;
762b581a7f9SRafael J. Wysocki 		goto out;
7631da177e4SLinus Torvalds 	}
764b581a7f9SRafael J. Wysocki 	wakeup->sleep_state = element->integer.value;
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 	if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
767b581a7f9SRafael J. Wysocki 		status = AE_NO_MEMORY;
768b581a7f9SRafael J. Wysocki 		goto out;
7691da177e4SLinus Torvalds 	}
770b581a7f9SRafael J. Wysocki 	wakeup->resources.count = package->package.count - 2;
771b581a7f9SRafael J. Wysocki 	for (i = 0; i < wakeup->resources.count; i++) {
7721da177e4SLinus Torvalds 		element = &(package->package.elements[i + 2]);
773b581a7f9SRafael J. Wysocki 		if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
774b581a7f9SRafael J. Wysocki 			status = AE_BAD_DATA;
775b581a7f9SRafael J. Wysocki 			goto out;
7761da177e4SLinus Torvalds 		}
7771da177e4SLinus Torvalds 
778b581a7f9SRafael J. Wysocki 		wakeup->resources.handles[i] = element->reference.handle;
779b581a7f9SRafael J. Wysocki 	}
7809874647bSRafael J. Wysocki 
781bba63a29SLin Ming 	acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
782b581a7f9SRafael J. Wysocki 
783b581a7f9SRafael J. Wysocki  out:
784b581a7f9SRafael J. Wysocki 	kfree(buffer.pointer);
785b581a7f9SRafael J. Wysocki 
786b581a7f9SRafael J. Wysocki 	return status;
7871da177e4SLinus Torvalds }
7881da177e4SLinus Torvalds 
789f517709dSRafael J. Wysocki static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
7901da177e4SLinus Torvalds {
79129b71a1cSThomas Renninger 	struct acpi_device_id button_device_ids[] = {
79229b71a1cSThomas Renninger 		{"PNP0C0D", 0},
79329b71a1cSThomas Renninger 		{"PNP0C0C", 0},
79429b71a1cSThomas Renninger 		{"PNP0C0E", 0},
79529b71a1cSThomas Renninger 		{"", 0},
79629b71a1cSThomas Renninger 	};
797f517709dSRafael J. Wysocki 	acpi_status status;
798f517709dSRafael J. Wysocki 	acpi_event_status event_status;
799f517709dSRafael J. Wysocki 
800b67ea761SRafael J. Wysocki 	device->wakeup.flags.notifier_present = 0;
801f517709dSRafael J. Wysocki 
802f517709dSRafael J. Wysocki 	/* Power button, Lid switch always enable wakeup */
803f517709dSRafael J. Wysocki 	if (!acpi_match_device_ids(device, button_device_ids)) {
804f517709dSRafael J. Wysocki 		device->wakeup.flags.run_wake = 1;
805f2b56bc8SRafael J. Wysocki 		device_set_wakeup_capable(&device->dev, true);
806f517709dSRafael J. Wysocki 		return;
807f517709dSRafael J. Wysocki 	}
808f517709dSRafael J. Wysocki 
809e8e18c95SRafael J. Wysocki 	status = acpi_get_gpe_status(device->wakeup.gpe_device,
810e8e18c95SRafael J. Wysocki 					device->wakeup.gpe_number,
8110f849d2cSLin Ming 						&event_status);
812f517709dSRafael J. Wysocki 	if (status == AE_OK)
813f517709dSRafael J. Wysocki 		device->wakeup.flags.run_wake =
814f517709dSRafael J. Wysocki 				!!(event_status & ACPI_EVENT_FLAG_HANDLE);
815f517709dSRafael J. Wysocki }
816f517709dSRafael J. Wysocki 
817d57d09a4SRafael J. Wysocki static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
818f517709dSRafael J. Wysocki {
819d57d09a4SRafael J. Wysocki 	acpi_handle temp;
820f517709dSRafael J. Wysocki 	acpi_status status = 0;
821f517709dSRafael J. Wysocki 	int psw_error;
82229b71a1cSThomas Renninger 
823d57d09a4SRafael J. Wysocki 	/* Presence of _PRW indicates wake capable */
824d57d09a4SRafael J. Wysocki 	status = acpi_get_handle(device->handle, "_PRW", &temp);
825d57d09a4SRafael J. Wysocki 	if (ACPI_FAILURE(status))
826d57d09a4SRafael J. Wysocki 		return;
827d57d09a4SRafael J. Wysocki 
828b581a7f9SRafael J. Wysocki 	status = acpi_bus_extract_wakeup_device_power_package(device->handle,
829b581a7f9SRafael J. Wysocki 							      &device->wakeup);
8301da177e4SLinus Torvalds 	if (ACPI_FAILURE(status)) {
8311da177e4SLinus Torvalds 		ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
832d57d09a4SRafael J. Wysocki 		return;
8331da177e4SLinus Torvalds 	}
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	device->wakeup.flags.valid = 1;
8369b83ccd2SRafael J. Wysocki 	device->wakeup.prepare_count = 0;
837f517709dSRafael J. Wysocki 	acpi_bus_set_run_wake_flags(device);
838729b2bdbSZhao Yakui 	/* Call _PSW/_DSW object to disable its ability to wake the sleeping
839729b2bdbSZhao Yakui 	 * system for the ACPI device with the _PRW object.
840729b2bdbSZhao Yakui 	 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
841729b2bdbSZhao Yakui 	 * So it is necessary to call _DSW object first. Only when it is not
842729b2bdbSZhao Yakui 	 * present will the _PSW object used.
843729b2bdbSZhao Yakui 	 */
84477e76609SRafael J. Wysocki 	psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
84577e76609SRafael J. Wysocki 	if (psw_error)
84677e76609SRafael J. Wysocki 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
84777e76609SRafael J. Wysocki 				"error in _DSW or _PSW evaluation\n"));
8481da177e4SLinus Torvalds }
8491da177e4SLinus Torvalds 
850bf325f95SRafael J. Wysocki static void acpi_bus_add_power_resource(acpi_handle handle);
851bf325f95SRafael J. Wysocki 
8529e89dde2SZhang Rui static int acpi_bus_get_power_flags(struct acpi_device *device)
8531da177e4SLinus Torvalds {
8549e89dde2SZhang Rui 	acpi_status status = 0;
8559e89dde2SZhang Rui 	acpi_handle handle = NULL;
8569e89dde2SZhang Rui 	u32 i = 0;
8579e89dde2SZhang Rui 
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	/*
8609e89dde2SZhang Rui 	 * Power Management Flags
8611da177e4SLinus Torvalds 	 */
8629e89dde2SZhang Rui 	status = acpi_get_handle(device->handle, "_PSC", &handle);
8631da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
8649e89dde2SZhang Rui 		device->power.flags.explicit_get = 1;
8659e89dde2SZhang Rui 	status = acpi_get_handle(device->handle, "_IRC", &handle);
8669e89dde2SZhang Rui 	if (ACPI_SUCCESS(status))
8679e89dde2SZhang Rui 		device->power.flags.inrush_current = 1;
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	/*
8709e89dde2SZhang Rui 	 * Enumerate supported power management states
8711da177e4SLinus Torvalds 	 */
8729e89dde2SZhang Rui 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
8739e89dde2SZhang Rui 		struct acpi_device_power_state *ps = &device->power.states[i];
8749e89dde2SZhang Rui 		char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
8751da177e4SLinus Torvalds 
8769e89dde2SZhang Rui 		/* Evaluate "_PRx" to se if power resources are referenced */
8779e89dde2SZhang Rui 		acpi_evaluate_reference(device->handle, object_name, NULL,
8789e89dde2SZhang Rui 					&ps->resources);
8799e89dde2SZhang Rui 		if (ps->resources.count) {
880bf325f95SRafael J. Wysocki 			int j;
881bf325f95SRafael J. Wysocki 
8829e89dde2SZhang Rui 			device->power.flags.power_resources = 1;
883bf325f95SRafael J. Wysocki 			for (j = 0; j < ps->resources.count; j++)
884bf325f95SRafael J. Wysocki 				acpi_bus_add_power_resource(ps->resources.handles[j]);
8851da177e4SLinus Torvalds 		}
8861da177e4SLinus Torvalds 
8879e89dde2SZhang Rui 		/* Evaluate "_PSx" to see if we can do explicit sets */
8889e89dde2SZhang Rui 		object_name[2] = 'S';
8899e89dde2SZhang Rui 		status = acpi_get_handle(device->handle, object_name, &handle);
890*37239978SAlex He 		if (ACPI_SUCCESS(status))
8919e89dde2SZhang Rui 			ps->flags.explicit_set = 1;
892c8f7a62cSLen Brown 
8939e89dde2SZhang Rui 		/* State is valid if we have some power control */
8949e89dde2SZhang Rui 		if (ps->resources.count || ps->flags.explicit_set)
8959e89dde2SZhang Rui 			ps->flags.valid = 1;
8969e89dde2SZhang Rui 
8979e89dde2SZhang Rui 		ps->power = -1;	/* Unknown - driver assigned */
8989e89dde2SZhang Rui 		ps->latency = -1;	/* Unknown - driver assigned */
8999e89dde2SZhang Rui 	}
9009e89dde2SZhang Rui 
9019e89dde2SZhang Rui 	/* Set defaults for D0 and D3 states (always valid) */
9029e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].flags.valid = 1;
9039e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].power = 100;
9049e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D3].flags.valid = 1;
9059e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D3].power = 0;
9069e89dde2SZhang Rui 
907ade3e7feSRafael J. Wysocki 	acpi_bus_init_power(device);
9089e89dde2SZhang Rui 
9099e89dde2SZhang Rui 	return 0;
9109e89dde2SZhang Rui }
911c8f7a62cSLen Brown 
9124be44fcdSLen Brown static int acpi_bus_get_flags(struct acpi_device *device)
9131da177e4SLinus Torvalds {
9141da177e4SLinus Torvalds 	acpi_status status = AE_OK;
9151da177e4SLinus Torvalds 	acpi_handle temp = NULL;
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	/* Presence of _STA indicates 'dynamic_status' */
9191da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_STA", &temp);
9201da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
9211da177e4SLinus Torvalds 		device->flags.dynamic_status = 1;
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds 	/* Presence of _RMV indicates 'removable' */
9241da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_RMV", &temp);
9251da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
9261da177e4SLinus Torvalds 		device->flags.removable = 1;
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
9291da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_EJD", &temp);
9301da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
9311da177e4SLinus Torvalds 		device->flags.ejectable = 1;
9321da177e4SLinus Torvalds 	else {
9331da177e4SLinus Torvalds 		status = acpi_get_handle(device->handle, "_EJ0", &temp);
9341da177e4SLinus Torvalds 		if (ACPI_SUCCESS(status))
9351da177e4SLinus Torvalds 			device->flags.ejectable = 1;
9361da177e4SLinus Torvalds 	}
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds 	/* Presence of _LCK indicates 'lockable' */
9391da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_LCK", &temp);
9401da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
9411da177e4SLinus Torvalds 		device->flags.lockable = 1;
9421da177e4SLinus Torvalds 
9437bed50c5SRafael J. Wysocki 	/* Power resources cannot be power manageable. */
9447bed50c5SRafael J. Wysocki 	if (device->device_type == ACPI_BUS_TYPE_POWER)
9457bed50c5SRafael J. Wysocki 		return 0;
9467bed50c5SRafael J. Wysocki 
9471da177e4SLinus Torvalds 	/* Presence of _PS0|_PR0 indicates 'power manageable' */
9481da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_PS0", &temp);
9491da177e4SLinus Torvalds 	if (ACPI_FAILURE(status))
9501da177e4SLinus Torvalds 		status = acpi_get_handle(device->handle, "_PR0", &temp);
9511da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
9521da177e4SLinus Torvalds 		device->flags.power_manageable = 1;
9531da177e4SLinus Torvalds 
9543c5f9be4SJoe Perches 	/* TBD: Performance management */
9551da177e4SLinus Torvalds 
956d550d98dSPatrick Mochel 	return 0;
9571da177e4SLinus Torvalds }
9581da177e4SLinus Torvalds 
959c7bcb4e9SBjorn Helgaas static void acpi_device_get_busid(struct acpi_device *device)
9601da177e4SLinus Torvalds {
9611da177e4SLinus Torvalds 	char bus_id[5] = { '?', 0 };
9621da177e4SLinus Torvalds 	struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
9631da177e4SLinus Torvalds 	int i = 0;
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	/*
9661da177e4SLinus Torvalds 	 * Bus ID
9671da177e4SLinus Torvalds 	 * ------
9681da177e4SLinus Torvalds 	 * The device's Bus ID is simply the object name.
9691da177e4SLinus Torvalds 	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
9701da177e4SLinus Torvalds 	 */
971859ac9a4SBjorn Helgaas 	if (ACPI_IS_ROOT_DEVICE(device)) {
9721da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "ACPI");
973859ac9a4SBjorn Helgaas 		return;
974859ac9a4SBjorn Helgaas 	}
975859ac9a4SBjorn Helgaas 
976859ac9a4SBjorn Helgaas 	switch (device->device_type) {
9771da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
9781da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "PWRF");
9791da177e4SLinus Torvalds 		break;
9801da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
9811da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "SLPF");
9821da177e4SLinus Torvalds 		break;
9831da177e4SLinus Torvalds 	default:
98466b7ed40SBjorn Helgaas 		acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
9851da177e4SLinus Torvalds 		/* Clean up trailing underscores (if any) */
9861da177e4SLinus Torvalds 		for (i = 3; i > 1; i--) {
9871da177e4SLinus Torvalds 			if (bus_id[i] == '_')
9881da177e4SLinus Torvalds 				bus_id[i] = '\0';
9891da177e4SLinus Torvalds 			else
9901da177e4SLinus Torvalds 				break;
9911da177e4SLinus Torvalds 		}
9921da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, bus_id);
9931da177e4SLinus Torvalds 		break;
9941da177e4SLinus Torvalds 	}
9951da177e4SLinus Torvalds }
9961da177e4SLinus Torvalds 
99754735266SZhang Rui /*
99854735266SZhang Rui  * acpi_bay_match - see if a device is an ejectable driver bay
99954735266SZhang Rui  *
100054735266SZhang Rui  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
100154735266SZhang Rui  * then we can safely call it an ejectable drive bay
100254735266SZhang Rui  */
100354735266SZhang Rui static int acpi_bay_match(struct acpi_device *device){
100454735266SZhang Rui 	acpi_status status;
100554735266SZhang Rui 	acpi_handle handle;
100654735266SZhang Rui 	acpi_handle tmp;
100754735266SZhang Rui 	acpi_handle phandle;
100854735266SZhang Rui 
100954735266SZhang Rui 	handle = device->handle;
101054735266SZhang Rui 
101154735266SZhang Rui 	status = acpi_get_handle(handle, "_EJ0", &tmp);
101254735266SZhang Rui 	if (ACPI_FAILURE(status))
101354735266SZhang Rui 		return -ENODEV;
101454735266SZhang Rui 
101554735266SZhang Rui 	if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
101654735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
101754735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
101854735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
101954735266SZhang Rui 		return 0;
102054735266SZhang Rui 
102154735266SZhang Rui 	if (acpi_get_parent(handle, &phandle))
102254735266SZhang Rui 		return -ENODEV;
102354735266SZhang Rui 
102454735266SZhang Rui         if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
102554735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
102654735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
102754735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
102854735266SZhang Rui                 return 0;
102954735266SZhang Rui 
103054735266SZhang Rui 	return -ENODEV;
103154735266SZhang Rui }
103254735266SZhang Rui 
10333620f2f2SFrank Seidel /*
10343620f2f2SFrank Seidel  * acpi_dock_match - see if a device has a _DCK method
10353620f2f2SFrank Seidel  */
10363620f2f2SFrank Seidel static int acpi_dock_match(struct acpi_device *device)
10373620f2f2SFrank Seidel {
10383620f2f2SFrank Seidel 	acpi_handle tmp;
10393620f2f2SFrank Seidel 	return acpi_get_handle(device->handle, "_DCK", &tmp);
10403620f2f2SFrank Seidel }
10413620f2f2SFrank Seidel 
1042620e112cSThomas Renninger const char *acpi_device_hid(struct acpi_device *device)
104315b8dd53SBob Moore {
10447f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *hid;
104515b8dd53SBob Moore 
10462b2ae7c7SThomas Renninger 	if (list_empty(&device->pnp.ids))
10472b2ae7c7SThomas Renninger 		return dummy_hid;
10482b2ae7c7SThomas Renninger 
10497f47fa6cSBjorn Helgaas 	hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
10507f47fa6cSBjorn Helgaas 	return hid->id;
10517f47fa6cSBjorn Helgaas }
10527f47fa6cSBjorn Helgaas EXPORT_SYMBOL(acpi_device_hid);
105315b8dd53SBob Moore 
10547f47fa6cSBjorn Helgaas static void acpi_add_id(struct acpi_device *device, const char *dev_id)
10557f47fa6cSBjorn Helgaas {
10567f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id;
105715b8dd53SBob Moore 
10587f47fa6cSBjorn Helgaas 	id = kmalloc(sizeof(*id), GFP_KERNEL);
10597f47fa6cSBjorn Helgaas 	if (!id)
10607f47fa6cSBjorn Helgaas 		return;
106115b8dd53SBob Moore 
1062581de59eSThomas Meyer 	id->id = kstrdup(dev_id, GFP_KERNEL);
10637f47fa6cSBjorn Helgaas 	if (!id->id) {
10647f47fa6cSBjorn Helgaas 		kfree(id);
10657f47fa6cSBjorn Helgaas 		return;
106615b8dd53SBob Moore 	}
106715b8dd53SBob Moore 
10687f47fa6cSBjorn Helgaas 	list_add_tail(&id->list, &device->pnp.ids);
106915b8dd53SBob Moore }
107015b8dd53SBob Moore 
1071222e82acSDarrick J. Wong /*
1072222e82acSDarrick J. Wong  * Old IBM workstations have a DSDT bug wherein the SMBus object
1073222e82acSDarrick J. Wong  * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1074222e82acSDarrick J. Wong  * prefix.  Work around this.
1075222e82acSDarrick J. Wong  */
1076222e82acSDarrick J. Wong static int acpi_ibm_smbus_match(struct acpi_device *device)
1077222e82acSDarrick J. Wong {
1078222e82acSDarrick J. Wong 	acpi_handle h_dummy;
1079222e82acSDarrick J. Wong 	struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1080222e82acSDarrick J. Wong 	int result;
1081222e82acSDarrick J. Wong 
1082222e82acSDarrick J. Wong 	if (!dmi_name_in_vendors("IBM"))
1083222e82acSDarrick J. Wong 		return -ENODEV;
1084222e82acSDarrick J. Wong 
1085222e82acSDarrick J. Wong 	/* Look for SMBS object */
1086222e82acSDarrick J. Wong 	result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1087222e82acSDarrick J. Wong 	if (result)
1088222e82acSDarrick J. Wong 		return result;
1089222e82acSDarrick J. Wong 
1090222e82acSDarrick J. Wong 	if (strcmp("SMBS", path.pointer)) {
1091222e82acSDarrick J. Wong 		result = -ENODEV;
1092222e82acSDarrick J. Wong 		goto out;
1093222e82acSDarrick J. Wong 	}
1094222e82acSDarrick J. Wong 
1095222e82acSDarrick J. Wong 	/* Does it have the necessary (but misnamed) methods? */
1096222e82acSDarrick J. Wong 	result = -ENODEV;
1097222e82acSDarrick J. Wong 	if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1098222e82acSDarrick J. Wong 	    ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1099222e82acSDarrick J. Wong 	    ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1100222e82acSDarrick J. Wong 		result = 0;
1101222e82acSDarrick J. Wong out:
1102222e82acSDarrick J. Wong 	kfree(path.pointer);
1103222e82acSDarrick J. Wong 	return result;
1104222e82acSDarrick J. Wong }
1105222e82acSDarrick J. Wong 
1106c7bcb4e9SBjorn Helgaas static void acpi_device_set_id(struct acpi_device *device)
11071da177e4SLinus Torvalds {
11081da177e4SLinus Torvalds 	acpi_status status;
110957f3674fSBjorn Helgaas 	struct acpi_device_info *info;
111057f3674fSBjorn Helgaas 	struct acpica_device_id_list *cid_list;
11117f47fa6cSBjorn Helgaas 	int i;
11121da177e4SLinus Torvalds 
1113c7bcb4e9SBjorn Helgaas 	switch (device->device_type) {
11141da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
1115859ac9a4SBjorn Helgaas 		if (ACPI_IS_ROOT_DEVICE(device)) {
111657f3674fSBjorn Helgaas 			acpi_add_id(device, ACPI_SYSTEM_HID);
1117859ac9a4SBjorn Helgaas 			break;
1118859ac9a4SBjorn Helgaas 		}
1119859ac9a4SBjorn Helgaas 
112066b7ed40SBjorn Helgaas 		status = acpi_get_object_info(device->handle, &info);
11211da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
112296b2dd1fSHarvey Harrison 			printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
11231da177e4SLinus Torvalds 			return;
11241da177e4SLinus Torvalds 		}
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_HID)
112757f3674fSBjorn Helgaas 			acpi_add_id(device, info->hardware_id.string);
112857f3674fSBjorn Helgaas 		if (info->valid & ACPI_VALID_CID) {
112915b8dd53SBob Moore 			cid_list = &info->compatible_id_list;
113057f3674fSBjorn Helgaas 			for (i = 0; i < cid_list->count; i++)
113157f3674fSBjorn Helgaas 				acpi_add_id(device, cid_list->ids[i].string);
113257f3674fSBjorn Helgaas 		}
11331da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_ADR) {
11341da177e4SLinus Torvalds 			device->pnp.bus_address = info->address;
11351da177e4SLinus Torvalds 			device->flags.bus_address = 1;
11361da177e4SLinus Torvalds 		}
1137ae843332SZhang Rui 
1138a83893aeSBjorn Helgaas 		kfree(info);
1139a83893aeSBjorn Helgaas 
114057f3674fSBjorn Helgaas 		/*
114157f3674fSBjorn Helgaas 		 * Some devices don't reliably have _HIDs & _CIDs, so add
114257f3674fSBjorn Helgaas 		 * synthetic HIDs to make sure drivers can find them.
11433620f2f2SFrank Seidel 		 */
1144c3d6de69SThomas Renninger 		if (acpi_is_video_device(device))
114557f3674fSBjorn Helgaas 			acpi_add_id(device, ACPI_VIDEO_HID);
11463620f2f2SFrank Seidel 		else if (ACPI_SUCCESS(acpi_bay_match(device)))
114757f3674fSBjorn Helgaas 			acpi_add_id(device, ACPI_BAY_HID);
11483620f2f2SFrank Seidel 		else if (ACPI_SUCCESS(acpi_dock_match(device)))
114957f3674fSBjorn Helgaas 			acpi_add_id(device, ACPI_DOCK_HID);
1150222e82acSDarrick J. Wong 		else if (!acpi_ibm_smbus_match(device))
1151222e82acSDarrick J. Wong 			acpi_add_id(device, ACPI_SMBUS_IBM_HID);
1152b7b30de5SBjorn Helgaas 		else if (!acpi_device_hid(device) &&
1153b7b30de5SBjorn Helgaas 			 ACPI_IS_ROOT_DEVICE(device->parent)) {
1154b7b30de5SBjorn Helgaas 			acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1155b7b30de5SBjorn Helgaas 			strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1156b7b30de5SBjorn Helgaas 			strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1157b7b30de5SBjorn Helgaas 		}
115854735266SZhang Rui 
11591da177e4SLinus Torvalds 		break;
11601da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER:
116157f3674fSBjorn Helgaas 		acpi_add_id(device, ACPI_POWER_HID);
11621da177e4SLinus Torvalds 		break;
11631da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_PROCESSOR:
116457f3674fSBjorn Helgaas 		acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
11651da177e4SLinus Torvalds 		break;
11661da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_THERMAL:
116757f3674fSBjorn Helgaas 		acpi_add_id(device, ACPI_THERMAL_HID);
11681da177e4SLinus Torvalds 		break;
11691da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
117057f3674fSBjorn Helgaas 		acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
11711da177e4SLinus Torvalds 		break;
11721da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
117357f3674fSBjorn Helgaas 		acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
11741da177e4SLinus Torvalds 		break;
11751da177e4SLinus Torvalds 	}
11761da177e4SLinus Torvalds }
11771da177e4SLinus Torvalds 
1178bc3b0772SBjorn Helgaas static int acpi_device_set_context(struct acpi_device *device)
11791da177e4SLinus Torvalds {
1180bc3b0772SBjorn Helgaas 	acpi_status status;
1181bc3b0772SBjorn Helgaas 
11821da177e4SLinus Torvalds 	/*
11831da177e4SLinus Torvalds 	 * Context
11841da177e4SLinus Torvalds 	 * -------
11851da177e4SLinus Torvalds 	 * Attach this 'struct acpi_device' to the ACPI object.  This makes
1186bc3b0772SBjorn Helgaas 	 * resolutions from handle->device very efficient.  Fixed hardware
1187bc3b0772SBjorn Helgaas 	 * devices have no handles, so we skip them.
11881da177e4SLinus Torvalds 	 */
1189bc3b0772SBjorn Helgaas 	if (!device->handle)
1190bc3b0772SBjorn Helgaas 		return 0;
1191bc3b0772SBjorn Helgaas 
11921da177e4SLinus Torvalds 	status = acpi_attach_data(device->handle,
11931da177e4SLinus Torvalds 				  acpi_bus_data_handler, device);
1194bc3b0772SBjorn Helgaas 	if (ACPI_SUCCESS(status))
1195bc3b0772SBjorn Helgaas 		return 0;
11961da177e4SLinus Torvalds 
119787ecd5cdSLen Brown 	printk(KERN_ERR PREFIX "Error attaching device data\n");
1198bc3b0772SBjorn Helgaas 	return -ENODEV;
11991da177e4SLinus Torvalds }
12001da177e4SLinus Torvalds 
12014be44fcdSLen Brown static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
12021da177e4SLinus Torvalds {
12031da177e4SLinus Torvalds 	if (!dev)
1204d550d98dSPatrick Mochel 		return -EINVAL;
12051da177e4SLinus Torvalds 
120696333578SLi Shaohua 	dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
12071890a97aSPatrick Mochel 	device_release_driver(&dev->dev);
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 	if (!rmdevice)
1210d550d98dSPatrick Mochel 		return 0;
12111da177e4SLinus Torvalds 
12122786f6e3SRui Zhang 	/*
12132786f6e3SRui Zhang 	 * unbind _ADR-Based Devices when hot removal
12142786f6e3SRui Zhang 	 */
12151da177e4SLinus Torvalds 	if (dev->flags.bus_address) {
12161da177e4SLinus Torvalds 		if ((dev->parent) && (dev->parent->ops.unbind))
12171da177e4SLinus Torvalds 			dev->parent->ops.unbind(dev);
12181da177e4SLinus Torvalds 	}
12191da177e4SLinus Torvalds 	acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
12201da177e4SLinus Torvalds 
1221d550d98dSPatrick Mochel 	return 0;
12221da177e4SLinus Torvalds }
12231da177e4SLinus Torvalds 
12245c478f49SBjorn Helgaas static int acpi_add_single_object(struct acpi_device **child,
12255c478f49SBjorn Helgaas 				  acpi_handle handle, int type,
1226778cbc1dSBjorn Helgaas 				  unsigned long long sta,
1227c4168bffSLi Shaohua 				  struct acpi_bus_ops *ops)
12281da177e4SLinus Torvalds {
122977c24888SBjorn Helgaas 	int result;
123077c24888SBjorn Helgaas 	struct acpi_device *device;
123129aaefa6SBjorn Helgaas 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
12321da177e4SLinus Torvalds 
123336bcbec7SBurman Yan 	device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
12341da177e4SLinus Torvalds 	if (!device) {
12356468463aSLen Brown 		printk(KERN_ERR PREFIX "Memory allocation error\n");
1236d550d98dSPatrick Mochel 		return -ENOMEM;
12371da177e4SLinus Torvalds 	}
12381da177e4SLinus Torvalds 
12397f47fa6cSBjorn Helgaas 	INIT_LIST_HEAD(&device->pnp.ids);
1240caaa6efbSBjorn Helgaas 	device->device_type = type;
12411da177e4SLinus Torvalds 	device->handle = handle;
12425c478f49SBjorn Helgaas 	device->parent = acpi_bus_get_parent(handle);
1243c4168bffSLi Shaohua 	device->bus_ops = *ops; /* workround for not call .start */
1244778cbc1dSBjorn Helgaas 	STRUCT_TO_INT(device->status) = sta;
1245c4168bffSLi Shaohua 
1246c7bcb4e9SBjorn Helgaas 	acpi_device_get_busid(device);
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds 	/*
12491da177e4SLinus Torvalds 	 * Flags
12501da177e4SLinus Torvalds 	 * -----
1251778cbc1dSBjorn Helgaas 	 * Note that we only look for object handles -- cannot evaluate objects
1252778cbc1dSBjorn Helgaas 	 * until we know the device is present and properly initialized.
12531da177e4SLinus Torvalds 	 */
12541da177e4SLinus Torvalds 	result = acpi_bus_get_flags(device);
12551da177e4SLinus Torvalds 	if (result)
12561da177e4SLinus Torvalds 		goto end;
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 	/*
12591da177e4SLinus Torvalds 	 * Initialize Device
12601da177e4SLinus Torvalds 	 * -----------------
12611da177e4SLinus Torvalds 	 * TBD: Synch with Core's enumeration/initialization process.
12621da177e4SLinus Torvalds 	 */
1263c7bcb4e9SBjorn Helgaas 	acpi_device_set_id(device);
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 	/*
12661da177e4SLinus Torvalds 	 * Power Management
12671da177e4SLinus Torvalds 	 * ----------------
12681da177e4SLinus Torvalds 	 */
12691da177e4SLinus Torvalds 	if (device->flags.power_manageable) {
12701da177e4SLinus Torvalds 		result = acpi_bus_get_power_flags(device);
12711da177e4SLinus Torvalds 		if (result)
12721da177e4SLinus Torvalds 			goto end;
12731da177e4SLinus Torvalds 	}
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 	/*
12761da177e4SLinus Torvalds 	 * Wakeup device management
12771da177e4SLinus Torvalds 	 *-----------------------
12781da177e4SLinus Torvalds 	 */
1279d57d09a4SRafael J. Wysocki 	acpi_bus_get_wakeup_device_flags(device);
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds 	/*
12821da177e4SLinus Torvalds 	 * Performance Management
12831da177e4SLinus Torvalds 	 * ----------------------
12841da177e4SLinus Torvalds 	 */
12851da177e4SLinus Torvalds 	if (device->flags.performance_manageable) {
12861da177e4SLinus Torvalds 		result = acpi_bus_get_perf_flags(device);
12871da177e4SLinus Torvalds 		if (result)
12881da177e4SLinus Torvalds 			goto end;
12891da177e4SLinus Torvalds 	}
12901da177e4SLinus Torvalds 
1291bc3b0772SBjorn Helgaas 	if ((result = acpi_device_set_context(device)))
1292f61f9258SLen Brown 		goto end;
12931da177e4SLinus Torvalds 
129466b7ed40SBjorn Helgaas 	result = acpi_device_register(device);
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 	/*
12972786f6e3SRui Zhang 	 * Bind _ADR-Based Devices when hot add
12981da177e4SLinus Torvalds 	 */
12991da177e4SLinus Torvalds 	if (device->flags.bus_address) {
13001da177e4SLinus Torvalds 		if (device->parent && device->parent->ops.bind)
13011da177e4SLinus Torvalds 			device->parent->ops.bind(device);
13021da177e4SLinus Torvalds 	}
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds end:
130529aaefa6SBjorn Helgaas 	if (!result) {
130629aaefa6SBjorn Helgaas 		acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
130729aaefa6SBjorn Helgaas 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
130829aaefa6SBjorn Helgaas 			"Adding %s [%s] parent %s\n", dev_name(&device->dev),
130929aaefa6SBjorn Helgaas 			 (char *) buffer.pointer,
131029aaefa6SBjorn Helgaas 			 device->parent ? dev_name(&device->parent->dev) :
131129aaefa6SBjorn Helgaas 					  "(null)"));
131229aaefa6SBjorn Helgaas 		kfree(buffer.pointer);
13131da177e4SLinus Torvalds 		*child = device;
131429aaefa6SBjorn Helgaas 	} else
1315718fb0deSHugh Dickins 		acpi_device_release(&device->dev);
13161da177e4SLinus Torvalds 
1317d550d98dSPatrick Mochel 	return result;
13181da177e4SLinus Torvalds }
13191da177e4SLinus Torvalds 
1320778cbc1dSBjorn Helgaas #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1321778cbc1dSBjorn Helgaas 			  ACPI_STA_DEVICE_UI      | ACPI_STA_DEVICE_FUNCTIONING)
1322778cbc1dSBjorn Helgaas 
1323bf325f95SRafael J. Wysocki static void acpi_bus_add_power_resource(acpi_handle handle)
1324bf325f95SRafael J. Wysocki {
1325bf325f95SRafael J. Wysocki 	struct acpi_bus_ops ops = {
1326bf325f95SRafael J. Wysocki 		.acpi_op_add = 1,
1327bf325f95SRafael J. Wysocki 		.acpi_op_start = 1,
1328bf325f95SRafael J. Wysocki 	};
1329bf325f95SRafael J. Wysocki 	struct acpi_device *device = NULL;
1330bf325f95SRafael J. Wysocki 
1331bf325f95SRafael J. Wysocki 	acpi_bus_get_device(handle, &device);
1332bf325f95SRafael J. Wysocki 	if (!device)
1333bf325f95SRafael J. Wysocki 		acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER,
1334bf325f95SRafael J. Wysocki 					ACPI_STA_DEFAULT, &ops);
1335bf325f95SRafael J. Wysocki }
1336bf325f95SRafael J. Wysocki 
1337778cbc1dSBjorn Helgaas static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1338778cbc1dSBjorn Helgaas 				    unsigned long long *sta)
1339778cbc1dSBjorn Helgaas {
1340778cbc1dSBjorn Helgaas 	acpi_status status;
1341778cbc1dSBjorn Helgaas 	acpi_object_type acpi_type;
1342778cbc1dSBjorn Helgaas 
1343778cbc1dSBjorn Helgaas 	status = acpi_get_type(handle, &acpi_type);
1344778cbc1dSBjorn Helgaas 	if (ACPI_FAILURE(status))
1345778cbc1dSBjorn Helgaas 		return -ENODEV;
1346778cbc1dSBjorn Helgaas 
1347778cbc1dSBjorn Helgaas 	switch (acpi_type) {
1348778cbc1dSBjorn Helgaas 	case ACPI_TYPE_ANY:		/* for ACPI_ROOT_OBJECT */
1349778cbc1dSBjorn Helgaas 	case ACPI_TYPE_DEVICE:
1350778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_DEVICE;
1351778cbc1dSBjorn Helgaas 		status = acpi_bus_get_status_handle(handle, sta);
1352778cbc1dSBjorn Helgaas 		if (ACPI_FAILURE(status))
1353778cbc1dSBjorn Helgaas 			return -ENODEV;
1354778cbc1dSBjorn Helgaas 		break;
1355778cbc1dSBjorn Helgaas 	case ACPI_TYPE_PROCESSOR:
1356778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_PROCESSOR;
1357778cbc1dSBjorn Helgaas 		status = acpi_bus_get_status_handle(handle, sta);
1358778cbc1dSBjorn Helgaas 		if (ACPI_FAILURE(status))
1359778cbc1dSBjorn Helgaas 			return -ENODEV;
1360778cbc1dSBjorn Helgaas 		break;
1361778cbc1dSBjorn Helgaas 	case ACPI_TYPE_THERMAL:
1362778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_THERMAL;
1363778cbc1dSBjorn Helgaas 		*sta = ACPI_STA_DEFAULT;
1364778cbc1dSBjorn Helgaas 		break;
1365778cbc1dSBjorn Helgaas 	case ACPI_TYPE_POWER:
1366778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_POWER;
1367778cbc1dSBjorn Helgaas 		*sta = ACPI_STA_DEFAULT;
1368778cbc1dSBjorn Helgaas 		break;
1369778cbc1dSBjorn Helgaas 	default:
1370778cbc1dSBjorn Helgaas 		return -ENODEV;
1371778cbc1dSBjorn Helgaas 	}
1372778cbc1dSBjorn Helgaas 
1373778cbc1dSBjorn Helgaas 	return 0;
1374778cbc1dSBjorn Helgaas }
1375778cbc1dSBjorn Helgaas 
137651a85fafSBjorn Helgaas static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
137751a85fafSBjorn Helgaas 				      void *context, void **return_value)
13781da177e4SLinus Torvalds {
137951a85fafSBjorn Helgaas 	struct acpi_bus_ops *ops = context;
1380778cbc1dSBjorn Helgaas 	int type;
1381778cbc1dSBjorn Helgaas 	unsigned long long sta;
1382e3b87f8aSBjorn Helgaas 	struct acpi_device *device;
1383e3b87f8aSBjorn Helgaas 	acpi_status status;
1384778cbc1dSBjorn Helgaas 	int result;
13851da177e4SLinus Torvalds 
1386778cbc1dSBjorn Helgaas 	result = acpi_bus_type_and_status(handle, &type, &sta);
1387778cbc1dSBjorn Helgaas 	if (result)
138851a85fafSBjorn Helgaas 		return AE_OK;
13891da177e4SLinus Torvalds 
1390778cbc1dSBjorn Helgaas 	if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1391b581a7f9SRafael J. Wysocki 	    !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
139286e4e20eSRafael J. Wysocki 		struct acpi_device_wakeup wakeup;
139386e4e20eSRafael J. Wysocki 		acpi_handle temp;
139486e4e20eSRafael J. Wysocki 
139586e4e20eSRafael J. Wysocki 		status = acpi_get_handle(handle, "_PRW", &temp);
139686e4e20eSRafael J. Wysocki 		if (ACPI_SUCCESS(status))
139786e4e20eSRafael J. Wysocki 			acpi_bus_extract_wakeup_device_power_package(handle,
139886e4e20eSRafael J. Wysocki 								     &wakeup);
1399778cbc1dSBjorn Helgaas 		return AE_CTRL_DEPTH;
1400b581a7f9SRafael J. Wysocki 	}
14011da177e4SLinus Torvalds 
1402e3b87f8aSBjorn Helgaas 	/*
1403e3b87f8aSBjorn Helgaas 	 * We may already have an acpi_device from a previous enumeration.  If
1404e3b87f8aSBjorn Helgaas 	 * so, we needn't add it again, but we may still have to start it.
1405e3b87f8aSBjorn Helgaas 	 */
1406e3b87f8aSBjorn Helgaas 	device = NULL;
1407e3b87f8aSBjorn Helgaas 	acpi_bus_get_device(handle, &device);
1408e3b87f8aSBjorn Helgaas 	if (ops->acpi_op_add && !device)
1409e3b87f8aSBjorn Helgaas 		acpi_add_single_object(&device, handle, type, sta, ops);
14103fb02738SRajesh Shah 
1411e3b87f8aSBjorn Helgaas 	if (!device)
141251a85fafSBjorn Helgaas 		return AE_CTRL_DEPTH;
14131da177e4SLinus Torvalds 
1414c4168bffSLi Shaohua 	if (ops->acpi_op_start && !(ops->acpi_op_add)) {
141551a85fafSBjorn Helgaas 		status = acpi_start_single_object(device);
14163fb02738SRajesh Shah 		if (ACPI_FAILURE(status))
141751a85fafSBjorn Helgaas 			return AE_CTRL_DEPTH;
14183fb02738SRajesh Shah 	}
14193fb02738SRajesh Shah 
142051a85fafSBjorn Helgaas 	if (!*return_value)
142151a85fafSBjorn Helgaas 		*return_value = device;
142251a85fafSBjorn Helgaas 	return AE_OK;
14231da177e4SLinus Torvalds }
14241da177e4SLinus Torvalds 
142551a85fafSBjorn Helgaas static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
142651a85fafSBjorn Helgaas 			 struct acpi_device **child)
142751a85fafSBjorn Helgaas {
142851a85fafSBjorn Helgaas 	acpi_status status;
142951a85fafSBjorn Helgaas 	void *device = NULL;
143051a85fafSBjorn Helgaas 
143151a85fafSBjorn Helgaas 	status = acpi_bus_check_add(handle, 0, ops, &device);
143251a85fafSBjorn Helgaas 	if (ACPI_SUCCESS(status))
143351a85fafSBjorn Helgaas 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
14342263576cSLin Ming 				    acpi_bus_check_add, NULL, ops, &device);
143551a85fafSBjorn Helgaas 
143651a85fafSBjorn Helgaas 	if (child)
143751a85fafSBjorn Helgaas 		*child = device;
14387779688fSThomas Renninger 
14397779688fSThomas Renninger 	if (device)
1440d550d98dSPatrick Mochel 		return 0;
14417779688fSThomas Renninger 	else
14427779688fSThomas Renninger 		return -ENODEV;
14431da177e4SLinus Torvalds }
14441da177e4SLinus Torvalds 
14457779688fSThomas Renninger /*
14467779688fSThomas Renninger  * acpi_bus_add and acpi_bus_start
14477779688fSThomas Renninger  *
14487779688fSThomas Renninger  * scan a given ACPI tree and (probably recently hot-plugged)
14497779688fSThomas Renninger  * create and add or starts found devices.
14507779688fSThomas Renninger  *
14517779688fSThomas Renninger  * If no devices were found -ENODEV is returned which does not
14527779688fSThomas Renninger  * mean that this is a real error, there just have been no suitable
14537779688fSThomas Renninger  * ACPI objects in the table trunk from which the kernel could create
14547779688fSThomas Renninger  * a device and add/start an appropriate driver.
14557779688fSThomas Renninger  */
14567779688fSThomas Renninger 
14573fb02738SRajesh Shah int
14584be44fcdSLen Brown acpi_bus_add(struct acpi_device **child,
14594be44fcdSLen Brown 	     struct acpi_device *parent, acpi_handle handle, int type)
14603fb02738SRajesh Shah {
14613fb02738SRajesh Shah 	struct acpi_bus_ops ops;
14623fb02738SRajesh Shah 
14633fb02738SRajesh Shah 	memset(&ops, 0, sizeof(ops));
14643fb02738SRajesh Shah 	ops.acpi_op_add = 1;
1465c4168bffSLi Shaohua 
14667779688fSThomas Renninger 	return acpi_bus_scan(handle, &ops, child);
14673fb02738SRajesh Shah }
14683fb02738SRajesh Shah EXPORT_SYMBOL(acpi_bus_add);
14693fb02738SRajesh Shah 
14704be44fcdSLen Brown int acpi_bus_start(struct acpi_device *device)
14713fb02738SRajesh Shah {
14723fb02738SRajesh Shah 	struct acpi_bus_ops ops;
1473a2100801SRafael J. Wysocki 	int result;
14743fb02738SRajesh Shah 
1475d2f6650aSThomas Renninger 	if (!device)
1476d2f6650aSThomas Renninger 		return -EINVAL;
1477d2f6650aSThomas Renninger 
14783fb02738SRajesh Shah 	memset(&ops, 0, sizeof(ops));
14793fb02738SRajesh Shah 	ops.acpi_op_start = 1;
14808e029bf0SBjorn Helgaas 
1481a2100801SRafael J. Wysocki 	result = acpi_bus_scan(device->handle, &ops, NULL);
1482a2100801SRafael J. Wysocki 
14833a37898dSLin Ming 	acpi_update_all_gpes();
1484a2100801SRafael J. Wysocki 
1485a2100801SRafael J. Wysocki 	return result;
14863fb02738SRajesh Shah }
14873fb02738SRajesh Shah EXPORT_SYMBOL(acpi_bus_start);
14881da177e4SLinus Torvalds 
1489ceaba663SKristen Accardi int acpi_bus_trim(struct acpi_device *start, int rmdevice)
14901da177e4SLinus Torvalds {
14911da177e4SLinus Torvalds 	acpi_status status;
14921da177e4SLinus Torvalds 	struct acpi_device *parent, *child;
14931da177e4SLinus Torvalds 	acpi_handle phandle, chandle;
14941da177e4SLinus Torvalds 	acpi_object_type type;
14951da177e4SLinus Torvalds 	u32 level = 1;
14961da177e4SLinus Torvalds 	int err = 0;
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds 	parent = start;
14991da177e4SLinus Torvalds 	phandle = start->handle;
15001da177e4SLinus Torvalds 	child = chandle = NULL;
15011da177e4SLinus Torvalds 
15021da177e4SLinus Torvalds 	while ((level > 0) && parent && (!err)) {
15031da177e4SLinus Torvalds 		status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
15041da177e4SLinus Torvalds 					      chandle, &chandle);
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds 		/*
15071da177e4SLinus Torvalds 		 * If this scope is exhausted then move our way back up.
15081da177e4SLinus Torvalds 		 */
15091da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
15101da177e4SLinus Torvalds 			level--;
15111da177e4SLinus Torvalds 			chandle = phandle;
15121da177e4SLinus Torvalds 			acpi_get_parent(phandle, &phandle);
15131da177e4SLinus Torvalds 			child = parent;
15141da177e4SLinus Torvalds 			parent = parent->parent;
15151da177e4SLinus Torvalds 
15161da177e4SLinus Torvalds 			if (level == 0)
15171da177e4SLinus Torvalds 				err = acpi_bus_remove(child, rmdevice);
15181da177e4SLinus Torvalds 			else
15191da177e4SLinus Torvalds 				err = acpi_bus_remove(child, 1);
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds 			continue;
15221da177e4SLinus Torvalds 		}
15231da177e4SLinus Torvalds 
15241da177e4SLinus Torvalds 		status = acpi_get_type(chandle, &type);
15251da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
15261da177e4SLinus Torvalds 			continue;
15271da177e4SLinus Torvalds 		}
15281da177e4SLinus Torvalds 		/*
15291da177e4SLinus Torvalds 		 * If there is a device corresponding to chandle then
15301da177e4SLinus Torvalds 		 * parse it (depth-first).
15311da177e4SLinus Torvalds 		 */
15321da177e4SLinus Torvalds 		if (acpi_bus_get_device(chandle, &child) == 0) {
15331da177e4SLinus Torvalds 			level++;
15341da177e4SLinus Torvalds 			phandle = chandle;
15351da177e4SLinus Torvalds 			chandle = NULL;
15361da177e4SLinus Torvalds 			parent = child;
15371da177e4SLinus Torvalds 		}
15381da177e4SLinus Torvalds 		continue;
15391da177e4SLinus Torvalds 	}
15401da177e4SLinus Torvalds 	return err;
15411da177e4SLinus Torvalds }
1542ceaba663SKristen Accardi EXPORT_SYMBOL_GPL(acpi_bus_trim);
1543ceaba663SKristen Accardi 
1544e8b945c9SBjorn Helgaas static int acpi_bus_scan_fixed(void)
15451da177e4SLinus Torvalds {
15461da177e4SLinus Torvalds 	int result = 0;
15471da177e4SLinus Torvalds 	struct acpi_device *device = NULL;
1548c4168bffSLi Shaohua 	struct acpi_bus_ops ops;
15491da177e4SLinus Torvalds 
1550c4168bffSLi Shaohua 	memset(&ops, 0, sizeof(ops));
1551c4168bffSLi Shaohua 	ops.acpi_op_add = 1;
1552c4168bffSLi Shaohua 	ops.acpi_op_start = 1;
1553c4168bffSLi Shaohua 
15541da177e4SLinus Torvalds 	/*
15551da177e4SLinus Torvalds 	 * Enumerate all fixed-feature devices.
15561da177e4SLinus Torvalds 	 */
1557cee324b1SAlexey Starikovskiy 	if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
15585c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
1559c4168bffSLi Shaohua 						ACPI_BUS_TYPE_POWER_BUTTON,
1560778cbc1dSBjorn Helgaas 						ACPI_STA_DEFAULT,
1561c4168bffSLi Shaohua 						&ops);
15623fb02738SRajesh Shah 	}
15631da177e4SLinus Torvalds 
1564cee324b1SAlexey Starikovskiy 	if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
15655c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
1566c4168bffSLi Shaohua 						ACPI_BUS_TYPE_SLEEP_BUTTON,
1567778cbc1dSBjorn Helgaas 						ACPI_STA_DEFAULT,
1568c4168bffSLi Shaohua 						&ops);
15693fb02738SRajesh Shah 	}
15701da177e4SLinus Torvalds 
1571d550d98dSPatrick Mochel 	return result;
15721da177e4SLinus Torvalds }
15731da177e4SLinus Torvalds 
1574e747f274SBjorn Helgaas int __init acpi_scan_init(void)
15751da177e4SLinus Torvalds {
15761da177e4SLinus Torvalds 	int result;
15773fb02738SRajesh Shah 	struct acpi_bus_ops ops;
15781da177e4SLinus Torvalds 
1579c4168bffSLi Shaohua 	memset(&ops, 0, sizeof(ops));
1580c4168bffSLi Shaohua 	ops.acpi_op_add = 1;
1581c4168bffSLi Shaohua 	ops.acpi_op_start = 1;
15821da177e4SLinus Torvalds 
15835b327265SPatrick Mochel 	result = bus_register(&acpi_bus_type);
15845b327265SPatrick Mochel 	if (result) {
15855b327265SPatrick Mochel 		/* We don't want to quit even if we failed to add suspend/resume */
15865b327265SPatrick Mochel 		printk(KERN_ERR PREFIX "Could not register bus type\n");
15875b327265SPatrick Mochel 	}
15885b327265SPatrick Mochel 
158997d9a9e9SRafael J. Wysocki 	acpi_power_init();
159097d9a9e9SRafael J. Wysocki 
15911da177e4SLinus Torvalds 	/*
15921da177e4SLinus Torvalds 	 * Enumerate devices in the ACPI namespace.
15931da177e4SLinus Torvalds 	 */
159451a85fafSBjorn Helgaas 	result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
1595c04209a7SAlexey Starikovskiy 
1596c4168bffSLi Shaohua 	if (!result)
1597adc08e20SBjorn Helgaas 		result = acpi_bus_scan_fixed();
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds 	if (result)
16001da177e4SLinus Torvalds 		acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1601a2100801SRafael J. Wysocki 	else
16023a37898dSLin Ming 		acpi_update_all_gpes();
16031da177e4SLinus Torvalds 
1604d550d98dSPatrick Mochel 	return result;
16051da177e4SLinus Torvalds }
1606