blob: e8efaac71e74836e740d5dd712af45d82b12e1bb [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Len Brown4be44fc2005-08-05 00:44:28 -040027static void acpi_device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Len Brown4be44fc2005-08-05 00:44:28 -040029 struct acpi_device *dev = container_of(kobj, struct acpi_device, kobj);
Jesper Juhl6044ec82005-11-07 01:01:32 -080030 kfree(dev->pnp.cid_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 kfree(dev);
32}
33
34struct acpi_device_attribute {
35 struct attribute attr;
Len Brown4be44fc2005-08-05 00:44:28 -040036 ssize_t(*show) (struct acpi_device *, char *);
37 ssize_t(*store) (struct acpi_device *, const char *, size_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038};
39
40typedef void acpi_device_sysfs_files(struct kobject *,
Len Brown4be44fc2005-08-05 00:44:28 -040041 const struct attribute *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43static void setup_sys_fs_device_files(struct acpi_device *dev,
Len Brown4be44fc2005-08-05 00:44:28 -040044 acpi_device_sysfs_files * func);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
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 Torvalds1da177e2005-04-16 15:20:36 -070051#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
54static ssize_t acpi_device_attr_show(struct kobject *kobj,
Len Brown4be44fc2005-08-05 00:44:28 -040055 struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 struct acpi_device *device = to_acpi_device(kobj);
58 struct acpi_device_attribute *attribute = to_handle_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -050059 return attribute->show ? attribute->show(device, buf) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61static ssize_t acpi_device_attr_store(struct kobject *kobj,
Len Brown4be44fc2005-08-05 00:44:28 -040062 struct attribute *attr, const char *buf,
63 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 struct acpi_device *device = to_acpi_device(kobj);
66 struct acpi_device_attribute *attribute = to_handle_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -050067 return attribute->store ? attribute->store(device, buf, len) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
70static struct sysfs_ops acpi_device_sysfs_ops = {
Len Brown4be44fc2005-08-05 00:44:28 -040071 .show = acpi_device_attr_show,
72 .store = acpi_device_attr_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
75static struct kobj_type ktype_acpi_ns = {
Len Brown4be44fc2005-08-05 00:44:28 -040076 .sysfs_ops = &acpi_device_sysfs_ops,
77 .release = acpi_device_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79
Kay Sievers312c0042005-11-16 09:00:00 +010080static int namespace_uevent(struct kset *kset, struct kobject *kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 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 Sievers312c0042005-11-16 09:00:00 +010091 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
92 "PHYSDEVDRIVER=%s", dev->driver->name))
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return -ENOMEM;
94
95 envp[i] = NULL;
96
97 return 0;
98}
99
Kay Sievers312c0042005-11-16 09:00:00 +0100100static struct kset_uevent_ops namespace_uevent_ops = {
101 .uevent = &namespace_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
104static struct kset acpi_namespace_kset = {
Len Brown4be44fc2005-08-05 00:44:28 -0400105 .kobj = {
106 .name = "namespace",
107 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 .subsys = &acpi_subsys,
Len Brown4be44fc2005-08-05 00:44:28 -0400109 .ktype = &ktype_acpi_ns,
Kay Sievers312c0042005-11-16 09:00:00 +0100110 .uevent_ops = &namespace_uevent_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
Len Brown4be44fc2005-08-05 00:44:28 -0400113static void acpi_device_register(struct acpi_device *device,
114 struct acpi_device *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
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 Brown4be44fc2005-08-05 00:44:28 -0400129 list_add_tail(&device->g_list, &device->parent->g_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 } else
Len Brown4be44fc2005-08-05 00:44:28 -0400131 list_add_tail(&device->g_list, &acpi_device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (device->wakeup.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400133 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 spin_unlock(&acpi_device_lock);
135
Len Brown4be44fc2005-08-05 00:44:28 -0400136 strlcpy(device->kobj.name, device->pnp.bus_id, KOBJ_NAME_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 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
Bjorn Helgaas06ea8e02006-04-27 05:25:00 -0400145static void acpi_device_unregister(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Len Brown4be44fc2005-08-05 00:44:28 -0400163void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 ACPI_FUNCTION_TRACE("acpi_bus_data_handler");
166
167 /* TBD */
168
169 return_VOID;
170}
171
Len Brown4be44fc2005-08-05 00:44:28 -0400172static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Len Brown4be44fc2005-08-05 00:44:28 -0400174 acpi_status status = 0;
175 acpi_handle handle = NULL;
176 u32 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 ACPI_FUNCTION_TRACE("acpi_bus_get_power_flags");
179
180 /*
181 * Power Management Flags
182 */
183 status = acpi_get_handle(device->handle, "_PSC", &handle);
184 if (ACPI_SUCCESS(status))
185 device->power.flags.explicit_get = 1;
186 status = acpi_get_handle(device->handle, "_IRC", &handle);
187 if (ACPI_SUCCESS(status))
188 device->power.flags.inrush_current = 1;
189
190 /*
191 * Enumerate supported power management states
192 */
193 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
194 struct acpi_device_power_state *ps = &device->power.states[i];
Len Brown4be44fc2005-08-05 00:44:28 -0400195 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 /* Evaluate "_PRx" to se if power resources are referenced */
198 acpi_evaluate_reference(device->handle, object_name, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400199 &ps->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (ps->resources.count) {
201 device->power.flags.power_resources = 1;
202 ps->flags.valid = 1;
203 }
204
205 /* Evaluate "_PSx" to see if we can do explicit sets */
206 object_name[2] = 'S';
207 status = acpi_get_handle(device->handle, object_name, &handle);
208 if (ACPI_SUCCESS(status)) {
209 ps->flags.explicit_set = 1;
210 ps->flags.valid = 1;
211 }
212
213 /* State is valid if we have some power control */
214 if (ps->resources.count || ps->flags.explicit_set)
215 ps->flags.valid = 1;
216
Len Brown4be44fc2005-08-05 00:44:28 -0400217 ps->power = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 ps->latency = -1; /* Unknown - driver assigned */
219 }
220
221 /* Set defaults for D0 and D3 states (always valid) */
222 device->power.states[ACPI_STATE_D0].flags.valid = 1;
223 device->power.states[ACPI_STATE_D0].power = 100;
224 device->power.states[ACPI_STATE_D3].flags.valid = 1;
225 device->power.states[ACPI_STATE_D3].power = 0;
226
227 /* TBD: System wake support and resource requirements. */
228
229 device->power.state = ACPI_STATE_UNKNOWN;
230
231 return_VALUE(0);
232}
233
Len Brown4be44fc2005-08-05 00:44:28 -0400234int acpi_match_ids(struct acpi_device *device, char *ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 int error = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400237 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 if (device->flags.hardware_id)
240 if (strstr(ids, device->pnp.hardware_id))
241 goto Done;
242
243 if (device->flags.compatible_ids) {
244 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
245 int i;
246
247 /* compare multiple _CID entries against driver ids */
Len Brown4be44fc2005-08-05 00:44:28 -0400248 for (i = 0; i < cid_list->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (strstr(ids, cid_list->id[i].value))
250 goto Done;
251 }
252 }
253 error = -ENOENT;
254
Len Brown4be44fc2005-08-05 00:44:28 -0400255 Done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (buffer.pointer)
257 acpi_os_free(buffer.pointer);
258 return error;
259}
260
261static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400262acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
263 union acpi_object *package)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Len Brown4be44fc2005-08-05 00:44:28 -0400265 int i = 0;
266 union acpi_object *element = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 if (!device || !package || (package->package.count < 2))
269 return AE_BAD_PARAMETER;
270
271 element = &(package->package.elements[0]);
272 if (!element)
273 return AE_BAD_PARAMETER;
274 if (element->type == ACPI_TYPE_PACKAGE) {
275 if ((element->package.count < 2) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400276 (element->package.elements[0].type !=
277 ACPI_TYPE_LOCAL_REFERENCE)
278 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return AE_BAD_DATA;
Len Brown4be44fc2005-08-05 00:44:28 -0400280 device->wakeup.gpe_device =
281 element->package.elements[0].reference.handle;
282 device->wakeup.gpe_number =
283 (u32) element->package.elements[1].integer.value;
284 } else if (element->type == ACPI_TYPE_INTEGER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 device->wakeup.gpe_number = element->integer.value;
Len Brown4be44fc2005-08-05 00:44:28 -0400286 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return AE_BAD_DATA;
288
289 element = &(package->package.elements[1]);
290 if (element->type != ACPI_TYPE_INTEGER) {
291 return AE_BAD_DATA;
292 }
293 device->wakeup.sleep_state = element->integer.value;
294
295 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
296 return AE_NO_MEMORY;
297 }
298 device->wakeup.resources.count = package->package.count - 2;
Len Brown4be44fc2005-08-05 00:44:28 -0400299 for (i = 0; i < device->wakeup.resources.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 element = &(package->package.elements[i + 2]);
Len Brown4be44fc2005-08-05 00:44:28 -0400301 if (element->type != ACPI_TYPE_ANY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return AE_BAD_DATA;
303 }
304
305 device->wakeup.resources.handles[i] = element->reference.handle;
306 }
307
308 return AE_OK;
309}
310
Len Brown4be44fc2005-08-05 00:44:28 -0400311static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Len Brown4be44fc2005-08-05 00:44:28 -0400313 acpi_status status = 0;
314 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
315 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 ACPI_FUNCTION_TRACE("acpi_bus_get_wakeup_flags");
318
319 /* _PRW */
320 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
321 if (ACPI_FAILURE(status)) {
322 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PRW\n"));
323 goto end;
324 }
325
Len Brown4be44fc2005-08-05 00:44:28 -0400326 package = (union acpi_object *)buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 status = acpi_bus_extract_wakeup_device_power_package(device, package);
328 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400329 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
330 "Error extracting _PRW package\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 goto end;
332 }
333
334 acpi_os_free(buffer.pointer);
335
336 device->wakeup.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400337 /* Power button, Lid switch always enable wakeup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
339 device->wakeup.flags.run_wake = 1;
340
Len Brown4be44fc2005-08-05 00:44:28 -0400341 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (ACPI_FAILURE(status))
343 device->flags.wake_capable = 0;
344 return_VALUE(0);
345}
346
347/* --------------------------------------------------------------------------
Kay Sievers312c0042005-11-16 09:00:00 +0100348 ACPI sysfs device file support
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 -------------------------------------------------------------------------- */
Len Brown4be44fc2005-08-05 00:44:28 -0400350static ssize_t acpi_eject_store(struct acpi_device *device,
351 const char *buf, size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353#define ACPI_DEVICE_ATTR(_name,_mode,_show,_store) \
354static struct acpi_device_attribute acpi_device_attr_##_name = \
355 __ATTR(_name, _mode, _show, _store)
356
357ACPI_DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
358
359/**
360 * setup_sys_fs_device_files - sets up the device files under device namespace
Martin Waitz67be2dd2005-05-01 08:59:26 -0700361 * @dev: acpi_device object
362 * @func: function pointer to create or destroy the device file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 */
364static void
Len Brown4be44fc2005-08-05 00:44:28 -0400365setup_sys_fs_device_files(struct acpi_device *dev,
366 acpi_device_sysfs_files * func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Len Brown4be44fc2005-08-05 00:44:28 -0400368 acpi_status status;
369 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 /*
372 * If device has _EJ0, 'eject' file is created that is used to trigger
373 * hot-removal function from userland.
374 */
375 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
376 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400377 (*(func)) (&dev->kobj, &acpi_device_attr_eject.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Len Brown4be44fc2005-08-05 00:44:28 -0400380static int acpi_eject_operation(acpi_handle handle, int lockable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 struct acpi_object_list arg_list;
383 union acpi_object arg;
384 acpi_status status = AE_OK;
385
386 /*
387 * TBD: evaluate _PS3?
388 */
389
390 if (lockable) {
391 arg_list.count = 1;
392 arg_list.pointer = &arg;
393 arg.type = ACPI_TYPE_INTEGER;
394 arg.integer.value = 0;
395 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
396 }
397
398 arg_list.count = 1;
399 arg_list.pointer = &arg;
400 arg.type = ACPI_TYPE_INTEGER;
401 arg.integer.value = 1;
402
403 /*
404 * TBD: _EJD support.
405 */
406
407 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
408 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400409 return (-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411
Len Brown4be44fc2005-08-05 00:44:28 -0400412 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415static ssize_t
416acpi_eject_store(struct acpi_device *device, const char *buf, size_t count)
417{
Len Brown4be44fc2005-08-05 00:44:28 -0400418 int result;
419 int ret = count;
420 int islockable;
421 acpi_status status;
422 acpi_handle handle;
423 acpi_object_type type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 if ((!count) || (buf[0] != '1')) {
426 return -EINVAL;
427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428#ifndef FORCE_EJECT
429 if (device->driver == NULL) {
430 ret = -ENODEV;
431 goto err;
432 }
433#endif
434 status = acpi_get_type(device->handle, &type);
Len Brown4be44fc2005-08-05 00:44:28 -0400435 if (ACPI_FAILURE(status) || (!device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 ret = -ENODEV;
437 goto err;
438 }
439
440 islockable = device->flags.lockable;
441 handle = device->handle;
442
443 if (type == ACPI_TYPE_PROCESSOR)
444 result = acpi_bus_trim(device, 0);
445 else
446 result = acpi_bus_trim(device, 1);
447
448 if (!result)
449 result = acpi_eject_operation(handle, islockable);
450
451 if (result) {
452 ret = -EBUSY;
453 }
Len Brown4be44fc2005-08-05 00:44:28 -0400454 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return ret;
456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/* --------------------------------------------------------------------------
459 Performance Management
460 -------------------------------------------------------------------------- */
461
Len Brown4be44fc2005-08-05 00:44:28 -0400462static int acpi_bus_get_perf_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 device->performance.state = ACPI_STATE_UNKNOWN;
465 return 0;
466}
467
468/* --------------------------------------------------------------------------
469 Driver Management
470 -------------------------------------------------------------------------- */
471
472static LIST_HEAD(acpi_bus_drivers);
473static DECLARE_MUTEX(acpi_bus_drivers_lock);
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500476 * acpi_bus_match - match device IDs to driver's supported IDs
477 * @device: the device that we are trying to match to a driver
478 * @driver: driver whose device id table is being checked
479 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 * 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/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500492 * acpi_bus_driver_init - add a device to a driver
493 * @device: the device to add and initialize
494 * @driver: driver for the device
495 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 * Used to initialize a device via its device driver. Called whenever a
497 * driver is bound to a device. Invokes the driver's add() and start() ops.
498 */
499static int
Len Brown4be44fc2005-08-05 00:44:28 -0400500acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Len Brown4be44fc2005-08-05 00:44:28 -0400502 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 ACPI_FUNCTION_TRACE("acpi_bus_driver_init");
505
506 if (!device || !driver)
507 return_VALUE(-EINVAL);
508
509 if (!driver->ops.add)
510 return_VALUE(-ENOSYS);
511
512 result = driver->ops.add(device);
513 if (result) {
514 device->driver = NULL;
515 acpi_driver_data(device) = NULL;
516 return_VALUE(result);
517 }
518
519 device->driver = driver;
520
521 /*
522 * TBD - Configuration Management: Assign resources to device based
523 * upon possible configuration and currently allocated resources.
524 */
525
Len Brown4be44fc2005-08-05 00:44:28 -0400526 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
527 "Driver successfully bound to device\n"));
Rajesh Shah3fb02732005-04-28 00:25:52 -0700528 return_VALUE(0);
529}
530
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400531static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700532{
533 int result = 0;
534 struct acpi_driver *driver;
535
536 ACPI_FUNCTION_TRACE("acpi_start_single_object");
537
538 if (!(driver = device->driver))
539 return_VALUE(0);
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (driver->ops.start) {
542 result = driver->ops.start(device);
543 if (result && driver->ops.remove)
544 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546
Rajesh Shah3fb02732005-04-28 00:25:52 -0700547 return_VALUE(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500550static void acpi_driver_attach(struct acpi_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Len Brown4be44fc2005-08-05 00:44:28 -0400552 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 ACPI_FUNCTION_TRACE("acpi_driver_attach");
555
556 spin_lock(&acpi_device_lock);
557 list_for_each_safe(node, next, &acpi_device_list) {
Len Brown4be44fc2005-08-05 00:44:28 -0400558 struct acpi_device *dev =
559 container_of(node, struct acpi_device, g_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 if (dev->driver || !dev->status.present)
562 continue;
563 spin_unlock(&acpi_device_lock);
564
565 if (!acpi_bus_match(dev, drv)) {
566 if (!acpi_bus_driver_init(dev, drv)) {
Rajesh Shah3fb02732005-04-28 00:25:52 -0700567 acpi_start_single_object(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 atomic_inc(&drv->references);
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
Bjorn Helgaas06ea8e02006-04-27 05:25:00 -0400579static void acpi_driver_detach(struct acpi_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Len Brown4be44fc2005-08-05 00:44:28 -0400581 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 ACPI_FUNCTION_TRACE("acpi_driver_detach");
584
585 spin_lock(&acpi_device_lock);
Len Brown4be44fc2005-08-05 00:44:28 -0400586 list_for_each_safe(node, next, &acpi_device_list) {
587 struct acpi_device *dev =
588 container_of(node, struct acpi_device, g_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 if (dev->driver == drv) {
591 spin_unlock(&acpi_device_lock);
592 if (drv->ops.remove)
Len Brown4be44fc2005-08-05 00:44:28 -0400593 drv->ops.remove(dev, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 spin_lock(&acpi_device_lock);
595 dev->driver = NULL;
596 dev->driver_data = NULL;
597 atomic_dec(&drv->references);
598 }
599 }
600 spin_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500604 * acpi_bus_register_driver - register a driver with the ACPI bus
605 * @driver: driver being registered
606 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500608 * devices that match the driver's criteria and binds. Returns zero for
609 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 */
Len Brown4be44fc2005-08-05 00:44:28 -0400611int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 ACPI_FUNCTION_TRACE("acpi_bus_register_driver");
614
615 if (acpi_disabled)
616 return_VALUE(-ENODEV);
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 spin_lock(&acpi_device_lock);
619 list_add_tail(&driver->node, &acpi_bus_drivers);
620 spin_unlock(&acpi_device_lock);
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500621 acpi_driver_attach(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500623 return_VALUE(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Len Brown4be44fc2005-08-05 00:44:28 -0400626EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500629 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
630 * @driver: driver to unregister
631 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 * Unregisters a driver with the ACPI bus. Searches the namespace for all
633 * devices that match the driver's criteria and unbinds.
634 */
Bjorn Helgaas06ea8e02006-04-27 05:25:00 -0400635void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Bjorn Helgaas06ea8e02006-04-27 05:25:00 -0400637 acpi_driver_detach(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Bjorn Helgaas06ea8e02006-04-27 05:25:00 -0400639 if (!atomic_read(&driver->references)) {
640 spin_lock(&acpi_device_lock);
641 list_del_init(&driver->node);
642 spin_unlock(&acpi_device_lock);
643 }
644 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
Len Brown4be44fc2005-08-05 00:44:28 -0400646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647EXPORT_SYMBOL(acpi_bus_unregister_driver);
648
649/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500650 * acpi_bus_find_driver - check if there is a driver installed for the device
651 * @device: device that we are trying to find a supporting driver for
652 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 * Parses the list of registered drivers looking for a driver applicable for
654 * the specified device.
655 */
Len Brown4be44fc2005-08-05 00:44:28 -0400656static int acpi_bus_find_driver(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Len Brown4be44fc2005-08-05 00:44:28 -0400658 int result = 0;
659 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 ACPI_FUNCTION_TRACE("acpi_bus_find_driver");
662
663 spin_lock(&acpi_device_lock);
Len Brown4be44fc2005-08-05 00:44:28 -0400664 list_for_each_safe(node, next, &acpi_bus_drivers) {
665 struct acpi_driver *driver =
666 container_of(node, struct acpi_driver, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 atomic_inc(&driver->references);
669 spin_unlock(&acpi_device_lock);
670 if (!acpi_bus_match(device, driver)) {
671 result = acpi_bus_driver_init(device, driver);
672 if (!result)
673 goto Done;
674 }
675 atomic_dec(&driver->references);
676 spin_lock(&acpi_device_lock);
677 }
678 spin_unlock(&acpi_device_lock);
679
Len Brown4be44fc2005-08-05 00:44:28 -0400680 Done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return_VALUE(result);
682}
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684/* --------------------------------------------------------------------------
685 Device Enumeration
686 -------------------------------------------------------------------------- */
687
Len Brown4be44fc2005-08-05 00:44:28 -0400688static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Len Brown4be44fc2005-08-05 00:44:28 -0400690 acpi_status status = AE_OK;
691 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 ACPI_FUNCTION_TRACE("acpi_bus_get_flags");
694
695 /* Presence of _STA indicates 'dynamic_status' */
696 status = acpi_get_handle(device->handle, "_STA", &temp);
697 if (ACPI_SUCCESS(status))
698 device->flags.dynamic_status = 1;
699
700 /* Presence of _CID indicates 'compatible_ids' */
701 status = acpi_get_handle(device->handle, "_CID", &temp);
702 if (ACPI_SUCCESS(status))
703 device->flags.compatible_ids = 1;
704
705 /* Presence of _RMV indicates 'removable' */
706 status = acpi_get_handle(device->handle, "_RMV", &temp);
707 if (ACPI_SUCCESS(status))
708 device->flags.removable = 1;
709
710 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
711 status = acpi_get_handle(device->handle, "_EJD", &temp);
712 if (ACPI_SUCCESS(status))
713 device->flags.ejectable = 1;
714 else {
715 status = acpi_get_handle(device->handle, "_EJ0", &temp);
716 if (ACPI_SUCCESS(status))
717 device->flags.ejectable = 1;
718 }
719
720 /* Presence of _LCK indicates 'lockable' */
721 status = acpi_get_handle(device->handle, "_LCK", &temp);
722 if (ACPI_SUCCESS(status))
723 device->flags.lockable = 1;
724
725 /* Presence of _PS0|_PR0 indicates 'power manageable' */
726 status = acpi_get_handle(device->handle, "_PS0", &temp);
727 if (ACPI_FAILURE(status))
728 status = acpi_get_handle(device->handle, "_PR0", &temp);
729 if (ACPI_SUCCESS(status))
730 device->flags.power_manageable = 1;
731
732 /* Presence of _PRW indicates wake capable */
733 status = acpi_get_handle(device->handle, "_PRW", &temp);
734 if (ACPI_SUCCESS(status))
735 device->flags.wake_capable = 1;
736
737 /* TBD: Peformance management */
738
739 return_VALUE(0);
740}
741
Len Brown4be44fc2005-08-05 00:44:28 -0400742static void acpi_device_get_busid(struct acpi_device *device,
743 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Len Brown4be44fc2005-08-05 00:44:28 -0400745 char bus_id[5] = { '?', 0 };
746 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
747 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 /*
750 * Bus ID
751 * ------
752 * The device's Bus ID is simply the object name.
753 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
754 */
755 switch (type) {
756 case ACPI_BUS_TYPE_SYSTEM:
757 strcpy(device->pnp.bus_id, "ACPI");
758 break;
759 case ACPI_BUS_TYPE_POWER_BUTTON:
760 strcpy(device->pnp.bus_id, "PWRF");
761 break;
762 case ACPI_BUS_TYPE_SLEEP_BUTTON:
763 strcpy(device->pnp.bus_id, "SLPF");
764 break;
765 default:
766 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
767 /* Clean up trailing underscores (if any) */
768 for (i = 3; i > 1; i--) {
769 if (bus_id[i] == '_')
770 bus_id[i] = '\0';
771 else
772 break;
773 }
774 strcpy(device->pnp.bus_id, bus_id);
775 break;
776 }
777}
778
Len Brown4be44fc2005-08-05 00:44:28 -0400779static void acpi_device_set_id(struct acpi_device *device,
780 struct acpi_device *parent, acpi_handle handle,
781 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Len Brown4be44fc2005-08-05 00:44:28 -0400783 struct acpi_device_info *info;
784 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
785 char *hid = NULL;
786 char *uid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 struct acpi_compatible_id_list *cid_list = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400788 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 switch (type) {
791 case ACPI_BUS_TYPE_DEVICE:
792 status = acpi_get_object_info(handle, &buffer);
793 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400794 printk("%s: Error reading device info\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return;
796 }
797
798 info = buffer.pointer;
799 if (info->valid & ACPI_VALID_HID)
800 hid = info->hardware_id.value;
801 if (info->valid & ACPI_VALID_UID)
802 uid = info->unique_id.value;
803 if (info->valid & ACPI_VALID_CID)
804 cid_list = &info->compatibility_id;
805 if (info->valid & ACPI_VALID_ADR) {
806 device->pnp.bus_address = info->address;
807 device->flags.bus_address = 1;
808 }
809 break;
810 case ACPI_BUS_TYPE_POWER:
811 hid = ACPI_POWER_HID;
812 break;
813 case ACPI_BUS_TYPE_PROCESSOR:
814 hid = ACPI_PROCESSOR_HID;
815 break;
816 case ACPI_BUS_TYPE_SYSTEM:
817 hid = ACPI_SYSTEM_HID;
818 break;
819 case ACPI_BUS_TYPE_THERMAL:
820 hid = ACPI_THERMAL_HID;
821 break;
822 case ACPI_BUS_TYPE_POWER_BUTTON:
823 hid = ACPI_BUTTON_HID_POWERF;
824 break;
825 case ACPI_BUS_TYPE_SLEEP_BUTTON:
826 hid = ACPI_BUTTON_HID_SLEEPF;
827 break;
828 }
829
830 /*
831 * \_SB
832 * ----
833 * Fix for the system root bus device -- the only root-level device.
834 */
Bob Moorec51a4de2005-11-17 13:07:00 -0500835 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 hid = ACPI_BUS_HID;
837 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
838 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
839 }
840
841 if (hid) {
842 strcpy(device->pnp.hardware_id, hid);
843 device->flags.hardware_id = 1;
844 }
845 if (uid) {
846 strcpy(device->pnp.unique_id, uid);
847 device->flags.unique_id = 1;
848 }
849 if (cid_list) {
850 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
851 if (device->pnp.cid_list)
852 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
853 else
854 printk(KERN_ERR "Memory allocation error\n");
855 }
856
857 acpi_os_free(buffer.pointer);
858}
859
Len Brown4be44fc2005-08-05 00:44:28 -0400860static int acpi_device_set_context(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 acpi_status status = AE_OK;
863 int result = 0;
864 /*
865 * Context
866 * -------
867 * Attach this 'struct acpi_device' to the ACPI object. This makes
868 * resolutions from handle->device very efficient. Note that we need
869 * to be careful with fixed-feature devices as they all attach to the
870 * root object.
871 */
Len Brown4be44fc2005-08-05 00:44:28 -0400872 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
874 status = acpi_attach_data(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400875 acpi_bus_data_handler, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 if (ACPI_FAILURE(status)) {
878 printk("Error attaching device data\n");
879 result = -ENODEV;
880 }
881 }
882 return result;
883}
884
Len Brown4be44fc2005-08-05 00:44:28 -0400885static void acpi_device_get_debug_info(struct acpi_device *device,
886 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888#ifdef CONFIG_ACPI_DEBUG_OUTPUT
Len Brown4be44fc2005-08-05 00:44:28 -0400889 char *type_string = NULL;
890 char name[80] = { '?', '\0' };
891 struct acpi_buffer buffer = { sizeof(name), name };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 switch (type) {
894 case ACPI_BUS_TYPE_DEVICE:
895 type_string = "Device";
896 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
897 break;
898 case ACPI_BUS_TYPE_POWER:
899 type_string = "Power Resource";
900 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
901 break;
902 case ACPI_BUS_TYPE_PROCESSOR:
903 type_string = "Processor";
904 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
905 break;
906 case ACPI_BUS_TYPE_SYSTEM:
907 type_string = "System";
908 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
909 break;
910 case ACPI_BUS_TYPE_THERMAL:
911 type_string = "Thermal Zone";
912 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
913 break;
914 case ACPI_BUS_TYPE_POWER_BUTTON:
915 type_string = "Power Button";
916 sprintf(name, "PWRB");
917 break;
918 case ACPI_BUS_TYPE_SLEEP_BUTTON:
919 type_string = "Sleep Button";
920 sprintf(name, "SLPB");
921 break;
922 }
923
924 printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
Len Brown4be44fc2005-08-05 00:44:28 -0400925#endif /*CONFIG_ACPI_DEBUG_OUTPUT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
Len Brown4be44fc2005-08-05 00:44:28 -0400928static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Len Brown4be44fc2005-08-05 00:44:28 -0400930 int result = 0;
931 struct acpi_driver *driver;
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 ACPI_FUNCTION_TRACE("acpi_bus_remove");
934
935 if (!dev)
936 return_VALUE(-EINVAL);
937
938 driver = dev->driver;
939
940 if ((driver) && (driver->ops.remove)) {
941
942 if (driver->ops.stop) {
943 result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT);
944 if (result)
945 return_VALUE(result);
946 }
947
948 result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT);
949 if (result) {
950 return_VALUE(result);
951 }
952
953 atomic_dec(&dev->driver->references);
954 dev->driver = NULL;
955 acpi_driver_data(dev) = NULL;
956 }
957
958 if (!rmdevice)
959 return_VALUE(0);
960
961 if (dev->flags.bus_address) {
962 if ((dev->parent) && (dev->parent->ops.unbind))
963 dev->parent->ops.unbind(dev);
964 }
Len Brown4be44fc2005-08-05 00:44:28 -0400965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
967
968 return_VALUE(0);
969}
970
Rajesh Shah3fb02732005-04-28 00:25:52 -0700971static int
Len Brown4be44fc2005-08-05 00:44:28 -0400972acpi_add_single_object(struct acpi_device **child,
973 struct acpi_device *parent, acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Len Brown4be44fc2005-08-05 00:44:28 -0400975 int result = 0;
976 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Rajesh Shah3fb02732005-04-28 00:25:52 -0700978 ACPI_FUNCTION_TRACE("acpi_add_single_object");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 if (!child)
981 return_VALUE(-EINVAL);
982
983 device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
984 if (!device) {
985 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Memory allocation error\n"));
986 return_VALUE(-ENOMEM);
987 }
988 memset(device, 0, sizeof(struct acpi_device));
989
990 device->handle = handle;
991 device->parent = parent;
992
Len Brown4be44fc2005-08-05 00:44:28 -0400993 acpi_device_get_busid(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 /*
996 * Flags
997 * -----
998 * Get prior to calling acpi_bus_get_status() so we know whether
999 * or not _STA is present. Note that we only look for object
1000 * handles -- cannot evaluate objects until we know the device is
1001 * present and properly initialized.
1002 */
1003 result = acpi_bus_get_flags(device);
1004 if (result)
1005 goto end;
1006
1007 /*
1008 * Status
1009 * ------
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001010 * See if the device is present. We always assume that non-Device
1011 * and non-Processor objects (e.g. thermal zones, power resources,
1012 * etc.) are present, functioning, etc. (at least when parent object
1013 * is present). Note that _STA has a different meaning for some
1014 * objects (e.g. power resources) so we need to be careful how we use
1015 * it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 */
1017 switch (type) {
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001018 case ACPI_BUS_TYPE_PROCESSOR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 case ACPI_BUS_TYPE_DEVICE:
1020 result = acpi_bus_get_status(device);
1021 if (ACPI_FAILURE(result) || !device->status.present) {
1022 result = -ENOENT;
1023 goto end;
1024 }
1025 break;
1026 default:
1027 STRUCT_TO_INT(device->status) = 0x0F;
1028 break;
1029 }
1030
1031 /*
1032 * Initialize Device
1033 * -----------------
1034 * TBD: Synch with Core's enumeration/initialization process.
1035 */
1036
1037 /*
1038 * Hardware ID, Unique ID, & Bus Address
1039 * -------------------------------------
1040 */
Len Brown4be44fc2005-08-05 00:44:28 -04001041 acpi_device_set_id(device, parent, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 /*
1044 * Power Management
1045 * ----------------
1046 */
1047 if (device->flags.power_manageable) {
1048 result = acpi_bus_get_power_flags(device);
1049 if (result)
1050 goto end;
1051 }
1052
Len Brown4be44fc2005-08-05 00:44:28 -04001053 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 * Wakeup device management
1055 *-----------------------
1056 */
1057 if (device->flags.wake_capable) {
1058 result = acpi_bus_get_wakeup_device_flags(device);
1059 if (result)
1060 goto end;
1061 }
1062
1063 /*
1064 * Performance Management
1065 * ----------------------
1066 */
1067 if (device->flags.performance_manageable) {
1068 result = acpi_bus_get_perf_flags(device);
1069 if (result)
1070 goto end;
1071 }
1072
Len Brown4be44fc2005-08-05 00:44:28 -04001073 if ((result = acpi_device_set_context(device, type)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 goto end;
1075
Len Brown4be44fc2005-08-05 00:44:28 -04001076 acpi_device_get_debug_info(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Len Brown4be44fc2005-08-05 00:44:28 -04001078 acpi_device_register(device, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 /*
1081 * Bind _ADR-Based Devices
1082 * -----------------------
1083 * If there's a a bus address (_ADR) then we utilize the parent's
1084 * 'bind' function (if exists) to bind the ACPI- and natively-
1085 * enumerated device representations.
1086 */
1087 if (device->flags.bus_address) {
1088 if (device->parent && device->parent->ops.bind)
1089 device->parent->ops.bind(device);
1090 }
1091
1092 /*
1093 * Locate & Attach Driver
1094 * ----------------------
1095 * If there's a hardware id (_HID) or compatible ids (_CID) we check
1096 * to see if there's a driver installed for this kind of device. Note
1097 * that drivers can install before or after a device is enumerated.
1098 *
1099 * TBD: Assumes LDM provides driver hot-plug capability.
1100 */
Thomas Renningerbd7ce5b2005-10-03 10:39:00 -07001101 acpi_bus_find_driver(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Len Brown4be44fc2005-08-05 00:44:28 -04001103 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (!result)
1105 *child = device;
1106 else {
Jesper Juhl6044ec82005-11-07 01:01:32 -08001107 kfree(device->pnp.cid_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 kfree(device);
1109 }
1110
1111 return_VALUE(result);
1112}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Len Brown4be44fc2005-08-05 00:44:28 -04001114static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
Len Brown4be44fc2005-08-05 00:44:28 -04001116 acpi_status status = AE_OK;
1117 struct acpi_device *parent = NULL;
1118 struct acpi_device *child = NULL;
1119 acpi_handle phandle = NULL;
1120 acpi_handle chandle = NULL;
1121 acpi_object_type type = 0;
1122 u32 level = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 ACPI_FUNCTION_TRACE("acpi_bus_scan");
1125
1126 if (!start)
1127 return_VALUE(-EINVAL);
1128
1129 parent = start;
1130 phandle = start->handle;
Len Brown4be44fc2005-08-05 00:44:28 -04001131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 /*
1133 * Parse through the ACPI namespace, identify all 'devices', and
1134 * create a new 'struct acpi_device' for each.
1135 */
1136 while ((level > 0) && parent) {
1137
1138 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001139 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 /*
1142 * If this scope is exhausted then move our way back up.
1143 */
1144 if (ACPI_FAILURE(status)) {
1145 level--;
1146 chandle = phandle;
1147 acpi_get_parent(phandle, &phandle);
1148 if (parent->parent)
1149 parent = parent->parent;
1150 continue;
1151 }
1152
1153 status = acpi_get_type(chandle, &type);
1154 if (ACPI_FAILURE(status))
1155 continue;
1156
1157 /*
1158 * If this is a scope object then parse it (depth-first).
1159 */
1160 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1161 level++;
1162 phandle = chandle;
1163 chandle = NULL;
1164 continue;
1165 }
1166
1167 /*
1168 * We're only interested in objects that we consider 'devices'.
1169 */
1170 switch (type) {
1171 case ACPI_TYPE_DEVICE:
1172 type = ACPI_BUS_TYPE_DEVICE;
1173 break;
1174 case ACPI_TYPE_PROCESSOR:
1175 type = ACPI_BUS_TYPE_PROCESSOR;
1176 break;
1177 case ACPI_TYPE_THERMAL:
1178 type = ACPI_BUS_TYPE_THERMAL;
1179 break;
1180 case ACPI_TYPE_POWER:
1181 type = ACPI_BUS_TYPE_POWER;
1182 break;
1183 default:
1184 continue;
1185 }
1186
Rajesh Shah3fb02732005-04-28 00:25:52 -07001187 if (ops->acpi_op_add)
1188 status = acpi_add_single_object(&child, parent,
Len Brown4be44fc2005-08-05 00:44:28 -04001189 chandle, type);
1190 else
Rajesh Shah3fb02732005-04-28 00:25:52 -07001191 status = acpi_bus_get_device(chandle, &child);
1192
Len Brown4be44fc2005-08-05 00:44:28 -04001193 if (ACPI_FAILURE(status))
1194 continue;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001195
1196 if (ops->acpi_op_start) {
1197 status = acpi_start_single_object(child);
1198 if (ACPI_FAILURE(status))
1199 continue;
1200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
1202 /*
1203 * If the device is present, enabled, and functioning then
1204 * parse its scope (depth-first). Note that we need to
1205 * represent absent devices to facilitate PnP notifications
1206 * -- but only the subtree head (not all of its children,
1207 * which will be enumerated when the parent is inserted).
1208 *
1209 * TBD: Need notifications and other detection mechanisms
Len Brown4be44fc2005-08-05 00:44:28 -04001210 * in place before we can fully implement this.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 */
1212 if (child->status.present) {
1213 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1214 NULL, NULL);
1215 if (ACPI_SUCCESS(status)) {
1216 level++;
1217 phandle = chandle;
1218 chandle = NULL;
1219 parent = child;
1220 }
1221 }
1222 }
1223
1224 return_VALUE(0);
1225}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Rajesh Shah3fb02732005-04-28 00:25:52 -07001227int
Len Brown4be44fc2005-08-05 00:44:28 -04001228acpi_bus_add(struct acpi_device **child,
1229 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001230{
1231 int result;
1232 struct acpi_bus_ops ops;
1233
1234 ACPI_FUNCTION_TRACE("acpi_bus_add");
1235
1236 result = acpi_add_single_object(child, parent, handle, type);
1237 if (!result) {
1238 memset(&ops, 0, sizeof(ops));
1239 ops.acpi_op_add = 1;
1240 result = acpi_bus_scan(*child, &ops);
1241 }
1242 return_VALUE(result);
1243}
Len Brown4be44fc2005-08-05 00:44:28 -04001244
Rajesh Shah3fb02732005-04-28 00:25:52 -07001245EXPORT_SYMBOL(acpi_bus_add);
1246
Len Brown4be44fc2005-08-05 00:44:28 -04001247int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001248{
1249 int result;
1250 struct acpi_bus_ops ops;
1251
1252 ACPI_FUNCTION_TRACE("acpi_bus_start");
1253
1254 if (!device)
1255 return_VALUE(-EINVAL);
1256
1257 result = acpi_start_single_object(device);
1258 if (!result) {
1259 memset(&ops, 0, sizeof(ops));
1260 ops.acpi_op_start = 1;
1261 result = acpi_bus_scan(device, &ops);
1262 }
1263 return_VALUE(result);
1264}
Len Brown4be44fc2005-08-05 00:44:28 -04001265
Rajesh Shah3fb02732005-04-28 00:25:52 -07001266EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Kristen Accardiceaba662006-02-23 17:56:01 -08001268int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Len Brown4be44fc2005-08-05 00:44:28 -04001270 acpi_status status;
1271 struct acpi_device *parent, *child;
1272 acpi_handle phandle, chandle;
1273 acpi_object_type type;
1274 u32 level = 1;
1275 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Len Brown4be44fc2005-08-05 00:44:28 -04001277 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 phandle = start->handle;
1279 child = chandle = NULL;
1280
1281 while ((level > 0) && parent && (!err)) {
1282 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001283 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 /*
1286 * If this scope is exhausted then move our way back up.
1287 */
1288 if (ACPI_FAILURE(status)) {
1289 level--;
1290 chandle = phandle;
1291 acpi_get_parent(phandle, &phandle);
1292 child = parent;
1293 parent = parent->parent;
1294
1295 if (level == 0)
1296 err = acpi_bus_remove(child, rmdevice);
1297 else
1298 err = acpi_bus_remove(child, 1);
1299
1300 continue;
1301 }
1302
1303 status = acpi_get_type(chandle, &type);
1304 if (ACPI_FAILURE(status)) {
1305 continue;
1306 }
1307 /*
1308 * If there is a device corresponding to chandle then
1309 * parse it (depth-first).
1310 */
1311 if (acpi_bus_get_device(chandle, &child) == 0) {
1312 level++;
1313 phandle = chandle;
1314 chandle = NULL;
1315 parent = child;
1316 }
1317 continue;
1318 }
1319 return err;
1320}
Kristen Accardiceaba662006-02-23 17:56:01 -08001321EXPORT_SYMBOL_GPL(acpi_bus_trim);
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Len Brown4be44fc2005-08-05 00:44:28 -04001324static int acpi_bus_scan_fixed(struct acpi_device *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Len Brown4be44fc2005-08-05 00:44:28 -04001326 int result = 0;
1327 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 ACPI_FUNCTION_TRACE("acpi_bus_scan_fixed");
1330
1331 if (!root)
1332 return_VALUE(-ENODEV);
1333
1334 /*
1335 * Enumerate all fixed-feature devices.
1336 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001337 if (acpi_fadt.pwr_button == 0) {
1338 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001339 NULL,
1340 ACPI_BUS_TYPE_POWER_BUTTON);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001341 if (!result)
1342 result = acpi_start_single_object(device);
1343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Rajesh Shah3fb02732005-04-28 00:25:52 -07001345 if (acpi_fadt.sleep_button == 0) {
1346 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001347 NULL,
1348 ACPI_BUS_TYPE_SLEEP_BUTTON);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001349 if (!result)
1350 result = acpi_start_single_object(device);
1351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 return_VALUE(result);
1354}
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356static int __init acpi_scan_init(void)
1357{
1358 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001359 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 ACPI_FUNCTION_TRACE("acpi_scan_init");
1362
1363 if (acpi_disabled)
1364 return_VALUE(0);
1365
1366 kset_register(&acpi_namespace_kset);
1367
1368 /*
1369 * Create the root device in the bus's device tree
1370 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001371 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
Len Brown4be44fc2005-08-05 00:44:28 -04001372 ACPI_BUS_TYPE_SYSTEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 if (result)
1374 goto Done;
1375
Rajesh Shah3fb02732005-04-28 00:25:52 -07001376 result = acpi_start_single_object(acpi_root);
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 /*
1379 * Enumerate devices in the ACPI namespace.
1380 */
1381 result = acpi_bus_scan_fixed(acpi_root);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001382 if (!result) {
1383 memset(&ops, 0, sizeof(ops));
1384 ops.acpi_op_add = 1;
1385 ops.acpi_op_start = 1;
1386 result = acpi_bus_scan(acpi_root, &ops);
1387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 if (result)
1390 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1391
Len Brown4be44fc2005-08-05 00:44:28 -04001392 Done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return_VALUE(result);
1394}
1395
1396subsys_initcall(acpi_scan_init);