blob: 95d5bc2acadd3179c73ce8a6e04c84145697ad83 [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
Len Brown4be44fc2005-08-05 00:44:28 -0400145static int 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);
161 return 0;
162}
163
Len Brown4be44fc2005-08-05 00:44:28 -0400164void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 ACPI_FUNCTION_TRACE("acpi_bus_data_handler");
167
168 /* TBD */
169
170 return_VOID;
171}
172
Len Brown4be44fc2005-08-05 00:44:28 -0400173static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Len Brown4be44fc2005-08-05 00:44:28 -0400175 acpi_status status = 0;
176 acpi_handle handle = NULL;
177 u32 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 ACPI_FUNCTION_TRACE("acpi_bus_get_power_flags");
180
181 /*
182 * Power Management Flags
183 */
184 status = acpi_get_handle(device->handle, "_PSC", &handle);
185 if (ACPI_SUCCESS(status))
186 device->power.flags.explicit_get = 1;
187 status = acpi_get_handle(device->handle, "_IRC", &handle);
188 if (ACPI_SUCCESS(status))
189 device->power.flags.inrush_current = 1;
190
191 /*
192 * Enumerate supported power management states
193 */
194 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
195 struct acpi_device_power_state *ps = &device->power.states[i];
Len Brown4be44fc2005-08-05 00:44:28 -0400196 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 /* Evaluate "_PRx" to se if power resources are referenced */
199 acpi_evaluate_reference(device->handle, object_name, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400200 &ps->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (ps->resources.count) {
202 device->power.flags.power_resources = 1;
203 ps->flags.valid = 1;
204 }
205
206 /* Evaluate "_PSx" to see if we can do explicit sets */
207 object_name[2] = 'S';
208 status = acpi_get_handle(device->handle, object_name, &handle);
209 if (ACPI_SUCCESS(status)) {
210 ps->flags.explicit_set = 1;
211 ps->flags.valid = 1;
212 }
213
214 /* State is valid if we have some power control */
215 if (ps->resources.count || ps->flags.explicit_set)
216 ps->flags.valid = 1;
217
Len Brown4be44fc2005-08-05 00:44:28 -0400218 ps->power = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 ps->latency = -1; /* Unknown - driver assigned */
220 }
221
222 /* Set defaults for D0 and D3 states (always valid) */
223 device->power.states[ACPI_STATE_D0].flags.valid = 1;
224 device->power.states[ACPI_STATE_D0].power = 100;
225 device->power.states[ACPI_STATE_D3].flags.valid = 1;
226 device->power.states[ACPI_STATE_D3].power = 0;
227
228 /* TBD: System wake support and resource requirements. */
229
230 device->power.state = ACPI_STATE_UNKNOWN;
231
232 return_VALUE(0);
233}
234
Len Brown4be44fc2005-08-05 00:44:28 -0400235int acpi_match_ids(struct acpi_device *device, char *ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 int error = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400238 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 if (device->flags.hardware_id)
241 if (strstr(ids, device->pnp.hardware_id))
242 goto Done;
243
244 if (device->flags.compatible_ids) {
245 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
246 int i;
247
248 /* compare multiple _CID entries against driver ids */
Len Brown4be44fc2005-08-05 00:44:28 -0400249 for (i = 0; i < cid_list->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (strstr(ids, cid_list->id[i].value))
251 goto Done;
252 }
253 }
254 error = -ENOENT;
255
Len Brown4be44fc2005-08-05 00:44:28 -0400256 Done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (buffer.pointer)
258 acpi_os_free(buffer.pointer);
259 return error;
260}
261
262static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400263acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
264 union acpi_object *package)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Len Brown4be44fc2005-08-05 00:44:28 -0400266 int i = 0;
267 union acpi_object *element = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 if (!device || !package || (package->package.count < 2))
270 return AE_BAD_PARAMETER;
271
272 element = &(package->package.elements[0]);
273 if (!element)
274 return AE_BAD_PARAMETER;
275 if (element->type == ACPI_TYPE_PACKAGE) {
276 if ((element->package.count < 2) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400277 (element->package.elements[0].type !=
278 ACPI_TYPE_LOCAL_REFERENCE)
279 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return AE_BAD_DATA;
Len Brown4be44fc2005-08-05 00:44:28 -0400281 device->wakeup.gpe_device =
282 element->package.elements[0].reference.handle;
283 device->wakeup.gpe_number =
284 (u32) element->package.elements[1].integer.value;
285 } else if (element->type == ACPI_TYPE_INTEGER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 device->wakeup.gpe_number = element->integer.value;
Len Brown4be44fc2005-08-05 00:44:28 -0400287 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return AE_BAD_DATA;
289
290 element = &(package->package.elements[1]);
291 if (element->type != ACPI_TYPE_INTEGER) {
292 return AE_BAD_DATA;
293 }
294 device->wakeup.sleep_state = element->integer.value;
295
296 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
297 return AE_NO_MEMORY;
298 }
299 device->wakeup.resources.count = package->package.count - 2;
Len Brown4be44fc2005-08-05 00:44:28 -0400300 for (i = 0; i < device->wakeup.resources.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 element = &(package->package.elements[i + 2]);
Len Brown4be44fc2005-08-05 00:44:28 -0400302 if (element->type != ACPI_TYPE_ANY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return AE_BAD_DATA;
304 }
305
306 device->wakeup.resources.handles[i] = element->reference.handle;
307 }
308
309 return AE_OK;
310}
311
Len Brown4be44fc2005-08-05 00:44:28 -0400312static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Len Brown4be44fc2005-08-05 00:44:28 -0400314 acpi_status status = 0;
315 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
316 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 ACPI_FUNCTION_TRACE("acpi_bus_get_wakeup_flags");
319
320 /* _PRW */
321 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
322 if (ACPI_FAILURE(status)) {
323 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PRW\n"));
324 goto end;
325 }
326
Len Brown4be44fc2005-08-05 00:44:28 -0400327 package = (union acpi_object *)buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 status = acpi_bus_extract_wakeup_device_power_package(device, package);
329 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400330 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
331 "Error extracting _PRW package\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 goto end;
333 }
334
335 acpi_os_free(buffer.pointer);
336
337 device->wakeup.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400338 /* Power button, Lid switch always enable wakeup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
340 device->wakeup.flags.run_wake = 1;
341
Len Brown4be44fc2005-08-05 00:44:28 -0400342 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (ACPI_FAILURE(status))
344 device->flags.wake_capable = 0;
345 return_VALUE(0);
346}
347
348/* --------------------------------------------------------------------------
Kay Sievers312c0042005-11-16 09:00:00 +0100349 ACPI sysfs device file support
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 -------------------------------------------------------------------------- */
Len Brown4be44fc2005-08-05 00:44:28 -0400351static ssize_t acpi_eject_store(struct acpi_device *device,
352 const char *buf, size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354#define ACPI_DEVICE_ATTR(_name,_mode,_show,_store) \
355static struct acpi_device_attribute acpi_device_attr_##_name = \
356 __ATTR(_name, _mode, _show, _store)
357
358ACPI_DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
359
360/**
361 * setup_sys_fs_device_files - sets up the device files under device namespace
Martin Waitz67be2dd2005-05-01 08:59:26 -0700362 * @dev: acpi_device object
363 * @func: function pointer to create or destroy the device file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 */
365static void
Len Brown4be44fc2005-08-05 00:44:28 -0400366setup_sys_fs_device_files(struct acpi_device *dev,
367 acpi_device_sysfs_files * func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Len Brown4be44fc2005-08-05 00:44:28 -0400369 acpi_status status;
370 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 /*
373 * If device has _EJ0, 'eject' file is created that is used to trigger
374 * hot-removal function from userland.
375 */
376 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
377 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400378 (*(func)) (&dev->kobj, &acpi_device_attr_eject.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Len Brown4be44fc2005-08-05 00:44:28 -0400381static int acpi_eject_operation(acpi_handle handle, int lockable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 struct acpi_object_list arg_list;
384 union acpi_object arg;
385 acpi_status status = AE_OK;
386
387 /*
388 * TBD: evaluate _PS3?
389 */
390
391 if (lockable) {
392 arg_list.count = 1;
393 arg_list.pointer = &arg;
394 arg.type = ACPI_TYPE_INTEGER;
395 arg.integer.value = 0;
396 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
397 }
398
399 arg_list.count = 1;
400 arg_list.pointer = &arg;
401 arg.type = ACPI_TYPE_INTEGER;
402 arg.integer.value = 1;
403
404 /*
405 * TBD: _EJD support.
406 */
407
408 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
409 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400410 return (-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412
Len Brown4be44fc2005-08-05 00:44:28 -0400413 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416static ssize_t
417acpi_eject_store(struct acpi_device *device, const char *buf, size_t count)
418{
Len Brown4be44fc2005-08-05 00:44:28 -0400419 int result;
420 int ret = count;
421 int islockable;
422 acpi_status status;
423 acpi_handle handle;
424 acpi_object_type type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 if ((!count) || (buf[0] != '1')) {
427 return -EINVAL;
428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429#ifndef FORCE_EJECT
430 if (device->driver == NULL) {
431 ret = -ENODEV;
432 goto err;
433 }
434#endif
435 status = acpi_get_type(device->handle, &type);
Len Brown4be44fc2005-08-05 00:44:28 -0400436 if (ACPI_FAILURE(status) || (!device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 ret = -ENODEV;
438 goto err;
439 }
440
441 islockable = device->flags.lockable;
442 handle = device->handle;
443
Ashok Rajeefa27a2006-03-28 17:04:00 -0500444 result = acpi_bus_trim(device, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 if (!result)
447 result = acpi_eject_operation(handle, islockable);
448
449 if (result) {
450 ret = -EBUSY;
451 }
Len Brown4be44fc2005-08-05 00:44:28 -0400452 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return ret;
454}
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456/* --------------------------------------------------------------------------
457 Performance Management
458 -------------------------------------------------------------------------- */
459
Len Brown4be44fc2005-08-05 00:44:28 -0400460static int acpi_bus_get_perf_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 device->performance.state = ACPI_STATE_UNKNOWN;
463 return 0;
464}
465
466/* --------------------------------------------------------------------------
467 Driver Management
468 -------------------------------------------------------------------------- */
469
470static LIST_HEAD(acpi_bus_drivers);
471static DECLARE_MUTEX(acpi_bus_drivers_lock);
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500474 * acpi_bus_match - match device IDs to driver's supported IDs
475 * @device: the device that we are trying to match to a driver
476 * @driver: driver whose device id table is being checked
477 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it
479 * matches the specified driver's criteria.
480 */
481static int
Len Brown4be44fc2005-08-05 00:44:28 -0400482acpi_bus_match(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 if (driver && driver->ops.match)
485 return driver->ops.match(device, driver);
486 return acpi_match_ids(device, driver->ids);
487}
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500490 * acpi_bus_driver_init - add a device to a driver
491 * @device: the device to add and initialize
492 * @driver: driver for the device
493 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 * 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/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500606 * acpi_bus_register_driver - register a driver with the ACPI bus
607 * @driver: driver being registered
608 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 * Registers a driver with the ACPI bus. Searches the namespace for all
610 * devices that match the driver's criteria and binds. Returns the
611 * number of devices that were claimed by the driver, or a negative
612 * error status for failure.
613 */
Len Brown4be44fc2005-08-05 00:44:28 -0400614int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 int count;
617
618 ACPI_FUNCTION_TRACE("acpi_bus_register_driver");
619
620 if (acpi_disabled)
621 return_VALUE(-ENODEV);
622
623 if (!driver)
624 return_VALUE(-EINVAL);
625
626 spin_lock(&acpi_device_lock);
627 list_add_tail(&driver->node, &acpi_bus_drivers);
628 spin_unlock(&acpi_device_lock);
629 count = acpi_driver_attach(driver);
630
631 return_VALUE(count);
632}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Len Brown4be44fc2005-08-05 00:44:28 -0400634EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500637 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
638 * @driver: driver to unregister
639 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 * Unregisters a driver with the ACPI bus. Searches the namespace for all
641 * devices that match the driver's criteria and unbinds.
642 */
Len Brown4be44fc2005-08-05 00:44:28 -0400643int acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 int error = 0;
646
647 ACPI_FUNCTION_TRACE("acpi_bus_unregister_driver");
648
649 if (driver) {
650 acpi_driver_detach(driver);
651
652 if (!atomic_read(&driver->references)) {
653 spin_lock(&acpi_device_lock);
654 list_del_init(&driver->node);
655 spin_unlock(&acpi_device_lock);
Len Brown4be44fc2005-08-05 00:44:28 -0400656 }
657 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 error = -EINVAL;
659 return_VALUE(error);
660}
Len Brown4be44fc2005-08-05 00:44:28 -0400661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662EXPORT_SYMBOL(acpi_bus_unregister_driver);
663
664/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500665 * acpi_bus_find_driver - check if there is a driver installed for the device
666 * @device: device that we are trying to find a supporting driver for
667 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 * Parses the list of registered drivers looking for a driver applicable for
669 * the specified device.
670 */
Len Brown4be44fc2005-08-05 00:44:28 -0400671static int acpi_bus_find_driver(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Len Brown4be44fc2005-08-05 00:44:28 -0400673 int result = 0;
674 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 ACPI_FUNCTION_TRACE("acpi_bus_find_driver");
677
678 spin_lock(&acpi_device_lock);
Len Brown4be44fc2005-08-05 00:44:28 -0400679 list_for_each_safe(node, next, &acpi_bus_drivers) {
680 struct acpi_driver *driver =
681 container_of(node, struct acpi_driver, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 atomic_inc(&driver->references);
684 spin_unlock(&acpi_device_lock);
685 if (!acpi_bus_match(device, driver)) {
686 result = acpi_bus_driver_init(device, driver);
687 if (!result)
688 goto Done;
689 }
690 atomic_dec(&driver->references);
691 spin_lock(&acpi_device_lock);
692 }
693 spin_unlock(&acpi_device_lock);
694
Len Brown4be44fc2005-08-05 00:44:28 -0400695 Done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return_VALUE(result);
697}
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699/* --------------------------------------------------------------------------
700 Device Enumeration
701 -------------------------------------------------------------------------- */
702
Len Brown4be44fc2005-08-05 00:44:28 -0400703static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Len Brown4be44fc2005-08-05 00:44:28 -0400705 acpi_status status = AE_OK;
706 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 ACPI_FUNCTION_TRACE("acpi_bus_get_flags");
709
710 /* Presence of _STA indicates 'dynamic_status' */
711 status = acpi_get_handle(device->handle, "_STA", &temp);
712 if (ACPI_SUCCESS(status))
713 device->flags.dynamic_status = 1;
714
715 /* Presence of _CID indicates 'compatible_ids' */
716 status = acpi_get_handle(device->handle, "_CID", &temp);
717 if (ACPI_SUCCESS(status))
718 device->flags.compatible_ids = 1;
719
720 /* Presence of _RMV indicates 'removable' */
721 status = acpi_get_handle(device->handle, "_RMV", &temp);
722 if (ACPI_SUCCESS(status))
723 device->flags.removable = 1;
724
725 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
726 status = acpi_get_handle(device->handle, "_EJD", &temp);
727 if (ACPI_SUCCESS(status))
728 device->flags.ejectable = 1;
729 else {
730 status = acpi_get_handle(device->handle, "_EJ0", &temp);
731 if (ACPI_SUCCESS(status))
732 device->flags.ejectable = 1;
733 }
734
735 /* Presence of _LCK indicates 'lockable' */
736 status = acpi_get_handle(device->handle, "_LCK", &temp);
737 if (ACPI_SUCCESS(status))
738 device->flags.lockable = 1;
739
740 /* Presence of _PS0|_PR0 indicates 'power manageable' */
741 status = acpi_get_handle(device->handle, "_PS0", &temp);
742 if (ACPI_FAILURE(status))
743 status = acpi_get_handle(device->handle, "_PR0", &temp);
744 if (ACPI_SUCCESS(status))
745 device->flags.power_manageable = 1;
746
747 /* Presence of _PRW indicates wake capable */
748 status = acpi_get_handle(device->handle, "_PRW", &temp);
749 if (ACPI_SUCCESS(status))
750 device->flags.wake_capable = 1;
751
752 /* TBD: Peformance management */
753
754 return_VALUE(0);
755}
756
Len Brown4be44fc2005-08-05 00:44:28 -0400757static void acpi_device_get_busid(struct acpi_device *device,
758 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Len Brown4be44fc2005-08-05 00:44:28 -0400760 char bus_id[5] = { '?', 0 };
761 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
762 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 /*
765 * Bus ID
766 * ------
767 * The device's Bus ID is simply the object name.
768 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
769 */
770 switch (type) {
771 case ACPI_BUS_TYPE_SYSTEM:
772 strcpy(device->pnp.bus_id, "ACPI");
773 break;
774 case ACPI_BUS_TYPE_POWER_BUTTON:
775 strcpy(device->pnp.bus_id, "PWRF");
776 break;
777 case ACPI_BUS_TYPE_SLEEP_BUTTON:
778 strcpy(device->pnp.bus_id, "SLPF");
779 break;
780 default:
781 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
782 /* Clean up trailing underscores (if any) */
783 for (i = 3; i > 1; i--) {
784 if (bus_id[i] == '_')
785 bus_id[i] = '\0';
786 else
787 break;
788 }
789 strcpy(device->pnp.bus_id, bus_id);
790 break;
791 }
792}
793
Len Brown4be44fc2005-08-05 00:44:28 -0400794static void acpi_device_set_id(struct acpi_device *device,
795 struct acpi_device *parent, acpi_handle handle,
796 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Len Brown4be44fc2005-08-05 00:44:28 -0400798 struct acpi_device_info *info;
799 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
800 char *hid = NULL;
801 char *uid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 struct acpi_compatible_id_list *cid_list = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400803 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 switch (type) {
806 case ACPI_BUS_TYPE_DEVICE:
807 status = acpi_get_object_info(handle, &buffer);
808 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400809 printk("%s: Error reading device info\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return;
811 }
812
813 info = buffer.pointer;
814 if (info->valid & ACPI_VALID_HID)
815 hid = info->hardware_id.value;
816 if (info->valid & ACPI_VALID_UID)
817 uid = info->unique_id.value;
818 if (info->valid & ACPI_VALID_CID)
819 cid_list = &info->compatibility_id;
820 if (info->valid & ACPI_VALID_ADR) {
821 device->pnp.bus_address = info->address;
822 device->flags.bus_address = 1;
823 }
824 break;
825 case ACPI_BUS_TYPE_POWER:
826 hid = ACPI_POWER_HID;
827 break;
828 case ACPI_BUS_TYPE_PROCESSOR:
829 hid = ACPI_PROCESSOR_HID;
830 break;
831 case ACPI_BUS_TYPE_SYSTEM:
832 hid = ACPI_SYSTEM_HID;
833 break;
834 case ACPI_BUS_TYPE_THERMAL:
835 hid = ACPI_THERMAL_HID;
836 break;
837 case ACPI_BUS_TYPE_POWER_BUTTON:
838 hid = ACPI_BUTTON_HID_POWERF;
839 break;
840 case ACPI_BUS_TYPE_SLEEP_BUTTON:
841 hid = ACPI_BUTTON_HID_SLEEPF;
842 break;
843 }
844
845 /*
846 * \_SB
847 * ----
848 * Fix for the system root bus device -- the only root-level device.
849 */
Bob Moorec51a4de2005-11-17 13:07:00 -0500850 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 hid = ACPI_BUS_HID;
852 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
853 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
854 }
855
856 if (hid) {
857 strcpy(device->pnp.hardware_id, hid);
858 device->flags.hardware_id = 1;
859 }
860 if (uid) {
861 strcpy(device->pnp.unique_id, uid);
862 device->flags.unique_id = 1;
863 }
864 if (cid_list) {
865 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
866 if (device->pnp.cid_list)
867 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
868 else
869 printk(KERN_ERR "Memory allocation error\n");
870 }
871
872 acpi_os_free(buffer.pointer);
873}
874
Len Brown4be44fc2005-08-05 00:44:28 -0400875static int acpi_device_set_context(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
877 acpi_status status = AE_OK;
878 int result = 0;
879 /*
880 * Context
881 * -------
882 * Attach this 'struct acpi_device' to the ACPI object. This makes
883 * resolutions from handle->device very efficient. Note that we need
884 * to be careful with fixed-feature devices as they all attach to the
885 * root object.
886 */
Len Brown4be44fc2005-08-05 00:44:28 -0400887 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
889 status = acpi_attach_data(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400890 acpi_bus_data_handler, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 if (ACPI_FAILURE(status)) {
893 printk("Error attaching device data\n");
894 result = -ENODEV;
895 }
896 }
897 return result;
898}
899
Len Brown4be44fc2005-08-05 00:44:28 -0400900static void acpi_device_get_debug_info(struct acpi_device *device,
901 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903#ifdef CONFIG_ACPI_DEBUG_OUTPUT
Len Brown4be44fc2005-08-05 00:44:28 -0400904 char *type_string = NULL;
905 char name[80] = { '?', '\0' };
906 struct acpi_buffer buffer = { sizeof(name), name };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 switch (type) {
909 case ACPI_BUS_TYPE_DEVICE:
910 type_string = "Device";
911 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
912 break;
913 case ACPI_BUS_TYPE_POWER:
914 type_string = "Power Resource";
915 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
916 break;
917 case ACPI_BUS_TYPE_PROCESSOR:
918 type_string = "Processor";
919 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
920 break;
921 case ACPI_BUS_TYPE_SYSTEM:
922 type_string = "System";
923 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
924 break;
925 case ACPI_BUS_TYPE_THERMAL:
926 type_string = "Thermal Zone";
927 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
928 break;
929 case ACPI_BUS_TYPE_POWER_BUTTON:
930 type_string = "Power Button";
931 sprintf(name, "PWRB");
932 break;
933 case ACPI_BUS_TYPE_SLEEP_BUTTON:
934 type_string = "Sleep Button";
935 sprintf(name, "SLPB");
936 break;
937 }
938
939 printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
Len Brown4be44fc2005-08-05 00:44:28 -0400940#endif /*CONFIG_ACPI_DEBUG_OUTPUT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
Len Brown4be44fc2005-08-05 00:44:28 -0400943static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Len Brown4be44fc2005-08-05 00:44:28 -0400945 int result = 0;
946 struct acpi_driver *driver;
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 ACPI_FUNCTION_TRACE("acpi_bus_remove");
949
950 if (!dev)
951 return_VALUE(-EINVAL);
952
953 driver = dev->driver;
954
955 if ((driver) && (driver->ops.remove)) {
956
957 if (driver->ops.stop) {
958 result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT);
959 if (result)
960 return_VALUE(result);
961 }
962
963 result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT);
964 if (result) {
965 return_VALUE(result);
966 }
967
968 atomic_dec(&dev->driver->references);
969 dev->driver = NULL;
970 acpi_driver_data(dev) = NULL;
971 }
972
973 if (!rmdevice)
974 return_VALUE(0);
975
976 if (dev->flags.bus_address) {
977 if ((dev->parent) && (dev->parent->ops.unbind))
978 dev->parent->ops.unbind(dev);
979 }
Len Brown4be44fc2005-08-05 00:44:28 -0400980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
982
983 return_VALUE(0);
984}
985
Rajesh Shah3fb02732005-04-28 00:25:52 -0700986static int
Len Brown4be44fc2005-08-05 00:44:28 -0400987acpi_add_single_object(struct acpi_device **child,
988 struct acpi_device *parent, acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Len Brown4be44fc2005-08-05 00:44:28 -0400990 int result = 0;
991 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Rajesh Shah3fb02732005-04-28 00:25:52 -0700993 ACPI_FUNCTION_TRACE("acpi_add_single_object");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 if (!child)
996 return_VALUE(-EINVAL);
997
998 device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
999 if (!device) {
1000 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Memory allocation error\n"));
1001 return_VALUE(-ENOMEM);
1002 }
1003 memset(device, 0, sizeof(struct acpi_device));
1004
1005 device->handle = handle;
1006 device->parent = parent;
1007
Len Brown4be44fc2005-08-05 00:44:28 -04001008 acpi_device_get_busid(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 /*
1011 * Flags
1012 * -----
1013 * Get prior to calling acpi_bus_get_status() so we know whether
1014 * or not _STA is present. Note that we only look for object
1015 * handles -- cannot evaluate objects until we know the device is
1016 * present and properly initialized.
1017 */
1018 result = acpi_bus_get_flags(device);
1019 if (result)
1020 goto end;
1021
1022 /*
1023 * Status
1024 * ------
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001025 * See if the device is present. We always assume that non-Device
1026 * and non-Processor objects (e.g. thermal zones, power resources,
1027 * etc.) are present, functioning, etc. (at least when parent object
1028 * is present). Note that _STA has a different meaning for some
1029 * objects (e.g. power resources) so we need to be careful how we use
1030 * it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 */
1032 switch (type) {
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001033 case ACPI_BUS_TYPE_PROCESSOR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 case ACPI_BUS_TYPE_DEVICE:
1035 result = acpi_bus_get_status(device);
1036 if (ACPI_FAILURE(result) || !device->status.present) {
1037 result = -ENOENT;
1038 goto end;
1039 }
1040 break;
1041 default:
1042 STRUCT_TO_INT(device->status) = 0x0F;
1043 break;
1044 }
1045
1046 /*
1047 * Initialize Device
1048 * -----------------
1049 * TBD: Synch with Core's enumeration/initialization process.
1050 */
1051
1052 /*
1053 * Hardware ID, Unique ID, & Bus Address
1054 * -------------------------------------
1055 */
Len Brown4be44fc2005-08-05 00:44:28 -04001056 acpi_device_set_id(device, parent, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 /*
1059 * Power Management
1060 * ----------------
1061 */
1062 if (device->flags.power_manageable) {
1063 result = acpi_bus_get_power_flags(device);
1064 if (result)
1065 goto end;
1066 }
1067
Len Brown4be44fc2005-08-05 00:44:28 -04001068 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 * Wakeup device management
1070 *-----------------------
1071 */
1072 if (device->flags.wake_capable) {
1073 result = acpi_bus_get_wakeup_device_flags(device);
1074 if (result)
1075 goto end;
1076 }
1077
1078 /*
1079 * Performance Management
1080 * ----------------------
1081 */
1082 if (device->flags.performance_manageable) {
1083 result = acpi_bus_get_perf_flags(device);
1084 if (result)
1085 goto end;
1086 }
1087
Len Brown4be44fc2005-08-05 00:44:28 -04001088 if ((result = acpi_device_set_context(device, type)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 goto end;
1090
Len Brown4be44fc2005-08-05 00:44:28 -04001091 acpi_device_get_debug_info(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Len Brown4be44fc2005-08-05 00:44:28 -04001093 acpi_device_register(device, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 /*
1096 * Bind _ADR-Based Devices
1097 * -----------------------
1098 * If there's a a bus address (_ADR) then we utilize the parent's
1099 * 'bind' function (if exists) to bind the ACPI- and natively-
1100 * enumerated device representations.
1101 */
1102 if (device->flags.bus_address) {
1103 if (device->parent && device->parent->ops.bind)
1104 device->parent->ops.bind(device);
1105 }
1106
1107 /*
1108 * Locate & Attach Driver
1109 * ----------------------
1110 * If there's a hardware id (_HID) or compatible ids (_CID) we check
1111 * to see if there's a driver installed for this kind of device. Note
1112 * that drivers can install before or after a device is enumerated.
1113 *
1114 * TBD: Assumes LDM provides driver hot-plug capability.
1115 */
Thomas Renningerbd7ce5b2005-10-03 10:39:00 -07001116 acpi_bus_find_driver(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Len Brown4be44fc2005-08-05 00:44:28 -04001118 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (!result)
1120 *child = device;
1121 else {
Jesper Juhl6044ec82005-11-07 01:01:32 -08001122 kfree(device->pnp.cid_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 kfree(device);
1124 }
1125
1126 return_VALUE(result);
1127}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Len Brown4be44fc2005-08-05 00:44:28 -04001129static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Len Brown4be44fc2005-08-05 00:44:28 -04001131 acpi_status status = AE_OK;
1132 struct acpi_device *parent = NULL;
1133 struct acpi_device *child = NULL;
1134 acpi_handle phandle = NULL;
1135 acpi_handle chandle = NULL;
1136 acpi_object_type type = 0;
1137 u32 level = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 ACPI_FUNCTION_TRACE("acpi_bus_scan");
1140
1141 if (!start)
1142 return_VALUE(-EINVAL);
1143
1144 parent = start;
1145 phandle = start->handle;
Len Brown4be44fc2005-08-05 00:44:28 -04001146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 /*
1148 * Parse through the ACPI namespace, identify all 'devices', and
1149 * create a new 'struct acpi_device' for each.
1150 */
1151 while ((level > 0) && parent) {
1152
1153 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001154 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 /*
1157 * If this scope is exhausted then move our way back up.
1158 */
1159 if (ACPI_FAILURE(status)) {
1160 level--;
1161 chandle = phandle;
1162 acpi_get_parent(phandle, &phandle);
1163 if (parent->parent)
1164 parent = parent->parent;
1165 continue;
1166 }
1167
1168 status = acpi_get_type(chandle, &type);
1169 if (ACPI_FAILURE(status))
1170 continue;
1171
1172 /*
1173 * If this is a scope object then parse it (depth-first).
1174 */
1175 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1176 level++;
1177 phandle = chandle;
1178 chandle = NULL;
1179 continue;
1180 }
1181
1182 /*
1183 * We're only interested in objects that we consider 'devices'.
1184 */
1185 switch (type) {
1186 case ACPI_TYPE_DEVICE:
1187 type = ACPI_BUS_TYPE_DEVICE;
1188 break;
1189 case ACPI_TYPE_PROCESSOR:
1190 type = ACPI_BUS_TYPE_PROCESSOR;
1191 break;
1192 case ACPI_TYPE_THERMAL:
1193 type = ACPI_BUS_TYPE_THERMAL;
1194 break;
1195 case ACPI_TYPE_POWER:
1196 type = ACPI_BUS_TYPE_POWER;
1197 break;
1198 default:
1199 continue;
1200 }
1201
Rajesh Shah3fb02732005-04-28 00:25:52 -07001202 if (ops->acpi_op_add)
1203 status = acpi_add_single_object(&child, parent,
Len Brown4be44fc2005-08-05 00:44:28 -04001204 chandle, type);
1205 else
Rajesh Shah3fb02732005-04-28 00:25:52 -07001206 status = acpi_bus_get_device(chandle, &child);
1207
Len Brown4be44fc2005-08-05 00:44:28 -04001208 if (ACPI_FAILURE(status))
1209 continue;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001210
1211 if (ops->acpi_op_start) {
1212 status = acpi_start_single_object(child);
1213 if (ACPI_FAILURE(status))
1214 continue;
1215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 /*
1218 * If the device is present, enabled, and functioning then
1219 * parse its scope (depth-first). Note that we need to
1220 * represent absent devices to facilitate PnP notifications
1221 * -- but only the subtree head (not all of its children,
1222 * which will be enumerated when the parent is inserted).
1223 *
1224 * TBD: Need notifications and other detection mechanisms
Len Brown4be44fc2005-08-05 00:44:28 -04001225 * in place before we can fully implement this.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 */
1227 if (child->status.present) {
1228 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1229 NULL, NULL);
1230 if (ACPI_SUCCESS(status)) {
1231 level++;
1232 phandle = chandle;
1233 chandle = NULL;
1234 parent = child;
1235 }
1236 }
1237 }
1238
1239 return_VALUE(0);
1240}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Rajesh Shah3fb02732005-04-28 00:25:52 -07001242int
Len Brown4be44fc2005-08-05 00:44:28 -04001243acpi_bus_add(struct acpi_device **child,
1244 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001245{
1246 int result;
1247 struct acpi_bus_ops ops;
1248
1249 ACPI_FUNCTION_TRACE("acpi_bus_add");
1250
1251 result = acpi_add_single_object(child, parent, handle, type);
1252 if (!result) {
1253 memset(&ops, 0, sizeof(ops));
1254 ops.acpi_op_add = 1;
1255 result = acpi_bus_scan(*child, &ops);
1256 }
1257 return_VALUE(result);
1258}
Len Brown4be44fc2005-08-05 00:44:28 -04001259
Rajesh Shah3fb02732005-04-28 00:25:52 -07001260EXPORT_SYMBOL(acpi_bus_add);
1261
Len Brown4be44fc2005-08-05 00:44:28 -04001262int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001263{
1264 int result;
1265 struct acpi_bus_ops ops;
1266
1267 ACPI_FUNCTION_TRACE("acpi_bus_start");
1268
1269 if (!device)
1270 return_VALUE(-EINVAL);
1271
1272 result = acpi_start_single_object(device);
1273 if (!result) {
1274 memset(&ops, 0, sizeof(ops));
1275 ops.acpi_op_start = 1;
1276 result = acpi_bus_scan(device, &ops);
1277 }
1278 return_VALUE(result);
1279}
Len Brown4be44fc2005-08-05 00:44:28 -04001280
Rajesh Shah3fb02732005-04-28 00:25:52 -07001281EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Kristen Accardiceaba662006-02-23 17:56:01 -08001283int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Len Brown4be44fc2005-08-05 00:44:28 -04001285 acpi_status status;
1286 struct acpi_device *parent, *child;
1287 acpi_handle phandle, chandle;
1288 acpi_object_type type;
1289 u32 level = 1;
1290 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Len Brown4be44fc2005-08-05 00:44:28 -04001292 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 phandle = start->handle;
1294 child = chandle = NULL;
1295
1296 while ((level > 0) && parent && (!err)) {
1297 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001298 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 /*
1301 * If this scope is exhausted then move our way back up.
1302 */
1303 if (ACPI_FAILURE(status)) {
1304 level--;
1305 chandle = phandle;
1306 acpi_get_parent(phandle, &phandle);
1307 child = parent;
1308 parent = parent->parent;
1309
1310 if (level == 0)
1311 err = acpi_bus_remove(child, rmdevice);
1312 else
1313 err = acpi_bus_remove(child, 1);
1314
1315 continue;
1316 }
1317
1318 status = acpi_get_type(chandle, &type);
1319 if (ACPI_FAILURE(status)) {
1320 continue;
1321 }
1322 /*
1323 * If there is a device corresponding to chandle then
1324 * parse it (depth-first).
1325 */
1326 if (acpi_bus_get_device(chandle, &child) == 0) {
1327 level++;
1328 phandle = chandle;
1329 chandle = NULL;
1330 parent = child;
1331 }
1332 continue;
1333 }
1334 return err;
1335}
Kristen Accardiceaba662006-02-23 17:56:01 -08001336EXPORT_SYMBOL_GPL(acpi_bus_trim);
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Len Brown4be44fc2005-08-05 00:44:28 -04001339static int acpi_bus_scan_fixed(struct acpi_device *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Len Brown4be44fc2005-08-05 00:44:28 -04001341 int result = 0;
1342 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 ACPI_FUNCTION_TRACE("acpi_bus_scan_fixed");
1345
1346 if (!root)
1347 return_VALUE(-ENODEV);
1348
1349 /*
1350 * Enumerate all fixed-feature devices.
1351 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001352 if (acpi_fadt.pwr_button == 0) {
1353 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001354 NULL,
1355 ACPI_BUS_TYPE_POWER_BUTTON);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001356 if (!result)
1357 result = acpi_start_single_object(device);
1358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Rajesh Shah3fb02732005-04-28 00:25:52 -07001360 if (acpi_fadt.sleep_button == 0) {
1361 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001362 NULL,
1363 ACPI_BUS_TYPE_SLEEP_BUTTON);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001364 if (!result)
1365 result = acpi_start_single_object(device);
1366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 return_VALUE(result);
1369}
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371static int __init acpi_scan_init(void)
1372{
1373 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001374 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 ACPI_FUNCTION_TRACE("acpi_scan_init");
1377
1378 if (acpi_disabled)
1379 return_VALUE(0);
1380
1381 kset_register(&acpi_namespace_kset);
1382
1383 /*
1384 * Create the root device in the bus's device tree
1385 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001386 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
Len Brown4be44fc2005-08-05 00:44:28 -04001387 ACPI_BUS_TYPE_SYSTEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 if (result)
1389 goto Done;
1390
Rajesh Shah3fb02732005-04-28 00:25:52 -07001391 result = acpi_start_single_object(acpi_root);
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 /*
1394 * Enumerate devices in the ACPI namespace.
1395 */
1396 result = acpi_bus_scan_fixed(acpi_root);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001397 if (!result) {
1398 memset(&ops, 0, sizeof(ops));
1399 ops.acpi_op_add = 1;
1400 ops.acpi_op_start = 1;
1401 result = acpi_bus_scan(acpi_root, &ops);
1402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 if (result)
1405 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1406
Len Brown4be44fc2005-08-05 00:44:28 -04001407 Done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return_VALUE(result);
1409}
1410
1411subsys_initcall(acpi_scan_init);