xref: /openbmc/linux/drivers/acpi/scan.c (revision 859ac9a4be0c753cece0e30a2e4a65fd2cdcaeee)
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 
25*859ac9a4SBjorn Helgaas #define ACPI_IS_ROOT_DEVICE(device)    (!(device)->parent)
26*859ac9a4SBjorn Helgaas 
271da177e4SLinus Torvalds static LIST_HEAD(acpi_device_list);
28e49bd2ddSZhang Rui static LIST_HEAD(acpi_bus_id_list);
299090589dSShaohua Li DEFINE_MUTEX(acpi_device_lock);
301da177e4SLinus Torvalds LIST_HEAD(acpi_wakeup_device_list);
311da177e4SLinus Torvalds 
32e49bd2ddSZhang Rui struct acpi_device_bus_id{
33bb095854SZhang Rui 	char bus_id[15];
34e49bd2ddSZhang Rui 	unsigned int instance_no;
35e49bd2ddSZhang Rui 	struct list_head node;
36e49bd2ddSZhang Rui };
3729b71a1cSThomas Renninger 
3829b71a1cSThomas Renninger /*
3929b71a1cSThomas Renninger  * Creates hid/cid(s) string needed for modalias and uevent
4029b71a1cSThomas Renninger  * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
4129b71a1cSThomas Renninger  * char *modalias: "acpi:IBM0001:ACPI0001"
4229b71a1cSThomas Renninger */
43b3e572d2SAdrian Bunk static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
44b3e572d2SAdrian Bunk 			   int size)
45b3e572d2SAdrian Bunk {
4629b71a1cSThomas Renninger 	int len;
475c9fcb5dSZhang Rui 	int count;
4829b71a1cSThomas Renninger 
495c9fcb5dSZhang Rui 	if (!acpi_dev->flags.hardware_id && !acpi_dev->flags.compatible_ids)
5029b71a1cSThomas Renninger 		return -ENODEV;
5129b71a1cSThomas Renninger 
525c9fcb5dSZhang Rui 	len = snprintf(modalias, size, "acpi:");
5329b71a1cSThomas Renninger 	size -= len;
5429b71a1cSThomas Renninger 
555c9fcb5dSZhang Rui 	if (acpi_dev->flags.hardware_id) {
565c9fcb5dSZhang Rui 		count = snprintf(&modalias[len], size, "%s:",
575c9fcb5dSZhang Rui 				 acpi_dev->pnp.hardware_id);
585c9fcb5dSZhang Rui 		if (count < 0 || count >= size)
595c9fcb5dSZhang Rui 			return -EINVAL;
605c9fcb5dSZhang Rui 		len += count;
615c9fcb5dSZhang Rui 		size -= count;
625c9fcb5dSZhang Rui 	}
635c9fcb5dSZhang Rui 
6429b71a1cSThomas Renninger 	if (acpi_dev->flags.compatible_ids) {
6515b8dd53SBob Moore 		struct acpica_device_id_list *cid_list;
6629b71a1cSThomas Renninger 		int i;
6729b71a1cSThomas Renninger 
6829b71a1cSThomas Renninger 		cid_list = acpi_dev->pnp.cid_list;
6929b71a1cSThomas Renninger 		for (i = 0; i < cid_list->count; i++) {
7029b71a1cSThomas Renninger 			count = snprintf(&modalias[len], size, "%s:",
7115b8dd53SBob Moore 					 cid_list->ids[i].string);
7229b71a1cSThomas Renninger 			if (count < 0 || count >= size) {
7387ecd5cdSLen Brown 				printk(KERN_ERR PREFIX "%s cid[%i] exceeds event buffer size",
7429b71a1cSThomas Renninger 				       acpi_dev->pnp.device_name, i);
7529b71a1cSThomas Renninger 				break;
7629b71a1cSThomas Renninger 			}
7729b71a1cSThomas Renninger 			len += count;
7829b71a1cSThomas Renninger 			size -= count;
7929b71a1cSThomas Renninger 		}
8029b71a1cSThomas Renninger 	}
8129b71a1cSThomas Renninger 
8229b71a1cSThomas Renninger 	modalias[len] = '\0';
8329b71a1cSThomas Renninger 	return len;
8429b71a1cSThomas Renninger }
8529b71a1cSThomas Renninger 
8629b71a1cSThomas Renninger static ssize_t
8729b71a1cSThomas Renninger acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
8829b71a1cSThomas Renninger 	struct acpi_device *acpi_dev = to_acpi_device(dev);
8929b71a1cSThomas Renninger 	int len;
9029b71a1cSThomas Renninger 
9129b71a1cSThomas Renninger 	/* Device has no HID and no CID or string is >1024 */
9229b71a1cSThomas Renninger 	len = create_modalias(acpi_dev, buf, 1024);
9329b71a1cSThomas Renninger 	if (len <= 0)
9429b71a1cSThomas Renninger 		return 0;
9529b71a1cSThomas Renninger 	buf[len++] = '\n';
9629b71a1cSThomas Renninger 	return len;
9729b71a1cSThomas Renninger }
9829b71a1cSThomas Renninger static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
9929b71a1cSThomas Renninger 
100c8d72a5eSZhang Rui static void acpi_bus_hot_remove_device(void *context)
1011da177e4SLinus Torvalds {
10226d46867SZhang Rui 	struct acpi_device *device;
10326d46867SZhang Rui 	acpi_handle handle = context;
1041da177e4SLinus Torvalds 	struct acpi_object_list arg_list;
1051da177e4SLinus Torvalds 	union acpi_object arg;
1061da177e4SLinus Torvalds 	acpi_status status = AE_OK;
1071da177e4SLinus Torvalds 
10826d46867SZhang Rui 	if (acpi_bus_get_device(handle, &device))
109c8d72a5eSZhang Rui 		return;
1101da177e4SLinus Torvalds 
11126d46867SZhang Rui 	if (!device)
112c8d72a5eSZhang Rui 		return;
11326d46867SZhang Rui 
11426d46867SZhang Rui 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1150794469dSKay Sievers 		"Hot-removing device %s...\n", dev_name(&device->dev)));
11626d46867SZhang Rui 
11726d46867SZhang Rui 	if (acpi_bus_trim(device, 1)) {
11855ac9a01SLin Ming 		printk(KERN_ERR PREFIX
11955ac9a01SLin Ming 				"Removing device failed\n");
120c8d72a5eSZhang Rui 		return;
12126d46867SZhang Rui 	}
12226d46867SZhang Rui 
12326d46867SZhang Rui 	/* power off device */
12426d46867SZhang Rui 	status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
12526d46867SZhang Rui 	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
12655ac9a01SLin Ming 		printk(KERN_WARNING PREFIX
12755ac9a01SLin Ming 				"Power-off device failed\n");
12826d46867SZhang Rui 
12926d46867SZhang Rui 	if (device->flags.lockable) {
1301da177e4SLinus Torvalds 		arg_list.count = 1;
1311da177e4SLinus Torvalds 		arg_list.pointer = &arg;
1321da177e4SLinus Torvalds 		arg.type = ACPI_TYPE_INTEGER;
1331da177e4SLinus Torvalds 		arg.integer.value = 0;
1341da177e4SLinus Torvalds 		acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
1351da177e4SLinus Torvalds 	}
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	arg_list.count = 1;
1381da177e4SLinus Torvalds 	arg_list.pointer = &arg;
1391da177e4SLinus Torvalds 	arg.type = ACPI_TYPE_INTEGER;
1401da177e4SLinus Torvalds 	arg.integer.value = 1;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	/*
1431da177e4SLinus Torvalds 	 * TBD: _EJD support.
1441da177e4SLinus Torvalds 	 */
1451da177e4SLinus Torvalds 	status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
14626d46867SZhang Rui 	if (ACPI_FAILURE(status))
147c8d72a5eSZhang Rui 		printk(KERN_WARNING PREFIX
148c8d72a5eSZhang Rui 				"Eject device failed\n");
1491da177e4SLinus Torvalds 
150c8d72a5eSZhang Rui 	return;
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds static ssize_t
154f883d9dbSPatrick Mochel acpi_eject_store(struct device *d, struct device_attribute *attr,
155f883d9dbSPatrick Mochel 		const char *buf, size_t count)
1561da177e4SLinus Torvalds {
1571da177e4SLinus Torvalds 	int ret = count;
1581da177e4SLinus Torvalds 	acpi_status status;
1591da177e4SLinus Torvalds 	acpi_object_type type = 0;
160f883d9dbSPatrick Mochel 	struct acpi_device *acpi_device = to_acpi_device(d);
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	if ((!count) || (buf[0] != '1')) {
1631da177e4SLinus Torvalds 		return -EINVAL;
1641da177e4SLinus Torvalds 	}
1651da177e4SLinus Torvalds #ifndef FORCE_EJECT
166f883d9dbSPatrick Mochel 	if (acpi_device->driver == NULL) {
1671da177e4SLinus Torvalds 		ret = -ENODEV;
1681da177e4SLinus Torvalds 		goto err;
1691da177e4SLinus Torvalds 	}
1701da177e4SLinus Torvalds #endif
171f883d9dbSPatrick Mochel 	status = acpi_get_type(acpi_device->handle, &type);
172f883d9dbSPatrick Mochel 	if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
1731da177e4SLinus Torvalds 		ret = -ENODEV;
1741da177e4SLinus Torvalds 		goto err;
1751da177e4SLinus Torvalds 	}
1761da177e4SLinus Torvalds 
177c8d72a5eSZhang Rui 	acpi_os_hotplug_execute(acpi_bus_hot_remove_device, acpi_device->handle);
1781da177e4SLinus Torvalds err:
1791da177e4SLinus Torvalds 	return ret;
1801da177e4SLinus Torvalds }
1811da177e4SLinus Torvalds 
182f883d9dbSPatrick Mochel static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
183f883d9dbSPatrick Mochel 
184e49bd2ddSZhang Rui static ssize_t
185e49bd2ddSZhang Rui acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
186e49bd2ddSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
187e49bd2ddSZhang Rui 
188e49bd2ddSZhang Rui 	return sprintf(buf, "%s\n", acpi_dev->pnp.hardware_id);
189e49bd2ddSZhang Rui }
190e49bd2ddSZhang Rui static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
191e49bd2ddSZhang Rui 
192e49bd2ddSZhang Rui static ssize_t
193e49bd2ddSZhang Rui acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
194e49bd2ddSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
195e49bd2ddSZhang Rui 	struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
196e49bd2ddSZhang Rui 	int result;
197e49bd2ddSZhang Rui 
198e49bd2ddSZhang Rui 	result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
199e49bd2ddSZhang Rui 	if (result)
200e49bd2ddSZhang Rui 		goto end;
201e49bd2ddSZhang Rui 
202e49bd2ddSZhang Rui 	result = sprintf(buf, "%s\n", (char*)path.pointer);
203e49bd2ddSZhang Rui 	kfree(path.pointer);
204e49bd2ddSZhang Rui end:
205e49bd2ddSZhang Rui 	return result;
206e49bd2ddSZhang Rui }
207e49bd2ddSZhang Rui static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
208e49bd2ddSZhang Rui 
209e49bd2ddSZhang Rui static int acpi_device_setup_files(struct acpi_device *dev)
210f883d9dbSPatrick Mochel {
211f883d9dbSPatrick Mochel 	acpi_status status;
212f883d9dbSPatrick Mochel 	acpi_handle temp;
213e49bd2ddSZhang Rui 	int result = 0;
214e49bd2ddSZhang Rui 
215e49bd2ddSZhang Rui 	/*
216e49bd2ddSZhang Rui 	 * Devices gotten from FADT don't have a "path" attribute
217e49bd2ddSZhang Rui 	 */
218e49bd2ddSZhang Rui 	if (dev->handle) {
219e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_path);
220e49bd2ddSZhang Rui 		if (result)
221e49bd2ddSZhang Rui 			goto end;
222e49bd2ddSZhang Rui 	}
223e49bd2ddSZhang Rui 
224e49bd2ddSZhang Rui 	if (dev->flags.hardware_id) {
225e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_hid);
226e49bd2ddSZhang Rui 		if (result)
227e49bd2ddSZhang Rui 			goto end;
228e49bd2ddSZhang Rui 	}
229f883d9dbSPatrick Mochel 
23029b71a1cSThomas Renninger 	if (dev->flags.hardware_id || dev->flags.compatible_ids) {
23129b71a1cSThomas Renninger 		result = device_create_file(&dev->dev, &dev_attr_modalias);
23229b71a1cSThomas Renninger 		if (result)
23329b71a1cSThomas Renninger 			goto end;
23429b71a1cSThomas Renninger 	}
23529b71a1cSThomas Renninger 
236f883d9dbSPatrick Mochel         /*
237f883d9dbSPatrick Mochel          * If device has _EJ0, 'eject' file is created that is used to trigger
238f883d9dbSPatrick Mochel          * hot-removal function from userland.
239f883d9dbSPatrick Mochel          */
240f883d9dbSPatrick Mochel 	status = acpi_get_handle(dev->handle, "_EJ0", &temp);
241f883d9dbSPatrick Mochel 	if (ACPI_SUCCESS(status))
242e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_eject);
243e49bd2ddSZhang Rui end:
244e49bd2ddSZhang Rui 	return result;
245f883d9dbSPatrick Mochel }
246f883d9dbSPatrick Mochel 
247f883d9dbSPatrick Mochel static void acpi_device_remove_files(struct acpi_device *dev)
248f883d9dbSPatrick Mochel {
249f883d9dbSPatrick Mochel 	acpi_status status;
250f883d9dbSPatrick Mochel 	acpi_handle temp;
251f883d9dbSPatrick Mochel 
252f883d9dbSPatrick Mochel 	/*
253f883d9dbSPatrick Mochel 	 * If device has _EJ0, 'eject' file is created that is used to trigger
254f883d9dbSPatrick Mochel 	 * hot-removal function from userland.
255f883d9dbSPatrick Mochel 	 */
256f883d9dbSPatrick Mochel 	status = acpi_get_handle(dev->handle, "_EJ0", &temp);
257f883d9dbSPatrick Mochel 	if (ACPI_SUCCESS(status))
258f883d9dbSPatrick Mochel 		device_remove_file(&dev->dev, &dev_attr_eject);
259e49bd2ddSZhang Rui 
26029b71a1cSThomas Renninger 	if (dev->flags.hardware_id || dev->flags.compatible_ids)
26129b71a1cSThomas Renninger 		device_remove_file(&dev->dev, &dev_attr_modalias);
26229b71a1cSThomas Renninger 
263e49bd2ddSZhang Rui 	if (dev->flags.hardware_id)
264e49bd2ddSZhang Rui 		device_remove_file(&dev->dev, &dev_attr_hid);
265e49bd2ddSZhang Rui 	if (dev->handle)
266e49bd2ddSZhang Rui 		device_remove_file(&dev->dev, &dev_attr_path);
267f883d9dbSPatrick Mochel }
2681da177e4SLinus Torvalds /* --------------------------------------------------------------------------
2699e89dde2SZhang Rui 			ACPI Bus operations
2701da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
27129b71a1cSThomas Renninger 
27229b71a1cSThomas Renninger int acpi_match_device_ids(struct acpi_device *device,
27329b71a1cSThomas Renninger 			  const struct acpi_device_id *ids)
27429b71a1cSThomas Renninger {
27529b71a1cSThomas Renninger 	const struct acpi_device_id *id;
27629b71a1cSThomas Renninger 
27739a0ad87SZhao Yakui 	/*
27839a0ad87SZhao Yakui 	 * If the device is not present, it is unnecessary to load device
27939a0ad87SZhao Yakui 	 * driver for it.
28039a0ad87SZhao Yakui 	 */
28139a0ad87SZhao Yakui 	if (!device->status.present)
28239a0ad87SZhao Yakui 		return -ENODEV;
28339a0ad87SZhao Yakui 
28429b71a1cSThomas Renninger 	if (device->flags.hardware_id) {
28529b71a1cSThomas Renninger 		for (id = ids; id->id[0]; id++) {
28629b71a1cSThomas Renninger 			if (!strcmp((char*)id->id, device->pnp.hardware_id))
28729b71a1cSThomas Renninger 				return 0;
28829b71a1cSThomas Renninger 		}
28929b71a1cSThomas Renninger 	}
29029b71a1cSThomas Renninger 
29129b71a1cSThomas Renninger 	if (device->flags.compatible_ids) {
29215b8dd53SBob Moore 		struct acpica_device_id_list *cid_list = device->pnp.cid_list;
29329b71a1cSThomas Renninger 		int i;
29429b71a1cSThomas Renninger 
29529b71a1cSThomas Renninger 		for (id = ids; id->id[0]; id++) {
29629b71a1cSThomas Renninger 			/* compare multiple _CID entries against driver ids */
29729b71a1cSThomas Renninger 			for (i = 0; i < cid_list->count; i++) {
29829b71a1cSThomas Renninger 				if (!strcmp((char*)id->id,
29915b8dd53SBob Moore 					    cid_list->ids[i].string))
30029b71a1cSThomas Renninger 					return 0;
30129b71a1cSThomas Renninger 			}
30229b71a1cSThomas Renninger 		}
30329b71a1cSThomas Renninger 	}
30429b71a1cSThomas Renninger 
30529b71a1cSThomas Renninger 	return -ENOENT;
30629b71a1cSThomas Renninger }
30729b71a1cSThomas Renninger EXPORT_SYMBOL(acpi_match_device_ids);
30829b71a1cSThomas Renninger 
3091890a97aSPatrick Mochel static void acpi_device_release(struct device *dev)
3101890a97aSPatrick Mochel {
3111890a97aSPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3121890a97aSPatrick Mochel 
3131890a97aSPatrick Mochel 	kfree(acpi_dev->pnp.cid_list);
314718fb0deSHugh Dickins 	if (acpi_dev->flags.hardware_id)
315718fb0deSHugh Dickins 		kfree(acpi_dev->pnp.hardware_id);
316718fb0deSHugh Dickins 	if (acpi_dev->flags.unique_id)
317718fb0deSHugh Dickins 		kfree(acpi_dev->pnp.unique_id);
3181890a97aSPatrick Mochel 	kfree(acpi_dev);
3191890a97aSPatrick Mochel }
3201890a97aSPatrick Mochel 
3219e89dde2SZhang Rui static int acpi_device_suspend(struct device *dev, pm_message_t state)
3229e89dde2SZhang Rui {
3231d268b0aSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3245d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
3251da177e4SLinus Torvalds 
3265d9464a4SPatrick Mochel 	if (acpi_drv && acpi_drv->ops.suspend)
3275d9464a4SPatrick Mochel 		return acpi_drv->ops.suspend(acpi_dev, state);
3281da177e4SLinus Torvalds 	return 0;
3299e89dde2SZhang Rui }
3301da177e4SLinus Torvalds 
3319e89dde2SZhang Rui static int acpi_device_resume(struct device *dev)
3329e89dde2SZhang Rui {
3331d268b0aSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3345d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
3359e89dde2SZhang Rui 
3365d9464a4SPatrick Mochel 	if (acpi_drv && acpi_drv->ops.resume)
3375d9464a4SPatrick Mochel 		return acpi_drv->ops.resume(acpi_dev);
3389e89dde2SZhang Rui 	return 0;
3399e89dde2SZhang Rui }
3401da177e4SLinus Torvalds 
3415d9464a4SPatrick Mochel static int acpi_bus_match(struct device *dev, struct device_driver *drv)
3421da177e4SLinus Torvalds {
3435d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3445d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(drv);
3455d9464a4SPatrick Mochel 
34629b71a1cSThomas Renninger 	return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
3475d9464a4SPatrick Mochel }
3485d9464a4SPatrick Mochel 
3497eff2e7aSKay Sievers static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
3505d9464a4SPatrick Mochel {
3515d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3527eff2e7aSKay Sievers 	int len;
3535d9464a4SPatrick Mochel 
3547eff2e7aSKay Sievers 	if (add_uevent_var(env, "MODALIAS="))
3557eff2e7aSKay Sievers 		return -ENOMEM;
3567eff2e7aSKay Sievers 	len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
3577eff2e7aSKay Sievers 			      sizeof(env->buf) - env->buflen);
3587eff2e7aSKay Sievers 	if (len >= (sizeof(env->buf) - env->buflen))
3597eff2e7aSKay Sievers 		return -ENOMEM;
3607eff2e7aSKay Sievers 	env->buflen += len;
3611da177e4SLinus Torvalds 	return 0;
3621da177e4SLinus Torvalds }
3631da177e4SLinus Torvalds 
36446ec8598SBjorn Helgaas static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
36546ec8598SBjorn Helgaas {
36646ec8598SBjorn Helgaas 	struct acpi_device *device = data;
36746ec8598SBjorn Helgaas 
36846ec8598SBjorn Helgaas 	device->driver->ops.notify(device, event);
36946ec8598SBjorn Helgaas }
37046ec8598SBjorn Helgaas 
37146ec8598SBjorn Helgaas static acpi_status acpi_device_notify_fixed(void *data)
37246ec8598SBjorn Helgaas {
37346ec8598SBjorn Helgaas 	struct acpi_device *device = data;
37446ec8598SBjorn Helgaas 
37553de5356SBjorn Helgaas 	/* Fixed hardware devices have no handles */
37653de5356SBjorn Helgaas 	acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
37746ec8598SBjorn Helgaas 	return AE_OK;
37846ec8598SBjorn Helgaas }
37946ec8598SBjorn Helgaas 
38046ec8598SBjorn Helgaas static int acpi_device_install_notify_handler(struct acpi_device *device)
38146ec8598SBjorn Helgaas {
38246ec8598SBjorn Helgaas 	acpi_status status;
38346ec8598SBjorn Helgaas 
384ccba2a36SBjorn Helgaas 	if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
38546ec8598SBjorn Helgaas 		status =
38646ec8598SBjorn Helgaas 		    acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
38746ec8598SBjorn Helgaas 						     acpi_device_notify_fixed,
38846ec8598SBjorn Helgaas 						     device);
389ccba2a36SBjorn Helgaas 	else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
39046ec8598SBjorn Helgaas 		status =
39146ec8598SBjorn Helgaas 		    acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
39246ec8598SBjorn Helgaas 						     acpi_device_notify_fixed,
39346ec8598SBjorn Helgaas 						     device);
39446ec8598SBjorn Helgaas 	else
39546ec8598SBjorn Helgaas 		status = acpi_install_notify_handler(device->handle,
39646ec8598SBjorn Helgaas 						     ACPI_DEVICE_NOTIFY,
39746ec8598SBjorn Helgaas 						     acpi_device_notify,
39846ec8598SBjorn Helgaas 						     device);
39946ec8598SBjorn Helgaas 
40046ec8598SBjorn Helgaas 	if (ACPI_FAILURE(status))
40146ec8598SBjorn Helgaas 		return -EINVAL;
40246ec8598SBjorn Helgaas 	return 0;
40346ec8598SBjorn Helgaas }
40446ec8598SBjorn Helgaas 
40546ec8598SBjorn Helgaas static void acpi_device_remove_notify_handler(struct acpi_device *device)
40646ec8598SBjorn Helgaas {
407ccba2a36SBjorn Helgaas 	if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
40846ec8598SBjorn Helgaas 		acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
40946ec8598SBjorn Helgaas 						acpi_device_notify_fixed);
410ccba2a36SBjorn Helgaas 	else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
41146ec8598SBjorn Helgaas 		acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
41246ec8598SBjorn Helgaas 						acpi_device_notify_fixed);
41346ec8598SBjorn Helgaas 	else
41446ec8598SBjorn Helgaas 		acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
41546ec8598SBjorn Helgaas 					   acpi_device_notify);
41646ec8598SBjorn Helgaas }
41746ec8598SBjorn Helgaas 
4185d9464a4SPatrick Mochel static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
4195d9464a4SPatrick Mochel static int acpi_start_single_object(struct acpi_device *);
4205d9464a4SPatrick Mochel static int acpi_device_probe(struct device * dev)
4215d9464a4SPatrick Mochel {
4225d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
4235d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
4245d9464a4SPatrick Mochel 	int ret;
4255d9464a4SPatrick Mochel 
4265d9464a4SPatrick Mochel 	ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
4275d9464a4SPatrick Mochel 	if (!ret) {
428c4168bffSLi Shaohua 		if (acpi_dev->bus_ops.acpi_op_start)
4295d9464a4SPatrick Mochel 			acpi_start_single_object(acpi_dev);
43046ec8598SBjorn Helgaas 
43146ec8598SBjorn Helgaas 		if (acpi_drv->ops.notify) {
43246ec8598SBjorn Helgaas 			ret = acpi_device_install_notify_handler(acpi_dev);
43346ec8598SBjorn Helgaas 			if (ret) {
43446ec8598SBjorn Helgaas 				if (acpi_drv->ops.remove)
43546ec8598SBjorn Helgaas 					acpi_drv->ops.remove(acpi_dev,
43646ec8598SBjorn Helgaas 						     acpi_dev->removal_type);
43746ec8598SBjorn Helgaas 				return ret;
43846ec8598SBjorn Helgaas 			}
43946ec8598SBjorn Helgaas 		}
44046ec8598SBjorn Helgaas 
4415d9464a4SPatrick Mochel 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4425d9464a4SPatrick Mochel 			"Found driver [%s] for device [%s]\n",
4435d9464a4SPatrick Mochel 			acpi_drv->name, acpi_dev->pnp.bus_id));
4445d9464a4SPatrick Mochel 		get_device(dev);
4455d9464a4SPatrick Mochel 	}
4465d9464a4SPatrick Mochel 	return ret;
4475d9464a4SPatrick Mochel }
4485d9464a4SPatrick Mochel 
4495d9464a4SPatrick Mochel static int acpi_device_remove(struct device * dev)
4505d9464a4SPatrick Mochel {
4515d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
4525d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
4535d9464a4SPatrick Mochel 
4545d9464a4SPatrick Mochel 	if (acpi_drv) {
45546ec8598SBjorn Helgaas 		if (acpi_drv->ops.notify)
45646ec8598SBjorn Helgaas 			acpi_device_remove_notify_handler(acpi_dev);
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 
47766b7ed40SBjorn Helgaas static int acpi_device_register(struct acpi_device *device)
4781da177e4SLinus Torvalds {
479e49bd2ddSZhang Rui 	int result;
480e49bd2ddSZhang Rui 	struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
481e49bd2ddSZhang Rui 	int found = 0;
48266b7ed40SBjorn Helgaas 
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)
52766b7ed40SBjorn Helgaas 		device->dev.parent = &device->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    -------------------------------------------------------------------------- */
6675c478f49SBjorn Helgaas static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
6685c478f49SBjorn Helgaas {
6695c478f49SBjorn Helgaas 	acpi_status status;
6705c478f49SBjorn Helgaas 	int ret;
6715c478f49SBjorn Helgaas 	struct acpi_device *device;
6725c478f49SBjorn Helgaas 
6735c478f49SBjorn Helgaas 	/*
6745c478f49SBjorn Helgaas 	 * Fixed hardware devices do not appear in the namespace and do not
6755c478f49SBjorn Helgaas 	 * have handles, but we fabricate acpi_devices for them, so we have
6765c478f49SBjorn Helgaas 	 * to deal with them specially.
6775c478f49SBjorn Helgaas 	 */
6785c478f49SBjorn Helgaas 	if (handle == NULL)
6795c478f49SBjorn Helgaas 		return acpi_root;
6805c478f49SBjorn Helgaas 
6815c478f49SBjorn Helgaas 	do {
6825c478f49SBjorn Helgaas 		status = acpi_get_parent(handle, &handle);
6835c478f49SBjorn Helgaas 		if (status == AE_NULL_ENTRY)
6845c478f49SBjorn Helgaas 			return NULL;
6855c478f49SBjorn Helgaas 		if (ACPI_FAILURE(status))
6865c478f49SBjorn Helgaas 			return acpi_root;
6875c478f49SBjorn Helgaas 
6885c478f49SBjorn Helgaas 		ret = acpi_bus_get_device(handle, &device);
6895c478f49SBjorn Helgaas 		if (ret == 0)
6905c478f49SBjorn Helgaas 			return device;
6915c478f49SBjorn Helgaas 	} while (1);
6925c478f49SBjorn Helgaas }
6935c478f49SBjorn Helgaas 
694c8f7a62cSLen Brown acpi_status
695c8f7a62cSLen Brown acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
696c8f7a62cSLen Brown {
697c8f7a62cSLen Brown 	acpi_status status;
698c8f7a62cSLen Brown 	acpi_handle tmp;
699c8f7a62cSLen Brown 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
700c8f7a62cSLen Brown 	union acpi_object *obj;
701c8f7a62cSLen Brown 
702c8f7a62cSLen Brown 	status = acpi_get_handle(handle, "_EJD", &tmp);
703c8f7a62cSLen Brown 	if (ACPI_FAILURE(status))
704c8f7a62cSLen Brown 		return status;
705c8f7a62cSLen Brown 
706c8f7a62cSLen Brown 	status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
707c8f7a62cSLen Brown 	if (ACPI_SUCCESS(status)) {
708c8f7a62cSLen Brown 		obj = buffer.pointer;
7093b5fee59SHolger Macht 		status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
7103b5fee59SHolger Macht 					 ejd);
711c8f7a62cSLen Brown 		kfree(buffer.pointer);
712c8f7a62cSLen Brown 	}
713c8f7a62cSLen Brown 	return status;
714c8f7a62cSLen Brown }
715c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
716c8f7a62cSLen Brown 
7178e4319c4SBob Moore void acpi_bus_data_handler(acpi_handle handle, void *context)
7181da177e4SLinus Torvalds {
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	/* TBD */
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 	return;
7231da177e4SLinus Torvalds }
7241da177e4SLinus Torvalds 
7259e89dde2SZhang Rui static int acpi_bus_get_perf_flags(struct acpi_device *device)
7269e89dde2SZhang Rui {
7279e89dde2SZhang Rui 	device->performance.state = ACPI_STATE_UNKNOWN;
7289e89dde2SZhang Rui 	return 0;
7299e89dde2SZhang Rui }
7309e89dde2SZhang Rui 
7311da177e4SLinus Torvalds static acpi_status
7321da177e4SLinus Torvalds acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
7331da177e4SLinus Torvalds 					     union acpi_object *package)
7341da177e4SLinus Torvalds {
7351da177e4SLinus Torvalds 	int i = 0;
7361da177e4SLinus Torvalds 	union acpi_object *element = NULL;
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 	if (!device || !package || (package->package.count < 2))
7391da177e4SLinus Torvalds 		return AE_BAD_PARAMETER;
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	element = &(package->package.elements[0]);
7421da177e4SLinus Torvalds 	if (!element)
7431da177e4SLinus Torvalds 		return AE_BAD_PARAMETER;
7441da177e4SLinus Torvalds 	if (element->type == ACPI_TYPE_PACKAGE) {
7451da177e4SLinus Torvalds 		if ((element->package.count < 2) ||
7461da177e4SLinus Torvalds 		    (element->package.elements[0].type !=
7471da177e4SLinus Torvalds 		     ACPI_TYPE_LOCAL_REFERENCE)
7481da177e4SLinus Torvalds 		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
7491da177e4SLinus Torvalds 			return AE_BAD_DATA;
7501da177e4SLinus Torvalds 		device->wakeup.gpe_device =
7511da177e4SLinus Torvalds 		    element->package.elements[0].reference.handle;
7521da177e4SLinus Torvalds 		device->wakeup.gpe_number =
7531da177e4SLinus Torvalds 		    (u32) element->package.elements[1].integer.value;
7541da177e4SLinus Torvalds 	} else if (element->type == ACPI_TYPE_INTEGER) {
7551da177e4SLinus Torvalds 		device->wakeup.gpe_number = element->integer.value;
7561da177e4SLinus Torvalds 	} else
7571da177e4SLinus Torvalds 		return AE_BAD_DATA;
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	element = &(package->package.elements[1]);
7601da177e4SLinus Torvalds 	if (element->type != ACPI_TYPE_INTEGER) {
7611da177e4SLinus Torvalds 		return AE_BAD_DATA;
7621da177e4SLinus Torvalds 	}
7631da177e4SLinus Torvalds 	device->wakeup.sleep_state = element->integer.value;
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
7661da177e4SLinus Torvalds 		return AE_NO_MEMORY;
7671da177e4SLinus Torvalds 	}
7681da177e4SLinus Torvalds 	device->wakeup.resources.count = package->package.count - 2;
7691da177e4SLinus Torvalds 	for (i = 0; i < device->wakeup.resources.count; i++) {
7701da177e4SLinus Torvalds 		element = &(package->package.elements[i + 2]);
771cd0b2248SBob Moore 		if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
7721da177e4SLinus Torvalds 			return AE_BAD_DATA;
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 		device->wakeup.resources.handles[i] = element->reference.handle;
7751da177e4SLinus Torvalds 	}
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 	return AE_OK;
7781da177e4SLinus Torvalds }
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
7811da177e4SLinus Torvalds {
7821da177e4SLinus Torvalds 	acpi_status status = 0;
7831da177e4SLinus Torvalds 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
7841da177e4SLinus Torvalds 	union acpi_object *package = NULL;
78577e76609SRafael J. Wysocki 	int psw_error;
7861da177e4SLinus Torvalds 
78729b71a1cSThomas Renninger 	struct acpi_device_id button_device_ids[] = {
78829b71a1cSThomas Renninger 		{"PNP0C0D", 0},
78929b71a1cSThomas Renninger 		{"PNP0C0C", 0},
79029b71a1cSThomas Renninger 		{"PNP0C0E", 0},
79129b71a1cSThomas Renninger 		{"", 0},
79229b71a1cSThomas Renninger 	};
79329b71a1cSThomas Renninger 
7941da177e4SLinus Torvalds 	/* _PRW */
7951da177e4SLinus Torvalds 	status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
7961da177e4SLinus Torvalds 	if (ACPI_FAILURE(status)) {
7971da177e4SLinus Torvalds 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
7981da177e4SLinus Torvalds 		goto end;
7991da177e4SLinus Torvalds 	}
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	package = (union acpi_object *)buffer.pointer;
8021da177e4SLinus Torvalds 	status = acpi_bus_extract_wakeup_device_power_package(device, package);
8031da177e4SLinus Torvalds 	if (ACPI_FAILURE(status)) {
8041da177e4SLinus Torvalds 		ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
8051da177e4SLinus Torvalds 		goto end;
8061da177e4SLinus Torvalds 	}
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 	kfree(buffer.pointer);
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	device->wakeup.flags.valid = 1;
8119b83ccd2SRafael J. Wysocki 	device->wakeup.prepare_count = 0;
812729b2bdbSZhao Yakui 	/* Call _PSW/_DSW object to disable its ability to wake the sleeping
813729b2bdbSZhao Yakui 	 * system for the ACPI device with the _PRW object.
814729b2bdbSZhao Yakui 	 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
815729b2bdbSZhao Yakui 	 * So it is necessary to call _DSW object first. Only when it is not
816729b2bdbSZhao Yakui 	 * present will the _PSW object used.
817729b2bdbSZhao Yakui 	 */
81877e76609SRafael J. Wysocki 	psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
81977e76609SRafael J. Wysocki 	if (psw_error)
82077e76609SRafael J. Wysocki 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
82177e76609SRafael J. Wysocki 				"error in _DSW or _PSW evaluation\n"));
82277e76609SRafael J. Wysocki 
8231da177e4SLinus Torvalds 	/* Power button, Lid switch always enable wakeup */
82429b71a1cSThomas Renninger 	if (!acpi_match_device_ids(device, button_device_ids))
8251da177e4SLinus Torvalds 		device->wakeup.flags.run_wake = 1;
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds end:
8281da177e4SLinus Torvalds 	if (ACPI_FAILURE(status))
8291da177e4SLinus Torvalds 		device->flags.wake_capable = 0;
8301da177e4SLinus Torvalds 	return 0;
8311da177e4SLinus Torvalds }
8321da177e4SLinus Torvalds 
8339e89dde2SZhang Rui static int acpi_bus_get_power_flags(struct acpi_device *device)
8341da177e4SLinus Torvalds {
8359e89dde2SZhang Rui 	acpi_status status = 0;
8369e89dde2SZhang Rui 	acpi_handle handle = NULL;
8379e89dde2SZhang Rui 	u32 i = 0;
8389e89dde2SZhang Rui 
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 	/*
8419e89dde2SZhang Rui 	 * Power Management Flags
8421da177e4SLinus Torvalds 	 */
8439e89dde2SZhang Rui 	status = acpi_get_handle(device->handle, "_PSC", &handle);
8441da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
8459e89dde2SZhang Rui 		device->power.flags.explicit_get = 1;
8469e89dde2SZhang Rui 	status = acpi_get_handle(device->handle, "_IRC", &handle);
8479e89dde2SZhang Rui 	if (ACPI_SUCCESS(status))
8489e89dde2SZhang Rui 		device->power.flags.inrush_current = 1;
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 	/*
8519e89dde2SZhang Rui 	 * Enumerate supported power management states
8521da177e4SLinus Torvalds 	 */
8539e89dde2SZhang Rui 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
8549e89dde2SZhang Rui 		struct acpi_device_power_state *ps = &device->power.states[i];
8559e89dde2SZhang Rui 		char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
8561da177e4SLinus Torvalds 
8579e89dde2SZhang Rui 		/* Evaluate "_PRx" to se if power resources are referenced */
8589e89dde2SZhang Rui 		acpi_evaluate_reference(device->handle, object_name, NULL,
8599e89dde2SZhang Rui 					&ps->resources);
8609e89dde2SZhang Rui 		if (ps->resources.count) {
8619e89dde2SZhang Rui 			device->power.flags.power_resources = 1;
8629e89dde2SZhang Rui 			ps->flags.valid = 1;
8631da177e4SLinus Torvalds 		}
8641da177e4SLinus Torvalds 
8659e89dde2SZhang Rui 		/* Evaluate "_PSx" to see if we can do explicit sets */
8669e89dde2SZhang Rui 		object_name[2] = 'S';
8679e89dde2SZhang Rui 		status = acpi_get_handle(device->handle, object_name, &handle);
868c8f7a62cSLen Brown 		if (ACPI_SUCCESS(status)) {
8699e89dde2SZhang Rui 			ps->flags.explicit_set = 1;
8709e89dde2SZhang Rui 			ps->flags.valid = 1;
871c8f7a62cSLen Brown 		}
872c8f7a62cSLen Brown 
8739e89dde2SZhang Rui 		/* State is valid if we have some power control */
8749e89dde2SZhang Rui 		if (ps->resources.count || ps->flags.explicit_set)
8759e89dde2SZhang Rui 			ps->flags.valid = 1;
8769e89dde2SZhang Rui 
8779e89dde2SZhang Rui 		ps->power = -1;	/* Unknown - driver assigned */
8789e89dde2SZhang Rui 		ps->latency = -1;	/* Unknown - driver assigned */
8799e89dde2SZhang Rui 	}
8809e89dde2SZhang Rui 
8819e89dde2SZhang Rui 	/* Set defaults for D0 and D3 states (always valid) */
8829e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].flags.valid = 1;
8839e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].power = 100;
8849e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D3].flags.valid = 1;
8859e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D3].power = 0;
8869e89dde2SZhang Rui 
8879e89dde2SZhang Rui 	/* TBD: System wake support and resource requirements. */
8889e89dde2SZhang Rui 
8899e89dde2SZhang Rui 	device->power.state = ACPI_STATE_UNKNOWN;
890a51e145fSZhao Yakui 	acpi_bus_get_power(device->handle, &(device->power.state));
8919e89dde2SZhang Rui 
8929e89dde2SZhang Rui 	return 0;
8939e89dde2SZhang Rui }
894c8f7a62cSLen Brown 
8954be44fcdSLen Brown static int acpi_bus_get_flags(struct acpi_device *device)
8961da177e4SLinus Torvalds {
8971da177e4SLinus Torvalds 	acpi_status status = AE_OK;
8981da177e4SLinus Torvalds 	acpi_handle temp = NULL;
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	/* Presence of _STA indicates 'dynamic_status' */
9021da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_STA", &temp);
9031da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
9041da177e4SLinus Torvalds 		device->flags.dynamic_status = 1;
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 	/* Presence of _CID indicates 'compatible_ids' */
9071da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_CID", &temp);
9081da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
9091da177e4SLinus Torvalds 		device->flags.compatible_ids = 1;
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	/* Presence of _RMV indicates 'removable' */
9121da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_RMV", &temp);
9131da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
9141da177e4SLinus Torvalds 		device->flags.removable = 1;
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds 	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
9171da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_EJD", &temp);
9181da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
9191da177e4SLinus Torvalds 		device->flags.ejectable = 1;
9201da177e4SLinus Torvalds 	else {
9211da177e4SLinus Torvalds 		status = acpi_get_handle(device->handle, "_EJ0", &temp);
9221da177e4SLinus Torvalds 		if (ACPI_SUCCESS(status))
9231da177e4SLinus Torvalds 			device->flags.ejectable = 1;
9241da177e4SLinus Torvalds 	}
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	/* Presence of _LCK indicates 'lockable' */
9271da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_LCK", &temp);
9281da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
9291da177e4SLinus Torvalds 		device->flags.lockable = 1;
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	/* Presence of _PS0|_PR0 indicates 'power manageable' */
9321da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_PS0", &temp);
9331da177e4SLinus Torvalds 	if (ACPI_FAILURE(status))
9341da177e4SLinus Torvalds 		status = acpi_get_handle(device->handle, "_PR0", &temp);
9351da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
9361da177e4SLinus Torvalds 		device->flags.power_manageable = 1;
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds 	/* Presence of _PRW indicates wake capable */
9391da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_PRW", &temp);
9401da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
9411da177e4SLinus Torvalds 		device->flags.wake_capable = 1;
9421da177e4SLinus Torvalds 
9433c5f9be4SJoe Perches 	/* TBD: Performance management */
9441da177e4SLinus Torvalds 
945d550d98dSPatrick Mochel 	return 0;
9461da177e4SLinus Torvalds }
9471da177e4SLinus Torvalds 
948c7bcb4e9SBjorn Helgaas static void acpi_device_get_busid(struct acpi_device *device)
9491da177e4SLinus Torvalds {
9501da177e4SLinus Torvalds 	char bus_id[5] = { '?', 0 };
9511da177e4SLinus Torvalds 	struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
9521da177e4SLinus Torvalds 	int i = 0;
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 	/*
9551da177e4SLinus Torvalds 	 * Bus ID
9561da177e4SLinus Torvalds 	 * ------
9571da177e4SLinus Torvalds 	 * The device's Bus ID is simply the object name.
9581da177e4SLinus Torvalds 	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
9591da177e4SLinus Torvalds 	 */
960*859ac9a4SBjorn Helgaas 	if (ACPI_IS_ROOT_DEVICE(device)) {
9611da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "ACPI");
962*859ac9a4SBjorn Helgaas 		return;
963*859ac9a4SBjorn Helgaas 	}
964*859ac9a4SBjorn Helgaas 
965*859ac9a4SBjorn Helgaas 	switch (device->device_type) {
9661da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
9671da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "PWRF");
9681da177e4SLinus Torvalds 		break;
9691da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
9701da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "SLPF");
9711da177e4SLinus Torvalds 		break;
9721da177e4SLinus Torvalds 	default:
97366b7ed40SBjorn Helgaas 		acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
9741da177e4SLinus Torvalds 		/* Clean up trailing underscores (if any) */
9751da177e4SLinus Torvalds 		for (i = 3; i > 1; i--) {
9761da177e4SLinus Torvalds 			if (bus_id[i] == '_')
9771da177e4SLinus Torvalds 				bus_id[i] = '\0';
9781da177e4SLinus Torvalds 			else
9791da177e4SLinus Torvalds 				break;
9801da177e4SLinus Torvalds 		}
9811da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, bus_id);
9821da177e4SLinus Torvalds 		break;
9831da177e4SLinus Torvalds 	}
9841da177e4SLinus Torvalds }
9851da177e4SLinus Torvalds 
98654735266SZhang Rui /*
98754735266SZhang Rui  * acpi_bay_match - see if a device is an ejectable driver bay
98854735266SZhang Rui  *
98954735266SZhang Rui  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
99054735266SZhang Rui  * then we can safely call it an ejectable drive bay
99154735266SZhang Rui  */
99254735266SZhang Rui static int acpi_bay_match(struct acpi_device *device){
99354735266SZhang Rui 	acpi_status status;
99454735266SZhang Rui 	acpi_handle handle;
99554735266SZhang Rui 	acpi_handle tmp;
99654735266SZhang Rui 	acpi_handle phandle;
99754735266SZhang Rui 
99854735266SZhang Rui 	handle = device->handle;
99954735266SZhang Rui 
100054735266SZhang Rui 	status = acpi_get_handle(handle, "_EJ0", &tmp);
100154735266SZhang Rui 	if (ACPI_FAILURE(status))
100254735266SZhang Rui 		return -ENODEV;
100354735266SZhang Rui 
100454735266SZhang Rui 	if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
100554735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
100654735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
100754735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
100854735266SZhang Rui 		return 0;
100954735266SZhang Rui 
101054735266SZhang Rui 	if (acpi_get_parent(handle, &phandle))
101154735266SZhang Rui 		return -ENODEV;
101254735266SZhang Rui 
101354735266SZhang Rui         if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
101454735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
101554735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
101654735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
101754735266SZhang Rui                 return 0;
101854735266SZhang Rui 
101954735266SZhang Rui 	return -ENODEV;
102054735266SZhang Rui }
102154735266SZhang Rui 
10223620f2f2SFrank Seidel /*
10233620f2f2SFrank Seidel  * acpi_dock_match - see if a device has a _DCK method
10243620f2f2SFrank Seidel  */
10253620f2f2SFrank Seidel static int acpi_dock_match(struct acpi_device *device)
10263620f2f2SFrank Seidel {
10273620f2f2SFrank Seidel 	acpi_handle tmp;
10283620f2f2SFrank Seidel 	return acpi_get_handle(device->handle, "_DCK", &tmp);
10293620f2f2SFrank Seidel }
10303620f2f2SFrank Seidel 
103115b8dd53SBob Moore static struct acpica_device_id_list*
103215b8dd53SBob Moore acpi_add_cid(
103315b8dd53SBob Moore 	struct acpi_device_info         *info,
103415b8dd53SBob Moore 	struct acpica_device_id         *new_cid)
103515b8dd53SBob Moore {
103615b8dd53SBob Moore 	struct acpica_device_id_list    *cid;
103715b8dd53SBob Moore 	char                            *next_id_string;
103815b8dd53SBob Moore 	acpi_size                       cid_length;
103915b8dd53SBob Moore 	acpi_size                       new_cid_length;
104015b8dd53SBob Moore 	u32                             i;
104115b8dd53SBob Moore 
104215b8dd53SBob Moore 
104315b8dd53SBob Moore 	/* Allocate new CID list with room for the new CID */
104415b8dd53SBob Moore 
104515b8dd53SBob Moore 	if (!new_cid)
104615b8dd53SBob Moore 		new_cid_length = info->compatible_id_list.list_size;
104715b8dd53SBob Moore 	else if (info->compatible_id_list.list_size)
104815b8dd53SBob Moore 		new_cid_length = info->compatible_id_list.list_size +
104915b8dd53SBob Moore 			new_cid->length + sizeof(struct acpica_device_id);
105015b8dd53SBob Moore 	else
105115b8dd53SBob Moore 		new_cid_length = sizeof(struct acpica_device_id_list) + new_cid->length;
105215b8dd53SBob Moore 
105315b8dd53SBob Moore 	cid = ACPI_ALLOCATE_ZEROED(new_cid_length);
105415b8dd53SBob Moore 	if (!cid) {
105515b8dd53SBob Moore 		return NULL;
105615b8dd53SBob Moore 	}
105715b8dd53SBob Moore 
105815b8dd53SBob Moore 	cid->list_size = new_cid_length;
105915b8dd53SBob Moore 	cid->count = info->compatible_id_list.count;
106015b8dd53SBob Moore 	if (new_cid)
106115b8dd53SBob Moore 		cid->count++;
106215b8dd53SBob Moore 	next_id_string = (char *) cid->ids + (cid->count * sizeof(struct acpica_device_id));
106315b8dd53SBob Moore 
106415b8dd53SBob Moore 	/* Copy all existing CIDs */
106515b8dd53SBob Moore 
106615b8dd53SBob Moore 	for (i = 0; i < info->compatible_id_list.count; i++) {
106715b8dd53SBob Moore 		cid_length = info->compatible_id_list.ids[i].length;
106815b8dd53SBob Moore 		cid->ids[i].string = next_id_string;
106915b8dd53SBob Moore 		cid->ids[i].length = cid_length;
107015b8dd53SBob Moore 
107115b8dd53SBob Moore 		ACPI_MEMCPY(next_id_string, info->compatible_id_list.ids[i].string,
107215b8dd53SBob Moore 			cid_length);
107315b8dd53SBob Moore 
107415b8dd53SBob Moore 		next_id_string += cid_length;
107515b8dd53SBob Moore 	}
107615b8dd53SBob Moore 
107715b8dd53SBob Moore 	/* Append the new CID */
107815b8dd53SBob Moore 
107915b8dd53SBob Moore 	if (new_cid) {
108015b8dd53SBob Moore 		cid->ids[i].string = next_id_string;
108115b8dd53SBob Moore 		cid->ids[i].length = new_cid->length;
108215b8dd53SBob Moore 
108315b8dd53SBob Moore 		ACPI_MEMCPY(next_id_string, new_cid->string, new_cid->length);
108415b8dd53SBob Moore 	}
108515b8dd53SBob Moore 
108615b8dd53SBob Moore 	return cid;
108715b8dd53SBob Moore }
108815b8dd53SBob Moore 
1089c7bcb4e9SBjorn Helgaas static void acpi_device_set_id(struct acpi_device *device)
10901da177e4SLinus Torvalds {
109115b8dd53SBob Moore 	struct acpi_device_info *info = NULL;
10921da177e4SLinus Torvalds 	char *hid = NULL;
10931da177e4SLinus Torvalds 	char *uid = NULL;
109415b8dd53SBob Moore 	struct acpica_device_id_list *cid_list = NULL;
109515b8dd53SBob Moore 	char *cid_add = NULL;
10961da177e4SLinus Torvalds 	acpi_status status;
10971da177e4SLinus Torvalds 
1098c7bcb4e9SBjorn Helgaas 	switch (device->device_type) {
10991da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
1100*859ac9a4SBjorn Helgaas 		if (ACPI_IS_ROOT_DEVICE(device)) {
1101*859ac9a4SBjorn Helgaas 			hid = ACPI_SYSTEM_HID;
1102*859ac9a4SBjorn Helgaas 			break;
1103*859ac9a4SBjorn Helgaas 		}
1104*859ac9a4SBjorn Helgaas 
110566b7ed40SBjorn Helgaas 		status = acpi_get_object_info(device->handle, &info);
11061da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
110796b2dd1fSHarvey Harrison 			printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
11081da177e4SLinus Torvalds 			return;
11091da177e4SLinus Torvalds 		}
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_HID)
111215b8dd53SBob Moore 			hid = info->hardware_id.string;
11131da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_UID)
111415b8dd53SBob Moore 			uid = info->unique_id.string;
11151da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_CID)
111615b8dd53SBob Moore 			cid_list = &info->compatible_id_list;
11171da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_ADR) {
11181da177e4SLinus Torvalds 			device->pnp.bus_address = info->address;
11191da177e4SLinus Torvalds 			device->flags.bus_address = 1;
11201da177e4SLinus Torvalds 		}
1121ae843332SZhang Rui 
11223620f2f2SFrank Seidel 		/* If we have a video/bay/dock device, add our selfdefined
11233620f2f2SFrank Seidel 		   HID to the CID list. Like that the video/bay/dock drivers
11243620f2f2SFrank Seidel 		   will get autoloaded and the device might still match
11253620f2f2SFrank Seidel 		   against another driver.
11263620f2f2SFrank Seidel 		*/
1127c3d6de69SThomas Renninger 		if (acpi_is_video_device(device))
11283620f2f2SFrank Seidel 			cid_add = ACPI_VIDEO_HID;
11293620f2f2SFrank Seidel 		else if (ACPI_SUCCESS(acpi_bay_match(device)))
11303620f2f2SFrank Seidel 			cid_add = ACPI_BAY_HID;
11313620f2f2SFrank Seidel 		else if (ACPI_SUCCESS(acpi_dock_match(device)))
11323620f2f2SFrank Seidel 			cid_add = ACPI_DOCK_HID;
113354735266SZhang Rui 
11341da177e4SLinus Torvalds 		break;
11351da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER:
11361da177e4SLinus Torvalds 		hid = ACPI_POWER_HID;
11371da177e4SLinus Torvalds 		break;
11381da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_PROCESSOR:
1139ad93a765SMyron Stowe 		hid = ACPI_PROCESSOR_OBJECT_HID;
11401da177e4SLinus Torvalds 		break;
11411da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_THERMAL:
11421da177e4SLinus Torvalds 		hid = ACPI_THERMAL_HID;
11431da177e4SLinus Torvalds 		break;
11441da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
11451da177e4SLinus Torvalds 		hid = ACPI_BUTTON_HID_POWERF;
11461da177e4SLinus Torvalds 		break;
11471da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
11481da177e4SLinus Torvalds 		hid = ACPI_BUTTON_HID_SLEEPF;
11491da177e4SLinus Torvalds 		break;
11501da177e4SLinus Torvalds 	}
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds 	/*
11531da177e4SLinus Torvalds 	 * \_SB
11541da177e4SLinus Torvalds 	 * ----
11551da177e4SLinus Torvalds 	 * Fix for the system root bus device -- the only root-level device.
11561da177e4SLinus Torvalds 	 */
115766b7ed40SBjorn Helgaas 	if (((acpi_handle)device->parent == ACPI_ROOT_OBJECT) &&
1158c7bcb4e9SBjorn Helgaas 	     (device->device_type == ACPI_BUS_TYPE_DEVICE)) {
11591da177e4SLinus Torvalds 		hid = ACPI_BUS_HID;
11601da177e4SLinus Torvalds 		strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
11611da177e4SLinus Torvalds 		strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
11621da177e4SLinus Torvalds 	}
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 	if (hid) {
116515b8dd53SBob Moore 		device->pnp.hardware_id = ACPI_ALLOCATE_ZEROED(strlen (hid) + 1);
116615b8dd53SBob Moore 		if (device->pnp.hardware_id) {
11671da177e4SLinus Torvalds 			strcpy(device->pnp.hardware_id, hid);
11681da177e4SLinus Torvalds 			device->flags.hardware_id = 1;
11691da177e4SLinus Torvalds 		}
1170718fb0deSHugh Dickins 	}
1171718fb0deSHugh Dickins 	if (!device->flags.hardware_id)
1172718fb0deSHugh Dickins 		device->pnp.hardware_id = "";
117315b8dd53SBob Moore 
11741da177e4SLinus Torvalds 	if (uid) {
117515b8dd53SBob Moore 		device->pnp.unique_id = ACPI_ALLOCATE_ZEROED(strlen (uid) + 1);
117615b8dd53SBob Moore 		if (device->pnp.unique_id) {
11771da177e4SLinus Torvalds 			strcpy(device->pnp.unique_id, uid);
11781da177e4SLinus Torvalds 			device->flags.unique_id = 1;
11791da177e4SLinus Torvalds 		}
1180718fb0deSHugh Dickins 	}
1181718fb0deSHugh Dickins 	if (!device->flags.unique_id)
1182718fb0deSHugh Dickins 		device->pnp.unique_id = "";
11833620f2f2SFrank Seidel 
118415b8dd53SBob Moore 	if (cid_list || cid_add) {
118515b8dd53SBob Moore 		struct acpica_device_id_list *list;
118615b8dd53SBob Moore 
118715b8dd53SBob Moore 		if (cid_add) {
118815b8dd53SBob Moore 			struct acpica_device_id cid;
118915b8dd53SBob Moore 			cid.length = strlen (cid_add) + 1;
119015b8dd53SBob Moore 			cid.string = cid_add;
119115b8dd53SBob Moore 
119215b8dd53SBob Moore 			list = acpi_add_cid(info, &cid);
11933620f2f2SFrank Seidel 		} else {
119415b8dd53SBob Moore 			list = acpi_add_cid(info, NULL);
11953620f2f2SFrank Seidel 		}
11963620f2f2SFrank Seidel 
11973620f2f2SFrank Seidel 		if (list) {
119815b8dd53SBob Moore 			device->pnp.cid_list = list;
119915b8dd53SBob Moore 			if (cid_add)
12003620f2f2SFrank Seidel 				device->flags.compatible_ids = 1;
12013620f2f2SFrank Seidel 		}
12021da177e4SLinus Torvalds 	}
12031da177e4SLinus Torvalds 
120415b8dd53SBob Moore 	kfree(info);
12051da177e4SLinus Torvalds }
12061da177e4SLinus Torvalds 
1207bc3b0772SBjorn Helgaas static int acpi_device_set_context(struct acpi_device *device)
12081da177e4SLinus Torvalds {
1209bc3b0772SBjorn Helgaas 	acpi_status status;
1210bc3b0772SBjorn Helgaas 
12111da177e4SLinus Torvalds 	/*
12121da177e4SLinus Torvalds 	 * Context
12131da177e4SLinus Torvalds 	 * -------
12141da177e4SLinus Torvalds 	 * Attach this 'struct acpi_device' to the ACPI object.  This makes
1215bc3b0772SBjorn Helgaas 	 * resolutions from handle->device very efficient.  Fixed hardware
1216bc3b0772SBjorn Helgaas 	 * devices have no handles, so we skip them.
12171da177e4SLinus Torvalds 	 */
1218bc3b0772SBjorn Helgaas 	if (!device->handle)
1219bc3b0772SBjorn Helgaas 		return 0;
1220bc3b0772SBjorn Helgaas 
12211da177e4SLinus Torvalds 	status = acpi_attach_data(device->handle,
12221da177e4SLinus Torvalds 				  acpi_bus_data_handler, device);
1223bc3b0772SBjorn Helgaas 	if (ACPI_SUCCESS(status))
1224bc3b0772SBjorn Helgaas 		return 0;
12251da177e4SLinus Torvalds 
122687ecd5cdSLen Brown 	printk(KERN_ERR PREFIX "Error attaching device data\n");
1227bc3b0772SBjorn Helgaas 	return -ENODEV;
12281da177e4SLinus Torvalds }
12291da177e4SLinus Torvalds 
12304be44fcdSLen Brown static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
12311da177e4SLinus Torvalds {
12321da177e4SLinus Torvalds 	if (!dev)
1233d550d98dSPatrick Mochel 		return -EINVAL;
12341da177e4SLinus Torvalds 
123596333578SLi Shaohua 	dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
12361890a97aSPatrick Mochel 	device_release_driver(&dev->dev);
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds 	if (!rmdevice)
1239d550d98dSPatrick Mochel 		return 0;
12401da177e4SLinus Torvalds 
12412786f6e3SRui Zhang 	/*
12422786f6e3SRui Zhang 	 * unbind _ADR-Based Devices when hot removal
12432786f6e3SRui Zhang 	 */
12441da177e4SLinus Torvalds 	if (dev->flags.bus_address) {
12451da177e4SLinus Torvalds 		if ((dev->parent) && (dev->parent->ops.unbind))
12461da177e4SLinus Torvalds 			dev->parent->ops.unbind(dev);
12471da177e4SLinus Torvalds 	}
12481da177e4SLinus Torvalds 	acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
12491da177e4SLinus Torvalds 
1250d550d98dSPatrick Mochel 	return 0;
12511da177e4SLinus Torvalds }
12521da177e4SLinus Torvalds 
12535c478f49SBjorn Helgaas static int acpi_add_single_object(struct acpi_device **child,
12545c478f49SBjorn Helgaas 				  acpi_handle handle, int type,
1255c4168bffSLi Shaohua 				  struct acpi_bus_ops *ops)
12561da177e4SLinus Torvalds {
125777c24888SBjorn Helgaas 	int result;
125877c24888SBjorn Helgaas 	struct acpi_device *device;
125929aaefa6SBjorn Helgaas 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
12601da177e4SLinus Torvalds 
126136bcbec7SBurman Yan 	device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
12621da177e4SLinus Torvalds 	if (!device) {
12636468463aSLen Brown 		printk(KERN_ERR PREFIX "Memory allocation error\n");
1264d550d98dSPatrick Mochel 		return -ENOMEM;
12651da177e4SLinus Torvalds 	}
12661da177e4SLinus Torvalds 
1267caaa6efbSBjorn Helgaas 	device->device_type = type;
12681da177e4SLinus Torvalds 	device->handle = handle;
12695c478f49SBjorn Helgaas 	device->parent = acpi_bus_get_parent(handle);
1270c4168bffSLi Shaohua 	device->bus_ops = *ops; /* workround for not call .start */
1271c4168bffSLi Shaohua 
1272c7bcb4e9SBjorn Helgaas 	acpi_device_get_busid(device);
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 	/*
12751da177e4SLinus Torvalds 	 * Flags
12761da177e4SLinus Torvalds 	 * -----
12771da177e4SLinus Torvalds 	 * Get prior to calling acpi_bus_get_status() so we know whether
12781da177e4SLinus Torvalds 	 * or not _STA is present.  Note that we only look for object
12791da177e4SLinus Torvalds 	 * handles -- cannot evaluate objects until we know the device is
12801da177e4SLinus Torvalds 	 * present and properly initialized.
12811da177e4SLinus Torvalds 	 */
12821da177e4SLinus Torvalds 	result = acpi_bus_get_flags(device);
12831da177e4SLinus Torvalds 	if (result)
12841da177e4SLinus Torvalds 		goto end;
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 	/*
12871da177e4SLinus Torvalds 	 * Status
12881da177e4SLinus Torvalds 	 * ------
12896940fabaSKeiichiro Tokunaga 	 * See if the device is present.  We always assume that non-Device
12906940fabaSKeiichiro Tokunaga 	 * and non-Processor objects (e.g. thermal zones, power resources,
12916940fabaSKeiichiro Tokunaga 	 * etc.) are present, functioning, etc. (at least when parent object
12926940fabaSKeiichiro Tokunaga 	 * is present).  Note that _STA has a different meaning for some
12936940fabaSKeiichiro Tokunaga 	 * objects (e.g. power resources) so we need to be careful how we use
12946940fabaSKeiichiro Tokunaga 	 * it.
12951da177e4SLinus Torvalds 	 */
12961da177e4SLinus Torvalds 	switch (type) {
12976940fabaSKeiichiro Tokunaga 	case ACPI_BUS_TYPE_PROCESSOR:
12981da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
12991da177e4SLinus Torvalds 		result = acpi_bus_get_status(device);
13003620f2f2SFrank Seidel 		if (ACPI_FAILURE(result)) {
13013620f2f2SFrank Seidel 			result = -ENODEV;
13021da177e4SLinus Torvalds 			goto end;
13031da177e4SLinus Torvalds 		}
130439a0ad87SZhao Yakui 		/*
130539a0ad87SZhao Yakui 		 * When the device is neither present nor functional, the
130639a0ad87SZhao Yakui 		 * device should not be added to Linux ACPI device tree.
130739a0ad87SZhao Yakui 		 * When the status of the device is not present but functinal,
130839a0ad87SZhao Yakui 		 * it should be added to Linux ACPI tree. For example : bay
130939a0ad87SZhao Yakui 		 * device , dock device.
131039a0ad87SZhao Yakui 		 * In such conditions it is unncessary to check whether it is
131139a0ad87SZhao Yakui 		 * bay device or dock device.
131239a0ad87SZhao Yakui 		 */
131339a0ad87SZhao Yakui 		if (!device->status.present && !device->status.functional) {
13143620f2f2SFrank Seidel 			result = -ENODEV;
13153620f2f2SFrank Seidel 			goto end;
13163620f2f2SFrank Seidel 		}
13171da177e4SLinus Torvalds 		break;
13181da177e4SLinus Torvalds 	default:
13190c0e8921SBjorn Helgaas 		STRUCT_TO_INT(device->status) =
13200c0e8921SBjorn Helgaas 		    ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
13210c0e8921SBjorn Helgaas 		    ACPI_STA_DEVICE_UI      | ACPI_STA_DEVICE_FUNCTIONING;
13221da177e4SLinus Torvalds 		break;
13231da177e4SLinus Torvalds 	}
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 	/*
13261da177e4SLinus Torvalds 	 * Initialize Device
13271da177e4SLinus Torvalds 	 * -----------------
13281da177e4SLinus Torvalds 	 * TBD: Synch with Core's enumeration/initialization process.
13291da177e4SLinus Torvalds 	 */
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds 	/*
13321da177e4SLinus Torvalds 	 * Hardware ID, Unique ID, & Bus Address
13331da177e4SLinus Torvalds 	 * -------------------------------------
13341da177e4SLinus Torvalds 	 */
1335c7bcb4e9SBjorn Helgaas 	acpi_device_set_id(device);
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds 	/*
13381da177e4SLinus Torvalds 	 * Power Management
13391da177e4SLinus Torvalds 	 * ----------------
13401da177e4SLinus Torvalds 	 */
13411da177e4SLinus Torvalds 	if (device->flags.power_manageable) {
13421da177e4SLinus Torvalds 		result = acpi_bus_get_power_flags(device);
13431da177e4SLinus Torvalds 		if (result)
13441da177e4SLinus Torvalds 			goto end;
13451da177e4SLinus Torvalds 	}
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds 	/*
13481da177e4SLinus Torvalds 	 * Wakeup device management
13491da177e4SLinus Torvalds 	 *-----------------------
13501da177e4SLinus Torvalds 	 */
13511da177e4SLinus Torvalds 	if (device->flags.wake_capable) {
13521da177e4SLinus Torvalds 		result = acpi_bus_get_wakeup_device_flags(device);
13531da177e4SLinus Torvalds 		if (result)
13541da177e4SLinus Torvalds 			goto end;
13551da177e4SLinus Torvalds 	}
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds 	/*
13581da177e4SLinus Torvalds 	 * Performance Management
13591da177e4SLinus Torvalds 	 * ----------------------
13601da177e4SLinus Torvalds 	 */
13611da177e4SLinus Torvalds 	if (device->flags.performance_manageable) {
13621da177e4SLinus Torvalds 		result = acpi_bus_get_perf_flags(device);
13631da177e4SLinus Torvalds 		if (result)
13641da177e4SLinus Torvalds 			goto end;
13651da177e4SLinus Torvalds 	}
13661da177e4SLinus Torvalds 
1367bc3b0772SBjorn Helgaas 	if ((result = acpi_device_set_context(device)))
1368f61f9258SLen Brown 		goto end;
13691da177e4SLinus Torvalds 
137066b7ed40SBjorn Helgaas 	result = acpi_device_register(device);
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds 	/*
13732786f6e3SRui Zhang 	 * Bind _ADR-Based Devices when hot add
13741da177e4SLinus Torvalds 	 */
13751da177e4SLinus Torvalds 	if (device->flags.bus_address) {
13761da177e4SLinus Torvalds 		if (device->parent && device->parent->ops.bind)
13771da177e4SLinus Torvalds 			device->parent->ops.bind(device);
13781da177e4SLinus Torvalds 	}
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds end:
138129aaefa6SBjorn Helgaas 	if (!result) {
138229aaefa6SBjorn Helgaas 		acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
138329aaefa6SBjorn Helgaas 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
138429aaefa6SBjorn Helgaas 			"Adding %s [%s] parent %s\n", dev_name(&device->dev),
138529aaefa6SBjorn Helgaas 			 (char *) buffer.pointer,
138629aaefa6SBjorn Helgaas 			 device->parent ? dev_name(&device->parent->dev) :
138729aaefa6SBjorn Helgaas 					  "(null)"));
138829aaefa6SBjorn Helgaas 		kfree(buffer.pointer);
13891da177e4SLinus Torvalds 		*child = device;
139029aaefa6SBjorn Helgaas 	} else
1391718fb0deSHugh Dickins 		acpi_device_release(&device->dev);
13921da177e4SLinus Torvalds 
1393d550d98dSPatrick Mochel 	return result;
13941da177e4SLinus Torvalds }
13951da177e4SLinus Torvalds 
13968e029bf0SBjorn Helgaas static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops)
13971da177e4SLinus Torvalds {
13981da177e4SLinus Torvalds 	acpi_status status = AE_OK;
13991da177e4SLinus Torvalds 	struct acpi_device *parent = NULL;
14001da177e4SLinus Torvalds 	struct acpi_device *child = NULL;
14011da177e4SLinus Torvalds 	acpi_handle phandle = NULL;
14021da177e4SLinus Torvalds 	acpi_handle chandle = NULL;
14031da177e4SLinus Torvalds 	acpi_object_type type = 0;
14041da177e4SLinus Torvalds 	u32 level = 1;
14058e029bf0SBjorn Helgaas 	int ret;
14061da177e4SLinus Torvalds 
14078e029bf0SBjorn Helgaas 	/*
14088e029bf0SBjorn Helgaas 	 * We must have an acpi_device for the starting node already, and
14098e029bf0SBjorn Helgaas 	 * we scan its children.
14108e029bf0SBjorn Helgaas 	 */
14118e029bf0SBjorn Helgaas 	phandle = handle;
14128e029bf0SBjorn Helgaas 	ret = acpi_bus_get_device(phandle, &parent);
14138e029bf0SBjorn Helgaas 	if (ret)
14148e029bf0SBjorn Helgaas 		return ret;
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds 	/*
14171da177e4SLinus Torvalds 	 * Parse through the ACPI namespace, identify all 'devices', and
14181da177e4SLinus Torvalds 	 * create a new 'struct acpi_device' for each.
14191da177e4SLinus Torvalds 	 */
14201da177e4SLinus Torvalds 	while ((level > 0) && parent) {
14211da177e4SLinus Torvalds 
14221da177e4SLinus Torvalds 		status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
14231da177e4SLinus Torvalds 					      chandle, &chandle);
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds 		/*
14261da177e4SLinus Torvalds 		 * If this scope is exhausted then move our way back up.
14271da177e4SLinus Torvalds 		 */
14281da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
14291da177e4SLinus Torvalds 			level--;
14301da177e4SLinus Torvalds 			chandle = phandle;
14311da177e4SLinus Torvalds 			acpi_get_parent(phandle, &phandle);
14321da177e4SLinus Torvalds 			if (parent->parent)
14331da177e4SLinus Torvalds 				parent = parent->parent;
14341da177e4SLinus Torvalds 			continue;
14351da177e4SLinus Torvalds 		}
14361da177e4SLinus Torvalds 
14371da177e4SLinus Torvalds 		status = acpi_get_type(chandle, &type);
14381da177e4SLinus Torvalds 		if (ACPI_FAILURE(status))
14391da177e4SLinus Torvalds 			continue;
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds 		/*
14421da177e4SLinus Torvalds 		 * If this is a scope object then parse it (depth-first).
14431da177e4SLinus Torvalds 		 */
14441da177e4SLinus Torvalds 		if (type == ACPI_TYPE_LOCAL_SCOPE) {
14451da177e4SLinus Torvalds 			level++;
14461da177e4SLinus Torvalds 			phandle = chandle;
14471da177e4SLinus Torvalds 			chandle = NULL;
14481da177e4SLinus Torvalds 			continue;
14491da177e4SLinus Torvalds 		}
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds 		/*
14521da177e4SLinus Torvalds 		 * We're only interested in objects that we consider 'devices'.
14531da177e4SLinus Torvalds 		 */
14541da177e4SLinus Torvalds 		switch (type) {
14551da177e4SLinus Torvalds 		case ACPI_TYPE_DEVICE:
14561da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_DEVICE;
14571da177e4SLinus Torvalds 			break;
14581da177e4SLinus Torvalds 		case ACPI_TYPE_PROCESSOR:
14591da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_PROCESSOR;
14601da177e4SLinus Torvalds 			break;
14611da177e4SLinus Torvalds 		case ACPI_TYPE_THERMAL:
14621da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_THERMAL;
14631da177e4SLinus Torvalds 			break;
14641da177e4SLinus Torvalds 		case ACPI_TYPE_POWER:
14651da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_POWER;
14661da177e4SLinus Torvalds 			break;
14671da177e4SLinus Torvalds 		default:
14681da177e4SLinus Torvalds 			continue;
14691da177e4SLinus Torvalds 		}
14701da177e4SLinus Torvalds 
14713fb02738SRajesh Shah 		if (ops->acpi_op_add)
14725c478f49SBjorn Helgaas 			status = acpi_add_single_object(&child, chandle, type,
14735c478f49SBjorn Helgaas 							ops);
14743fb02738SRajesh Shah 		else
14753fb02738SRajesh Shah 			status = acpi_bus_get_device(chandle, &child);
14763fb02738SRajesh Shah 
14771da177e4SLinus Torvalds 		if (ACPI_FAILURE(status))
14781da177e4SLinus Torvalds 			continue;
14791da177e4SLinus Torvalds 
1480c4168bffSLi Shaohua 		if (ops->acpi_op_start && !(ops->acpi_op_add)) {
14813fb02738SRajesh Shah 			status = acpi_start_single_object(child);
14823fb02738SRajesh Shah 			if (ACPI_FAILURE(status))
14833fb02738SRajesh Shah 				continue;
14843fb02738SRajesh Shah 		}
14853fb02738SRajesh Shah 
14861da177e4SLinus Torvalds 		/*
14871da177e4SLinus Torvalds 		 * If the device is present, enabled, and functioning then
14881da177e4SLinus Torvalds 		 * parse its scope (depth-first).  Note that we need to
14891da177e4SLinus Torvalds 		 * represent absent devices to facilitate PnP notifications
14901da177e4SLinus Torvalds 		 * -- but only the subtree head (not all of its children,
14911da177e4SLinus Torvalds 		 * which will be enumerated when the parent is inserted).
14921da177e4SLinus Torvalds 		 *
14931da177e4SLinus Torvalds 		 * TBD: Need notifications and other detection mechanisms
14941da177e4SLinus Torvalds 		 *      in place before we can fully implement this.
14951da177e4SLinus Torvalds 		 */
149639a0ad87SZhao Yakui 		 /*
149739a0ad87SZhao Yakui 		 * When the device is not present but functional, it is also
149839a0ad87SZhao Yakui 		 * necessary to scan the children of this device.
149939a0ad87SZhao Yakui 		 */
150039a0ad87SZhao Yakui 		if (child->status.present || (!child->status.present &&
150139a0ad87SZhao Yakui 					child->status.functional)) {
15021da177e4SLinus Torvalds 			status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
15031da177e4SLinus Torvalds 						      NULL, NULL);
15041da177e4SLinus Torvalds 			if (ACPI_SUCCESS(status)) {
15051da177e4SLinus Torvalds 				level++;
15061da177e4SLinus Torvalds 				phandle = chandle;
15071da177e4SLinus Torvalds 				chandle = NULL;
15081da177e4SLinus Torvalds 				parent = child;
15091da177e4SLinus Torvalds 			}
15101da177e4SLinus Torvalds 		}
15111da177e4SLinus Torvalds 	}
15121da177e4SLinus Torvalds 
1513d550d98dSPatrick Mochel 	return 0;
15141da177e4SLinus Torvalds }
15151da177e4SLinus Torvalds 
15163fb02738SRajesh Shah int
15174be44fcdSLen Brown acpi_bus_add(struct acpi_device **child,
15184be44fcdSLen Brown 	     struct acpi_device *parent, acpi_handle handle, int type)
15193fb02738SRajesh Shah {
15203fb02738SRajesh Shah 	int result;
15213fb02738SRajesh Shah 	struct acpi_bus_ops ops;
15223fb02738SRajesh Shah 
15233fb02738SRajesh Shah 	memset(&ops, 0, sizeof(ops));
15243fb02738SRajesh Shah 	ops.acpi_op_add = 1;
1525c4168bffSLi Shaohua 
15265c478f49SBjorn Helgaas 	result = acpi_add_single_object(child, handle, type, &ops);
1527c4168bffSLi Shaohua 	if (!result)
15288e029bf0SBjorn Helgaas 		result = acpi_bus_scan((*child)->handle, &ops);
1529c4168bffSLi Shaohua 
1530d550d98dSPatrick Mochel 	return result;
15313fb02738SRajesh Shah }
15323fb02738SRajesh Shah EXPORT_SYMBOL(acpi_bus_add);
15333fb02738SRajesh Shah 
15344be44fcdSLen Brown int acpi_bus_start(struct acpi_device *device)
15353fb02738SRajesh Shah {
15363fb02738SRajesh Shah 	int result;
15373fb02738SRajesh Shah 	struct acpi_bus_ops ops;
15383fb02738SRajesh Shah 
15393fb02738SRajesh Shah 	memset(&ops, 0, sizeof(ops));
15403fb02738SRajesh Shah 	ops.acpi_op_start = 1;
15418e029bf0SBjorn Helgaas 
15428e029bf0SBjorn Helgaas 	result = acpi_start_single_object(device);
15438e029bf0SBjorn Helgaas 	if (!result)
15448e029bf0SBjorn Helgaas 		result = acpi_bus_scan(device->handle, &ops);
15458e029bf0SBjorn Helgaas 
1546d550d98dSPatrick Mochel 	return result;
15473fb02738SRajesh Shah }
15483fb02738SRajesh Shah EXPORT_SYMBOL(acpi_bus_start);
15491da177e4SLinus Torvalds 
1550ceaba663SKristen Accardi int acpi_bus_trim(struct acpi_device *start, int rmdevice)
15511da177e4SLinus Torvalds {
15521da177e4SLinus Torvalds 	acpi_status status;
15531da177e4SLinus Torvalds 	struct acpi_device *parent, *child;
15541da177e4SLinus Torvalds 	acpi_handle phandle, chandle;
15551da177e4SLinus Torvalds 	acpi_object_type type;
15561da177e4SLinus Torvalds 	u32 level = 1;
15571da177e4SLinus Torvalds 	int err = 0;
15581da177e4SLinus Torvalds 
15591da177e4SLinus Torvalds 	parent = start;
15601da177e4SLinus Torvalds 	phandle = start->handle;
15611da177e4SLinus Torvalds 	child = chandle = NULL;
15621da177e4SLinus Torvalds 
15631da177e4SLinus Torvalds 	while ((level > 0) && parent && (!err)) {
15641da177e4SLinus Torvalds 		status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
15651da177e4SLinus Torvalds 					      chandle, &chandle);
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds 		/*
15681da177e4SLinus Torvalds 		 * If this scope is exhausted then move our way back up.
15691da177e4SLinus Torvalds 		 */
15701da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
15711da177e4SLinus Torvalds 			level--;
15721da177e4SLinus Torvalds 			chandle = phandle;
15731da177e4SLinus Torvalds 			acpi_get_parent(phandle, &phandle);
15741da177e4SLinus Torvalds 			child = parent;
15751da177e4SLinus Torvalds 			parent = parent->parent;
15761da177e4SLinus Torvalds 
15771da177e4SLinus Torvalds 			if (level == 0)
15781da177e4SLinus Torvalds 				err = acpi_bus_remove(child, rmdevice);
15791da177e4SLinus Torvalds 			else
15801da177e4SLinus Torvalds 				err = acpi_bus_remove(child, 1);
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds 			continue;
15831da177e4SLinus Torvalds 		}
15841da177e4SLinus Torvalds 
15851da177e4SLinus Torvalds 		status = acpi_get_type(chandle, &type);
15861da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
15871da177e4SLinus Torvalds 			continue;
15881da177e4SLinus Torvalds 		}
15891da177e4SLinus Torvalds 		/*
15901da177e4SLinus Torvalds 		 * If there is a device corresponding to chandle then
15911da177e4SLinus Torvalds 		 * parse it (depth-first).
15921da177e4SLinus Torvalds 		 */
15931da177e4SLinus Torvalds 		if (acpi_bus_get_device(chandle, &child) == 0) {
15941da177e4SLinus Torvalds 			level++;
15951da177e4SLinus Torvalds 			phandle = chandle;
15961da177e4SLinus Torvalds 			chandle = NULL;
15971da177e4SLinus Torvalds 			parent = child;
15981da177e4SLinus Torvalds 		}
15991da177e4SLinus Torvalds 		continue;
16001da177e4SLinus Torvalds 	}
16011da177e4SLinus Torvalds 	return err;
16021da177e4SLinus Torvalds }
1603ceaba663SKristen Accardi EXPORT_SYMBOL_GPL(acpi_bus_trim);
1604ceaba663SKristen Accardi 
1605e8b945c9SBjorn Helgaas static int acpi_bus_scan_fixed(void)
16061da177e4SLinus Torvalds {
16071da177e4SLinus Torvalds 	int result = 0;
16081da177e4SLinus Torvalds 	struct acpi_device *device = NULL;
1609c4168bffSLi Shaohua 	struct acpi_bus_ops ops;
16101da177e4SLinus Torvalds 
1611c4168bffSLi Shaohua 	memset(&ops, 0, sizeof(ops));
1612c4168bffSLi Shaohua 	ops.acpi_op_add = 1;
1613c4168bffSLi Shaohua 	ops.acpi_op_start = 1;
1614c4168bffSLi Shaohua 
16151da177e4SLinus Torvalds 	/*
16161da177e4SLinus Torvalds 	 * Enumerate all fixed-feature devices.
16171da177e4SLinus Torvalds 	 */
1618cee324b1SAlexey Starikovskiy 	if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
16195c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
1620c4168bffSLi Shaohua 						ACPI_BUS_TYPE_POWER_BUTTON,
1621c4168bffSLi Shaohua 						&ops);
16223fb02738SRajesh Shah 	}
16231da177e4SLinus Torvalds 
1624cee324b1SAlexey Starikovskiy 	if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
16255c478f49SBjorn Helgaas 		result = acpi_add_single_object(&device, NULL,
1626c4168bffSLi Shaohua 						ACPI_BUS_TYPE_SLEEP_BUTTON,
1627c4168bffSLi Shaohua 						&ops);
16283fb02738SRajesh Shah 	}
16291da177e4SLinus Torvalds 
1630d550d98dSPatrick Mochel 	return result;
16311da177e4SLinus Torvalds }
16321da177e4SLinus Torvalds 
1633e747f274SBjorn Helgaas int __init acpi_scan_init(void)
16341da177e4SLinus Torvalds {
16351da177e4SLinus Torvalds 	int result;
16363fb02738SRajesh Shah 	struct acpi_bus_ops ops;
16371da177e4SLinus Torvalds 
1638c4168bffSLi Shaohua 	memset(&ops, 0, sizeof(ops));
1639c4168bffSLi Shaohua 	ops.acpi_op_add = 1;
1640c4168bffSLi Shaohua 	ops.acpi_op_start = 1;
16411da177e4SLinus Torvalds 
16425b327265SPatrick Mochel 	result = bus_register(&acpi_bus_type);
16435b327265SPatrick Mochel 	if (result) {
16445b327265SPatrick Mochel 		/* We don't want to quit even if we failed to add suspend/resume */
16455b327265SPatrick Mochel 		printk(KERN_ERR PREFIX "Could not register bus type\n");
16465b327265SPatrick Mochel 	}
16475b327265SPatrick Mochel 
16481da177e4SLinus Torvalds 	/*
16491da177e4SLinus Torvalds 	 * Create the root device in the bus's device tree
16501da177e4SLinus Torvalds 	 */
16515c478f49SBjorn Helgaas 	result = acpi_add_single_object(&acpi_root, ACPI_ROOT_OBJECT,
1652*859ac9a4SBjorn Helgaas 					ACPI_BUS_TYPE_DEVICE, &ops);
16531da177e4SLinus Torvalds 	if (result)
16541da177e4SLinus Torvalds 		goto Done;
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds 	/*
16571da177e4SLinus Torvalds 	 * Enumerate devices in the ACPI namespace.
16581da177e4SLinus Torvalds 	 */
1659adc08e20SBjorn Helgaas 	result = acpi_bus_scan(acpi_root->handle, &ops);
1660c04209a7SAlexey Starikovskiy 
1661c4168bffSLi Shaohua 	if (!result)
1662adc08e20SBjorn Helgaas 		result = acpi_bus_scan_fixed();
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds 	if (result)
16651da177e4SLinus Torvalds 		acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
16661da177e4SLinus Torvalds 
16671da177e4SLinus Torvalds Done:
1668d550d98dSPatrick Mochel 	return result;
16691da177e4SLinus Torvalds }
1670