xref: /openbmc/linux/drivers/acpi/scan.c (revision 9d9f749b316ac21cb59ad3e595cbce469b409e1a)
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 #define _COMPONENT		ACPI_BUS_COMPONENT
131da177e4SLinus Torvalds ACPI_MODULE_NAME("scan")
141da177e4SLinus Torvalds #define STRUCT_TO_INT(s)	(*((int*)&s))
151da177e4SLinus Torvalds extern struct acpi_device *acpi_root;
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #define ACPI_BUS_CLASS			"system_bus"
181da177e4SLinus Torvalds #define ACPI_BUS_HID			"ACPI_BUS"
191da177e4SLinus Torvalds #define ACPI_BUS_DRIVER_NAME		"ACPI Bus Driver"
201da177e4SLinus Torvalds #define ACPI_BUS_DEVICE_NAME		"System Bus"
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds static LIST_HEAD(acpi_device_list);
231da177e4SLinus Torvalds DEFINE_SPINLOCK(acpi_device_lock);
241da177e4SLinus Torvalds LIST_HEAD(acpi_wakeup_device_list);
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds static void acpi_device_release(struct kobject *kobj)
281da177e4SLinus Torvalds {
291da177e4SLinus Torvalds 	struct acpi_device *dev = container_of(kobj, struct acpi_device, kobj);
301da177e4SLinus Torvalds 	kfree(dev->pnp.cid_list);
311da177e4SLinus Torvalds 	kfree(dev);
321da177e4SLinus Torvalds }
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds struct acpi_device_attribute {
351da177e4SLinus Torvalds 	struct attribute attr;
361da177e4SLinus Torvalds 	 ssize_t(*show) (struct acpi_device *, char *);
371da177e4SLinus Torvalds 	 ssize_t(*store) (struct acpi_device *, const char *, size_t);
381da177e4SLinus Torvalds };
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds typedef void acpi_device_sysfs_files(struct kobject *,
411da177e4SLinus Torvalds 				     const struct attribute *);
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds static void setup_sys_fs_device_files(struct acpi_device *dev,
441da177e4SLinus Torvalds 				      acpi_device_sysfs_files * func);
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #define create_sysfs_device_files(dev)	\
471da177e4SLinus Torvalds 	setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_create_file)
481da177e4SLinus Torvalds #define remove_sysfs_device_files(dev)	\
491da177e4SLinus Torvalds 	setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_remove_file)
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #define to_acpi_device(n) container_of(n, struct acpi_device, kobj)
521da177e4SLinus Torvalds #define to_handle_attr(n) container_of(n, struct acpi_device_attribute, attr);
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds static ssize_t acpi_device_attr_show(struct kobject *kobj,
551da177e4SLinus Torvalds 				     struct attribute *attr, char *buf)
561da177e4SLinus Torvalds {
571da177e4SLinus Torvalds 	struct acpi_device *device = to_acpi_device(kobj);
581da177e4SLinus Torvalds 	struct acpi_device_attribute *attribute = to_handle_attr(attr);
5970f2817aSDmitry Torokhov 	return attribute->show ? attribute->show(device, buf) : -EIO;
601da177e4SLinus Torvalds }
611da177e4SLinus Torvalds static ssize_t acpi_device_attr_store(struct kobject *kobj,
624be44fcdSLen Brown 				      struct attribute *attr, const char *buf,
634be44fcdSLen Brown 				      size_t len)
641da177e4SLinus Torvalds {
651da177e4SLinus Torvalds 	struct acpi_device *device = to_acpi_device(kobj);
661da177e4SLinus Torvalds 	struct acpi_device_attribute *attribute = to_handle_attr(attr);
6770f2817aSDmitry Torokhov 	return attribute->store ? attribute->store(device, buf, len) : -EIO;
681da177e4SLinus Torvalds }
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds static struct sysfs_ops acpi_device_sysfs_ops = {
711da177e4SLinus Torvalds 	.show = acpi_device_attr_show,
721da177e4SLinus Torvalds 	.store = acpi_device_attr_store,
731da177e4SLinus Torvalds };
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds static struct kobj_type ktype_acpi_ns = {
761da177e4SLinus Torvalds 	.sysfs_ops = &acpi_device_sysfs_ops,
771da177e4SLinus Torvalds 	.release = acpi_device_release,
781da177e4SLinus Torvalds };
791da177e4SLinus Torvalds 
80312c004dSKay Sievers static int namespace_uevent(struct kset *kset, struct kobject *kobj,
811da177e4SLinus Torvalds 			     char **envp, int num_envp, char *buffer,
821da177e4SLinus Torvalds 			     int buffer_size)
831da177e4SLinus Torvalds {
841da177e4SLinus Torvalds 	struct acpi_device *dev = to_acpi_device(kobj);
851da177e4SLinus Torvalds 	int i = 0;
861da177e4SLinus Torvalds 	int len = 0;
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	if (!dev->driver)
891da177e4SLinus Torvalds 		return 0;
901da177e4SLinus Torvalds 
91312c004dSKay Sievers 	if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
921da177e4SLinus Torvalds 			   "PHYSDEVDRIVER=%s", dev->driver->name))
931da177e4SLinus Torvalds 		return -ENOMEM;
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	envp[i] = NULL;
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	return 0;
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
100312c004dSKay Sievers static struct kset_uevent_ops namespace_uevent_ops = {
101312c004dSKay Sievers 	.uevent = &namespace_uevent,
1021da177e4SLinus Torvalds };
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds static struct kset acpi_namespace_kset = {
1051da177e4SLinus Torvalds 	.kobj = {
1061da177e4SLinus Torvalds 		 .name = "namespace",
1071da177e4SLinus Torvalds 		 },
1081da177e4SLinus Torvalds 	.subsys = &acpi_subsys,
1091da177e4SLinus Torvalds 	.ktype = &ktype_acpi_ns,
110312c004dSKay Sievers 	.uevent_ops = &namespace_uevent_ops,
1111da177e4SLinus Torvalds };
1121da177e4SLinus Torvalds 
1134be44fcdSLen Brown static void acpi_device_register(struct acpi_device *device,
1144be44fcdSLen Brown 				 struct acpi_device *parent)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	/*
1171da177e4SLinus Torvalds 	 * Linkage
1181da177e4SLinus Torvalds 	 * -------
1191da177e4SLinus Torvalds 	 * Link this device to its parent and siblings.
1201da177e4SLinus Torvalds 	 */
1211da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->children);
1221da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->node);
1231da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->g_list);
1241da177e4SLinus Torvalds 	INIT_LIST_HEAD(&device->wakeup_list);
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	spin_lock(&acpi_device_lock);
1271da177e4SLinus Torvalds 	if (device->parent) {
1281da177e4SLinus Torvalds 		list_add_tail(&device->node, &device->parent->children);
1291da177e4SLinus Torvalds 		list_add_tail(&device->g_list, &device->parent->g_list);
1301da177e4SLinus Torvalds 	} else
1311da177e4SLinus Torvalds 		list_add_tail(&device->g_list, &acpi_device_list);
1321da177e4SLinus Torvalds 	if (device->wakeup.flags.valid)
1331da177e4SLinus Torvalds 		list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
1341da177e4SLinus Torvalds 	spin_unlock(&acpi_device_lock);
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	strlcpy(device->kobj.name, device->pnp.bus_id, KOBJ_NAME_LEN);
1371da177e4SLinus Torvalds 	if (parent)
1381da177e4SLinus Torvalds 		device->kobj.parent = &parent->kobj;
1391da177e4SLinus Torvalds 	device->kobj.ktype = &ktype_acpi_ns;
1401da177e4SLinus Torvalds 	device->kobj.kset = &acpi_namespace_kset;
1411da177e4SLinus Torvalds 	kobject_register(&device->kobj);
1421da177e4SLinus Torvalds 	create_sysfs_device_files(device);
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
1454be44fcdSLen Brown static int acpi_device_unregister(struct acpi_device *device, int type)
1461da177e4SLinus Torvalds {
1471da177e4SLinus Torvalds 	spin_lock(&acpi_device_lock);
1481da177e4SLinus Torvalds 	if (device->parent) {
1491da177e4SLinus Torvalds 		list_del(&device->node);
1501da177e4SLinus Torvalds 		list_del(&device->g_list);
1511da177e4SLinus Torvalds 	} else
1521da177e4SLinus Torvalds 		list_del(&device->g_list);
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	list_del(&device->wakeup_list);
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	spin_unlock(&acpi_device_lock);
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	acpi_detach_data(device->handle, acpi_bus_data_handler);
1591da177e4SLinus Torvalds 	remove_sysfs_device_files(device);
1601da177e4SLinus Torvalds 	kobject_unregister(&device->kobj);
1611da177e4SLinus Torvalds 	return 0;
1621da177e4SLinus Torvalds }
1631da177e4SLinus Torvalds 
1644be44fcdSLen Brown void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
1651da177e4SLinus Torvalds {
1661da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_bus_data_handler");
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	/* TBD */
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 	return_VOID;
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds 
1734be44fcdSLen Brown static int acpi_bus_get_power_flags(struct acpi_device *device)
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds 	acpi_status status = 0;
1761da177e4SLinus Torvalds 	acpi_handle handle = NULL;
1771da177e4SLinus Torvalds 	u32 i = 0;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_bus_get_power_flags");
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	/*
1821da177e4SLinus Torvalds 	 * Power Management Flags
1831da177e4SLinus Torvalds 	 */
1841da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_PSC", &handle);
1851da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
1861da177e4SLinus Torvalds 		device->power.flags.explicit_get = 1;
1871da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_IRC", &handle);
1881da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
1891da177e4SLinus Torvalds 		device->power.flags.inrush_current = 1;
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	/*
1921da177e4SLinus Torvalds 	 * Enumerate supported power management states
1931da177e4SLinus Torvalds 	 */
1941da177e4SLinus Torvalds 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
1951da177e4SLinus Torvalds 		struct acpi_device_power_state *ps = &device->power.states[i];
1961da177e4SLinus Torvalds 		char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 		/* Evaluate "_PRx" to se if power resources are referenced */
1991da177e4SLinus Torvalds 		acpi_evaluate_reference(device->handle, object_name, NULL,
2001da177e4SLinus Torvalds 					&ps->resources);
2011da177e4SLinus Torvalds 		if (ps->resources.count) {
2021da177e4SLinus Torvalds 			device->power.flags.power_resources = 1;
2031da177e4SLinus Torvalds 			ps->flags.valid = 1;
2041da177e4SLinus Torvalds 		}
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 		/* Evaluate "_PSx" to see if we can do explicit sets */
2071da177e4SLinus Torvalds 		object_name[2] = 'S';
2081da177e4SLinus Torvalds 		status = acpi_get_handle(device->handle, object_name, &handle);
2091da177e4SLinus Torvalds 		if (ACPI_SUCCESS(status)) {
2101da177e4SLinus Torvalds 			ps->flags.explicit_set = 1;
2111da177e4SLinus Torvalds 			ps->flags.valid = 1;
2121da177e4SLinus Torvalds 		}
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 		/* State is valid if we have some power control */
2151da177e4SLinus Torvalds 		if (ps->resources.count || ps->flags.explicit_set)
2161da177e4SLinus Torvalds 			ps->flags.valid = 1;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 		ps->power = -1;	/* Unknown - driver assigned */
2191da177e4SLinus Torvalds 		ps->latency = -1;	/* Unknown - driver assigned */
2201da177e4SLinus Torvalds 	}
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	/* Set defaults for D0 and D3 states (always valid) */
2231da177e4SLinus Torvalds 	device->power.states[ACPI_STATE_D0].flags.valid = 1;
2241da177e4SLinus Torvalds 	device->power.states[ACPI_STATE_D0].power = 100;
2251da177e4SLinus Torvalds 	device->power.states[ACPI_STATE_D3].flags.valid = 1;
2261da177e4SLinus Torvalds 	device->power.states[ACPI_STATE_D3].power = 0;
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	/* TBD: System wake support and resource requirements. */
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	device->power.state = ACPI_STATE_UNKNOWN;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	return_VALUE(0);
2331da177e4SLinus Torvalds }
2341da177e4SLinus Torvalds 
2354be44fcdSLen Brown int acpi_match_ids(struct acpi_device *device, char *ids)
2361da177e4SLinus Torvalds {
2371da177e4SLinus Torvalds 	int error = 0;
2381da177e4SLinus Torvalds 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	if (device->flags.hardware_id)
2411da177e4SLinus Torvalds 		if (strstr(ids, device->pnp.hardware_id))
2421da177e4SLinus Torvalds 			goto Done;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	if (device->flags.compatible_ids) {
2451da177e4SLinus Torvalds 		struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
2461da177e4SLinus Torvalds 		int i;
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 		/* compare multiple _CID entries against driver ids */
2494be44fcdSLen Brown 		for (i = 0; i < cid_list->count; i++) {
2501da177e4SLinus Torvalds 			if (strstr(ids, cid_list->id[i].value))
2511da177e4SLinus Torvalds 				goto Done;
2521da177e4SLinus Torvalds 		}
2531da177e4SLinus Torvalds 	}
2541da177e4SLinus Torvalds 	error = -ENOENT;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds       Done:
2571da177e4SLinus Torvalds 	if (buffer.pointer)
2581da177e4SLinus Torvalds 		acpi_os_free(buffer.pointer);
2591da177e4SLinus Torvalds 	return error;
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds static acpi_status
2634be44fcdSLen Brown acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
2641da177e4SLinus Torvalds 					     union acpi_object *package)
2651da177e4SLinus Torvalds {
2661da177e4SLinus Torvalds 	int i = 0;
2671da177e4SLinus Torvalds 	union acpi_object *element = NULL;
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	if (!device || !package || (package->package.count < 2))
2701da177e4SLinus Torvalds 		return AE_BAD_PARAMETER;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	element = &(package->package.elements[0]);
2731da177e4SLinus Torvalds 	if (!element)
2741da177e4SLinus Torvalds 		return AE_BAD_PARAMETER;
2751da177e4SLinus Torvalds 	if (element->type == ACPI_TYPE_PACKAGE) {
2761da177e4SLinus Torvalds 		if ((element->package.count < 2) ||
2774be44fcdSLen Brown 		    (element->package.elements[0].type !=
2784be44fcdSLen Brown 		     ACPI_TYPE_LOCAL_REFERENCE)
2794be44fcdSLen Brown 		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
2801da177e4SLinus Torvalds 			return AE_BAD_DATA;
2814be44fcdSLen Brown 		device->wakeup.gpe_device =
2824be44fcdSLen Brown 		    element->package.elements[0].reference.handle;
2834be44fcdSLen Brown 		device->wakeup.gpe_number =
2844be44fcdSLen Brown 		    (u32) element->package.elements[1].integer.value;
2851da177e4SLinus Torvalds 	} else if (element->type == ACPI_TYPE_INTEGER) {
2861da177e4SLinus Torvalds 		device->wakeup.gpe_number = element->integer.value;
2871da177e4SLinus Torvalds 	} else
2881da177e4SLinus Torvalds 		return AE_BAD_DATA;
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	element = &(package->package.elements[1]);
2911da177e4SLinus Torvalds 	if (element->type != ACPI_TYPE_INTEGER) {
2921da177e4SLinus Torvalds 		return AE_BAD_DATA;
2931da177e4SLinus Torvalds 	}
2941da177e4SLinus Torvalds 	device->wakeup.sleep_state = element->integer.value;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
2971da177e4SLinus Torvalds 		return AE_NO_MEMORY;
2981da177e4SLinus Torvalds 	}
2991da177e4SLinus Torvalds 	device->wakeup.resources.count = package->package.count - 2;
3001da177e4SLinus Torvalds 	for (i = 0; i < device->wakeup.resources.count; i++) {
3011da177e4SLinus Torvalds 		element = &(package->package.elements[i + 2]);
3021da177e4SLinus Torvalds 		if (element->type != ACPI_TYPE_ANY) {
3031da177e4SLinus Torvalds 			return AE_BAD_DATA;
3041da177e4SLinus Torvalds 		}
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 		device->wakeup.resources.handles[i] = element->reference.handle;
3071da177e4SLinus Torvalds 	}
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	return AE_OK;
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
3124be44fcdSLen Brown static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
3131da177e4SLinus Torvalds {
3141da177e4SLinus Torvalds 	acpi_status status = 0;
3151da177e4SLinus Torvalds 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
3161da177e4SLinus Torvalds 	union acpi_object *package = NULL;
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_bus_get_wakeup_flags");
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	/* _PRW */
3211da177e4SLinus Torvalds 	status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
3221da177e4SLinus Torvalds 	if (ACPI_FAILURE(status)) {
3231da177e4SLinus Torvalds 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PRW\n"));
3241da177e4SLinus Torvalds 		goto end;
3251da177e4SLinus Torvalds 	}
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 	package = (union acpi_object *)buffer.pointer;
3281da177e4SLinus Torvalds 	status = acpi_bus_extract_wakeup_device_power_package(device, package);
3291da177e4SLinus Torvalds 	if (ACPI_FAILURE(status)) {
3304be44fcdSLen Brown 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
3314be44fcdSLen Brown 				  "Error extracting _PRW package\n"));
3321da177e4SLinus Torvalds 		goto end;
3331da177e4SLinus Torvalds 	}
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	acpi_os_free(buffer.pointer);
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	device->wakeup.flags.valid = 1;
3381da177e4SLinus Torvalds 	/* Power button, Lid switch always enable wakeup */
3391da177e4SLinus Torvalds 	if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
3401da177e4SLinus Torvalds 		device->wakeup.flags.run_wake = 1;
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds       end:
3431da177e4SLinus Torvalds 	if (ACPI_FAILURE(status))
3441da177e4SLinus Torvalds 		device->flags.wake_capable = 0;
3451da177e4SLinus Torvalds 	return_VALUE(0);
3461da177e4SLinus Torvalds }
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds /* --------------------------------------------------------------------------
349312c004dSKay Sievers 		ACPI sysfs device file support
3501da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
3511da177e4SLinus Torvalds static ssize_t acpi_eject_store(struct acpi_device *device,
3521da177e4SLinus Torvalds 				const char *buf, size_t count);
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds #define ACPI_DEVICE_ATTR(_name,_mode,_show,_store) \
3551da177e4SLinus Torvalds static struct acpi_device_attribute acpi_device_attr_##_name = \
3561da177e4SLinus Torvalds 		__ATTR(_name, _mode, _show, _store)
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds ACPI_DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds /**
3611da177e4SLinus Torvalds  * setup_sys_fs_device_files - sets up the device files under device namespace
36267be2dd1SMartin Waitz  * @dev:	acpi_device object
36367be2dd1SMartin Waitz  * @func:	function pointer to create or destroy the device file
3641da177e4SLinus Torvalds  */
3651da177e4SLinus Torvalds static void
3664be44fcdSLen Brown setup_sys_fs_device_files(struct acpi_device *dev,
3671da177e4SLinus Torvalds 			  acpi_device_sysfs_files * func)
3681da177e4SLinus Torvalds {
3691da177e4SLinus Torvalds 	acpi_status status;
3701da177e4SLinus Torvalds 	acpi_handle temp = NULL;
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	/*
3731da177e4SLinus Torvalds 	 * If device has _EJ0, 'eject' file is created that is used to trigger
3741da177e4SLinus Torvalds 	 * hot-removal function from userland.
3751da177e4SLinus Torvalds 	 */
3761da177e4SLinus Torvalds 	status = acpi_get_handle(dev->handle, "_EJ0", &temp);
3771da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
3781da177e4SLinus Torvalds 		(*(func)) (&dev->kobj, &acpi_device_attr_eject.attr);
3791da177e4SLinus Torvalds }
3801da177e4SLinus Torvalds 
3814be44fcdSLen Brown static int acpi_eject_operation(acpi_handle handle, int lockable)
3821da177e4SLinus Torvalds {
3831da177e4SLinus Torvalds 	struct acpi_object_list arg_list;
3841da177e4SLinus Torvalds 	union acpi_object arg;
3851da177e4SLinus Torvalds 	acpi_status status = AE_OK;
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	/*
3881da177e4SLinus Torvalds 	 * TBD: evaluate _PS3?
3891da177e4SLinus Torvalds 	 */
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	if (lockable) {
3921da177e4SLinus Torvalds 		arg_list.count = 1;
3931da177e4SLinus Torvalds 		arg_list.pointer = &arg;
3941da177e4SLinus Torvalds 		arg.type = ACPI_TYPE_INTEGER;
3951da177e4SLinus Torvalds 		arg.integer.value = 0;
3961da177e4SLinus Torvalds 		acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
3971da177e4SLinus Torvalds 	}
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	arg_list.count = 1;
4001da177e4SLinus Torvalds 	arg_list.pointer = &arg;
4011da177e4SLinus Torvalds 	arg.type = ACPI_TYPE_INTEGER;
4021da177e4SLinus Torvalds 	arg.integer.value = 1;
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 	/*
4051da177e4SLinus Torvalds 	 * TBD: _EJD support.
4061da177e4SLinus Torvalds 	 */
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
4091da177e4SLinus Torvalds 	if (ACPI_FAILURE(status)) {
4101da177e4SLinus Torvalds 		return (-ENODEV);
4111da177e4SLinus Torvalds 	}
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	return (0);
4141da177e4SLinus Torvalds }
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds static ssize_t
4171da177e4SLinus Torvalds acpi_eject_store(struct acpi_device *device, const char *buf, size_t count)
4181da177e4SLinus Torvalds {
4191da177e4SLinus Torvalds 	int result;
4201da177e4SLinus Torvalds 	int ret = count;
4211da177e4SLinus Torvalds 	int islockable;
4221da177e4SLinus Torvalds 	acpi_status status;
4231da177e4SLinus Torvalds 	acpi_handle handle;
4241da177e4SLinus Torvalds 	acpi_object_type type = 0;
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	if ((!count) || (buf[0] != '1')) {
4271da177e4SLinus Torvalds 		return -EINVAL;
4281da177e4SLinus Torvalds 	}
4291da177e4SLinus Torvalds #ifndef FORCE_EJECT
4301da177e4SLinus Torvalds 	if (device->driver == NULL) {
4311da177e4SLinus Torvalds 		ret = -ENODEV;
4321da177e4SLinus Torvalds 		goto err;
4331da177e4SLinus Torvalds 	}
4341da177e4SLinus Torvalds #endif
4351da177e4SLinus Torvalds 	status = acpi_get_type(device->handle, &type);
4361da177e4SLinus Torvalds 	if (ACPI_FAILURE(status) || (!device->flags.ejectable)) {
4371da177e4SLinus Torvalds 		ret = -ENODEV;
4381da177e4SLinus Torvalds 		goto err;
4391da177e4SLinus Torvalds 	}
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	islockable = device->flags.lockable;
4421da177e4SLinus Torvalds 	handle = device->handle;
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	if (type == ACPI_TYPE_PROCESSOR)
4451da177e4SLinus Torvalds 		result = acpi_bus_trim(device, 0);
4461da177e4SLinus Torvalds 	else
4471da177e4SLinus Torvalds 		result = acpi_bus_trim(device, 1);
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	if (!result)
4501da177e4SLinus Torvalds 		result = acpi_eject_operation(handle, islockable);
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	if (result) {
4531da177e4SLinus Torvalds 		ret = -EBUSY;
4541da177e4SLinus Torvalds 	}
4551da177e4SLinus Torvalds       err:
4561da177e4SLinus Torvalds 	return ret;
4571da177e4SLinus Torvalds }
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds /* --------------------------------------------------------------------------
4601da177e4SLinus Torvalds                               Performance Management
4611da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
4621da177e4SLinus Torvalds 
4634be44fcdSLen Brown static int acpi_bus_get_perf_flags(struct acpi_device *device)
4641da177e4SLinus Torvalds {
4651da177e4SLinus Torvalds 	device->performance.state = ACPI_STATE_UNKNOWN;
4661da177e4SLinus Torvalds 	return 0;
4671da177e4SLinus Torvalds }
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds /* --------------------------------------------------------------------------
4701da177e4SLinus Torvalds                                  Driver Management
4711da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds static LIST_HEAD(acpi_bus_drivers);
4741da177e4SLinus Torvalds static DECLARE_MUTEX(acpi_bus_drivers_lock);
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds /**
477d758a8faSRandy Dunlap  * acpi_bus_match - match device IDs to driver's supported IDs
478d758a8faSRandy Dunlap  * @device: the device that we are trying to match to a driver
479d758a8faSRandy Dunlap  * @driver: driver whose device id table is being checked
480d758a8faSRandy Dunlap  *
4811da177e4SLinus Torvalds  * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it
4821da177e4SLinus Torvalds  * matches the specified driver's criteria.
4831da177e4SLinus Torvalds  */
4841da177e4SLinus Torvalds static int
4854be44fcdSLen Brown acpi_bus_match(struct acpi_device *device, struct acpi_driver *driver)
4861da177e4SLinus Torvalds {
4871da177e4SLinus Torvalds 	if (driver && driver->ops.match)
4881da177e4SLinus Torvalds 		return driver->ops.match(device, driver);
4891da177e4SLinus Torvalds 	return acpi_match_ids(device, driver->ids);
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds /**
493d758a8faSRandy Dunlap  * acpi_bus_driver_init - add a device to a driver
494d758a8faSRandy Dunlap  * @device: the device to add and initialize
495d758a8faSRandy Dunlap  * @driver: driver for the device
496d758a8faSRandy Dunlap  *
4971da177e4SLinus Torvalds  * Used to initialize a device via its device driver.  Called whenever a
4981da177e4SLinus Torvalds  * driver is bound to a device.  Invokes the driver's add() and start() ops.
4991da177e4SLinus Torvalds  */
5001da177e4SLinus Torvalds static int
5014be44fcdSLen Brown acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
5021da177e4SLinus Torvalds {
5031da177e4SLinus Torvalds 	int result = 0;
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_bus_driver_init");
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	if (!device || !driver)
5081da177e4SLinus Torvalds 		return_VALUE(-EINVAL);
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	if (!driver->ops.add)
5111da177e4SLinus Torvalds 		return_VALUE(-ENOSYS);
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 	result = driver->ops.add(device);
5141da177e4SLinus Torvalds 	if (result) {
5151da177e4SLinus Torvalds 		device->driver = NULL;
5161da177e4SLinus Torvalds 		acpi_driver_data(device) = NULL;
5171da177e4SLinus Torvalds 		return_VALUE(result);
5181da177e4SLinus Torvalds 	}
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	device->driver = driver;
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	/*
5231da177e4SLinus Torvalds 	 * TBD - Configuration Management: Assign resources to device based
5241da177e4SLinus Torvalds 	 * upon possible configuration and currently allocated resources.
5251da177e4SLinus Torvalds 	 */
5261da177e4SLinus Torvalds 
5274be44fcdSLen Brown 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
5284be44fcdSLen Brown 			  "Driver successfully bound to device\n"));
5293fb02738SRajesh Shah 	return_VALUE(0);
5303fb02738SRajesh Shah }
5313fb02738SRajesh Shah 
5328713cbefSAdrian Bunk static int acpi_start_single_object(struct acpi_device *device)
5333fb02738SRajesh Shah {
5343fb02738SRajesh Shah 	int result = 0;
5353fb02738SRajesh Shah 	struct acpi_driver *driver;
5363fb02738SRajesh Shah 
5373fb02738SRajesh Shah 	ACPI_FUNCTION_TRACE("acpi_start_single_object");
5383fb02738SRajesh Shah 
5393fb02738SRajesh Shah 	if (!(driver = device->driver))
5403fb02738SRajesh Shah 		return_VALUE(0);
5413fb02738SRajesh Shah 
5421da177e4SLinus Torvalds 	if (driver->ops.start) {
5431da177e4SLinus Torvalds 		result = driver->ops.start(device);
5441da177e4SLinus Torvalds 		if (result && driver->ops.remove)
5451da177e4SLinus Torvalds 			driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
5463fb02738SRajesh Shah 	}
5473fb02738SRajesh Shah 
5481da177e4SLinus Torvalds 	return_VALUE(result);
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds 
551*9d9f749bSBjorn Helgaas static void acpi_driver_attach(struct acpi_driver *drv)
5521da177e4SLinus Torvalds {
5531da177e4SLinus Torvalds 	struct list_head *node, *next;
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_driver_attach");
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	spin_lock(&acpi_device_lock);
5581da177e4SLinus Torvalds 	list_for_each_safe(node, next, &acpi_device_list) {
5594be44fcdSLen Brown 		struct acpi_device *dev =
5604be44fcdSLen Brown 		    container_of(node, struct acpi_device, g_list);
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 		if (dev->driver || !dev->status.present)
5631da177e4SLinus Torvalds 			continue;
5641da177e4SLinus Torvalds 		spin_unlock(&acpi_device_lock);
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 		if (!acpi_bus_match(dev, drv)) {
5671da177e4SLinus Torvalds 			if (!acpi_bus_driver_init(dev, drv)) {
5683fb02738SRajesh Shah 				acpi_start_single_object(dev);
5691da177e4SLinus Torvalds 				atomic_inc(&drv->references);
5704be44fcdSLen Brown 				ACPI_DEBUG_PRINT((ACPI_DB_INFO,
5714be44fcdSLen Brown 						  "Found driver [%s] for device [%s]\n",
5721da177e4SLinus Torvalds 						  drv->name, dev->pnp.bus_id));
5731da177e4SLinus Torvalds 			}
5741da177e4SLinus Torvalds 		}
5751da177e4SLinus Torvalds 		spin_lock(&acpi_device_lock);
5761da177e4SLinus Torvalds 	}
5771da177e4SLinus Torvalds 	spin_unlock(&acpi_device_lock);
5781da177e4SLinus Torvalds }
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds static int acpi_driver_detach(struct acpi_driver *drv)
5811da177e4SLinus Torvalds {
5821da177e4SLinus Torvalds 	struct list_head *node, *next;
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_driver_detach");
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	spin_lock(&acpi_device_lock);
5871da177e4SLinus Torvalds 	list_for_each_safe(node, next, &acpi_device_list) {
5884be44fcdSLen Brown 		struct acpi_device *dev =
5894be44fcdSLen Brown 		    container_of(node, struct acpi_device, g_list);
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 		if (dev->driver == drv) {
5921da177e4SLinus Torvalds 			spin_unlock(&acpi_device_lock);
5931da177e4SLinus Torvalds 			if (drv->ops.remove)
5941da177e4SLinus Torvalds 				drv->ops.remove(dev, ACPI_BUS_REMOVAL_NORMAL);
5951da177e4SLinus Torvalds 			spin_lock(&acpi_device_lock);
5961da177e4SLinus Torvalds 			dev->driver = NULL;
5971da177e4SLinus Torvalds 			dev->driver_data = NULL;
5981da177e4SLinus Torvalds 			atomic_dec(&drv->references);
5991da177e4SLinus Torvalds 		}
6001da177e4SLinus Torvalds 	}
6011da177e4SLinus Torvalds 	spin_unlock(&acpi_device_lock);
6021da177e4SLinus Torvalds 	return_VALUE(0);
6031da177e4SLinus Torvalds }
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds /**
606d758a8faSRandy Dunlap  * acpi_bus_register_driver - register a driver with the ACPI bus
607d758a8faSRandy Dunlap  * @driver: driver being registered
608d758a8faSRandy Dunlap  *
6091da177e4SLinus Torvalds  * Registers a driver with the ACPI bus.  Searches the namespace for all
610*9d9f749bSBjorn Helgaas  * devices that match the driver's criteria and binds.  Returns zero for
611*9d9f749bSBjorn Helgaas  * success or a negative error status for failure.
6121da177e4SLinus Torvalds  */
6134be44fcdSLen Brown int acpi_bus_register_driver(struct acpi_driver *driver)
6141da177e4SLinus Torvalds {
6151da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_bus_register_driver");
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	if (acpi_disabled)
6181da177e4SLinus Torvalds 		return_VALUE(-ENODEV);
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 	if (!driver)
6211da177e4SLinus Torvalds 		return_VALUE(-EINVAL);
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	spin_lock(&acpi_device_lock);
6241da177e4SLinus Torvalds 	list_add_tail(&driver->node, &acpi_bus_drivers);
6251da177e4SLinus Torvalds 	spin_unlock(&acpi_device_lock);
626*9d9f749bSBjorn Helgaas 	acpi_driver_attach(driver);
6271da177e4SLinus Torvalds 
628*9d9f749bSBjorn Helgaas 	return_VALUE(0);
6291da177e4SLinus Torvalds }
6301da177e4SLinus Torvalds 
6314be44fcdSLen Brown EXPORT_SYMBOL(acpi_bus_register_driver);
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds /**
634d758a8faSRandy Dunlap  * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
635d758a8faSRandy Dunlap  * @driver: driver to unregister
636d758a8faSRandy Dunlap  *
6371da177e4SLinus Torvalds  * Unregisters a driver with the ACPI bus.  Searches the namespace for all
6381da177e4SLinus Torvalds  * devices that match the driver's criteria and unbinds.
6391da177e4SLinus Torvalds  */
6404be44fcdSLen Brown int acpi_bus_unregister_driver(struct acpi_driver *driver)
6411da177e4SLinus Torvalds {
6421da177e4SLinus Torvalds 	int error = 0;
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_bus_unregister_driver");
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	if (driver) {
6471da177e4SLinus Torvalds 		acpi_driver_detach(driver);
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 		if (!atomic_read(&driver->references)) {
6501da177e4SLinus Torvalds 			spin_lock(&acpi_device_lock);
6511da177e4SLinus Torvalds 			list_del_init(&driver->node);
6521da177e4SLinus Torvalds 			spin_unlock(&acpi_device_lock);
6531da177e4SLinus Torvalds 		}
6541da177e4SLinus Torvalds 	} else
6551da177e4SLinus Torvalds 		error = -EINVAL;
6561da177e4SLinus Torvalds 	return_VALUE(error);
6571da177e4SLinus Torvalds }
6584be44fcdSLen Brown 
6591da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_unregister_driver);
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds /**
662d758a8faSRandy Dunlap  * acpi_bus_find_driver - check if there is a driver installed for the device
663d758a8faSRandy Dunlap  * @device: device that we are trying to find a supporting driver for
664d758a8faSRandy Dunlap  *
6651da177e4SLinus Torvalds  * Parses the list of registered drivers looking for a driver applicable for
6661da177e4SLinus Torvalds  * the specified device.
6671da177e4SLinus Torvalds  */
6684be44fcdSLen Brown static int acpi_bus_find_driver(struct acpi_device *device)
6691da177e4SLinus Torvalds {
6701da177e4SLinus Torvalds 	int result = 0;
6711da177e4SLinus Torvalds 	struct list_head *node, *next;
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_bus_find_driver");
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 	spin_lock(&acpi_device_lock);
6761da177e4SLinus Torvalds 	list_for_each_safe(node, next, &acpi_bus_drivers) {
6774be44fcdSLen Brown 		struct acpi_driver *driver =
6784be44fcdSLen Brown 		    container_of(node, struct acpi_driver, node);
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 		atomic_inc(&driver->references);
6811da177e4SLinus Torvalds 		spin_unlock(&acpi_device_lock);
6821da177e4SLinus Torvalds 		if (!acpi_bus_match(device, driver)) {
6831da177e4SLinus Torvalds 			result = acpi_bus_driver_init(device, driver);
6841da177e4SLinus Torvalds 			if (!result)
6851da177e4SLinus Torvalds 				goto Done;
6861da177e4SLinus Torvalds 		}
6871da177e4SLinus Torvalds 		atomic_dec(&driver->references);
6881da177e4SLinus Torvalds 		spin_lock(&acpi_device_lock);
6891da177e4SLinus Torvalds 	}
6901da177e4SLinus Torvalds 	spin_unlock(&acpi_device_lock);
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds       Done:
6931da177e4SLinus Torvalds 	return_VALUE(result);
6941da177e4SLinus Torvalds }
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds /* --------------------------------------------------------------------------
6971da177e4SLinus Torvalds                                  Device Enumeration
6981da177e4SLinus Torvalds    -------------------------------------------------------------------------- */
6991da177e4SLinus Torvalds 
7004be44fcdSLen Brown static int acpi_bus_get_flags(struct acpi_device *device)
7011da177e4SLinus Torvalds {
7021da177e4SLinus Torvalds 	acpi_status status = AE_OK;
7031da177e4SLinus Torvalds 	acpi_handle temp = NULL;
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_bus_get_flags");
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	/* Presence of _STA indicates 'dynamic_status' */
7081da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_STA", &temp);
7091da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
7101da177e4SLinus Torvalds 		device->flags.dynamic_status = 1;
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 	/* Presence of _CID indicates 'compatible_ids' */
7131da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_CID", &temp);
7141da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
7151da177e4SLinus Torvalds 		device->flags.compatible_ids = 1;
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	/* Presence of _RMV indicates 'removable' */
7181da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_RMV", &temp);
7191da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
7201da177e4SLinus Torvalds 		device->flags.removable = 1;
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 	/* Presence of _EJD|_EJ0 indicates 'ejectable' */
7231da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_EJD", &temp);
7241da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
7251da177e4SLinus Torvalds 		device->flags.ejectable = 1;
7261da177e4SLinus Torvalds 	else {
7271da177e4SLinus Torvalds 		status = acpi_get_handle(device->handle, "_EJ0", &temp);
7281da177e4SLinus Torvalds 		if (ACPI_SUCCESS(status))
7291da177e4SLinus Torvalds 			device->flags.ejectable = 1;
7301da177e4SLinus Torvalds 	}
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	/* Presence of _LCK indicates 'lockable' */
7331da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_LCK", &temp);
7341da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
7351da177e4SLinus Torvalds 		device->flags.lockable = 1;
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 	/* Presence of _PS0|_PR0 indicates 'power manageable' */
7381da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_PS0", &temp);
7391da177e4SLinus Torvalds 	if (ACPI_FAILURE(status))
7401da177e4SLinus Torvalds 		status = acpi_get_handle(device->handle, "_PR0", &temp);
7411da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
7421da177e4SLinus Torvalds 		device->flags.power_manageable = 1;
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	/* Presence of _PRW indicates wake capable */
7451da177e4SLinus Torvalds 	status = acpi_get_handle(device->handle, "_PRW", &temp);
7461da177e4SLinus Torvalds 	if (ACPI_SUCCESS(status))
7471da177e4SLinus Torvalds 		device->flags.wake_capable = 1;
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 	/* TBD: Peformance management */
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 	return_VALUE(0);
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds 
7544be44fcdSLen Brown static void acpi_device_get_busid(struct acpi_device *device,
7554be44fcdSLen Brown 				  acpi_handle handle, int type)
7561da177e4SLinus Torvalds {
7571da177e4SLinus Torvalds 	char bus_id[5] = { '?', 0 };
7581da177e4SLinus Torvalds 	struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
7591da177e4SLinus Torvalds 	int i = 0;
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	/*
7621da177e4SLinus Torvalds 	 * Bus ID
7631da177e4SLinus Torvalds 	 * ------
7641da177e4SLinus Torvalds 	 * The device's Bus ID is simply the object name.
7651da177e4SLinus Torvalds 	 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
7661da177e4SLinus Torvalds 	 */
7671da177e4SLinus Torvalds 	switch (type) {
7681da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SYSTEM:
7691da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "ACPI");
7701da177e4SLinus Torvalds 		break;
7711da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
7721da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "PWRF");
7731da177e4SLinus Torvalds 		break;
7741da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
7751da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, "SLPF");
7761da177e4SLinus Torvalds 		break;
7771da177e4SLinus Torvalds 	default:
7781da177e4SLinus Torvalds 		acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
7791da177e4SLinus Torvalds 		/* Clean up trailing underscores (if any) */
7801da177e4SLinus Torvalds 		for (i = 3; i > 1; i--) {
7811da177e4SLinus Torvalds 			if (bus_id[i] == '_')
7821da177e4SLinus Torvalds 				bus_id[i] = '\0';
7831da177e4SLinus Torvalds 			else
7841da177e4SLinus Torvalds 				break;
7851da177e4SLinus Torvalds 		}
7861da177e4SLinus Torvalds 		strcpy(device->pnp.bus_id, bus_id);
7871da177e4SLinus Torvalds 		break;
7881da177e4SLinus Torvalds 	}
7891da177e4SLinus Torvalds }
7901da177e4SLinus Torvalds 
7914be44fcdSLen Brown static void acpi_device_set_id(struct acpi_device *device,
7924be44fcdSLen Brown 			       struct acpi_device *parent, acpi_handle handle,
7934be44fcdSLen Brown 			       int type)
7941da177e4SLinus Torvalds {
7951da177e4SLinus Torvalds 	struct acpi_device_info *info;
7961da177e4SLinus Torvalds 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
7971da177e4SLinus Torvalds 	char *hid = NULL;
7981da177e4SLinus Torvalds 	char *uid = NULL;
7991da177e4SLinus Torvalds 	struct acpi_compatible_id_list *cid_list = NULL;
8001da177e4SLinus Torvalds 	acpi_status status;
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	switch (type) {
8031da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
8041da177e4SLinus Torvalds 		status = acpi_get_object_info(handle, &buffer);
8051da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
8061da177e4SLinus Torvalds 			printk("%s: Error reading device info\n", __FUNCTION__);
8071da177e4SLinus Torvalds 			return;
8081da177e4SLinus Torvalds 		}
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 		info = buffer.pointer;
8111da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_HID)
8121da177e4SLinus Torvalds 			hid = info->hardware_id.value;
8131da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_UID)
8141da177e4SLinus Torvalds 			uid = info->unique_id.value;
8151da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_CID)
8161da177e4SLinus Torvalds 			cid_list = &info->compatibility_id;
8171da177e4SLinus Torvalds 		if (info->valid & ACPI_VALID_ADR) {
8181da177e4SLinus Torvalds 			device->pnp.bus_address = info->address;
8191da177e4SLinus Torvalds 			device->flags.bus_address = 1;
8201da177e4SLinus Torvalds 		}
8211da177e4SLinus Torvalds 		break;
8221da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER:
8231da177e4SLinus Torvalds 		hid = ACPI_POWER_HID;
8241da177e4SLinus Torvalds 		break;
8251da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_PROCESSOR:
8261da177e4SLinus Torvalds 		hid = ACPI_PROCESSOR_HID;
8271da177e4SLinus Torvalds 		break;
8281da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SYSTEM:
8291da177e4SLinus Torvalds 		hid = ACPI_SYSTEM_HID;
8301da177e4SLinus Torvalds 		break;
8311da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_THERMAL:
8321da177e4SLinus Torvalds 		hid = ACPI_THERMAL_HID;
8331da177e4SLinus Torvalds 		break;
8341da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
8351da177e4SLinus Torvalds 		hid = ACPI_BUTTON_HID_POWERF;
8361da177e4SLinus Torvalds 		break;
8371da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
8381da177e4SLinus Torvalds 		hid = ACPI_BUTTON_HID_SLEEPF;
8391da177e4SLinus Torvalds 		break;
8401da177e4SLinus Torvalds 	}
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	/*
8431da177e4SLinus Torvalds 	 * \_SB
8441da177e4SLinus Torvalds 	 * ----
8451da177e4SLinus Torvalds 	 * Fix for the system root bus device -- the only root-level device.
8461da177e4SLinus Torvalds 	 */
847c51a4de8SBob Moore 	if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
8481da177e4SLinus Torvalds 		hid = ACPI_BUS_HID;
8491da177e4SLinus Torvalds 		strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
8501da177e4SLinus Torvalds 		strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
8511da177e4SLinus Torvalds 	}
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 	if (hid) {
8541da177e4SLinus Torvalds 		strcpy(device->pnp.hardware_id, hid);
8551da177e4SLinus Torvalds 		device->flags.hardware_id = 1;
8561da177e4SLinus Torvalds 	}
8571da177e4SLinus Torvalds 	if (uid) {
8581da177e4SLinus Torvalds 		strcpy(device->pnp.unique_id, uid);
8591da177e4SLinus Torvalds 		device->flags.unique_id = 1;
8601da177e4SLinus Torvalds 	}
8611da177e4SLinus Torvalds 	if (cid_list) {
8621da177e4SLinus Torvalds 		device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
8631da177e4SLinus Torvalds 		if (device->pnp.cid_list)
8641da177e4SLinus Torvalds 			memcpy(device->pnp.cid_list, cid_list, cid_list->size);
8651da177e4SLinus Torvalds 		else
8661da177e4SLinus Torvalds 			printk(KERN_ERR "Memory allocation error\n");
8671da177e4SLinus Torvalds 	}
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	acpi_os_free(buffer.pointer);
8701da177e4SLinus Torvalds }
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds static int acpi_device_set_context(struct acpi_device *device, int type)
8731da177e4SLinus Torvalds {
8741da177e4SLinus Torvalds 	acpi_status status = AE_OK;
8751da177e4SLinus Torvalds 	int result = 0;
8761da177e4SLinus Torvalds 	/*
8771da177e4SLinus Torvalds 	 * Context
8781da177e4SLinus Torvalds 	 * -------
8791da177e4SLinus Torvalds 	 * Attach this 'struct acpi_device' to the ACPI object.  This makes
8801da177e4SLinus Torvalds 	 * resolutions from handle->device very efficient.  Note that we need
8811da177e4SLinus Torvalds 	 * to be careful with fixed-feature devices as they all attach to the
8821da177e4SLinus Torvalds 	 * root object.
8831da177e4SLinus Torvalds 	 */
8841da177e4SLinus Torvalds 	if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
8851da177e4SLinus Torvalds 	    type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
8861da177e4SLinus Torvalds 		status = acpi_attach_data(device->handle,
8871da177e4SLinus Torvalds 					  acpi_bus_data_handler, device);
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
8901da177e4SLinus Torvalds 			printk("Error attaching device data\n");
8911da177e4SLinus Torvalds 			result = -ENODEV;
8921da177e4SLinus Torvalds 		}
8931da177e4SLinus Torvalds 	}
8941da177e4SLinus Torvalds 	return result;
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds 
8974be44fcdSLen Brown static void acpi_device_get_debug_info(struct acpi_device *device,
8984be44fcdSLen Brown 				       acpi_handle handle, int type)
8991da177e4SLinus Torvalds {
9001da177e4SLinus Torvalds #ifdef CONFIG_ACPI_DEBUG_OUTPUT
9011da177e4SLinus Torvalds 	char *type_string = NULL;
9021da177e4SLinus Torvalds 	char name[80] = { '?', '\0' };
9031da177e4SLinus Torvalds 	struct acpi_buffer buffer = { sizeof(name), name };
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	switch (type) {
9061da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
9071da177e4SLinus Torvalds 		type_string = "Device";
9081da177e4SLinus Torvalds 		acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
9091da177e4SLinus Torvalds 		break;
9101da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER:
9111da177e4SLinus Torvalds 		type_string = "Power Resource";
9121da177e4SLinus Torvalds 		acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
9131da177e4SLinus Torvalds 		break;
9141da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_PROCESSOR:
9151da177e4SLinus Torvalds 		type_string = "Processor";
9161da177e4SLinus Torvalds 		acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
9171da177e4SLinus Torvalds 		break;
9181da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SYSTEM:
9191da177e4SLinus Torvalds 		type_string = "System";
9201da177e4SLinus Torvalds 		acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
9211da177e4SLinus Torvalds 		break;
9221da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_THERMAL:
9231da177e4SLinus Torvalds 		type_string = "Thermal Zone";
9241da177e4SLinus Torvalds 		acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
9251da177e4SLinus Torvalds 		break;
9261da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_POWER_BUTTON:
9271da177e4SLinus Torvalds 		type_string = "Power Button";
9281da177e4SLinus Torvalds 		sprintf(name, "PWRB");
9291da177e4SLinus Torvalds 		break;
9301da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_SLEEP_BUTTON:
9311da177e4SLinus Torvalds 		type_string = "Sleep Button";
9321da177e4SLinus Torvalds 		sprintf(name, "SLPB");
9331da177e4SLinus Torvalds 		break;
9341da177e4SLinus Torvalds 	}
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
9371da177e4SLinus Torvalds #endif				/*CONFIG_ACPI_DEBUG_OUTPUT */
9381da177e4SLinus Torvalds }
9391da177e4SLinus Torvalds 
9404be44fcdSLen Brown static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
9411da177e4SLinus Torvalds {
9421da177e4SLinus Torvalds 	int result = 0;
9431da177e4SLinus Torvalds 	struct acpi_driver *driver;
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_bus_remove");
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	if (!dev)
9481da177e4SLinus Torvalds 		return_VALUE(-EINVAL);
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 	driver = dev->driver;
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 	if ((driver) && (driver->ops.remove)) {
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 		if (driver->ops.stop) {
9551da177e4SLinus Torvalds 			result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT);
9561da177e4SLinus Torvalds 			if (result)
9571da177e4SLinus Torvalds 				return_VALUE(result);
9581da177e4SLinus Torvalds 		}
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 		result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT);
9611da177e4SLinus Torvalds 		if (result) {
9621da177e4SLinus Torvalds 			return_VALUE(result);
9631da177e4SLinus Torvalds 		}
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 		atomic_dec(&dev->driver->references);
9661da177e4SLinus Torvalds 		dev->driver = NULL;
9671da177e4SLinus Torvalds 		acpi_driver_data(dev) = NULL;
9681da177e4SLinus Torvalds 	}
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	if (!rmdevice)
9711da177e4SLinus Torvalds 		return_VALUE(0);
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 	if (dev->flags.bus_address) {
9741da177e4SLinus Torvalds 		if ((dev->parent) && (dev->parent->ops.unbind))
9751da177e4SLinus Torvalds 			dev->parent->ops.unbind(dev);
9761da177e4SLinus Torvalds 	}
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 	acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	return_VALUE(0);
9811da177e4SLinus Torvalds }
9821da177e4SLinus Torvalds 
9833fb02738SRajesh Shah static int
9844be44fcdSLen Brown acpi_add_single_object(struct acpi_device **child,
9854be44fcdSLen Brown 		       struct acpi_device *parent, acpi_handle handle, int type)
9861da177e4SLinus Torvalds {
9871da177e4SLinus Torvalds 	int result = 0;
9881da177e4SLinus Torvalds 	struct acpi_device *device = NULL;
9891da177e4SLinus Torvalds 
9903fb02738SRajesh Shah 	ACPI_FUNCTION_TRACE("acpi_add_single_object");
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 	if (!child)
9931da177e4SLinus Torvalds 		return_VALUE(-EINVAL);
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
9961da177e4SLinus Torvalds 	if (!device) {
9971da177e4SLinus Torvalds 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Memory allocation error\n"));
9981da177e4SLinus Torvalds 		return_VALUE(-ENOMEM);
9991da177e4SLinus Torvalds 	}
10001da177e4SLinus Torvalds 	memset(device, 0, sizeof(struct acpi_device));
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 	device->handle = handle;
10031da177e4SLinus Torvalds 	device->parent = parent;
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	acpi_device_get_busid(device, handle, type);
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	/*
10081da177e4SLinus Torvalds 	 * Flags
10091da177e4SLinus Torvalds 	 * -----
10101da177e4SLinus Torvalds 	 * Get prior to calling acpi_bus_get_status() so we know whether
10111da177e4SLinus Torvalds 	 * or not _STA is present.  Note that we only look for object
10121da177e4SLinus Torvalds 	 * handles -- cannot evaluate objects until we know the device is
10131da177e4SLinus Torvalds 	 * present and properly initialized.
10141da177e4SLinus Torvalds 	 */
10151da177e4SLinus Torvalds 	result = acpi_bus_get_flags(device);
10161da177e4SLinus Torvalds 	if (result)
10171da177e4SLinus Torvalds 		goto end;
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds 	/*
10201da177e4SLinus Torvalds 	 * Status
10211da177e4SLinus Torvalds 	 * ------
10226940fabaSKeiichiro Tokunaga 	 * See if the device is present.  We always assume that non-Device
10236940fabaSKeiichiro Tokunaga 	 * and non-Processor objects (e.g. thermal zones, power resources,
10246940fabaSKeiichiro Tokunaga 	 * etc.) are present, functioning, etc. (at least when parent object
10256940fabaSKeiichiro Tokunaga 	 * is present).  Note that _STA has a different meaning for some
10266940fabaSKeiichiro Tokunaga 	 * objects (e.g. power resources) so we need to be careful how we use
10276940fabaSKeiichiro Tokunaga 	 * it.
10281da177e4SLinus Torvalds 	 */
10291da177e4SLinus Torvalds 	switch (type) {
10306940fabaSKeiichiro Tokunaga 	case ACPI_BUS_TYPE_PROCESSOR:
10311da177e4SLinus Torvalds 	case ACPI_BUS_TYPE_DEVICE:
10321da177e4SLinus Torvalds 		result = acpi_bus_get_status(device);
10331da177e4SLinus Torvalds 		if (ACPI_FAILURE(result) || !device->status.present) {
10341da177e4SLinus Torvalds 			result = -ENOENT;
10351da177e4SLinus Torvalds 			goto end;
10361da177e4SLinus Torvalds 		}
10371da177e4SLinus Torvalds 		break;
10381da177e4SLinus Torvalds 	default:
10391da177e4SLinus Torvalds 		STRUCT_TO_INT(device->status) = 0x0F;
10401da177e4SLinus Torvalds 		break;
10411da177e4SLinus Torvalds 	}
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds 	/*
10441da177e4SLinus Torvalds 	 * Initialize Device
10451da177e4SLinus Torvalds 	 * -----------------
10461da177e4SLinus Torvalds 	 * TBD: Synch with Core's enumeration/initialization process.
10471da177e4SLinus Torvalds 	 */
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds 	/*
10501da177e4SLinus Torvalds 	 * Hardware ID, Unique ID, & Bus Address
10511da177e4SLinus Torvalds 	 * -------------------------------------
10521da177e4SLinus Torvalds 	 */
10531da177e4SLinus Torvalds 	acpi_device_set_id(device, parent, handle, type);
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	/*
10561da177e4SLinus Torvalds 	 * Power Management
10571da177e4SLinus Torvalds 	 * ----------------
10581da177e4SLinus Torvalds 	 */
10591da177e4SLinus Torvalds 	if (device->flags.power_manageable) {
10601da177e4SLinus Torvalds 		result = acpi_bus_get_power_flags(device);
10611da177e4SLinus Torvalds 		if (result)
10621da177e4SLinus Torvalds 			goto end;
10631da177e4SLinus Torvalds 	}
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 	/*
10661da177e4SLinus Torvalds 	 * Wakeup device management
10671da177e4SLinus Torvalds 	 *-----------------------
10681da177e4SLinus Torvalds 	 */
10691da177e4SLinus Torvalds 	if (device->flags.wake_capable) {
10701da177e4SLinus Torvalds 		result = acpi_bus_get_wakeup_device_flags(device);
10711da177e4SLinus Torvalds 		if (result)
10721da177e4SLinus Torvalds 			goto end;
10731da177e4SLinus Torvalds 	}
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 	/*
10761da177e4SLinus Torvalds 	 * Performance Management
10771da177e4SLinus Torvalds 	 * ----------------------
10781da177e4SLinus Torvalds 	 */
10791da177e4SLinus Torvalds 	if (device->flags.performance_manageable) {
10801da177e4SLinus Torvalds 		result = acpi_bus_get_perf_flags(device);
10811da177e4SLinus Torvalds 		if (result)
10821da177e4SLinus Torvalds 			goto end;
10831da177e4SLinus Torvalds 	}
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds 	if ((result = acpi_device_set_context(device, type)))
10861da177e4SLinus Torvalds 		goto end;
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds 	acpi_device_get_debug_info(device, handle, type);
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 	acpi_device_register(device, parent);
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 	/*
10931da177e4SLinus Torvalds 	 * Bind _ADR-Based Devices
10941da177e4SLinus Torvalds 	 * -----------------------
10951da177e4SLinus Torvalds 	 * If there's a a bus address (_ADR) then we utilize the parent's
10961da177e4SLinus Torvalds 	 * 'bind' function (if exists) to bind the ACPI- and natively-
10971da177e4SLinus Torvalds 	 * enumerated device representations.
10981da177e4SLinus Torvalds 	 */
10991da177e4SLinus Torvalds 	if (device->flags.bus_address) {
11001da177e4SLinus Torvalds 		if (device->parent && device->parent->ops.bind)
11011da177e4SLinus Torvalds 			device->parent->ops.bind(device);
11021da177e4SLinus Torvalds 	}
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	/*
11051da177e4SLinus Torvalds 	 * Locate & Attach Driver
11061da177e4SLinus Torvalds 	 * ----------------------
11071da177e4SLinus Torvalds 	 * If there's a hardware id (_HID) or compatible ids (_CID) we check
11081da177e4SLinus Torvalds 	 * to see if there's a driver installed for this kind of device.  Note
11091da177e4SLinus Torvalds 	 * that drivers can install before or after a device is enumerated.
11101da177e4SLinus Torvalds 	 *
11111da177e4SLinus Torvalds 	 * TBD: Assumes LDM provides driver hot-plug capability.
11121da177e4SLinus Torvalds 	 */
1113bd7ce5b5SThomas Renninger 	acpi_bus_find_driver(device);
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds       end:
11161da177e4SLinus Torvalds 	if (!result)
11171da177e4SLinus Torvalds 		*child = device;
11181da177e4SLinus Torvalds 	else {
11191da177e4SLinus Torvalds 		kfree(device->pnp.cid_list);
11201da177e4SLinus Torvalds 		kfree(device);
11211da177e4SLinus Torvalds 	}
11221da177e4SLinus Torvalds 
11231da177e4SLinus Torvalds 	return_VALUE(result);
11241da177e4SLinus Torvalds }
11251da177e4SLinus Torvalds 
11264be44fcdSLen Brown static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
11271da177e4SLinus Torvalds {
11281da177e4SLinus Torvalds 	acpi_status status = AE_OK;
11291da177e4SLinus Torvalds 	struct acpi_device *parent = NULL;
11301da177e4SLinus Torvalds 	struct acpi_device *child = NULL;
11311da177e4SLinus Torvalds 	acpi_handle phandle = NULL;
11321da177e4SLinus Torvalds 	acpi_handle chandle = NULL;
11331da177e4SLinus Torvalds 	acpi_object_type type = 0;
11341da177e4SLinus Torvalds 	u32 level = 1;
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_bus_scan");
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 	if (!start)
11391da177e4SLinus Torvalds 		return_VALUE(-EINVAL);
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds 	parent = start;
11421da177e4SLinus Torvalds 	phandle = start->handle;
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	/*
11451da177e4SLinus Torvalds 	 * Parse through the ACPI namespace, identify all 'devices', and
11461da177e4SLinus Torvalds 	 * create a new 'struct acpi_device' for each.
11471da177e4SLinus Torvalds 	 */
11481da177e4SLinus Torvalds 	while ((level > 0) && parent) {
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds 		status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
11511da177e4SLinus Torvalds 					      chandle, &chandle);
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds 		/*
11541da177e4SLinus Torvalds 		 * If this scope is exhausted then move our way back up.
11551da177e4SLinus Torvalds 		 */
11561da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
11571da177e4SLinus Torvalds 			level--;
11581da177e4SLinus Torvalds 			chandle = phandle;
11591da177e4SLinus Torvalds 			acpi_get_parent(phandle, &phandle);
11601da177e4SLinus Torvalds 			if (parent->parent)
11611da177e4SLinus Torvalds 				parent = parent->parent;
11621da177e4SLinus Torvalds 			continue;
11631da177e4SLinus Torvalds 		}
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 		status = acpi_get_type(chandle, &type);
11661da177e4SLinus Torvalds 		if (ACPI_FAILURE(status))
11671da177e4SLinus Torvalds 			continue;
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 		/*
11701da177e4SLinus Torvalds 		 * If this is a scope object then parse it (depth-first).
11711da177e4SLinus Torvalds 		 */
11721da177e4SLinus Torvalds 		if (type == ACPI_TYPE_LOCAL_SCOPE) {
11731da177e4SLinus Torvalds 			level++;
11741da177e4SLinus Torvalds 			phandle = chandle;
11751da177e4SLinus Torvalds 			chandle = NULL;
11761da177e4SLinus Torvalds 			continue;
11771da177e4SLinus Torvalds 		}
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds 		/*
11801da177e4SLinus Torvalds 		 * We're only interested in objects that we consider 'devices'.
11811da177e4SLinus Torvalds 		 */
11821da177e4SLinus Torvalds 		switch (type) {
11831da177e4SLinus Torvalds 		case ACPI_TYPE_DEVICE:
11841da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_DEVICE;
11851da177e4SLinus Torvalds 			break;
11861da177e4SLinus Torvalds 		case ACPI_TYPE_PROCESSOR:
11871da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_PROCESSOR;
11881da177e4SLinus Torvalds 			break;
11891da177e4SLinus Torvalds 		case ACPI_TYPE_THERMAL:
11901da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_THERMAL;
11911da177e4SLinus Torvalds 			break;
11921da177e4SLinus Torvalds 		case ACPI_TYPE_POWER:
11931da177e4SLinus Torvalds 			type = ACPI_BUS_TYPE_POWER;
11941da177e4SLinus Torvalds 			break;
11951da177e4SLinus Torvalds 		default:
11961da177e4SLinus Torvalds 			continue;
11971da177e4SLinus Torvalds 		}
11981da177e4SLinus Torvalds 
11993fb02738SRajesh Shah 		if (ops->acpi_op_add)
12003fb02738SRajesh Shah 			status = acpi_add_single_object(&child, parent,
12013fb02738SRajesh Shah 							chandle, type);
12023fb02738SRajesh Shah 		else
12033fb02738SRajesh Shah 			status = acpi_bus_get_device(chandle, &child);
12043fb02738SRajesh Shah 
12051da177e4SLinus Torvalds 		if (ACPI_FAILURE(status))
12061da177e4SLinus Torvalds 			continue;
12071da177e4SLinus Torvalds 
12083fb02738SRajesh Shah 		if (ops->acpi_op_start) {
12093fb02738SRajesh Shah 			status = acpi_start_single_object(child);
12103fb02738SRajesh Shah 			if (ACPI_FAILURE(status))
12113fb02738SRajesh Shah 				continue;
12123fb02738SRajesh Shah 		}
12133fb02738SRajesh Shah 
12141da177e4SLinus Torvalds 		/*
12151da177e4SLinus Torvalds 		 * If the device is present, enabled, and functioning then
12161da177e4SLinus Torvalds 		 * parse its scope (depth-first).  Note that we need to
12171da177e4SLinus Torvalds 		 * represent absent devices to facilitate PnP notifications
12181da177e4SLinus Torvalds 		 * -- but only the subtree head (not all of its children,
12191da177e4SLinus Torvalds 		 * which will be enumerated when the parent is inserted).
12201da177e4SLinus Torvalds 		 *
12211da177e4SLinus Torvalds 		 * TBD: Need notifications and other detection mechanisms
12221da177e4SLinus Torvalds 		 *      in place before we can fully implement this.
12231da177e4SLinus Torvalds 		 */
12241da177e4SLinus Torvalds 		if (child->status.present) {
12251da177e4SLinus Torvalds 			status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
12261da177e4SLinus Torvalds 						      NULL, NULL);
12271da177e4SLinus Torvalds 			if (ACPI_SUCCESS(status)) {
12281da177e4SLinus Torvalds 				level++;
12291da177e4SLinus Torvalds 				phandle = chandle;
12301da177e4SLinus Torvalds 				chandle = NULL;
12311da177e4SLinus Torvalds 				parent = child;
12321da177e4SLinus Torvalds 			}
12331da177e4SLinus Torvalds 		}
12341da177e4SLinus Torvalds 	}
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 	return_VALUE(0);
12371da177e4SLinus Torvalds }
12381da177e4SLinus Torvalds 
12393fb02738SRajesh Shah int
12404be44fcdSLen Brown acpi_bus_add(struct acpi_device **child,
12414be44fcdSLen Brown 	     struct acpi_device *parent, acpi_handle handle, int type)
12423fb02738SRajesh Shah {
12433fb02738SRajesh Shah 	int result;
12443fb02738SRajesh Shah 	struct acpi_bus_ops ops;
12453fb02738SRajesh Shah 
12463fb02738SRajesh Shah 	ACPI_FUNCTION_TRACE("acpi_bus_add");
12473fb02738SRajesh Shah 
12483fb02738SRajesh Shah 	result = acpi_add_single_object(child, parent, handle, type);
12493fb02738SRajesh Shah 	if (!result) {
12503fb02738SRajesh Shah 		memset(&ops, 0, sizeof(ops));
12513fb02738SRajesh Shah 		ops.acpi_op_add = 1;
12523fb02738SRajesh Shah 		result = acpi_bus_scan(*child, &ops);
12533fb02738SRajesh Shah 	}
12543fb02738SRajesh Shah 	return_VALUE(result);
12553fb02738SRajesh Shah }
12564be44fcdSLen Brown 
12573fb02738SRajesh Shah EXPORT_SYMBOL(acpi_bus_add);
12583fb02738SRajesh Shah 
12594be44fcdSLen Brown int acpi_bus_start(struct acpi_device *device)
12603fb02738SRajesh Shah {
12613fb02738SRajesh Shah 	int result;
12623fb02738SRajesh Shah 	struct acpi_bus_ops ops;
12633fb02738SRajesh Shah 
12643fb02738SRajesh Shah 	ACPI_FUNCTION_TRACE("acpi_bus_start");
12653fb02738SRajesh Shah 
12663fb02738SRajesh Shah 	if (!device)
12673fb02738SRajesh Shah 		return_VALUE(-EINVAL);
12683fb02738SRajesh Shah 
12693fb02738SRajesh Shah 	result = acpi_start_single_object(device);
12703fb02738SRajesh Shah 	if (!result) {
12713fb02738SRajesh Shah 		memset(&ops, 0, sizeof(ops));
12723fb02738SRajesh Shah 		ops.acpi_op_start = 1;
12733fb02738SRajesh Shah 		result = acpi_bus_scan(device, &ops);
12743fb02738SRajesh Shah 	}
12753fb02738SRajesh Shah 	return_VALUE(result);
12763fb02738SRajesh Shah }
12774be44fcdSLen Brown 
12783fb02738SRajesh Shah EXPORT_SYMBOL(acpi_bus_start);
12791da177e4SLinus Torvalds 
1280ceaba663SKristen Accardi int acpi_bus_trim(struct acpi_device *start, int rmdevice)
12811da177e4SLinus Torvalds {
12821da177e4SLinus Torvalds 	acpi_status status;
12831da177e4SLinus Torvalds 	struct acpi_device *parent, *child;
12841da177e4SLinus Torvalds 	acpi_handle phandle, chandle;
12851da177e4SLinus Torvalds 	acpi_object_type type;
12861da177e4SLinus Torvalds 	u32 level = 1;
12871da177e4SLinus Torvalds 	int err = 0;
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds 	parent = start;
12901da177e4SLinus Torvalds 	phandle = start->handle;
12911da177e4SLinus Torvalds 	child = chandle = NULL;
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds 	while ((level > 0) && parent && (!err)) {
12941da177e4SLinus Torvalds 		status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
12951da177e4SLinus Torvalds 					      chandle, &chandle);
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds 		/*
12981da177e4SLinus Torvalds 		 * If this scope is exhausted then move our way back up.
12991da177e4SLinus Torvalds 		 */
13001da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
13011da177e4SLinus Torvalds 			level--;
13021da177e4SLinus Torvalds 			chandle = phandle;
13031da177e4SLinus Torvalds 			acpi_get_parent(phandle, &phandle);
13041da177e4SLinus Torvalds 			child = parent;
13051da177e4SLinus Torvalds 			parent = parent->parent;
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 			if (level == 0)
13081da177e4SLinus Torvalds 				err = acpi_bus_remove(child, rmdevice);
13091da177e4SLinus Torvalds 			else
13101da177e4SLinus Torvalds 				err = acpi_bus_remove(child, 1);
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds 			continue;
13131da177e4SLinus Torvalds 		}
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 		status = acpi_get_type(chandle, &type);
13161da177e4SLinus Torvalds 		if (ACPI_FAILURE(status)) {
13171da177e4SLinus Torvalds 			continue;
13181da177e4SLinus Torvalds 		}
13191da177e4SLinus Torvalds 		/*
13201da177e4SLinus Torvalds 		 * If there is a device corresponding to chandle then
13211da177e4SLinus Torvalds 		 * parse it (depth-first).
13221da177e4SLinus Torvalds 		 */
13231da177e4SLinus Torvalds 		if (acpi_bus_get_device(chandle, &child) == 0) {
13241da177e4SLinus Torvalds 			level++;
13251da177e4SLinus Torvalds 			phandle = chandle;
13261da177e4SLinus Torvalds 			chandle = NULL;
13271da177e4SLinus Torvalds 			parent = child;
13281da177e4SLinus Torvalds 		}
13291da177e4SLinus Torvalds 		continue;
13301da177e4SLinus Torvalds 	}
13311da177e4SLinus Torvalds 	return err;
13321da177e4SLinus Torvalds }
1333ceaba663SKristen Accardi EXPORT_SYMBOL_GPL(acpi_bus_trim);
1334ceaba663SKristen Accardi 
13351da177e4SLinus Torvalds 
13364be44fcdSLen Brown static int acpi_bus_scan_fixed(struct acpi_device *root)
13371da177e4SLinus Torvalds {
13381da177e4SLinus Torvalds 	int result = 0;
13391da177e4SLinus Torvalds 	struct acpi_device *device = NULL;
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_bus_scan_fixed");
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 	if (!root)
13441da177e4SLinus Torvalds 		return_VALUE(-ENODEV);
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 	/*
13471da177e4SLinus Torvalds 	 * Enumerate all fixed-feature devices.
13481da177e4SLinus Torvalds 	 */
13493fb02738SRajesh Shah 	if (acpi_fadt.pwr_button == 0) {
13503fb02738SRajesh Shah 		result = acpi_add_single_object(&device, acpi_root,
13514be44fcdSLen Brown 						NULL,
13524be44fcdSLen Brown 						ACPI_BUS_TYPE_POWER_BUTTON);
13533fb02738SRajesh Shah 		if (!result)
13543fb02738SRajesh Shah 			result = acpi_start_single_object(device);
13553fb02738SRajesh Shah 	}
13561da177e4SLinus Torvalds 
13573fb02738SRajesh Shah 	if (acpi_fadt.sleep_button == 0) {
13583fb02738SRajesh Shah 		result = acpi_add_single_object(&device, acpi_root,
13594be44fcdSLen Brown 						NULL,
13604be44fcdSLen Brown 						ACPI_BUS_TYPE_SLEEP_BUTTON);
13613fb02738SRajesh Shah 		if (!result)
13623fb02738SRajesh Shah 			result = acpi_start_single_object(device);
13633fb02738SRajesh Shah 	}
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds 	return_VALUE(result);
13661da177e4SLinus Torvalds }
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds static int __init acpi_scan_init(void)
13691da177e4SLinus Torvalds {
13701da177e4SLinus Torvalds 	int result;
13713fb02738SRajesh Shah 	struct acpi_bus_ops ops;
13721da177e4SLinus Torvalds 
13731da177e4SLinus Torvalds 	ACPI_FUNCTION_TRACE("acpi_scan_init");
13741da177e4SLinus Torvalds 
13751da177e4SLinus Torvalds 	if (acpi_disabled)
13761da177e4SLinus Torvalds 		return_VALUE(0);
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds 	kset_register(&acpi_namespace_kset);
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 	/*
13811da177e4SLinus Torvalds 	 * Create the root device in the bus's device tree
13821da177e4SLinus Torvalds 	 */
13833fb02738SRajesh Shah 	result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
13841da177e4SLinus Torvalds 					ACPI_BUS_TYPE_SYSTEM);
13851da177e4SLinus Torvalds 	if (result)
13861da177e4SLinus Torvalds 		goto Done;
13871da177e4SLinus Torvalds 
13883fb02738SRajesh Shah 	result = acpi_start_single_object(acpi_root);
13893fb02738SRajesh Shah 
13901da177e4SLinus Torvalds 	/*
13911da177e4SLinus Torvalds 	 * Enumerate devices in the ACPI namespace.
13921da177e4SLinus Torvalds 	 */
13931da177e4SLinus Torvalds 	result = acpi_bus_scan_fixed(acpi_root);
13943fb02738SRajesh Shah 	if (!result) {
13953fb02738SRajesh Shah 		memset(&ops, 0, sizeof(ops));
13963fb02738SRajesh Shah 		ops.acpi_op_add = 1;
13973fb02738SRajesh Shah 		ops.acpi_op_start = 1;
13983fb02738SRajesh Shah 		result = acpi_bus_scan(acpi_root, &ops);
13993fb02738SRajesh Shah 	}
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 	if (result)
14021da177e4SLinus Torvalds 		acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds       Done:
14051da177e4SLinus Torvalds 	return_VALUE(result);
14061da177e4SLinus Torvalds }
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds subsys_initcall(acpi_scan_init);
1409