| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * scan.c - support for transforming the ACPI namespace into individual objects | 
 | 3 |  */ | 
 | 4 |  | 
 | 5 | #include <linux/module.h> | 
 | 6 | #include <linux/init.h> | 
 | 7 | #include <linux/acpi.h> | 
 | 8 |  | 
 | 9 | #include <acpi/acpi_drivers.h> | 
 | 10 | #include <acpi/acinterp.h>	/* for acpi_ex_eisa_id_to_string() */ | 
 | 11 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #define _COMPONENT		ACPI_BUS_COMPONENT | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 13 | ACPI_MODULE_NAME("scan") | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #define STRUCT_TO_INT(s)	(*((int*)&s)) | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 15 | extern struct acpi_device *acpi_root; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 |  | 
 | 17 | #define ACPI_BUS_CLASS			"system_bus" | 
 | 18 | #define ACPI_BUS_HID			"ACPI_BUS" | 
 | 19 | #define ACPI_BUS_DRIVER_NAME		"ACPI Bus Driver" | 
 | 20 | #define ACPI_BUS_DEVICE_NAME		"System Bus" | 
 | 21 |  | 
 | 22 | static LIST_HEAD(acpi_device_list); | 
 | 23 | DEFINE_SPINLOCK(acpi_device_lock); | 
 | 24 | LIST_HEAD(acpi_wakeup_device_list); | 
 | 25 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 27 | static void acpi_device_release(struct kobject *kobj) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 29 | 	struct acpi_device *dev = container_of(kobj, struct acpi_device, kobj); | 
