xref: /openbmc/linux/drivers/acpi/scan.c (revision f61f925859c57f6175082aeeee17743c68558a6e)
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 
251da177e4SLinus Torvalds static LIST_HEAD(acpi_device_list);
26e49bd2ddSZhang Rui static LIST_HEAD(acpi_bus_id_list);
279090589dSShaohua Li DEFINE_MUTEX(acpi_device_lock);
281da177e4SLinus Torvalds LIST_HEAD(acpi_wakeup_device_list);
291da177e4SLinus Torvalds 
30e49bd2ddSZhang Rui struct acpi_device_bus_id{
31bb095854SZhang Rui 	char bus_id[15];
32e49bd2ddSZhang Rui 	unsigned int instance_no;
33e49bd2ddSZhang Rui 	struct list_head node;
34e49bd2ddSZhang Rui };
3529b71a1cSThomas Renninger 
3629b71a1cSThomas Renninger /*
3729b71a1cSThomas Renninger  * Creates hid/cid(s) string needed for modalias and uevent
3829b71a1cSThomas Renninger  * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
3929b71a1cSThomas Renninger  * char *modalias: "acpi:IBM0001:ACPI0001"
4029b71a1cSThomas Renninger */
41b3e572d2SAdrian Bunk static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
42b3e572d2SAdrian Bunk 			   int size)
43b3e572d2SAdrian Bunk {
4429b71a1cSThomas Renninger 	int len;
455c9fcb5dSZhang Rui 	int count;
4629b71a1cSThomas Renninger 
475c9fcb5dSZhang Rui 	if (!acpi_dev->flags.hardware_id && !acpi_dev->flags.compatible_ids)
4829b71a1cSThomas Renninger 		return -ENODEV;
4929b71a1cSThomas Renninger 
505c9fcb5dSZhang Rui 	len = snprintf(modalias, size, "acpi:");
5129b71a1cSThomas Renninger 	size -= len;
5229b71a1cSThomas Renninger 
535c9fcb5dSZhang Rui 	if (acpi_dev->flags.hardware_id) {
545c9fcb5dSZhang Rui 		count = snprintf(&modalias[len], size, "%s:",
555c9fcb5dSZhang Rui 				 acpi_dev->pnp.hardware_id);
565c9fcb5dSZhang Rui 		if (count < 0 || count >= size)
575c9fcb5dSZhang Rui 			return -EINVAL;
585c9fcb5dSZhang Rui 		len += count;
595c9fcb5dSZhang Rui 		size -= count;
605c9fcb5dSZhang Rui 	}
615c9fcb5dSZhang Rui 
6229b71a1cSThomas Renninger 	if (acpi_dev->flags.compatible_ids) {
6329b71a1cSThomas Renninger 		struct acpi_compatible_id_list *cid_list;
6429b71a1cSThomas Renninger 		int i;
6529b71a1cSThomas Renninger 
6629b71a1cSThomas Renninger 		cid_list = acpi_dev->pnp.cid_list;
6729b71a1cSThomas Renninger 		for (i = 0; i < cid_list->count; i++) {
6829b71a1cSThomas Renninger 			count = snprintf(&modalias[len], size, "%s:",
6929b71a1cSThomas Renninger 					 cid_list->id[i].value);
7029b71a1cSThomas Renninger 			if (count < 0 || count >= size) {
7187ecd5cdSLen Brown 				printk(KERN_ERR PREFIX "%s cid[%i] exceeds event buffer size",
7229b71a1cSThomas Renninger 				       acpi_dev->pnp.device_name, i);
7329b71a1cSThomas Renninger 				break;
7429b71a1cSThomas Renninger 			}
7529b71a1cSThomas Renninger 			len += count;
7629b71a1cSThomas Renninger 			size -= count;
7729b71a1cSThomas Renninger 		}
7829b71a1cSThomas Renninger 	}
7929b71a1cSThomas Renninger 
8029b71a1cSThomas Renninger 	modalias[len] = '\0';
8129b71a1cSThomas Renninger 	return len;
8229b71a1cSThomas Renninger }
8329b71a1cSThomas Renninger 
8429b71a1cSThomas Renninger static ssize_t
8529b71a1cSThomas Renninger acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
8629b71a1cSThomas Renninger 	struct acpi_device *acpi_dev = to_acpi_device(dev);
8729b71a1cSThomas Renninger 	int len;
8829b71a1cSThomas Renninger 
8929b71a1cSThomas Renninger 	/* Device has no HID and no CID or string is >1024 */
9029b71a1cSThomas Renninger 	len = create_modalias(acpi_dev, buf, 1024);
9129b71a1cSThomas Renninger 	if (len <= 0)
9229b71a1cSThomas Renninger 		return 0;
9329b71a1cSThomas Renninger 	buf[len++] = '\n';
9429b71a1cSThomas Renninger 	return len;
9529b71a1cSThomas Renninger }
9629b71a1cSThomas Renninger static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
9729b71a1cSThomas Renninger 
98c8d72a5eSZhang Rui static void acpi_bus_hot_remove_device(void *context)
991da177e4SLinus Torvalds {
10026d46867SZhang Rui 	struct acpi_device *device;
10126d46867SZhang Rui 	acpi_handle handle = context;
1021da177e4SLinus Torvalds 	struct acpi_object_list arg_list;
1031da177e4SLinus Torvalds 	union acpi_object arg;
1041da177e4SLinus Torvalds 	acpi_status status = AE_OK;
1051da177e4SLinus Torvalds 
10626d46867SZhang Rui 	if (acpi_bus_get_device(handle, &device))
107c8d72a5eSZhang Rui 		return;
1081da177e4SLinus Torvalds 
10926d46867SZhang Rui 	if (!device)
110c8d72a5eSZhang Rui 		return;
11126d46867SZhang Rui 
11226d46867SZhang Rui 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1130794469dSKay Sievers 		"Hot-removing device %s...\n", dev_name(&device->dev)));
11426d46867SZhang Rui 
11526d46867SZhang Rui 	if (acpi_bus_trim(device, 1)) {
11655ac9a01SLin Ming 		printk(KERN_ERR PREFIX
11755ac9a01SLin Ming 				"Removing device failed\n");
118c8d72a5eSZhang Rui 		return;
11926d46867SZhang Rui 	}
12026d46867SZhang Rui 
12126d46867SZhang Rui 	/* power off device */
12226d46867SZhang Rui 	status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
12326d46867SZhang Rui 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
12455ac9a01SLin Ming 		printk(KERN_WARNING PREFIX
12555ac9a01SLin Ming 				"Power-off device failed\n");
12626d46867SZhang Rui 
12726d46867SZhang Rui 	if (device->flags.lockable) {
1281da177e4SLinus Torvalds 		arg_list.count = 1;
1291da177e4SLinus Torvalds 		arg_list.pointer = &arg;
1301da177e4SLinus Torvalds 		arg.type = ACPI_TYPE_INTEGER;
1311da177e4SLinus Torvalds 		arg.integer.value = 0;
1321da177e4SLinus Torvalds 		acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
1331da177e4SLinus Torvalds 	}
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	arg_list.count = 1;
1361da177e4SLinus Torvalds 	arg_list.pointer = &arg;
1371da177e4SLinus Torvalds 	arg.type = ACPI_TYPE_INTEGER;
1381da177e4SLinus Torvalds 	arg.integer.value = 1;
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 	/*
1411da177e4SLinus Torvalds 	 * TBD: _EJD support.
1421da177e4SLinus Torvalds 	 */
1431da177e4SLinus Torvalds 	status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
14426d46867SZhang Rui 	if (ACPI_FAILURE(status))
145c8d72a5eSZhang Rui 		printk(KERN_WARNING PREFIX
146c8d72a5eSZhang Rui 				"Eject device failed\n");
1471da177e4SLinus Torvalds 
148c8d72a5eSZhang Rui 	return;
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds static ssize_t
152f883d9dbSPatrick Mochel acpi_eject_store(struct device *d, struct device_attribute *attr,
153f883d9dbSPatrick Mochel 		const char *buf, size_t count)
1541da177e4SLinus Torvalds {
1551da177e4SLinus Torvalds 	int ret = count;
1561da177e4SLinus Torvalds 	acpi_status status;
1571da177e4SLinus Torvalds 	acpi_object_type type = 0;
158f883d9dbSPatrick Mochel 	struct acpi_device *acpi_device = to_acpi_device(d);
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	if ((!count) || (buf[0] != '1')) {
1611da177e4SLinus Torvalds 		return -EINVAL;
1621da177e4SLinus Torvalds 	}
1631da177e4SLinus Torvalds #ifndef FORCE_EJECT
164f883d9dbSPatrick Mochel 	if (acpi_device->driver == NULL) {
1651da177e4SLinus Torvalds 		ret = -ENODEV;
1661da177e4SLinus Torvalds 		goto err;
1671da177e4SLinus Torvalds 	}
1681da177e4SLinus Torvalds #endif
169f883d9dbSPatrick Mochel 	status = acpi_get_type(acpi_device->handle, &type);
170f883d9dbSPatrick Mochel 	if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
1711da177e4SLinus Torvalds 		ret = -ENODEV;
1721da177e4SLinus Torvalds 		goto err;
1731da177e4SLinus Torvalds 	}
1741da177e4SLinus Torvalds 
175c8d72a5eSZhang Rui 	acpi_os_hotplug_execute(acpi_bus_hot_remove_device, acpi_device->handle);
1761da177e4SLinus Torvalds err:
1771da177e4SLinus Torvalds 	return ret;
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds 
180f883d9dbSPatrick Mochel static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
181f883d9dbSPatrick Mochel 
182e49bd2ddSZhang Rui static ssize_t
183e49bd2ddSZhang Rui acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
184e49bd2ddSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
185e49bd2ddSZhang Rui 
186e49bd2ddSZhang Rui 	return sprintf(buf, "%s\n", acpi_dev->pnp.hardware_id);
187e49bd2ddSZhang Rui }
188e49bd2ddSZhang Rui static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
189e49bd2ddSZhang Rui 
190e49bd2ddSZhang Rui static ssize_t
191e49bd2ddSZhang Rui acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
192e49bd2ddSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
193e49bd2ddSZhang Rui 	struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
194e49bd2ddSZhang Rui 	int result;
195e49bd2ddSZhang Rui 
196e49bd2ddSZhang Rui 	result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
197e49bd2ddSZhang Rui 	if (result)
198e49bd2ddSZhang Rui 		goto end;
199e49bd2ddSZhang Rui 
200e49bd2ddSZhang Rui 	result = sprintf(buf, "%s\n", (char*)path.pointer);
201e49bd2ddSZhang Rui 	kfree(path.pointer);
202e49bd2ddSZhang Rui end:
203e49bd2ddSZhang Rui 	return result;
204e49bd2ddSZhang Rui }
205e49bd2ddSZhang Rui static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
206e49bd2ddSZhang Rui 
207e49bd2ddSZhang Rui static int acpi_device_setup_files(struct acpi_device *dev)
208f883d9dbSPatrick Mochel {
209f883d9dbSPatrick Mochel 	acpi_status status;
210f883d9dbSPatrick Mochel 	acpi_handle temp;
211e49bd2ddSZhang Rui 	int result = 0;
212e49bd2ddSZhang Rui 
213e49bd2ddSZhang Rui 	/*
214e49bd2ddSZhang Rui 	 * Devices gotten from FADT don't have a "path" attribute
215e49bd2ddSZhang Rui 	 */
216e49bd2ddSZhang Rui 	if (dev->handle) {
217e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_path);
218e49bd2ddSZhang Rui 		if (result)
219e49bd2ddSZhang Rui 			goto end;
220e49bd2ddSZhang Rui 	}
221e49bd2ddSZhang Rui 
222e49bd2ddSZhang Rui 	if (dev->flags.hardware_id) {
223e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_hid);
224e49bd2ddSZhang Rui 		if (result)
225e49bd2ddSZhang Rui 			goto end;
226e49bd2ddSZhang Rui 	}
227f883d9dbSPatrick Mochel 
22829b71a1cSThomas Renninger 	if (dev->flags.hardware_id || dev->flags.compatible_ids) {
22929b71a1cSThomas Renninger 		result = device_create_file(&dev->dev, &dev_attr_modalias);
23029b71a1cSThomas Renninger 		if (result)
23129b71a1cSThomas Renninger 			goto end;
23229b71a1cSThomas Renninger 	}
23329b71a1cSThomas Renninger 
234f883d9dbSPatrick Mochel         /*
235f883d9dbSPatrick Mochel          * If device has _EJ0, 'eject' file is created that is used to trigger
236f883d9dbSPatrick Mochel          * hot-removal function from userland.
237f883d9dbSPatrick Mochel          */
238f883d9dbSPatrick Mochel 	status = acpi_get_handle(dev->handle, "_EJ0", &temp);
239f883d9dbSPatrick Mochel 	if (ACPI_SUCCESS(status))
240e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_eject);
241e49bd2ddSZhang Rui end:
242e49bd2ddSZhang Rui 	return result;
243f883d9dbSPatrick Mochel }
244f883d9dbSPatrick Mochel 
245f883d9dbSPatrick Mochel static void acpi_device_remove_files(struct acpi_device *dev)
246f883d9dbSPatrick Mochel {
247f883d9dbSPatrick Mochel 	acpi_status status;
248f883d9dbSPatrick Mochel 	acpi_handle temp;
249f883d9dbSPatrick Mochel 
250f883d9dbSPatrick Mochel 	/*
251f883d9dbSPatrick Mochel 	 * If device has _EJ0, 'eject' file is created that is used to trigger
252f883d9dbSPatrick Mochel 	 * hot-removal function from userland.
253f883d9dbSPatrick Mochel 	 */
254f883d9dbSPatrick Mochel 	status = acpi_get_handle(dev->handle, "_EJ0", &temp);
255f883d9dbSPatrick Mochel 	if (ACPI_SUCCESS(status))
256f883d9dbSPatrick Mochel 		device_remove_file(&dev->dev, &dev_attr_eject);
257e49bd2ddSZhang Rui 
25829b71a1cSThomas Renninger 	if (dev->flags.hardware_id || dev->flags.compatible_ids)
25929b71a1cSThomas Renninger 		device_remove_file(&dev->dev, &dev_attr_modalias);
26029b71a1cSThomas Renninger 
261e49bd2ddSZhang Rui 	if (dev->flags.hardware_id)
262e49bd2ddSZhang Rui 		device_remove_file(&dev->dev, &dev_attr_hid);
263e49bd2ddSZhang Rui 	if (dev->handle)
264e49bd2ddSZhang Rui 		device_remove_file(&dev->dev, &dev_attr_path);
265f883d9dbSPatrick Mochel }
2661da177e4SLinus Torvalds /* --------------------------------------------------------------------------
2679e89dde2SZhang Rui 			ACPI Bus operations
2681da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
26929b71a1cSThomas Renninger 
27029b71a1cSThomas Renninger int acpi_match_device_ids(struct acpi_device *device,
27129b71a1cSThomas Renninger 			  const struct acpi_device_id *ids)
27229b71a1cSThomas Renninger {
27329b71a1cSThomas Renninger 	const struct acpi_device_id *id;
27429b71a1cSThomas Renninger 
27539a0ad87SZhao Yakui 	/*
27639a0ad87SZhao Yakui 	 * If the device is not present, it is unnecessary to load device
27739a0ad87SZhao Yakui 	 * driver for it.
27839a0ad87SZhao Yakui 	 */
27939a0ad87SZhao Yakui 	if (!device->status.present)
28039a0ad87SZhao Yakui 		return -ENODEV;
28139a0ad87SZhao Yakui 
28229b71a1cSThomas Renninger 	if (device->flags.hardware_id) {
28329b71a1cSThomas Renninger 		for (id = ids; id->id[0]; id++) {
28429b71a1cSThomas Renninger 			if (!strcmp((char*)id->id, device->pnp.hardware_id))
28529b71a1cSThomas Renninger 				return 0;
28629b71a1cSThomas Renninger 		}
28729b71a1cSThomas Renninger 	}
28829b71a1cSThomas Renninger 
28929b71a1cSThomas Renninger 	if (device->flags.compatible_ids) {
29029b71a1cSThomas Renninger 		struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
29129b71a1cSThomas Renninger 		int i;
29229b71a1cSThomas Renninger 
29329b71a1cSThomas Renninger 		for (id = ids; id->id[0]; id++) {
29429b71a1cSThomas Renninger 			/* compare multiple _CID entries against driver ids */
29529b71a1cSThomas Renninger 			for (i = 0; i < cid_list->count; i++) {
29629b71a1cSThomas Renninger 				if (!strcmp((char*)id->id,
29729b71a1cSThomas Renninger 					    cid_list->id[i].value))
29829b71a1cSThomas Renninger 					return 0;
29929b71a1cSThomas Renninger 			}
30029b71a1cSThomas Renninger 		}
30129b71a1cSThomas Renninger 	}
30229b71a1cSThomas Renninger 
30329b71a1cSThomas Renninger 	return -ENOENT;
30429b71a1cSThomas Renninger }
30529b71a1cSThomas Renninger EXPORT_SYMBOL(acpi_match_device_ids);
30629b71a1cSThomas Renninger 
3071890a97aSPatrick Mochel static void acpi_device_release(struct device *dev)
3081890a97aSPatrick Mochel {
3091890a97aSPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3101890a97aSPatrick Mochel 
3111890a97aSPatrick Mochel 	kfree(acpi_dev->pnp.cid_list);
3121890a97aSPatrick Mochel 	kfree(acpi_dev);
3131890a97aSPatrick Mochel }
3141890a97aSPatrick Mochel 
3159e89dde2SZhang Rui static int acpi_device_suspend(struct device *dev, pm_message_t state)
3169e89dde2SZhang Rui {
3171d268b0aSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3185d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
3191da177e4SLinus Torvalds 
3205d9464a4SPatrick Mochel 	if (acpi_drv && acpi_drv->ops.suspend)
3215d9464a4SPatrick Mochel 		return acpi_drv->ops.suspend(acpi_dev, state);
3221da177e4SLinus Torvalds 	return 0;
3239e89dde2SZhang Rui }
3241da177e4SLinus Torvalds 
3259e89dde2SZhang Rui static int acpi_device_resume(struct device *dev)
3269e89dde2SZhang Rui {
3271d268b0aSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3285d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
3299e89dde2SZhang Rui 
3305d9464a4SPatrick Mochel 	if (acpi_drv && acpi_drv->ops.resume)
3315d9464a4SPatrick Mochel 		return acpi_drv->ops.resume(acpi_dev);
3329e89dde2SZhang Rui 	return 0;
3339e89dde2SZhang Rui }
3341da177e4SLinus Torvalds 
3355d9464a4SPatrick Mochel static int acpi_bus_match(struct device *dev, struct device_driver *drv)
3361da177e4SLinus Torvalds {
3375d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3385d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(drv);
3395d9464a4SPatrick Mochel 
34029b71a1cSThomas Renninger 	return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
3415d9464a4SPatrick Mochel }
3425d9464a4SPatrick Mochel 
3437eff2e7aSKay Sievers static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
3445d9464a4SPatrick Mochel {
3455d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3467eff2e7aSKay Sievers 	int len;
3475d9464a4SPatrick Mochel 
3487eff2e7aSKay Sievers 	if (add_uevent_var(env, "MODALIAS="))
3497eff2e7aSKay Sievers 		return -ENOMEM;
3507eff2e7aSKay Sievers 	len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
3517eff2e7aSKay Sievers 			      sizeof(env->buf) - env->buflen);
3527eff2e7aSKay Sievers 	if (len >= (sizeof(env->buf) - env->buflen))
3537eff2e7aSKay Sievers 		return -ENOMEM;
3547eff2e7aSKay Sievers 	env->buflen += len;
3551da177e4SLinus Torvalds 	return 0;
3561da177e4SLinus Torvalds }
3571da177e4SLinus Torvalds 
35846ec8598SBjorn Helgaas static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
35946ec8598SBjorn Helgaas {
36046ec8598SBjorn Helgaas 	struct acpi_device *device = data;
36146ec8598SBjorn Helgaas 
36246ec8598SBjorn Helgaas 	device->driver->ops.notify(device, event);
36346ec8598SBjorn Helgaas }
36446ec8598SBjorn Helgaas 
36546ec8598SBjorn Helgaas static acpi_status acpi_device_notify_fixed(void *data)
36646ec8598SBjorn Helgaas {
36746ec8598SBjorn Helgaas 	struct acpi_device *device = data;
36846ec8598SBjorn Helgaas 
36946ec8598SBjorn Helgaas 	acpi_device_notify(device->handle, ACPI_FIXED_HARDWARE_EVENT, device);
37046ec8598SBjorn Helgaas 	return AE_OK;
37146ec8598SBjorn Helgaas }
37246ec8598SBjorn Helgaas 
37346ec8598SBjorn Helgaas static int acpi_device_install_notify_handler(struct acpi_device *device)
37446ec8598SBjorn Helgaas {
37546ec8598SBjorn Helgaas 	acpi_status status;
37646ec8598SBjorn Helgaas 	char *hid;
37746ec8598SBjorn Helgaas 
37846ec8598SBjorn Helgaas 	hid = acpi_device_hid(device);
37946ec8598SBjorn Helgaas 	if (!strcmp(hid, ACPI_BUTTON_HID_POWERF))
38046ec8598SBjorn Helgaas 		status =
38146ec8598SBjorn Helgaas 		    acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
38246ec8598SBjorn Helgaas 						     acpi_device_notify_fixed,
38346ec8598SBjorn Helgaas 						     device);
38446ec8598SBjorn Helgaas 	else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEPF))
38546ec8598SBjorn Helgaas 		status =
38646ec8598SBjorn Helgaas 		    acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
38746ec8598SBjorn Helgaas 						     acpi_device_notify_fixed,
38846ec8598SBjorn Helgaas 						     device);
38946ec8598SBjorn Helgaas 	else
39046ec8598SBjorn Helgaas 		status = acpi_install_notify_handler(device->handle,
39146ec8598SBjorn Helgaas 						     ACPI_DEVICE_NOTIFY,
39246ec8598SBjorn Helgaas 						     acpi_device_notify,
39346ec8598SBjorn Helgaas 						     device);
39446ec8598SBjorn Helgaas 
39546ec8598SBjorn Helgaas 	if (ACPI_FAILURE(status))
39646ec8598SBjorn Helgaas 		return -EINVAL;
39746ec8598SBjorn Helgaas 	return 0;
39846ec8598SBjorn Helgaas }
39946ec8598SBjorn Helgaas 
40046ec8598SBjorn Helgaas static void acpi_device_remove_notify_handler(struct acpi_device *device)
40146ec8598SBjorn Helgaas {
40246ec8598SBjorn Helgaas 	if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF))
40346ec8598SBjorn Helgaas 		acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
40446ec8598SBjorn Helgaas 						acpi_device_notify_fixed);
40546ec8598SBjorn Helgaas 	else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF))
40646ec8598SBjorn Helgaas 		acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
40746ec8598SBjorn Helgaas 						acpi_device_notify_fixed);
40846ec8598SBjorn Helgaas 	else
40946ec8598SBjorn Helgaas 		acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
41046ec8598SBjorn Helgaas 					   acpi_device_notify);
41146ec8598SBjorn Helgaas }
41246ec8598SBjorn Helgaas 
4135d9464a4SPatrick Mochel static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
4145d9464a4SPatrick Mochel static int acpi_start_single_object(struct acpi_device *);
4155d9464a4SPatrick Mochel static int acpi_device_probe(struct device * dev)
4165d9464a4SPatrick Mochel {
4175d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
4185d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
4195d9464a4SPatrick Mochel 	int ret;
4205d9464a4SPatrick Mochel 
4215d9464a4SPatrick Mochel 	ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
4225d9464a4SPatrick Mochel 	if (!ret) {
423c4168bffSLi Shaohua 		if (acpi_dev->bus_ops.acpi_op_start)
4245d9464a4SPatrick Mochel 			acpi_start_single_object(acpi_dev);
42546ec8598SBjorn Helgaas 
42646ec8598SBjorn Helgaas 		if (acpi_drv->ops.notify) {
42746ec8598SBjorn Helgaas 			ret = acpi_device_install_notify_handler(acpi_dev);
42846ec8598SBjorn Helgaas 			if (ret) {
42946ec8598SBjorn Helgaas 				if (acpi_drv->ops.stop)
43046ec8598SBjorn Helgaas 					acpi_drv->ops.stop(acpi_dev,
43146ec8598SBjorn Helgaas 						   acpi_dev->removal_type);
43246ec8598SBjorn Helgaas 				if (acpi_drv->ops.remove)
43346ec8598SBjorn Helgaas 					acpi_drv->ops.remove(acpi_dev,
43446ec8598SBjorn Helgaas 						     acpi_dev->removal_type);
43546ec8598SBjorn Helgaas 				return ret;
43646ec8598SBjorn Helgaas 			}
43746ec8598SBjorn Helgaas 		}
43846ec8598SBjorn Helgaas 
4395d9464a4SPatrick Mochel 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4405d9464a4SPatrick Mochel 			"Found driver [%s] for device [%s]\n",
4415d9464a4SPatrick Mochel 			acpi_drv->name, acpi_dev->pnp.bus_id));
4425d9464a4SPatrick Mochel 		get_device(dev);
4435d9464a4SPatrick Mochel 	}
4445d9464a4SPatrick Mochel 	return ret;
4455d9464a4SPatrick Mochel }
4465d9464a4SPatrick Mochel 
4475d9464a4SPatrick Mochel static int acpi_device_remove(struct device * dev)
4485d9464a4SPatrick Mochel {
4495d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
4505d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
4515d9464a4SPatrick Mochel 
4525d9464a4SPatrick Mochel 	if (acpi_drv) {
45346ec8598SBjorn Helgaas 		if (acpi_drv->ops.notify)
45446ec8598SBjorn Helgaas 			acpi_device_remove_notify_handler(acpi_dev);
4555d9464a4SPatrick Mochel 		if (acpi_drv->ops.stop)
45696333578SLi Shaohua 			acpi_drv->ops.stop(acpi_dev, acpi_dev->removal_type);
4575d9464a4SPatrick Mochel 		if (acpi_drv->ops.remove)
45896333578SLi Shaohua 			acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
4595d9464a4SPatrick Mochel 	}
4605d9464a4SPatrick Mochel 	acpi_dev->driver = NULL;
461db89b4f0SPavel Machek 	acpi_dev->driver_data = NULL;
4625d9464a4SPatrick Mochel 
4635d9464a4SPatrick Mochel 	put_device(dev);
4645d9464a4SPatrick Mochel 	return 0;
4655d9464a4SPatrick Mochel }
4665d9464a4SPatrick Mochel 
46755955aadSDavid Brownell struct bus_type acpi_bus_type = {
4689e89dde2SZhang Rui 	.name		= "acpi",
4699e89dde2SZhang Rui 	.suspend	= acpi_device_suspend,
4709e89dde2SZhang Rui 	.resume		= acpi_device_resume,
4715d9464a4SPatrick Mochel 	.match		= acpi_bus_match,
4725d9464a4SPatrick Mochel 	.probe		= acpi_device_probe,
4735d9464a4SPatrick Mochel 	.remove		= acpi_device_remove,
4745d9464a4SPatrick Mochel 	.uevent		= acpi_device_uevent,
4751da177e4SLinus Torvalds };
4761da177e4SLinus Torvalds 
477e49bd2ddSZhang Rui static int acpi_device_register(struct acpi_device *device,
4781da177e4SLinus Torvalds 				 struct acpi_device *parent)
4791da177e4SLinus Torvalds {
480e49bd2ddSZhang Rui 	int result;
481e49bd2ddSZhang Rui 	struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
482e49bd2ddSZhang Rui 	int found = 0;
4831da177e4SLinus Torvalds 	/*
4841da177e4SLinus Torvalds 	 * Linkage
4851da177e4SLinus Torvalds 	 * -------
4861da177e4SLinus Torvalds 	 * Link this device to its parent and siblings.
4871da177e4SLinus Torvalds 	 */
4881da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->children);
4891da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->node);
4901da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->wakeup_list);
4911da177e4SLinus Torvalds 
492e49bd2ddSZhang Rui 	new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
493e49bd2ddSZhang Rui 	if (!new_bus_id) {
494e49bd2ddSZhang Rui 		printk(KERN_ERR PREFIX "Memory allocation error\n");
495e49bd2ddSZhang Rui 		return -ENOMEM;
496e49bd2ddSZhang Rui 	}
497e49bd2ddSZhang Rui 
4989090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
499e49bd2ddSZhang Rui 	/*
500e49bd2ddSZhang Rui 	 * Find suitable bus_id and instance number in acpi_bus_id_list
501e49bd2ddSZhang Rui 	 * If failed, create one and link it into acpi_bus_id_list
502e49bd2ddSZhang Rui 	 */
503e49bd2ddSZhang Rui 	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
504bb095854SZhang Rui 		if(!strcmp(acpi_device_bus_id->bus_id, device->flags.hardware_id? device->pnp.hardware_id : "device")) {
505e49bd2ddSZhang Rui 			acpi_device_bus_id->instance_no ++;
506e49bd2ddSZhang Rui 			found = 1;
507e49bd2ddSZhang Rui 			kfree(new_bus_id);
508e49bd2ddSZhang Rui 			break;
509e49bd2ddSZhang Rui 		}
510e49bd2ddSZhang Rui 	}
511e49bd2ddSZhang Rui 	if (!found) {
512e49bd2ddSZhang Rui 		acpi_device_bus_id = new_bus_id;
513bb095854SZhang Rui 		strcpy(acpi_device_bus_id->bus_id, device->flags.hardware_id ? device->pnp.hardware_id : "device");
514e49bd2ddSZhang Rui 		acpi_device_bus_id->instance_no = 0;
515e49bd2ddSZhang Rui 		list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
516e49bd2ddSZhang Rui 	}
5170794469dSKay Sievers 	dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
518e49bd2ddSZhang Rui 
51933b57150SLen Brown 	if (device->parent)
5201da177e4SLinus Torvalds 		list_add_tail(&device->node, &device->parent->children);
52133b57150SLen Brown 
5221da177e4SLinus Torvalds 	if (device->wakeup.flags.valid)
5231da177e4SLinus Torvalds 		list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
5249090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
5251da177e4SLinus Torvalds 
5261890a97aSPatrick Mochel 	if (device->parent)
5271890a97aSPatrick Mochel 		device->dev.parent = &parent->dev;
5281890a97aSPatrick Mochel 	device->dev.bus = &acpi_bus_type;
5291890a97aSPatrick Mochel 	device->dev.release = &acpi_device_release;
5308b12b922SAlex Chiang 	result = device_register(&device->dev);
531e49bd2ddSZhang Rui 	if (result) {
5328b12b922SAlex Chiang 		dev_err(&device->dev, "Error registering device\n");
533e49bd2ddSZhang Rui 		goto end;
534e49bd2ddSZhang Rui 	}
535f883d9dbSPatrick Mochel 
536e49bd2ddSZhang Rui 	result = acpi_device_setup_files(device);
537e49bd2ddSZhang Rui 	if (result)
5380794469dSKay Sievers 		printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
5390794469dSKay Sievers 		       dev_name(&device->dev));
540e49bd2ddSZhang Rui 
54196333578SLi Shaohua 	device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
542e49bd2ddSZhang Rui 	return 0;
543e49bd2ddSZhang Rui end:
5449090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
54533b57150SLen Brown 	if (device->parent)
546e49bd2ddSZhang Rui 		list_del(&device->node);
547e49bd2ddSZhang Rui 	list_del(&device->wakeup_list);
5489090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
549e49bd2ddSZhang Rui 	return result;
5501da177e4SLinus Torvalds }
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds static void acpi_device_unregister(struct acpi_device *device, int type)
5531da177e4SLinus Torvalds {
5549090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
55533b57150SLen Brown 	if (device->parent)
5561da177e4SLinus Torvalds 		list_del(&device->node);
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 	list_del(&device->wakeup_list);
5599090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 	acpi_detach_data(device->handle, acpi_bus_data_handler);
5621890a97aSPatrick Mochel 
563f883d9dbSPatrick Mochel 	acpi_device_remove_files(device);
5641890a97aSPatrick Mochel 	device_unregister(&device->dev);
5651da177e4SLinus Torvalds }
5661da177e4SLinus Torvalds 
5679e89dde2SZhang Rui /* --------------------------------------------------------------------------
5689e89dde2SZhang Rui                                  Driver Management
5699e89dde2SZhang Rui    -------------------------------------------------------------------------- */
5701da177e4SLinus Torvalds /**
571d758a8faSRandy Dunlap  * acpi_bus_driver_init - add a device to a driver
572d758a8faSRandy Dunlap  * @device: the device to add and initialize
573d758a8faSRandy Dunlap  * @driver: driver for the device
574d758a8faSRandy Dunlap  *
5751da177e4SLinus Torvalds  * Used to initialize a device via its device driver.  Called whenever a
5761890a97aSPatrick Mochel  * driver is bound to a device.  Invokes the driver's add() ops.
5771da177e4SLinus Torvalds  */
5781da177e4SLinus Torvalds static int
5794be44fcdSLen Brown acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
5801da177e4SLinus Torvalds {
5811da177e4SLinus Torvalds 	int result = 0;
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	if (!device || !driver)
584d550d98dSPatrick Mochel 		return -EINVAL;
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	if (!driver->ops.add)
587d550d98dSPatrick Mochel 		return -ENOSYS;
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 	result = driver->ops.add(device);
5901da177e4SLinus Torvalds 	if (result) {
5911da177e4SLinus Torvalds 		device->driver = NULL;
592db89b4f0SPavel Machek 		device->driver_data = NULL;
593d550d98dSPatrick Mochel 		return result;
5941da177e4SLinus Torvalds 	}
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	device->driver = driver;
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	/*
5991da177e4SLinus Torvalds 	 * TBD - Configuration Management: Assign resources to device based
6001da177e4SLinus Torvalds 	 * upon possible configuration and currently allocated resources.
6011da177e4SLinus Torvalds 	 */
6021da177e4SLinus Torvalds 
6034be44fcdSLen Brown 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
6044be44fcdSLen Brown 			  "Driver successfully bound to device\n"));
605d550d98dSPatrick Mochel 	return 0;
6063fb02738SRajesh Shah }
6073fb02738SRajesh Shah 
6088713cbefSAdrian Bunk static int acpi_start_single_object(struct acpi_device *device)
6093fb02738SRajesh Shah {
6103fb02738SRajesh Shah 	int result = 0;
6113fb02738SRajesh Shah 	struct acpi_driver *driver;
6123fb02738SRajesh Shah 
6133fb02738SRajesh Shah 
6143fb02738SRajesh Shah 	if (!(driver = device->driver))
615d550d98dSPatrick Mochel 		return 0;
6163fb02738SRajesh Shah 
6171da177e4SLinus Torvalds 	if (driver->ops.start) {
6181da177e4SLinus Torvalds 		result = driver->ops.start(device);
6191da177e4SLinus Torvalds 		if (result && driver->ops.remove)
6201da177e4SLinus Torvalds 			driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
6213fb02738SRajesh Shah 	}
6223fb02738SRajesh Shah 
623d550d98dSPatrick Mochel 	return result;
6241da177e4SLinus Torvalds }
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds /**
627d758a8faSRandy Dunlap  * acpi_bus_register_driver - register a driver with the ACPI bus
628d758a8faSRandy Dunlap  * @driver: driver being registered
629d758a8faSRandy Dunlap  *
6301da177e4SLinus Torvalds  * Registers a driver with the ACPI bus.  Searches the namespace for all
6319d9f749bSBjorn Helgaas  * devices that match the driver's criteria and binds.  Returns zero for
6329d9f749bSBjorn Helgaas  * success or a negative error status for failure.
6331da177e4SLinus Torvalds  */
6344be44fcdSLen Brown int acpi_bus_register_driver(struct acpi_driver *driver)
6351da177e4SLinus Torvalds {
6361890a97aSPatrick Mochel 	int ret;
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	if (acpi_disabled)
639d550d98dSPatrick Mochel 		return -ENODEV;
6401890a97aSPatrick Mochel 	driver->drv.name = driver->name;
6411890a97aSPatrick Mochel 	driver->drv.bus = &acpi_bus_type;
6421890a97aSPatrick Mochel 	driver->drv.owner = driver->owner;
6431da177e4SLinus Torvalds 
6441890a97aSPatrick Mochel 	ret = driver_register(&driver->drv);
6451890a97aSPatrick Mochel 	return ret;
6461da177e4SLinus Torvalds }
6471da177e4SLinus Torvalds 
6484be44fcdSLen Brown EXPORT_SYMBOL(acpi_bus_register_driver);
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds /**
651d758a8faSRandy Dunlap  * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
652d758a8faSRandy Dunlap  * @driver: driver to unregister
653d758a8faSRandy Dunlap  *
6541da177e4SLinus Torvalds  * Unregisters a driver with the ACPI bus.  Searches the namespace for all
6551da177e4SLinus Torvalds  * devices that match the driver's criteria and unbinds.
6561da177e4SLinus Torvalds  */
65706ea8e08SBjorn Helgaas void acpi_bus_unregister_driver(struct acpi_driver *driver)
6581da177e4SLinus Torvalds {
6591890a97aSPatrick Mochel 	driver_unregister(&driver->drv);
6601da177e4SLinus Torvalds }
6614be44fcdSLen Brown 
6621da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_unregister_driver);
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds /* --------------------------------------------------------------------------
6651da177e4SLinus Torvalds                                  Device Enumeration
6661da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
667c8f7a62cSLen Brown acpi_status
668c8f7a62cSLen Brown acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
669c8f7a62cSLen Brown {
670c8f7a62cSLen Brown 	acpi_status status;
671c8f7a62cSLen Brown 	acpi_handle tmp;
672c8f7a62cSLen Brown 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
673c8f7a62cSLen Brown 	union acpi_object *obj;
674c8f7a62cSLen Brown 
675c8f7a62cSLen Brown 	status = acpi_get_handle(handle, "_EJD", &tmp);
676c8f7a62cSLen Brown 	if (ACPI_FAILURE(status))
677c8f7a62cSLen Brown 		return status;
678c8f7a62cSLen Brown 
679c8f7a62cSLen Brown 	status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
680c8f7a62cSLen Brown 	if (ACPI_SUCCESS(status)) {
681c8f7a62cSLen Brown 		obj = buffer.pointer;
6823b5fee59SHolger Macht 		status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
6833b5fee59SHolger Macht 					 ejd);
684c8f7a62cSLen Brown 		kfree(buffer.pointer);
685c8f7a62cSLen Brown 	}
686c8f7a62cSLen Brown 	return status;
687c8f7a62cSLen Brown }
688c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
689c8f7a62cSLen Brown 
6901da177e4SLinus Torvalds void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
6911da177e4SLinus Torvalds {
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 	/* TBD */
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	return;
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds 
6989e89dde2SZhang Rui static int acpi_bus_get_perf_flags(struct acpi_device *device)
6999e89dde2SZhang Rui {
7009e89dde2SZhang Rui 	device->performance.state = ACPI_STATE_UNKNOWN;
7019e89dde2SZhang Rui 	return 0;
7029e89dde2SZhang Rui }
7039e89dde2SZhang Rui 
7041da177e4SLinus Torvalds static acpi_status
7051da177e4SLinus Torvalds acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
7061da177e4SLinus Torvalds 					     union acpi_object *package)
7071da177e4SLinus Torvalds {
7081da177e4SLinus Torvalds 	int i = 0;
7091da177e4SLinus Torvalds 	union acpi_object *element = NULL;
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 	if (!device || !package || (package->package.count < 2))
7121da177e4SLinus Torvalds 		return AE_BAD_PARAMETER;
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	element = &(package->package.elements[0]);
7151da177e4SLinus Torvalds 	if (!element)
7161da177e4SLinus Torvalds 		return AE_BAD_PARAMETER;
7171da177e4SLinus Torvalds 	if (element->type == ACPI_TYPE_PACKAGE) {
7181da177e4SLinus Torvalds 		if ((element->package.count < 2) ||
7191da177e4SLinus Torvalds 		    (element->package.elements[0].type !=
7201da177e4SLinus Torvalds 		     ACPI_TYPE_LOCAL_REFERENCE)
7211da177e4SLinus Torvalds 		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
7221da177e4SLinus Torvalds 			return AE_BAD_DATA;
7231da177e4SLinus Torvalds 		device->wakeup.gpe_device =
7241da177e4SLinus Torvalds 		    element->package.elements[0].reference.handle;
7251da177e4SLinus Torvalds 		device->wakeup.gpe_number =
7261da177e4SLinus Torvalds 		    (u32) element->package.elements[1].integer.value;
7271da177e4SLinus Torvalds 	} else if (element->type == ACPI_TYPE_INTEGER) {
7281da177e4SLinus Torvalds 		device->wakeup.gpe_number = element->integer.value;
7291da177e4SLinus Torvalds 	} else
7301da177e4SLinus Torvalds 		return AE_BAD_DATA;
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	element = &(package->package.elements[1]);
7331da177e4SLinus Torvalds 	if (element->type != ACPI_TYPE_INTEGER) {
7341da177e4SLinus Torvalds 		return AE_BAD_DATA;
7351da177e4SLinus Torvalds 	}
7361da177e4SLinus Torvalds 	device->wakeup.sleep_state = element->integer.value;
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 	if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
7391da177e4SLinus Torvalds 		return AE_NO_MEMORY;
7401da177e4SLinus Torvalds 	}
7411da177e4SLinus Torvalds 	device->wakeup.resources.count = package->package.count - 2;
7421da177e4SLinus Torvalds 	for (i = 0; i < device->wakeup.resources.count; i++) {
7431da177e4SLinus Torvalds 		element = &(package->package.elements[i + 2]);
744cd0b2248SBob Moore 		if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
7451da177e4SLinus Torvalds 			return AE_BAD_DATA;
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 		device->wakeup.resources.handles[i] = element->reference.handle;
7481da177e4SLinus Torvalds 	}
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 	return AE_OK;
7511da177e4SLinus Torvalds }
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
7541da177e4SLinus Torvalds {
7551da177e4SLinus Torvalds 	acpi_status status = 0;
7561da177e4SLinus Torvalds 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
7571da177e4SLinus Torvalds 	union acpi_object *package = NULL;
75877e76609SRafael J. Wysocki 	int psw_error;
7591da177e4SLinus Torvalds 
76029b71a1cSThomas Renninger 	struct acpi_device_id button_device_ids[] = {
76129b71a1cSThomas Renninger 		{"PNP0C0D", 0},
76229b71a1cSThomas Renninger 		{"PNP0C0C", 0},
76329b71a1cSThomas Renninger 		{"PNP0C0E", 0},
76429b71a1cSThomas Renninger 		{"", 0},
76529b71a1cSThomas Renninger 	};
76629b71a1cSThomas Renninger 
7671da177e4SLinus Torvalds 	/* _PRW */
7681da177e4SLinus Torvalds 	status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
7691da177e4SLinus Torvalds 	if (ACPI_FAILURE(status)) {
7701da177e4SLinus Torvalds 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
7711da177e4SLinus Torvalds 		goto end;
7721da177e4SLinus Torvalds 	}
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	package = (union acpi_object *)buffer.pointer;
7751da177e4SLinus Torvalds 	status = acpi_bus_extract_wakeup_device_power_package(device, package);
7761da177e4SLinus Torvalds 	if (ACPI_FAILURE(status)) {
7771da177e4SLinus Torvalds 		ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
7781da177e4SLinus Torvalds 		goto end;
7791da177e4SLinus Torvalds 	}
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	kfree(buffer.pointer);
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	device->wakeup.flags.valid = 1;
784729b2bdbSZhao Yakui 	/* Call _PSW/_DSW object to disable its ability to wake the sleeping
785729b2bdbSZhao Yakui 	 * system for the ACPI device with the _PRW object.
786729b2bdbSZhao Yakui 	 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
787729b2bdbSZhao Yakui 	 * So it is necessary to call _DSW object first. Only when it is not
788729b2bdbSZhao Yakui 	 * present will the _PSW object used.
789729b2bdbSZhao Yakui 	 */
79077e76609SRafael J. Wysocki 	psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
79177e76609SRafael J. Wysocki 	if (psw_error)
79277e76609SRafael J. Wysocki 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
79377e76609SRafael J. Wysocki 				"error in _DSW or _PSW evaluation\n"));
79477e76609SRafael J. Wysocki 
7951da177e4SLinus Torvalds 	/* Power button, Lid switch always enable wakeup */
79629b71a1cSThomas Renninger 	if (!acpi_match_device_ids(device, button_device_ids))
7971da177e4SLinus Torvalds 		device->wakeup.flags.run_wake = 1;
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds end:
8001da177e4SLinus Torvalds 	if (ACPI_FAILURE(status))
8011da177e4SLinus Torvalds 		device->flags.wake_capable = 0;
8021da177e4SLinus Torvalds 	return 0;
8031da177e4SLinus Torvalds }
8041da177e4SLinus Torvalds 
8059e89dde2SZhang Rui static int acpi_bus_get_power_flags(struct acpi_device *device)
8061da177e4SLinus Torvalds {
8079e89dde2SZhang Rui 	acpi_status status = 0;
8089e89dde2SZhang Rui 	acpi_handle handle = NULL;
8099e89dde2SZhang Rui 	u32 i = 0;
8109e89dde2SZhang Rui 
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 	/*
8139e89dde2SZhang Rui 	 * Power Management Flags
8141da177e4SLinus Torvalds 	 */
8159e89dde2SZhang Rui 	status = acpi_get_handle(device->handle, "_PSC", &handle);
8161da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
8179e89dde2SZhang Rui 		device->power.flags.explicit_get = 1;
8189e89dde2SZhang Rui 	status = acpi_get_handle(device->handle, "_IRC", &handle);
8199e89dde2SZhang Rui 	if (ACPI_SUCCESS(status))
8209e89dde2SZhang Rui 		device->power.flags.inrush_current = 1;
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds 	/*
8239e89dde2SZhang Rui 	 * Enumerate supported power management states
8241da177e4SLinus Torvalds 	 */
8259e89dde2SZhang Rui 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
8269e89dde2SZhang Rui 		struct acpi_device_power_state *ps = &device->power.states[i];
8279e89dde2SZhang Rui 		char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
8281da177e4SLinus Torvalds 
8299e89dde2SZhang Rui 		/* Evaluate "_PRx" to se if power resources are referenced */
8309e89dde2SZhang Rui 		acpi_evaluate_reference(device->handle, object_name, NULL,
8319e89dde2SZhang Rui 					&ps->resources);
8329e89dde2SZhang Rui 		if (ps->resources.count) {
8339e89dde2SZhang Rui 			device->power.flags.power_resources = 1;
8349e89dde2SZhang Rui 			ps->flags.valid = 1;
8351da177e4SLinus Torvalds 		}
8361da177e4SLinus Torvalds 
8379e89dde2SZhang Rui 		/* Evaluate "_PSx" to see if we can do explicit sets */
8389e89dde2SZhang Rui 		object_name[2] = 'S';
8399e89dde2SZhang Rui 		status = acpi_get_handle(device->handle, object_name, &handle);
840c8f7a62cSLen Brown 		if (ACPI_SUCCESS(status)) {
8419e89dde2SZhang Rui 			ps->flags.explicit_set = 1;
8429e89dde2SZhang Rui 			ps->flags.valid = 1;
843c8f7a62cSLen Brown 		}
844c8f7a62cSLen Brown 
8459e89dde2SZhang Rui 		/* State is valid if we have some power control */
8469e89dde2SZhang Rui 		if (ps->resources.count || ps->flags.explicit_set)
8479e89dde2SZhang Rui 			ps->flags.valid = 1;
8489e89dde2SZhang Rui 
8499e89dde2SZhang Rui 		ps->power = -1;	/* Unknown - driver assigned */
8509e89dde2SZhang Rui 		ps->latency = -1;	/* Unknown - driver assigned */
8519e89dde2SZhang Rui 	}
8529e89dde2SZhang Rui 
8539e89dde2SZhang Rui 	/* Set defaults for D0 and D3 states (always valid) */
8549e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].flags.valid = 1;
8559e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].power = 100;
8569e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D3].flags.valid = 1;
8579e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D3].power = 0;
8589e89dde2SZhang Rui 
8599e89dde2SZhang Rui 	/* TBD: System wake support and resource requirements. */
8609e89dde2SZhang Rui 
8619e89dde2SZhang Rui 	device->power.state = ACPI_STATE_UNKNOWN;
862a51e145fSZhao Yakui 	acpi_bus_get_power(device->handle, &(device->power.state));
8639e89dde2SZhang Rui 
8649e89dde2SZhang Rui 	return 0;
8659e89dde2SZhang Rui }
866c8f7a62cSLen Brown 
8674be44fcdSLen Brown static int acpi_bus_get_flags(struct acpi_device *device)
8681da177e4SLinus Torvalds {
8691da177e4SLinus Torvalds 	acpi_status status = AE_OK;
8701da177e4SLinus Torvalds 	acpi_handle temp = NULL;
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	/* Presence of _STA indicates 'dynamic_status' */
8741da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_STA", &temp);
8751da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
8761da177e4SLinus Torvalds 		device->flags.dynamic_status = 1;
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 	/* Presence of _CID indicates 'compatible_ids' */
8791da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_CID", &temp);
8801da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
8811da177e4SLinus Torvalds 		device->flags.compatible_ids = 1;
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	/* Presence of _RMV indicates 'removable' */
8841da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_RMV", &temp);
8851da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
8861da177e4SLinus Torvalds 		device->flags.removable = 1;
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
8891da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_EJD", &temp);
8901da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
8911da177e4SLinus Torvalds 		device->flags.ejectable = 1;
8921da177e4SLinus Torvalds 	else {
8931da177e4SLinus Torvalds 		status = acpi_get_handle(device->handle, "_EJ0", &temp);
8941da177e4SLinus Torvalds 		if (ACPI_SUCCESS(status))
8951da177e4SLinus Torvalds 			device->flags.ejectable = 1;
8961da177e4SLinus Torvalds 	}
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	/* Presence of _LCK indicates 'lockable' */
8991da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_LCK", &temp);
9001da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
9011da177e4SLinus Torvalds 		device->flags.lockable = 1;
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds 	/* Presence of _PS0|_PR0 indicates 'power manageable' */
9041da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_PS0", &temp);
9051da177e4SLinus Torvalds 	if (ACPI_FAILURE(status))
9061da177e4SLinus Torvalds 		status = acpi_get_handle(device->handle, "_PR0", &temp);
9071da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
9081da177e4SLinus Torvalds 		device->flags.power_manageable = 1;
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 	/* Presence of _PRW indicates wake capable */
9111da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_PRW", &temp);
9121da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
9131da177e4SLinus Torvalds 		device->flags.wake_capable = 1;
9141da177e4SLinus Torvalds 
9153c5f9be4SJoe Perches 	/* TBD: Performance management */
9161da177e4SLinus Torvalds 
917d550d98dSPatrick Mochel 	return 0;
9181da177e4SLinus Torvalds }
9191da177e4SLinus Torvalds 
9204be44fcdSLen Brown static void acpi_device_get_busid(struct acpi_device *device,
9214be44fcdSLen Brown 				  acpi_handle handle, int type)
9221da177e4SLinus Torvalds {
9231da177e4SLinus Torvalds 	char bus_id[5] = { '?', 0 };
9241da177e4SLinus Torvalds 	struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
9251da177e4SLinus Torvalds 	int i = 0;
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	/*
9281da177e4SLinus Torvalds 	 * Bus ID
9291da177e4SLinus Torvalds 	 * ------
9301da177e4SLinus Torvalds 	 * The device's Bus ID is simply the object name.
9311da177e4SLinus Torvalds 	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
9321da177e4SLinus Torvalds 	 */
9331da177e4SLinus Torvalds 	switch (type) {
9341da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SYSTEM:
9351da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "ACPI");
9361da177e4SLinus Torvalds 		break;
9371da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
9381da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "PWRF");
9391da177e4SLinus Torvalds 		break;
9401da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
9411da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "SLPF");
9421da177e4SLinus Torvalds 		break;
9431da177e4SLinus Torvalds 	default:
9441da177e4SLinus Torvalds 		acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
9451da177e4SLinus Torvalds 		/* Clean up trailing underscores (if any) */
9461da177e4SLinus Torvalds 		for (i = 3; i > 1; i--) {
9471da177e4SLinus Torvalds 			if (bus_id[i] == '_')
9481da177e4SLinus Torvalds 				bus_id[i] = '\0';
9491da177e4SLinus Torvalds 			else
9501da177e4SLinus Torvalds 				break;
9511da177e4SLinus Torvalds 		}
9521da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, bus_id);
9531da177e4SLinus Torvalds 		break;
9541da177e4SLinus Torvalds 	}
9551da177e4SLinus Torvalds }
9561da177e4SLinus Torvalds 
95754735266SZhang Rui /*
95854735266SZhang Rui  * acpi_bay_match - see if a device is an ejectable driver bay
95954735266SZhang Rui  *
96054735266SZhang Rui  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
96154735266SZhang Rui  * then we can safely call it an ejectable drive bay
96254735266SZhang Rui  */
96354735266SZhang Rui static int acpi_bay_match(struct acpi_device *device){
96454735266SZhang Rui 	acpi_status status;
96554735266SZhang Rui 	acpi_handle handle;
96654735266SZhang Rui 	acpi_handle tmp;
96754735266SZhang Rui 	acpi_handle phandle;
96854735266SZhang Rui 
96954735266SZhang Rui 	handle = device->handle;
97054735266SZhang Rui 
97154735266SZhang Rui 	status = acpi_get_handle(handle, "_EJ0", &tmp);
97254735266SZhang Rui 	if (ACPI_FAILURE(status))
97354735266SZhang Rui 		return -ENODEV;
97454735266SZhang Rui 
97554735266SZhang Rui 	if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
97654735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
97754735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
97854735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
97954735266SZhang Rui 		return 0;
98054735266SZhang Rui 
98154735266SZhang Rui 	if (acpi_get_parent(handle, &phandle))
98254735266SZhang Rui 		return -ENODEV;
98354735266SZhang Rui 
98454735266SZhang Rui         if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
98554735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
98654735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
98754735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
98854735266SZhang Rui                 return 0;
98954735266SZhang Rui 
99054735266SZhang Rui 	return -ENODEV;
99154735266SZhang Rui }
99254735266SZhang Rui 
9933620f2f2SFrank Seidel /*
9943620f2f2SFrank Seidel  * acpi_dock_match - see if a device has a _DCK method
9953620f2f2SFrank Seidel  */
9963620f2f2SFrank Seidel static int acpi_dock_match(struct acpi_device *device)
9973620f2f2SFrank Seidel {
9983620f2f2SFrank Seidel 	acpi_handle tmp;
9993620f2f2SFrank Seidel 	return acpi_get_handle(device->handle, "_DCK", &tmp);
10003620f2f2SFrank Seidel }
10013620f2f2SFrank Seidel 
10024be44fcdSLen Brown static void acpi_device_set_id(struct acpi_device *device,
10034be44fcdSLen Brown 			       struct acpi_device *parent, acpi_handle handle,
10044be44fcdSLen Brown 			       int type)
10051da177e4SLinus Torvalds {
10061da177e4SLinus Torvalds 	struct acpi_device_info *info;
10071da177e4SLinus Torvalds 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
10081da177e4SLinus Torvalds 	char *hid = NULL;
10091da177e4SLinus Torvalds 	char *uid = NULL;
10101da177e4SLinus Torvalds 	struct acpi_compatible_id_list *cid_list = NULL;
10113620f2f2SFrank Seidel 	const char *cid_add = NULL;
10121da177e4SLinus Torvalds 	acpi_status status;
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds 	switch (type) {
10151da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
10161da177e4SLinus Torvalds 		status = acpi_get_object_info(handle, &buffer);
10171da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
101896b2dd1fSHarvey Harrison 			printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
10191da177e4SLinus Torvalds 			return;
10201da177e4SLinus Torvalds 		}
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 		info = buffer.pointer;
10231da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_HID)
10241da177e4SLinus Torvalds 			hid = info->hardware_id.value;
10251da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_UID)
10261da177e4SLinus Torvalds 			uid = info->unique_id.value;
10271da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_CID)
10281da177e4SLinus Torvalds 			cid_list = &info->compatibility_id;
10291da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_ADR) {
10301da177e4SLinus Torvalds 			device->pnp.bus_address = info->address;
10311da177e4SLinus Torvalds 			device->flags.bus_address = 1;
10321da177e4SLinus Torvalds 		}
1033ae843332SZhang Rui 
10343620f2f2SFrank Seidel 		/* If we have a video/bay/dock device, add our selfdefined
10353620f2f2SFrank Seidel 		   HID to the CID list. Like that the video/bay/dock drivers
10363620f2f2SFrank Seidel 		   will get autoloaded and the device might still match
10373620f2f2SFrank Seidel 		   against another driver.
10383620f2f2SFrank Seidel 		*/
1039c3d6de69SThomas Renninger 		if (acpi_is_video_device(device))
10403620f2f2SFrank Seidel 			cid_add = ACPI_VIDEO_HID;
10413620f2f2SFrank Seidel 		else if (ACPI_SUCCESS(acpi_bay_match(device)))
10423620f2f2SFrank Seidel 			cid_add = ACPI_BAY_HID;
10433620f2f2SFrank Seidel 		else if (ACPI_SUCCESS(acpi_dock_match(device)))
10443620f2f2SFrank Seidel 			cid_add = ACPI_DOCK_HID;
104554735266SZhang Rui 
10461da177e4SLinus Torvalds 		break;
10471da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER:
10481da177e4SLinus Torvalds 		hid = ACPI_POWER_HID;
10491da177e4SLinus Torvalds 		break;
10501da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_PROCESSOR:
1051ad93a765SMyron Stowe 		hid = ACPI_PROCESSOR_OBJECT_HID;
10521da177e4SLinus Torvalds 		break;
10531da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SYSTEM:
10541da177e4SLinus Torvalds 		hid = ACPI_SYSTEM_HID;
10551da177e4SLinus Torvalds 		break;
10561da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_THERMAL:
10571da177e4SLinus Torvalds 		hid = ACPI_THERMAL_HID;
10581da177e4SLinus Torvalds 		break;
10591da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
10601da177e4SLinus Torvalds 		hid = ACPI_BUTTON_HID_POWERF;
10611da177e4SLinus Torvalds 		break;
10621da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
10631da177e4SLinus Torvalds 		hid = ACPI_BUTTON_HID_SLEEPF;
10641da177e4SLinus Torvalds 		break;
10651da177e4SLinus Torvalds 	}
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 	/*
10681da177e4SLinus Torvalds 	 * \_SB
10691da177e4SLinus Torvalds 	 * ----
10701da177e4SLinus Torvalds 	 * Fix for the system root bus device -- the only root-level device.
10711da177e4SLinus Torvalds 	 */
1072c51a4de8SBob Moore 	if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
10731da177e4SLinus Torvalds 		hid = ACPI_BUS_HID;
10741da177e4SLinus Torvalds 		strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
10751da177e4SLinus Torvalds 		strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
10761da177e4SLinus Torvalds 	}
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 	if (hid) {
10791da177e4SLinus Torvalds 		strcpy(device->pnp.hardware_id, hid);
10801da177e4SLinus Torvalds 		device->flags.hardware_id = 1;
10811da177e4SLinus Torvalds 	}
10821da177e4SLinus Torvalds 	if (uid) {
10831da177e4SLinus Torvalds 		strcpy(device->pnp.unique_id, uid);
10841da177e4SLinus Torvalds 		device->flags.unique_id = 1;
10851da177e4SLinus Torvalds 	}
10863620f2f2SFrank Seidel 	if (cid_list || cid_add) {
10873620f2f2SFrank Seidel 		struct  acpi_compatible_id_list *list;
10883620f2f2SFrank Seidel 		int size = 0;
10893620f2f2SFrank Seidel 		int count = 0;
10903620f2f2SFrank Seidel 
10911da177e4SLinus Torvalds 		if (cid_list) {
10923620f2f2SFrank Seidel 			size = cid_list->size;
10933620f2f2SFrank Seidel 		} else if (cid_add) {
10943620f2f2SFrank Seidel 			size = sizeof(struct acpi_compatible_id_list);
10953620f2f2SFrank Seidel 			cid_list = ACPI_ALLOCATE_ZEROED((acpi_size) size);
10963620f2f2SFrank Seidel 			if (!cid_list) {
10973620f2f2SFrank Seidel 				printk(KERN_ERR "Memory allocation error\n");
10983620f2f2SFrank Seidel 				kfree(buffer.pointer);
10993620f2f2SFrank Seidel 				return;
11003620f2f2SFrank Seidel 			} else {
11013620f2f2SFrank Seidel 				cid_list->count = 0;
11023620f2f2SFrank Seidel 				cid_list->size = size;
11033620f2f2SFrank Seidel 			}
11043620f2f2SFrank Seidel 		}
11053620f2f2SFrank Seidel 		if (cid_add)
11063620f2f2SFrank Seidel 			size += sizeof(struct acpi_compatible_id);
11073620f2f2SFrank Seidel 		list = kmalloc(size, GFP_KERNEL);
11083620f2f2SFrank Seidel 
11093620f2f2SFrank Seidel 		if (list) {
11103620f2f2SFrank Seidel 			if (cid_list) {
11113620f2f2SFrank Seidel 				memcpy(list, cid_list, cid_list->size);
11123620f2f2SFrank Seidel 				count = cid_list->count;
11133620f2f2SFrank Seidel 			}
11143620f2f2SFrank Seidel 			if (cid_add) {
11153620f2f2SFrank Seidel 				strncpy(list->id[count].value, cid_add,
11163620f2f2SFrank Seidel 					ACPI_MAX_CID_LENGTH);
11173620f2f2SFrank Seidel 				count++;
11183620f2f2SFrank Seidel 				device->flags.compatible_ids = 1;
11193620f2f2SFrank Seidel 			}
11203620f2f2SFrank Seidel 			list->size = size;
11213620f2f2SFrank Seidel 			list->count = count;
11223620f2f2SFrank Seidel 			device->pnp.cid_list = list;
11233620f2f2SFrank Seidel 		} else
112487ecd5cdSLen Brown 			printk(KERN_ERR PREFIX "Memory allocation error\n");
11251da177e4SLinus Torvalds 	}
11261da177e4SLinus Torvalds 
112702438d87SLen Brown 	kfree(buffer.pointer);
11281da177e4SLinus Torvalds }
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds static int acpi_device_set_context(struct acpi_device *device, int type)
11311da177e4SLinus Torvalds {
11321da177e4SLinus Torvalds 	acpi_status status = AE_OK;
11331da177e4SLinus Torvalds 	int result = 0;
11341da177e4SLinus Torvalds 	/*
11351da177e4SLinus Torvalds 	 * Context
11361da177e4SLinus Torvalds 	 * -------
11371da177e4SLinus Torvalds 	 * Attach this 'struct acpi_device' to the ACPI object.  This makes
11381da177e4SLinus Torvalds 	 * resolutions from handle->device very efficient.  Note that we need
11391da177e4SLinus Torvalds 	 * to be careful with fixed-feature devices as they all attach to the
11401da177e4SLinus Torvalds 	 * root object.
11411da177e4SLinus Torvalds 	 */
11421da177e4SLinus Torvalds 	if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
11431da177e4SLinus Torvalds 	    type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
11441da177e4SLinus Torvalds 		status = acpi_attach_data(device->handle,
11451da177e4SLinus Torvalds 					  acpi_bus_data_handler, device);
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
114887ecd5cdSLen Brown 			printk(KERN_ERR PREFIX "Error attaching device data\n");
11491da177e4SLinus Torvalds 			result = -ENODEV;
11501da177e4SLinus Torvalds 		}
11511da177e4SLinus Torvalds 	}
11521da177e4SLinus Torvalds 	return result;
11531da177e4SLinus Torvalds }
11541da177e4SLinus Torvalds 
11554be44fcdSLen Brown static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
11561da177e4SLinus Torvalds {
11571da177e4SLinus Torvalds 	if (!dev)
1158d550d98dSPatrick Mochel 		return -EINVAL;
11591da177e4SLinus Torvalds 
116096333578SLi Shaohua 	dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
11611890a97aSPatrick Mochel 	device_release_driver(&dev->dev);
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds 	if (!rmdevice)
1164d550d98dSPatrick Mochel 		return 0;
11651da177e4SLinus Torvalds 
11662786f6e3SRui Zhang 	/*
11672786f6e3SRui Zhang 	 * unbind _ADR-Based Devices when hot removal
11682786f6e3SRui Zhang 	 */
11691da177e4SLinus Torvalds 	if (dev->flags.bus_address) {
11701da177e4SLinus Torvalds 		if ((dev->parent) && (dev->parent->ops.unbind))
11711da177e4SLinus Torvalds 			dev->parent->ops.unbind(dev);
11721da177e4SLinus Torvalds 	}
11731da177e4SLinus Torvalds 	acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
11741da177e4SLinus Torvalds 
1175d550d98dSPatrick Mochel 	return 0;
11761da177e4SLinus Torvalds }
11771da177e4SLinus Torvalds 
11783fb02738SRajesh Shah static int
11794be44fcdSLen Brown acpi_add_single_object(struct acpi_device **child,
1180c4168bffSLi Shaohua 		       struct acpi_device *parent, acpi_handle handle, int type,
1181c4168bffSLi Shaohua 			struct acpi_bus_ops *ops)
11821da177e4SLinus Torvalds {
11831da177e4SLinus Torvalds 	int result = 0;
11841da177e4SLinus Torvalds 	struct acpi_device *device = NULL;
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds 
11871da177e4SLinus Torvalds 	if (!child)
1188d550d98dSPatrick Mochel 		return -EINVAL;
11891da177e4SLinus Torvalds 
119036bcbec7SBurman Yan 	device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
11911da177e4SLinus Torvalds 	if (!device) {
11926468463aSLen Brown 		printk(KERN_ERR PREFIX "Memory allocation error\n");
1193d550d98dSPatrick Mochel 		return -ENOMEM;
11941da177e4SLinus Torvalds 	}
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 	device->handle = handle;
11971da177e4SLinus Torvalds 	device->parent = parent;
1198c4168bffSLi Shaohua 	device->bus_ops = *ops; /* workround for not call .start */
1199c4168bffSLi Shaohua 
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds 	acpi_device_get_busid(device, handle, type);
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds 	/*
12041da177e4SLinus Torvalds 	 * Flags
12051da177e4SLinus Torvalds 	 * -----
12061da177e4SLinus Torvalds 	 * Get prior to calling acpi_bus_get_status() so we know whether
12071da177e4SLinus Torvalds 	 * or not _STA is present.  Note that we only look for object
12081da177e4SLinus Torvalds 	 * handles -- cannot evaluate objects until we know the device is
12091da177e4SLinus Torvalds 	 * present and properly initialized.
12101da177e4SLinus Torvalds 	 */
12111da177e4SLinus Torvalds 	result = acpi_bus_get_flags(device);
12121da177e4SLinus Torvalds 	if (result)
12131da177e4SLinus Torvalds 		goto end;
12141da177e4SLinus Torvalds 
12151da177e4SLinus Torvalds 	/*
12161da177e4SLinus Torvalds 	 * Status
12171da177e4SLinus Torvalds 	 * ------
12186940fabaSKeiichiro Tokunaga 	 * See if the device is present.  We always assume that non-Device
12196940fabaSKeiichiro Tokunaga 	 * and non-Processor objects (e.g. thermal zones, power resources,
12206940fabaSKeiichiro Tokunaga 	 * etc.) are present, functioning, etc. (at least when parent object
12216940fabaSKeiichiro Tokunaga 	 * is present).  Note that _STA has a different meaning for some
12226940fabaSKeiichiro Tokunaga 	 * objects (e.g. power resources) so we need to be careful how we use
12236940fabaSKeiichiro Tokunaga 	 * it.
12241da177e4SLinus Torvalds 	 */
12251da177e4SLinus Torvalds 	switch (type) {
12266940fabaSKeiichiro Tokunaga 	case ACPI_BUS_TYPE_PROCESSOR:
12271da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
12281da177e4SLinus Torvalds 		result = acpi_bus_get_status(device);
12293620f2f2SFrank Seidel 		if (ACPI_FAILURE(result)) {
12303620f2f2SFrank Seidel 			result = -ENODEV;
12311da177e4SLinus Torvalds 			goto end;
12321da177e4SLinus Torvalds 		}
123339a0ad87SZhao Yakui 		/*
123439a0ad87SZhao Yakui 		 * When the device is neither present nor functional, the
123539a0ad87SZhao Yakui 		 * device should not be added to Linux ACPI device tree.
123639a0ad87SZhao Yakui 		 * When the status of the device is not present but functinal,
123739a0ad87SZhao Yakui 		 * it should be added to Linux ACPI tree. For example : bay
123839a0ad87SZhao Yakui 		 * device , dock device.
123939a0ad87SZhao Yakui 		 * In such conditions it is unncessary to check whether it is
124039a0ad87SZhao Yakui 		 * bay device or dock device.
124139a0ad87SZhao Yakui 		 */
124239a0ad87SZhao Yakui 		if (!device->status.present && !device->status.functional) {
12433620f2f2SFrank Seidel 			result = -ENODEV;
12443620f2f2SFrank Seidel 			goto end;
12453620f2f2SFrank Seidel 		}
12461da177e4SLinus Torvalds 		break;
12471da177e4SLinus Torvalds 	default:
12480c0e8921SBjorn Helgaas 		STRUCT_TO_INT(device->status) =
12490c0e8921SBjorn Helgaas 		    ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
12500c0e8921SBjorn Helgaas 		    ACPI_STA_DEVICE_UI      | ACPI_STA_DEVICE_FUNCTIONING;
12511da177e4SLinus Torvalds 		break;
12521da177e4SLinus Torvalds 	}
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds 	/*
12551da177e4SLinus Torvalds 	 * Initialize Device
12561da177e4SLinus Torvalds 	 * -----------------
12571da177e4SLinus Torvalds 	 * TBD: Synch with Core's enumeration/initialization process.
12581da177e4SLinus Torvalds 	 */
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds 	/*
12611da177e4SLinus Torvalds 	 * Hardware ID, Unique ID, & Bus Address
12621da177e4SLinus Torvalds 	 * -------------------------------------
12631da177e4SLinus Torvalds 	 */
12641da177e4SLinus Torvalds 	acpi_device_set_id(device, parent, handle, type);
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 	/*
12671da177e4SLinus Torvalds 	 * Power Management
12681da177e4SLinus Torvalds 	 * ----------------
12691da177e4SLinus Torvalds 	 */
12701da177e4SLinus Torvalds 	if (device->flags.power_manageable) {
12711da177e4SLinus Torvalds 		result = acpi_bus_get_power_flags(device);
12721da177e4SLinus Torvalds 		if (result)
12731da177e4SLinus Torvalds 			goto end;
12741da177e4SLinus Torvalds 	}
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds 	/*
12771da177e4SLinus Torvalds 	 * Wakeup device management
12781da177e4SLinus Torvalds 	 *-----------------------
12791da177e4SLinus Torvalds 	 */
12801da177e4SLinus Torvalds 	if (device->flags.wake_capable) {
12811da177e4SLinus Torvalds 		result = acpi_bus_get_wakeup_device_flags(device);
12821da177e4SLinus Torvalds 		if (result)
12831da177e4SLinus Torvalds 			goto end;
12841da177e4SLinus Torvalds 	}
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 	/*
12871da177e4SLinus Torvalds 	 * Performance Management
12881da177e4SLinus Torvalds 	 * ----------------------
12891da177e4SLinus Torvalds 	 */
12901da177e4SLinus Torvalds 	if (device->flags.performance_manageable) {
12911da177e4SLinus Torvalds 		result = acpi_bus_get_perf_flags(device);
12921da177e4SLinus Torvalds 		if (result)
12931da177e4SLinus Torvalds 			goto end;
12941da177e4SLinus Torvalds 	}
12951da177e4SLinus Torvalds 
1296*f61f9258SLen Brown 	if ((result = acpi_device_set_context(device, type)))
1297*f61f9258SLen Brown 		goto end;
12981da177e4SLinus Torvalds 
1299e49bd2ddSZhang Rui 	result = acpi_device_register(device, parent);
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 	/*
13022786f6e3SRui Zhang 	 * Bind _ADR-Based Devices when hot add
13031da177e4SLinus Torvalds 	 */
13041da177e4SLinus Torvalds 	if (device->flags.bus_address) {
13051da177e4SLinus Torvalds 		if (device->parent && device->parent->ops.bind)
13061da177e4SLinus Torvalds 			device->parent->ops.bind(device);
13071da177e4SLinus Torvalds 	}
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds end:
13101da177e4SLinus Torvalds 	if (!result)
13111da177e4SLinus Torvalds 		*child = device;
13121da177e4SLinus Torvalds 	else {
13131da177e4SLinus Torvalds 		kfree(device->pnp.cid_list);
13141da177e4SLinus Torvalds 		kfree(device);
13151da177e4SLinus Torvalds 	}
13161da177e4SLinus Torvalds 
1317d550d98dSPatrick Mochel 	return result;
13181da177e4SLinus Torvalds }
13191da177e4SLinus Torvalds 
13204be44fcdSLen Brown static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
13211da177e4SLinus Torvalds {
13221da177e4SLinus Torvalds 	acpi_status status = AE_OK;
13231da177e4SLinus Torvalds 	struct acpi_device *parent = NULL;
13241da177e4SLinus Torvalds 	struct acpi_device *child = NULL;
13251da177e4SLinus Torvalds 	acpi_handle phandle = NULL;
13261da177e4SLinus Torvalds 	acpi_handle chandle = NULL;
13271da177e4SLinus Torvalds 	acpi_object_type type = 0;
13281da177e4SLinus Torvalds 	u32 level = 1;
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds 	if (!start)
1332d550d98dSPatrick Mochel 		return -EINVAL;
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds 	parent = start;
13351da177e4SLinus Torvalds 	phandle = start->handle;
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds 	/*
13381da177e4SLinus Torvalds 	 * Parse through the ACPI namespace, identify all 'devices', and
13391da177e4SLinus Torvalds 	 * create a new 'struct acpi_device' for each.
13401da177e4SLinus Torvalds 	 */
13411da177e4SLinus Torvalds 	while ((level > 0) && parent) {
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 		status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
13441da177e4SLinus Torvalds 					      chandle, &chandle);
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 		/*
13471da177e4SLinus Torvalds 		 * If this scope is exhausted then move our way back up.
13481da177e4SLinus Torvalds 		 */
13491da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
13501da177e4SLinus Torvalds 			level--;
13511da177e4SLinus Torvalds 			chandle = phandle;
13521da177e4SLinus Torvalds 			acpi_get_parent(phandle, &phandle);
13531da177e4SLinus Torvalds 			if (parent->parent)
13541da177e4SLinus Torvalds 				parent = parent->parent;
13551da177e4SLinus Torvalds 			continue;
13561da177e4SLinus Torvalds 		}
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds 		status = acpi_get_type(chandle, &type);
13591da177e4SLinus Torvalds 		if (ACPI_FAILURE(status))
13601da177e4SLinus Torvalds 			continue;
13611da177e4SLinus Torvalds 
13621da177e4SLinus Torvalds 		/*
13631da177e4SLinus Torvalds 		 * If this is a scope object then parse it (depth-first).
13641da177e4SLinus Torvalds 		 */
13651da177e4SLinus Torvalds 		if (type == ACPI_TYPE_LOCAL_SCOPE) {
13661da177e4SLinus Torvalds 			level++;
13671da177e4SLinus Torvalds 			phandle = chandle;
13681da177e4SLinus Torvalds 			chandle = NULL;
13691da177e4SLinus Torvalds 			continue;
13701da177e4SLinus Torvalds 		}
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds 		/*
13731da177e4SLinus Torvalds 		 * We're only interested in objects that we consider 'devices'.
13741da177e4SLinus Torvalds 		 */
13751da177e4SLinus Torvalds 		switch (type) {
13761da177e4SLinus Torvalds 		case ACPI_TYPE_DEVICE:
13771da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_DEVICE;
13781da177e4SLinus Torvalds 			break;
13791da177e4SLinus Torvalds 		case ACPI_TYPE_PROCESSOR:
13801da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_PROCESSOR;
13811da177e4SLinus Torvalds 			break;
13821da177e4SLinus Torvalds 		case ACPI_TYPE_THERMAL:
13831da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_THERMAL;
13841da177e4SLinus Torvalds 			break;
13851da177e4SLinus Torvalds 		case ACPI_TYPE_POWER:
13861da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_POWER;
13871da177e4SLinus Torvalds 			break;
13881da177e4SLinus Torvalds 		default:
13891da177e4SLinus Torvalds 			continue;
13901da177e4SLinus Torvalds 		}
13911da177e4SLinus Torvalds 
13923fb02738SRajesh Shah 		if (ops->acpi_op_add)
13933fb02738SRajesh Shah 			status = acpi_add_single_object(&child, parent,
1394c4168bffSLi Shaohua 				chandle, type, ops);
13953fb02738SRajesh Shah 		else
13963fb02738SRajesh Shah 			status = acpi_bus_get_device(chandle, &child);
13973fb02738SRajesh Shah 
13981da177e4SLinus Torvalds 		if (ACPI_FAILURE(status))
13991da177e4SLinus Torvalds 			continue;
14001da177e4SLinus Torvalds 
1401c4168bffSLi Shaohua 		if (ops->acpi_op_start && !(ops->acpi_op_add)) {
14023fb02738SRajesh Shah 			status = acpi_start_single_object(child);
14033fb02738SRajesh Shah 			if (ACPI_FAILURE(status))
14043fb02738SRajesh Shah 				continue;
14053fb02738SRajesh Shah 		}
14063fb02738SRajesh Shah 
14071da177e4SLinus Torvalds 		/*
14081da177e4SLinus Torvalds 		 * If the device is present, enabled, and functioning then
14091da177e4SLinus Torvalds 		 * parse its scope (depth-first).  Note that we need to
14101da177e4SLinus Torvalds 		 * represent absent devices to facilitate PnP notifications
14111da177e4SLinus Torvalds 		 * -- but only the subtree head (not all of its children,
14121da177e4SLinus Torvalds 		 * which will be enumerated when the parent is inserted).
14131da177e4SLinus Torvalds 		 *
14141da177e4SLinus Torvalds 		 * TBD: Need notifications and other detection mechanisms
14151da177e4SLinus Torvalds 		 *      in place before we can fully implement this.
14161da177e4SLinus Torvalds 		 */
141739a0ad87SZhao Yakui 		 /*
141839a0ad87SZhao Yakui 		 * When the device is not present but functional, it is also
141939a0ad87SZhao Yakui 		 * necessary to scan the children of this device.
142039a0ad87SZhao Yakui 		 */
142139a0ad87SZhao Yakui 		if (child->status.present || (!child->status.present &&
142239a0ad87SZhao Yakui 					child->status.functional)) {
14231da177e4SLinus Torvalds 			status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
14241da177e4SLinus Torvalds 						      NULL, NULL);
14251da177e4SLinus Torvalds 			if (ACPI_SUCCESS(status)) {
14261da177e4SLinus Torvalds 				level++;
14271da177e4SLinus Torvalds 				phandle = chandle;
14281da177e4SLinus Torvalds 				chandle = NULL;
14291da177e4SLinus Torvalds 				parent = child;
14301da177e4SLinus Torvalds 			}
14311da177e4SLinus Torvalds 		}
14321da177e4SLinus Torvalds 	}
14331da177e4SLinus Torvalds 
1434d550d98dSPatrick Mochel 	return 0;
14351da177e4SLinus Torvalds }
14361da177e4SLinus Torvalds 
14373fb02738SRajesh Shah int
14384be44fcdSLen Brown acpi_bus_add(struct acpi_device **child,
14394be44fcdSLen Brown 	     struct acpi_device *parent, acpi_handle handle, int type)
14403fb02738SRajesh Shah {
14413fb02738SRajesh Shah 	int result;
14423fb02738SRajesh Shah 	struct acpi_bus_ops ops;
14433fb02738SRajesh Shah 
14443fb02738SRajesh Shah 	memset(&ops, 0, sizeof(ops));
14453fb02738SRajesh Shah 	ops.acpi_op_add = 1;
1446c4168bffSLi Shaohua 
1447c4168bffSLi Shaohua 	result = acpi_add_single_object(child, parent, handle, type, &ops);
1448c4168bffSLi Shaohua 	if (!result)
14493fb02738SRajesh Shah 		result = acpi_bus_scan(*child, &ops);
1450c4168bffSLi Shaohua 
1451d550d98dSPatrick Mochel 	return result;
14523fb02738SRajesh Shah }
14533fb02738SRajesh Shah EXPORT_SYMBOL(acpi_bus_add);
14543fb02738SRajesh Shah 
14554be44fcdSLen Brown int acpi_bus_start(struct acpi_device *device)
14563fb02738SRajesh Shah {
14573fb02738SRajesh Shah 	int result;
14583fb02738SRajesh Shah 	struct acpi_bus_ops ops;
14593fb02738SRajesh Shah 
14603fb02738SRajesh Shah 
14613fb02738SRajesh Shah 	if (!device)
1462d550d98dSPatrick Mochel 		return -EINVAL;
14633fb02738SRajesh Shah 
14643fb02738SRajesh Shah 	result = acpi_start_single_object(device);
14653fb02738SRajesh Shah 	if (!result) {
14663fb02738SRajesh Shah 		memset(&ops, 0, sizeof(ops));
14673fb02738SRajesh Shah 		ops.acpi_op_start = 1;
14683fb02738SRajesh Shah 		result = acpi_bus_scan(device, &ops);
14693fb02738SRajesh Shah 	}
1470d550d98dSPatrick Mochel 	return result;
14713fb02738SRajesh Shah }
14723fb02738SRajesh Shah EXPORT_SYMBOL(acpi_bus_start);
14731da177e4SLinus Torvalds 
1474ceaba663SKristen Accardi int acpi_bus_trim(struct acpi_device *start, int rmdevice)
14751da177e4SLinus Torvalds {
14761da177e4SLinus Torvalds 	acpi_status status;
14771da177e4SLinus Torvalds 	struct acpi_device *parent, *child;
14781da177e4SLinus Torvalds 	acpi_handle phandle, chandle;
14791da177e4SLinus Torvalds 	acpi_object_type type;
14801da177e4SLinus Torvalds 	u32 level = 1;
14811da177e4SLinus Torvalds 	int err = 0;
14821da177e4SLinus Torvalds 
14831da177e4SLinus Torvalds 	parent = start;
14841da177e4SLinus Torvalds 	phandle = start->handle;
14851da177e4SLinus Torvalds 	child = chandle = NULL;
14861da177e4SLinus Torvalds 
14871da177e4SLinus Torvalds 	while ((level > 0) && parent && (!err)) {
14881da177e4SLinus Torvalds 		status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
14891da177e4SLinus Torvalds 					      chandle, &chandle);
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 		/*
14921da177e4SLinus Torvalds 		 * If this scope is exhausted then move our way back up.
14931da177e4SLinus Torvalds 		 */
14941da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
14951da177e4SLinus Torvalds 			level--;
14961da177e4SLinus Torvalds 			chandle = phandle;
14971da177e4SLinus Torvalds 			acpi_get_parent(phandle, &phandle);
14981da177e4SLinus Torvalds 			child = parent;
14991da177e4SLinus Torvalds 			parent = parent->parent;
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds 			if (level == 0)
15021da177e4SLinus Torvalds 				err = acpi_bus_remove(child, rmdevice);
15031da177e4SLinus Torvalds 			else
15041da177e4SLinus Torvalds 				err = acpi_bus_remove(child, 1);
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds 			continue;
15071da177e4SLinus Torvalds 		}
15081da177e4SLinus Torvalds 
15091da177e4SLinus Torvalds 		status = acpi_get_type(chandle, &type);
15101da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
15111da177e4SLinus Torvalds 			continue;
15121da177e4SLinus Torvalds 		}
15131da177e4SLinus Torvalds 		/*
15141da177e4SLinus Torvalds 		 * If there is a device corresponding to chandle then
15151da177e4SLinus Torvalds 		 * parse it (depth-first).
15161da177e4SLinus Torvalds 		 */
15171da177e4SLinus Torvalds 		if (acpi_bus_get_device(chandle, &child) == 0) {
15181da177e4SLinus Torvalds 			level++;
15191da177e4SLinus Torvalds 			phandle = chandle;
15201da177e4SLinus Torvalds 			chandle = NULL;
15211da177e4SLinus Torvalds 			parent = child;
15221da177e4SLinus Torvalds 		}
15231da177e4SLinus Torvalds 		continue;
15241da177e4SLinus Torvalds 	}
15251da177e4SLinus Torvalds 	return err;
15261da177e4SLinus Torvalds }
1527ceaba663SKristen Accardi EXPORT_SYMBOL_GPL(acpi_bus_trim);
1528ceaba663SKristen Accardi 
15294be44fcdSLen Brown static int acpi_bus_scan_fixed(struct acpi_device *root)
15301da177e4SLinus Torvalds {
15311da177e4SLinus Torvalds 	int result = 0;
15321da177e4SLinus Torvalds 	struct acpi_device *device = NULL;
1533c4168bffSLi Shaohua 	struct acpi_bus_ops ops;
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds 	if (!root)
1536d550d98dSPatrick Mochel 		return -ENODEV;
15371da177e4SLinus Torvalds 
1538c4168bffSLi Shaohua 	memset(&ops, 0, sizeof(ops));
1539c4168bffSLi Shaohua 	ops.acpi_op_add = 1;
1540c4168bffSLi Shaohua 	ops.acpi_op_start = 1;
1541c4168bffSLi Shaohua 
15421da177e4SLinus Torvalds 	/*
15431da177e4SLinus Torvalds 	 * Enumerate all fixed-feature devices.
15441da177e4SLinus Torvalds 	 */
1545cee324b1SAlexey Starikovskiy 	if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
15463fb02738SRajesh Shah 		result = acpi_add_single_object(&device, acpi_root,
15474be44fcdSLen Brown 						NULL,
1548c4168bffSLi Shaohua 						ACPI_BUS_TYPE_POWER_BUTTON,
1549c4168bffSLi Shaohua 						&ops);
15503fb02738SRajesh Shah 	}
15511da177e4SLinus Torvalds 
1552cee324b1SAlexey Starikovskiy 	if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
15533fb02738SRajesh Shah 		result = acpi_add_single_object(&device, acpi_root,
15544be44fcdSLen Brown 						NULL,
1555c4168bffSLi Shaohua 						ACPI_BUS_TYPE_SLEEP_BUTTON,
1556c4168bffSLi Shaohua 						&ops);
15573fb02738SRajesh Shah 	}
15581da177e4SLinus Torvalds 
1559d550d98dSPatrick Mochel 	return result;
15601da177e4SLinus Torvalds }
15611da177e4SLinus Torvalds 
1562e747f274SBjorn Helgaas int __init acpi_scan_init(void)
15631da177e4SLinus Torvalds {
15641da177e4SLinus Torvalds 	int result;
15653fb02738SRajesh Shah 	struct acpi_bus_ops ops;
15661da177e4SLinus Torvalds 
1567c4168bffSLi Shaohua 	memset(&ops, 0, sizeof(ops));
1568c4168bffSLi Shaohua 	ops.acpi_op_add = 1;
1569c4168bffSLi Shaohua 	ops.acpi_op_start = 1;
15701da177e4SLinus Torvalds 
15715b327265SPatrick Mochel 	result = bus_register(&acpi_bus_type);
15725b327265SPatrick Mochel 	if (result) {
15735b327265SPatrick Mochel 		/* We don't want to quit even if we failed to add suspend/resume */
15745b327265SPatrick Mochel 		printk(KERN_ERR PREFIX "Could not register bus type\n");
15755b327265SPatrick Mochel 	}
15765b327265SPatrick Mochel 
15771da177e4SLinus Torvalds 	/*
15781da177e4SLinus Torvalds 	 * Create the root device in the bus's device tree
15791da177e4SLinus Torvalds 	 */
15803fb02738SRajesh Shah 	result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
1581c4168bffSLi Shaohua 					ACPI_BUS_TYPE_SYSTEM, &ops);
15821da177e4SLinus Torvalds 	if (result)
15831da177e4SLinus Torvalds 		goto Done;
15841da177e4SLinus Torvalds 
15851da177e4SLinus Torvalds 	/*
15861da177e4SLinus Torvalds 	 * Enumerate devices in the ACPI namespace.
15871da177e4SLinus Torvalds 	 */
15881da177e4SLinus Torvalds 	result = acpi_bus_scan_fixed(acpi_root);
1589c04209a7SAlexey Starikovskiy 
1590c4168bffSLi Shaohua 	if (!result)
15913fb02738SRajesh Shah 		result = acpi_bus_scan(acpi_root, &ops);
15921da177e4SLinus Torvalds 
15931da177e4SLinus Torvalds 	if (result)
15941da177e4SLinus Torvalds 		acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds Done:
1597d550d98dSPatrick Mochel 	return result;
15981da177e4SLinus Torvalds }
1599