xref: /openbmc/linux/drivers/acpi/scan.c (revision 55ac9a018f83e4f42f3c6ce98a8dbda73b985935)
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 #include <acpi/acinterp.h>	/* for acpi_ex_eisa_id_to_string() */
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds #define _COMPONENT		ACPI_BUS_COMPONENT
16f52fd66dSLen Brown ACPI_MODULE_NAME("scan");
171da177e4SLinus Torvalds #define STRUCT_TO_INT(s)	(*((int*)&s))
181da177e4SLinus Torvalds extern struct acpi_device *acpi_root;
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #define ACPI_BUS_CLASS			"system_bus"
2129b71a1cSThomas Renninger #define ACPI_BUS_HID			"LNXSYBUS"
221da177e4SLinus Torvalds #define ACPI_BUS_DEVICE_NAME		"System Bus"
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds static LIST_HEAD(acpi_device_list);
25e49bd2ddSZhang Rui static LIST_HEAD(acpi_bus_id_list);
261da177e4SLinus Torvalds DEFINE_SPINLOCK(acpi_device_lock);
271da177e4SLinus Torvalds LIST_HEAD(acpi_wakeup_device_list);
281da177e4SLinus Torvalds 
29e49bd2ddSZhang Rui struct acpi_device_bus_id{
30bb095854SZhang Rui 	char bus_id[15];
31e49bd2ddSZhang Rui 	unsigned int instance_no;
32e49bd2ddSZhang Rui 	struct list_head node;
33e49bd2ddSZhang Rui };
3429b71a1cSThomas Renninger 
3529b71a1cSThomas Renninger /*
3629b71a1cSThomas Renninger  * Creates hid/cid(s) string needed for modalias and uevent
3729b71a1cSThomas Renninger  * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
3829b71a1cSThomas Renninger  * char *modalias: "acpi:IBM0001:ACPI0001"
3929b71a1cSThomas Renninger */
40b3e572d2SAdrian Bunk static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
41b3e572d2SAdrian Bunk 			   int size)
42b3e572d2SAdrian Bunk {
4329b71a1cSThomas Renninger 	int len;
445c9fcb5dSZhang Rui 	int count;
4529b71a1cSThomas Renninger 
465c9fcb5dSZhang Rui 	if (!acpi_dev->flags.hardware_id && !acpi_dev->flags.compatible_ids)
4729b71a1cSThomas Renninger 		return -ENODEV;
4829b71a1cSThomas Renninger 
495c9fcb5dSZhang Rui 	len = snprintf(modalias, size, "acpi:");
5029b71a1cSThomas Renninger 	size -= len;
5129b71a1cSThomas Renninger 
525c9fcb5dSZhang Rui 	if (acpi_dev->flags.hardware_id) {
535c9fcb5dSZhang Rui 		count = snprintf(&modalias[len], size, "%s:",
545c9fcb5dSZhang Rui 				 acpi_dev->pnp.hardware_id);
555c9fcb5dSZhang Rui 		if (count < 0 || count >= size)
565c9fcb5dSZhang Rui 			return -EINVAL;
575c9fcb5dSZhang Rui 		len += count;
585c9fcb5dSZhang Rui 		size -= count;
595c9fcb5dSZhang Rui 	}
605c9fcb5dSZhang Rui 
6129b71a1cSThomas Renninger 	if (acpi_dev->flags.compatible_ids) {
6229b71a1cSThomas Renninger 		struct acpi_compatible_id_list *cid_list;
6329b71a1cSThomas Renninger 		int i;
6429b71a1cSThomas Renninger 
6529b71a1cSThomas Renninger 		cid_list = acpi_dev->pnp.cid_list;
6629b71a1cSThomas Renninger 		for (i = 0; i < cid_list->count; i++) {
6729b71a1cSThomas Renninger 			count = snprintf(&modalias[len], size, "%s:",
6829b71a1cSThomas Renninger 					 cid_list->id[i].value);
6929b71a1cSThomas Renninger 			if (count < 0 || count >= size) {
7087ecd5cdSLen Brown 				printk(KERN_ERR PREFIX "%s cid[%i] exceeds event buffer size",
7129b71a1cSThomas Renninger 				       acpi_dev->pnp.device_name, i);
7229b71a1cSThomas Renninger 				break;
7329b71a1cSThomas Renninger 			}
7429b71a1cSThomas Renninger 			len += count;
7529b71a1cSThomas Renninger 			size -= count;
7629b71a1cSThomas Renninger 		}
7729b71a1cSThomas Renninger 	}
7829b71a1cSThomas Renninger 
7929b71a1cSThomas Renninger 	modalias[len] = '\0';
8029b71a1cSThomas Renninger 	return len;
8129b71a1cSThomas Renninger }
8229b71a1cSThomas Renninger 
8329b71a1cSThomas Renninger static ssize_t
8429b71a1cSThomas Renninger acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
8529b71a1cSThomas Renninger 	struct acpi_device *acpi_dev = to_acpi_device(dev);
8629b71a1cSThomas Renninger 	int len;
8729b71a1cSThomas Renninger 
8829b71a1cSThomas Renninger 	/* Device has no HID and no CID or string is >1024 */
8929b71a1cSThomas Renninger 	len = create_modalias(acpi_dev, buf, 1024);
9029b71a1cSThomas Renninger 	if (len <= 0)
9129b71a1cSThomas Renninger 		return 0;
9229b71a1cSThomas Renninger 	buf[len++] = '\n';
9329b71a1cSThomas Renninger 	return len;
9429b71a1cSThomas Renninger }
9529b71a1cSThomas Renninger static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
9629b71a1cSThomas Renninger 
9726d46867SZhang Rui static int acpi_bus_hot_remove_device(void *context)
981da177e4SLinus Torvalds {
9926d46867SZhang Rui 	struct acpi_device *device;
10026d46867SZhang Rui 	acpi_handle handle = context;
1011da177e4SLinus Torvalds 	struct acpi_object_list arg_list;
1021da177e4SLinus Torvalds 	union acpi_object arg;
1031da177e4SLinus Torvalds 	acpi_status status = AE_OK;
1041da177e4SLinus Torvalds 
10526d46867SZhang Rui 	if (acpi_bus_get_device(handle, &device))
10626d46867SZhang Rui 		return 0;
1071da177e4SLinus Torvalds 
10826d46867SZhang Rui 	if (!device)
10926d46867SZhang Rui 		return 0;
11026d46867SZhang Rui 
11126d46867SZhang Rui 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
11226d46867SZhang Rui 		"Hot-removing device %s...\n", device->dev.bus_id));
11326d46867SZhang Rui 
11426d46867SZhang Rui 
11526d46867SZhang Rui 	if (acpi_bus_trim(device, 1)) {
116*55ac9a01SLin Ming 		printk(KERN_ERR PREFIX
117*55ac9a01SLin Ming 				"Removing device failed\n");
11826d46867SZhang Rui 		return -1;
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)
124*55ac9a01SLin Ming 		printk(KERN_WARNING PREFIX
125*55ac9a01SLin 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))
14526d46867SZhang Rui 		return -ENODEV;
1461da177e4SLinus Torvalds 
14726d46867SZhang Rui 	return 0;
1481da177e4SLinus Torvalds }
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds static ssize_t
151f883d9dbSPatrick Mochel acpi_eject_store(struct device *d, struct device_attribute *attr,
152f883d9dbSPatrick Mochel 		const char *buf, size_t count)
1531da177e4SLinus Torvalds {
1541da177e4SLinus Torvalds 	int ret = count;
1551da177e4SLinus Torvalds 	acpi_status status;
1561da177e4SLinus Torvalds 	acpi_object_type type = 0;
157f883d9dbSPatrick Mochel 	struct acpi_device *acpi_device = to_acpi_device(d);
15874523c90SAlok N Kataria 	struct task_struct *task;
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 
17526d46867SZhang Rui 	/* remove the device in another thread to fix the deadlock issue */
17674523c90SAlok N Kataria 	task = kthread_run(acpi_bus_hot_remove_device,
17774523c90SAlok N Kataria 				acpi_device->handle, "acpi_hot_remove_device");
17874523c90SAlok N Kataria 	if (IS_ERR(task))
17974523c90SAlok N Kataria 		ret = PTR_ERR(task);
1801da177e4SLinus Torvalds err:
1811da177e4SLinus Torvalds 	return ret;
1821da177e4SLinus Torvalds }
1831da177e4SLinus Torvalds 
184f883d9dbSPatrick Mochel static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
185f883d9dbSPatrick Mochel 
186e49bd2ddSZhang Rui static ssize_t
187e49bd2ddSZhang Rui acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
188e49bd2ddSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
189e49bd2ddSZhang Rui 
190e49bd2ddSZhang Rui 	return sprintf(buf, "%s\n", acpi_dev->pnp.hardware_id);
191e49bd2ddSZhang Rui }
192e49bd2ddSZhang Rui static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
193e49bd2ddSZhang Rui 
194e49bd2ddSZhang Rui static ssize_t
195e49bd2ddSZhang Rui acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
196e49bd2ddSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
197e49bd2ddSZhang Rui 	struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
198e49bd2ddSZhang Rui 	int result;
199e49bd2ddSZhang Rui 
200e49bd2ddSZhang Rui 	result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
201e49bd2ddSZhang Rui 	if(result)
202e49bd2ddSZhang Rui 		goto end;
203e49bd2ddSZhang Rui 
204e49bd2ddSZhang Rui 	result = sprintf(buf, "%s\n", (char*)path.pointer);
205e49bd2ddSZhang Rui 	kfree(path.pointer);
206e49bd2ddSZhang Rui   end:
207e49bd2ddSZhang Rui 	return result;
208e49bd2ddSZhang Rui }
209e49bd2ddSZhang Rui static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
210e49bd2ddSZhang Rui 
211e49bd2ddSZhang Rui static int acpi_device_setup_files(struct acpi_device *dev)
212f883d9dbSPatrick Mochel {
213f883d9dbSPatrick Mochel 	acpi_status status;
214f883d9dbSPatrick Mochel 	acpi_handle temp;
215e49bd2ddSZhang Rui 	int result = 0;
216e49bd2ddSZhang Rui 
217e49bd2ddSZhang Rui 	/*
218e49bd2ddSZhang Rui 	 * Devices gotten from FADT don't have a "path" attribute
219e49bd2ddSZhang Rui 	 */
220e49bd2ddSZhang Rui 	if(dev->handle) {
221e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_path);
222e49bd2ddSZhang Rui 		if(result)
223e49bd2ddSZhang Rui 			goto end;
224e49bd2ddSZhang Rui 	}
225e49bd2ddSZhang Rui 
226e49bd2ddSZhang Rui 	if(dev->flags.hardware_id) {
227e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_hid);
228e49bd2ddSZhang Rui 		if(result)
229e49bd2ddSZhang Rui 			goto end;
230e49bd2ddSZhang Rui 	}
231f883d9dbSPatrick Mochel 
23229b71a1cSThomas Renninger 	if (dev->flags.hardware_id || dev->flags.compatible_ids){
23329b71a1cSThomas Renninger 		result = device_create_file(&dev->dev, &dev_attr_modalias);
23429b71a1cSThomas Renninger 		if(result)
23529b71a1cSThomas Renninger 			goto end;
23629b71a1cSThomas Renninger 	}
23729b71a1cSThomas Renninger 
238f883d9dbSPatrick Mochel         /*
239f883d9dbSPatrick Mochel          * If device has _EJ0, 'eject' file is created that is used to trigger
240f883d9dbSPatrick Mochel          * hot-removal function from userland.
241f883d9dbSPatrick Mochel          */
242f883d9dbSPatrick Mochel 	status = acpi_get_handle(dev->handle, "_EJ0", &temp);
243f883d9dbSPatrick Mochel 	if (ACPI_SUCCESS(status))
244e49bd2ddSZhang Rui 		result = device_create_file(&dev->dev, &dev_attr_eject);
245e49bd2ddSZhang Rui   end:
246e49bd2ddSZhang Rui 	return result;
247f883d9dbSPatrick Mochel }
248f883d9dbSPatrick Mochel 
249f883d9dbSPatrick Mochel static void acpi_device_remove_files(struct acpi_device *dev)
250f883d9dbSPatrick Mochel {
251f883d9dbSPatrick Mochel 	acpi_status status;
252f883d9dbSPatrick Mochel 	acpi_handle temp;
253f883d9dbSPatrick Mochel 
254f883d9dbSPatrick Mochel 	/*
255f883d9dbSPatrick Mochel 	 * If device has _EJ0, 'eject' file is created that is used to trigger
256f883d9dbSPatrick Mochel 	 * hot-removal function from userland.
257f883d9dbSPatrick Mochel 	 */
258f883d9dbSPatrick Mochel 	status = acpi_get_handle(dev->handle, "_EJ0", &temp);
259f883d9dbSPatrick Mochel 	if (ACPI_SUCCESS(status))
260f883d9dbSPatrick Mochel 		device_remove_file(&dev->dev, &dev_attr_eject);
261e49bd2ddSZhang Rui 
26229b71a1cSThomas Renninger 	if (dev->flags.hardware_id || dev->flags.compatible_ids)
26329b71a1cSThomas Renninger 		device_remove_file(&dev->dev, &dev_attr_modalias);
26429b71a1cSThomas Renninger 
265e49bd2ddSZhang Rui 	if(dev->flags.hardware_id)
266e49bd2ddSZhang Rui 		device_remove_file(&dev->dev, &dev_attr_hid);
267e49bd2ddSZhang Rui 	if(dev->handle)
268e49bd2ddSZhang Rui 		device_remove_file(&dev->dev, &dev_attr_path);
269f883d9dbSPatrick Mochel }
2701da177e4SLinus Torvalds /* --------------------------------------------------------------------------
2719e89dde2SZhang Rui 			ACPI Bus operations
2721da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
27329b71a1cSThomas Renninger 
27429b71a1cSThomas Renninger int acpi_match_device_ids(struct acpi_device *device,
27529b71a1cSThomas Renninger 			  const struct acpi_device_id *ids)
27629b71a1cSThomas Renninger {
27729b71a1cSThomas Renninger 	const struct acpi_device_id *id;
27829b71a1cSThomas Renninger 
27929b71a1cSThomas Renninger 	if (device->flags.hardware_id) {
28029b71a1cSThomas Renninger 		for (id = ids; id->id[0]; id++) {
28129b71a1cSThomas Renninger 			if (!strcmp((char*)id->id, device->pnp.hardware_id))
28229b71a1cSThomas Renninger 				return 0;
28329b71a1cSThomas Renninger 		}
28429b71a1cSThomas Renninger 	}
28529b71a1cSThomas Renninger 
28629b71a1cSThomas Renninger 	if (device->flags.compatible_ids) {
28729b71a1cSThomas Renninger 		struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
28829b71a1cSThomas Renninger 		int i;
28929b71a1cSThomas Renninger 
29029b71a1cSThomas Renninger 		for (id = ids; id->id[0]; id++) {
29129b71a1cSThomas Renninger 			/* compare multiple _CID entries against driver ids */
29229b71a1cSThomas Renninger 			for (i = 0; i < cid_list->count; i++) {
29329b71a1cSThomas Renninger 				if (!strcmp((char*)id->id,
29429b71a1cSThomas Renninger 					    cid_list->id[i].value))
29529b71a1cSThomas Renninger 					return 0;
29629b71a1cSThomas Renninger 			}
29729b71a1cSThomas Renninger 		}
29829b71a1cSThomas Renninger 	}
29929b71a1cSThomas Renninger 
30029b71a1cSThomas Renninger 	return -ENOENT;
30129b71a1cSThomas Renninger }
30229b71a1cSThomas Renninger EXPORT_SYMBOL(acpi_match_device_ids);
30329b71a1cSThomas Renninger 
3041890a97aSPatrick Mochel static void acpi_device_release(struct device *dev)
3051890a97aSPatrick Mochel {
3061890a97aSPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3071890a97aSPatrick Mochel 
3081890a97aSPatrick Mochel 	kfree(acpi_dev->pnp.cid_list);
3091890a97aSPatrick Mochel 	kfree(acpi_dev);
3101890a97aSPatrick Mochel }
3111890a97aSPatrick Mochel 
3129e89dde2SZhang Rui static int acpi_device_suspend(struct device *dev, pm_message_t state)
3139e89dde2SZhang Rui {
3141d268b0aSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3155d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
3161da177e4SLinus Torvalds 
3175d9464a4SPatrick Mochel 	if (acpi_drv && acpi_drv->ops.suspend)
3185d9464a4SPatrick Mochel 		return acpi_drv->ops.suspend(acpi_dev, state);
3191da177e4SLinus Torvalds 	return 0;
3209e89dde2SZhang Rui }
3211da177e4SLinus Torvalds 
3229e89dde2SZhang Rui static int acpi_device_resume(struct device *dev)
3239e89dde2SZhang Rui {
3241d268b0aSZhang Rui 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3255d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
3269e89dde2SZhang Rui 
3275d9464a4SPatrick Mochel 	if (acpi_drv && acpi_drv->ops.resume)
3285d9464a4SPatrick Mochel 		return acpi_drv->ops.resume(acpi_dev);
3299e89dde2SZhang Rui 	return 0;
3309e89dde2SZhang Rui }
3311da177e4SLinus Torvalds 
3325d9464a4SPatrick Mochel static int acpi_bus_match(struct device *dev, struct device_driver *drv)
3331da177e4SLinus Torvalds {
3345d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3355d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(drv);
3365d9464a4SPatrick Mochel 
33729b71a1cSThomas Renninger 	return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
3385d9464a4SPatrick Mochel }
3395d9464a4SPatrick Mochel 
3407eff2e7aSKay Sievers static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
3415d9464a4SPatrick Mochel {
3425d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3437eff2e7aSKay Sievers 	int len;
3445d9464a4SPatrick Mochel 
3457eff2e7aSKay Sievers 	if (add_uevent_var(env, "MODALIAS="))
3467eff2e7aSKay Sievers 		return -ENOMEM;
3477eff2e7aSKay Sievers 	len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
3487eff2e7aSKay Sievers 			      sizeof(env->buf) - env->buflen);
3497eff2e7aSKay Sievers 	if (len >= (sizeof(env->buf) - env->buflen))
3507eff2e7aSKay Sievers 		return -ENOMEM;
3517eff2e7aSKay Sievers 	env->buflen += len;
3521da177e4SLinus Torvalds 	return 0;
3531da177e4SLinus Torvalds }
3541da177e4SLinus Torvalds 
3555d9464a4SPatrick Mochel static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
3565d9464a4SPatrick Mochel static int acpi_start_single_object(struct acpi_device *);
3575d9464a4SPatrick Mochel static int acpi_device_probe(struct device * dev)
3585d9464a4SPatrick Mochel {
3595d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3605d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
3615d9464a4SPatrick Mochel 	int ret;
3625d9464a4SPatrick Mochel 
3635d9464a4SPatrick Mochel 	ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
3645d9464a4SPatrick Mochel 	if (!ret) {
365c4168bffSLi Shaohua 		if (acpi_dev->bus_ops.acpi_op_start)
3665d9464a4SPatrick Mochel 			acpi_start_single_object(acpi_dev);
3675d9464a4SPatrick Mochel 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
3685d9464a4SPatrick Mochel 			"Found driver [%s] for device [%s]\n",
3695d9464a4SPatrick Mochel 			acpi_drv->name, acpi_dev->pnp.bus_id));
3705d9464a4SPatrick Mochel 		get_device(dev);
3715d9464a4SPatrick Mochel 	}
3725d9464a4SPatrick Mochel 	return ret;
3735d9464a4SPatrick Mochel }
3745d9464a4SPatrick Mochel 
3755d9464a4SPatrick Mochel static int acpi_device_remove(struct device * dev)
3765d9464a4SPatrick Mochel {
3775d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3785d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
3795d9464a4SPatrick Mochel 
3805d9464a4SPatrick Mochel 	if (acpi_drv) {
3815d9464a4SPatrick Mochel 		if (acpi_drv->ops.stop)
38296333578SLi Shaohua 			acpi_drv->ops.stop(acpi_dev, acpi_dev->removal_type);
3835d9464a4SPatrick Mochel 		if (acpi_drv->ops.remove)
38496333578SLi Shaohua 			acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
3855d9464a4SPatrick Mochel 	}
3865d9464a4SPatrick Mochel 	acpi_dev->driver = NULL;
3875d9464a4SPatrick Mochel 	acpi_driver_data(dev) = NULL;
3885d9464a4SPatrick Mochel 
3895d9464a4SPatrick Mochel 	put_device(dev);
3905d9464a4SPatrick Mochel 	return 0;
3915d9464a4SPatrick Mochel }
3925d9464a4SPatrick Mochel 
3935d9464a4SPatrick Mochel static void acpi_device_shutdown(struct device *dev)
3945d9464a4SPatrick Mochel {
3955d9464a4SPatrick Mochel 	struct acpi_device *acpi_dev = to_acpi_device(dev);
3965d9464a4SPatrick Mochel 	struct acpi_driver *acpi_drv = acpi_dev->driver;
3975d9464a4SPatrick Mochel 
3985d9464a4SPatrick Mochel 	if (acpi_drv && acpi_drv->ops.shutdown)
3995d9464a4SPatrick Mochel 		acpi_drv->ops.shutdown(acpi_dev);
4005d9464a4SPatrick Mochel 
4015d9464a4SPatrick Mochel 	return ;
4021da177e4SLinus Torvalds }
4031da177e4SLinus Torvalds 
40455955aadSDavid Brownell struct bus_type acpi_bus_type = {
4059e89dde2SZhang Rui 	.name		= "acpi",
4069e89dde2SZhang Rui 	.suspend	= acpi_device_suspend,
4079e89dde2SZhang Rui 	.resume		= acpi_device_resume,
4085d9464a4SPatrick Mochel 	.shutdown	= acpi_device_shutdown,
4095d9464a4SPatrick Mochel 	.match		= acpi_bus_match,
4105d9464a4SPatrick Mochel 	.probe		= acpi_device_probe,
4115d9464a4SPatrick Mochel 	.remove		= acpi_device_remove,
4125d9464a4SPatrick Mochel 	.uevent		= acpi_device_uevent,
4131da177e4SLinus Torvalds };
4141da177e4SLinus Torvalds 
415e49bd2ddSZhang Rui static int acpi_device_register(struct acpi_device *device,
4161da177e4SLinus Torvalds 				 struct acpi_device *parent)
4171da177e4SLinus Torvalds {
418e49bd2ddSZhang Rui 	int result;
419e49bd2ddSZhang Rui 	struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
420e49bd2ddSZhang Rui 	int found = 0;
4211da177e4SLinus Torvalds 	/*
4221da177e4SLinus Torvalds 	 * Linkage
4231da177e4SLinus Torvalds 	 * -------
4241da177e4SLinus Torvalds 	 * Link this device to its parent and siblings.
4251da177e4SLinus Torvalds 	 */
4261da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->children);
4271da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->node);
4281da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->g_list);
4291da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->wakeup_list);
4301da177e4SLinus Torvalds 
431e49bd2ddSZhang Rui 	new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
432e49bd2ddSZhang Rui 	if (!new_bus_id) {
433e49bd2ddSZhang Rui 		printk(KERN_ERR PREFIX "Memory allocation error\n");
434e49bd2ddSZhang Rui 		return -ENOMEM;
435e49bd2ddSZhang Rui 	}
436e49bd2ddSZhang Rui 
4371da177e4SLinus Torvalds 	spin_lock(&acpi_device_lock);
438e49bd2ddSZhang Rui 	/*
439e49bd2ddSZhang Rui 	 * Find suitable bus_id and instance number in acpi_bus_id_list
440e49bd2ddSZhang Rui 	 * If failed, create one and link it into acpi_bus_id_list
441e49bd2ddSZhang Rui 	 */
442e49bd2ddSZhang Rui 	list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
443bb095854SZhang Rui 		if(!strcmp(acpi_device_bus_id->bus_id, device->flags.hardware_id? device->pnp.hardware_id : "device")) {
444e49bd2ddSZhang Rui 			acpi_device_bus_id->instance_no ++;
445e49bd2ddSZhang Rui 			found = 1;
446e49bd2ddSZhang Rui 			kfree(new_bus_id);
447e49bd2ddSZhang Rui 			break;
448e49bd2ddSZhang Rui 		}
449e49bd2ddSZhang Rui 	}
450e49bd2ddSZhang Rui 	if(!found) {
451e49bd2ddSZhang Rui 		acpi_device_bus_id = new_bus_id;
452bb095854SZhang Rui 		strcpy(acpi_device_bus_id->bus_id, device->flags.hardware_id ? device->pnp.hardware_id : "device");
453e49bd2ddSZhang Rui 		acpi_device_bus_id->instance_no = 0;
454e49bd2ddSZhang Rui 		list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
455e49bd2ddSZhang Rui 	}
456e49bd2ddSZhang Rui 	sprintf(device->dev.bus_id, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
457e49bd2ddSZhang Rui 
4581da177e4SLinus Torvalds 	if (device->parent) {
4591da177e4SLinus Torvalds 		list_add_tail(&device->node, &device->parent->children);
4601da177e4SLinus Torvalds 		list_add_tail(&device->g_list, &device->parent->g_list);
4611da177e4SLinus Torvalds 	} else
4621da177e4SLinus Torvalds 		list_add_tail(&device->g_list, &acpi_device_list);
4631da177e4SLinus Torvalds 	if (device->wakeup.flags.valid)
4641da177e4SLinus Torvalds 		list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
4651da177e4SLinus Torvalds 	spin_unlock(&acpi_device_lock);
4661da177e4SLinus Torvalds 
4671890a97aSPatrick Mochel 	if (device->parent)
4681890a97aSPatrick Mochel 		device->dev.parent = &parent->dev;
4691890a97aSPatrick Mochel 	device->dev.bus = &acpi_bus_type;
4701890a97aSPatrick Mochel 	device_initialize(&device->dev);
4711890a97aSPatrick Mochel 	device->dev.release = &acpi_device_release;
472e49bd2ddSZhang Rui 	result = device_add(&device->dev);
473e49bd2ddSZhang Rui 	if(result) {
474fc3a8828SGreg Kroah-Hartman 		dev_err(&device->dev, "Error adding device\n");
475e49bd2ddSZhang Rui 		goto end;
476e49bd2ddSZhang Rui 	}
477f883d9dbSPatrick Mochel 
478e49bd2ddSZhang Rui 	result = acpi_device_setup_files(device);
479e49bd2ddSZhang Rui 	if(result)
480*55ac9a01SLin Ming 		printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n", device->dev.bus_id);
481e49bd2ddSZhang Rui 
48296333578SLi Shaohua 	device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
483e49bd2ddSZhang Rui 	return 0;
484e49bd2ddSZhang Rui   end:
485e49bd2ddSZhang Rui 	spin_lock(&acpi_device_lock);
486e49bd2ddSZhang Rui 	if (device->parent) {
487e49bd2ddSZhang Rui 		list_del(&device->node);
488e49bd2ddSZhang Rui 		list_del(&device->g_list);
489e49bd2ddSZhang Rui 	} else
490e49bd2ddSZhang Rui 		list_del(&device->g_list);
491e49bd2ddSZhang Rui 	list_del(&device->wakeup_list);
492e49bd2ddSZhang Rui 	spin_unlock(&acpi_device_lock);
493e49bd2ddSZhang Rui 	return result;
4941da177e4SLinus Torvalds }
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds static void acpi_device_unregister(struct acpi_device *device, int type)
4971da177e4SLinus Torvalds {
4981da177e4SLinus Torvalds 	spin_lock(&acpi_device_lock);
4991da177e4SLinus Torvalds 	if (device->parent) {
5001da177e4SLinus Torvalds 		list_del(&device->node);
5011da177e4SLinus Torvalds 		list_del(&device->g_list);
5021da177e4SLinus Torvalds 	} else
5031da177e4SLinus Torvalds 		list_del(&device->g_list);
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	list_del(&device->wakeup_list);
5061da177e4SLinus Torvalds 	spin_unlock(&acpi_device_lock);
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 	acpi_detach_data(device->handle, acpi_bus_data_handler);
5091890a97aSPatrick Mochel 
510f883d9dbSPatrick Mochel 	acpi_device_remove_files(device);
5111890a97aSPatrick Mochel 	device_unregister(&device->dev);
5121da177e4SLinus Torvalds }
5131da177e4SLinus Torvalds 
5149e89dde2SZhang Rui /* --------------------------------------------------------------------------
5159e89dde2SZhang Rui                                  Driver Management
5169e89dde2SZhang Rui    -------------------------------------------------------------------------- */
5171da177e4SLinus Torvalds /**
518d758a8faSRandy Dunlap  * acpi_bus_driver_init - add a device to a driver
519d758a8faSRandy Dunlap  * @device: the device to add and initialize
520d758a8faSRandy Dunlap  * @driver: driver for the device
521d758a8faSRandy Dunlap  *
5221da177e4SLinus Torvalds  * Used to initialize a device via its device driver.  Called whenever a
5231890a97aSPatrick Mochel  * driver is bound to a device.  Invokes the driver's add() ops.
5241da177e4SLinus Torvalds  */
5251da177e4SLinus Torvalds static int
5264be44fcdSLen Brown acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
5271da177e4SLinus Torvalds {
5281da177e4SLinus Torvalds 	int result = 0;
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	if (!device || !driver)
532d550d98dSPatrick Mochel 		return -EINVAL;
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	if (!driver->ops.add)
535d550d98dSPatrick Mochel 		return -ENOSYS;
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 	result = driver->ops.add(device);
5381da177e4SLinus Torvalds 	if (result) {
5391da177e4SLinus Torvalds 		device->driver = NULL;
5401da177e4SLinus Torvalds 		acpi_driver_data(device) = NULL;
541d550d98dSPatrick Mochel 		return result;
5421da177e4SLinus Torvalds 	}
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 	device->driver = driver;
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds 	/*
5471da177e4SLinus Torvalds 	 * TBD - Configuration Management: Assign resources to device based
5481da177e4SLinus Torvalds 	 * upon possible configuration and currently allocated resources.
5491da177e4SLinus Torvalds 	 */
5501da177e4SLinus Torvalds 
5514be44fcdSLen Brown 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
5524be44fcdSLen Brown 			  "Driver successfully bound to device\n"));
553d550d98dSPatrick Mochel 	return 0;
5543fb02738SRajesh Shah }
5553fb02738SRajesh Shah 
5568713cbefSAdrian Bunk static int acpi_start_single_object(struct acpi_device *device)
5573fb02738SRajesh Shah {
5583fb02738SRajesh Shah 	int result = 0;
5593fb02738SRajesh Shah 	struct acpi_driver *driver;
5603fb02738SRajesh Shah 
5613fb02738SRajesh Shah 
5623fb02738SRajesh Shah 	if (!(driver = device->driver))
563d550d98dSPatrick Mochel 		return 0;
5643fb02738SRajesh Shah 
5651da177e4SLinus Torvalds 	if (driver->ops.start) {
5661da177e4SLinus Torvalds 		result = driver->ops.start(device);
5671da177e4SLinus Torvalds 		if (result && driver->ops.remove)
5681da177e4SLinus Torvalds 			driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
5693fb02738SRajesh Shah 	}
5703fb02738SRajesh Shah 
571d550d98dSPatrick Mochel 	return result;
5721da177e4SLinus Torvalds }
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds /**
575d758a8faSRandy Dunlap  * acpi_bus_register_driver - register a driver with the ACPI bus
576d758a8faSRandy Dunlap  * @driver: driver being registered
577d758a8faSRandy Dunlap  *
5781da177e4SLinus Torvalds  * Registers a driver with the ACPI bus.  Searches the namespace for all
5799d9f749bSBjorn Helgaas  * devices that match the driver's criteria and binds.  Returns zero for
5809d9f749bSBjorn Helgaas  * success or a negative error status for failure.
5811da177e4SLinus Torvalds  */
5824be44fcdSLen Brown int acpi_bus_register_driver(struct acpi_driver *driver)
5831da177e4SLinus Torvalds {
5841890a97aSPatrick Mochel 	int ret;
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	if (acpi_disabled)
587d550d98dSPatrick Mochel 		return -ENODEV;
5881890a97aSPatrick Mochel 	driver->drv.name = driver->name;
5891890a97aSPatrick Mochel 	driver->drv.bus = &acpi_bus_type;
5901890a97aSPatrick Mochel 	driver->drv.owner = driver->owner;
5911da177e4SLinus Torvalds 
5921890a97aSPatrick Mochel 	ret = driver_register(&driver->drv);
5931890a97aSPatrick Mochel 	return ret;
5941da177e4SLinus Torvalds }
5951da177e4SLinus Torvalds 
5964be44fcdSLen Brown EXPORT_SYMBOL(acpi_bus_register_driver);
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds /**
599d758a8faSRandy Dunlap  * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
600d758a8faSRandy Dunlap  * @driver: driver to unregister
601d758a8faSRandy Dunlap  *
6021da177e4SLinus Torvalds  * Unregisters a driver with the ACPI bus.  Searches the namespace for all
6031da177e4SLinus Torvalds  * devices that match the driver's criteria and unbinds.
6041da177e4SLinus Torvalds  */
60506ea8e08SBjorn Helgaas void acpi_bus_unregister_driver(struct acpi_driver *driver)
6061da177e4SLinus Torvalds {
6071890a97aSPatrick Mochel 	driver_unregister(&driver->drv);
6081da177e4SLinus Torvalds }
6094be44fcdSLen Brown 
6101da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_unregister_driver);
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds /* --------------------------------------------------------------------------
6131da177e4SLinus Torvalds                                  Device Enumeration
6141da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
615c8f7a62cSLen Brown acpi_status
616c8f7a62cSLen Brown acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
617c8f7a62cSLen Brown {
618c8f7a62cSLen Brown 	acpi_status status;
619c8f7a62cSLen Brown 	acpi_handle tmp;
620c8f7a62cSLen Brown 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
621c8f7a62cSLen Brown 	union acpi_object *obj;
622c8f7a62cSLen Brown 
623c8f7a62cSLen Brown 	status = acpi_get_handle(handle, "_EJD", &tmp);
624c8f7a62cSLen Brown 	if (ACPI_FAILURE(status))
625c8f7a62cSLen Brown 		return status;
626c8f7a62cSLen Brown 
627c8f7a62cSLen Brown 	status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
628c8f7a62cSLen Brown 	if (ACPI_SUCCESS(status)) {
629c8f7a62cSLen Brown 		obj = buffer.pointer;
6303b5fee59SHolger Macht 		status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
6313b5fee59SHolger Macht 					 ejd);
632c8f7a62cSLen Brown 		kfree(buffer.pointer);
633c8f7a62cSLen Brown 	}
634c8f7a62cSLen Brown 	return status;
635c8f7a62cSLen Brown }
636c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
637c8f7a62cSLen Brown 
6381da177e4SLinus Torvalds void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
6391da177e4SLinus Torvalds {
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	/* TBD */
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 	return;
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
6469e89dde2SZhang Rui static int acpi_bus_get_perf_flags(struct acpi_device *device)
6479e89dde2SZhang Rui {
6489e89dde2SZhang Rui 	device->performance.state = ACPI_STATE_UNKNOWN;
6499e89dde2SZhang Rui 	return 0;
6509e89dde2SZhang Rui }
6519e89dde2SZhang Rui 
6521da177e4SLinus Torvalds static acpi_status
6531da177e4SLinus Torvalds acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
6541da177e4SLinus Torvalds 					     union acpi_object *package)
6551da177e4SLinus Torvalds {
6561da177e4SLinus Torvalds 	int i = 0;
6571da177e4SLinus Torvalds 	union acpi_object *element = NULL;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	if (!device || !package || (package->package.count < 2))
6601da177e4SLinus Torvalds 		return AE_BAD_PARAMETER;
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 	element = &(package->package.elements[0]);
6631da177e4SLinus Torvalds 	if (!element)
6641da177e4SLinus Torvalds 		return AE_BAD_PARAMETER;
6651da177e4SLinus Torvalds 	if (element->type == ACPI_TYPE_PACKAGE) {
6661da177e4SLinus Torvalds 		if ((element->package.count < 2) ||
6671da177e4SLinus Torvalds 		    (element->package.elements[0].type !=
6681da177e4SLinus Torvalds 		     ACPI_TYPE_LOCAL_REFERENCE)
6691da177e4SLinus Torvalds 		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
6701da177e4SLinus Torvalds 			return AE_BAD_DATA;
6711da177e4SLinus Torvalds 		device->wakeup.gpe_device =
6721da177e4SLinus Torvalds 		    element->package.elements[0].reference.handle;
6731da177e4SLinus Torvalds 		device->wakeup.gpe_number =
6741da177e4SLinus Torvalds 		    (u32) element->package.elements[1].integer.value;
6751da177e4SLinus Torvalds 	} else if (element->type == ACPI_TYPE_INTEGER) {
6761da177e4SLinus Torvalds 		device->wakeup.gpe_number = element->integer.value;
6771da177e4SLinus Torvalds 	} else
6781da177e4SLinus Torvalds 		return AE_BAD_DATA;
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	element = &(package->package.elements[1]);
6811da177e4SLinus Torvalds 	if (element->type != ACPI_TYPE_INTEGER) {
6821da177e4SLinus Torvalds 		return AE_BAD_DATA;
6831da177e4SLinus Torvalds 	}
6841da177e4SLinus Torvalds 	device->wakeup.sleep_state = element->integer.value;
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 	if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
6871da177e4SLinus Torvalds 		return AE_NO_MEMORY;
6881da177e4SLinus Torvalds 	}
6891da177e4SLinus Torvalds 	device->wakeup.resources.count = package->package.count - 2;
6901da177e4SLinus Torvalds 	for (i = 0; i < device->wakeup.resources.count; i++) {
6911da177e4SLinus Torvalds 		element = &(package->package.elements[i + 2]);
692cd0b2248SBob Moore 		if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
6931da177e4SLinus Torvalds 			return AE_BAD_DATA;
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 		device->wakeup.resources.handles[i] = element->reference.handle;
6961da177e4SLinus Torvalds 	}
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	return AE_OK;
6991da177e4SLinus Torvalds }
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
7021da177e4SLinus Torvalds {
7031da177e4SLinus Torvalds 	acpi_status status = 0;
7041da177e4SLinus Torvalds 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
7051da177e4SLinus Torvalds 	union acpi_object *package = NULL;
70677e76609SRafael J. Wysocki 	int psw_error;
7071da177e4SLinus Torvalds 
70829b71a1cSThomas Renninger 	struct acpi_device_id button_device_ids[] = {
70929b71a1cSThomas Renninger 		{"PNP0C0D", 0},
71029b71a1cSThomas Renninger 		{"PNP0C0C", 0},
71129b71a1cSThomas Renninger 		{"PNP0C0E", 0},
71229b71a1cSThomas Renninger 		{"", 0},
71329b71a1cSThomas Renninger 	};
71429b71a1cSThomas Renninger 
7151da177e4SLinus Torvalds 	/* _PRW */
7161da177e4SLinus Torvalds 	status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
7171da177e4SLinus Torvalds 	if (ACPI_FAILURE(status)) {
7181da177e4SLinus Torvalds 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
7191da177e4SLinus Torvalds 		goto end;
7201da177e4SLinus Torvalds 	}
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 	package = (union acpi_object *)buffer.pointer;
7231da177e4SLinus Torvalds 	status = acpi_bus_extract_wakeup_device_power_package(device, package);
7241da177e4SLinus Torvalds 	if (ACPI_FAILURE(status)) {
7251da177e4SLinus Torvalds 		ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
7261da177e4SLinus Torvalds 		goto end;
7271da177e4SLinus Torvalds 	}
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	kfree(buffer.pointer);
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 	device->wakeup.flags.valid = 1;
732729b2bdbSZhao Yakui 	/* Call _PSW/_DSW object to disable its ability to wake the sleeping
733729b2bdbSZhao Yakui 	 * system for the ACPI device with the _PRW object.
734729b2bdbSZhao Yakui 	 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
735729b2bdbSZhao Yakui 	 * So it is necessary to call _DSW object first. Only when it is not
736729b2bdbSZhao Yakui 	 * present will the _PSW object used.
737729b2bdbSZhao Yakui 	 */
73877e76609SRafael J. Wysocki 	psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
73977e76609SRafael J. Wysocki 	if (psw_error)
74077e76609SRafael J. Wysocki 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
74177e76609SRafael J. Wysocki 				"error in _DSW or _PSW evaluation\n"));
74277e76609SRafael J. Wysocki 
7431da177e4SLinus Torvalds 	/* Power button, Lid switch always enable wakeup */
74429b71a1cSThomas Renninger 	if (!acpi_match_device_ids(device, button_device_ids))
7451da177e4SLinus Torvalds 		device->wakeup.flags.run_wake = 1;
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds       end:
7481da177e4SLinus Torvalds 	if (ACPI_FAILURE(status))
7491da177e4SLinus Torvalds 		device->flags.wake_capable = 0;
7501da177e4SLinus Torvalds 	return 0;
7511da177e4SLinus Torvalds }
7521da177e4SLinus Torvalds 
7539e89dde2SZhang Rui static int acpi_bus_get_power_flags(struct acpi_device *device)
7541da177e4SLinus Torvalds {
7559e89dde2SZhang Rui 	acpi_status status = 0;
7569e89dde2SZhang Rui 	acpi_handle handle = NULL;
7579e89dde2SZhang Rui 	u32 i = 0;
7589e89dde2SZhang Rui 
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	/*
7619e89dde2SZhang Rui 	 * Power Management Flags
7621da177e4SLinus Torvalds 	 */
7639e89dde2SZhang Rui 	status = acpi_get_handle(device->handle, "_PSC", &handle);
7641da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
7659e89dde2SZhang Rui 		device->power.flags.explicit_get = 1;
7669e89dde2SZhang Rui 	status = acpi_get_handle(device->handle, "_IRC", &handle);
7679e89dde2SZhang Rui 	if (ACPI_SUCCESS(status))
7689e89dde2SZhang Rui 		device->power.flags.inrush_current = 1;
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 	/*
7719e89dde2SZhang Rui 	 * Enumerate supported power management states
7721da177e4SLinus Torvalds 	 */
7739e89dde2SZhang Rui 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
7749e89dde2SZhang Rui 		struct acpi_device_power_state *ps = &device->power.states[i];
7759e89dde2SZhang Rui 		char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
7761da177e4SLinus Torvalds 
7779e89dde2SZhang Rui 		/* Evaluate "_PRx" to se if power resources are referenced */
7789e89dde2SZhang Rui 		acpi_evaluate_reference(device->handle, object_name, NULL,
7799e89dde2SZhang Rui 					&ps->resources);
7809e89dde2SZhang Rui 		if (ps->resources.count) {
7819e89dde2SZhang Rui 			device->power.flags.power_resources = 1;
7829e89dde2SZhang Rui 			ps->flags.valid = 1;
7831da177e4SLinus Torvalds 		}
7841da177e4SLinus Torvalds 
7859e89dde2SZhang Rui 		/* Evaluate "_PSx" to see if we can do explicit sets */
7869e89dde2SZhang Rui 		object_name[2] = 'S';
7879e89dde2SZhang Rui 		status = acpi_get_handle(device->handle, object_name, &handle);
788c8f7a62cSLen Brown 		if (ACPI_SUCCESS(status)) {
7899e89dde2SZhang Rui 			ps->flags.explicit_set = 1;
7909e89dde2SZhang Rui 			ps->flags.valid = 1;
791c8f7a62cSLen Brown 		}
792c8f7a62cSLen Brown 
7939e89dde2SZhang Rui 		/* State is valid if we have some power control */
7949e89dde2SZhang Rui 		if (ps->resources.count || ps->flags.explicit_set)
7959e89dde2SZhang Rui 			ps->flags.valid = 1;
7969e89dde2SZhang Rui 
7979e89dde2SZhang Rui 		ps->power = -1;	/* Unknown - driver assigned */
7989e89dde2SZhang Rui 		ps->latency = -1;	/* Unknown - driver assigned */
7999e89dde2SZhang Rui 	}
8009e89dde2SZhang Rui 
8019e89dde2SZhang Rui 	/* Set defaults for D0 and D3 states (always valid) */
8029e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].flags.valid = 1;
8039e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D0].power = 100;
8049e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D3].flags.valid = 1;
8059e89dde2SZhang Rui 	device->power.states[ACPI_STATE_D3].power = 0;
8069e89dde2SZhang Rui 
8079e89dde2SZhang Rui 	/* TBD: System wake support and resource requirements. */
8089e89dde2SZhang Rui 
8099e89dde2SZhang Rui 	device->power.state = ACPI_STATE_UNKNOWN;
8109e89dde2SZhang Rui 
8119e89dde2SZhang Rui 	return 0;
8129e89dde2SZhang Rui }
813c8f7a62cSLen Brown 
8144be44fcdSLen Brown static int acpi_bus_get_flags(struct acpi_device *device)
8151da177e4SLinus Torvalds {
8161da177e4SLinus Torvalds 	acpi_status status = AE_OK;
8171da177e4SLinus Torvalds 	acpi_handle temp = NULL;
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 	/* Presence of _STA indicates 'dynamic_status' */
8211da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_STA", &temp);
8221da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
8231da177e4SLinus Torvalds 		device->flags.dynamic_status = 1;
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 	/* Presence of _CID indicates 'compatible_ids' */
8261da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_CID", &temp);
8271da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
8281da177e4SLinus Torvalds 		device->flags.compatible_ids = 1;
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 	/* Presence of _RMV indicates 'removable' */
8311da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_RMV", &temp);
8321da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
8331da177e4SLinus Torvalds 		device->flags.removable = 1;
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
8361da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_EJD", &temp);
8371da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
8381da177e4SLinus Torvalds 		device->flags.ejectable = 1;
8391da177e4SLinus Torvalds 	else {
8401da177e4SLinus Torvalds 		status = acpi_get_handle(device->handle, "_EJ0", &temp);
8411da177e4SLinus Torvalds 		if (ACPI_SUCCESS(status))
8421da177e4SLinus Torvalds 			device->flags.ejectable = 1;
8431da177e4SLinus Torvalds 	}
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 	/* Presence of _LCK indicates 'lockable' */
8461da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_LCK", &temp);
8471da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
8481da177e4SLinus Torvalds 		device->flags.lockable = 1;
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 	/* Presence of _PS0|_PR0 indicates 'power manageable' */
8511da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_PS0", &temp);
8521da177e4SLinus Torvalds 	if (ACPI_FAILURE(status))
8531da177e4SLinus Torvalds 		status = acpi_get_handle(device->handle, "_PR0", &temp);
8541da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
8551da177e4SLinus Torvalds 		device->flags.power_manageable = 1;
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 	/* Presence of _PRW indicates wake capable */
8581da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_PRW", &temp);
8591da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
8601da177e4SLinus Torvalds 		device->flags.wake_capable = 1;
8611da177e4SLinus Torvalds 
8623c5f9be4SJoe Perches 	/* TBD: Performance management */
8631da177e4SLinus Torvalds 
864d550d98dSPatrick Mochel 	return 0;
8651da177e4SLinus Torvalds }
8661da177e4SLinus Torvalds 
8674be44fcdSLen Brown static void acpi_device_get_busid(struct acpi_device *device,
8684be44fcdSLen Brown 				  acpi_handle handle, int type)
8691da177e4SLinus Torvalds {
8701da177e4SLinus Torvalds 	char bus_id[5] = { '?', 0 };
8711da177e4SLinus Torvalds 	struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
8721da177e4SLinus Torvalds 	int i = 0;
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	/*
8751da177e4SLinus Torvalds 	 * Bus ID
8761da177e4SLinus Torvalds 	 * ------
8771da177e4SLinus Torvalds 	 * The device's Bus ID is simply the object name.
8781da177e4SLinus Torvalds 	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
8791da177e4SLinus Torvalds 	 */
8801da177e4SLinus Torvalds 	switch (type) {
8811da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SYSTEM:
8821da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "ACPI");
8831da177e4SLinus Torvalds 		break;
8841da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
8851da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "PWRF");
8861da177e4SLinus Torvalds 		break;
8871da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
8881da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "SLPF");
8891da177e4SLinus Torvalds 		break;
8901da177e4SLinus Torvalds 	default:
8911da177e4SLinus Torvalds 		acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
8921da177e4SLinus Torvalds 		/* Clean up trailing underscores (if any) */
8931da177e4SLinus Torvalds 		for (i = 3; i > 1; i--) {
8941da177e4SLinus Torvalds 			if (bus_id[i] == '_')
8951da177e4SLinus Torvalds 				bus_id[i] = '\0';
8961da177e4SLinus Torvalds 			else
8971da177e4SLinus Torvalds 				break;
8981da177e4SLinus Torvalds 		}
8991da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, bus_id);
9001da177e4SLinus Torvalds 		break;
9011da177e4SLinus Torvalds 	}
9021da177e4SLinus Torvalds }
9031da177e4SLinus Torvalds 
904ae843332SZhang Rui static int
905ae843332SZhang Rui acpi_video_bus_match(struct acpi_device *device)
906ae843332SZhang Rui {
90766fb9d12SThomas Renninger 	acpi_handle h_dummy;
908ae843332SZhang Rui 
909ae843332SZhang Rui 	if (!device)
910ae843332SZhang Rui 		return -EINVAL;
911ae843332SZhang Rui 
912ae843332SZhang Rui 	/* Since there is no HID, CID for ACPI Video drivers, we have
913ae843332SZhang Rui 	 * to check well known required nodes for each feature we support.
914ae843332SZhang Rui 	 */
915ae843332SZhang Rui 
916ae843332SZhang Rui 	/* Does this device able to support video switching ? */
91766fb9d12SThomas Renninger 	if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOD", &h_dummy)) &&
91866fb9d12SThomas Renninger 	    ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOS", &h_dummy)))
919ae843332SZhang Rui 		return 0;
920ae843332SZhang Rui 
921ae843332SZhang Rui 	/* Does this device able to retrieve a video ROM ? */
92266fb9d12SThomas Renninger 	if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_ROM", &h_dummy)))
923ae843332SZhang Rui 		return 0;
924ae843332SZhang Rui 
925ae843332SZhang Rui 	/* Does this device able to configure which video head to be POSTed ? */
92666fb9d12SThomas Renninger 	if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_VPO", &h_dummy)) &&
92766fb9d12SThomas Renninger 	    ACPI_SUCCESS(acpi_get_handle(device->handle, "_GPD", &h_dummy)) &&
92866fb9d12SThomas Renninger 	    ACPI_SUCCESS(acpi_get_handle(device->handle, "_SPD", &h_dummy)))
929ae843332SZhang Rui 		return 0;
930ae843332SZhang Rui 
931ae843332SZhang Rui 	return -ENODEV;
932ae843332SZhang Rui }
933ae843332SZhang Rui 
93454735266SZhang Rui /*
93554735266SZhang Rui  * acpi_bay_match - see if a device is an ejectable driver bay
93654735266SZhang Rui  *
93754735266SZhang Rui  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
93854735266SZhang Rui  * then we can safely call it an ejectable drive bay
93954735266SZhang Rui  */
94054735266SZhang Rui static int acpi_bay_match(struct acpi_device *device){
94154735266SZhang Rui 	acpi_status status;
94254735266SZhang Rui 	acpi_handle handle;
94354735266SZhang Rui 	acpi_handle tmp;
94454735266SZhang Rui 	acpi_handle phandle;
94554735266SZhang Rui 
94654735266SZhang Rui 	handle = device->handle;
94754735266SZhang Rui 
94854735266SZhang Rui 	status = acpi_get_handle(handle, "_EJ0", &tmp);
94954735266SZhang Rui 	if (ACPI_FAILURE(status))
95054735266SZhang Rui 		return -ENODEV;
95154735266SZhang Rui 
95254735266SZhang Rui 	if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
95354735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
95454735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
95554735266SZhang Rui 		(ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
95654735266SZhang Rui 		return 0;
95754735266SZhang Rui 
95854735266SZhang Rui 	if (acpi_get_parent(handle, &phandle))
95954735266SZhang Rui 		return -ENODEV;
96054735266SZhang Rui 
96154735266SZhang Rui         if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
96254735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
96354735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
96454735266SZhang Rui                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
96554735266SZhang Rui                 return 0;
96654735266SZhang Rui 
96754735266SZhang Rui 	return -ENODEV;
96854735266SZhang Rui }
96954735266SZhang Rui 
9703620f2f2SFrank Seidel /*
9713620f2f2SFrank Seidel  * acpi_dock_match - see if a device has a _DCK method
9723620f2f2SFrank Seidel  */
9733620f2f2SFrank Seidel static int acpi_dock_match(struct acpi_device *device)
9743620f2f2SFrank Seidel {
9753620f2f2SFrank Seidel 	acpi_handle tmp;
9763620f2f2SFrank Seidel 	return acpi_get_handle(device->handle, "_DCK", &tmp);
9773620f2f2SFrank Seidel }
9783620f2f2SFrank Seidel 
9794be44fcdSLen Brown static void acpi_device_set_id(struct acpi_device *device,
9804be44fcdSLen Brown 			       struct acpi_device *parent, acpi_handle handle,
9814be44fcdSLen Brown 			       int type)
9821da177e4SLinus Torvalds {
9831da177e4SLinus Torvalds 	struct acpi_device_info *info;
9841da177e4SLinus Torvalds 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
9851da177e4SLinus Torvalds 	char *hid = NULL;
9861da177e4SLinus Torvalds 	char *uid = NULL;
9871da177e4SLinus Torvalds 	struct acpi_compatible_id_list *cid_list = NULL;
9883620f2f2SFrank Seidel 	const char *cid_add = NULL;
9891da177e4SLinus Torvalds 	acpi_status status;
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	switch (type) {
9921da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
9931da177e4SLinus Torvalds 		status = acpi_get_object_info(handle, &buffer);
9941da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
99596b2dd1fSHarvey Harrison 			printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
9961da177e4SLinus Torvalds 			return;
9971da177e4SLinus Torvalds 		}
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds 		info = buffer.pointer;
10001da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_HID)
10011da177e4SLinus Torvalds 			hid = info->hardware_id.value;
10021da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_UID)
10031da177e4SLinus Torvalds 			uid = info->unique_id.value;
10041da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_CID)
10051da177e4SLinus Torvalds 			cid_list = &info->compatibility_id;
10061da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_ADR) {
10071da177e4SLinus Torvalds 			device->pnp.bus_address = info->address;
10081da177e4SLinus Torvalds 			device->flags.bus_address = 1;
10091da177e4SLinus Torvalds 		}
1010ae843332SZhang Rui 
10113620f2f2SFrank Seidel 		/* If we have a video/bay/dock device, add our selfdefined
10123620f2f2SFrank Seidel 		   HID to the CID list. Like that the video/bay/dock drivers
10133620f2f2SFrank Seidel 		   will get autoloaded and the device might still match
10143620f2f2SFrank Seidel 		   against another driver.
10153620f2f2SFrank Seidel 		*/
10163620f2f2SFrank Seidel 		if (ACPI_SUCCESS(acpi_video_bus_match(device)))
10173620f2f2SFrank Seidel 			cid_add = ACPI_VIDEO_HID;
10183620f2f2SFrank Seidel 		else if (ACPI_SUCCESS(acpi_bay_match(device)))
10193620f2f2SFrank Seidel 			cid_add = ACPI_BAY_HID;
10203620f2f2SFrank Seidel 		else if (ACPI_SUCCESS(acpi_dock_match(device)))
10213620f2f2SFrank Seidel 			cid_add = ACPI_DOCK_HID;
102254735266SZhang Rui 
10231da177e4SLinus Torvalds 		break;
10241da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER:
10251da177e4SLinus Torvalds 		hid = ACPI_POWER_HID;
10261da177e4SLinus Torvalds 		break;
10271da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_PROCESSOR:
10281da177e4SLinus Torvalds 		hid = ACPI_PROCESSOR_HID;
10291da177e4SLinus Torvalds 		break;
10301da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SYSTEM:
10311da177e4SLinus Torvalds 		hid = ACPI_SYSTEM_HID;
10321da177e4SLinus Torvalds 		break;
10331da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_THERMAL:
10341da177e4SLinus Torvalds 		hid = ACPI_THERMAL_HID;
10351da177e4SLinus Torvalds 		break;
10361da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
10371da177e4SLinus Torvalds 		hid = ACPI_BUTTON_HID_POWERF;
10381da177e4SLinus Torvalds 		break;
10391da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
10401da177e4SLinus Torvalds 		hid = ACPI_BUTTON_HID_SLEEPF;
10411da177e4SLinus Torvalds 		break;
10421da177e4SLinus Torvalds 	}
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	/*
10451da177e4SLinus Torvalds 	 * \_SB
10461da177e4SLinus Torvalds 	 * ----
10471da177e4SLinus Torvalds 	 * Fix for the system root bus device -- the only root-level device.
10481da177e4SLinus Torvalds 	 */
1049c51a4de8SBob Moore 	if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
10501da177e4SLinus Torvalds 		hid = ACPI_BUS_HID;
10511da177e4SLinus Torvalds 		strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
10521da177e4SLinus Torvalds 		strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
10531da177e4SLinus Torvalds 	}
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	if (hid) {
10561da177e4SLinus Torvalds 		strcpy(device->pnp.hardware_id, hid);
10571da177e4SLinus Torvalds 		device->flags.hardware_id = 1;
10581da177e4SLinus Torvalds 	}
10591da177e4SLinus Torvalds 	if (uid) {
10601da177e4SLinus Torvalds 		strcpy(device->pnp.unique_id, uid);
10611da177e4SLinus Torvalds 		device->flags.unique_id = 1;
10621da177e4SLinus Torvalds 	}
10633620f2f2SFrank Seidel 	if (cid_list || cid_add) {
10643620f2f2SFrank Seidel 		struct  acpi_compatible_id_list *list;
10653620f2f2SFrank Seidel 		int size = 0;
10663620f2f2SFrank Seidel 		int count = 0;
10673620f2f2SFrank Seidel 
10681da177e4SLinus Torvalds 		if (cid_list) {
10693620f2f2SFrank Seidel 			size = cid_list->size;
10703620f2f2SFrank Seidel 		} else if (cid_add) {
10713620f2f2SFrank Seidel 			size = sizeof(struct acpi_compatible_id_list);
10723620f2f2SFrank Seidel 			cid_list = ACPI_ALLOCATE_ZEROED((acpi_size) size);
10733620f2f2SFrank Seidel 			if (!cid_list) {
10743620f2f2SFrank Seidel 				printk(KERN_ERR "Memory allocation error\n");
10753620f2f2SFrank Seidel 				kfree(buffer.pointer);
10763620f2f2SFrank Seidel 				return;
10773620f2f2SFrank Seidel 			} else {
10783620f2f2SFrank Seidel 				cid_list->count = 0;
10793620f2f2SFrank Seidel 				cid_list->size = size;
10803620f2f2SFrank Seidel 			}
10813620f2f2SFrank Seidel 		}
10823620f2f2SFrank Seidel 		if (cid_add)
10833620f2f2SFrank Seidel 			size += sizeof(struct acpi_compatible_id);
10843620f2f2SFrank Seidel 		list = kmalloc(size, GFP_KERNEL);
10853620f2f2SFrank Seidel 
10863620f2f2SFrank Seidel 		if (list) {
10873620f2f2SFrank Seidel 			if (cid_list) {
10883620f2f2SFrank Seidel 				memcpy(list, cid_list, cid_list->size);
10893620f2f2SFrank Seidel 				count = cid_list->count;
10903620f2f2SFrank Seidel 			}
10913620f2f2SFrank Seidel 			if (cid_add) {
10923620f2f2SFrank Seidel 				strncpy(list->id[count].value, cid_add,
10933620f2f2SFrank Seidel 					ACPI_MAX_CID_LENGTH);
10943620f2f2SFrank Seidel 				count++;
10953620f2f2SFrank Seidel 				device->flags.compatible_ids = 1;
10963620f2f2SFrank Seidel 			}
10973620f2f2SFrank Seidel 			list->size = size;
10983620f2f2SFrank Seidel 			list->count = count;
10993620f2f2SFrank Seidel 			device->pnp.cid_list = list;
11003620f2f2SFrank Seidel 		} else
110187ecd5cdSLen Brown 			printk(KERN_ERR PREFIX "Memory allocation error\n");
11021da177e4SLinus Torvalds 	}
11031da177e4SLinus Torvalds 
110402438d87SLen Brown 	kfree(buffer.pointer);
11051da177e4SLinus Torvalds }
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds static int acpi_device_set_context(struct acpi_device *device, int type)
11081da177e4SLinus Torvalds {
11091da177e4SLinus Torvalds 	acpi_status status = AE_OK;
11101da177e4SLinus Torvalds 	int result = 0;
11111da177e4SLinus Torvalds 	/*
11121da177e4SLinus Torvalds 	 * Context
11131da177e4SLinus Torvalds 	 * -------
11141da177e4SLinus Torvalds 	 * Attach this 'struct acpi_device' to the ACPI object.  This makes
11151da177e4SLinus Torvalds 	 * resolutions from handle->device very efficient.  Note that we need
11161da177e4SLinus Torvalds 	 * to be careful with fixed-feature devices as they all attach to the
11171da177e4SLinus Torvalds 	 * root object.
11181da177e4SLinus Torvalds 	 */
11191da177e4SLinus Torvalds 	if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
11201da177e4SLinus Torvalds 	    type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
11211da177e4SLinus Torvalds 		status = acpi_attach_data(device->handle,
11221da177e4SLinus Torvalds 					  acpi_bus_data_handler, device);
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
112587ecd5cdSLen Brown 			printk(KERN_ERR PREFIX "Error attaching device data\n");
11261da177e4SLinus Torvalds 			result = -ENODEV;
11271da177e4SLinus Torvalds 		}
11281da177e4SLinus Torvalds 	}
11291da177e4SLinus Torvalds 	return result;
11301da177e4SLinus Torvalds }
11311da177e4SLinus Torvalds 
11324be44fcdSLen Brown static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
11331da177e4SLinus Torvalds {
11341da177e4SLinus Torvalds 	if (!dev)
1135d550d98dSPatrick Mochel 		return -EINVAL;
11361da177e4SLinus Torvalds 
113796333578SLi Shaohua 	dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
11381890a97aSPatrick Mochel 	device_release_driver(&dev->dev);
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 	if (!rmdevice)
1141d550d98dSPatrick Mochel 		return 0;
11421da177e4SLinus Torvalds 
11432786f6e3SRui Zhang 	/*
11442786f6e3SRui Zhang 	 * unbind _ADR-Based Devices when hot removal
11452786f6e3SRui Zhang 	 */
11461da177e4SLinus Torvalds 	if (dev->flags.bus_address) {
11471da177e4SLinus Torvalds 		if ((dev->parent) && (dev->parent->ops.unbind))
11481da177e4SLinus Torvalds 			dev->parent->ops.unbind(dev);
11491da177e4SLinus Torvalds 	}
11501da177e4SLinus Torvalds 	acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
11511da177e4SLinus Torvalds 
1152d550d98dSPatrick Mochel 	return 0;
11531da177e4SLinus Torvalds }
11541da177e4SLinus Torvalds 
11553fb02738SRajesh Shah static int
11563620f2f2SFrank Seidel acpi_is_child_device(struct acpi_device *device,
11573620f2f2SFrank Seidel 			int (*matcher)(struct acpi_device *))
11583620f2f2SFrank Seidel {
11593620f2f2SFrank Seidel 	int result = -ENODEV;
11603620f2f2SFrank Seidel 
11613620f2f2SFrank Seidel 	do {
11623620f2f2SFrank Seidel 		if (ACPI_SUCCESS(matcher(device)))
11633620f2f2SFrank Seidel 			return AE_OK;
11643620f2f2SFrank Seidel 	} while ((device = device->parent));
11653620f2f2SFrank Seidel 
11663620f2f2SFrank Seidel 	return result;
11673620f2f2SFrank Seidel }
11683620f2f2SFrank Seidel 
11693620f2f2SFrank Seidel static int
11704be44fcdSLen Brown acpi_add_single_object(struct acpi_device **child,
1171c4168bffSLi Shaohua 		       struct acpi_device *parent, acpi_handle handle, int type,
1172c4168bffSLi Shaohua 			struct acpi_bus_ops *ops)
11731da177e4SLinus Torvalds {
11741da177e4SLinus Torvalds 	int result = 0;
11751da177e4SLinus Torvalds 	struct acpi_device *device = NULL;
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 	if (!child)
1179d550d98dSPatrick Mochel 		return -EINVAL;
11801da177e4SLinus Torvalds 
118136bcbec7SBurman Yan 	device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
11821da177e4SLinus Torvalds 	if (!device) {
11836468463aSLen Brown 		printk(KERN_ERR PREFIX "Memory allocation error\n");
1184d550d98dSPatrick Mochel 		return -ENOMEM;
11851da177e4SLinus Torvalds 	}
11861da177e4SLinus Torvalds 
11871da177e4SLinus Torvalds 	device->handle = handle;
11881da177e4SLinus Torvalds 	device->parent = parent;
1189c4168bffSLi Shaohua 	device->bus_ops = *ops; /* workround for not call .start */
1190c4168bffSLi Shaohua 
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	acpi_device_get_busid(device, handle, type);
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 	/*
11951da177e4SLinus Torvalds 	 * Flags
11961da177e4SLinus Torvalds 	 * -----
11971da177e4SLinus Torvalds 	 * Get prior to calling acpi_bus_get_status() so we know whether
11981da177e4SLinus Torvalds 	 * or not _STA is present.  Note that we only look for object
11991da177e4SLinus Torvalds 	 * handles -- cannot evaluate objects until we know the device is
12001da177e4SLinus Torvalds 	 * present and properly initialized.
12011da177e4SLinus Torvalds 	 */
12021da177e4SLinus Torvalds 	result = acpi_bus_get_flags(device);
12031da177e4SLinus Torvalds 	if (result)
12041da177e4SLinus Torvalds 		goto end;
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds 	/*
12071da177e4SLinus Torvalds 	 * Status
12081da177e4SLinus Torvalds 	 * ------
12096940fabaSKeiichiro Tokunaga 	 * See if the device is present.  We always assume that non-Device
12106940fabaSKeiichiro Tokunaga 	 * and non-Processor objects (e.g. thermal zones, power resources,
12116940fabaSKeiichiro Tokunaga 	 * etc.) are present, functioning, etc. (at least when parent object
12126940fabaSKeiichiro Tokunaga 	 * is present).  Note that _STA has a different meaning for some
12136940fabaSKeiichiro Tokunaga 	 * objects (e.g. power resources) so we need to be careful how we use
12146940fabaSKeiichiro Tokunaga 	 * it.
12151da177e4SLinus Torvalds 	 */
12161da177e4SLinus Torvalds 	switch (type) {
12176940fabaSKeiichiro Tokunaga 	case ACPI_BUS_TYPE_PROCESSOR:
12181da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
12191da177e4SLinus Torvalds 		result = acpi_bus_get_status(device);
12203620f2f2SFrank Seidel 		if (ACPI_FAILURE(result)) {
12213620f2f2SFrank Seidel 			result = -ENODEV;
12221da177e4SLinus Torvalds 			goto end;
12231da177e4SLinus Torvalds 		}
12243620f2f2SFrank Seidel 		if (!device->status.present) {
12253620f2f2SFrank Seidel 			/* Bay and dock should be handled even if absent */
12263620f2f2SFrank Seidel 			if (!ACPI_SUCCESS(
12273620f2f2SFrank Seidel 			     acpi_is_child_device(device, acpi_bay_match)) &&
12283620f2f2SFrank Seidel 			    !ACPI_SUCCESS(
12293620f2f2SFrank Seidel 			     acpi_is_child_device(device, acpi_dock_match))) {
12303620f2f2SFrank Seidel 					result = -ENODEV;
12313620f2f2SFrank Seidel 					goto end;
12323620f2f2SFrank Seidel 			}
12333620f2f2SFrank Seidel 		}
12341da177e4SLinus Torvalds 		break;
12351da177e4SLinus Torvalds 	default:
12360c0e8921SBjorn Helgaas 		STRUCT_TO_INT(device->status) =
12370c0e8921SBjorn Helgaas 		    ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
12380c0e8921SBjorn Helgaas 		    ACPI_STA_DEVICE_UI      | ACPI_STA_DEVICE_FUNCTIONING;
12391da177e4SLinus Torvalds 		break;
12401da177e4SLinus Torvalds 	}
12411da177e4SLinus Torvalds 
12421da177e4SLinus Torvalds 	/*
12431da177e4SLinus Torvalds 	 * Initialize Device
12441da177e4SLinus Torvalds 	 * -----------------
12451da177e4SLinus Torvalds 	 * TBD: Synch with Core's enumeration/initialization process.
12461da177e4SLinus Torvalds 	 */
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds 	/*
12491da177e4SLinus Torvalds 	 * Hardware ID, Unique ID, & Bus Address
12501da177e4SLinus Torvalds 	 * -------------------------------------
12511da177e4SLinus Torvalds 	 */
12521da177e4SLinus Torvalds 	acpi_device_set_id(device, parent, handle, type);
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds 	/*
12551da177e4SLinus Torvalds 	 * Power Management
12561da177e4SLinus Torvalds 	 * ----------------
12571da177e4SLinus Torvalds 	 */
12581da177e4SLinus Torvalds 	if (device->flags.power_manageable) {
12591da177e4SLinus Torvalds 		result = acpi_bus_get_power_flags(device);
12601da177e4SLinus Torvalds 		if (result)
12611da177e4SLinus Torvalds 			goto end;
12621da177e4SLinus Torvalds 	}
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds 	/*
12651da177e4SLinus Torvalds 	 * Wakeup device management
12661da177e4SLinus Torvalds 	 *-----------------------
12671da177e4SLinus Torvalds 	 */
12681da177e4SLinus Torvalds 	if (device->flags.wake_capable) {
12691da177e4SLinus Torvalds 		result = acpi_bus_get_wakeup_device_flags(device);
12701da177e4SLinus Torvalds 		if (result)
12711da177e4SLinus Torvalds 			goto end;
12721da177e4SLinus Torvalds 	}
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 	/*
12751da177e4SLinus Torvalds 	 * Performance Management
12761da177e4SLinus Torvalds 	 * ----------------------
12771da177e4SLinus Torvalds 	 */
12781da177e4SLinus Torvalds 	if (device->flags.performance_manageable) {
12791da177e4SLinus Torvalds 		result = acpi_bus_get_perf_flags(device);
12801da177e4SLinus Torvalds 		if (result)
12811da177e4SLinus Torvalds 			goto end;
12821da177e4SLinus Torvalds 	}
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 	if ((result = acpi_device_set_context(device, type)))
12851da177e4SLinus Torvalds 		goto end;
12861da177e4SLinus Torvalds 
1287e49bd2ddSZhang Rui 	result = acpi_device_register(device, parent);
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds 	/*
12902786f6e3SRui Zhang 	 * Bind _ADR-Based Devices when hot add
12911da177e4SLinus Torvalds 	 */
12921da177e4SLinus Torvalds 	if (device->flags.bus_address) {
12931da177e4SLinus Torvalds 		if (device->parent && device->parent->ops.bind)
12941da177e4SLinus Torvalds 			device->parent->ops.bind(device);
12951da177e4SLinus Torvalds 	}
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds       end:
12981da177e4SLinus Torvalds 	if (!result)
12991da177e4SLinus Torvalds 		*child = device;
13001da177e4SLinus Torvalds 	else {
13011da177e4SLinus Torvalds 		kfree(device->pnp.cid_list);
13021da177e4SLinus Torvalds 		kfree(device);
13031da177e4SLinus Torvalds 	}
13041da177e4SLinus Torvalds 
1305d550d98dSPatrick Mochel 	return result;
13061da177e4SLinus Torvalds }
13071da177e4SLinus Torvalds 
13084be44fcdSLen Brown static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
13091da177e4SLinus Torvalds {
13101da177e4SLinus Torvalds 	acpi_status status = AE_OK;
13111da177e4SLinus Torvalds 	struct acpi_device *parent = NULL;
13121da177e4SLinus Torvalds 	struct acpi_device *child = NULL;
13131da177e4SLinus Torvalds 	acpi_handle phandle = NULL;
13141da177e4SLinus Torvalds 	acpi_handle chandle = NULL;
13151da177e4SLinus Torvalds 	acpi_object_type type = 0;
13161da177e4SLinus Torvalds 	u32 level = 1;
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds 	if (!start)
1320d550d98dSPatrick Mochel 		return -EINVAL;
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 	parent = start;
13231da177e4SLinus Torvalds 	phandle = start->handle;
13241da177e4SLinus Torvalds 
13251da177e4SLinus Torvalds 	/*
13261da177e4SLinus Torvalds 	 * Parse through the ACPI namespace, identify all 'devices', and
13271da177e4SLinus Torvalds 	 * create a new 'struct acpi_device' for each.
13281da177e4SLinus Torvalds 	 */
13291da177e4SLinus Torvalds 	while ((level > 0) && parent) {
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds 		status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
13321da177e4SLinus Torvalds 					      chandle, &chandle);
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds 		/*
13351da177e4SLinus Torvalds 		 * If this scope is exhausted then move our way back up.
13361da177e4SLinus Torvalds 		 */
13371da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
13381da177e4SLinus Torvalds 			level--;
13391da177e4SLinus Torvalds 			chandle = phandle;
13401da177e4SLinus Torvalds 			acpi_get_parent(phandle, &phandle);
13411da177e4SLinus Torvalds 			if (parent->parent)
13421da177e4SLinus Torvalds 				parent = parent->parent;
13431da177e4SLinus Torvalds 			continue;
13441da177e4SLinus Torvalds 		}
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 		status = acpi_get_type(chandle, &type);
13471da177e4SLinus Torvalds 		if (ACPI_FAILURE(status))
13481da177e4SLinus Torvalds 			continue;
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds 		/*
13511da177e4SLinus Torvalds 		 * If this is a scope object then parse it (depth-first).
13521da177e4SLinus Torvalds 		 */
13531da177e4SLinus Torvalds 		if (type == ACPI_TYPE_LOCAL_SCOPE) {
13541da177e4SLinus Torvalds 			level++;
13551da177e4SLinus Torvalds 			phandle = chandle;
13561da177e4SLinus Torvalds 			chandle = NULL;
13571da177e4SLinus Torvalds 			continue;
13581da177e4SLinus Torvalds 		}
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 		/*
13611da177e4SLinus Torvalds 		 * We're only interested in objects that we consider 'devices'.
13621da177e4SLinus Torvalds 		 */
13631da177e4SLinus Torvalds 		switch (type) {
13641da177e4SLinus Torvalds 		case ACPI_TYPE_DEVICE:
13651da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_DEVICE;
13661da177e4SLinus Torvalds 			break;
13671da177e4SLinus Torvalds 		case ACPI_TYPE_PROCESSOR:
13681da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_PROCESSOR;
13691da177e4SLinus Torvalds 			break;
13701da177e4SLinus Torvalds 		case ACPI_TYPE_THERMAL:
13711da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_THERMAL;
13721da177e4SLinus Torvalds 			break;
13731da177e4SLinus Torvalds 		case ACPI_TYPE_POWER:
13741da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_POWER;
13751da177e4SLinus Torvalds 			break;
13761da177e4SLinus Torvalds 		default:
13771da177e4SLinus Torvalds 			continue;
13781da177e4SLinus Torvalds 		}
13791da177e4SLinus Torvalds 
13803fb02738SRajesh Shah 		if (ops->acpi_op_add)
13813fb02738SRajesh Shah 			status = acpi_add_single_object(&child, parent,
1382c4168bffSLi Shaohua 				chandle, type, ops);
13833fb02738SRajesh Shah 		else
13843fb02738SRajesh Shah 			status = acpi_bus_get_device(chandle, &child);
13853fb02738SRajesh Shah 
13861da177e4SLinus Torvalds 		if (ACPI_FAILURE(status))
13871da177e4SLinus Torvalds 			continue;
13881da177e4SLinus Torvalds 
1389c4168bffSLi Shaohua 		if (ops->acpi_op_start && !(ops->acpi_op_add)) {
13903fb02738SRajesh Shah 			status = acpi_start_single_object(child);
13913fb02738SRajesh Shah 			if (ACPI_FAILURE(status))
13923fb02738SRajesh Shah 				continue;
13933fb02738SRajesh Shah 		}
13943fb02738SRajesh Shah 
13951da177e4SLinus Torvalds 		/*
13961da177e4SLinus Torvalds 		 * If the device is present, enabled, and functioning then
13971da177e4SLinus Torvalds 		 * parse its scope (depth-first).  Note that we need to
13981da177e4SLinus Torvalds 		 * represent absent devices to facilitate PnP notifications
13991da177e4SLinus Torvalds 		 * -- but only the subtree head (not all of its children,
14001da177e4SLinus Torvalds 		 * which will be enumerated when the parent is inserted).
14011da177e4SLinus Torvalds 		 *
14021da177e4SLinus Torvalds 		 * TBD: Need notifications and other detection mechanisms
14031da177e4SLinus Torvalds 		 *      in place before we can fully implement this.
14041da177e4SLinus Torvalds 		 */
14051da177e4SLinus Torvalds 		if (child->status.present) {
14061da177e4SLinus Torvalds 			status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
14071da177e4SLinus Torvalds 						      NULL, NULL);
14081da177e4SLinus Torvalds 			if (ACPI_SUCCESS(status)) {
14091da177e4SLinus Torvalds 				level++;
14101da177e4SLinus Torvalds 				phandle = chandle;
14111da177e4SLinus Torvalds 				chandle = NULL;
14121da177e4SLinus Torvalds 				parent = child;
14131da177e4SLinus Torvalds 			}
14141da177e4SLinus Torvalds 		}
14151da177e4SLinus Torvalds 	}
14161da177e4SLinus Torvalds 
1417d550d98dSPatrick Mochel 	return 0;
14181da177e4SLinus Torvalds }
14191da177e4SLinus Torvalds 
14203fb02738SRajesh Shah int
14214be44fcdSLen Brown acpi_bus_add(struct acpi_device **child,
14224be44fcdSLen Brown 	     struct acpi_device *parent, acpi_handle handle, int type)
14233fb02738SRajesh Shah {
14243fb02738SRajesh Shah 	int result;
14253fb02738SRajesh Shah 	struct acpi_bus_ops ops;
14263fb02738SRajesh Shah 
14273fb02738SRajesh Shah 	memset(&ops, 0, sizeof(ops));
14283fb02738SRajesh Shah 	ops.acpi_op_add = 1;
1429c4168bffSLi Shaohua 
1430c4168bffSLi Shaohua 	result = acpi_add_single_object(child, parent, handle, type, &ops);
1431c4168bffSLi Shaohua 	if (!result)
14323fb02738SRajesh Shah 		result = acpi_bus_scan(*child, &ops);
1433c4168bffSLi Shaohua 
1434d550d98dSPatrick Mochel 	return result;
14353fb02738SRajesh Shah }
14364be44fcdSLen Brown 
14373fb02738SRajesh Shah EXPORT_SYMBOL(acpi_bus_add);
14383fb02738SRajesh Shah 
14394be44fcdSLen Brown int acpi_bus_start(struct acpi_device *device)
14403fb02738SRajesh Shah {
14413fb02738SRajesh Shah 	int result;
14423fb02738SRajesh Shah 	struct acpi_bus_ops ops;
14433fb02738SRajesh Shah 
14443fb02738SRajesh Shah 
14453fb02738SRajesh Shah 	if (!device)
1446d550d98dSPatrick Mochel 		return -EINVAL;
14473fb02738SRajesh Shah 
14483fb02738SRajesh Shah 	result = acpi_start_single_object(device);
14493fb02738SRajesh Shah 	if (!result) {
14503fb02738SRajesh Shah 		memset(&ops, 0, sizeof(ops));
14513fb02738SRajesh Shah 		ops.acpi_op_start = 1;
14523fb02738SRajesh Shah 		result = acpi_bus_scan(device, &ops);
14533fb02738SRajesh Shah 	}
1454d550d98dSPatrick Mochel 	return result;
14553fb02738SRajesh Shah }
14564be44fcdSLen Brown 
14573fb02738SRajesh Shah EXPORT_SYMBOL(acpi_bus_start);
14581da177e4SLinus Torvalds 
1459ceaba663SKristen Accardi int acpi_bus_trim(struct acpi_device *start, int rmdevice)
14601da177e4SLinus Torvalds {
14611da177e4SLinus Torvalds 	acpi_status status;
14621da177e4SLinus Torvalds 	struct acpi_device *parent, *child;
14631da177e4SLinus Torvalds 	acpi_handle phandle, chandle;
14641da177e4SLinus Torvalds 	acpi_object_type type;
14651da177e4SLinus Torvalds 	u32 level = 1;
14661da177e4SLinus Torvalds 	int err = 0;
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds 	parent = start;
14691da177e4SLinus Torvalds 	phandle = start->handle;
14701da177e4SLinus Torvalds 	child = chandle = NULL;
14711da177e4SLinus Torvalds 
14721da177e4SLinus Torvalds 	while ((level > 0) && parent && (!err)) {
14731da177e4SLinus Torvalds 		status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
14741da177e4SLinus Torvalds 					      chandle, &chandle);
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds 		/*
14771da177e4SLinus Torvalds 		 * If this scope is exhausted then move our way back up.
14781da177e4SLinus Torvalds 		 */
14791da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
14801da177e4SLinus Torvalds 			level--;
14811da177e4SLinus Torvalds 			chandle = phandle;
14821da177e4SLinus Torvalds 			acpi_get_parent(phandle, &phandle);
14831da177e4SLinus Torvalds 			child = parent;
14841da177e4SLinus Torvalds 			parent = parent->parent;
14851da177e4SLinus Torvalds 
14861da177e4SLinus Torvalds 			if (level == 0)
14871da177e4SLinus Torvalds 				err = acpi_bus_remove(child, rmdevice);
14881da177e4SLinus Torvalds 			else
14891da177e4SLinus Torvalds 				err = acpi_bus_remove(child, 1);
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 			continue;
14921da177e4SLinus Torvalds 		}
14931da177e4SLinus Torvalds 
14941da177e4SLinus Torvalds 		status = acpi_get_type(chandle, &type);
14951da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
14961da177e4SLinus Torvalds 			continue;
14971da177e4SLinus Torvalds 		}
14981da177e4SLinus Torvalds 		/*
14991da177e4SLinus Torvalds 		 * If there is a device corresponding to chandle then
15001da177e4SLinus Torvalds 		 * parse it (depth-first).
15011da177e4SLinus Torvalds 		 */
15021da177e4SLinus Torvalds 		if (acpi_bus_get_device(chandle, &child) == 0) {
15031da177e4SLinus Torvalds 			level++;
15041da177e4SLinus Torvalds 			phandle = chandle;
15051da177e4SLinus Torvalds 			chandle = NULL;
15061da177e4SLinus Torvalds 			parent = child;
15071da177e4SLinus Torvalds 		}
15081da177e4SLinus Torvalds 		continue;
15091da177e4SLinus Torvalds 	}
15101da177e4SLinus Torvalds 	return err;
15111da177e4SLinus Torvalds }
1512ceaba663SKristen Accardi EXPORT_SYMBOL_GPL(acpi_bus_trim);
1513ceaba663SKristen Accardi 
15141da177e4SLinus Torvalds 
15154be44fcdSLen Brown static int acpi_bus_scan_fixed(struct acpi_device *root)
15161da177e4SLinus Torvalds {
15171da177e4SLinus Torvalds 	int result = 0;
15181da177e4SLinus Torvalds 	struct acpi_device *device = NULL;
1519c4168bffSLi Shaohua 	struct acpi_bus_ops ops;
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds 	if (!root)
1522d550d98dSPatrick Mochel 		return -ENODEV;
15231da177e4SLinus Torvalds 
1524c4168bffSLi Shaohua 	memset(&ops, 0, sizeof(ops));
1525c4168bffSLi Shaohua 	ops.acpi_op_add = 1;
1526c4168bffSLi Shaohua 	ops.acpi_op_start = 1;
1527c4168bffSLi Shaohua 
15281da177e4SLinus Torvalds 	/*
15291da177e4SLinus Torvalds 	 * Enumerate all fixed-feature devices.
15301da177e4SLinus Torvalds 	 */
1531cee324b1SAlexey Starikovskiy 	if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
15323fb02738SRajesh Shah 		result = acpi_add_single_object(&device, acpi_root,
15334be44fcdSLen Brown 						NULL,
1534c4168bffSLi Shaohua 						ACPI_BUS_TYPE_POWER_BUTTON,
1535c4168bffSLi Shaohua 						&ops);
15363fb02738SRajesh Shah 	}
15371da177e4SLinus Torvalds 
1538cee324b1SAlexey Starikovskiy 	if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
15393fb02738SRajesh Shah 		result = acpi_add_single_object(&device, acpi_root,
15404be44fcdSLen Brown 						NULL,
1541c4168bffSLi Shaohua 						ACPI_BUS_TYPE_SLEEP_BUTTON,
1542c4168bffSLi Shaohua 						&ops);
15433fb02738SRajesh Shah 	}
15441da177e4SLinus Torvalds 
1545d550d98dSPatrick Mochel 	return result;
15461da177e4SLinus Torvalds }
15471da177e4SLinus Torvalds 
1548c04209a7SAlexey Starikovskiy int __init acpi_boot_ec_enable(void);
1549c04209a7SAlexey Starikovskiy 
15501da177e4SLinus Torvalds static int __init acpi_scan_init(void)
15511da177e4SLinus Torvalds {
15521da177e4SLinus Torvalds 	int result;
15533fb02738SRajesh Shah 	struct acpi_bus_ops ops;
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds 
15561da177e4SLinus Torvalds 	if (acpi_disabled)
1557d550d98dSPatrick Mochel 		return 0;
15581da177e4SLinus Torvalds 
1559c4168bffSLi Shaohua 	memset(&ops, 0, sizeof(ops));
1560c4168bffSLi Shaohua 	ops.acpi_op_add = 1;
1561c4168bffSLi Shaohua 	ops.acpi_op_start = 1;
15621da177e4SLinus Torvalds 
15635b327265SPatrick Mochel 	result = bus_register(&acpi_bus_type);
15645b327265SPatrick Mochel 	if (result) {
15655b327265SPatrick Mochel 		/* We don't want to quit even if we failed to add suspend/resume */
15665b327265SPatrick Mochel 		printk(KERN_ERR PREFIX "Could not register bus type\n");
15675b327265SPatrick Mochel 	}
15685b327265SPatrick Mochel 
15691da177e4SLinus Torvalds 	/*
15701da177e4SLinus Torvalds 	 * Create the root device in the bus's device tree
15711da177e4SLinus Torvalds 	 */
15723fb02738SRajesh Shah 	result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
1573c4168bffSLi Shaohua 					ACPI_BUS_TYPE_SYSTEM, &ops);
15741da177e4SLinus Torvalds 	if (result)
15751da177e4SLinus Torvalds 		goto Done;
15761da177e4SLinus Torvalds 
15771da177e4SLinus Torvalds 	/*
15781da177e4SLinus Torvalds 	 * Enumerate devices in the ACPI namespace.
15791da177e4SLinus Torvalds 	 */
15801da177e4SLinus Torvalds 	result = acpi_bus_scan_fixed(acpi_root);
1581c04209a7SAlexey Starikovskiy 
1582c04209a7SAlexey Starikovskiy 	/* EC region might be needed at bus_scan, so enable it now */
1583c04209a7SAlexey Starikovskiy 	acpi_boot_ec_enable();
1584c04209a7SAlexey Starikovskiy 
1585c4168bffSLi Shaohua 	if (!result)
15863fb02738SRajesh Shah 		result = acpi_bus_scan(acpi_root, &ops);
15871da177e4SLinus Torvalds 
15881da177e4SLinus Torvalds 	if (result)
15891da177e4SLinus Torvalds 		acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds       Done:
1592d550d98dSPatrick Mochel 	return result;
15931da177e4SLinus Torvalds }
15941da177e4SLinus Torvalds 
15951da177e4SLinus Torvalds subsys_initcall(acpi_scan_init);
1596