blob: 31218e1d2a18313a8ea9410c3bbab5934bb7b396 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070012#define _COMPONENT ACPI_BUS_COMPONENT
Len Brown4be44fc2005-08-05 00:44:28 -040013ACPI_MODULE_NAME("scan")
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040015extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
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
22static LIST_HEAD(acpi_device_list);
23DEFINE_SPINLOCK(acpi_device_lock);
24LIST_HEAD(acpi_wakeup_device_list);
25
Len Brown4be44fc2005-08-05 00:44:28 -040026static int acpi_bus_trim(struct acpi_device *start, int rmdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Len Brown4be44fc2005-08-05 00:44:28 -040028static void acpi_device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Len Brown4be44fc2005-08-05 00:44:28 -040030 struct acpi_device *dev = container_of(kobj, struct acpi_device, kobj);
Jesper Juhl6044ec82005-11-07 01:01:32 -080031 kfree(dev->pnp.cid_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 kfree(dev);
33}
34
35struct acpi_device_attribute {
36 struct attribute attr;
Len Brown4be44fc2005-08-05 00:44:28 -040037 ssize_t(*show) (struct acpi_device *, char *);
38 ssize_t(*store) (struct acpi_device *, const char *, size_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
40
41typedef void acpi_device_sysfs_files(struct kobject *,
Len Brown4be44fc2005-08-05 00:44:28 -040042 const struct attribute *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44static void setup_sys_fs_device_files(struct acpi_device *dev,
Len Brown4be44fc2005-08-05 00:44:28 -040045 acpi_device_sysfs_files * func);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define create_sysfs_device_files(dev) \
48 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_create_file)
49#define remove_sysfs_device_files(dev) \
50 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_remove_file)
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define to_acpi_device(n) container_of(n, struct acpi_device, kobj)
53#define to_handle_attr(n) container_of(n, struct acpi_device_attribute, attr);
54
55static ssize_t acpi_device_attr_show(struct kobject *kobj,
Len Brown4be44fc2005-08-05 00:44:28 -040056 struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 struct acpi_device *device = to_acpi_device(kobj);
59 struct acpi_device_attribute *attribute = to_handle_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -050060 return attribute->show ? attribute->show(device, buf) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62static ssize_t acpi_device_attr_store(struct kobject *kobj,
Len Brown4be44fc2005-08-05 00:44:28 -040063 struct attribute *attr, const char *buf,
64 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 struct acpi_device *device = to_acpi_device(kobj);
67 struct acpi_device_attribute *attribute = to_handle_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -050068 return attribute->store ? attribute->store(device, buf, len) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
71static struct sysfs_ops acpi_device_sysfs_ops = {
Len Brown4be44fc2005-08-05 00:44:28 -040072 .show = acpi_device_attr_show,
73 .store = acpi_device_attr_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074};
75
76static struct kobj_type ktype_acpi_ns = {
Len Brown4be44fc2005-08-05 00:44:28 -040077 .sysfs_ops = &acpi_device_sysfs_ops,
78 .release = acpi_device_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
81static int namespace_hotplug(struct kset *kset, struct kobject *kobj,
82 char **envp, int num_envp, char *buffer,
83 int buffer_size)
84{
85 struct acpi_device *dev = to_acpi_device(kobj);
86 int i = 0;
87 int len = 0;
88
89 if (!dev->driver)
90 return 0;
91
92 if (add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, &len,
93 "PHYSDEVDRIVER=%s", dev->driver->name))
94 return -ENOMEM;
95
96 envp[i] = NULL;
97
98 return 0;
99}
100
101static struct kset_hotplug_ops namespace_hotplug_ops = {
102 .hotplug = &namespace_hotplug,
103};
104
105static struct kset acpi_namespace_kset = {
Len Brown4be44fc2005-08-05 00:44:28 -0400106 .kobj = {
107 .name = "namespace",
108 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 .subsys = &acpi_subsys,
Len Brown4be44fc2005-08-05 00:44:28 -0400110 .ktype = &ktype_acpi_ns,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 .hotplug_ops = &namespace_hotplug_ops,
112};
113
Len Brown4be44fc2005-08-05 00:44:28 -0400114static void acpi_device_register(struct acpi_device *device,
115 struct acpi_device *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 /*
118 * Linkage
119 * -------
120 * Link this device to its parent and siblings.
121 */
122 INIT_LIST_HEAD(&device->children);
123 INIT_LIST_HEAD(&device->node);
124 INIT_LIST_HEAD(&device->g_list);
125 INIT_LIST_HEAD(&device->wakeup_list);
126
127 spin_lock(&acpi_device_lock);
128 if (device->parent) {
129 list_add_tail(&device->node, &device->parent->children);
Len Brown4be44fc2005-08-05 00:44:28 -0400130 list_add_tail(&device->g_list, &device->parent->g_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 } else
Len Brown4be44fc2005-08-05 00:44:28 -0400132 list_add_tail(&device->g_list, &acpi_device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (device->wakeup.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400134 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 spin_unlock(&acpi_device_lock);
136
Len Brown4be44fc2005-08-05 00:44:28 -0400137 strlcpy(device->kobj.name, device->pnp.bus_id, KOBJ_NAME_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (parent)
139 device->kobj.parent = &parent->kobj;
140 device->kobj.ktype = &ktype_acpi_ns;
141 device->kobj.kset = &acpi_namespace_kset;
142 kobject_register(&device->kobj);
143 create_sysfs_device_files(device);
144}
145
Len Brown4be44fc2005-08-05 00:44:28 -0400146static int acpi_device_unregister(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 spin_lock(&acpi_device_lock);
149 if (device->parent) {
150 list_del(&device->node);
151 list_del(&device->g_list);
152 } else
153 list_del(&device->g_list);
154
155 list_del(&device->wakeup_list);
156
157 spin_unlock(&acpi_device_lock);
158
159 acpi_detach_data(device->handle, acpi_bus_data_handler);
160 remove_sysfs_device_files(device);
161 kobject_unregister(&device->kobj);
162 return 0;
163}
164
Len Brown4be44fc2005-08-05 00:44:28 -0400165void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 ACPI_FUNCTION_TRACE("acpi_bus_data_handler");
168
169 /* TBD */
170
171 return_VOID;
172}
173
Len Brown4be44fc2005-08-05 00:44:28 -0400174static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Len Brown4be44fc2005-08-05 00:44:28 -0400176 acpi_status status = 0;
177 acpi_handle handle = NULL;
178 u32 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 ACPI_FUNCTION_TRACE("acpi_bus_get_power_flags");
181
182 /*
183 * Power Management Flags
184 */
185 status = acpi_get_handle(device->handle, "_PSC", &handle);
186 if (ACPI_SUCCESS(status))
187 device->power.flags.explicit_get = 1;
188 status = acpi_get_handle(device->handle, "_IRC", &handle);
189 if (ACPI_SUCCESS(status))
190 device->power.flags.inrush_current = 1;
191
192 /*
193 * Enumerate supported power management states
194 */
195 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
196 struct acpi_device_power_state *ps = &device->power.states[i];
Len Brown4be44fc2005-08-05 00:44:28 -0400197 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 /* Evaluate "_PRx" to se if power resources are referenced */
200 acpi_evaluate_reference(device->handle, object_name, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400201 &ps->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (ps->resources.count) {
203 device->power.flags.power_resources = 1;
204 ps->flags.valid = 1;
205 }
206
207 /* Evaluate "_PSx" to see if we can do explicit sets */
208 object_name[2] = 'S';
209 status = acpi_get_handle(device->handle, object_name, &handle);
210 if (ACPI_SUCCESS(status)) {
211 ps->flags.explicit_set = 1;
212 ps->flags.valid = 1;
213 }
214
215 /* State is valid if we have some power control */
216 if (ps->resources.count || ps->flags.explicit_set)
217 ps->flags.valid = 1;
218
Len Brown4be44fc2005-08-05 00:44:28 -0400219 ps->power = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 ps->latency = -1; /* Unknown - driver assigned */
221 }
222
223 /* Set defaults for D0 and D3 states (always valid) */
224 device->power.states[ACPI_STATE_D0].flags.valid = 1;
225 device->power.states[ACPI_STATE_D0].power = 100;
226 device->power.states[ACPI_STATE_D3].flags.valid = 1;
227 device->power.states[ACPI_STATE_D3].power = 0;
228
229 /* TBD: System wake support and resource requirements. */
230
231 device->power.state = ACPI_STATE_UNKNOWN;
232
233 return_VALUE(0);
234}
235
Len Brown4be44fc2005-08-05 00:44:28 -0400236int acpi_match_ids(struct acpi_device *device, char *ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 int error = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400239 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 if (device->flags.hardware_id)
242 if (strstr(ids, device->pnp.hardware_id))
243 goto Done;
244
245 if (device->flags.compatible_ids) {
246 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
247 int i;
248
249 /* compare multiple _CID entries against driver ids */
Len Brown4be44fc2005-08-05 00:44:28 -0400250 for (i = 0; i < cid_list->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (strstr(ids, cid_list->id[i].value))
252 goto Done;
253 }
254 }
255 error = -ENOENT;
256
Len Brown4be44fc2005-08-05 00:44:28 -0400257 Done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (buffer.pointer)
259 acpi_os_free(buffer.pointer);
260 return error;
261}
262
263static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400264acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
265 union acpi_object *package)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Len Brown4be44fc2005-08-05 00:44:28 -0400267 int i = 0;
268 union acpi_object *element = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 if (!device || !package || (package->package.count < 2))
271 return AE_BAD_PARAMETER;
272
273 element = &(package->package.elements[0]);
274 if (!element)
275 return AE_BAD_PARAMETER;
276 if (element->type == ACPI_TYPE_PACKAGE) {
277 if ((element->package.count < 2) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400278 (element->package.elements[0].type !=
279 ACPI_TYPE_LOCAL_REFERENCE)
280 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return AE_BAD_DATA;
Len Brown4be44fc2005-08-05 00:44:28 -0400282 device->wakeup.gpe_device =
283 element->package.elements[0].reference.handle;
284 device->wakeup.gpe_number =
285 (u32) element->package.elements[1].integer.value;
286 } else if (element->type == ACPI_TYPE_INTEGER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 device->wakeup.gpe_number = element->integer.value;
Len Brown4be44fc2005-08-05 00:44:28 -0400288 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return AE_BAD_DATA;
290
291 element = &(package->package.elements[1]);
292 if (element->type != ACPI_TYPE_INTEGER) {
293 return AE_BAD_DATA;
294 }
295 device->wakeup.sleep_state = element->integer.value;
296
297 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
298 return AE_NO_MEMORY;
299 }
300 device->wakeup.resources.count = package->package.count - 2;
Len Brown4be44fc2005-08-05 00:44:28 -0400301 for (i = 0; i < device->wakeup.resources.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 element = &(package->package.elements[i + 2]);
Len Brown4be44fc2005-08-05 00:44:28 -0400303 if (element->type != ACPI_TYPE_ANY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return AE_BAD_DATA;
305 }
306
307 device->wakeup.resources.handles[i] = element->reference.handle;
308 }
309
310 return AE_OK;
311}
312
Len Brown4be44fc2005-08-05 00:44:28 -0400313static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Len Brown4be44fc2005-08-05 00:44:28 -0400315 acpi_status status = 0;
316 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
317 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 ACPI_FUNCTION_TRACE("acpi_bus_get_wakeup_flags");
320
321 /* _PRW */
322 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
323 if (ACPI_FAILURE(status)) {
324 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PRW\n"));
325 goto end;
326 }
327
Len Brown4be44fc2005-08-05 00:44:28 -0400328 package = (union acpi_object *)buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 status = acpi_bus_extract_wakeup_device_power_package(device, package);
330 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400331 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
332 "Error extracting _PRW package\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 goto end;
334 }
335
336 acpi_os_free(buffer.pointer);
337
338 device->wakeup.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400339 /* Power button, Lid switch always enable wakeup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
341 device->wakeup.flags.run_wake = 1;
342
Len Brown4be44fc2005-08-05 00:44:28 -0400343 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (ACPI_FAILURE(status))
345 device->flags.wake_capable = 0;
346 return_VALUE(0);
347}
348
349/* --------------------------------------------------------------------------
350 ACPI hotplug sysfs device file support
351 -------------------------------------------------------------------------- */
Len Brown4be44fc2005-08-05 00:44:28 -0400352static ssize_t acpi_eject_store(struct acpi_device *device,
353 const char *buf, size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355#define ACPI_DEVICE_ATTR(_name,_mode,_show,_store) \
356static struct acpi_device_attribute acpi_device_attr_##_name = \
357 __ATTR(_name, _mode, _show, _store)
358
359ACPI_DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
360
361/**
362 * setup_sys_fs_device_files - sets up the device files under device namespace
Martin Waitz67be2dd2005-05-01 08:59:26 -0700363 * @dev: acpi_device object
364 * @func: function pointer to create or destroy the device file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 */
366static void
Len Brown4be44fc2005-08-05 00:44:28 -0400367setup_sys_fs_device_files(struct acpi_device *dev,
368 acpi_device_sysfs_files * func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Len Brown4be44fc2005-08-05 00:44:28 -0400370 acpi_status status;
371 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 /*
374 * If device has _EJ0, 'eject' file is created that is used to trigger
375 * hot-removal function from userland.
376 */
377 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
378 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400379 (*(func)) (&dev->kobj, &acpi_device_attr_eject.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Len Brown4be44fc2005-08-05 00:44:28 -0400382static int acpi_eject_operation(acpi_handle handle, int lockable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 struct acpi_object_list arg_list;
385 union acpi_object arg;
386 acpi_status status = AE_OK;
387
388 /*
389 * TBD: evaluate _PS3?
390 */
391
392 if (lockable) {
393 arg_list.count = 1;
394 arg_list.pointer = &arg;
395 arg.type = ACPI_TYPE_INTEGER;
396 arg.integer.value = 0;
397 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
398 }
399
400 arg_list.count = 1;
401 arg_list.pointer = &arg;
402 arg.type = ACPI_TYPE_INTEGER;
403 arg.integer.value = 1;
404
405 /*
406 * TBD: _EJD support.
407 */
408
409 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
410 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400411 return (-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413
Len Brown4be44fc2005-08-05 00:44:28 -0400414 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417static ssize_t
418acpi_eject_store(struct acpi_device *device, const char *buf, size_t count)
419{
Len Brown4be44fc2005-08-05 00:44:28 -0400420 int result;
421 int ret = count;
422 int islockable;
423 acpi_status status;
424 acpi_handle handle;
425 acpi_object_type type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 if ((!count) || (buf[0] != '1')) {
428 return -EINVAL;
429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430#ifndef FORCE_EJECT
431 if (device->driver == NULL) {
432 ret = -ENODEV;
433 goto err;
434 }
435#endif
436 status = acpi_get_type(device->handle, &type);
Len Brown4be44fc2005-08-05 00:44:28 -0400437 if (ACPI_FAILURE(status) || (!device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 ret = -ENODEV;
439 goto err;
440 }
441
442 islockable = device->flags.lockable;
443 handle = device->handle;
444
445 if (type == ACPI_TYPE_PROCESSOR)
446 result = acpi_bus_trim(device, 0);
447 else
448 result = acpi_bus_trim(device, 1);
449
450 if (!result)
451 result = acpi_eject_operation(handle, islockable);
452
453 if (result) {
454 ret = -EBUSY;
455 }
Len Brown4be44fc2005-08-05 00:44:28 -0400456 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return ret;
458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460/* --------------------------------------------------------------------------
461 Performance Management
462 -------------------------------------------------------------------------- */
463
Len Brown4be44fc2005-08-05 00:44:28 -0400464static int acpi_bus_get_perf_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 device->performance.state = ACPI_STATE_UNKNOWN;
467 return 0;
468}
469
470/* --------------------------------------------------------------------------
471 Driver Management
472 -------------------------------------------------------------------------- */
473
474static LIST_HEAD(acpi_bus_drivers);
475static DECLARE_MUTEX(acpi_bus_drivers_lock);
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477/**
478 * acpi_bus_match
479 * --------------
480 * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it
481 * matches the specified driver's criteria.
482 */
483static int
Len Brown4be44fc2005-08-05 00:44:28 -0400484acpi_bus_match(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 if (driver && driver->ops.match)
487 return driver->ops.match(device, driver);
488 return acpi_match_ids(device, driver->ids);
489}
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491/**
492 * acpi_bus_driver_init
493 * --------------------
494 * Used to initialize a device via its device driver. Called whenever a
495 * driver is bound to a device. Invokes the driver's add() and start() ops.
496 */
497static int
Len Brown4be44fc2005-08-05 00:44:28 -0400498acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Len Brown4be44fc2005-08-05 00:44:28 -0400500 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 ACPI_FUNCTION_TRACE("acpi_bus_driver_init");
503
504 if (!device || !driver)
505 return_VALUE(-EINVAL);
506
507 if (!driver->ops.add)
508 return_VALUE(-ENOSYS);
509
510 result = driver->ops.add(device);
511 if (result) {
512 device->driver = NULL;
513 acpi_driver_data(device) = NULL;
514 return_VALUE(result);
515 }
516
517 device->driver = driver;
518
519 /*
520 * TBD - Configuration Management: Assign resources to device based
521 * upon possible configuration and currently allocated resources.
522 */
523
Len Brown4be44fc2005-08-05 00:44:28 -0400524 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
525 "Driver successfully bound to device\n"));
Rajesh Shah3fb02732005-04-28 00:25:52 -0700526 return_VALUE(0);
527}
528
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400529static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700530{
531 int result = 0;
532 struct acpi_driver *driver;
533
534 ACPI_FUNCTION_TRACE("acpi_start_single_object");
535
536 if (!(driver = device->driver))
537 return_VALUE(0);
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (driver->ops.start) {
540 result = driver->ops.start(device);
541 if (result && driver->ops.remove)
542 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
544
Rajesh Shah3fb02732005-04-28 00:25:52 -0700545 return_VALUE(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Len Brown4be44fc2005-08-05 00:44:28 -0400548static int acpi_driver_attach(struct acpi_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Len Brown4be44fc2005-08-05 00:44:28 -0400550 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 int count = 0;
552
553 ACPI_FUNCTION_TRACE("acpi_driver_attach");
554
555 spin_lock(&acpi_device_lock);
556 list_for_each_safe(node, next, &acpi_device_list) {
Len Brown4be44fc2005-08-05 00:44:28 -0400557 struct acpi_device *dev =
558 container_of(node, struct acpi_device, g_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 if (dev->driver || !dev->status.present)
561 continue;
562 spin_unlock(&acpi_device_lock);
563
564 if (!acpi_bus_match(dev, drv)) {
565 if (!acpi_bus_driver_init(dev, drv)) {
Rajesh Shah3fb02732005-04-28 00:25:52 -0700566 acpi_start_single_object(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 atomic_inc(&drv->references);
568 count++;
Len Brown4be44fc2005-08-05 00:44:28 -0400569 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
570 "Found driver [%s] for device [%s]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 drv->name, dev->pnp.bus_id));
572 }
573 }
574 spin_lock(&acpi_device_lock);
575 }
576 spin_unlock(&acpi_device_lock);
577 return_VALUE(count);
578}
579
Len Brown4be44fc2005-08-05 00:44:28 -0400580static int acpi_driver_detach(struct acpi_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Len Brown4be44fc2005-08-05 00:44:28 -0400582 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 ACPI_FUNCTION_TRACE("acpi_driver_detach");
585
586 spin_lock(&acpi_device_lock);
Len Brown4be44fc2005-08-05 00:44:28 -0400587 list_for_each_safe(node, next, &acpi_device_list) {
588 struct acpi_device *dev =
589 container_of(node, struct acpi_device, g_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 if (dev->driver == drv) {
592 spin_unlock(&acpi_device_lock);
593 if (drv->ops.remove)
Len Brown4be44fc2005-08-05 00:44:28 -0400594 drv->ops.remove(dev, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 spin_lock(&acpi_device_lock);
596 dev->driver = NULL;
597 dev->driver_data = NULL;
598 atomic_dec(&drv->references);
599 }
600 }
601 spin_unlock(&acpi_device_lock);
602 return_VALUE(0);
603}
604
605/**
606 * acpi_bus_register_driver
607 * ------------------------
608 * Registers a driver with the ACPI bus. Searches the namespace for all
609 * devices that match the driver's criteria and binds. Returns the
610 * number of devices that were claimed by the driver, or a negative
611 * error status for failure.
612 */
Len Brown4be44fc2005-08-05 00:44:28 -0400613int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 int count;
616
617 ACPI_FUNCTION_TRACE("acpi_bus_register_driver");
618
619 if (acpi_disabled)
620 return_VALUE(-ENODEV);
621
622 if (!driver)
623 return_VALUE(-EINVAL);
624
625 spin_lock(&acpi_device_lock);
626 list_add_tail(&driver->node, &acpi_bus_drivers);
627 spin_unlock(&acpi_device_lock);
628 count = acpi_driver_attach(driver);
629
630 return_VALUE(count);
631}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Len Brown4be44fc2005-08-05 00:44:28 -0400633EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635/**
636 * acpi_bus_unregister_driver
637 * --------------------------
638 * Unregisters a driver with the ACPI bus. Searches the namespace for all
639 * devices that match the driver's criteria and unbinds.
640 */
Len Brown4be44fc2005-08-05 00:44:28 -0400641int acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 int error = 0;
644
645 ACPI_FUNCTION_TRACE("acpi_bus_unregister_driver");
646
647 if (driver) {
648 acpi_driver_detach(driver);
649
650 if (!atomic_read(&driver->references)) {
651 spin_lock(&acpi_device_lock);
652 list_del_init(&driver->node);
653 spin_unlock(&acpi_device_lock);
Len Brown4be44fc2005-08-05 00:44:28 -0400654 }
655 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 error = -EINVAL;
657 return_VALUE(error);
658}
Len Brown4be44fc2005-08-05 00:44:28 -0400659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660EXPORT_SYMBOL(acpi_bus_unregister_driver);
661
662/**
663 * acpi_bus_find_driver
664 * --------------------
665 * Parses the list of registered drivers looking for a driver applicable for
666 * the specified device.
667 */
Len Brown4be44fc2005-08-05 00:44:28 -0400668static int acpi_bus_find_driver(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Len Brown4be44fc2005-08-05 00:44:28 -0400670 int result = 0;
671 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 ACPI_FUNCTION_TRACE("acpi_bus_find_driver");
674
675 spin_lock(&acpi_device_lock);
Len Brown4be44fc2005-08-05 00:44:28 -0400676 list_for_each_safe(node, next, &acpi_bus_drivers) {
677 struct acpi_driver *driver =
678 container_of(node, struct acpi_driver, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 atomic_inc(&driver->references);
681 spin_unlock(&acpi_device_lock);
682 if (!acpi_bus_match(device, driver)) {
683 result = acpi_bus_driver_init(device, driver);
684 if (!result)
685 goto Done;
686 }
687 atomic_dec(&driver->references);
688 spin_lock(&acpi_device_lock);
689 }
690 spin_unlock(&acpi_device_lock);
691
Len Brown4be44fc2005-08-05 00:44:28 -0400692 Done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return_VALUE(result);
694}
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696/* --------------------------------------------------------------------------
697 Device Enumeration
698 -------------------------------------------------------------------------- */
699
Len Brown4be44fc2005-08-05 00:44:28 -0400700static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Len Brown4be44fc2005-08-05 00:44:28 -0400702 acpi_status status = AE_OK;
703 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 ACPI_FUNCTION_TRACE("acpi_bus_get_flags");
706
707 /* Presence of _STA indicates 'dynamic_status' */
708 status = acpi_get_handle(device->handle, "_STA", &temp);
709 if (ACPI_SUCCESS(status))
710 device->flags.dynamic_status = 1;
711
712 /* Presence of _CID indicates 'compatible_ids' */
713 status = acpi_get_handle(device->handle, "_CID", &temp);
714 if (ACPI_SUCCESS(status))
715 device->flags.compatible_ids = 1;
716
717 /* Presence of _RMV indicates 'removable' */
718 status = acpi_get_handle(device->handle, "_RMV", &temp);
719 if (ACPI_SUCCESS(status))
720 device->flags.removable = 1;
721
722 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
723 status = acpi_get_handle(device->handle, "_EJD", &temp);
724 if (ACPI_SUCCESS(status))
725 device->flags.ejectable = 1;
726 else {
727 status = acpi_get_handle(device->handle, "_EJ0", &temp);
728 if (ACPI_SUCCESS(status))
729 device->flags.ejectable = 1;
730 }
731
732 /* Presence of _LCK indicates 'lockable' */
733 status = acpi_get_handle(device->handle, "_LCK", &temp);
734 if (ACPI_SUCCESS(status))
735 device->flags.lockable = 1;
736
737 /* Presence of _PS0|_PR0 indicates 'power manageable' */
738 status = acpi_get_handle(device->handle, "_PS0", &temp);
739 if (ACPI_FAILURE(status))
740 status = acpi_get_handle(device->handle, "_PR0", &temp);
741 if (ACPI_SUCCESS(status))
742 device->flags.power_manageable = 1;
743
744 /* Presence of _PRW indicates wake capable */
745 status = acpi_get_handle(device->handle, "_PRW", &temp);
746 if (ACPI_SUCCESS(status))
747 device->flags.wake_capable = 1;
748
749 /* TBD: Peformance management */
750
751 return_VALUE(0);
752}
753
Len Brown4be44fc2005-08-05 00:44:28 -0400754static void acpi_device_get_busid(struct acpi_device *device,
755 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Len Brown4be44fc2005-08-05 00:44:28 -0400757 char bus_id[5] = { '?', 0 };
758 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
759 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 /*
762 * Bus ID
763 * ------
764 * The device's Bus ID is simply the object name.
765 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
766 */
767 switch (type) {
768 case ACPI_BUS_TYPE_SYSTEM:
769 strcpy(device->pnp.bus_id, "ACPI");
770 break;
771 case ACPI_BUS_TYPE_POWER_BUTTON:
772 strcpy(device->pnp.bus_id, "PWRF");
773 break;
774 case ACPI_BUS_TYPE_SLEEP_BUTTON:
775 strcpy(device->pnp.bus_id, "SLPF");
776 break;
777 default:
778 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
779 /* Clean up trailing underscores (if any) */
780 for (i = 3; i > 1; i--) {
781 if (bus_id[i] == '_')
782 bus_id[i] = '\0';
783 else
784 break;
785 }
786 strcpy(device->pnp.bus_id, bus_id);
787 break;
788 }
789}
790
Len Brown4be44fc2005-08-05 00:44:28 -0400791static void acpi_device_set_id(struct acpi_device *device,
792 struct acpi_device *parent, acpi_handle handle,
793 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Len Brown4be44fc2005-08-05 00:44:28 -0400795 struct acpi_device_info *info;
796 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
797 char *hid = NULL;
798 char *uid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 struct acpi_compatible_id_list *cid_list = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400800 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 switch (type) {
803 case ACPI_BUS_TYPE_DEVICE:
804 status = acpi_get_object_info(handle, &buffer);
805 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400806 printk("%s: Error reading device info\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return;
808 }
809
810 info = buffer.pointer;
811 if (info->valid & ACPI_VALID_HID)
812 hid = info->hardware_id.value;
813 if (info->valid & ACPI_VALID_UID)
814 uid = info->unique_id.value;
815 if (info->valid & ACPI_VALID_CID)
816 cid_list = &info->compatibility_id;
817 if (info->valid & ACPI_VALID_ADR) {
818 device->pnp.bus_address = info->address;
819 device->flags.bus_address = 1;
820 }
821 break;
822 case ACPI_BUS_TYPE_POWER:
823 hid = ACPI_POWER_HID;
824 break;
825 case ACPI_BUS_TYPE_PROCESSOR:
826 hid = ACPI_PROCESSOR_HID;
827 break;
828 case ACPI_BUS_TYPE_SYSTEM:
829 hid = ACPI_SYSTEM_HID;
830 break;
831 case ACPI_BUS_TYPE_THERMAL:
832 hid = ACPI_THERMAL_HID;
833 break;
834 case ACPI_BUS_TYPE_POWER_BUTTON:
835 hid = ACPI_BUTTON_HID_POWERF;
836 break;
837 case ACPI_BUS_TYPE_SLEEP_BUTTON:
838 hid = ACPI_BUTTON_HID_SLEEPF;
839 break;
840 }
841
842 /*
843 * \_SB
844 * ----
845 * Fix for the system root bus device -- the only root-level device.
846 */
847 if ((parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
848 hid = ACPI_BUS_HID;
849 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
850 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
851 }
852
853 if (hid) {
854 strcpy(device->pnp.hardware_id, hid);
855 device->flags.hardware_id = 1;
856 }
857 if (uid) {
858 strcpy(device->pnp.unique_id, uid);
859 device->flags.unique_id = 1;
860 }
861 if (cid_list) {
862 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
863 if (device->pnp.cid_list)
864 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
865 else
866 printk(KERN_ERR "Memory allocation error\n");
867 }
868
869 acpi_os_free(buffer.pointer);
870}
871
Len Brown4be44fc2005-08-05 00:44:28 -0400872static int acpi_device_set_context(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 acpi_status status = AE_OK;
875 int result = 0;
876 /*
877 * Context
878 * -------
879 * Attach this 'struct acpi_device' to the ACPI object. This makes
880 * resolutions from handle->device very efficient. Note that we need
881 * to be careful with fixed-feature devices as they all attach to the
882 * root object.
883 */
Len Brown4be44fc2005-08-05 00:44:28 -0400884 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
886 status = acpi_attach_data(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400887 acpi_bus_data_handler, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 if (ACPI_FAILURE(status)) {
890 printk("Error attaching device data\n");
891 result = -ENODEV;
892 }
893 }
894 return result;
895}
896
Len Brown4be44fc2005-08-05 00:44:28 -0400897static void acpi_device_get_debug_info(struct acpi_device *device,
898 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
900#ifdef CONFIG_ACPI_DEBUG_OUTPUT
Len Brown4be44fc2005-08-05 00:44:28 -0400901 char *type_string = NULL;
902 char name[80] = { '?', '\0' };
903 struct acpi_buffer buffer = { sizeof(name), name };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 switch (type) {
906 case ACPI_BUS_TYPE_DEVICE:
907 type_string = "Device";
908 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
909 break;
910 case ACPI_BUS_TYPE_POWER:
911 type_string = "Power Resource";
912 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
913 break;
914 case ACPI_BUS_TYPE_PROCESSOR:
915 type_string = "Processor";
916 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
917 break;
918 case ACPI_BUS_TYPE_SYSTEM:
919 type_string = "System";
920 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
921 break;
922 case ACPI_BUS_TYPE_THERMAL:
923 type_string = "Thermal Zone";
924 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
925 break;
926 case ACPI_BUS_TYPE_POWER_BUTTON:
927 type_string = "Power Button";
928 sprintf(name, "PWRB");
929 break;
930 case ACPI_BUS_TYPE_SLEEP_BUTTON:
931 type_string = "Sleep Button";
932 sprintf(name, "SLPB");
933 break;
934 }
935
936 printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
Len Brown4be44fc2005-08-05 00:44:28 -0400937#endif /*CONFIG_ACPI_DEBUG_OUTPUT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Len Brown4be44fc2005-08-05 00:44:28 -0400940static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Len Brown4be44fc2005-08-05 00:44:28 -0400942 int result = 0;
943 struct acpi_driver *driver;
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 ACPI_FUNCTION_TRACE("acpi_bus_remove");
946
947 if (!dev)
948 return_VALUE(-EINVAL);
949
950 driver = dev->driver;
951
952 if ((driver) && (driver->ops.remove)) {
953
954 if (driver->ops.stop) {
955 result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT);
956 if (result)
957 return_VALUE(result);
958 }
959
960 result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT);
961 if (result) {
962 return_VALUE(result);
963 }
964
965 atomic_dec(&dev->driver->references);
966 dev->driver = NULL;
967 acpi_driver_data(dev) = NULL;
968 }
969
970 if (!rmdevice)
971 return_VALUE(0);
972
973 if (dev->flags.bus_address) {
974 if ((dev->parent) && (dev->parent->ops.unbind))
975 dev->parent->ops.unbind(dev);
976 }
Len Brown4be44fc2005-08-05 00:44:28 -0400977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
979
980 return_VALUE(0);
981}
982
Rajesh Shah3fb02732005-04-28 00:25:52 -0700983static int
Len Brown4be44fc2005-08-05 00:44:28 -0400984acpi_add_single_object(struct acpi_device **child,
985 struct acpi_device *parent, acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Len Brown4be44fc2005-08-05 00:44:28 -0400987 int result = 0;
988 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Rajesh Shah3fb02732005-04-28 00:25:52 -0700990 ACPI_FUNCTION_TRACE("acpi_add_single_object");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 if (!child)
993 return_VALUE(-EINVAL);
994
995 device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
996 if (!device) {
997 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Memory allocation error\n"));
998 return_VALUE(-ENOMEM);
999 }
1000 memset(device, 0, sizeof(struct acpi_device));
1001
1002 device->handle = handle;
1003 device->parent = parent;
1004
Len Brown4be44fc2005-08-05 00:44:28 -04001005 acpi_device_get_busid(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 /*
1008 * Flags
1009 * -----
1010 * Get prior to calling acpi_bus_get_status() so we know whether
1011 * or not _STA is present. Note that we only look for object
1012 * handles -- cannot evaluate objects until we know the device is
1013 * present and properly initialized.
1014 */
1015 result = acpi_bus_get_flags(device);
1016 if (result)
1017 goto end;
1018
1019 /*
1020 * Status
1021 * ------
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001022 * See if the device is present. We always assume that non-Device
1023 * and non-Processor objects (e.g. thermal zones, power resources,
1024 * etc.) are present, functioning, etc. (at least when parent object
1025 * is present). Note that _STA has a different meaning for some
1026 * objects (e.g. power resources) so we need to be careful how we use
1027 * it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 */
1029 switch (type) {
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001030 case ACPI_BUS_TYPE_PROCESSOR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 case ACPI_BUS_TYPE_DEVICE:
1032 result = acpi_bus_get_status(device);
1033 if (ACPI_FAILURE(result) || !device->status.present) {
1034 result = -ENOENT;
1035 goto end;
1036 }
1037 break;
1038 default:
1039 STRUCT_TO_INT(device->status) = 0x0F;
1040 break;
1041 }
1042
1043 /*
1044 * Initialize Device
1045 * -----------------
1046 * TBD: Synch with Core's enumeration/initialization process.
1047 */
1048
1049 /*
1050 * Hardware ID, Unique ID, & Bus Address
1051 * -------------------------------------
1052 */
Len Brown4be44fc2005-08-05 00:44:28 -04001053 acpi_device_set_id(device, parent, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 /*
1056 * Power Management
1057 * ----------------
1058 */
1059 if (device->flags.power_manageable) {
1060 result = acpi_bus_get_power_flags(device);
1061 if (result)
1062 goto end;
1063 }
1064
Len Brown4be44fc2005-08-05 00:44:28 -04001065 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 * Wakeup device management
1067 *-----------------------
1068 */
1069 if (device->flags.wake_capable) {
1070 result = acpi_bus_get_wakeup_device_flags(device);
1071 if (result)
1072 goto end;
1073 }
1074
1075 /*
1076 * Performance Management
1077 * ----------------------
1078 */
1079 if (device->flags.performance_manageable) {
1080 result = acpi_bus_get_perf_flags(device);
1081 if (result)
1082 goto end;
1083 }
1084
Len Brown4be44fc2005-08-05 00:44:28 -04001085 if ((result = acpi_device_set_context(device, type)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 goto end;
1087
Len Brown4be44fc2005-08-05 00:44:28 -04001088 acpi_device_get_debug_info(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Len Brown4be44fc2005-08-05 00:44:28 -04001090 acpi_device_register(device, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 /*
1093 * Bind _ADR-Based Devices
1094 * -----------------------
1095 * If there's a a bus address (_ADR) then we utilize the parent's
1096 * 'bind' function (if exists) to bind the ACPI- and natively-
1097 * enumerated device representations.
1098 */
1099 if (device->flags.bus_address) {
1100 if (device->parent && device->parent->ops.bind)
1101 device->parent->ops.bind(device);
1102 }
1103
1104 /*
1105 * Locate & Attach Driver
1106 * ----------------------
1107 * If there's a hardware id (_HID) or compatible ids (_CID) we check
1108 * to see if there's a driver installed for this kind of device. Note
1109 * that drivers can install before or after a device is enumerated.
1110 *
1111 * TBD: Assumes LDM provides driver hot-plug capability.
1112 */
Thomas Renningerbd7ce5b2005-10-03 10:39:00 -07001113 acpi_bus_find_driver(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Len Brown4be44fc2005-08-05 00:44:28 -04001115 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (!result)
1117 *child = device;
1118 else {
Jesper Juhl6044ec82005-11-07 01:01:32 -08001119 kfree(device->pnp.cid_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 kfree(device);
1121 }
1122
1123 return_VALUE(result);
1124}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Len Brown4be44fc2005-08-05 00:44:28 -04001126static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Len Brown4be44fc2005-08-05 00:44:28 -04001128 acpi_status status = AE_OK;
1129 struct acpi_device *parent = NULL;
1130 struct acpi_device *child = NULL;
1131 acpi_handle phandle = NULL;
1132 acpi_handle chandle = NULL;
1133 acpi_object_type type = 0;
1134 u32 level = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 ACPI_FUNCTION_TRACE("acpi_bus_scan");
1137
1138 if (!start)
1139 return_VALUE(-EINVAL);
1140
1141 parent = start;
1142 phandle = start->handle;
Len Brown4be44fc2005-08-05 00:44:28 -04001143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 /*
1145 * Parse through the ACPI namespace, identify all 'devices', and
1146 * create a new 'struct acpi_device' for each.
1147 */
1148 while ((level > 0) && parent) {
1149
1150 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001151 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 /*
1154 * If this scope is exhausted then move our way back up.
1155 */
1156 if (ACPI_FAILURE(status)) {
1157 level--;
1158 chandle = phandle;
1159 acpi_get_parent(phandle, &phandle);
1160 if (parent->parent)
1161 parent = parent->parent;
1162 continue;
1163 }
1164
1165 status = acpi_get_type(chandle, &type);
1166 if (ACPI_FAILURE(status))
1167 continue;
1168
1169 /*
1170 * If this is a scope object then parse it (depth-first).
1171 */
1172 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1173 level++;
1174 phandle = chandle;
1175 chandle = NULL;
1176 continue;
1177 }
1178
1179 /*
1180 * We're only interested in objects that we consider 'devices'.
1181 */
1182 switch (type) {
1183 case ACPI_TYPE_DEVICE:
1184 type = ACPI_BUS_TYPE_DEVICE;
1185 break;
1186 case ACPI_TYPE_PROCESSOR:
1187 type = ACPI_BUS_TYPE_PROCESSOR;
1188 break;
1189 case ACPI_TYPE_THERMAL:
1190 type = ACPI_BUS_TYPE_THERMAL;
1191 break;
1192 case ACPI_TYPE_POWER:
1193 type = ACPI_BUS_TYPE_POWER;
1194 break;
1195 default:
1196 continue;
1197 }
1198
Rajesh Shah3fb02732005-04-28 00:25:52 -07001199 if (ops->acpi_op_add)
1200 status = acpi_add_single_object(&child, parent,
Len Brown4be44fc2005-08-05 00:44:28 -04001201 chandle, type);
1202 else
Rajesh Shah3fb02732005-04-28 00:25:52 -07001203 status = acpi_bus_get_device(chandle, &child);
1204
Len Brown4be44fc2005-08-05 00:44:28 -04001205 if (ACPI_FAILURE(status))
1206 continue;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001207
1208 if (ops->acpi_op_start) {
1209 status = acpi_start_single_object(child);
1210 if (ACPI_FAILURE(status))
1211 continue;
1212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 /*
1215 * If the device is present, enabled, and functioning then
1216 * parse its scope (depth-first). Note that we need to
1217 * represent absent devices to facilitate PnP notifications
1218 * -- but only the subtree head (not all of its children,
1219 * which will be enumerated when the parent is inserted).
1220 *
1221 * TBD: Need notifications and other detection mechanisms
Len Brown4be44fc2005-08-05 00:44:28 -04001222 * in place before we can fully implement this.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 */
1224 if (child->status.present) {
1225 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1226 NULL, NULL);
1227 if (ACPI_SUCCESS(status)) {
1228 level++;
1229 phandle = chandle;
1230 chandle = NULL;
1231 parent = child;
1232 }
1233 }
1234 }
1235
1236 return_VALUE(0);
1237}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Rajesh Shah3fb02732005-04-28 00:25:52 -07001239int
Len Brown4be44fc2005-08-05 00:44:28 -04001240acpi_bus_add(struct acpi_device **child,
1241 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001242{
1243 int result;
1244 struct acpi_bus_ops ops;
1245
1246 ACPI_FUNCTION_TRACE("acpi_bus_add");
1247
1248 result = acpi_add_single_object(child, parent, handle, type);
1249 if (!result) {
1250 memset(&ops, 0, sizeof(ops));
1251 ops.acpi_op_add = 1;
1252 result = acpi_bus_scan(*child, &ops);
1253 }
1254 return_VALUE(result);
1255}
Len Brown4be44fc2005-08-05 00:44:28 -04001256
Rajesh Shah3fb02732005-04-28 00:25:52 -07001257EXPORT_SYMBOL(acpi_bus_add);
1258
Len Brown4be44fc2005-08-05 00:44:28 -04001259int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001260{
1261 int result;
1262 struct acpi_bus_ops ops;
1263
1264 ACPI_FUNCTION_TRACE("acpi_bus_start");
1265
1266 if (!device)
1267 return_VALUE(-EINVAL);
1268
1269 result = acpi_start_single_object(device);
1270 if (!result) {
1271 memset(&ops, 0, sizeof(ops));
1272 ops.acpi_op_start = 1;
1273 result = acpi_bus_scan(device, &ops);
1274 }
1275 return_VALUE(result);
1276}
Len Brown4be44fc2005-08-05 00:44:28 -04001277
Rajesh Shah3fb02732005-04-28 00:25:52 -07001278EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Len Brown4be44fc2005-08-05 00:44:28 -04001280static int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Len Brown4be44fc2005-08-05 00:44:28 -04001282 acpi_status status;
1283 struct acpi_device *parent, *child;
1284 acpi_handle phandle, chandle;
1285 acpi_object_type type;
1286 u32 level = 1;
1287 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Len Brown4be44fc2005-08-05 00:44:28 -04001289 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 phandle = start->handle;
1291 child = chandle = NULL;
1292
1293 while ((level > 0) && parent && (!err)) {
1294 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001295 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 /*
1298 * If this scope is exhausted then move our way back up.
1299 */
1300 if (ACPI_FAILURE(status)) {
1301 level--;
1302 chandle = phandle;
1303 acpi_get_parent(phandle, &phandle);
1304 child = parent;
1305 parent = parent->parent;
1306
1307 if (level == 0)
1308 err = acpi_bus_remove(child, rmdevice);
1309 else
1310 err = acpi_bus_remove(child, 1);
1311
1312 continue;
1313 }
1314
1315 status = acpi_get_type(chandle, &type);
1316 if (ACPI_FAILURE(status)) {
1317 continue;
1318 }
1319 /*
1320 * If there is a device corresponding to chandle then
1321 * parse it (depth-first).
1322 */
1323 if (acpi_bus_get_device(chandle, &child) == 0) {
1324 level++;
1325 phandle = chandle;
1326 chandle = NULL;
1327 parent = child;
1328 }
1329 continue;
1330 }
1331 return err;
1332}
1333
Len Brown4be44fc2005-08-05 00:44:28 -04001334static int acpi_bus_scan_fixed(struct acpi_device *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
Len Brown4be44fc2005-08-05 00:44:28 -04001336 int result = 0;
1337 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 ACPI_FUNCTION_TRACE("acpi_bus_scan_fixed");
1340
1341 if (!root)
1342 return_VALUE(-ENODEV);
1343
1344 /*
1345 * Enumerate all fixed-feature devices.
1346 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001347 if (acpi_fadt.pwr_button == 0) {
1348 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001349 NULL,
1350 ACPI_BUS_TYPE_POWER_BUTTON);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001351 if (!result)
1352 result = acpi_start_single_object(device);
1353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Rajesh Shah3fb02732005-04-28 00:25:52 -07001355 if (acpi_fadt.sleep_button == 0) {
1356 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001357 NULL,
1358 ACPI_BUS_TYPE_SLEEP_BUTTON);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001359 if (!result)
1360 result = acpi_start_single_object(device);
1361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 return_VALUE(result);
1364}
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366static int __init acpi_scan_init(void)
1367{
1368 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001369 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 ACPI_FUNCTION_TRACE("acpi_scan_init");
1372
1373 if (acpi_disabled)
1374 return_VALUE(0);
1375
1376 kset_register(&acpi_namespace_kset);
1377
1378 /*
1379 * Create the root device in the bus's device tree
1380 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001381 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
Len Brown4be44fc2005-08-05 00:44:28 -04001382 ACPI_BUS_TYPE_SYSTEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (result)
1384 goto Done;
1385
Rajesh Shah3fb02732005-04-28 00:25:52 -07001386 result = acpi_start_single_object(acpi_root);
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 /*
1389 * Enumerate devices in the ACPI namespace.
1390 */
1391 result = acpi_bus_scan_fixed(acpi_root);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001392 if (!result) {
1393 memset(&ops, 0, sizeof(ops));
1394 ops.acpi_op_add = 1;
1395 ops.acpi_op_start = 1;
1396 result = acpi_bus_scan(acpi_root, &ops);
1397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 if (result)
1400 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1401
Len Brown4be44fc2005-08-05 00:44:28 -04001402 Done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return_VALUE(result);
1404}
1405
1406subsys_initcall(acpi_scan_init);