xref: /openbmc/linux/drivers/acpi/scan.c (revision f517709d65beed95f52f021b43e3035b52ef791a)
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>
79b6d97b6SRandy Dunlap #include <linux/kernel.h>
81da177e4SLinus Torvalds #include <linux/acpi.h>
974523c90SAlok N Kataria #include <linux/signal.h>
1074523c90SAlok N Kataria #include <linux/kthread.h>
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <acpi/acpi_drivers.h>
131da177e4SLinus Torvalds 
14e60cc7a6SBjorn Helgaas #include "internal.h"
15e60cc7a6SBjorn Helgaas 
161da177e4SLinus Torvalds #define _COMPONENT		ACPI_BUS_COMPONENT
17f52fd66dSLen Brown ACPI_MODULE_NAME("scan");
181da177e4SLinus Torvalds #define STRUCT_TO_INT(s)	(*((int*)&s))
191da177e4SLinus Torvalds extern struct acpi_device *acpi_root;
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #define ACPI_BUS_CLASS			"system_bus"
2229b71a1cSThomas Renninger #define ACPI_BUS_HID			"LNXSYBUS"
231da177e4SLinus Torvalds #define ACPI_BUS_DEVICE_NAME		"System Bus"
241da177e4SLinus Torvalds 
25859ac9a4SBjorn Helgaas #define ACPI_IS_ROOT_DEVICE(device)    (!(device)->parent)
26859ac9a4SBjorn Helgaas 
271da177e4SLinus Torvalds static LIST_HEAD(acpi_device_list);
28e49bd2ddSZhang Rui static LIST_HEAD(acpi_bus_id_list);
299090589dSShaohua Li DEFINE_MUTEX(acpi_device_lock);
301da177e4SLinus Torvalds LIST_HEAD(acpi_wakeup_device_list);
311da177e4SLinus Torvalds 
32e49bd2ddSZhang Rui struct acpi_device_bus_id{
33bb095854SZhang Rui 	char bus_id[15];
34e49bd2ddSZhang Rui 	unsigned int instance_no;
35e49bd2ddSZhang Rui 	struct list_head node;
36e49bd2ddSZhang Rui };
3729b71a1cSThomas Renninger 
3829b71a1cSThomas Renninger /*
3929b71a1cSThomas Renninger  * Creates hid/cid(s) string needed for modalias and uevent
4029b71a1cSThomas Renninger  * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
4129b71a1cSThomas Renninger  * char *modalias: "acpi:IBM0001:ACPI0001"
4229b71a1cSThomas Renninger */
43b3e572d2SAdrian Bunk static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
44b3e572d2SAdrian Bunk 			   int size)
45b3e572d2SAdrian Bunk {
4629b71a1cSThomas Renninger 	int len;
475c9fcb5dSZhang Rui 	int count;
487f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id;
4929b71a1cSThomas Renninger 
505c9fcb5dSZhang Rui 	len = snprintf(modalias, size, "acpi:");
5129b71a1cSThomas Renninger 	size -= len;
5229b71a1cSThomas Renninger 
537f47fa6cSBjorn Helgaas 	list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
547f47fa6cSBjorn Helgaas 		count = snprintf(&modalias[len], size, "%s:", id->id);
555c9fcb5dSZhang Rui 		if (count < 0 || count >= size)
565c9fcb5dSZhang Rui 			return -EINVAL;
575c9fcb5dSZhang Rui 		len += count;
585c9fcb5dSZhang Rui 		size -= count;
595c9fcb5dSZhang Rui 	}
605c9fcb5dSZhang Rui 
6129b71a1cSThomas Renninger 	modalias[len] = '\0';
6229b71a1cSThomas Renninger 	return len;
6329b71a1cSThomas Renninger }
6429b71a1cSThomas Renninger 
6529b71a1cSThomas Renninger static ssize_t
6629b71a1cSThomas Renninger acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
6729b71a1cSThomas Renninger 	struct acpi_device *acpi_dev = to_acpi_device(dev);
6829b71a1cSThomas Renninger 	int len;
6929b71a1cSThomas Renninger 
7029b71a1cSThomas Renninger 	/* Device has no HID and no CID or string is >1024 */
7129b71a1cSThomas Renninger 	len = create_modalias(acpi_dev, buf, 1024);
7229b71a1cSThomas Renninger 	if (len <= 0)
7329b71a1cSThomas Renninger 		return 0;
7429b71a1cSThomas Renninger 	buf[len++] = '\n';
7529b71a1cSThomas Renninger 	return len;
7629b71a1cSThomas Renninger }
7729b71a1cSThomas Renninger static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
7829b71a1cSThomas Renninger 
79c8d72a5eSZhang Rui static void acpi_bus_hot_remove_device(void *context)
801da177e4SLinus Torvalds {
8126d46867SZhang Rui 	struct acpi_device *device;
8226d46867SZhang Rui 	acpi_handle handle = context;
831da177e4SLinus Torvalds 	struct acpi_object_list arg_list;
841da177e4SLinus Torvalds 	union acpi_object arg;
851da177e4SLinus Torvalds 	acpi_status status = AE_OK;
861da177e4SLinus Torvalds 
8726d46867SZhang Rui 	if (acpi_bus_get_device(handle, &device))
88c8d72a5eSZhang Rui 		return;
891da177e4SLinus Torvalds 
9026d46867SZhang Rui 	if (!device)
91c8d72a5eSZhang Rui 		return;
9226d46867SZhang Rui 
9326d46867SZhang Rui 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
940794469dSKay Sievers 		"Hot-removing device %s...\n", dev_name(&device->dev)));
9526d46867SZhang Rui 
9626d46867SZhang Rui 	if (acpi_bus_trim(device, 1)) {
9755ac9a01SLin Ming 		printk(KERN_ERR PREFIX
9855ac9a01SLin Ming 				"Removing device failed\n");
99c8d72a5eSZhang Rui 		return;
10026d46867SZhang Rui 	}
10126d46867SZhang Rui 
10226d46867SZhang Rui 	/* power off device */
10326d46867SZhang Rui 	status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
10426d46867SZhang Rui 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
10555ac9a01SLin Ming 		printk(KERN_WARNING PREFIX
10655ac9a01SLin Ming 				"Power-off device failed\n");
10726d46867SZhang Rui 
10826d46867SZhang Rui 	if (device->flags.lockable) {
1091da177e4SLinus Torvalds 		arg_list.count = 1;
1101da177e4SLinus Torvalds 		arg_list.pointer = &arg;
1111da177e4SLinus Torvalds 		arg.type = ACPI_TYPE_INTEGER;
1121da177e4SLinus Torvalds 		arg.integer.value = 0;
1131da177e4SLinus Torvalds 		acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
1141da177e4SLinus Torvalds 	}
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	arg_list.count = 1;
1171da177e4SLinus Torvalds 	arg_list.pointer = &arg;
1181da177e4SLinus Torvalds 	arg.type = ACPI_TYPE_INTEGER;
1191da177e4SLinus Torvalds 	arg.integer.value = 1;
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	/*
1221da177e4SLinus Torvalds 	 * TBD: _EJD support.
1231da177e4SLinus Torvalds 	 */
1241da177e4SLinus Torvalds 	status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
12526d46867SZhang Rui 	if (ACPI_FAILURE(status))
126c8d72a5eSZhang Rui 		printk(KERN_WARNING PREFIX
127c8d72a5eSZhang Rui 				"Eject device failed\n");
1281da177e4SLinus Torvalds 
129c8d72a5eSZhang Rui 	return;
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds static ssize_t
133f883d9dbSPatrick Mochel acpi_eject_store(struct device *d, struct device_attribute *attr,
134f883d9dbSPatrick Mochel 		const char *buf, size_t count)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	int ret = count;
1371da177e4SLinus Torvalds 	acpi_status status;
1381da177e4SLinus Torvalds 	acpi_object_type type = 0;
139f883d9dbSPatrick Mochel 	struct acpi_device *acpi_device = to_acpi_device(d);
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 	if ((!count) || (buf[0] != '1')) {
1421da177e4SLinus Torvalds 		return -EINVAL;
1431da177e4SLinus Torvalds 	}
1441da177e4SLinus Torvalds #ifndef FORCE_EJECT
145f883d9dbSPatrick Mochel 	if (acpi_device->driver == NULL) {
1461da177e4SLinus Torvalds 		ret = -ENODEV;
1471da177e4SLinus Torvalds 		goto err;
1481da177e4SLinus Torvalds 	}
1491da177e4SLinus Torvalds #endif
150f883d9dbSPatrick Mochel 	status = acpi_get_type(acpi_device->handle, &type);
151f883d9dbSPatrick Mochel 	if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
1521da177e4SLinus Torvalds 		ret = -ENODEV;
1531da177e4SLinus Torvalds 		goto err;
1541da177e4SLinus Torvalds 	}
1551da177e4SLinus Torvalds 
156c8d72a5eSZhang Rui 	acpi_os_hotplug_execute(acpi_bus_hot_remove_device, acpi_device->handle);
1571da177e4SLinus Torvalds err:
1581da177e4SLinus Torvalds 	return ret;
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds 
161f883d9dbSPatrick Mochel static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
162f883d9dbSPatrick Mochel 
163e49bd2ddSZhang Rui static ssize_t
164e49bd2ddSZhang Rui acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
165e49bd2ddSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
166e49bd2ddSZhang Rui 
167ea8d82fdSBjorn Helgaas 	return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
168e49bd2ddSZhang Rui }
169e49bd2ddSZhang Rui static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
170e49bd2ddSZhang Rui 
171e49bd2ddSZhang Rui static ssize_t
172e49bd2ddSZhang Rui acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
173e49bd2ddSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
174e49bd2ddSZhang Rui 	struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
175e49bd2ddSZhang Rui 	int result;
176e49bd2ddSZhang Rui 
177e49bd2ddSZhang Rui 	result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
178e49bd2ddSZhang Rui 	if (result)
179e49bd2ddSZhang Rui 		goto end;
180e49bd2ddSZhang Rui 
181e49bd2ddSZhang Rui 	result = sprintf(buf, "%s\n", (char*)path.pointer);
182e49bd2ddSZhang Rui 	kfree(path.pointer);
183e49bd2ddSZhang Rui end:
184e49bd2ddSZhang Rui 	return result;
185e49bd2ddSZhang Rui }
186e49bd2ddSZhang Rui static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
187e49bd2ddSZhang Rui 
188e49bd2ddSZhang Rui static int acpi_device_setup_files(struct acpi_device *dev)
189f883d9dbSPatrick Mochel {
190f883d9dbSPatrick Mochel 	acpi_status status;
191f883d9dbSPatrick Mochel 	acpi_handle temp;
192e49bd2ddSZhang Rui 	int result = 0;
193e49bd2ddSZhang Rui 
194e49bd2ddSZhang Rui 	/*
195e49bd2ddSZhang Rui 	 * Devices gotten from FADT don't have a "path" attribute
196e49bd2ddSZhang Rui 	 */
197e49bd2ddSZhang Rui 	if (dev->handle) {
198e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_path);
199e49bd2ddSZhang Rui 		if (result)
200e49bd2ddSZhang Rui 			goto end;
201e49bd2ddSZhang Rui 	}
202e49bd2ddSZhang Rui 
203e49bd2ddSZhang Rui 	result = device_create_file(&dev->dev, &dev_attr_hid);
204e49bd2ddSZhang Rui 	if (result)
205e49bd2ddSZhang Rui 		goto end;
206f883d9dbSPatrick Mochel 
20729b71a1cSThomas Renninger 	result = device_create_file(&dev->dev, &dev_attr_modalias);
20829b71a1cSThomas Renninger 	if (result)
20929b71a1cSThomas Renninger 		goto end;
21029b71a1cSThomas Renninger 
211f883d9dbSPatrick Mochel         /*
212f883d9dbSPatrick Mochel          * If device has _EJ0, 'eject' file is created that is used to trigger
213f883d9dbSPatrick Mochel          * hot-removal function from userland.
214f883d9dbSPatrick Mochel          */
215f883d9dbSPatrick Mochel 	status = acpi_get_handle(dev->handle, "_EJ0", &temp);
216f883d9dbSPatrick Mochel 	if (ACPI_SUCCESS(status))
217e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_eject);
218e49bd2ddSZhang Rui end:
219e49bd2ddSZhang Rui 	return result;
220f883d9dbSPatrick Mochel }
221f883d9dbSPatrick Mochel 
222f883d9dbSPatrick Mochel static void acpi_device_remove_files(struct acpi_device *dev)
223f883d9dbSPatrick Mochel {
224f883d9dbSPatrick Mochel 	acpi_status status;
225f883d9dbSPatrick Mochel 	acpi_handle temp;
226f883d9dbSPatrick Mochel 
227f883d9dbSPatrick Mochel 	/*
228f883d9dbSPatrick Mochel 	 * If device has _EJ0, 'eject' file is created that is used to trigger
229f883d9dbSPatrick Mochel 	 * hot-removal function from userland.
230f883d9dbSPatrick Mochel 	 */
231f883d9dbSPatrick Mochel 	status = acpi_get_handle(dev->handle, "_EJ0", &temp);
232f883d9dbSPatrick Mochel 	if (ACPI_SUCCESS(status))
233f883d9dbSPatrick Mochel 		device_remove_file(&dev->dev, &dev_attr_eject);
234e49bd2ddSZhang Rui 
23529b71a1cSThomas Renninger 	device_remove_file(&dev->dev, &dev_attr_modalias);
236e49bd2ddSZhang Rui 	device_remove_file(&dev->dev, &dev_attr_hid);
237e49bd2ddSZhang Rui 	if (dev->handle)
238e49bd2ddSZhang Rui 		device_remove_file(&dev->dev, &dev_attr_path);
239f883d9dbSPatrick Mochel }
2401da177e4SLinus Torvalds /* --------------------------------------------------------------------------
2419e89dde2SZhang Rui 			ACPI Bus operations
2421da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
24329b71a1cSThomas Renninger 
24429b71a1cSThomas Renninger int acpi_match_device_ids(struct acpi_device *device,
24529b71a1cSThomas Renninger 			  const struct acpi_device_id *ids)
24629b71a1cSThomas Renninger {
24729b71a1cSThomas Renninger 	const struct acpi_device_id *id;
2487f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *hwid;
24929b71a1cSThomas Renninger 
25039a0ad87SZhao Yakui 	/*
25139a0ad87SZhao Yakui 	 * If the device is not present, it is unnecessary to load device
25239a0ad87SZhao Yakui 	 * driver for it.
25339a0ad87SZhao Yakui 	 */
25439a0ad87SZhao Yakui 	if (!device->status.present)
25539a0ad87SZhao Yakui 		return -ENODEV;
25639a0ad87SZhao Yakui 
2577f47fa6cSBjorn Helgaas 	for (id = ids; id->id[0]; id++)
2587f47fa6cSBjorn Helgaas 		list_for_each_entry(hwid, &device->pnp.ids, list)
2597f47fa6cSBjorn Helgaas 			if (!strcmp((char *) id->id, hwid->id))
26029b71a1cSThomas Renninger 				return 0;
26129b71a1cSThomas Renninger 
26229b71a1cSThomas Renninger 	return -ENOENT;
26329b71a1cSThomas Renninger }
26429b71a1cSThomas Renninger EXPORT_SYMBOL(acpi_match_device_ids);
26529b71a1cSThomas Renninger 
2667f47fa6cSBjorn Helgaas static void acpi_free_ids(struct acpi_device *device)
2677f47fa6cSBjorn Helgaas {
2687f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id, *tmp;
2697f47fa6cSBjorn Helgaas 
2707f47fa6cSBjorn Helgaas 	list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
2717f47fa6cSBjorn Helgaas 		kfree(id->id);
2727f47fa6cSBjorn Helgaas 		kfree(id);
2737f47fa6cSBjorn Helgaas 	}
2747f47fa6cSBjorn Helgaas }
2757f47fa6cSBjorn Helgaas 
2761890a97aSPatrick Mochel static void acpi_device_release(struct device *dev)
2771890a97aSPatrick Mochel {
2781890a97aSPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
2791890a97aSPatrick Mochel 
2807f47fa6cSBjorn Helgaas 	acpi_free_ids(acpi_dev);
2811890a97aSPatrick Mochel 	kfree(acpi_dev);
2821890a97aSPatrick Mochel }
2831890a97aSPatrick Mochel 
2849e89dde2SZhang Rui static int acpi_device_suspend(struct device *dev, pm_message_t state)
2859e89dde2SZhang Rui {
2861d268b0aSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
2875d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
2881da177e4SLinus Torvalds 
2895d9464a4SPatrick Mochel 	if (acpi_drv && acpi_drv->ops.suspend)
2905d9464a4SPatrick Mochel 		return acpi_drv->ops.suspend(acpi_dev, state);
2911da177e4SLinus Torvalds 	return 0;
2929e89dde2SZhang Rui }
2931da177e4SLinus Torvalds 
2949e89dde2SZhang Rui static int acpi_device_resume(struct device *dev)
2959e89dde2SZhang Rui {
2961d268b0aSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
2975d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
2989e89dde2SZhang Rui 
2995d9464a4SPatrick Mochel 	if (acpi_drv && acpi_drv->ops.resume)
3005d9464a4SPatrick Mochel 		return acpi_drv->ops.resume(acpi_dev);
3019e89dde2SZhang Rui 	return 0;
3029e89dde2SZhang Rui }
3031da177e4SLinus Torvalds 
3045d9464a4SPatrick Mochel static int acpi_bus_match(struct device *dev, struct device_driver *drv)
3051da177e4SLinus Torvalds {
3065d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3075d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(drv);
3085d9464a4SPatrick Mochel 
30929b71a1cSThomas Renninger 	return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
3105d9464a4SPatrick Mochel }
3115d9464a4SPatrick Mochel 
3127eff2e7aSKay Sievers static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
3135d9464a4SPatrick Mochel {
3145d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3157eff2e7aSKay Sievers 	int len;
3165d9464a4SPatrick Mochel 
3177eff2e7aSKay Sievers 	if (add_uevent_var(env, "MODALIAS="))
3187eff2e7aSKay Sievers 		return -ENOMEM;
3197eff2e7aSKay Sievers 	len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
3207eff2e7aSKay Sievers 			      sizeof(env->buf) - env->buflen);
3217eff2e7aSKay Sievers 	if (len >= (sizeof(env->buf) - env->buflen))
3227eff2e7aSKay Sievers 		return -ENOMEM;
3237eff2e7aSKay Sievers 	env->buflen += len;
3241da177e4SLinus Torvalds 	return 0;
3251da177e4SLinus Torvalds }
3261da177e4SLinus Torvalds 
32746ec8598SBjorn Helgaas static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
32846ec8598SBjorn Helgaas {
32946ec8598SBjorn Helgaas 	struct acpi_device *device = data;
33046ec8598SBjorn Helgaas 
33146ec8598SBjorn Helgaas 	device->driver->ops.notify(device, event);
33246ec8598SBjorn Helgaas }
33346ec8598SBjorn Helgaas 
33446ec8598SBjorn Helgaas static acpi_status acpi_device_notify_fixed(void *data)
33546ec8598SBjorn Helgaas {
33646ec8598SBjorn Helgaas 	struct acpi_device *device = data;
33746ec8598SBjorn Helgaas 
33853de5356SBjorn Helgaas 	/* Fixed hardware devices have no handles */
33953de5356SBjorn Helgaas 	acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
34046ec8598SBjorn Helgaas 	return AE_OK;
34146ec8598SBjorn Helgaas }
34246ec8598SBjorn Helgaas 
34346ec8598SBjorn Helgaas static int acpi_device_install_notify_handler(struct acpi_device *device)
34446ec8598SBjorn Helgaas {
34546ec8598SBjorn Helgaas 	acpi_status status;
34646ec8598SBjorn Helgaas 
347ccba2a36SBjorn Helgaas 	if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
34846ec8598SBjorn Helgaas 		status =
34946ec8598SBjorn Helgaas 		    acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
35046ec8598SBjorn Helgaas 						     acpi_device_notify_fixed,
35146ec8598SBjorn Helgaas 						     device);
352ccba2a36SBjorn Helgaas 	else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
35346ec8598SBjorn Helgaas 		status =
35446ec8598SBjorn Helgaas 		    acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
35546ec8598SBjorn Helgaas 						     acpi_device_notify_fixed,
35646ec8598SBjorn Helgaas 						     device);
35746ec8598SBjorn Helgaas 	else
35846ec8598SBjorn Helgaas 		status = acpi_install_notify_handler(device->handle,
35946ec8598SBjorn Helgaas 						     ACPI_DEVICE_NOTIFY,
36046ec8598SBjorn Helgaas 						     acpi_device_notify,
36146ec8598SBjorn Helgaas 						     device);
36246ec8598SBjorn Helgaas 
36346ec8598SBjorn Helgaas 	if (ACPI_FAILURE(status))
36446ec8598SBjorn Helgaas 		return -EINVAL;
36546ec8598SBjorn Helgaas 	return 0;
36646ec8598SBjorn Helgaas }
36746ec8598SBjorn Helgaas 
36846ec8598SBjorn Helgaas static void acpi_device_remove_notify_handler(struct acpi_device *device)
36946ec8598SBjorn Helgaas {
370ccba2a36SBjorn Helgaas 	if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
37146ec8598SBjorn Helgaas 		acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
37246ec8598SBjorn Helgaas 						acpi_device_notify_fixed);
373ccba2a36SBjorn Helgaas 	else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
37446ec8598SBjorn Helgaas 		acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
37546ec8598SBjorn Helgaas 						acpi_device_notify_fixed);
37646ec8598SBjorn Helgaas 	else
37746ec8598SBjorn Helgaas 		acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
37846ec8598SBjorn Helgaas 					   acpi_device_notify);
37946ec8598SBjorn Helgaas }
38046ec8598SBjorn Helgaas 
3815d9464a4SPatrick Mochel static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
3825d9464a4SPatrick Mochel static int acpi_start_single_object(struct acpi_device *);
3835d9464a4SPatrick Mochel static int acpi_device_probe(struct device * dev)
3845d9464a4SPatrick Mochel {
3855d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3865d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
3875d9464a4SPatrick Mochel 	int ret;
3885d9464a4SPatrick Mochel 
3895d9464a4SPatrick Mochel 	ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
3905d9464a4SPatrick Mochel 	if (!ret) {
391c4168bffSLi Shaohua 		if (acpi_dev->bus_ops.acpi_op_start)
3925d9464a4SPatrick Mochel 			acpi_start_single_object(acpi_dev);
39346ec8598SBjorn Helgaas 
39446ec8598SBjorn Helgaas 		if (acpi_drv->ops.notify) {
39546ec8598SBjorn Helgaas 			ret = acpi_device_install_notify_handler(acpi_dev);
39646ec8598SBjorn Helgaas 			if (ret) {
39746ec8598SBjorn Helgaas 				if (acpi_drv->ops.remove)
39846ec8598SBjorn Helgaas 					acpi_drv->ops.remove(acpi_dev,
39946ec8598SBjorn Helgaas 						     acpi_dev->removal_type);
40046ec8598SBjorn Helgaas 				return ret;
40146ec8598SBjorn Helgaas 			}
40246ec8598SBjorn Helgaas 		}
40346ec8598SBjorn Helgaas 
4045d9464a4SPatrick Mochel 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4055d9464a4SPatrick Mochel 			"Found driver [%s] for device [%s]\n",
4065d9464a4SPatrick Mochel 			acpi_drv->name, acpi_dev->pnp.bus_id));
4075d9464a4SPatrick Mochel 		get_device(dev);
4085d9464a4SPatrick Mochel 	}
4095d9464a4SPatrick Mochel 	return ret;
4105d9464a4SPatrick Mochel }
4115d9464a4SPatrick Mochel 
4125d9464a4SPatrick Mochel static int acpi_device_remove(struct device * dev)
4135d9464a4SPatrick Mochel {
4145d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
4155d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
4165d9464a4SPatrick Mochel 
4175d9464a4SPatrick Mochel 	if (acpi_drv) {
41846ec8598SBjorn Helgaas 		if (acpi_drv->ops.notify)
41946ec8598SBjorn Helgaas 			acpi_device_remove_notify_handler(acpi_dev);
4205d9464a4SPatrick Mochel 		if (acpi_drv->ops.remove)
42196333578SLi Shaohua 			acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
4225d9464a4SPatrick Mochel 	}
4235d9464a4SPatrick Mochel 	acpi_dev->driver = NULL;
424db89b4f0SPavel Machek 	acpi_dev->driver_data = NULL;
4255d9464a4SPatrick Mochel 
4265d9464a4SPatrick Mochel 	put_device(dev);
4275d9464a4SPatrick Mochel 	return 0;
4285d9464a4SPatrick Mochel }
4295d9464a4SPatrick Mochel 
43055955aadSDavid Brownell struct bus_type acpi_bus_type = {
4319e89dde2SZhang Rui 	.name		= "acpi",
4329e89dde2SZhang Rui 	.suspend	= acpi_device_suspend,
4339e89dde2SZhang Rui 	.resume		= acpi_device_resume,
4345d9464a4SPatrick Mochel 	.match		= acpi_bus_match,
4355d9464a4SPatrick Mochel 	.probe		= acpi_device_probe,
4365d9464a4SPatrick Mochel 	.remove		= acpi_device_remove,
4375d9464a4SPatrick Mochel 	.uevent		= acpi_device_uevent,
4381da177e4SLinus Torvalds };
4391da177e4SLinus Torvalds 
44066b7ed40SBjorn Helgaas static int acpi_device_register(struct acpi_device *device)
4411da177e4SLinus Torvalds {
442e49bd2ddSZhang Rui 	int result;
443e49bd2ddSZhang Rui 	struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
444e49bd2ddSZhang Rui 	int found = 0;
44566b7ed40SBjorn Helgaas 
4461da177e4SLinus Torvalds 	/*
4471da177e4SLinus Torvalds 	 * Linkage
4481da177e4SLinus Torvalds 	 * -------
4491da177e4SLinus Torvalds 	 * Link this device to its parent and siblings.
4501da177e4SLinus Torvalds 	 */
4511da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->children);
4521da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->node);
4531da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->wakeup_list);
4541da177e4SLinus Torvalds 
455e49bd2ddSZhang Rui 	new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
456e49bd2ddSZhang Rui 	if (!new_bus_id) {
457e49bd2ddSZhang Rui 		printk(KERN_ERR PREFIX "Memory allocation error\n");
458e49bd2ddSZhang Rui 		return -ENOMEM;
459e49bd2ddSZhang Rui 	}
460e49bd2ddSZhang Rui 
4619090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
462e49bd2ddSZhang Rui 	/*
463e49bd2ddSZhang Rui 	 * Find suitable bus_id and instance number in acpi_bus_id_list
464e49bd2ddSZhang Rui 	 * If failed, create one and link it into acpi_bus_id_list
465e49bd2ddSZhang Rui 	 */
466e49bd2ddSZhang Rui 	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
4671131b938SBjorn Helgaas 		if (!strcmp(acpi_device_bus_id->bus_id,
4681131b938SBjorn Helgaas 			    acpi_device_hid(device))) {
469e49bd2ddSZhang Rui 			acpi_device_bus_id->instance_no++;
470e49bd2ddSZhang Rui 			found = 1;
471e49bd2ddSZhang Rui 			kfree(new_bus_id);
472e49bd2ddSZhang Rui 			break;
473e49bd2ddSZhang Rui 		}
474e49bd2ddSZhang Rui 	}
475e49bd2ddSZhang Rui 	if (!found) {
476e49bd2ddSZhang Rui 		acpi_device_bus_id = new_bus_id;
4771131b938SBjorn Helgaas 		strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
478e49bd2ddSZhang Rui 		acpi_device_bus_id->instance_no = 0;
479e49bd2ddSZhang Rui 		list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
480e49bd2ddSZhang Rui 	}
4810794469dSKay Sievers 	dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
482e49bd2ddSZhang Rui 
48333b57150SLen Brown 	if (device->parent)
4841da177e4SLinus Torvalds 		list_add_tail(&device->node, &device->parent->children);
48533b57150SLen Brown 
4861da177e4SLinus Torvalds 	if (device->wakeup.flags.valid)
4871da177e4SLinus Torvalds 		list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
4889090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
4891da177e4SLinus Torvalds 
4901890a97aSPatrick Mochel 	if (device->parent)
49166b7ed40SBjorn Helgaas 		device->dev.parent = &device->parent->dev;
4921890a97aSPatrick Mochel 	device->dev.bus = &acpi_bus_type;
4931890a97aSPatrick Mochel 	device->dev.release = &acpi_device_release;
4948b12b922SAlex Chiang 	result = device_register(&device->dev);
495e49bd2ddSZhang Rui 	if (result) {
4968b12b922SAlex Chiang 		dev_err(&device->dev, "Error registering device\n");
497e49bd2ddSZhang Rui 		goto end;
498e49bd2ddSZhang Rui 	}
499f883d9dbSPatrick Mochel 
500e49bd2ddSZhang Rui 	result = acpi_device_setup_files(device);
501e49bd2ddSZhang Rui 	if (result)
5020794469dSKay Sievers 		printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
5030794469dSKay Sievers 		       dev_name(&device->dev));
504e49bd2ddSZhang Rui 
50596333578SLi Shaohua 	device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
506e49bd2ddSZhang Rui 	return 0;
507e49bd2ddSZhang Rui end:
5089090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
50933b57150SLen Brown 	if (device->parent)
510e49bd2ddSZhang Rui 		list_del(&device->node);
511e49bd2ddSZhang Rui 	list_del(&device->wakeup_list);
5129090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
513e49bd2ddSZhang Rui 	return result;
5141da177e4SLinus Torvalds }
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds static void acpi_device_unregister(struct acpi_device *device, int type)
5171da177e4SLinus Torvalds {
5189090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
51933b57150SLen Brown 	if (device->parent)
5201da177e4SLinus Torvalds 		list_del(&device->node);
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	list_del(&device->wakeup_list);
5239090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 	acpi_detach_data(device->handle, acpi_bus_data_handler);
5261890a97aSPatrick Mochel 
527f883d9dbSPatrick Mochel 	acpi_device_remove_files(device);
5281890a97aSPatrick Mochel 	device_unregister(&device->dev);
5291da177e4SLinus Torvalds }
5301da177e4SLinus Torvalds 
5319e89dde2SZhang Rui /* --------------------------------------------------------------------------
5329e89dde2SZhang Rui                                  Driver Management
5339e89dde2SZhang Rui    -------------------------------------------------------------------------- */
5341da177e4SLinus Torvalds /**
535d758a8faSRandy Dunlap  * acpi_bus_driver_init - add a device to a driver
536d758a8faSRandy Dunlap  * @device: the device to add and initialize
537d758a8faSRandy Dunlap  * @driver: driver for the device
538d758a8faSRandy Dunlap  *
5391da177e4SLinus Torvalds  * Used to initialize a device via its device driver.  Called whenever a
5401890a97aSPatrick Mochel  * driver is bound to a device.  Invokes the driver's add() ops.
5411da177e4SLinus Torvalds  */
5421da177e4SLinus Torvalds static int
5434be44fcdSLen Brown acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
5441da177e4SLinus Torvalds {
5451da177e4SLinus Torvalds 	int result = 0;
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	if (!device || !driver)
548d550d98dSPatrick Mochel 		return -EINVAL;
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 	if (!driver->ops.add)
551d550d98dSPatrick Mochel 		return -ENOSYS;
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	result = driver->ops.add(device);
5541da177e4SLinus Torvalds 	if (result) {
5551da177e4SLinus Torvalds 		device->driver = NULL;
556db89b4f0SPavel Machek 		device->driver_data = NULL;
557d550d98dSPatrick Mochel 		return result;
5581da177e4SLinus Torvalds 	}
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	device->driver = driver;
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	/*
5631da177e4SLinus Torvalds 	 * TBD - Configuration Management: Assign resources to device based
5641da177e4SLinus Torvalds 	 * upon possible configuration and currently allocated resources.
5651da177e4SLinus Torvalds 	 */
5661da177e4SLinus Torvalds 
5674be44fcdSLen Brown 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
5684be44fcdSLen Brown 			  "Driver successfully bound to device\n"));
569d550d98dSPatrick Mochel 	return 0;
5703fb02738SRajesh Shah }
5713fb02738SRajesh Shah 
5728713cbefSAdrian Bunk static int acpi_start_single_object(struct acpi_device *device)
5733fb02738SRajesh Shah {
5743fb02738SRajesh Shah 	int result = 0;
5753fb02738SRajesh Shah 	struct acpi_driver *driver;
5763fb02738SRajesh Shah 
5773fb02738SRajesh Shah 
5783fb02738SRajesh Shah 	if (!(driver = device->driver))
579d550d98dSPatrick Mochel 		return 0;
5803fb02738SRajesh Shah 
5811da177e4SLinus Torvalds 	if (driver->ops.start) {
5821da177e4SLinus Torvalds 		result = driver->ops.start(device);
5831da177e4SLinus Torvalds 		if (result && driver->ops.remove)
5841da177e4SLinus Torvalds 			driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
5853fb02738SRajesh Shah 	}
5863fb02738SRajesh Shah 
587d550d98dSPatrick Mochel 	return result;
5881da177e4SLinus Torvalds }
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds /**
591d758a8faSRandy Dunlap  * acpi_bus_register_driver - register a driver with the ACPI bus
592d758a8faSRandy Dunlap  * @driver: driver being registered
593d758a8faSRandy Dunlap  *
5941da177e4SLinus Torvalds  * Registers a driver with the ACPI bus.  Searches the namespace for all
5959d9f749bSBjorn Helgaas  * devices that match the driver's criteria and binds.  Returns zero for
5969d9f749bSBjorn Helgaas  * success or a negative error status for failure.
5971da177e4SLinus Torvalds  */
5984be44fcdSLen Brown int acpi_bus_register_driver(struct acpi_driver *driver)
5991da177e4SLinus Torvalds {
6001890a97aSPatrick Mochel 	int ret;
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	if (acpi_disabled)
603d550d98dSPatrick Mochel 		return -ENODEV;
6041890a97aSPatrick Mochel 	driver->drv.name = driver->name;
6051890a97aSPatrick Mochel 	driver->drv.bus = &acpi_bus_type;
6061890a97aSPatrick Mochel 	driver->drv.owner = driver->owner;
6071da177e4SLinus Torvalds 
6081890a97aSPatrick Mochel 	ret = driver_register(&driver->drv);
6091890a97aSPatrick Mochel 	return ret;
6101da177e4SLinus Torvalds }
6111da177e4SLinus Torvalds 
6124be44fcdSLen Brown EXPORT_SYMBOL(acpi_bus_register_driver);
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds /**
615d758a8faSRandy Dunlap  * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
616d758a8faSRandy Dunlap  * @driver: driver to unregister
617d758a8faSRandy Dunlap  *
6181da177e4SLinus Torvalds  * Unregisters a driver with the ACPI bus.  Searches the namespace for all
6191da177e4SLinus Torvalds  * devices that match the driver's criteria and unbinds.
6201da177e4SLinus Torvalds  */
62106ea8e08SBjorn Helgaas void acpi_bus_unregister_driver(struct acpi_driver *driver)
6221da177e4SLinus Torvalds {
6231890a97aSPatrick Mochel 	driver_unregister(&driver->drv);
6241da177e4SLinus Torvalds }
6254be44fcdSLen Brown 
6261da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_unregister_driver);
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds /* --------------------------------------------------------------------------
6291da177e4SLinus Torvalds                                  Device Enumeration
6301da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
6315c478f49SBjorn Helgaas static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
6325c478f49SBjorn Helgaas {
6335c478f49SBjorn Helgaas 	acpi_status status;
6345c478f49SBjorn Helgaas 	int ret;
6355c478f49SBjorn Helgaas 	struct acpi_device *device;
6365c478f49SBjorn Helgaas 
6375c478f49SBjorn Helgaas 	/*
6385c478f49SBjorn Helgaas 	 * Fixed hardware devices do not appear in the namespace and do not
6395c478f49SBjorn Helgaas 	 * have handles, but we fabricate acpi_devices for them, so we have
6405c478f49SBjorn Helgaas 	 * to deal with them specially.
6415c478f49SBjorn Helgaas 	 */
6425c478f49SBjorn Helgaas 	if (handle == NULL)
6435c478f49SBjorn Helgaas 		return acpi_root;
6445c478f49SBjorn Helgaas 
6455c478f49SBjorn Helgaas 	do {
6465c478f49SBjorn Helgaas 		status = acpi_get_parent(handle, &handle);
6475c478f49SBjorn Helgaas 		if (status == AE_NULL_ENTRY)
6485c478f49SBjorn Helgaas 			return NULL;
6495c478f49SBjorn Helgaas 		if (ACPI_FAILURE(status))
6505c478f49SBjorn Helgaas 			return acpi_root;
6515c478f49SBjorn Helgaas 
6525c478f49SBjorn Helgaas 		ret = acpi_bus_get_device(handle, &device);
6535c478f49SBjorn Helgaas 		if (ret == 0)
6545c478f49SBjorn Helgaas 			return device;
6555c478f49SBjorn Helgaas 	} while (1);
6565c478f49SBjorn Helgaas }
6575c478f49SBjorn Helgaas 
658c8f7a62cSLen Brown acpi_status
659c8f7a62cSLen Brown acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
660c8f7a62cSLen Brown {
661c8f7a62cSLen Brown 	acpi_status status;
662c8f7a62cSLen Brown 	acpi_handle tmp;
663c8f7a62cSLen Brown 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
664c8f7a62cSLen Brown 	union acpi_object *obj;
665c8f7a62cSLen Brown 
666c8f7a62cSLen Brown 	status = acpi_get_handle(handle, "_EJD", &tmp);
667c8f7a62cSLen Brown 	if (ACPI_FAILURE(status))
668c8f7a62cSLen Brown 		return status;
669c8f7a62cSLen Brown 
670c8f7a62cSLen Brown 	status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
671c8f7a62cSLen Brown 	if (ACPI_SUCCESS(status)) {
672c8f7a62cSLen Brown 		obj = buffer.pointer;
6733b5fee59SHolger Macht 		status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
6743b5fee59SHolger Macht 					 ejd);
675c8f7a62cSLen Brown 		kfree(buffer.pointer);
676c8f7a62cSLen Brown 	}
677c8f7a62cSLen Brown 	return status;
678c8f7a62cSLen Brown }
679c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
680c8f7a62cSLen Brown 
6818e4319c4SBob Moore void acpi_bus_data_handler(acpi_handle handle, void *context)
6821da177e4SLinus Torvalds {
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 	/* TBD */
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 	return;
6871da177e4SLinus Torvalds }
6881da177e4SLinus Torvalds 
6899e89dde2SZhang Rui static int acpi_bus_get_perf_flags(struct acpi_device *device)
6909e89dde2SZhang Rui {
6919e89dde2SZhang Rui 	device->performance.state = ACPI_STATE_UNKNOWN;
6929e89dde2SZhang Rui 	return 0;
6939e89dde2SZhang Rui }
6949e89dde2SZhang Rui 
6951da177e4SLinus Torvalds static acpi_status
6961da177e4SLinus Torvalds acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
6971da177e4SLinus Torvalds 					     union acpi_object *package)
6981da177e4SLinus Torvalds {
6991da177e4SLinus Torvalds 	int i = 0;
7001da177e4SLinus Torvalds 	union acpi_object *element = NULL;
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 	if (!device || !package || (package->package.count < 2))
7031da177e4SLinus Torvalds 		return AE_BAD_PARAMETER;
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	element = &(package->package.elements[0]);
7061da177e4SLinus Torvalds 	if (!element)
7071da177e4SLinus Torvalds 		return AE_BAD_PARAMETER;
7081da177e4SLinus Torvalds 	if (element->type == ACPI_TYPE_PACKAGE) {
7091da177e4SLinus Torvalds 		if ((element->package.count < 2) ||
7101da177e4SLinus Torvalds 		    (element->package.elements[0].type !=
7111da177e4SLinus Torvalds 		     ACPI_TYPE_LOCAL_REFERENCE)
7121da177e4SLinus Torvalds 		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
7131da177e4SLinus Torvalds 			return AE_BAD_DATA;
7141da177e4SLinus Torvalds 		device->wakeup.gpe_device =
7151da177e4SLinus Torvalds 		    element->package.elements[0].reference.handle;
7161da177e4SLinus Torvalds 		device->wakeup.gpe_number =
7171da177e4SLinus Torvalds 		    (u32) element->package.elements[1].integer.value;
7181da177e4SLinus Torvalds 	} else if (element->type == ACPI_TYPE_INTEGER) {
7191da177e4SLinus Torvalds 		device->wakeup.gpe_number = element->integer.value;
7201da177e4SLinus Torvalds 	} else
7211da177e4SLinus Torvalds 		return AE_BAD_DATA;
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	element = &(package->package.elements[1]);
7241da177e4SLinus Torvalds 	if (element->type != ACPI_TYPE_INTEGER) {
7251da177e4SLinus Torvalds 		return AE_BAD_DATA;
7261da177e4SLinus Torvalds 	}
7271da177e4SLinus Torvalds 	device->wakeup.sleep_state = element->integer.value;
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
7301da177e4SLinus Torvalds 		return AE_NO_MEMORY;
7311da177e4SLinus Torvalds 	}
7321da177e4SLinus Torvalds 	device->wakeup.resources.count = package->package.count - 2;
7331da177e4SLinus Torvalds 	for (i = 0; i < device->wakeup.resources.count; i++) {
7341da177e4SLinus Torvalds 		element = &(package->package.elements[i + 2]);
735cd0b2248SBob Moore 		if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
7361da177e4SLinus Torvalds 			return AE_BAD_DATA;
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 		device->wakeup.resources.handles[i] = element->reference.handle;
7391da177e4SLinus Torvalds 	}
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	return AE_OK;
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
744*f517709dSRafael J. Wysocki static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
7451da177e4SLinus Torvalds {
74629b71a1cSThomas Renninger 	struct acpi_device_id button_device_ids[] = {
74729b71a1cSThomas Renninger 		{"PNP0C0D", 0},
74829b71a1cSThomas Renninger 		{"PNP0C0C", 0},
74929b71a1cSThomas Renninger 		{"PNP0C0E", 0},
75029b71a1cSThomas Renninger 		{"", 0},
75129b71a1cSThomas Renninger 	};
752*f517709dSRafael J. Wysocki 	acpi_status status;
753*f517709dSRafael J. Wysocki 	acpi_event_status event_status;
754*f517709dSRafael J. Wysocki 
755*f517709dSRafael J. Wysocki 	device->wakeup.run_wake_count = 0;
756*f517709dSRafael J. Wysocki 
757*f517709dSRafael J. Wysocki 	/* Power button, Lid switch always enable wakeup */
758*f517709dSRafael J. Wysocki 	if (!acpi_match_device_ids(device, button_device_ids)) {
759*f517709dSRafael J. Wysocki 		device->wakeup.flags.run_wake = 1;
760*f517709dSRafael J. Wysocki 		device->wakeup.flags.always_enabled = 1;
761*f517709dSRafael J. Wysocki 		return;
762*f517709dSRafael J. Wysocki 	}
763*f517709dSRafael J. Wysocki 
764*f517709dSRafael J. Wysocki 	status = acpi_get_gpe_status(NULL, device->wakeup.gpe_number,
765*f517709dSRafael J. Wysocki 					ACPI_NOT_ISR, &event_status);
766*f517709dSRafael J. Wysocki 	if (status == AE_OK)
767*f517709dSRafael J. Wysocki 		device->wakeup.flags.run_wake =
768*f517709dSRafael J. Wysocki 				!!(event_status & ACPI_EVENT_FLAG_HANDLE);
769*f517709dSRafael J. Wysocki }
770*f517709dSRafael J. Wysocki 
771*f517709dSRafael J. Wysocki static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
772*f517709dSRafael J. Wysocki {
773*f517709dSRafael J. Wysocki 	acpi_status status = 0;
774*f517709dSRafael J. Wysocki 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
775*f517709dSRafael J. Wysocki 	union acpi_object *package = NULL;
776*f517709dSRafael J. Wysocki 	int psw_error;
77729b71a1cSThomas Renninger 
7781da177e4SLinus Torvalds 	/* _PRW */
7791da177e4SLinus Torvalds 	status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
7801da177e4SLinus Torvalds 	if (ACPI_FAILURE(status)) {
7811da177e4SLinus Torvalds 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
7821da177e4SLinus Torvalds 		goto end;
7831da177e4SLinus Torvalds 	}
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds 	package = (union acpi_object *)buffer.pointer;
7861da177e4SLinus Torvalds 	status = acpi_bus_extract_wakeup_device_power_package(device, package);
7871da177e4SLinus Torvalds 	if (ACPI_FAILURE(status)) {
7881da177e4SLinus Torvalds 		ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
7891da177e4SLinus Torvalds 		goto end;
7901da177e4SLinus Torvalds 	}
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	kfree(buffer.pointer);
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	device->wakeup.flags.valid = 1;
7959b83ccd2SRafael J. Wysocki 	device->wakeup.prepare_count = 0;
796*f517709dSRafael J. Wysocki 	acpi_bus_set_run_wake_flags(device);
797729b2bdbSZhao Yakui 	/* Call _PSW/_DSW object to disable its ability to wake the sleeping
798729b2bdbSZhao Yakui 	 * system for the ACPI device with the _PRW object.
799729b2bdbSZhao Yakui 	 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
800729b2bdbSZhao Yakui 	 * So it is necessary to call _DSW object first. Only when it is not
801729b2bdbSZhao Yakui 	 * present will the _PSW object used.
802729b2bdbSZhao Yakui 	 */
80377e76609SRafael J. Wysocki 	psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
80477e76609SRafael J. Wysocki 	if (psw_error)
80577e76609SRafael J. Wysocki 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
80677e76609SRafael J. Wysocki 				"error in _DSW or _PSW evaluation\n"));
80777e76609SRafael J. Wysocki 
8081da177e4SLinus Torvalds end:
8091da177e4SLinus Torvalds 	if (ACPI_FAILURE(status))
8101da177e4SLinus Torvalds 		device->flags.wake_capable = 0;
8111da177e4SLinus Torvalds 	return 0;
8121da177e4SLinus Torvalds }
8131da177e4SLinus Torvalds 
8149e89dde2SZhang Rui static int acpi_bus_get_power_flags(struct acpi_device *device)
8151da177e4SLinus Torvalds {
8169e89dde2SZhang Rui 	acpi_status status = 0;
8179e89dde2SZhang Rui 	acpi_handle handle = NULL;
8189e89dde2SZhang Rui 	u32 i = 0;
8199e89dde2SZhang Rui 
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	/*
8229e89dde2SZhang Rui 	 * Power Management Flags
8231da177e4SLinus Torvalds 	 */
8249e89dde2SZhang Rui 	status = acpi_get_handle(device->handle, "_PSC", &handle);
8251da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
8269e89dde2SZhang Rui 		device->power.flags.explicit_get = 1;
8279e89dde2SZhang Rui 	status = acpi_get_handle(device->handle, "_IRC", &handle);
8289e89dde2SZhang Rui 	if (ACPI_SUCCESS(status))
8299e89dde2SZhang Rui 		device->power.flags.inrush_current = 1;
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 	/*
8329e89dde2SZhang Rui 	 * Enumerate supported power management states
8331da177e4SLinus Torvalds 	 */
8349e89dde2SZhang Rui 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
8359e89dde2SZhang Rui 		struct acpi_device_power_state *ps = &device->power.states[i];
8369e89dde2SZhang Rui 		char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
8371da177e4SLinus Torvalds 
8389e89dde2SZhang Rui 		/* Evaluate "_PRx" to se if power resources are referenced */
8399e89dde2SZhang Rui 		acpi_evaluate_reference(device->handle, object_name, NULL,
8409e89dde2SZhang Rui 					&ps->resources);
8419e89dde2SZhang Rui 		if (ps->resources.count) {
8429e89dde2SZhang Rui 			device->power.flags.power_resources = 1;
8439e89dde2SZhang Rui 			ps->flags.valid = 1;
8441da177e4SLinus Torvalds 		}
8451da177e4SLinus Torvalds 
8469e89dde2SZhang Rui 		/* Evaluate "_PSx" to see if we can do explicit sets */
8479e89dde2SZhang Rui 		object_name[2] = 'S';
8489e89dde2SZhang Rui 		status = acpi_get_handle(device->handle, object_name, &handle);
849c8f7a62cSLen Brown 		if (ACPI_SUCCESS(status)) {
8509e89dde2SZhang Rui 			ps->flags.explicit_set = 1;
8519e89dde2SZhang Rui 			ps->flags.valid = 1;
852c8f7a62cSLen Brown 		}
853c8f7a62cSLen Brown 
8549e89dde2SZhang Rui 		/* State is valid if we have some power control */
8559e89dde2SZhang Rui 		if (ps->resources.count || ps->flags.explicit_set)
8569e89dde2SZhang Rui 			ps->flags.valid = 1;
8579e89dde2SZhang Rui 
8589e89dde2SZhang Rui 		ps->power = -1;	/* Unknown - driver assigned */
8599e89dde2SZhang Rui 		ps->latency = -1;	/* Unknown - driver assigned */
8609e89dde2SZhang Rui 	}
8619e89dde2SZhang Rui 
8629e89dde2SZhang Rui 	/* Set defaults for D0 and D3 states (always valid) */
8639e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].flags.valid = 1;
8649e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].power = 100;
8659e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D3].flags.valid = 1;
8669e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D3].power = 0;
8679e89dde2SZhang Rui 
8689e89dde2SZhang Rui 	/* TBD: System wake support and resource requirements. */
8699e89dde2SZhang Rui 
8709e89dde2SZhang Rui 	device->power.state = ACPI_STATE_UNKNOWN;
871a51e145fSZhao Yakui 	acpi_bus_get_power(device->handle, &(device->power.state));
8729e89dde2SZhang Rui 
8739e89dde2SZhang Rui 	return 0;
8749e89dde2SZhang Rui }
875c8f7a62cSLen Brown 
8764be44fcdSLen Brown static int acpi_bus_get_flags(struct acpi_device *device)
8771da177e4SLinus Torvalds {
8781da177e4SLinus Torvalds 	acpi_status status = AE_OK;
8791da177e4SLinus Torvalds 	acpi_handle temp = NULL;
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	/* Presence of _STA indicates 'dynamic_status' */
8831da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_STA", &temp);
8841da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
8851da177e4SLinus Torvalds 		device->flags.dynamic_status = 1;
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	/* Presence of _RMV indicates 'removable' */
8881da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_RMV", &temp);
8891da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
8901da177e4SLinus Torvalds 		device->flags.removable = 1;
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
8931da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_EJD", &temp);
8941da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
8951da177e4SLinus Torvalds 		device->flags.ejectable = 1;
8961da177e4SLinus Torvalds 	else {
8971da177e4SLinus Torvalds 		status = acpi_get_handle(device->handle, "_EJ0", &temp);
8981da177e4SLinus Torvalds 		if (ACPI_SUCCESS(status))
8991da177e4SLinus Torvalds 			device->flags.ejectable = 1;
9001da177e4SLinus Torvalds 	}
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	/* Presence of _LCK indicates 'lockable' */
9031da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_LCK", &temp);
9041da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
9051da177e4SLinus Torvalds 		device->flags.lockable = 1;
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	/* Presence of _PS0|_PR0 indicates 'power manageable' */
9081da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_PS0", &temp);
9091da177e4SLinus Torvalds 	if (ACPI_FAILURE(status))
9101da177e4SLinus Torvalds 		status = acpi_get_handle(device->handle, "_PR0", &temp);
9111da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
9121da177e4SLinus Torvalds 		device->flags.power_manageable = 1;
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 	/* Presence of _PRW indicates wake capable */
9151da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_PRW", &temp);
9161da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
9171da177e4SLinus Torvalds 		device->flags.wake_capable = 1;
9181da177e4SLinus Torvalds 
9193c5f9be4SJoe Perches 	/* TBD: Performance management */
9201da177e4SLinus Torvalds 
921d550d98dSPatrick Mochel 	return 0;
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds 
924c7bcb4e9SBjorn Helgaas static void acpi_device_get_busid(struct acpi_device *device)
9251da177e4SLinus Torvalds {
9261da177e4SLinus Torvalds 	char bus_id[5] = { '?', 0 };
9271da177e4SLinus Torvalds 	struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
9281da177e4SLinus Torvalds 	int i = 0;
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 	/*
9311da177e4SLinus Torvalds 	 * Bus ID
9321da177e4SLinus Torvalds 	 * ------
9331da177e4SLinus Torvalds 	 * The device's Bus ID is simply the object name.
9341da177e4SLinus Torvalds 	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
9351da177e4SLinus Torvalds 	 */
936859ac9a4SBjorn Helgaas 	if (ACPI_IS_ROOT_DEVICE(device)) {
9371da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "ACPI");
938859ac9a4SBjorn Helgaas 		return;
939859ac9a4SBjorn Helgaas 	}
940859ac9a4SBjorn Helgaas 
941859ac9a4SBjorn Helgaas 	switch (device->device_type) {
9421da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
9431da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "PWRF");
9441da177e4SLinus Torvalds 		break;
9451da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
9461da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "SLPF");
9471da177e4SLinus Torvalds 		break;
9481da177e4SLinus Torvalds 	default:
94966b7ed40SBjorn Helgaas 		acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
9501da177e4SLinus Torvalds 		/* Clean up trailing underscores (if any) */
9511da177e4SLinus Torvalds 		for (i = 3; i > 1; i--) {
9521da177e4SLinus Torvalds 			if (bus_id[i] == '_')
9531da177e4SLinus Torvalds 				bus_id[i] = '\0';
9541da177e4SLinus Torvalds 			else
9551da177e4SLinus Torvalds 				break;
9561da177e4SLinus Torvalds 		}
9571da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, bus_id);
9581da177e4SLinus Torvalds 		break;
9591da177e4SLinus Torvalds 	}
9601da177e4SLinus Torvalds }
9611da177e4SLinus Torvalds 
96254735266SZhang Rui /*
96354735266SZhang Rui  * acpi_bay_match - see if a device is an ejectable driver bay
96454735266SZhang Rui  *
96554735266SZhang Rui  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
96654735266SZhang Rui  * then we can safely call it an ejectable drive bay
96754735266SZhang Rui  */
96854735266SZhang Rui static int acpi_bay_match(struct acpi_device *device){
96954735266SZhang Rui 	acpi_status status;
97054735266SZhang Rui 	acpi_handle handle;
97154735266SZhang Rui 	acpi_handle tmp;
97254735266SZhang Rui 	acpi_handle phandle;
97354735266SZhang Rui 
97454735266SZhang Rui 	handle = device->handle;
97554735266SZhang Rui 
97654735266SZhang Rui 	status = acpi_get_handle(handle, "_EJ0", &tmp);
97754735266SZhang Rui 	if (ACPI_FAILURE(status))
97854735266SZhang Rui 		return -ENODEV;
97954735266SZhang Rui 
98054735266SZhang Rui 	if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
98154735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
98254735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
98354735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
98454735266SZhang Rui 		return 0;
98554735266SZhang Rui 
98654735266SZhang Rui 	if (acpi_get_parent(handle, &phandle))
98754735266SZhang Rui 		return -ENODEV;
98854735266SZhang Rui 
98954735266SZhang Rui         if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
99054735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
99154735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
99254735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
99354735266SZhang Rui                 return 0;
99454735266SZhang Rui 
99554735266SZhang Rui 	return -ENODEV;
99654735266SZhang Rui }
99754735266SZhang Rui 
9983620f2f2SFrank Seidel /*
9993620f2f2SFrank Seidel  * acpi_dock_match - see if a device has a _DCK method
10003620f2f2SFrank Seidel  */
10013620f2f2SFrank Seidel static int acpi_dock_match(struct acpi_device *device)
10023620f2f2SFrank Seidel {
10033620f2f2SFrank Seidel 	acpi_handle tmp;
10043620f2f2SFrank Seidel 	return acpi_get_handle(device->handle, "_DCK", &tmp);
10053620f2f2SFrank Seidel }
10063620f2f2SFrank Seidel 
10077f47fa6cSBjorn Helgaas char *acpi_device_hid(struct acpi_device *device)
100815b8dd53SBob Moore {
10097f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *hid;
101015b8dd53SBob Moore 
10117f47fa6cSBjorn Helgaas 	hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
10127f47fa6cSBjorn Helgaas 	return hid->id;
10137f47fa6cSBjorn Helgaas }
10147f47fa6cSBjorn Helgaas EXPORT_SYMBOL(acpi_device_hid);
101515b8dd53SBob Moore 
10167f47fa6cSBjorn Helgaas static void acpi_add_id(struct acpi_device *device, const char *dev_id)
10177f47fa6cSBjorn Helgaas {
10187f47fa6cSBjorn Helgaas 	struct acpi_hardware_id *id;
101915b8dd53SBob Moore 
10207f47fa6cSBjorn Helgaas 	id = kmalloc(sizeof(*id), GFP_KERNEL);
10217f47fa6cSBjorn Helgaas 	if (!id)
10227f47fa6cSBjorn Helgaas 		return;
102315b8dd53SBob Moore 
10247f47fa6cSBjorn Helgaas 	id->id = kmalloc(strlen(dev_id) + 1, GFP_KERNEL);
10257f47fa6cSBjorn Helgaas 	if (!id->id) {
10267f47fa6cSBjorn Helgaas 		kfree(id);
10277f47fa6cSBjorn Helgaas 		return;
102815b8dd53SBob Moore 	}
102915b8dd53SBob Moore 
10307f47fa6cSBjorn Helgaas 	strcpy(id->id, dev_id);
10317f47fa6cSBjorn Helgaas 	list_add_tail(&id->list, &device->pnp.ids);
103215b8dd53SBob Moore }
103315b8dd53SBob Moore 
1034c7bcb4e9SBjorn Helgaas static void acpi_device_set_id(struct acpi_device *device)
10351da177e4SLinus Torvalds {
10361da177e4SLinus Torvalds 	acpi_status status;
103757f3674fSBjorn Helgaas 	struct acpi_device_info *info;
103857f3674fSBjorn Helgaas 	struct acpica_device_id_list *cid_list;
10397f47fa6cSBjorn Helgaas 	int i;
10401da177e4SLinus Torvalds 
1041c7bcb4e9SBjorn Helgaas 	switch (device->device_type) {
10421da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
1043859ac9a4SBjorn Helgaas 		if (ACPI_IS_ROOT_DEVICE(device)) {
104457f3674fSBjorn Helgaas 			acpi_add_id(device, ACPI_SYSTEM_HID);
1045859ac9a4SBjorn Helgaas 			break;
104678b8e141SBjorn Helgaas 		} else if (ACPI_IS_ROOT_DEVICE(device->parent)) {
104778b8e141SBjorn Helgaas 			/* \_SB_, the only root-level namespace device */
104857f3674fSBjorn Helgaas 			acpi_add_id(device, ACPI_BUS_HID);
104978b8e141SBjorn Helgaas 			strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
105078b8e141SBjorn Helgaas 			strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
105178b8e141SBjorn Helgaas 			break;
1052859ac9a4SBjorn Helgaas 		}
1053859ac9a4SBjorn Helgaas 
105466b7ed40SBjorn Helgaas 		status = acpi_get_object_info(device->handle, &info);
10551da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
105696b2dd1fSHarvey Harrison 			printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
10571da177e4SLinus Torvalds 			return;
10581da177e4SLinus Torvalds 		}
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_HID)
106157f3674fSBjorn Helgaas 			acpi_add_id(device, info->hardware_id.string);
106257f3674fSBjorn Helgaas 		if (info->valid & ACPI_VALID_CID) {
106315b8dd53SBob Moore 			cid_list = &info->compatible_id_list;
106457f3674fSBjorn Helgaas 			for (i = 0; i < cid_list->count; i++)
106557f3674fSBjorn Helgaas 				acpi_add_id(device, cid_list->ids[i].string);
106657f3674fSBjorn Helgaas 		}
10671da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_ADR) {
10681da177e4SLinus Torvalds 			device->pnp.bus_address = info->address;
10691da177e4SLinus Torvalds 			device->flags.bus_address = 1;
10701da177e4SLinus Torvalds 		}
1071ae843332SZhang Rui 
1072a83893aeSBjorn Helgaas 		kfree(info);
1073a83893aeSBjorn Helgaas 
107457f3674fSBjorn Helgaas 		/*
107557f3674fSBjorn Helgaas 		 * Some devices don't reliably have _HIDs & _CIDs, so add
107657f3674fSBjorn Helgaas 		 * synthetic HIDs to make sure drivers can find them.
10773620f2f2SFrank Seidel 		 */
1078c3d6de69SThomas Renninger 		if (acpi_is_video_device(device))
107957f3674fSBjorn Helgaas 			acpi_add_id(device, ACPI_VIDEO_HID);
10803620f2f2SFrank Seidel 		else if (ACPI_SUCCESS(acpi_bay_match(device)))
108157f3674fSBjorn Helgaas 			acpi_add_id(device, ACPI_BAY_HID);
10823620f2f2SFrank Seidel 		else if (ACPI_SUCCESS(acpi_dock_match(device)))
108357f3674fSBjorn Helgaas 			acpi_add_id(device, ACPI_DOCK_HID);
108454735266SZhang Rui 
10851da177e4SLinus Torvalds 		break;
10861da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER:
108757f3674fSBjorn Helgaas 		acpi_add_id(device, ACPI_POWER_HID);
10881da177e4SLinus Torvalds 		break;
10891da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_PROCESSOR:
109057f3674fSBjorn Helgaas 		acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
10911da177e4SLinus Torvalds 		break;
10921da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_THERMAL:
109357f3674fSBjorn Helgaas 		acpi_add_id(device, ACPI_THERMAL_HID);
10941da177e4SLinus Torvalds 		break;
10951da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
109657f3674fSBjorn Helgaas 		acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
10971da177e4SLinus Torvalds 		break;
10981da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
109957f3674fSBjorn Helgaas 		acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
11001da177e4SLinus Torvalds 		break;
11011da177e4SLinus Torvalds 	}
11021da177e4SLinus Torvalds 
1103b1fbfb2aSBjorn Helgaas 	/*
1104b1fbfb2aSBjorn Helgaas 	 * We build acpi_devices for some objects that don't have _HID or _CID,
1105b1fbfb2aSBjorn Helgaas 	 * e.g., PCI bridges and slots.  Drivers can't bind to these objects,
1106b1fbfb2aSBjorn Helgaas 	 * but we do use them indirectly by traversing the acpi_device tree.
1107b1fbfb2aSBjorn Helgaas 	 * This generic ID isn't useful for driver binding, but it provides
1108b1fbfb2aSBjorn Helgaas 	 * the useful property that "every acpi_device has an ID."
1109b1fbfb2aSBjorn Helgaas 	 */
111057f3674fSBjorn Helgaas 	if (list_empty(&device->pnp.ids))
111157f3674fSBjorn Helgaas 		acpi_add_id(device, "device");
11121da177e4SLinus Torvalds }
11131da177e4SLinus Torvalds 
1114bc3b0772SBjorn Helgaas static int acpi_device_set_context(struct acpi_device *device)
11151da177e4SLinus Torvalds {
1116bc3b0772SBjorn Helgaas 	acpi_status status;
1117bc3b0772SBjorn Helgaas 
11181da177e4SLinus Torvalds 	/*
11191da177e4SLinus Torvalds 	 * Context
11201da177e4SLinus Torvalds 	 * -------
11211da177e4SLinus Torvalds 	 * Attach this 'struct acpi_device' to the ACPI object.  This makes
1122bc3b0772SBjorn Helgaas 	 * resolutions from handle->device very efficient.  Fixed hardware
1123bc3b0772SBjorn Helgaas 	 * devices have no handles, so we skip them.
11241da177e4SLinus Torvalds 	 */
1125bc3b0772SBjorn Helgaas 	if (!device->handle)
1126bc3b0772SBjorn Helgaas 		return 0;
1127bc3b0772SBjorn Helgaas 
11281da177e4SLinus Torvalds 	status = acpi_attach_data(device->handle,
11291da177e4SLinus Torvalds 				  acpi_bus_data_handler, device);
1130bc3b0772SBjorn Helgaas 	if (ACPI_SUCCESS(status))
1131bc3b0772SBjorn Helgaas 		return 0;
11321da177e4SLinus Torvalds 
113387ecd5cdSLen Brown 	printk(KERN_ERR PREFIX "Error attaching device data\n");
1134bc3b0772SBjorn Helgaas 	return -ENODEV;
11351da177e4SLinus Torvalds }
11361da177e4SLinus Torvalds 
11374be44fcdSLen Brown static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
11381da177e4SLinus Torvalds {
11391da177e4SLinus Torvalds 	if (!dev)
1140d550d98dSPatrick Mochel 		return -EINVAL;
11411da177e4SLinus Torvalds 
114296333578SLi Shaohua 	dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
11431890a97aSPatrick Mochel 	device_release_driver(&dev->dev);
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds 	if (!rmdevice)
1146d550d98dSPatrick Mochel 		return 0;
11471da177e4SLinus Torvalds 
11482786f6e3SRui Zhang 	/*
11492786f6e3SRui Zhang 	 * unbind _ADR-Based Devices when hot removal
11502786f6e3SRui Zhang 	 */
11511da177e4SLinus Torvalds 	if (dev->flags.bus_address) {
11521da177e4SLinus Torvalds 		if ((dev->parent) && (dev->parent->ops.unbind))
11531da177e4SLinus Torvalds 			dev->parent->ops.unbind(dev);
11541da177e4SLinus Torvalds 	}
11551da177e4SLinus Torvalds 	acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
11561da177e4SLinus Torvalds 
1157d550d98dSPatrick Mochel 	return 0;
11581da177e4SLinus Torvalds }
11591da177e4SLinus Torvalds 
11605c478f49SBjorn Helgaas static int acpi_add_single_object(struct acpi_device **child,
11615c478f49SBjorn Helgaas 				  acpi_handle handle, int type,
1162778cbc1dSBjorn Helgaas 				  unsigned long long sta,
1163c4168bffSLi Shaohua 				  struct acpi_bus_ops *ops)
11641da177e4SLinus Torvalds {
116577c24888SBjorn Helgaas 	int result;
116677c24888SBjorn Helgaas 	struct acpi_device *device;
116729aaefa6SBjorn Helgaas 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
11681da177e4SLinus Torvalds 
116936bcbec7SBurman Yan 	device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
11701da177e4SLinus Torvalds 	if (!device) {
11716468463aSLen Brown 		printk(KERN_ERR PREFIX "Memory allocation error\n");
1172d550d98dSPatrick Mochel 		return -ENOMEM;
11731da177e4SLinus Torvalds 	}
11741da177e4SLinus Torvalds 
11757f47fa6cSBjorn Helgaas 	INIT_LIST_HEAD(&device->pnp.ids);
1176caaa6efbSBjorn Helgaas 	device->device_type = type;
11771da177e4SLinus Torvalds 	device->handle = handle;
11785c478f49SBjorn Helgaas 	device->parent = acpi_bus_get_parent(handle);
1179c4168bffSLi Shaohua 	device->bus_ops = *ops; /* workround for not call .start */
1180778cbc1dSBjorn Helgaas 	STRUCT_TO_INT(device->status) = sta;
1181c4168bffSLi Shaohua 
1182c7bcb4e9SBjorn Helgaas 	acpi_device_get_busid(device);
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds 	/*
11851da177e4SLinus Torvalds 	 * Flags
11861da177e4SLinus Torvalds 	 * -----
1187778cbc1dSBjorn Helgaas 	 * Note that we only look for object handles -- cannot evaluate objects
1188778cbc1dSBjorn Helgaas 	 * until we know the device is present and properly initialized.
11891da177e4SLinus Torvalds 	 */
11901da177e4SLinus Torvalds 	result = acpi_bus_get_flags(device);
11911da177e4SLinus Torvalds 	if (result)
11921da177e4SLinus Torvalds 		goto end;
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 	/*
11951da177e4SLinus Torvalds 	 * Initialize Device
11961da177e4SLinus Torvalds 	 * -----------------
11971da177e4SLinus Torvalds 	 * TBD: Synch with Core's enumeration/initialization process.
11981da177e4SLinus Torvalds 	 */
1199c7bcb4e9SBjorn Helgaas 	acpi_device_set_id(device);
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds 	/*
12021da177e4SLinus Torvalds 	 * Power Management
12031da177e4SLinus Torvalds 	 * ----------------
12041da177e4SLinus Torvalds 	 */
12051da177e4SLinus Torvalds 	if (device->flags.power_manageable) {
12061da177e4SLinus Torvalds 		result = acpi_bus_get_power_flags(device);
12071da177e4SLinus Torvalds 		if (result)
12081da177e4SLinus Torvalds 			goto end;
12091da177e4SLinus Torvalds 	}
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 	/*
12121da177e4SLinus Torvalds 	 * Wakeup device management
12131da177e4SLinus Torvalds 	 *-----------------------
12141da177e4SLinus Torvalds 	 */
12151da177e4SLinus Torvalds 	if (device->flags.wake_capable) {
12161da177e4SLinus Torvalds 		result = acpi_bus_get_wakeup_device_flags(device);
12171da177e4SLinus Torvalds 		if (result)
12181da177e4SLinus Torvalds 			goto end;
12191da177e4SLinus Torvalds 	}
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 	/*
12221da177e4SLinus Torvalds 	 * Performance Management
12231da177e4SLinus Torvalds 	 * ----------------------
12241da177e4SLinus Torvalds 	 */
12251da177e4SLinus Torvalds 	if (device->flags.performance_manageable) {
12261da177e4SLinus Torvalds 		result = acpi_bus_get_perf_flags(device);
12271da177e4SLinus Torvalds 		if (result)
12281da177e4SLinus Torvalds 			goto end;
12291da177e4SLinus Torvalds 	}
12301da177e4SLinus Torvalds 
1231bc3b0772SBjorn Helgaas 	if ((result = acpi_device_set_context(device)))
1232f61f9258SLen Brown 		goto end;
12331da177e4SLinus Torvalds 
123466b7ed40SBjorn Helgaas 	result = acpi_device_register(device);
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 	/*
12372786f6e3SRui Zhang 	 * Bind _ADR-Based Devices when hot add
12381da177e4SLinus Torvalds 	 */
12391da177e4SLinus Torvalds 	if (device->flags.bus_address) {
12401da177e4SLinus Torvalds 		if (device->parent && device->parent->ops.bind)
12411da177e4SLinus Torvalds 			device->parent->ops.bind(device);
12421da177e4SLinus Torvalds 	}
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds end:
124529aaefa6SBjorn Helgaas 	if (!result) {
124629aaefa6SBjorn Helgaas 		acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
124729aaefa6SBjorn Helgaas 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
124829aaefa6SBjorn Helgaas 			"Adding %s [%s] parent %s\n", dev_name(&device->dev),
124929aaefa6SBjorn Helgaas 			 (char *) buffer.pointer,
125029aaefa6SBjorn Helgaas 			 device->parent ? dev_name(&device->parent->dev) :
125129aaefa6SBjorn Helgaas 					  "(null)"));
125229aaefa6SBjorn Helgaas 		kfree(buffer.pointer);
12531da177e4SLinus Torvalds 		*child = device;
125429aaefa6SBjorn Helgaas 	} else
1255718fb0deSHugh Dickins 		acpi_device_release(&device->dev);
12561da177e4SLinus Torvalds 
1257d550d98dSPatrick Mochel 	return result;
12581da177e4SLinus Torvalds }
12591da177e4SLinus Torvalds 
1260778cbc1dSBjorn Helgaas #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1261778cbc1dSBjorn Helgaas 			  ACPI_STA_DEVICE_UI      | ACPI_STA_DEVICE_FUNCTIONING)
1262778cbc1dSBjorn Helgaas 
1263778cbc1dSBjorn Helgaas static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1264778cbc1dSBjorn Helgaas 				    unsigned long long *sta)
1265778cbc1dSBjorn Helgaas {
1266778cbc1dSBjorn Helgaas 	acpi_status status;
1267778cbc1dSBjorn Helgaas 	acpi_object_type acpi_type;
1268778cbc1dSBjorn Helgaas 
1269778cbc1dSBjorn Helgaas 	status = acpi_get_type(handle, &acpi_type);
1270778cbc1dSBjorn Helgaas 	if (ACPI_FAILURE(status))
1271778cbc1dSBjorn Helgaas 		return -ENODEV;
1272778cbc1dSBjorn Helgaas 
1273778cbc1dSBjorn Helgaas 	switch (acpi_type) {
1274778cbc1dSBjorn Helgaas 	case ACPI_TYPE_ANY:		/* for ACPI_ROOT_OBJECT */
1275778cbc1dSBjorn Helgaas 	case ACPI_TYPE_DEVICE:
1276778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_DEVICE;
1277778cbc1dSBjorn Helgaas 		status = acpi_bus_get_status_handle(handle, sta);
1278778cbc1dSBjorn Helgaas 		if (ACPI_FAILURE(status))
1279778cbc1dSBjorn Helgaas 			return -ENODEV;
1280778cbc1dSBjorn Helgaas 		break;
1281778cbc1dSBjorn Helgaas 	case ACPI_TYPE_PROCESSOR:
1282778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_PROCESSOR;
1283778cbc1dSBjorn Helgaas 		status = acpi_bus_get_status_handle(handle, sta);
1284778cbc1dSBjorn Helgaas 		if (ACPI_FAILURE(status))
1285778cbc1dSBjorn Helgaas 			return -ENODEV;
1286778cbc1dSBjorn Helgaas 		break;
1287778cbc1dSBjorn Helgaas 	case ACPI_TYPE_THERMAL:
1288778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_THERMAL;
1289778cbc1dSBjorn Helgaas 		*sta = ACPI_STA_DEFAULT;
1290778cbc1dSBjorn Helgaas 		break;
1291778cbc1dSBjorn Helgaas 	case ACPI_TYPE_POWER:
1292778cbc1dSBjorn Helgaas 		*type = ACPI_BUS_TYPE_POWER;
1293778cbc1dSBjorn Helgaas 		*sta = ACPI_STA_DEFAULT;
1294778cbc1dSBjorn Helgaas 		break;
1295778cbc1dSBjorn Helgaas 	default:
1296778cbc1dSBjorn Helgaas 		return -ENODEV;
1297778cbc1dSBjorn Helgaas 	}
1298778cbc1dSBjorn Helgaas 
1299778cbc1dSBjorn Helgaas 	return 0;
1300778cbc1dSBjorn Helgaas }
1301778cbc1dSBjorn Helgaas 
130251a85fafSBjorn Helgaas static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
130351a85fafSBjorn Helgaas 				      void *context, void **return_value)
13041da177e4SLinus Torvalds {
130551a85fafSBjorn Helgaas 	struct acpi_bus_ops *ops = context;
1306778cbc1dSBjorn Helgaas 	int type;
1307778cbc1dSBjorn Helgaas 	unsigned long long sta;
1308e3b87f8aSBjorn Helgaas 	struct acpi_device *device;
1309e3b87f8aSBjorn Helgaas 	acpi_status status;
1310778cbc1dSBjorn Helgaas 	int result;
13111da177e4SLinus Torvalds 
1312778cbc1dSBjorn Helgaas 	result = acpi_bus_type_and_status(handle, &type, &sta);
1313778cbc1dSBjorn Helgaas 	if (result)
131451a85fafSBjorn Helgaas 		return AE_OK;
13151da177e4SLinus Torvalds 
1316778cbc1dSBjorn Helgaas 	if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1317778cbc1dSBjorn Helgaas 	    !(sta & ACPI_STA_DEVICE_FUNCTIONING))
1318778cbc1dSBjorn Helgaas 		return AE_CTRL_DEPTH;
13191da177e4SLinus Torvalds 
1320e3b87f8aSBjorn Helgaas 	/*
1321e3b87f8aSBjorn Helgaas 	 * We may already have an acpi_device from a previous enumeration.  If
1322e3b87f8aSBjorn Helgaas 	 * so, we needn't add it again, but we may still have to start it.
1323e3b87f8aSBjorn Helgaas 	 */
1324e3b87f8aSBjorn Helgaas 	device = NULL;
1325e3b87f8aSBjorn Helgaas 	acpi_bus_get_device(handle, &device);
1326e3b87f8aSBjorn Helgaas 	if (ops->acpi_op_add && !device)
1327e3b87f8aSBjorn Helgaas 		acpi_add_single_object(&device, handle, type, sta, ops);
13283fb02738SRajesh Shah 
1329e3b87f8aSBjorn Helgaas 	if (!device)
133051a85fafSBjorn Helgaas 		return AE_CTRL_DEPTH;
13311da177e4SLinus Torvalds 
1332c4168bffSLi Shaohua 	if (ops->acpi_op_start && !(ops->acpi_op_add)) {
133351a85fafSBjorn Helgaas 		status = acpi_start_single_object(device);
13343fb02738SRajesh Shah 		if (ACPI_FAILURE(status))
133551a85fafSBjorn Helgaas 			return AE_CTRL_DEPTH;
13363fb02738SRajesh Shah 	}
13373fb02738SRajesh Shah 
133851a85fafSBjorn Helgaas 	if (!*return_value)
133951a85fafSBjorn Helgaas 		*return_value = device;
134051a85fafSBjorn Helgaas 	return AE_OK;
13411da177e4SLinus Torvalds }
13421da177e4SLinus Torvalds 
134351a85fafSBjorn Helgaas static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
134451a85fafSBjorn Helgaas 			 struct acpi_device **child)
134551a85fafSBjorn Helgaas {
134651a85fafSBjorn Helgaas 	acpi_status status;
134751a85fafSBjorn Helgaas 	void *device = NULL;
134851a85fafSBjorn Helgaas 
134951a85fafSBjorn Helgaas 	status = acpi_bus_check_add(handle, 0, ops, &device);
135051a85fafSBjorn Helgaas 	if (ACPI_SUCCESS(status))
135151a85fafSBjorn Helgaas 		acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
13522263576cSLin Ming 				    acpi_bus_check_add, NULL, ops, &device);
135351a85fafSBjorn Helgaas 
135451a85fafSBjorn Helgaas 	if (child)
135551a85fafSBjorn Helgaas 		*child = device;
13567779688fSThomas Renninger 
13577779688fSThomas Renninger 	if (device)
1358d550d98dSPatrick Mochel 		return 0;
13597779688fSThomas Renninger 	else
13607779688fSThomas Renninger 		return -ENODEV;
13611da177e4SLinus Torvalds }
13621da177e4SLinus Torvalds 
13637779688fSThomas Renninger /*
13647779688fSThomas Renninger  * acpi_bus_add and acpi_bus_start
13657779688fSThomas Renninger  *
13667779688fSThomas Renninger  * scan a given ACPI tree and (probably recently hot-plugged)
13677779688fSThomas Renninger  * create and add or starts found devices.
13687779688fSThomas Renninger  *
13697779688fSThomas Renninger  * If no devices were found -ENODEV is returned which does not
13707779688fSThomas Renninger  * mean that this is a real error, there just have been no suitable
13717779688fSThomas Renninger  * ACPI objects in the table trunk from which the kernel could create
13727779688fSThomas Renninger  * a device and add/start an appropriate driver.
13737779688fSThomas Renninger  */
13747779688fSThomas Renninger 
13753fb02738SRajesh Shah int
13764be44fcdSLen Brown acpi_bus_add(struct acpi_device **child,
13774be44fcdSLen Brown 	     struct acpi_device *parent, acpi_handle handle, int type)
13783fb02738SRajesh Shah {
13793fb02738SRajesh Shah 	struct acpi_bus_ops ops;
13803fb02738SRajesh Shah 
13813fb02738SRajesh Shah 	memset(&ops, 0, sizeof(ops));
13823fb02738SRajesh Shah 	ops.acpi_op_add = 1;
1383c4168bffSLi Shaohua 
13847779688fSThomas Renninger 	return acpi_bus_scan(handle, &ops, child);
13853fb02738SRajesh Shah }
13863fb02738SRajesh Shah EXPORT_SYMBOL(acpi_bus_add);
13873fb02738SRajesh Shah 
13884be44fcdSLen Brown int acpi_bus_start(struct acpi_device *device)
13893fb02738SRajesh Shah {
13903fb02738SRajesh Shah 	struct acpi_bus_ops ops;
13913fb02738SRajesh Shah 
1392d2f6650aSThomas Renninger 	if (!device)
1393d2f6650aSThomas Renninger 		return -EINVAL;
1394d2f6650aSThomas Renninger 
13953fb02738SRajesh Shah 	memset(&ops, 0, sizeof(ops));
13963fb02738SRajesh Shah 	ops.acpi_op_start = 1;
13978e029bf0SBjorn Helgaas 
13987779688fSThomas Renninger 	return acpi_bus_scan(device->handle, &ops, NULL);
13993fb02738SRajesh Shah }
14003fb02738SRajesh Shah EXPORT_SYMBOL(acpi_bus_start);
14011da177e4SLinus Torvalds 
1402ceaba663SKristen Accardi int acpi_bus_trim(struct acpi_device *start, int rmdevice)
14031da177e4SLinus Torvalds {
14041da177e4SLinus Torvalds 	acpi_status status;
14051da177e4SLinus Torvalds 	struct acpi_device *parent, *child;
14061da177e4SLinus Torvalds 	acpi_handle phandle, chandle;
14071da177e4SLinus Torvalds 	acpi_object_type type;
14081da177e4SLinus Torvalds 	u32 level = 1;
14091da177e4SLinus Torvalds 	int err = 0;
14101da177e4SLinus Torvalds 
14111da177e4SLinus Torvalds 	parent = start;
14121da177e4SLinus Torvalds 	phandle = start->handle;
14131da177e4SLinus Torvalds 	child = chandle = NULL;
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds 	while ((level > 0) && parent && (!err)) {
14161da177e4SLinus Torvalds 		status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
14171da177e4SLinus Torvalds 					      chandle, &chandle);
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds 		/*
14201da177e4SLinus Torvalds 		 * If this scope is exhausted then move our way back up.
14211da177e4SLinus Torvalds 		 */
14221da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
14231da177e4SLinus Torvalds 			level--;
14241da177e4SLinus Torvalds 			chandle = phandle;
14251da177e4SLinus Torvalds 			acpi_get_parent(phandle, &phandle);
14261da177e4SLinus Torvalds 			child = parent;
14271da177e4SLinus Torvalds 			parent = parent->parent;
14281da177e4SLinus Torvalds 
14291da177e4SLinus Torvalds 			if (level == 0)
14301da177e4SLinus Torvalds 				err = acpi_bus_remove(child, rmdevice);
14311da177e4SLinus Torvalds 			else
14321da177e4SLinus Torvalds 				err = acpi_bus_remove(child, 1);
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds 			continue;
14351da177e4SLinus Torvalds 		}
14361da177e4SLinus Torvalds 
14371da177e4SLinus Torvalds 		status = acpi_get_type(chandle, &type);
14381da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
14391da177e4SLinus Torvalds 			continue;
14401da177e4SLinus Torvalds 		}
14411da177e4SLinus Torvalds 		/*
14421da177e4SLinus Torvalds 		 * If there is a device corresponding to chandle then
14431da177e4SLinus Torvalds 		 * parse it (depth-first).
14441da177e4SLinus Torvalds 		 */
14451da177e4SLinus Torvalds 		if (acpi_bus_get_device(chandle, &child) == 0) {
14461da177e4SLinus Torvalds 			level++;
14471da177e4SLinus Torvalds 			phandle = chandle;
14481da177e4SLinus Torvalds 			chandle = NULL;
14491da177e4SLinus Torvalds 			parent = child;
14501da177e4SLinus Torvalds 		}
14511da177e4SLinus Torvalds 		continue;
14521da177e4SLinus Torvalds 	}
14531da177e4SLinus Torvalds 	return err;
14541da177e4SLinus Torvalds }
1455ceaba663SKristen Accardi EXPORT_SYMBOL_GPL(acpi_bus_trim);
1456ceaba663SKristen Accardi 
1457e8b945c9SBjorn Helgaas static int acpi_bus_scan_fixed(void)
14581da177e4SLinus Torvalds {
14591da177e4SLinus Torvalds 	int result = 0;
14601da177e4SLinus Torvalds 	struct acpi_device *device = NULL;
1461c4168bffSLi Shaohua 	struct acpi_bus_ops ops;
14621da177e4SLinus Torvalds 
1463c4168bffSLi Shaohua 	memset(&ops, 0, sizeof(ops));
1464c4168bffSLi Shaohua 	ops.acpi_op_add = 1;
1465c4168bffSLi Shaohua 	ops.acpi_op_start = 1;
1466c4168bffSLi Shaohua 
14671da177e4SLinus Torvalds 	/*
14681da177e4SLinus Torvalds 	 * Enumerate all fixed-feature devices.
14691da177e4SLinus Torvalds 	 */
1470cee324b1SAlexey Starikovskiy 	if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
14715c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
1472c4168bffSLi Shaohua 						ACPI_BUS_TYPE_POWER_BUTTON,
1473778cbc1dSBjorn Helgaas 						ACPI_STA_DEFAULT,
1474c4168bffSLi Shaohua 						&ops);
14753fb02738SRajesh Shah 	}
14761da177e4SLinus Torvalds 
1477cee324b1SAlexey Starikovskiy 	if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
14785c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
1479c4168bffSLi Shaohua 						ACPI_BUS_TYPE_SLEEP_BUTTON,
1480778cbc1dSBjorn Helgaas 						ACPI_STA_DEFAULT,
1481c4168bffSLi Shaohua 						&ops);
14823fb02738SRajesh Shah 	}
14831da177e4SLinus Torvalds 
1484d550d98dSPatrick Mochel 	return result;
14851da177e4SLinus Torvalds }
14861da177e4SLinus Torvalds 
1487e747f274SBjorn Helgaas int __init acpi_scan_init(void)
14881da177e4SLinus Torvalds {
14891da177e4SLinus Torvalds 	int result;
14903fb02738SRajesh Shah 	struct acpi_bus_ops ops;
14911da177e4SLinus Torvalds 
1492c4168bffSLi Shaohua 	memset(&ops, 0, sizeof(ops));
1493c4168bffSLi Shaohua 	ops.acpi_op_add = 1;
1494c4168bffSLi Shaohua 	ops.acpi_op_start = 1;
14951da177e4SLinus Torvalds 
14965b327265SPatrick Mochel 	result = bus_register(&acpi_bus_type);
14975b327265SPatrick Mochel 	if (result) {
14985b327265SPatrick Mochel 		/* We don't want to quit even if we failed to add suspend/resume */
14995b327265SPatrick Mochel 		printk(KERN_ERR PREFIX "Could not register bus type\n");
15005b327265SPatrick Mochel 	}
15015b327265SPatrick Mochel 
15021da177e4SLinus Torvalds 	/*
15031da177e4SLinus Torvalds 	 * Enumerate devices in the ACPI namespace.
15041da177e4SLinus Torvalds 	 */
150551a85fafSBjorn Helgaas 	result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
1506c04209a7SAlexey Starikovskiy 
1507c4168bffSLi Shaohua 	if (!result)
1508adc08e20SBjorn Helgaas 		result = acpi_bus_scan_fixed();
15091da177e4SLinus Torvalds 
15101da177e4SLinus Torvalds 	if (result)
15111da177e4SLinus Torvalds 		acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
15121da177e4SLinus Torvalds 
1513d550d98dSPatrick Mochel 	return result;
15141da177e4SLinus Torvalds }
1515