xref: /openbmc/linux/drivers/acpi/scan.c (revision bc3b07726aa288e2a5e60d9a1dd8188b3faa7385)
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) {
6315b8dd53SBob Moore 		struct acpica_device_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:",
6915b8dd53SBob Moore 					 cid_list->ids[i].string);
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) {
29015b8dd53SBob Moore 		struct acpica_device_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,
29715b8dd53SBob Moore 					    cid_list->ids[i].string))
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);
312718fb0deSHugh Dickins 	if (acpi_dev->flags.hardware_id)
313718fb0deSHugh Dickins 		kfree(acpi_dev->pnp.hardware_id);
314718fb0deSHugh Dickins 	if (acpi_dev->flags.unique_id)
315718fb0deSHugh Dickins 		kfree(acpi_dev->pnp.unique_id);
3161890a97aSPatrick Mochel 	kfree(acpi_dev);
3171890a97aSPatrick Mochel }
3181890a97aSPatrick Mochel 
3199e89dde2SZhang Rui static int acpi_device_suspend(struct device *dev, pm_message_t state)
3209e89dde2SZhang Rui {
3211d268b0aSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3225d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
3231da177e4SLinus Torvalds 
3245d9464a4SPatrick Mochel 	if (acpi_drv && acpi_drv->ops.suspend)
3255d9464a4SPatrick Mochel 		return acpi_drv->ops.suspend(acpi_dev, state);
3261da177e4SLinus Torvalds 	return 0;
3279e89dde2SZhang Rui }
3281da177e4SLinus Torvalds 
3299e89dde2SZhang Rui static int acpi_device_resume(struct device *dev)
3309e89dde2SZhang Rui {
3311d268b0aSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3325d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
3339e89dde2SZhang Rui 
3345d9464a4SPatrick Mochel 	if (acpi_drv && acpi_drv->ops.resume)
3355d9464a4SPatrick Mochel 		return acpi_drv->ops.resume(acpi_dev);
3369e89dde2SZhang Rui 	return 0;
3379e89dde2SZhang Rui }
3381da177e4SLinus Torvalds 
3395d9464a4SPatrick Mochel static int acpi_bus_match(struct device *dev, struct device_driver *drv)
3401da177e4SLinus Torvalds {
3415d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3425d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(drv);
3435d9464a4SPatrick Mochel 
34429b71a1cSThomas Renninger 	return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
3455d9464a4SPatrick Mochel }
3465d9464a4SPatrick Mochel 
3477eff2e7aSKay Sievers static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
3485d9464a4SPatrick Mochel {
3495d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3507eff2e7aSKay Sievers 	int len;
3515d9464a4SPatrick Mochel 
3527eff2e7aSKay Sievers 	if (add_uevent_var(env, "MODALIAS="))
3537eff2e7aSKay Sievers 		return -ENOMEM;
3547eff2e7aSKay Sievers 	len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
3557eff2e7aSKay Sievers 			      sizeof(env->buf) - env->buflen);
3567eff2e7aSKay Sievers 	if (len >= (sizeof(env->buf) - env->buflen))
3577eff2e7aSKay Sievers 		return -ENOMEM;
3587eff2e7aSKay Sievers 	env->buflen += len;
3591da177e4SLinus Torvalds 	return 0;
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds 
36246ec8598SBjorn Helgaas static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
36346ec8598SBjorn Helgaas {
36446ec8598SBjorn Helgaas 	struct acpi_device *device = data;
36546ec8598SBjorn Helgaas 
36646ec8598SBjorn Helgaas 	device->driver->ops.notify(device, event);
36746ec8598SBjorn Helgaas }
36846ec8598SBjorn Helgaas 
36946ec8598SBjorn Helgaas static acpi_status acpi_device_notify_fixed(void *data)
37046ec8598SBjorn Helgaas {
37146ec8598SBjorn Helgaas 	struct acpi_device *device = data;
37246ec8598SBjorn Helgaas 
37353de5356SBjorn Helgaas 	/* Fixed hardware devices have no handles */
37453de5356SBjorn Helgaas 	acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
37546ec8598SBjorn Helgaas 	return AE_OK;
37646ec8598SBjorn Helgaas }
37746ec8598SBjorn Helgaas 
37846ec8598SBjorn Helgaas static int acpi_device_install_notify_handler(struct acpi_device *device)
37946ec8598SBjorn Helgaas {
38046ec8598SBjorn Helgaas 	acpi_status status;
38146ec8598SBjorn Helgaas 
382ccba2a36SBjorn Helgaas 	if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
38346ec8598SBjorn Helgaas 		status =
38446ec8598SBjorn Helgaas 		    acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
38546ec8598SBjorn Helgaas 						     acpi_device_notify_fixed,
38646ec8598SBjorn Helgaas 						     device);
387ccba2a36SBjorn Helgaas 	else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
38846ec8598SBjorn Helgaas 		status =
38946ec8598SBjorn Helgaas 		    acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
39046ec8598SBjorn Helgaas 						     acpi_device_notify_fixed,
39146ec8598SBjorn Helgaas 						     device);
39246ec8598SBjorn Helgaas 	else
39346ec8598SBjorn Helgaas 		status = acpi_install_notify_handler(device->handle,
39446ec8598SBjorn Helgaas 						     ACPI_DEVICE_NOTIFY,
39546ec8598SBjorn Helgaas 						     acpi_device_notify,
39646ec8598SBjorn Helgaas 						     device);
39746ec8598SBjorn Helgaas 
39846ec8598SBjorn Helgaas 	if (ACPI_FAILURE(status))
39946ec8598SBjorn Helgaas 		return -EINVAL;
40046ec8598SBjorn Helgaas 	return 0;
40146ec8598SBjorn Helgaas }
40246ec8598SBjorn Helgaas 
40346ec8598SBjorn Helgaas static void acpi_device_remove_notify_handler(struct acpi_device *device)
40446ec8598SBjorn Helgaas {
405ccba2a36SBjorn Helgaas 	if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
40646ec8598SBjorn Helgaas 		acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
40746ec8598SBjorn Helgaas 						acpi_device_notify_fixed);
408ccba2a36SBjorn Helgaas 	else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
40946ec8598SBjorn Helgaas 		acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
41046ec8598SBjorn Helgaas 						acpi_device_notify_fixed);
41146ec8598SBjorn Helgaas 	else
41246ec8598SBjorn Helgaas 		acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
41346ec8598SBjorn Helgaas 					   acpi_device_notify);
41446ec8598SBjorn Helgaas }
41546ec8598SBjorn Helgaas 
4165d9464a4SPatrick Mochel static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
4175d9464a4SPatrick Mochel static int acpi_start_single_object(struct acpi_device *);
4185d9464a4SPatrick Mochel static int acpi_device_probe(struct device * dev)
4195d9464a4SPatrick Mochel {
4205d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
4215d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
4225d9464a4SPatrick Mochel 	int ret;
4235d9464a4SPatrick Mochel 
4245d9464a4SPatrick Mochel 	ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
4255d9464a4SPatrick Mochel 	if (!ret) {
426c4168bffSLi Shaohua 		if (acpi_dev->bus_ops.acpi_op_start)
4275d9464a4SPatrick Mochel 			acpi_start_single_object(acpi_dev);
42846ec8598SBjorn Helgaas 
42946ec8598SBjorn Helgaas 		if (acpi_drv->ops.notify) {
43046ec8598SBjorn Helgaas 			ret = acpi_device_install_notify_handler(acpi_dev);
43146ec8598SBjorn Helgaas 			if (ret) {
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.remove)
45696333578SLi Shaohua 			acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
4575d9464a4SPatrick Mochel 	}
4585d9464a4SPatrick Mochel 	acpi_dev->driver = NULL;
459db89b4f0SPavel Machek 	acpi_dev->driver_data = NULL;
4605d9464a4SPatrick Mochel 
4615d9464a4SPatrick Mochel 	put_device(dev);
4625d9464a4SPatrick Mochel 	return 0;
4635d9464a4SPatrick Mochel }
4645d9464a4SPatrick Mochel 
46555955aadSDavid Brownell struct bus_type acpi_bus_type = {
4669e89dde2SZhang Rui 	.name		= "acpi",
4679e89dde2SZhang Rui 	.suspend	= acpi_device_suspend,
4689e89dde2SZhang Rui 	.resume		= acpi_device_resume,
4695d9464a4SPatrick Mochel 	.match		= acpi_bus_match,
4705d9464a4SPatrick Mochel 	.probe		= acpi_device_probe,
4715d9464a4SPatrick Mochel 	.remove		= acpi_device_remove,
4725d9464a4SPatrick Mochel 	.uevent		= acpi_device_uevent,
4731da177e4SLinus Torvalds };
4741da177e4SLinus Torvalds 
47566b7ed40SBjorn Helgaas static int acpi_device_register(struct acpi_device *device)
4761da177e4SLinus Torvalds {
477e49bd2ddSZhang Rui 	int result;
478e49bd2ddSZhang Rui 	struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
479e49bd2ddSZhang Rui 	int found = 0;
48066b7ed40SBjorn Helgaas 
4811da177e4SLinus Torvalds 	/*
4821da177e4SLinus Torvalds 	 * Linkage
4831da177e4SLinus Torvalds 	 * -------
4841da177e4SLinus Torvalds 	 * Link this device to its parent and siblings.
4851da177e4SLinus Torvalds 	 */
4861da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->children);
4871da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->node);
4881da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->wakeup_list);
4891da177e4SLinus Torvalds 
490e49bd2ddSZhang Rui 	new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
491e49bd2ddSZhang Rui 	if (!new_bus_id) {
492e49bd2ddSZhang Rui 		printk(KERN_ERR PREFIX "Memory allocation error\n");
493e49bd2ddSZhang Rui 		return -ENOMEM;
494e49bd2ddSZhang Rui 	}
495e49bd2ddSZhang Rui 
4969090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
497e49bd2ddSZhang Rui 	/*
498e49bd2ddSZhang Rui 	 * Find suitable bus_id and instance number in acpi_bus_id_list
499e49bd2ddSZhang Rui 	 * If failed, create one and link it into acpi_bus_id_list
500e49bd2ddSZhang Rui 	 */
501e49bd2ddSZhang Rui 	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
502bb095854SZhang Rui 		if(!strcmp(acpi_device_bus_id->bus_id, device->flags.hardware_id? device->pnp.hardware_id : "device")) {
503e49bd2ddSZhang Rui 			acpi_device_bus_id->instance_no ++;
504e49bd2ddSZhang Rui 			found = 1;
505e49bd2ddSZhang Rui 			kfree(new_bus_id);
506e49bd2ddSZhang Rui 			break;
507e49bd2ddSZhang Rui 		}
508e49bd2ddSZhang Rui 	}
509e49bd2ddSZhang Rui 	if (!found) {
510e49bd2ddSZhang Rui 		acpi_device_bus_id = new_bus_id;
511bb095854SZhang Rui 		strcpy(acpi_device_bus_id->bus_id, device->flags.hardware_id ? device->pnp.hardware_id : "device");
512e49bd2ddSZhang Rui 		acpi_device_bus_id->instance_no = 0;
513e49bd2ddSZhang Rui 		list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
514e49bd2ddSZhang Rui 	}
5150794469dSKay Sievers 	dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
516e49bd2ddSZhang Rui 
51733b57150SLen Brown 	if (device->parent)
5181da177e4SLinus Torvalds 		list_add_tail(&device->node, &device->parent->children);
51933b57150SLen Brown 
5201da177e4SLinus Torvalds 	if (device->wakeup.flags.valid)
5211da177e4SLinus Torvalds 		list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
5229090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
5231da177e4SLinus Torvalds 
5241890a97aSPatrick Mochel 	if (device->parent)
52566b7ed40SBjorn Helgaas 		device->dev.parent = &device->parent->dev;
5261890a97aSPatrick Mochel 	device->dev.bus = &acpi_bus_type;
5271890a97aSPatrick Mochel 	device->dev.release = &acpi_device_release;
5288b12b922SAlex Chiang 	result = device_register(&device->dev);
529e49bd2ddSZhang Rui 	if (result) {
5308b12b922SAlex Chiang 		dev_err(&device->dev, "Error registering device\n");
531e49bd2ddSZhang Rui 		goto end;
532e49bd2ddSZhang Rui 	}
533f883d9dbSPatrick Mochel 
534e49bd2ddSZhang Rui 	result = acpi_device_setup_files(device);
535e49bd2ddSZhang Rui 	if (result)
5360794469dSKay Sievers 		printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
5370794469dSKay Sievers 		       dev_name(&device->dev));
538e49bd2ddSZhang Rui 
53996333578SLi Shaohua 	device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
540e49bd2ddSZhang Rui 	return 0;
541e49bd2ddSZhang Rui end:
5429090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
54333b57150SLen Brown 	if (device->parent)
544e49bd2ddSZhang Rui 		list_del(&device->node);
545e49bd2ddSZhang Rui 	list_del(&device->wakeup_list);
5469090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
547e49bd2ddSZhang Rui 	return result;
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds static void acpi_device_unregister(struct acpi_device *device, int type)
5511da177e4SLinus Torvalds {
5529090589dSShaohua Li 	mutex_lock(&acpi_device_lock);
55333b57150SLen Brown 	if (device->parent)
5541da177e4SLinus Torvalds 		list_del(&device->node);
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 	list_del(&device->wakeup_list);
5579090589dSShaohua Li 	mutex_unlock(&acpi_device_lock);
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	acpi_detach_data(device->handle, acpi_bus_data_handler);
5601890a97aSPatrick Mochel 
561f883d9dbSPatrick Mochel 	acpi_device_remove_files(device);
5621890a97aSPatrick Mochel 	device_unregister(&device->dev);
5631da177e4SLinus Torvalds }
5641da177e4SLinus Torvalds 
5659e89dde2SZhang Rui /* --------------------------------------------------------------------------
5669e89dde2SZhang Rui                                  Driver Management
5679e89dde2SZhang Rui    -------------------------------------------------------------------------- */
5681da177e4SLinus Torvalds /**
569d758a8faSRandy Dunlap  * acpi_bus_driver_init - add a device to a driver
570d758a8faSRandy Dunlap  * @device: the device to add and initialize
571d758a8faSRandy Dunlap  * @driver: driver for the device
572d758a8faSRandy Dunlap  *
5731da177e4SLinus Torvalds  * Used to initialize a device via its device driver.  Called whenever a
5741890a97aSPatrick Mochel  * driver is bound to a device.  Invokes the driver's add() ops.
5751da177e4SLinus Torvalds  */
5761da177e4SLinus Torvalds static int
5774be44fcdSLen Brown acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
5781da177e4SLinus Torvalds {
5791da177e4SLinus Torvalds 	int result = 0;
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	if (!device || !driver)
582d550d98dSPatrick Mochel 		return -EINVAL;
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	if (!driver->ops.add)
585d550d98dSPatrick Mochel 		return -ENOSYS;
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 	result = driver->ops.add(device);
5881da177e4SLinus Torvalds 	if (result) {
5891da177e4SLinus Torvalds 		device->driver = NULL;
590db89b4f0SPavel Machek 		device->driver_data = NULL;
591d550d98dSPatrick Mochel 		return result;
5921da177e4SLinus Torvalds 	}
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	device->driver = driver;
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	/*
5971da177e4SLinus Torvalds 	 * TBD - Configuration Management: Assign resources to device based
5981da177e4SLinus Torvalds 	 * upon possible configuration and currently allocated resources.
5991da177e4SLinus Torvalds 	 */
6001da177e4SLinus Torvalds 
6014be44fcdSLen Brown 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
6024be44fcdSLen Brown 			  "Driver successfully bound to device\n"));
603d550d98dSPatrick Mochel 	return 0;
6043fb02738SRajesh Shah }
6053fb02738SRajesh Shah 
6068713cbefSAdrian Bunk static int acpi_start_single_object(struct acpi_device *device)
6073fb02738SRajesh Shah {
6083fb02738SRajesh Shah 	int result = 0;
6093fb02738SRajesh Shah 	struct acpi_driver *driver;
6103fb02738SRajesh Shah 
6113fb02738SRajesh Shah 
6123fb02738SRajesh Shah 	if (!(driver = device->driver))
613d550d98dSPatrick Mochel 		return 0;
6143fb02738SRajesh Shah 
6151da177e4SLinus Torvalds 	if (driver->ops.start) {
6161da177e4SLinus Torvalds 		result = driver->ops.start(device);
6171da177e4SLinus Torvalds 		if (result && driver->ops.remove)
6181da177e4SLinus Torvalds 			driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
6193fb02738SRajesh Shah 	}
6203fb02738SRajesh Shah 
621d550d98dSPatrick Mochel 	return result;
6221da177e4SLinus Torvalds }
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds /**
625d758a8faSRandy Dunlap  * acpi_bus_register_driver - register a driver with the ACPI bus
626d758a8faSRandy Dunlap  * @driver: driver being registered
627d758a8faSRandy Dunlap  *
6281da177e4SLinus Torvalds  * Registers a driver with the ACPI bus.  Searches the namespace for all
6299d9f749bSBjorn Helgaas  * devices that match the driver's criteria and binds.  Returns zero for
6309d9f749bSBjorn Helgaas  * success or a negative error status for failure.
6311da177e4SLinus Torvalds  */
6324be44fcdSLen Brown int acpi_bus_register_driver(struct acpi_driver *driver)
6331da177e4SLinus Torvalds {
6341890a97aSPatrick Mochel 	int ret;
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 	if (acpi_disabled)
637d550d98dSPatrick Mochel 		return -ENODEV;
6381890a97aSPatrick Mochel 	driver->drv.name = driver->name;
6391890a97aSPatrick Mochel 	driver->drv.bus = &acpi_bus_type;
6401890a97aSPatrick Mochel 	driver->drv.owner = driver->owner;
6411da177e4SLinus Torvalds 
6421890a97aSPatrick Mochel 	ret = driver_register(&driver->drv);
6431890a97aSPatrick Mochel 	return ret;
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
6464be44fcdSLen Brown EXPORT_SYMBOL(acpi_bus_register_driver);
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds /**
649d758a8faSRandy Dunlap  * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
650d758a8faSRandy Dunlap  * @driver: driver to unregister
651d758a8faSRandy Dunlap  *
6521da177e4SLinus Torvalds  * Unregisters a driver with the ACPI bus.  Searches the namespace for all
6531da177e4SLinus Torvalds  * devices that match the driver's criteria and unbinds.
6541da177e4SLinus Torvalds  */
65506ea8e08SBjorn Helgaas void acpi_bus_unregister_driver(struct acpi_driver *driver)
6561da177e4SLinus Torvalds {
6571890a97aSPatrick Mochel 	driver_unregister(&driver->drv);
6581da177e4SLinus Torvalds }
6594be44fcdSLen Brown 
6601da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_unregister_driver);
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds /* --------------------------------------------------------------------------
6631da177e4SLinus Torvalds                                  Device Enumeration
6641da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
665c8f7a62cSLen Brown acpi_status
666c8f7a62cSLen Brown acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
667c8f7a62cSLen Brown {
668c8f7a62cSLen Brown 	acpi_status status;
669c8f7a62cSLen Brown 	acpi_handle tmp;
670c8f7a62cSLen Brown 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
671c8f7a62cSLen Brown 	union acpi_object *obj;
672c8f7a62cSLen Brown 
673c8f7a62cSLen Brown 	status = acpi_get_handle(handle, "_EJD", &tmp);
674c8f7a62cSLen Brown 	if (ACPI_FAILURE(status))
675c8f7a62cSLen Brown 		return status;
676c8f7a62cSLen Brown 
677c8f7a62cSLen Brown 	status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
678c8f7a62cSLen Brown 	if (ACPI_SUCCESS(status)) {
679c8f7a62cSLen Brown 		obj = buffer.pointer;
6803b5fee59SHolger Macht 		status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
6813b5fee59SHolger Macht 					 ejd);
682c8f7a62cSLen Brown 		kfree(buffer.pointer);
683c8f7a62cSLen Brown 	}
684c8f7a62cSLen Brown 	return status;
685c8f7a62cSLen Brown }
686c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
687c8f7a62cSLen Brown 
6888e4319c4SBob Moore void acpi_bus_data_handler(acpi_handle handle, void *context)
6891da177e4SLinus Torvalds {
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 	/* TBD */
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 	return;
6941da177e4SLinus Torvalds }
6951da177e4SLinus Torvalds 
6969e89dde2SZhang Rui static int acpi_bus_get_perf_flags(struct acpi_device *device)
6979e89dde2SZhang Rui {
6989e89dde2SZhang Rui 	device->performance.state = ACPI_STATE_UNKNOWN;
6999e89dde2SZhang Rui 	return 0;
7009e89dde2SZhang Rui }
7019e89dde2SZhang Rui 
7021da177e4SLinus Torvalds static acpi_status
7031da177e4SLinus Torvalds acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
7041da177e4SLinus Torvalds 					     union acpi_object *package)
7051da177e4SLinus Torvalds {
7061da177e4SLinus Torvalds 	int i = 0;
7071da177e4SLinus Torvalds 	union acpi_object *element = NULL;
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	if (!device || !package || (package->package.count < 2))
7101da177e4SLinus Torvalds 		return AE_BAD_PARAMETER;
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 	element = &(package->package.elements[0]);
7131da177e4SLinus Torvalds 	if (!element)
7141da177e4SLinus Torvalds 		return AE_BAD_PARAMETER;
7151da177e4SLinus Torvalds 	if (element->type == ACPI_TYPE_PACKAGE) {
7161da177e4SLinus Torvalds 		if ((element->package.count < 2) ||
7171da177e4SLinus Torvalds 		    (element->package.elements[0].type !=
7181da177e4SLinus Torvalds 		     ACPI_TYPE_LOCAL_REFERENCE)
7191da177e4SLinus Torvalds 		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
7201da177e4SLinus Torvalds 			return AE_BAD_DATA;
7211da177e4SLinus Torvalds 		device->wakeup.gpe_device =
7221da177e4SLinus Torvalds 		    element->package.elements[0].reference.handle;
7231da177e4SLinus Torvalds 		device->wakeup.gpe_number =
7241da177e4SLinus Torvalds 		    (u32) element->package.elements[1].integer.value;
7251da177e4SLinus Torvalds 	} else if (element->type == ACPI_TYPE_INTEGER) {
7261da177e4SLinus Torvalds 		device->wakeup.gpe_number = element->integer.value;
7271da177e4SLinus Torvalds 	} else
7281da177e4SLinus Torvalds 		return AE_BAD_DATA;
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 	element = &(package->package.elements[1]);
7311da177e4SLinus Torvalds 	if (element->type != ACPI_TYPE_INTEGER) {
7321da177e4SLinus Torvalds 		return AE_BAD_DATA;
7331da177e4SLinus Torvalds 	}
7341da177e4SLinus Torvalds 	device->wakeup.sleep_state = element->integer.value;
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 	if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
7371da177e4SLinus Torvalds 		return AE_NO_MEMORY;
7381da177e4SLinus Torvalds 	}
7391da177e4SLinus Torvalds 	device->wakeup.resources.count = package->package.count - 2;
7401da177e4SLinus Torvalds 	for (i = 0; i < device->wakeup.resources.count; i++) {
7411da177e4SLinus Torvalds 		element = &(package->package.elements[i + 2]);
742cd0b2248SBob Moore 		if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
7431da177e4SLinus Torvalds 			return AE_BAD_DATA;
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 		device->wakeup.resources.handles[i] = element->reference.handle;
7461da177e4SLinus Torvalds 	}
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	return AE_OK;
7491da177e4SLinus Torvalds }
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
7521da177e4SLinus Torvalds {
7531da177e4SLinus Torvalds 	acpi_status status = 0;
7541da177e4SLinus Torvalds 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
7551da177e4SLinus Torvalds 	union acpi_object *package = NULL;
75677e76609SRafael J. Wysocki 	int psw_error;
7571da177e4SLinus Torvalds 
75829b71a1cSThomas Renninger 	struct acpi_device_id button_device_ids[] = {
75929b71a1cSThomas Renninger 		{"PNP0C0D", 0},
76029b71a1cSThomas Renninger 		{"PNP0C0C", 0},
76129b71a1cSThomas Renninger 		{"PNP0C0E", 0},
76229b71a1cSThomas Renninger 		{"", 0},
76329b71a1cSThomas Renninger 	};
76429b71a1cSThomas Renninger 
7651da177e4SLinus Torvalds 	/* _PRW */
7661da177e4SLinus Torvalds 	status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
7671da177e4SLinus Torvalds 	if (ACPI_FAILURE(status)) {
7681da177e4SLinus Torvalds 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
7691da177e4SLinus Torvalds 		goto end;
7701da177e4SLinus Torvalds 	}
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 	package = (union acpi_object *)buffer.pointer;
7731da177e4SLinus Torvalds 	status = acpi_bus_extract_wakeup_device_power_package(device, package);
7741da177e4SLinus Torvalds 	if (ACPI_FAILURE(status)) {
7751da177e4SLinus Torvalds 		ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
7761da177e4SLinus Torvalds 		goto end;
7771da177e4SLinus Torvalds 	}
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 	kfree(buffer.pointer);
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	device->wakeup.flags.valid = 1;
7829b83ccd2SRafael J. Wysocki 	device->wakeup.prepare_count = 0;
783729b2bdbSZhao Yakui 	/* Call _PSW/_DSW object to disable its ability to wake the sleeping
784729b2bdbSZhao Yakui 	 * system for the ACPI device with the _PRW object.
785729b2bdbSZhao Yakui 	 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
786729b2bdbSZhao Yakui 	 * So it is necessary to call _DSW object first. Only when it is not
787729b2bdbSZhao Yakui 	 * present will the _PSW object used.
788729b2bdbSZhao Yakui 	 */
78977e76609SRafael J. Wysocki 	psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
79077e76609SRafael J. Wysocki 	if (psw_error)
79177e76609SRafael J. Wysocki 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
79277e76609SRafael J. Wysocki 				"error in _DSW or _PSW evaluation\n"));
79377e76609SRafael J. Wysocki 
7941da177e4SLinus Torvalds 	/* Power button, Lid switch always enable wakeup */
79529b71a1cSThomas Renninger 	if (!acpi_match_device_ids(device, button_device_ids))
7961da177e4SLinus Torvalds 		device->wakeup.flags.run_wake = 1;
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds end:
7991da177e4SLinus Torvalds 	if (ACPI_FAILURE(status))
8001da177e4SLinus Torvalds 		device->flags.wake_capable = 0;
8011da177e4SLinus Torvalds 	return 0;
8021da177e4SLinus Torvalds }
8031da177e4SLinus Torvalds 
8049e89dde2SZhang Rui static int acpi_bus_get_power_flags(struct acpi_device *device)
8051da177e4SLinus Torvalds {
8069e89dde2SZhang Rui 	acpi_status status = 0;
8079e89dde2SZhang Rui 	acpi_handle handle = NULL;
8089e89dde2SZhang Rui 	u32 i = 0;
8099e89dde2SZhang Rui 
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 	/*
8129e89dde2SZhang Rui 	 * Power Management Flags
8131da177e4SLinus Torvalds 	 */
8149e89dde2SZhang Rui 	status = acpi_get_handle(device->handle, "_PSC", &handle);
8151da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
8169e89dde2SZhang Rui 		device->power.flags.explicit_get = 1;
8179e89dde2SZhang Rui 	status = acpi_get_handle(device->handle, "_IRC", &handle);
8189e89dde2SZhang Rui 	if (ACPI_SUCCESS(status))
8199e89dde2SZhang Rui 		device->power.flags.inrush_current = 1;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	/*
8229e89dde2SZhang Rui 	 * Enumerate supported power management states
8231da177e4SLinus Torvalds 	 */
8249e89dde2SZhang Rui 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
8259e89dde2SZhang Rui 		struct acpi_device_power_state *ps = &device->power.states[i];
8269e89dde2SZhang Rui 		char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
8271da177e4SLinus Torvalds 
8289e89dde2SZhang Rui 		/* Evaluate "_PRx" to se if power resources are referenced */
8299e89dde2SZhang Rui 		acpi_evaluate_reference(device->handle, object_name, NULL,
8309e89dde2SZhang Rui 					&ps->resources);
8319e89dde2SZhang Rui 		if (ps->resources.count) {
8329e89dde2SZhang Rui 			device->power.flags.power_resources = 1;
8339e89dde2SZhang Rui 			ps->flags.valid = 1;
8341da177e4SLinus Torvalds 		}
8351da177e4SLinus Torvalds 
8369e89dde2SZhang Rui 		/* Evaluate "_PSx" to see if we can do explicit sets */
8379e89dde2SZhang Rui 		object_name[2] = 'S';
8389e89dde2SZhang Rui 		status = acpi_get_handle(device->handle, object_name, &handle);
839c8f7a62cSLen Brown 		if (ACPI_SUCCESS(status)) {
8409e89dde2SZhang Rui 			ps->flags.explicit_set = 1;
8419e89dde2SZhang Rui 			ps->flags.valid = 1;
842c8f7a62cSLen Brown 		}
843c8f7a62cSLen Brown 
8449e89dde2SZhang Rui 		/* State is valid if we have some power control */
8459e89dde2SZhang Rui 		if (ps->resources.count || ps->flags.explicit_set)
8469e89dde2SZhang Rui 			ps->flags.valid = 1;
8479e89dde2SZhang Rui 
8489e89dde2SZhang Rui 		ps->power = -1;	/* Unknown - driver assigned */
8499e89dde2SZhang Rui 		ps->latency = -1;	/* Unknown - driver assigned */
8509e89dde2SZhang Rui 	}
8519e89dde2SZhang Rui 
8529e89dde2SZhang Rui 	/* Set defaults for D0 and D3 states (always valid) */
8539e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].flags.valid = 1;
8549e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].power = 100;
8559e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D3].flags.valid = 1;
8569e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D3].power = 0;
8579e89dde2SZhang Rui 
8589e89dde2SZhang Rui 	/* TBD: System wake support and resource requirements. */
8599e89dde2SZhang Rui 
8609e89dde2SZhang Rui 	device->power.state = ACPI_STATE_UNKNOWN;
861a51e145fSZhao Yakui 	acpi_bus_get_power(device->handle, &(device->power.state));
8629e89dde2SZhang Rui 
8639e89dde2SZhang Rui 	return 0;
8649e89dde2SZhang Rui }
865c8f7a62cSLen Brown 
8664be44fcdSLen Brown static int acpi_bus_get_flags(struct acpi_device *device)
8671da177e4SLinus Torvalds {
8681da177e4SLinus Torvalds 	acpi_status status = AE_OK;
8691da177e4SLinus Torvalds 	acpi_handle temp = NULL;
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 	/* Presence of _STA indicates 'dynamic_status' */
8731da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_STA", &temp);
8741da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
8751da177e4SLinus Torvalds 		device->flags.dynamic_status = 1;
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	/* Presence of _CID indicates 'compatible_ids' */
8781da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_CID", &temp);
8791da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
8801da177e4SLinus Torvalds 		device->flags.compatible_ids = 1;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	/* Presence of _RMV indicates 'removable' */
8831da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_RMV", &temp);
8841da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
8851da177e4SLinus Torvalds 		device->flags.removable = 1;
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
8881da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_EJD", &temp);
8891da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
8901da177e4SLinus Torvalds 		device->flags.ejectable = 1;
8911da177e4SLinus Torvalds 	else {
8921da177e4SLinus Torvalds 		status = acpi_get_handle(device->handle, "_EJ0", &temp);
8931da177e4SLinus Torvalds 		if (ACPI_SUCCESS(status))
8941da177e4SLinus Torvalds 			device->flags.ejectable = 1;
8951da177e4SLinus Torvalds 	}
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 	/* Presence of _LCK indicates 'lockable' */
8981da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_LCK", &temp);
8991da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
9001da177e4SLinus Torvalds 		device->flags.lockable = 1;
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	/* Presence of _PS0|_PR0 indicates 'power manageable' */
9031da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_PS0", &temp);
9041da177e4SLinus Torvalds 	if (ACPI_FAILURE(status))
9051da177e4SLinus Torvalds 		status = acpi_get_handle(device->handle, "_PR0", &temp);
9061da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
9071da177e4SLinus Torvalds 		device->flags.power_manageable = 1;
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 	/* Presence of _PRW indicates wake capable */
9101da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_PRW", &temp);
9111da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
9121da177e4SLinus Torvalds 		device->flags.wake_capable = 1;
9131da177e4SLinus Torvalds 
9143c5f9be4SJoe Perches 	/* TBD: Performance management */
9151da177e4SLinus Torvalds 
916d550d98dSPatrick Mochel 	return 0;
9171da177e4SLinus Torvalds }
9181da177e4SLinus Torvalds 
91966b7ed40SBjorn Helgaas static void acpi_device_get_busid(struct acpi_device *device, int type)
9201da177e4SLinus Torvalds {
9211da177e4SLinus Torvalds 	char bus_id[5] = { '?', 0 };
9221da177e4SLinus Torvalds 	struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
9231da177e4SLinus Torvalds 	int i = 0;
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	/*
9261da177e4SLinus Torvalds 	 * Bus ID
9271da177e4SLinus Torvalds 	 * ------
9281da177e4SLinus Torvalds 	 * The device's Bus ID is simply the object name.
9291da177e4SLinus Torvalds 	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
9301da177e4SLinus Torvalds 	 */
9311da177e4SLinus Torvalds 	switch (type) {
9321da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SYSTEM:
9331da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "ACPI");
9341da177e4SLinus Torvalds 		break;
9351da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
9361da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "PWRF");
9371da177e4SLinus Torvalds 		break;
9381da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
9391da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "SLPF");
9401da177e4SLinus Torvalds 		break;
9411da177e4SLinus Torvalds 	default:
94266b7ed40SBjorn Helgaas 		acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
9431da177e4SLinus Torvalds 		/* Clean up trailing underscores (if any) */
9441da177e4SLinus Torvalds 		for (i = 3; i > 1; i--) {
9451da177e4SLinus Torvalds 			if (bus_id[i] == '_')
9461da177e4SLinus Torvalds 				bus_id[i] = '\0';
9471da177e4SLinus Torvalds 			else
9481da177e4SLinus Torvalds 				break;
9491da177e4SLinus Torvalds 		}
9501da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, bus_id);
9511da177e4SLinus Torvalds 		break;
9521da177e4SLinus Torvalds 	}
9531da177e4SLinus Torvalds }
9541da177e4SLinus Torvalds 
95554735266SZhang Rui /*
95654735266SZhang Rui  * acpi_bay_match - see if a device is an ejectable driver bay
95754735266SZhang Rui  *
95854735266SZhang Rui  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
95954735266SZhang Rui  * then we can safely call it an ejectable drive bay
96054735266SZhang Rui  */
96154735266SZhang Rui static int acpi_bay_match(struct acpi_device *device){
96254735266SZhang Rui 	acpi_status status;
96354735266SZhang Rui 	acpi_handle handle;
96454735266SZhang Rui 	acpi_handle tmp;
96554735266SZhang Rui 	acpi_handle phandle;
96654735266SZhang Rui 
96754735266SZhang Rui 	handle = device->handle;
96854735266SZhang Rui 
96954735266SZhang Rui 	status = acpi_get_handle(handle, "_EJ0", &tmp);
97054735266SZhang Rui 	if (ACPI_FAILURE(status))
97154735266SZhang Rui 		return -ENODEV;
97254735266SZhang Rui 
97354735266SZhang Rui 	if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
97454735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
97554735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
97654735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
97754735266SZhang Rui 		return 0;
97854735266SZhang Rui 
97954735266SZhang Rui 	if (acpi_get_parent(handle, &phandle))
98054735266SZhang Rui 		return -ENODEV;
98154735266SZhang Rui 
98254735266SZhang Rui         if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
98354735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
98454735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
98554735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
98654735266SZhang Rui                 return 0;
98754735266SZhang Rui 
98854735266SZhang Rui 	return -ENODEV;
98954735266SZhang Rui }
99054735266SZhang Rui 
9913620f2f2SFrank Seidel /*
9923620f2f2SFrank Seidel  * acpi_dock_match - see if a device has a _DCK method
9933620f2f2SFrank Seidel  */
9943620f2f2SFrank Seidel static int acpi_dock_match(struct acpi_device *device)
9953620f2f2SFrank Seidel {
9963620f2f2SFrank Seidel 	acpi_handle tmp;
9973620f2f2SFrank Seidel 	return acpi_get_handle(device->handle, "_DCK", &tmp);
9983620f2f2SFrank Seidel }
9993620f2f2SFrank Seidel 
100015b8dd53SBob Moore static struct acpica_device_id_list*
100115b8dd53SBob Moore acpi_add_cid(
100215b8dd53SBob Moore 	struct acpi_device_info         *info,
100315b8dd53SBob Moore 	struct acpica_device_id         *new_cid)
100415b8dd53SBob Moore {
100515b8dd53SBob Moore 	struct acpica_device_id_list    *cid;
100615b8dd53SBob Moore 	char                            *next_id_string;
100715b8dd53SBob Moore 	acpi_size                       cid_length;
100815b8dd53SBob Moore 	acpi_size                       new_cid_length;
100915b8dd53SBob Moore 	u32                             i;
101015b8dd53SBob Moore 
101115b8dd53SBob Moore 
101215b8dd53SBob Moore 	/* Allocate new CID list with room for the new CID */
101315b8dd53SBob Moore 
101415b8dd53SBob Moore 	if (!new_cid)
101515b8dd53SBob Moore 		new_cid_length = info->compatible_id_list.list_size;
101615b8dd53SBob Moore 	else if (info->compatible_id_list.list_size)
101715b8dd53SBob Moore 		new_cid_length = info->compatible_id_list.list_size +
101815b8dd53SBob Moore 			new_cid->length + sizeof(struct acpica_device_id);
101915b8dd53SBob Moore 	else
102015b8dd53SBob Moore 		new_cid_length = sizeof(struct acpica_device_id_list) + new_cid->length;
102115b8dd53SBob Moore 
102215b8dd53SBob Moore 	cid = ACPI_ALLOCATE_ZEROED(new_cid_length);
102315b8dd53SBob Moore 	if (!cid) {
102415b8dd53SBob Moore 		return NULL;
102515b8dd53SBob Moore 	}
102615b8dd53SBob Moore 
102715b8dd53SBob Moore 	cid->list_size = new_cid_length;
102815b8dd53SBob Moore 	cid->count = info->compatible_id_list.count;
102915b8dd53SBob Moore 	if (new_cid)
103015b8dd53SBob Moore 		cid->count++;
103115b8dd53SBob Moore 	next_id_string = (char *) cid->ids + (cid->count * sizeof(struct acpica_device_id));
103215b8dd53SBob Moore 
103315b8dd53SBob Moore 	/* Copy all existing CIDs */
103415b8dd53SBob Moore 
103515b8dd53SBob Moore 	for (i = 0; i < info->compatible_id_list.count; i++) {
103615b8dd53SBob Moore 		cid_length = info->compatible_id_list.ids[i].length;
103715b8dd53SBob Moore 		cid->ids[i].string = next_id_string;
103815b8dd53SBob Moore 		cid->ids[i].length = cid_length;
103915b8dd53SBob Moore 
104015b8dd53SBob Moore 		ACPI_MEMCPY(next_id_string, info->compatible_id_list.ids[i].string,
104115b8dd53SBob Moore 			cid_length);
104215b8dd53SBob Moore 
104315b8dd53SBob Moore 		next_id_string += cid_length;
104415b8dd53SBob Moore 	}
104515b8dd53SBob Moore 
104615b8dd53SBob Moore 	/* Append the new CID */
104715b8dd53SBob Moore 
104815b8dd53SBob Moore 	if (new_cid) {
104915b8dd53SBob Moore 		cid->ids[i].string = next_id_string;
105015b8dd53SBob Moore 		cid->ids[i].length = new_cid->length;
105115b8dd53SBob Moore 
105215b8dd53SBob Moore 		ACPI_MEMCPY(next_id_string, new_cid->string, new_cid->length);
105315b8dd53SBob Moore 	}
105415b8dd53SBob Moore 
105515b8dd53SBob Moore 	return cid;
105615b8dd53SBob Moore }
105715b8dd53SBob Moore 
105866b7ed40SBjorn Helgaas static void acpi_device_set_id(struct acpi_device *device, int type)
10591da177e4SLinus Torvalds {
106015b8dd53SBob Moore 	struct acpi_device_info *info = NULL;
10611da177e4SLinus Torvalds 	char *hid = NULL;
10621da177e4SLinus Torvalds 	char *uid = NULL;
106315b8dd53SBob Moore 	struct acpica_device_id_list *cid_list = NULL;
106415b8dd53SBob Moore 	char *cid_add = NULL;
10651da177e4SLinus Torvalds 	acpi_status status;
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 	switch (type) {
10681da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
106966b7ed40SBjorn Helgaas 		status = acpi_get_object_info(device->handle, &info);
10701da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
107196b2dd1fSHarvey Harrison 			printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
10721da177e4SLinus Torvalds 			return;
10731da177e4SLinus Torvalds 		}
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_HID)
107615b8dd53SBob Moore 			hid = info->hardware_id.string;
10771da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_UID)
107815b8dd53SBob Moore 			uid = info->unique_id.string;
10791da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_CID)
108015b8dd53SBob Moore 			cid_list = &info->compatible_id_list;
10811da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_ADR) {
10821da177e4SLinus Torvalds 			device->pnp.bus_address = info->address;
10831da177e4SLinus Torvalds 			device->flags.bus_address = 1;
10841da177e4SLinus Torvalds 		}
1085ae843332SZhang Rui 
10863620f2f2SFrank Seidel 		/* If we have a video/bay/dock device, add our selfdefined
10873620f2f2SFrank Seidel 		   HID to the CID list. Like that the video/bay/dock drivers
10883620f2f2SFrank Seidel 		   will get autoloaded and the device might still match
10893620f2f2SFrank Seidel 		   against another driver.
10903620f2f2SFrank Seidel 		*/
1091c3d6de69SThomas Renninger 		if (acpi_is_video_device(device))
10923620f2f2SFrank Seidel 			cid_add = ACPI_VIDEO_HID;
10933620f2f2SFrank Seidel 		else if (ACPI_SUCCESS(acpi_bay_match(device)))
10943620f2f2SFrank Seidel 			cid_add = ACPI_BAY_HID;
10953620f2f2SFrank Seidel 		else if (ACPI_SUCCESS(acpi_dock_match(device)))
10963620f2f2SFrank Seidel 			cid_add = ACPI_DOCK_HID;
109754735266SZhang Rui 
10981da177e4SLinus Torvalds 		break;
10991da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER:
11001da177e4SLinus Torvalds 		hid = ACPI_POWER_HID;
11011da177e4SLinus Torvalds 		break;
11021da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_PROCESSOR:
1103ad93a765SMyron Stowe 		hid = ACPI_PROCESSOR_OBJECT_HID;
11041da177e4SLinus Torvalds 		break;
11051da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SYSTEM:
11061da177e4SLinus Torvalds 		hid = ACPI_SYSTEM_HID;
11071da177e4SLinus Torvalds 		break;
11081da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_THERMAL:
11091da177e4SLinus Torvalds 		hid = ACPI_THERMAL_HID;
11101da177e4SLinus Torvalds 		break;
11111da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
11121da177e4SLinus Torvalds 		hid = ACPI_BUTTON_HID_POWERF;
11131da177e4SLinus Torvalds 		break;
11141da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
11151da177e4SLinus Torvalds 		hid = ACPI_BUTTON_HID_SLEEPF;
11161da177e4SLinus Torvalds 		break;
11171da177e4SLinus Torvalds 	}
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 	/*
11201da177e4SLinus Torvalds 	 * \_SB
11211da177e4SLinus Torvalds 	 * ----
11221da177e4SLinus Torvalds 	 * Fix for the system root bus device -- the only root-level device.
11231da177e4SLinus Torvalds 	 */
112466b7ed40SBjorn Helgaas 	if (((acpi_handle)device->parent == ACPI_ROOT_OBJECT) &&
112566b7ed40SBjorn Helgaas 	     (type == ACPI_BUS_TYPE_DEVICE)) {
11261da177e4SLinus Torvalds 		hid = ACPI_BUS_HID;
11271da177e4SLinus Torvalds 		strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
11281da177e4SLinus Torvalds 		strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
11291da177e4SLinus Torvalds 	}
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 	if (hid) {
113215b8dd53SBob Moore 		device->pnp.hardware_id = ACPI_ALLOCATE_ZEROED(strlen (hid) + 1);
113315b8dd53SBob Moore 		if (device->pnp.hardware_id) {
11341da177e4SLinus Torvalds 			strcpy(device->pnp.hardware_id, hid);
11351da177e4SLinus Torvalds 			device->flags.hardware_id = 1;
11361da177e4SLinus Torvalds 		}
1137718fb0deSHugh Dickins 	}
1138718fb0deSHugh Dickins 	if (!device->flags.hardware_id)
1139718fb0deSHugh Dickins 		device->pnp.hardware_id = "";
114015b8dd53SBob Moore 
11411da177e4SLinus Torvalds 	if (uid) {
114215b8dd53SBob Moore 		device->pnp.unique_id = ACPI_ALLOCATE_ZEROED(strlen (uid) + 1);
114315b8dd53SBob Moore 		if (device->pnp.unique_id) {
11441da177e4SLinus Torvalds 			strcpy(device->pnp.unique_id, uid);
11451da177e4SLinus Torvalds 			device->flags.unique_id = 1;
11461da177e4SLinus Torvalds 		}
1147718fb0deSHugh Dickins 	}
1148718fb0deSHugh Dickins 	if (!device->flags.unique_id)
1149718fb0deSHugh Dickins 		device->pnp.unique_id = "";
11503620f2f2SFrank Seidel 
115115b8dd53SBob Moore 	if (cid_list || cid_add) {
115215b8dd53SBob Moore 		struct acpica_device_id_list *list;
115315b8dd53SBob Moore 
115415b8dd53SBob Moore 		if (cid_add) {
115515b8dd53SBob Moore 			struct acpica_device_id cid;
115615b8dd53SBob Moore 			cid.length = strlen (cid_add) + 1;
115715b8dd53SBob Moore 			cid.string = cid_add;
115815b8dd53SBob Moore 
115915b8dd53SBob Moore 			list = acpi_add_cid(info, &cid);
11603620f2f2SFrank Seidel 		} else {
116115b8dd53SBob Moore 			list = acpi_add_cid(info, NULL);
11623620f2f2SFrank Seidel 		}
11633620f2f2SFrank Seidel 
11643620f2f2SFrank Seidel 		if (list) {
116515b8dd53SBob Moore 			device->pnp.cid_list = list;
116615b8dd53SBob Moore 			if (cid_add)
11673620f2f2SFrank Seidel 				device->flags.compatible_ids = 1;
11683620f2f2SFrank Seidel 		}
11691da177e4SLinus Torvalds 	}
11701da177e4SLinus Torvalds 
117115b8dd53SBob Moore 	kfree(info);
11721da177e4SLinus Torvalds }
11731da177e4SLinus Torvalds 
1174*bc3b0772SBjorn Helgaas static int acpi_device_set_context(struct acpi_device *device)
11751da177e4SLinus Torvalds {
1176*bc3b0772SBjorn Helgaas 	acpi_status status;
1177*bc3b0772SBjorn Helgaas 
11781da177e4SLinus Torvalds 	/*
11791da177e4SLinus Torvalds 	 * Context
11801da177e4SLinus Torvalds 	 * -------
11811da177e4SLinus Torvalds 	 * Attach this 'struct acpi_device' to the ACPI object.  This makes
1182*bc3b0772SBjorn Helgaas 	 * resolutions from handle->device very efficient.  Fixed hardware
1183*bc3b0772SBjorn Helgaas 	 * devices have no handles, so we skip them.
11841da177e4SLinus Torvalds 	 */
1185*bc3b0772SBjorn Helgaas 	if (!device->handle)
1186*bc3b0772SBjorn Helgaas 		return 0;
1187*bc3b0772SBjorn Helgaas 
11881da177e4SLinus Torvalds 	status = acpi_attach_data(device->handle,
11891da177e4SLinus Torvalds 				  acpi_bus_data_handler, device);
1190*bc3b0772SBjorn Helgaas 	if (ACPI_SUCCESS(status))
1191*bc3b0772SBjorn Helgaas 		return 0;
11921da177e4SLinus Torvalds 
119387ecd5cdSLen Brown 	printk(KERN_ERR PREFIX "Error attaching device data\n");
1194*bc3b0772SBjorn Helgaas 	return -ENODEV;
11951da177e4SLinus Torvalds }
11961da177e4SLinus Torvalds 
11974be44fcdSLen Brown static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
11981da177e4SLinus Torvalds {
11991da177e4SLinus Torvalds 	if (!dev)
1200d550d98dSPatrick Mochel 		return -EINVAL;
12011da177e4SLinus Torvalds 
120296333578SLi Shaohua 	dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
12031890a97aSPatrick Mochel 	device_release_driver(&dev->dev);
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds 	if (!rmdevice)
1206d550d98dSPatrick Mochel 		return 0;
12071da177e4SLinus Torvalds 
12082786f6e3SRui Zhang 	/*
12092786f6e3SRui Zhang 	 * unbind _ADR-Based Devices when hot removal
12102786f6e3SRui Zhang 	 */
12111da177e4SLinus Torvalds 	if (dev->flags.bus_address) {
12121da177e4SLinus Torvalds 		if ((dev->parent) && (dev->parent->ops.unbind))
12131da177e4SLinus Torvalds 			dev->parent->ops.unbind(dev);
12141da177e4SLinus Torvalds 	}
12151da177e4SLinus Torvalds 	acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
12161da177e4SLinus Torvalds 
1217d550d98dSPatrick Mochel 	return 0;
12181da177e4SLinus Torvalds }
12191da177e4SLinus Torvalds 
12203fb02738SRajesh Shah static int
12214be44fcdSLen Brown acpi_add_single_object(struct acpi_device **child,
1222c4168bffSLi Shaohua 		       struct acpi_device *parent, acpi_handle handle, int type,
1223c4168bffSLi Shaohua 			struct acpi_bus_ops *ops)
12241da177e4SLinus Torvalds {
12251da177e4SLinus Torvalds 	int result = 0;
12261da177e4SLinus Torvalds 	struct acpi_device *device = NULL;
122729aaefa6SBjorn Helgaas 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 	if (!child)
1231d550d98dSPatrick Mochel 		return -EINVAL;
12321da177e4SLinus Torvalds 
123336bcbec7SBurman Yan 	device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
12341da177e4SLinus Torvalds 	if (!device) {
12356468463aSLen Brown 		printk(KERN_ERR PREFIX "Memory allocation error\n");
1236d550d98dSPatrick Mochel 		return -ENOMEM;
12371da177e4SLinus Torvalds 	}
12381da177e4SLinus Torvalds 
1239caaa6efbSBjorn Helgaas 	device->device_type = type;
12401da177e4SLinus Torvalds 	device->handle = handle;
12411da177e4SLinus Torvalds 	device->parent = parent;
1242c4168bffSLi Shaohua 	device->bus_ops = *ops; /* workround for not call .start */
1243c4168bffSLi Shaohua 
124466b7ed40SBjorn Helgaas 	acpi_device_get_busid(device, type);
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 	/*
12471da177e4SLinus Torvalds 	 * Flags
12481da177e4SLinus Torvalds 	 * -----
12491da177e4SLinus Torvalds 	 * Get prior to calling acpi_bus_get_status() so we know whether
12501da177e4SLinus Torvalds 	 * or not _STA is present.  Note that we only look for object
12511da177e4SLinus Torvalds 	 * handles -- cannot evaluate objects until we know the device is
12521da177e4SLinus Torvalds 	 * present and properly initialized.
12531da177e4SLinus Torvalds 	 */
12541da177e4SLinus Torvalds 	result = acpi_bus_get_flags(device);
12551da177e4SLinus Torvalds 	if (result)
12561da177e4SLinus Torvalds 		goto end;
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 	/*
12591da177e4SLinus Torvalds 	 * Status
12601da177e4SLinus Torvalds 	 * ------
12616940fabaSKeiichiro Tokunaga 	 * See if the device is present.  We always assume that non-Device
12626940fabaSKeiichiro Tokunaga 	 * and non-Processor objects (e.g. thermal zones, power resources,
12636940fabaSKeiichiro Tokunaga 	 * etc.) are present, functioning, etc. (at least when parent object
12646940fabaSKeiichiro Tokunaga 	 * is present).  Note that _STA has a different meaning for some
12656940fabaSKeiichiro Tokunaga 	 * objects (e.g. power resources) so we need to be careful how we use
12666940fabaSKeiichiro Tokunaga 	 * it.
12671da177e4SLinus Torvalds 	 */
12681da177e4SLinus Torvalds 	switch (type) {
12696940fabaSKeiichiro Tokunaga 	case ACPI_BUS_TYPE_PROCESSOR:
12701da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
12711da177e4SLinus Torvalds 		result = acpi_bus_get_status(device);
12723620f2f2SFrank Seidel 		if (ACPI_FAILURE(result)) {
12733620f2f2SFrank Seidel 			result = -ENODEV;
12741da177e4SLinus Torvalds 			goto end;
12751da177e4SLinus Torvalds 		}
127639a0ad87SZhao Yakui 		/*
127739a0ad87SZhao Yakui 		 * When the device is neither present nor functional, the
127839a0ad87SZhao Yakui 		 * device should not be added to Linux ACPI device tree.
127939a0ad87SZhao Yakui 		 * When the status of the device is not present but functinal,
128039a0ad87SZhao Yakui 		 * it should be added to Linux ACPI tree. For example : bay
128139a0ad87SZhao Yakui 		 * device , dock device.
128239a0ad87SZhao Yakui 		 * In such conditions it is unncessary to check whether it is
128339a0ad87SZhao Yakui 		 * bay device or dock device.
128439a0ad87SZhao Yakui 		 */
128539a0ad87SZhao Yakui 		if (!device->status.present && !device->status.functional) {
12863620f2f2SFrank Seidel 			result = -ENODEV;
12873620f2f2SFrank Seidel 			goto end;
12883620f2f2SFrank Seidel 		}
12891da177e4SLinus Torvalds 		break;
12901da177e4SLinus Torvalds 	default:
12910c0e8921SBjorn Helgaas 		STRUCT_TO_INT(device->status) =
12920c0e8921SBjorn Helgaas 		    ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
12930c0e8921SBjorn Helgaas 		    ACPI_STA_DEVICE_UI      | ACPI_STA_DEVICE_FUNCTIONING;
12941da177e4SLinus Torvalds 		break;
12951da177e4SLinus Torvalds 	}
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds 	/*
12981da177e4SLinus Torvalds 	 * Initialize Device
12991da177e4SLinus Torvalds 	 * -----------------
13001da177e4SLinus Torvalds 	 * TBD: Synch with Core's enumeration/initialization process.
13011da177e4SLinus Torvalds 	 */
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds 	/*
13041da177e4SLinus Torvalds 	 * Hardware ID, Unique ID, & Bus Address
13051da177e4SLinus Torvalds 	 * -------------------------------------
13061da177e4SLinus Torvalds 	 */
130766b7ed40SBjorn Helgaas 	acpi_device_set_id(device, type);
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds 	/*
13101da177e4SLinus Torvalds 	 * Power Management
13111da177e4SLinus Torvalds 	 * ----------------
13121da177e4SLinus Torvalds 	 */
13131da177e4SLinus Torvalds 	if (device->flags.power_manageable) {
13141da177e4SLinus Torvalds 		result = acpi_bus_get_power_flags(device);
13151da177e4SLinus Torvalds 		if (result)
13161da177e4SLinus Torvalds 			goto end;
13171da177e4SLinus Torvalds 	}
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds 	/*
13201da177e4SLinus Torvalds 	 * Wakeup device management
13211da177e4SLinus Torvalds 	 *-----------------------
13221da177e4SLinus Torvalds 	 */
13231da177e4SLinus Torvalds 	if (device->flags.wake_capable) {
13241da177e4SLinus Torvalds 		result = acpi_bus_get_wakeup_device_flags(device);
13251da177e4SLinus Torvalds 		if (result)
13261da177e4SLinus Torvalds 			goto end;
13271da177e4SLinus Torvalds 	}
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 	/*
13301da177e4SLinus Torvalds 	 * Performance Management
13311da177e4SLinus Torvalds 	 * ----------------------
13321da177e4SLinus Torvalds 	 */
13331da177e4SLinus Torvalds 	if (device->flags.performance_manageable) {
13341da177e4SLinus Torvalds 		result = acpi_bus_get_perf_flags(device);
13351da177e4SLinus Torvalds 		if (result)
13361da177e4SLinus Torvalds 			goto end;
13371da177e4SLinus Torvalds 	}
13381da177e4SLinus Torvalds 
1339*bc3b0772SBjorn Helgaas 	if ((result = acpi_device_set_context(device)))
1340f61f9258SLen Brown 		goto end;
13411da177e4SLinus Torvalds 
134266b7ed40SBjorn Helgaas 	result = acpi_device_register(device);
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds 	/*
13452786f6e3SRui Zhang 	 * Bind _ADR-Based Devices when hot add
13461da177e4SLinus Torvalds 	 */
13471da177e4SLinus Torvalds 	if (device->flags.bus_address) {
13481da177e4SLinus Torvalds 		if (device->parent && device->parent->ops.bind)
13491da177e4SLinus Torvalds 			device->parent->ops.bind(device);
13501da177e4SLinus Torvalds 	}
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds end:
135329aaefa6SBjorn Helgaas 	if (!result) {
135429aaefa6SBjorn Helgaas 		acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
135529aaefa6SBjorn Helgaas 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
135629aaefa6SBjorn Helgaas 			"Adding %s [%s] parent %s\n", dev_name(&device->dev),
135729aaefa6SBjorn Helgaas 			 (char *) buffer.pointer,
135829aaefa6SBjorn Helgaas 			 device->parent ? dev_name(&device->parent->dev) :
135929aaefa6SBjorn Helgaas 					  "(null)"));
136029aaefa6SBjorn Helgaas 		kfree(buffer.pointer);
13611da177e4SLinus Torvalds 		*child = device;
136229aaefa6SBjorn Helgaas 	} else
1363718fb0deSHugh Dickins 		acpi_device_release(&device->dev);
13641da177e4SLinus Torvalds 
1365d550d98dSPatrick Mochel 	return result;
13661da177e4SLinus Torvalds }
13671da177e4SLinus Torvalds 
13684be44fcdSLen Brown static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
13691da177e4SLinus Torvalds {
13701da177e4SLinus Torvalds 	acpi_status status = AE_OK;
13711da177e4SLinus Torvalds 	struct acpi_device *parent = NULL;
13721da177e4SLinus Torvalds 	struct acpi_device *child = NULL;
13731da177e4SLinus Torvalds 	acpi_handle phandle = NULL;
13741da177e4SLinus Torvalds 	acpi_handle chandle = NULL;
13751da177e4SLinus Torvalds 	acpi_object_type type = 0;
13761da177e4SLinus Torvalds 	u32 level = 1;
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 	if (!start)
1380d550d98dSPatrick Mochel 		return -EINVAL;
13811da177e4SLinus Torvalds 
13821da177e4SLinus Torvalds 	parent = start;
13831da177e4SLinus Torvalds 	phandle = start->handle;
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds 	/*
13861da177e4SLinus Torvalds 	 * Parse through the ACPI namespace, identify all 'devices', and
13871da177e4SLinus Torvalds 	 * create a new 'struct acpi_device' for each.
13881da177e4SLinus Torvalds 	 */
13891da177e4SLinus Torvalds 	while ((level > 0) && parent) {
13901da177e4SLinus Torvalds 
13911da177e4SLinus Torvalds 		status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
13921da177e4SLinus Torvalds 					      chandle, &chandle);
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds 		/*
13951da177e4SLinus Torvalds 		 * If this scope is exhausted then move our way back up.
13961da177e4SLinus Torvalds 		 */
13971da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
13981da177e4SLinus Torvalds 			level--;
13991da177e4SLinus Torvalds 			chandle = phandle;
14001da177e4SLinus Torvalds 			acpi_get_parent(phandle, &phandle);
14011da177e4SLinus Torvalds 			if (parent->parent)
14021da177e4SLinus Torvalds 				parent = parent->parent;
14031da177e4SLinus Torvalds 			continue;
14041da177e4SLinus Torvalds 		}
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds 		status = acpi_get_type(chandle, &type);
14071da177e4SLinus Torvalds 		if (ACPI_FAILURE(status))
14081da177e4SLinus Torvalds 			continue;
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds 		/*
14111da177e4SLinus Torvalds 		 * If this is a scope object then parse it (depth-first).
14121da177e4SLinus Torvalds 		 */
14131da177e4SLinus Torvalds 		if (type == ACPI_TYPE_LOCAL_SCOPE) {
14141da177e4SLinus Torvalds 			level++;
14151da177e4SLinus Torvalds 			phandle = chandle;
14161da177e4SLinus Torvalds 			chandle = NULL;
14171da177e4SLinus Torvalds 			continue;
14181da177e4SLinus Torvalds 		}
14191da177e4SLinus Torvalds 
14201da177e4SLinus Torvalds 		/*
14211da177e4SLinus Torvalds 		 * We're only interested in objects that we consider 'devices'.
14221da177e4SLinus Torvalds 		 */
14231da177e4SLinus Torvalds 		switch (type) {
14241da177e4SLinus Torvalds 		case ACPI_TYPE_DEVICE:
14251da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_DEVICE;
14261da177e4SLinus Torvalds 			break;
14271da177e4SLinus Torvalds 		case ACPI_TYPE_PROCESSOR:
14281da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_PROCESSOR;
14291da177e4SLinus Torvalds 			break;
14301da177e4SLinus Torvalds 		case ACPI_TYPE_THERMAL:
14311da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_THERMAL;
14321da177e4SLinus Torvalds 			break;
14331da177e4SLinus Torvalds 		case ACPI_TYPE_POWER:
14341da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_POWER;
14351da177e4SLinus Torvalds 			break;
14361da177e4SLinus Torvalds 		default:
14371da177e4SLinus Torvalds 			continue;
14381da177e4SLinus Torvalds 		}
14391da177e4SLinus Torvalds 
14403fb02738SRajesh Shah 		if (ops->acpi_op_add)
14413fb02738SRajesh Shah 			status = acpi_add_single_object(&child, parent,
1442c4168bffSLi Shaohua 				chandle, type, ops);
14433fb02738SRajesh Shah 		else
14443fb02738SRajesh Shah 			status = acpi_bus_get_device(chandle, &child);
14453fb02738SRajesh Shah 
14461da177e4SLinus Torvalds 		if (ACPI_FAILURE(status))
14471da177e4SLinus Torvalds 			continue;
14481da177e4SLinus Torvalds 
1449c4168bffSLi Shaohua 		if (ops->acpi_op_start && !(ops->acpi_op_add)) {
14503fb02738SRajesh Shah 			status = acpi_start_single_object(child);
14513fb02738SRajesh Shah 			if (ACPI_FAILURE(status))
14523fb02738SRajesh Shah 				continue;
14533fb02738SRajesh Shah 		}
14543fb02738SRajesh Shah 
14551da177e4SLinus Torvalds 		/*
14561da177e4SLinus Torvalds 		 * If the device is present, enabled, and functioning then
14571da177e4SLinus Torvalds 		 * parse its scope (depth-first).  Note that we need to
14581da177e4SLinus Torvalds 		 * represent absent devices to facilitate PnP notifications
14591da177e4SLinus Torvalds 		 * -- but only the subtree head (not all of its children,
14601da177e4SLinus Torvalds 		 * which will be enumerated when the parent is inserted).
14611da177e4SLinus Torvalds 		 *
14621da177e4SLinus Torvalds 		 * TBD: Need notifications and other detection mechanisms
14631da177e4SLinus Torvalds 		 *      in place before we can fully implement this.
14641da177e4SLinus Torvalds 		 */
146539a0ad87SZhao Yakui 		 /*
146639a0ad87SZhao Yakui 		 * When the device is not present but functional, it is also
146739a0ad87SZhao Yakui 		 * necessary to scan the children of this device.
146839a0ad87SZhao Yakui 		 */
146939a0ad87SZhao Yakui 		if (child->status.present || (!child->status.present &&
147039a0ad87SZhao Yakui 					child->status.functional)) {
14711da177e4SLinus Torvalds 			status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
14721da177e4SLinus Torvalds 						      NULL, NULL);
14731da177e4SLinus Torvalds 			if (ACPI_SUCCESS(status)) {
14741da177e4SLinus Torvalds 				level++;
14751da177e4SLinus Torvalds 				phandle = chandle;
14761da177e4SLinus Torvalds 				chandle = NULL;
14771da177e4SLinus Torvalds 				parent = child;
14781da177e4SLinus Torvalds 			}
14791da177e4SLinus Torvalds 		}
14801da177e4SLinus Torvalds 	}
14811da177e4SLinus Torvalds 
1482d550d98dSPatrick Mochel 	return 0;
14831da177e4SLinus Torvalds }
14841da177e4SLinus Torvalds 
14853fb02738SRajesh Shah int
14864be44fcdSLen Brown acpi_bus_add(struct acpi_device **child,
14874be44fcdSLen Brown 	     struct acpi_device *parent, acpi_handle handle, int type)
14883fb02738SRajesh Shah {
14893fb02738SRajesh Shah 	int result;
14903fb02738SRajesh Shah 	struct acpi_bus_ops ops;
14913fb02738SRajesh Shah 
14923fb02738SRajesh Shah 	memset(&ops, 0, sizeof(ops));
14933fb02738SRajesh Shah 	ops.acpi_op_add = 1;
1494c4168bffSLi Shaohua 
1495c4168bffSLi Shaohua 	result = acpi_add_single_object(child, parent, handle, type, &ops);
1496c4168bffSLi Shaohua 	if (!result)
14973fb02738SRajesh Shah 		result = acpi_bus_scan(*child, &ops);
1498c4168bffSLi Shaohua 
1499d550d98dSPatrick Mochel 	return result;
15003fb02738SRajesh Shah }
15013fb02738SRajesh Shah EXPORT_SYMBOL(acpi_bus_add);
15023fb02738SRajesh Shah 
15034be44fcdSLen Brown int acpi_bus_start(struct acpi_device *device)
15043fb02738SRajesh Shah {
15053fb02738SRajesh Shah 	int result;
15063fb02738SRajesh Shah 	struct acpi_bus_ops ops;
15073fb02738SRajesh Shah 
15083fb02738SRajesh Shah 
15093fb02738SRajesh Shah 	if (!device)
1510d550d98dSPatrick Mochel 		return -EINVAL;
15113fb02738SRajesh Shah 
15123fb02738SRajesh Shah 	result = acpi_start_single_object(device);
15133fb02738SRajesh Shah 	if (!result) {
15143fb02738SRajesh Shah 		memset(&ops, 0, sizeof(ops));
15153fb02738SRajesh Shah 		ops.acpi_op_start = 1;
15163fb02738SRajesh Shah 		result = acpi_bus_scan(device, &ops);
15173fb02738SRajesh Shah 	}
1518d550d98dSPatrick Mochel 	return result;
15193fb02738SRajesh Shah }
15203fb02738SRajesh Shah EXPORT_SYMBOL(acpi_bus_start);
15211da177e4SLinus Torvalds 
1522ceaba663SKristen Accardi int acpi_bus_trim(struct acpi_device *start, int rmdevice)
15231da177e4SLinus Torvalds {
15241da177e4SLinus Torvalds 	acpi_status status;
15251da177e4SLinus Torvalds 	struct acpi_device *parent, *child;
15261da177e4SLinus Torvalds 	acpi_handle phandle, chandle;
15271da177e4SLinus Torvalds 	acpi_object_type type;
15281da177e4SLinus Torvalds 	u32 level = 1;
15291da177e4SLinus Torvalds 	int err = 0;
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 	parent = start;
15321da177e4SLinus Torvalds 	phandle = start->handle;
15331da177e4SLinus Torvalds 	child = chandle = NULL;
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds 	while ((level > 0) && parent && (!err)) {
15361da177e4SLinus Torvalds 		status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
15371da177e4SLinus Torvalds 					      chandle, &chandle);
15381da177e4SLinus Torvalds 
15391da177e4SLinus Torvalds 		/*
15401da177e4SLinus Torvalds 		 * If this scope is exhausted then move our way back up.
15411da177e4SLinus Torvalds 		 */
15421da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
15431da177e4SLinus Torvalds 			level--;
15441da177e4SLinus Torvalds 			chandle = phandle;
15451da177e4SLinus Torvalds 			acpi_get_parent(phandle, &phandle);
15461da177e4SLinus Torvalds 			child = parent;
15471da177e4SLinus Torvalds 			parent = parent->parent;
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds 			if (level == 0)
15501da177e4SLinus Torvalds 				err = acpi_bus_remove(child, rmdevice);
15511da177e4SLinus Torvalds 			else
15521da177e4SLinus Torvalds 				err = acpi_bus_remove(child, 1);
15531da177e4SLinus Torvalds 
15541da177e4SLinus Torvalds 			continue;
15551da177e4SLinus Torvalds 		}
15561da177e4SLinus Torvalds 
15571da177e4SLinus Torvalds 		status = acpi_get_type(chandle, &type);
15581da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
15591da177e4SLinus Torvalds 			continue;
15601da177e4SLinus Torvalds 		}
15611da177e4SLinus Torvalds 		/*
15621da177e4SLinus Torvalds 		 * If there is a device corresponding to chandle then
15631da177e4SLinus Torvalds 		 * parse it (depth-first).
15641da177e4SLinus Torvalds 		 */
15651da177e4SLinus Torvalds 		if (acpi_bus_get_device(chandle, &child) == 0) {
15661da177e4SLinus Torvalds 			level++;
15671da177e4SLinus Torvalds 			phandle = chandle;
15681da177e4SLinus Torvalds 			chandle = NULL;
15691da177e4SLinus Torvalds 			parent = child;
15701da177e4SLinus Torvalds 		}
15711da177e4SLinus Torvalds 		continue;
15721da177e4SLinus Torvalds 	}
15731da177e4SLinus Torvalds 	return err;
15741da177e4SLinus Torvalds }
1575ceaba663SKristen Accardi EXPORT_SYMBOL_GPL(acpi_bus_trim);
1576ceaba663SKristen Accardi 
1577e8b945c9SBjorn Helgaas static int acpi_bus_scan_fixed(void)
15781da177e4SLinus Torvalds {
15791da177e4SLinus Torvalds 	int result = 0;
15801da177e4SLinus Torvalds 	struct acpi_device *device = NULL;
1581c4168bffSLi Shaohua 	struct acpi_bus_ops ops;
15821da177e4SLinus Torvalds 
1583c4168bffSLi Shaohua 	memset(&ops, 0, sizeof(ops));
1584c4168bffSLi Shaohua 	ops.acpi_op_add = 1;
1585c4168bffSLi Shaohua 	ops.acpi_op_start = 1;
1586c4168bffSLi Shaohua 
15871da177e4SLinus Torvalds 	/*
15881da177e4SLinus Torvalds 	 * Enumerate all fixed-feature devices.
15891da177e4SLinus Torvalds 	 */
1590cee324b1SAlexey Starikovskiy 	if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
15913fb02738SRajesh Shah 		result = acpi_add_single_object(&device, acpi_root,
15924be44fcdSLen Brown 						NULL,
1593c4168bffSLi Shaohua 						ACPI_BUS_TYPE_POWER_BUTTON,
1594c4168bffSLi Shaohua 						&ops);
15953fb02738SRajesh Shah 	}
15961da177e4SLinus Torvalds 
1597cee324b1SAlexey Starikovskiy 	if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
15983fb02738SRajesh Shah 		result = acpi_add_single_object(&device, acpi_root,
15994be44fcdSLen Brown 						NULL,
1600c4168bffSLi Shaohua 						ACPI_BUS_TYPE_SLEEP_BUTTON,
1601c4168bffSLi Shaohua 						&ops);
16023fb02738SRajesh Shah 	}
16031da177e4SLinus Torvalds 
1604d550d98dSPatrick Mochel 	return result;
16051da177e4SLinus Torvalds }
16061da177e4SLinus Torvalds 
1607e747f274SBjorn Helgaas int __init acpi_scan_init(void)
16081da177e4SLinus Torvalds {
16091da177e4SLinus Torvalds 	int result;
16103fb02738SRajesh Shah 	struct acpi_bus_ops ops;
16111da177e4SLinus Torvalds 
1612c4168bffSLi Shaohua 	memset(&ops, 0, sizeof(ops));
1613c4168bffSLi Shaohua 	ops.acpi_op_add = 1;
1614c4168bffSLi Shaohua 	ops.acpi_op_start = 1;
16151da177e4SLinus Torvalds 
16165b327265SPatrick Mochel 	result = bus_register(&acpi_bus_type);
16175b327265SPatrick Mochel 	if (result) {
16185b327265SPatrick Mochel 		/* We don't want to quit even if we failed to add suspend/resume */
16195b327265SPatrick Mochel 		printk(KERN_ERR PREFIX "Could not register bus type\n");
16205b327265SPatrick Mochel 	}
16215b327265SPatrick Mochel 
16221da177e4SLinus Torvalds 	/*
16231da177e4SLinus Torvalds 	 * Create the root device in the bus's device tree
16241da177e4SLinus Torvalds 	 */
16253fb02738SRajesh Shah 	result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
1626c4168bffSLi Shaohua 					ACPI_BUS_TYPE_SYSTEM, &ops);
16271da177e4SLinus Torvalds 	if (result)
16281da177e4SLinus Torvalds 		goto Done;
16291da177e4SLinus Torvalds 
16301da177e4SLinus Torvalds 	/*
16311da177e4SLinus Torvalds 	 * Enumerate devices in the ACPI namespace.
16321da177e4SLinus Torvalds 	 */
1633e8b945c9SBjorn Helgaas 	result = acpi_bus_scan_fixed();
1634c04209a7SAlexey Starikovskiy 
1635c4168bffSLi Shaohua 	if (!result)
16363fb02738SRajesh Shah 		result = acpi_bus_scan(acpi_root, &ops);
16371da177e4SLinus Torvalds 
16381da177e4SLinus Torvalds 	if (result)
16391da177e4SLinus Torvalds 		acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds Done:
1642d550d98dSPatrick Mochel 	return result;
16431da177e4SLinus Torvalds }
1644