| Jesper Juhl | 6044ec8 | 2005-11-07 01:01:32 -0800 | [diff] [blame] | 30 | 	kfree(dev->pnp.cid_list); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | 	kfree(dev); | 
 | 32 | } | 
 | 33 |  | 
 | 34 | struct acpi_device_attribute { | 
 | 35 | 	struct attribute attr; | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 36 | 	 ssize_t(*show) (struct acpi_device *, char *); | 
 | 37 | 	 ssize_t(*store) (struct acpi_device *, const char *, size_t); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | }; | 
 | 39 |  | 
 | 40 | typedef void acpi_device_sysfs_files(struct kobject *, | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 41 | 				     const struct attribute *); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 |  | 
 | 43 | static void setup_sys_fs_device_files(struct acpi_device *dev, | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 44 | 				      acpi_device_sysfs_files * func); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 |  | 
 | 46 | #define create_sysfs_device_files(dev)	\ | 
 | 47 | 	setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_create_file) | 
 | 48 | #define remove_sysfs_device_files(dev)	\ | 
 | 49 | 	setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_remove_file) | 
 | 50 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #define to_acpi_device(n) container_of(n, struct acpi_device, kobj) | 
 | 52 | #define to_handle_attr(n) container_of(n, struct acpi_device_attribute, attr); | 
 | 53 |  | 
 | 54 | static ssize_t acpi_device_attr_show(struct kobject *kobj, | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 55 | 				     struct attribute *attr, char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { | 
 | 57 | 	struct acpi_device *device = to_acpi_device(kobj); | 
 | 58 | 	struct acpi_device_attribute *attribute = to_handle_attr(attr); | 
| Dmitry Torokhov | 70f2817 | 2005-04-29 01:27:34 -0500 | [diff] [blame] | 59 | 	return attribute->show ? attribute->show(device, buf) : -EIO; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } | 
 | 61 | static ssize_t acpi_device_attr_store(struct kobject *kobj, | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 62 | 				      struct attribute *attr, const char *buf, | 
 | 63 | 				      size_t len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { | 
 | 65 | 	struct acpi_device *device = to_acpi_device(kobj); | 
 | 66 | 	struct acpi_device_attribute *attribute = to_handle_attr(attr); | 
| Dmitry Torokhov | 70f2817 | 2005-04-29 01:27:34 -0500 | [diff] [blame] | 67 | 	return attribute->store ? attribute->store(device, buf, len) : -EIO; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } | 
 | 69 |  | 
 | 70 | static struct sysfs_ops acpi_device_sysfs_ops = { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 71 | 	.show = acpi_device_attr_show, | 
 | 72 | 	.store = acpi_device_attr_store, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | }; | 
 | 74 |  | 
 | 75 | static struct kobj_type ktype_acpi_ns = { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 76 | 	.sysfs_ops = &acpi_device_sysfs_ops, | 
 | 77 | 	.release = acpi_device_release, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | }; | 
 | 79 |  | 
| Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 80 | static int namespace_uevent(struct kset *kset, struct kobject *kobj, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | 			     char **envp, int num_envp, char *buffer, | 
 | 82 | 			     int buffer_size) | 
 | 83 | { | 
 | 84 | 	struct acpi_device *dev = to_acpi_device(kobj); | 
 | 85 | 	int i = 0; | 
 | 86 | 	int len = 0; | 
 | 87 |  | 
 | 88 | 	if (!dev->driver) | 
 | 89 | 		return 0; | 
 | 90 |  | 
| Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 91 | 	if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len, | 
 | 92 | 			   "PHYSDEVDRIVER=%s", dev->driver->name)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | 		return -ENOMEM; | 
 | 94 |  | 
 | 95 | 	envp[i] = NULL; | 
 | 96 |  | 
 | 97 | 	return 0; | 
 | 98 | } | 
 | 99 |  | 
| Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 100 | static struct kset_uevent_ops namespace_uevent_ops = { | 
 | 101 | 	.uevent = &namespace_uevent, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | }; | 
 | 103 |  | 
 | 104 | static struct kset acpi_namespace_kset = { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 105 | 	.kobj = { | 
 | 106 | 		 .name = "namespace", | 
 | 107 | 		 }, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | 	.subsys = &acpi_subsys, | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 109 | 	.ktype = &ktype_acpi_ns, | 
| Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 110 | 	.uevent_ops = &namespace_uevent_ops, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | }; | 
 | 112 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 113 | static void acpi_device_register(struct acpi_device *device, | 
 | 114 | 				 struct acpi_device *parent) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { | 
 | 116 | 	/* | 
 | 117 | 	 * Linkage | 
 | 118 | 	 * ------- | 
 | 119 | 	 * Link this device to its parent and siblings. | 
 | 120 | 	 */ | 
 | 121 | 	INIT_LIST_HEAD(&device->children); | 
 | 122 | 	INIT_LIST_HEAD(&device->node); | 
 | 123 | 	INIT_LIST_HEAD(&device->g_list); | 
 | 124 | 	INIT_LIST_HEAD(&device->wakeup_list); | 
 | 125 |  | 
 | 126 | 	spin_lock(&acpi_device_lock); | 
 | 127 | 	if (device->parent) { | 
 | 128 | 		list_add_tail(&device->node, &device->parent->children); | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 129 | 		list_add_tail(&device->g_list, &device->parent->g_list); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | 	} else | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 131 | 		list_add_tail(&device->g_list, &acpi_device_list); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | 	if (device->wakeup.flags.valid) | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 133 | 		list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | 	spin_unlock(&acpi_device_lock); | 
 | 135 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 136 | 	strlcpy(device->kobj.name, device->pnp.bus_id, KOBJ_NAME_LEN); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | 	if (parent) | 
 | 138 | 		device->kobj.parent = &parent->kobj; | 
 | 139 | 	device->kobj.ktype = &ktype_acpi_ns; | 
 | 140 | 	device->kobj.kset = &acpi_namespace_kset; | 
 | 141 | 	kobject_register(&device->kobj); | 
 | 142 | 	create_sysfs_device_files(device); | 
 | 143 | } | 
 | 144 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 145 | static int acpi_device_unregister(struct acpi_device *device, int type) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | { | 
 | 147 | 	spin_lock(&acpi_device_lock); | 
 | 148 | 	if (device->parent) { | 
 | 149 | 		list_del(&device->node); | 
 | 150 | 		list_del(&device->g_list); | 
 | 151 | 	} else | 
 | 152 | 		list_del(&device->g_list); | 
 | 153 |  | 
 | 154 | 	list_del(&device->wakeup_list); | 
 | 155 |  | 
 | 156 | 	spin_unlock(&acpi_device_lock); | 
 | 157 |  | 
 | 158 | 	acpi_detach_data(device->handle, acpi_bus_data_handler); | 
 | 159 | 	remove_sysfs_device_files(device); | 
 | 160 | 	kobject_unregister(&device->kobj); | 
 | 161 | 	return 0; | 
 | 162 | } | 
 | 163 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 164 | void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | { | 
 | 166 | 	ACPI_FUNCTION_TRACE("acpi_bus_data_handler"); | 
 | 167 |  | 
 | 168 | 	/* TBD */ | 
 | 169 |  | 
 | 170 | 	return_VOID; | 
 | 171 | } | 
 | 172 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 173 | static int acpi_bus_get_power_flags(struct acpi_device *device) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 175 | 	acpi_status status = 0; | 
 | 176 | 	acpi_handle handle = NULL; | 
 | 177 | 	u32 i = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 |  | 
 | 179 | 	ACPI_FUNCTION_TRACE("acpi_bus_get_power_flags"); | 
 | 180 |  | 
 | 181 | 	/* | 
 | 182 | 	 * Power Management Flags | 
 | 183 | 	 */ | 
 | 184 | 	status = acpi_get_handle(device->handle, "_PSC", &handle); | 
 | 185 | 	if (ACPI_SUCCESS(status)) | 
 | 186 | 		device->power.flags.explicit_get = 1; | 
 | 187 | 	status = acpi_get_handle(device->handle, "_IRC", &handle); | 
 | 188 | 	if (ACPI_SUCCESS(status)) | 
 | 189 | 		device->power.flags.inrush_current = 1; | 
 | 190 |  | 
 | 191 | 	/* | 
 | 192 | 	 * Enumerate supported power management states | 
 | 193 | 	 */ | 
 | 194 | 	for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) { | 
 | 195 | 		struct acpi_device_power_state *ps = &device->power.states[i]; | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 196 | 		char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 |  | 
 | 198 | 		/* Evaluate "_PRx" to se if power resources are referenced */ | 
 | 199 | 		acpi_evaluate_reference(device->handle, object_name, NULL, | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 200 | 					&ps->resources); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | 		if (ps->resources.count) { | 
 | 202 | 			device->power.flags.power_resources = 1; | 
 | 203 | 			ps->flags.valid = 1; | 
 | 204 | 		} | 
 | 205 |  | 
 | 206 | 		/* Evaluate "_PSx" to see if we can do explicit sets */ | 
 | 207 | 		object_name[2] = 'S'; | 
 | 208 | 		status = acpi_get_handle(device->handle, object_name, &handle); | 
 | 209 | 		if (ACPI_SUCCESS(status)) { | 
 | 210 | 			ps->flags.explicit_set = 1; | 
 | 211 | 			ps->flags.valid = 1; | 
 | 212 | 		} | 
 | 213 |  | 
 | 214 | 		/* State is valid if we have some power control */ | 
 | 215 | 		if (ps->resources.count || ps->flags.explicit_set) | 
 | 216 | 			ps->flags.valid = 1; | 
 | 217 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 218 | 		ps->power = -1;	/* Unknown - driver assigned */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | 		ps->latency = -1;	/* Unknown - driver assigned */ | 
 | 220 | 	} | 
 | 221 |  | 
 | 222 | 	/* Set defaults for D0 and D3 states (always valid) */ | 
 | 223 | 	device->power.states[ACPI_STATE_D0].flags.valid = 1; | 
 | 224 | 	device->power.states[ACPI_STATE_D0].power = 100; | 
 | 225 | 	device->power.states[ACPI_STATE_D3].flags.valid = 1; | 
 | 226 | 	device->power.states[ACPI_STATE_D3].power = 0; | 
 | 227 |  | 
 | 228 | 	/* TBD: System wake support and resource requirements. */ | 
 | 229 |  | 
 | 230 | 	device->power.state = ACPI_STATE_UNKNOWN; | 
 | 231 |  | 
 | 232 | 	return_VALUE(0); | 
 | 233 | } | 
 | 234 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 235 | int acpi_match_ids(struct acpi_device *device, char *ids) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { | 
 | 237 | 	int error = 0; | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 238 | 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 |  | 
 | 240 | 	if (device->flags.hardware_id) | 
 | 241 | 		if (strstr(ids, device->pnp.hardware_id)) | 
 | 242 | 			goto Done; | 
 | 243 |  | 
 | 244 | 	if (device->flags.compatible_ids) { | 
 | 245 | 		struct acpi_compatible_id_list *cid_list = device->pnp.cid_list; | 
 | 246 | 		int i; | 
 | 247 |  | 
 | 248 | 		/* compare multiple _CID entries against driver ids */ | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 249 | 		for (i = 0; i < cid_list->count; i++) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | 			if (strstr(ids, cid_list->id[i].value)) | 
 | 251 | 				goto Done; | 
 | 252 | 		} | 
 | 253 | 	} | 
 | 254 | 	error = -ENOENT; | 
 | 255 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 256 |       Done: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | 	if (buffer.pointer) | 
 | 258 | 		acpi_os_free(buffer.pointer); | 
 | 259 | 	return error; | 
 | 260 | } | 
 | 261 |  | 
 | 262 | static acpi_status | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 263 | acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device, | 
 | 264 | 					     union acpi_object *package) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 266 | 	int i = 0; | 
 | 267 | 	union acpi_object *element = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 |  | 
 | 269 | 	if (!device || !package || (package->package.count < 2)) | 
 | 270 | 		return AE_BAD_PARAMETER; | 
 | 271 |  | 
 | 272 | 	element = &(package->package.elements[0]); | 
 | 273 | 	if (!element) | 
 | 274 | 		return AE_BAD_PARAMETER; | 
 | 275 | 	if (element->type == ACPI_TYPE_PACKAGE) { | 
 | 276 | 		if ((element->package.count < 2) || | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 277 | 		    (element->package.elements[0].type != | 
 | 278 | 		     ACPI_TYPE_LOCAL_REFERENCE) | 
 | 279 | 		    || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | 			return AE_BAD_DATA; | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 281 | 		device->wakeup.gpe_device = | 
 | 282 | 		    element->package.elements[0].reference.handle; | 
 | 283 | 		device->wakeup.gpe_number = | 
 | 284 | 		    (u32) element->package.elements[1].integer.value; | 
 | 285 | 	} else if (element->type == ACPI_TYPE_INTEGER) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | 		device->wakeup.gpe_number = element->integer.value; | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 287 | 	} else | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | 		return AE_BAD_DATA; | 
 | 289 |  | 
 | 290 | 	element = &(package->package.elements[1]); | 
 | 291 | 	if (element->type != ACPI_TYPE_INTEGER) { | 
 | 292 | 		return AE_BAD_DATA; | 
 | 293 | 	} | 
 | 294 | 	device->wakeup.sleep_state = element->integer.value; | 
 | 295 |  | 
 | 296 | 	if ((package->package.count - 2) > ACPI_MAX_HANDLES) { | 
 | 297 | 		return AE_NO_MEMORY; | 
 | 298 | 	} | 
 | 299 | 	device->wakeup.resources.count = package->package.count - 2; | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 300 | 	for (i = 0; i < device->wakeup.resources.count; i++) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | 		element = &(package->package.elements[i + 2]); | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 302 | 		if (element->type != ACPI_TYPE_ANY) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | 			return AE_BAD_DATA; | 
 | 304 | 		} | 
 | 305 |  | 
 | 306 | 		device->wakeup.resources.handles[i] = element->reference.handle; | 
 | 307 | 	} | 
 | 308 |  | 
 | 309 | 	return AE_OK; | 
 | 310 | } | 
 | 311 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 312 | static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 314 | 	acpi_status status = 0; | 
 | 315 | 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | 
 | 316 | 	union acpi_object *package = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 |  | 
 | 318 | 	ACPI_FUNCTION_TRACE("acpi_bus_get_wakeup_flags"); | 
 | 319 |  | 
 | 320 | 	/* _PRW */ | 
 | 321 | 	status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer); | 
 | 322 | 	if (ACPI_FAILURE(status)) { | 
 | 323 | 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PRW\n")); | 
 | 324 | 		goto end; | 
 | 325 | 	} | 
 | 326 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 327 | 	package = (union acpi_object *)buffer.pointer; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | 	status = acpi_bus_extract_wakeup_device_power_package(device, package); | 
 | 329 | 	if (ACPI_FAILURE(status)) { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 330 | 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, | 
 | 331 | 				  "Error extracting _PRW package\n")); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | 		goto end; | 
 | 333 | 	} | 
 | 334 |  | 
 | 335 | 	acpi_os_free(buffer.pointer); | 
 | 336 |  | 
 | 337 | 	device->wakeup.flags.valid = 1; | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 338 | 	/* Power button, Lid switch always enable wakeup */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | 	if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E")) | 
 | 340 | 		device->wakeup.flags.run_wake = 1; | 
 | 341 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 342 |       end: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | 	if (ACPI_FAILURE(status)) | 
 | 344 | 		device->flags.wake_capable = 0; | 
 | 345 | 	return_VALUE(0); | 
 | 346 | } | 
 | 347 |  | 
 | 348 | /* -------------------------------------------------------------------------- | 
| Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 349 | 		ACPI sysfs device file support | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 |    -------------------------------------------------------------------------- */ | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 351 | static ssize_t acpi_eject_store(struct acpi_device *device, | 
 | 352 | 				const char *buf, size_t count); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 |  | 
 | 354 | #define ACPI_DEVICE_ATTR(_name,_mode,_show,_store) \ | 
 | 355 | static struct acpi_device_attribute acpi_device_attr_##_name = \ | 
 | 356 | 		__ATTR(_name, _mode, _show, _store) | 
 | 357 |  | 
 | 358 | ACPI_DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store); | 
 | 359 |  | 
 | 360 | /** | 
 | 361 |  * setup_sys_fs_device_files - sets up the device files under device namespace | 
| Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 362 |  * @dev:	acpi_device object | 
 | 363 |  * @func:	function pointer to create or destroy the device file | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 |  */ | 
 | 365 | static void | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 366 | setup_sys_fs_device_files(struct acpi_device *dev, | 
 | 367 | 			  acpi_device_sysfs_files * func) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 369 | 	acpi_status status; | 
 | 370 | 	acpi_handle temp = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 |  | 
 | 372 | 	/* | 
 | 373 | 	 * If device has _EJ0, 'eject' file is created that is used to trigger | 
 | 374 | 	 * hot-removal function from userland. | 
 | 375 | 	 */ | 
 | 376 | 	status = acpi_get_handle(dev->handle, "_EJ0", &temp); | 
 | 377 | 	if (ACPI_SUCCESS(status)) | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 378 | 		(*(func)) (&dev->kobj, &acpi_device_attr_eject.attr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | } | 
 | 380 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 381 | static int acpi_eject_operation(acpi_handle handle, int lockable) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | { | 
 | 383 | 	struct acpi_object_list arg_list; | 
 | 384 | 	union acpi_object arg; | 
 | 385 | 	acpi_status status = AE_OK; | 
 | 386 |  | 
 | 387 | 	/* | 
 | 388 | 	 * TBD: evaluate _PS3? | 
 | 389 | 	 */ | 
 | 390 |  | 
 | 391 | 	if (lockable) { | 
 | 392 | 		arg_list.count = 1; | 
 | 393 | 		arg_list.pointer = &arg; | 
 | 394 | 		arg.type = ACPI_TYPE_INTEGER; | 
 | 395 | 		arg.integer.value = 0; | 
 | 396 | 		acpi_evaluate_object(handle, "_LCK", &arg_list, NULL); | 
 | 397 | 	} | 
 | 398 |  | 
 | 399 | 	arg_list.count = 1; | 
 | 400 | 	arg_list.pointer = &arg; | 
 | 401 | 	arg.type = ACPI_TYPE_INTEGER; | 
 | 402 | 	arg.integer.value = 1; | 
 | 403 |  | 
 | 404 | 	/* | 
 | 405 | 	 * TBD: _EJD support. | 
 | 406 | 	 */ | 
 | 407 |  | 
 | 408 | 	status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL); | 
 | 409 | 	if (ACPI_FAILURE(status)) { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 410 | 		return (-ENODEV); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | 	} | 
 | 412 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 413 | 	return (0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } | 
 | 415 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | static ssize_t | 
 | 417 | acpi_eject_store(struct acpi_device *device, const char *buf, size_t count) | 
 | 418 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 419 | 	int result; | 
 | 420 | 	int ret = count; | 
 | 421 | 	int islockable; | 
 | 422 | 	acpi_status status; | 
 | 423 | 	acpi_handle handle; | 
 | 424 | 	acpi_object_type type = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 |  | 
 | 426 | 	if ((!count) || (buf[0] != '1')) { | 
 | 427 | 		return -EINVAL; | 
 | 428 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | #ifndef FORCE_EJECT | 
 | 430 | 	if (device->driver == NULL) { | 
 | 431 | 		ret = -ENODEV; | 
 | 432 | 		goto err; | 
 | 433 | 	} | 
 | 434 | #endif | 
 | 435 | 	status = acpi_get_type(device->handle, &type); | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 436 | 	if (ACPI_FAILURE(status) || (!device->flags.ejectable)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | 		ret = -ENODEV; | 
 | 438 | 		goto err; | 
 | 439 | 	} | 
 | 440 |  | 
 | 441 | 	islockable = device->flags.lockable; | 
 | 442 | 	handle = device->handle; | 
 | 443 |  | 
 | 444 | 	if (type == ACPI_TYPE_PROCESSOR) | 
 | 445 | 		result = acpi_bus_trim(device, 0); | 
 | 446 | 	else | 
 | 447 | 		result = acpi_bus_trim(device, 1); | 
 | 448 |  | 
 | 449 | 	if (!result) | 
 | 450 | 		result = acpi_eject_operation(handle, islockable); | 
 | 451 |  | 
 | 452 | 	if (result) { | 
 | 453 | 		ret = -EBUSY; | 
 | 454 | 	} | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 455 |       err: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | 	return ret; | 
 | 457 | } | 
 | 458 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | /* -------------------------------------------------------------------------- | 
 | 460 |                               Performance Management | 
 | 461 |    -------------------------------------------------------------------------- */ | 
 | 462 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 463 | static int acpi_bus_get_perf_flags(struct acpi_device *device) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | { | 
 | 465 | 	device->performance.state = ACPI_STATE_UNKNOWN; | 
 | 466 | 	return 0; | 
 | 467 | } | 
 | 468 |  | 
 | 469 | /* -------------------------------------------------------------------------- | 
 | 470 |                                  Driver Management | 
 | 471 |    -------------------------------------------------------------------------- */ | 
 | 472 |  | 
 | 473 | static LIST_HEAD(acpi_bus_drivers); | 
 | 474 | static DECLARE_MUTEX(acpi_bus_drivers_lock); | 
 | 475 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | /** | 
| Randy Dunlap | d758a8f | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 477 |  * acpi_bus_match - match device IDs to driver's supported IDs | 
 | 478 |  * @device: the device that we are trying to match to a driver | 
 | 479 |  * @driver: driver whose device id table is being checked | 
 | 480 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 |  * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it | 
 | 482 |  * matches the specified driver's criteria. | 
 | 483 |  */ | 
 | 484 | static int | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 485 | acpi_bus_match(struct acpi_device *device, struct acpi_driver *driver) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | { | 
 | 487 | 	if (driver && driver->ops.match) | 
 | 488 | 		return driver->ops.match(device, driver); | 
 | 489 | 	return acpi_match_ids(device, driver->ids); | 
 | 490 | } | 
 | 491 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | /** | 
| Randy Dunlap | d758a8f | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 493 |  * acpi_bus_driver_init - add a device to a driver | 
 | 494 |  * @device: the device to add and initialize | 
 | 495 |  * @driver: driver for the device | 
 | 496 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 |  * Used to initialize a device via its device driver.  Called whenever a  | 
 | 498 |  * driver is bound to a device.  Invokes the driver's add() and start() ops. | 
 | 499 |  */ | 
 | 500 | static int | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 501 | acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 503 | 	int result = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 |  | 
 | 505 | 	ACPI_FUNCTION_TRACE("acpi_bus_driver_init"); | 
 | 506 |  | 
 | 507 | 	if (!device || !driver) | 
 | 508 | 		return_VALUE(-EINVAL); | 
 | 509 |  | 
 | 510 | 	if (!driver->ops.add) | 
 | 511 | 		return_VALUE(-ENOSYS); | 
 | 512 |  | 
 | 513 | 	result = driver->ops.add(device); | 
 | 514 | 	if (result) { | 
 | 515 | 		device->driver = NULL; | 
 | 516 | 		acpi_driver_data(device) = NULL; | 
 | 517 | 		return_VALUE(result); | 
 | 518 | 	} | 
 | 519 |  | 
 | 520 | 	device->driver = driver; | 
 | 521 |  | 
 | 522 | 	/* | 
 | 523 | 	 * TBD - Configuration Management: Assign resources to device based | 
 | 524 | 	 * upon possible configuration and currently allocated resources. | 
 | 525 | 	 */ | 
 | 526 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 527 | 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, | 
 | 528 | 			  "Driver successfully bound to device\n")); | 
| Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 529 | 	return_VALUE(0); | 
 | 530 | } | 
 | 531 |  | 
| Adrian Bunk | 8713cbe | 2005-09-02 17:16:48 -0400 | [diff] [blame] | 532 | static int acpi_start_single_object(struct acpi_device *device) | 
| Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 533 | { | 
 | 534 | 	int result = 0; | 
 | 535 | 	struct acpi_driver *driver; | 
 | 536 |  | 
 | 537 | 	ACPI_FUNCTION_TRACE("acpi_start_single_object"); | 
 | 538 |  | 
 | 539 | 	if (!(driver = device->driver)) | 
 | 540 | 		return_VALUE(0); | 
 | 541 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | 	if (driver->ops.start) { | 
 | 543 | 		result = driver->ops.start(device); | 
 | 544 | 		if (result && driver->ops.remove) | 
 | 545 | 			driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | 	} | 
 | 547 |  | 
| Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 548 | 	return_VALUE(result); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | } | 
 | 550 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 551 | static int acpi_driver_attach(struct acpi_driver *drv) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 553 | 	struct list_head *node, *next; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | 	int count = 0; | 
 | 555 |  | 
 | 556 | 	ACPI_FUNCTION_TRACE("acpi_driver_attach"); | 
 | 557 |  | 
 | 558 | 	spin_lock(&acpi_device_lock); | 
 | 559 | 	list_for_each_safe(node, next, &acpi_device_list) { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 560 | 		struct acpi_device *dev = | 
 | 561 | 		    container_of(node, struct acpi_device, g_list); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 |  | 
 | 563 | 		if (dev->driver || !dev->status.present) | 
 | 564 | 			continue; | 
 | 565 | 		spin_unlock(&acpi_device_lock); | 
 | 566 |  | 
 | 567 | 		if (!acpi_bus_match(dev, drv)) { | 
 | 568 | 			if (!acpi_bus_driver_init(dev, drv)) { | 
| Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 569 | 				acpi_start_single_object(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | 				atomic_inc(&drv->references); | 
 | 571 | 				count++; | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 572 | 				ACPI_DEBUG_PRINT((ACPI_DB_INFO, | 
 | 573 | 						  "Found driver [%s] for device [%s]\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | 						  drv->name, dev->pnp.bus_id)); | 
 | 575 | 			} | 
 | 576 | 		} | 
 | 577 | 		spin_lock(&acpi_device_lock); | 
 | 578 | 	} | 
 | 579 | 	spin_unlock(&acpi_device_lock); | 
 | 580 | 	return_VALUE(count); | 
 | 581 | } | 
 | 582 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 583 | static int acpi_driver_detach(struct acpi_driver *drv) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 585 | 	struct list_head *node, *next; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 |  | 
 | 587 | 	ACPI_FUNCTION_TRACE("acpi_driver_detach"); | 
 | 588 |  | 
 | 589 | 	spin_lock(&acpi_device_lock); | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 590 | 	list_for_each_safe(node, next, &acpi_device_list) { | 
 | 591 | 		struct acpi_device *dev = | 
 | 592 | 		    container_of(node, struct acpi_device, g_list); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 |  | 
 | 594 | 		if (dev->driver == drv) { | 
 | 595 | 			spin_unlock(&acpi_device_lock); | 
 | 596 | 			if (drv->ops.remove) | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 597 | 				drv->ops.remove(dev, ACPI_BUS_REMOVAL_NORMAL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | 			spin_lock(&acpi_device_lock); | 
 | 599 | 			dev->driver = NULL; | 
 | 600 | 			dev->driver_data = NULL; | 
 | 601 | 			atomic_dec(&drv->references); | 
 | 602 | 		} | 
 | 603 | 	} | 
 | 604 | 	spin_unlock(&acpi_device_lock); | 
 | 605 | 	return_VALUE(0); | 
 | 606 | } | 
 | 607 |  | 
 | 608 | /** | 
| Randy Dunlap | d758a8f | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 609 |  * acpi_bus_register_driver - register a driver with the ACPI bus | 
 | 610 |  * @driver: driver being registered | 
 | 611 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 |  * Registers a driver with the ACPI bus.  Searches the namespace for all | 
 | 613 |  * devices that match the driver's criteria and binds.  Returns the | 
 | 614 |  * number of devices that were claimed by the driver, or a negative | 
 | 615 |  * error status for failure. | 
 | 616 |  */ | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 617 | int acpi_bus_register_driver(struct acpi_driver *driver) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | { | 
 | 619 | 	int count; | 
 | 620 |  | 
 | 621 | 	ACPI_FUNCTION_TRACE("acpi_bus_register_driver"); | 
 | 622 |  | 
 | 623 | 	if (acpi_disabled) | 
 | 624 | 		return_VALUE(-ENODEV); | 
 | 625 |  | 
 | 626 | 	if (!driver) | 
 | 627 | 		return_VALUE(-EINVAL); | 
 | 628 |  | 
 | 629 | 	spin_lock(&acpi_device_lock); | 
 | 630 | 	list_add_tail(&driver->node, &acpi_bus_drivers); | 
 | 631 | 	spin_unlock(&acpi_device_lock); | 
 | 632 | 	count = acpi_driver_attach(driver); | 
 | 633 |  | 
 | 634 | 	return_VALUE(count); | 
 | 635 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 637 | EXPORT_SYMBOL(acpi_bus_register_driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 |  | 
 | 639 | /** | 
| Randy Dunlap | d758a8f | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 640 |  * acpi_bus_unregister_driver - unregisters a driver with the APIC bus | 
 | 641 |  * @driver: driver to unregister | 
 | 642 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 |  * Unregisters a driver with the ACPI bus.  Searches the namespace for all | 
 | 644 |  * devices that match the driver's criteria and unbinds. | 
 | 645 |  */ | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 646 | int acpi_bus_unregister_driver(struct acpi_driver *driver) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | { | 
 | 648 | 	int error = 0; | 
 | 649 |  | 
 | 650 | 	ACPI_FUNCTION_TRACE("acpi_bus_unregister_driver"); | 
 | 651 |  | 
 | 652 | 	if (driver) { | 
 | 653 | 		acpi_driver_detach(driver); | 
 | 654 |  | 
 | 655 | 		if (!atomic_read(&driver->references)) { | 
 | 656 | 			spin_lock(&acpi_device_lock); | 
 | 657 | 			list_del_init(&driver->node); | 
 | 658 | 			spin_unlock(&acpi_device_lock); | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 659 | 		} | 
 | 660 | 	} else | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | 		error = -EINVAL; | 
 | 662 | 	return_VALUE(error); | 
 | 663 | } | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 664 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | EXPORT_SYMBOL(acpi_bus_unregister_driver); | 
 | 666 |  | 
 | 667 | /** | 
| Randy Dunlap | d758a8f | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 668 |  * acpi_bus_find_driver - check if there is a driver installed for the device | 
 | 669 |  * @device: device that we are trying to find a supporting driver for | 
 | 670 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 |  * Parses the list of registered drivers looking for a driver applicable for | 
 | 672 |  * the specified device. | 
 | 673 |  */ | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 674 | static int acpi_bus_find_driver(struct acpi_device *device) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 676 | 	int result = 0; | 
 | 677 | 	struct list_head *node, *next; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 |  | 
 | 679 | 	ACPI_FUNCTION_TRACE("acpi_bus_find_driver"); | 
 | 680 |  | 
 | 681 | 	spin_lock(&acpi_device_lock); | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 682 | 	list_for_each_safe(node, next, &acpi_bus_drivers) { | 
 | 683 | 		struct acpi_driver *driver = | 
 | 684 | 		    container_of(node, struct acpi_driver, node); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 |  | 
 | 686 | 		atomic_inc(&driver->references); | 
 | 687 | 		spin_unlock(&acpi_device_lock); | 
 | 688 | 		if (!acpi_bus_match(device, driver)) { | 
 | 689 | 			result = acpi_bus_driver_init(device, driver); | 
 | 690 | 			if (!result) | 
 | 691 | 				goto Done; | 
 | 692 | 		} | 
 | 693 | 		atomic_dec(&driver->references); | 
 | 694 | 		spin_lock(&acpi_device_lock); | 
 | 695 | 	} | 
 | 696 | 	spin_unlock(&acpi_device_lock); | 
 | 697 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 698 |       Done: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | 	return_VALUE(result); | 
 | 700 | } | 
 | 701 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | /* -------------------------------------------------------------------------- | 
 | 703 |                                  Device Enumeration | 
 | 704 |    -------------------------------------------------------------------------- */ | 
 | 705 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 706 | static int acpi_bus_get_flags(struct acpi_device *device) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 708 | 	acpi_status status = AE_OK; | 
 | 709 | 	acpi_handle temp = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 |  | 
 | 711 | 	ACPI_FUNCTION_TRACE("acpi_bus_get_flags"); | 
 | 712 |  | 
 | 713 | 	/* Presence of _STA indicates 'dynamic_status' */ | 
 | 714 | 	status = acpi_get_handle(device->handle, "_STA", &temp); | 
 | 715 | 	if (ACPI_SUCCESS(status)) | 
 | 716 | 		device->flags.dynamic_status = 1; | 
 | 717 |  | 
 | 718 | 	/* Presence of _CID indicates 'compatible_ids' */ | 
 | 719 | 	status = acpi_get_handle(device->handle, "_CID", &temp); | 
 | 720 | 	if (ACPI_SUCCESS(status)) | 
 | 721 | 		device->flags.compatible_ids = 1; | 
 | 722 |  | 
 | 723 | 	/* Presence of _RMV indicates 'removable' */ | 
 | 724 | 	status = acpi_get_handle(device->handle, "_RMV", &temp); | 
 | 725 | 	if (ACPI_SUCCESS(status)) | 
 | 726 | 		device->flags.removable = 1; | 
 | 727 |  | 
 | 728 | 	/* Presence of _EJD|_EJ0 indicates 'ejectable' */ | 
 | 729 | 	status = acpi_get_handle(device->handle, "_EJD", &temp); | 
 | 730 | 	if (ACPI_SUCCESS(status)) | 
 | 731 | 		device->flags.ejectable = 1; | 
 | 732 | 	else { | 
 | 733 | 		status = acpi_get_handle(device->handle, "_EJ0", &temp); | 
 | 734 | 		if (ACPI_SUCCESS(status)) | 
 | 735 | 			device->flags.ejectable = 1; | 
 | 736 | 	} | 
 | 737 |  | 
 | 738 | 	/* Presence of _LCK indicates 'lockable' */ | 
 | 739 | 	status = acpi_get_handle(device->handle, "_LCK", &temp); | 
 | 740 | 	if (ACPI_SUCCESS(status)) | 
 | 741 | 		device->flags.lockable = 1; | 
 | 742 |  | 
 | 743 | 	/* Presence of _PS0|_PR0 indicates 'power manageable' */ | 
 | 744 | 	status = acpi_get_handle(device->handle, "_PS0", &temp); | 
 | 745 | 	if (ACPI_FAILURE(status)) | 
 | 746 | 		status = acpi_get_handle(device->handle, "_PR0", &temp); | 
 | 747 | 	if (ACPI_SUCCESS(status)) | 
 | 748 | 		device->flags.power_manageable = 1; | 
 | 749 |  | 
 | 750 | 	/* Presence of _PRW indicates wake capable */ | 
 | 751 | 	status = acpi_get_handle(device->handle, "_PRW", &temp); | 
 | 752 | 	if (ACPI_SUCCESS(status)) | 
 | 753 | 		device->flags.wake_capable = 1; | 
 | 754 |  | 
 | 755 | 	/* TBD: Peformance management */ | 
 | 756 |  | 
 | 757 | 	return_VALUE(0); | 
 | 758 | } | 
 | 759 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 760 | static void acpi_device_get_busid(struct acpi_device *device, | 
 | 761 | 				  acpi_handle handle, int type) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 763 | 	char bus_id[5] = { '?', 0 }; | 
 | 764 | 	struct acpi_buffer buffer = { sizeof(bus_id), bus_id }; | 
 | 765 | 	int i = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 |  | 
 | 767 | 	/* | 
 | 768 | 	 * Bus ID | 
 | 769 | 	 * ------ | 
 | 770 | 	 * The device's Bus ID is simply the object name. | 
 | 771 | 	 * TBD: Shouldn't this value be unique (within the ACPI namespace)? | 
 | 772 | 	 */ | 
 | 773 | 	switch (type) { | 
 | 774 | 	case ACPI_BUS_TYPE_SYSTEM: | 
 | 775 | 		strcpy(device->pnp.bus_id, "ACPI"); | 
 | 776 | 		break; | 
 | 777 | 	case ACPI_BUS_TYPE_POWER_BUTTON: | 
 | 778 | 		strcpy(device->pnp.bus_id, "PWRF"); | 
 | 779 | 		break; | 
 | 780 | 	case ACPI_BUS_TYPE_SLEEP_BUTTON: | 
 | 781 | 		strcpy(device->pnp.bus_id, "SLPF"); | 
 | 782 | 		break; | 
 | 783 | 	default: | 
 | 784 | 		acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer); | 
 | 785 | 		/* Clean up trailing underscores (if any) */ | 
 | 786 | 		for (i = 3; i > 1; i--) { | 
 | 787 | 			if (bus_id[i] == '_') | 
 | 788 | 				bus_id[i] = '\0'; | 
 | 789 | 			else | 
 | 790 | 				break; | 
 | 791 | 		} | 
 | 792 | 		strcpy(device->pnp.bus_id, bus_id); | 
 | 793 | 		break; | 
 | 794 | 	} | 
 | 795 | } | 
 | 796 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 797 | static void acpi_device_set_id(struct acpi_device *device, | 
 | 798 | 			       struct acpi_device *parent, acpi_handle handle, | 
 | 799 | 			       int type) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 801 | 	struct acpi_device_info *info; | 
 | 802 | 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | 
 | 803 | 	char *hid = NULL; | 
 | 804 | 	char *uid = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | 	struct acpi_compatible_id_list *cid_list = NULL; | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 806 | 	acpi_status status; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 |  | 
 | 808 | 	switch (type) { | 
 | 809 | 	case ACPI_BUS_TYPE_DEVICE: | 
 | 810 | 		status = acpi_get_object_info(handle, &buffer); | 
 | 811 | 		if (ACPI_FAILURE(status)) { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 812 | 			printk("%s: Error reading device info\n", __FUNCTION__); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | 			return; | 
 | 814 | 		} | 
 | 815 |  | 
 | 816 | 		info = buffer.pointer; | 
 | 817 | 		if (info->valid & ACPI_VALID_HID) | 
 | 818 | 			hid = info->hardware_id.value; | 
 | 819 | 		if (info->valid & ACPI_VALID_UID) | 
 | 820 | 			uid = info->unique_id.value; | 
 | 821 | 		if (info->valid & ACPI_VALID_CID) | 
 | 822 | 			cid_list = &info->compatibility_id; | 
 | 823 | 		if (info->valid & ACPI_VALID_ADR) { | 
 | 824 | 			device->pnp.bus_address = info->address; | 
 | 825 | 			device->flags.bus_address = 1; | 
 | 826 | 		} | 
 | 827 | 		break; | 
 | 828 | 	case ACPI_BUS_TYPE_POWER: | 
 | 829 | 		hid = ACPI_POWER_HID; | 
 | 830 | 		break; | 
 | 831 | 	case ACPI_BUS_TYPE_PROCESSOR: | 
 | 832 | 		hid = ACPI_PROCESSOR_HID; | 
 | 833 | 		break; | 
 | 834 | 	case ACPI_BUS_TYPE_SYSTEM: | 
 | 835 | 		hid = ACPI_SYSTEM_HID; | 
 | 836 | 		break; | 
 | 837 | 	case ACPI_BUS_TYPE_THERMAL: | 
 | 838 | 		hid = ACPI_THERMAL_HID; | 
 | 839 | 		break; | 
 | 840 | 	case ACPI_BUS_TYPE_POWER_BUTTON: | 
 | 841 | 		hid = ACPI_BUTTON_HID_POWERF; | 
 | 842 | 		break; | 
 | 843 | 	case ACPI_BUS_TYPE_SLEEP_BUTTON: | 
 | 844 | 		hid = ACPI_BUTTON_HID_SLEEPF; | 
 | 845 | 		break; | 
 | 846 | 	} | 
 | 847 |  | 
 | 848 | 	/*  | 
 | 849 | 	 * \_SB | 
 | 850 | 	 * ---- | 
 | 851 | 	 * Fix for the system root bus device -- the only root-level device. | 
 | 852 | 	 */ | 
