xref: /openbmc/linux/drivers/acpi/scan.c (revision 67be2dd1bace0ec7ce2dbc1bba3f8df3d7be597e)
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>
71da177e4SLinus Torvalds #include <linux/acpi.h>
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <acpi/acpi_drivers.h>
101da177e4SLinus Torvalds #include <acpi/acinterp.h>	/* for acpi_ex_eisa_id_to_string() */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #define _COMPONENT		ACPI_BUS_COMPONENT
141da177e4SLinus Torvalds ACPI_MODULE_NAME		("scan")
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #define STRUCT_TO_INT(s)	(*((int*)&s))
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds extern struct acpi_device		*acpi_root;
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #define ACPI_BUS_CLASS			"system_bus"
221da177e4SLinus Torvalds #define ACPI_BUS_HID			"ACPI_BUS"
231da177e4SLinus Torvalds #define ACPI_BUS_DRIVER_NAME		"ACPI Bus Driver"
241da177e4SLinus Torvalds #define ACPI_BUS_DEVICE_NAME		"System Bus"
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds static LIST_HEAD(acpi_device_list);
271da177e4SLinus Torvalds DEFINE_SPINLOCK(acpi_device_lock);
281da177e4SLinus Torvalds LIST_HEAD(acpi_wakeup_device_list);
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds static int
311da177e4SLinus Torvalds acpi_bus_trim(struct acpi_device	*start,
321da177e4SLinus Torvalds 		int rmdevice);
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds static void acpi_device_release(struct kobject * kobj)
351da177e4SLinus Torvalds {
361da177e4SLinus Torvalds 	struct acpi_device * dev = container_of(kobj,struct acpi_device,kobj);
371da177e4SLinus Torvalds 	if (dev->pnp.cid_list)
381da177e4SLinus Torvalds 		kfree(dev->pnp.cid_list);
391da177e4SLinus Torvalds 	kfree(dev);
401da177e4SLinus Torvalds }
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds struct acpi_device_attribute {
431da177e4SLinus Torvalds 	struct attribute attr;
441da177e4SLinus Torvalds 	ssize_t (*show)(struct acpi_device *, char *);
451da177e4SLinus Torvalds 	ssize_t (*store)(struct acpi_device *, const char *, size_t);
461da177e4SLinus Torvalds };
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds typedef void acpi_device_sysfs_files(struct kobject *,
491da177e4SLinus Torvalds 				const struct attribute *);
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds static void setup_sys_fs_device_files(struct acpi_device *dev,
521da177e4SLinus Torvalds 		acpi_device_sysfs_files *func);
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds #define create_sysfs_device_files(dev)	\
551da177e4SLinus Torvalds 	setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_create_file)
561da177e4SLinus Torvalds #define remove_sysfs_device_files(dev)	\
571da177e4SLinus Torvalds 	setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_remove_file)
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds #define to_acpi_device(n) container_of(n, struct acpi_device, kobj)
611da177e4SLinus Torvalds #define to_handle_attr(n) container_of(n, struct acpi_device_attribute, attr);
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds static ssize_t acpi_device_attr_show(struct kobject *kobj,
641da177e4SLinus Torvalds 		struct attribute *attr, char *buf)
651da177e4SLinus Torvalds {
661da177e4SLinus Torvalds 	struct acpi_device *device = to_acpi_device(kobj);
671da177e4SLinus Torvalds 	struct acpi_device_attribute *attribute = to_handle_attr(attr);
681da177e4SLinus Torvalds 	return attribute->show ? attribute->show(device, buf) : 0;
691da177e4SLinus Torvalds }
701da177e4SLinus Torvalds static ssize_t acpi_device_attr_store(struct kobject *kobj,
711da177e4SLinus Torvalds 		struct attribute *attr, const char *buf, size_t len)
721da177e4SLinus Torvalds {
731da177e4SLinus Torvalds 	struct acpi_device *device = to_acpi_device(kobj);
741da177e4SLinus Torvalds 	struct acpi_device_attribute *attribute = to_handle_attr(attr);
751da177e4SLinus Torvalds 	return attribute->store ? attribute->store(device, buf, len) : len;
761da177e4SLinus Torvalds }
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds static struct sysfs_ops acpi_device_sysfs_ops = {
791da177e4SLinus Torvalds 	.show	= acpi_device_attr_show,
801da177e4SLinus Torvalds 	.store	= acpi_device_attr_store,
811da177e4SLinus Torvalds };
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds static struct kobj_type ktype_acpi_ns = {
841da177e4SLinus Torvalds 	.sysfs_ops	= &acpi_device_sysfs_ops,
851da177e4SLinus Torvalds 	.release	= acpi_device_release,
861da177e4SLinus Torvalds };
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds static int namespace_hotplug(struct kset *kset, struct kobject *kobj,
891da177e4SLinus Torvalds 			     char **envp, int num_envp, char *buffer,
901da177e4SLinus Torvalds 			     int buffer_size)
911da177e4SLinus Torvalds {
921da177e4SLinus Torvalds 	struct acpi_device *dev = to_acpi_device(kobj);
931da177e4SLinus Torvalds 	int i = 0;
941da177e4SLinus Torvalds 	int len = 0;
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	if (!dev->driver)
971da177e4SLinus Torvalds 		return 0;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	if (add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, &len,
1001da177e4SLinus Torvalds 				"PHYSDEVDRIVER=%s", dev->driver->name))
1011da177e4SLinus Torvalds 		return -ENOMEM;
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 	envp[i] = NULL;
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	return 0;
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds static struct kset_hotplug_ops namespace_hotplug_ops = {
1091da177e4SLinus Torvalds 	.hotplug = &namespace_hotplug,
1101da177e4SLinus Torvalds };
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds static struct kset acpi_namespace_kset = {
1131da177e4SLinus Torvalds 	.kobj		= {
1141da177e4SLinus Torvalds 		.name = "namespace",
1151da177e4SLinus Torvalds 	},
1161da177e4SLinus Torvalds 	.subsys = &acpi_subsys,
1171da177e4SLinus Torvalds 	.ktype	= &ktype_acpi_ns,
1181da177e4SLinus Torvalds 	.hotplug_ops = &namespace_hotplug_ops,
1191da177e4SLinus Torvalds };
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds static void acpi_device_register(struct acpi_device * device, struct acpi_device * parent)
1231da177e4SLinus Torvalds {
1241da177e4SLinus Torvalds 	/*
1251da177e4SLinus Torvalds 	 * Linkage
1261da177e4SLinus Torvalds 	 * -------
1271da177e4SLinus Torvalds 	 * Link this device to its parent and siblings.
1281da177e4SLinus Torvalds 	 */
1291da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->children);
1301da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->node);
1311da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->g_list);
1321da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->wakeup_list);
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	spin_lock(&acpi_device_lock);
1351da177e4SLinus Torvalds 	if (device->parent) {
1361da177e4SLinus Torvalds 		list_add_tail(&device->node, &device->parent->children);
1371da177e4SLinus Torvalds 		list_add_tail(&device->g_list,&device->parent->g_list);
1381da177e4SLinus Torvalds 	} else
1391da177e4SLinus Torvalds 		list_add_tail(&device->g_list,&acpi_device_list);
1401da177e4SLinus Torvalds 	if (device->wakeup.flags.valid)
1411da177e4SLinus Torvalds 		list_add_tail(&device->wakeup_list,&acpi_wakeup_device_list);
1421da177e4SLinus Torvalds 	spin_unlock(&acpi_device_lock);
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	strlcpy(device->kobj.name,device->pnp.bus_id,KOBJ_NAME_LEN);
1451da177e4SLinus Torvalds 	if (parent)
1461da177e4SLinus Torvalds 		device->kobj.parent = &parent->kobj;
1471da177e4SLinus Torvalds 	device->kobj.ktype = &ktype_acpi_ns;
1481da177e4SLinus Torvalds 	device->kobj.kset = &acpi_namespace_kset;
1491da177e4SLinus Torvalds 	kobject_register(&device->kobj);
1501da177e4SLinus Torvalds 	create_sysfs_device_files(device);
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds static int
1541da177e4SLinus Torvalds acpi_device_unregister (
1551da177e4SLinus Torvalds 	struct acpi_device	*device,
1561da177e4SLinus Torvalds 	int			type)
1571da177e4SLinus Torvalds {
1581da177e4SLinus Torvalds 	spin_lock(&acpi_device_lock);
1591da177e4SLinus Torvalds 	if (device->parent) {
1601da177e4SLinus Torvalds 		list_del(&device->node);
1611da177e4SLinus Torvalds 		list_del(&device->g_list);
1621da177e4SLinus Torvalds 	} else
1631da177e4SLinus Torvalds 		list_del(&device->g_list);
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	list_del(&device->wakeup_list);
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	spin_unlock(&acpi_device_lock);
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	acpi_detach_data(device->handle, acpi_bus_data_handler);
1701da177e4SLinus Torvalds 	remove_sysfs_device_files(device);
1711da177e4SLinus Torvalds 	kobject_unregister(&device->kobj);
1721da177e4SLinus Torvalds 	return 0;
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds void
1761da177e4SLinus Torvalds acpi_bus_data_handler (
1771da177e4SLinus Torvalds 	acpi_handle		handle,
1781da177e4SLinus Torvalds 	u32			function,
1791da177e4SLinus Torvalds 	void			*context)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_bus_data_handler");
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	/* TBD */
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	return_VOID;
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds static int
1891da177e4SLinus Torvalds acpi_bus_get_power_flags (
1901da177e4SLinus Torvalds 	struct acpi_device	*device)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	acpi_status             status = 0;
1931da177e4SLinus Torvalds 	acpi_handle		handle = NULL;
1941da177e4SLinus Torvalds 	u32                     i = 0;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_bus_get_power_flags");
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	/*
1991da177e4SLinus Torvalds 	 * Power Management Flags
2001da177e4SLinus Torvalds 	 */
2011da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_PSC", &handle);
2021da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
2031da177e4SLinus Torvalds 		device->power.flags.explicit_get = 1;
2041da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_IRC", &handle);
2051da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
2061da177e4SLinus Torvalds 		device->power.flags.inrush_current = 1;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	/*
2091da177e4SLinus Torvalds 	 * Enumerate supported power management states
2101da177e4SLinus Torvalds 	 */
2111da177e4SLinus Torvalds 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
2121da177e4SLinus Torvalds 		struct acpi_device_power_state *ps = &device->power.states[i];
2131da177e4SLinus Torvalds 		char		object_name[5] = {'_','P','R','0'+i,'\0'};
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 		/* Evaluate "_PRx" to se if power resources are referenced */
2161da177e4SLinus Torvalds 		acpi_evaluate_reference(device->handle, object_name, NULL,
2171da177e4SLinus Torvalds 			&ps->resources);
2181da177e4SLinus Torvalds 		if (ps->resources.count) {
2191da177e4SLinus Torvalds 			device->power.flags.power_resources = 1;
2201da177e4SLinus Torvalds 			ps->flags.valid = 1;
2211da177e4SLinus Torvalds 		}
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 		/* Evaluate "_PSx" to see if we can do explicit sets */
2241da177e4SLinus Torvalds 		object_name[2] = 'S';
2251da177e4SLinus Torvalds 		status = acpi_get_handle(device->handle, object_name, &handle);
2261da177e4SLinus Torvalds 		if (ACPI_SUCCESS(status)) {
2271da177e4SLinus Torvalds 			ps->flags.explicit_set = 1;
2281da177e4SLinus Torvalds 			ps->flags.valid = 1;
2291da177e4SLinus Torvalds 		}
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 		/* State is valid if we have some power control */
2321da177e4SLinus Torvalds 		if (ps->resources.count || ps->flags.explicit_set)
2331da177e4SLinus Torvalds 			ps->flags.valid = 1;
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 		ps->power = -1;		/* Unknown - driver assigned */
2361da177e4SLinus Torvalds 		ps->latency = -1;	/* Unknown - driver assigned */
2371da177e4SLinus Torvalds 	}
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	/* Set defaults for D0 and D3 states (always valid) */
2401da177e4SLinus Torvalds 	device->power.states[ACPI_STATE_D0].flags.valid = 1;
2411da177e4SLinus Torvalds 	device->power.states[ACPI_STATE_D0].power = 100;
2421da177e4SLinus Torvalds 	device->power.states[ACPI_STATE_D3].flags.valid = 1;
2431da177e4SLinus Torvalds 	device->power.states[ACPI_STATE_D3].power = 0;
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	/* TBD: System wake support and resource requirements. */
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	device->power.state = ACPI_STATE_UNKNOWN;
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	return_VALUE(0);
2501da177e4SLinus Torvalds }
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds int
2531da177e4SLinus Torvalds acpi_match_ids (
2541da177e4SLinus Torvalds 	struct acpi_device	*device,
2551da177e4SLinus Torvalds 	char			*ids)
2561da177e4SLinus Torvalds {
2571da177e4SLinus Torvalds 	int error = 0;
2581da177e4SLinus Torvalds 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	if (device->flags.hardware_id)
2611da177e4SLinus Torvalds 		if (strstr(ids, device->pnp.hardware_id))
2621da177e4SLinus Torvalds 			goto Done;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	if (device->flags.compatible_ids) {
2651da177e4SLinus Torvalds 		struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
2661da177e4SLinus Torvalds 		int i;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 		/* compare multiple _CID entries against driver ids */
2691da177e4SLinus Torvalds 		for (i = 0; i < cid_list->count; i++)
2701da177e4SLinus Torvalds 		{
2711da177e4SLinus Torvalds 			if (strstr(ids, cid_list->id[i].value))
2721da177e4SLinus Torvalds 				goto Done;
2731da177e4SLinus Torvalds 		}
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds 	error = -ENOENT;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds  Done:
2781da177e4SLinus Torvalds 	if (buffer.pointer)
2791da177e4SLinus Torvalds 		acpi_os_free(buffer.pointer);
2801da177e4SLinus Torvalds 	return error;
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds static acpi_status
2841da177e4SLinus Torvalds acpi_bus_extract_wakeup_device_power_package (
2851da177e4SLinus Torvalds 	struct acpi_device	*device,
2861da177e4SLinus Torvalds 	union acpi_object	*package)
2871da177e4SLinus Torvalds {
2881da177e4SLinus Torvalds 	int 	 i = 0;
2891da177e4SLinus Torvalds 	union acpi_object	*element = NULL;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	if (!device || !package || (package->package.count < 2))
2921da177e4SLinus Torvalds 		return AE_BAD_PARAMETER;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	element = &(package->package.elements[0]);
2951da177e4SLinus Torvalds 	if (!element)
2961da177e4SLinus Torvalds 		return AE_BAD_PARAMETER;
2971da177e4SLinus Torvalds 	if (element->type == ACPI_TYPE_PACKAGE) {
2981da177e4SLinus Torvalds 		if ((element->package.count < 2) ||
2991da177e4SLinus Torvalds 			(element->package.elements[0].type != ACPI_TYPE_LOCAL_REFERENCE) ||
3001da177e4SLinus Torvalds 			(element->package.elements[1].type != ACPI_TYPE_INTEGER))
3011da177e4SLinus Torvalds 			return AE_BAD_DATA;
3021da177e4SLinus Torvalds 		device->wakeup.gpe_device = element->package.elements[0].reference.handle;
3031da177e4SLinus Torvalds 		device->wakeup.gpe_number = (u32)element->package.elements[1].integer.value;
3041da177e4SLinus Torvalds 	}else if (element->type == ACPI_TYPE_INTEGER) {
3051da177e4SLinus Torvalds 		device->wakeup.gpe_number = element->integer.value;
3061da177e4SLinus Torvalds 	}else
3071da177e4SLinus Torvalds 		return AE_BAD_DATA;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	element = &(package->package.elements[1]);
3101da177e4SLinus Torvalds 	if (element->type != ACPI_TYPE_INTEGER) {
3111da177e4SLinus Torvalds 		return AE_BAD_DATA;
3121da177e4SLinus Torvalds 	}
3131da177e4SLinus Torvalds 	device->wakeup.sleep_state = element->integer.value;
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
3161da177e4SLinus Torvalds 		return AE_NO_MEMORY;
3171da177e4SLinus Torvalds 	}
3181da177e4SLinus Torvalds 	device->wakeup.resources.count = package->package.count - 2;
3191da177e4SLinus Torvalds 	for (i=0; i < device->wakeup.resources.count; i++) {
3201da177e4SLinus Torvalds 		element = &(package->package.elements[i + 2]);
3211da177e4SLinus Torvalds 		if (element->type != ACPI_TYPE_ANY ) {
3221da177e4SLinus Torvalds 			return AE_BAD_DATA;
3231da177e4SLinus Torvalds 		}
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 		device->wakeup.resources.handles[i] = element->reference.handle;
3261da177e4SLinus Torvalds 	}
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	return AE_OK;
3291da177e4SLinus Torvalds }
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds static int
3321da177e4SLinus Torvalds acpi_bus_get_wakeup_device_flags (
3331da177e4SLinus Torvalds 	struct acpi_device	*device)
3341da177e4SLinus Torvalds {
3351da177e4SLinus Torvalds 	acpi_status	status = 0;
3361da177e4SLinus Torvalds 	struct acpi_buffer	buffer = {ACPI_ALLOCATE_BUFFER, NULL};
3371da177e4SLinus Torvalds 	union acpi_object	*package = NULL;
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_bus_get_wakeup_flags");
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	/* _PRW */
3421da177e4SLinus Torvalds 	status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
3431da177e4SLinus Torvalds 	if (ACPI_FAILURE(status)) {
3441da177e4SLinus Torvalds 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PRW\n"));
3451da177e4SLinus Torvalds 		goto end;
3461da177e4SLinus Torvalds 	}
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	package = (union acpi_object *) buffer.pointer;
3491da177e4SLinus Torvalds 	status = acpi_bus_extract_wakeup_device_power_package(device, package);
3501da177e4SLinus Torvalds 	if (ACPI_FAILURE(status)) {
3511da177e4SLinus Torvalds 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error extracting _PRW package\n"));
3521da177e4SLinus Torvalds 		goto end;
3531da177e4SLinus Torvalds 	}
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	acpi_os_free(buffer.pointer);
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	device->wakeup.flags.valid = 1;
3581da177e4SLinus Torvalds 	/* Power button, Lid switch always enable wakeup*/
3591da177e4SLinus Torvalds 	if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
3601da177e4SLinus Torvalds 		device->wakeup.flags.run_wake = 1;
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds end:
3631da177e4SLinus Torvalds 	if (ACPI_FAILURE(status))
3641da177e4SLinus Torvalds 		device->flags.wake_capable = 0;
3651da177e4SLinus Torvalds 	return_VALUE(0);
3661da177e4SLinus Torvalds }
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds /* --------------------------------------------------------------------------
3691da177e4SLinus Torvalds 		ACPI hotplug sysfs device file support
3701da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
3711da177e4SLinus Torvalds static ssize_t acpi_eject_store(struct acpi_device *device,
3721da177e4SLinus Torvalds 		const char *buf, size_t count);
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds #define ACPI_DEVICE_ATTR(_name,_mode,_show,_store) \
3751da177e4SLinus Torvalds static struct acpi_device_attribute acpi_device_attr_##_name = \
3761da177e4SLinus Torvalds 		__ATTR(_name, _mode, _show, _store)
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds ACPI_DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds /**
3811da177e4SLinus Torvalds  * setup_sys_fs_device_files - sets up the device files under device namespace
382*67be2dd1SMartin Waitz  * @dev:	acpi_device object
383*67be2dd1SMartin Waitz  * @func:	function pointer to create or destroy the device file
3841da177e4SLinus Torvalds  */
3851da177e4SLinus Torvalds static void
3861da177e4SLinus Torvalds setup_sys_fs_device_files (
3871da177e4SLinus Torvalds 	struct acpi_device *dev,
3881da177e4SLinus Torvalds 	acpi_device_sysfs_files *func)
3891da177e4SLinus Torvalds {
3901da177e4SLinus Torvalds 	acpi_status		status;
3911da177e4SLinus Torvalds 	acpi_handle		temp = NULL;
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	/*
3941da177e4SLinus Torvalds 	 * If device has _EJ0, 'eject' file is created that is used to trigger
3951da177e4SLinus Torvalds 	 * hot-removal function from userland.
3961da177e4SLinus Torvalds 	 */
3971da177e4SLinus Torvalds 	status = acpi_get_handle(dev->handle, "_EJ0", &temp);
3981da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
3991da177e4SLinus Torvalds 		(*(func))(&dev->kobj,&acpi_device_attr_eject.attr);
4001da177e4SLinus Torvalds }
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds static int
4031da177e4SLinus Torvalds acpi_eject_operation(acpi_handle handle, int lockable)
4041da177e4SLinus Torvalds {
4051da177e4SLinus Torvalds 	struct acpi_object_list arg_list;
4061da177e4SLinus Torvalds 	union acpi_object arg;
4071da177e4SLinus Torvalds 	acpi_status status = AE_OK;
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 	/*
4101da177e4SLinus Torvalds 	 * TBD: evaluate _PS3?
4111da177e4SLinus Torvalds 	 */
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	if (lockable) {
4141da177e4SLinus Torvalds 		arg_list.count = 1;
4151da177e4SLinus Torvalds 		arg_list.pointer = &arg;
4161da177e4SLinus Torvalds 		arg.type = ACPI_TYPE_INTEGER;
4171da177e4SLinus Torvalds 		arg.integer.value = 0;
4181da177e4SLinus Torvalds 		acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
4191da177e4SLinus Torvalds 	}
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	arg_list.count = 1;
4221da177e4SLinus Torvalds 	arg_list.pointer = &arg;
4231da177e4SLinus Torvalds 	arg.type = ACPI_TYPE_INTEGER;
4241da177e4SLinus Torvalds 	arg.integer.value = 1;
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	/*
4271da177e4SLinus Torvalds 	 * TBD: _EJD support.
4281da177e4SLinus Torvalds 	 */
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
4311da177e4SLinus Torvalds 	if (ACPI_FAILURE(status)) {
4321da177e4SLinus Torvalds 		return(-ENODEV);
4331da177e4SLinus Torvalds 	}
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 	return(0);
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds static ssize_t
4401da177e4SLinus Torvalds acpi_eject_store(struct acpi_device *device, const char *buf, size_t count)
4411da177e4SLinus Torvalds {
4421da177e4SLinus Torvalds 	int	result;
4431da177e4SLinus Torvalds 	int	ret = count;
4441da177e4SLinus Torvalds 	int	islockable;
4451da177e4SLinus Torvalds 	acpi_status	status;
4461da177e4SLinus Torvalds 	acpi_handle	handle;
4471da177e4SLinus Torvalds 	acpi_object_type	type = 0;
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	if ((!count) || (buf[0] != '1')) {
4501da177e4SLinus Torvalds 		return -EINVAL;
4511da177e4SLinus Torvalds 	}
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds #ifndef FORCE_EJECT
4541da177e4SLinus Torvalds 	if (device->driver == NULL) {
4551da177e4SLinus Torvalds 		ret = -ENODEV;
4561da177e4SLinus Torvalds 		goto err;
4571da177e4SLinus Torvalds 	}
4581da177e4SLinus Torvalds #endif
4591da177e4SLinus Torvalds 	status = acpi_get_type(device->handle, &type);
4601da177e4SLinus Torvalds 	if (ACPI_FAILURE(status) || (!device->flags.ejectable) ) {
4611da177e4SLinus Torvalds 		ret = -ENODEV;
4621da177e4SLinus Torvalds 		goto err;
4631da177e4SLinus Torvalds 	}
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	islockable = device->flags.lockable;
4661da177e4SLinus Torvalds 	handle = device->handle;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	if (type == ACPI_TYPE_PROCESSOR)
4691da177e4SLinus Torvalds 		result = acpi_bus_trim(device, 0);
4701da177e4SLinus Torvalds 	else
4711da177e4SLinus Torvalds 		result = acpi_bus_trim(device, 1);
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	if (!result)
4741da177e4SLinus Torvalds 		result = acpi_eject_operation(handle, islockable);
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	if (result) {
4771da177e4SLinus Torvalds 		ret = -EBUSY;
4781da177e4SLinus Torvalds 	}
4791da177e4SLinus Torvalds err:
4801da177e4SLinus Torvalds 	return ret;
4811da177e4SLinus Torvalds }
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds /* --------------------------------------------------------------------------
4851da177e4SLinus Torvalds                               Performance Management
4861da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds static int
4891da177e4SLinus Torvalds acpi_bus_get_perf_flags (
4901da177e4SLinus Torvalds 	struct acpi_device	*device)
4911da177e4SLinus Torvalds {
4921da177e4SLinus Torvalds 	device->performance.state = ACPI_STATE_UNKNOWN;
4931da177e4SLinus Torvalds 	return 0;
4941da177e4SLinus Torvalds }
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds /* --------------------------------------------------------------------------
4971da177e4SLinus Torvalds                                  Driver Management
4981da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds static LIST_HEAD(acpi_bus_drivers);
5011da177e4SLinus Torvalds static DECLARE_MUTEX(acpi_bus_drivers_lock);
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds /**
5051da177e4SLinus Torvalds  * acpi_bus_match
5061da177e4SLinus Torvalds  * --------------
5071da177e4SLinus Torvalds  * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it
5081da177e4SLinus Torvalds  * matches the specified driver's criteria.
5091da177e4SLinus Torvalds  */
5101da177e4SLinus Torvalds static int
5111da177e4SLinus Torvalds acpi_bus_match (
5121da177e4SLinus Torvalds 	struct acpi_device	*device,
5131da177e4SLinus Torvalds 	struct acpi_driver	*driver)
5141da177e4SLinus Torvalds {
5151da177e4SLinus Torvalds 	if (driver && driver->ops.match)
5161da177e4SLinus Torvalds 		return driver->ops.match(device, driver);
5171da177e4SLinus Torvalds 	return acpi_match_ids(device, driver->ids);
5181da177e4SLinus Torvalds }
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds /**
5221da177e4SLinus Torvalds  * acpi_bus_driver_init
5231da177e4SLinus Torvalds  * --------------------
5241da177e4SLinus Torvalds  * Used to initialize a device via its device driver.  Called whenever a
5251da177e4SLinus Torvalds  * driver is bound to a device.  Invokes the driver's add() and start() ops.
5261da177e4SLinus Torvalds  */
5271da177e4SLinus Torvalds static int
5281da177e4SLinus Torvalds acpi_bus_driver_init (
5291da177e4SLinus Torvalds 	struct acpi_device	*device,
5301da177e4SLinus Torvalds 	struct acpi_driver	*driver)
5311da177e4SLinus Torvalds {
5321da177e4SLinus Torvalds 	int			result = 0;
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_bus_driver_init");
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	if (!device || !driver)
5371da177e4SLinus Torvalds 		return_VALUE(-EINVAL);
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	if (!driver->ops.add)
5401da177e4SLinus Torvalds 		return_VALUE(-ENOSYS);
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	result = driver->ops.add(device);
5431da177e4SLinus Torvalds 	if (result) {
5441da177e4SLinus Torvalds 		device->driver = NULL;
5451da177e4SLinus Torvalds 		acpi_driver_data(device) = NULL;
5461da177e4SLinus Torvalds 		return_VALUE(result);
5471da177e4SLinus Torvalds 	}
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 	device->driver = driver;
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	/*
5521da177e4SLinus Torvalds 	 * TBD - Configuration Management: Assign resources to device based
5531da177e4SLinus Torvalds 	 * upon possible configuration and currently allocated resources.
5541da177e4SLinus Torvalds 	 */
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 	if (driver->ops.start) {
5571da177e4SLinus Torvalds 		result = driver->ops.start(device);
5581da177e4SLinus Torvalds 		if (result && driver->ops.remove)
5591da177e4SLinus Torvalds 			driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
5601da177e4SLinus Torvalds 		return_VALUE(result);
5611da177e4SLinus Torvalds 	}
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Driver successfully bound to device\n"));
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	if (driver->ops.scan) {
5661da177e4SLinus Torvalds 		driver->ops.scan(device);
5671da177e4SLinus Torvalds 	}
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	return_VALUE(0);
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds static int acpi_driver_attach(struct acpi_driver * drv)
5731da177e4SLinus Torvalds {
5741da177e4SLinus Torvalds 	struct list_head * node, * next;
5751da177e4SLinus Torvalds 	int count = 0;
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_driver_attach");
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	spin_lock(&acpi_device_lock);
5801da177e4SLinus Torvalds 	list_for_each_safe(node, next, &acpi_device_list) {
5811da177e4SLinus Torvalds 		struct acpi_device * dev = container_of(node, struct acpi_device, g_list);
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 		if (dev->driver || !dev->status.present)
5841da177e4SLinus Torvalds 			continue;
5851da177e4SLinus Torvalds 		spin_unlock(&acpi_device_lock);
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 		if (!acpi_bus_match(dev, drv)) {
5881da177e4SLinus Torvalds 			if (!acpi_bus_driver_init(dev, drv)) {
5891da177e4SLinus Torvalds 				atomic_inc(&drv->references);
5901da177e4SLinus Torvalds 				count++;
5911da177e4SLinus Torvalds 				ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
5921da177e4SLinus Torvalds 						  drv->name, dev->pnp.bus_id));
5931da177e4SLinus Torvalds 			}
5941da177e4SLinus Torvalds 		}
5951da177e4SLinus Torvalds 		spin_lock(&acpi_device_lock);
5961da177e4SLinus Torvalds 	}
5971da177e4SLinus Torvalds 	spin_unlock(&acpi_device_lock);
5981da177e4SLinus Torvalds 	return_VALUE(count);
5991da177e4SLinus Torvalds }
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds static int acpi_driver_detach(struct acpi_driver * drv)
6021da177e4SLinus Torvalds {
6031da177e4SLinus Torvalds 	struct list_head * node, * next;
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_driver_detach");
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	spin_lock(&acpi_device_lock);
6081da177e4SLinus Torvalds 	list_for_each_safe(node,next,&acpi_device_list) {
6091da177e4SLinus Torvalds 		struct acpi_device * dev = container_of(node,struct acpi_device,g_list);
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 		if (dev->driver == drv) {
6121da177e4SLinus Torvalds 			spin_unlock(&acpi_device_lock);
6131da177e4SLinus Torvalds 			if (drv->ops.remove)
6141da177e4SLinus Torvalds 				drv->ops.remove(dev,ACPI_BUS_REMOVAL_NORMAL);
6151da177e4SLinus Torvalds 			spin_lock(&acpi_device_lock);
6161da177e4SLinus Torvalds 			dev->driver = NULL;
6171da177e4SLinus Torvalds 			dev->driver_data = NULL;
6181da177e4SLinus Torvalds 			atomic_dec(&drv->references);
6191da177e4SLinus Torvalds 		}
6201da177e4SLinus Torvalds 	}
6211da177e4SLinus Torvalds 	spin_unlock(&acpi_device_lock);
6221da177e4SLinus Torvalds 	return_VALUE(0);
6231da177e4SLinus Torvalds }
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds /**
6261da177e4SLinus Torvalds  * acpi_bus_register_driver
6271da177e4SLinus Torvalds  * ------------------------
6281da177e4SLinus Torvalds  * Registers a driver with the ACPI bus.  Searches the namespace for all
6291da177e4SLinus Torvalds  * devices that match the driver's criteria and binds.  Returns the
6301da177e4SLinus Torvalds  * number of devices that were claimed by the driver, or a negative
6311da177e4SLinus Torvalds  * error status for failure.
6321da177e4SLinus Torvalds  */
6331da177e4SLinus Torvalds int
6341da177e4SLinus Torvalds acpi_bus_register_driver (
6351da177e4SLinus Torvalds 	struct acpi_driver	*driver)
6361da177e4SLinus Torvalds {
6371da177e4SLinus Torvalds 	int count;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_bus_register_driver");
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	if (acpi_disabled)
6421da177e4SLinus Torvalds 		return_VALUE(-ENODEV);
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	if (!driver)
6451da177e4SLinus Torvalds 		return_VALUE(-EINVAL);
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 	spin_lock(&acpi_device_lock);
6481da177e4SLinus Torvalds 	list_add_tail(&driver->node, &acpi_bus_drivers);
6491da177e4SLinus Torvalds 	spin_unlock(&acpi_device_lock);
6501da177e4SLinus Torvalds 	count = acpi_driver_attach(driver);
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	return_VALUE(count);
6531da177e4SLinus Torvalds }
6541da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_register_driver);
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds /**
6581da177e4SLinus Torvalds  * acpi_bus_unregister_driver
6591da177e4SLinus Torvalds  * --------------------------
6601da177e4SLinus Torvalds  * Unregisters a driver with the ACPI bus.  Searches the namespace for all
6611da177e4SLinus Torvalds  * devices that match the driver's criteria and unbinds.
6621da177e4SLinus Torvalds  */
6631da177e4SLinus Torvalds int
6641da177e4SLinus Torvalds acpi_bus_unregister_driver (
6651da177e4SLinus Torvalds 	struct acpi_driver	*driver)
6661da177e4SLinus Torvalds {
6671da177e4SLinus Torvalds 	int error = 0;
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_bus_unregister_driver");
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 	if (driver) {
6721da177e4SLinus Torvalds 		acpi_driver_detach(driver);
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 		if (!atomic_read(&driver->references)) {
6751da177e4SLinus Torvalds 			spin_lock(&acpi_device_lock);
6761da177e4SLinus Torvalds 			list_del_init(&driver->node);
6771da177e4SLinus Torvalds 			spin_unlock(&acpi_device_lock);
6781da177e4SLinus Torvalds 		}
6791da177e4SLinus Torvalds 	} else
6801da177e4SLinus Torvalds 		error = -EINVAL;
6811da177e4SLinus Torvalds 	return_VALUE(error);
6821da177e4SLinus Torvalds }
6831da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_unregister_driver);
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds /**
6861da177e4SLinus Torvalds  * acpi_bus_find_driver
6871da177e4SLinus Torvalds  * --------------------
6881da177e4SLinus Torvalds  * Parses the list of registered drivers looking for a driver applicable for
6891da177e4SLinus Torvalds  * the specified device.
6901da177e4SLinus Torvalds  */
6911da177e4SLinus Torvalds static int
6921da177e4SLinus Torvalds acpi_bus_find_driver (
6931da177e4SLinus Torvalds 	struct acpi_device	*device)
6941da177e4SLinus Torvalds {
6951da177e4SLinus Torvalds 	int			result = 0;
6961da177e4SLinus Torvalds 	struct list_head	* node, *next;
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_bus_find_driver");
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	spin_lock(&acpi_device_lock);
7011da177e4SLinus Torvalds 	list_for_each_safe(node,next,&acpi_bus_drivers) {
7021da177e4SLinus Torvalds 		struct acpi_driver * driver = container_of(node,struct acpi_driver,node);
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 		atomic_inc(&driver->references);
7051da177e4SLinus Torvalds 		spin_unlock(&acpi_device_lock);
7061da177e4SLinus Torvalds 		if (!acpi_bus_match(device, driver)) {
7071da177e4SLinus Torvalds 			result = acpi_bus_driver_init(device, driver);
7081da177e4SLinus Torvalds 			if (!result)
7091da177e4SLinus Torvalds 				goto Done;
7101da177e4SLinus Torvalds 		}
7111da177e4SLinus Torvalds 		atomic_dec(&driver->references);
7121da177e4SLinus Torvalds 		spin_lock(&acpi_device_lock);
7131da177e4SLinus Torvalds 	}
7141da177e4SLinus Torvalds 	spin_unlock(&acpi_device_lock);
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds  Done:
7171da177e4SLinus Torvalds 	return_VALUE(result);
7181da177e4SLinus Torvalds }
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds /* --------------------------------------------------------------------------
7221da177e4SLinus Torvalds                                  Device Enumeration
7231da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds static int
7261da177e4SLinus Torvalds acpi_bus_get_flags (
7271da177e4SLinus Torvalds 	struct acpi_device	*device)
7281da177e4SLinus Torvalds {
7291da177e4SLinus Torvalds 	acpi_status		status = AE_OK;
7301da177e4SLinus Torvalds 	acpi_handle		temp = NULL;
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_bus_get_flags");
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 	/* Presence of _STA indicates 'dynamic_status' */
7351da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_STA", &temp);
7361da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
7371da177e4SLinus Torvalds 		device->flags.dynamic_status = 1;
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 	/* Presence of _CID indicates 'compatible_ids' */
7401da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_CID", &temp);
7411da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
7421da177e4SLinus Torvalds 		device->flags.compatible_ids = 1;
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	/* Presence of _RMV indicates 'removable' */
7451da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_RMV", &temp);
7461da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
7471da177e4SLinus Torvalds 		device->flags.removable = 1;
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
7501da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_EJD", &temp);
7511da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
7521da177e4SLinus Torvalds 		device->flags.ejectable = 1;
7531da177e4SLinus Torvalds 	else {
7541da177e4SLinus Torvalds 		status = acpi_get_handle(device->handle, "_EJ0", &temp);
7551da177e4SLinus Torvalds 		if (ACPI_SUCCESS(status))
7561da177e4SLinus Torvalds 			device->flags.ejectable = 1;
7571da177e4SLinus Torvalds 	}
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	/* Presence of _LCK indicates 'lockable' */
7601da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_LCK", &temp);
7611da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
7621da177e4SLinus Torvalds 		device->flags.lockable = 1;
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 	/* Presence of _PS0|_PR0 indicates 'power manageable' */
7651da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_PS0", &temp);
7661da177e4SLinus Torvalds 	if (ACPI_FAILURE(status))
7671da177e4SLinus Torvalds 		status = acpi_get_handle(device->handle, "_PR0", &temp);
7681da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
7691da177e4SLinus Torvalds 		device->flags.power_manageable = 1;
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 	/* Presence of _PRW indicates wake capable */
7721da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_PRW", &temp);
7731da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
7741da177e4SLinus Torvalds 		device->flags.wake_capable = 1;
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	/* TBD: Peformance management */
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	return_VALUE(0);
7791da177e4SLinus Torvalds }
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds static void acpi_device_get_busid(struct acpi_device * device, acpi_handle handle, int type)
7821da177e4SLinus Torvalds {
7831da177e4SLinus Torvalds 	char			bus_id[5] = {'?',0};
7841da177e4SLinus Torvalds 	struct acpi_buffer	buffer = {sizeof(bus_id), bus_id};
7851da177e4SLinus Torvalds 	int			i = 0;
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	/*
7881da177e4SLinus Torvalds 	 * Bus ID
7891da177e4SLinus Torvalds 	 * ------
7901da177e4SLinus Torvalds 	 * The device's Bus ID is simply the object name.
7911da177e4SLinus Torvalds 	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
7921da177e4SLinus Torvalds 	 */
7931da177e4SLinus Torvalds 	switch (type) {
7941da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SYSTEM:
7951da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "ACPI");
7961da177e4SLinus Torvalds 		break;
7971da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
7981da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "PWRF");
7991da177e4SLinus Torvalds 		break;
8001da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
8011da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "SLPF");
8021da177e4SLinus Torvalds 		break;
8031da177e4SLinus Torvalds 	default:
8041da177e4SLinus Torvalds 		acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
8051da177e4SLinus Torvalds 		/* Clean up trailing underscores (if any) */
8061da177e4SLinus Torvalds 		for (i = 3; i > 1; i--) {
8071da177e4SLinus Torvalds 			if (bus_id[i] == '_')
8081da177e4SLinus Torvalds 				bus_id[i] = '\0';
8091da177e4SLinus Torvalds 			else
8101da177e4SLinus Torvalds 				break;
8111da177e4SLinus Torvalds 		}
8121da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, bus_id);
8131da177e4SLinus Torvalds 		break;
8141da177e4SLinus Torvalds 	}
8151da177e4SLinus Torvalds }
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds static void acpi_device_set_id(struct acpi_device * device, struct acpi_device * parent,
8181da177e4SLinus Torvalds 			       acpi_handle handle, int type)
8191da177e4SLinus Torvalds {
8201da177e4SLinus Torvalds 	struct acpi_device_info	*info;
8211da177e4SLinus Torvalds 	struct acpi_buffer	buffer = {ACPI_ALLOCATE_BUFFER, NULL};
8221da177e4SLinus Torvalds 	char			*hid = NULL;
8231da177e4SLinus Torvalds 	char			*uid = NULL;
8241da177e4SLinus Torvalds 	struct acpi_compatible_id_list *cid_list = NULL;
8251da177e4SLinus Torvalds 	acpi_status		status;
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 	switch (type) {
8281da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
8291da177e4SLinus Torvalds 		status = acpi_get_object_info(handle, &buffer);
8301da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
8311da177e4SLinus Torvalds 			printk("%s: Error reading device info\n",__FUNCTION__);
8321da177e4SLinus Torvalds 			return;
8331da177e4SLinus Torvalds 		}
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 		info = buffer.pointer;
8361da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_HID)
8371da177e4SLinus Torvalds 			hid = info->hardware_id.value;
8381da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_UID)
8391da177e4SLinus Torvalds 			uid = info->unique_id.value;
8401da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_CID)
8411da177e4SLinus Torvalds 			cid_list = &info->compatibility_id;
8421da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_ADR) {
8431da177e4SLinus Torvalds 			device->pnp.bus_address = info->address;
8441da177e4SLinus Torvalds 			device->flags.bus_address = 1;
8451da177e4SLinus Torvalds 		}
8461da177e4SLinus Torvalds 		break;
8471da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER:
8481da177e4SLinus Torvalds 		hid = ACPI_POWER_HID;
8491da177e4SLinus Torvalds 		break;
8501da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_PROCESSOR:
8511da177e4SLinus Torvalds 		hid = ACPI_PROCESSOR_HID;
8521da177e4SLinus Torvalds 		break;
8531da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SYSTEM:
8541da177e4SLinus Torvalds 		hid = ACPI_SYSTEM_HID;
8551da177e4SLinus Torvalds 		break;
8561da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_THERMAL:
8571da177e4SLinus Torvalds 		hid = ACPI_THERMAL_HID;
8581da177e4SLinus Torvalds 		break;
8591da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
8601da177e4SLinus Torvalds 		hid = ACPI_BUTTON_HID_POWERF;
8611da177e4SLinus Torvalds 		break;
8621da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
8631da177e4SLinus Torvalds 		hid = ACPI_BUTTON_HID_SLEEPF;
8641da177e4SLinus Torvalds 		break;
8651da177e4SLinus Torvalds 	}
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	/*
8681da177e4SLinus Torvalds 	 * \_SB
8691da177e4SLinus Torvalds 	 * ----
8701da177e4SLinus Torvalds 	 * Fix for the system root bus device -- the only root-level device.
8711da177e4SLinus Torvalds 	 */
8721da177e4SLinus Torvalds 	if ((parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
8731da177e4SLinus Torvalds 		hid = ACPI_BUS_HID;
8741da177e4SLinus Torvalds 		strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
8751da177e4SLinus Torvalds 		strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
8761da177e4SLinus Torvalds 	}
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 	if (hid) {
8791da177e4SLinus Torvalds 		strcpy(device->pnp.hardware_id, hid);
8801da177e4SLinus Torvalds 		device->flags.hardware_id = 1;
8811da177e4SLinus Torvalds 	}
8821da177e4SLinus Torvalds 	if (uid) {
8831da177e4SLinus Torvalds 		strcpy(device->pnp.unique_id, uid);
8841da177e4SLinus Torvalds 		device->flags.unique_id = 1;
8851da177e4SLinus Torvalds 	}
8861da177e4SLinus Torvalds 	if (cid_list) {
8871da177e4SLinus Torvalds 		device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
8881da177e4SLinus Torvalds 		if (device->pnp.cid_list)
8891da177e4SLinus Torvalds 			memcpy(device->pnp.cid_list, cid_list, cid_list->size);
8901da177e4SLinus Torvalds 		else
8911da177e4SLinus Torvalds 			printk(KERN_ERR "Memory allocation error\n");
8921da177e4SLinus Torvalds 	}
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	acpi_os_free(buffer.pointer);
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds static int acpi_device_set_context(struct acpi_device * device, int type)
8981da177e4SLinus Torvalds {
8991da177e4SLinus Torvalds 	acpi_status status = AE_OK;
9001da177e4SLinus Torvalds 	int result = 0;
9011da177e4SLinus Torvalds 	/*
9021da177e4SLinus Torvalds 	 * Context
9031da177e4SLinus Torvalds 	 * -------
9041da177e4SLinus Torvalds 	 * Attach this 'struct acpi_device' to the ACPI object.  This makes
9051da177e4SLinus Torvalds 	 * resolutions from handle->device very efficient.  Note that we need
9061da177e4SLinus Torvalds 	 * to be careful with fixed-feature devices as they all attach to the
9071da177e4SLinus Torvalds 	 * root object.
9081da177e4SLinus Torvalds 	 */
9091da177e4SLinus Torvalds 	if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
9101da177e4SLinus Torvalds 	    type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
9111da177e4SLinus Torvalds 		status = acpi_attach_data(device->handle,
9121da177e4SLinus Torvalds 			acpi_bus_data_handler, device);
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
9151da177e4SLinus Torvalds 			printk("Error attaching device data\n");
9161da177e4SLinus Torvalds 			result = -ENODEV;
9171da177e4SLinus Torvalds 		}
9181da177e4SLinus Torvalds 	}
9191da177e4SLinus Torvalds 	return result;
9201da177e4SLinus Torvalds }
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds static void acpi_device_get_debug_info(struct acpi_device * device, acpi_handle handle, int type)
9231da177e4SLinus Torvalds {
9241da177e4SLinus Torvalds #ifdef CONFIG_ACPI_DEBUG_OUTPUT
9251da177e4SLinus Torvalds 	char		*type_string = NULL;
9261da177e4SLinus Torvalds 	char		name[80] = {'?','\0'};
9271da177e4SLinus Torvalds 	struct acpi_buffer	buffer = {sizeof(name), name};
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 	switch (type) {
9301da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
9311da177e4SLinus Torvalds 		type_string = "Device";
9321da177e4SLinus Torvalds 		acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
9331da177e4SLinus Torvalds 		break;
9341da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER:
9351da177e4SLinus Torvalds 		type_string = "Power Resource";
9361da177e4SLinus Torvalds 		acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
9371da177e4SLinus Torvalds 		break;
9381da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_PROCESSOR:
9391da177e4SLinus Torvalds 		type_string = "Processor";
9401da177e4SLinus Torvalds 		acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
9411da177e4SLinus Torvalds 		break;
9421da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SYSTEM:
9431da177e4SLinus Torvalds 		type_string = "System";
9441da177e4SLinus Torvalds 		acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
9451da177e4SLinus Torvalds 		break;
9461da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_THERMAL:
9471da177e4SLinus Torvalds 		type_string = "Thermal Zone";
9481da177e4SLinus Torvalds 		acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
9491da177e4SLinus Torvalds 		break;
9501da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
9511da177e4SLinus Torvalds 		type_string = "Power Button";
9521da177e4SLinus Torvalds 		sprintf(name, "PWRB");
9531da177e4SLinus Torvalds 		break;
9541da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
9551da177e4SLinus Torvalds 		type_string = "Sleep Button";
9561da177e4SLinus Torvalds 		sprintf(name, "SLPB");
9571da177e4SLinus Torvalds 		break;
9581da177e4SLinus Torvalds 	}
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
9611da177e4SLinus Torvalds #endif /*CONFIG_ACPI_DEBUG_OUTPUT*/
9621da177e4SLinus Torvalds }
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds static int
9661da177e4SLinus Torvalds acpi_bus_remove (
9671da177e4SLinus Torvalds 	struct acpi_device *dev,
9681da177e4SLinus Torvalds 	int rmdevice)
9691da177e4SLinus Torvalds {
9701da177e4SLinus Torvalds 	int 			result = 0;
9711da177e4SLinus Torvalds 	struct acpi_driver	*driver;
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_bus_remove");
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds 	if (!dev)
9761da177e4SLinus Torvalds 		return_VALUE(-EINVAL);
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 	driver = dev->driver;
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	if ((driver) && (driver->ops.remove)) {
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 		if (driver->ops.stop) {
9831da177e4SLinus Torvalds 			result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT);
9841da177e4SLinus Torvalds 			if (result)
9851da177e4SLinus Torvalds 				return_VALUE(result);
9861da177e4SLinus Torvalds 		}
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 		result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT);
9891da177e4SLinus Torvalds 		if (result) {
9901da177e4SLinus Torvalds 			return_VALUE(result);
9911da177e4SLinus Torvalds 		}
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 		atomic_dec(&dev->driver->references);
9941da177e4SLinus Torvalds 		dev->driver = NULL;
9951da177e4SLinus Torvalds 		acpi_driver_data(dev) = NULL;
9961da177e4SLinus Torvalds 	}
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 	if (!rmdevice)
9991da177e4SLinus Torvalds 		return_VALUE(0);
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 	if (dev->flags.bus_address) {
10021da177e4SLinus Torvalds 		if ((dev->parent) && (dev->parent->ops.unbind))
10031da177e4SLinus Torvalds 			dev->parent->ops.unbind(dev);
10041da177e4SLinus Torvalds 	}
10051da177e4SLinus Torvalds 
10061da177e4SLinus Torvalds 	acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 	return_VALUE(0);
10091da177e4SLinus Torvalds }
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds int
10131da177e4SLinus Torvalds acpi_bus_add (
10141da177e4SLinus Torvalds 	struct acpi_device	**child,
10151da177e4SLinus Torvalds 	struct acpi_device	*parent,
10161da177e4SLinus Torvalds 	acpi_handle		handle,
10171da177e4SLinus Torvalds 	int			type)
10181da177e4SLinus Torvalds {
10191da177e4SLinus Torvalds 	int			result = 0;
10201da177e4SLinus Torvalds 	struct acpi_device	*device = NULL;
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_bus_add");
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 	if (!child)
10251da177e4SLinus Torvalds 		return_VALUE(-EINVAL);
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 	device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
10281da177e4SLinus Torvalds 	if (!device) {
10291da177e4SLinus Torvalds 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Memory allocation error\n"));
10301da177e4SLinus Torvalds 		return_VALUE(-ENOMEM);
10311da177e4SLinus Torvalds 	}
10321da177e4SLinus Torvalds 	memset(device, 0, sizeof(struct acpi_device));
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds 	device->handle = handle;
10351da177e4SLinus Torvalds 	device->parent = parent;
10361da177e4SLinus Torvalds 
10371da177e4SLinus Torvalds 	acpi_device_get_busid(device,handle,type);
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 	/*
10401da177e4SLinus Torvalds 	 * Flags
10411da177e4SLinus Torvalds 	 * -----
10421da177e4SLinus Torvalds 	 * Get prior to calling acpi_bus_get_status() so we know whether
10431da177e4SLinus Torvalds 	 * or not _STA is present.  Note that we only look for object
10441da177e4SLinus Torvalds 	 * handles -- cannot evaluate objects until we know the device is
10451da177e4SLinus Torvalds 	 * present and properly initialized.
10461da177e4SLinus Torvalds 	 */
10471da177e4SLinus Torvalds 	result = acpi_bus_get_flags(device);
10481da177e4SLinus Torvalds 	if (result)
10491da177e4SLinus Torvalds 		goto end;
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 	/*
10521da177e4SLinus Torvalds 	 * Status
10531da177e4SLinus Torvalds 	 * ------
10541da177e4SLinus Torvalds 	 * See if the device is present.  We always assume that non-Device()
10551da177e4SLinus Torvalds 	 * objects (e.g. thermal zones, power resources, processors, etc.) are
10561da177e4SLinus Torvalds 	 * present, functioning, etc. (at least when parent object is present).
10571da177e4SLinus Torvalds 	 * Note that _STA has a different meaning for some objects (e.g.
10581da177e4SLinus Torvalds 	 * power resources) so we need to be careful how we use it.
10591da177e4SLinus Torvalds 	 */
10601da177e4SLinus Torvalds 	switch (type) {
10611da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
10621da177e4SLinus Torvalds 		result = acpi_bus_get_status(device);
10631da177e4SLinus Torvalds 		if (ACPI_FAILURE(result) || !device->status.present) {
10641da177e4SLinus Torvalds 			result = -ENOENT;
10651da177e4SLinus Torvalds 			goto end;
10661da177e4SLinus Torvalds 		}
10671da177e4SLinus Torvalds 		break;
10681da177e4SLinus Torvalds 	default:
10691da177e4SLinus Torvalds 		STRUCT_TO_INT(device->status) = 0x0F;
10701da177e4SLinus Torvalds 		break;
10711da177e4SLinus Torvalds 	}
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds 	/*
10741da177e4SLinus Torvalds 	 * Initialize Device
10751da177e4SLinus Torvalds 	 * -----------------
10761da177e4SLinus Torvalds 	 * TBD: Synch with Core's enumeration/initialization process.
10771da177e4SLinus Torvalds 	 */
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds 	/*
10801da177e4SLinus Torvalds 	 * Hardware ID, Unique ID, & Bus Address
10811da177e4SLinus Torvalds 	 * -------------------------------------
10821da177e4SLinus Torvalds 	 */
10831da177e4SLinus Torvalds 	acpi_device_set_id(device,parent,handle,type);
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds 	/*
10861da177e4SLinus Torvalds 	 * Power Management
10871da177e4SLinus Torvalds 	 * ----------------
10881da177e4SLinus Torvalds 	 */
10891da177e4SLinus Torvalds 	if (device->flags.power_manageable) {
10901da177e4SLinus Torvalds 		result = acpi_bus_get_power_flags(device);
10911da177e4SLinus Torvalds 		if (result)
10921da177e4SLinus Torvalds 			goto end;
10931da177e4SLinus Torvalds 	}
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds  	/*
10961da177e4SLinus Torvalds 	 * Wakeup device management
10971da177e4SLinus Torvalds 	 *-----------------------
10981da177e4SLinus Torvalds 	 */
10991da177e4SLinus Torvalds 	if (device->flags.wake_capable) {
11001da177e4SLinus Torvalds 		result = acpi_bus_get_wakeup_device_flags(device);
11011da177e4SLinus Torvalds 		if (result)
11021da177e4SLinus Torvalds 			goto end;
11031da177e4SLinus Torvalds 	}
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds 	/*
11061da177e4SLinus Torvalds 	 * Performance Management
11071da177e4SLinus Torvalds 	 * ----------------------
11081da177e4SLinus Torvalds 	 */
11091da177e4SLinus Torvalds 	if (device->flags.performance_manageable) {
11101da177e4SLinus Torvalds 		result = acpi_bus_get_perf_flags(device);
11111da177e4SLinus Torvalds 		if (result)
11121da177e4SLinus Torvalds 			goto end;
11131da177e4SLinus Torvalds 	}
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 	if ((result = acpi_device_set_context(device,type)))
11161da177e4SLinus Torvalds 		goto end;
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds 	acpi_device_get_debug_info(device,handle,type);
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds 	acpi_device_register(device,parent);
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 	/*
11231da177e4SLinus Torvalds 	 * Bind _ADR-Based Devices
11241da177e4SLinus Torvalds 	 * -----------------------
11251da177e4SLinus Torvalds 	 * If there's a a bus address (_ADR) then we utilize the parent's
11261da177e4SLinus Torvalds 	 * 'bind' function (if exists) to bind the ACPI- and natively-
11271da177e4SLinus Torvalds 	 * enumerated device representations.
11281da177e4SLinus Torvalds 	 */
11291da177e4SLinus Torvalds 	if (device->flags.bus_address) {
11301da177e4SLinus Torvalds 		if (device->parent && device->parent->ops.bind)
11311da177e4SLinus Torvalds 			device->parent->ops.bind(device);
11321da177e4SLinus Torvalds 	}
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds 	/*
11351da177e4SLinus Torvalds 	 * Locate & Attach Driver
11361da177e4SLinus Torvalds 	 * ----------------------
11371da177e4SLinus Torvalds 	 * If there's a hardware id (_HID) or compatible ids (_CID) we check
11381da177e4SLinus Torvalds 	 * to see if there's a driver installed for this kind of device.  Note
11391da177e4SLinus Torvalds 	 * that drivers can install before or after a device is enumerated.
11401da177e4SLinus Torvalds 	 *
11411da177e4SLinus Torvalds 	 * TBD: Assumes LDM provides driver hot-plug capability.
11421da177e4SLinus Torvalds 	 */
11431da177e4SLinus Torvalds 	acpi_bus_find_driver(device);
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds end:
11461da177e4SLinus Torvalds 	if (!result)
11471da177e4SLinus Torvalds 		*child = device;
11481da177e4SLinus Torvalds 	else {
11491da177e4SLinus Torvalds 		if (device->pnp.cid_list)
11501da177e4SLinus Torvalds 			kfree(device->pnp.cid_list);
11511da177e4SLinus Torvalds 		kfree(device);
11521da177e4SLinus Torvalds 	}
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 	return_VALUE(result);
11551da177e4SLinus Torvalds }
11561da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_add);
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds int acpi_bus_scan (struct acpi_device	*start)
11601da177e4SLinus Torvalds {
11611da177e4SLinus Torvalds 	acpi_status		status = AE_OK;
11621da177e4SLinus Torvalds 	struct acpi_device	*parent = NULL;
11631da177e4SLinus Torvalds 	struct acpi_device	*child = NULL;
11641da177e4SLinus Torvalds 	acpi_handle		phandle = NULL;
11651da177e4SLinus Torvalds 	acpi_handle		chandle = NULL;
11661da177e4SLinus Torvalds 	acpi_object_type	type = 0;
11671da177e4SLinus Torvalds 	u32			level = 1;
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_bus_scan");
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 	if (!start)
11721da177e4SLinus Torvalds 		return_VALUE(-EINVAL);
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	parent = start;
11751da177e4SLinus Torvalds 	phandle = start->handle;
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 	/*
11781da177e4SLinus Torvalds 	 * Parse through the ACPI namespace, identify all 'devices', and
11791da177e4SLinus Torvalds 	 * create a new 'struct acpi_device' for each.
11801da177e4SLinus Torvalds 	 */
11811da177e4SLinus Torvalds 	while ((level > 0) && parent) {
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 		status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
11841da177e4SLinus Torvalds 			chandle, &chandle);
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds 		/*
11871da177e4SLinus Torvalds 		 * If this scope is exhausted then move our way back up.
11881da177e4SLinus Torvalds 		 */
11891da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
11901da177e4SLinus Torvalds 			level--;
11911da177e4SLinus Torvalds 			chandle = phandle;
11921da177e4SLinus Torvalds 			acpi_get_parent(phandle, &phandle);
11931da177e4SLinus Torvalds 			if (parent->parent)
11941da177e4SLinus Torvalds 				parent = parent->parent;
11951da177e4SLinus Torvalds 			continue;
11961da177e4SLinus Torvalds 		}
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds 		status = acpi_get_type(chandle, &type);
11991da177e4SLinus Torvalds 		if (ACPI_FAILURE(status))
12001da177e4SLinus Torvalds 			continue;
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 		/*
12031da177e4SLinus Torvalds 		 * If this is a scope object then parse it (depth-first).
12041da177e4SLinus Torvalds 		 */
12051da177e4SLinus Torvalds 		if (type == ACPI_TYPE_LOCAL_SCOPE) {
12061da177e4SLinus Torvalds 			level++;
12071da177e4SLinus Torvalds 			phandle = chandle;
12081da177e4SLinus Torvalds 			chandle = NULL;
12091da177e4SLinus Torvalds 			continue;
12101da177e4SLinus Torvalds 		}
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds 		/*
12131da177e4SLinus Torvalds 		 * We're only interested in objects that we consider 'devices'.
12141da177e4SLinus Torvalds 		 */
12151da177e4SLinus Torvalds 		switch (type) {
12161da177e4SLinus Torvalds 		case ACPI_TYPE_DEVICE:
12171da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_DEVICE;
12181da177e4SLinus Torvalds 			break;
12191da177e4SLinus Torvalds 		case ACPI_TYPE_PROCESSOR:
12201da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_PROCESSOR;
12211da177e4SLinus Torvalds 			break;
12221da177e4SLinus Torvalds 		case ACPI_TYPE_THERMAL:
12231da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_THERMAL;
12241da177e4SLinus Torvalds 			break;
12251da177e4SLinus Torvalds 		case ACPI_TYPE_POWER:
12261da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_POWER;
12271da177e4SLinus Torvalds 			break;
12281da177e4SLinus Torvalds 		default:
12291da177e4SLinus Torvalds 			continue;
12301da177e4SLinus Torvalds 		}
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 		status = acpi_bus_add(&child, parent, chandle, type);
12331da177e4SLinus Torvalds 		if (ACPI_FAILURE(status))
12341da177e4SLinus Torvalds 			continue;
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 		/*
12371da177e4SLinus Torvalds 		 * If the device is present, enabled, and functioning then
12381da177e4SLinus Torvalds 		 * parse its scope (depth-first).  Note that we need to
12391da177e4SLinus Torvalds 		 * represent absent devices to facilitate PnP notifications
12401da177e4SLinus Torvalds 		 * -- but only the subtree head (not all of its children,
12411da177e4SLinus Torvalds 		 * which will be enumerated when the parent is inserted).
12421da177e4SLinus Torvalds 		 *
12431da177e4SLinus Torvalds 		 * TBD: Need notifications and other detection mechanisms
12441da177e4SLinus Torvalds 		 *	in place before we can fully implement this.
12451da177e4SLinus Torvalds 		 */
12461da177e4SLinus Torvalds 		if (child->status.present) {
12471da177e4SLinus Torvalds 			status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
12481da177e4SLinus Torvalds 						      NULL, NULL);
12491da177e4SLinus Torvalds 			if (ACPI_SUCCESS(status)) {
12501da177e4SLinus Torvalds 				level++;
12511da177e4SLinus Torvalds 				phandle = chandle;
12521da177e4SLinus Torvalds 				chandle = NULL;
12531da177e4SLinus Torvalds 				parent = child;
12541da177e4SLinus Torvalds 			}
12551da177e4SLinus Torvalds 		}
12561da177e4SLinus Torvalds 	}
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 	return_VALUE(0);
12591da177e4SLinus Torvalds }
12601da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_scan);
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds static int
12641da177e4SLinus Torvalds acpi_bus_trim(struct acpi_device	*start,
12651da177e4SLinus Torvalds 		int rmdevice)
12661da177e4SLinus Torvalds {
12671da177e4SLinus Torvalds 	acpi_status		status;
12681da177e4SLinus Torvalds 	struct acpi_device	*parent, *child;
12691da177e4SLinus Torvalds 	acpi_handle		phandle, chandle;
12701da177e4SLinus Torvalds 	acpi_object_type	type;
12711da177e4SLinus Torvalds 	u32			level = 1;
12721da177e4SLinus Torvalds 	int			err = 0;
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 	parent  = start;
12751da177e4SLinus Torvalds 	phandle = start->handle;
12761da177e4SLinus Torvalds 	child = chandle = NULL;
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds 	while ((level > 0) && parent && (!err)) {
12791da177e4SLinus Torvalds 		status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
12801da177e4SLinus Torvalds 			chandle, &chandle);
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds 		/*
12831da177e4SLinus Torvalds 		 * If this scope is exhausted then move our way back up.
12841da177e4SLinus Torvalds 		 */
12851da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
12861da177e4SLinus Torvalds 			level--;
12871da177e4SLinus Torvalds 			chandle = phandle;
12881da177e4SLinus Torvalds 			acpi_get_parent(phandle, &phandle);
12891da177e4SLinus Torvalds 			child = parent;
12901da177e4SLinus Torvalds 			parent = parent->parent;
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 			if (level == 0)
12931da177e4SLinus Torvalds 				err = acpi_bus_remove(child, rmdevice);
12941da177e4SLinus Torvalds 			else
12951da177e4SLinus Torvalds 				err = acpi_bus_remove(child, 1);
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds 			continue;
12981da177e4SLinus Torvalds 		}
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds 		status = acpi_get_type(chandle, &type);
13011da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
13021da177e4SLinus Torvalds 			continue;
13031da177e4SLinus Torvalds 		}
13041da177e4SLinus Torvalds 		/*
13051da177e4SLinus Torvalds 		 * If there is a device corresponding to chandle then
13061da177e4SLinus Torvalds 		 * parse it (depth-first).
13071da177e4SLinus Torvalds 		 */
13081da177e4SLinus Torvalds 		if (acpi_bus_get_device(chandle, &child) == 0) {
13091da177e4SLinus Torvalds 			level++;
13101da177e4SLinus Torvalds 			phandle = chandle;
13111da177e4SLinus Torvalds 			chandle = NULL;
13121da177e4SLinus Torvalds 			parent = child;
13131da177e4SLinus Torvalds 		}
13141da177e4SLinus Torvalds 		continue;
13151da177e4SLinus Torvalds 	}
13161da177e4SLinus Torvalds 	return err;
13171da177e4SLinus Torvalds }
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds static int
13201da177e4SLinus Torvalds acpi_bus_scan_fixed (
13211da177e4SLinus Torvalds 	struct acpi_device	*root)
13221da177e4SLinus Torvalds {
13231da177e4SLinus Torvalds 	int			result = 0;
13241da177e4SLinus Torvalds 	struct acpi_device	*device = NULL;
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_bus_scan_fixed");
13271da177e4SLinus Torvalds 
13281da177e4SLinus Torvalds 	if (!root)
13291da177e4SLinus Torvalds 		return_VALUE(-ENODEV);
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds 	/*
13321da177e4SLinus Torvalds 	 * Enumerate all fixed-feature devices.
13331da177e4SLinus Torvalds 	 */
13341da177e4SLinus Torvalds 	if (acpi_fadt.pwr_button == 0)
13351da177e4SLinus Torvalds 		result = acpi_bus_add(&device, acpi_root,
13361da177e4SLinus Torvalds 			NULL, ACPI_BUS_TYPE_POWER_BUTTON);
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 	if (acpi_fadt.sleep_button == 0)
13391da177e4SLinus Torvalds 		result = acpi_bus_add(&device, acpi_root,
13401da177e4SLinus Torvalds 			NULL, ACPI_BUS_TYPE_SLEEP_BUTTON);
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds 	return_VALUE(result);
13431da177e4SLinus Torvalds }
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds static int __init acpi_scan_init(void)
13471da177e4SLinus Torvalds {
13481da177e4SLinus Torvalds 	int result;
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_scan_init");
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds 	if (acpi_disabled)
13531da177e4SLinus Torvalds 		return_VALUE(0);
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 	kset_register(&acpi_namespace_kset);
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds 	/*
13581da177e4SLinus Torvalds 	 * Create the root device in the bus's device tree
13591da177e4SLinus Torvalds 	 */
13601da177e4SLinus Torvalds 	result = acpi_bus_add(&acpi_root, NULL, ACPI_ROOT_OBJECT,
13611da177e4SLinus Torvalds 		ACPI_BUS_TYPE_SYSTEM);
13621da177e4SLinus Torvalds 	if (result)
13631da177e4SLinus Torvalds 		goto Done;
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds 	/*
13661da177e4SLinus Torvalds 	 * Enumerate devices in the ACPI namespace.
13671da177e4SLinus Torvalds 	 */
13681da177e4SLinus Torvalds 	result = acpi_bus_scan_fixed(acpi_root);
13691da177e4SLinus Torvalds 	if (!result)
13701da177e4SLinus Torvalds 		result = acpi_bus_scan(acpi_root);
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds 	if (result)
13731da177e4SLinus Torvalds 		acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
13741da177e4SLinus Torvalds 
13751da177e4SLinus Torvalds  Done:
13761da177e4SLinus Torvalds 	return_VALUE(result);
13771da177e4SLinus Torvalds }
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds subsys_initcall(acpi_scan_init);
1380