| Bob Moore | c51a4de | 2005-11-17 13:07:00 -0500 | [diff] [blame] | 853 | 	if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | 		hid = ACPI_BUS_HID; | 
 | 855 | 		strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME); | 
 | 856 | 		strcpy(device->pnp.device_class, ACPI_BUS_CLASS); | 
 | 857 | 	} | 
 | 858 |  | 
 | 859 | 	if (hid) { | 
 | 860 | 		strcpy(device->pnp.hardware_id, hid); | 
 | 861 | 		device->flags.hardware_id = 1; | 
 | 862 | 	} | 
 | 863 | 	if (uid) { | 
 | 864 | 		strcpy(device->pnp.unique_id, uid); | 
 | 865 | 		device->flags.unique_id = 1; | 
 | 866 | 	} | 
 | 867 | 	if (cid_list) { | 
 | 868 | 		device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL); | 
 | 869 | 		if (device->pnp.cid_list) | 
 | 870 | 			memcpy(device->pnp.cid_list, cid_list, cid_list->size); | 
 | 871 | 		else | 
 | 872 | 			printk(KERN_ERR "Memory allocation error\n"); | 
 | 873 | 	} | 
 | 874 |  | 
 | 875 | 	acpi_os_free(buffer.pointer); | 
 | 876 | } | 
 | 877 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 878 | static int acpi_device_set_context(struct acpi_device *device, int type) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | { | 
 | 880 | 	acpi_status status = AE_OK; | 
 | 881 | 	int result = 0; | 
 | 882 | 	/* | 
 | 883 | 	 * Context | 
 | 884 | 	 * ------- | 
 | 885 | 	 * Attach this 'struct acpi_device' to the ACPI object.  This makes | 
 | 886 | 	 * resolutions from handle->device very efficient.  Note that we need | 
 | 887 | 	 * to be careful with fixed-feature devices as they all attach to the | 
 | 888 | 	 * root object. | 
 | 889 | 	 */ | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 890 | 	if (type != ACPI_BUS_TYPE_POWER_BUTTON && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | 	    type != ACPI_BUS_TYPE_SLEEP_BUTTON) { | 
 | 892 | 		status = acpi_attach_data(device->handle, | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 893 | 					  acpi_bus_data_handler, device); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 |  | 
 | 895 | 		if (ACPI_FAILURE(status)) { | 
 | 896 | 			printk("Error attaching device data\n"); | 
 | 897 | 			result = -ENODEV; | 
 | 898 | 		} | 
 | 899 | 	} | 
 | 900 | 	return result; | 
 | 901 | } | 
 | 902 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 903 | static void acpi_device_get_debug_info(struct acpi_device *device, | 
 | 904 | 				       acpi_handle handle, int type) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | { | 
 | 906 | #ifdef CONFIG_ACPI_DEBUG_OUTPUT | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 907 | 	char *type_string = NULL; | 
 | 908 | 	char name[80] = { '?', '\0' }; | 
 | 909 | 	struct acpi_buffer buffer = { sizeof(name), name }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 |  | 
 | 911 | 	switch (type) { | 
 | 912 | 	case ACPI_BUS_TYPE_DEVICE: | 
 | 913 | 		type_string = "Device"; | 
 | 914 | 		acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); | 
 | 915 | 		break; | 
 | 916 | 	case ACPI_BUS_TYPE_POWER: | 
 | 917 | 		type_string = "Power Resource"; | 
 | 918 | 		acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); | 
 | 919 | 		break; | 
 | 920 | 	case ACPI_BUS_TYPE_PROCESSOR: | 
 | 921 | 		type_string = "Processor"; | 
 | 922 | 		acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); | 
 | 923 | 		break; | 
 | 924 | 	case ACPI_BUS_TYPE_SYSTEM: | 
 | 925 | 		type_string = "System"; | 
 | 926 | 		acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); | 
 | 927 | 		break; | 
 | 928 | 	case ACPI_BUS_TYPE_THERMAL: | 
 | 929 | 		type_string = "Thermal Zone"; | 
 | 930 | 		acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); | 
 | 931 | 		break; | 
 | 932 | 	case ACPI_BUS_TYPE_POWER_BUTTON: | 
 | 933 | 		type_string = "Power Button"; | 
 | 934 | 		sprintf(name, "PWRB"); | 
 | 935 | 		break; | 
 | 936 | 	case ACPI_BUS_TYPE_SLEEP_BUTTON: | 
 | 937 | 		type_string = "Sleep Button"; | 
 | 938 | 		sprintf(name, "SLPB"); | 
 | 939 | 		break; | 
 | 940 | 	} | 
 | 941 |  | 
 | 942 | 	printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle); | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 943 | #endif				/*CONFIG_ACPI_DEBUG_OUTPUT */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | } | 
 | 945 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 946 | static int acpi_bus_remove(struct acpi_device *dev, int rmdevice) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 948 | 	int result = 0; | 
 | 949 | 	struct acpi_driver *driver; | 
 | 950 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | 	ACPI_FUNCTION_TRACE("acpi_bus_remove"); | 
 | 952 |  | 
 | 953 | 	if (!dev) | 
 | 954 | 		return_VALUE(-EINVAL); | 
 | 955 |  | 
 | 956 | 	driver = dev->driver; | 
 | 957 |  | 
 | 958 | 	if ((driver) && (driver->ops.remove)) { | 
 | 959 |  | 
 | 960 | 		if (driver->ops.stop) { | 
 | 961 | 			result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT); | 
 | 962 | 			if (result) | 
 | 963 | 				return_VALUE(result); | 
 | 964 | 		} | 
 | 965 |  | 
 | 966 | 		result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT); | 
 | 967 | 		if (result) { | 
 | 968 | 			return_VALUE(result); | 
 | 969 | 		} | 
 | 970 |  | 
 | 971 | 		atomic_dec(&dev->driver->references); | 
 | 972 | 		dev->driver = NULL; | 
 | 973 | 		acpi_driver_data(dev) = NULL; | 
 | 974 | 	} | 
 | 975 |  | 
 | 976 | 	if (!rmdevice) | 
 | 977 | 		return_VALUE(0); | 
 | 978 |  | 
 | 979 | 	if (dev->flags.bus_address) { | 
 | 980 | 		if ((dev->parent) && (dev->parent->ops.unbind)) | 
 | 981 | 			dev->parent->ops.unbind(dev); | 
 | 982 | 	} | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 983 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | 	acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT); | 
 | 985 |  | 
 | 986 | 	return_VALUE(0); | 
 | 987 | } | 
 | 988 |  | 
| Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 989 | static int | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 990 | acpi_add_single_object(struct acpi_device **child, | 
 | 991 | 		       struct acpi_device *parent, acpi_handle handle, int type) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 993 | 	int result = 0; | 
 | 994 | 	struct acpi_device *device = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 |  | 
| Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 996 | 	ACPI_FUNCTION_TRACE("acpi_add_single_object"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 |  | 
 | 998 | 	if (!child) | 
 | 999 | 		return_VALUE(-EINVAL); | 
 | 1000 |  | 
 | 1001 | 	device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL); | 
 | 1002 | 	if (!device) { | 
 | 1003 | 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Memory allocation error\n")); | 
 | 1004 | 		return_VALUE(-ENOMEM); | 
 | 1005 | 	} | 
 | 1006 | 	memset(device, 0, sizeof(struct acpi_device)); | 
 | 1007 |  | 
 | 1008 | 	device->handle = handle; | 
 | 1009 | 	device->parent = parent; | 
 | 1010 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1011 | 	acpi_device_get_busid(device, handle, type); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 |  | 
 | 1013 | 	/* | 
 | 1014 | 	 * Flags | 
 | 1015 | 	 * ----- | 
 | 1016 | 	 * Get prior to calling acpi_bus_get_status() so we know whether | 
 | 1017 | 	 * or not _STA is present.  Note that we only look for object | 
 | 1018 | 	 * handles -- cannot evaluate objects until we know the device is | 
 | 1019 | 	 * present and properly initialized. | 
 | 1020 | 	 */ | 
 | 1021 | 	result = acpi_bus_get_flags(device); | 
 | 1022 | 	if (result) | 
 | 1023 | 		goto end; | 
 | 1024 |  | 
 | 1025 | 	/* | 
 | 1026 | 	 * Status | 
 | 1027 | 	 * ------ | 
| Keiichiro Tokunaga | 6940fab | 2005-03-30 23:15:47 -0500 | [diff] [blame] | 1028 | 	 * See if the device is present.  We always assume that non-Device | 
 | 1029 | 	 * and non-Processor objects (e.g. thermal zones, power resources, | 
 | 1030 | 	 * etc.) are present, functioning, etc. (at least when parent object | 
 | 1031 | 	 * is present).  Note that _STA has a different meaning for some | 
 | 1032 | 	 * objects (e.g. power resources) so we need to be careful how we use | 
 | 1033 | 	 * it. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | 	 */ | 
 | 1035 | 	switch (type) { | 
| Keiichiro Tokunaga | 6940fab | 2005-03-30 23:15:47 -0500 | [diff] [blame] | 1036 | 	case ACPI_BUS_TYPE_PROCESSOR: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | 	case ACPI_BUS_TYPE_DEVICE: | 
 | 1038 | 		result = acpi_bus_get_status(device); | 
 | 1039 | 		if (ACPI_FAILURE(result) || !device->status.present) { | 
 | 1040 | 			result = -ENOENT; | 
 | 1041 | 			goto end; | 
 | 1042 | 		} | 
 | 1043 | 		break; | 
 | 1044 | 	default: | 
 | 1045 | 		STRUCT_TO_INT(device->status) = 0x0F; | 
 | 1046 | 		break; | 
 | 1047 | 	} | 
 | 1048 |  | 
 | 1049 | 	/* | 
 | 1050 | 	 * Initialize Device | 
 | 1051 | 	 * ----------------- | 
 | 1052 | 	 * TBD: Synch with Core's enumeration/initialization process. | 
 | 1053 | 	 */ | 
 | 1054 |  | 
 | 1055 | 	/* | 
 | 1056 | 	 * Hardware ID, Unique ID, & Bus Address | 
 | 1057 | 	 * ------------------------------------- | 
 | 1058 | 	 */ | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1059 | 	acpi_device_set_id(device, parent, handle, type); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 |  | 
 | 1061 | 	/* | 
 | 1062 | 	 * Power Management | 
 | 1063 | 	 * ---------------- | 
 | 1064 | 	 */ | 
 | 1065 | 	if (device->flags.power_manageable) { | 
 | 1066 | 		result = acpi_bus_get_power_flags(device); | 
 | 1067 | 		if (result) | 
 | 1068 | 			goto end; | 
 | 1069 | 	} | 
 | 1070 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1071 | 	/* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | 	 * Wakeup device management | 
 | 1073 | 	 *----------------------- | 
 | 1074 | 	 */ | 
 | 1075 | 	if (device->flags.wake_capable) { | 
 | 1076 | 		result = acpi_bus_get_wakeup_device_flags(device); | 
 | 1077 | 		if (result) | 
 | 1078 | 			goto end; | 
 | 1079 | 	} | 
 | 1080 |  | 
 | 1081 | 	/* | 
 | 1082 | 	 * Performance Management | 
 | 1083 | 	 * ---------------------- | 
 | 1084 | 	 */ | 
 | 1085 | 	if (device->flags.performance_manageable) { | 
 | 1086 | 		result = acpi_bus_get_perf_flags(device); | 
 | 1087 | 		if (result) | 
 | 1088 | 			goto end; | 
 | 1089 | 	} | 
 | 1090 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1091 | 	if ((result = acpi_device_set_context(device, type))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | 		goto end; | 
 | 1093 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1094 | 	acpi_device_get_debug_info(device, handle, type); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1096 | 	acpi_device_register(device, parent); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 |  | 
 | 1098 | 	/* | 
 | 1099 | 	 * Bind _ADR-Based Devices | 
 | 1100 | 	 * ----------------------- | 
 | 1101 | 	 * If there's a a bus address (_ADR) then we utilize the parent's  | 
 | 1102 | 	 * 'bind' function (if exists) to bind the ACPI- and natively- | 
 | 1103 | 	 * enumerated device representations. | 
 | 1104 | 	 */ | 
 | 1105 | 	if (device->flags.bus_address) { | 
 | 1106 | 		if (device->parent && device->parent->ops.bind) | 
 | 1107 | 			device->parent->ops.bind(device); | 
 | 1108 | 	} | 
 | 1109 |  | 
 | 1110 | 	/* | 
 | 1111 | 	 * Locate & Attach Driver | 
 | 1112 | 	 * ---------------------- | 
 | 1113 | 	 * If there's a hardware id (_HID) or compatible ids (_CID) we check | 
 | 1114 | 	 * to see if there's a driver installed for this kind of device.  Note | 
 | 1115 | 	 * that drivers can install before or after a device is enumerated. | 
 | 1116 | 	 * | 
 | 1117 | 	 * TBD: Assumes LDM provides driver hot-plug capability. | 
 | 1118 | 	 */ | 
| Thomas Renninger | bd7ce5b | 2005-10-03 10:39:00 -0700 | [diff] [blame] | 1119 | 	acpi_bus_find_driver(device); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1121 |       end: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | 	if (!result) | 
 | 1123 | 		*child = device; | 
 | 1124 | 	else { | 
| Jesper Juhl | 6044ec8 | 2005-11-07 01:01:32 -0800 | [diff] [blame] | 1125 | 		kfree(device->pnp.cid_list); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | 		kfree(device); | 
 | 1127 | 	} | 
 | 1128 |  | 
 | 1129 | 	return_VALUE(result); | 
 | 1130 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1132 | static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1134 | 	acpi_status status = AE_OK; | 
 | 1135 | 	struct acpi_device *parent = NULL; | 
 | 1136 | 	struct acpi_device *child = NULL; | 
 | 1137 | 	acpi_handle phandle = NULL; | 
 | 1138 | 	acpi_handle chandle = NULL; | 
 | 1139 | 	acpi_object_type type = 0; | 
 | 1140 | 	u32 level = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 |  | 
 | 1142 | 	ACPI_FUNCTION_TRACE("acpi_bus_scan"); | 
 | 1143 |  | 
 | 1144 | 	if (!start) | 
 | 1145 | 		return_VALUE(-EINVAL); | 
 | 1146 |  | 
 | 1147 | 	parent = start; | 
 | 1148 | 	phandle = start->handle; | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1149 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | 	/* | 
 | 1151 | 	 * Parse through the ACPI namespace, identify all 'devices', and | 
 | 1152 | 	 * create a new 'struct acpi_device' for each. | 
 | 1153 | 	 */ | 
 | 1154 | 	while ((level > 0) && parent) { | 
 | 1155 |  | 
 | 1156 | 		status = acpi_get_next_object(ACPI_TYPE_ANY, phandle, | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1157 | 					      chandle, &chandle); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 |  | 
 | 1159 | 		/* | 
 | 1160 | 		 * If this scope is exhausted then move our way back up. | 
 | 1161 | 		 */ | 
 | 1162 | 		if (ACPI_FAILURE(status)) { | 
 | 1163 | 			level--; | 
 | 1164 | 			chandle = phandle; | 
 | 1165 | 			acpi_get_parent(phandle, &phandle); | 
 | 1166 | 			if (parent->parent) | 
 | 1167 | 				parent = parent->parent; | 
 | 1168 | 			continue; | 
 | 1169 | 		} | 
 | 1170 |  | 
 | 1171 | 		status = acpi_get_type(chandle, &type); | 
 | 1172 | 		if (ACPI_FAILURE(status)) | 
 | 1173 | 			continue; | 
 | 1174 |  | 
 | 1175 | 		/* | 
 | 1176 | 		 * If this is a scope object then parse it (depth-first). | 
 | 1177 | 		 */ | 
 | 1178 | 		if (type == ACPI_TYPE_LOCAL_SCOPE) { | 
 | 1179 | 			level++; | 
 | 1180 | 			phandle = chandle; | 
 | 1181 | 			chandle = NULL; | 
 | 1182 | 			continue; | 
 | 1183 | 		} | 
 | 1184 |  | 
 | 1185 | 		/* | 
 | 1186 | 		 * We're only interested in objects that we consider 'devices'. | 
 | 1187 | 		 */ | 
 | 1188 | 		switch (type) { | 
 | 1189 | 		case ACPI_TYPE_DEVICE: | 
 | 1190 | 			type = ACPI_BUS_TYPE_DEVICE; | 
 | 1191 | 			break; | 
 | 1192 | 		case ACPI_TYPE_PROCESSOR: | 
 | 1193 | 			type = ACPI_BUS_TYPE_PROCESSOR; | 
 | 1194 | 			break; | 
 | 1195 | 		case ACPI_TYPE_THERMAL: | 
 | 1196 | 			type = ACPI_BUS_TYPE_THERMAL; | 
 | 1197 | 			break; | 
 | 1198 | 		case ACPI_TYPE_POWER: | 
 | 1199 | 			type = ACPI_BUS_TYPE_POWER; | 
 | 1200 | 			break; | 
 | 1201 | 		default: | 
 | 1202 | 			continue; | 
 | 1203 | 		} | 
 | 1204 |  | 
| Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1205 | 		if (ops->acpi_op_add) | 
 | 1206 | 			status = acpi_add_single_object(&child, parent, | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1207 | 							chandle, type); | 
 | 1208 | 		else | 
| Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1209 | 			status = acpi_bus_get_device(chandle, &child); | 
 | 1210 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1211 | 		if (ACPI_FAILURE(status)) | 
 | 1212 | 			continue; | 
| Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1213 |  | 
 | 1214 | 		if (ops->acpi_op_start) { | 
 | 1215 | 			status = acpi_start_single_object(child); | 
 | 1216 | 			if (ACPI_FAILURE(status)) | 
 | 1217 | 				continue; | 
 | 1218 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 |  | 
 | 1220 | 		/* | 
 | 1221 | 		 * If the device is present, enabled, and functioning then | 
 | 1222 | 		 * parse its scope (depth-first).  Note that we need to | 
 | 1223 | 		 * represent absent devices to facilitate PnP notifications | 
 | 1224 | 		 * -- but only the subtree head (not all of its children, | 
 | 1225 | 		 * which will be enumerated when the parent is inserted). | 
 | 1226 | 		 * | 
 | 1227 | 		 * TBD: Need notifications and other detection mechanisms | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1228 | 		 *      in place before we can fully implement this. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | 		 */ | 
 | 1230 | 		if (child->status.present) { | 
 | 1231 | 			status = acpi_get_next_object(ACPI_TYPE_ANY, chandle, | 
 | 1232 | 						      NULL, NULL); | 
 | 1233 | 			if (ACPI_SUCCESS(status)) { | 
 | 1234 | 				level++; | 
 | 1235 | 				phandle = chandle; | 
 | 1236 | 				chandle = NULL; | 
 | 1237 | 				parent = child; | 
 | 1238 | 			} | 
 | 1239 | 		} | 
 | 1240 | 	} | 
 | 1241 |  | 
 | 1242 | 	return_VALUE(0); | 
 | 1243 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 |  | 
| Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1245 | int | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1246 | acpi_bus_add(struct acpi_device **child, | 
 | 1247 | 	     struct acpi_device *parent, acpi_handle handle, int type) | 
| Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1248 | { | 
 | 1249 | 	int result; | 
 | 1250 | 	struct acpi_bus_ops ops; | 
 | 1251 |  | 
 | 1252 | 	ACPI_FUNCTION_TRACE("acpi_bus_add"); | 
 | 1253 |  | 
 | 1254 | 	result = acpi_add_single_object(child, parent, handle, type); | 
 | 1255 | 	if (!result) { | 
 | 1256 | 		memset(&ops, 0, sizeof(ops)); | 
 | 1257 | 		ops.acpi_op_add = 1; | 
 | 1258 | 		result = acpi_bus_scan(*child, &ops); | 
 | 1259 | 	} | 
 | 1260 | 	return_VALUE(result); | 
 | 1261 | } | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1262 |  | 
| Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1263 | EXPORT_SYMBOL(acpi_bus_add); | 
 | 1264 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1265 | int acpi_bus_start(struct acpi_device *device) | 
| Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1266 | { | 
 | 1267 | 	int result; | 
 | 1268 | 	struct acpi_bus_ops ops; | 
 | 1269 |  | 
 | 1270 | 	ACPI_FUNCTION_TRACE("acpi_bus_start"); | 
 | 1271 |  | 
 | 1272 | 	if (!device) | 
 | 1273 | 		return_VALUE(-EINVAL); | 
 | 1274 |  | 
 | 1275 | 	result = acpi_start_single_object(device); | 
 | 1276 | 	if (!result) { | 
 | 1277 | 		memset(&ops, 0, sizeof(ops)); | 
 | 1278 | 		ops.acpi_op_start = 1; | 
 | 1279 | 		result = acpi_bus_scan(device, &ops); | 
 | 1280 | 	} | 
 | 1281 | 	return_VALUE(result); | 
 | 1282 | } | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1283 |  | 
| Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1284 | EXPORT_SYMBOL(acpi_bus_start); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 |  | 
| Kristen Accardi | ceaba66 | 2006-02-23 17:56:01 -0800 | [diff] [blame] | 1286 | int acpi_bus_trim(struct acpi_device *start, int rmdevice) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1288 | 	acpi_status status; | 
 | 1289 | 	struct acpi_device *parent, *child; | 
 | 1290 | 	acpi_handle phandle, chandle; | 
 | 1291 | 	acpi_object_type type; | 
 | 1292 | 	u32 level = 1; | 
 | 1293 | 	int err = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1295 | 	parent = start; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | 	phandle = start->handle; | 
 | 1297 | 	child = chandle = NULL; | 
 | 1298 |  | 
 | 1299 | 	while ((level > 0) && parent && (!err)) { | 
 | 1300 | 		status = acpi_get_next_object(ACPI_TYPE_ANY, phandle, | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1301 | 					      chandle, &chandle); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 |  | 
 | 1303 | 		/* | 
 | 1304 | 		 * If this scope is exhausted then move our way back up. | 
 | 1305 | 		 */ | 
 | 1306 | 		if (ACPI_FAILURE(status)) { | 
 | 1307 | 			level--; | 
 | 1308 | 			chandle = phandle; | 
 | 1309 | 			acpi_get_parent(phandle, &phandle); | 
 | 1310 | 			child = parent; | 
 | 1311 | 			parent = parent->parent; | 
 | 1312 |  | 
 | 1313 | 			if (level == 0) | 
 | 1314 | 				err = acpi_bus_remove(child, rmdevice); | 
 | 1315 | 			else | 
 | 1316 | 				err = acpi_bus_remove(child, 1); | 
 | 1317 |  | 
 | 1318 | 			continue; | 
 | 1319 | 		} | 
 | 1320 |  | 
 | 1321 | 		status = acpi_get_type(chandle, &type); | 
 | 1322 | 		if (ACPI_FAILURE(status)) { | 
 | 1323 | 			continue; | 
 | 1324 | 		} | 
 | 1325 | 		/* | 
 | 1326 | 		 * If there is a device corresponding to chandle then | 
 | 1327 | 		 * parse it (depth-first). | 
 | 1328 | 		 */ | 
 | 1329 | 		if (acpi_bus_get_device(chandle, &child) == 0) { | 
 | 1330 | 			level++; | 
 | 1331 | 			phandle = chandle; | 
 | 1332 | 			chandle = NULL; | 
 | 1333 | 			parent = child; | 
 | 1334 | 		} | 
 | 1335 | 		continue; | 
 | 1336 | 	} | 
 | 1337 | 	return err; | 
 | 1338 | } | 
| Kristen Accardi | ceaba66 | 2006-02-23 17:56:01 -0800 | [diff] [blame] | 1339 | EXPORT_SYMBOL_GPL(acpi_bus_trim); | 
 | 1340 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1342 | static int acpi_bus_scan_fixed(struct acpi_device *root) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | { | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1344 | 	int result = 0; | 
 | 1345 | 	struct acpi_device *device = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 |  | 
 | 1347 | 	ACPI_FUNCTION_TRACE("acpi_bus_scan_fixed"); | 
 | 1348 |  | 
 | 1349 | 	if (!root) | 
 | 1350 | 		return_VALUE(-ENODEV); | 
 | 1351 |  | 
 | 1352 | 	/* | 
 | 1353 | 	 * Enumerate all fixed-feature devices. | 
 | 1354 | 	 */ | 
| Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1355 | 	if (acpi_fadt.pwr_button == 0) { | 
 | 1356 | 		result = acpi_add_single_object(&device, acpi_root, | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1357 | 						NULL, | 
 | 1358 | 						ACPI_BUS_TYPE_POWER_BUTTON); | 
| Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1359 | 		if (!result) | 
 | 1360 | 			result = acpi_start_single_object(device); | 
 | 1361 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 |  | 
| Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1363 | 	if (acpi_fadt.sleep_button == 0) { | 
 | 1364 | 		result = acpi_add_single_object(&device, acpi_root, | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1365 | 						NULL, | 
 | 1366 | 						ACPI_BUS_TYPE_SLEEP_BUTTON); | 
| Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1367 | 		if (!result) | 
 | 1368 | 			result = acpi_start_single_object(device); | 
 | 1369 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 |  | 
 | 1371 | 	return_VALUE(result); | 
 | 1372 | } | 
 | 1373 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | static int __init acpi_scan_init(void) | 
 | 1375 | { | 
 | 1376 | 	int result; | 
| Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1377 | 	struct acpi_bus_ops ops; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 |  | 
 | 1379 | 	ACPI_FUNCTION_TRACE("acpi_scan_init"); | 
 | 1380 |  | 
 | 1381 | 	if (acpi_disabled) | 
 | 1382 | 		return_VALUE(0); | 
 | 1383 |  | 
 | 1384 | 	kset_register(&acpi_namespace_kset); | 
 | 1385 |  | 
 | 1386 | 	/* | 
 | 1387 | 	 * Create the root device in the bus's device tree | 
 | 1388 | 	 */ | 
| Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1389 | 	result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT, | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1390 | 					ACPI_BUS_TYPE_SYSTEM); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | 	if (result) | 
 | 1392 | 		goto Done; | 
 | 1393 |  | 
| Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1394 | 	result = acpi_start_single_object(acpi_root); | 
 | 1395 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | 	/* | 
 | 1397 | 	 * Enumerate devices in the ACPI namespace. | 
 | 1398 | 	 */ | 
 | 1399 | 	result = acpi_bus_scan_fixed(acpi_root); | 
| Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1400 | 	if (!result) { | 
 | 1401 | 		memset(&ops, 0, sizeof(ops)); | 
 | 1402 | 		ops.acpi_op_add = 1; | 
 | 1403 | 		ops.acpi_op_start = 1; | 
 | 1404 | 		result = acpi_bus_scan(acpi_root, &ops); | 
 | 1405 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 |  | 
 | 1407 | 	if (result) | 
 | 1408 | 		acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL); | 
 | 1409 |  | 
| Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1410 |       Done: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | 	return_VALUE(result); | 
 | 1412 | } | 
 | 1413 |  | 
 | 1414 | subsys_initcall(acpi_scan_init); |